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 strcpy(temp_buf, info[U_UID]);
255 if (GetValueFromUser("User's UID", &info[U_UID]) == SUB_ERROR)
257 if (strcmp(info[U_UID], UNIQUE_UID) && strcmp(info[U_UID], temp_buf))
259 struct mqelem *elem = NULL;
260 if (do_mr_query("get_user_account_by_uid", 1, &info[U_UID],
261 StoreInfo, &elem) == MR_SUCCESS)
263 Put_message("A user with that uid already exists in the database.");
264 Loop(QueueTop(elem), PrintUserInfo);
265 Loop(QueueTop(elem), FreeInfo);
267 if (YesNoQuestion("Add new user anyway", TRUE) != TRUE)
272 if (GetValueFromUser("User's shell", &info[U_SHELL]) == SUB_ERROR)
276 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
278 CorrectCapitalization(&info[U_LAST]);
279 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
281 CorrectCapitalization(&info[U_FIRST]);
282 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) == SUB_ERROR)
284 CorrectCapitalization(&info[U_MIDDLE]);
289 if (GetValueFromUser("User's status (? for help)", &info[U_STATE]) ==
292 if (isdigit(info[U_STATE][0]))
294 Put_message("Valid status numbers:");
295 for (i = 0; i < US_END; i++)
297 sprintf(temp_buf, " %d: %s", i, states[i]);
298 Put_message(temp_buf);
301 if (GetValueFromUser("User's MIT ID number", &info[U_MITID]) == SUB_ERROR)
303 RemoveHyphens(info[U_MITID]);
304 if (GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]) ==
307 if (GetValueFromUser("Comments", &info[U_COMMENT]) == SUB_ERROR)
310 if (!name || !atoi(info[U_STATE]))
312 if (YesNoQuestion("User needs secure Account Coupon to register",
313 atoi(info[U_SECURE]) ? TRUE : FALSE) == FALSE)
315 free(info[U_SECURE]);
316 info[U_SECURE] = strdup("0");
320 free(info[U_SECURE]);
321 info[U_SECURE] = strdup("1");
327 if (strcmp(info[U_NAME], UNIQUE_LOGIN))
330 sprintf(temp_buf, "%s:%s", newname, info[U_MITID]);
332 sprintf(temp_buf, "%s:%s", info[U_NAME], info[U_MITID]);
334 i = GDSS_Verify(temp_buf, strlen(temp_buf), info[U_SIGNATURE], &si);
335 /* If it's already signed OK, don't resign it. */
336 if (i != GDSS_SUCCESS)
338 free(info[U_SIGNATURE]);
339 info[U_SIGNATURE] = malloc(GDSS_Sig_Size() * 2);
341 i = GDSS_Sign(temp_buf, strlen(temp_buf), info[U_SIGNATURE]);
342 if (i != GDSS_SUCCESS)
343 com_err(program_name, gdss2et(i), "Failed to create signature");
346 unsigned char buf[256];
348 i = GDSS_Verify(temp_buf, strlen(temp_buf),
349 info[U_SIGNATURE], &si);
350 if (strlen(buf) > 68)
356 info[U_SIGNATURE] = strdup("");
359 FreeAndClear(&info[U_MODTIME], TRUE);
360 FreeAndClear(&info[U_MODBY], TRUE);
361 FreeAndClear(&info[U_MODWITH], TRUE);
364 * Slide the newname into the #2 slot, this screws up all future references
365 * to this list, since we slip the pointer into a info list it gets freed
366 * when the rest of the list gets freed.
369 SlipInNewName(info, newname);
374 /* Function Name: GetUserInfo
375 * Description: Stores the user information in a queue.
376 * Arguments: type - type of field given to get info, one of:
377 * LOGIN, UID, BY_NAME, CLASS.
378 * name1 - name of thing specified by type (wildcards okay)
379 * name2 - other name, only used in get user by first and last.
381 * Returns: the first element of the queue containing the user info.
385 struct mqelem *GetUserInfo(int type, char *name1, char *name2)
389 struct mqelem *elem = NULL;
395 if ((status = do_mr_query("get_user_account_by_login", 1, args,
398 com_err(program_name, status,
399 " when attempting to get_user_account_by_login.");
405 if ((status = do_mr_query("get_user_account_by_uid", 1, args,
408 com_err(program_name, status,
409 " when attempting to get_user_account_by_uid.");
416 if ((status = do_mr_query("get_user_account_by_name", 2, args,
419 com_err(program_name, status,
420 " when attempting to get_user_account_by_name.");
426 if ((status = do_mr_query("get_user_account_by_class", 1, args,
429 com_err(program_name, status,
430 " when attempting to get_user_account_by_class.");
436 if ((status = do_mr_query("get_user_account_by_id", 1, args,
439 com_err(program_name, status,
440 " when attempting to get_user_account_by_id.");
445 return QueueTop(elem) ;
448 /* Function Name: AddNewUser
449 * Description: Adds a new user to the database.
451 * Returns: DM_NORMAL.
454 int AddNewUser(int argc, char **argv)
457 char **args, *info[MAX_ARGS_SIZE];
459 if (!(args = AskUserInfo(SetUserDefaults(info), FALSE)))
461 Put_message("Aborted.");
464 if ((status = do_mr_query("add_user_account", CountArgs(args),
466 com_err(program_name, status, " in add_user_account");
468 Put_message("New user added to database.");
474 /* Function Name: GetLoginName
475 * Description: Asks the user for a login name and reserves
478 * Returns: a malloced login name for the user.
481 static char *GetLoginName(void)
486 if (GetValueFromUser("Login name for this user? ", &name) == SUB_ERROR)
488 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
493 /* Function Name: ChooseUser
494 * Description: Choose a user from a list and return the uid.
495 * Arguments: top - a queue of user information.
496 * Returns: uid - the malloced uid of the user that was chosen.
499 static char *ChooseUser(struct mqelem *elem)
503 char **info = elem->q_data;
505 switch (YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE))
508 return strdup(info[U_UID]);
511 default: /* quit or ^C. */
519 /* Function Name: GetUidNumberFromName
520 * Description: Gets the users uid number, from the name.
522 * Returns: uid - a malloced string containing the uid.
525 static char *GetUidNumberFromName(void)
527 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
529 struct mqelem *top = NULL;
531 if (!Prompt_input("First Name: ", first, BUFSIZ))
533 if (!Prompt_input("Last Name: ", last, BUFSIZ))
541 switch ((status = do_mr_query("get_user_account_by_name", 2, args,
547 Put_message("There is no user in the database with that name.");
550 com_err(program_name, status, " in get_account_user_by_name.");
555 if (QueueCount(top) == 1) /* This is a unique name. */
557 char **info = top->q_data;
558 Put_message("User ID Number retrieved for the user: ");
561 uid = strdup(info[U_UID]);
566 Put_message("That name is not unique, choose the user that you want.");
567 uid = ChooseUser(top);
572 /* Function Name: SetUserPassword
573 * Description: Set the new kerberos password for this user.
574 * Arguments: name - kerberos principle name for this user, (login name).
578 static void SetUserPassword(char *name)
580 name = name; /* make saber happy. */
581 Put_message("Kerberos password not changed, code non-existant.");
582 /* clever message to call account_admin, if this fails. */
585 /* Function Name: GiveBackLogin
586 * Description: Gives back previously reserved kerberous principle.
587 * Arguments: name - principle to give back.
591 static void GiveBackLogin(char *name)
593 name = name; /* make saber happy. */
594 Put_message("kerberos code not implimented, name not given back.");
595 /* send mail to db maintainer if this fails. */
598 /* Function Name: RegisterUser
599 * Description: This function registers a user.
601 * Returns: DM_NORMAL.
604 int RegisterUser(int argc, char **argv)
606 char *args[MAX_ARGS_SIZE];
607 char *login, *fstype = NULL;
608 char temp_buf[BUFSIZ];
611 Put_message("This function has NO kerberos support, so strange things");
612 Put_message("may happen if you use it to register a user.");
614 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE))
617 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
618 args[0] = strdup(temp_buf);
621 if (!(args[0] = GetUidNumberFromName()))
628 sprintf(temp_buf, "u%s", args[0]);
629 login = strdup(temp_buf);
630 if ((GetValueFromUser("Login name for this user? ", &login) == SUB_ERROR) ||
631 (GetFSTypes(&fstype, FALSE) == SUB_ERROR))
634 FreeInfo(args); /* This work because the NULL temination is ok. */
637 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
642 switch ((status = do_mr_query("register_user", CountArgs(args),
646 sprintf(temp_buf, "User %s successfully registered.", login);
647 Put_message(temp_buf);
648 SetUserPassword(login);
651 GiveBackLogin(login);
652 sprintf(temp_buf, "The username %s is already in use.", login);
653 Put_message(temp_buf);
656 com_err(program_name, status, " in register_user");
663 /* Function Name: RealUpdateUser
664 * Description: actuall updates the user information.
665 * Arguments: info - all current information for the user fields.
666 * junk - an UNUSED boolean.
670 static void RealUpdateUser(char **info, Bool junk)
673 char error_buf[BUFSIZ];
674 char **args = AskUserInfo(info, TRUE);
678 Put_message("Aborted.");
681 if ((status = do_mr_query("update_user_account", CountArgs(args),
684 com_err(program_name, status, " in ModifyFields");
685 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
686 Put_message(error_buf);
690 /* Function Name: UpdateUser
691 * Description: Modify some of the information about a user.
692 * Arguments: argc, argv - login name of the user in argv[1].
693 * Returns: DM_NORMAL.
696 int UpdateUser(int argc, char **argv)
700 elem = GetUserInfo(LOGIN, argv[1], NULL);
701 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
707 /* Function Name: RealDeactivateUser
708 * Description: sets the user's status to 3.
709 * Arguments: info - all current information for the user fields
710 * one_item - indicates the user hasn't been queried yet
714 static void RealDeactivateUser(char **info, Bool one_item)
717 char txt_buf[BUFSIZ];
718 char *qargs[2], **args;
719 struct mqelem *elem = NULL;
723 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
724 if (YesNoQuestion(txt_buf, FALSE) != TRUE)
728 qargs[0] = info[NAME];
730 if ((status = do_mr_query("update_user_status", 2, qargs, NULL, NULL)))
732 com_err(program_name, status, " in update_user_status");
733 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
734 Put_message(txt_buf);
736 else if (YesNoQuestion("Also deactivate matching list and filesystem (y/n)",
739 status = do_mr_query("get_list_info", 1, &(info[NAME]), StoreInfo,
741 if (status == MR_SUCCESS)
743 args = QueueTop(elem)->q_data;
744 free(args[L_ACTIVE]);
745 args[L_ACTIVE] = strdup("0");
746 FreeAndClear(&args[L_MODTIME], TRUE);
747 FreeAndClear(&args[L_MODBY], TRUE);
748 FreeAndClear(&args[L_MODWITH], TRUE);
749 SlipInNewName(args, args[L_NAME]);
750 if ((status = do_mr_query("update_list", CountArgs(args), args,
753 com_err(program_name, status, " updating list, "
754 "not deactivating list or filesystem");
763 else if (status != MR_NO_MATCH)
765 com_err(program_name, status, " getting list info, "
766 "not deactivating list or filesystem");
770 if ((status = do_mr_query("get_filesys_by_label", 1, &(info[NAME]),
773 com_err(program_name, status, " getting filsys info, "
774 "not deactivating filesystem");
777 args = QueueTop(elem)->q_data;
779 args[FS_TYPE] = strdup("ERR");
780 free(args[FS_COMMENTS]);
781 args[FS_COMMENTS] = strdup("Locker disabled; call 3-1325 for help");
782 FreeAndClear(&args[FS_MODTIME], TRUE);
783 FreeAndClear(&args[FS_MODBY], TRUE);
784 FreeAndClear(&args[FS_MODWITH], TRUE);
785 SlipInNewName(args, args[FS_NAME]);
786 if ((status = do_mr_query("update_filesys", CountArgs(args), args,
789 com_err(program_name, status, " updating filesystem, "
790 "not deactivating filesystem");
801 /* Function Name: DeactivateUser
802 * Description: sets the user's status to 3.
803 * Arguments: argc, argv - login name of the user in argv[1].
804 * Returns: DM_NORMAL.
807 int DeactivateUser(int argc, char **argv)
811 elem = GetUserInfo(LOGIN, argv[1], NULL);
812 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
819 /* ------------------------- Top Menu ------------------------- */
821 /* DeleteUser() in delete.c */
823 /* Function Name: DeleteUserByUid
824 * Description: Deletes the user given a uid number.
825 * Arguments: argc, argv - uid if user in argv[1].
826 * Returns: DM_NORMAL.
827 * NOTES: This just gets the username from the mr server
828 * and performs a DeleteUser().
831 int DeleteUserByUid(int argc, char **argv)
834 struct mqelem *elem = NULL;
837 if (!ValidName(argv[1]))
840 if ((status = do_mr_query("get_user_account_by_uid", 1, argv + 1, StoreInfo,
842 com_err(program_name, status, " in get_user_account_by_uid");
845 argv[1] = info[U_NAME];
847 DeleteUser(argc, argv);
851 /* ------------------------- Show User Information ------------------------- */
853 /* Function Name: ShowUserByLogin
854 * Description: Shows user information given a login name.
855 * Arguments: argc, argv - login name in argv[1].
859 int ShowUserByLogin(int argc, char *argv[])
861 struct mqelem *top, *elem;
863 elem = top = GetUserInfo(LOGIN, argv[1], NULL);
864 Loop(elem, PrintUserInfo);
870 /* Function Name: RetrieveUserByName
871 * Description: Show information on a user give fist and/or last name.
872 * Arguments: argc, argv - argv[1] - first name.
873 * argv[2] - last name.
874 * Returns: DM_NORMAL.
877 int ShowUserByName(int argc, char *argv[])
882 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
884 if (!top) /* if there was an error then return. */
887 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
895 Loop(top, PrintUserInfo);
899 Loop(top, PrintUserName);
907 /* Function Name: ShowUserByClass
908 * Description: Shows real and login names of all users in class.
909 * Arguments: argc, argv - argv[1] contains the class.
913 int ShowUserByClass(int argc, char **argv)
917 if (YesNoQuestion("This will take a long time. Are you sure", 0) == FALSE)
919 top = GetUserInfo(CLASS, argv[1], NULL);
920 Loop(top, PrintUserName);
927 /* Function Name: ShowUserById
928 * Description: Shows user information given an ID number.
929 * Arguments: argc, argv - ID number in argv[1].
933 int ShowUserById(int argc, char *argv[])
935 struct mqelem *top, *elem;
937 elem = top = GetUserInfo(ID, argv[1], NULL);
938 Loop(elem, PrintUserInfo);
945 /* Function Name: GetKrbmap
946 * Description: Shows user <-> Kerberos mappings
947 * Arguments: argc, argv - argv[1] contains the user login name,
948 * argv[2] contains the principal
952 int GetKrbmap(int argc, char **argv)
955 struct mqelem *elem = NULL, *top;
958 if ((stat = do_mr_query("get_kerberos_user_map", 2, &argv[1],
961 com_err(program_name, stat, " in GetKrbMap.");
965 top = elem = QueueTop(elem);
969 char **info = elem->q_data;
970 sprintf(buf, "User: %-9s Principal: %s",
971 info[KMAP_USER], info[KMAP_PRINCIPAL]);
976 FreeQueue(QueueTop(top));
981 /* Function Name: AddKrbmap
982 * Description: Add a new user <-> Kerberos mapping
983 * Arguments: argc, argv - argv[1] contains the user login name,
984 * argv[2] contains the principal
988 int AddKrbmap(int argc, char **argv)
992 if (!strchr(argv[KMAP_PRINCIPAL + 1], '@'))
994 Put_message("Please specify a realm for the kerberos principal.");
997 if ((stat = do_mr_query("add_kerberos_user_map", 2, &argv[1],
1000 com_err(program_name, stat, " in AddKrbMap.");
1001 if (stat == MR_EXISTS)
1002 Put_message("No user or principal may have more than one mapping.");
1008 /* Function Name: DeleteKrbmap
1009 * Description: Remove a user <-> Kerberos mapping
1010 * Arguments: argc, argv - argv[1] contains the user login name,
1011 * argv[2] contains the principal
1015 int DeleteKrbmap(int argc, char **argv)
1019 if ((stat = do_mr_query("delete_kerberos_user_map", 2, &argv[1],
1021 com_err(program_name, stat, " in DeleteKrbMap.");