2 * Command line oriented Moira users tool.
4 * zacheiss@mit.edu, September 2001
8 * Copyright (C) 2000, 2001 by the Massachusetts Institute of Technology.
9 * For copying and distribution information, please see the file
13 #include <mit-copyright.h>
15 #include <moira_site.h>
28 struct string_list *next;
31 /* argument parsing macro */
32 #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
34 /* flags from command line */
35 int info_flag, update_flag, create_flag, deact_flag, reg_flag;
36 int list_res_flag, update_res_flag, unformatted_flag, verbose, noauth;
38 struct string_list *reservation_add_queue, *reservation_remove_queue;
40 char *username, *whoami;
42 char *newlogin, *uid, *shell, *winshell, *last, *first, *middle, *u_status;
43 char *clearid, *class, *comment, *secure;
45 static char *states[] = {
48 "Half Registered (2)",
50 "Not registerable (4)",
51 "Enrolled/Registerable (5)",
52 "Enrolled/Not Registerable (6)",
56 static char *UserState(int state)
58 static char buf[BUFSIZ];
60 if (state < 0 || state >= US_END)
62 sprintf(buf, "Unknown (%d)", state);
68 void usage(char **argv);
69 int save_query_info(int argc, char **argv, void *hint);
70 int show_reservations(int argc, char **argv, void *hint);
71 void show_user_info(char **argv);
72 void show_user_info_unformatted(char **argv);
73 struct string_list *add_to_string_list(struct string_list *old_list, char *s);
74 int wrap_mr_query(char *handle, int argc, char **argv,
75 int (*callback)(int, char **, void *), void *callarg);
76 void print_query(char *query_name, int argc, char **argv);
78 int main(int argc, char **argv)
84 /* clear all flags & lists */
85 info_flag = update_flag = create_flag = deact_flag = reg_flag = 0;
86 list_res_flag = update_res_flag = unformatted_flag = verbose = noauth = 0;
87 newlogin = uid = shell = winshell = last = first = middle = NULL;
88 u_status = clearid = class = comment = secure = NULL;
89 reservation_add_queue = reservation_remove_queue = NULL;
93 while (++arg - argv < argc)
97 if (argis("i", "info"))
99 else if (argis("C", "create"))
101 else if (argis("D", "deact"))
103 else if (argis("r", "register"))
105 else if (argis("R", "rename")) {
106 if (arg - argv < argc - 1) {
113 else if (argis("U", "uid")) {
114 if (arg - argv < argc - 1) {
121 else if (argis("s", "shell")) {
122 if (arg - argv < argc - 1) {
129 else if (argis("w", "winshell")) {
130 if (arg - argv < argc - 1) {
137 else if (argis("L", "last")) {
138 if (arg - argv < argc - 1) {
145 else if (argis("F", "first")) {
146 if (arg - argv < argc - 1) {
153 else if (argis("M", "middle")) {
154 if (arg - argv < argc - 1) {
161 else if (argis("S", "status")) {
162 if (arg - argv < argc - 1) {
169 len = strlen(u_status);
170 for (i = 0; i < len; i++) {
171 if (!isdigit(u_status[i])) {
172 printf("Error: status code %s is not numeric.\n", u_status);
179 else if (argis("I", "clearid")) {
180 if (arg - argv < argc - 1) {
187 else if (argis("cl", "class")) {
188 if (arg - argv < argc - 1) {
195 else if (argis("c", "comment")) {
196 if (arg - argv < argc - 1) {
203 else if (argis("6", "secure")) {
204 if (arg - argv < argc - 1) {
211 else if (argis("ar", "addreservation")) {
212 if (arg - argv < argc - 1) {
214 reservation_add_queue = add_to_string_list(reservation_add_queue,
220 else if (argis("dr", "deletereservation")) {
221 if (arg - argv < argc - 1) {
223 reservation_remove_queue = add_to_string_list(reservation_remove_queue, *arg);
228 else if (argis("lr", "listreservation"))
230 else if (argis("u", "unformatted"))
232 else if (argis("n", "noauth"))
234 else if (argis("v", "verbose"))
236 else if (argis("db", "database"))
238 if (arg - argv < argc - 1)
249 else if (username == NULL)
254 if (username == NULL && !create_flag)
257 /* default to info_flag if nothing else was specified */
258 if(!(info_flag || update_flag || create_flag || \
259 deact_flag || reg_flag || list_res_flag || \
265 status = mrcl_connect(server, "stanley", 8, !noauth);
266 if (status == MRCL_AUTH_ERROR)
268 com_err(whoami, 0, "Try the -noauth flag if you don't "
269 "need authentication.");
274 /* create if needed */
280 for (cnt = 0; cnt < 14; cnt++) {
285 argv[U_NAME] = username;
287 argv[U_NAME] = UNIQUE_LOGIN;
291 argv[U_UID] = UNIQUE_UID;
293 argv[U_SHELL] = shell;
295 argv[U_SHELL] = "/bin/athena/tcsh";
297 argv[U_WINCONSOLESHELL] = winshell;
299 argv[U_WINCONSOLESHELL] = "cmd";
303 argv[U_FIRST] = first;
305 argv[U_MIDDLE] = middle;
307 argv[U_STATE] = u_status;
311 argv[U_MITID] = clearid;
313 argv[U_CLASS] = class;
315 argv[U_COMMENT] = comment;
316 /* Signature field always is the empty string */
317 argv[U_SIGNATURE] = "";
319 argv[U_SECURE] = secure;
321 argv[U_SECURE] = "0";
323 status = wrap_mr_query("add_user_account", 13, argv, NULL, NULL);
326 com_err(whoami, status, "while adding user account.");
330 else if (update_flag)
338 status = wrap_mr_query("get_user_account_by_login", 1, args,
339 save_query_info, old_argv);
342 com_err(whoami, status, "while getting user information.");
346 argv[1] = old_argv[0];
347 argv[2] = old_argv[1];
348 argv[3] = old_argv[2];
349 argv[4] = old_argv[3];
350 argv[5] = old_argv[4];
351 argv[6] = old_argv[5];
352 argv[7] = old_argv[6];
353 argv[8] = old_argv[7];
354 argv[9] = old_argv[8];
355 argv[10] = old_argv[9];
356 argv[11] = old_argv[10];
357 argv[12] = old_argv[11];
358 argv[13] = old_argv[12];
386 status = wrap_mr_query("update_user_account", 14, argv, NULL, NULL);
389 com_err(whoami, status, "while updating user.");
394 /* Deactivate a user, and the matching list and filesystem if they exist */
404 status = wrap_mr_query("update_user_status", 2, args, NULL, NULL);
407 com_err(whoami, status, "while deactivating user.");
411 status = wrap_mr_query("get_list_info", 1, args, save_query_info, argv);
412 if (status == MR_SUCCESS)
414 for (i = 13; i > 0; i--)
415 argv[i + 1] = argv[i];
417 argv[L_ACTIVE + 1] = "0";
419 status = wrap_mr_query("update_list", 14, argv, NULL, NULL);
422 com_err(whoami, status, "while updating list, "
423 "not deactivating list or filesystem.");
427 else if (status && status != MR_NO_MATCH)
429 com_err(whoami, status, "while retrieving list information.");
433 status = wrap_mr_query("get_filesys_by_label", 1, args, save_query_info,
435 if (status == MR_SUCCESS)
437 for (i = 11; i > 0; i--)
438 argv[i + 1] = argv[i];
440 argv[FS_TYPE + 1] = "ERR";
441 argv[FS_COMMENTS + 1] = "Locker disabled; call 3-1325 for help";
443 status = wrap_mr_query("update_filesys", 12, argv, NULL, NULL);
446 com_err(whoami, status, "while updating filesystem, "
447 "not deactivating filesystem.");
451 else if (status && status != MR_NO_MATCH)
453 com_err(whoami, status, "while retrieving filesystem information.");
458 /* Display user info */
465 status = wrap_mr_query("get_user_account_by_login", 1, args,
466 save_query_info, argv);
469 com_err(whoami, status, "while getting user information.");
472 if (unformatted_flag)
473 show_user_info_unformatted(argv);
475 show_user_info(argv);
478 /* register a user */
485 status = wrap_mr_query("get_user_account_by_login", 1, args,
486 save_query_info, argv);
489 com_err(whoami, status, "while looking up uid.");
493 args[0] = argv[U_UID];
497 status = wrap_mr_query("register_user", 3, args, NULL, NULL);
500 com_err(whoami, status, "while registering user.");
505 /* list user reservations */
511 status = wrap_mr_query("get_user_reservations", 1, args,
512 show_reservations, NULL);
514 if (status != MR_NO_MATCH) {
515 com_err(whoami, status, "while getting user reservations.");
520 /* add user reservations */
521 if (reservation_add_queue)
523 struct string_list *q = reservation_add_queue;
527 char *reservation = q->string;
531 args[1] = reservation;
532 status = wrap_mr_query("add_user_reservation", 2, args, NULL, NULL);
535 com_err(whoami, status, "while adding user reservation.");
543 /* delete user reservations */
544 if (reservation_remove_queue)
546 struct string_list *q = reservation_remove_queue;
550 char *reservation = q->string;
554 args[1] = reservation;
555 status = wrap_mr_query("delete_user_reservation", 2, args, NULL,
559 com_err(whoami, status, "while deleting user reservation.");
571 int save_query_info(int argc, char **argv, void *hint)
576 for(i = 0; i < argc; i++)
577 nargv[i] = strdup(argv[i]);
582 int show_reservations(int argc, char **argv, void *hint)
584 printf("Reservation: %s\n", argv[0]);
589 struct string_list *add_to_string_list(struct string_list *old_list, char *s) {
590 struct string_list *new_list;
592 new_list = (struct string_list *)malloc(sizeof(struct string_list *));
593 new_list->next = old_list;
594 new_list->string = s;
599 int wrap_mr_query(char *handle, int argc, char **argv,
600 int (*callback)(int, char **, void *), void *callarg) {
602 print_query(handle, argc, argv);
604 return mr_query(handle, argc, argv, callback, callarg);
607 void print_query(char *query_name, int argc, char **argv) {
610 printf("qy %s", query_name);
611 for(cnt = 0; cnt < argc; cnt++)
612 printf(" <%s>", argv[cnt]);
616 void show_user_info(char **argv)
621 sprintf(tbuf, "%s, %s %s", argv[U_LAST], argv[U_FIRST], argv[U_MIDDLE]);
622 printf("Login name: %-20s Full name: %s\n", argv[U_NAME], tbuf);
623 printf("User id: %-23s Login shell: %-10s\n", argv[U_UID], argv[U_SHELL]);
624 printf("Class: %-25s Windows Console Shell: %-10s\n", argv[U_CLASS],
625 argv[U_WINCONSOLESHELL]);
626 printf("Account is: %-20s MIT ID number: %s\n",
627 UserState(atoi(argv[U_STATE])), argv[U_MITID]);
628 status = atoi(argv[U_STATE]);
629 if (status == 0 || status == 2)
631 printf("User %s secure Account Coupon to register\n",
632 atoi(argv[U_SECURE]) ? "needs" : "does not need");
634 printf("Comments: %s\n", argv[U_COMMENT]);
635 printf("Last mod by %s at %s with %s.\n", argv[U_MODBY], argv[U_MODTIME],
639 void show_user_info_unformatted(char **argv)
641 printf("Login name: %s\n", argv[U_NAME]);
642 printf("Full name: %s, %s %s\n", argv[U_LAST], argv[U_FIRST],
644 printf("User id: %s\n", argv[U_UID]);
645 printf("Class: %s\n", argv[U_CLASS]);
646 printf("Login shell: %s\n", argv[U_SHELL]);
647 printf("Windows Console Shell: %s\n", argv[U_WINCONSOLESHELL]);
648 printf("Account is: %s\n", UserState(atoi(argv[U_STATE])));
649 printf("MIT ID number: %s\n", argv[U_MITID]);
650 printf("Secure: %s\n", argv[U_SECURE]);
651 printf("Comments: %s\n", argv[U_COMMENT]);
652 printf("Last mod by: %s\n", argv[U_MODBY]);
653 printf("Last mod on: %s\n", argv[U_MODTIME]);
654 printf("Last mod with: %s\n", argv[U_MODWITH]);
657 void usage(char **argv)
659 #define USAGE_OPTIONS_FORMAT " %-39s%s\n"
660 fprintf(stderr, "Usage: %s username [options]\n", argv[0]);
661 fprintf(stderr, "Options are\n");
662 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-C | -create",
664 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-r | -register",
665 "-R | -rename newname");
666 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-U | -uid uid",
667 "-s | -shell shell");
668 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-S | -status status",
669 "-w | -winshell winshell");
670 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-F | -first firstname",
671 "-L | -last lastname");
672 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-M | -middle middlename",
673 "-I | -clearid mitid");
674 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-cl | -class class",
675 "-c | -comment comment");
676 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-6 | -secure 0|1",
677 "-lr | -listreservation");
678 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-ar | -addreservation reservation",
679 "-dr | -deletereservation reservation");
680 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-u | -unformatted",
682 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-v | -verbose",
683 "-db | -database host[:port]");