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