]> andersk Git - moira.git/blob - clients/addusr/addusr.c
Fix bugs in registration code
[moira.git] / clients / addusr / addusr.c
1 /* $Id$
2  *
3  * Program to add users en masse to the moira database
4  *
5  * by Mark Rosenstein, July 1992.
6  *
7  * Copyright (C) 1992-1998 by the Massachusetts Institute of Technology.
8  * For copying and distribution information, please see the file
9  * <mit-copyright.h>.
10  */
11
12 #include <mit-copyright.h>
13 #include <moira.h>
14 #include <moira_site.h>
15
16 #include <ctype.h>
17 #include <errno.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 RCSID("$Header$");
22
23 #ifdef ATHENA
24 #define DEFAULT_SHELL "/bin/athena/tcsh"
25 #else
26 #define DEFAULT_SHELL "/bin/csh"
27 #endif
28
29 /* flags from command line */
30 char *class, *comment, *status_str, *shell, *filename;
31 int reg_only, reg, verbose, nodupcheck, securereg;
32
33 /* argument parsing macro */
34 #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
35
36 char *whoami;
37 int duplicate, errors;
38
39 void usage(char **argv);
40 int usercheck(int argc, char **argv, void *qargv);
41 int get_uid(int argc, char **argv, void *qargv);
42
43 int main(int argc, char **argv)
44 {
45   int status, lineno;
46   char **arg = argv, *qargv[U_END];
47   char *motd, *p, *first, *middle, *last, *id, *login, *server;
48   char buf[BUFSIZ], idbuf[32];
49   FILE *input;
50
51   /* clear all flags & lists */
52   reg_only = reg = verbose = lineno = nodupcheck = errors = securereg = 0;
53   server = NULL;
54   filename = "-";
55   shell = DEFAULT_SHELL;
56   class = "TEMP";
57   comment = "";
58   status_str = "0";
59
60   whoami = argv[0];
61
62   /* parse args */
63   while (++arg - argv < argc)
64     {
65       if  (**arg == '-')
66         {
67           if (argis("c", "class"))
68             {
69               if (arg - argv < argc - 1)
70                 {
71                   ++arg;
72                   class = *arg;
73                 }
74               else
75                 usage(argv);
76             }
77           else if (argis("C", "comment"))
78             {
79               if (arg - argv < argc - 1)
80                 {
81                   ++arg;
82                   comment = *arg;
83                 }
84               else
85                 usage(argv);
86             }
87           else if (argis("s", "status"))
88             {
89               if (arg - argv < argc - 1)
90                 {
91                   ++arg;
92                   status_str = *arg;
93                 }
94               else
95                 usage(argv);
96             }
97           else if (argis("h", "shell"))
98             {
99               if (arg - argv < argc - 1)
100                 {
101                   ++arg;
102                   shell = *arg;
103                 }
104               else
105                 usage(argv);
106             }
107           else if (argis("6", "secure"))
108             securereg++;
109           else if (argis("r", "reg_only"))
110             reg_only++;
111           else if (argis("R", "register"))
112             reg++;
113           else if (argis("f", "file"))
114             {
115               if (arg - argv < argc - 1)
116                 {
117                   ++arg;
118                   filename = *arg;
119                 }
120               else
121                 usage(argv);
122             }
123           else if (argis("v", "verbose"))
124             verbose++;
125           else if (argis("d", "nodupcheck"))
126             nodupcheck++;
127           else if (argis("S", "server") || argis("db", "database"))
128             {
129               if (arg - argv < argc - 1)
130                 {
131                   ++arg;
132                   server = *arg;
133                 }
134               else
135                 usage(argv);
136             }
137           else
138             usage(argv);
139         }
140       else
141         usage(argv);
142     }
143
144   if (!strcmp(filename, "-"))
145     input = stdin;
146   else
147     {
148       input = fopen(filename, "r");
149       if (!input)
150         {
151           com_err(whoami, errno, "opening input file %s", filename);
152           exit(2);
153         }
154     }
155
156   /* fire up Moira */
157   if ((status = mr_connect(server)))
158     {
159       com_err(whoami, status, "unable to connect to the Moira server");
160       exit(2);
161     }
162   if ((status = mr_motd(&motd)))
163     {
164       com_err(whoami, status, "unable to check server status");
165       exit(2);
166     }
167   if (motd)
168     {
169       fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
170               motd);
171       mr_disconnect();
172       exit(2);
173     }
174
175   if ((status = mr_auth("addusr")))
176     {
177       if (status == MR_USER_AUTH)
178         com_err(whoami, status, "");
179       else
180         {
181           com_err(whoami, status, "unable to authenticate to Moira");
182           exit(2);
183         }
184     }
185
186   qargv[U_NAME] = UNIQUE_LOGIN;
187   qargv[U_UID] = UNIQUE_UID;
188   qargv[U_SHELL] = shell;
189   qargv[U_STATE] = status_str;
190   qargv[U_CLASS] = class;
191   qargv[U_COMMENT] = comment;
192   qargv[U_SIGNATURE] = "";
193   qargv[U_SECURE] = securereg ? "1" : "0";
194   while (fgets(buf, BUFSIZ, input))
195     {
196       /* throw away terminating newline */
197       p = &buf[strlen(buf) - 1];
198       if (*p == '\n')
199         *p = '\0';
200       lineno++;
201       if (strlen(buf) == 0)
202         continue;
203       /* Last name is first thing on line */
204       last = buf;
205       /* First name follows a comma */
206       p = strchr(last, ',');
207       if (!p)
208         {
209           com_err(whoami, MR_BAD_CHAR, "Missing comma on line %d", lineno);
210           errors++;
211           continue;
212         }
213       *p++ = '\0';
214       first = p;
215       while (*p)                /* find end-of-line */
216         p++;
217       if (reg_only || reg)
218         {
219           while (!isspace(*p))
220             p--;
221           if (p <= first)
222             {
223               com_err(whoami, 0, "Missing login on line %d", lineno);
224               errors++;
225               continue;
226             }
227           login = strtrim(&p[1]);
228           *p-- = '\0';
229         }
230       else
231         {
232           /* ID is last thing on line */
233           p--;
234         }
235       while (isspace(*p))
236         p--;
237       while (!isspace(*p))
238         p--;
239       if (p <= first)
240         {
241           com_err(whoami, 0, "Missing ID on line %d", lineno);
242           errors++;
243           continue;
244         }
245       id = &p[1];
246       *p-- = '\0';
247       /* If something between first name & ID, it's middle name */
248       while (isspace(*p))
249         p--;
250       while (!isspace(*p))
251         p--;
252       if (p <= first)
253         middle = "";
254       else
255         {
256           middle = &p[1];
257           *p = '\0';
258         }
259       qargv[U_FIRST] = strtrim(first);
260       qargv[U_MIDDLE] = strtrim(middle);
261       qargv[U_LAST] = strtrim(last);
262       qargv[U_MITID] = strtrim(id);
263       FixCase(qargv[U_FIRST]);
264       FixCase(qargv[U_MIDDLE]);
265       FixCase(qargv[U_LAST]);
266       RemoveHyphens(qargv[U_MITID]);
267       if (!reg_only)
268         {
269           if (!nodupcheck)
270             {
271               char *dargv[2];
272
273               dargv[0] = qargv[U_FIRST];
274               dargv[1] = qargv[U_LAST];
275               duplicate = 0;
276               status = mr_query("get_user_account_by_name", 2, dargv,
277                                 usercheck, qargv);
278               if (status && status != MR_NO_MATCH)
279                 {
280                   com_err(whoami, status,
281                           "checking to see if user %s %s already exists",
282                           qargv[0], qargv[1]);
283                   com_err(whoami, 0, "NOT ADDING USER");
284                   errors++;
285                   continue;
286                 }
287               if (duplicate > 0)
288                 {
289                   com_err(whoami, MR_EXISTS, "user %s %s already exists",
290                           qargv[0], qargv[1]);
291                   com_err(whoami, 0, "NOT ADDING USER");
292                   errors++;
293                   continue;
294                 }
295               else if (duplicate < 0)
296                 {
297                   com_err(whoami, MR_EXISTS,
298                           "user %s %s already exists with different ID number",
299                           qargv[U_FIRST], qargv[U_LAST]);
300                   com_err(whoami, 0, "ADDING user anyway");
301                 }
302             }
303           status = mr_query("add_user_account", U_SECURE + 1, qargv,
304                             NULL, NULL);
305           if (status)
306             {
307               com_err(whoami, status, "adding user %s %s", first, last);
308               errors++;
309             }
310           else if (verbose)
311             {
312               printf("Added user %s %s %s (%s)\n", qargv[U_FIRST],
313                      qargv[U_MIDDLE], qargv[U_LAST], qargv[U_MITID]);
314             }
315         }
316       if (reg || reg_only)
317         {
318           char *gargv[2], *rargv[3], uid[10];
319
320           uid[0] = '\0';
321           gargv[0] = qargv[U_FIRST];
322           gargv[1] = qargv[U_LAST];
323           status = mr_query("get_user_account_by_name", 2, gargv,
324                             get_uid, &uid);
325           if (status)
326             {
327               com_err(whoami, status, "while looking up uid for %s %s",
328                       qargv[U_FIRST], qargv[U_LAST]);
329               errors++;
330               continue;
331             }
332
333           rargv[0] = uid;
334           rargv[1] = login;
335           rargv[2] = "0";
336
337           status = mr_query("register_user", 3, rargv, NULL, NULL);
338           if (status)
339             {
340               com_err(whoami, status, "while registering (login) %s %s",
341                       qargv[U_FIRST], qargv[U_LAST]);
342               errors++;
343               continue;
344             }
345           else if (verbose)
346             {
347               printf("Registered user %s %s as %s\n", qargv[U_FIRST],
348                      qargv[U_LAST], login);
349             }
350         }
351     }
352
353   exit(errors);
354 }
355
356
357 void usage(char **argv)
358 {
359   fprintf(stderr, "Usage: %s [options]\n", argv[0]);
360   fprintf(stderr, "Options are\n");
361   fprintf(stderr, "   -f | -file filename (default STDIN)\n");
362   fprintf(stderr, "   -c | -class class (default TEMP)\n");
363   fprintf(stderr, "   -C | -comment \"comment\" (default \"\")\n");
364   fprintf(stderr, "   -s | -status status (default 0)\n");
365   fprintf(stderr, "   -h | -shell shell (default %s)\n", DEFAULT_SHELL);
366   fprintf(stderr, "   -r | -reg_only\n");
367   fprintf(stderr, "   -R | -register (and add to database)\n");
368   fprintf(stderr, "   -v | -verbose\n");
369   fprintf(stderr, "   -d | -nodupcheck (don't check for duplicates)\n");
370   fprintf(stderr, "   -db | -database host:port\n");
371   exit(1);
372 }
373
374
375 /* query callback routine to check for duplicate users */
376
377 int usercheck(int argc, char **argv, void *qargv)
378 {
379   if (!strcmp(argv[U_MITID], ((char **)qargv)[U_MITID]))
380     duplicate++;
381   else
382     duplicate--;
383
384   return MR_CONT;
385 }
386
387 /* query callback to get uid of a just-added account */
388 int get_uid(int argc, char **argv, void *uidv)
389 {
390   char *uid = uidv;
391
392   if (uid[0] == '\0')
393     strcpy(uid, argv[U_UID]);
394   else
395     {
396       if (!strcmp(argv[U_MODWITH], "addusr"))
397         strcpy(uid, argv[U_UID]);
398     }
399
400   return MR_CONT;
401 }
This page took 0.236737 seconds and 5 git commands to generate.