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