]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id $ |
93d0b3bf | 2 | * |
7ac48069 | 3 | * Program to add users en masse to the moira database |
93d0b3bf | 4 | * |
5 | * by Mark Rosenstein, July 1992. | |
6 | * | |
7ac48069 | 7 | * Copyright (C) 1992-1998 by the Massachusetts Institute of Technology. |
93d0b3bf | 8 | * For copying and distribution information, please see the file |
9 | * <mit-copyright.h>. | |
10 | */ | |
11 | ||
12 | #include <mit-copyright.h> | |
93d0b3bf | 13 | #include <moira.h> |
14 | #include <moira_site.h> | |
15 | ||
7ac48069 | 16 | #include <ctype.h> |
17 | #include <errno.h> | |
18 | #include <stdio.h> | |
19 | #include <string.h> | |
20 | ||
21 | RCSID("$Header$"); | |
93d0b3bf | 22 | |
17490e42 | 23 | #ifdef ATHENA |
24 | #define DEFAULT_SHELL "/bin/athena/tcsh" | |
25 | #else | |
26 | #define DEFAULT_SHELL "/bin/csh" | |
27 | #endif | |
28 | ||
93d0b3bf | 29 | /* flags from command line */ |
30 | char *class, *comment, *status_str, *shell, *filename; | |
31 | int reg_only, reg, verbose, nodupcheck; | |
32 | ||
33 | /* argument parsing macro */ | |
5eaef520 | 34 | #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b)) |
93d0b3bf | 35 | |
36 | char *whoami; | |
37 | int duplicate, errors; | |
38 | ||
7ac48069 | 39 | extern int ureg_init(void); |
40 | extern int grab_login(char *first, char *last, char *idnumber, | |
41 | char *hashidnumber, char *login); | |
42 | extern int set_password(char *first, char *last, char *idnumber, | |
43 | char *hashidnumber, char *password); | |
44 | ||
93d0b3bf | 45 | |
7ac48069 | 46 | void usage(char **argv); |
47 | int usercheck(int argc, char **argv, void *qargv); | |
93d0b3bf | 48 | |
5eaef520 | 49 | int main(int argc, char **argv) |
93d0b3bf | 50 | { |
5eaef520 | 51 | int status, lineno; |
52 | char **arg = argv, *qargv[U_END]; | |
53 | char *motd, *p, *first, *middle, *last, *id, *login, *passwd, *server; | |
54 | char buf[BUFSIZ], idbuf[32]; | |
55 | FILE *input; | |
93d0b3bf | 56 | |
5eaef520 | 57 | /* clear all flags & lists */ |
58 | reg_only = reg = verbose = lineno = nodupcheck = errors = 0; | |
59 | server = NULL; | |
60 | filename = "-"; | |
61 | shell = DEFAULT_SHELL; | |
62 | class = "TEMP"; | |
63 | comment = ""; | |
64 | status_str = "0"; | |
93d0b3bf | 65 | |
5eaef520 | 66 | whoami = argv[0]; |
93d0b3bf | 67 | |
5eaef520 | 68 | /* parse args */ |
69 | while (++arg - argv < argc) | |
70 | { | |
71 | if (**arg == '-') | |
93d0b3bf | 72 | { |
5eaef520 | 73 | if (argis("c", "class")) |
74 | { | |
75 | if (arg - argv < argc - 1) | |
76 | { | |
77 | ++arg; | |
78 | class = *arg; | |
79 | } | |
80 | else | |
81 | usage(argv); | |
82 | } | |
83 | else if (argis("C", "comment")) | |
84 | { | |
85 | if (arg - argv < argc - 1) | |
86 | { | |
87 | ++arg; | |
88 | comment = *arg; | |
89 | } | |
90 | else | |
91 | usage(argv); | |
92 | } | |
93 | else if (argis("s", "status")) | |
94 | { | |
95 | if (arg - argv < argc - 1) | |
96 | { | |
97 | ++arg; | |
98 | status_str = *arg; | |
99 | } | |
100 | else | |
101 | usage(argv); | |
102 | } | |
103 | else if (argis("h", "shell")) | |
104 | { | |
105 | if (arg - argv < argc - 1) | |
106 | { | |
107 | ++arg; | |
108 | shell = *arg; | |
109 | } | |
110 | else | |
111 | usage(argv); | |
112 | } | |
113 | else if (argis("r", "reg_only")) | |
114 | reg_only++; | |
115 | else if (argis("R", "register")) | |
116 | reg++; | |
117 | else if (argis("f", "file")) | |
118 | { | |
119 | if (arg - argv < argc - 1) | |
120 | { | |
121 | ++arg; | |
122 | filename = *arg; | |
123 | } | |
124 | else | |
125 | usage(argv); | |
126 | } | |
127 | else if (argis("v", "verbose")) | |
128 | verbose++; | |
129 | else if (argis("d", "nodupcheck")) | |
130 | nodupcheck++; | |
131 | else if (argis("S", "server") || argis("db", "database")) | |
132 | { | |
133 | if (arg - argv < argc - 1) | |
134 | { | |
135 | ++arg; | |
136 | server = *arg; | |
137 | } | |
138 | else | |
93d0b3bf | 139 | usage(argv); |
5eaef520 | 140 | } |
141 | else | |
142 | usage(argv); | |
143 | } | |
144 | else | |
145 | usage(argv); | |
93d0b3bf | 146 | } |
147 | ||
5eaef520 | 148 | if (!strcmp(filename, "-")) |
149 | input = stdin; | |
150 | else | |
151 | { | |
152 | input = fopen(filename, "r"); | |
153 | if (!input) | |
154 | { | |
155 | com_err(whoami, errno, "opening input file %s", filename); | |
156 | exit(2); | |
93d0b3bf | 157 | } |
158 | } | |
159 | ||
5eaef520 | 160 | if (!reg_only) |
161 | { | |
162 | /* fire up Moira */ | |
163 | if ((status = mr_connect(server))) | |
164 | { | |
165 | com_err(whoami, status, "unable to connect to the Moira server"); | |
166 | exit(2); | |
93d0b3bf | 167 | } |
5eaef520 | 168 | if ((status = mr_motd(&motd))) |
169 | { | |
170 | com_err(whoami, status, "unable to check server status"); | |
171 | exit(2); | |
93d0b3bf | 172 | } |
5eaef520 | 173 | if (motd) |
174 | { | |
175 | fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", | |
176 | motd); | |
177 | mr_disconnect(); | |
178 | exit(2); | |
93d0b3bf | 179 | } |
180 | ||
5eaef520 | 181 | if ((status = mr_auth("addusr"))) |
182 | { | |
183 | if (status == MR_USER_AUTH) | |
184 | com_err(whoami, status, ""); | |
185 | else | |
186 | { | |
187 | com_err(whoami, status, "unable to authenticate to Moira"); | |
188 | exit(2); | |
93d0b3bf | 189 | } |
190 | } | |
191 | } | |
192 | ||
5eaef520 | 193 | if (reg || reg_only) |
194 | ureg_init(); | |
93d0b3bf | 195 | |
5eaef520 | 196 | qargv[U_NAME] = UNIQUE_LOGIN; |
197 | qargv[U_UID] = UNIQUE_UID; | |
198 | qargv[U_SHELL] = shell; | |
199 | qargv[U_STATE] = status_str; | |
200 | qargv[U_CLASS] = class; | |
201 | qargv[U_COMMENT] = comment; | |
202 | qargv[U_SIGNATURE] = ""; | |
203 | qargv[U_SECURE] = "0"; | |
204 | while (fgets(buf, BUFSIZ, input)) | |
205 | { | |
206 | /* throw away terminating newline */ | |
207 | p = &buf[strlen(buf) - 1]; | |
208 | if (*p == '\n') | |
209 | *p = '\0'; | |
210 | lineno++; | |
211 | if (strlen(buf) == 0) | |
212 | continue; | |
213 | /* Last name is first thing on line */ | |
214 | last = buf; | |
215 | /* First name follows a comma */ | |
216 | p = strchr(last, ','); | |
217 | if (!p) | |
218 | { | |
219 | com_err(whoami, MR_BAD_CHAR, "Missing comma on line %d", lineno); | |
220 | errors++; | |
93d0b3bf | 221 | continue; |
93d0b3bf | 222 | } |
5eaef520 | 223 | *p++ = '\0'; |
224 | first = p; | |
225 | while (*p) /* find end-of-line */ | |
226 | p++; | |
227 | if (reg_only || reg) | |
228 | { | |
229 | while (!isspace(*p)) | |
230 | p--; | |
231 | if (p <= first) | |
232 | { | |
233 | com_err(whoami, 0, "Missing password on line %d", lineno); | |
234 | errors++; | |
235 | continue; | |
93d0b3bf | 236 | } |
5eaef520 | 237 | passwd = strtrim(&p[1]); |
238 | *p-- = '\0'; | |
239 | /* previous field is login name */ | |
240 | while (isspace(*p)) | |
241 | p--; | |
242 | while (!isspace(*p)) | |
93d0b3bf | 243 | p--; |
5eaef520 | 244 | if (p <= first) |
245 | { | |
246 | com_err(whoami, 0, "Missing login on line %d", lineno); | |
247 | errors++; | |
248 | continue; | |
249 | } | |
250 | login = strtrim(&p[1]); | |
251 | *p-- = '\0'; | |
93d0b3bf | 252 | } |
5eaef520 | 253 | else |
254 | { | |
255 | /* ID is last thing on line */ | |
256 | p--; | |
93d0b3bf | 257 | } |
5eaef520 | 258 | while (isspace(*p)) |
259 | p--; | |
260 | while (!isspace(*p)) | |
261 | p--; | |
262 | if (p <= first) | |
263 | { | |
264 | com_err(whoami, 0, "Missing ID on line %d", lineno); | |
265 | errors++; | |
266 | continue; | |
93d0b3bf | 267 | } |
5eaef520 | 268 | id = &p[1]; |
269 | *p-- = '\0'; | |
270 | /* If something between first name & ID, it's middle name */ | |
271 | while (isspace(*p)) | |
272 | p--; | |
273 | while (!isspace(*p)) | |
274 | p--; | |
275 | if (p <= first) | |
276 | middle = ""; | |
277 | else | |
278 | { | |
279 | middle = &p[1]; | |
280 | *p = '\0'; | |
281 | } | |
282 | qargv[U_FIRST] = strtrim(first); | |
283 | qargv[U_MIDDLE] = strtrim(middle); | |
284 | qargv[U_LAST] = strtrim(last); | |
285 | qargv[U_MITID] = strtrim(id); | |
286 | FixCase(qargv[U_FIRST]); | |
287 | FixCase(qargv[U_MIDDLE]); | |
288 | FixCase(qargv[U_LAST]); | |
289 | RemoveHyphens(qargv[U_MITID]); | |
290 | if (!reg_only) | |
291 | { | |
292 | if (!nodupcheck) | |
293 | { | |
294 | char *dargv[2]; | |
93d0b3bf | 295 | |
5eaef520 | 296 | dargv[0] = qargv[U_FIRST]; |
297 | dargv[1] = qargv[U_LAST]; | |
298 | duplicate = 0; | |
299 | status = mr_query("get_user_account_by_name", 2, dargv, | |
7ac48069 | 300 | usercheck, qargv); |
5eaef520 | 301 | if (status && status != MR_NO_MATCH) |
302 | { | |
303 | com_err(whoami, status, | |
304 | "checking to see if user %s %s already exists", | |
305 | qargv[0], qargv[1]); | |
306 | com_err(whoami, 0, "NOT ADDING USER"); | |
307 | errors++; | |
308 | continue; | |
309 | } | |
310 | if (duplicate > 0) | |
311 | { | |
312 | com_err(whoami, MR_EXISTS, "user %s %s already exists", | |
313 | qargv[0], qargv[1]); | |
314 | com_err(whoami, 0, "NOT ADDING USER"); | |
315 | errors++; | |
316 | continue; | |
93d0b3bf | 317 | } |
5eaef520 | 318 | else if (duplicate < 0) |
319 | { | |
320 | com_err(whoami, MR_EXISTS, | |
321 | "user %s %s already exists with different ID number", | |
322 | qargv[U_FIRST], qargv[U_LAST]); | |
323 | com_err(whoami, 0, "ADDING user anyway"); | |
93d0b3bf | 324 | } |
325 | } | |
5eaef520 | 326 | status = mr_query("add_user_account", U_SECURE + 1, qargv, |
7ac48069 | 327 | NULL, NULL); |
5eaef520 | 328 | if (status) |
329 | { | |
330 | com_err(whoami, status, "adding user %s %s", first, last); | |
331 | errors++; | |
332 | } | |
333 | else if (verbose) | |
334 | { | |
93d0b3bf | 335 | printf("Added user %s %s %s (%s)\n", qargv[U_FIRST], |
336 | qargv[U_MIDDLE], qargv[U_LAST], qargv[U_MITID]); | |
5eaef520 | 337 | } |
93d0b3bf | 338 | } |
5eaef520 | 339 | if (reg || reg_only) |
340 | { | |
341 | EncryptID(idbuf, qargv[U_MITID], qargv[U_FIRST], qargv[U_LAST]); | |
342 | status = grab_login(qargv[U_FIRST], qargv[U_LAST], | |
343 | qargv[U_MITID], idbuf, login); | |
344 | if (status) | |
345 | { | |
346 | com_err(whoami, status, "while registering (login) %s %s", | |
347 | qargv[U_FIRST], qargv[U_LAST]); | |
348 | errors++; | |
349 | continue; | |
350 | } | |
351 | else if (verbose) | |
352 | { | |
93d0b3bf | 353 | printf("Registered user %s %s as %s\n", qargv[U_FIRST], |
354 | qargv[U_LAST], login); | |
5eaef520 | 355 | } |
356 | status = set_password(qargv[U_FIRST], qargv[U_LAST], | |
357 | qargv[U_MITID], idbuf, passwd); | |
358 | if (status) | |
359 | { | |
360 | com_err(whoami, status, "While registering (passwd) %s %s", | |
361 | qargv[U_FIRST], qargv[U_LAST]); | |
362 | errors++; | |
363 | continue; | |
93d0b3bf | 364 | } else if (verbose) |
365 | printf("Set password for user %s\n", login); | |
366 | } | |
367 | } | |
368 | ||
5eaef520 | 369 | exit(errors); |
93d0b3bf | 370 | } |
371 | ||
372 | ||
7ac48069 | 373 | void usage(char **argv) |
93d0b3bf | 374 | { |
5eaef520 | 375 | fprintf(stderr, "Usage: %s [options]\n", argv[0]); |
376 | fprintf(stderr, "Options are\n"); | |
377 | fprintf(stderr, " -f | -file filename (default STDIN)\n"); | |
378 | fprintf(stderr, " -c | -class class (default TEMP)\n"); | |
379 | fprintf(stderr, " -C | -comment \"comment\" (default \"\")\n"); | |
380 | fprintf(stderr, " -s | -status status (default 0)\n"); | |
381 | fprintf(stderr, " -h | -shell shell (default %s)\n", DEFAULT_SHELL); | |
382 | fprintf(stderr, " -r | -reg_only\n"); | |
383 | fprintf(stderr, " -R | -register (and add to database)\n"); | |
384 | fprintf(stderr, " -v | -verbose\n"); | |
385 | fprintf(stderr, " -d | -nodupcheck (don't check for duplicates)\n"); | |
386 | fprintf(stderr, " -db | -database host:port\n"); | |
387 | exit(1); | |
93d0b3bf | 388 | } |
389 | ||
390 | ||
93d0b3bf | 391 | /* query callback routine to check for duplicate users */ |
392 | ||
7ac48069 | 393 | int usercheck(int argc, char **argv, void *qargv) |
93d0b3bf | 394 | { |
7ac48069 | 395 | if (!strcmp(argv[U_MITID], ((char **)qargv)[U_MITID])) |
5eaef520 | 396 | duplicate++; |
397 | else | |
398 | duplicate--; | |
7ac48069 | 399 | |
400 | return MR_CONT; | |
93d0b3bf | 401 | } |