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]);
127 sprintf(buf, "Account is: %-20s MIT ID number: %s Signed: %s",
128 UserState(atoi(info[U_STATE])), info[U_MITID],
129 *info[U_SIGNATURE] ? (status ? "Bad" : "Yes") : "No");
131 if (atoi(info[U_SECURE]))
132 sprintf(buf, "Secure password set on %s.", atot(info[U_SECURE]));
134 sprintf(buf, "No secure password set.");
136 sprintf(buf, "Comments: %s", info[U_COMMENT]);
138 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME],info[U_MODWITH]);
142 /* Function Name: SetUserDefaults
143 * Description: Sets the default values for add user.
144 * Arguments: info - a blank user info array of char *'s.
145 * Returns: args - the filled info structure.
149 SetUserDefaults(info)
152 info[U_NAME] = Strsave(UNIQUE_LOGIN);
153 info[U_UID] = Strsave(UNIQUE_UID);
154 info[U_SHELL] = Strsave(DEFAULT_SHELL);
155 info[U_LAST] = Strsave(DEFAULT_NONE);
156 info[U_FIRST] = Strsave(DEFAULT_NONE);
157 info[U_MIDDLE] = Strsave(DEFAULT_NONE);
158 info[U_STATE] = Strsave(DEFAULT_NO);
159 info[U_MITID] = Strsave(DEFAULT_NONE);
160 info[U_CLASS] = Strsave(DEFAULT_CLASS);
161 info[U_COMMENT] = Strsave("");
162 info[U_SIGNATURE] = Strsave("");
163 info[U_SECURE] = Strsave("0");
164 info[U_MODTIME] = info[U_MODBY] = info[U_MODWITH] = info[U_END] = NULL;
169 /* Check that the supplied name follows the capitalization rules, and
170 * offer to correct it if not.
173 CorrectCapitalization(name)
176 char temp_buf[BUFSIZ], fixname[BUFSIZ];
178 strcpy(fixname, *name);
180 if (strcmp(fixname, *name)) {
181 Put_message("You entered a name which does not follow the capitalization conventions.");
182 sprintf(temp_buf, "Correct it to \"%s\"", fixname);
183 if (YesNoQuestion(temp_buf, 1) == TRUE) {
185 *name = strsave(fixname);
191 /* Function Name: AskUserInfo.
192 * Description: This function askes the user for information about a
193 * machine and saves it into a structure.
194 * Arguments: info - a pointer the the structure to put the info into.
195 * flags - Flags asking us which info we want.
196 * Returns: the args to pass to the query.
197 * NOTES: the return args are not necessarily in the correct order to
198 * use the #defined names (e.g args[UID] is not the uid anymore).
202 AskUserInfo(info, name)
208 char temp_buf[BUFSIZ], *newname, *temp_ptr, *sig, sig_buf[BUFSIZ];
211 sprintf(temp_buf,"\nChanging Attributes of user %s.\n",info[U_NAME]);
212 Put_message(temp_buf);
214 struct qelem *elem = NULL;
217 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
219 CorrectCapitalization(&info[U_LAST]);
220 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
222 CorrectCapitalization(&info[U_FIRST]);
223 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) ==
226 CorrectCapitalization(&info[U_MIDDLE]);
227 argv[0] = info[U_FIRST];
228 argv[1] = info[U_LAST];
229 if (do_mr_query("get_user_account_by_name", 2, argv,
230 StoreInfo, (char *) &elem) == 0) {
231 Put_message("A user by that name already exists in the database.");
232 Loop(QueueTop(elem), PrintUserInfo);
233 Loop(QueueTop(elem), FreeInfo);
235 if (YesNoQuestion("Add new user anyway", TRUE) != TRUE)
240 newname = Strsave(info[U_NAME]);
241 if (GetValueFromUser("The new login name for this user", &newname) ==
244 } else if (GetValueFromUser("Login name for this user", &info[U_NAME]) ==
248 if (GetValueFromUser("User's UID", &info[U_UID]) == SUB_ERROR)
250 if (GetValueFromUser("User's shell", &info[U_SHELL]) == SUB_ERROR)
253 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
255 CorrectCapitalization(&info[U_LAST]);
256 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
258 CorrectCapitalization(&info[U_FIRST]);
259 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) ==
262 CorrectCapitalization(&info[U_MIDDLE]);
266 if (GetValueFromUser("User's status (? for help)", &info[U_STATE]) ==
269 if (isdigit(info[U_STATE][0]))
271 Put_message("Valid status numbers:");
272 for (i = 0; i < US_END; i++) {
273 sprintf(temp_buf, " %d: %s", i, states[i]);
274 Put_message(temp_buf);
277 if (GetValueFromUser("User's MIT ID number", &info[U_MITID]) == SUB_ERROR)
279 RemoveHyphens(info[U_MITID]);
280 if (GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]) ==
283 if (GetValueFromUser("Comments", &info[U_COMMENT]) == SUB_ERROR)
286 if (YesNoQuestion("Secure password set",
287 atoi(info[U_SECURE]) ? TRUE : FALSE) == FALSE) {
288 free(info[U_SECURE]);
289 info[U_SECURE] = strsave("0");
290 } else if (!strcmp(info[U_SECURE], "0")) {
294 gettimeofday(&tv, (struct timezone *)NULL);
295 sprintf(buf, "%d", tv.tv_sec);
296 free(info[U_SECURE]);
297 info[U_SECURE] = strsave(buf);
302 if (strcmp(info[U_NAME], UNIQUE_LOGIN)) {
303 sprintf(temp_buf, "%s:%s", info[U_NAME], info[U_MITID]);
305 i = GDSS_Verify(temp_buf, strlen(temp_buf), info[U_SIGNATURE], &si);
306 /* If it's already signed OK, don't resign it. */
307 if (i != GDSS_SUCCESS) {
308 free(info[U_SIGNATURE]);
309 info[U_SIGNATURE] = malloc(GDSS_Sig_Size() * 2);
310 i = GDSS_Sign(temp_buf, strlen(temp_buf), info[U_SIGNATURE]);
311 if (i != GDSS_SUCCESS)
312 com_err(program_name, gdss2et(i), "Failed to create signature");
314 Put_message("Made signature:");hex_dump(info[U_SIGNATURE]);
316 Put_message("Don't need to remake signature");
321 info[U_SIGNATURE] = strsave("");
324 FreeAndClear(&info[U_MODTIME], TRUE);
325 FreeAndClear(&info[U_MODBY], TRUE);
326 FreeAndClear(&info[U_MODWITH], TRUE);
329 * Slide the newname into the #2 slot, this screws up all future references
330 * to this list, since we slip the pointer into a info list it gets freed
331 * when the rest of the list gets freed.
334 SlipInNewName(info, newname);
339 /* Function Name: GetUserInfo
340 * Description: Stores the user information in a queue.
341 * Arguments: type - type of field given to get info, one of:
342 * LOGIN, UID, BY_NAME, CLASS.
343 * name1 - name of thing specified by type (wildcards okay)
344 * name2 - other name, only used in get user by first and last.
346 * Returns: the first element of the queue containing the user info.
350 GetUserInfo(type, name1, name2)
356 struct qelem * elem = NULL;
361 if ( (status = do_mr_query("get_user_account_by_login", 1, args,
362 StoreInfo, (char *) &elem)) != 0) {
363 com_err(program_name, status,
364 " when attempting to get_user_account_by_login.");
370 if ( (status = do_mr_query("get_user_account_by_uid", 1, args,
371 StoreInfo, (char *) &elem)) != 0) {
372 com_err(program_name, status,
373 " when attempting to get_user_account_by_uid.");
380 if ( (status = do_mr_query("get_user_account_by_name", 2, args,
381 StoreInfo, (char *) &elem)) != 0) {
382 com_err(program_name, status,
383 " when attempting to get_user_account_by_name.");
389 if ( (status = do_mr_query("get_user_account_by_class", 1, args,
390 StoreInfo, (char *) &elem)) != 0) {
391 com_err(program_name, status,
392 " when attempting to get_user_account_by_class.");
397 return( QueueTop(elem) );
400 /* Function Name: AddNewUser
401 * Description: Adds a new user to the database.
403 * Returns: DM_NORMAL.
411 char ** args, *info[MAX_ARGS_SIZE];
413 if ((args = AskUserInfo(SetUserDefaults(info), FALSE)) == NULL) {
414 Put_message("Aborted.");
419 if ( (status = do_mr_query("add_user_account", CountArgs(args),
420 args, Scream, (char *) NULL)) != MR_SUCCESS)
421 com_err(program_name, status, " in add_user_account");
423 Put_message("New user added to database.");
429 /* Function Name: GetLoginName
430 * Description: Asks the user for a login name and reserves
433 * Returns: a malloced login name for the user.
442 if (GetValueFromUser("Login name for this user? ", &name) == SUB_ERROR)
444 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
449 /* Function Name: ChooseUser
450 * Description: Choose a user from a list and return the uid.
451 * Arguments: top - a queue of user information.
452 * Returns: uid - the malloced uid of the user that was chosen.
459 while (elem != NULL) {
460 char ** info = (char **) elem->q_data;
462 switch(YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE)) {
464 return(Strsave(info[U_UID]));
467 default: /* quit or ^C. */
475 /* Function Name: GetUidNumberFromName
476 * Description: Gets the users uid number, from the name.
478 * Returns: uid - a malloced string containing the uid.
482 GetUidNumberFromName()
484 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
486 struct qelem * top = NULL;
488 if (!Prompt_input("First Name: ", first, BUFSIZ))
490 if (!Prompt_input("Last Name: ", last, BUFSIZ))
498 switch (status = do_mr_query("get_user_account_by_name", 2, args,
499 StoreInfo, (char *) &top)) {
503 Put_message("There is no user in the database with that name.");
506 com_err(program_name, status, " in get_account_user_by_name.");
511 if (QueueCount(top) == 1) /* This is a unique name. */ {
512 char ** info = (char **) top->q_data;
513 Put_message("User ID Number retrieved for the user: ");
516 uid = Strsave(info[U_UID]);
518 return(Strsave(uid));
521 Put_message("That name is not unique, choose the user that you want.");
522 uid = ChooseUser(top);
527 /* Function Name: SetUserPassword
528 * Description: Set the new kerberos password for this user.
529 * Arguments: name - kerberos principle name for this user, (login name).
534 SetUserPassword(name)
537 name = name; /* make saber happy. */
538 Put_message("Kerberos password not changed, code non-existant.");
539 /* clever message to call account_admin, if this fails. */
542 /* Function Name: GiveBackLogin
543 * Description: Gives back previously reserved kerberous principle.
544 * Arguments: name - principle to give back.
552 name = name; /* make saber happy. */
553 Put_message("kerberos code not implimented, name not given back.");
554 /* send mail to db maintainer if this fails. */
557 /* Function Name: RegisterUser
558 * Description: This function registers a user.
560 * Returns: DM_NORMAL.
566 char * args[MAX_ARGS_SIZE];
567 char *login, *fstype = NULL;
568 char temp_buf[BUFSIZ];
571 Put_message("This function has NO kerberos support, so strange things");
572 Put_message("may happen if you use it to register a user.");
574 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE)) {
576 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
577 args[0] = Strsave(temp_buf);
580 if ( (args[0] = GetUidNumberFromName()) == NULL)
587 if ( ((login = args[1] = GetLoginName()) == NULL) ||
588 ( GetFSTypes(&fstype, FALSE) == SUB_ERROR ) ) {
589 FreeInfo(args); /* This work because the NULL temination is ok. */
595 switch (status = do_mr_query("register_user", CountArgs(args),
596 args, Scream, (char *) NULL)) {
598 sprintf(temp_buf, "User %s successfully registered.", login);
599 Put_message(temp_buf);
600 SetUserPassword(login);
603 GiveBackLogin(login);
604 sprintf(temp_buf, "The username %s is already in use.", login);
605 Put_message(temp_buf);
608 com_err(program_name, status, " in register_user");
615 /* Function Name: RealUpdateUser
616 * Description: actuall updates the user information.
617 * Arguments: info - all current information for the user fields.
618 * junk - an UNUSED boolean.
624 RealUpdateUser(info, junk)
629 char error_buf[BUFSIZ];
630 char ** args = AskUserInfo(info, TRUE);
633 Put_message("Aborted.");
636 if ( (status = do_mr_query("update_user_account", CountArgs(args),
637 args, Scream, (char *) NULL)) != MR_SUCCESS) {
638 com_err(program_name, status, " in ModifyFields");
639 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
640 Put_message(error_buf);
644 /* Function Name: UpdateUser
645 * Description: Modify some of the information about a user.
646 * Arguments: argc, argv - login name of the user in argv[1].
647 * Returns: DM_NORMAL.
652 UpdateUser(argc, argv)
658 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
659 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
665 /* Function Name: RealDeactivateUser
666 * Description: sets the user's status to 3.
667 * Arguments: info - all current information for the user fields
668 * one_item - indicates the user hasn't been queried yet
673 RealDeactivateUser(info, one_item)
678 char txt_buf[BUFSIZ];
682 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
683 if (YesNoQuestion(txt_buf, FALSE) != TRUE)
687 qargs[0] = info[NAME];
689 if ((status = do_mr_query("update_user_status", 2, qargs, Scream,
690 (char *) NULL)) != MR_SUCCESS) {
691 com_err(program_name, status, " in update_user_status");
692 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
693 Put_message(txt_buf);
698 /* Function Name: DeactivateUser
699 * Description: sets the user's status to 3.
700 * Arguments: argc, argv - login name of the user in argv[1].
701 * Returns: DM_NORMAL.
706 DeactivateUser(argc, argv)
712 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
713 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
720 /* ------------------------- Top Menu ------------------------- */
722 /* DeleteUser() in delete.c */
724 /* Function Name: DeleteUserByUid
725 * Description: Deletes the user given a uid number.
726 * Arguments: argc, argv - uid if user in argv[1].
727 * Returns: DM_NORMAL.
728 * NOTES: This just gets the username from the mr server
729 * and performs a DeleteUser().
733 DeleteUserByUid(argc, argv)
738 struct qelem *elem = NULL;
741 if(!ValidName(argv[1]))
744 if ( (status = do_mr_query("get_user_account_by_uid", 1, argv+1, StoreInfo,
745 (char * ) &elem)) != MR_SUCCESS)
746 com_err(program_name, status, " in get_user_account_by_uid");
748 info = (char **) elem->q_data;
749 argv[1] = info[U_NAME];
751 (void) DeleteUser(argc, argv);
755 /* ------------------------- Show User Information ------------------------- */
757 /* Function Name: ShowUserByLogin
758 * Description: Shows user information given a login name.
759 * Arguments: argc, argv - login name in argv[1].
765 ShowUserByLogin(argc, argv)
769 struct qelem *top, *elem;
771 elem = top = GetUserInfo(LOGIN, argv[1], (char *) NULL);
772 Loop(elem, PrintUserInfo);
778 /* Function Name: RetrieveUserByName
779 * Description: Show information on a user give fist and/or last name.
780 * Arguments: argc, argv - argv[1] - first name.
781 * argv[2] - last name.
782 * Returns: DM_NORMAL.
787 ShowUserByName(argc, argv)
794 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
796 if (top == NULL) /* if there was an error then return. */
799 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
806 Loop(top, PrintUserInfo);
810 Loop(top, PrintUserName);
818 /* Function Name: ShowUserByClass
819 * Description: Shows real and login names of all users in class.
820 * Arguments: argc, argv - argv[1] contains the class.
826 ShowUserByClass(argc, argv)
832 if (YesNoQuestion("This will take a long time. Are you sure", 0) == FALSE)
834 top = GetUserInfo(CLASS, argv[1], (char *) NULL);
835 Loop(top, PrintUserName);
842 /* Function Name: GetKrbmap
843 * Description: Shows user <-> Kerberos mappings
844 * Arguments: argc, argv - argv[1] contains the user login name,
845 * argv[2] contains the principal
851 GetKrbmap(argc, argv)
856 struct qelem *elem = NULL, *top;
859 if ((stat = do_mr_query("get_kerberos_user_map", 2, &argv[1],
860 StoreInfo, (char *)&elem)) != 0) {
861 com_err(program_name, stat, " in GetKrbMap.");
865 top = elem = QueueTop(elem);
867 while (elem != NULL) {
868 char **info = (char **) elem->q_data;
869 sprintf(buf, "User: %-9s Principal: %s",
870 info[KMAP_USER], info[KMAP_PRINCIPAL]);
875 FreeQueue(QueueTop(top));
880 /* Function Name: AddKrbmap
881 * Description: Add a new user <-> Kerberos mapping
882 * Arguments: argc, argv - argv[1] contains the user login name,
883 * argv[2] contains the principal
889 AddKrbmap(argc, argv)
895 if (!index(argv[KMAP_PRINCIPAL + 1], '@')) {
896 Put_message("Please specify a realm for the kerberos principal.");
899 if ((stat = do_mr_query("add_kerberos_user_map", 2, &argv[1],
900 Scream, NULL)) != 0) {
901 com_err(program_name, stat, " in AddKrbMap.");
902 if (stat == MR_EXISTS)
903 Put_message("No user or principal may have more than one mapping.");
909 /* Function Name: DeleteKrbmap
910 * Description: Remove a user <-> Kerberos mapping
911 * Arguments: argc, argv - argv[1] contains the user login name,
912 * argv[2] contains the principal
918 DeleteKrbmap(argc, argv)
924 if ((stat = do_mr_query("delete_kerberos_user_map", 2, &argv[1],
925 Scream, NULL)) != 0) {
926 com_err(program_name, stat, " in DeleteKrbMap.");
938 sprintf(buf, "Size: %d", strlen(p));
940 while (strlen(p) >= 8) {
941 sprintf(buf, "%02x %02x %02x %02x %02x %02x %02x %02x",
942 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
948 sprintf(buf, "%02x %02x %02x %02x %02x %02x %02x",
949 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
952 sprintf(buf, "%02x %02x %02x %02x %02x %02x",
953 p[0], p[1], p[2], p[3], p[4], p[5]);
956 sprintf(buf, "%02x %02x %02x %02x %02x",
957 p[0], p[1], p[2], p[3], p[4]);
960 sprintf(buf, "%02x %02x %02x %02x",
961 p[0], p[1], p[2], p[3]);
964 sprintf(buf, "%02x %02x %02x",
968 sprintf(buf, "%02x %02x",