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;
37 extern char *FixCase(), *RemoveHyphens(), *strtrim();
39 int usercheck(), scream();
47 char **arg = argv, *qargv[U_END];
48 char *motd, *p, *first, *middle, *last, *id, *login, *passwd, *server;
49 char buf[BUFSIZ], idbuf[32];
52 /* clear all flags & lists */
53 reg_only = reg = verbose = lineno = nodupcheck = errors = 0;
64 while (++arg - argv < argc) {
67 if (argis("c", "class"))
68 if (arg - argv < argc - 1) {
73 else if (argis("C", "comment"))
74 if (arg - argv < argc - 1) {
79 else if (argis("s", "status"))
80 if (arg - argv < argc - 1) {
85 else if (argis("h", "shell"))
86 if (arg - argv < argc - 1) {
91 else if (argis("r", "reg_only"))
93 else if (argis("R", "register"))
95 else if (argis("f", "file"))
96 if (arg - argv < argc - 1) {
101 else if (argis("v","verbose"))
103 else if (argis("d","nodupcheck"))
105 else if (argis("S","server"))
106 if (arg - argv < argc - 1) {
117 if (!strcmp(filename, "-"))
120 input = fopen(filename, "r");
122 com_err(whoami, errno, "opening input file %s", filename);
129 if (status = mr_connect(server)) {
130 com_err(whoami, status, "unable to connect to the Moira server");
133 if ( status = mr_motd(&motd) ) {
134 com_err(whoami, status, "unable to check server status");
138 fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", motd);
143 if (status = mr_auth("addusr")) {
144 if (status == MR_USER_AUTH)
145 com_err(whoami, status, "");
147 com_err(whoami, status, "unable to authenticate to Moira");
156 qargv[U_NAME] = UNIQUE_LOGIN;
157 qargv[U_UID] = UNIQUE_UID;
158 qargv[U_SHELL] = shell;
159 qargv[U_STATE] = status_str;
160 qargv[U_CLASS] = class;
161 qargv[U_COMMENT] = comment;
162 qargv[U_SIGNATURE] = "";
163 qargv[U_SECURE] = "0";
164 while (fgets(buf, BUFSIZ, input)) {
165 /* throw away terminating newline */
166 p = &buf[strlen(buf) - 1];
170 if (strlen(buf) == 0)
172 /* Last name is first thing on line */
174 /* First name follows a comma */
175 p = strchr(last, ',');
177 com_err(whoami, MR_BAD_CHAR, "Missing comma on line %d", lineno);
183 while (*p) p++; /* find end-of-line */
184 if (reg_only || reg) {
185 while (!isspace(*p)) p--;
187 com_err(whoami, 0, "Missing password on line %d", lineno);
191 passwd = strtrim(&p[1]);
193 /* previous field is login name */
194 while (isspace(*p)) p--;
195 while (!isspace(*p)) p--;
197 com_err(whoami, 0, "Missing login on line %d", lineno);
201 login = strtrim(&p[1]);
204 /* ID is last thing on line */
207 while (isspace(*p)) p--;
208 while (!isspace(*p)) p--;
210 com_err(whoami, 0, "Missing ID on line %d", lineno);
216 /* If something between first name & ID, it's middle name */
217 while (isspace(*p)) p--;
218 while (!isspace(*p)) p--;
225 qargv[U_FIRST] = strtrim(first);
226 qargv[U_MIDDLE] = strtrim(middle);
227 qargv[U_LAST] = strtrim(last);
228 qargv[U_MITID] = strtrim(id);
229 FixCase(qargv[U_FIRST]);
230 FixCase(qargv[U_MIDDLE]);
231 FixCase(qargv[U_LAST]);
232 RemoveHyphens(qargv[U_MITID]);
237 dargv[0] = qargv[U_FIRST];
238 dargv[1] = qargv[U_LAST];
240 status = mr_query("get_user_account_by_name", 2, dargv,
241 usercheck, (char *) qargv);
242 if (status && status != MR_NO_MATCH) {
243 com_err(whoami, status,
244 "checking to see if user %s %s already exists",
246 com_err(whoami, 0, "NOT ADDING USER");
251 com_err(whoami, MR_EXISTS, "user %s %s already exists",
253 com_err(whoami, 0, "NOT ADDING USER");
256 } else if (duplicate < 0) {
257 com_err(whoami, MR_EXISTS,
258 "user %s %s already exists with different ID number",
259 qargv[U_FIRST], qargv[U_LAST]);
260 com_err(whoami, 0, "ADDING user anyway");
263 status = mr_query("add_user_account", U_SECURE + 1, qargv,
266 com_err(whoami, status, "adding user %s %s", first, last);
269 printf("Added user %s %s %s (%s)\n", qargv[U_FIRST],
270 qargv[U_MIDDLE], qargv[U_LAST], qargv[U_MITID]);
272 if (reg || reg_only) {
273 EncryptID(idbuf, qargv[U_MITID], qargv[U_FIRST], qargv[U_LAST]);
274 status = grab_login(qargv[U_FIRST], qargv[U_LAST],
275 qargv[U_MITID], idbuf, login);
277 com_err(whoami, status, "while registering (login) %s %s",
278 qargv[U_FIRST], qargv[U_LAST]);
282 printf("Registered user %s %s as %s\n", qargv[U_FIRST],
283 qargv[U_LAST], login);
284 status = set_password(qargv[U_FIRST], qargv[U_LAST],
285 qargv[U_MITID], idbuf, passwd);
287 com_err(whoami, status, "While registering (passwd) %s %s",
288 qargv[U_FIRST], qargv[U_LAST]);
292 printf("Set password for user %s\n", login);
303 fprintf(stderr, "Usage: %s [options]\n",argv[0]);
304 fprintf(stderr, "Options are\n");
305 fprintf(stderr, " -f | -file filename (default STDIN)\n");
306 fprintf(stderr, " -c | -class class (default TEMP)\n");
307 fprintf(stderr, " -C | -comment \"comment\" (default \"\")\n");
308 fprintf(stderr, " -s | -status status (default 0)\n");
309 fprintf(stderr, " -h | -shell shell (default /bin/csh)\n");
310 fprintf(stderr, " -r | -reg_only\n");
311 fprintf(stderr, " -R | -register (and add to database)\n");
312 fprintf(stderr, " -v | -verbose\n");
313 fprintf(stderr, " -d | -nodupcheck (don't check for duplicates)\n");
314 fprintf(stderr, " -S | -server servername\n");
319 /* Called only if a query returns a value that we weren't expecting */
323 fprintf(stderr, "Programmer botch\n");
328 /* query callback routine to check for duplicate users */
330 usercheck(argc, argv, qargv)
335 if (!strcmp(argv[U_MITID], qargv[U_MITID]))