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. */
45 #define NO_MACHINE_BAD ("[NONE]")
47 #define DEFAULT_TYPE ("NFS")
48 #define DEFAULT_MACHINE DEFAULT_NONE
49 #define DEFAULT_PACK DEFAULT_NONE
50 #define DEFAULT_M_POINT DEFAULT_NONE
51 #define DEFAULT_ACCESS ("w")
52 #define DEFAULT_COMMENTS DEFAULT_COMMENT
53 #define DEFAULT_OWNER (user)
54 #define DEFAULT_OWNERS (user)
55 #define DEFAULT_CREATE DEFAULT_YES
56 #define DEFAULT_L_TYPE ("PROJECT")
58 /* Function Name: SetDefaults
59 * Description: sets the default values for filesystem additions.
60 * Arguments: info - an array of char pointers to recieve defaults.
61 * Returns: char ** (this array, now filled).
65 SetDefaults(info, name)
69 info[FS_NAME] = Strsave(name);
70 info[FS_TYPE] = Strsave(DEFAULT_TYPE);
71 info[FS_MACHINE] = Strsave(DEFAULT_MACHINE);
72 info[FS_PACK] = Strsave(DEFAULT_PACK);
73 info[FS_M_POINT] = Strsave(DEFAULT_M_POINT);
74 info[FS_ACCESS] = Strsave(DEFAULT_ACCESS);
75 info[FS_COMMENTS] = Strsave(DEFAULT_COMMENTS);
76 info[FS_OWNER] = Strsave(DEFAULT_OWNER);
77 info[FS_OWNERS] = Strsave(DEFAULT_OWNERS);
78 info[FS_CREATE] = Strsave(DEFAULT_CREATE);
79 info[FS_L_TYPE] = Strsave(DEFAULT_L_TYPE);
80 info[FS_MODTIME] = info[FS_MODBY] = info[FS_MODWITH] = info[FS_END] = NULL;
84 /* Function Name: GetFSInfo
85 * Description: Stores the info in a queue.
86 * Arguments: type - type of information to get.
87 * name - name of the item to get information on.
88 * Returns: a pointer to the first element in the queue.
97 struct qelem * elem = NULL;
102 if ( (stat = do_sms_query("get_filesys_by_label", 1, &name,
103 StoreInfo, (char *)&elem)) != 0) {
104 com_err(program_name, stat, NULL);
109 if ( (stat = do_sms_query("get_filesys_by_machine", 1, &name,
110 StoreInfo, (char *)&elem)) != 0) {
111 com_err(program_name, stat, NULL);
116 if ( (stat = do_sms_query("get_filesys_by_group", 1, &name,
117 StoreInfo, (char *)&elem)) != 0) {
118 com_err(program_name, stat, NULL);
123 args[ALIAS_NAME] = name;
124 args[ALIAS_TYPE] = FS_ALIAS_TYPE;
125 args[ALIAS_TRANS] = "*";
126 if ( (stat = do_sms_query("get_alias", 3, args, StoreInfo,
127 (char *) &elem)) != 0) {
128 com_err(program_name, stat, " in get_alias.");
133 return(QueueTop(elem));
136 /* Function Name: PrintFSAlias
137 * Description: Prints a filesystem alias
138 * Arguments: info - an array contains the strings of info.
139 * Returns: the name of the filesys - used be QueryLoop().
148 sprintf(buf,"Alias: %-25s Filesystem: %s",info[ALIAS_NAME],
151 return(info[ALIAS_NAME]);
154 static int fsgCount = 1;
157 PrintFSGMembers(info)
160 char print_buf[BUFSIZ];
162 sprintf(print_buf, " %d. Filesystem: %-32s (sort key: %s)", fsgCount++, info[0], info[1]);
163 Put_message(print_buf);
168 /* Function Name: PrintFSInfo
169 * Description: Prints the filesystem information.
170 * Arguments: info - a pointer to the filesystem information.
178 char print_buf[BUFSIZ];
182 if (!strcmp(info[FS_TYPE], "FSGROUP")) {
184 struct qelem *elem = NULL;
186 sprintf(print_buf,"%20s Filesystem Group: %s", " ", info[FS_NAME]);
187 Put_message(print_buf);
189 sprintf(print_buf,"Comments; %s",info[FS_COMMENTS]);
190 Put_message(print_buf);
191 sprintf(print_buf, MOD_FORMAT, info[FS_MODBY], info[FS_MODTIME],
193 Put_message(print_buf);
194 Put_message("Containing the filesystems (in order):");
195 if ((stat = do_sms_query("get_fsgroup_members", 1, &info[FS_NAME],
196 StoreInfo, (char *)&elem)) != 0) {
197 if (stat == SMS_NO_MATCH)
198 Put_message(" [no members]");
200 com_err(program_name, stat, NULL);
203 Loop(QueueTop(elem), (void *) PrintFSGMembers);
207 sprintf(print_buf,"%20s Filesystem: %s", " ", info[FS_NAME]);
208 Put_message(print_buf);
209 sprintf(print_buf,"Type: %-40s Machine: %-15s",
210 info[FS_TYPE], info[FS_MACHINE]);
211 Put_message(print_buf);
212 sprintf(print_buf,"Default Access: %-2s Packname: %-17s Mountpoint %s ",
213 info[FS_ACCESS], info[FS_PACK], info[FS_M_POINT]);
214 Put_message(print_buf);
215 sprintf(print_buf,"Comments; %s",info[FS_COMMENTS]);
216 Put_message(print_buf);
217 sprintf(print_buf, "User Ownership: %-30s Group Ownership: %s",
218 info[FS_OWNER], info[FS_OWNERS]);
219 Put_message(print_buf);
220 sprintf(print_buf, "Auto Create: %-34s Locker Type: %s",
221 atoi(info[FS_CREATE]) ? "ON" : "OFF",
223 Put_message(print_buf);
224 sprintf(print_buf, MOD_FORMAT, info[FS_MODBY], info[FS_MODTIME],
226 Put_message(print_buf);
228 return(info[FS_NAME]);
231 /* Function Name: AskFSInfo.
232 * Description: This function askes the user for information about a
233 * machine and saves it into a structure.
234 * Arguments: info - a pointer the the structure to put the
236 * name - add a newname field? (T/F)
241 AskFSInfo(info, name)
245 char temp_buf[BUFSIZ], *newname;
249 sprintf(temp_buf, "Changing Attributes of filesystem %s.",
251 Put_message(temp_buf);
255 newname = Strsave(info[FS_NAME]);
256 GetValueFromUser("The new name for this filesystem",
260 GetTypeFromUser("Filesystem's Type", "filesys", &info[FS_TYPE]);
261 if (!strcasecmp(info[FS_TYPE], "FSGROUP"))
263 if (fsgroup || !strcasecmp(info[FS_TYPE], "AFS")) {
264 free(info[FS_MACHINE]);
265 info[FS_MACHINE] = Strsave(NO_MACHINE);
267 if (!strcmp(info[FS_MACHINE], NO_MACHINE_BAD)) {
268 free(info[FS_MACHINE]);
269 info[FS_MACHINE] = Strsave(NO_MACHINE);
271 GetValueFromUser("Filesystem's Machine", &info[FS_MACHINE]);
272 info[FS_MACHINE] = canonicalize_hostname(info[FS_MACHINE]);
275 GetValueFromUser("Filesystem's Pack Name", &info[FS_PACK]);
276 GetValueFromUser("Filesystem's Mount Point", &info[FS_M_POINT]);
277 GetValueFromUser("Filesystem's Default Access", &info[FS_ACCESS]);
279 GetValueFromUser("Comments about this Filesystem", &info[FS_COMMENTS]);
280 GetValueFromUser("Filesystem's owner (user)", &info[FS_OWNER]);
281 GetValueFromUser("Filesystem's owners (group)", &info[FS_OWNERS]);
283 GetYesNoValueFromUser("Automatically create this filesystem",
285 GetTypeFromUser("Filesystem's lockertype", "lockertype", &info[FS_L_TYPE]);
287 FreeAndClear(&info[FS_MODTIME], TRUE);
288 FreeAndClear(&info[FS_MODBY], TRUE);
289 FreeAndClear(&info[FS_MODWITH], TRUE);
291 if (name) /* slide the newname into the #2 slot. */
292 SlipInNewName(info, newname);
297 /* --------------- Filesystem Menu ------------- */
299 /* Function Name: GetFS
300 * Description: Get Filesystem information by name.
301 * Arguments: argc, argv - name of filsys in argv[1].
302 * Returns: DM_NORMAL.
313 top = GetFSInfo(LABEL, argv[1]); /* get info. */
314 Loop(top, (void *) PrintFSInfo);
315 FreeQueue(top); /* clean the queue. */
319 /* Function Name: RealDeleteFS
320 * Description: Does the real deletion work.
321 * Arguments: info - array of char *'s containing all useful info.
322 * one_item - a Boolean that is true if only one item
323 * in queue that dumped us here.
328 RealDeleteFS(info, one_item)
333 char temp_buf[BUFSIZ];
336 * Deletetions are performed if the user hits 'y' on a list of multiple
337 * filesystem, or if the user confirms on a unique alias.
339 sprintf(temp_buf, "Are you sure that you want to delete filesystem %s",
341 if(!one_item || Confirm(temp_buf)) {
342 if ( (stat = do_sms_query("delete_filesys", 1,
343 &info[FS_NAME], Scream, NULL)) != 0)
344 com_err(program_name, stat, " filesystem not deleted.");
346 Put_message("Filesystem deleted.");
349 Put_message("Filesystem not deleted.");
352 /* Function Name: DeleteFS
353 * Description: Delete a filesystem give its name.
354 * Arguments: argc, argv - argv[1] is the name of the filesystem.
365 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
366 QueryLoop(elem, PrintFSInfo, RealDeleteFS, "Delete the Filesystem");
372 /* Function Name: RealChangeFS
373 * Description: performs the actual change to the filesys.
374 * Arguments: info - the information
375 * junk - an unused boolean.
381 RealChangeFS(info, junk)
386 char ** args = AskFSInfo(info, TRUE);
387 extern Menu nfsphys_menu;
389 stat = do_sms_query("update_filesys", CountArgs(args), args,
393 Put_message("That NFS filesystem is not exported.");
394 if (YesNoQuestion("Fix this now (Y/N)")) {
395 Do_menu(&nfsphys_menu, 0, NULL);
396 if (YesNoQuestion("Retry filesystem update now (Y/N)")) {
397 if (stat = do_sms_query("update_filesys", CountArgs(args), args,
399 com_err(program_name, stat, " filesystem not updated");
401 Put_message("filesystem sucessfully updated.");
408 com_err(program_name, stat, " in UpdateFS");
412 /* Function Name: ChangeFS
413 * Description: change the information in a filesys record.
414 * Arguments: arc, argv - value of filsys in argv[1].
415 * Returns: DM_NORMAL.
424 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
425 QueryLoop(elem, NullPrint, RealChangeFS, "Update the Filesystem");
431 /* Function Name: AddFS
432 * Description: change the information in a filesys record.
433 * Arguments: arc, argv - name of filsys in argv[1].
434 * Returns: DM_NORMAL.
443 char *info[MAX_ARGS_SIZE], **args;
445 extern Menu nfsphys_menu;
447 if ( !ValidName(argv[1]) )
450 if ( (stat = do_sms_query("get_filesys_by_label", 1, argv + 1,
451 NullFunc, NULL)) == 0) {
452 Put_message ("A Filesystem by that name already exists.");
454 } else if (stat != SMS_NO_MATCH) {
455 com_err(program_name, stat, " in AddFS");
459 args = AskFSInfo(SetDefaults(info, argv[1]), FALSE );
461 stat = do_sms_query("add_filesys", CountArgs(args), args, NullFunc, NULL);
464 Put_message("That NFS filesystem is not exported.");
465 if (YesNoQuestion("Fix this now (Y/N)")) {
466 Do_menu(&nfsphys_menu, 0, NULL);
467 if (YesNoQuestion("Retry filesystem creation now (Y/N)")) {
468 if (stat = do_sms_query("add_filesys", CountArgs(args), args,
470 com_err(program_name, stat, " in AddFS");
472 Put_message("Created.");
479 com_err(program_name, stat, " in AddFS");
486 /* Function Name: SortAfter
487 * Description: choose a sortkey to cause an item to be added after
488 * the count element in the queue
489 * Arguments: queue of filesys names & sortkeys, queue count pointer
490 * Returns: sort key to use.
495 SortAfter(elem, count)
499 char *prev, *next, prevnext, *key, keybuf[9];
501 /* first find the two keys we need to insert between */
503 for (; count > 0; count--) {
504 prev = ((char **)elem->q_data)[1];
513 next = ((char **)elem->q_data)[1];
515 /* now copy the matching characters */
516 for (key = keybuf; *prev && *prev == *next; next++) {
520 /* and set the last character */
522 *prev = prevnext = 'A';
529 if (*next - *prev > 1) {
530 *key++ = (*next + *prev)/2;
533 *key++ = (prevnext + 'Z')/2;
536 return(Strsave(keybuf));
539 /* Function Name: AddFSToGroup
540 * Description: add a filesystem to an FS group
541 * Arguments: arc, argv - name of group in argv[1], filesys in argv[2].
542 * Returns: DM_NORMAL.
547 AddFSToGroup(argc, argv)
552 struct qelem *elem = NULL;
553 char buf[BUFSIZ], *args[5], *bufp;
555 if ((stat = do_sms_query("get_fsgroup_members", 1, argv+1, StoreInfo,
556 (char *)&elem)) != 0) {
557 if (stat != SMS_NO_MATCH)
558 com_err(program_name, stat, " in AddFSToGroup");
564 stat = do_sms_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
566 com_err(program_name, stat, " in AddFSToGroup");
569 elem = QueueTop(elem);
571 Loop(elem, (void *) PrintFSGMembers);
572 sprintf(buf, "%d", QueueCount(elem));
574 stat = GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp);
577 args[2] = SortAfter(elem, count);
579 FreeQueue(QueueTop(elem));
582 stat = do_sms_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
583 if (stat == SMS_EXISTS) {
584 Put_message("That filesystem is already a member of the group.");
585 Put_message("Use the order command if you want to change the sorting order.");
587 com_err(program_name, stat, " in AddFSToGroup");
592 /* Function Name: RemoveFSFromGroup
593 * Description: delete a filesystem from an FS group
594 * Arguments: arc, argv - name of group in argv[1].
595 * Returns: DM_NORMAL.
600 RemoveFSFromGroup(argc, argv)
607 sprintf(buf, "Delete filesystem %s from FS group %s", argv[2], argv[1]);
610 if ((stat = do_sms_query("remove_filesys_from_fsgroup", 2, argv+1,
611 Scream, NULL)) != 0) {
612 com_err(program_name, stat, ", not removed.");
617 /* Function Name: ChangeFSGroupOrder
618 * Description: change the sortkey on a filesys in an FSgroup
619 * Arguments: arc, argv - name of group in argv[1].
620 * Returns: DM_NORMAL.
625 ChangeFSGroupOrder(argc, argv)
630 struct qelem *elem = NULL, *top;
631 char buf[BUFSIZ], *bufp, *args[3];
633 if ((stat = do_sms_query("get_fsgroup_members", 1, argv+1, StoreInfo,
634 (char *)&elem)) != 0) {
635 if (stat == SMS_NO_MATCH) {
636 sprintf(buf, "Ether %s is not a filesystem group or it has no members", argv[1]);
639 com_err(program_name, stat, " in ChangeFSGroupOrder");
642 top = QueueTop(elem);
644 Loop(top, (void *) PrintFSGMembers);
647 stat = GetValueFromUser("Enter number of the filesystem to move:",
652 Put_message("You must enter a positive number (or 0 to abort).");
654 } else if (src == 0) {
655 Put_message("Aborted.");
658 for (elem = top; src-- > 1 && elem->q_forw; elem = elem->q_forw);
660 Put_message("You entered a number that is too high");
666 stat = GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp);
669 if (src == dst || src == dst + 1) {
670 Put_message("That has no effect on the sorting order!");
673 args[2] = SortAfter(top, dst);
675 args[1] = ((char **)elem->q_data)[0];
676 if ((stat = do_sms_query("remove_filesys_from_fsgroup", 2, args,
677 Scream, NULL)) != 0) {
678 com_err(program_name, stat, " in ChangeFSGroupOrder");
681 if ((stat = do_sms_query("add_filesys_to_fsgroup", 3, args,
682 Scream, NULL)) != 0) {
683 com_err(program_name, stat, " in ChangeFSGroupOrder");
689 /* -------------- Top Level Menu ---------------- */
691 /* Function Name: GetFSAlias
692 * Description: Gets the value for a Filesystem Alias.
693 * Arguments: argc, argv - name of alias in argv[1].
694 * Returns: DM_NORMAL.
695 * NOTES: There should only be one filesystem per alias, thus
696 * this will work correctly.
701 GetFSAlias(argc, argv)
707 top = GetFSInfo(ALIAS, argv[1]);
708 Put_message(" "); /* blank line. */
709 Loop(top, (void *) PrintFSAlias);
714 /* Function Name: CreateFSAlias
715 * Description: Create an alias name for a filesystem
716 * Arguments: argc, argv - name of alias in argv[1].
717 * Returns: DM_NORMAL.
718 * NOTES: This requires (name, type, transl) I get {name, translation}
719 * from the user. I provide type, which is well-known.
724 CreateFSAlias(argc, argv)
729 struct qelem *elem, *top;
730 char *args[MAX_ARGS_SIZE], buf[BUFSIZ], **info;
734 if (!ValidName(argv[1]))
737 args[ALIAS_NAME] = Strsave(argv[1]);
738 args[ALIAS_TYPE] = Strsave(FS_ALIAS_TYPE);
739 args[ALIAS_TRANS] = Strsave("*");
742 * Check to see if this alias already exists in the database, if so then
743 * print out values, free memory used and then exit.
746 if ( (stat = do_sms_query("get_alias", 3, args, StoreInfo,
747 (char *)&elem)) == 0) {
748 top = elem = QueueTop(elem);
749 while (elem != NULL) {
750 info = (char **) elem->q_data;
751 sprintf(buf,"The alias: %s currently describes the filesystem %s",
752 info[ALIAS_NAME], info[ALIAS_TRANS]);
759 else if ( stat != SMS_NO_MATCH) {
760 com_err(program_name, stat, " in CreateFSAlias.");
764 args[ALIAS_TRANS]= args[ALIAS_END] = NULL; /* set to NULL initially. */
765 GetValueFromUser("Which filesystem will this alias point to?",
768 if ( (stat = do_sms_query("add_alias", 3, args, NullFunc, NULL)) != 0)
769 com_err(program_name, stat, " in CreateFSAlias.");
775 /* Function Name: RealDeleteFSAlias
776 * Description: Does the real deletion work.
777 * Arguments: info - array of char *'s containing all useful info.
778 * one_item - a Boolean that is true if only one item
779 * in queue that dumped us here.
784 RealDeleteFSAlias(info, one_item)
789 char temp_buf[BUFSIZ];
792 * Deletetions are performed if the user hits 'y' on a list of multiple
793 * filesystem, or if the user confirms on a unique alias.
796 "Are you sure that you want to delete the filesystem alias %s",
798 if(!one_item || Confirm(temp_buf)) {
799 if ( (stat = do_sms_query("delete_alias", CountArgs(info),
800 info, Scream, NULL)) != 0 )
801 com_err(program_name, stat, " filesystem alias not deleted.");
803 Put_message("Filesystem alias deleted.");
806 Put_message("Filesystem alias not deleted.");
809 /* Function Name: DeleteFSAlias
810 * Description: Delete an alias name for a filesystem
811 * Arguments: argc, argv - name of alias in argv[1].
812 * Returns: DM_NORMAL.
813 * NOTES: This requires (name, type, transl) I get {name, translation}
814 * from the user. I provide type, which is well-known.
819 DeleteFSAlias(argc, argv)
823 struct qelem *elem = GetFSInfo(ALIAS, argv[1]);
824 QueryLoop(elem, PrintFSAlias, RealDeleteFSAlias,
825 "Delete the Filesystem Alias");
830 /* Function Name: AttachHelp
831 * Description: Print help info on attachmaint.
833 * Returns: DM_NORMAL.
839 static char *message[] = {
840 "These are the options:",
842 "get - get information about a filesystem.",
843 "add - add a new filesystem to the data base.",
844 "update - update the information in the database on a filesystem.",
845 "delete - delete a filesystem from the database.",
846 "check - check information about association of a name and a filesys.",
847 "alias - associate a name with a filesystem.",
848 "unalias - disassociate a name with a filesystem.",
849 "verbose - toggle the request for delete confirmation.",
853 return(PrintHelp(message));
856 /* Function Name: FSGroupHelp
857 * Description: Print help info on fsgroups.
859 * Returns: DM_NORMAL.
865 static char *message[] = {
866 "A filesystem group is a named sorted list of filesystems.",
868 "To create, modify, or delete a group itself, use the menu above",
869 " this one, and manipulate a filesystem of type FSGROUP.",
871 " get - get info about a group and show its members",
872 " add - add a new member to a group.",
873 " remove - remove a member from a group.",
874 " order - change the sorting order of a group.",
878 return(PrintHelp(message));