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