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>
29 #include "mit-copyright.h"
39 #define DEFAULT_SHELL "/bin/csh"
40 #define DEFAULT_CLASS "?"
43 /* Function Name: UserState
44 * Description: Convert a numeric state into a descriptive string.
45 * Arguments: state value
46 * Returns: pointer to statically allocated string.
49 static char *states[] = { "Registerable (0)",
51 "Half Registered (2)",
53 "Not registerable (4)",
54 "Enrolled/Registerable (5)",
55 "Enrolled/Not Registerable (6)",
56 "Half Enrolled (7)" };
58 static char *UserState(state)
63 if (state < 0 || state >= US_END) {
64 sprintf(buf, "Unknown (%d)", state);
67 return(states[state]);
71 /* Function Name: PrintUserName
72 * Description: Print name of a user.
73 * Arguments: info - the information about a user.
81 char buf[BUFSIZ], print_buf[BUFSIZ];
82 sprintf(buf, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
83 sprintf(print_buf, "%-40s User Name: %s", buf, info[U_NAME]);
84 Put_message(print_buf);
87 /* Function Name: PrintUserInfo
88 * Description: Prints Information about a user.
89 * Arguments: info - an argument list with the user information
98 char name[BUFSIZ], buf[BUFSIZ];
100 sprintf(name, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
101 sprintf(buf, "Login name: %-20s Full name: %s", info[U_NAME], name);
103 sprintf(buf, "User id: %-23s Login shell %-10s Class: %s",
104 info[U_UID], info[U_SHELL], info[U_CLASS]);
106 sprintf(buf, "Account is: %-20s MIT ID number: %s Signed: %s",
107 UserState(atoi(info[U_STATE])), info[U_MITID],
108 *info[U_SIGNATURE] ? "Yes" : "No");
110 sprintf(buf, "Comments: %s", info[U_COMMENT]);
112 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME],info[U_MODWITH]);
116 /* Function Name: SetUserDefaults
117 * Description: Sets the default values for add user.
118 * Arguments: info - a blank user info array of char *'s.
119 * Returns: args - the filled info structure.
123 SetUserDefaults(info)
126 info[U_NAME] = Strsave(UNIQUE_LOGIN);
127 info[U_UID] = Strsave(UNIQUE_UID);
128 info[U_SHELL] = Strsave(DEFAULT_SHELL);
129 info[U_LAST] = Strsave(DEFAULT_NONE);
130 info[U_FIRST] = Strsave(DEFAULT_NONE);
131 info[U_MIDDLE] = Strsave(DEFAULT_NONE);
132 info[U_STATE] = Strsave(DEFAULT_NO);
133 info[U_MITID] = Strsave(DEFAULT_NONE);
134 info[U_CLASS] = Strsave(DEFAULT_CLASS);
135 info[U_COMMENT] = Strsave("");
136 info[U_SIGNATURE] = Strsave("");
137 info[U_MODTIME] = info[U_MODBY] = info[U_MODWITH] = info[U_END] = NULL;
142 /* Check that the supplied name follows the capitalization rules, and
143 * offer to correct it if not.
146 CorrectCapitalization(name)
149 char temp_buf[BUFSIZ], fixname[BUFSIZ];
151 strcpy(fixname, *name);
153 if (strcmp(fixname, *name)) {
154 Put_message("You entered a name which does not follow the capitalization conventions.");
155 sprintf(temp_buf, "Correct it to \"%s\"", fixname);
156 if (YesNoQuestion(temp_buf, 1) == TRUE) {
158 *name = strsave(fixname);
164 /* Function Name: AskUserInfo.
165 * Description: This function askes the user for information about a
166 * machine and saves it into a structure.
167 * Arguments: info - a pointer the the structure to put the info into.
168 * flags - Flags asking us which info we want.
169 * Returns: the args to pass to the query.
170 * NOTES: the return args are not necessarily in the correct order to
171 * use the #defined names (e.g args[UID] is not the uid anymore).
175 AskUserInfo(info, name)
180 char temp_buf[BUFSIZ], *newname, *temp_ptr, *sig;
183 sprintf(temp_buf,"\nChanging Attributes of user %s.\n",info[U_NAME]);
184 Put_message(temp_buf);
186 struct qelem *elem = NULL;
189 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
191 CorrectCapitalization(&info[U_LAST]);
192 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
194 CorrectCapitalization(&info[U_FIRST]);
195 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) ==
198 CorrectCapitalization(&info[U_MIDDLE]);
199 argv[0] = info[U_FIRST];
200 argv[1] = info[U_LAST];
201 if (do_mr_query("get_user_account_by_name", 2, argv,
202 StoreInfo, (char *) &elem) == 0) {
203 Put_message("A user by that name already exists in the database.");
204 Loop(QueueTop(elem), PrintUserInfo);
205 Loop(QueueTop(elem), FreeInfo);
207 if (YesNoQuestion("Add new user anyway", TRUE) != TRUE)
212 newname = Strsave(info[U_NAME]);
213 if (GetValueFromUser("The new login name for this user", &newname) ==
216 } else if (GetValueFromUser("Login name for this user", &info[U_NAME]) ==
220 if (GetValueFromUser("User's UID", &info[U_UID]) == SUB_ERROR)
222 if (GetValueFromUser("User's shell", &info[U_SHELL]) == SUB_ERROR)
225 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
227 CorrectCapitalization(&info[U_LAST]);
228 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
230 CorrectCapitalization(&info[U_FIRST]);
231 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) ==
234 CorrectCapitalization(&info[U_MIDDLE]);
238 if (GetValueFromUser("User's status (? for help)", &info[U_STATE]) ==
241 if (isdigit(info[U_STATE][0]))
243 Put_message("Valid status numbers:");
244 for (i = 0; i < US_END; i++) {
245 sprintf(temp_buf, " %d: %s", i, states[i]);
246 Put_message(temp_buf);
249 if (GetValueFromUser("User's MIT ID number", &info[U_MITID]) == SUB_ERROR)
251 RemoveHyphens(info[U_MITID]);
252 if (GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]) ==
255 if (GetValueFromUser("Comments", &info[U_COMMENT]) == SUB_ERROR)
260 info[U_SIGNATURE] = malloc(GDSS_Sig_Size());
261 sprintf(temp_buf, "%s:%s", info[U_NAME], info[U_MITID]);
262 GDSS_Sign(temp_buf, strlen(temp_buf), info[U_SIGNATURE], &siglen);
264 info[U_SIGNATURE] = strsave("");
267 FreeAndClear(&info[U_MODTIME], TRUE);
268 FreeAndClear(&info[U_MODBY], TRUE);
269 FreeAndClear(&info[U_MODWITH], TRUE);
272 * Slide the newname into the #2 slot, this screws up all future references
273 * to this list, since we slip the pointer into a info list it gets freed
274 * when the rest of the list gets freed.
277 SlipInNewName(info, newname);
282 /* Function Name: GetUserInfo
283 * Description: Stores the user information in a queue.
284 * Arguments: type - type of field given to get info, one of:
285 * LOGIN, UID, BY_NAME, CLASS.
286 * name1 - name of thing specified by type (wildcards okay)
287 * name2 - other name, only used in get user by first and last.
289 * Returns: the first element of the queue containing the user info.
293 GetUserInfo(type, name1, name2)
299 struct qelem * elem = NULL;
304 if ( (status = do_mr_query("get_user_account_by_login", 1, args,
305 StoreInfo, (char *) &elem)) != 0) {
306 com_err(program_name, status,
307 " when attempting to get_user_account_by_login.");
313 if ( (status = do_mr_query("get_user_account_by_uid", 1, args,
314 StoreInfo, (char *) &elem)) != 0) {
315 com_err(program_name, status,
316 " when attempting to get_user_account_by_uid.");
323 if ( (status = do_mr_query("get_user_account_by_name", 2, args,
324 StoreInfo, (char *) &elem)) != 0) {
325 com_err(program_name, status,
326 " when attempting to get_user_account_by_name.");
332 if ( (status = do_mr_query("get_user_account_by_class", 1, args,
333 StoreInfo, (char *) &elem)) != 0) {
334 com_err(program_name, status,
335 " when attempting to get_user_account_by_class.");
340 return( QueueTop(elem) );
343 /* Function Name: AddNewUser
344 * Description: Adds a new user to the database.
346 * Returns: DM_NORMAL.
354 char ** args, *info[MAX_ARGS_SIZE];
356 if ((args = AskUserInfo(SetUserDefaults(info), FALSE)) == NULL) {
357 Put_message("Aborted.");
362 if ( (status = do_mr_query("add_user_account", CountArgs(args),
363 args, Scream, (char *) NULL)) != MR_SUCCESS)
364 com_err(program_name, status, " in add_user_account");
366 Put_message("New user added to database.");
372 /* Function Name: GetLoginName
373 * Description: Asks the user for a login name and reserves
376 * Returns: a malloced login name for the user.
385 if (GetValueFromUser("Login name for this user? ", &name) == SUB_ERROR)
387 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
392 /* Function Name: ChooseUser
393 * Description: Choose a user from a list and return the uid.
394 * Arguments: top - a queue of user information.
395 * Returns: uid - the malloced uid of the user that was chosen.
402 while (elem != NULL) {
403 char ** info = (char **) elem->q_data;
405 switch(YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE)) {
407 return(Strsave(info[U_UID]));
410 default: /* quit or ^C. */
418 /* Function Name: GetUidNumberFromName
419 * Description: Gets the users uid number, from the name.
421 * Returns: uid - a malloced string containing the uid.
425 GetUidNumberFromName()
427 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
429 struct qelem * top = NULL;
431 if (!Prompt_input("First Name: ", first, BUFSIZ))
433 if (!Prompt_input("Last Name: ", last, BUFSIZ))
441 switch (status = do_mr_query("get_user_account_by_name", 2, args,
442 StoreInfo, (char *) &top)) {
446 Put_message("There is no user in the database with that name.");
449 com_err(program_name, status, " in get_account_user_by_name.");
454 if (QueueCount(top) == 1) /* This is a unique name. */ {
455 char ** info = (char **) top->q_data;
456 Put_message("User ID Number retrieved for the user: ");
459 uid = Strsave(info[U_UID]);
461 return(Strsave(uid));
464 Put_message("That name is not unique, choose the user that you want.");
465 uid = ChooseUser(top);
470 /* Function Name: SetUserPassword
471 * Description: Set the new kerberos password for this user.
472 * Arguments: name - kerberos principle name for this user, (login name).
477 SetUserPassword(name)
480 name = name; /* make saber happy. */
481 Put_message("Kerberos password not changed, code non-existant.");
482 /* clever message to call account_admin, if this fails. */
485 /* Function Name: GiveBackLogin
486 * Description: Gives back previously reserved kerberous principle.
487 * Arguments: name - principle to give back.
495 name = name; /* make saber happy. */
496 Put_message("kerberos code not implimented, name not given back.");
497 /* send mail to db maintainer if this fails. */
500 /* Function Name: RegisterUser
501 * Description: This function registers a user.
503 * Returns: DM_NORMAL.
509 char * args[MAX_ARGS_SIZE];
510 char *login, *fstype = NULL;
511 char temp_buf[BUFSIZ];
514 Put_message("This function has NO kerberos support, so strange things");
515 Put_message("may happen if you use it to register a user.");
517 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE)) {
519 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
520 args[0] = Strsave(temp_buf);
523 if ( (args[0] = GetUidNumberFromName()) == NULL)
530 if ( ((login = args[1] = GetLoginName()) == NULL) ||
531 ( GetFSTypes(&fstype, FALSE) == SUB_ERROR ) ) {
532 FreeInfo(args); /* This work because the NULL temination is ok. */
538 switch (status = do_mr_query("register_user", CountArgs(args),
539 args, Scream, (char *) NULL)) {
541 sprintf(temp_buf, "User %s successfully registered.", login);
542 Put_message(temp_buf);
543 SetUserPassword(login);
546 GiveBackLogin(login);
547 sprintf(temp_buf, "The username %s is already in use.", login);
548 Put_message(temp_buf);
551 com_err(program_name, status, " in register_user");
558 /* Function Name: RealUpdateUser
559 * Description: actuall updates the user information.
560 * Arguments: info - all current information for the user fields.
561 * junk - an UNUSED boolean.
567 RealUpdateUser(info, junk)
572 char error_buf[BUFSIZ];
573 char ** args = AskUserInfo(info, TRUE);
576 Put_message("Aborted.");
579 if ( (status = do_mr_query("update_user_account", CountArgs(args),
580 args, Scream, (char *) NULL)) != MR_SUCCESS) {
581 com_err(program_name, status, " in ModifyFields");
582 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
583 Put_message(error_buf);
587 /* Function Name: UpdateUser
588 * Description: Modify some of the information about a user.
589 * Arguments: argc, argv - login name of the user in argv[1].
590 * Returns: DM_NORMAL.
595 UpdateUser(argc, argv)
601 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
602 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
608 /* Function Name: RealDeactivateUser
609 * Description: sets the user's status to 3.
610 * Arguments: info - all current information for the user fields
611 * one_item - indicates the user hasn't been queried yet
616 RealDeactivateUser(info, one_item)
621 char txt_buf[BUFSIZ];
625 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
626 if (YesNoQuestion(txt_buf, FALSE) != TRUE)
630 qargs[0] = info[NAME];
632 if ((status = do_mr_query("update_user_status", 2, qargs, Scream,
633 (char *) NULL)) != MR_SUCCESS) {
634 com_err(program_name, status, " in update_user_status");
635 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
636 Put_message(txt_buf);
641 /* Function Name: DeactivateUser
642 * Description: sets the user's status to 3.
643 * Arguments: argc, argv - login name of the user in argv[1].
644 * Returns: DM_NORMAL.
649 DeactivateUser(argc, argv)
655 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
656 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
663 /* ------------------------- Top Menu ------------------------- */
665 /* DeleteUser() in delete.c */
667 /* Function Name: DeleteUserByUid
668 * Description: Deletes the user given a uid number.
669 * Arguments: argc, argv - uid if user in argv[1].
670 * Returns: DM_NORMAL.
671 * NOTES: This just gets the username from the mr server
672 * and performs a DeleteUser().
676 DeleteUserByUid(argc, argv)
681 struct qelem *elem = NULL;
684 if(!ValidName(argv[1]))
687 if ( (status = do_mr_query("get_user_account_by_uid", 1, argv+1, StoreInfo,
688 (char * ) &elem)) != MR_SUCCESS)
689 com_err(program_name, status, " in get_user_account_by_uid");
691 info = (char **) elem->q_data;
692 argv[1] = info[U_NAME];
694 (void) DeleteUser(argc, argv);
698 /* ------------------------- Show User Information ------------------------- */
700 /* Function Name: ShowUserByLogin
701 * Description: Shows user information given a login name.
702 * Arguments: argc, argv - login name in argv[1].
708 ShowUserByLogin(argc, argv)
712 struct qelem *top, *elem;
714 elem = top = GetUserInfo(LOGIN, argv[1], (char *) NULL);
715 Loop(elem, PrintUserInfo);
721 /* Function Name: RetrieveUserByName
722 * Description: Show information on a user give fist and/or last name.
723 * Arguments: argc, argv - argv[1] - first name.
724 * argv[2] - last name.
725 * Returns: DM_NORMAL.
730 ShowUserByName(argc, argv)
737 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
739 if (top == NULL) /* if there was an error then return. */
742 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
749 Loop(top, PrintUserInfo);
753 Loop(top, PrintUserName);
761 /* Function Name: ShowUserByClass
762 * Description: Shows real and login names of all users in class.
763 * Arguments: argc, argv - argv[1] contains the class.
769 ShowUserByClass(argc, argv)
775 if (YesNoQuestion("This will take a long time. Are you sure", 0) == FALSE)
777 top = GetUserInfo(CLASS, argv[1], (char *) NULL);
778 Loop(top, PrintUserName);
785 /* Function Name: GetKrbmap
786 * Description: Shows user <-> Kerberos mappings
787 * Arguments: argc, argv - argv[1] contains the user login name,
788 * argv[2] contains the principal
794 GetKrbmap(argc, argv)
799 struct qelem *elem = NULL, *top;
802 if ((stat = do_mr_query("get_kerberos_user_map", 2, &argv[1],
803 StoreInfo, (char *)&elem)) != 0) {
804 com_err(program_name, stat, " in GetKrbMap.");
808 top = elem = QueueTop(elem);
810 while (elem != NULL) {
811 char **info = (char **) elem->q_data;
812 sprintf(buf, "User: %-9s Principal: %s",
813 info[KMAP_USER], info[KMAP_PRINCIPAL]);
818 FreeQueue(QueueTop(top));
823 /* Function Name: AddKrbmap
824 * Description: Add a new user <-> Kerberos mapping
825 * Arguments: argc, argv - argv[1] contains the user login name,
826 * argv[2] contains the principal
832 AddKrbmap(argc, argv)
838 if (!index(argv[KMAP_PRINCIPAL + 1], '@')) {
839 Put_message("Please specify a realm for the kerberos principal.");
842 if ((stat = do_mr_query("add_kerberos_user_map", 2, &argv[1],
843 Scream, NULL)) != 0) {
844 com_err(program_name, stat, " in AddKrbMap.");
845 if (stat == MR_EXISTS)
846 Put_message("No user or principal may have more than one mapping.");
852 /* Function Name: DeleteKrbmap
853 * Description: Remove a user <-> Kerberos mapping
854 * Arguments: argc, argv - argv[1] contains the user login name,
855 * argv[2] contains the principal
861 DeleteKrbmap(argc, argv)
867 if ((stat = do_mr_query("delete_kerberos_user_map", 2, &argv[1],
868 Scream, NULL)) != 0) {
869 com_err(program_name, stat, " in DeleteKrbMap.");