1 #if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
5 /* This is the file attach.c for the MOIRA Client, which allows a nieve
6 * user to quickly and easily maintain most parts of the MOIRA 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
27 #include <moira_site.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_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)
70 info[FS_NAME] = Strsave(name);
71 info[FS_TYPE] = Strsave(DEFAULT_TYPE);
72 info[FS_MACHINE] = Strsave(DEFAULT_MACHINE);
73 info[FS_PACK] = Strsave(DEFAULT_PACK);
74 sprintf(buf, "/mit/%s", name);
75 info[FS_M_POINT] = Strsave(buf);
76 info[FS_ACCESS] = Strsave(DEFAULT_ACCESS);
77 info[FS_COMMENTS] = Strsave(DEFAULT_COMMENTS);
78 info[FS_OWNER] = Strsave(DEFAULT_OWNER);
79 info[FS_OWNERS] = Strsave(DEFAULT_OWNERS);
80 info[FS_CREATE] = Strsave(DEFAULT_CREATE);
81 info[FS_L_TYPE] = Strsave(DEFAULT_L_TYPE);
82 info[FS_MODTIME] = info[FS_MODBY] = info[FS_MODWITH] = info[FS_END] = NULL;
86 /* Function Name: GetFSInfo
87 * Description: Stores the info in a queue.
88 * Arguments: type - type of information to get.
89 * name - name of the item to get information on.
90 * Returns: a pointer to the first element in the queue.
99 struct qelem * elem = NULL;
104 if ( (stat = do_mr_query("get_filesys_by_label", 1, &name,
105 StoreInfo, (char *)&elem)) != 0) {
106 com_err(program_name, stat, " in GetFSInfo");
111 if ( (stat = do_mr_query("get_filesys_by_machine", 1, &name,
112 StoreInfo, (char *)&elem)) != 0) {
113 com_err(program_name, stat, " in GetFSInfo");
118 if ( (stat = do_mr_query("get_filesys_by_group", 1, &name,
119 StoreInfo, (char *)&elem)) != 0) {
120 com_err(program_name, stat, " in GetFSInfo");
125 args[ALIAS_NAME] = name;
126 args[ALIAS_TYPE] = FS_ALIAS_TYPE;
127 args[ALIAS_TRANS] = "*";
128 if ( (stat = do_mr_query("get_alias", 3, args, StoreInfo,
129 (char *) &elem)) != 0) {
130 com_err(program_name, stat, " in get_alias.");
135 return(QueueTop(elem));
138 /* Function Name: PrintFSAlias
139 * Description: Prints a filesystem alias
140 * Arguments: info - an array contains the strings of info.
141 * Returns: the name of the filesys - used be QueryLoop().
150 sprintf(buf,"Alias: %-25s Filesystem: %s",info[ALIAS_NAME],
153 return(info[ALIAS_NAME]);
156 static int fsgCount = 1;
159 PrintFSGMembers(info)
162 char print_buf[BUFSIZ];
164 sprintf(print_buf, " %d. Filesystem: %-32s (sort key: %s)", fsgCount++, info[0], info[1]);
165 Put_message(print_buf);
170 /* Function Name: PrintFSInfo
171 * Description: Prints the filesystem information.
172 * Arguments: info - a pointer to the filesystem information.
180 char print_buf[BUFSIZ];
184 if (!strcmp(info[FS_TYPE], "FSGROUP") || !strcmp(info[FS_TYPE], "MUL")) {
186 struct qelem *elem = NULL;
188 if (!strcmp(info[FS_TYPE], "MUL"))
189 sprintf(print_buf,"%20s Multiple Filesystem: %s", " ", info[FS_NAME]);
191 sprintf(print_buf,"%20s Filesystem Group: %s", " ", info[FS_NAME]);
192 Put_message(print_buf);
194 sprintf(print_buf,"Comments: %s",info[FS_COMMENTS]);
195 Put_message(print_buf);
196 sprintf(print_buf, MOD_FORMAT, info[FS_MODBY], info[FS_MODTIME],
198 Put_message(print_buf);
199 Put_message("Containing the filesystems (in order):");
200 if ((stat = do_mr_query("get_fsgroup_members", 1, &info[FS_NAME],
201 StoreInfo, (char *)&elem)) != 0) {
202 if (stat == MR_NO_MATCH)
203 Put_message(" [no members]");
205 com_err(program_name, stat, " in PrintFSInfo");
208 Loop(QueueTop(elem), (void *) PrintFSGMembers);
212 sprintf(print_buf,"%20s Filesystem: %s", " ", info[FS_NAME]);
213 Put_message(print_buf);
214 sprintf(print_buf,"Type: %-40s Machine: %-15s",
215 info[FS_TYPE], info[FS_MACHINE]);
216 Put_message(print_buf);
217 sprintf(print_buf,"Default Access: %-2s Packname: %-17s Mountpoint %s ",
218 info[FS_ACCESS], info[FS_PACK], info[FS_M_POINT]);
219 Put_message(print_buf);
220 sprintf(print_buf,"Comments: %s",info[FS_COMMENTS]);
221 Put_message(print_buf);
222 sprintf(print_buf, "User Ownership: %-30s Group Ownership: %s",
223 info[FS_OWNER], info[FS_OWNERS]);
224 Put_message(print_buf);
225 sprintf(print_buf, "Auto Create: %-34s Locker Type: %s",
226 atoi(info[FS_CREATE]) ? "ON" : "OFF",
228 Put_message(print_buf);
229 sprintf(print_buf, MOD_FORMAT, info[FS_MODBY], info[FS_MODTIME],
231 Put_message(print_buf);
233 return(info[FS_NAME]);
236 /* Function Name: AskFSInfo.
237 * Description: This function askes the user for information about a
238 * machine and saves it into a structure.
239 * Arguments: info - a pointer the the structure to put the
241 * name - add a newname field? (T/F)
246 AskFSInfo(info, name)
250 char temp_buf[BUFSIZ], *newname, access_type[32];
254 sprintf(temp_buf, "Changing Attributes of filesystem %s.",
256 Put_message(temp_buf);
260 newname = Strsave(info[FS_NAME]);
261 if (GetValueFromUser("The new name for this filesystem",
262 &newname) == SUB_ERROR)
266 if (GetTypeFromUser("Filesystem's Type", "filesys", &info[FS_TYPE]) ==
269 if (!strcasecmp(info[FS_TYPE], "FSGROUP") ||
270 !strcasecmp(info[FS_TYPE], "MUL"))
272 if (fsgroup || !strcasecmp(info[FS_TYPE], "AFS")) {
273 free(info[FS_MACHINE]);
274 info[FS_MACHINE] = Strsave(NO_MACHINE);
276 if (!strcmp(info[FS_MACHINE], NO_MACHINE_BAD)) {
277 free(info[FS_MACHINE]);
278 info[FS_MACHINE] = Strsave(NO_MACHINE);
280 if (GetValueFromUser("Filesystem's Machine", &info[FS_MACHINE]) ==
283 info[FS_MACHINE] = canonicalize_hostname(info[FS_MACHINE]);
286 if (GetValueFromUser("Filesystem's Pack Name", &info[FS_PACK]) ==
289 if (GetValueFromUser("Filesystem's Mount Point", &info[FS_M_POINT]) ==
292 sprintf(access_type, "fs_access_%s", info[FS_TYPE]);
293 if (GetTypeFromUser("Filesystem's Default Access", access_type,
294 &info[FS_ACCESS]) == SUB_ERROR)
297 if (GetValueFromUser("Comments about this Filesystem", &info[FS_COMMENTS])
300 if (GetValueFromUser("Filesystem's owner (user)", &info[FS_OWNER]) ==
303 if (GetValueFromUser("Filesystem's owners (group)", &info[FS_OWNERS]) ==
307 if (GetYesNoValueFromUser("Automatically create this filesystem",
308 &info[FS_CREATE]) == SUB_ERROR)
310 if (GetTypeFromUser("Filesystem's lockertype", "lockertype",
311 &info[FS_L_TYPE]) == SUB_ERROR)
314 FreeAndClear(&info[FS_MODTIME], TRUE);
315 FreeAndClear(&info[FS_MODBY], TRUE);
316 FreeAndClear(&info[FS_MODWITH], TRUE);
318 if (name) /* slide the newname into the #2 slot. */
319 SlipInNewName(info, newname);
324 /* --------------- Filesystem Menu ------------- */
326 /* Function Name: GetFS
327 * Description: Get Filesystem information by name.
328 * Arguments: argc, argv - name of filsys in argv[1].
329 * Returns: DM_NORMAL.
340 top = GetFSInfo(LABEL, argv[1]); /* get info. */
341 Loop(top, (void *) PrintFSInfo);
342 FreeQueue(top); /* clean the queue. */
346 /* Function Name: RealDeleteFS
347 * Description: Does the real deletion work.
348 * Arguments: info - array of char *'s containing all useful info.
349 * one_item - a Boolean that is true if only one item
350 * in queue that dumped us here.
355 RealDeleteFS(info, one_item)
360 char temp_buf[BUFSIZ];
363 * Deletetions are performed if the user hits 'y' on a list of multiple
364 * filesystem, or if the user confirms on a unique alias.
366 sprintf(temp_buf, "Are you sure that you want to delete filesystem %s",
368 if(!one_item || Confirm(temp_buf)) {
369 if ( (stat = do_mr_query("delete_filesys", 1,
370 &info[FS_NAME], Scream, NULL)) != 0)
371 com_err(program_name, stat, " filesystem not deleted.");
373 Put_message("Filesystem deleted.");
376 Put_message("Filesystem not deleted.");
379 /* Function Name: DeleteFS
380 * Description: Delete a filesystem give its name.
381 * Arguments: argc, argv - argv[1] is the name of the filesystem.
392 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
393 QueryLoop(elem, PrintFSInfo, RealDeleteFS, "Delete the Filesystem");
399 /* Function Name: RealChangeFS
400 * Description: performs the actual change to the filesys.
401 * Arguments: info - the information
402 * junk - an unused boolean.
408 RealChangeFS(info, junk)
414 extern Menu nfsphys_menu;
416 args = AskFSInfo(info, TRUE);
418 Put_message("Aborted.");
421 stat = do_mr_query("update_filesys", CountArgs(args), args,
425 Put_message("That NFS filesystem is not exported.");
426 if (YesNoQuestion("Fix this now (Y/N)", TRUE) == TRUE) {
427 Do_menu(&nfsphys_menu, 0, NULL);
428 if (YesNoQuestion("Retry filesystem update now (Y/N)", TRUE)
430 if (stat = do_mr_query("update_filesys", CountArgs(args), args,
432 com_err(program_name, stat, " filesystem not updated");
434 Put_message("filesystem sucessfully updated.");
441 com_err(program_name, stat, " in UpdateFS");
445 /* Function Name: ChangeFS
446 * Description: change the information in a filesys record.
447 * Arguments: arc, argv - value of filsys in argv[1].
448 * Returns: DM_NORMAL.
457 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
458 QueryLoop(elem, NullPrint, RealChangeFS, "Update the Filesystem");
464 /* Function Name: AddFS
465 * Description: change the information in a filesys record.
466 * Arguments: arc, argv - name of filsys in argv[1].
467 * Returns: DM_NORMAL.
476 char *info[MAX_ARGS_SIZE], **args, buf[BUFSIZ];
478 extern Menu nfsphys_menu;
480 if ( !ValidName(argv[1]) )
483 if ( (stat = do_mr_query("get_filesys_by_label", 1, argv + 1,
484 NullFunc, NULL)) == 0) {
485 Put_message ("A Filesystem by that name already exists.");
487 } else if (stat != MR_NO_MATCH) {
488 com_err(program_name, stat, " in AddFS");
492 if ((args = AskFSInfo(SetDefaults(info, argv[1]), FALSE )) == NULL) {
493 Put_message("Aborted.");
497 stat = do_mr_query("add_filesys", CountArgs(args), args, NullFunc, NULL);
500 Put_message("That NFS filesystem is not exported.");
501 if (YesNoQuestion("Fix this now (Y/N)", TRUE) == TRUE) {
502 Do_menu(&nfsphys_menu, 0, NULL);
503 if (YesNoQuestion("Retry filesystem creation now (Y/N)", TRUE)
505 if (stat = do_mr_query("add_filesys", CountArgs(args), args,
507 com_err(program_name, stat, " in AddFS");
509 Put_message("Created.");
516 com_err(program_name, stat, " in AddFS");
519 if (stat == MR_SUCCESS && !strcasecmp(info[FS_L_TYPE], "HOMEDIR")) {
520 static char *val[] = {"def_quota", NULL};
521 static char *def_quota = NULL;
522 char *argv[Q_QUOTA + 1];
523 struct qelem *top = NULL;
525 if (def_quota == NULL) {
526 stat = do_mr_query("get_value", CountArgs(val), val,
527 StoreInfo, (char *) &top);
528 if (stat != MR_SUCCESS) {
529 com_err(program_name, stat, " getting default quota");
532 def_quota = Strsave(((char **)top->q_data)[0]);
536 if (def_quota != NULL) {
537 sprintf(buf, "Give user %s a quota of %s on filesys %s (Y/N)",
538 info[FS_NAME], def_quota, info[FS_NAME]);
539 if (YesNoQuestion(buf, 1) == TRUE) {
540 argv[Q_NAME] = argv[Q_FILESYS] = info[FS_NAME];
541 argv[Q_TYPE] = "USER";
542 argv[Q_QUOTA] = def_quota;
543 if ((stat = do_mr_query("add_quota", Q_QUOTA+1, argv, Scream,
544 (char *) NULL)) != MR_SUCCESS) {
545 com_err(program_name, stat, " while adding quota");
555 /* Function Name: SortAfter
556 * Description: choose a sortkey to cause an item to be added after
557 * the count element in the queue
558 * Arguments: queue of filesys names & sortkeys, queue count pointer
559 * Returns: sort key to use.
564 SortAfter(elem, count)
568 char *prev, *next, prevnext, *key, keybuf[9];
570 /* first find the two keys we need to insert between */
572 for (; count > 0; count--) {
573 prev = ((char **)elem->q_data)[1];
582 next = ((char **)elem->q_data)[1];
584 /* now copy the matching characters */
585 for (key = keybuf; *prev && *prev == *next; next++) {
589 /* and set the last character */
591 *prev = prevnext = 'A';
598 if (*next - *prev > 1) {
599 *key++ = (*next + *prev)/2;
602 *key++ = (prevnext + 'Z')/2;
605 return(Strsave(keybuf));
608 /* Function Name: AddFSToGroup
609 * Description: add a filesystem to an FS group
610 * Arguments: arc, argv - name of group in argv[1], filesys in argv[2].
611 * Returns: DM_NORMAL.
616 AddFSToGroup(argc, argv)
621 struct qelem *elem = NULL;
622 char buf[BUFSIZ], *args[5], *bufp;
624 if ((stat = do_mr_query("get_fsgroup_members", 1, argv+1, StoreInfo,
625 (char *)&elem)) != 0) {
626 if (stat != MR_NO_MATCH)
627 com_err(program_name, stat, " in AddFSToGroup");
633 stat = do_mr_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
635 com_err(program_name, stat, " in AddFSToGroup");
638 elem = QueueTop(elem);
640 Loop(elem, (void *) PrintFSGMembers);
641 sprintf(buf, "%d", QueueCount(elem));
643 if (GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp) == SUB_ERROR)
647 args[2] = SortAfter(elem, count);
649 FreeQueue(QueueTop(elem));
652 stat = do_mr_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
653 if (stat == MR_EXISTS) {
654 Put_message("That filesystem is already a member of the group.");
655 Put_message("Use the order command if you want to change the sorting order.");
657 com_err(program_name, stat, " in AddFSToGroup");
662 /* Function Name: RemoveFSFromGroup
663 * Description: delete a filesystem from an FS group
664 * Arguments: arc, argv - name of group in argv[1].
665 * Returns: DM_NORMAL.
670 RemoveFSFromGroup(argc, argv)
677 sprintf(buf, "Delete filesystem %s from FS group %s", argv[2], argv[1]);
680 if ((stat = do_mr_query("remove_filesys_from_fsgroup", 2, argv+1,
681 Scream, NULL)) != 0) {
682 com_err(program_name, stat, ", not removed.");
687 /* Function Name: ChangeFSGroupOrder
688 * Description: change the sortkey on a filesys in an FSgroup
689 * Arguments: arc, argv - name of group in argv[1].
690 * Returns: DM_NORMAL.
695 ChangeFSGroupOrder(argc, argv)
699 int stat, src, dst, i;
700 struct qelem *elem = NULL, *top, *tmpelem;
701 char buf[BUFSIZ], *bufp, *args[3];
703 if ((stat = do_mr_query("get_fsgroup_members", 1, argv+1, StoreInfo,
704 (char *)&elem)) != 0) {
705 if (stat == MR_NO_MATCH) {
707 "Ether %s is not a filesystem group or it has no members",
711 com_err(program_name, stat, " in ChangeFSGroupOrder");
714 top = QueueTop(elem);
716 Loop(top, (void *) PrintFSGMembers);
719 if (GetValueFromUser("Enter number of the filesystem to move:",
725 Put_message("You must enter a positive number (or 0 to abort).");
727 } else if (src == 0) {
728 Put_message("Aborted.");
731 for (elem = top, i = src; i-- > 1 && elem->q_forw; elem = elem->q_forw);
733 Put_message("You entered a number that is too high");
740 if (GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp) == SUB_ERROR)
744 if (src == dst || src == dst + 1) {
745 Put_message("That has no effect on the sorting order!");
749 Put_message("You must enter a non-negative number.");
752 for (tmpelem = top, i = dst;
753 i-- > 1 && tmpelem->q_forw;
754 tmpelem = tmpelem->q_forw);
756 Put_message("You entered a number that is too high");
761 args[2] = SortAfter(top, dst);
763 args[1] = ((char **)elem->q_data)[0];
764 if ((stat = do_mr_query("remove_filesys_from_fsgroup", 2, args,
765 Scream, NULL)) != 0) {
766 com_err(program_name, stat, " in ChangeFSGroupOrder");
769 if ((stat = do_mr_query("add_filesys_to_fsgroup", 3, args,
770 Scream, NULL)) != 0) {
771 com_err(program_name, stat, " in ChangeFSGroupOrder");
777 /* -------------- Top Level Menu ---------------- */
779 /* Function Name: GetFSAlias
780 * Description: Gets the value for a Filesystem Alias.
781 * Arguments: argc, argv - name of alias in argv[1].
782 * Returns: DM_NORMAL.
783 * NOTES: There should only be one filesystem per alias, thus
784 * this will work correctly.
789 GetFSAlias(argc, argv)
795 top = GetFSInfo(ALIAS, argv[1]);
796 Put_message(" "); /* blank line. */
797 Loop(top, (void *) PrintFSAlias);
802 /* Function Name: CreateFSAlias
803 * Description: Create 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 CreateFSAlias(argc, argv)
817 struct qelem *elem, *top;
818 char *args[MAX_ARGS_SIZE], buf[BUFSIZ], **info;
822 if (!ValidName(argv[1]))
825 args[ALIAS_NAME] = Strsave(argv[1]);
826 args[ALIAS_TYPE] = Strsave(FS_ALIAS_TYPE);
827 args[ALIAS_TRANS] = Strsave("*");
830 * Check to see if this alias already exists in the database, if so then
831 * print out values, free memory used and then exit.
834 if ( (stat = do_mr_query("get_alias", 3, args, StoreInfo,
835 (char *)&elem)) == 0) {
836 top = elem = QueueTop(elem);
837 while (elem != NULL) {
838 info = (char **) elem->q_data;
839 sprintf(buf,"The alias: %s currently describes the filesystem %s",
840 info[ALIAS_NAME], info[ALIAS_TRANS]);
847 else if ( stat != MR_NO_MATCH) {
848 com_err(program_name, stat, " in CreateFSAlias.");
852 args[ALIAS_TRANS]= Strsave("");
853 args[ALIAS_END] = NULL;
854 if (GetValueFromUser("Which filesystem will this alias point to?",
855 &args[ALIAS_TRANS]) == SUB_ERROR)
858 if ( (stat = do_mr_query("add_alias", 3, args, NullFunc, NULL)) != 0)
859 com_err(program_name, stat, " in CreateFSAlias.");
865 /* Function Name: RealDeleteFSAlias
866 * Description: Does the real deletion work.
867 * Arguments: info - array of char *'s containing all useful info.
868 * one_item - a Boolean that is true if only one item
869 * in queue that dumped us here.
874 RealDeleteFSAlias(info, one_item)
879 char temp_buf[BUFSIZ];
882 * Deletetions are performed if the user hits 'y' on a list of multiple
883 * filesystem, or if the user confirms on a unique alias.
886 "Are you sure that you want to delete the filesystem alias %s",
888 if(!one_item || Confirm(temp_buf)) {
889 if ( (stat = do_mr_query("delete_alias", CountArgs(info),
890 info, Scream, NULL)) != 0 )
891 com_err(program_name, stat, " filesystem alias not deleted.");
893 Put_message("Filesystem alias deleted.");
896 Put_message("Filesystem alias not deleted.");
899 /* Function Name: DeleteFSAlias
900 * Description: Delete an alias name for a filesystem
901 * Arguments: argc, argv - name of alias in argv[1].
902 * Returns: DM_NORMAL.
903 * NOTES: This requires (name, type, transl) I get {name, translation}
904 * from the user. I provide type, which is well-known.
909 DeleteFSAlias(argc, argv)
913 struct qelem *elem = GetFSInfo(ALIAS, argv[1]);
914 QueryLoop(elem, PrintFSAlias, RealDeleteFSAlias,
915 "Delete the Filesystem Alias");
920 /* Function Name: AttachHelp
921 * Description: Print help info on attachmaint.
923 * Returns: DM_NORMAL.
929 static char *message[] = {
930 "These are the options:",
932 "get - get information about a filesystem.",
933 "add - add a new filesystem to the data base.",
934 "update - update the information in the database on a filesystem.",
935 "delete - delete a filesystem from the database.",
936 "check - check information about association of a name and a filesys.",
937 "alias - associate a name with a filesystem.",
938 "unalias - disassociate a name with a filesystem.",
939 "verbose - toggle the request for delete confirmation.",
943 return(PrintHelp(message));
946 /* Function Name: FSGroupHelp
947 * Description: Print help info on fsgroups.
949 * Returns: DM_NORMAL.
955 static char *message[] = {
956 "A filesystem group is a named sorted list of filesystems.",
958 "To create, modify, or delete a group itself, use the menu above",
959 " this one, and manipulate a filesystem of type FSGROUP.",
961 " get - get info about a group and show its members",
962 " add - add a new member to a group.",
963 " remove - remove a member from a group.",
964 " order - change the sorting order of a group.",
968 return(PrintHelp(message));