3 * Program to add users en batch to the moira database
5 * by Mark Rosenstein, July 1992.
7 * Copyright 1992 by the Massachusetts Institute of Technology.
9 * (c) Copyright 1992 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, please see the file
14 #include <mit-copyright.h>
21 #include <moira_site.h>
24 static char adduser_rcsid[] = "$Header$";
28 #define DEFAULT_SHELL "/bin/athena/tcsh"
30 #define DEFAULT_SHELL "/bin/csh"
33 /* flags from command line */
34 char *class, *comment, *status_str, *shell, *filename;
35 int reg_only, reg, verbose, nodupcheck;
37 /* argument parsing macro */
38 #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
41 int duplicate, errors;
43 extern char *FixCase(), *RemoveHyphens(), *strtrim();
45 int usercheck(), scream();
48 int main(int argc, char **argv)
51 char **arg = argv, *qargv[U_END];
52 char *motd, *p, *first, *middle, *last, *id, *login, *passwd, *server;
53 char buf[BUFSIZ], idbuf[32];
56 /* clear all flags & lists */
57 reg_only = reg = verbose = lineno = nodupcheck = errors = 0;
60 shell = DEFAULT_SHELL;
68 while (++arg - argv < argc)
72 if (argis("c", "class"))
74 if (arg - argv < argc - 1)
82 else if (argis("C", "comment"))
84 if (arg - argv < argc - 1)
92 else if (argis("s", "status"))
94 if (arg - argv < argc - 1)
102 else if (argis("h", "shell"))
104 if (arg - argv < argc - 1)
112 else if (argis("r", "reg_only"))
114 else if (argis("R", "register"))
116 else if (argis("f", "file"))
118 if (arg - argv < argc - 1)
126 else if (argis("v", "verbose"))
128 else if (argis("d", "nodupcheck"))
130 else if (argis("S", "server") || argis("db", "database"))
132 if (arg - argv < argc - 1)
147 if (!strcmp(filename, "-"))
151 input = fopen(filename, "r");
154 com_err(whoami, errno, "opening input file %s", filename);
162 if ((status = mr_connect(server)))
164 com_err(whoami, status, "unable to connect to the Moira server");
167 if ((status = mr_motd(&motd)))
169 com_err(whoami, status, "unable to check server status");
174 fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
180 if ((status = mr_auth("addusr")))
182 if (status == MR_USER_AUTH)
183 com_err(whoami, status, "");
186 com_err(whoami, status, "unable to authenticate to Moira");
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))
205 /* throw away terminating newline */
206 p = &buf[strlen(buf) - 1];
210 if (strlen(buf) == 0)
212 /* Last name is first thing on line */
214 /* First name follows a comma */
215 p = strchr(last, ',');
218 com_err(whoami, MR_BAD_CHAR, "Missing comma on line %d", lineno);
224 while (*p) /* find end-of-line */
232 com_err(whoami, 0, "Missing password on line %d", lineno);
236 passwd = strtrim(&p[1]);
238 /* previous field is login name */
245 com_err(whoami, 0, "Missing login on line %d", lineno);
249 login = strtrim(&p[1]);
254 /* ID is last thing on line */
263 com_err(whoami, 0, "Missing ID on line %d", lineno);
269 /* If something between first name & ID, it's middle name */
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]);
295 dargv[0] = qargv[U_FIRST];
296 dargv[1] = qargv[U_LAST];
298 status = mr_query("get_user_account_by_name", 2, dargv,
299 usercheck, (char *) qargv);
300 if (status && status != MR_NO_MATCH)
302 com_err(whoami, status,
303 "checking to see if user %s %s already exists",
305 com_err(whoami, 0, "NOT ADDING USER");
311 com_err(whoami, MR_EXISTS, "user %s %s already exists",
313 com_err(whoami, 0, "NOT ADDING USER");
317 else if (duplicate < 0)
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");
325 status = mr_query("add_user_account", U_SECURE + 1, qargv,
329 com_err(whoami, status, "adding user %s %s", first, last);
334 printf("Added user %s %s %s (%s)\n", qargv[U_FIRST],
335 qargv[U_MIDDLE], qargv[U_LAST], qargv[U_MITID]);
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);
345 com_err(whoami, status, "while registering (login) %s %s",
346 qargv[U_FIRST], qargv[U_LAST]);
352 printf("Registered user %s %s as %s\n", qargv[U_FIRST],
353 qargv[U_LAST], login);
355 status = set_password(qargv[U_FIRST], qargv[U_LAST],
356 qargv[U_MITID], idbuf, passwd);
359 com_err(whoami, status, "While registering (passwd) %s %s",
360 qargv[U_FIRST], qargv[U_LAST]);
364 printf("Set password for user %s\n", login);
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");
390 /* Called only if a query returns a value that we weren't expecting */
394 fprintf(stderr, "Programmer botch\n");
399 /* query callback routine to check for duplicate users */
401 usercheck(int argc, char **argv, char **qargv)
403 if (!strcmp(argv[U_MITID], qargv[U_MITID]))