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)" };
57 static char *UserState(state)
62 if (state < 0 || state >= US_END) {
63 sprintf(buf, "Unknown (%d)", state);
66 return(states[state]);
70 /* Function Name: PrintUserName
71 * Description: Print name of a user.
72 * Arguments: info - the information about a user.
80 char buf[BUFSIZ], print_buf[BUFSIZ];
81 sprintf(buf, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
82 sprintf(print_buf, "%-40s User Name: %s", buf, info[U_NAME]);
83 Put_message(print_buf);
86 /* Function Name: PrintUserInfo
87 * Description: Prints Information about a user.
88 * Arguments: info - an argument list with the user information
97 char name[BUFSIZ], buf[BUFSIZ];
99 sprintf(name, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
100 sprintf(buf, "Login name: %-20s Full name: %s", info[U_NAME], name);
102 sprintf(buf, "User id: %-23s Login shell %-10s Class: %s",
103 info[U_UID], info[U_SHELL], info[U_CLASS]);
105 sprintf(buf, "Account is: %-20s Encrypted MIT ID number: %s",
106 UserState(atoi(info[U_STATE])), info[U_MITID]);
108 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME],info[U_MODWITH]);
112 /* Function Name: SetUserDefaults
113 * Description: Sets the default values for add user.
114 * Arguments: info - a blank user info array of char *'s.
115 * Returns: args - the filled info structure.
119 SetUserDefaults(info)
122 info[U_NAME] = Strsave(UNIQUE_LOGIN);
123 info[U_UID] = Strsave(UNIQUE_UID);
124 info[U_SHELL] = Strsave(DEFAULT_SHELL);
125 info[U_LAST] = Strsave(DEFAULT_NONE);
126 info[U_FIRST] = Strsave(DEFAULT_NONE);
127 info[U_MIDDLE] = Strsave(DEFAULT_NONE);
128 info[U_STATE] = Strsave(DEFAULT_NO);
129 info[U_MITID] = Strsave(DEFAULT_NONE);
130 info[U_CLASS] = Strsave(DEFAULT_CLASS);
131 info[U_MODTIME] = info[U_MODBY] = info[U_MODWITH] = info[U_END] = NULL;
135 /* Function Name: AskUserInfo.
136 * Description: This function askes the user for information about a
137 * machine and saves it into a structure.
138 * Arguments: info - a pointer the the structure to put the info into.
139 * flags - Flags asking us which info we want.
140 * Returns: the args to pass to the query.
141 * NOTES: the return args are not necessarily in the correct order to
142 * use the #defined names (e.g args[UID] is not the uid anymore).
146 AskUserInfo(info, name)
150 char temp_buf[BUFSIZ], *newname, *temp_ptr;
153 sprintf(temp_buf,"\nChanging Attributes of user %s.\n",info[U_NAME]);
154 Put_message(temp_buf);
156 struct qelem *elem = NULL;
159 GetValueFromUser("User's last name", &info[U_LAST]);
160 GetValueFromUser("User's first name", &info[U_FIRST]);
161 GetValueFromUser("User's middle name", &info[U_MIDDLE]);
162 argv[0] = info[U_FIRST];
163 argv[1] = info[U_LAST];
164 if (do_sms_query("get_user_by_name", 2, argv,
165 StoreInfo, (char *) &elem) == 0) {
166 Put_message("A user by that name already exists in the database.");
167 Loop(QueueTop(elem), PrintUserInfo);
168 Loop(QueueTop(elem), FreeInfo);
170 if (YesNoQuestion("Add new user anyway", TRUE) == FALSE)
175 newname = Strsave(info[U_NAME]);
176 GetValueFromUser("The new login name for this user", &newname);
179 GetValueFromUser("Login name for this user", &info[U_NAME]);
181 GetValueFromUser("User's UID", &info[U_UID]);
182 GetValueFromUser("User's shell", &info[U_SHELL]);
184 GetValueFromUser("User's last name", &info[U_LAST]);
185 GetValueFromUser("User's first name", &info[U_FIRST]);
186 GetValueFromUser("User's middle name", &info[U_MIDDLE]);
190 GetValueFromUser("User's status (? for help)", &info[U_STATE]);
191 if (isdigit(info[U_STATE][0]))
193 Put_message("Valid status numbers:");
194 for (i = 0; i < US_END; i++) {
195 sprintf(temp_buf, " %d: %s", i, states[i]);
196 Put_message(temp_buf);
199 temp_ptr = Strsave(info[U_MITID]);
200 Put_message("User's MIT ID number (type a new unencrypted number, or keep same encryption)");
201 GetValueFromUser("", &temp_ptr);
202 if ( strcmp( temp_ptr, info[U_MITID] ) != 0) {
203 EncryptID(temp_buf, temp_ptr, info[U_FIRST], info[U_LAST]);
205 info[U_MITID] = Strsave(temp_buf);
208 GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]);
210 FreeAndClear(&info[U_MODTIME], TRUE);
211 FreeAndClear(&info[U_MODBY], TRUE);
212 FreeAndClear(&info[U_MODWITH], TRUE);
215 * Slide the newname into the #2 slot, this screws up all future references
216 * to this list, since we slip the pointer into a info list it gets freed
217 * when the rest of the list gets freed.
220 SlipInNewName(info, newname);
225 /* Function Name: GetUserInfo
226 * Description: Stores the user information in a queue.
227 * Arguments: type - type of field given to get info, one of:
228 * LOGIN, UID, BY_NAME, CLASS.
229 * name1 - name of thing specified by type (wildcards okay)
230 * name2 - other name, only used in get user by first and last.
232 * Returns: the first element of the queue containing the user info.
236 GetUserInfo(type, name1, name2)
242 struct qelem * elem = NULL;
247 if ( (status = do_sms_query("get_user_by_login", 1, args,
248 StoreInfo, (char *) &elem)) != 0) {
249 com_err(program_name, status,
250 " when attempting to get_user_by_login.");
256 if ( (status = do_sms_query("get_user_by_uid", 1, args,
257 StoreInfo, (char *) &elem)) != 0) {
258 com_err(program_name, status,
259 " when attempting to get_user_by_uid.");
266 if ( (status = do_sms_query("get_user_by_name", 2, args,
267 StoreInfo, (char *) &elem)) != 0) {
268 com_err(program_name, status,
269 " when attempting to get_user_by_name.");
275 if ( (status = do_sms_query("get_user_by_class", 1, args,
276 StoreInfo, (char *) &elem)) != 0) {
277 com_err(program_name, status,
278 " when attempting to get_user_by_class.");
283 return( QueueTop(elem) );
286 /* Function Name: AddNewUser
287 * Description: Adds a new user to the database.
289 * Returns: DM_NORMAL.
297 char ** args, *info[MAX_ARGS_SIZE];
299 args = AskUserInfo(SetUserDefaults(info), FALSE);
302 if ( (status = do_sms_query("add_user", CountArgs(args),
303 args, Scream, (char *) NULL)) != SMS_SUCCESS)
304 com_err(program_name, status, " in add_user");
306 Put_message("New user added to database.");
312 /* Function Name: GetLoginName
313 * Description: Asks the user for a login name and reserves
316 * Returns: a malloced login name for the user.
324 Prompt_input("Login name for this user? ", name, BUFSIZ);
327 "KERBEROS code not added, did not reserve name with kerberos.");
329 return(Strsave(name));
333 /* Function Name: ChooseUser
334 * Description: Choose a user from a list and return the uid.
335 * Arguments: top - a queue of user information.
336 * Returns: uid - the malloced uid of the user that was chosen.
343 while (elem != NULL) {
344 char ** info = (char **) elem->q_data;
346 switch(YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE)) {
348 return(Strsave(info[U_UID]));
351 default: /* quit or ^C. */
359 /* Function Name: GetUidNumberFromName
360 * Description: Gets the users uid number, from the name.
362 * Returns: uid - a malloced string containing the uid.
366 GetUidNumberFromName()
368 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
370 struct qelem * top = NULL;
372 Prompt_input("First Name: ", first, BUFSIZ);
373 Prompt_input("Last Name: ", last, BUFSIZ);
378 switch (status = do_sms_query("get_user_by_name", 2, args,
379 StoreInfo, (char *) &top)) {
383 Put_message("There is no user in the database with that name.");
386 com_err(program_name, status, " in get_user_by_name.");
391 if (QueueCount(top) == 1) /* This is a unique name. */ {
392 char ** info = (char **) top->q_data;
393 Put_message("User ID Number retrieved for the user: ");
396 uid = Strsave(info[U_UID]);
398 return(Strsave(uid));
401 Put_message("That name is not unique, choose the user that you want.");
402 uid = ChooseUser(top);
407 /* Function Name: SetUserPassword
408 * Description: Set the new kerberos password for this user.
409 * Arguments: name - kerberos principle name for this user, (login name).
414 SetUserPassword(name)
417 name = name; /* make saber happy. */
418 Put_message("Kerberos password not changed, code non-existant.");
419 /* clever message to call account_admin, if this fails. */
422 /* Function Name: GiveBackLogin
423 * Description: Gives back previously reserved kerberous principle.
424 * Arguments: name - principle to give back.
432 name = name; /* make saber happy. */
433 Put_message("kerberos code not implimented, name not given back.");
434 /* send mail to db maintainer if this fails. */
437 /* Function Name: RegisterUser
438 * Description: This function registers a user.
440 * Returns: DM_NORMAL.
446 char * args[MAX_ARGS_SIZE];
447 char *login, *fstype = NULL;
448 char temp_buf[BUFSIZ];
451 Put_message("This function has NO kerberos support, so stange things");
452 Put_message("may happen if you use it to register a user.");
454 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE)) {
456 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
457 args[0] = Strsave(temp_buf);
460 if ( (args[0] = GetUidNumberFromName()) == NULL)
467 if ( ((login = args[1] = GetLoginName()) == NULL) ||
468 ( GetFSTypes(&fstype) == SUB_ERROR ) ) {
469 FreeInfo(args); /* This work because the NULL temination is ok. */
475 switch (status = do_sms_query("register_user", CountArgs(args),
476 args, Scream, (char *) NULL)) {
478 sprintf(temp_buf, "User %s successfully registered.", login);
479 Put_message(temp_buf);
480 SetUserPassword(login);
483 GiveBackLogin(login);
484 sprintf(temp_buf, "The username %s is already in use.", login);
485 Put_message(temp_buf);
488 com_err(program_name, status, " in register_user");
495 /* Function Name: RealUpdateUser
496 * Description: actuall updates the user information.
497 * Arguments: info - all current information for the user fields.
498 * junk - an UNUSED boolean.
504 RealUpdateUser(info, junk)
509 char error_buf[BUFSIZ];
510 char ** args = AskUserInfo(info, TRUE);
512 if ( (status = do_sms_query("update_user", CountArgs(args),
513 args, Scream, (char *) NULL)) != SMS_SUCCESS) {
514 com_err(program_name, status, " in ModifyFields");
515 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
516 Put_message(error_buf);
520 /* Function Name: UpdateUser
521 * Description: Modify some of the information about a user.
522 * Arguments: argc, argv - login name of the user in argv[1].
523 * Returns: DM_NORMAL.
528 UpdateUser(argc, argv)
534 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
535 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
541 /* Function Name: RealDeactivateUser
542 * Description: sets the user's status to 3.
543 * Arguments: info - all current information for the user fields
544 * one_item - indicates the user hasn't been queried yet
549 RealDeactivateUser(info, one_item)
554 char txt_buf[BUFSIZ];
558 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
559 if (!YesNoQuestion(txt_buf, 2))
563 qargs[0] = info[NAME];
565 if ((status = do_sms_query("update_user_status", 2, qargs, Scream,
566 (char *) NULL)) != SMS_SUCCESS) {
567 com_err(program_name, status, " in update_user_status");
568 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
569 Put_message(txt_buf);
574 /* Function Name: DeactivateUser
575 * Description: sets the user's status to 3.
576 * Arguments: argc, argv - login name of the user in argv[1].
577 * Returns: DM_NORMAL.
582 DeactivateUser(argc, argv)
588 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
589 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
596 /* ------------------------- Top Menu ------------------------- */
598 /* DeleteUser() in delete.c */
600 /* Function Name: DeleteUserByUid
601 * Description: Deletes the user given a uid number.
602 * Arguments: argc, argv - uid if user in argv[1].
603 * Returns: DM_NORMAL.
604 * NOTES: This just gets the username from the sms server
605 * and performs a DeleteUser().
609 DeleteUserByUid(argc, argv)
614 struct qelem *elem = NULL;
617 if(!ValidName(argv[1]))
620 if ( (status = do_sms_query("get_user_by_uid", 1, argv+1, StoreInfo,
621 (char * ) &elem)) != SMS_SUCCESS)
622 com_err(program_name, status, " in get_user_by_uid");
624 info = (char **) elem->q_data;
625 argv[1] = info[U_NAME];
627 (void) DeleteUser(argc, argv);
631 /* ------------------------- Show User Information ------------------------- */
633 /* Function Name: ShowUserByLogin
634 * Description: Shows user information given a login name.
635 * Arguments: argc, argv - login name in argv[1].
641 ShowUserByLogin(argc, argv)
645 struct qelem *top, *elem;
647 elem = top = GetUserInfo(LOGIN, argv[1], (char *) NULL);
648 Loop(elem, PrintUserInfo);
654 /* Function Name: RetrieveUserByName
655 * Description: Show information on a user give fist and/or last name.
656 * Arguments: argc, argv - argv[1] - first name.
657 * argv[2] - last name.
658 * Returns: DM_NORMAL.
663 ShowUserByName(argc, argv)
670 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
672 if (top == NULL) /* if there was an error then return. */
675 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
682 Loop(top, PrintUserInfo);
686 Loop(top, PrintUserName);
694 /* Function Name: ShowUserByClass
695 * Description: Shows real and login names of all users in class.
696 * Arguments: argc, argv - argv[1] contains the class.
702 ShowUserByClass(argc, argv)
708 top = GetUserInfo(CLASS, argv[1], (char *) NULL);
709 Loop(top, PrintUserName);
716 /* Function Name: GetKrbmap
717 * Description: Shows user <-> Kerberos mappings
718 * Arguments: argc, argv - argv[1] contains the user login name,
719 * argv[2] contains the principal
725 GetKrbmap(argc, argv)
730 struct qelem *elem = NULL, *top;
733 if ((stat = do_sms_query("get_kerberos_user_map", 2, &argv[1],
734 StoreInfo, (char *)&elem)) != 0) {
735 com_err(program_name, stat, " in GetKrbMap.");
739 top = elem = QueueTop(elem);
741 while (elem != NULL) {
742 char **info = (char **) elem->q_data;
743 sprintf(buf, "User: %-9s Principal: %s",
744 info[KMAP_USER], info[KMAP_PRINCIPAL]);
749 FreeQueue(QueueTop(top));
754 /* Function Name: AddKrbmap
755 * Description: Add a new user <-> Kerberos mapping
756 * Arguments: argc, argv - argv[1] contains the user login name,
757 * argv[2] contains the principal
763 AddKrbmap(argc, argv)
769 if (!index(argv[KMAP_PRINCIPAL + 1], '@')) {
770 Put_message("Please specify a realm for the kerberos principal.");
773 if ((stat = do_sms_query("add_kerberos_user_map", 2, &argv[1],
774 Scream, NULL)) != 0) {
775 com_err(program_name, stat, " in AddKrbMap.");
776 if (stat == SMS_EXISTS)
777 Put_message("No user or principal may have more than one mapping.");
783 /* Function Name: DeleteKrbmap
784 * Description: Remove a user <-> Kerberos mapping
785 * Arguments: argc, argv - argv[1] contains the user login name,
786 * argv[2] contains the principal
792 DeleteKrbmap(argc, argv)
798 if ((stat = do_sms_query("delete_kerberos_user_map", 2, &argv[1],
799 Scream, NULL)) != 0) {
800 com_err(program_name, stat, " in DeleteKrbMap.");