1 #if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
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.
12 * By: Chris D. Peterson
18 * Copyright 1988 by the Massachusetts Institute of Technology.
20 * For further information on copyright and distribution
21 * see the file mit-copyright.h
30 #include "mit-copyright.h"
35 #define FS_ALIAS_TYPE "FILESYS"
42 #define NO_MACHINE ("\\[NONE\\]") /* C will remove one of the /'s here,
43 * and the other quotes the [ for
44 * ingres' regexp facility. */
46 #define DEFAULT_TYPE ("NFS")
47 #define DEFAULT_MACHINE DEFAULT_NONE
48 #define DEFAULT_PACK DEFAULT_NONE
49 #define DEFAULT_M_POINT DEFAULT_NONE
50 #define DEFAULT_ACCESS ("w")
51 #define DEFAULT_COMMENTS DEFAULT_COMMENT
52 #define DEFAULT_OWNER (user)
53 #define DEFAULT_OWNERS (user)
54 #define DEFAULT_CREATE DEFAULT_YES
55 #define DEFAULT_L_TYPE ("PROJECT")
57 /* Function Name: SetDefaults
58 * Description: sets the default values for filesystem additions.
59 * Arguments: info - an array of char pointers to recieve defaults.
60 * Returns: char ** (this array, now filled).
64 SetDefaults(info, name)
68 info[FS_NAME] = Strsave(name);
69 info[FS_TYPE] = Strsave(DEFAULT_TYPE);
70 info[FS_MACHINE] = Strsave(DEFAULT_MACHINE);
71 info[FS_PACK] = Strsave(DEFAULT_PACK);
72 info[FS_M_POINT] = Strsave(DEFAULT_M_POINT);
73 info[FS_ACCESS] = Strsave(DEFAULT_ACCESS);
74 info[FS_COMMENTS] = Strsave(DEFAULT_COMMENTS);
75 info[FS_OWNER] = Strsave(DEFAULT_OWNER);
76 info[FS_OWNERS] = Strsave(DEFAULT_OWNERS);
77 info[FS_CREATE] = Strsave(DEFAULT_CREATE);
78 info[FS_L_TYPE] = Strsave(DEFAULT_L_TYPE);
79 info[FS_MODTIME] = info[FS_MODBY] = info[FS_MODWITH] = info[FS_END] = NULL;
83 /* Function Name: GetFSInfo
84 * Description: Stores the info in a queue.
85 * Arguments: type - type of information to get.
86 * name - name of the item to get information on.
87 * Returns: a pointer to the first element in the queue.
96 struct qelem * elem = NULL;
101 if ( (stat = do_sms_query("get_filesys_by_label", 1, &name,
102 StoreInfo, (char *)&elem)) != 0) {
103 com_err(program_name, stat, NULL);
108 if ( (stat = do_sms_query("get_filesys_by_machine", 1, &name,
109 StoreInfo, (char *)&elem)) != 0) {
110 com_err(program_name, stat, NULL);
115 if ( (stat = do_sms_query("get_filesys_by_group", 1, &name,
116 StoreInfo, (char *)&elem)) != 0) {
117 com_err(program_name, stat, NULL);
122 args[ALIAS_NAME] = name;
123 args[ALIAS_TYPE] = FS_ALIAS_TYPE;
124 args[ALIAS_TRANS] = "*";
125 if ( (stat = do_sms_query("get_alias", 3, args, StoreInfo,
126 (char *) &elem)) != 0) {
127 com_err(program_name, stat, " in get_alias.");
132 return(QueueTop(elem));
135 /* Function Name: PrintFSAlias
136 * Description: Prints a filesystem alias
137 * Arguments: info - an array contains the strings of info.
138 * Returns: the name of the filesys - used be QueryLoop().
147 sprintf(buf,"Alias: %-25s Filesystem: %s",info[ALIAS_NAME],
150 return(info[ALIAS_NAME]);
153 static int fsgCount = 1;
156 PrintFSGMembers(info)
159 char print_buf[BUFSIZ];
161 sprintf(print_buf, " %d. Filesystem: %-32s (sort key: %s)", fsgCount++, info[0], info[1]);
162 Put_message(print_buf);
167 /* Function Name: PrintFSInfo
168 * Description: Prints the filesystem information.
169 * Arguments: info - a pointer to the filesystem information.
177 char print_buf[BUFSIZ];
180 sprintf(print_buf,"%20s Filesystem Group: %s",
182 Put_message(print_buf);
184 if (!strcmp(info[FS_TYPE], "FSGROUP")) {
186 struct qelem *elem = NULL;
188 sprintf(print_buf,"Comments; %s",info[FS_COMMENTS]);
189 Put_message(print_buf);
190 sprintf(print_buf, MOD_FORMAT, info[FS_MODBY], info[FS_MODTIME],
192 Put_message(print_buf);
193 Put_message("Containing the filesystems (in order):");
194 if ((stat = do_sms_query("get_fsgroup_members", 1, &info[FS_NAME],
195 StoreInfo, (char *)&elem)) != 0) {
196 if (stat == SMS_NO_MATCH)
197 Put_message(" [no members]");
199 com_err(program_name, stat, NULL);
202 Loop(QueueTop(elem), (void *) PrintFSGMembers);
206 sprintf(print_buf,"Type: %-40s Machine: %-15s",
207 info[FS_TYPE], info[FS_MACHINE]);
208 Put_message(print_buf);
209 sprintf(print_buf,"Default Access: %-2s Packname: %-17s Mountpoint %s ",
210 info[FS_ACCESS], info[FS_PACK], info[FS_M_POINT]);
211 Put_message(print_buf);
212 sprintf(print_buf,"Comments; %s",info[FS_COMMENTS]);
213 Put_message(print_buf);
214 sprintf(print_buf, "User Ownership: %-30s Group Ownership: %s",
215 info[FS_OWNER], info[FS_OWNERS]);
216 Put_message(print_buf);
217 sprintf(print_buf, "Auto Create: %-34s Locker Type: %s",
218 atoi(info[FS_CREATE]) ? "ON" : "OFF",
220 Put_message(print_buf);
221 sprintf(print_buf, MOD_FORMAT, info[FS_MODBY], info[FS_MODTIME],
223 Put_message(print_buf);
225 return(info[FS_NAME]);
228 /* Function Name: AskFSInfo.
229 * Description: This function askes the user for information about a
230 * machine and saves it into a structure.
231 * Arguments: info - a pointer the the structure to put the
233 * name - add a newname field? (T/F)
238 AskFSInfo(info, name)
242 char temp_buf[BUFSIZ], *newname;
246 sprintf(temp_buf, "Changing Attributes of filesystem %s.",
248 Put_message(temp_buf);
252 newname = Strsave(info[FS_NAME]);
253 GetValueFromUser("The new name for this filesystem",
257 GetTypeFromUser("Filesystem's Type", "filesys", &info[FS_TYPE]);
258 if (!strcmp(info[FS_TYPE], "FSGROUP") || !strcmp(info[FS_TYPE], "fsgroup"))
261 free(info[FS_MACHINE]);
262 info[FS_MACHINE] = Strsave(NO_MACHINE);
264 GetValueFromUser("Filesystem's Machine", &info[FS_MACHINE]);
265 strcpy(temp_buf, CanonicalizeHostname(info[FS_MACHINE]));
266 free(info[FS_MACHINE]);
267 info[FS_MACHINE] = Strsave(temp_buf);
270 GetValueFromUser("Filesystem's Pack Name", &info[FS_PACK]);
271 GetValueFromUser("Filesystem's Mount Point", &info[FS_M_POINT]);
272 GetValueFromUser("Filesystem's Default Access", &info[FS_ACCESS]);
274 GetValueFromUser("Comments about this Filesystem", &info[FS_COMMENTS]);
275 GetValueFromUser("Filesystem's owner (user)", &info[FS_OWNER]);
276 GetValueFromUser("Filesystem's owners (group)", &info[FS_OWNERS]);
278 GetYesNoValueFromUser("Automatically create this filesystem",
280 GetTypeFromUser("Filesystem's lockertype", "lockertype", &info[FS_L_TYPE]);
282 FreeAndClear(&info[FS_MODTIME], TRUE);
283 FreeAndClear(&info[FS_MODBY], TRUE);
284 FreeAndClear(&info[FS_MODWITH], TRUE);
286 if (name) /* slide the newname into the #2 slot. */
287 SlipInNewName(info, newname);
292 /* --------------- Filesystem Menu ------------- */
294 /* Function Name: GetFS
295 * Description: Get Filesystem information by name.
296 * Arguments: argc, argv - name of filsys in argv[1].
297 * Returns: DM_NORMAL.
308 top = GetFSInfo(LABEL, argv[1]); /* get info. */
309 Loop(top, (void *) PrintFSInfo);
310 FreeQueue(top); /* clean the queue. */
314 /* Function Name: RealDeleteFS
315 * Description: Does the real deletion work.
316 * Arguments: info - array of char *'s containing all useful info.
317 * one_item - a Boolean that is true if only one item
318 * in queue that dumped us here.
323 RealDeleteFS(info, one_item)
328 char temp_buf[BUFSIZ];
331 * Deletetions are performed if the user hits 'y' on a list of multiple
332 * filesystem, or if the user confirms on a unique alias.
334 sprintf(temp_buf, "Are you sure that you want to delete filesystem %s",
336 if(!one_item || Confirm(temp_buf)) {
337 if ( (stat = do_sms_query("delete_filesys", 1,
338 &info[FS_NAME], Scream, NULL)) != 0)
339 com_err(program_name, stat, " filesystem not deleted.");
341 Put_message("Filesystem deleted.");
344 Put_message("Filesystem not deleted.");
347 /* Function Name: DeleteFS
348 * Description: Delete a filesystem give its name.
349 * Arguments: argc, argv - argv[1] is the name of the filesystem.
360 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
361 QueryLoop(elem, PrintFSInfo, RealDeleteFS, "Delete the Filesystem");
367 /* Function Name: RealChangeFS
368 * Description: performs the actual change to the filesys.
369 * Arguments: info - the information
370 * junk - an unused boolean.
376 RealChangeFS(info, junk)
381 char ** args = AskFSInfo(info, TRUE);
382 extern Menu nfsphys_menu;
384 stat = do_sms_query("update_filesys", CountArgs(args), args,
388 Put_message("That NFS filesystem is not exported.");
389 if (YesNoQuestion("Fix this now (Y/N)")) {
390 Do_menu(&nfsphys_menu, 0, NULL);
391 if (YesNoQuestion("Retry filesystem update now (Y/N)")) {
392 if (stat = do_sms_query("update_filesys", CountArgs(args), args,
394 com_err(program_name, stat, " filesystem not updated");
396 Put_message("filesystem sucessfully updated.");
403 com_err(program_name, stat, " in UpdateFS");
407 /* Function Name: ChangeFS
408 * Description: change the information in a filesys record.
409 * Arguments: arc, argv - value of filsys in argv[1].
410 * Returns: DM_NORMAL.
419 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
420 QueryLoop(elem, NullPrint, RealChangeFS, "Update the Filesystem");
426 /* Function Name: AddFS
427 * Description: change the information in a filesys record.
428 * Arguments: arc, argv - name of filsys in argv[1].
429 * Returns: DM_NORMAL.
438 char *info[MAX_ARGS_SIZE], **args;
440 extern Menu nfsphys_menu;
442 if ( !ValidName(argv[1]) )
445 if ( (stat = do_sms_query("get_filesys_by_label", 1, argv + 1,
446 NullFunc, NULL)) == 0) {
447 Put_message ("A Filesystem by that name already exists.");
449 } else if (stat != SMS_NO_MATCH) {
450 com_err(program_name, stat, " in AddFS");
454 args = AskFSInfo(SetDefaults(info, argv[1]), FALSE );
456 stat = do_sms_query("add_filesys", CountArgs(args), args, NullFunc, NULL);
459 Put_message("That NFS filesystem is not exported.");
460 if (YesNoQuestion("Fix this now (Y/N)")) {
461 Do_menu(&nfsphys_menu, 0, NULL);
462 if (YesNoQuestion("Retry filesystem creation now (Y/N)")) {
463 if (stat = do_sms_query("add_filesys", CountArgs(args), args,
465 com_err(program_name, stat, " in AddFS");
467 Put_message("Created.");
474 com_err(program_name, stat, " in AddFS");
481 /* Function Name: SortAfter
482 * Description: choose a sortkey to cause an item to be added after
483 * the count element in the queue
484 * Arguments: queue of filesys names & sortkeys, queue count pointer
485 * Returns: sort key to use.
490 SortAfter(elem, count)
494 char *prev, *next, prevnext, *key, keybuf[9];
496 /* first find the two keys we need to insert between */
498 for (; count > 0; count--) {
499 prev = ((char **)elem->q_data)[1];
508 next = ((char **)elem->q_data)[1];
510 /* now copy the matching characters */
511 for (key = keybuf; *prev && *prev == *next; next++) {
515 /* and set the last character */
517 *prev = prevnext = 'A';
524 if (*next - *prev > 1) {
525 *key++ = (*next + *prev)/2;
528 *key++ = (prevnext + 'Z')/2;
531 return(Strsave(keybuf));
534 /* Function Name: AddFSToGroup
535 * Description: add a filesystem to an FS group
536 * Arguments: arc, argv - name of group in argv[1], filesys in argv[2].
537 * Returns: DM_NORMAL.
542 AddFSToGroup(argc, argv)
547 struct qelem *elem = NULL;
548 char buf[BUFSIZ], *args[5], *bufp;
550 if ((stat = do_sms_query("get_fsgroup_members", 1, argv+1, StoreInfo,
551 (char *)&elem)) != 0) {
552 if (stat != SMS_NO_MATCH)
553 com_err(program_name, stat, " in AddFSToGroup");
559 stat = do_sms_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
561 com_err(program_name, stat, " in AddFSToGroup");
564 elem = QueueTop(elem);
566 Loop(elem, (void *) PrintFSGMembers);
567 sprintf(buf, "%d", QueueCount(elem));
569 stat = GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp);
572 args[2] = SortAfter(elem, count);
574 FreeQueue(QueueTop(elem));
577 stat = do_sms_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
578 if (stat == SMS_EXISTS) {
579 Put_message("That filesystem is already a member of the group.");
580 Put_message("Use the order command if you want to change the sorting order.");
582 com_err(program_name, stat, " in AddFSToGroup");
587 /* Function Name: RemoveFSFromGroup
588 * Description: delete a filesystem from an FS group
589 * Arguments: arc, argv - name of group in argv[1].
590 * Returns: DM_NORMAL.
595 RemoveFSFromGroup(argc, argv)
602 sprintf(buf, "Delete filesystem %s from FS group %s", argv[2], argv[1]);
605 if ((stat = do_sms_query("remove_filesys_from_fsgroup", 2, argv+1,
606 Scream, NULL)) != 0) {
607 com_err(program_name, stat, ", not removed.");
612 /* Function Name: ChangeFSGroupOrder
613 * Description: change the sortkey on a filesys in an FSgroup
614 * Arguments: arc, argv - name of group in argv[1].
615 * Returns: DM_NORMAL.
620 ChangeFSGroupOrder(argc, argv)
625 struct qelem *elem = NULL, *top;
626 char buf[BUFSIZ], *bufp, *args[3];
628 if ((stat = do_sms_query("get_fsgroup_members", 1, argv+1, StoreInfo,
629 (char *)&elem)) != 0) {
630 if (stat == SMS_NO_MATCH) {
631 sprintf(buf, "Ether %s is not a filesystem group or it has no members", argv[1]);
634 com_err(program_name, stat, " in ChangeFSGroupOrder");
637 top = QueueTop(elem);
639 Loop(top, (void *) PrintFSGMembers);
642 stat = GetValueFromUser("Enter number of the filesystem to move:",
647 Put_message("You must enter a positive number (or 0 to abort).");
649 } else if (src == 0) {
650 Put_message("Aborted.");
653 for (elem = top; src-- > 1 && elem->q_forw; elem = elem->q_forw);
655 Put_message("You entered a number that is too high");
661 stat = GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp);
664 if (src == dst || src == dst + 1) {
665 Put_message("That has no effect on the sorting order!");
668 args[2] = SortAfter(top, dst);
670 args[1] = ((char **)elem->q_data)[0];
671 if ((stat = do_sms_query("remove_filesys_from_fsgroup", 2, args,
672 Scream, NULL)) != 0) {
673 com_err(program_name, stat, " in ChangeFSGroupOrder");
676 if ((stat = do_sms_query("add_filesys_to_fsgroup", 3, args,
677 Scream, NULL)) != 0) {
678 com_err(program_name, stat, " in ChangeFSGroupOrder");
684 /* -------------- Top Level Menu ---------------- */
686 /* Function Name: GetFSAlias
687 * Description: Gets the value for a Filesystem Alias.
688 * Arguments: argc, argv - name of alias in argv[1].
689 * Returns: DM_NORMAL.
690 * NOTES: There should only be one filesystem per alias, thus
691 * this will work correctly.
696 GetFSAlias(argc, argv)
702 top = GetFSInfo(ALIAS, argv[1]);
703 Put_message(" "); /* blank line. */
704 Loop(top, (void *) PrintFSAlias);
709 /* Function Name: CreateFSAlias
710 * Description: Create an alias name for a filesystem
711 * Arguments: argc, argv - name of alias in argv[1].
712 * Returns: DM_NORMAL.
713 * NOTES: This requires (name, type, transl) I get {name, translation}
714 * from the user. I provide type, which is well-known.
719 CreateFSAlias(argc, argv)
724 struct qelem *elem, *top;
725 char *args[MAX_ARGS_SIZE], buf[BUFSIZ], **info;
729 if (!ValidName(argv[1]))
732 args[ALIAS_NAME] = Strsave(argv[1]);
733 args[ALIAS_TYPE] = Strsave(FS_ALIAS_TYPE);
734 args[ALIAS_TRANS] = Strsave("*");
737 * Check to see if this alias already exists in the database, if so then
738 * print out values, free memory used and then exit.
741 if ( (stat = do_sms_query("get_alias", 3, args, StoreInfo,
742 (char *)&elem)) == 0) {
743 top = elem = QueueTop(elem);
744 while (elem != NULL) {
745 info = (char **) elem->q_data;
746 sprintf(buf,"The alias: %s currently describes the filesystem %s",
747 info[ALIAS_NAME], info[ALIAS_TRANS]);
754 else if ( stat != SMS_NO_MATCH) {
755 com_err(program_name, stat, " in CreateFSAlias.");
759 args[ALIAS_TRANS]= args[ALIAS_END] = NULL; /* set to NULL initially. */
760 GetValueFromUser("Which filesystem will this alias point to?",
763 if ( (stat = do_sms_query("add_alias", 3, args, NullFunc, NULL)) != 0)
764 com_err(program_name, stat, " in CreateFSAlias.");
770 /* Function Name: RealDeleteFSAlias
771 * Description: Does the real deletion work.
772 * Arguments: info - array of char *'s containing all useful info.
773 * one_item - a Boolean that is true if only one item
774 * in queue that dumped us here.
779 RealDeleteFSAlias(info, one_item)
784 char temp_buf[BUFSIZ];
787 * Deletetions are performed if the user hits 'y' on a list of multiple
788 * filesystem, or if the user confirms on a unique alias.
791 "Are you sure that you want to delete the filesystem alias %s",
793 if(!one_item || Confirm(temp_buf)) {
794 if ( (stat = do_sms_query("delete_alias", CountArgs(info),
795 info, Scream, NULL)) != 0 )
796 com_err(program_name, stat, " filesystem alias not deleted.");
798 Put_message("Filesystem alias deleted.");
801 Put_message("Filesystem alias not deleted.");
804 /* Function Name: DeleteFSAlias
805 * Description: Delete an alias name for a filesystem
806 * Arguments: argc, argv - name of alias in argv[1].
807 * Returns: DM_NORMAL.
808 * NOTES: This requires (name, type, transl) I get {name, translation}
809 * from the user. I provide type, which is well-known.
814 DeleteFSAlias(argc, argv)
818 struct qelem *elem = GetFSInfo(ALIAS, argv[1]);
819 QueryLoop(elem, PrintFSAlias, RealDeleteFSAlias,
820 "Delete the Filesystem Alias");
825 /* Function Name: AttachHelp
826 * Description: Print help info on attachmaint.
828 * Returns: DM_NORMAL.
834 static char *message[] = {
835 "These are the options:",
837 "get - get information about a filesystem.",
838 "add - add a new filesystem to the data base.",
839 "update - update the information in the database on a filesystem.",
840 "delete - delete a filesystem from the database.",
841 "check - check information about association of a name and a filesys.",
842 "alias - associate a name with a filesystem.",
843 "unalias - disassociate a name with a filesystem.",
844 "verbose - toggle the request for delete confirmation.",
848 return(PrintHelp(message));
851 /* Function Name: FSGroupHelp
852 * Description: Print help info on fsgroups.
854 * Returns: DM_NORMAL.
860 static char *message[] = {
861 "A filesystem group is a named sorted list of filesystems.",
863 "To create, modify, or delete a group itself, use the menu above",
864 " this one, and manipulate a filesystem of type FSGROUP.",
866 " get - get info about a group and show its members",
867 " add - add a new member to a group.",
868 " remove - remove a member from a group.",
869 " order - change the sorting order of a group.",
873 return(PrintHelp(message));