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