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