1 #if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
5 /* This is the file user.c for the MOIRA Client, which allows a nieve
6 * user to quickly and easily maintain most parts of the MOIRA database.
7 * It Contains: Functions for manipulating user information.
10 * By: Chris D. Peterson
16 * Copyright 1988 by the Massachusetts Institute of Technology.
18 * For further information on copyright and distribution
19 * see the file mit-copyright.h
25 #include <moira_site.h>
34 #include "mit-copyright.h"
44 #define DEFAULT_SHELL "/bin/csh"
45 #define DEFAULT_CLASS "?"
48 /* Function Name: UserState
49 * Description: Convert a numeric state into a descriptive string.
50 * Arguments: state value
51 * Returns: pointer to statically allocated string.
54 static char *states[] = { "Registerable (0)",
56 "Half Registered (2)",
58 "Not registerable (4)",
59 "Enrolled/Registerable (5)",
60 "Enrolled/Not Registerable (6)",
61 "Half Enrolled (7)" };
63 static char *UserState(state)
68 if (state < 0 || state >= US_END) {
69 sprintf(buf, "Unknown (%d)", state);
72 return(states[state]);
76 /* Function Name: PrintUserName
77 * Description: Print name of a user.
78 * Arguments: info - the information about a user.
86 char buf[BUFSIZ], print_buf[BUFSIZ];
87 sprintf(buf, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
88 sprintf(print_buf, "%-40s User Name: %s", buf, info[U_NAME]);
89 Put_message(print_buf);
92 /* Function Name: PrintUserInfo
93 * Description: Prints Information about a user.
94 * Arguments: info - an argument list with the user information
103 char name[BUFSIZ], buf[BUFSIZ];
109 sprintf(name, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
110 sprintf(buf, "Login name: %-20s Full name: %s", info[U_NAME], name);
112 sprintf(buf, "User id: %-23s Login shell %-10s Class: %s",
113 info[U_UID], info[U_SHELL], info[U_CLASS]);
117 sprintf(buf, "%s:%s", info[U_NAME], info[U_MITID]);
119 status = GDSS_Verify(buf, strlen(buf), info[U_SIGNATURE], &si);
121 hex_dump(info[U_SIGNATURE]);
122 sprintf(buf, "GDSS_Verify => %d", status);
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_SECURE]))
134 sprintf(buf, "Secure password set on %s.", atot(info[U_SECURE]));
136 sprintf(buf, "No secure password set.");
138 sprintf(buf, "Comments: %s", info[U_COMMENT]);
140 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME],info[U_MODWITH]);
144 /* Function Name: SetUserDefaults
145 * Description: Sets the default values for add user.
146 * Arguments: info - a blank user info array of char *'s.
147 * Returns: args - the filled info structure.
151 SetUserDefaults(info)
154 info[U_NAME] = Strsave(UNIQUE_LOGIN);
155 info[U_UID] = Strsave(UNIQUE_UID);
156 info[U_SHELL] = Strsave(DEFAULT_SHELL);
157 info[U_LAST] = Strsave(DEFAULT_NONE);
158 info[U_FIRST] = Strsave(DEFAULT_NONE);
159 info[U_MIDDLE] = Strsave(DEFAULT_NONE);
160 info[U_STATE] = Strsave(DEFAULT_NO);
161 info[U_MITID] = Strsave(DEFAULT_NONE);
162 info[U_CLASS] = Strsave(DEFAULT_CLASS);
163 info[U_COMMENT] = Strsave("");
164 info[U_SIGNATURE] = Strsave("");
165 info[U_SECURE] = Strsave("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 CorrectCapitalization(name)
178 char temp_buf[BUFSIZ], fixname[BUFSIZ];
180 strcpy(fixname, *name);
182 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) {
187 *name = strsave(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).
204 AskUserInfo(info, name)
210 char temp_buf[BUFSIZ], *newname, *temp_ptr, *sig, sig_buf[BUFSIZ];
213 sprintf(temp_buf,"\nChanging Attributes of user %s.\n",info[U_NAME]);
214 Put_message(temp_buf);
216 struct qelem *elem = NULL;
219 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
221 CorrectCapitalization(&info[U_LAST]);
222 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
224 CorrectCapitalization(&info[U_FIRST]);
225 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) ==
228 CorrectCapitalization(&info[U_MIDDLE]);
229 argv[0] = info[U_FIRST];
230 argv[1] = info[U_LAST];
231 if (do_mr_query("get_user_account_by_name", 2, argv,
232 StoreInfo, (char *) &elem) == 0) {
233 Put_message("A user by that name already exists in the database.");
234 Loop(QueueTop(elem), PrintUserInfo);
235 Loop(QueueTop(elem), FreeInfo);
237 if (YesNoQuestion("Add new user anyway", TRUE) != TRUE)
242 newname = Strsave(info[U_NAME]);
243 if (GetValueFromUser("The new login name for this user", &newname) ==
246 } else if (GetValueFromUser("Login name for this user", &info[U_NAME]) ==
250 if (GetValueFromUser("User's UID", &info[U_UID]) == SUB_ERROR)
252 if (GetValueFromUser("User's shell", &info[U_SHELL]) == SUB_ERROR)
255 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
257 CorrectCapitalization(&info[U_LAST]);
258 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
260 CorrectCapitalization(&info[U_FIRST]);
261 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) ==
264 CorrectCapitalization(&info[U_MIDDLE]);
268 if (GetValueFromUser("User's status (? for help)", &info[U_STATE]) ==
271 if (isdigit(info[U_STATE][0]))
273 Put_message("Valid status numbers:");
274 for (i = 0; i < US_END; i++) {
275 sprintf(temp_buf, " %d: %s", i, states[i]);
276 Put_message(temp_buf);
279 if (GetValueFromUser("User's MIT ID number", &info[U_MITID]) == SUB_ERROR)
281 RemoveHyphens(info[U_MITID]);
282 if (GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]) ==
285 if (GetValueFromUser("Comments", &info[U_COMMENT]) == SUB_ERROR)
288 if (YesNoQuestion("Secure password set",
289 atoi(info[U_SECURE]) ? TRUE : FALSE) == FALSE) {
290 free(info[U_SECURE]);
291 info[U_SECURE] = strsave("0");
292 } else if (!strcmp(info[U_SECURE], "0")) {
296 gettimeofday(&tv, (struct timezone *)NULL);
297 sprintf(buf, "%d", tv.tv_sec);
298 free(info[U_SECURE]);
299 info[U_SECURE] = strsave(buf);
304 if (strcmp(info[U_NAME], UNIQUE_LOGIN)) {
306 sprintf(temp_buf, "%s:%s", newname, info[U_MITID]);
308 sprintf(temp_buf, "%s:%s", info[U_NAME], info[U_MITID]);
310 i = GDSS_Verify(temp_buf, strlen(temp_buf), info[U_SIGNATURE], &si);
311 /* If it's already signed OK, don't resign it. */
312 if (i != GDSS_SUCCESS) {
313 free(info[U_SIGNATURE]);
314 info[U_SIGNATURE] = malloc(GDSS_Sig_Size() * 2);
316 i = GDSS_Sign(temp_buf, strlen(temp_buf), info[U_SIGNATURE]);
317 if (i != GDSS_SUCCESS)
318 com_err(program_name, gdss2et(i), "Failed to create signature");
320 unsigned char buf[256];
322 i = GDSS_Verify(temp_buf, strlen(temp_buf),
323 info[U_SIGNATURE], &si);
324 if (strlen(buf) > 68) {
326 Put_message("Signature too long, trying again");
332 Put_message("Made signature:");hex_dump(info[U_SIGNATURE]);
334 Put_message("Don't need to remake signature");
339 info[U_SIGNATURE] = strsave("");
342 FreeAndClear(&info[U_MODTIME], TRUE);
343 FreeAndClear(&info[U_MODBY], TRUE);
344 FreeAndClear(&info[U_MODWITH], TRUE);
347 * Slide the newname into the #2 slot, this screws up all future references
348 * to this list, since we slip the pointer into a info list it gets freed
349 * when the rest of the list gets freed.
352 SlipInNewName(info, newname);
357 /* Function Name: GetUserInfo
358 * Description: Stores the user information in a queue.
359 * Arguments: type - type of field given to get info, one of:
360 * LOGIN, UID, BY_NAME, CLASS.
361 * name1 - name of thing specified by type (wildcards okay)
362 * name2 - other name, only used in get user by first and last.
364 * Returns: the first element of the queue containing the user info.
368 GetUserInfo(type, name1, name2)
374 struct qelem * elem = NULL;
379 if ( (status = do_mr_query("get_user_account_by_login", 1, args,
380 StoreInfo, (char *) &elem)) != 0) {
381 com_err(program_name, status,
382 " when attempting to get_user_account_by_login.");
388 if ( (status = do_mr_query("get_user_account_by_uid", 1, args,
389 StoreInfo, (char *) &elem)) != 0) {
390 com_err(program_name, status,
391 " when attempting to get_user_account_by_uid.");
398 if ( (status = do_mr_query("get_user_account_by_name", 2, args,
399 StoreInfo, (char *) &elem)) != 0) {
400 com_err(program_name, status,
401 " when attempting to get_user_account_by_name.");
407 if ( (status = do_mr_query("get_user_account_by_class", 1, args,
408 StoreInfo, (char *) &elem)) != 0) {
409 com_err(program_name, status,
410 " when attempting to get_user_account_by_class.");
415 return( QueueTop(elem) );
418 /* Function Name: AddNewUser
419 * Description: Adds a new user to the database.
421 * Returns: DM_NORMAL.
429 char ** args, *info[MAX_ARGS_SIZE];
431 if ((args = AskUserInfo(SetUserDefaults(info), FALSE)) == NULL) {
432 Put_message("Aborted.");
437 if ( (status = do_mr_query("add_user_account", CountArgs(args),
438 args, Scream, (char *) NULL)) != MR_SUCCESS)
439 com_err(program_name, status, " in add_user_account");
441 Put_message("New user added to database.");
447 /* Function Name: GetLoginName
448 * Description: Asks the user for a login name and reserves
451 * Returns: a malloced login name for the user.
460 if (GetValueFromUser("Login name for this user? ", &name) == SUB_ERROR)
462 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
467 /* Function Name: ChooseUser
468 * Description: Choose a user from a list and return the uid.
469 * Arguments: top - a queue of user information.
470 * Returns: uid - the malloced uid of the user that was chosen.
477 while (elem != NULL) {
478 char ** info = (char **) elem->q_data;
480 switch(YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE)) {
482 return(Strsave(info[U_UID]));
485 default: /* quit or ^C. */
493 /* Function Name: GetUidNumberFromName
494 * Description: Gets the users uid number, from the name.
496 * Returns: uid - a malloced string containing the uid.
500 GetUidNumberFromName()
502 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
504 struct qelem * top = NULL;
506 if (!Prompt_input("First Name: ", first, BUFSIZ))
508 if (!Prompt_input("Last Name: ", last, BUFSIZ))
516 switch (status = do_mr_query("get_user_account_by_name", 2, args,
517 StoreInfo, (char *) &top)) {
521 Put_message("There is no user in the database with that name.");
524 com_err(program_name, status, " in get_account_user_by_name.");
529 if (QueueCount(top) == 1) /* This is a unique name. */ {
530 char ** info = (char **) top->q_data;
531 Put_message("User ID Number retrieved for the user: ");
534 uid = Strsave(info[U_UID]);
536 return(Strsave(uid));
539 Put_message("That name is not unique, choose the user that you want.");
540 uid = ChooseUser(top);
545 /* Function Name: SetUserPassword
546 * Description: Set the new kerberos password for this user.
547 * Arguments: name - kerberos principle name for this user, (login name).
552 SetUserPassword(name)
555 name = name; /* make saber happy. */
556 Put_message("Kerberos password not changed, code non-existant.");
557 /* clever message to call account_admin, if this fails. */
560 /* Function Name: GiveBackLogin
561 * Description: Gives back previously reserved kerberous principle.
562 * Arguments: name - principle to give back.
570 name = name; /* make saber happy. */
571 Put_message("kerberos code not implimented, name not given back.");
572 /* send mail to db maintainer if this fails. */
575 /* Function Name: RegisterUser
576 * Description: This function registers a user.
578 * Returns: DM_NORMAL.
584 char * args[MAX_ARGS_SIZE];
585 char *login, *fstype = NULL;
586 char temp_buf[BUFSIZ];
589 Put_message("This function has NO kerberos support, so strange things");
590 Put_message("may happen if you use it to register a user.");
592 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE)) {
594 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
595 args[0] = Strsave(temp_buf);
598 if ( (args[0] = GetUidNumberFromName()) == NULL)
605 if ( ((login = args[1] = GetLoginName()) == NULL) ||
606 ( GetFSTypes(&fstype, FALSE) == SUB_ERROR ) ) {
607 FreeInfo(args); /* This work because the NULL temination is ok. */
613 switch (status = do_mr_query("register_user", CountArgs(args),
614 args, Scream, (char *) NULL)) {
616 sprintf(temp_buf, "User %s successfully registered.", login);
617 Put_message(temp_buf);
618 SetUserPassword(login);
621 GiveBackLogin(login);
622 sprintf(temp_buf, "The username %s is already in use.", login);
623 Put_message(temp_buf);
626 com_err(program_name, status, " in register_user");
633 /* Function Name: RealUpdateUser
634 * Description: actuall updates the user information.
635 * Arguments: info - all current information for the user fields.
636 * junk - an UNUSED boolean.
642 RealUpdateUser(info, junk)
647 char error_buf[BUFSIZ];
648 char ** args = AskUserInfo(info, TRUE);
651 Put_message("Aborted.");
654 if ( (status = do_mr_query("update_user_account", CountArgs(args),
655 args, Scream, (char *) NULL)) != MR_SUCCESS) {
656 com_err(program_name, status, " in ModifyFields");
657 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
658 Put_message(error_buf);
662 /* Function Name: UpdateUser
663 * Description: Modify some of the information about a user.
664 * Arguments: argc, argv - login name of the user in argv[1].
665 * Returns: DM_NORMAL.
670 UpdateUser(argc, argv)
676 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
677 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
683 /* Function Name: RealDeactivateUser
684 * Description: sets the user's status to 3.
685 * Arguments: info - all current information for the user fields
686 * one_item - indicates the user hasn't been queried yet
691 RealDeactivateUser(info, one_item)
696 char txt_buf[BUFSIZ];
697 char * qargs[2], **args;
698 struct qelem *elem = NULL;
701 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
702 if (YesNoQuestion(txt_buf, FALSE) != TRUE)
706 qargs[0] = info[NAME];
708 if ((status = do_mr_query("update_user_status", 2, qargs, Scream,
709 (char *) NULL)) != MR_SUCCESS) {
710 com_err(program_name, status, " in update_user_status");
711 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
712 Put_message(txt_buf);
713 } else if (YesNoQuestion("Also deactivate matching list and filesystem (y/n)",
715 if (status = do_mr_query("get_list_info", 1, &(info[NAME]),
716 StoreInfo, (char *) &elem)) {
717 com_err(program_name, status, " getting list info, not deactivating list or filesystem");
720 args =(char **) (QueueTop(elem)->q_data);
721 free(args[L_ACTIVE]);
722 args[L_ACTIVE] = strsave("0");
723 FreeAndClear(&args[L_MODTIME], TRUE);
724 FreeAndClear(&args[L_MODBY], TRUE);
725 FreeAndClear(&args[L_MODWITH], TRUE);
726 SlipInNewName(args, args[L_NAME]);
727 if (status = do_mr_query("update_list", CountArgs(args), args,
728 Scream, (char *) NULL)) {
729 com_err(program_name, status, " updating list, not deactivating list or filesystem");
736 elem = (struct qelem *) NULL;
737 if (status = do_mr_query("get_filesys_by_label", 1, &(info[NAME]),
738 StoreInfo, (char *) &elem)) {
739 com_err(program_name, status, " getting filsys info, not deactivating filesystem");
744 args = (char **) (QueueTop(elem)->q_data);
746 args[FS_TYPE] = strsave("ERR");
747 free(args[FS_COMMENTS]);
748 args[FS_COMMENTS] = strsave("Locker disabled; call 3-1325 for help");
749 FreeAndClear(&args[FS_MODTIME], TRUE);
750 FreeAndClear(&args[FS_MODBY], TRUE);
751 FreeAndClear(&args[FS_MODWITH], TRUE);
752 SlipInNewName(args, args[FS_NAME]);
753 if (status = do_mr_query("update_filesys", CountArgs(args), args,
754 Scream, (char *) NULL)) {
755 com_err(program_name, status, " updating filesystem, not deactivating filesystem");
766 /* Function Name: DeactivateUser
767 * Description: sets the user's status to 3.
768 * Arguments: argc, argv - login name of the user in argv[1].
769 * Returns: DM_NORMAL.
774 DeactivateUser(argc, argv)
780 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
781 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
788 /* ------------------------- Top Menu ------------------------- */
790 /* DeleteUser() in delete.c */
792 /* Function Name: DeleteUserByUid
793 * Description: Deletes the user given a uid number.
794 * Arguments: argc, argv - uid if user in argv[1].
795 * Returns: DM_NORMAL.
796 * NOTES: This just gets the username from the mr server
797 * and performs a DeleteUser().
801 DeleteUserByUid(argc, argv)
806 struct qelem *elem = NULL;
809 if(!ValidName(argv[1]))
812 if ( (status = do_mr_query("get_user_account_by_uid", 1, argv+1, StoreInfo,
813 (char * ) &elem)) != MR_SUCCESS)
814 com_err(program_name, status, " in get_user_account_by_uid");
816 info = (char **) elem->q_data;
817 argv[1] = info[U_NAME];
819 (void) DeleteUser(argc, argv);
823 /* ------------------------- Show User Information ------------------------- */
825 /* Function Name: ShowUserByLogin
826 * Description: Shows user information given a login name.
827 * Arguments: argc, argv - login name in argv[1].
833 ShowUserByLogin(argc, argv)
837 struct qelem *top, *elem;
839 elem = top = GetUserInfo(LOGIN, argv[1], (char *) NULL);
840 Loop(elem, PrintUserInfo);
846 /* Function Name: RetrieveUserByName
847 * Description: Show information on a user give fist and/or last name.
848 * Arguments: argc, argv - argv[1] - first name.
849 * argv[2] - last name.
850 * Returns: DM_NORMAL.
855 ShowUserByName(argc, argv)
862 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
864 if (top == NULL) /* if there was an error then return. */
867 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
874 Loop(top, PrintUserInfo);
878 Loop(top, PrintUserName);
886 /* Function Name: ShowUserByClass
887 * Description: Shows real and login names of all users in class.
888 * Arguments: argc, argv - argv[1] contains the class.
894 ShowUserByClass(argc, argv)
900 if (YesNoQuestion("This will take a long time. Are you sure", 0) == FALSE)
902 top = GetUserInfo(CLASS, argv[1], (char *) NULL);
903 Loop(top, PrintUserName);
910 /* Function Name: GetKrbmap
911 * Description: Shows user <-> Kerberos mappings
912 * Arguments: argc, argv - argv[1] contains the user login name,
913 * argv[2] contains the principal
919 GetKrbmap(argc, argv)
924 struct qelem *elem = NULL, *top;
927 if ((stat = do_mr_query("get_kerberos_user_map", 2, &argv[1],
928 StoreInfo, (char *)&elem)) != 0) {
929 com_err(program_name, stat, " in GetKrbMap.");
933 top = elem = QueueTop(elem);
935 while (elem != NULL) {
936 char **info = (char **) elem->q_data;
937 sprintf(buf, "User: %-9s Principal: %s",
938 info[KMAP_USER], info[KMAP_PRINCIPAL]);
943 FreeQueue(QueueTop(top));
948 /* Function Name: AddKrbmap
949 * Description: Add a new user <-> Kerberos mapping
950 * Arguments: argc, argv - argv[1] contains the user login name,
951 * argv[2] contains the principal
957 AddKrbmap(argc, argv)
963 if (!index(argv[KMAP_PRINCIPAL + 1], '@')) {
964 Put_message("Please specify a realm for the kerberos principal.");
967 if ((stat = do_mr_query("add_kerberos_user_map", 2, &argv[1],
968 Scream, NULL)) != 0) {
969 com_err(program_name, stat, " in AddKrbMap.");
970 if (stat == MR_EXISTS)
971 Put_message("No user or principal may have more than one mapping.");
977 /* Function Name: DeleteKrbmap
978 * Description: Remove a user <-> Kerberos mapping
979 * Arguments: argc, argv - argv[1] contains the user login name,
980 * argv[2] contains the principal
986 DeleteKrbmap(argc, argv)
992 if ((stat = do_mr_query("delete_kerberos_user_map", 2, &argv[1],
993 Scream, NULL)) != 0) {
994 com_err(program_name, stat, " in DeleteKrbMap.");
1006 sprintf(buf, "Size: %d", strlen(p));
1008 while (strlen(p) >= 8) {
1009 sprintf(buf, "%02x %02x %02x %02x %02x %02x %02x %02x",
1010 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
1014 switch (strlen(p)) {
1016 sprintf(buf, "%02x %02x %02x %02x %02x %02x %02x",
1017 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
1020 sprintf(buf, "%02x %02x %02x %02x %02x %02x",
1021 p[0], p[1], p[2], p[3], p[4], p[5]);
1024 sprintf(buf, "%02x %02x %02x %02x %02x",
1025 p[0], p[1], p[2], p[3], p[4]);
1028 sprintf(buf, "%02x %02x %02x %02x",
1029 p[0], p[1], p[2], p[3]);
1032 sprintf(buf, "%02x %02x %02x",
1036 sprintf(buf, "%02x %02x",
1040 sprintf(buf, "%02x",