]> andersk Git - moira.git/blame - clients/moira/user.c
Allow a self-referential list to be deleted; don't really delete
[moira.git] / clients / moira / user.c
CommitLineData
402461ad 1#if (!defined(lint) && !defined(SABER))
08345b74 2 static char rcsid_module_c[] = "$Header$";
3#endif lint
4
0a2c64cb 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.
08345b74 8 *
9 * Created: 5/9/88
10 * By: Chris D. Peterson
11 *
12 * $Source$
13 * $Author$
14 * $Header$
15 *
0a2c64cb 16 * Copyright 1988 by the Massachusetts Institute of Technology.
08345b74 17 *
18 * For further information on copyright and distribution
19 * see the file mit-copyright.h
20 */
21
08345b74 22#include <stdio.h>
23#include <strings.h>
08345b74 24#include <sms.h>
08345b74 25#include <menu.h>
26
461c03b6 27#include "mit-copyright.h"
0a2c64cb 28#include "defs.h"
29#include "f_defs.h"
461c03b6 30#include "globals.h"
31#include "infodefs.h"
32
08345b74 33#define LOGIN 0
34#define UID 1
402461ad 35#define BY_NAME 2
075fe5bb 36#define CLASS 3
08345b74 37
32de30b0 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.
42 */
43
44static char *states[] = { "Not registered",
45 "Active",
46 "Half registered",
47 "Marked for deletion",
48 "Not registerable" };
49
50static char *UserState(state)
51int state;
52{
53 if (state < 0 || state > 4)
54 return("Unknown");
55 return(states[state]);
56}
57
58
075fe5bb 59/* Function Name: PrintUserName
60 * Description: Print name of a user.
61 * Arguments: info - the information about a user.
62 * Returns: none.
63 */
64
65static void
66PrintUserName(info)
67char ** info;
68{
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);
73}
74
75/* Function Name: PrintUserInfo
76 * Description: Prints Information about a user.
77 * Arguments: info - an argument list with the user information
78 * in it.
79 * Returns: none
80 */
81
82static void
83PrintUserInfo(info)
84char ** info;
85{
86 char name[BUFSIZ], buf[BUFSIZ];
87
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);
90 Put_message(buf);
91 sprintf(buf, "User id: %-13s Login shell %-15s Class: %s",
92 info[U_UID], info[U_SHELL], info[U_CLASS]);
93 Put_message(buf);
94 sprintf(buf, "Account is: %-10s Encrypted MIT ID number: %s",
32de30b0 95 UserState(atoi(info[U_STATE])), info[U_MITID]);
075fe5bb 96 Put_message(buf);
97 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME],info[U_MODWITH]);
98 Put_message(buf);
99}
100
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.
105 */
106
107static char **
108SetUserDefaults(info)
109char ** info;
110{
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;
121 return(info);
122}
461c03b6 123
08345b74 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).
132 */
133
461c03b6 134char **
135AskUserInfo(info, name)
08345b74 136char ** info;
137Bool name;
138{
075fe5bb 139 char temp_buf[BUFSIZ], *newname, *temp_ptr;
08345b74 140
075fe5bb 141 if (name) {
142 sprintf(temp_buf,"\nChanging Attributes of user %s.\n",info[U_NAME]);
143 Put_message(temp_buf);
144 }
08345b74 145 if (name) {
146 newname = Strsave(info[U_NAME]);
075fe5bb 147 GetValueFromUser("The new login name for this user", &newname);
08345b74 148 }
075fe5bb 149 else
150 GetValueFromUser("Login name for this user", &info[U_NAME]);
151
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 MIT ID number", &temp_ptr);
160 if ( strcmp( temp_ptr, info[U_MITID] ) != 0) {
161 RemoveHyphens(temp_ptr); /* zap'em */
162 EncryptMITID(temp_buf, temp_ptr, info[U_FIRST], info[U_LAST]);
163 free(info[U_MITID]);
164 info[U_MITID] = Strsave(temp_buf);
165 }
166 free(temp_ptr);
167 GetValueFromUser("User's MIT Year (class)", &info[U_CLASS]);
08345b74 168
169 FreeAndClear(&info[U_MODTIME], TRUE);
170 FreeAndClear(&info[U_MODBY], TRUE);
171 FreeAndClear(&info[U_MODWITH], TRUE);
172
173/*
174 * Slide the newname into the #2 slot, this screws up all future references
175 * to this list, since we slip the pointer into a info list it gets freed
176 * when the rest of the list gets freed.
177 */
178 if (name)
179 SlipInNewName(info, newname);
180
181 return(info);
182}
183
184/* Function Name: GetUserInfo
185 * Description: Stores the user information in a queue.
186 * Arguments: type - type of field given to get info, one of:
402461ad 187 * LOGIN, UID, BY_NAME, CLASS.
08345b74 188 * name1 - name of thing specified by type (wildcards okay)
189 * name2 - other name, only used in get user by first and last.
190 * (wildcards okay).
191 * Returns: the first element of the queue containing the user info.
192 */
193
194struct qelem *
461c03b6 195GetUserInfo(type, name1, name2)
08345b74 196int type;
197char *name1, *name2;
198{
199 char * args[2];
461c03b6 200 register int status;
201 struct qelem * elem = NULL;
08345b74 202
203 switch(type) {
204 case LOGIN:
205 args[0] = name1;
206 if ( (status = sms_query("get_user_by_login", 1, args,
461c03b6 207 StoreInfo, (char *) &elem)) != 0) {
85ca828a 208 com_err(program_name, status,
075fe5bb 209 " when attempting to get_user_by_login.");
08345b74 210 return (NULL);
211 }
212 break;
213 case UID:
214 args[0] = name1;
215 if ( (status = sms_query("get_user_by_uid", 1, args,
461c03b6 216 StoreInfo, (char *) &elem)) != 0) {
217 com_err(program_name, status,
075fe5bb 218 " when attempting to get_user_by_uid.");
08345b74 219 return (NULL);
220 }
221 break;
402461ad 222 case BY_NAME:
08345b74 223 args[0] = name1;
224 args[1] = name2;
075fe5bb 225 if ( (status = sms_query("get_user_by_name", 2, args,
461c03b6 226 StoreInfo, (char *) &elem)) != 0) {
227 com_err(program_name, status,
075fe5bb 228 " when attempting to get_user_by_name.");
08345b74 229 return (NULL);
230 }
231 break;
232 case CLASS:
233 args[0] = name1;
234 if ( (status = sms_query("get_user_by_class", 1, args,
461c03b6 235 StoreInfo, (char *) &elem)) != 0) {
236 com_err(program_name, status,
075fe5bb 237 " when attempting to get_user_by_class.");
08345b74 238 return (NULL);
239 }
240 break;
08345b74 241 }
242 return( QueueTop(elem) );
243}
244
075fe5bb 245/* Function Name: AddNewUser
246 * Description: Adds a new user to the database.
247 * Arguments: none.
08345b74 248 * Returns: DM_NORMAL.
249 */
250
251/* ARGSUSED */
252int
075fe5bb 253AddNewUser()
08345b74 254{
075fe5bb 255 register int status;
256 char ** args, *info[MAX_ARGS_SIZE];
257
258 args = AskUserInfo(SetUserDefaults(info), FALSE);
259 if ( (status = sms_query("add_user", CountArgs(args),
260 args, Scream, (char *) NULL)) != SMS_SUCCESS)
261 com_err(program_name, status, " in add_user");
262 else
263 Put_message("New user added to database.");
264 FreeInfo(args);
08345b74 265 return(DM_NORMAL);
266}
267
268
075fe5bb 269/* Function Name: GetLoginName
270 * Description: Asks the user for a login name and reserves
271 * it with kerberous.
272 * Arguments: none.
273 * Returns: a malloced login name for the user.
08345b74 274 */
275
075fe5bb 276static char *
277GetLoginName()
08345b74 278{
075fe5bb 279 char name[BUFSIZ];
280
281 Prompt_input("Login name for this user? ", name, BUFSIZ);
282
283 Put_message(
284 "KERBEROS code not added, did not reserve name with kerberos.");
285
286 return(Strsave(name));
287}
288
289
290/* Function Name: ChooseUser
291 * Description: Choose a user from a list and return the uid.
292 * Arguments: top - a queue of user information.
293 * Returns: uid - the malloced uid of the user that was chosen.
294 */
08345b74 295
075fe5bb 296static char *
297ChooseUser(elem)
298struct qelem * elem;
299{
08345b74 300 while (elem != NULL) {
075fe5bb 301 char ** info = (char **) elem->q_data;
302 PrintUserInfo(info);
303 switch(YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE)) {
304 case TRUE:
305 return(Strsave(info[U_UID]));
306 case FALSE:
307 break;
308 default: /* quit or ^C. */
309 return(NULL);
08345b74 310 }
311 elem = elem->q_forw;
312 }
075fe5bb 313 return(NULL);
08345b74 314}
315
075fe5bb 316/* Function Name: GetUidNumberFromName
317 * Description: Gets the users uid number, from the name.
318 * Arguments: none.
319 * Returns: uid - a malloced string containing the uid.
08345b74 320 */
321
075fe5bb 322static char *
323GetUidNumberFromName()
08345b74 324{
075fe5bb 325 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
461c03b6 326 register int status;
075fe5bb 327 struct qelem * top = NULL;
328
329 Prompt_input("First Name: ", first, BUFSIZ);
330 Prompt_input("Last Name: ", last, BUFSIZ);
08345b74 331
075fe5bb 332 args[0] = first;
333 args[1] = last;
08345b74 334
075fe5bb 335 switch (status = sms_query("get_user_by_name", 2, args,
336 StoreInfo, &top)) {
337 case SMS_SUCCESS:
338 break;
339 case SMS_NO_MATCH:
340 Put_message("There is no user in the database with that name.");
341 return(NULL);
342 default:
343 com_err(program_name, status, " in get_user_by_name.");
344 return(NULL);
08345b74 345 }
346
075fe5bb 347 top = QueueTop(top);
348 if (QueueCount(top) == 1) /* This is a unique name. */ {
349 char ** info = (char **) top->q_data;
350 Put_message("User ID Number retrieved for the user: ");
351 Put_message("");
352 PrintUserName(info);
353 uid = Strsave(info[U_UID]);
354 FreeQueue(top);
355 return(Strsave(uid));
356 }
08345b74 357
075fe5bb 358 Put_message("That name is not unique, choose the user that you want.");
359 uid = ChooseUser(top);
360 FreeQueue(top);
361 return(uid);
08345b74 362}
363
075fe5bb 364/* Function Name: SetUserPassword
365 * Description: Set the new kerberos password for this user.
366 * Arguments: name - kerberos principle name for this user, (login name).
367 * Returns: none.
08345b74 368 */
369
075fe5bb 370static void
371SetUserPassword(name)
372char * name;
08345b74 373{
075fe5bb 374 name = name; /* make saber happy. */
375 Put_message("Kerberos password not changed, code non-existant.");
376 /* clever message to call account_admin, if this fails. */
377}
08345b74 378
075fe5bb 379/* Function Name: GiveBackLogin
380 * Description: Gives back previously reserved kerberous principle.
381 * Arguments: name - principle to give back.
382 * Returns: void.
08345b74 383 */
384
075fe5bb 385static void
386GiveBackLogin(name)
387char * name;
08345b74 388{
075fe5bb 389 name = name; /* make saber happy. */
390 Put_message("kerberos code not implimented, name not given back.");
391 /* send mail to db maintainer if this fails. */
08345b74 392}
393
075fe5bb 394/* Function Name: RegisterUser
395 * Description: This function registers a user.
396 * Arguments: none.
397 * Returns: DM_NORMAL.
08345b74 398 */
399
08345b74 400int
075fe5bb 401RegisterUser()
08345b74 402{
075fe5bb 403 char * args[MAX_ARGS_SIZE];
404 char *login, *fstype = NULL;
405 char temp_buf[BUFSIZ];
406 register int status;
461c03b6 407
075fe5bb 408 Put_message("This function has NO kerberos support, so stange things");
409 Put_message("may happen if you use it to register a user.");
410
411 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE)) {
412 case TRUE:
413 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
414 args[0] = Strsave(temp_buf);
415 break;
416 case FALSE:
417 if ( (args[0] = GetUidNumberFromName()) == NULL)
418 return(DM_NORMAL);
419 break;
420 default:
421 return(DM_NORMAL);
461c03b6 422 }
075fe5bb 423
424 if ( ((login = args[1] = GetLoginName()) == NULL) ||
425 ( GetFSTypes(&fstype) == SUB_ERROR ) ) {
426 FreeInfo(args); /* This work because the NULL temination is ok. */
427 return(DM_NORMAL);
428 }
429 args[2] = fstype;
430 args[3] = NULL;
431
432 switch (status = sms_query("register_user", CountArgs(args),
433 args, Scream, (char *) NULL)) {
434 case SMS_SUCCESS:
435 sprintf(temp_buf, "User %s successfully registered.", login);
436 Put_message(temp_buf);
437 SetUserPassword(login);
438 break;
439 case SMS_IN_USE:
440 GiveBackLogin(login);
441 sprintf(temp_buf, "The username %s is already in use.", login);
442 Put_message(temp_buf);
443 break;
444 default:
445 com_err(program_name, status, " in register_user");
446 break;
447 }
448 FreeInfo(args);
449 return(DM_NORMAL);
08345b74 450}
451
075fe5bb 452/* Function Name: RealUpdateUser
453 * Description: actuall updates the user information.
454 * Arguments: info - all current information for the user fields.
455 * junk - an UNUSED boolean.
456 * Returns: none.
08345b74 457 */
458
075fe5bb 459/* ARGSUSED */
460static void
461RealUpdateUser(info, junk)
462char ** info;
463Bool junk;
08345b74 464{
075fe5bb 465 register int status;
466 char error_buf[BUFSIZ];
467 char ** args = AskUserInfo(info, TRUE);
468
469 if ( (status = sms_query("update_user", CountArgs(args),
470 args, Scream, (char *) NULL)) != SMS_SUCCESS) {
471 com_err(program_name, status, " in ModifyFields");
472 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
473 Put_message(error_buf);
08345b74 474 }
08345b74 475}
476
075fe5bb 477/* Function Name: UpdateUser
478 * Description: Modify some of the information about a user.
479 * Arguments: argc, argv - login name of the user in argv[1].
08345b74 480 * Returns: DM_NORMAL.
481 */
482
075fe5bb 483/* ARGSUSED */
08345b74 484int
075fe5bb 485UpdateUser(argc, argv)
08345b74 486int argc;
487char **argv;
488{
075fe5bb 489 struct qelem * elem;
08345b74 490
075fe5bb 491 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
492 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
08345b74 493
075fe5bb 494 FreeQueue(elem);
08345b74 495 return(DM_NORMAL);
496}
497
075fe5bb 498/* ------------------------- Top Menu ------------------------- */
499
500/* DeleteUser() in delete.c */
461c03b6 501
075fe5bb 502/* Function Name: DeleteUserByUid
503 * Description: Deletes the user given a uid number.
504 * Arguments: argc, argv - uid if user in argv[1].
08345b74 505 * Returns: DM_NORMAL.
075fe5bb 506 * NOTES: This just gets the username from the sms server
507 * and performs a DeleteUser().
08345b74 508 */
509
08345b74 510int
075fe5bb 511DeleteUserByUid(argc, argv)
08345b74 512int argc;
513char **argv;
514{
461c03b6 515 int status;
075fe5bb 516 struct qelem *elem = NULL;
517 char ** info;
518
519 if(!ValidName(argv[1]))
520 return(DM_NORMAL);
521
522 if ( (status = sms_query("get_user_by_uid", 1, argv+1, StoreInfo,
523 (char * ) &elem)) != SMS_SUCCESS)
524 com_err(program_name, status, " in get_user_by_uid");
525
526 info = (char **) elem->q_data;
527 argv[1] = info[U_NAME];
08345b74 528
075fe5bb 529 (void) DeleteUser(argc, argv);
08345b74 530 return(DM_NORMAL);
075fe5bb 531}
08345b74 532
075fe5bb 533/* ------------------------- Show User Information ------------------------- */
08345b74 534
535/* Function Name: ShowUserByLogin
536 * Description: Shows user information given a login name.
537 * Arguments: argc, argv - login name in argv[1].
538 * Returns: DM_NORMAL
539 */
540
541/* ARGSUSED */
542int
543ShowUserByLogin(argc, argv)
544int argc;
545char *argv[];
546{
547 struct qelem *top, *elem;
548
549 elem = top = GetUserInfo(LOGIN, argv[1], (char *) NULL);
075fe5bb 550 Loop(elem, PrintUserInfo);
08345b74 551
552 FreeQueue(top);
553 return (DM_NORMAL);
554}
555
556/* Function Name: RetrieveUserByName
557 * Description: Show information on a user give fist and/or last name.
558 * Arguments: argc, argv - argv[1] - first name.
559 * argv[2] - last name.
560 * Returns: DM_NORMAL.
561 */
562
563/* ARGSUSED */
564int
565ShowUserByName(argc, argv)
566int argc;
567char *argv[];
568{
075fe5bb 569 struct qelem *top;
3c1a8806 570 char buf[BUFSIZ];
08345b74 571
075fe5bb 572 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
08345b74 573
075fe5bb 574 if (top == NULL) /* if there was an error then return. */
08345b74 575 return(DM_NORMAL);
576
075fe5bb 577 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
3c1a8806 578 buf, 1, "f"))
075fe5bb 579 return(DM_NORMAL);
08345b74 580
3c1a8806 581 switch(buf[0]) {
075fe5bb 582 case 'F':
583 case 'f':
584 Loop(top, PrintUserInfo);
585 break;
586 case 'N':
587 case 'n':
588 Loop(top, PrintUserName);
589 break;
590 }
591
08345b74 592 FreeQueue(top);
593 return (DM_NORMAL);
594}
595
596/* Function Name: ShowUserByClass
597 * Description: Shows real and login names of all users in class.
598 * Arguments: argc, argv - argv[1] contains the class.
599 * Returns: none.
600 */
601
85ca828a 602/* ARGSUSED */
08345b74 603int
604ShowUserByClass(argc, argv)
605int argc;
606char **argv;
607{
075fe5bb 608 struct qelem *top;
08345b74 609
075fe5bb 610 top = GetUserInfo(CLASS, argv[1], (char *) NULL);
611 Loop(top, PrintUserName);
08345b74 612
613 FreeQueue(top);
614 return (DM_NORMAL);
615}
616
08345b74 617/*
618 * Local Variables:
619 * mode: c
620 * c-indent-level: 4
621 * c-continued-statement-offset: 4
622 * c-brace-offset: -4
623 * c-argdecl-indent: 4
624 * c-label-offset: -4
625 * End:
626 */
This page took 0.152721 seconds and 5 git commands to generate.