3 * This is the file user.c for the Moira Client, which allows users
4 * to quickly and easily maintain most parts of the Moira database.
5 * It Contains: Functions for manipulating user information.
8 * By: Chris D. Peterson
10 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
11 * For copying and distribution information, please see the file
15 #include <mit-copyright.h>
17 #include <moira_site.h>
37 void CorrectCapitalization(char **name);
38 char **AskUserInfo(char **info, Bool name);
39 struct mqelem *GetUserInfo(int type, char *name1, char *name2);
48 #define DEFAULT_SHELL "/bin/athena/tcsh"
50 #define DEFAULT_SHELL "/bin/csh"
52 #define DEFAULT_CLASS "?"
55 /* Function Name: UserState
56 * Description: Convert a numeric state into a descriptive string.
57 * Arguments: state value
58 * Returns: pointer to statically allocated string.
61 static char *states[] = {
64 "Half Registered (2)",
66 "Not registerable (4)",
67 "Enrolled/Registerable (5)",
68 "Enrolled/Not Registerable (6)",
72 static char *UserState(int state)
74 static char buf[BUFSIZ];
76 if (state < 0 || state >= US_END)
78 sprintf(buf, "Unknown (%d)", state);
85 /* Function Name: PrintUserName
86 * Description: Print name of a user.
87 * Arguments: info - the information about a user.
91 static void PrintUserName(char **info)
93 char buf[BUFSIZ], print_buf[BUFSIZ];
94 sprintf(buf, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
95 sprintf(print_buf, "%-40s User Name: %s", buf, info[U_NAME]);
96 Put_message(print_buf);
99 /* Function Name: PrintUserInfo
100 * Description: Prints Information about a user.
101 * Arguments: info - an argument list with the user information
106 static void PrintUserInfo(char **info)
108 char name[BUFSIZ], buf[BUFSIZ];
114 sprintf(name, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
115 sprintf(buf, "Login name: %-20s Full name: %s", info[U_NAME], name);
117 sprintf(buf, "User id: %-23s Login shell %-10s Class: %s",
118 info[U_UID], info[U_SHELL], info[U_CLASS]);
122 sprintf(buf, "%s:%s", info[U_NAME], info[U_MITID]);
124 status = GDSS_Verify((unsigned char *)buf, strlen(buf),
125 (unsigned char *)info[U_SIGNATURE], &si);
130 sprintf(buf, "Account is: %-20s MIT ID number: %s Signed: %s",
131 UserState(atoi(info[U_STATE])), info[U_MITID],
132 *info[U_SIGNATURE] ? (status ? "Bad" : "Yes") : "No");
134 if (!atoi(info[U_STATE]))
136 sprintf(buf, "User %s secure Account Coupon to register",
137 atoi(info[U_SECURE]) ? "needs" : "does not need");
140 sprintf(buf, "Comments: %s", info[U_COMMENT]);
142 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME], info[U_MODWITH]);
146 /* Function Name: SetUserDefaults
147 * Description: Sets the default values for add user.
148 * Arguments: info - a blank user info array of char *'s.
149 * Returns: args - the filled info structure.
152 static char **SetUserDefaults(char **info)
154 info[U_NAME] = strdup(UNIQUE_LOGIN);
155 info[U_UID] = strdup(UNIQUE_UID);
156 info[U_SHELL] = strdup(DEFAULT_SHELL);
157 info[U_LAST] = strdup(DEFAULT_NONE);
158 info[U_FIRST] = strdup(DEFAULT_NONE);
159 info[U_MIDDLE] = strdup(DEFAULT_NONE);
160 info[U_STATE] = strdup(DEFAULT_NO);
161 info[U_MITID] = strdup(DEFAULT_NONE);
162 info[U_CLASS] = strdup(DEFAULT_CLASS);
163 info[U_COMMENT] = strdup("");
164 info[U_SIGNATURE] = strdup("");
165 info[U_SECURE] = strdup("0");
166 info[U_MODTIME] = info[U_MODBY] = info[U_MODWITH] = info[U_END] = NULL;
171 /* Check that the supplied name follows the capitalization rules, and
172 * offer to correct it if not.
175 void CorrectCapitalization(char **name)
177 char temp_buf[BUFSIZ], fixname[BUFSIZ];
179 strcpy(fixname, *name);
181 if (strcmp(fixname, *name))
183 Put_message("You entered a name which does not follow the capitalization conventions.");
184 sprintf(temp_buf, "Correct it to \"%s\"", fixname);
185 if (YesNoQuestion(temp_buf, 1) == TRUE)
188 *name = strdup(fixname);
194 /* Function Name: AskUserInfo.
195 * Description: This function askes the user for information about a
196 * machine and saves it into a structure.
197 * Arguments: info - a pointer the the structure to put the info into.
198 * flags - Flags asking us which info we want.
199 * Returns: the args to pass to the query.
200 * NOTES: the return args are not necessarily in the correct order to
201 * use the #defined names (e.g args[UID] is not the uid anymore).
204 char **AskUserInfo(char **info, Bool name)
210 char temp_buf[BUFSIZ], *newname;
214 sprintf(temp_buf, "\nChanging Attributes of user %s.\n", info[U_NAME]);
215 Put_message(temp_buf);
219 struct mqelem *elem = NULL;
222 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
224 CorrectCapitalization(&info[U_LAST]);
225 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
227 CorrectCapitalization(&info[U_FIRST]);
228 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) == SUB_ERROR)
230 CorrectCapitalization(&info[U_MIDDLE]);
231 argv[0] = info[U_FIRST];
232 argv[1] = info[U_LAST];
233 if (do_mr_query("get_user_account_by_name", 2, argv,
234 StoreInfo, &elem) == MR_SUCCESS)
236 Put_message("A user by that name already exists in the database.");
237 Loop(QueueTop(elem), PrintUserInfo);
238 Loop(QueueTop(elem), FreeInfo);
240 if (YesNoQuestion("Add new user anyway", TRUE) != TRUE)
246 newname = strdup(info[U_NAME]);
247 if (GetValueFromUser("The new login name for this user", &newname) ==
251 else if (GetValueFromUser("Login name for this user", &info[U_NAME]) ==
255 strcpy(temp_buf, info[U_UID]);
256 if (GetValueFromUser("User's UID", &info[U_UID]) == SUB_ERROR)
258 if (strcmp(info[U_UID], UNIQUE_UID) && strcmp(info[U_UID], temp_buf))
260 struct mqelem *elem = NULL;
261 if (do_mr_query("get_user_account_by_uid", 1, &info[U_UID],
262 StoreInfo, &elem) == MR_SUCCESS)
264 Put_message("A user with that uid already exists in the database.");
265 Loop(QueueTop(elem), PrintUserInfo);
266 Loop(QueueTop(elem), FreeInfo);
268 if (YesNoQuestion("Add new user anyway", TRUE) != TRUE)
273 if (GetValueFromUser("User's shell", &info[U_SHELL]) == SUB_ERROR)
277 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
279 CorrectCapitalization(&info[U_LAST]);
280 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
282 CorrectCapitalization(&info[U_FIRST]);
283 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) == SUB_ERROR)
285 CorrectCapitalization(&info[U_MIDDLE]);
290 if (GetValueFromUser("User's status (? for help)", &info[U_STATE]) ==
293 if (isdigit(info[U_STATE][0]))
295 Put_message("Valid status numbers:");
296 for (i = 0; i < US_END; i++)
298 sprintf(temp_buf, " %d: %s", i, states[i]);
299 Put_message(temp_buf);
302 if (GetValueFromUser("User's MIT ID number", &info[U_MITID]) == SUB_ERROR)
304 RemoveHyphens(info[U_MITID]);
305 if (GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]) ==
308 if (GetValueFromUser("Comments", &info[U_COMMENT]) == SUB_ERROR)
311 if (!name || !atoi(info[U_STATE]))
313 if (YesNoQuestion("User needs secure Account Coupon to register",
314 atoi(info[U_SECURE]) ? TRUE : FALSE) == FALSE)
316 free(info[U_SECURE]);
317 info[U_SECURE] = strdup("0");
321 free(info[U_SECURE]);
322 info[U_SECURE] = strdup("1");
328 if (strcmp(info[U_NAME], UNIQUE_LOGIN))
331 sprintf(temp_buf, "%s:%s", newname, info[U_MITID]);
333 sprintf(temp_buf, "%s:%s", info[U_NAME], info[U_MITID]);
335 i = GDSS_Verify((unsigned char *)temp_buf, strlen(temp_buf),
336 (unsigned char *)info[U_SIGNATURE], &si);
337 /* If it's already signed OK, don't resign it. */
338 if (i != GDSS_SUCCESS)
340 free(info[U_SIGNATURE]);
341 info[U_SIGNATURE] = malloc(GDSS_Sig_Size() * 2);
343 i = GDSS_Sign((unsigned char *)temp_buf, strlen(temp_buf),
344 (unsigned char *)info[U_SIGNATURE]);
345 if (i != GDSS_SUCCESS)
346 com_err(program_name, gdss2et(i), "Failed to create signature");
349 unsigned char buf[256];
351 i = GDSS_Verify((unsigned char *)temp_buf, strlen(temp_buf),
352 (unsigned char *)info[U_SIGNATURE], &si);
353 if (strlen((char *)buf) > 68)
359 info[U_SIGNATURE] = strdup("");
362 FreeAndClear(&info[U_MODTIME], TRUE);
363 FreeAndClear(&info[U_MODBY], TRUE);
364 FreeAndClear(&info[U_MODWITH], TRUE);
367 * Slide the newname into the #2 slot, this screws up all future references
368 * to this list, since we slip the pointer into a info list it gets freed
369 * when the rest of the list gets freed.
372 SlipInNewName(info, newname);
377 /* Function Name: GetUserInfo
378 * Description: Stores the user information in a queue.
379 * Arguments: type - type of field given to get info, one of:
380 * LOGIN, UID, BY_NAME, CLASS.
381 * name1 - name of thing specified by type (wildcards okay)
382 * name2 - other name, only used in get user by first and last.
384 * Returns: the first element of the queue containing the user info.
388 struct mqelem *GetUserInfo(int type, char *name1, char *name2)
392 struct mqelem *elem = NULL;
398 if ((status = do_mr_query("get_user_account_by_login", 1, args,
401 com_err(program_name, status,
402 " when attempting to get_user_account_by_login.");
408 if ((status = do_mr_query("get_user_account_by_uid", 1, args,
411 com_err(program_name, status,
412 " when attempting to get_user_account_by_uid.");
419 if ((status = do_mr_query("get_user_account_by_name", 2, args,
422 com_err(program_name, status,
423 " when attempting to get_user_account_by_name.");
429 if ((status = do_mr_query("get_user_account_by_class", 1, args,
432 com_err(program_name, status,
433 " when attempting to get_user_account_by_class.");
439 if ((status = do_mr_query("get_user_account_by_id", 1, args,
442 com_err(program_name, status,
443 " when attempting to get_user_account_by_id.");
448 return QueueTop(elem) ;
451 /* Function Name: AddNewUser
452 * Description: Adds a new user to the database.
454 * Returns: DM_NORMAL.
457 int AddNewUser(int argc, char **argv)
460 char **args, *info[MAX_ARGS_SIZE];
462 if (!(args = AskUserInfo(SetUserDefaults(info), FALSE)))
464 Put_message("Aborted.");
467 if ((status = do_mr_query("add_user_account", CountArgs(args),
469 com_err(program_name, status, " in add_user_account");
471 Put_message("New user added to database.");
477 /* Function Name: GetLoginName
478 * Description: Asks the user for a login name and reserves
481 * Returns: a malloced login name for the user.
484 static char *GetLoginName(void)
489 if (GetValueFromUser("Login name for this user? ", &name) == SUB_ERROR)
491 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
496 /* Function Name: ChooseUser
497 * Description: Choose a user from a list and return the uid.
498 * Arguments: top - a queue of user information.
499 * Returns: uid - the malloced uid of the user that was chosen.
502 static char *ChooseUser(struct mqelem *elem)
506 char **info = elem->q_data;
508 switch (YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE))
511 return strdup(info[U_UID]);
514 default: /* quit or ^C. */
522 /* Function Name: GetUidNumberFromName
523 * Description: Gets the users uid number, from the name.
525 * Returns: uid - a malloced string containing the uid.
528 static char *GetUidNumberFromName(void)
530 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
532 struct mqelem *top = NULL;
534 if (!Prompt_input("First Name: ", first, BUFSIZ))
536 if (!Prompt_input("Last Name: ", last, BUFSIZ))
544 switch ((status = do_mr_query("get_user_account_by_name", 2, args,
550 Put_message("There is no user in the database with that name.");
553 com_err(program_name, status, " in get_account_user_by_name.");
558 if (QueueCount(top) == 1) /* This is a unique name. */
560 char **info = top->q_data;
561 Put_message("User ID Number retrieved for the user: ");
564 uid = strdup(info[U_UID]);
569 Put_message("That name is not unique, choose the user that you want.");
570 uid = ChooseUser(top);
575 /* Function Name: SetUserPassword
576 * Description: Set the new kerberos password for this user.
577 * Arguments: name - kerberos principle name for this user, (login name).
581 static void SetUserPassword(char *name)
583 name = name; /* make saber happy. */
584 Put_message("Kerberos password not changed, code non-existant.");
585 /* clever message to call account_admin, if this fails. */
588 /* Function Name: GiveBackLogin
589 * Description: Gives back previously reserved kerberous principle.
590 * Arguments: name - principle to give back.
594 static void GiveBackLogin(char *name)
596 name = name; /* make saber happy. */
597 Put_message("kerberos code not implimented, name not given back.");
598 /* send mail to db maintainer if this fails. */
601 /* Function Name: RegisterUser
602 * Description: This function registers a user.
604 * Returns: DM_NORMAL.
607 int RegisterUser(int argc, char **argv)
609 char *args[MAX_ARGS_SIZE];
610 char *login, *fstype = NULL;
611 char temp_buf[BUFSIZ];
614 Put_message("This function has NO kerberos support, so strange things");
615 Put_message("may happen if you use it to register a user.");
617 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE))
620 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
621 args[0] = strdup(temp_buf);
624 if (!(args[0] = GetUidNumberFromName()))
631 sprintf(temp_buf, "u%s", args[0]);
632 login = strdup(temp_buf);
633 if ((GetValueFromUser("Login name for this user? ", &login) == SUB_ERROR) ||
634 (GetFSTypes(&fstype, FALSE) == SUB_ERROR))
637 FreeInfo(args); /* This work because the NULL temination is ok. */
640 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
645 switch ((status = do_mr_query("register_user", CountArgs(args),
649 sprintf(temp_buf, "User %s successfully registered.", login);
650 Put_message(temp_buf);
651 SetUserPassword(login);
654 GiveBackLogin(login);
655 sprintf(temp_buf, "The username %s is already in use.", login);
656 Put_message(temp_buf);
659 com_err(program_name, status, " in register_user");
666 /* Function Name: RealUpdateUser
667 * Description: actuall updates the user information.
668 * Arguments: info - all current information for the user fields.
669 * junk - an UNUSED boolean.
673 static void RealUpdateUser(char **info, Bool junk)
676 char error_buf[BUFSIZ];
677 char **args = AskUserInfo(info, TRUE);
681 Put_message("Aborted.");
684 if ((status = do_mr_query("update_user_account", CountArgs(args),
687 com_err(program_name, status, " in ModifyFields");
688 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
689 Put_message(error_buf);
693 /* Function Name: UpdateUser
694 * Description: Modify some of the information about a user.
695 * Arguments: argc, argv - login name of the user in argv[1].
696 * Returns: DM_NORMAL.
699 int UpdateUser(int argc, char **argv)
703 elem = GetUserInfo(LOGIN, argv[1], NULL);
704 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
710 /* Function Name: RealDeactivateUser
711 * Description: sets the user's status to 3.
712 * Arguments: info - all current information for the user fields
713 * one_item - indicates the user hasn't been queried yet
717 static void RealDeactivateUser(char **info, Bool one_item)
720 char txt_buf[BUFSIZ];
721 char *qargs[2], **args;
722 struct mqelem *elem = NULL;
726 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
727 if (YesNoQuestion(txt_buf, FALSE) != TRUE)
731 qargs[0] = info[NAME];
733 if ((status = do_mr_query("update_user_status", 2, qargs, NULL, NULL)))
735 com_err(program_name, status, " in update_user_status");
736 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
737 Put_message(txt_buf);
739 else if (YesNoQuestion("Also deactivate matching list and filesystem (y/n)",
742 status = do_mr_query("get_list_info", 1, &(info[NAME]), StoreInfo,
744 if (status == MR_SUCCESS)
746 args = QueueTop(elem)->q_data;
747 free(args[L_ACTIVE]);
748 args[L_ACTIVE] = strdup("0");
749 FreeAndClear(&args[L_MODTIME], TRUE);
750 FreeAndClear(&args[L_MODBY], TRUE);
751 FreeAndClear(&args[L_MODWITH], TRUE);
752 SlipInNewName(args, args[L_NAME]);
753 if ((status = do_mr_query("update_list", CountArgs(args), args,
756 com_err(program_name, status, " updating list, "
757 "not deactivating list or filesystem");
766 else if (status != MR_NO_MATCH)
768 com_err(program_name, status, " getting list info, "
769 "not deactivating list or filesystem");
773 if ((status = do_mr_query("get_filesys_by_label", 1, &(info[NAME]),
776 com_err(program_name, status, " getting filsys info, "
777 "not deactivating filesystem");
780 args = QueueTop(elem)->q_data;
782 args[FS_TYPE] = strdup("ERR");
783 free(args[FS_COMMENTS]);
784 args[FS_COMMENTS] = strdup("Locker disabled; call 3-1325 for help");
785 FreeAndClear(&args[FS_MODTIME], TRUE);
786 FreeAndClear(&args[FS_MODBY], TRUE);
787 FreeAndClear(&args[FS_MODWITH], TRUE);
788 SlipInNewName(args, args[FS_NAME]);
789 if ((status = do_mr_query("update_filesys", CountArgs(args), args,
792 com_err(program_name, status, " updating filesystem, "
793 "not deactivating filesystem");
804 /* Function Name: DeactivateUser
805 * Description: sets the user's status to 3.
806 * Arguments: argc, argv - login name of the user in argv[1].
807 * Returns: DM_NORMAL.
810 int DeactivateUser(int argc, char **argv)
814 elem = GetUserInfo(LOGIN, argv[1], NULL);
815 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
822 /* ------------------------- Top Menu ------------------------- */
824 /* DeleteUser() in delete.c */
826 /* Function Name: DeleteUserByUid
827 * Description: Deletes the user given a uid number.
828 * Arguments: argc, argv - uid if user in argv[1].
829 * Returns: DM_NORMAL.
830 * NOTES: This just gets the username from the mr server
831 * and performs a DeleteUser().
834 int DeleteUserByUid(int argc, char **argv)
837 struct mqelem *elem = NULL;
840 if (!ValidName(argv[1]))
843 if ((status = do_mr_query("get_user_account_by_uid", 1, argv + 1, StoreInfo,
845 com_err(program_name, status, " in get_user_account_by_uid");
848 argv[1] = info[U_NAME];
850 DeleteUser(argc, argv);
854 /* ------------------------- Show User Information ------------------------- */
856 /* Function Name: ShowUserByLogin
857 * Description: Shows user information given a login name.
858 * Arguments: argc, argv - login name in argv[1].
862 int ShowUserByLogin(int argc, char *argv[])
864 struct mqelem *top, *elem;
866 elem = top = GetUserInfo(LOGIN, argv[1], NULL);
867 Loop(elem, PrintUserInfo);
873 /* Function Name: RetrieveUserByName
874 * Description: Show information on a user give fist and/or last name.
875 * Arguments: argc, argv - argv[1] - first name.
876 * argv[2] - last name.
877 * Returns: DM_NORMAL.
880 int ShowUserByName(int argc, char *argv[])
885 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
887 if (!top) /* if there was an error then return. */
890 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
898 Loop(top, PrintUserInfo);
902 Loop(top, PrintUserName);
910 /* Function Name: ShowUserByClass
911 * Description: Shows real and login names of all users in class.
912 * Arguments: argc, argv - argv[1] contains the class.
916 int ShowUserByClass(int argc, char **argv)
920 if (YesNoQuestion("This will take a long time. Are you sure", 0) == FALSE)
922 top = GetUserInfo(CLASS, argv[1], NULL);
923 Loop(top, PrintUserName);
930 /* Function Name: ShowUserById
931 * Description: Shows user information given an ID number.
932 * Arguments: argc, argv - ID number in argv[1].
936 int ShowUserById(int argc, char *argv[])
938 struct mqelem *top, *elem;
940 elem = top = GetUserInfo(ID, argv[1], NULL);
941 Loop(elem, PrintUserInfo);
948 /* Function Name: GetKrbmap
949 * Description: Shows user <-> Kerberos mappings
950 * Arguments: argc, argv - argv[1] contains the user login name,
951 * argv[2] contains the principal
955 int GetKrbmap(int argc, char **argv)
958 struct mqelem *elem = NULL, *top;
961 if ((stat = do_mr_query("get_kerberos_user_map", 2, &argv[1],
964 com_err(program_name, stat, " in GetKrbMap.");
968 top = elem = QueueTop(elem);
972 char **info = elem->q_data;
973 sprintf(buf, "User: %-9s Principal: %s",
974 info[KMAP_USER], info[KMAP_PRINCIPAL]);
979 FreeQueue(QueueTop(top));
984 /* Function Name: AddKrbmap
985 * Description: Add a new user <-> Kerberos mapping
986 * Arguments: argc, argv - argv[1] contains the user login name,
987 * argv[2] contains the principal
991 int AddKrbmap(int argc, char **argv)
995 if (!strchr(argv[KMAP_PRINCIPAL + 1], '@'))
997 Put_message("Please specify a realm for the kerberos principal.");
1000 if ((stat = do_mr_query("add_kerberos_user_map", 2, &argv[1],
1003 com_err(program_name, stat, " in AddKrbMap.");
1004 if (stat == MR_EXISTS)
1005 Put_message("No user or principal may have more than one mapping.");
1011 /* Function Name: DeleteKrbmap
1012 * Description: Remove a user <-> Kerberos mapping
1013 * Arguments: argc, argv - argv[1] contains the user login name,
1014 * argv[2] contains the principal
1018 int DeleteKrbmap(int argc, char **argv)
1022 if ((stat = do_mr_query("delete_kerberos_user_map", 2, &argv[1],
1024 com_err(program_name, stat, " in DeleteKrbMap.");