]> andersk Git - moira.git/blob - clients/moira/attach.c
added commands to switch protocol versions, and more abbreviations
[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 <menu.h>
28
29 #include "mit-copyright.h"
30 #include "defs.h"
31 #include "f_defs.h"
32 #include "globals.h"
33 #include "infodefs.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   ("r")
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   ("HOMEDIR")
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 = sms_query("get_filesys_by_label", 1, &name,
98                                StoreInfo, &elem)) != 0) {
99             com_err(program_name, stat, NULL);
100             return(NULL);
101         }
102         break;
103     case MACHINE:
104         if ( (stat = sms_query("get_filesys_by_machine", 1, &name,
105                                StoreInfo, &elem)) != 0) {
106             com_err(program_name, stat, NULL);
107             return(NULL);
108         }
109         break;
110     case GROUP:
111         if ( (stat = sms_query("get_filesys_by_group", 1, &name,
112                                StoreInfo, &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 = sms_query("get_alias", 3, args, StoreInfo, &elem)) != 0) {
122             com_err(program_name, stat, " in get_alias.");
123             return(NULL);
124         }
125     }
126
127     return(QueueTop(elem));
128 }
129
130 /*      Function Name: PrintFSAlias
131  *      Description: Prints a filesystem alias
132  *      Arguments: info - an array contains the strings of info.
133  *      Returns: the name of the filesys - used be QueryLoop().
134  */
135
136 static char *
137 PrintFSAlias(info)
138 char ** info;
139 {
140     char buf[BUFSIZ];
141
142     sprintf(buf,"Alias: %-25s Filesystem: %s",info[ALIAS_NAME], 
143             info[ALIAS_TRANS]);
144     Put_message(buf);
145     return(info[ALIAS_NAME]);
146 }
147
148 /*      Function Name: PrintFSInfo
149  *      Description: Prints the filesystem information.
150  *      Arguments: info - a pointer to the filesystem information.
151  *      Returns: none.
152  */
153
154 static char *
155 PrintFSInfo(info)
156 char ** info;
157 {
158     char print_buf[BUFSIZ];
159     FORMFEED;
160     sprintf(print_buf,"%20s Filesystem: %s", 
161             " ",info[FS_NAME]);
162     Put_message(print_buf);
163     sprintf(print_buf,"Type: %-40s Machine: %-15s",
164             info[FS_TYPE], info[FS_MACHINE]);
165     Put_message(print_buf);
166     sprintf(print_buf,"Default Access: %-2s Packname: %-17s Mountpoint %s ",
167             info[FS_ACCESS], info[FS_PACK], info[FS_M_POINT]);
168     Put_message(print_buf);
169     sprintf(print_buf,"Comments; %s",info[FS_COMMENTS]);
170     Put_message(print_buf);
171     sprintf(print_buf, "User Ownership: %-30s Group Ownership: %s",
172             info[FS_OWNER], info[FS_OWNERS]);
173     Put_message(print_buf);
174     sprintf(print_buf, "Auto Create: %-34s Locker Type: %s",
175             atoi(info[FS_CREATE]) ? "ON" : "OFF", 
176             info[FS_L_TYPE]);
177     Put_message(print_buf);
178     sprintf(print_buf, MOD_FORMAT, info[FS_MODBY], info[FS_MODTIME], 
179             info[FS_MODWITH]);
180     Put_message(print_buf);
181     return(info[FS_NAME]);
182 }
183
184 /*      Function Name: AskFSInfo.
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
188  *                             info into.
189  *                 name - add a newname field? (T/F)
190  *      Returns: none.
191  */
192
193 static char **
194 AskFSInfo(info, name)
195 char ** info;
196 Bool name;
197 {
198     char temp_buf[BUFSIZ], *newname;
199
200     Put_message("");
201     sprintf(temp_buf, "Changing Attributes of filesystem %s.", 
202             info[FS_NAME]);
203     Put_message(temp_buf);
204     Put_message("");
205
206     if (name) {
207         newname = Strsave(info[FS_NAME]);
208         GetValueFromUser("The new name for this filesystem",
209                          &newname);
210     }
211
212     GetValueFromUser("Filesystem's Type", &info[FS_TYPE]);
213     GetValueFromUser("Filesystem's Machine", &info[FS_MACHINE]);
214     strcpy(temp_buf, CanonicalizeHostname(info[FS_MACHINE]));
215     free(info[FS_MACHINE]);
216     info[FS_MACHINE] = Strsave(temp_buf);
217     GetValueFromUser("Filesystem's Pack Name", &info[FS_PACK]);
218     GetValueFromUser("Filesystem's Mount Point", &info[FS_M_POINT]);
219     GetValueFromUser("Filesystem's Default Access", &info[FS_ACCESS]);
220     GetValueFromUser("Comments about this Filesystem", &info[FS_COMMENTS]);
221     GetValueFromUser("Filesystem's owner (user)", &info[FS_OWNER]);
222     GetValueFromUser("Filesystem's owners (group)", &info[FS_OWNERS]);
223     GetYesNoValueFromUser("Automatically create this filsystem",
224                      &info[FS_CREATE]);
225     GetValueFromUser("Filesystem's lockertype", &info[FS_L_TYPE]);
226
227     FreeAndClear(&info[FS_MODTIME], TRUE);
228     FreeAndClear(&info[FS_MODBY], TRUE);
229     FreeAndClear(&info[FS_MODWITH], TRUE);
230
231     if (name)                   /* slide the newname into the #2 slot. */
232         SlipInNewName(info, newname);
233
234     return(info);
235 }
236
237 /* --------------- Filesystem Menu ------------- */
238
239 /*      Function Name: GetFS
240  *      Description: Get Filesystem information by name.
241  *      Arguments: argc, argv - name of filsys in argv[1].
242  *      Returns: DM_NORMAL.
243  */
244
245 /* ARGSUSED */
246 int
247 GetFS(argc, argv)
248 int argc;
249 char **argv;
250 {
251     struct qelem *top;
252
253     top = GetFSInfo(LABEL, argv[1]); /* get info. */
254     Loop(top, (void *) PrintFSInfo);
255     FreeQueue(top);             /* clean the queue. */
256     return (DM_NORMAL);
257 }
258
259 /*      Function Name: RealDeleteFS
260  *      Description: Does the real deletion work.
261  *      Arguments: info - array of char *'s containing all useful info.
262  *                 one_item - a Boolean that is true if only one item 
263  *                              in queue that dumped us here.
264  *      Returns: none.
265  */
266
267 void
268 RealDeleteFS(info, one_item)
269 char ** info;
270 Bool one_item;
271 {
272     int stat;
273     char temp_buf[BUFSIZ];
274
275 /* 
276  * Deletetions are  performed if the user hits 'y' on a list of multiple 
277  * filesystem, or if the user confirms on a unique alias.
278  */
279     sprintf(temp_buf, "Are you sure that you want to delete filesystem %s",
280             info[FS_NAME]);
281     if(!one_item || Confirm(temp_buf)) {
282         if ( (stat = sms_query("delete_filesys", 1,
283                                &info[FS_NAME], Scream, NULL)) != 0)
284             com_err(program_name, stat, " filesystem not deleted.");
285         else
286             Put_message("Filesystem deleted.");
287     }
288     else 
289         Put_message("Filesystem not deleted.");
290 }
291
292 /*      Function Name: DeleteFS
293  *      Description: Delete a filesystem give its name.
294  *      Arguments: argc, argv - argv[1] is the name of the filesystem.
295  *      Returns: none.
296  */
297
298 /* ARGSUSED */
299  
300 int
301 DeleteFS(argc, argv)
302 int argc;
303 char **argv;
304 {
305     struct qelem *elem = GetFSInfo(LABEL, argv[1]);
306     QueryLoop(elem, PrintFSInfo, RealDeleteFS, "Delete the Filesystem");
307
308     FreeQueue(elem);
309     return (DM_NORMAL);
310 }
311
312 /*      Function Name: RealChangeFS
313  *      Description: performs the actual change to the filesys.
314  *      Arguments: info - the information 
315  *                 junk - an unused boolean.
316  *      Returns: none.
317  */
318
319 /* ARGSUSED. */
320 static void
321 RealChangeFS(info, junk)
322 char ** info;
323 Bool junk;
324 {
325     int stat;
326     char ** args = AskFSInfo(info, TRUE);
327
328     if ( (stat = sms_query("update_filesys", CountArgs(args), 
329                            args, NullFunc, NULL)) != 0)
330         com_err(program_name, stat, ", filesystem not updated");
331     else
332         Put_message("filesystem sucessfully updated.");
333 }
334
335 /*      Function Name: ChangeFS
336  *      Description: change the information in a filesys record.
337  *      Arguments: arc, argv - value of filsys in argv[1].
338  *      Returns: DM_NORMAL.
339  */
340
341 /* ARGSUSED */
342 int
343 ChangeFS(argc, argv)
344 char **argv;
345 int argc;
346 {
347     struct qelem *elem = GetFSInfo(LABEL, argv[1]);
348     QueryLoop(elem, NullPrint, RealChangeFS, "Update the Filesystem");
349
350     FreeQueue(elem);
351     return (DM_NORMAL);
352 }
353
354 /*      Function Name: AddFS
355  *      Description: change the information in a filesys record.
356  *      Arguments: arc, argv - name of filsys in argv[1].
357  *      Returns: DM_NORMAL.
358  */
359
360 /* ARGSUSED */
361 int
362 AddFS(argc, argv)
363 char **argv;
364 int argc;
365 {
366     char *info[MAX_ARGS_SIZE], **args;
367     int stat;
368
369     if ( !ValidName(argv[1]) )
370         return(DM_NORMAL);
371
372     if ( (stat = sms_query("get_filesys_by_label", 1, argv + 1,
373                            NullFunc, NULL)) == 0) {
374         Put_message ("A Filesystem by that name already exists.");
375         return(DM_NORMAL);
376     } else if (stat != SMS_NO_MATCH) {
377         com_err(program_name, stat, " in AddFS");
378         return(DM_NORMAL);
379     } 
380
381     args = AskFSInfo(SetDefaults(info, argv[1]), FALSE );
382
383     if ( (stat = sms_query("add_filesys", CountArgs(args), args, 
384                          NullFunc, NULL)) != 0)
385         com_err(program_name, stat, " in AddFS");
386
387     FreeInfo(info);
388     return (DM_NORMAL);
389 }
390
391 /* -------------- Top Level Menu ---------------- */
392
393 /*      Function Name: GetFSAlias
394  *      Description: Gets the value for a Filesystem Alias.
395  *      Arguments: argc, argv - name of alias in argv[1].
396  *      Returns: DM_NORMAL.
397  *      NOTES: There should only be one filesystem per alias, thus
398  *             this will work correctly.
399  */
400
401 /* ARGSUSED */
402 int
403 GetFSAlias(argc, argv)
404 int argc;
405 char **argv;
406 {
407     struct qelem *top;
408
409     top = GetFSInfo(ALIAS, argv[1]);
410     Put_message(" ");           /* blank line. */
411     Loop(top, (void *) PrintFSAlias);
412     FreeQueue(top);
413     return(DM_NORMAL);
414 }
415
416 /*      Function Name: CreateFSAlias
417  *      Description: Create an alias name for a filsystem
418  *      Arguments: argc, argv - name of alias in argv[1].
419  *      Returns: DM_NORMAL.
420  *      NOTES:  This requires (name, type, transl)  I get {name, translation}
421  *              from the user.  I provide type, which is well-known. 
422  */
423
424 /* ARGSUSED */
425 int
426 CreateFSAlias(argc, argv)
427 int argc;
428 char **argv;
429 {
430     register int stat;
431     struct qelem *elem, *top;
432     char *args[MAX_ARGS_SIZE], buf[BUFSIZ], **info;
433
434     elem = NULL;
435
436     if (!ValidName(argv[1]))
437         return(DM_NORMAL);
438
439     args[ALIAS_NAME] = Strsave(argv[1]);
440     args[ALIAS_TYPE] = Strsave(FS_ALIAS_TYPE);
441     args[ALIAS_TRANS] = Strsave("*");
442
443 /*
444  * Check to see if this alias already exists in the database, if so then
445  * print out values, free memory used and then exit.
446  */
447
448     if ( (stat = sms_query("get_alias", 3, args, StoreInfo, &elem)) == 0) {
449         top = elem = QueueTop(elem);
450         while (elem != NULL) {
451             info = (char **) elem->q_data;          
452             sprintf(buf,"The alias: %s currently describes the filesystem %s",
453                     info[ALIAS_NAME], info[ALIAS_TRANS]);
454             Put_message(buf);
455             elem = elem->q_forw;
456         }
457         FreeQueue(top);
458         return(DM_NORMAL);
459     }
460     else if ( stat != SMS_NO_MATCH) {
461         com_err(program_name, stat, " in CreateFSAlias.");
462         return(DM_NORMAL);
463     }
464
465     args[ALIAS_TRANS]= args[ALIAS_END] = NULL;  /* set to NULL initially. */
466     GetValueFromUser("Which filesystem will this alias point to?",
467                      &args[ALIAS_TRANS]);
468
469     if ( (stat = sms_query("add_alias", 3, args, NullFunc, NULL)) != 0)
470         com_err(program_name, stat, " in CreateFSAlias.");
471
472     FreeInfo(args);
473     return (DM_NORMAL);
474 }
475     
476 /*      Function Name: RealDeleteFSAlias
477  *      Description: Does the real deletion work.
478  *      Arguments: info - array of char *'s containing all useful info.
479  *                 one_item - a Boolean that is true if only one item 
480  *                              in queue that dumped us here.
481  *      Returns: none.
482  */
483
484 void
485 RealDeleteFSAlias(info, one_item)
486 char ** info;
487 Bool one_item;
488 {
489     int stat;
490     char temp_buf[BUFSIZ];
491
492 /* 
493  * Deletetions are  performed if the user hits 'y' on a list of multiple 
494  * filesystem, or if the user confirms on a unique alias.
495  */
496     sprintf(temp_buf, 
497             "Are you sure that you want to delete the filesystem alias %s",
498             info[ALIAS_NAME]);
499     if(!one_item || Confirm(temp_buf)) {
500         if ( (stat = sms_query("delete_alias", CountArgs(info),
501                                info, Scream, NULL)) != 0 )
502             com_err(program_name, stat, " filesystem alias not deleted.");
503         else
504             Put_message("Filesystem alias deleted.");
505     }
506     else 
507         Put_message("Filesystem alias not deleted.");
508 }
509
510 /*      Function Name: DeleteFSAlias
511  *      Description: Delete an alias name for a filsystem
512  *      Arguments: argc, argv - name of alias in argv[1].
513  *      Returns: DM_NORMAL.
514  *      NOTES:  This requires (name, type, transl)  I get {name, translation}
515  *              from the user.  I provide type, which is well-known. 
516  */
517
518 /* ARGSUSED */
519 int
520 DeleteFSAlias(argc, argv)
521 int argc;
522 char **argv;
523 {
524     struct qelem *elem = GetFSInfo(ALIAS, argv[1]);
525     QueryLoop(elem, PrintFSAlias, RealDeleteFSAlias,
526               "Delete the Filesystem Alias");
527     FreeQueue(elem);
528     return (DM_NORMAL);
529 }
530
531 /*      Function Name: AttachHelp
532  *      Description: Print help info on attachmaint.
533  *      Arguments: none
534  *      Returns: DM_NORMAL.
535  */
536
537 int
538 AttachHelp()
539 {
540     static char *message[] = {
541       "These are the options:\n\n",
542       "get - get information about a filesystem.\n",
543       "add - add a new filesystem to the data base.\n",
544       "update - update the information in the database on a filesystem.\n",
545       "delete - delete a filesystem from the database.\n",
546       "check - check information about association of a name and a filesys.\n",
547       "alias - associate a name with a filsystem.\n",
548       "unalias - disassociate a name with a filesystem.\n",
549       "verbose - toggle the request for delete confirmation.\n",
550         NULL,
551     };
552
553     return(PrintHelp(message));
554 }
555
556 /* 
557  * Local Variables:
558  * mode: c
559  * c-indent-level: 4
560  * c-continued-statement-offset: 4
561  * c-brace-offset: -4
562  * c-argdecl-indent: 4
563  * c-label-offset: -4
564  * End:
565  */
This page took 0.272085 seconds and 5 git commands to generate.