2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
17 #include <WINNT/afsevent.h>
23 #include <afs/cellconfig.h>
24 #include <afs/afsutil.h>
25 #include <afs/com_err.h>
30 #include "ptprototypes.h"
33 static void skip(char **);
36 report_error(afs_int32 code, char *name, char *gname)
40 printf(" added %s to %s.\n", name, gname);
41 } else if (code == PRIDEXIST) {
43 printf(" user %s already on group %s\n", name, gname);
45 fprintf(stderr, "Couldn't add %s to %s!\n", name, gname);
46 fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code), code);
53 /* OK, this REALLY sucks bigtime, but I can't tell who is calling
54 * afsconf_CheckAuth easily, and only *SERVERS* should be calling osi_audit
55 * anyway. It's gonna give somebody fits to debug, I know, I know.
60 #include "AFS_component_version_number.c"
63 main(int argc, char **argv)
66 char name[PR_MAXNAMELEN];
67 char gname[PR_MAXNAMELEN];
68 char owner[PR_MAXNAMELEN];
80 fprintf(stderr, "Usage: readgroup [-v] [-c cellname] groupfile.\n");
84 for (i = 1; i < argc; i++) {
85 if (!strcmp(argv[i], "-v"))
88 if (!strcmp(argv[i], "-c")) {
89 cellname = malloc(100);
90 strncpy(cellname, argv[++i], 100);
92 strncpy(buf, argv[i], 150);
95 code = pr_Initialize(2, AFSDIR_CLIENT_ETC_DIRPATH, cellname);
98 fprintf(stderr, "pr_Initialize failed .. exiting.\n");
99 fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code), code);
103 if ((fp = fopen(buf, "r")) == NULL) {
104 fprintf(stderr, "Couldn't open %s.\n", argv[1]);
108 while ((tmp = fgets(buf, 3000, fp)) != NULL) {
109 /* group file lines must either have the name of a group or a tab or blank space at beginning */
112 if (buf[0] != ' ' && buf[0] != '\t') {
113 /* grab the group name */
114 memset(gname, 0, PR_MAXNAMELEN);
115 memset(owner, 0, PR_MAXNAMELEN);
116 sscanf(buf, "%s %d", gname, &id);
121 ptr = strchr(gname, ':');
122 strncpy(owner, gname, ptr - gname);
123 if (strcmp(owner, "system") == 0)
124 strncpy(owner, "system:administrators", PR_MAXNAMELEN);
127 printf("Group is %s, owner is %s, id is %d.\n", gname, owner,
129 code = pr_CreateGroup(gname, owner, &id);
131 if (code != PRIDEXIST) { /* already exists */
132 fprintf(stderr, "Failed to create group %s with id %d!\n",
134 fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code), code);
136 if (code != PREXIST && code != PRIDEXIST) { /* we won't add users if it's not there */
141 /* read members out of buf and add to the group */
142 memset(name, 0, PR_MAXNAMELEN);
143 while (sscanf(tmp, "%s", name) != EOF) {
144 if (strchr(name, ':') == NULL) {
145 /* then it's not a group */
146 code = pr_AddToGroup(name, gname);
147 report_error(code, name, gname);
149 /* add the members of a group to the group */
151 printf("Adding %s to %s.\n", name, gname);
152 code = pr_ListMembers(name, &lnames);
155 "Couldn't get the members for %s to add to %s.\n",
157 fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code),
160 for (i = 0; i < lnames.namelist_len; i++) {
162 pr_AddToGroup(lnames.namelist_val[i], gname);
163 report_error(code, lnames.namelist_val[i], gname);
165 if (lnames.namelist_val)
166 free(lnames.namelist_val);
168 memset(name, 0, PR_MAXNAMELEN);
172 } else { /* must have more names to add */
173 /* if we couldn't create the group, and it wasn't already there, don't try to add more users */
176 /* read members out of buf and add to the group */
177 memset(name, 0, PR_MAXNAMELEN);
180 while (sscanf(tmp, "%s", name) != EOF) {
181 if (strchr(name, ':') == NULL) {
182 /* then it's not a group */
183 code = pr_AddToGroup(name, gname);
184 report_error(code, name, gname);
186 /* add the members of a group to the group */
187 code = pr_ListMembers(name, &lnames);
190 "Couldn't get the members for %s to add to %s.\n",
192 fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code),
195 for (i = 0; i < lnames.namelist_len; i++) {
197 printf("Adding %s to %s.\n",
198 lnames.namelist_val[i], gname);
199 code = pr_AddToGroup(lnames.namelist_val[i], gname);
200 report_error(code, lnames.namelist_val[i], gname);
202 if (lnames.namelist_val)
203 free(lnames.namelist_val);
205 memset(name, 0, PR_MAXNAMELEN);
216 while (**s != ' ' && **s != '\t' && **s != '\0')
218 while (**s == ' ' || **s == '\t')