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",
107 UserState(atoi(info[U_STATE])), info[U_MITID]);
109 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME],info[U_MODWITH]);
113 /* Function Name: SetUserDefaults
114 * Description: Sets the default values for add user.
115 * Arguments: info - a blank user info array of char *'s.
116 * Returns: args - the filled info structure.
120 SetUserDefaults(info)
123 info[U_NAME] = Strsave(UNIQUE_LOGIN);
124 info[U_UID] = Strsave(UNIQUE_UID);
125 info[U_SHELL] = Strsave(DEFAULT_SHELL);
126 info[U_LAST] = Strsave(DEFAULT_NONE);
127 info[U_FIRST] = Strsave(DEFAULT_NONE);
128 info[U_MIDDLE] = Strsave(DEFAULT_NONE);
129 info[U_STATE] = Strsave(DEFAULT_NO);
130 info[U_MITID] = Strsave(DEFAULT_NONE);
131 info[U_CLASS] = Strsave(DEFAULT_CLASS);
132 info[U_MODTIME] = info[U_MODBY] = info[U_MODWITH] = info[U_END] = NULL;
137 /* Check that the supplied name follows the capitalization rules, and
138 * offer to correct it if not.
141 CorrectCapitalization(name)
144 char temp_buf[BUFSIZ], fixname[BUFSIZ];
146 strcpy(fixname, *name);
148 if (strcmp(fixname, *name)) {
149 Put_message("You entered a name which does not follow the capitalization conventions.");
150 sprintf(temp_buf, "Correct it to \"%s\"", fixname);
151 if (YesNoQuestion(temp_buf, 1) == TRUE) {
153 *name = strsave(fixname);
159 /* Function Name: AskUserInfo.
160 * Description: This function askes the user for information about a
161 * machine and saves it into a structure.
162 * Arguments: info - a pointer the the structure to put the info into.
163 * flags - Flags asking us which info we want.
164 * Returns: the args to pass to the query.
165 * NOTES: the return args are not necessarily in the correct order to
166 * use the #defined names (e.g args[UID] is not the uid anymore).
170 AskUserInfo(info, name)
174 char temp_buf[BUFSIZ], *newname, *temp_ptr;
177 sprintf(temp_buf,"\nChanging Attributes of user %s.\n",info[U_NAME]);
178 Put_message(temp_buf);
180 struct qelem *elem = NULL;
183 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
185 CorrectCapitalization(&info[U_LAST]);
186 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
188 CorrectCapitalization(&info[U_FIRST]);
189 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) ==
192 CorrectCapitalization(&info[U_MIDDLE]);
193 argv[0] = info[U_FIRST];
194 argv[1] = info[U_LAST];
195 if (do_mr_query("get_user_by_name", 2, argv,
196 StoreInfo, (char *) &elem) == 0) {
197 Put_message("A user by that name already exists in the database.");
198 Loop(QueueTop(elem), PrintUserInfo);
199 Loop(QueueTop(elem), FreeInfo);
201 if (YesNoQuestion("Add new user anyway", TRUE) != TRUE)
206 newname = Strsave(info[U_NAME]);
207 if (GetValueFromUser("The new login name for this user", &newname) ==
210 } else if (GetValueFromUser("Login name for this user", &info[U_NAME]) ==
214 if (GetValueFromUser("User's UID", &info[U_UID]) == SUB_ERROR)
216 if (GetValueFromUser("User's shell", &info[U_SHELL]) == SUB_ERROR)
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]);
232 if (GetValueFromUser("User's status (? for help)", &info[U_STATE]) ==
235 if (isdigit(info[U_STATE][0]))
237 Put_message("Valid status numbers:");
238 for (i = 0; i < US_END; i++) {
239 sprintf(temp_buf, " %d: %s", i, states[i]);
240 Put_message(temp_buf);
243 if (GetValueFromUser("User's MIT ID number", &info[U_MITID]) == SUB_ERROR)
245 if (GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]) ==
249 FreeAndClear(&info[U_MODTIME], TRUE);
250 FreeAndClear(&info[U_MODBY], TRUE);
251 FreeAndClear(&info[U_MODWITH], TRUE);
254 * Slide the newname into the #2 slot, this screws up all future references
255 * to this list, since we slip the pointer into a info list it gets freed
256 * when the rest of the list gets freed.
259 SlipInNewName(info, newname);
264 /* Function Name: GetUserInfo
265 * Description: Stores the user information in a queue.
266 * Arguments: type - type of field given to get info, one of:
267 * LOGIN, UID, BY_NAME, CLASS.
268 * name1 - name of thing specified by type (wildcards okay)
269 * name2 - other name, only used in get user by first and last.
271 * Returns: the first element of the queue containing the user info.
275 GetUserInfo(type, name1, name2)
281 struct qelem * elem = NULL;
286 if ( (status = do_mr_query("get_user_by_login", 1, args,
287 StoreInfo, (char *) &elem)) != 0) {
288 com_err(program_name, status,
289 " when attempting to get_user_by_login.");
295 if ( (status = do_mr_query("get_user_by_uid", 1, args,
296 StoreInfo, (char *) &elem)) != 0) {
297 com_err(program_name, status,
298 " when attempting to get_user_by_uid.");
305 if ( (status = do_mr_query("get_user_by_name", 2, args,
306 StoreInfo, (char *) &elem)) != 0) {
307 com_err(program_name, status,
308 " when attempting to get_user_by_name.");
314 if ( (status = do_mr_query("get_user_by_class", 1, args,
315 StoreInfo, (char *) &elem)) != 0) {
316 com_err(program_name, status,
317 " when attempting to get_user_by_class.");
322 return( QueueTop(elem) );
325 /* Function Name: AddNewUser
326 * Description: Adds a new user to the database.
328 * Returns: DM_NORMAL.
336 char ** args, *info[MAX_ARGS_SIZE];
338 if ((args = AskUserInfo(SetUserDefaults(info), FALSE)) == NULL) {
339 Put_message("Aborted.");
344 if ( (status = do_mr_query("add_user", CountArgs(args),
345 args, Scream, (char *) NULL)) != MR_SUCCESS)
346 com_err(program_name, status, " in add_user");
348 Put_message("New user added to database.");
354 /* Function Name: GetLoginName
355 * Description: Asks the user for a login name and reserves
358 * Returns: a malloced login name for the user.
367 if (GetValueFromUser("Login name for this user? ", &name) == SUB_ERROR)
369 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
374 /* Function Name: ChooseUser
375 * Description: Choose a user from a list and return the uid.
376 * Arguments: top - a queue of user information.
377 * Returns: uid - the malloced uid of the user that was chosen.
384 while (elem != NULL) {
385 char ** info = (char **) elem->q_data;
387 switch(YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE)) {
389 return(Strsave(info[U_UID]));
392 default: /* quit or ^C. */
400 /* Function Name: GetUidNumberFromName
401 * Description: Gets the users uid number, from the name.
403 * Returns: uid - a malloced string containing the uid.
407 GetUidNumberFromName()
409 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
411 struct qelem * top = NULL;
413 if (!Prompt_input("First Name: ", first, BUFSIZ))
415 if (!Prompt_input("Last Name: ", last, BUFSIZ))
423 switch (status = do_mr_query("get_user_by_name", 2, args,
424 StoreInfo, (char *) &top)) {
428 Put_message("There is no user in the database with that name.");
431 com_err(program_name, status, " in get_user_by_name.");
436 if (QueueCount(top) == 1) /* This is a unique name. */ {
437 char ** info = (char **) top->q_data;
438 Put_message("User ID Number retrieved for the user: ");
441 uid = Strsave(info[U_UID]);
443 return(Strsave(uid));
446 Put_message("That name is not unique, choose the user that you want.");
447 uid = ChooseUser(top);
452 /* Function Name: SetUserPassword
453 * Description: Set the new kerberos password for this user.
454 * Arguments: name - kerberos principle name for this user, (login name).
459 SetUserPassword(name)
462 name = name; /* make saber happy. */
463 Put_message("Kerberos password not changed, code non-existant.");
464 /* clever message to call account_admin, if this fails. */
467 /* Function Name: GiveBackLogin
468 * Description: Gives back previously reserved kerberous principle.
469 * Arguments: name - principle to give back.
477 name = name; /* make saber happy. */
478 Put_message("kerberos code not implimented, name not given back.");
479 /* send mail to db maintainer if this fails. */
482 /* Function Name: RegisterUser
483 * Description: This function registers a user.
485 * Returns: DM_NORMAL.
491 char * args[MAX_ARGS_SIZE];
492 char *login, *fstype = NULL;
493 char temp_buf[BUFSIZ];
496 Put_message("This function has NO kerberos support, so strange things");
497 Put_message("may happen if you use it to register a user.");
499 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE)) {
501 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
502 args[0] = Strsave(temp_buf);
505 if ( (args[0] = GetUidNumberFromName()) == NULL)
512 if ( ((login = args[1] = GetLoginName()) == NULL) ||
513 ( GetFSTypes(&fstype, FALSE) == SUB_ERROR ) ) {
514 FreeInfo(args); /* This work because the NULL temination is ok. */
520 switch (status = do_mr_query("register_user", CountArgs(args),
521 args, Scream, (char *) NULL)) {
523 sprintf(temp_buf, "User %s successfully registered.", login);
524 Put_message(temp_buf);
525 SetUserPassword(login);
528 GiveBackLogin(login);
529 sprintf(temp_buf, "The username %s is already in use.", login);
530 Put_message(temp_buf);
533 com_err(program_name, status, " in register_user");
540 /* Function Name: RealUpdateUser
541 * Description: actuall updates the user information.
542 * Arguments: info - all current information for the user fields.
543 * junk - an UNUSED boolean.
549 RealUpdateUser(info, junk)
554 char error_buf[BUFSIZ];
555 char ** args = AskUserInfo(info, TRUE);
558 Put_message("Aborted.");
561 if ( (status = do_mr_query("update_user", CountArgs(args),
562 args, Scream, (char *) NULL)) != MR_SUCCESS) {
563 com_err(program_name, status, " in ModifyFields");
564 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
565 Put_message(error_buf);
569 /* Function Name: UpdateUser
570 * Description: Modify some of the information about a user.
571 * Arguments: argc, argv - login name of the user in argv[1].
572 * Returns: DM_NORMAL.
577 UpdateUser(argc, argv)
583 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
584 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
590 /* Function Name: RealDeactivateUser
591 * Description: sets the user's status to 3.
592 * Arguments: info - all current information for the user fields
593 * one_item - indicates the user hasn't been queried yet
598 RealDeactivateUser(info, one_item)
603 char txt_buf[BUFSIZ];
607 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
608 if (YesNoQuestion(txt_buf, FALSE) != TRUE)
612 qargs[0] = info[NAME];
614 if ((status = do_mr_query("update_user_status", 2, qargs, Scream,
615 (char *) NULL)) != MR_SUCCESS) {
616 com_err(program_name, status, " in update_user_status");
617 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
618 Put_message(txt_buf);
623 /* Function Name: DeactivateUser
624 * Description: sets the user's status to 3.
625 * Arguments: argc, argv - login name of the user in argv[1].
626 * Returns: DM_NORMAL.
631 DeactivateUser(argc, argv)
637 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
638 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
645 /* ------------------------- Top Menu ------------------------- */
647 /* DeleteUser() in delete.c */
649 /* Function Name: DeleteUserByUid
650 * Description: Deletes the user given a uid number.
651 * Arguments: argc, argv - uid if user in argv[1].
652 * Returns: DM_NORMAL.
653 * NOTES: This just gets the username from the mr server
654 * and performs a DeleteUser().
658 DeleteUserByUid(argc, argv)
663 struct qelem *elem = NULL;
666 if(!ValidName(argv[1]))
669 if ( (status = do_mr_query("get_user_by_uid", 1, argv+1, StoreInfo,
670 (char * ) &elem)) != MR_SUCCESS)
671 com_err(program_name, status, " in get_user_by_uid");
673 info = (char **) elem->q_data;
674 argv[1] = info[U_NAME];
676 (void) DeleteUser(argc, argv);
680 /* ------------------------- Show User Information ------------------------- */
682 /* Function Name: ShowUserByLogin
683 * Description: Shows user information given a login name.
684 * Arguments: argc, argv - login name in argv[1].
690 ShowUserByLogin(argc, argv)
694 struct qelem *top, *elem;
696 elem = top = GetUserInfo(LOGIN, argv[1], (char *) NULL);
697 Loop(elem, PrintUserInfo);
703 /* Function Name: RetrieveUserByName
704 * Description: Show information on a user give fist and/or last name.
705 * Arguments: argc, argv - argv[1] - first name.
706 * argv[2] - last name.
707 * Returns: DM_NORMAL.
712 ShowUserByName(argc, argv)
719 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
721 if (top == NULL) /* if there was an error then return. */
724 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
731 Loop(top, PrintUserInfo);
735 Loop(top, PrintUserName);
743 /* Function Name: ShowUserByClass
744 * Description: Shows real and login names of all users in class.
745 * Arguments: argc, argv - argv[1] contains the class.
751 ShowUserByClass(argc, argv)
757 top = GetUserInfo(CLASS, argv[1], (char *) NULL);
758 Loop(top, PrintUserName);
765 /* Function Name: GetKrbmap
766 * Description: Shows user <-> Kerberos mappings
767 * Arguments: argc, argv - argv[1] contains the user login name,
768 * argv[2] contains the principal
774 GetKrbmap(argc, argv)
779 struct qelem *elem = NULL, *top;
782 if ((stat = do_mr_query("get_kerberos_user_map", 2, &argv[1],
783 StoreInfo, (char *)&elem)) != 0) {
784 com_err(program_name, stat, " in GetKrbMap.");
788 top = elem = QueueTop(elem);
790 while (elem != NULL) {
791 char **info = (char **) elem->q_data;
792 sprintf(buf, "User: %-9s Principal: %s",
793 info[KMAP_USER], info[KMAP_PRINCIPAL]);
798 FreeQueue(QueueTop(top));
803 /* Function Name: AddKrbmap
804 * Description: Add a new user <-> Kerberos mapping
805 * Arguments: argc, argv - argv[1] contains the user login name,
806 * argv[2] contains the principal
812 AddKrbmap(argc, argv)
818 if (!index(argv[KMAP_PRINCIPAL + 1], '@')) {
819 Put_message("Please specify a realm for the kerberos principal.");
822 if ((stat = do_mr_query("add_kerberos_user_map", 2, &argv[1],
823 Scream, NULL)) != 0) {
824 com_err(program_name, stat, " in AddKrbMap.");
825 if (stat == MR_EXISTS)
826 Put_message("No user or principal may have more than one mapping.");
832 /* Function Name: DeleteKrbmap
833 * Description: Remove a user <-> Kerberos mapping
834 * Arguments: argc, argv - argv[1] contains the user login name,
835 * argv[2] contains the principal
841 DeleteKrbmap(argc, argv)
847 if ((stat = do_mr_query("delete_kerberos_user_map", 2, &argv[1],
848 Scream, NULL)) != 0) {
849 com_err(program_name, stat, " in DeleteKrbMap.");