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)",
54 "Registerable, Kerberos only (8)",
55 "Active, Kerberos only (9)"
58 static char *UserState(int state)
60 static char buf[BUFSIZ];
62 if (state < 0 || state >= US_END)
64 sprintf(buf, "Unknown (%d)", state);
70 void usage(char **argv);
71 int save_query_info(int argc, char **argv, void *hint);
72 int show_reservations(int argc, char **argv, void *hint);
73 void show_user_info(char **argv);
74 void show_user_info_unformatted(char **argv);
75 struct string_list *add_to_string_list(struct string_list *old_list, char *s);
76 int wrap_mr_query(char *handle, int argc, char **argv,
77 int (*callback)(int, char **, void *), void *callarg);
78 void print_query(char *query_name, int argc, char **argv);
80 int main(int argc, char **argv)
86 /* clear all flags & lists */
87 info_flag = update_flag = create_flag = deact_flag = reg_flag = 0;
88 list_res_flag = update_res_flag = unformatted_flag = verbose = noauth = 0;
89 newlogin = uid = shell = winshell = last = first = middle = NULL;
90 u_status = clearid = class = comment = secure = NULL;
91 reservation_add_queue = reservation_remove_queue = NULL;
95 while (++arg - argv < argc)
99 if (argis("i", "info"))
101 else if (argis("C", "create"))
103 else if (argis("D", "deact"))
105 else if (argis("r", "register"))
107 else if (argis("R", "rename")) {
108 if (arg - argv < argc - 1) {
115 else if (argis("U", "uid")) {
116 if (arg - argv < argc - 1) {
123 else if (argis("s", "shell")) {
124 if (arg - argv < argc - 1) {
131 else if (argis("w", "winshell")) {
132 if (arg - argv < argc - 1) {
139 else if (argis("L", "last")) {
140 if (arg - argv < argc - 1) {
147 else if (argis("F", "first")) {
148 if (arg - argv < argc - 1) {
155 else if (argis("M", "middle")) {
156 if (arg - argv < argc - 1) {
163 else if (argis("S", "status")) {
164 if (arg - argv < argc - 1) {
171 len = strlen(u_status);
172 for (i = 0; i < len; i++) {
173 if (!isdigit(u_status[i])) {
174 printf("Error: status code %s is not numeric.\n", u_status);
181 else if (argis("I", "mitid")) {
182 if (arg - argv < argc - 1) {
189 else if (argis("cl", "class")) {
190 if (arg - argv < argc - 1) {
197 else if (argis("c", "comment")) {
198 if (arg - argv < argc - 1) {
205 else if (argis("6", "secure")) {
206 if (arg - argv < argc - 1) {
213 else if (argis("ar", "addreservation")) {
214 if (arg - argv < argc - 1) {
216 reservation_add_queue = add_to_string_list(reservation_add_queue,
222 else if (argis("dr", "deletereservation")) {
223 if (arg - argv < argc - 1) {
225 reservation_remove_queue = add_to_string_list(reservation_remove_queue, *arg);
230 else if (argis("lr", "listreservation"))
232 else if (argis("u", "unformatted"))
234 else if (argis("n", "noauth"))
236 else if (argis("v", "verbose"))
238 else if (argis("db", "database"))
240 if (arg - argv < argc - 1)
251 else if (username == NULL)
256 if (username == NULL && !create_flag)
259 /* default to info_flag if nothing else was specified */
260 if(!(info_flag || update_flag || create_flag || \
261 deact_flag || reg_flag || list_res_flag || \
267 status = mrcl_connect(server, "stanley", 8, !noauth);
268 if (status == MRCL_AUTH_ERROR)
270 com_err(whoami, 0, "Try the -noauth flag if you don't "
271 "need authentication.");
276 /* create if needed */
282 for (cnt = 0; cnt < 14; cnt++) {
287 argv[U_NAME] = username;
289 argv[U_NAME] = UNIQUE_LOGIN;
293 argv[U_UID] = UNIQUE_UID;
295 argv[U_SHELL] = shell;
297 argv[U_SHELL] = "/bin/athena/tcsh";
299 argv[U_WINCONSOLESHELL] = winshell;
301 argv[U_WINCONSOLESHELL] = "cmd";
305 argv[U_FIRST] = first;
307 argv[U_MIDDLE] = middle;
309 argv[U_STATE] = u_status;
313 argv[U_MITID] = clearid;
315 argv[U_CLASS] = class;
317 argv[U_COMMENT] = comment;
318 /* Signature field always is the empty string */
319 argv[U_SIGNATURE] = "";
321 argv[U_SECURE] = secure;
323 argv[U_SECURE] = "0";
325 status = wrap_mr_query("add_user_account", 13, argv, NULL, NULL);
328 com_err(whoami, status, "while adding user account.");
332 else if (update_flag)
340 status = wrap_mr_query("get_user_account_by_login", 1, args,
341 save_query_info, old_argv);
344 com_err(whoami, status, "while getting user information.");
348 argv[1] = old_argv[0];
349 argv[2] = old_argv[1];
350 argv[3] = old_argv[2];
351 argv[4] = old_argv[3];
352 argv[5] = old_argv[4];
353 argv[6] = old_argv[5];
354 argv[7] = old_argv[6];
355 argv[8] = old_argv[7];
356 argv[9] = old_argv[8];
357 argv[10] = old_argv[9];
358 argv[11] = old_argv[10];
359 argv[12] = old_argv[11];
360 argv[13] = old_argv[12];
388 status = wrap_mr_query("update_user_account", 14, argv, NULL, NULL);
391 com_err(whoami, status, "while updating user.");
396 /* Deactivate a user, and the matching list and filesystem if they exist */
406 status = wrap_mr_query("update_user_status", 2, args, NULL, NULL);
409 com_err(whoami, status, "while deactivating user.");
413 status = wrap_mr_query("get_list_info", 1, args, save_query_info, argv);
414 if (status == MR_SUCCESS)
416 for (i = 13; i > 0; i--)
417 argv[i + 1] = argv[i];
419 argv[L_ACTIVE + 1] = "0";
421 status = wrap_mr_query("update_list", 14, argv, NULL, NULL);
424 com_err(whoami, status, "while updating list, "
425 "not deactivating list or filesystem.");
429 else if (status && status != MR_NO_MATCH)
431 com_err(whoami, status, "while retrieving list information.");
435 status = wrap_mr_query("get_filesys_by_label", 1, args, save_query_info,
437 if (status == MR_SUCCESS)
439 for (i = 11; i > 0; i--)
440 argv[i + 1] = argv[i];
442 argv[FS_TYPE + 1] = "ERR";
443 argv[FS_COMMENTS + 1] = "Locker disabled; call 3-1325 for help";
445 status = wrap_mr_query("update_filesys", 12, argv, NULL, NULL);
448 com_err(whoami, status, "while updating filesystem, "
449 "not deactivating filesystem.");
453 else if (status && status != MR_NO_MATCH)
455 com_err(whoami, status, "while retrieving filesystem information.");
460 /* Display user info */
467 status = wrap_mr_query("get_user_account_by_login", 1, args,
468 save_query_info, argv);
471 com_err(whoami, status, "while getting user information.");
474 if (unformatted_flag)
475 show_user_info_unformatted(argv);
477 show_user_info(argv);
480 /* register a user */
487 status = wrap_mr_query("get_user_account_by_login", 1, args,
488 save_query_info, argv);
491 com_err(whoami, status, "while looking up uid.");
495 args[0] = argv[U_UID];
499 status = wrap_mr_query("register_user", 3, args, NULL, NULL);
502 com_err(whoami, status, "while registering user.");
507 /* list user reservations */
513 status = wrap_mr_query("get_user_reservations", 1, args,
514 show_reservations, NULL);
516 if (status != MR_NO_MATCH) {
517 com_err(whoami, status, "while getting user reservations.");
522 /* add user reservations */
523 if (reservation_add_queue)
525 struct string_list *q = reservation_add_queue;
529 char *reservation = q->string;
533 args[1] = reservation;
534 status = wrap_mr_query("add_user_reservation", 2, args, NULL, NULL);
537 com_err(whoami, status, "while adding user reservation.");
545 /* delete user reservations */
546 if (reservation_remove_queue)
548 struct string_list *q = reservation_remove_queue;
552 char *reservation = q->string;
556 args[1] = reservation;
557 status = wrap_mr_query("delete_user_reservation", 2, args, NULL,
561 com_err(whoami, status, "while deleting user reservation.");
573 int save_query_info(int argc, char **argv, void *hint)
578 for(i = 0; i < argc; i++)
579 nargv[i] = strdup(argv[i]);
584 int show_reservations(int argc, char **argv, void *hint)
586 printf("Reservation: %s\n", argv[0]);
591 struct string_list *add_to_string_list(struct string_list *old_list, char *s) {
592 struct string_list *new_list;
594 new_list = (struct string_list *)malloc(sizeof(struct string_list *));
595 new_list->next = old_list;
596 new_list->string = s;
601 int wrap_mr_query(char *handle, int argc, char **argv,
602 int (*callback)(int, char **, void *), void *callarg) {
604 print_query(handle, argc, argv);
606 return mr_query(handle, argc, argv, callback, callarg);
609 void print_query(char *query_name, int argc, char **argv) {
612 printf("qy %s", query_name);
613 for(cnt = 0; cnt < argc; cnt++)
614 printf(" <%s>", argv[cnt]);
618 void show_user_info(char **argv)
623 sprintf(tbuf, "%s, %s %s", argv[U_LAST], argv[U_FIRST], argv[U_MIDDLE]);
624 printf("Login name: %-20s Full name: %s\n", argv[U_NAME], tbuf);
625 printf("User id: %-23s Login shell: %-10s\n", argv[U_UID], argv[U_SHELL]);
626 printf("Class: %-25s Windows Console Shell: %-10s\n", argv[U_CLASS],
627 argv[U_WINCONSOLESHELL]);
628 printf("Account is: %-20s MIT ID number: %s\n",
629 UserState(atoi(argv[U_STATE])), argv[U_MITID]);
630 status = atoi(argv[U_STATE]);
631 if (status == 0 || status == 2)
633 printf("User %s secure Account Coupon to register\n",
634 atoi(argv[U_SECURE]) ? "needs" : "does not need");
636 printf("Comments: %s\n", argv[U_COMMENT]);
637 printf("Created by %s on %s.\n", argv[U_CREATOR], argv[U_CREATED]);
638 printf("Last mod by %s at %s with %s.\n", argv[U_MODBY], argv[U_MODTIME],
642 void show_user_info_unformatted(char **argv)
646 printf("Login name: %s\n", argv[U_NAME]);
647 printf("Full name: %s, %s %s\n", argv[U_LAST], argv[U_FIRST],
649 printf("User id: %s\n", argv[U_UID]);
650 printf("Class: %s\n", argv[U_CLASS]);
651 printf("Login shell: %s\n", argv[U_SHELL]);
652 printf("Windows Console Shell: %s\n", argv[U_WINCONSOLESHELL]);
653 printf("Account is: %s\n", UserState(atoi(argv[U_STATE])));
654 printf("MIT ID number: %s\n", argv[U_MITID]);
655 status = atoi(argv[U_STATE]);
656 if (status == 0 || status == 2)
657 printf("Secure: %s secure Account Coupon to register\n",
658 atoi(argv[U_SECURE]) ? "Needs" : "Does not need");
659 printf("Comments: %s\n", argv[U_COMMENT]);
660 printf("Created by: %s\n", argv[U_CREATOR]);
661 printf("Created on: %s\n", argv[U_CREATED]);
662 printf("Last mod by: %s\n", argv[U_MODBY]);
663 printf("Last mod on: %s\n", argv[U_MODTIME]);
664 printf("Last mod with: %s\n", argv[U_MODWITH]);
667 void usage(char **argv)
669 #define USAGE_OPTIONS_FORMAT " %-39s%s\n"
670 fprintf(stderr, "Usage: %s username [options]\n", argv[0]);
671 fprintf(stderr, "Options are\n");
672 fprintf(stderr, " %-39s\n", "-i | -info");
673 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-C | -create",
675 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-r | -register",
676 "-R | -rename newname");
677 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-U | -uid uid",
678 "-s | -shell shell");
679 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-S | -status status",
680 "-w | -winshell winshell");
681 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-F | -first firstname",
682 "-L | -last lastname");
683 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-M | -middle middlename",
684 "-I | -mitid mitid");
685 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-cl | -class class",
686 "-c | -comment comment");
687 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-6 | -secure 0|1",
688 "-lr | -listreservation");
689 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-ar | -addreservation reservation",
690 "-dr | -deletereservation reservation");
691 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-u | -unformatted",
693 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-v | -verbose",
694 "-db | -database host[:port]");