]> andersk Git - moira.git/blob - clients/addusr/addusr.c
Add -w flag, for setting a user's Windows shell.
[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;
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 = 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("S", "server") || argis("db", "database"))
142             {
143               if (arg - argv < argc - 1)
144                 {
145                   ++arg;
146                   server = *arg;
147                 }
148               else
149                 usage(argv);
150             }
151           else
152             usage(argv);
153         }
154       else
155         usage(argv);
156     }
157
158   if (!strcmp(filename, "-"))
159     input = stdin;
160   else
161     {
162       input = fopen(filename, "r");
163       if (!input)
164         {
165           com_err(whoami, errno, "opening input file %s", filename);
166           exit(2);
167         }
168     }
169
170   /* fire up Moira */
171   if (mrcl_connect(server, "addusr", 3, 1) != MRCL_SUCCESS)
172     exit(2);
173
174   qargv[U_NAME] = UNIQUE_LOGIN;
175   qargv[U_UID] = UNIQUE_UID;
176   qargv[U_SHELL] = shell;
177   qargv[U_WINCONSOLESHELL] = winconsoleshell;
178   qargv[U_STATE] = status_str;
179   qargv[U_CLASS] = class;
180   qargv[U_COMMENT] = comment;
181   qargv[U_SIGNATURE] = "";
182   qargv[U_SECURE] = securereg ? "1" : "0";
183   while (fgets(buf, BUFSIZ, input))
184     {
185       /* throw away terminating newline */
186       p = &buf[strlen(buf) - 1];
187       if (*p == '\n')
188         *p = '\0';
189       lineno++;
190       if (strlen(buf) == 0)
191         continue;
192       /* Last name is first thing on line */
193       last = buf;
194       /* First name follows a comma */
195       p = strchr(last, ',');
196       if (!p)
197         {
198           com_err(whoami, MR_BAD_CHAR, "Missing comma on line %d", lineno);
199           errors++;
200           continue;
201         }
202       *p++ = '\0';
203       first = p;
204       while (*p)                /* find end-of-line */
205         p++;
206       if (reg_only || reg)
207         {
208           while (!isspace(*p))
209             p--;
210           if (p <= first)
211             {
212               com_err(whoami, 0, "Missing login on line %d", lineno);
213               errors++;
214               continue;
215             }
216           login = strtrim(&p[1]);
217           *p-- = '\0';
218         }
219       else
220         {
221           /* ID is last thing on line */
222           p--;
223         }
224       while (isspace(*p))
225         p--;
226       while (!isspace(*p))
227         p--;
228       if (p <= first)
229         {
230           com_err(whoami, 0, "Missing ID on line %d", lineno);
231           errors++;
232           continue;
233         }
234       id = &p[1];
235       *p-- = '\0';
236       /* If something between first name & ID, it's middle name */
237       while (isspace(*p))
238         p--;
239       while (!isspace(*p))
240         p--;
241       if (p <= first)
242         middle = "";
243       else
244         {
245           middle = &p[1];
246           *p = '\0';
247         }
248       qargv[U_FIRST] = strtrim(first);
249       qargv[U_MIDDLE] = strtrim(middle);
250       qargv[U_LAST] = strtrim(last);
251       qargv[U_MITID] = strtrim(id);
252       FixCase(qargv[U_FIRST]);
253       FixCase(qargv[U_MIDDLE]);
254       FixCase(qargv[U_LAST]);
255       RemoveHyphens(qargv[U_MITID]);
256       if (!reg_only)
257         {
258           if (!nodupcheck)
259             {
260               char *dargv[2];
261
262               dargv[0] = qargv[U_FIRST];
263               dargv[1] = qargv[U_LAST];
264               duplicate = 0;
265               status = mr_query("get_user_account_by_name", 2, dargv,
266                                 usercheck, qargv);
267               if (status && status != MR_NO_MATCH)
268                 {
269                   com_err(whoami, status,
270                           "checking to see if user %s %s already exists",
271                           qargv[0], qargv[1]);
272                   com_err(whoami, 0, "NOT ADDING USER");
273                   errors++;
274                   continue;
275                 }
276               if (duplicate > 0)
277                 {
278                   com_err(whoami, MR_EXISTS, "user %s %s already exists",
279                           qargv[0], qargv[1]);
280                   com_err(whoami, 0, "NOT ADDING USER");
281                   errors++;
282                   continue;
283                 }
284               else if (duplicate < 0)
285                 {
286                   com_err(whoami, MR_EXISTS,
287                           "user %s %s already exists with different ID number",
288                           qargv[U_FIRST], qargv[U_LAST]);
289                   com_err(whoami, 0, "ADDING user anyway");
290                 }
291             }
292           status = mr_query("add_user_account", U_SECURE + 1, qargv,
293                             NULL, NULL);
294           if (status)
295             {
296               com_err(whoami, status, "adding user %s %s", first, last);
297               errors++;
298             }
299           else if (verbose)
300             {
301               printf("Added user %s %s %s (%s)\n", qargv[U_FIRST],
302                      qargv[U_MIDDLE], qargv[U_LAST], qargv[U_MITID]);
303             }
304         }
305       if (reg || reg_only)
306         {
307           char *gargv[2], *rargv[3], uid[10];
308
309           uid[0] = '\0';
310           gargv[0] = qargv[U_FIRST];
311           gargv[1] = qargv[U_LAST];
312           status = mr_query("get_user_account_by_name", 2, gargv,
313                             get_uid, &uid);
314           if (status)
315             {
316               com_err(whoami, status, "while looking up uid for %s %s",
317                       qargv[U_FIRST], qargv[U_LAST]);
318               errors++;
319               continue;
320             }
321
322           rargv[0] = uid;
323           rargv[1] = login;
324           rargv[2] = "IMAP";
325
326           status = mr_query("register_user", 3, rargv, NULL, NULL);
327           if (status)
328             {
329               com_err(whoami, status, "while registering (login) %s %s",
330                       qargv[U_FIRST], qargv[U_LAST]);
331               errors++;
332               continue;
333             }
334           else if (verbose)
335             {
336               printf("Registered user %s %s as %s\n", qargv[U_FIRST],
337                      qargv[U_LAST], login);
338             }
339         }
340     }
341
342   exit(errors);
343 }
344
345
346 void usage(char **argv)
347 {
348   fprintf(stderr, "Usage: %s [options]\n", argv[0]);
349   fprintf(stderr, "Options are\n");
350   fprintf(stderr, "   -f | -file filename (default STDIN)\n");
351   fprintf(stderr, "   -c | -class class (default TEMP)\n");
352   fprintf(stderr, "   -C | -comment \"comment\" (default \"\")\n");
353   fprintf(stderr, "   -s | -status status (default 0)\n");
354   fprintf(stderr, "   -h | -shell shell (default %s)\n", DEFAULT_SHELL);
355   fprintf(stderr, "   -w | -winshell windows console shell (default %s)\n",
356           DEFAULT_WINCONSOLESHELL);
357   fprintf(stderr, "   -r | -reg_only\n");
358   fprintf(stderr, "   -R | -register (and add to database)\n");
359   fprintf(stderr, "   -v | -verbose\n");
360   fprintf(stderr, "   -d | -nodupcheck (don't check for duplicates)\n");
361   fprintf(stderr, "   -db | -database host:port\n");
362   exit(1);
363 }
364
365
366 /* query callback routine to check for duplicate users */
367
368 int usercheck(int argc, char **argv, void *qargv)
369 {
370   if (!strcmp(argv[U_MITID], ((char **)qargv)[U_MITID]))
371     duplicate++;
372   else
373     duplicate--;
374
375   return MR_CONT;
376 }
377
378 /* query callback to get uid of a just-added account */
379 int get_uid(int argc, char **argv, void *uidv)
380 {
381   char *uid = uidv;
382
383   if (uid[0] == '\0')
384     strcpy(uid, argv[U_UID]);
385   else
386     {
387       if (!strcmp(argv[U_MODWITH], "addusr"))
388         strcpy(uid, argv[U_UID]);
389     }
390
391   return MR_CONT;
392 }
This page took 0.072195 seconds and 5 git commands to generate.