1 #if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
5 /* This is the file user.c for the SMS Client, which allows a nieve
6 * user to quickly and easily maintain most parts of the SMS 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
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;
136 /* Function Name: AskUserInfo.
137 * Description: This function askes the user for information about a
138 * machine and saves it into a structure.
139 * Arguments: info - a pointer the the structure to put the info into.
140 * flags - Flags asking us which info we want.
141 * Returns: the args to pass to the query.
142 * NOTES: the return args are not necessarily in the correct order to
143 * use the #defined names (e.g args[UID] is not the uid anymore).
147 AskUserInfo(info, name)
151 char temp_buf[BUFSIZ], *newname, *temp_ptr;
154 sprintf(temp_buf,"\nChanging Attributes of user %s.\n",info[U_NAME]);
155 Put_message(temp_buf);
157 struct qelem *elem = NULL;
160 GetValueFromUser("User's last name", &info[U_LAST]);
161 GetValueFromUser("User's first name", &info[U_FIRST]);
162 GetValueFromUser("User's middle name", &info[U_MIDDLE]);
163 argv[0] = info[U_FIRST];
164 argv[1] = info[U_LAST];
165 if (do_sms_query("get_user_by_name", 2, argv,
166 StoreInfo, (char *) &elem) == 0) {
167 Put_message("A user by that name already exists in the database.");
168 Loop(QueueTop(elem), PrintUserInfo);
169 Loop(QueueTop(elem), FreeInfo);
171 if (YesNoQuestion("Add new user anyway", TRUE) == FALSE)
176 newname = Strsave(info[U_NAME]);
177 GetValueFromUser("The new login name for this user", &newname);
180 GetValueFromUser("Login name for this user", &info[U_NAME]);
182 GetValueFromUser("User's UID", &info[U_UID]);
183 GetValueFromUser("User's shell", &info[U_SHELL]);
185 GetValueFromUser("User's last name", &info[U_LAST]);
186 GetValueFromUser("User's first name", &info[U_FIRST]);
187 GetValueFromUser("User's middle name", &info[U_MIDDLE]);
191 GetValueFromUser("User's status (? for help)", &info[U_STATE]);
192 if (isdigit(info[U_STATE][0]))
194 Put_message("Valid status numbers:");
195 for (i = 0; i < US_END; i++) {
196 sprintf(temp_buf, " %d: %s", i, states[i]);
197 Put_message(temp_buf);
200 temp_ptr = Strsave(info[U_MITID]);
201 Put_message("User's MIT ID number (type a new unencrypted number, or keep same encryption)");
202 GetValueFromUser("", &temp_ptr);
203 if ( strcmp( temp_ptr, info[U_MITID] ) != 0) {
204 EncryptID(temp_buf, temp_ptr, info[U_FIRST], info[U_LAST]);
206 info[U_MITID] = Strsave(temp_buf);
209 GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]);
211 FreeAndClear(&info[U_MODTIME], TRUE);
212 FreeAndClear(&info[U_MODBY], TRUE);
213 FreeAndClear(&info[U_MODWITH], TRUE);
216 * Slide the newname into the #2 slot, this screws up all future references
217 * to this list, since we slip the pointer into a info list it gets freed
218 * when the rest of the list gets freed.
221 SlipInNewName(info, newname);
226 /* Function Name: GetUserInfo
227 * Description: Stores the user information in a queue.
228 * Arguments: type - type of field given to get info, one of:
229 * LOGIN, UID, BY_NAME, CLASS.
230 * name1 - name of thing specified by type (wildcards okay)
231 * name2 - other name, only used in get user by first and last.
233 * Returns: the first element of the queue containing the user info.
237 GetUserInfo(type, name1, name2)
243 struct qelem * elem = NULL;
248 if ( (status = do_sms_query("get_user_by_login", 1, args,
249 StoreInfo, (char *) &elem)) != 0) {
250 com_err(program_name, status,
251 " when attempting to get_user_by_login.");
257 if ( (status = do_sms_query("get_user_by_uid", 1, args,
258 StoreInfo, (char *) &elem)) != 0) {
259 com_err(program_name, status,
260 " when attempting to get_user_by_uid.");
267 if ( (status = do_sms_query("get_user_by_name", 2, args,
268 StoreInfo, (char *) &elem)) != 0) {
269 com_err(program_name, status,
270 " when attempting to get_user_by_name.");
276 if ( (status = do_sms_query("get_user_by_class", 1, args,
277 StoreInfo, (char *) &elem)) != 0) {
278 com_err(program_name, status,
279 " when attempting to get_user_by_class.");
284 return( QueueTop(elem) );
287 /* Function Name: AddNewUser
288 * Description: Adds a new user to the database.
290 * Returns: DM_NORMAL.
298 char ** args, *info[MAX_ARGS_SIZE];
300 args = AskUserInfo(SetUserDefaults(info), FALSE);
303 if ( (status = do_sms_query("add_user", CountArgs(args),
304 args, Scream, (char *) NULL)) != SMS_SUCCESS)
305 com_err(program_name, status, " in add_user");
307 Put_message("New user added to database.");
313 /* Function Name: GetLoginName
314 * Description: Asks the user for a login name and reserves
317 * Returns: a malloced login name for the user.
325 Prompt_input("Login name for this user? ", name, BUFSIZ);
328 "KERBEROS code not added, did not reserve name with kerberos.");
330 return(Strsave(name));
334 /* Function Name: ChooseUser
335 * Description: Choose a user from a list and return the uid.
336 * Arguments: top - a queue of user information.
337 * Returns: uid - the malloced uid of the user that was chosen.
344 while (elem != NULL) {
345 char ** info = (char **) elem->q_data;
347 switch(YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE)) {
349 return(Strsave(info[U_UID]));
352 default: /* quit or ^C. */
360 /* Function Name: GetUidNumberFromName
361 * Description: Gets the users uid number, from the name.
363 * Returns: uid - a malloced string containing the uid.
367 GetUidNumberFromName()
369 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
371 struct qelem * top = NULL;
373 Prompt_input("First Name: ", first, BUFSIZ);
374 Prompt_input("Last Name: ", last, BUFSIZ);
379 switch (status = do_sms_query("get_user_by_name", 2, args,
380 StoreInfo, (char *) &top)) {
384 Put_message("There is no user in the database with that name.");
387 com_err(program_name, status, " in get_user_by_name.");
392 if (QueueCount(top) == 1) /* This is a unique name. */ {
393 char ** info = (char **) top->q_data;
394 Put_message("User ID Number retrieved for the user: ");
397 uid = Strsave(info[U_UID]);
399 return(Strsave(uid));
402 Put_message("That name is not unique, choose the user that you want.");
403 uid = ChooseUser(top);
408 /* Function Name: SetUserPassword
409 * Description: Set the new kerberos password for this user.
410 * Arguments: name - kerberos principle name for this user, (login name).
415 SetUserPassword(name)
418 name = name; /* make saber happy. */
419 Put_message("Kerberos password not changed, code non-existant.");
420 /* clever message to call account_admin, if this fails. */
423 /* Function Name: GiveBackLogin
424 * Description: Gives back previously reserved kerberous principle.
425 * Arguments: name - principle to give back.
433 name = name; /* make saber happy. */
434 Put_message("kerberos code not implimented, name not given back.");
435 /* send mail to db maintainer if this fails. */
438 /* Function Name: RegisterUser
439 * Description: This function registers a user.
441 * Returns: DM_NORMAL.
447 char * args[MAX_ARGS_SIZE];
448 char *login, *fstype = NULL;
449 char temp_buf[BUFSIZ];
452 Put_message("This function has NO kerberos support, so stange things");
453 Put_message("may happen if you use it to register a user.");
455 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE)) {
457 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
458 args[0] = Strsave(temp_buf);
461 if ( (args[0] = GetUidNumberFromName()) == NULL)
468 if ( ((login = args[1] = GetLoginName()) == NULL) ||
469 ( GetFSTypes(&fstype) == SUB_ERROR ) ) {
470 FreeInfo(args); /* This work because the NULL temination is ok. */
476 switch (status = do_sms_query("register_user", CountArgs(args),
477 args, Scream, (char *) NULL)) {
479 sprintf(temp_buf, "User %s successfully registered.", login);
480 Put_message(temp_buf);
481 SetUserPassword(login);
484 GiveBackLogin(login);
485 sprintf(temp_buf, "The username %s is already in use.", login);
486 Put_message(temp_buf);
489 com_err(program_name, status, " in register_user");
496 /* Function Name: RealUpdateUser
497 * Description: actuall updates the user information.
498 * Arguments: info - all current information for the user fields.
499 * junk - an UNUSED boolean.
505 RealUpdateUser(info, junk)
510 char error_buf[BUFSIZ];
511 char ** args = AskUserInfo(info, TRUE);
513 if ( (status = do_sms_query("update_user", CountArgs(args),
514 args, Scream, (char *) NULL)) != SMS_SUCCESS) {
515 com_err(program_name, status, " in ModifyFields");
516 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
517 Put_message(error_buf);
521 /* Function Name: UpdateUser
522 * Description: Modify some of the information about a user.
523 * Arguments: argc, argv - login name of the user in argv[1].
524 * Returns: DM_NORMAL.
529 UpdateUser(argc, argv)
535 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
536 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
542 /* Function Name: RealDeactivateUser
543 * Description: sets the user's status to 3.
544 * Arguments: info - all current information for the user fields
545 * one_item - indicates the user hasn't been queried yet
550 RealDeactivateUser(info, one_item)
555 char txt_buf[BUFSIZ];
559 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
560 if (!YesNoQuestion(txt_buf, 2))
564 qargs[0] = info[NAME];
566 if ((status = do_sms_query("update_user_status", 2, qargs, Scream,
567 (char *) NULL)) != SMS_SUCCESS) {
568 com_err(program_name, status, " in update_user_status");
569 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
570 Put_message(txt_buf);
575 /* Function Name: DeactivateUser
576 * Description: sets the user's status to 3.
577 * Arguments: argc, argv - login name of the user in argv[1].
578 * Returns: DM_NORMAL.
583 DeactivateUser(argc, argv)
589 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
590 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
597 /* ------------------------- Top Menu ------------------------- */
599 /* DeleteUser() in delete.c */
601 /* Function Name: DeleteUserByUid
602 * Description: Deletes the user given a uid number.
603 * Arguments: argc, argv - uid if user in argv[1].
604 * Returns: DM_NORMAL.
605 * NOTES: This just gets the username from the sms server
606 * and performs a DeleteUser().
610 DeleteUserByUid(argc, argv)
615 struct qelem *elem = NULL;
618 if(!ValidName(argv[1]))
621 if ( (status = do_sms_query("get_user_by_uid", 1, argv+1, StoreInfo,
622 (char * ) &elem)) != SMS_SUCCESS)
623 com_err(program_name, status, " in get_user_by_uid");
625 info = (char **) elem->q_data;
626 argv[1] = info[U_NAME];
628 (void) DeleteUser(argc, argv);
632 /* ------------------------- Show User Information ------------------------- */
634 /* Function Name: ShowUserByLogin
635 * Description: Shows user information given a login name.
636 * Arguments: argc, argv - login name in argv[1].
642 ShowUserByLogin(argc, argv)
646 struct qelem *top, *elem;
648 elem = top = GetUserInfo(LOGIN, argv[1], (char *) NULL);
649 Loop(elem, PrintUserInfo);
655 /* Function Name: RetrieveUserByName
656 * Description: Show information on a user give fist and/or last name.
657 * Arguments: argc, argv - argv[1] - first name.
658 * argv[2] - last name.
659 * Returns: DM_NORMAL.
664 ShowUserByName(argc, argv)
671 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
673 if (top == NULL) /* if there was an error then return. */
676 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
683 Loop(top, PrintUserInfo);
687 Loop(top, PrintUserName);
695 /* Function Name: ShowUserByClass
696 * Description: Shows real and login names of all users in class.
697 * Arguments: argc, argv - argv[1] contains the class.
703 ShowUserByClass(argc, argv)
709 top = GetUserInfo(CLASS, argv[1], (char *) NULL);
710 Loop(top, PrintUserName);
717 /* Function Name: GetKrbmap
718 * Description: Shows user <-> Kerberos mappings
719 * Arguments: argc, argv - argv[1] contains the user login name,
720 * argv[2] contains the principal
726 GetKrbmap(argc, argv)
731 struct qelem *elem = NULL, *top;
734 if ((stat = do_sms_query("get_kerberos_user_map", 2, &argv[1],
735 StoreInfo, (char *)&elem)) != 0) {
736 com_err(program_name, stat, " in GetKrbMap.");
740 top = elem = QueueTop(elem);
742 while (elem != NULL) {
743 char **info = (char **) elem->q_data;
744 sprintf(buf, "User: %-9s Principal: %s",
745 info[KMAP_USER], info[KMAP_PRINCIPAL]);
750 FreeQueue(QueueTop(top));
755 /* Function Name: AddKrbmap
756 * Description: Add a new user <-> Kerberos mapping
757 * Arguments: argc, argv - argv[1] contains the user login name,
758 * argv[2] contains the principal
764 AddKrbmap(argc, argv)
770 if (!index(argv[KMAP_PRINCIPAL + 1], '@')) {
771 Put_message("Please specify a realm for the kerberos principal.");
774 if ((stat = do_sms_query("add_kerberos_user_map", 2, &argv[1],
775 Scream, NULL)) != 0) {
776 com_err(program_name, stat, " in AddKrbMap.");
777 if (stat == SMS_EXISTS)
778 Put_message("No user or principal may have more than one mapping.");
784 /* Function Name: DeleteKrbmap
785 * Description: Remove a user <-> Kerberos mapping
786 * Arguments: argc, argv - argv[1] contains the user login name,
787 * argv[2] contains the principal
793 DeleteKrbmap(argc, argv)
799 if ((stat = do_sms_query("delete_kerberos_user_map", 2, &argv[1],
800 Scream, NULL)) != 0) {
801 com_err(program_name, stat, " in DeleteKrbMap.");