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 /* Function Name: UserState
39 * Description: Convert a numeric state into a descriptive string.
40 * Arguments: state value
41 * Returns: pointer to statically allocated string.
44 static char *states[] = { "Not registered",
47 "Marked for deletion",
50 static char *UserState(state)
53 if (state < 0 || state > 4)
55 return(states[state]);
59 /* Function Name: PrintUserName
60 * Description: Print name of a user.
61 * Arguments: info - the information about a user.
69 char buf[BUFSIZ], print_buf[BUFSIZ];
70 sprintf(buf, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
71 sprintf(print_buf, "%-40s User Name: %s", buf, info[U_NAME]);
72 Put_message(print_buf);
75 /* Function Name: PrintUserInfo
76 * Description: Prints Information about a user.
77 * Arguments: info - an argument list with the user information
86 char name[BUFSIZ], buf[BUFSIZ];
88 sprintf(name, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
89 sprintf(buf, "Login name: %-10s Full name: %s", info[U_NAME], name);
91 sprintf(buf, "User id: %-13s Login shell %-15s Class: %s",
92 info[U_UID], info[U_SHELL], info[U_CLASS]);
94 sprintf(buf, "Account is: %-10s Encrypted MIT ID number: %s",
95 UserState(atoi(info[U_STATE])), info[U_MITID]);
97 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME],info[U_MODWITH]);
101 /* Function Name: SetUserDefaults
102 * Description: Sets the default values for add user.
103 * Arguments: info - a blank user info array of char *'s.
104 * Returns: args - the filled info structure.
108 SetUserDefaults(info)
111 info[U_NAME] = Strsave(UNIQUE_LOGIN);
112 info[U_UID] = Strsave(UNIQUE_UID);
113 info[U_SHELL] = Strsave(DEFAULT_NONE);
114 info[U_LAST] = Strsave(DEFAULT_NONE);
115 info[U_FIRST] = Strsave(DEFAULT_NONE);
116 info[U_MIDDLE] = Strsave(DEFAULT_NONE);
117 info[U_STATE] = Strsave(DEFAULT_NO);
118 info[U_MITID] = Strsave(DEFAULT_NONE);
119 info[U_CLASS] = Strsave(DEFAULT_NONE);
120 info[U_MODTIME] = info[U_MODBY] = info[U_MODWITH] = info[U_END] = NULL;
124 /* Function Name: AskUserInfo.
125 * Description: This function askes the user for information about a
126 * machine and saves it into a structure.
127 * Arguments: info - a pointer the the structure to put the info into.
128 * flags - Flags asking us which info we want.
129 * Returns: the args to pass to the query.
130 * NOTES: the return args are not necessarily in the correct order to
131 * use the #defined names (e.g args[UID] is not the uid anymore).
135 AskUserInfo(info, name)
139 char temp_buf[BUFSIZ], *newname, *temp_ptr;
142 sprintf(temp_buf,"\nChanging Attributes of user %s.\n",info[U_NAME]);
143 Put_message(temp_buf);
146 newname = Strsave(info[U_NAME]);
147 GetValueFromUser("The new login name for this user", &newname);
150 GetValueFromUser("Login name for this user", &info[U_NAME]);
152 GetValueFromUser("User's UID", &info[U_UID]);
153 GetValueFromUser("User's shell", &info[U_SHELL]);
154 GetValueFromUser("User's last name", &info[U_LAST]);
155 GetValueFromUser("User's first name", &info[U_FIRST]);
156 GetValueFromUser("User's middle name", &info[U_MIDDLE]);
157 GetValueFromUser("User's status", &info[U_STATE]);
158 temp_ptr = Strsave(info[U_MITID]);
159 GetValueFromUser("User's (unencrypted) MIT ID number", &temp_ptr);
160 if ( strcmp( temp_ptr, info[U_MITID] ) != 0) {
161 EncryptID(temp_buf, temp_ptr, info[U_FIRST], info[U_LAST]);
163 info[U_MITID] = Strsave(temp_buf);
166 GetValueFromUser("User's MIT Year (class)", &info[U_CLASS]);
168 FreeAndClear(&info[U_MODTIME], TRUE);
169 FreeAndClear(&info[U_MODBY], TRUE);
170 FreeAndClear(&info[U_MODWITH], TRUE);
173 * Slide the newname into the #2 slot, this screws up all future references
174 * to this list, since we slip the pointer into a info list it gets freed
175 * when the rest of the list gets freed.
178 SlipInNewName(info, newname);
183 /* Function Name: GetUserInfo
184 * Description: Stores the user information in a queue.
185 * Arguments: type - type of field given to get info, one of:
186 * LOGIN, UID, BY_NAME, CLASS.
187 * name1 - name of thing specified by type (wildcards okay)
188 * name2 - other name, only used in get user by first and last.
190 * Returns: the first element of the queue containing the user info.
194 GetUserInfo(type, name1, name2)
200 struct qelem * elem = NULL;
205 if ( (status = sms_query("get_user_by_login", 1, args,
206 StoreInfo, (char *) &elem)) != 0) {
207 com_err(program_name, status,
208 " when attempting to get_user_by_login.");
214 if ( (status = sms_query("get_user_by_uid", 1, args,
215 StoreInfo, (char *) &elem)) != 0) {
216 com_err(program_name, status,
217 " when attempting to get_user_by_uid.");
224 if ( (status = sms_query("get_user_by_name", 2, args,
225 StoreInfo, (char *) &elem)) != 0) {
226 com_err(program_name, status,
227 " when attempting to get_user_by_name.");
233 if ( (status = sms_query("get_user_by_class", 1, args,
234 StoreInfo, (char *) &elem)) != 0) {
235 com_err(program_name, status,
236 " when attempting to get_user_by_class.");
241 return( QueueTop(elem) );
244 /* Function Name: AddNewUser
245 * Description: Adds a new user to the database.
247 * Returns: DM_NORMAL.
255 char ** args, *info[MAX_ARGS_SIZE];
257 args = AskUserInfo(SetUserDefaults(info), FALSE);
258 if ( (status = sms_query("add_user", CountArgs(args),
259 args, Scream, (char *) NULL)) != SMS_SUCCESS)
260 com_err(program_name, status, " in add_user");
262 Put_message("New user added to database.");
268 /* Function Name: GetLoginName
269 * Description: Asks the user for a login name and reserves
272 * Returns: a malloced login name for the user.
280 Prompt_input("Login name for this user? ", name, BUFSIZ);
283 "KERBEROS code not added, did not reserve name with kerberos.");
285 return(Strsave(name));
289 /* Function Name: ChooseUser
290 * Description: Choose a user from a list and return the uid.
291 * Arguments: top - a queue of user information.
292 * Returns: uid - the malloced uid of the user that was chosen.
299 while (elem != NULL) {
300 char ** info = (char **) elem->q_data;
302 switch(YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE)) {
304 return(Strsave(info[U_UID]));
307 default: /* quit or ^C. */
315 /* Function Name: GetUidNumberFromName
316 * Description: Gets the users uid number, from the name.
318 * Returns: uid - a malloced string containing the uid.
322 GetUidNumberFromName()
324 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
326 struct qelem * top = NULL;
328 Prompt_input("First Name: ", first, BUFSIZ);
329 Prompt_input("Last Name: ", last, BUFSIZ);
334 switch (status = sms_query("get_user_by_name", 2, args,
335 StoreInfo, (char *) &top)) {
339 Put_message("There is no user in the database with that name.");
342 com_err(program_name, status, " in get_user_by_name.");
347 if (QueueCount(top) == 1) /* This is a unique name. */ {
348 char ** info = (char **) top->q_data;
349 Put_message("User ID Number retrieved for the user: ");
352 uid = Strsave(info[U_UID]);
354 return(Strsave(uid));
357 Put_message("That name is not unique, choose the user that you want.");
358 uid = ChooseUser(top);
363 /* Function Name: SetUserPassword
364 * Description: Set the new kerberos password for this user.
365 * Arguments: name - kerberos principle name for this user, (login name).
370 SetUserPassword(name)
373 name = name; /* make saber happy. */
374 Put_message("Kerberos password not changed, code non-existant.");
375 /* clever message to call account_admin, if this fails. */
378 /* Function Name: GiveBackLogin
379 * Description: Gives back previously reserved kerberous principle.
380 * Arguments: name - principle to give back.
388 name = name; /* make saber happy. */
389 Put_message("kerberos code not implimented, name not given back.");
390 /* send mail to db maintainer if this fails. */
393 /* Function Name: RegisterUser
394 * Description: This function registers a user.
396 * Returns: DM_NORMAL.
402 char * args[MAX_ARGS_SIZE];
403 char *login, *fstype = NULL;
404 char temp_buf[BUFSIZ];
407 Put_message("This function has NO kerberos support, so stange things");
408 Put_message("may happen if you use it to register a user.");
410 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE)) {
412 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
413 args[0] = Strsave(temp_buf);
416 if ( (args[0] = GetUidNumberFromName()) == NULL)
423 if ( ((login = args[1] = GetLoginName()) == NULL) ||
424 ( GetFSTypes(&fstype) == SUB_ERROR ) ) {
425 FreeInfo(args); /* This work because the NULL temination is ok. */
431 switch (status = sms_query("register_user", CountArgs(args),
432 args, Scream, (char *) NULL)) {
434 sprintf(temp_buf, "User %s successfully registered.", login);
435 Put_message(temp_buf);
436 SetUserPassword(login);
439 GiveBackLogin(login);
440 sprintf(temp_buf, "The username %s is already in use.", login);
441 Put_message(temp_buf);
444 com_err(program_name, status, " in register_user");
451 /* Function Name: RealUpdateUser
452 * Description: actuall updates the user information.
453 * Arguments: info - all current information for the user fields.
454 * junk - an UNUSED boolean.
460 RealUpdateUser(info, junk)
465 char error_buf[BUFSIZ];
466 char ** args = AskUserInfo(info, TRUE);
468 if ( (status = sms_query("update_user", CountArgs(args),
469 args, Scream, (char *) NULL)) != SMS_SUCCESS) {
470 com_err(program_name, status, " in ModifyFields");
471 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
472 Put_message(error_buf);
476 /* Function Name: UpdateUser
477 * Description: Modify some of the information about a user.
478 * Arguments: argc, argv - login name of the user in argv[1].
479 * Returns: DM_NORMAL.
484 UpdateUser(argc, argv)
490 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
491 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
497 /* Function Name: RealDeactivateUser
498 * Description: sets the user's status to 3.
499 * Arguments: info - all current information for the user fields
500 * one_item - indicates the user hasn't been queried yet
505 RealDeactivateUser(info, one_item)
510 char txt_buf[BUFSIZ];
514 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
515 if (!YesNoQuestion(txt_buf, 1))
519 qargs[0] = info[NAME];
521 if ((status = sms_query("update_user_status", 2, qargs, Scream,
522 (char *) NULL)) != SMS_SUCCESS) {
523 com_err(program_name, status, " in update_user_status");
524 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
525 Put_message(txt_buf);
530 /* Function Name: DeactivateUser
531 * Description: sets the user's status to 3.
532 * Arguments: argc, argv - login name of the user in argv[1].
533 * Returns: DM_NORMAL.
538 DeactivateUser(argc, argv)
544 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
545 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
552 /* ------------------------- Top Menu ------------------------- */
554 /* DeleteUser() in delete.c */
556 /* Function Name: DeleteUserByUid
557 * Description: Deletes the user given a uid number.
558 * Arguments: argc, argv - uid if user in argv[1].
559 * Returns: DM_NORMAL.
560 * NOTES: This just gets the username from the sms server
561 * and performs a DeleteUser().
565 DeleteUserByUid(argc, argv)
570 struct qelem *elem = NULL;
573 if(!ValidName(argv[1]))
576 if ( (status = sms_query("get_user_by_uid", 1, argv+1, StoreInfo,
577 (char * ) &elem)) != SMS_SUCCESS)
578 com_err(program_name, status, " in get_user_by_uid");
580 info = (char **) elem->q_data;
581 argv[1] = info[U_NAME];
583 (void) DeleteUser(argc, argv);
587 /* ------------------------- Show User Information ------------------------- */
589 /* Function Name: ShowUserByLogin
590 * Description: Shows user information given a login name.
591 * Arguments: argc, argv - login name in argv[1].
597 ShowUserByLogin(argc, argv)
601 struct qelem *top, *elem;
603 elem = top = GetUserInfo(LOGIN, argv[1], (char *) NULL);
604 Loop(elem, PrintUserInfo);
610 /* Function Name: RetrieveUserByName
611 * Description: Show information on a user give fist and/or last name.
612 * Arguments: argc, argv - argv[1] - first name.
613 * argv[2] - last name.
614 * Returns: DM_NORMAL.
619 ShowUserByName(argc, argv)
626 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
628 if (top == NULL) /* if there was an error then return. */
631 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
638 Loop(top, PrintUserInfo);
642 Loop(top, PrintUserName);
650 /* Function Name: ShowUserByClass
651 * Description: Shows real and login names of all users in class.
652 * Arguments: argc, argv - argv[1] contains the class.
658 ShowUserByClass(argc, argv)
664 top = GetUserInfo(CLASS, argv[1], (char *) NULL);
665 Loop(top, PrintUserName);
675 * c-continued-statement-offset: 4
677 * c-argdecl-indent: 4