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) == 0) || (strcmp(*arg+1, b) == 0))
41 int duplicate, errors;
43 extern char *FixCase(), *RemoveHyphens(), *strtrim();
45 int usercheck(), scream();
53 char **arg = argv, *qargv[U_END];
54 char *motd, *p, *first, *middle, *last, *id, *login, *passwd, *server;
55 char buf[BUFSIZ], idbuf[32];
58 /* clear all flags & lists */
59 reg_only = reg = verbose = lineno = nodupcheck = errors = 0;
62 shell = DEFAULT_SHELL;
70 while (++arg - argv < argc) {
73 if (argis("c", "class"))
74 if (arg - argv < argc - 1) {
79 else if (argis("C", "comment"))
80 if (arg - argv < argc - 1) {
85 else if (argis("s", "status"))
86 if (arg - argv < argc - 1) {
91 else if (argis("h", "shell"))
92 if (arg - argv < argc - 1) {
97 else if (argis("r", "reg_only"))
99 else if (argis("R", "register"))
101 else if (argis("f", "file"))
102 if (arg - argv < argc - 1) {
107 else if (argis("v","verbose"))
109 else if (argis("d","nodupcheck"))
111 else if (argis("S","server") || argis("db","database"))
112 if (arg - argv < argc - 1) {
123 if (!strcmp(filename, "-"))
126 input = fopen(filename, "r");
128 com_err(whoami, errno, "opening input file %s", filename);
135 if (status = mr_connect(server)) {
136 com_err(whoami, status, "unable to connect to the Moira server");
139 if ( status = mr_motd(&motd) ) {
140 com_err(whoami, status, "unable to check server status");
144 fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", motd);
149 if (status = mr_auth("addusr")) {
150 if (status == MR_USER_AUTH)
151 com_err(whoami, status, "");
153 com_err(whoami, status, "unable to authenticate to Moira");
162 qargv[U_NAME] = UNIQUE_LOGIN;
163 qargv[U_UID] = UNIQUE_UID;
164 qargv[U_SHELL] = shell;
165 qargv[U_STATE] = status_str;
166 qargv[U_CLASS] = class;
167 qargv[U_COMMENT] = comment;
168 qargv[U_SIGNATURE] = "";
169 qargv[U_SECURE] = "0";
170 while (fgets(buf, BUFSIZ, input)) {
171 /* throw away terminating newline */
172 p = &buf[strlen(buf) - 1];
176 if (strlen(buf) == 0)
178 /* Last name is first thing on line */
180 /* First name follows a comma */
181 p = strchr(last, ',');
183 com_err(whoami, MR_BAD_CHAR, "Missing comma on line %d", lineno);
189 while (*p) p++; /* find end-of-line */
190 if (reg_only || reg) {
191 while (!isspace(*p)) p--;
193 com_err(whoami, 0, "Missing password on line %d", lineno);
197 passwd = strtrim(&p[1]);
199 /* previous field is login name */
200 while (isspace(*p)) p--;
201 while (!isspace(*p)) p--;
203 com_err(whoami, 0, "Missing login on line %d", lineno);
207 login = strtrim(&p[1]);
210 /* ID is last thing on line */
213 while (isspace(*p)) p--;
214 while (!isspace(*p)) p--;
216 com_err(whoami, 0, "Missing ID on line %d", lineno);
222 /* If something between first name & ID, it's middle name */
223 while (isspace(*p)) p--;
224 while (!isspace(*p)) p--;
231 qargv[U_FIRST] = strtrim(first);
232 qargv[U_MIDDLE] = strtrim(middle);
233 qargv[U_LAST] = strtrim(last);
234 qargv[U_MITID] = strtrim(id);
235 FixCase(qargv[U_FIRST]);
236 FixCase(qargv[U_MIDDLE]);
237 FixCase(qargv[U_LAST]);
238 RemoveHyphens(qargv[U_MITID]);
243 dargv[0] = qargv[U_FIRST];
244 dargv[1] = qargv[U_LAST];
246 status = mr_query("get_user_account_by_name", 2, dargv,
247 usercheck, (char *) qargv);
248 if (status && status != MR_NO_MATCH) {
249 com_err(whoami, status,
250 "checking to see if user %s %s already exists",
252 com_err(whoami, 0, "NOT ADDING USER");
257 com_err(whoami, MR_EXISTS, "user %s %s already exists",
259 com_err(whoami, 0, "NOT ADDING USER");
262 } else if (duplicate < 0) {
263 com_err(whoami, MR_EXISTS,
264 "user %s %s already exists with different ID number",
265 qargv[U_FIRST], qargv[U_LAST]);
266 com_err(whoami, 0, "ADDING user anyway");
269 status = mr_query("add_user_account", U_SECURE + 1, qargv,
272 com_err(whoami, status, "adding user %s %s", first, last);
275 printf("Added user %s %s %s (%s)\n", qargv[U_FIRST],
276 qargv[U_MIDDLE], qargv[U_LAST], qargv[U_MITID]);
278 if (reg || reg_only) {
279 EncryptID(idbuf, qargv[U_MITID], qargv[U_FIRST], qargv[U_LAST]);
280 status = grab_login(qargv[U_FIRST], qargv[U_LAST],
281 qargv[U_MITID], idbuf, login);
283 com_err(whoami, status, "while registering (login) %s %s",
284 qargv[U_FIRST], qargv[U_LAST]);
288 printf("Registered user %s %s as %s\n", qargv[U_FIRST],
289 qargv[U_LAST], login);
290 status = set_password(qargv[U_FIRST], qargv[U_LAST],
291 qargv[U_MITID], idbuf, passwd);
293 com_err(whoami, status, "While registering (passwd) %s %s",
294 qargv[U_FIRST], qargv[U_LAST]);
298 printf("Set password for user %s\n", login);
309 fprintf(stderr, "Usage: %s [options]\n",argv[0]);
310 fprintf(stderr, "Options are\n");
311 fprintf(stderr, " -f | -file filename (default STDIN)\n");
312 fprintf(stderr, " -c | -class class (default TEMP)\n");
313 fprintf(stderr, " -C | -comment \"comment\" (default \"\")\n");
314 fprintf(stderr, " -s | -status status (default 0)\n");
315 fprintf(stderr, " -h | -shell shell (default %s)\n", DEFAULT_SHELL);
316 fprintf(stderr, " -r | -reg_only\n");
317 fprintf(stderr, " -R | -register (and add to database)\n");
318 fprintf(stderr, " -v | -verbose\n");
319 fprintf(stderr, " -d | -nodupcheck (don't check for duplicates)\n");
320 fprintf(stderr, " -S | -server servername\n");
325 /* Called only if a query returns a value that we weren't expecting */
329 fprintf(stderr, "Programmer botch\n");
334 /* query callback routine to check for duplicate users */
336 usercheck(argc, argv, qargv)
341 if (!strcmp(argv[U_MITID], qargv[U_MITID]))