readgroup: Don't use results if error occurs
[openafs.git] / src / ptserver / readgroup.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14 #include <afs/opr.h>
15
16 #ifdef AFS_NT40_ENV
17 #include <WINNT/afsevent.h>
18 #endif
19 #include <ctype.h>
20
21 #include <rx/rx.h>
22 #include <rx/xdr.h>
23 #include <afs/cellconfig.h>
24 #include <afs/afsutil.h>
25 #include <afs/com_err.h>
26
27 #include "ptclient.h"
28 #include "ptuser.h"
29 #include "pterror.h"
30 #include "ptprototypes.h"
31
32 int verbose = 0;
33 static void skip(char **);
34
35 void
36 report_error(afs_int32 code, char *name, char *gname)
37 {
38     if (code == 0) {
39         if (verbose)
40             printf("  added %s to %s.\n", name, gname);
41     } else if (code == PRIDEXIST) {
42         if (verbose)
43             printf("  user %s already on group %s\n", name, gname);
44     } else {
45         fprintf(stderr, "Couldn't add %s to %s!\n", name, gname);
46         fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code), code);
47     }
48 }
49
50 int
51 osi_audit(void)
52 {
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.
56  */
57     return 0;
58 }
59
60 #include "AFS_component_version_number.c"
61
62 int
63 main(int argc, char **argv)
64 {
65     afs_int32 code;
66     char name[PR_MAXNAMELEN];
67     char gname[PR_MAXNAMELEN];
68     char owner[PR_MAXNAMELEN];
69     afs_int32 id;
70     char buf[3000];
71     FILE *fp;
72     char *ptr;
73     char *tmp;
74     char *cellname;
75     namelist lnames;
76     afs_int32 i;
77     afs_int32 fail = 0;
78
79     if (argc < 2) {
80         fprintf(stderr, "Usage: readgroup [-v] [-c cellname] groupfile.\n");
81         exit(0);
82     }
83     cellname = 0;
84     for (i = 1; i < argc; i++) {
85         if (!strcmp(argv[i], "-v"))
86             verbose = 1;
87         else {
88             if (!strcmp(argv[i], "-c")) {
89                 cellname = malloc(100);
90                 strncpy(cellname, argv[++i], 100);
91             } else
92                 strncpy(buf, argv[i], 150);
93         }
94     }
95     code = pr_Initialize(2, AFSDIR_CLIENT_ETC_DIRPATH, cellname);
96     free(cellname);
97     if (code) {
98         fprintf(stderr, "pr_Initialize failed .. exiting.\n");
99         fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code), code);
100         exit(1);
101     }
102
103     if ((fp = fopen(buf, "r")) == NULL) {
104         fprintf(stderr, "Couldn't open %s.\n", argv[1]);
105         exit(1);
106     }
107
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 */
110         if (buf[0] == '\n')
111             break;
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);
117             tmp = buf;
118             skip(&tmp);
119             skip(&tmp);
120             stolower(gname);
121             ptr = strchr(gname, ':');
122             strncpy(owner, gname, ptr - gname);
123             if (strcmp(owner, "system") == 0)
124                 strncpy(owner, "system:administrators", PR_MAXNAMELEN);
125             fail = 0;
126             if (verbose)
127                 printf("Group is %s, owner is %s, id is %d.\n", gname, owner,
128                        id);
129             code = pr_CreateGroup(gname, owner, &id);
130             if (code != 0) {
131                 if (code != PRIDEXIST) {        /* already exists */
132                     fprintf(stderr, "Failed to create group %s with id %d!\n",
133                             gname, id);
134                     fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code), code);
135                 }
136                 if (code != PREXIST && code != PRIDEXIST) {     /* we won't add users if it's not there */
137                     fail = 1;
138                 }
139             }
140             if (!fail) {
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);
148                     } else {
149                         /* add the members of a group to the group */
150                         if (verbose)
151                             printf("Adding %s to %s.\n", name, gname);
152                         code = pr_ListMembers(name, &lnames);
153                         if (code) {
154                             fprintf(stderr,
155                                     "Couldn't get the members for %s to add to %s.\n",
156                                     name, gname);
157                             fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code),
158                                     code);
159                         } else {
160                             for (i = 0; i < lnames.namelist_len; i++) {
161                                 code =
162                                     pr_AddToGroup(lnames.namelist_val[i], gname);
163                                 report_error(code, lnames.namelist_val[i], gname);
164                             }
165                             if (lnames.namelist_val)
166                                 free(lnames.namelist_val);
167                         }
168                     }
169                     memset(name, 0, PR_MAXNAMELEN);
170                     skip(&tmp);
171                 }
172             }
173         } else {                /* must have more names to add */
174             /* if we couldn't create the group, and it wasn't already there, don't try to add more users */
175             if (fail)
176                 continue;
177             /* read members out of buf and add to the group */
178             memset(name, 0, PR_MAXNAMELEN);
179             tmp = buf;
180             tmp++;
181             while (sscanf(tmp, "%s", name) != EOF) {
182                 if (strchr(name, ':') == NULL) {
183                     /* then it's not a group */
184                     code = pr_AddToGroup(name, gname);
185                     report_error(code, name, gname);
186                 } else {
187                     /* add the members of a group to the group */
188                     code = pr_ListMembers(name, &lnames);
189                     if (code) {
190                         fprintf(stderr,
191                                 "Couldn't get the members for %s to add to %s.\n",
192                                 name, gname);
193                         fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code),
194                                 code);
195                     } else {
196                         for (i = 0; i < lnames.namelist_len; i++) {
197                             if (verbose)
198                                 printf("Adding %s to %s.\n",
199                                        lnames.namelist_val[i], gname);
200                             code = pr_AddToGroup(lnames.namelist_val[i], gname);
201                             report_error(code, lnames.namelist_val[i], gname);
202                         }
203                         if (lnames.namelist_val)
204                             free(lnames.namelist_val);
205                     }
206                 }
207                 memset(name, 0, PR_MAXNAMELEN);
208                 skip(&tmp);
209             }
210         }
211     }
212     return 0;
213 }
214
215 static void
216 skip(char **s)
217 {
218     while (**s != ' ' && **s != '\t' && **s != '\0')
219         (*s)++;
220     while (**s == ' ' || **s == '\t')
221         (*s)++;
222 }