]>
Commit | Line | Data |
---|---|---|
5d429b61 | 1 | /* |
2 | * Command line oriented Moira users tool. | |
3 | * | |
4 | * zacheiss@mit.edu, September 2001 | |
5 | * | |
6 | * Inspired by blanche | |
7 | * | |
8 | * Copyright (C) 2000, 2001 by the Massachusetts Institute of Technology. | |
9 | * For copying and distribution information, please see the file | |
10 | * <mit-copyright.h>. | |
11 | */ | |
12 | ||
13 | #include <mit-copyright.h> | |
14 | #include <moira.h> | |
15 | #include <moira_site.h> | |
16 | #include <mrclient.h> | |
17 | ||
18 | #include <ctype.h> | |
19 | #include <errno.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | ||
24 | RCSID("$Header$"); | |
25 | ||
7902e669 | 26 | struct owner_type { |
27 | int type; | |
28 | char *name; | |
29 | }; | |
30 | ||
5d429b61 | 31 | struct string_list { |
32 | char *string; | |
33 | struct string_list *next; | |
34 | }; | |
35 | ||
7902e669 | 36 | #define M_ANY 0 |
37 | #define M_USER 1 | |
38 | #define M_LIST 2 | |
39 | #define M_KERBEROS 3 | |
40 | #define M_NONE 4 | |
41 | ||
5d429b61 | 42 | /* argument parsing macro */ |
43 | #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b)) | |
44 | ||
45 | /* flags from command line */ | |
46 | int info_flag, update_flag, create_flag, deact_flag, reg_flag; | |
47 | int list_res_flag, update_res_flag, unformatted_flag, verbose, noauth; | |
48 | ||
7902e669 | 49 | struct owner_type *sponsor; |
5d429b61 | 50 | struct string_list *reservation_add_queue, *reservation_remove_queue; |
51 | ||
52 | char *username, *whoami; | |
53 | ||
54 | char *newlogin, *uid, *shell, *winshell, *last, *first, *middle, *u_status; | |
aec7d432 | 55 | char *clearid, *class, *comment, *secure, *winhomedir, *winprofiledir; |
5d429b61 | 56 | |
7902e669 | 57 | struct owner_type *parse_member(char *s); |
58 | ||
5d429b61 | 59 | static char *states[] = { |
60 | "Registerable (0)", | |
61 | "Active (1)", | |
62 | "Half Registered (2)", | |
63 | "Deleted (3)", | |
64 | "Not registerable (4)", | |
65 | "Enrolled/Registerable (5)", | |
66 | "Enrolled/Not Registerable (6)", | |
2860a176 | 67 | "Half Enrolled (7)", |
68 | "Registerable, Kerberos only (8)", | |
69 | "Active, Kerberos only (9)" | |
5d429b61 | 70 | }; |
71 | ||
72 | static char *UserState(int state) | |
73 | { | |
74 | static char buf[BUFSIZ]; | |
75 | ||
76 | if (state < 0 || state >= US_END) | |
77 | { | |
78 | sprintf(buf, "Unknown (%d)", state); | |
79 | return buf; | |
80 | } | |
81 | return states[state]; | |
82 | } | |
83 | ||
84 | void usage(char **argv); | |
85 | int save_query_info(int argc, char **argv, void *hint); | |
86 | int show_reservations(int argc, char **argv, void *hint); | |
87 | void show_user_info(char **argv); | |
88 | void show_user_info_unformatted(char **argv); | |
89 | struct string_list *add_to_string_list(struct string_list *old_list, char *s); | |
90 | int wrap_mr_query(char *handle, int argc, char **argv, | |
91 | int (*callback)(int, char **, void *), void *callarg); | |
92 | void print_query(char *query_name, int argc, char **argv); | |
93 | ||
94 | int main(int argc, char **argv) | |
95 | { | |
96 | int status; | |
97 | char **arg = argv; | |
98 | char *server = NULL; | |
99 | ||
100 | /* clear all flags & lists */ | |
101 | info_flag = update_flag = create_flag = deact_flag = reg_flag = 0; | |
102 | list_res_flag = update_res_flag = unformatted_flag = verbose = noauth = 0; | |
103 | newlogin = uid = shell = winshell = last = first = middle = NULL; | |
104 | u_status = clearid = class = comment = secure = NULL; | |
aec7d432 | 105 | winhomedir = winprofiledir = NULL; |
5d429b61 | 106 | reservation_add_queue = reservation_remove_queue = NULL; |
7902e669 | 107 | sponsor = NULL; |
5d429b61 | 108 | whoami = argv[0]; |
109 | ||
110 | /* parse args */ | |
111 | while (++arg - argv < argc) | |
112 | { | |
113 | if (**arg == '-') | |
114 | { | |
115 | if (argis("i", "info")) | |
116 | info_flag++; | |
117 | else if (argis("C", "create")) | |
118 | create_flag++; | |
119 | else if (argis("D", "deact")) | |
120 | deact_flag++; | |
121 | else if (argis("r", "register")) | |
122 | reg_flag++; | |
123 | else if (argis("R", "rename")) { | |
124 | if (arg - argv < argc - 1) { | |
125 | arg++; | |
126 | update_flag++; | |
127 | newlogin = *arg; | |
128 | } else | |
129 | usage(argv); | |
130 | } | |
131 | else if (argis("U", "uid")) { | |
132 | if (arg - argv < argc - 1) { | |
133 | arg++; | |
134 | update_flag++; | |
135 | uid = *arg; | |
136 | } else | |
137 | usage(argv); | |
138 | } | |
139 | else if (argis("s", "shell")) { | |
140 | if (arg - argv < argc - 1) { | |
141 | arg++; | |
142 | update_flag++; | |
143 | shell = *arg; | |
144 | } else | |
145 | usage(argv); | |
146 | } | |
147 | else if (argis("w", "winshell")) { | |
148 | if (arg - argv < argc - 1) { | |
149 | arg++; | |
150 | update_flag++; | |
151 | winshell = *arg; | |
152 | } else | |
153 | usage(argv); | |
154 | } | |
155 | else if (argis("L", "last")) { | |
156 | if (arg - argv < argc - 1) { | |
157 | arg++; | |
158 | update_flag++; | |
159 | last = *arg; | |
160 | } else | |
161 | usage(argv); | |
162 | } | |
163 | else if (argis("F", "first")) { | |
164 | if (arg - argv < argc - 1) { | |
165 | arg++; | |
166 | update_flag++; | |
167 | first = *arg; | |
168 | } else | |
169 | usage(argv); | |
170 | } | |
171 | else if (argis("M", "middle")) { | |
172 | if (arg - argv < argc - 1) { | |
173 | arg++; | |
174 | update_flag++; | |
175 | middle = *arg; | |
176 | } else | |
177 | usage(argv); | |
178 | } | |
179 | else if (argis("S", "status")) { | |
180 | if (arg - argv < argc - 1) { | |
181 | int i; | |
182 | int len; | |
183 | ||
184 | arg++; | |
185 | update_flag++; | |
186 | u_status = *arg; | |
187 | len = strlen(u_status); | |
188 | for (i = 0; i < len; i++) { | |
189 | if (!isdigit(u_status[i])) { | |
190 | printf("Error: status code %s is not numeric.\n", u_status); | |
191 | exit(1); | |
192 | } | |
193 | } | |
194 | } else | |
195 | usage(argv); | |
196 | } | |
484f87be | 197 | else if (argis("I", "mitid")) { |
5d429b61 | 198 | if (arg - argv < argc - 1) { |
199 | arg++; | |
200 | update_flag++; | |
201 | clearid = *arg; | |
202 | } else | |
203 | usage(argv); | |
204 | } | |
205 | else if (argis("cl", "class")) { | |
206 | if (arg - argv < argc - 1) { | |
207 | arg++; | |
208 | update_flag++; | |
209 | class = *arg; | |
210 | } else | |
211 | usage(argv); | |
212 | } | |
213 | else if (argis("c", "comment")) { | |
214 | if (arg - argv < argc - 1) { | |
215 | arg++; | |
216 | update_flag++; | |
217 | comment = *arg; | |
218 | } else | |
219 | usage(argv); | |
220 | } | |
221 | else if (argis("6", "secure")) { | |
222 | if (arg - argv < argc - 1) { | |
223 | arg++; | |
224 | update_flag++; | |
225 | secure = *arg; | |
226 | } else | |
227 | usage(argv); | |
228 | } | |
aec7d432 | 229 | else if (argis("wh", "winhomedir")) { |
230 | if (arg - argv < argc - 1) { | |
231 | arg++; | |
232 | update_flag++; | |
233 | winhomedir = *arg; | |
234 | } else | |
235 | usage(argv); | |
236 | } | |
237 | else if (argis("wp", "winprofiledir")) { | |
238 | if (arg - argv < argc - 1) { | |
239 | arg++; | |
240 | update_flag++; | |
241 | winprofiledir = *arg; | |
242 | } else | |
243 | usage(argv); | |
244 | } | |
7902e669 | 245 | else if (argis("sp", "sponsor")) { |
246 | if (arg - argv < argc - 1) { | |
247 | arg++; | |
248 | update_flag++; | |
249 | sponsor = parse_member(*arg); | |
250 | } else | |
251 | usage(argv); | |
252 | } | |
5d429b61 | 253 | else if (argis("ar", "addreservation")) { |
254 | if (arg - argv < argc - 1) { | |
255 | arg++; | |
256 | reservation_add_queue = add_to_string_list(reservation_add_queue, | |
257 | *arg); | |
258 | } else | |
259 | usage(argv); | |
260 | update_res_flag++; | |
261 | } | |
262 | else if (argis("dr", "deletereservation")) { | |
263 | if (arg - argv < argc - 1) { | |
264 | arg++; | |
265 | reservation_remove_queue = add_to_string_list(reservation_remove_queue, *arg); | |
266 | } else | |
267 | usage(argv); | |
268 | update_res_flag++; | |
269 | } | |
ec7f0346 | 270 | else if (argis("lr", "listreservation")) |
5d429b61 | 271 | list_res_flag++; |
272 | else if (argis("u", "unformatted")) | |
273 | unformatted_flag++; | |
274 | else if (argis("n", "noauth")) | |
275 | noauth++; | |
276 | else if (argis("v", "verbose")) | |
277 | verbose++; | |
278 | else if (argis("db", "database")) | |
279 | { | |
280 | if (arg - argv < argc - 1) | |
281 | { | |
282 | ++arg; | |
283 | server = *arg; | |
284 | } | |
285 | else | |
286 | usage(argv); | |
287 | } | |
288 | else | |
289 | usage(argv); | |
290 | } | |
291 | else if (username == NULL) | |
292 | username = *arg; | |
293 | else | |
294 | usage(argv); | |
295 | } | |
ec7f0346 | 296 | if (username == NULL && !create_flag) |
5d429b61 | 297 | usage(argv); |
298 | ||
299 | /* default to info_flag if nothing else was specified */ | |
300 | if(!(info_flag || update_flag || create_flag || \ | |
301 | deact_flag || reg_flag || list_res_flag || \ | |
302 | update_res_flag)) { | |
303 | info_flag++; | |
304 | } | |
305 | ||
306 | /* fire up Moira */ | |
7902e669 | 307 | status = mrcl_connect(server, "stanley", 12, !noauth); |
5d429b61 | 308 | if (status == MRCL_AUTH_ERROR) |
309 | { | |
310 | com_err(whoami, 0, "Try the -noauth flag if you don't " | |
311 | "need authentication."); | |
312 | } | |
313 | if (status) | |
314 | exit(2); | |
315 | ||
316 | /* create if needed */ | |
317 | if (create_flag) | |
318 | { | |
319 | char *argv[20]; | |
320 | int cnt; | |
321 | ||
322 | for (cnt = 0; cnt < 14; cnt++) { | |
ec7f0346 | 323 | argv[cnt] = ""; |
5d429b61 | 324 | } |
325 | ||
ec7f0346 | 326 | if (username) |
327 | argv[U_NAME] = username; | |
328 | else | |
329 | argv[U_NAME] = UNIQUE_LOGIN; | |
5d429b61 | 330 | if (uid) |
331 | argv[U_UID] = uid; | |
332 | else | |
333 | argv[U_UID] = UNIQUE_UID; | |
334 | if (shell) | |
335 | argv[U_SHELL] = shell; | |
336 | else | |
337 | argv[U_SHELL] = "/bin/athena/tcsh"; | |
338 | if (winshell) | |
339 | argv[U_WINCONSOLESHELL] = winshell; | |
340 | else | |
341 | argv[U_WINCONSOLESHELL] = "cmd"; | |
342 | if (last) | |
343 | argv[U_LAST] = last; | |
344 | if (first) | |
345 | argv[U_FIRST] = first; | |
346 | if (middle) | |
347 | argv[U_MIDDLE] = middle; | |
348 | if (u_status) | |
349 | argv[U_STATE] = u_status; | |
350 | else | |
351 | argv[U_STATE] = "0"; | |
352 | if (clearid) | |
353 | argv[U_MITID] = clearid; | |
354 | if (class) | |
355 | argv[U_CLASS] = class; | |
356 | if (comment) | |
357 | argv[U_COMMENT] = comment; | |
358 | /* Signature field always is the empty string */ | |
359 | argv[U_SIGNATURE] = ""; | |
360 | if (secure) | |
361 | argv[U_SECURE] = secure; | |
362 | else | |
363 | argv[U_SECURE] = "0"; | |
aec7d432 | 364 | if (winhomedir) |
365 | argv[U_WINHOMEDIR] = winhomedir; | |
366 | else | |
e20b02d1 | 367 | argv[U_WINHOMEDIR] = "[DFS]"; |
aec7d432 | 368 | if (winprofiledir) |
369 | argv[U_WINPROFILEDIR] = winprofiledir; | |
370 | else | |
e20b02d1 | 371 | argv[U_WINPROFILEDIR] = "[DFS]"; |
7902e669 | 372 | if (sponsor) |
373 | { | |
374 | argv[U_SPONSOR_NAME] = sponsor->name; | |
375 | switch (sponsor->type) | |
376 | { | |
377 | case M_ANY: | |
378 | case M_USER: | |
379 | argv[U_SPONSOR_TYPE] = "USER"; | |
380 | status = wrap_mr_query("add_user_account", 17, argv, NULL, NULL); | |
381 | if (sponsor->type != M_ANY || status != MR_USER) | |
382 | break; | |
383 | ||
384 | case M_LIST: | |
385 | argv[U_SPONSOR_TYPE] = "LIST"; | |
386 | status = wrap_mr_query("add_user_account", 17, argv, NULL, NULL); | |
387 | break; | |
388 | ||
389 | case M_KERBEROS: | |
390 | argv[U_SPONSOR_TYPE] = "KERBEROS"; | |
391 | status = mrcl_validate_kerberos_member(argv[U_SPONSOR_NAME], | |
392 | &argv[U_SPONSOR_NAME]); | |
393 | if (mrcl_get_message()) | |
394 | mrcl_com_err(whoami); | |
395 | if (status == MRCL_REJECT) | |
396 | exit(1); | |
397 | status = wrap_mr_query("add_user_account", 17, argv, NULL, NULL); | |
398 | break; | |
399 | ||
400 | case M_NONE: | |
401 | argv[U_SPONSOR_TYPE] = "NONE"; | |
402 | status = wrap_mr_query("add_user_account", 17, argv, NULL, NULL); | |
403 | break; | |
404 | } | |
405 | } | |
406 | else | |
407 | { | |
408 | argv[U_SPONSOR_TYPE] = "NONE"; | |
409 | argv[U_SPONSOR_NAME] = "NONE"; | |
410 | ||
411 | status = wrap_mr_query("add_user_account", 17, argv, NULL, NULL); | |
412 | } | |
413 | ||
5d429b61 | 414 | if (status) |
415 | { | |
416 | com_err(whoami, status, "while adding user account."); | |
417 | exit(1); | |
418 | } | |
419 | } | |
420 | else if (update_flag) | |
421 | { | |
422 | char *old_argv[20]; | |
423 | char *argv[20]; | |
424 | char *args[5]; | |
425 | ||
426 | args[0] = username; | |
427 | ||
428 | status = wrap_mr_query("get_user_account_by_login", 1, args, | |
429 | save_query_info, old_argv); | |
430 | if (status) | |
431 | { | |
432 | com_err(whoami, status, "while getting user information."); | |
433 | exit(1); | |
434 | } | |
435 | ||
436 | argv[1] = old_argv[0]; | |
437 | argv[2] = old_argv[1]; | |
438 | argv[3] = old_argv[2]; | |
439 | argv[4] = old_argv[3]; | |
440 | argv[5] = old_argv[4]; | |
441 | argv[6] = old_argv[5]; | |
442 | argv[7] = old_argv[6]; | |
443 | argv[8] = old_argv[7]; | |
444 | argv[9] = old_argv[8]; | |
445 | argv[10] = old_argv[9]; | |
446 | argv[11] = old_argv[10]; | |
447 | argv[12] = old_argv[11]; | |
448 | argv[13] = old_argv[12]; | |
aec7d432 | 449 | argv[14] = old_argv[13]; |
450 | argv[15] = old_argv[14]; | |
7902e669 | 451 | argv[16] = old_argv[15]; |
452 | argv[17] = old_argv[16]; | |
5d429b61 | 453 | |
454 | argv[0] = username; | |
455 | if (newlogin) | |
456 | argv[1] = newlogin; | |
457 | if (uid) | |
458 | argv[2] = uid; | |
459 | if (shell) | |
460 | argv[3] = shell; | |
461 | if (winshell) | |
462 | argv[4] = winshell; | |
463 | if (last) | |
464 | argv[5] = last; | |
465 | if (first) | |
466 | argv[6] = first; | |
467 | if (middle) | |
468 | argv[7] = middle; | |
469 | if (u_status) | |
470 | argv[8] = u_status; | |
471 | if (clearid) | |
472 | argv[9] = clearid; | |
473 | if (class) | |
474 | argv[10] = class; | |
475 | if (comment) | |
476 | argv[11] = comment; | |
477 | if (secure) | |
478 | argv[13] = secure; | |
aec7d432 | 479 | if (winhomedir) |
480 | argv[14] = winhomedir; | |
481 | if (winprofiledir) | |
482 | argv[15] = winprofiledir; | |
7902e669 | 483 | if (sponsor) |
484 | { | |
485 | argv[17] = sponsor->name; | |
486 | switch (sponsor->type) | |
487 | { | |
488 | case M_ANY: | |
489 | case M_USER: | |
490 | argv[16] = "USER"; | |
491 | status = wrap_mr_query("update_user_account", 18, argv, NULL, | |
492 | NULL); | |
493 | if (sponsor->type != M_ANY || status != MR_USER) | |
494 | break; | |
495 | ||
496 | case M_LIST: | |
497 | argv[16] = "LIST"; | |
498 | status = wrap_mr_query("update_user_account", 18, argv, NULL, | |
499 | NULL); | |
500 | break; | |
501 | ||
502 | case M_KERBEROS: | |
503 | argv[16] = "KERBEROS"; | |
504 | status = mrcl_validate_kerberos_member(argv[17], &argv[17]); | |
505 | if (mrcl_get_message()) | |
506 | mrcl_com_err(whoami); | |
507 | if (status == MRCL_REJECT) | |
508 | exit(1); | |
509 | status = wrap_mr_query("update_user_account", 18, argv, NULL, | |
510 | NULL); | |
511 | break; | |
512 | ||
513 | case M_NONE: | |
514 | argv[16] = "NONE"; | |
515 | status = wrap_mr_query("update_user_account", 18, argv, NULL, | |
516 | NULL); | |
517 | break; | |
518 | } | |
519 | } | |
520 | else | |
521 | status = wrap_mr_query("update_user_account", 18, argv, NULL, NULL); | |
5d429b61 | 522 | |
523 | if (status) | |
524 | com_err(whoami, status, "while updating user."); | |
525 | else if (newlogin) | |
526 | username = newlogin; | |
527 | } | |
528 | ||
ec7f0346 | 529 | /* Deactivate a user, and the matching list and filesystem if they exist */ |
5d429b61 | 530 | if (deact_flag) |
531 | { | |
532 | char *args[2]; | |
533 | char *argv[20]; | |
534 | int i; | |
535 | ||
536 | args[0] = username; | |
537 | args[1] = "3"; | |
538 | ||
539 | status = wrap_mr_query("update_user_status", 2, args, NULL, NULL); | |
540 | if (status) | |
541 | { | |
542 | com_err(whoami, status, "while deactivating user."); | |
543 | exit(1); | |
544 | } | |
545 | ||
ec7f0346 | 546 | status = wrap_mr_query("get_list_info", 1, args, save_query_info, argv); |
547 | if (status == MR_SUCCESS) | |
5d429b61 | 548 | { |
e6dfe8f9 | 549 | for (i = 15; i > 0; i--) |
ec7f0346 | 550 | argv[i + 1] = argv[i]; |
551 | argv[1] = username; | |
552 | argv[L_ACTIVE + 1] = "0"; | |
553 | ||
e6dfe8f9 | 554 | status = wrap_mr_query("update_list", 16, argv, NULL, NULL); |
ec7f0346 | 555 | if (status) |
556 | { | |
557 | com_err(whoami, status, "while updating list, " | |
558 | "not deactivating list or filesystem."); | |
559 | exit(1); | |
560 | } | |
5d429b61 | 561 | } |
ec7f0346 | 562 | else if (status && status != MR_NO_MATCH) |
5d429b61 | 563 | { |
ec7f0346 | 564 | com_err(whoami, status, "while retrieving list information."); |
5d429b61 | 565 | exit(1); |
566 | } | |
567 | ||
568 | status = wrap_mr_query("get_filesys_by_label", 1, args, save_query_info, | |
569 | argv); | |
ec7f0346 | 570 | if (status == MR_SUCCESS) |
5d429b61 | 571 | { |
ec7f0346 | 572 | for (i = 11; i > 0; i--) |
573 | argv[i + 1] = argv[i]; | |
574 | argv[1] = username; | |
575 | argv[FS_TYPE + 1] = "ERR"; | |
576 | argv[FS_COMMENTS + 1] = "Locker disabled; call 3-1325 for help"; | |
577 | ||
578 | status = wrap_mr_query("update_filesys", 12, argv, NULL, NULL); | |
579 | if (status) | |
580 | { | |
581 | com_err(whoami, status, "while updating filesystem, " | |
582 | "not deactivating filesystem."); | |
583 | exit(1); | |
584 | } | |
5d429b61 | 585 | } |
ec7f0346 | 586 | else if (status && status != MR_NO_MATCH) |
5d429b61 | 587 | { |
ec7f0346 | 588 | com_err(whoami, status, "while retrieving filesystem information."); |
5d429b61 | 589 | exit(1); |
590 | } | |
591 | } | |
592 | ||
593 | /* Display user info */ | |
594 | if (info_flag) | |
595 | { | |
596 | char *args[2]; | |
597 | char *argv[20]; | |
598 | ||
599 | args[0] = username; | |
600 | status = wrap_mr_query("get_user_account_by_login", 1, args, | |
601 | save_query_info, argv); | |
602 | if (status) | |
603 | { | |
604 | com_err(whoami, status, "while getting user information."); | |
605 | exit(1); | |
606 | } | |
607 | if (unformatted_flag) | |
608 | show_user_info_unformatted(argv); | |
609 | else | |
610 | show_user_info(argv); | |
611 | } | |
612 | ||
613 | /* register a user */ | |
614 | if (reg_flag) | |
615 | { | |
616 | char *args[3]; | |
617 | char *argv[20]; | |
5d429b61 | 618 | |
619 | args[0] = username; | |
620 | status = wrap_mr_query("get_user_account_by_login", 1, args, | |
621 | save_query_info, argv); | |
622 | if (status) | |
623 | { | |
624 | com_err(whoami, status, "while looking up uid."); | |
625 | exit(1); | |
626 | } | |
627 | ||
628 | args[0] = argv[U_UID]; | |
ec7f0346 | 629 | args[1] = username; |
630 | args[2] = "IMAP"; | |
5d429b61 | 631 | |
632 | status = wrap_mr_query("register_user", 3, args, NULL, NULL); | |
633 | if (status) | |
634 | { | |
635 | com_err(whoami, status, "while registering user."); | |
636 | exit(1); | |
637 | } | |
638 | } | |
639 | ||
640 | /* list user reservations */ | |
641 | if (list_res_flag) | |
642 | { | |
643 | char *args[1]; | |
644 | ||
645 | args[0] = username; | |
646 | status = wrap_mr_query("get_user_reservations", 1, args, | |
647 | show_reservations, NULL); | |
648 | if (status) | |
649 | if (status != MR_NO_MATCH) { | |
650 | com_err(whoami, status, "while getting user reservations."); | |
651 | exit(1); | |
652 | } | |
653 | } | |
654 | ||
655 | /* add user reservations */ | |
656 | if (reservation_add_queue) | |
657 | { | |
658 | struct string_list *q = reservation_add_queue; | |
659 | ||
660 | while (q) | |
661 | { | |
662 | char *reservation = q->string; | |
663 | char *args[2]; | |
664 | ||
665 | args[0] = username; | |
666 | args[1] = reservation; | |
667 | status = wrap_mr_query("add_user_reservation", 2, args, NULL, NULL); | |
668 | if (status) | |
669 | { | |
670 | com_err(whoami, status, "while adding user reservation."); | |
671 | exit(1); | |
672 | } | |
673 | ||
674 | q = q->next; | |
675 | } | |
676 | } | |
677 | ||
678 | /* delete user reservations */ | |
679 | if (reservation_remove_queue) | |
680 | { | |
681 | struct string_list *q = reservation_remove_queue; | |
682 | ||
683 | while (q) | |
684 | { | |
685 | char *reservation = q->string; | |
686 | char *args[2]; | |
687 | ||
688 | args[0] = username; | |
689 | args[1] = reservation; | |
690 | status = wrap_mr_query("delete_user_reservation", 2, args, NULL, | |
691 | NULL); | |
692 | if (status) | |
693 | { | |
694 | com_err(whoami, status, "while deleting user reservation."); | |
695 | exit(1); | |
696 | } | |
697 | ||
698 | q = q->next; | |
699 | } | |
700 | } | |
701 | /* We're done! */ | |
702 | mr_disconnect(); | |
703 | exit(0); | |
704 | } | |
705 | ||
706 | int save_query_info(int argc, char **argv, void *hint) | |
707 | { | |
708 | int i; | |
709 | char **nargv = hint; | |
710 | ||
711 | for(i = 0; i < argc; i++) | |
712 | nargv[i] = strdup(argv[i]); | |
713 | ||
714 | return MR_CONT; | |
715 | } | |
716 | ||
717 | int show_reservations(int argc, char **argv, void *hint) | |
718 | { | |
719 | printf("Reservation: %s\n", argv[0]); | |
720 | ||
721 | return MR_CONT; | |
722 | } | |
723 | ||
724 | struct string_list *add_to_string_list(struct string_list *old_list, char *s) { | |
725 | struct string_list *new_list; | |
726 | ||
727 | new_list = (struct string_list *)malloc(sizeof(struct string_list *)); | |
728 | new_list->next = old_list; | |
729 | new_list->string = s; | |
730 | ||
731 | return new_list; | |
732 | } | |
733 | ||
734 | int wrap_mr_query(char *handle, int argc, char **argv, | |
735 | int (*callback)(int, char **, void *), void *callarg) { | |
736 | if (verbose) | |
737 | print_query(handle, argc, argv); | |
738 | ||
739 | return mr_query(handle, argc, argv, callback, callarg); | |
740 | } | |
741 | ||
742 | void print_query(char *query_name, int argc, char **argv) { | |
743 | int cnt; | |
744 | ||
745 | printf("qy %s", query_name); | |
746 | for(cnt = 0; cnt < argc; cnt++) | |
747 | printf(" <%s>", argv[cnt]); | |
748 | printf("\n"); | |
749 | } | |
750 | ||
5d429b61 | 751 | void show_user_info(char **argv) |
752 | { | |
753 | char tbuf[BUFSIZ]; | |
754 | int status; | |
755 | ||
756 | sprintf(tbuf, "%s, %s %s", argv[U_LAST], argv[U_FIRST], argv[U_MIDDLE]); | |
757 | printf("Login name: %-20s Full name: %s\n", argv[U_NAME], tbuf); | |
758 | printf("User id: %-23s Login shell: %-10s\n", argv[U_UID], argv[U_SHELL]); | |
759 | printf("Class: %-25s Windows Console Shell: %-10s\n", argv[U_CLASS], | |
760 | argv[U_WINCONSOLESHELL]); | |
7902e669 | 761 | sprintf(tbuf, "%s %s", argv[U_SPONSOR_TYPE], |
762 | strcmp(argv[U_SPONSOR_TYPE], "NONE") ? argv[U_SPONSOR_NAME] : ""); | |
763 | printf("Sponsor: %-25s\n", tbuf); | |
5d429b61 | 764 | printf("Account is: %-20s MIT ID number: %s\n", |
765 | UserState(atoi(argv[U_STATE])), argv[U_MITID]); | |
aec7d432 | 766 | printf("Windows Home Directory: %s\n", argv[U_WINHOMEDIR]); |
767 | printf("Windows Profile Directory: %s\n", argv[U_WINPROFILEDIR]); | |
5d429b61 | 768 | status = atoi(argv[U_STATE]); |
769 | if (status == 0 || status == 2) | |
770 | { | |
ec7f0346 | 771 | printf("User %s secure Account Coupon to register\n", |
5d429b61 | 772 | atoi(argv[U_SECURE]) ? "needs" : "does not need"); |
773 | } | |
774 | printf("Comments: %s\n", argv[U_COMMENT]); | |
cfeeecec | 775 | printf("Created by %s on %s.\n", argv[U_CREATOR], argv[U_CREATED]); |
5d429b61 | 776 | printf("Last mod by %s at %s with %s.\n", argv[U_MODBY], argv[U_MODTIME], |
777 | argv[U_MODWITH]); | |
778 | } | |
779 | ||
780 | void show_user_info_unformatted(char **argv) | |
781 | { | |
7902e669 | 782 | char tbuf[BUFSIZ]; |
8598c363 | 783 | int status; |
784 | ||
aec7d432 | 785 | printf("Login name: %s\n", argv[U_NAME]); |
786 | printf("Full name: %s, %s %s\n", argv[U_LAST], argv[U_FIRST], | |
5d429b61 | 787 | argv[U_MIDDLE]); |
aec7d432 | 788 | printf("User id: %s\n", argv[U_UID]); |
789 | printf("Class: %s\n", argv[U_CLASS]); | |
7902e669 | 790 | sprintf(tbuf, "%s %s", argv[U_SPONSOR_TYPE], |
791 | strcmp(argv[U_SPONSOR_TYPE], "NONE") ? argv[U_SPONSOR_NAME] : ""); | |
792 | printf("Sponsor: %s\n", tbuf); | |
aec7d432 | 793 | printf("Login shell: %s\n", argv[U_SHELL]); |
794 | printf("Windows Console Shell: %s\n", argv[U_WINCONSOLESHELL]); | |
795 | printf("Account is: %s\n", UserState(atoi(argv[U_STATE]))); | |
796 | printf("MIT ID number: %s\n", argv[U_MITID]); | |
797 | printf("Windows Home Directory: %s\n", argv[U_WINHOMEDIR]); | |
798 | printf("Windows Profile Directory: %s\n", argv[U_WINPROFILEDIR]); | |
8598c363 | 799 | status = atoi(argv[U_STATE]); |
800 | if (status == 0 || status == 2) | |
aec7d432 | 801 | printf("Secure: %s secure Account Coupon to register\n", |
8598c363 | 802 | atoi(argv[U_SECURE]) ? "Needs" : "Does not need"); |
aec7d432 | 803 | printf("Comments: %s\n", argv[U_COMMENT]); |
804 | printf("Created by: %s\n", argv[U_CREATOR]); | |
805 | printf("Created on: %s\n", argv[U_CREATED]); | |
806 | printf("Last mod by: %s\n", argv[U_MODBY]); | |
807 | printf("Last mod on: %s\n", argv[U_MODTIME]); | |
808 | printf("Last mod with: %s\n", argv[U_MODWITH]); | |
5d429b61 | 809 | } |
ec7f0346 | 810 | |
811 | void usage(char **argv) | |
812 | { | |
813 | #define USAGE_OPTIONS_FORMAT " %-39s%s\n" | |
814 | fprintf(stderr, "Usage: %s username [options]\n", argv[0]); | |
815 | fprintf(stderr, "Options are\n"); | |
b30ed58c | 816 | fprintf(stderr, " %-39s\n", "-i | -info"); |
ec7f0346 | 817 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-C | -create", |
818 | "-D | -deact"); | |
819 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-r | -register", | |
820 | "-R | -rename newname"); | |
821 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-U | -uid uid", | |
822 | "-s | -shell shell"); | |
823 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-S | -status status", | |
824 | "-w | -winshell winshell"); | |
825 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-F | -first firstname", | |
826 | "-L | -last lastname"); | |
827 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-M | -middle middlename", | |
484f87be | 828 | "-I | -mitid mitid"); |
ec7f0346 | 829 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-cl | -class class", |
830 | "-c | -comment comment"); | |
831 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-6 | -secure 0|1", | |
832 | "-lr | -listreservation"); | |
833 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-ar | -addreservation reservation", | |
834 | "-dr | -deletereservation reservation"); | |
aec7d432 | 835 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-wh | -winhomedir winhomedir", |
836 | "-wp | -winprofiledir winprofiledir"); | |
7902e669 | 837 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-sp | -sponsor sponsor", |
838 | "-u | -unformatted"); | |
839 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-n | -noauth", | |
840 | "-v | -verbose"); | |
841 | fprintf(stderr, " %-39s\n", "-db | -database host[:port]"); | |
ec7f0346 | 842 | |
843 | exit(1); | |
844 | } | |
7902e669 | 845 | |
846 | /* Parse a line of input, fetching a member. NULL is returned if a member | |
847 | * is not found. ';' is a comment character. | |
848 | */ | |
849 | struct owner_type *parse_member(char *s) | |
850 | { | |
851 | struct owner_type *m; | |
852 | char *p, *lastchar; | |
853 | ||
854 | while (*s && isspace(*s)) | |
855 | s++; | |
856 | lastchar = p = s; | |
857 | while (*p && *p != '\n' && *p != ';') | |
858 | { | |
859 | if (isprint(*p) && !isspace(*p)) | |
860 | lastchar = p++; | |
861 | else | |
862 | p++; | |
863 | } | |
864 | lastchar++; | |
865 | *lastchar = '\0'; | |
866 | if (p == s || strlen(s) == 0) | |
867 | return NULL; | |
868 | ||
869 | if (!(m = malloc(sizeof(struct owner_type)))) | |
870 | return NULL; | |
871 | ||
872 | if ((p = strchr(s, ':'))) | |
873 | { | |
874 | *p = '\0'; | |
875 | m->name = ++p; | |
876 | if (!strcasecmp("user", s)) | |
877 | m->type = M_USER; | |
878 | else if (!strcasecmp("list", s)) | |
879 | m->type = M_LIST; | |
880 | else if (!strcasecmp("kerberos", s)) | |
881 | m->type = M_KERBEROS; | |
882 | else if (!strcasecmp("none", s)) | |
883 | m->type = M_NONE; | |
884 | else | |
885 | { | |
886 | m->type = M_ANY; | |
887 | *(--p) = ':'; | |
888 | m->name = s; | |
889 | } | |
890 | m->name = strdup(m->name); | |
891 | } | |
892 | else | |
893 | { | |
894 | m->name = strdup(s); | |
895 | m->type = strcasecmp(s, "none") ? M_ANY : M_NONE; | |
896 | } | |
897 | return m; | |
898 | } |