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