1 #if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
5 /* This is the file user.c for allmaint, the SMS client that allows
6 * a user to maintaint most important parts of the SMS database.
7 * It Contains: the functions for usermaint.
10 * By: Chris D. Peterson
16 * Copyright 1987, 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"
29 #include "allmaint_funcs.h"
38 /* Function Name: PrintUserName
39 * Description: Print name of a user.
40 * Arguments: info - the information about a user.
48 char buf[BUFSIZ], print_buf[BUFSIZ];
49 sprintf(buf, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
50 sprintf(print_buf, "%-40s User Name: %s", buf, info[U_NAME]);
51 Put_message(print_buf);
54 /* Function Name: PrintUserInfo
55 * Description: Prints Information about a user.
56 * Arguments: info - an argument list with the user information
65 char name[BUFSIZ], buf[BUFSIZ];
67 sprintf(name, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
68 sprintf(buf, "Login name: %-10s Full name: %s", info[U_NAME], name);
70 sprintf(buf, "User id: %-13s Login shell %-15s Class: %s",
71 info[U_UID], info[U_SHELL], info[U_CLASS]);
73 sprintf(buf, "Account is: %-10s Encrypted MIT ID number: %s",
74 atoi(info[U_STATE]) ? "active" : "inactive", info[U_MITID]);
76 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME],info[U_MODWITH]);
80 /* Function Name: SetUserDefaults
81 * Description: Sets the default values for add user.
82 * Arguments: info - a blank user info array of char *'s.
83 * Returns: args - the filled info structure.
90 info[U_NAME] = Strsave(UNIQUE_LOGIN);
91 info[U_UID] = Strsave(UNIQUE_UID);
92 info[U_SHELL] = Strsave(DEFAULT_NONE);
93 info[U_LAST] = Strsave(DEFAULT_NONE);
94 info[U_FIRST] = Strsave(DEFAULT_NONE);
95 info[U_MIDDLE] = Strsave(DEFAULT_NONE);
96 info[U_STATE] = Strsave(DEFAULT_NO);
97 info[U_MITID] = Strsave(DEFAULT_NONE);
98 info[U_CLASS] = Strsave(DEFAULT_NONE);
99 info[U_MODTIME] = info[U_MODBY] = info[U_MODWITH] = info[U_END] = NULL;
103 /* Function Name: AskUserInfo.
104 * Description: This function askes the user for information about a
105 * machine and saves it into a structure.
106 * Arguments: info - a pointer the the structure to put the info into.
107 * flags - Flags asking us which info we want.
108 * Returns: the args to pass to the query.
109 * NOTES: the return args are not necessarily in the correct order to
110 * use the #defined names (e.g args[UID] is not the uid anymore).
114 AskUserInfo(info, name)
118 char temp_buf[BUFSIZ], *newname, *temp_ptr;
121 sprintf(temp_buf,"\nChanging Attributes of user %s.\n",info[U_NAME]);
122 Put_message(temp_buf);
125 newname = Strsave(info[U_NAME]);
126 GetValueFromUser("The new login name for this user", &newname);
129 GetValueFromUser("Login name for this user", &info[U_NAME]);
131 GetValueFromUser("User's UID", &info[U_UID]);
132 GetValueFromUser("User's shell", &info[U_SHELL]);
133 GetValueFromUser("User's last name", &info[U_LAST]);
134 GetValueFromUser("User's first name", &info[U_FIRST]);
135 GetValueFromUser("User's middle name", &info[U_MIDDLE]);
136 GetValueFromUser("User's status", &info[U_STATE]);
137 temp_ptr = Strsave(info[U_MITID]);
138 GetValueFromUser("User's MIT ID number", &temp_ptr);
139 if ( strcmp( temp_ptr, info[U_MITID] ) != 0) {
140 RemoveHyphens(temp_ptr); /* zap'em */
141 EncryptMITID(temp_buf, temp_ptr, info[U_FIRST], info[U_LAST]);
143 info[U_MITID] = Strsave(temp_buf);
146 GetValueFromUser("User's MIT Year (class)", &info[U_CLASS]);
148 FreeAndClear(&info[U_MODTIME], TRUE);
149 FreeAndClear(&info[U_MODBY], TRUE);
150 FreeAndClear(&info[U_MODWITH], TRUE);
153 * Slide the newname into the #2 slot, this screws up all future references
154 * to this list, since we slip the pointer into a info list it gets freed
155 * when the rest of the list gets freed.
158 SlipInNewName(info, newname);
163 /* Function Name: GetUserInfo
164 * Description: Stores the user information in a queue.
165 * Arguments: type - type of field given to get info, one of:
166 * LOGIN, UID, BY_NAME, CLASS.
167 * name1 - name of thing specified by type (wildcards okay)
168 * name2 - other name, only used in get user by first and last.
170 * Returns: the first element of the queue containing the user info.
174 GetUserInfo(type, name1, name2)
180 struct qelem * elem = NULL;
185 if ( (status = sms_query("get_user_by_login", 1, args,
186 StoreInfo, (char *) &elem)) != 0) {
187 com_err(program_name, status,
188 " when attempting to get_user_by_login.");
194 if ( (status = sms_query("get_user_by_uid", 1, args,
195 StoreInfo, (char *) &elem)) != 0) {
196 com_err(program_name, status,
197 " when attempting to get_user_by_uid.");
204 if ( (status = sms_query("get_user_by_name", 2, args,
205 StoreInfo, (char *) &elem)) != 0) {
206 com_err(program_name, status,
207 " when attempting to get_user_by_name.");
213 if ( (status = sms_query("get_user_by_class", 1, args,
214 StoreInfo, (char *) &elem)) != 0) {
215 com_err(program_name, status,
216 " when attempting to get_user_by_class.");
221 return( QueueTop(elem) );
224 /* Function Name: AddNewUser
225 * Description: Adds a new user to the database.
227 * Returns: DM_NORMAL.
235 char ** args, *info[MAX_ARGS_SIZE];
237 args = AskUserInfo(SetUserDefaults(info), FALSE);
238 if ( (status = sms_query("add_user", CountArgs(args),
239 args, Scream, (char *) NULL)) != SMS_SUCCESS)
240 com_err(program_name, status, " in add_user");
242 Put_message("New user added to database.");
248 /* Function Name: GetLoginName
249 * Description: Asks the user for a login name and reserves
252 * Returns: a malloced login name for the user.
260 Prompt_input("Login name for this user? ", name, BUFSIZ);
263 "KERBEROS code not added, did not reserve name with kerberos.");
265 return(Strsave(name));
269 /* Function Name: ChooseUser
270 * Description: Choose a user from a list and return the uid.
271 * Arguments: top - a queue of user information.
272 * Returns: uid - the malloced uid of the user that was chosen.
279 while (elem != NULL) {
280 char ** info = (char **) elem->q_data;
282 switch(YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE)) {
284 return(Strsave(info[U_UID]));
287 default: /* quit or ^C. */
295 /* Function Name: GetUidNumberFromName
296 * Description: Gets the users uid number, from the name.
298 * Returns: uid - a malloced string containing the uid.
302 GetUidNumberFromName()
304 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
306 struct qelem * top = NULL;
308 Prompt_input("First Name: ", first, BUFSIZ);
309 Prompt_input("Last Name: ", last, BUFSIZ);
314 switch (status = sms_query("get_user_by_name", 2, args,
319 Put_message("There is no user in the database with that name.");
322 com_err(program_name, status, " in get_user_by_name.");
327 if (QueueCount(top) == 1) /* This is a unique name. */ {
328 char ** info = (char **) top->q_data;
329 Put_message("User ID Number retrieved for the user: ");
332 uid = Strsave(info[U_UID]);
334 return(Strsave(uid));
337 Put_message("That name is not unique, choose the user that you want.");
338 uid = ChooseUser(top);
343 /* Function Name: SetUserPassword
344 * Description: Set the new kerberos password for this user.
345 * Arguments: name - kerberos principle name for this user, (login name).
350 SetUserPassword(name)
353 name = name; /* make saber happy. */
354 Put_message("Kerberos password not changed, code non-existant.");
355 /* clever message to call account_admin, if this fails. */
358 /* Function Name: GiveBackLogin
359 * Description: Gives back previously reserved kerberous principle.
360 * Arguments: name - principle to give back.
368 name = name; /* make saber happy. */
369 Put_message("kerberos code not implimented, name not given back.");
370 /* send mail to db maintainer if this fails. */
373 /* Function Name: RegisterUser
374 * Description: This function registers a user.
376 * Returns: DM_NORMAL.
382 char * args[MAX_ARGS_SIZE];
383 char *login, *fstype = NULL;
384 char temp_buf[BUFSIZ];
387 Put_message("This function has NO kerberos support, so stange things");
388 Put_message("may happen if you use it to register a user.");
390 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE)) {
392 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
393 args[0] = Strsave(temp_buf);
396 if ( (args[0] = GetUidNumberFromName()) == NULL)
403 if ( ((login = args[1] = GetLoginName()) == NULL) ||
404 ( GetFSTypes(&fstype) == SUB_ERROR ) ) {
405 FreeInfo(args); /* This work because the NULL temination is ok. */
411 switch (status = sms_query("register_user", CountArgs(args),
412 args, Scream, (char *) NULL)) {
414 sprintf(temp_buf, "User %s successfully registered.", login);
415 Put_message(temp_buf);
416 SetUserPassword(login);
419 GiveBackLogin(login);
420 sprintf(temp_buf, "The username %s is already in use.", login);
421 Put_message(temp_buf);
424 com_err(program_name, status, " in register_user");
431 /* Function Name: RealUpdateUser
432 * Description: actuall updates the user information.
433 * Arguments: info - all current information for the user fields.
434 * junk - an UNUSED boolean.
440 RealUpdateUser(info, junk)
445 char error_buf[BUFSIZ];
446 char ** args = AskUserInfo(info, TRUE);
448 if ( (status = sms_query("update_user", CountArgs(args),
449 args, Scream, (char *) NULL)) != SMS_SUCCESS) {
450 com_err(program_name, status, " in ModifyFields");
451 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
452 Put_message(error_buf);
456 /* Function Name: UpdateUser
457 * Description: Modify some of the information about a user.
458 * Arguments: argc, argv - login name of the user in argv[1].
459 * Returns: DM_NORMAL.
464 UpdateUser(argc, argv)
470 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
471 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
477 /* ------------------------- Top Menu ------------------------- */
479 /* DeleteUser() in delete.c */
481 /* Function Name: DeleteUserByUid
482 * Description: Deletes the user given a uid number.
483 * Arguments: argc, argv - uid if user in argv[1].
484 * Returns: DM_NORMAL.
485 * NOTES: This just gets the username from the sms server
486 * and performs a DeleteUser().
490 DeleteUserByUid(argc, argv)
495 struct qelem *elem = NULL;
498 if(!ValidName(argv[1]))
501 if ( (status = sms_query("get_user_by_uid", 1, argv+1, StoreInfo,
502 (char * ) &elem)) != SMS_SUCCESS)
503 com_err(program_name, status, " in get_user_by_uid");
505 info = (char **) elem->q_data;
506 argv[1] = info[U_NAME];
508 (void) DeleteUser(argc, argv);
512 /* ------------------------- Show User Information ------------------------- */
514 /* Function Name: ShowUserByLogin
515 * Description: Shows user information given a login name.
516 * Arguments: argc, argv - login name in argv[1].
522 ShowUserByLogin(argc, argv)
526 struct qelem *top, *elem;
528 elem = top = GetUserInfo(LOGIN, argv[1], (char *) NULL);
529 Loop(elem, PrintUserInfo);
535 /* Function Name: RetrieveUserByName
536 * Description: Show information on a user give fist and/or last name.
537 * Arguments: argc, argv - argv[1] - first name.
538 * argv[2] - last name.
539 * Returns: DM_NORMAL.
544 ShowUserByName(argc, argv)
551 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
553 if (top == NULL) /* if there was an error then return. */
556 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
563 Loop(top, PrintUserInfo);
567 Loop(top, PrintUserName);
575 /* Function Name: ShowUserByClass
576 * Description: Shows real and login names of all users in class.
577 * Arguments: argc, argv - argv[1] contains the class.
583 ShowUserByClass(argc, argv)
589 top = GetUserInfo(CLASS, argv[1], (char *) NULL);
590 Loop(top, PrintUserName);
600 * c-continued-statement-offset: 4
602 * c-argdecl-indent: 4