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