assorted-warning-cleanup-20071126
[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 RCSID
14     ("$Header$");
15
16 #include <stdio.h>
17 #ifdef AFS_NT40_ENV
18 #include <WINNT/afsevent.h>
19 #endif
20 #include <ctype.h>
21 #include <string.h>
22 #include <rx/rx.h>
23 #include <rx/xdr.h>
24 #include <afs/cellconfig.h>
25 #include <afs/afsutil.h>
26 #include <afs/com_err.h>
27 #include "ptclient.h"
28 #include "pterror.h"
29
30 int verbose = 0;
31 void skip();
32
33 void
34 report_error(afs_int32 code, char *name, char *gname)
35 {
36     if (code == 0) {
37         if (verbose)
38             printf("  added %s to %s.\n", name, gname);
39     } else if (code == PRIDEXIST) {
40         if (verbose)
41             printf("  user %s already on group %s\n", name, gname);
42     } else {
43         fprintf(stderr, "Couldn't add %s to %s!\n", name, gname);
44         fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code), code);
45     }
46 }
47
48 int
49 osi_audit()
50 {
51 /* OK, this REALLY sucks bigtime, but I can't tell who is calling
52  * afsconf_CheckAuth easily, and only *SERVERS* should be calling osi_audit
53  * anyway.  It's gonna give somebody fits to debug, I know, I know.
54  */
55     return 0;
56 }
57
58 #include "AFS_component_version_number.c"
59
60 int
61 main(int argc, char **argv)
62 {
63     register afs_int32 code;
64     char name[PR_MAXNAMELEN];
65     char gname[PR_MAXNAMELEN];
66     char owner[PR_MAXNAMELEN];
67     afs_int32 id;
68     char buf[3000];
69     FILE *fp;
70     char *ptr;
71     char *tmp;
72     char *cellname;
73     namelist lnames;
74     afs_int32 i;
75     afs_int32 fail = 0;
76
77     if (argc < 2) {
78         fprintf(stderr, "Usage: readgroup [-v] [-c cellname] groupfile.\n");
79         exit(0);
80     }
81     cellname = 0;
82     for (i = 1; i < argc; i++) {
83         if (!strcmp(argv[i], "-v"))
84             verbose = 1;
85         else {
86             if (!strcmp(argv[i], "-c")) {
87                 cellname = (char *)malloc(100);
88                 strncpy(cellname, argv[++i], 100);
89             } else
90                 strncpy(buf, argv[i], 150);
91         }
92     }
93     code = pr_Initialize(2, AFSDIR_CLIENT_ETC_DIRPATH, cellname);
94     free(cellname);
95     if (code) {
96         fprintf(stderr, "pr_Initialize failed .. exiting.\n");
97         fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code), code);
98         exit(1);
99     }
100
101     if ((fp = fopen(buf, "r")) == NULL) {
102         fprintf(stderr, "Couldn't open %s.\n", argv[1]);
103         exit(1);
104     }
105
106     while ((tmp = fgets(buf, 3000, fp)) != NULL) {
107         /* group file lines must either have the name of a group or a tab or blank space at beginning */
108         if (buf[0] == '\n')
109             break;
110         if (buf[0] != ' ' && buf[0] != '\t') {
111             /* grab the group name */
112             memset(gname, 0, PR_MAXNAMELEN);
113             memset(owner, 0, PR_MAXNAMELEN);
114             sscanf(buf, "%s %d", gname, &id);
115             tmp = buf;
116             skip(&tmp);
117             skip(&tmp);
118             stolower(gname);
119             ptr = strchr(gname, ':');
120             strncpy(owner, gname, ptr - gname);
121             if (strcmp(owner, "system") == 0)
122                 strncpy(owner, "system:administrators", PR_MAXNAMELEN);
123             fail = 0;
124             if (verbose)
125                 printf("Group is %s, owner is %s, id is %d.\n", gname, owner,
126                        id);
127             code = pr_CreateGroup(gname, owner, &id);
128             if (code != 0) {
129                 if (code != PRIDEXIST) {        /* already exists */
130                     fprintf(stderr, "Failed to create group %s with id %d!\n",
131                             gname, id);
132                     fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code), code);
133                 }
134                 if (code != PREXIST && code != PRIDEXIST) {     /* we won't add users if it's not there */
135                     fail = 1;
136                 }
137             }
138             if (!fail) {
139                 /* read members out of buf and add to the group */
140                 memset(name, 0, PR_MAXNAMELEN);
141                 while (sscanf(tmp, "%s", name) != EOF) {
142                     if (strchr(name, ':') == NULL) {
143                         /* then it's not a group */
144                         code = pr_AddToGroup(name, gname);
145                         report_error(code, name, gname);
146                     } else {
147                         /* add the members of a group to the group */
148                         if (verbose)
149                             printf("Adding %s to %s.\n",
150                                    lnames.namelist_val[i], gname);
151                         code = pr_ListMembers(name, &lnames);
152                         if (code) {
153                             fprintf(stderr,
154                                     "Couldn't get the members for %s to add to %s.\n",
155                                     name, gname);
156                             fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code),
157                                     code);
158                         }
159                         for (i = 0; i < lnames.namelist_len; i++) {
160                             code =
161                                 pr_AddToGroup(lnames.namelist_val[i], gname);
162                             report_error(code, lnames.namelist_val[i], gname);
163                         }
164                         if (lnames.namelist_val)
165                             free(lnames.namelist_val);
166                     }
167                     memset(name, 0, PR_MAXNAMELEN);
168                     skip(&tmp);
169                 }
170             }
171         } else {                /* must have more names to add */
172             /* if we couldn't create the group, and it wasn't already there, don't try to add more users */
173             if (fail)
174                 continue;
175             /* read members out of buf and add to the group */
176             memset(name, 0, PR_MAXNAMELEN);
177             tmp = buf;
178             tmp++;
179             while (sscanf(tmp, "%s", name) != EOF) {
180                 if (strchr(name, ':') == NULL) {
181                     /* then it's not a group */
182                     code = pr_AddToGroup(name, gname);
183                     report_error(code, name, gname);
184                 } else {
185                     /* add the members of a group to the group */
186                     code = pr_ListMembers(name, &lnames);
187                     if (code) {
188                         fprintf(stderr,
189                                 "Couldn't get the members for %s to add to %s.\n",
190                                 name, gname);
191                         fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code),
192                                 code);
193                     }
194                     for (i = 0; i < lnames.namelist_len; i++) {
195                         if (verbose)
196                             printf("Adding %s to %s.\n",
197                                    lnames.namelist_val[i], gname);
198                         code = pr_AddToGroup(lnames.namelist_val[i], gname);
199                         report_error(code, lnames.namelist_val[i], gname);
200                     }
201                     if (lnames.namelist_val)
202                         free(lnames.namelist_val);
203                 }
204                 memset(name, 0, PR_MAXNAMELEN);
205                 skip(&tmp);
206             }
207         }
208     }
209     return 0;
210 }
211
212 void
213 skip(char **s)
214 {
215     while (**s != ' ' && **s != '\t' && **s != '\0')
216         (*s)++;
217     while (**s == ' ' || **s == '\t')
218         (*s)++;
219 }