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