]> andersk Git - moira.git/blob - clients/moira/attach.c
fixed include files
[moira.git] / clients / moira / attach.c
1 #if (!defined(lint) && !defined(SABER))
2   static char rcsid_module_c[] = "$Header$";
3 #endif
4
5 /*      This is the file attach.c for the SMS Client, which allows a nieve
6  *      user to quickly and easily maintain most parts of the SMS database.
7  *      It Contains: Functions for maintaining data used by Hesiod 
8  *                   to map courses/projects/users to their file systems, 
9  *                   and maintain filesys info. 
10  *      
11  *      Created:        5/4/88
12  *      By:             Chris D. Peterson
13  *
14  *      $Source$
15  *      $Author$
16  *      $Header$
17  *      
18  *      Copyright 1988 by the Massachusetts Institute of Technology.
19  *
20  *      For further information on copyright and distribution 
21  *      see the file mit-copyright.h
22  */
23
24 #include <stdio.h>
25 #include <strings.h>
26 #include <sms.h>
27 #include <sms_app.h>
28 #include <menu.h>
29
30 #include "mit-copyright.h"
31 #include "defs.h"
32 #include "f_defs.h"
33 #include "globals.h"
34
35 #define FS_ALIAS_TYPE "FILESYS"
36
37 #define LABEL        0
38 #define MACHINE      1
39 #define GROUP        2
40 #define ALIAS        3
41
42 #define DEFAULT_TYPE     ("NFS")
43 #define DEFAULT_MACHINE  DEFAULT_NONE
44 #define DEFAULT_PACK     DEFAULT_NONE
45 #define DEFAULT_M_POINT  DEFAULT_NONE
46 #define DEFAULT_ACCESS   ("w")
47 #define DEFAULT_COMMENTS DEFAULT_COMMENT
48 #define DEFAULT_OWNER    (user)
49 #define DEFAULT_OWNERS   (user)
50 #define DEFAULT_CREATE   DEFAULT_YES
51 #define DEFAULT_L_TYPE   ("PROJECT")
52
53 /*      Function Name: SetDefaults
54  *      Description: sets the default values for filesystem additions.
55  *      Arguments: info - an array of char pointers to recieve defaults. 
56  *      Returns: char ** (this array, now filled).
57  */
58
59 static char ** 
60 SetDefaults(info, name)
61 char ** info;
62 char * name;
63 {
64     info[FS_NAME] =     Strsave(name);
65     info[FS_TYPE] =     Strsave(DEFAULT_TYPE);
66     info[FS_MACHINE] =  Strsave(DEFAULT_MACHINE);
67     info[FS_PACK] =     Strsave(DEFAULT_PACK);
68     info[FS_M_POINT] =  Strsave(DEFAULT_M_POINT);
69     info[FS_ACCESS] =   Strsave(DEFAULT_ACCESS);
70     info[FS_COMMENTS] = Strsave(DEFAULT_COMMENTS);
71     info[FS_OWNER] =    Strsave(DEFAULT_OWNER);
72     info[FS_OWNERS] =   Strsave(DEFAULT_OWNERS);
73     info[FS_CREATE] =   Strsave(DEFAULT_CREATE);
74     info[FS_L_TYPE] =   Strsave(DEFAULT_L_TYPE);
75     info[FS_MODTIME] = info[FS_MODBY] = info[FS_MODWITH] = info[FS_END] = NULL;
76     return(info);
77 }
78
79 /*      Function Name: GetFSInfo
80  *      Description: Stores the info in a queue.
81  *      Arguments: type - type of information to get.
82  *                 name - name of the item to get information on.
83  *      Returns: a pointer to the first element in the queue.
84  */
85
86 static struct qelem *
87 GetFSInfo(type, name)
88 int type;
89 char *name;
90 {
91     int stat;
92     struct qelem * elem = NULL;
93     char * args[5];
94
95     switch (type) {
96     case LABEL:
97         if ( (stat = do_sms_query("get_filesys_by_label", 1, &name,
98                                   StoreInfo, (char *)&elem)) != 0) {
99             com_err(program_name, stat, NULL);
100             return(NULL);
101         }
102         break;
103     case MACHINE:
104         if ( (stat = do_sms_query("get_filesys_by_machine", 1, &name,
105                                   StoreInfo, (char *)&elem)) != 0) {
106             com_err(program_name, stat, NULL);
107             return(NULL);
108         }
109         break;
110     case GROUP:
111         if ( (stat = do_sms_query("get_filesys_by_group", 1, &name,
112                                   StoreInfo, (char *)&elem)) != 0) {
113             com_err(program_name, stat, NULL);
114             return(NULL);
115         }
116         break;
117     case ALIAS:
118         args[ALIAS_NAME] = name;
119         args[ALIAS_TYPE] = FS_ALIAS_TYPE;
120         args[ALIAS_TRANS] = "*";
121         if ( (stat = do_sms_query("get_alias", 3, args, StoreInfo, 
122                                   (char *) &elem)) != 0) {
123             com_err(program_name, stat, " in get_alias.");
124             return(NULL);
125         }
126     }
127
128     return(QueueTop(elem));
129 }
130
131 /*      Function Name: PrintFSAlias
132  *      Description: Prints a filesystem alias
133  *      Arguments: info - an array contains the strings of info.
134  *      Returns: the name of the filesys - used be QueryLoop().
135  */
136
137 static char *
138 PrintFSAlias(info)
139 char ** info;
140 {
141     char buf[BUFSIZ];
142
143     sprintf(buf,"Alias: %-25s Filesystem: %s",info[ALIAS_NAME], 
144             info[ALIAS_TRANS]);
145     Put_message(buf);
146     return(info[ALIAS_NAME]);
147 }
148
149 /*      Function Name: PrintFSInfo
150  *      Description: Prints the filesystem information.
151  *      Arguments: info - a pointer to the filesystem information.
152  *      Returns: none.
153  */
154
155 static char *
156 PrintFSInfo(info)
157 char ** info;
158 {
159     char print_buf[BUFSIZ];
160     FORMFEED;
161     sprintf(print_buf,"%20s Filesystem: %s", 
162             " ",info[FS_NAME]);
163     Put_message(print_buf);
164     sprintf(print_buf,"Type: %-40s Machine: %-15s",
165             info[FS_TYPE], info[FS_MACHINE]);
166     Put_message(print_buf);
167     sprintf(print_buf,"Default Access: %-2s Packname: %-17s Mountpoint %s ",
168             info[FS_ACCESS], info[FS_PACK], info[FS_M_POINT]);
169     Put_message(print_buf);
170     sprintf(print_buf,"Comments; %s",info[FS_COMMENTS]);
171     Put_message(print_buf);
172     sprintf(print_buf, "User Ownership: %-30s Group Ownership: %s",
173             info[FS_OWNER], info[FS_OWNERS]);
174     Put_message(print_buf);
175     sprintf(print_buf, "Auto Create: %-34s Locker Type: %s",
176             atoi(info[FS_CREATE]) ? "ON" : "OFF", 
177             info[FS_L_TYPE]);
178     Put_message(print_buf);
179     sprintf(print_buf, MOD_FORMAT, info[FS_MODBY], info[FS_MODTIME], 
180             info[FS_MODWITH]);
181     Put_message(print_buf);
182     return(info[FS_NAME]);
183 }
184
185 /*      Function Name: AskFSInfo.
186  *      Description: This function askes the user for information about a 
187  *                   machine and saves it into a structure.
188  *      Arguments: info - a pointer the the structure to put the
189  *                             info into.
190  *                 name - add a newname field? (T/F)
191  *      Returns: none.
192  */
193
194 static char **
195 AskFSInfo(info, name)
196 char ** info;
197 Bool name;
198 {
199     char temp_buf[BUFSIZ], *newname;
200
201     Put_message("");
202     sprintf(temp_buf, "Changing Attributes of filesystem %s.", 
203             info[FS_NAME]);
204     Put_message(temp_buf);
205     Put_message("");
206
207     if (name) {
208         newname = Strsave(info[FS_NAME]);
209         GetValueFromUser("The new name for this filesystem",
210                          &newname);
211     }
212
213     GetTypeFromUser("Filesystem's Type", "filesys", &info[FS_TYPE]);
214     GetValueFromUser("Filesystem's Machine", &info[FS_MACHINE]);
215     strcpy(temp_buf, CanonicalizeHostname(info[FS_MACHINE]));
216     free(info[FS_MACHINE]);
217     info[FS_MACHINE] = Strsave(temp_buf);
218     GetValueFromUser("Filesystem's Pack Name", &info[FS_PACK]);
219     GetValueFromUser("Filesystem's Mount Point", &info[FS_M_POINT]);
220     GetValueFromUser("Filesystem's Default Access", &info[FS_ACCESS]);
221     GetValueFromUser("Comments about this Filesystem", &info[FS_COMMENTS]);
222     GetValueFromUser("Filesystem's owner (user)", &info[FS_OWNER]);
223     GetValueFromUser("Filesystem's owners (group)", &info[FS_OWNERS]);
224     GetYesNoValueFromUser("Automatically create this filesystem",
225                      &info[FS_CREATE]);
226     GetTypeFromUser("Filesystem's lockertype", "lockertype", &info[FS_L_TYPE]);
227
228     FreeAndClear(&info[FS_MODTIME], TRUE);
229     FreeAndClear(&info[FS_MODBY], TRUE);
230     FreeAndClear(&info[FS_MODWITH], TRUE);
231
232     if (name)                   /* slide the newname into the #2 slot. */
233         SlipInNewName(info, newname);
234
235     return(info);
236 }
237
238 /* --------------- Filesystem Menu ------------- */
239
240 /*      Function Name: GetFS
241  *      Description: Get Filesystem information by name.
242  *      Arguments: argc, argv - name of filsys in argv[1].
243  *      Returns: DM_NORMAL.
244  */
245
246 /* ARGSUSED */
247 int
248 GetFS(argc, argv)
249 int argc;
250 char **argv;
251 {
252     struct qelem *top;
253
254     top = GetFSInfo(LABEL, argv[1]); /* get info. */
255     Loop(top, (void *) PrintFSInfo);
256     FreeQueue(top);             /* clean the queue. */
257     return (DM_NORMAL);
258 }
259
260 /*      Function Name: RealDeleteFS
261  *      Description: Does the real deletion work.
262  *      Arguments: info - array of char *'s containing all useful info.
263  *                 one_item - a Boolean that is true if only one item 
264  *                              in queue that dumped us here.
265  *      Returns: none.
266  */
267
268 void
269 RealDeleteFS(info, one_item)
270 char ** info;
271 Bool one_item;
272 {
273     int stat;
274     char temp_buf[BUFSIZ];
275
276 /* 
277  * Deletetions are  performed if the user hits 'y' on a list of multiple 
278  * filesystem, or if the user confirms on a unique alias.
279  */
280     sprintf(temp_buf, "Are you sure that you want to delete filesystem %s",
281             info[FS_NAME]);
282     if(!one_item || Confirm(temp_buf)) {
283         if ( (stat = do_sms_query("delete_filesys", 1,
284                                   &info[FS_NAME], Scream, NULL)) != 0)
285             com_err(program_name, stat, " filesystem not deleted.");
286         else
287             Put_message("Filesystem deleted.");
288     }
289     else 
290         Put_message("Filesystem not deleted.");
291 }
292
293 /*      Function Name: DeleteFS
294  *      Description: Delete a filesystem give its name.
295  *      Arguments: argc, argv - argv[1] is the name of the filesystem.
296  *      Returns: none.
297  */
298
299 /* ARGSUSED */
300  
301 int
302 DeleteFS(argc, argv)
303 int argc;
304 char **argv;
305 {
306     struct qelem *elem = GetFSInfo(LABEL, argv[1]);
307     QueryLoop(elem, PrintFSInfo, RealDeleteFS, "Delete the Filesystem");
308
309     FreeQueue(elem);
310     return (DM_NORMAL);
311 }
312
313 /*      Function Name: RealChangeFS
314  *      Description: performs the actual change to the filesys.
315  *      Arguments: info - the information 
316  *                 junk - an unused boolean.
317  *      Returns: none.
318  */
319
320 /* ARGSUSED. */
321 static void
322 RealChangeFS(info, junk)
323 char ** info;
324 Bool junk;
325 {
326     int stat;
327     char ** args = AskFSInfo(info, TRUE);
328     extern Menu nfsphys_menu;
329
330     stat = do_sms_query("update_filesys", CountArgs(args), args,
331                         NullFunc, NULL);
332     switch (stat) {
333     case SMS_NFS:
334         Put_message("That NFS filesystem is not exported.");
335         if (YesNoQuestion("Fix this now (Y/N)")) {
336             Do_menu(&nfsphys_menu, 0, NULL);
337             if (YesNoQuestion("Retry filesystem update now (Y/N)")) {
338                 if (stat = do_sms_query("update_filesys", CountArgs(args), args,
339                                         NullFunc, NULL))
340                     com_err(program_name, stat, " filesystem not updated");
341                 else
342                     Put_message("filesystem sucessfully updated.");
343             }
344         }
345         break;
346     case SMS_SUCCESS:
347         break;
348     default:
349         com_err(program_name, stat, " in UpdateFS");
350     }
351 }
352
353 /*      Function Name: ChangeFS
354  *      Description: change the information in a filesys record.
355  *      Arguments: arc, argv - value of filsys in argv[1].
356  *      Returns: DM_NORMAL.
357  */
358
359 /* ARGSUSED */
360 int
361 ChangeFS(argc, argv)
362 char **argv;
363 int argc;
364 {
365     struct qelem *elem = GetFSInfo(LABEL, argv[1]);
366     QueryLoop(elem, NullPrint, RealChangeFS, "Update the Filesystem");
367
368     FreeQueue(elem);
369     return (DM_NORMAL);
370 }
371
372 /*      Function Name: AddFS
373  *      Description: change the information in a filesys record.
374  *      Arguments: arc, argv - name of filsys in argv[1].
375  *      Returns: DM_NORMAL.
376  */
377
378 /* ARGSUSED */
379 int
380 AddFS(argc, argv)
381 char **argv;
382 int argc;
383 {
384     char *info[MAX_ARGS_SIZE], **args;
385     int stat;
386     extern Menu nfsphys_menu;
387
388     if ( !ValidName(argv[1]) )
389         return(DM_NORMAL);
390
391     if ( (stat = do_sms_query("get_filesys_by_label", 1, argv + 1,
392                               NullFunc, NULL)) == 0) {
393         Put_message ("A Filesystem by that name already exists.");
394         return(DM_NORMAL);
395     } else if (stat != SMS_NO_MATCH) {
396         com_err(program_name, stat, " in AddFS");
397         return(DM_NORMAL);
398     } 
399
400     args = AskFSInfo(SetDefaults(info, argv[1]), FALSE );
401
402     stat = do_sms_query("add_filesys", CountArgs(args), args, NullFunc, NULL);
403     switch (stat) {
404     case SMS_NFS:
405         Put_message("That NFS filesystem is not exported.");
406         if (YesNoQuestion("Fix this now (Y/N)")) {
407             Do_menu(&nfsphys_menu, 0, NULL);
408             if (YesNoQuestion("Retry filesystem creation now (Y/N)")) {
409                 if (stat = do_sms_query("add_filesys", CountArgs(args), args,
410                                         NullFunc, NULL))
411                     com_err(program_name, stat, " in AddFS");
412                 else
413                     Put_message("Created.");
414             }
415         }
416         break;
417     case SMS_SUCCESS:
418         break;
419     default:
420         com_err(program_name, stat, " in AddFS");
421     }
422
423     FreeInfo(info);
424     return (DM_NORMAL);
425 }
426
427 /* -------------- Top Level Menu ---------------- */
428
429 /*      Function Name: GetFSAlias
430  *      Description: Gets the value for a Filesystem Alias.
431  *      Arguments: argc, argv - name of alias in argv[1].
432  *      Returns: DM_NORMAL.
433  *      NOTES: There should only be one filesystem per alias, thus
434  *             this will work correctly.
435  */
436
437 /* ARGSUSED */
438 int
439 GetFSAlias(argc, argv)
440 int argc;
441 char **argv;
442 {
443     struct qelem *top;
444
445     top = GetFSInfo(ALIAS, argv[1]);
446     Put_message(" ");           /* blank line. */
447     Loop(top, (void *) PrintFSAlias);
448     FreeQueue(top);
449     return(DM_NORMAL);
450 }
451
452 /*      Function Name: CreateFSAlias
453  *      Description: Create an alias name for a filesystem
454  *      Arguments: argc, argv - name of alias in argv[1].
455  *      Returns: DM_NORMAL.
456  *      NOTES:  This requires (name, type, transl)  I get {name, translation}
457  *              from the user.  I provide type, which is well-known. 
458  */
459
460 /* ARGSUSED */
461 int
462 CreateFSAlias(argc, argv)
463 int argc;
464 char **argv;
465 {
466     register int stat;
467     struct qelem *elem, *top;
468     char *args[MAX_ARGS_SIZE], buf[BUFSIZ], **info;
469
470     elem = NULL;
471
472     if (!ValidName(argv[1]))
473         return(DM_NORMAL);
474
475     args[ALIAS_NAME] = Strsave(argv[1]);
476     args[ALIAS_TYPE] = Strsave(FS_ALIAS_TYPE);
477     args[ALIAS_TRANS] = Strsave("*");
478
479 /*
480  * Check to see if this alias already exists in the database, if so then
481  * print out values, free memory used and then exit.
482  */
483
484     if ( (stat = do_sms_query("get_alias", 3, args, StoreInfo, 
485                               (char *)&elem)) == 0) {
486         top = elem = QueueTop(elem);
487         while (elem != NULL) {
488             info = (char **) elem->q_data;          
489             sprintf(buf,"The alias: %s currently describes the filesystem %s",
490                     info[ALIAS_NAME], info[ALIAS_TRANS]);
491             Put_message(buf);
492             elem = elem->q_forw;
493         }
494         FreeQueue(top);
495         return(DM_NORMAL);
496     }
497     else if ( stat != SMS_NO_MATCH) {
498         com_err(program_name, stat, " in CreateFSAlias.");
499         return(DM_NORMAL);
500     }
501
502     args[ALIAS_TRANS]= args[ALIAS_END] = NULL;  /* set to NULL initially. */
503     GetValueFromUser("Which filesystem will this alias point to?",
504                      &args[ALIAS_TRANS]);
505
506     if ( (stat = do_sms_query("add_alias", 3, args, NullFunc, NULL)) != 0)
507         com_err(program_name, stat, " in CreateFSAlias.");
508
509     FreeInfo(args);
510     return (DM_NORMAL);
511 }
512     
513 /*      Function Name: RealDeleteFSAlias
514  *      Description: Does the real deletion work.
515  *      Arguments: info - array of char *'s containing all useful info.
516  *                 one_item - a Boolean that is true if only one item 
517  *                              in queue that dumped us here.
518  *      Returns: none.
519  */
520
521 void
522 RealDeleteFSAlias(info, one_item)
523 char ** info;
524 Bool one_item;
525 {
526     int stat;
527     char temp_buf[BUFSIZ];
528
529 /* 
530  * Deletetions are  performed if the user hits 'y' on a list of multiple 
531  * filesystem, or if the user confirms on a unique alias.
532  */
533     sprintf(temp_buf, 
534             "Are you sure that you want to delete the filesystem alias %s",
535             info[ALIAS_NAME]);
536     if(!one_item || Confirm(temp_buf)) {
537         if ( (stat = do_sms_query("delete_alias", CountArgs(info),
538                                   info, Scream, NULL)) != 0 )
539             com_err(program_name, stat, " filesystem alias not deleted.");
540         else
541             Put_message("Filesystem alias deleted.");
542     }
543     else 
544         Put_message("Filesystem alias not deleted.");
545 }
546
547 /*      Function Name: DeleteFSAlias
548  *      Description: Delete an alias name for a filesystem
549  *      Arguments: argc, argv - name of alias in argv[1].
550  *      Returns: DM_NORMAL.
551  *      NOTES:  This requires (name, type, transl)  I get {name, translation}
552  *              from the user.  I provide type, which is well-known. 
553  */
554
555 /* ARGSUSED */
556 int
557 DeleteFSAlias(argc, argv)
558 int argc;
559 char **argv;
560 {
561     struct qelem *elem = GetFSInfo(ALIAS, argv[1]);
562     QueryLoop(elem, PrintFSAlias, RealDeleteFSAlias,
563               "Delete the Filesystem Alias");
564     FreeQueue(elem);
565     return (DM_NORMAL);
566 }
567
568 /*      Function Name: AttachHelp
569  *      Description: Print help info on attachmaint.
570  *      Arguments: none
571  *      Returns: DM_NORMAL.
572  */
573
574 int
575 AttachHelp()
576 {
577     static char *message[] = {
578       "These are the options:\n\n",
579       "get - get information about a filesystem.\n",
580       "add - add a new filesystem to the data base.\n",
581       "update - update the information in the database on a filesystem.\n",
582       "delete - delete a filesystem from the database.\n",
583       "check - check information about association of a name and a filesys.\n",
584       "alias - associate a name with a filesystem.\n",
585       "unalias - disassociate a name with a filesystem.\n",
586       "verbose - toggle the request for delete confirmation.\n",
587         NULL,
588     };
589
590     return(PrintHelp(message));
591 }
This page took 3.360722 seconds and 5 git commands to generate.