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