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 status = atoi(info[U_STATE]);
135 if (status == 0 || status == 2)
137 sprintf(buf, "User %s secure Account Coupon to register",
138 atoi(info[U_SECURE]) ? "needs" : "does not need");
141 sprintf(buf, "Comments: %s", info[U_COMMENT]);
143 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME], info[U_MODWITH]);
147 /* Function Name: SetUserDefaults
148 * Description: Sets the default values for add user.
149 * Arguments: info - a blank user info array of char *'s.
150 * Returns: args - the filled info structure.
153 static char **SetUserDefaults(char **info)
155 info[U_NAME] = strdup(UNIQUE_LOGIN);
156 info[U_UID] = strdup(UNIQUE_UID);
157 info[U_SHELL] = strdup(DEFAULT_SHELL);
158 info[U_LAST] = strdup(DEFAULT_NONE);
159 info[U_FIRST] = strdup(DEFAULT_NONE);
160 info[U_MIDDLE] = strdup(DEFAULT_NONE);
161 info[U_STATE] = strdup(DEFAULT_NO);
162 info[U_MITID] = strdup(DEFAULT_NONE);
163 info[U_CLASS] = strdup(DEFAULT_CLASS);
164 info[U_COMMENT] = strdup("");
165 info[U_SIGNATURE] = strdup("");
166 info[U_SECURE] = strdup("0");
167 info[U_MODTIME] = info[U_MODBY] = info[U_MODWITH] = info[U_END] = NULL;
172 /* Check that the supplied name follows the capitalization rules, and
173 * offer to correct it if not.
176 void CorrectCapitalization(char **name)
178 char temp_buf[BUFSIZ], fixname[BUFSIZ];
180 strcpy(fixname, *name);
182 if (strcmp(fixname, *name))
184 Put_message("You entered a name which does not follow the capitalization conventions.");
185 sprintf(temp_buf, "Correct it to \"%s\"", fixname);
186 if (YesNoQuestion(temp_buf, 1) == TRUE)
189 *name = strdup(fixname);
195 /* Function Name: AskUserInfo.
196 * Description: This function askes the user for information about a
197 * machine and saves it into a structure.
198 * Arguments: info - a pointer the the structure to put the info into.
199 * flags - Flags asking us which info we want.
200 * Returns: the args to pass to the query.
201 * NOTES: the return args are not necessarily in the correct order to
202 * use the #defined names (e.g args[UID] is not the uid anymore).
205 char **AskUserInfo(char **info, Bool name)
211 char temp_buf[BUFSIZ], *newname;
215 sprintf(temp_buf, "\nChanging Attributes of user %s.\n", info[U_NAME]);
216 Put_message(temp_buf);
220 struct mqelem *elem = NULL;
223 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
225 CorrectCapitalization(&info[U_LAST]);
226 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
228 CorrectCapitalization(&info[U_FIRST]);
229 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) == SUB_ERROR)
231 CorrectCapitalization(&info[U_MIDDLE]);
232 argv[0] = info[U_FIRST];
233 argv[1] = info[U_LAST];
234 if (do_mr_query("get_user_account_by_name", 2, argv,
235 StoreInfo, &elem) == MR_SUCCESS)
237 Put_message("A user by that name already exists in the database.");
238 Loop(QueueTop(elem), PrintUserInfo);
239 Loop(QueueTop(elem), FreeInfo);
241 if (YesNoQuestion("Add new user anyway", TRUE) != TRUE)
247 newname = strdup(info[U_NAME]);
248 if (GetValueFromUser("The new login name for this user", &newname) ==
252 else if (GetValueFromUser("Login name for this user", &info[U_NAME]) ==
256 strcpy(temp_buf, info[U_UID]);
257 if (GetValueFromUser("User's UID", &info[U_UID]) == SUB_ERROR)
259 if (strcmp(info[U_UID], UNIQUE_UID) && strcmp(info[U_UID], temp_buf))
261 struct mqelem *elem = NULL;
262 if (do_mr_query("get_user_account_by_uid", 1, &info[U_UID],
263 StoreInfo, &elem) == MR_SUCCESS)
265 Put_message("A user with that uid already exists in the database.");
266 Loop(QueueTop(elem), PrintUserInfo);
267 Loop(QueueTop(elem), FreeInfo);
269 if (YesNoQuestion("Add new user anyway", TRUE) != TRUE)
274 if (GetValueFromUser("User's shell", &info[U_SHELL]) == SUB_ERROR)
278 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
280 CorrectCapitalization(&info[U_LAST]);
281 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
283 CorrectCapitalization(&info[U_FIRST]);
284 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) == SUB_ERROR)
286 CorrectCapitalization(&info[U_MIDDLE]);
291 if (GetValueFromUser("User's status (? for help)", &info[U_STATE]) ==
294 if (isdigit(info[U_STATE][0]))
296 Put_message("Valid status numbers:");
297 for (i = 0; i < US_END; i++)
299 sprintf(temp_buf, " %d: %s", i, states[i]);
300 Put_message(temp_buf);
303 if (GetValueFromUser("User's MIT ID number", &info[U_MITID]) == SUB_ERROR)
305 RemoveHyphens(info[U_MITID]);
306 if (GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]) ==
309 if (GetValueFromUser("Comments", &info[U_COMMENT]) == SUB_ERROR)
312 state = atoi(info[U_STATE]);
313 if (!name || state == 0 || state == 2)
315 if (YesNoQuestion("User needs secure Account Coupon to register",
316 atoi(info[U_SECURE]) ? TRUE : FALSE) == FALSE)
318 free(info[U_SECURE]);
319 info[U_SECURE] = strdup("0");
323 free(info[U_SECURE]);
324 info[U_SECURE] = strdup("1");
330 if (strcmp(info[U_NAME], UNIQUE_LOGIN))
333 sprintf(temp_buf, "%s:%s", newname, info[U_MITID]);
335 sprintf(temp_buf, "%s:%s", info[U_NAME], info[U_MITID]);
337 i = GDSS_Verify((unsigned char *)temp_buf, strlen(temp_buf),
338 (unsigned char *)info[U_SIGNATURE], &si);
339 /* If it's already signed OK, don't resign it. */
340 if (i != GDSS_SUCCESS)
342 free(info[U_SIGNATURE]);
343 info[U_SIGNATURE] = malloc(GDSS_Sig_Size() * 2);
345 i = GDSS_Sign((unsigned char *)temp_buf, strlen(temp_buf),
346 (unsigned char *)info[U_SIGNATURE]);
347 if (i != GDSS_SUCCESS)
348 com_err(program_name, gdss2et(i), "Failed to create signature");
351 unsigned char buf[256];
353 i = GDSS_Verify((unsigned char *)temp_buf, strlen(temp_buf),
354 (unsigned char *)info[U_SIGNATURE], &si);
355 if (strlen((char *)buf) > 68)
361 info[U_SIGNATURE] = strdup("");
364 FreeAndClear(&info[U_MODTIME], TRUE);
365 FreeAndClear(&info[U_MODBY], TRUE);
366 FreeAndClear(&info[U_MODWITH], TRUE);
369 * Slide the newname into the #2 slot, this screws up all future references
370 * to this list, since we slip the pointer into a info list it gets freed
371 * when the rest of the list gets freed.
374 SlipInNewName(info, newname);
379 /* Function Name: GetUserInfo
380 * Description: Stores the user information in a queue.
381 * Arguments: type - type of field given to get info, one of:
382 * LOGIN, UID, BY_NAME, CLASS.
383 * name1 - name of thing specified by type (wildcards okay)
384 * name2 - other name, only used in get user by first and last.
386 * Returns: the first element of the queue containing the user info.
390 struct mqelem *GetUserInfo(int type, char *name1, char *name2)
394 struct mqelem *elem = NULL;
400 if ((status = do_mr_query("get_user_account_by_login", 1, args,
403 com_err(program_name, status,
404 " when attempting to get_user_account_by_login.");
410 if ((status = do_mr_query("get_user_account_by_uid", 1, args,
413 com_err(program_name, status,
414 " when attempting to get_user_account_by_uid.");
421 if ((status = do_mr_query("get_user_account_by_name", 2, args,
424 com_err(program_name, status,
425 " when attempting to get_user_account_by_name.");
431 if ((status = do_mr_query("get_user_account_by_class", 1, args,
434 com_err(program_name, status,
435 " when attempting to get_user_account_by_class.");
441 if ((status = do_mr_query("get_user_account_by_id", 1, args,
444 com_err(program_name, status,
445 " when attempting to get_user_account_by_id.");
450 return QueueTop(elem) ;
453 /* Function Name: AddNewUser
454 * Description: Adds a new user to the database.
456 * Returns: DM_NORMAL.
459 int AddNewUser(int argc, char **argv)
462 char **args, *info[MAX_ARGS_SIZE];
464 if (!(args = AskUserInfo(SetUserDefaults(info), FALSE)))
466 Put_message("Aborted.");
469 if ((status = do_mr_query("add_user_account", CountArgs(args),
471 com_err(program_name, status, " in add_user_account");
473 Put_message("New user added to database.");
479 /* Function Name: GetLoginName
480 * Description: Asks the user for a login name and reserves
483 * Returns: a malloced login name for the user.
486 static char *GetLoginName(void)
491 if (GetValueFromUser("Login name for this user? ", &name) == SUB_ERROR)
493 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
498 /* Function Name: ChooseUser
499 * Description: Choose a user from a list and return the uid.
500 * Arguments: top - a queue of user information.
501 * Returns: uid - the malloced uid of the user that was chosen.
504 static char *ChooseUser(struct mqelem *elem)
508 char **info = elem->q_data;
510 switch (YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE))
513 return strdup(info[U_UID]);
516 default: /* quit or ^C. */
524 /* Function Name: GetUidNumberFromName
525 * Description: Gets the users uid number, from the name.
527 * Returns: uid - a malloced string containing the uid.
530 static char *GetUidNumberFromName(void)
532 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
534 struct mqelem *top = NULL;
536 if (!Prompt_input("First Name: ", first, BUFSIZ))
538 if (!Prompt_input("Last Name: ", last, BUFSIZ))
546 switch ((status = do_mr_query("get_user_account_by_name", 2, args,
552 Put_message("There is no user in the database with that name.");
555 com_err(program_name, status, " in get_account_user_by_name.");
560 if (QueueCount(top) == 1) /* This is a unique name. */
562 char **info = top->q_data;
563 Put_message("User ID Number retrieved for the user: ");
566 uid = strdup(info[U_UID]);
571 Put_message("That name is not unique, choose the user that you want.");
572 uid = ChooseUser(top);
577 /* Function Name: SetUserPassword
578 * Description: Set the new kerberos password for this user.
579 * Arguments: name - kerberos principle name for this user, (login name).
583 static void SetUserPassword(char *name)
585 name = name; /* make saber happy. */
586 Put_message("Kerberos password not changed, code non-existant.");
587 /* clever message to call account_admin, if this fails. */
590 /* Function Name: GiveBackLogin
591 * Description: Gives back previously reserved kerberous principle.
592 * Arguments: name - principle to give back.
596 static void GiveBackLogin(char *name)
598 name = name; /* make saber happy. */
599 Put_message("kerberos code not implimented, name not given back.");
600 /* send mail to db maintainer if this fails. */
603 /* Function Name: RegisterUser
604 * Description: This function registers a user.
606 * Returns: DM_NORMAL.
609 int RegisterUser(int argc, char **argv)
611 char *args[MAX_ARGS_SIZE];
612 char *login, *fstype = NULL;
613 char temp_buf[BUFSIZ];
616 Put_message("This function has NO kerberos support, so strange things");
617 Put_message("may happen if you use it to register a user.");
619 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE))
622 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
623 args[0] = strdup(temp_buf);
626 if (!(args[0] = GetUidNumberFromName()))
633 sprintf(temp_buf, "u%s", args[0]);
634 login = strdup(temp_buf);
635 if ((GetValueFromUser("Login name for this user? ", &login) == SUB_ERROR) ||
636 (GetFSTypes(&fstype, FALSE) == SUB_ERROR))
639 FreeInfo(args); /* This work because the NULL temination is ok. */
642 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
647 switch ((status = do_mr_query("register_user", CountArgs(args),
651 sprintf(temp_buf, "User %s successfully registered.", login);
652 Put_message(temp_buf);
653 SetUserPassword(login);
656 GiveBackLogin(login);
657 sprintf(temp_buf, "The username %s is already in use.", login);
658 Put_message(temp_buf);
661 com_err(program_name, status, " in register_user");
668 /* Function Name: RealUpdateUser
669 * Description: actuall updates the user information.
670 * Arguments: info - all current information for the user fields.
671 * junk - an UNUSED boolean.
675 static void RealUpdateUser(char **info, Bool junk)
678 char error_buf[BUFSIZ];
679 char **args = AskUserInfo(info, TRUE);
683 Put_message("Aborted.");
686 if ((status = do_mr_query("update_user_account", CountArgs(args),
689 com_err(program_name, status, " in ModifyFields");
690 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
691 Put_message(error_buf);
695 /* Function Name: UpdateUser
696 * Description: Modify some of the information about a user.
697 * Arguments: argc, argv - login name of the user in argv[1].
698 * Returns: DM_NORMAL.
701 int UpdateUser(int argc, char **argv)
705 elem = GetUserInfo(LOGIN, argv[1], NULL);
706 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
712 /* Function Name: RealDeactivateUser
713 * Description: sets the user's status to 3.
714 * Arguments: info - all current information for the user fields
715 * one_item - indicates the user hasn't been queried yet
719 static void RealDeactivateUser(char **info, Bool one_item)
722 char txt_buf[BUFSIZ];
723 char *qargs[2], **args;
724 struct mqelem *elem = NULL;
728 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
729 if (YesNoQuestion(txt_buf, FALSE) != TRUE)
733 qargs[0] = info[NAME];
735 if ((status = do_mr_query("update_user_status", 2, qargs, NULL, NULL)))
737 com_err(program_name, status, " in update_user_status");
738 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
739 Put_message(txt_buf);
741 else if (YesNoQuestion("Also deactivate matching list and filesystem (y/n)",
744 status = do_mr_query("get_list_info", 1, &(info[NAME]), StoreInfo,
746 if (status == MR_SUCCESS)
748 args = QueueTop(elem)->q_data;
749 free(args[L_ACTIVE]);
750 args[L_ACTIVE] = strdup("0");
751 FreeAndClear(&args[L_MODTIME], TRUE);
752 FreeAndClear(&args[L_MODBY], TRUE);
753 FreeAndClear(&args[L_MODWITH], TRUE);
754 SlipInNewName(args, args[L_NAME]);
755 if ((status = do_mr_query("update_list", CountArgs(args), args,
758 com_err(program_name, status, " updating list, "
759 "not deactivating list or filesystem");
768 else if (status != MR_NO_MATCH)
770 com_err(program_name, status, " getting list info, "
771 "not deactivating list or filesystem");
775 if ((status = do_mr_query("get_filesys_by_label", 1, &(info[NAME]),
778 com_err(program_name, status, " getting filsys info, "
779 "not deactivating filesystem");
782 args = QueueTop(elem)->q_data;
784 args[FS_TYPE] = strdup("ERR");
785 free(args[FS_COMMENTS]);
786 args[FS_COMMENTS] = strdup("Locker disabled; call 3-1325 for help");
787 FreeAndClear(&args[FS_MODTIME], TRUE);
788 FreeAndClear(&args[FS_MODBY], TRUE);
789 FreeAndClear(&args[FS_MODWITH], TRUE);
790 SlipInNewName(args, args[FS_NAME]);
791 if ((status = do_mr_query("update_filesys", CountArgs(args), args,
794 com_err(program_name, status, " updating filesystem, "
795 "not deactivating filesystem");
806 /* Function Name: DeactivateUser
807 * Description: sets the user's status to 3.
808 * Arguments: argc, argv - login name of the user in argv[1].
809 * Returns: DM_NORMAL.
812 int DeactivateUser(int argc, char **argv)
816 elem = GetUserInfo(LOGIN, argv[1], NULL);
817 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
824 /* ------------------------- Top Menu ------------------------- */
826 /* DeleteUser() in delete.c */
828 /* Function Name: DeleteUserByUid
829 * Description: Deletes the user given a uid number.
830 * Arguments: argc, argv - uid if user in argv[1].
831 * Returns: DM_NORMAL.
832 * NOTES: This just gets the username from the mr server
833 * and performs a DeleteUser().
836 int DeleteUserByUid(int argc, char **argv)
839 struct mqelem *elem = NULL;
842 if (!ValidName(argv[1]))
845 if ((status = do_mr_query("get_user_account_by_uid", 1, argv + 1, StoreInfo,
847 com_err(program_name, status, " in get_user_account_by_uid");
850 argv[1] = info[U_NAME];
852 DeleteUser(argc, argv);
856 /* ------------------------- Show User Information ------------------------- */
858 /* Function Name: ShowUserByLogin
859 * Description: Shows user information given a login name.
860 * Arguments: argc, argv - login name in argv[1].
864 int ShowUserByLogin(int argc, char *argv[])
866 struct mqelem *top, *elem;
868 elem = top = GetUserInfo(LOGIN, argv[1], NULL);
869 Loop(elem, PrintUserInfo);
875 /* Function Name: RetrieveUserByName
876 * Description: Show information on a user give fist and/or last name.
877 * Arguments: argc, argv - argv[1] - first name.
878 * argv[2] - last name.
879 * Returns: DM_NORMAL.
882 int ShowUserByName(int argc, char *argv[])
887 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
889 if (!top) /* if there was an error then return. */
892 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
900 Loop(top, PrintUserInfo);
904 Loop(top, PrintUserName);
912 /* Function Name: ShowUserByClass
913 * Description: Shows real and login names of all users in class.
914 * Arguments: argc, argv - argv[1] contains the class.
918 int ShowUserByClass(int argc, char **argv)
922 if (YesNoQuestion("This will take a long time. Are you sure", 0) == FALSE)
924 top = GetUserInfo(CLASS, argv[1], NULL);
925 Loop(top, PrintUserName);
932 /* Function Name: ShowUserById
933 * Description: Shows user information given an ID number.
934 * Arguments: argc, argv - ID number in argv[1].
938 int ShowUserById(int argc, char *argv[])
940 struct mqelem *top, *elem;
942 elem = top = GetUserInfo(ID, argv[1], NULL);
943 Loop(elem, PrintUserInfo);
950 /* Function Name: GetKrbmap
951 * Description: Shows user <-> Kerberos mappings
952 * Arguments: argc, argv - argv[1] contains the user login name,
953 * argv[2] contains the principal
957 int GetKrbmap(int argc, char **argv)
960 struct mqelem *elem = NULL, *top;
963 if ((stat = do_mr_query("get_kerberos_user_map", 2, &argv[1],
966 com_err(program_name, stat, " in GetKrbMap.");
970 top = elem = QueueTop(elem);
974 char **info = elem->q_data;
975 sprintf(buf, "User: %-9s Principal: %s",
976 info[KMAP_USER], info[KMAP_PRINCIPAL]);
981 FreeQueue(QueueTop(top));
986 /* Function Name: AddKrbmap
987 * Description: Add a new user <-> Kerberos mapping
988 * Arguments: argc, argv - argv[1] contains the user login name,
989 * argv[2] contains the principal
993 int AddKrbmap(int argc, char **argv)
997 if (!strchr(argv[KMAP_PRINCIPAL + 1], '@'))
999 Put_message("Please specify a realm for the kerberos principal.");
1002 if ((stat = do_mr_query("add_kerberos_user_map", 2, &argv[1],
1005 com_err(program_name, stat, " in AddKrbMap.");
1006 if (stat == MR_EXISTS)
1007 Put_message("No user or principal may have more than one mapping.");
1013 /* Function Name: DeleteKrbmap
1014 * Description: Remove a user <-> Kerberos mapping
1015 * Arguments: argc, argv - argv[1] contains the user login name,
1016 * argv[2] contains the principal
1020 int DeleteKrbmap(int argc, char **argv)
1024 if ((stat = do_mr_query("delete_kerberos_user_map", 2, &argv[1],
1026 com_err(program_name, stat, " in DeleteKrbMap.");