]> andersk Git - moira.git/blob - clients/moira/user.c
Don't assume an account has an associated list when deactivating it
[moira.git] / clients / moira / user.c
1 #if (!defined(lint) && !defined(SABER))
2   static char rcsid_module_c[] = "$Header$";
3 #endif
4
5 /*      This is the file user.c for the MOIRA Client, which allows a nieve
6  *      user to quickly and easily maintain most parts of the MOIRA database.
7  *      It Contains: Functions for manipulating user information.
8  *      
9  *      Created:        5/9/88
10  *      By:             Chris D. Peterson
11  *
12  *      $Source$
13  *      $Author$
14  *      $Header$
15  *      
16  *      Copyright 1988 by the Massachusetts Institute of Technology.
17  *
18  *      For further information on copyright and distribution 
19  *      see the file mit-copyright.h
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <moira.h>
25 #include <moira_site.h>
26 #include <menu.h>
27 #include <ctype.h>
28 #include <sys/time.h>
29 #ifdef GDSS
30 #include <des.h>
31 #include <krb.h>
32 #include <gdss.h>
33 #endif
34 #include "mit-copyright.h"
35 #include "defs.h"
36 #include "f_defs.h"
37 #include "globals.h"
38
39 #define LOGIN 0
40 #define UID   1
41 #define BY_NAME  2
42 #define CLASS 3
43 #define ID 4
44
45 #ifdef ATHENA
46 #define DEFAULT_SHELL "/bin/athena/tcsh"
47 #else
48 #define DEFAULT_SHELL "/bin/csh"
49 #endif
50 #define DEFAULT_CLASS "?"
51
52
53 /*      Function Name: UserState
54  *      Description: Convert a numeric state into a descriptive string.
55  *      Arguments: state value
56  *      Returns: pointer to statically allocated string.
57  */
58
59 static char *states[] = { "Registerable (0)",
60                           "Active (1)",
61                           "Half Registered (2)",
62                           "Deleted (3)",
63                           "Not registerable (4)",
64                           "Enrolled/Registerable (5)",
65                           "Enrolled/Not Registerable (6)",
66                           "Half Enrolled (7)" };
67
68 static char *UserState(state)
69 int state;
70 {
71     static char buf[BUFSIZ];
72
73     if (state < 0 || state >= US_END) {
74         sprintf(buf, "Unknown (%d)", state);
75         return(buf);
76     }
77     return(states[state]);
78 }
79
80
81 /*      Function Name: PrintUserName
82  *      Description: Print name of a user.
83  *      Arguments: info - the information about a user.
84  *      Returns: none.
85  */
86
87 static void
88 PrintUserName(info)
89 char ** info;
90 {
91     char buf[BUFSIZ], print_buf[BUFSIZ];
92     sprintf(buf, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
93     sprintf(print_buf, "%-40s User Name: %s", buf, info[U_NAME]);
94     Put_message(print_buf);
95 }
96
97 /*      Function Name: PrintUserInfo
98  *      Description: Prints Information about a user.
99  *      Arguments: info - an argument list with the user information
100  *                          in it.
101  *      Returns: none
102  */
103
104 static void
105 PrintUserInfo(info)
106 char ** info;
107 {
108     char name[BUFSIZ], buf[BUFSIZ];
109     int status;
110 #ifdef GDSS
111     SigInfo si;
112 #endif
113
114     sprintf(name, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
115     sprintf(buf, "Login name: %-20s Full name: %s", info[U_NAME], name);
116     Put_message(buf);
117     sprintf(buf, "User id: %-23s Login shell %-10s Class: %s", 
118             info[U_UID], info[U_SHELL], info[U_CLASS]);
119     Put_message(buf);
120
121 #ifdef GDSS
122     sprintf(buf, "%s:%s", info[U_NAME], info[U_MITID]);
123     si.rawsig = NULL;
124     status = GDSS_Verify(buf, strlen(buf), info[U_SIGNATURE], &si);
125 #ifdef DEBUG
126     hex_dump(info[U_SIGNATURE]);
127     sprintf(buf, "GDSS_Verify => %d", status);
128     Put_message(buf);
129 #endif /* DEBUG */
130 #else /* GDSS */
131     status = 0;
132 #endif /* GDSS */
133
134     sprintf(buf, "Account is: %-20s MIT ID number: %s Signed: %s",
135             UserState(atoi(info[U_STATE])), info[U_MITID],
136             *info[U_SIGNATURE] ? (status ? "Bad" : "Yes") : "No");
137     Put_message(buf);
138     if (atoi(info[U_SECURE]))
139       sprintf(buf, "Secure password set on %s.", atot(info[U_SECURE]));
140     else
141       sprintf(buf, "No secure password set.");
142     Put_message(buf);
143     sprintf(buf, "Comments: %s", info[U_COMMENT]);
144     Put_message(buf);
145     sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME],info[U_MODWITH]);
146     Put_message(buf);
147 }
148
149 /*      Function Name: SetUserDefaults
150  *      Description: Sets the default values for add user.
151  *      Arguments: info - a blank user info array of char *'s.
152  *      Returns: args - the filled info structure.
153  */
154
155 static char **
156 SetUserDefaults(info)
157 char ** info;
158 {
159     info[U_NAME] = Strsave(UNIQUE_LOGIN);
160     info[U_UID] = Strsave(UNIQUE_UID);
161     info[U_SHELL] = Strsave(DEFAULT_SHELL);
162     info[U_LAST] = Strsave(DEFAULT_NONE);
163     info[U_FIRST] = Strsave(DEFAULT_NONE);
164     info[U_MIDDLE] = Strsave(DEFAULT_NONE);
165     info[U_STATE] = Strsave(DEFAULT_NO);
166     info[U_MITID] = Strsave(DEFAULT_NONE);
167     info[U_CLASS] = Strsave(DEFAULT_CLASS);
168     info[U_COMMENT] = Strsave("");
169     info[U_SIGNATURE] = Strsave("");
170     info[U_SECURE] = Strsave("0");
171     info[U_MODTIME] = info[U_MODBY] = info[U_MODWITH] = info[U_END] = NULL;
172     return(info);
173 }
174
175
176 /* Check that the supplied name follows the capitalization rules, and 
177  * offer to correct it if not.
178  */
179
180 CorrectCapitalization(name)
181 char **name;
182 {
183     char temp_buf[BUFSIZ], fixname[BUFSIZ];
184
185     strcpy(fixname, *name);
186     FixCase(fixname);
187     if (strcmp(fixname, *name)) {
188         Put_message("You entered a name which does not follow the capitalization conventions.");
189         sprintf(temp_buf, "Correct it to \"%s\"", fixname);
190         if (YesNoQuestion(temp_buf, 1) == TRUE) {
191             free(*name);
192             *name = strsave(fixname);
193         }
194     }
195 }
196
197
198 /*      Function Name: AskUserInfo.
199  *      Description: This function askes the user for information about a 
200  *                   machine and saves it into a structure.
201  *      Arguments: info - a pointer the the structure to put the info into.
202  *                 flags - Flags asking us which info we want.
203  *      Returns: the args to pass to the query.
204  *      NOTES: the return args are not necessarily in the correct order to
205  *             use the #defined names (e.g args[UID] is not the uid anymore).
206  */
207
208 char **
209 AskUserInfo(info, name)
210 char ** info;
211 Bool name;
212 {
213     int siglen, i;
214 #ifdef GDSS
215     SigInfo si;
216 #endif
217     char temp_buf[BUFSIZ], *newname, *temp_ptr, *sig, sig_buf[BUFSIZ];
218
219     if (name) {
220         sprintf(temp_buf,"\nChanging Attributes of user %s.\n",info[U_NAME]);
221         Put_message(temp_buf);
222     } else {
223         struct qelem *elem = NULL;
224         char *argv[3];
225
226         if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
227           return(NULL);
228         CorrectCapitalization(&info[U_LAST]);
229         if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
230           return(NULL);
231         CorrectCapitalization(&info[U_FIRST]);
232         if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) ==
233             SUB_ERROR)
234           return(NULL);
235         CorrectCapitalization(&info[U_MIDDLE]);
236         argv[0] = info[U_FIRST];
237         argv[1] = info[U_LAST];
238         if (do_mr_query("get_user_account_by_name", 2, argv,
239                          StoreInfo, (char *) &elem) == 0) {
240             Put_message("A user by that name already exists in the database.");
241             Loop(QueueTop(elem), PrintUserInfo);
242             Loop(QueueTop(elem), FreeInfo);
243             FreeQueue(elem);
244             if (YesNoQuestion("Add new user anyway", TRUE) != TRUE)
245               return(NULL);
246         }
247     }
248     if (name) {
249         newname = Strsave(info[U_NAME]);
250         if (GetValueFromUser("The new login name for this user", &newname) ==
251             SUB_ERROR)
252           return(NULL);
253     } else if (GetValueFromUser("Login name for this user", &info[U_NAME]) ==
254                SUB_ERROR)
255       return(NULL);
256
257     if (GetValueFromUser("User's UID", &info[U_UID]) == SUB_ERROR)
258       return(NULL);
259     if (GetValueFromUser("User's shell", &info[U_SHELL]) == SUB_ERROR)
260       return(NULL);
261     if (name) {
262         if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
263           return(NULL);
264         CorrectCapitalization(&info[U_LAST]);
265         if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
266           return(NULL);
267         CorrectCapitalization(&info[U_FIRST]);
268         if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) ==
269             SUB_ERROR)
270           return(NULL);
271         CorrectCapitalization(&info[U_MIDDLE]);
272     }
273     while (1) {
274         int i;
275         if (GetValueFromUser("User's status (? for help)", &info[U_STATE]) ==
276             SUB_ERROR)
277           return(NULL);
278         if (isdigit(info[U_STATE][0]))
279           break;
280         Put_message("Valid status numbers:");
281         for (i = 0; i < US_END; i++) {
282             sprintf(temp_buf, "  %d: %s", i, states[i]);
283             Put_message(temp_buf);
284         }
285     }
286     if (GetValueFromUser("User's MIT ID number", &info[U_MITID]) == SUB_ERROR)
287       return(NULL);
288     RemoveHyphens(info[U_MITID]);
289     if (GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]) ==
290         SUB_ERROR)
291       return(NULL);
292     if (GetValueFromUser("Comments", &info[U_COMMENT]) == SUB_ERROR)
293       return(NULL);
294
295     if (YesNoQuestion("Secure password set",
296                       atoi(info[U_SECURE]) ? TRUE : FALSE) == FALSE) {
297         free(info[U_SECURE]);
298         info[U_SECURE] = strsave("0");
299     } else if (!strcmp(info[U_SECURE], "0")) {
300         char buf[16];
301         struct timeval tv;
302
303         gettimeofday(&tv, (struct timezone *)NULL);
304         sprintf(buf, "%d", tv.tv_sec);
305         free(info[U_SECURE]);
306         info[U_SECURE] = strsave(buf);
307     }
308
309     /* Sign record */
310 #ifdef GDSS
311     if (strcmp(info[U_NAME], UNIQUE_LOGIN)) {
312         if (name)
313           sprintf(temp_buf, "%s:%s", newname, info[U_MITID]);
314         else
315           sprintf(temp_buf, "%s:%s", info[U_NAME], info[U_MITID]);
316         si.rawsig = NULL;
317         i = GDSS_Verify(temp_buf, strlen(temp_buf), info[U_SIGNATURE], &si);
318         /* If it's already signed OK, don't resign it. */
319         if (i != GDSS_SUCCESS) {
320             free(info[U_SIGNATURE]);
321             info[U_SIGNATURE] = malloc(GDSS_Sig_Size() * 2);
322         sign_again:
323             i = GDSS_Sign(temp_buf, strlen(temp_buf), info[U_SIGNATURE]);
324             if (i != GDSS_SUCCESS)
325               com_err(program_name, gdss2et(i), "Failed to create signature");
326             else {
327                 unsigned char buf[256];
328                 si.rawsig = buf;
329                 i = GDSS_Verify(temp_buf, strlen(temp_buf),
330                                 info[U_SIGNATURE], &si);
331                 if (strlen(buf) > 68) {
332 #ifdef DEBUG
333                     Put_message("Signature too long, trying again");
334 #endif /* DEBUG */
335                     goto sign_again;
336                 }
337             }
338 #ifdef DEBUG
339             Put_message("Made signature:");
340         } else {
341             Put_message("Don't need to remake signature");
342 #endif /* DEBUG */
343         }
344 #ifdef DEBUG
345         hex_dump(info[U_SIGNATURE]);
346 #endif /* DEBUG */
347     }
348 #else /* GDSS */
349     info[U_SIGNATURE] = strsave("");
350 #endif /* GDSS */
351
352     FreeAndClear(&info[U_MODTIME], TRUE);
353     FreeAndClear(&info[U_MODBY], TRUE);
354     FreeAndClear(&info[U_MODWITH], TRUE);
355
356 /* 
357  * Slide the newname into the #2 slot, this screws up all future references 
358  * to this list, since we slip the pointer into a info list it gets freed 
359  * when the rest of the list gets freed.
360  */
361     if (name)                   
362         SlipInNewName(info, newname);
363
364     return(info);
365 }
366
367 /*      Function Name: GetUserInfo
368  *      Description: Stores the user information in a queue.
369  *      Arguments: type - type of field given to get info, one of:
370  *                        LOGIN, UID, BY_NAME, CLASS.
371  *                 name1 - name of thing specified by type (wildcards okay)
372  *                 name2 - other name, only used in get user by first and last.
373  *                         (wildcards okay).
374  *      Returns: the first element of the queue containing the user info.
375  *
376  */
377
378 struct qelem *
379 GetUserInfo(type, name1, name2)
380 int type;
381 char *name1, *name2;
382 {
383     char * args[2];
384     register int status;
385     struct qelem * elem = NULL;
386
387     switch(type) {
388     case LOGIN:
389         args[0] = name1;
390         if ( (status = do_mr_query("get_user_account_by_login", 1, args,
391                                     StoreInfo, (char *) &elem)) != 0) {
392             com_err(program_name, status, 
393                     " when attempting to get_user_account_by_login.");
394             return (NULL);               
395         }
396         break;
397     case UID:
398         args[0] = name1;
399         if ( (status = do_mr_query("get_user_account_by_uid", 1, args,
400                                     StoreInfo, (char *) &elem)) != 0) {
401             com_err(program_name, status, 
402                     " when attempting to get_user_account_by_uid.");
403             return (NULL);      
404         }
405         break;
406     case BY_NAME:
407         args[0] = name1;
408         args[1] = name2;    
409         if ( (status = do_mr_query("get_user_account_by_name", 2, args,
410                                     StoreInfo, (char *) &elem)) != 0) {
411             com_err(program_name, status, 
412                     " when attempting to get_user_account_by_name.");
413             return (NULL);      
414         }
415         break;
416     case CLASS:
417         args[0] = name1;
418         if ( (status = do_mr_query("get_user_account_by_class", 1, args,
419                                     StoreInfo, (char *) &elem)) != 0) {
420             com_err(program_name, status, 
421                     " when attempting to get_user_account_by_class.");
422             return (NULL);      
423         }
424         break;
425     case ID:
426         args[0] = name1;
427         if ( (status = do_mr_query("get_user_account_by_id", 1, args,
428                                     StoreInfo, (char *) &elem)) != 0) {
429             com_err(program_name, status, 
430                     " when attempting to get_user_account_by_id.");
431             return (NULL);      
432         }
433         break;
434     }
435     return( QueueTop(elem) );
436 }
437
438 /*      Function Name: AddNewUser
439  *      Description: Adds a new user to the database.
440  *      Arguments: none.
441  *      Returns: DM_NORMAL.
442  */
443
444 /* ARGSUSED */
445 int
446 AddNewUser()
447 {
448     register int status;
449     char ** args, *info[MAX_ARGS_SIZE];
450
451     if ((args = AskUserInfo(SetUserDefaults(info), FALSE)) == NULL) {
452         Put_message("Aborted.");
453         return(DM_NORMAL);
454     }
455     if (args == NULL)
456       return(DM_NORMAL);
457     if ( (status = do_mr_query("add_user_account", CountArgs(args), 
458                                 args, Scream, (char *) NULL)) != MR_SUCCESS)
459         com_err(program_name, status, " in add_user_account");
460     else
461         Put_message("New user added to database.");
462     FreeInfo(args);
463     return(DM_NORMAL);
464 }
465
466
467 /*      Function Name: GetLoginName
468  *      Description: Asks the user for a login name and reserves
469  *                   it with kerberous.
470  *      Arguments: none.
471  *      Returns: a malloced login name for the user.
472  */
473
474 static char *
475 GetLoginName()
476 {
477     char *name;
478
479     name = strsave("");
480     if (GetValueFromUser("Login name for this user? ", &name) == SUB_ERROR)
481       return(NULL);
482     Put_message("KERBEROS code not added, did not reserve name with kerberos.");
483     return(name);
484 }
485
486
487 /*      Function Name: ChooseUser
488  *      Description: Choose a user from a list and return the uid.
489  *      Arguments: top - a queue of user information.
490  *      Returns: uid - the malloced uid of the user that was chosen.
491  */
492
493 static char *
494 ChooseUser(elem)
495 struct qelem * elem;
496 {
497     while (elem != NULL) {
498         char ** info = (char **)  elem->q_data;
499         PrintUserInfo(info);
500         switch(YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE)) {
501         case TRUE:
502             return(Strsave(info[U_UID]));
503         case FALSE:
504             break;
505         default:                /* quit or ^C. */
506             return(NULL);
507         }
508         elem = elem->q_forw;
509     }
510     return(NULL);
511 }
512
513 /*      Function Name: GetUidNumberFromName
514  *      Description: Gets the users uid number, from the name.
515  *      Arguments: none.
516  *      Returns: uid - a malloced string containing the uid.
517  */
518
519 static char *
520 GetUidNumberFromName()
521 {
522     char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
523     register int status;
524     struct qelem * top = NULL;
525     
526     if (!Prompt_input("First Name: ", first, BUFSIZ))
527       return(NULL);
528     if (!Prompt_input("Last  Name: ", last, BUFSIZ))
529       return(NULL);
530     FixCase(first);
531     FixCase(last);
532
533     args[0] = first;
534     args[1] = last;
535     
536     switch (status = do_mr_query("get_user_account_by_name", 2, args,
537                                   StoreInfo, (char *) &top)) {
538     case MR_SUCCESS:
539         break;
540     case MR_NO_MATCH:
541         Put_message("There is no user in the database with that name.");
542         return(NULL);
543     default:
544         com_err(program_name, status, " in get_account_user_by_name.");
545         return(NULL);
546     }
547     
548     top = QueueTop(top);
549     if (QueueCount(top) == 1) /* This is a unique name. */ {
550         char ** info = (char **) top->q_data;
551         Put_message("User ID Number retrieved for the user: ");
552         Put_message("");
553         PrintUserName(info);
554         uid = Strsave(info[U_UID]);
555         FreeQueue(top);
556         return(Strsave(uid));
557     }
558
559     Put_message("That name is not unique, choose the user that you want.");
560     uid = ChooseUser(top);
561     FreeQueue(top);
562     return(uid);
563 }
564
565 /*      Function Name: SetUserPassword
566  *      Description: Set the new kerberos password for this user.
567  *      Arguments: name - kerberos principle name for this user, (login name).
568  *      Returns: none.
569  */
570
571 static void
572 SetUserPassword(name)
573 char * name;
574 {
575     name = name;                        /* make saber happy. */
576     Put_message("Kerberos password not changed, code non-existant.");
577     /* clever message to call account_admin, if this fails. */
578 }
579
580 /*      Function Name:  GiveBackLogin
581  *      Description: Gives back previously reserved kerberous principle.
582  *      Arguments: name - principle to give back.
583  *      Returns: void.
584  */
585
586 static void
587 GiveBackLogin(name)
588 char * name;
589 {
590     name = name;                        /* make saber happy. */
591     Put_message("kerberos code not implimented, name not given back.");
592     /* send mail to db maintainer if this fails. */
593 }
594
595 /*      Function Name: RegisterUser
596  *      Description: This function registers a user.
597  *      Arguments: none.
598  *      Returns: DM_NORMAL.
599  */
600
601 int
602 RegisterUser()
603 {
604     char * args[MAX_ARGS_SIZE];
605     char *login, *fstype = NULL;
606     char temp_buf[BUFSIZ];
607     register int status;
608     
609     Put_message("This function has NO kerberos support, so strange things");
610     Put_message("may happen if you use it to register a user.");
611
612     switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE)) {
613     case TRUE:
614         Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
615         args[0] = Strsave(temp_buf);
616         break;
617     case FALSE:
618         if ( (args[0] = GetUidNumberFromName()) == NULL)
619             return(DM_NORMAL);
620         break;
621     default:
622         return(DM_NORMAL);
623     }
624
625     sprintf(temp_buf, "u%s", args[0]);
626     login = strsave(temp_buf);
627     if ( (GetValueFromUser("Login name for this user? ", &login) == SUB_ERROR) ||
628         ( GetFSTypes(&fstype, FALSE) == SUB_ERROR ) ) {
629         args[1] = login;
630         FreeInfo(args);    /* This work because the NULL temination is ok. */
631         return(DM_NORMAL);
632     }
633     Put_message("KERBEROS code not added, did not reserve name with kerberos.");
634     args[1] = login;
635     args[2] = fstype;
636     args[3] = NULL;
637     
638     switch (status = do_mr_query("register_user", CountArgs(args),
639                                   args, Scream, (char *) NULL)) {
640     case MR_SUCCESS:
641         sprintf(temp_buf, "User %s successfully registered.", login);
642         Put_message(temp_buf);
643         SetUserPassword(login);
644         break;
645     case MR_IN_USE:
646         GiveBackLogin(login);
647         sprintf(temp_buf, "The username %s is already in use.", login);
648         Put_message(temp_buf);
649         break;
650     default:
651         com_err(program_name, status, " in register_user");
652         break;
653     }
654     FreeInfo(args);
655     return(DM_NORMAL);
656 }
657
658 /*      Function Name: RealUpdateUser
659  *      Description: actuall updates the user information.
660  *      Arguments: info - all current information for the user fields.
661  *                 junk - an UNUSED boolean.
662  *      Returns: none.
663  */
664
665 /* ARGSUSED */
666 static void
667 RealUpdateUser(info, junk)
668 char ** info;
669 Bool junk;
670 {
671     register int status;
672     char error_buf[BUFSIZ];
673     char ** args = AskUserInfo(info, TRUE);
674
675     if (args == NULL) {
676         Put_message("Aborted.");
677         return;
678     }
679     if ( (status = do_mr_query("update_user_account", CountArgs(args), 
680                                 args, Scream, (char *) NULL)) != MR_SUCCESS) {
681         com_err(program_name, status, " in ModifyFields");
682         sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
683         Put_message(error_buf);
684     }
685 }
686
687 /*      Function Name: UpdateUser
688  *      Description: Modify some of the information about a user.
689  *      Arguments: argc, argv - login name of the user in argv[1].
690  *      Returns: DM_NORMAL.
691  */
692
693 /* ARGSUSED */
694 int
695 UpdateUser(argc, argv)
696 int argc;
697 char **argv;
698 {
699     struct qelem * elem;
700
701     elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
702     QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
703     
704     FreeQueue(elem);
705     return(DM_NORMAL);
706 }
707
708 /*      Function Name: RealDeactivateUser
709  *      Description: sets the user's status to 3.
710  *      Arguments: info - all current information for the user fields
711  *                 one_item - indicates the user hasn't been queried yet
712  *      Returns: none.
713  */
714
715 static void
716 RealDeactivateUser(info, one_item)
717 char ** info;
718 Bool one_item;
719 {
720     register int status;
721     char txt_buf[BUFSIZ];
722     char * qargs[2], **args;
723     struct qelem *elem = NULL;
724
725     if (one_item) {
726         sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
727         if (YesNoQuestion(txt_buf, FALSE) != TRUE)
728             return;
729     }
730
731     qargs[0] = info[NAME];
732     qargs[1] = "3";
733     if ((status = do_mr_query("update_user_status", 2, qargs, Scream,
734                                (char *) NULL)) != MR_SUCCESS) {
735         com_err(program_name, status, " in update_user_status");
736         sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
737         Put_message(txt_buf);
738     } else if (YesNoQuestion("Also deactivate matching list and filesystem (y/n)",
739                              FALSE) == TRUE) {
740         status = do_mr_query("get_list_info", 1, &(info[NAME]),
741                              StoreInfo, (char *) &elem);
742         if (status == MR_SUCCESS) {
743             args =(char **) (QueueTop(elem)->q_data);
744             free(args[L_ACTIVE]);
745             args[L_ACTIVE] = strsave("0");
746             FreeAndClear(&args[L_MODTIME], TRUE);
747             FreeAndClear(&args[L_MODBY], TRUE);
748             FreeAndClear(&args[L_MODWITH], TRUE);
749             SlipInNewName(args, args[L_NAME]);
750             if (status = do_mr_query("update_list", CountArgs(args), args,
751                                      Scream, (char *) NULL)) {
752                 com_err(program_name, status, " updating list, not deactivating list or filesystem");
753                 FreeInfo(args);
754                 FreeQueue(elem);
755                 return;
756             }
757             FreeInfo(args);
758             FreeQueue(elem);
759             elem = (struct qelem *) NULL;
760         } else if (status != MR_NO_MATCH) {
761             com_err(program_name, status, " getting list info, not deactivating list or filesystem");
762             return;
763         }
764
765         if (status = do_mr_query("get_filesys_by_label", 1, &(info[NAME]),
766                                  StoreInfo, (char *) &elem)) {
767             com_err(program_name, status, " getting filsys info, not deactivating filesystem");
768             FreeInfo(args);
769             FreeQueue(elem);
770             return;
771         }
772         args = (char **) (QueueTop(elem)->q_data);
773         free(args[FS_TYPE]);
774         args[FS_TYPE] = strsave("ERR");
775         free(args[FS_COMMENTS]);
776         args[FS_COMMENTS] = strsave("Locker disabled; call 3-1325 for help");
777         FreeAndClear(&args[FS_MODTIME], TRUE);
778         FreeAndClear(&args[FS_MODBY], TRUE);
779         FreeAndClear(&args[FS_MODWITH], TRUE);
780         SlipInNewName(args, args[FS_NAME]);
781         if (status = do_mr_query("update_filesys", CountArgs(args), args,
782                                  Scream, (char *) NULL)) {
783             com_err(program_name, status, " updating filesystem, not deactivating filesystem");
784             FreeInfo(args);
785             FreeQueue(elem);
786             return;
787         }
788         FreeInfo(args);
789         FreeQueue(elem);
790     }
791 }
792
793
794 /*      Function Name: DeactivateUser
795  *      Description: sets the user's status to 3.
796  *      Arguments: argc, argv - login name of the user in argv[1].
797  *      Returns: DM_NORMAL.
798  */
799
800 /* ARGSUSED */
801 int
802 DeactivateUser(argc, argv)
803 int argc;
804 char **argv;
805 {
806     struct qelem * elem;
807
808     elem = GetUserInfo(LOGIN, argv[1], (char *) NULL);
809     QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
810     
811     FreeQueue(elem);
812     return(DM_NORMAL);
813 }
814
815
816 /* ------------------------- Top Menu ------------------------- */
817
818 /* DeleteUser() in delete.c */
819
820 /*      Function Name: DeleteUserByUid
821  *      Description: Deletes the user given a uid number.
822  *      Arguments: argc, argv - uid if user in argv[1].
823  *      Returns: DM_NORMAL.
824  *      NOTES: This just gets the username from the mr server 
825  *             and performs a DeleteUser().
826  */
827
828 int
829 DeleteUserByUid(argc, argv)
830 int argc;
831 char **argv;
832 {
833     int status;
834     struct qelem *elem = NULL;
835     char ** info;
836
837     if(!ValidName(argv[1]))
838         return(DM_NORMAL);
839     
840     if ( (status = do_mr_query("get_user_account_by_uid", 1, argv+1, StoreInfo,
841                                 (char * ) &elem)) != MR_SUCCESS)
842         com_err(program_name, status, " in get_user_account_by_uid");
843     
844     info = (char **) elem->q_data;
845     argv[1] = info[U_NAME];
846
847     (void) DeleteUser(argc, argv);
848     return(DM_NORMAL);
849
850
851 /* ------------------------- Show User Information ------------------------- */
852
853 /*      Function Name: ShowUserByLogin
854  *      Description: Shows user information given a login name.
855  *      Arguments: argc, argv - login name in argv[1].
856  *      Returns: DM_NORMAL
857  */
858
859 /* ARGSUSED */
860 int
861 ShowUserByLogin(argc, argv)
862 int argc;
863 char *argv[];
864 {
865     struct qelem *top, *elem;
866
867     elem = top = GetUserInfo(LOGIN, argv[1], (char *) NULL);
868     Loop(elem, PrintUserInfo);
869
870     FreeQueue(top);
871     return (DM_NORMAL);
872 }
873
874 /*      Function Name: RetrieveUserByName
875  *      Description: Show information on a user give fist and/or last name.
876  *      Arguments: argc, argv - argv[1] - first name.
877  *                              argv[2] - last name.
878  *      Returns: DM_NORMAL.
879  */
880
881 /* ARGSUSED */
882 int
883 ShowUserByName(argc, argv)
884 int argc;
885 char *argv[];
886 {
887     struct qelem *top;
888     char buf[BUFSIZ];
889
890     top = GetUserInfo(BY_NAME, argv[1], argv[2]);
891
892     if (top == NULL)            /* if there was an error then return. */
893         return(DM_NORMAL);
894
895     if (!PromptWithDefault("Print full information, or just the names (f/n)?",
896                            buf, 2, "f"))
897         return(DM_NORMAL);
898
899     switch(buf[0]) {
900     case 'F':
901     case 'f':
902         Loop(top, PrintUserInfo);
903         break;
904     case 'N':
905     case 'n':
906         Loop(top, PrintUserName);
907         break;
908     }
909     
910     FreeQueue(top);
911     return (DM_NORMAL);
912 }
913
914 /*      Function Name: ShowUserByClass
915  *      Description: Shows real and login names of all users in class.
916  *      Arguments: argc, argv - argv[1] contains the class.
917  *      Returns: none.
918  */
919
920 /* ARGSUSED */
921 int
922 ShowUserByClass(argc, argv)
923 int argc;
924 char **argv;
925 {
926     struct qelem *top;
927
928     if (YesNoQuestion("This will take a long time.  Are you sure", 0) == FALSE)
929       return (DM_NORMAL);
930     top = GetUserInfo(CLASS, argv[1], (char *) NULL);
931     Loop(top, PrintUserName);
932
933     FreeQueue(top);
934     return (DM_NORMAL);
935 }
936
937
938 /*      Function Name: ShowUserById
939  *      Description: Shows user information given an ID number.
940  *      Arguments: argc, argv - ID number in argv[1].
941  *      Returns: DM_NORMAL
942  */
943
944 /* ARGSUSED */
945 int
946 ShowUserById(argc, argv)
947 int argc;
948 char *argv[];
949 {
950     struct qelem *top, *elem;
951
952     elem = top = GetUserInfo(ID, argv[1], (char *) NULL);
953     Loop(elem, PrintUserInfo);
954
955     FreeQueue(top);
956     return (DM_NORMAL);
957 }
958
959
960 /*      Function Name: GetKrbmap
961  *      Description: Shows user <-> Kerberos mappings
962  *      Arguments: argc, argv - argv[1] contains the user login name,
963  *              argv[2] contains the principal
964  *      Returns: none.
965  */
966
967 /* ARGSUSED */
968 int
969 GetKrbmap(argc, argv)
970 int argc;
971 char **argv;
972 {
973     int stat;
974     struct qelem *elem = NULL, *top;
975     char buf[BUFSIZ];
976
977     if ((stat = do_mr_query("get_kerberos_user_map", 2, &argv[1],
978                              StoreInfo, (char *)&elem)) != 0) {
979         com_err(program_name, stat, " in GetKrbMap.");
980         return(DM_NORMAL);
981     }
982
983     top = elem = QueueTop(elem);
984     Put_message("");
985     while (elem != NULL) {
986         char **info = (char **) elem->q_data;
987         sprintf(buf, "User: %-9s Principal: %s",
988                 info[KMAP_USER], info[KMAP_PRINCIPAL]);
989         Put_message(buf);
990         elem = elem->q_forw;
991     }
992
993     FreeQueue(QueueTop(top));
994     return(DM_NORMAL);
995 }
996
997
998 /*      Function Name: AddKrbmap
999  *      Description: Add a new user <-> Kerberos mapping
1000  *      Arguments: argc, argv - argv[1] contains the user login name,
1001  *              argv[2] contains the principal
1002  *      Returns: none.
1003  */
1004
1005 /* ARGSUSED */
1006 int
1007 AddKrbmap(argc, argv)
1008 int argc;
1009 char **argv;
1010 {
1011     int stat;
1012
1013     if (!strchr(argv[KMAP_PRINCIPAL + 1], '@')) {
1014         Put_message("Please specify a realm for the kerberos principal.");
1015         return(DM_NORMAL);
1016     }
1017     if ((stat = do_mr_query("add_kerberos_user_map", 2, &argv[1],
1018                              Scream, NULL)) != 0) {
1019         com_err(program_name, stat, " in AddKrbMap.");
1020         if (stat == MR_EXISTS)
1021           Put_message("No user or principal may have more than one mapping.");
1022     }
1023     return(DM_NORMAL);
1024 }
1025
1026
1027 /*      Function Name: DeleteKrbmap
1028  *      Description: Remove a user <-> Kerberos mapping
1029  *      Arguments: argc, argv - argv[1] contains the user login name,
1030  *              argv[2] contains the principal
1031  *      Returns: none.
1032  */
1033
1034 /* ARGSUSED */
1035 int
1036 DeleteKrbmap(argc, argv)
1037 int argc;
1038 char **argv;
1039 {
1040     int stat;
1041
1042     if ((stat = do_mr_query("delete_kerberos_user_map", 2, &argv[1],
1043                              Scream, NULL)) != 0) {
1044         com_err(program_name, stat, " in DeleteKrbMap.");
1045     }
1046     return(DM_NORMAL);
1047 }
1048
1049
1050 /*      Function Name: GetDirFlags
1051  *      Description: Shows MITdir listing preferences
1052  *      Arguments: argc, argv - argv[1] contains the user login name
1053  *      Returns: none.
1054  */
1055
1056 /* ARGSUSED */
1057 int
1058 GetDirFlags(argc, argv)
1059 int argc;
1060 char **argv;
1061 {
1062     int stat, flags;
1063     struct qelem *elem = NULL;
1064     char buf[BUFSIZ], **info;
1065
1066     if (!ValidName(argv[1]))
1067         return(DM_NORMAL);
1068     
1069     if ((stat = do_mr_query("get_user_directory_flags", 1, &argv[1],
1070                              StoreInfo, (char *)&elem)) != 0) {
1071         com_err(program_name, stat, " in GetDirFlags.");
1072         return(DM_NORMAL);
1073     }
1074
1075     info = (char **) QueueTop(elem)->q_data;
1076     flags = atoi(info[0]);
1077     FreeQueue(QueueTop(elem));
1078
1079     Put_message("");
1080     sprintf(buf, "User: %s", argv[1]);
1081     Put_message(buf);
1082     if (flags & DIRFLAGS_SUPPRESS) {
1083         Put_message("Does NOT appear in the on-line directory.");
1084     } else {
1085         Put_message("Does appear in the on-line directory.");
1086         if (flags & DIRFLAGS_NONLOCAL) {
1087             Put_message("Is listed with non-MIT.EDU email address (if known)");
1088         } else {
1089             Put_message("Is listed with MIT.EDU email address.");
1090         }
1091     }
1092
1093     return(DM_NORMAL);
1094 }
1095
1096 /*      Function Name: SetDirFlags
1097  *      Description: Update online directory preferences
1098  *      Arguments: argc, argv - the login name of the user in argv[1].
1099  *      Returns: DM_NORMAL.
1100  */
1101
1102 int
1103 SetDirFlags(argc, argv)
1104 int argc;
1105 char **argv;
1106 {
1107     int stat, flags;
1108     char **info, buf[BUFSIZ], *args[2];
1109     struct qelem *elem = NULL;
1110
1111     if (!ValidName(argv[1]))
1112         return(DM_NORMAL);
1113     
1114     /* Fetch current prefs */
1115     if ((stat = do_mr_query("get_user_directory_flags", 1, &argv[1],
1116                              StoreInfo, (char *)&elem)) != 0) {
1117         com_err(program_name, stat, " in GetDirFlags.");
1118         return(DM_NORMAL);
1119     }
1120     info = (char **) QueueTop(elem)->q_data;
1121     flags = atoi(info[0]);
1122     FreeQueue(QueueTop(elem));
1123
1124     sprintf(buf, "List %s in the on-line directory (y/n)", argv[1]);
1125     if (YesNoQuestion(buf, !(flags & DIRFLAGS_SUPPRESS)))
1126         flags &= ~DIRFLAGS_SUPPRESS;
1127     else
1128         flags |= DIRFLAGS_SUPPRESS;
1129
1130     sprintf(buf, "List MIT.EDU email address even when mail is forwarded elsewhere? (y/n)");
1131     if (YesNoQuestion(buf, !(flags & DIRFLAGS_NONLOCAL)))
1132         flags &= ~DIRFLAGS_NONLOCAL;
1133     else
1134         flags |= DIRFLAGS_NONLOCAL;
1135
1136     args[0] = argv[1];
1137     sprintf(buf, "%d", flags);
1138     args[1] = buf;
1139     if ( (stat = do_mr_query("update_user_directory_flags", 2,
1140                                args, Scream, NULL)) != MR_SUCCESS)
1141         com_err(program_name, stat, " in SetDirFlags");
1142     else
1143         Put_message("Directory preferences set.");
1144
1145     return (DM_NORMAL);
1146 }
1147
1148 #ifdef DEBUG
1149 hex_dump(p)
1150 unsigned  char *p;
1151 {
1152     char buf[BUFSIZ];
1153     int i;
1154
1155     sprintf(buf, "Size: %d", strlen(p));
1156     Put_message(buf);
1157     while (strlen(p) >= 8) {
1158         sprintf(buf, "%02x %02x %02x %02x %02x %02x %02x %02x",
1159                 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
1160         Put_message(buf);
1161         p += 8;
1162     }
1163     switch (strlen(p)) {
1164     case 7:
1165         sprintf(buf, "%02x %02x %02x %02x %02x %02x %02x",
1166                 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
1167         break;
1168     case 6:
1169         sprintf(buf, "%02x %02x %02x %02x %02x %02x",
1170                 p[0], p[1], p[2], p[3], p[4], p[5]);
1171         break;
1172     case 5:
1173         sprintf(buf, "%02x %02x %02x %02x %02x",
1174                 p[0], p[1], p[2], p[3], p[4]);
1175         break;
1176     case 4:
1177         sprintf(buf, "%02x %02x %02x %02x",
1178                 p[0], p[1], p[2], p[3]);
1179         break;
1180     case 3:
1181         sprintf(buf, "%02x %02x %02x",
1182                 p[0], p[1], p[2]);
1183         break;
1184     case 2:
1185         sprintf(buf, "%02x %02x",
1186                 p[0], p[1]);
1187         break;
1188     case 1:
1189         sprintf(buf, "%02x",
1190                 p[0]);
1191         break;
1192     default:
1193         return;
1194     }
1195     Put_message(buf);
1196 }
1197 #endif
This page took 0.284258 seconds and 5 git commands to generate.