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 Encrypted 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], fixname[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 temp_ptr = Strsave(info[U_MITID]);
244 Put_message("User's MIT ID number (unencrypted, or encryption in quotes)");
245 if (GetValueFromUser("", &temp_ptr) == SUB_ERROR)
247 if ( strcmp( temp_ptr, info[U_MITID] ) != 0) {
248 if (temp_ptr[0] == '"' &&
249 temp_ptr[strlen(temp_ptr) - 1] == '"') {
251 temp_ptr[strlen(temp_ptr) - 1] = 0;
252 info[U_MITID] = Strsave(++temp_ptr);
254 EncryptID(temp_buf, temp_ptr, info[U_FIRST], info[U_LAST]);
256 info[U_MITID] = Strsave(temp_buf);
260 if (GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]) ==
264 FreeAndClear(&info[U_MODTIME], TRUE);
265 FreeAndClear(&info[U_MODBY], TRUE);
266 FreeAndClear(&info[U_MODWITH], TRUE);
269 * Slide the newname into the #2 slot, this screws up all future references
270 * to this list, since we slip the pointer into a info list it gets freed
271 * when the rest of the list gets freed.
274 SlipInNewName(info, newname);
279 /* Function Name: GetUserInfo
280 * Description: Stores the user information in a queue.
281 * Arguments: type - type of field given to get info, one of:
282 * LOGIN, UID, BY_NAME, CLASS.
283 * name1 - name of thing specified by type (wildcards okay)
284 * name2 - other name, only used in get user by first and last.
286 * Returns: the first element of the queue containing the user info.
290 GetUserInfo(type, name1, name2)
296 struct qelem * elem = NULL;
301 if ( (status = do_mr_query("get_user_by_login", 1, args,
302 StoreInfo, (char *) &elem)) != 0) {
303 com_err(program_name, status,
304 " when attempting to get_user_by_login.");
310 if ( (status = do_mr_query("get_user_by_uid", 1, args,
311 StoreInfo, (char *) &elem)) != 0) {
312 com_err(program_name, status,
313 " when attempting to get_user_by_uid.");
320 if ( (status = do_mr_query("get_user_by_name", 2, args,
321 StoreInfo, (char *) &elem)) != 0) {
322 com_err(program_name, status,
323 " when attempting to get_user_by_name.");
329 if ( (status = do_mr_query("get_user_by_class", 1, args,
330 StoreInfo, (char *) &elem)) != 0) {
331 com_err(program_name, status,
332 " when attempting to get_user_by_class.");
337 return( QueueTop(elem) );
340 /* Function Name: AddNewUser
341 * Description: Adds a new user to the database.
343 * Returns: DM_NORMAL.
351 char ** args, *info[MAX_ARGS_SIZE];
353 if ((args = AskUserInfo(SetUserDefaults(info), FALSE)) == NULL) {
354 Put_message("Aborted.");
359 if ( (status = do_mr_query("add_user", CountArgs(args),
360 args, Scream, (char *) NULL)) != MR_SUCCESS)
361 com_err(program_name, status, " in add_user");
363 Put_message("New user added to database.");
369 /* Function Name: GetLoginName
370 * Description: Asks the user for a login name and reserves
373 * Returns: a malloced login name for the user.
382 if (GetValueFromUser("Login name for this user? ", &name) == SUB_ERROR)
384 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
389 /* Function Name: ChooseUser
390 * Description: Choose a user from a list and return the uid.
391 * Arguments: top - a queue of user information.
392 * Returns: uid - the malloced uid of the user that was chosen.
399 while (elem != NULL) {
400 char ** info = (char **) elem->q_data;
402 switch(YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE)) {
404 return(Strsave(info[U_UID]));
407 default: /* quit or ^C. */
415 /* Function Name: GetUidNumberFromName
416 * Description: Gets the users uid number, from the name.
418 * Returns: uid - a malloced string containing the uid.
422 GetUidNumberFromName()
424 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
426 struct qelem * top = NULL;
428 if (!Prompt_input("First Name: ", first, BUFSIZ))
430 if (!Prompt_input("Last Name: ", last, BUFSIZ))
438 switch (status = do_mr_query("get_user_by_name", 2, args,
439 StoreInfo, (char *) &top)) {
443 Put_message("There is no user in the database with that name.");
446 com_err(program_name, status, " in get_user_by_name.");
451 if (QueueCount(top) == 1) /* This is a unique name. */ {
452 char ** info = (char **) top->q_data;
453 Put_message("User ID Number retrieved for the user: ");
456 uid = Strsave(info[U_UID]);
458 return(Strsave(uid));
461 Put_message("That name is not unique, choose the user that you want.");
462 uid = ChooseUser(top);
467 /* Function Name: SetUserPassword
468 * Description: Set the new kerberos password for this user.
469 * Arguments: name - kerberos principle name for this user, (login name).
474 SetUserPassword(name)
477 name = name; /* make saber happy. */
478 Put_message("Kerberos password not changed, code non-existant.");
479 /* clever message to call account_admin, if this fails. */
482 /* Function Name: GiveBackLogin
483 * Description: Gives back previously reserved kerberous principle.
484 * Arguments: name - principle to give back.
492 name = name; /* make saber happy. */
493 Put_message("kerberos code not implimented, name not given back.");
494 /* send mail to db maintainer if this fails. */
497 /* Function Name: RegisterUser
498 * Description: This function registers a user.
500 * Returns: DM_NORMAL.
506 char * args[MAX_ARGS_SIZE];
507 char *login, *fstype = NULL;
508 char temp_buf[BUFSIZ];
511 Put_message("This function has NO kerberos support, so stange things");
512 Put_message("may happen if you use it to register a user.");
514 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE)) {
516 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
517 args[0] = Strsave(temp_buf);
520 if ( (args[0] = GetUidNumberFromName()) == NULL)
527 if ( ((login = args[1] = GetLoginName()) == NULL) ||
528 ( GetFSTypes(&fstype, FALSE) == SUB_ERROR ) ) {
529 FreeInfo(args); /* This work because the NULL temination is ok. */
535 switch (status = do_mr_query("register_user", CountArgs(args),
536 args, Scream, (char *) NULL)) {
538 sprintf(temp_buf, "User %s successfully registered.", login);
539 Put_message(temp_buf);
540 SetUserPassword(login);
543 GiveBackLogin(login);
544 sprintf(temp_buf, "The username %s is already in use.", login);
545 Put_message(temp_buf);
548 com_err(program_name, status, " in register_user");
555 /* Function Name: RealUpdateUser
556 * Description: actuall updates the user information.
557 * Arguments: info - all current information for the user fields.
558 * junk - an UNUSED boolean.
564 RealUpdateUser(info, junk)
569 char error_buf[BUFSIZ];
570 char ** args = AskUserInfo(info, TRUE);
573 Put_message("Aborted.");
576 if ( (status = do_mr_query("update_user", CountArgs(args),
577 args, Scream, (char *) NULL)) != MR_SUCCESS) {
578 com_err(program_name, status, " in ModifyFields");
579 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
580 Put_message(error_buf);
584 /* Function Name: UpdateUser
585 * Description: Modify some of the information about a user.
586 * Arguments: argc, argv - login name of the user in argv[1].
587 * Returns: DM_NORMAL.
592 UpdateUser(argc, argv)
598 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
599 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
605 /* Function Name: RealDeactivateUser
606 * Description: sets the user's status to 3.
607 * Arguments: info - all current information for the user fields
608 * one_item - indicates the user hasn't been queried yet
613 RealDeactivateUser(info, one_item)
618 char txt_buf[BUFSIZ];
622 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
623 if (YesNoQuestion(txt_buf, FALSE) != TRUE)
627 qargs[0] = info[NAME];
629 if ((status = do_mr_query("update_user_status", 2, qargs, Scream,
630 (char *) NULL)) != MR_SUCCESS) {
631 com_err(program_name, status, " in update_user_status");
632 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
633 Put_message(txt_buf);
638 /* Function Name: DeactivateUser
639 * Description: sets the user's status to 3.
640 * Arguments: argc, argv - login name of the user in argv[1].
641 * Returns: DM_NORMAL.
646 DeactivateUser(argc, argv)
652 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
653 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
660 /* ------------------------- Top Menu ------------------------- */
662 /* DeleteUser() in delete.c */
664 /* Function Name: DeleteUserByUid
665 * Description: Deletes the user given a uid number.
666 * Arguments: argc, argv - uid if user in argv[1].
667 * Returns: DM_NORMAL.
668 * NOTES: This just gets the username from the mr server
669 * and performs a DeleteUser().
673 DeleteUserByUid(argc, argv)
678 struct qelem *elem = NULL;
681 if(!ValidName(argv[1]))
684 if ( (status = do_mr_query("get_user_by_uid", 1, argv+1, StoreInfo,
685 (char * ) &elem)) != MR_SUCCESS)
686 com_err(program_name, status, " in get_user_by_uid");
688 info = (char **) elem->q_data;
689 argv[1] = info[U_NAME];
691 (void) DeleteUser(argc, argv);
695 /* ------------------------- Show User Information ------------------------- */
697 /* Function Name: ShowUserByLogin
698 * Description: Shows user information given a login name.
699 * Arguments: argc, argv - login name in argv[1].
705 ShowUserByLogin(argc, argv)
709 struct qelem *top, *elem;
711 elem = top = GetUserInfo(LOGIN, argv[1], (char *) NULL);
712 Loop(elem, PrintUserInfo);
718 /* Function Name: RetrieveUserByName
719 * Description: Show information on a user give fist and/or last name.
720 * Arguments: argc, argv - argv[1] - first name.
721 * argv[2] - last name.
722 * Returns: DM_NORMAL.
727 ShowUserByName(argc, argv)
734 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
736 if (top == NULL) /* if there was an error then return. */
739 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
746 Loop(top, PrintUserInfo);
750 Loop(top, PrintUserName);
758 /* Function Name: ShowUserByClass
759 * Description: Shows real and login names of all users in class.
760 * Arguments: argc, argv - argv[1] contains the class.
766 ShowUserByClass(argc, argv)
772 top = GetUserInfo(CLASS, argv[1], (char *) NULL);
773 Loop(top, PrintUserName);
780 /* Function Name: GetKrbmap
781 * Description: Shows user <-> Kerberos mappings
782 * Arguments: argc, argv - argv[1] contains the user login name,
783 * argv[2] contains the principal
789 GetKrbmap(argc, argv)
794 struct qelem *elem = NULL, *top;
797 if ((stat = do_mr_query("get_kerberos_user_map", 2, &argv[1],
798 StoreInfo, (char *)&elem)) != 0) {
799 com_err(program_name, stat, " in GetKrbMap.");
803 top = elem = QueueTop(elem);
805 while (elem != NULL) {
806 char **info = (char **) elem->q_data;
807 sprintf(buf, "User: %-9s Principal: %s",
808 info[KMAP_USER], info[KMAP_PRINCIPAL]);
813 FreeQueue(QueueTop(top));
818 /* Function Name: AddKrbmap
819 * Description: Add a new user <-> Kerberos mapping
820 * Arguments: argc, argv - argv[1] contains the user login name,
821 * argv[2] contains the principal
827 AddKrbmap(argc, argv)
833 if (!index(argv[KMAP_PRINCIPAL + 1], '@')) {
834 Put_message("Please specify a realm for the kerberos principal.");
837 if ((stat = do_mr_query("add_kerberos_user_map", 2, &argv[1],
838 Scream, NULL)) != 0) {
839 com_err(program_name, stat, " in AddKrbMap.");
840 if (stat == MR_EXISTS)
841 Put_message("No user or principal may have more than one mapping.");
847 /* Function Name: DeleteKrbmap
848 * Description: Remove a user <-> Kerberos mapping
849 * Arguments: argc, argv - argv[1] contains the user login name,
850 * argv[2] contains the principal
856 DeleteKrbmap(argc, argv)
862 if ((stat = do_mr_query("delete_kerberos_user_map", 2, &argv[1],
863 Scream, NULL)) != 0) {
864 com_err(program_name, stat, " in DeleteKrbMap.");