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 (!strcasecmp(info[FS_TYPE], "FSGROUP"))
260 if (fsgroup || !strcasecmp(info[FS_TYPE], "AFS")) {
261 free(info[FS_MACHINE]);
262 info[FS_MACHINE] = Strsave(NO_MACHINE);
264 GetValueFromUser("Filesystem's Machine", &info[FS_MACHINE]);
265 info[FS_MACHINE] = canonicalize_hostname(info[FS_MACHINE]);
268 GetValueFromUser("Filesystem's Pack Name", &info[FS_PACK]);
269 GetValueFromUser("Filesystem's Mount Point", &info[FS_M_POINT]);
270 GetValueFromUser("Filesystem's Default Access", &info[FS_ACCESS]);
272 GetValueFromUser("Comments about this Filesystem", &info[FS_COMMENTS]);
273 GetValueFromUser("Filesystem's owner (user)", &info[FS_OWNER]);
274 GetValueFromUser("Filesystem's owners (group)", &info[FS_OWNERS]);
276 GetYesNoValueFromUser("Automatically create this filesystem",
278 GetTypeFromUser("Filesystem's lockertype", "lockertype", &info[FS_L_TYPE]);
280 FreeAndClear(&info[FS_MODTIME], TRUE);
281 FreeAndClear(&info[FS_MODBY], TRUE);
282 FreeAndClear(&info[FS_MODWITH], TRUE);
284 if (name) /* slide the newname into the #2 slot. */
285 SlipInNewName(info, newname);
290 /* --------------- Filesystem Menu ------------- */
292 /* Function Name: GetFS
293 * Description: Get Filesystem information by name.
294 * Arguments: argc, argv - name of filsys in argv[1].
295 * Returns: DM_NORMAL.
306 top = GetFSInfo(LABEL, argv[1]); /* get info. */
307 Loop(top, (void *) PrintFSInfo);
308 FreeQueue(top); /* clean the queue. */
312 /* Function Name: RealDeleteFS
313 * Description: Does the real deletion work.
314 * Arguments: info - array of char *'s containing all useful info.
315 * one_item - a Boolean that is true if only one item
316 * in queue that dumped us here.
321 RealDeleteFS(info, one_item)
326 char temp_buf[BUFSIZ];
329 * Deletetions are performed if the user hits 'y' on a list of multiple
330 * filesystem, or if the user confirms on a unique alias.
332 sprintf(temp_buf, "Are you sure that you want to delete filesystem %s",
334 if(!one_item || Confirm(temp_buf)) {
335 if ( (stat = do_sms_query("delete_filesys", 1,
336 &info[FS_NAME], Scream, NULL)) != 0)
337 com_err(program_name, stat, " filesystem not deleted.");
339 Put_message("Filesystem deleted.");
342 Put_message("Filesystem not deleted.");
345 /* Function Name: DeleteFS
346 * Description: Delete a filesystem give its name.
347 * Arguments: argc, argv - argv[1] is the name of the filesystem.
358 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
359 QueryLoop(elem, PrintFSInfo, RealDeleteFS, "Delete the Filesystem");
365 /* Function Name: RealChangeFS
366 * Description: performs the actual change to the filesys.
367 * Arguments: info - the information
368 * junk - an unused boolean.
374 RealChangeFS(info, junk)
379 char ** args = AskFSInfo(info, TRUE);
380 extern Menu nfsphys_menu;
382 stat = do_sms_query("update_filesys", CountArgs(args), args,
386 Put_message("That NFS filesystem is not exported.");
387 if (YesNoQuestion("Fix this now (Y/N)")) {
388 Do_menu(&nfsphys_menu, 0, NULL);
389 if (YesNoQuestion("Retry filesystem update now (Y/N)")) {
390 if (stat = do_sms_query("update_filesys", CountArgs(args), args,
392 com_err(program_name, stat, " filesystem not updated");
394 Put_message("filesystem sucessfully updated.");
401 com_err(program_name, stat, " in UpdateFS");
405 /* Function Name: ChangeFS
406 * Description: change the information in a filesys record.
407 * Arguments: arc, argv - value of filsys in argv[1].
408 * Returns: DM_NORMAL.
417 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
418 QueryLoop(elem, NullPrint, RealChangeFS, "Update the Filesystem");
424 /* Function Name: AddFS
425 * Description: change the information in a filesys record.
426 * Arguments: arc, argv - name of filsys in argv[1].
427 * Returns: DM_NORMAL.
436 char *info[MAX_ARGS_SIZE], **args;
438 extern Menu nfsphys_menu;
440 if ( !ValidName(argv[1]) )
443 if ( (stat = do_sms_query("get_filesys_by_label", 1, argv + 1,
444 NullFunc, NULL)) == 0) {
445 Put_message ("A Filesystem by that name already exists.");
447 } else if (stat != SMS_NO_MATCH) {
448 com_err(program_name, stat, " in AddFS");
452 args = AskFSInfo(SetDefaults(info, argv[1]), FALSE );
454 stat = do_sms_query("add_filesys", CountArgs(args), args, NullFunc, NULL);
457 Put_message("That NFS filesystem is not exported.");
458 if (YesNoQuestion("Fix this now (Y/N)")) {
459 Do_menu(&nfsphys_menu, 0, NULL);
460 if (YesNoQuestion("Retry filesystem creation now (Y/N)")) {
461 if (stat = do_sms_query("add_filesys", CountArgs(args), args,
463 com_err(program_name, stat, " in AddFS");
465 Put_message("Created.");
472 com_err(program_name, stat, " in AddFS");
479 /* Function Name: SortAfter
480 * Description: choose a sortkey to cause an item to be added after
481 * the count element in the queue
482 * Arguments: queue of filesys names & sortkeys, queue count pointer
483 * Returns: sort key to use.
488 SortAfter(elem, count)
492 char *prev, *next, prevnext, *key, keybuf[9];
494 /* first find the two keys we need to insert between */
496 for (; count > 0; count--) {
497 prev = ((char **)elem->q_data)[1];
506 next = ((char **)elem->q_data)[1];
508 /* now copy the matching characters */
509 for (key = keybuf; *prev && *prev == *next; next++) {
513 /* and set the last character */
515 *prev = prevnext = 'A';
522 if (*next - *prev > 1) {
523 *key++ = (*next + *prev)/2;
526 *key++ = (prevnext + 'Z')/2;
529 return(Strsave(keybuf));
532 /* Function Name: AddFSToGroup
533 * Description: add a filesystem to an FS group
534 * Arguments: arc, argv - name of group in argv[1], filesys in argv[2].
535 * Returns: DM_NORMAL.
540 AddFSToGroup(argc, argv)
545 struct qelem *elem = NULL;
546 char buf[BUFSIZ], *args[5], *bufp;
548 if ((stat = do_sms_query("get_fsgroup_members", 1, argv+1, StoreInfo,
549 (char *)&elem)) != 0) {
550 if (stat != SMS_NO_MATCH)
551 com_err(program_name, stat, " in AddFSToGroup");
557 stat = do_sms_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
559 com_err(program_name, stat, " in AddFSToGroup");
562 elem = QueueTop(elem);
564 Loop(elem, (void *) PrintFSGMembers);
565 sprintf(buf, "%d", QueueCount(elem));
567 stat = GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp);
570 args[2] = SortAfter(elem, count);
572 FreeQueue(QueueTop(elem));
575 stat = do_sms_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
576 if (stat == SMS_EXISTS) {
577 Put_message("That filesystem is already a member of the group.");
578 Put_message("Use the order command if you want to change the sorting order.");
580 com_err(program_name, stat, " in AddFSToGroup");
585 /* Function Name: RemoveFSFromGroup
586 * Description: delete a filesystem from an FS group
587 * Arguments: arc, argv - name of group in argv[1].
588 * Returns: DM_NORMAL.
593 RemoveFSFromGroup(argc, argv)
600 sprintf(buf, "Delete filesystem %s from FS group %s", argv[2], argv[1]);
603 if ((stat = do_sms_query("remove_filesys_from_fsgroup", 2, argv+1,
604 Scream, NULL)) != 0) {
605 com_err(program_name, stat, ", not removed.");
610 /* Function Name: ChangeFSGroupOrder
611 * Description: change the sortkey on a filesys in an FSgroup
612 * Arguments: arc, argv - name of group in argv[1].
613 * Returns: DM_NORMAL.
618 ChangeFSGroupOrder(argc, argv)
623 struct qelem *elem = NULL, *top;
624 char buf[BUFSIZ], *bufp, *args[3];
626 if ((stat = do_sms_query("get_fsgroup_members", 1, argv+1, StoreInfo,
627 (char *)&elem)) != 0) {
628 if (stat == SMS_NO_MATCH) {
629 sprintf(buf, "Ether %s is not a filesystem group or it has no members", argv[1]);
632 com_err(program_name, stat, " in ChangeFSGroupOrder");
635 top = QueueTop(elem);
637 Loop(top, (void *) PrintFSGMembers);
640 stat = GetValueFromUser("Enter number of the filesystem to move:",
645 Put_message("You must enter a positive number (or 0 to abort).");
647 } else if (src == 0) {
648 Put_message("Aborted.");
651 for (elem = top; src-- > 1 && elem->q_forw; elem = elem->q_forw);
653 Put_message("You entered a number that is too high");
659 stat = GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp);
662 if (src == dst || src == dst + 1) {
663 Put_message("That has no effect on the sorting order!");
666 args[2] = SortAfter(top, dst);
668 args[1] = ((char **)elem->q_data)[0];
669 if ((stat = do_sms_query("remove_filesys_from_fsgroup", 2, args,
670 Scream, NULL)) != 0) {
671 com_err(program_name, stat, " in ChangeFSGroupOrder");
674 if ((stat = do_sms_query("add_filesys_to_fsgroup", 3, args,
675 Scream, NULL)) != 0) {
676 com_err(program_name, stat, " in ChangeFSGroupOrder");
682 /* -------------- Top Level Menu ---------------- */
684 /* Function Name: GetFSAlias
685 * Description: Gets the value for a Filesystem Alias.
686 * Arguments: argc, argv - name of alias in argv[1].
687 * Returns: DM_NORMAL.
688 * NOTES: There should only be one filesystem per alias, thus
689 * this will work correctly.
694 GetFSAlias(argc, argv)
700 top = GetFSInfo(ALIAS, argv[1]);
701 Put_message(" "); /* blank line. */
702 Loop(top, (void *) PrintFSAlias);
707 /* Function Name: CreateFSAlias
708 * Description: Create an alias name for a filesystem
709 * Arguments: argc, argv - name of alias in argv[1].
710 * Returns: DM_NORMAL.
711 * NOTES: This requires (name, type, transl) I get {name, translation}
712 * from the user. I provide type, which is well-known.
717 CreateFSAlias(argc, argv)
722 struct qelem *elem, *top;
723 char *args[MAX_ARGS_SIZE], buf[BUFSIZ], **info;
727 if (!ValidName(argv[1]))
730 args[ALIAS_NAME] = Strsave(argv[1]);
731 args[ALIAS_TYPE] = Strsave(FS_ALIAS_TYPE);
732 args[ALIAS_TRANS] = Strsave("*");
735 * Check to see if this alias already exists in the database, if so then
736 * print out values, free memory used and then exit.
739 if ( (stat = do_sms_query("get_alias", 3, args, StoreInfo,
740 (char *)&elem)) == 0) {
741 top = elem = QueueTop(elem);
742 while (elem != NULL) {
743 info = (char **) elem->q_data;
744 sprintf(buf,"The alias: %s currently describes the filesystem %s",
745 info[ALIAS_NAME], info[ALIAS_TRANS]);
752 else if ( stat != SMS_NO_MATCH) {
753 com_err(program_name, stat, " in CreateFSAlias.");
757 args[ALIAS_TRANS]= args[ALIAS_END] = NULL; /* set to NULL initially. */
758 GetValueFromUser("Which filesystem will this alias point to?",
761 if ( (stat = do_sms_query("add_alias", 3, args, NullFunc, NULL)) != 0)
762 com_err(program_name, stat, " in CreateFSAlias.");
768 /* Function Name: RealDeleteFSAlias
769 * Description: Does the real deletion work.
770 * Arguments: info - array of char *'s containing all useful info.
771 * one_item - a Boolean that is true if only one item
772 * in queue that dumped us here.
777 RealDeleteFSAlias(info, one_item)
782 char temp_buf[BUFSIZ];
785 * Deletetions are performed if the user hits 'y' on a list of multiple
786 * filesystem, or if the user confirms on a unique alias.
789 "Are you sure that you want to delete the filesystem alias %s",
791 if(!one_item || Confirm(temp_buf)) {
792 if ( (stat = do_sms_query("delete_alias", CountArgs(info),
793 info, Scream, NULL)) != 0 )
794 com_err(program_name, stat, " filesystem alias not deleted.");
796 Put_message("Filesystem alias deleted.");
799 Put_message("Filesystem alias not deleted.");
802 /* Function Name: DeleteFSAlias
803 * Description: Delete an alias name for a filesystem
804 * Arguments: argc, argv - name of alias in argv[1].
805 * Returns: DM_NORMAL.
806 * NOTES: This requires (name, type, transl) I get {name, translation}
807 * from the user. I provide type, which is well-known.
812 DeleteFSAlias(argc, argv)
816 struct qelem *elem = GetFSInfo(ALIAS, argv[1]);
817 QueryLoop(elem, PrintFSAlias, RealDeleteFSAlias,
818 "Delete the Filesystem Alias");
823 /* Function Name: AttachHelp
824 * Description: Print help info on attachmaint.
826 * Returns: DM_NORMAL.
832 static char *message[] = {
833 "These are the options:",
835 "get - get information about a filesystem.",
836 "add - add a new filesystem to the data base.",
837 "update - update the information in the database on a filesystem.",
838 "delete - delete a filesystem from the database.",
839 "check - check information about association of a name and a filesys.",
840 "alias - associate a name with a filesystem.",
841 "unalias - disassociate a name with a filesystem.",
842 "verbose - toggle the request for delete confirmation.",
846 return(PrintHelp(message));
849 /* Function Name: FSGroupHelp
850 * Description: Print help info on fsgroups.
852 * Returns: DM_NORMAL.
858 static char *message[] = {
859 "A filesystem group is a named sorted list of filesystems.",
861 "To create, modify, or delete a group itself, use the menu above",
862 " this one, and manipulate a filesystem of type FSGROUP.",
864 " get - get info about a group and show its members",
865 " add - add a new member to a group.",
866 " remove - remove a member from a group.",
867 " order - change the sorting order of a group.",
871 return(PrintHelp(message));