]> andersk Git - moira.git/blame - clients/moira/user.c
Rewrote much of this file to follow the conventions of the rest
[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
07b2e652 38#define DEFAULT_SHELL "/bin/csh"
39#define DEFAULT_CLASS "?"
40
41
32de30b0 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.
46 */
47
48static char *states[] = { "Not registered",
49 "Active",
50 "Half registered",
51 "Marked for deletion",
52 "Not registerable" };
53
54static char *UserState(state)
55int state;
56{
57 if (state < 0 || state > 4)
58 return("Unknown");
59 return(states[state]);
60}
61
62
075fe5bb 63/* Function Name: PrintUserName
64 * Description: Print name of a user.
65 * Arguments: info - the information about a user.
66 * Returns: none.
67 */
68
69static void
70PrintUserName(info)
71char ** info;
72{
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);
77}
78
79/* Function Name: PrintUserInfo
80 * Description: Prints Information about a user.
81 * Arguments: info - an argument list with the user information
82 * in it.
83 * Returns: none
84 */
85
86static void
87PrintUserInfo(info)
88char ** info;
89{
90 char name[BUFSIZ], buf[BUFSIZ];
91
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);
94 Put_message(buf);
95 sprintf(buf, "User id: %-13s Login shell %-15s Class: %s",
96 info[U_UID], info[U_SHELL], info[U_CLASS]);
97 Put_message(buf);
98 sprintf(buf, "Account is: %-10s Encrypted MIT ID number: %s",
32de30b0 99 UserState(atoi(info[U_STATE])), info[U_MITID]);
075fe5bb 100 Put_message(buf);
101 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME],info[U_MODWITH]);
102 Put_message(buf);
103}
104
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.
109 */
110
111static char **
112SetUserDefaults(info)
113char ** info;
114{
115 info[U_NAME] = Strsave(UNIQUE_LOGIN);
116 info[U_UID] = Strsave(UNIQUE_UID);
07b2e652 117 info[U_SHELL] = Strsave(DEFAULT_SHELL);
075fe5bb 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);
07b2e652 123 info[U_CLASS] = Strsave(DEFAULT_CLASS);
075fe5bb 124 info[U_MODTIME] = info[U_MODBY] = info[U_MODWITH] = info[U_END] = NULL;
125 return(info);
126}
461c03b6 127
08345b74 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).
136 */
137
461c03b6 138char **
139AskUserInfo(info, name)
08345b74 140char ** info;
141Bool name;
142{
075fe5bb 143 char temp_buf[BUFSIZ], *newname, *temp_ptr;
08345b74 144
075fe5bb 145 if (name) {
146 sprintf(temp_buf,"\nChanging Attributes of user %s.\n",info[U_NAME]);
147 Put_message(temp_buf);
148 }
08345b74 149 if (name) {
150 newname = Strsave(info[U_NAME]);
075fe5bb 151 GetValueFromUser("The new login name for this user", &newname);
08345b74 152 }
075fe5bb 153 else
154 GetValueFromUser("Login name for this user", &info[U_NAME]);
155
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]);
07b2e652 163 Put_message("User's MIT ID number (type a new unencrypted number, or keep same encryption)");
164 GetValueFromUser("", &temp_ptr);
075fe5bb 165 if ( strcmp( temp_ptr, info[U_MITID] ) != 0) {
b3e25186 166 EncryptID(temp_buf, temp_ptr, info[U_FIRST], info[U_LAST]);
075fe5bb 167 free(info[U_MITID]);
168 info[U_MITID] = Strsave(temp_buf);
169 }
170 free(temp_ptr);
07b2e652 171 GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]);
08345b74 172
173 FreeAndClear(&info[U_MODTIME], TRUE);
174 FreeAndClear(&info[U_MODBY], TRUE);
175 FreeAndClear(&info[U_MODWITH], TRUE);
176
177/*
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.
181 */
182 if (name)
183 SlipInNewName(info, newname);
184
185 return(info);
186}
187
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:
402461ad 191 * LOGIN, UID, BY_NAME, CLASS.
08345b74 192 * name1 - name of thing specified by type (wildcards okay)
193 * name2 - other name, only used in get user by first and last.
194 * (wildcards okay).
195 * Returns: the first element of the queue containing the user info.
196 */
197
198struct qelem *
461c03b6 199GetUserInfo(type, name1, name2)
08345b74 200int type;
201char *name1, *name2;
202{
203 char * args[2];
461c03b6 204 register int status;
205 struct qelem * elem = NULL;
08345b74 206
207 switch(type) {
208 case LOGIN:
209 args[0] = name1;
14f99d7d 210 if ( (status = do_sms_query("get_user_by_login", 1, args,
211 StoreInfo, (char *) &elem)) != 0) {
85ca828a 212 com_err(program_name, status,
075fe5bb 213 " when attempting to get_user_by_login.");
08345b74 214 return (NULL);
215 }
216 break;
217 case UID:
218 args[0] = name1;
14f99d7d 219 if ( (status = do_sms_query("get_user_by_uid", 1, args,
220 StoreInfo, (char *) &elem)) != 0) {
461c03b6 221 com_err(program_name, status,
075fe5bb 222 " when attempting to get_user_by_uid.");
08345b74 223 return (NULL);
224 }
225 break;
402461ad 226 case BY_NAME:
08345b74 227 args[0] = name1;
228 args[1] = name2;
14f99d7d 229 if ( (status = do_sms_query("get_user_by_name", 2, args,
230 StoreInfo, (char *) &elem)) != 0) {
461c03b6 231 com_err(program_name, status,
075fe5bb 232 " when attempting to get_user_by_name.");
08345b74 233 return (NULL);
234 }
235 break;
236 case CLASS:
237 args[0] = name1;
14f99d7d 238 if ( (status = do_sms_query("get_user_by_class", 1, args,
239 StoreInfo, (char *) &elem)) != 0) {
461c03b6 240 com_err(program_name, status,
075fe5bb 241 " when attempting to get_user_by_class.");
08345b74 242 return (NULL);
243 }
244 break;
08345b74 245 }
246 return( QueueTop(elem) );
247}
248
075fe5bb 249/* Function Name: AddNewUser
250 * Description: Adds a new user to the database.
251 * Arguments: none.
08345b74 252 * Returns: DM_NORMAL.
253 */
254
255/* ARGSUSED */
256int
075fe5bb 257AddNewUser()
08345b74 258{
075fe5bb 259 register int status;
260 char ** args, *info[MAX_ARGS_SIZE];
261
262 args = AskUserInfo(SetUserDefaults(info), FALSE);
14f99d7d 263 if ( (status = do_sms_query("add_user", CountArgs(args),
264 args, Scream, (char *) NULL)) != SMS_SUCCESS)
075fe5bb 265 com_err(program_name, status, " in add_user");
266 else
267 Put_message("New user added to database.");
268 FreeInfo(args);
08345b74 269 return(DM_NORMAL);
270}
271
272
075fe5bb 273/* Function Name: GetLoginName
274 * Description: Asks the user for a login name and reserves
275 * it with kerberous.
276 * Arguments: none.
277 * Returns: a malloced login name for the user.
08345b74 278 */
279
075fe5bb 280static char *
281GetLoginName()
08345b74 282{
075fe5bb 283 char name[BUFSIZ];
284
285 Prompt_input("Login name for this user? ", name, BUFSIZ);
286
287 Put_message(
288 "KERBEROS code not added, did not reserve name with kerberos.");
289
290 return(Strsave(name));
291}
292
293
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.
298 */
08345b74 299
075fe5bb 300static char *
301ChooseUser(elem)
302struct qelem * elem;
303{
08345b74 304 while (elem != NULL) {
075fe5bb 305 char ** info = (char **) elem->q_data;
306 PrintUserInfo(info);
307 switch(YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE)) {
308 case TRUE:
309 return(Strsave(info[U_UID]));
310 case FALSE:
311 break;
312 default: /* quit or ^C. */
313 return(NULL);
08345b74 314 }
315 elem = elem->q_forw;
316 }
075fe5bb 317 return(NULL);
08345b74 318}
319
075fe5bb 320/* Function Name: GetUidNumberFromName
321 * Description: Gets the users uid number, from the name.
322 * Arguments: none.
323 * Returns: uid - a malloced string containing the uid.
08345b74 324 */
325
075fe5bb 326static char *
327GetUidNumberFromName()
08345b74 328{
075fe5bb 329 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
461c03b6 330 register int status;
075fe5bb 331 struct qelem * top = NULL;
332
333 Prompt_input("First Name: ", first, BUFSIZ);
334 Prompt_input("Last Name: ", last, BUFSIZ);
08345b74 335
075fe5bb 336 args[0] = first;
337 args[1] = last;
08345b74 338
14f99d7d 339 switch (status = do_sms_query("get_user_by_name", 2, args,
340 StoreInfo, (char *) &top)) {
075fe5bb 341 case SMS_SUCCESS:
342 break;
343 case SMS_NO_MATCH:
344 Put_message("There is no user in the database with that name.");
345 return(NULL);
346 default:
347 com_err(program_name, status, " in get_user_by_name.");
348 return(NULL);
08345b74 349 }
350
075fe5bb 351 top = QueueTop(top);
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: ");
355 Put_message("");
356 PrintUserName(info);
357 uid = Strsave(info[U_UID]);
358 FreeQueue(top);
359 return(Strsave(uid));
360 }
08345b74 361
075fe5bb 362 Put_message("That name is not unique, choose the user that you want.");
363 uid = ChooseUser(top);
364 FreeQueue(top);
365 return(uid);
08345b74 366}
367
075fe5bb 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).
371 * Returns: none.
08345b74 372 */
373
075fe5bb 374static void
375SetUserPassword(name)
376char * name;
08345b74 377{
075fe5bb 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. */
381}
08345b74 382
075fe5bb 383/* Function Name: GiveBackLogin
384 * Description: Gives back previously reserved kerberous principle.
385 * Arguments: name - principle to give back.
386 * Returns: void.
08345b74 387 */
388
075fe5bb 389static void
390GiveBackLogin(name)
391char * name;
08345b74 392{
075fe5bb 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. */
08345b74 396}
397
075fe5bb 398/* Function Name: RegisterUser
399 * Description: This function registers a user.
400 * Arguments: none.
401 * Returns: DM_NORMAL.
08345b74 402 */
403
08345b74 404int
075fe5bb 405RegisterUser()
08345b74 406{
075fe5bb 407 char * args[MAX_ARGS_SIZE];
408 char *login, *fstype = NULL;
409 char temp_buf[BUFSIZ];
410 register int status;
461c03b6 411
075fe5bb 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.");
414
415 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE)) {
416 case TRUE:
417 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
418 args[0] = Strsave(temp_buf);
419 break;
420 case FALSE:
421 if ( (args[0] = GetUidNumberFromName()) == NULL)
422 return(DM_NORMAL);
423 break;
424 default:
425 return(DM_NORMAL);
461c03b6 426 }
075fe5bb 427
428 if ( ((login = args[1] = GetLoginName()) == NULL) ||
429 ( GetFSTypes(&fstype) == SUB_ERROR ) ) {
430 FreeInfo(args); /* This work because the NULL temination is ok. */
431 return(DM_NORMAL);
432 }
433 args[2] = fstype;
434 args[3] = NULL;
435
14f99d7d 436 switch (status = do_sms_query("register_user", CountArgs(args),
437 args, Scream, (char *) NULL)) {
075fe5bb 438 case SMS_SUCCESS:
439 sprintf(temp_buf, "User %s successfully registered.", login);
440 Put_message(temp_buf);
441 SetUserPassword(login);
442 break;
443 case SMS_IN_USE:
444 GiveBackLogin(login);
445 sprintf(temp_buf, "The username %s is already in use.", login);
446 Put_message(temp_buf);
447 break;
448 default:
449 com_err(program_name, status, " in register_user");
450 break;
451 }
452 FreeInfo(args);
453 return(DM_NORMAL);
08345b74 454}
455
075fe5bb 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.
460 * Returns: none.
08345b74 461 */
462
075fe5bb 463/* ARGSUSED */
464static void
465RealUpdateUser(info, junk)
466char ** info;
467Bool junk;
08345b74 468{
075fe5bb 469 register int status;
470 char error_buf[BUFSIZ];
471 char ** args = AskUserInfo(info, TRUE);
472
14f99d7d 473 if ( (status = do_sms_query("update_user", CountArgs(args),
474 args, Scream, (char *) NULL)) != SMS_SUCCESS) {
075fe5bb 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);
08345b74 478 }
08345b74 479}
480
075fe5bb 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].
08345b74 484 * Returns: DM_NORMAL.
485 */
486
075fe5bb 487/* ARGSUSED */
08345b74 488int
075fe5bb 489UpdateUser(argc, argv)
08345b74 490int argc;
491char **argv;
492{
075fe5bb 493 struct qelem * elem;
08345b74 494
075fe5bb 495 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
496 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
08345b74 497
075fe5bb 498 FreeQueue(elem);
08345b74 499 return(DM_NORMAL);
500}
501
b3e25186 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
506 * Returns: none.
507 */
508
509static void
510RealDeactivateUser(info, one_item)
511char ** info;
512Bool one_item;
513{
514 register int status;
515 char txt_buf[BUFSIZ];
516 char * qargs[2];
517
518 if (one_item) {
519 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
334eb344 520 if (!YesNoQuestion(txt_buf, 2))
b3e25186 521 return;
522 }
523
524 qargs[0] = info[NAME];
525 qargs[1] = "3";
14f99d7d 526 if ((status = do_sms_query("update_user_status", 2, qargs, Scream,
527 (char *) NULL)) != SMS_SUCCESS) {
b3e25186 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);
531 }
532}
533
534
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.
539 */
540
541/* ARGSUSED */
542int
543DeactivateUser(argc, argv)
544int argc;
545char **argv;
546{
547 struct qelem * elem;
548
549 elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
550 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
551
552 FreeQueue(elem);
553 return(DM_NORMAL);
554}
555
556
075fe5bb 557/* ------------------------- Top Menu ------------------------- */
558
559/* DeleteUser() in delete.c */
461c03b6 560
075fe5bb 561/* Function Name: DeleteUserByUid
562 * Description: Deletes the user given a uid number.
563 * Arguments: argc, argv - uid if user in argv[1].
08345b74 564 * Returns: DM_NORMAL.
075fe5bb 565 * NOTES: This just gets the username from the sms server
566 * and performs a DeleteUser().
08345b74 567 */
568
08345b74 569int
075fe5bb 570DeleteUserByUid(argc, argv)
08345b74 571int argc;
572char **argv;
573{
461c03b6 574 int status;
075fe5bb 575 struct qelem *elem = NULL;
576 char ** info;
577
578 if(!ValidName(argv[1]))
579 return(DM_NORMAL);
580
14f99d7d 581 if ( (status = do_sms_query("get_user_by_uid", 1, argv+1, StoreInfo,
582 (char * ) &elem)) != SMS_SUCCESS)
075fe5bb 583 com_err(program_name, status, " in get_user_by_uid");
584
585 info = (char **) elem->q_data;
586 argv[1] = info[U_NAME];
08345b74 587
075fe5bb 588 (void) DeleteUser(argc, argv);
08345b74 589 return(DM_NORMAL);
075fe5bb 590}
08345b74 591
075fe5bb 592/* ------------------------- Show User Information ------------------------- */
08345b74 593
594/* Function Name: ShowUserByLogin
595 * Description: Shows user information given a login name.
596 * Arguments: argc, argv - login name in argv[1].
597 * Returns: DM_NORMAL
598 */
599
600/* ARGSUSED */
601int
602ShowUserByLogin(argc, argv)
603int argc;
604char *argv[];
605{
606 struct qelem *top, *elem;
607
608 elem = top = GetUserInfo(LOGIN, argv[1], (char *) NULL);
075fe5bb 609 Loop(elem, PrintUserInfo);
08345b74 610
611 FreeQueue(top);
612 return (DM_NORMAL);
613}
614
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.
620 */
621
622/* ARGSUSED */
623int
624ShowUserByName(argc, argv)
625int argc;
626char *argv[];
627{
075fe5bb 628 struct qelem *top;
3c1a8806 629 char buf[BUFSIZ];
08345b74 630
075fe5bb 631 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
08345b74 632
075fe5bb 633 if (top == NULL) /* if there was an error then return. */
08345b74 634 return(DM_NORMAL);
635
075fe5bb 636 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
334eb344 637 buf, 2, "f"))
075fe5bb 638 return(DM_NORMAL);
08345b74 639
3c1a8806 640 switch(buf[0]) {
075fe5bb 641 case 'F':
642 case 'f':
643 Loop(top, PrintUserInfo);
644 break;
645 case 'N':
646 case 'n':
647 Loop(top, PrintUserName);
648 break;
649 }
650
08345b74 651 FreeQueue(top);
652 return (DM_NORMAL);
653}
654
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.
658 * Returns: none.
659 */
660
85ca828a 661/* ARGSUSED */
08345b74 662int
663ShowUserByClass(argc, argv)
664int argc;
665char **argv;
666{
075fe5bb 667 struct qelem *top;
08345b74 668
075fe5bb 669 top = GetUserInfo(CLASS, argv[1], (char *) NULL);
670 Loop(top, PrintUserName);
08345b74 671
672 FreeQueue(top);
673 return (DM_NORMAL);
674}
This page took 0.169195 seconds and 5 git commands to generate.