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$";
27 /* flags from command line */
28 char *class, *comment, *status_str, *shell, *filename;
29 int reg_only, reg, verbose, nodupcheck;
31 /* argument parsing macro */
32 #define argis(a,b) ((strcmp(*arg+1, a) == 0) || (strcmp(*arg+1, b) == 0))
35 int duplicate, errors;
38 extern char *FixCase(), *RemoveHyphens(), *strtrim();
40 int usercheck(), scream();
48 char **arg = argv, *qargv[U_END];
49 char *motd, *p, *first, *middle, *last, *id, *login, *passwd, *server;
50 char buf[BUFSIZ], idbuf[32];
53 /* clear all flags & lists */
54 reg_only = reg = verbose = lineno = nodupcheck = errors = 0;
65 while (++arg - argv < argc) {
68 if (argis("c", "class"))
69 if (arg - argv < argc - 1) {
74 else if (argis("C", "comment"))
75 if (arg - argv < argc - 1) {
80 else if (argis("s", "status"))
81 if (arg - argv < argc - 1) {
86 else if (argis("h", "shell"))
87 if (arg - argv < argc - 1) {
92 else if (argis("r", "reg_only"))
94 else if (argis("R", "register"))
96 else if (argis("f", "file"))
97 if (arg - argv < argc - 1) {
102 else if (argis("v","verbose"))
104 else if (argis("d","nodupcheck"))
106 else if (argis("S","server"))
107 if (arg - argv < argc - 1) {
118 if (!strcmp(filename, "-"))
121 input = fopen(filename, "r");
123 com_err(whoami, errno, "opening input file %s", filename);
130 if (status = mr_connect(server)) {
131 com_err(whoami, status, "unable to connect to the Moira server");
134 if ( status = mr_motd(&motd) ) {
135 com_err(whoami, status, "unable to check server status");
139 fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", motd);
144 if (status = mr_auth("addusr")) {
145 if (status == MR_USER_AUTH)
146 com_err(whoami, status, "");
148 com_err(whoami, status, "unable to authenticate to Moira");
157 qargv[U_NAME] = UNIQUE_LOGIN;
158 qargv[U_UID] = UNIQUE_UID;
159 qargv[U_SHELL] = shell;
160 qargv[U_STATE] = status_str;
161 qargv[U_CLASS] = class;
162 qargv[U_COMMENT] = comment;
163 qargv[U_SIGNATURE] = "";
164 qargv[U_SECURE] = "0";
165 while (fgets(buf, BUFSIZ, input)) {
166 /* throw away terminating newline */
167 p = &buf[strlen(buf) - 1];
171 if (strlen(buf) == 0)
173 /* Last name is first thing on line */
175 /* First name follows a comma */
176 p = index(last, ',');
178 com_err(whoami, MR_BAD_CHAR, "Missing comma on line %d", lineno);
184 while (*p) p++; /* find end-of-line */
185 if (reg_only || reg) {
186 while (!isspace(*p)) p--;
188 com_err(whoami, 0, "Missing password on line %d", lineno);
192 passwd = strtrim(&p[1]);
194 /* previous field is login name */
195 while (isspace(*p)) p--;
196 while (!isspace(*p)) p--;
198 com_err(whoami, 0, "Missing login on line %d", lineno);
202 login = strtrim(&p[1]);
205 /* ID is last thing on line */
208 while (isspace(*p)) p--;
209 while (!isspace(*p)) p--;
211 com_err(whoami, 0, "Missing ID on line %d", lineno);
217 /* If something between first name & ID, it's middle name */
218 while (isspace(*p)) p--;
219 while (!isspace(*p)) p--;
226 qargv[U_FIRST] = strtrim(first);
227 qargv[U_MIDDLE] = strtrim(middle);
228 qargv[U_LAST] = strtrim(last);
229 qargv[U_MITID] = strtrim(id);
230 FixCase(qargv[U_FIRST]);
231 FixCase(qargv[U_MIDDLE]);
232 FixCase(qargv[U_LAST]);
233 RemoveHyphens(qargv[U_MITID]);
238 dargv[0] = qargv[U_FIRST];
239 dargv[1] = qargv[U_LAST];
241 status = mr_query("get_user_account_by_name", 2, dargv,
242 usercheck, (char *) qargv);
243 if (status && status != MR_NO_MATCH) {
244 com_err(whoami, status,
245 "checking to see if user %s %s already exists",
247 com_err(whoami, 0, "NOT ADDING USER");
252 com_err(whoami, MR_EXISTS, "user %s %s already exists",
254 com_err(whoami, 0, "NOT ADDING USER");
257 } else if (duplicate < 0) {
258 com_err(whoami, MR_EXISTS,
259 "user %s %s already exists with different ID number",
261 com_err(whoami, 0, "ADDING user anyway");
264 status = mr_query("add_user_account", U_SECURE + 1, qargv,
267 com_err(whoami, status, "adding user %s %s", first, last);
270 printf("Added user %s %s %s (%s)\n", qargv[U_FIRST],
271 qargv[U_MIDDLE], qargv[U_LAST], qargv[U_MITID]);
273 if (reg || reg_only) {
274 EncryptID(idbuf, qargv[U_MITID], qargv[U_FIRST], qargv[U_LAST]);
275 status = grab_login(qargv[U_FIRST], qargv[U_LAST],
276 qargv[U_MITID], idbuf, login);
278 com_err(whoami, status, "While registering (login) %s %s",
279 qargv[U_FIRST], qargv[U_LAST]);
283 printf("Registered user %s %s as %s\n", qargv[U_FIRST],
284 qargv[U_LAST], login);
285 status = set_password(qargv[U_FIRST], qargv[U_LAST],
286 qargv[U_MITID], idbuf, passwd);
288 com_err(whoami, status, "While registering (passwd) %s %s",
289 qargv[U_FIRST], qargv[U_LAST]);
293 printf("Set password for user %s\n", login);
304 fprintf(stderr, "Usage: %s [options]\n",argv[0]);
305 fprintf(stderr, "Options are\n");
306 fprintf(stderr, " -f | -file filename (default STDIN)\n");
307 fprintf(stderr, " -c | -class class (default TEMP)\n");
308 fprintf(stderr, " -C | -comment \"comment\" (default \"\")\n");
309 fprintf(stderr, " -s | -status status (default 0)\n");
310 fprintf(stderr, " -h | -shell shell (default /bin/csh)\n");
311 fprintf(stderr, " -r | -reg_only\n");
312 fprintf(stderr, " -R | -register (and add to database)\n");
313 fprintf(stderr, " -v | -verbose\n");
314 fprintf(stderr, " -d | -nodupcheck (don't check for duplicates)\n");
315 fprintf(stderr, " -S | -server servername\n");
320 /* Called only if a query returns a value that we weren't expecting */
324 fprintf(stderr, "Programmer botch\n");
329 /* query callback routine to check for duplicate users */
331 usercheck(argc, argv, qargv)
336 if (!strcmp(argv[U_MITID], qargv[U_MITID]))