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
27 #include "mit-copyright.h"
38 #define DEFAULT_SHELL "/bin/csh"
39 #define DEFAULT_CLASS "?"
42 /* Function Name: UserState
43 * Description: Convert a numeric state into a descriptive string.
44 * Arguments: state value
45 * Returns: pointer to statically allocated string.
48 static char *states[] = { "Not registered",
51 "Marked for deletion",
54 static char *UserState(state)
57 if (state < 0 || state > 4)
59 return(states[state]);
63 /* Function Name: PrintUserName
64 * Description: Print name of a user.
65 * Arguments: info - the information about a user.
73 char buf[BUFSIZ], print_buf[BUFSIZ];
74 sprintf(buf, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
75 sprintf(print_buf, "%-40s User Name: %s", buf, info[U_NAME]);
76 Put_message(print_buf);
79 /* Function Name: PrintUserInfo
80 * Description: Prints Information about a user.
81 * Arguments: info - an argument list with the user information
90 char name[BUFSIZ], buf[BUFSIZ];
92 sprintf(name, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
93 sprintf(buf, "Login name: %-10s Full name: %s", info[U_NAME], name);
95 sprintf(buf, "User id: %-13s Login shell %-15s Class: %s",
96 info[U_UID], info[U_SHELL], info[U_CLASS]);
98 sprintf(buf, "Account is: %-10s Encrypted MIT ID number: %s",
99 UserState(atoi(info[U_STATE])), info[U_MITID]);
101 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME],info[U_MODWITH]);
105 /* Function Name: SetUserDefaults
106 * Description: Sets the default values for add user.
107 * Arguments: info - a blank user info array of char *'s.
108 * Returns: args - the filled info structure.
112 SetUserDefaults(info)
115 info[U_NAME] = Strsave(UNIQUE_LOGIN);
116 info[U_UID] = Strsave(UNIQUE_UID);
117 info[U_SHELL] = Strsave(DEFAULT_SHELL);
118 info[U_LAST] = Strsave(DEFAULT_NONE);
119 info[U_FIRST] = Strsave(DEFAULT_NONE);
120 info[U_MIDDLE] = Strsave(DEFAULT_NONE);
121 info[U_STATE] = Strsave(DEFAULT_NO);
122 info[U_MITID] = Strsave(DEFAULT_NONE);
123 info[U_CLASS] = Strsave(DEFAULT_CLASS);
124 info[U_MODTIME] = info[U_MODBY] = info[U_MODWITH] = info[U_END] = NULL;
128 /* Function Name: AskUserInfo.
129 * Description: This function askes the user for information about a
130 * machine and saves it into a structure.
131 * Arguments: info - a pointer the the structure to put the info into.
132 * flags - Flags asking us which info we want.
133 * Returns: the args to pass to the query.
134 * NOTES: the return args are not necessarily in the correct order to
135 * use the #defined names (e.g args[UID] is not the uid anymore).
139 AskUserInfo(info, name)
143 char temp_buf[BUFSIZ], *newname, *temp_ptr;
146 sprintf(temp_buf,"\nChanging Attributes of user %s.\n",info[U_NAME]);
147 Put_message(temp_buf);
150 newname = Strsave(info[U_NAME]);
151 GetValueFromUser("The new login name for this user", &newname);
154 GetValueFromUser("Login name for this user", &info[U_NAME]);
156 GetValueFromUser("User's UID", &info[U_UID]);
157 GetValueFromUser("User's shell", &info[U_SHELL]);
158 GetValueFromUser("User's last name", &info[U_LAST]);
159 GetValueFromUser("User's first name", &info[U_FIRST]);
160 GetValueFromUser("User's middle name", &info[U_MIDDLE]);
161 GetValueFromUser("User's status", &info[U_STATE]);
162 temp_ptr = Strsave(info[U_MITID]);
163 Put_message("User's MIT ID number (type a new unencrypted number, or keep same encryption)");
164 GetValueFromUser("", &temp_ptr);
165 if ( strcmp( temp_ptr, info[U_MITID] ) != 0) {
166 EncryptID(temp_buf, temp_ptr, info[U_FIRST], info[U_LAST]);
168 info[U_MITID] = Strsave(temp_buf);
171 GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]);
173 FreeAndClear(&info[U_MODTIME], TRUE);
174 FreeAndClear(&info[U_MODBY], TRUE);
175 FreeAndClear(&info[U_MODWITH], TRUE);
178 * Slide the newname into the #2 slot, this screws up all future references
179 * to this list, since we slip the pointer into a info list it gets freed
180 * when the rest of the list gets freed.
183 SlipInNewName(info, newname);
188 /* Function Name: GetUserInfo
189 * Description: Stores the user information in a queue.
190 * Arguments: type - type of field given to get info, one of:
191 * LOGIN, UID, BY_NAME, CLASS.
192 * name1 - name of thing specified by type (wildcards okay)
193 * name2 - other name, only used in get user by first and last.
195 * Returns: the first element of the queue containing the user info.
199 GetUserInfo(type, name1, name2)
205 struct qelem * elem = NULL;
210 if ( (status = sms_query("get_user_by_login", 1, args,
211 StoreInfo, (char *) &elem)) != 0) {
212 com_err(program_name, status,
213 " when attempting to get_user_by_login.");
219 if ( (status = sms_query("get_user_by_uid", 1, args,
220 StoreInfo, (char *) &elem)) != 0) {
221 com_err(program_name, status,
222 " when attempting to get_user_by_uid.");
229 if ( (status = sms_query("get_user_by_name", 2, args,
230 StoreInfo, (char *) &elem)) != 0) {
231 com_err(program_name, status,
232 " when attempting to get_user_by_name.");
238 if ( (status = sms_query("get_user_by_class", 1, args,
239 StoreInfo, (char *) &elem)) != 0) {
240 com_err(program_name, status,
241 " when attempting to get_user_by_class.");
246 return( QueueTop(elem) );
249 /* Function Name: AddNewUser
250 * Description: Adds a new user to the database.
252 * Returns: DM_NORMAL.
260 char ** args, *info[MAX_ARGS_SIZE];
262 args = AskUserInfo(SetUserDefaults(info), FALSE);
263 if ( (status = sms_query("add_user", CountArgs(args),
264 args, Scream, (char *) NULL)) != SMS_SUCCESS)
265 com_err(program_name, status, " in add_user");
267 Put_message("New user added to database.");
273 /* Function Name: GetLoginName
274 * Description: Asks the user for a login name and reserves
277 * Returns: a malloced login name for the user.
285 Prompt_input("Login name for this user? ", name, BUFSIZ);
288 "KERBEROS code not added, did not reserve name with kerberos.");
290 return(Strsave(name));
294 /* Function Name: ChooseUser
295 * Description: Choose a user from a list and return the uid.
296 * Arguments: top - a queue of user information.
297 * Returns: uid - the malloced uid of the user that was chosen.
304 while (elem != NULL) {
305 char ** info = (char **) elem->q_data;
307 switch(YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE)) {
309 return(Strsave(info[U_UID]));
312 default: /* quit or ^C. */
320 /* Function Name: GetUidNumberFromName
321 * Description: Gets the users uid number, from the name.
323 * Returns: uid - a malloced string containing the uid.
327 GetUidNumberFromName()
329 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
331 struct qelem * top = NULL;
333 Prompt_input("First Name: ", first, BUFSIZ);
334 Prompt_input("Last Name: ", last, BUFSIZ);
339 switch (status = sms_query("get_user_by_name", 2, args,
340 StoreInfo, (char *) &top)) {
344 Put_message("There is no user in the database with that name.");
347 com_err(program_name, status, " in get_user_by_name.");
352 if (QueueCount(top) == 1) /* This is a unique name. */ {
353 char ** info = (char **) top->q_data;
354 Put_message("User ID Number retrieved for the user: ");
357 uid = Strsave(info[U_UID]);
359 return(Strsave(uid));
362 Put_message("That name is not unique, choose the user that you want.");
363 uid = ChooseUser(top);
368 /* Function Name: SetUserPassword
369 * Description: Set the new kerberos password for this user.
370 * Arguments: name - kerberos principle name for this user, (login name).
375 SetUserPassword(name)
378 name = name; /* make saber happy. */
379 Put_message("Kerberos password not changed, code non-existant.");
380 /* clever message to call account_admin, if this fails. */
383 /* Function Name: GiveBackLogin
384 * Description: Gives back previously reserved kerberous principle.
385 * Arguments: name - principle to give back.
393 name = name; /* make saber happy. */
394 Put_message("kerberos code not implimented, name not given back.");
395 /* send mail to db maintainer if this fails. */
398 /* Function Name: RegisterUser
399 * Description: This function registers a user.
401 * Returns: DM_NORMAL.
407 char * args[MAX_ARGS_SIZE];
408 char *login, *fstype = NULL;
409 char temp_buf[BUFSIZ];
412 Put_message("This function has NO kerberos support, so stange things");
413 Put_message("may happen if you use it to register a user.");
415 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE)) {
417 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
418 args[0] = Strsave(temp_buf);
421 if ( (args[0] = GetUidNumberFromName()) == NULL)
428 if ( ((login = args[1] = GetLoginName()) == NULL) ||
429 ( GetFSTypes(&fstype) == SUB_ERROR ) ) {
430 FreeInfo(args); /* This work because the NULL temination is ok. */
436 switch (status = sms_query("register_user", CountArgs(args),
437 args, Scream, (char *) NULL)) {
439 sprintf(temp_buf, "User %s successfully registered.", login);
440 Put_message(temp_buf);
441 SetUserPassword(login);
444 GiveBackLogin(login);
445 sprintf(temp_buf, "The username %s is already in use.", login);
446 Put_message(temp_buf);
449 com_err(program_name, status, " in register_user");
456 /* Function Name: RealUpdateUser
457 * Description: actuall updates the user information.
458 * Arguments: info - all current information for the user fields.
459 * junk - an UNUSED boolean.
465 RealUpdateUser(info, junk)
470 char error_buf[BUFSIZ];
471 char ** args = AskUserInfo(info, TRUE);
473 if ( (status = sms_query("update_user", CountArgs(args),
474 args, Scream, (char *) NULL)) != SMS_SUCCESS) {
475 com_err(program_name, status, " in ModifyFields");
476 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
477 Put_message(error_buf);
481 /* Function Name: UpdateUser
482 * Description: Modify some of the information about a user.
483 * Arguments: argc, argv - login name of the user in argv[1].
484 * Returns: DM_NORMAL.
489 UpdateUser(argc, argv)
495 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
496 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
502 /* Function Name: RealDeactivateUser
503 * Description: sets the user's status to 3.
504 * Arguments: info - all current information for the user fields
505 * one_item - indicates the user hasn't been queried yet
510 RealDeactivateUser(info, one_item)
515 char txt_buf[BUFSIZ];
519 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
520 if (!YesNoQuestion(txt_buf, 2))
524 qargs[0] = info[NAME];
526 if ((status = sms_query("update_user_status", 2, qargs, Scream,
527 (char *) NULL)) != SMS_SUCCESS) {
528 com_err(program_name, status, " in update_user_status");
529 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
530 Put_message(txt_buf);
535 /* Function Name: DeactivateUser
536 * Description: sets the user's status to 3.
537 * Arguments: argc, argv - login name of the user in argv[1].
538 * Returns: DM_NORMAL.
543 DeactivateUser(argc, argv)
549 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
550 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
557 /* ------------------------- Top Menu ------------------------- */
559 /* DeleteUser() in delete.c */
561 /* Function Name: DeleteUserByUid
562 * Description: Deletes the user given a uid number.
563 * Arguments: argc, argv - uid if user in argv[1].
564 * Returns: DM_NORMAL.
565 * NOTES: This just gets the username from the sms server
566 * and performs a DeleteUser().
570 DeleteUserByUid(argc, argv)
575 struct qelem *elem = NULL;
578 if(!ValidName(argv[1]))
581 if ( (status = sms_query("get_user_by_uid", 1, argv+1, StoreInfo,
582 (char * ) &elem)) != SMS_SUCCESS)
583 com_err(program_name, status, " in get_user_by_uid");
585 info = (char **) elem->q_data;
586 argv[1] = info[U_NAME];
588 (void) DeleteUser(argc, argv);
592 /* ------------------------- Show User Information ------------------------- */
594 /* Function Name: ShowUserByLogin
595 * Description: Shows user information given a login name.
596 * Arguments: argc, argv - login name in argv[1].
602 ShowUserByLogin(argc, argv)
606 struct qelem *top, *elem;
608 elem = top = GetUserInfo(LOGIN, argv[1], (char *) NULL);
609 Loop(elem, PrintUserInfo);
615 /* Function Name: RetrieveUserByName
616 * Description: Show information on a user give fist and/or last name.
617 * Arguments: argc, argv - argv[1] - first name.
618 * argv[2] - last name.
619 * Returns: DM_NORMAL.
624 ShowUserByName(argc, argv)
631 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
633 if (top == NULL) /* if there was an error then return. */
636 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
643 Loop(top, PrintUserInfo);
647 Loop(top, PrintUserName);
655 /* Function Name: ShowUserByClass
656 * Description: Shows real and login names of all users in class.
657 * Arguments: argc, argv - argv[1] contains the class.
663 ShowUserByClass(argc, argv)
669 top = GetUserInfo(CLASS, argv[1], (char *) NULL);
670 Loop(top, PrintUserName);
680 * c-continued-statement-offset: 4
682 * c-argdecl-indent: 4