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