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(buf, strlen(buf), info[U_SIGNATURE], &si);
129 sprintf(buf, "Account is: %-20s MIT ID number: %s Signed: %s",
130 UserState(atoi(info[U_STATE])), info[U_MITID],
131 *info[U_SIGNATURE] ? (status ? "Bad" : "Yes") : "No");
133 if (!atoi(info[U_STATE]))
135 sprintf(buf, "User %s secure Account Coupon to register",
136 atoi(info[U_SECURE]) ? "needs" : "does not need");
139 sprintf(buf, "Comments: %s", info[U_COMMENT]);
141 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME], info[U_MODWITH]);
145 /* Function Name: SetUserDefaults
146 * Description: Sets the default values for add user.
147 * Arguments: info - a blank user info array of char *'s.
148 * Returns: args - the filled info structure.
151 static char **SetUserDefaults(char **info)
153 info[U_NAME] = strdup(UNIQUE_LOGIN);
154 info[U_UID] = strdup(UNIQUE_UID);
155 info[U_SHELL] = strdup(DEFAULT_SHELL);
156 info[U_LAST] = strdup(DEFAULT_NONE);
157 info[U_FIRST] = strdup(DEFAULT_NONE);
158 info[U_MIDDLE] = strdup(DEFAULT_NONE);
159 info[U_STATE] = strdup(DEFAULT_NO);
160 info[U_MITID] = strdup(DEFAULT_NONE);
161 info[U_CLASS] = strdup(DEFAULT_CLASS);
162 info[U_COMMENT] = strdup("");
163 info[U_SIGNATURE] = strdup("");
164 info[U_SECURE] = strdup("0");
165 info[U_MODTIME] = info[U_MODBY] = info[U_MODWITH] = info[U_END] = NULL;
170 /* Check that the supplied name follows the capitalization rules, and
171 * offer to correct it if not.
174 void CorrectCapitalization(char **name)
176 char temp_buf[BUFSIZ], fixname[BUFSIZ];
178 strcpy(fixname, *name);
180 if (strcmp(fixname, *name))
182 Put_message("You entered a name which does not follow the capitalization conventions.");
183 sprintf(temp_buf, "Correct it to \"%s\"", fixname);
184 if (YesNoQuestion(temp_buf, 1) == TRUE)
187 *name = strdup(fixname);
193 /* Function Name: AskUserInfo.
194 * Description: This function askes the user for information about a
195 * machine and saves it into a structure.
196 * Arguments: info - a pointer the the structure to put the info into.
197 * flags - Flags asking us which info we want.
198 * Returns: the args to pass to the query.
199 * NOTES: the return args are not necessarily in the correct order to
200 * use the #defined names (e.g args[UID] is not the uid anymore).
203 char **AskUserInfo(char **info, Bool name)
209 char temp_buf[BUFSIZ], *newname;
213 sprintf(temp_buf, "\nChanging Attributes of user %s.\n", info[U_NAME]);
214 Put_message(temp_buf);
218 struct mqelem *elem = NULL;
221 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
223 CorrectCapitalization(&info[U_LAST]);
224 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
226 CorrectCapitalization(&info[U_FIRST]);
227 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) == SUB_ERROR)
229 CorrectCapitalization(&info[U_MIDDLE]);
230 argv[0] = info[U_FIRST];
231 argv[1] = info[U_LAST];
232 if (do_mr_query("get_user_account_by_name", 2, argv,
233 StoreInfo, &elem) == MR_SUCCESS)
235 Put_message("A user by that name already exists in the database.");
236 Loop(QueueTop(elem), PrintUserInfo);
237 Loop(QueueTop(elem), FreeInfo);
239 if (YesNoQuestion("Add new user anyway", TRUE) != TRUE)
245 newname = strdup(info[U_NAME]);
246 if (GetValueFromUser("The new login name for this user", &newname) ==
250 else if (GetValueFromUser("Login name for this user", &info[U_NAME]) ==
254 if (GetValueFromUser("User's UID", &info[U_UID]) == SUB_ERROR)
256 if (GetValueFromUser("User's shell", &info[U_SHELL]) == SUB_ERROR)
260 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
262 CorrectCapitalization(&info[U_LAST]);
263 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
265 CorrectCapitalization(&info[U_FIRST]);
266 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) == SUB_ERROR)
268 CorrectCapitalization(&info[U_MIDDLE]);
273 if (GetValueFromUser("User's status (? for help)", &info[U_STATE]) ==
276 if (isdigit(info[U_STATE][0]))
278 Put_message("Valid status numbers:");
279 for (i = 0; i < US_END; i++)
281 sprintf(temp_buf, " %d: %s", i, states[i]);
282 Put_message(temp_buf);
285 if (GetValueFromUser("User's MIT ID number", &info[U_MITID]) == SUB_ERROR)
287 RemoveHyphens(info[U_MITID]);
288 if (GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]) ==
291 if (GetValueFromUser("Comments", &info[U_COMMENT]) == SUB_ERROR)
296 if (YesNoQuestion("User needs secure Account Coupon to register",
297 atoi(info[U_SECURE]) ? TRUE : FALSE) == FALSE)
299 free(info[U_SECURE]);
300 info[U_SECURE] = strdup("0");
304 free(info[U_SECURE]);
305 info[U_SECURE] = strdup("1");
311 if (strcmp(info[U_NAME], UNIQUE_LOGIN))
314 sprintf(temp_buf, "%s:%s", newname, info[U_MITID]);
316 sprintf(temp_buf, "%s:%s", info[U_NAME], info[U_MITID]);
318 i = GDSS_Verify(temp_buf, strlen(temp_buf), info[U_SIGNATURE], &si);
319 /* If it's already signed OK, don't resign it. */
320 if (i != GDSS_SUCCESS)
322 free(info[U_SIGNATURE]);
323 info[U_SIGNATURE] = malloc(GDSS_Sig_Size() * 2);
325 i = GDSS_Sign(temp_buf, strlen(temp_buf), info[U_SIGNATURE]);
326 if (i != GDSS_SUCCESS)
327 com_err(program_name, gdss2et(i), "Failed to create signature");
330 unsigned char buf[256];
332 i = GDSS_Verify(temp_buf, strlen(temp_buf),
333 info[U_SIGNATURE], &si);
334 if (strlen(buf) > 68)
340 info[U_SIGNATURE] = strdup("");
343 FreeAndClear(&info[U_MODTIME], TRUE);
344 FreeAndClear(&info[U_MODBY], TRUE);
345 FreeAndClear(&info[U_MODWITH], TRUE);
348 * Slide the newname into the #2 slot, this screws up all future references
349 * to this list, since we slip the pointer into a info list it gets freed
350 * when the rest of the list gets freed.
353 SlipInNewName(info, newname);
358 /* Function Name: GetUserInfo
359 * Description: Stores the user information in a queue.
360 * Arguments: type - type of field given to get info, one of:
361 * LOGIN, UID, BY_NAME, CLASS.
362 * name1 - name of thing specified by type (wildcards okay)
363 * name2 - other name, only used in get user by first and last.
365 * Returns: the first element of the queue containing the user info.
369 struct mqelem *GetUserInfo(int type, char *name1, char *name2)
373 struct mqelem *elem = NULL;
379 if ((status = do_mr_query("get_user_account_by_login", 1, args,
382 com_err(program_name, status,
383 " when attempting to get_user_account_by_login.");
389 if ((status = do_mr_query("get_user_account_by_uid", 1, args,
392 com_err(program_name, status,
393 " when attempting to get_user_account_by_uid.");
400 if ((status = do_mr_query("get_user_account_by_name", 2, args,
403 com_err(program_name, status,
404 " when attempting to get_user_account_by_name.");
410 if ((status = do_mr_query("get_user_account_by_class", 1, args,
413 com_err(program_name, status,
414 " when attempting to get_user_account_by_class.");
420 if ((status = do_mr_query("get_user_account_by_id", 1, args,
423 com_err(program_name, status,
424 " when attempting to get_user_account_by_id.");
429 return QueueTop(elem) ;
432 /* Function Name: AddNewUser
433 * Description: Adds a new user to the database.
435 * Returns: DM_NORMAL.
438 int AddNewUser(int argc, char **argv)
441 char **args, *info[MAX_ARGS_SIZE];
443 if (!(args = AskUserInfo(SetUserDefaults(info), FALSE)))
445 Put_message("Aborted.");
448 if ((status = do_mr_query("add_user_account", CountArgs(args),
450 com_err(program_name, status, " in add_user_account");
452 Put_message("New user added to database.");
458 /* Function Name: GetLoginName
459 * Description: Asks the user for a login name and reserves
462 * Returns: a malloced login name for the user.
465 static char *GetLoginName(void)
470 if (GetValueFromUser("Login name for this user? ", &name) == SUB_ERROR)
472 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
477 /* Function Name: ChooseUser
478 * Description: Choose a user from a list and return the uid.
479 * Arguments: top - a queue of user information.
480 * Returns: uid - the malloced uid of the user that was chosen.
483 static char *ChooseUser(struct mqelem *elem)
487 char **info = elem->q_data;
489 switch (YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE))
492 return strdup(info[U_UID]);
495 default: /* quit or ^C. */
503 /* Function Name: GetUidNumberFromName
504 * Description: Gets the users uid number, from the name.
506 * Returns: uid - a malloced string containing the uid.
509 static char *GetUidNumberFromName(void)
511 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
513 struct mqelem *top = NULL;
515 if (!Prompt_input("First Name: ", first, BUFSIZ))
517 if (!Prompt_input("Last Name: ", last, BUFSIZ))
525 switch ((status = do_mr_query("get_user_account_by_name", 2, args,
531 Put_message("There is no user in the database with that name.");
534 com_err(program_name, status, " in get_account_user_by_name.");
539 if (QueueCount(top) == 1) /* This is a unique name. */
541 char **info = top->q_data;
542 Put_message("User ID Number retrieved for the user: ");
545 uid = strdup(info[U_UID]);
550 Put_message("That name is not unique, choose the user that you want.");
551 uid = ChooseUser(top);
556 /* Function Name: SetUserPassword
557 * Description: Set the new kerberos password for this user.
558 * Arguments: name - kerberos principle name for this user, (login name).
562 static void SetUserPassword(char *name)
564 name = name; /* make saber happy. */
565 Put_message("Kerberos password not changed, code non-existant.");
566 /* clever message to call account_admin, if this fails. */
569 /* Function Name: GiveBackLogin
570 * Description: Gives back previously reserved kerberous principle.
571 * Arguments: name - principle to give back.
575 static void GiveBackLogin(char *name)
577 name = name; /* make saber happy. */
578 Put_message("kerberos code not implimented, name not given back.");
579 /* send mail to db maintainer if this fails. */
582 /* Function Name: RegisterUser
583 * Description: This function registers a user.
585 * Returns: DM_NORMAL.
588 int RegisterUser(int argc, char **argv)
590 char *args[MAX_ARGS_SIZE];
591 char *login, *fstype = NULL;
592 char temp_buf[BUFSIZ];
595 Put_message("This function has NO kerberos support, so strange things");
596 Put_message("may happen if you use it to register a user.");
598 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE))
601 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
602 args[0] = strdup(temp_buf);
605 if (!(args[0] = GetUidNumberFromName()))
612 sprintf(temp_buf, "u%s", args[0]);
613 login = strdup(temp_buf);
614 if ((GetValueFromUser("Login name for this user? ", &login) == SUB_ERROR) ||
615 (GetFSTypes(&fstype, FALSE) == SUB_ERROR))
618 FreeInfo(args); /* This work because the NULL temination is ok. */
621 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
626 switch ((status = do_mr_query("register_user", CountArgs(args),
630 sprintf(temp_buf, "User %s successfully registered.", login);
631 Put_message(temp_buf);
632 SetUserPassword(login);
635 GiveBackLogin(login);
636 sprintf(temp_buf, "The username %s is already in use.", login);
637 Put_message(temp_buf);
640 com_err(program_name, status, " in register_user");
647 /* Function Name: RealUpdateUser
648 * Description: actuall updates the user information.
649 * Arguments: info - all current information for the user fields.
650 * junk - an UNUSED boolean.
654 static void RealUpdateUser(char **info, Bool junk)
657 char error_buf[BUFSIZ];
658 char **args = AskUserInfo(info, TRUE);
662 Put_message("Aborted.");
665 if ((status = do_mr_query("update_user_account", CountArgs(args),
668 com_err(program_name, status, " in ModifyFields");
669 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
670 Put_message(error_buf);
674 /* Function Name: UpdateUser
675 * Description: Modify some of the information about a user.
676 * Arguments: argc, argv - login name of the user in argv[1].
677 * Returns: DM_NORMAL.
680 int UpdateUser(int argc, char **argv)
684 elem = GetUserInfo(LOGIN, argv[1], NULL);
685 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
691 /* Function Name: RealDeactivateUser
692 * Description: sets the user's status to 3.
693 * Arguments: info - all current information for the user fields
694 * one_item - indicates the user hasn't been queried yet
698 static void RealDeactivateUser(char **info, Bool one_item)
701 char txt_buf[BUFSIZ];
702 char *qargs[2], **args;
703 struct mqelem *elem = NULL;
707 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
708 if (YesNoQuestion(txt_buf, FALSE) != TRUE)
712 qargs[0] = info[NAME];
714 if ((status = do_mr_query("update_user_status", 2, qargs, NULL, NULL)))
716 com_err(program_name, status, " in update_user_status");
717 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
718 Put_message(txt_buf);
720 else if (YesNoQuestion("Also deactivate matching list and filesystem (y/n)",
723 status = do_mr_query("get_list_info", 1, &(info[NAME]), StoreInfo,
725 if (status == MR_SUCCESS)
727 args = QueueTop(elem)->q_data;
728 free(args[L_ACTIVE]);
729 args[L_ACTIVE] = strdup("0");
730 FreeAndClear(&args[L_MODTIME], TRUE);
731 FreeAndClear(&args[L_MODBY], TRUE);
732 FreeAndClear(&args[L_MODWITH], TRUE);
733 SlipInNewName(args, args[L_NAME]);
734 if ((status = do_mr_query("update_list", CountArgs(args), args,
737 com_err(program_name, status, " updating list, "
738 "not deactivating list or filesystem");
747 else if (status != MR_NO_MATCH)
749 com_err(program_name, status, " getting list info, "
750 "not deactivating list or filesystem");
754 if ((status = do_mr_query("get_filesys_by_label", 1, &(info[NAME]),
757 com_err(program_name, status, " getting filsys info, "
758 "not deactivating filesystem");
761 args = QueueTop(elem)->q_data;
763 args[FS_TYPE] = strdup("ERR");
764 free(args[FS_COMMENTS]);
765 args[FS_COMMENTS] = strdup("Locker disabled; call 3-1325 for help");
766 FreeAndClear(&args[FS_MODTIME], TRUE);
767 FreeAndClear(&args[FS_MODBY], TRUE);
768 FreeAndClear(&args[FS_MODWITH], TRUE);
769 SlipInNewName(args, args[FS_NAME]);
770 if ((status = do_mr_query("update_filesys", CountArgs(args), args,
773 com_err(program_name, status, " updating filesystem, "
774 "not deactivating filesystem");
785 /* Function Name: DeactivateUser
786 * Description: sets the user's status to 3.
787 * Arguments: argc, argv - login name of the user in argv[1].
788 * Returns: DM_NORMAL.
791 int DeactivateUser(int argc, char **argv)
795 elem = GetUserInfo(LOGIN, argv[1], NULL);
796 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
803 /* ------------------------- Top Menu ------------------------- */
805 /* DeleteUser() in delete.c */
807 /* Function Name: DeleteUserByUid
808 * Description: Deletes the user given a uid number.
809 * Arguments: argc, argv - uid if user in argv[1].
810 * Returns: DM_NORMAL.
811 * NOTES: This just gets the username from the mr server
812 * and performs a DeleteUser().
815 int DeleteUserByUid(int argc, char **argv)
818 struct mqelem *elem = NULL;
821 if (!ValidName(argv[1]))
824 if ((status = do_mr_query("get_user_account_by_uid", 1, argv + 1, StoreInfo,
826 com_err(program_name, status, " in get_user_account_by_uid");
829 argv[1] = info[U_NAME];
831 DeleteUser(argc, argv);
835 /* ------------------------- Show User Information ------------------------- */
837 /* Function Name: ShowUserByLogin
838 * Description: Shows user information given a login name.
839 * Arguments: argc, argv - login name in argv[1].
843 int ShowUserByLogin(int argc, char *argv[])
845 struct mqelem *top, *elem;
847 elem = top = GetUserInfo(LOGIN, argv[1], NULL);
848 Loop(elem, PrintUserInfo);
854 /* Function Name: RetrieveUserByName
855 * Description: Show information on a user give fist and/or last name.
856 * Arguments: argc, argv - argv[1] - first name.
857 * argv[2] - last name.
858 * Returns: DM_NORMAL.
861 int ShowUserByName(int argc, char *argv[])
866 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
868 if (!top) /* if there was an error then return. */
871 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
879 Loop(top, PrintUserInfo);
883 Loop(top, PrintUserName);
891 /* Function Name: ShowUserByClass
892 * Description: Shows real and login names of all users in class.
893 * Arguments: argc, argv - argv[1] contains the class.
897 int ShowUserByClass(int argc, char **argv)
901 if (YesNoQuestion("This will take a long time. Are you sure", 0) == FALSE)
903 top = GetUserInfo(CLASS, argv[1], NULL);
904 Loop(top, PrintUserName);
911 /* Function Name: ShowUserById
912 * Description: Shows user information given an ID number.
913 * Arguments: argc, argv - ID number in argv[1].
917 int ShowUserById(int argc, char *argv[])
919 struct mqelem *top, *elem;
921 elem = top = GetUserInfo(ID, argv[1], NULL);
922 Loop(elem, PrintUserInfo);
929 /* Function Name: GetKrbmap
930 * Description: Shows user <-> Kerberos mappings
931 * Arguments: argc, argv - argv[1] contains the user login name,
932 * argv[2] contains the principal
936 int GetKrbmap(int argc, char **argv)
939 struct mqelem *elem = NULL, *top;
942 if ((stat = do_mr_query("get_kerberos_user_map", 2, &argv[1],
945 com_err(program_name, stat, " in GetKrbMap.");
949 top = elem = QueueTop(elem);
953 char **info = elem->q_data;
954 sprintf(buf, "User: %-9s Principal: %s",
955 info[KMAP_USER], info[KMAP_PRINCIPAL]);
960 FreeQueue(QueueTop(top));
965 /* Function Name: AddKrbmap
966 * Description: Add a new user <-> Kerberos mapping
967 * Arguments: argc, argv - argv[1] contains the user login name,
968 * argv[2] contains the principal
972 int AddKrbmap(int argc, char **argv)
976 if (!strchr(argv[KMAP_PRINCIPAL + 1], '@'))
978 Put_message("Please specify a realm for the kerberos principal.");
981 if ((stat = do_mr_query("add_kerberos_user_map", 2, &argv[1],
984 com_err(program_name, stat, " in AddKrbMap.");
985 if (stat == MR_EXISTS)
986 Put_message("No user or principal may have more than one mapping.");
992 /* Function Name: DeleteKrbmap
993 * Description: Remove a user <-> Kerberos mapping
994 * Arguments: argc, argv - argv[1] contains the user login name,
995 * argv[2] contains the principal
999 int DeleteKrbmap(int argc, char **argv)
1003 if ((stat = do_mr_query("delete_kerberos_user_map", 2, &argv[1],
1005 com_err(program_name, stat, " in DeleteKrbMap.");