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