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")) {
186 struct qelem *elem = NULL;
188 sprintf(print_buf,"%20s Filesystem Group: %s", " ", info[FS_NAME]);
189 Put_message(print_buf);
191 sprintf(print_buf,"Comments: %s",info[FS_COMMENTS]);
192 Put_message(print_buf);
193 sprintf(print_buf, MOD_FORMAT, info[FS_MODBY], info[FS_MODTIME],
195 Put_message(print_buf);
196 Put_message("Containing the filesystems (in order):");
197 if ((stat = do_mr_query("get_fsgroup_members", 1, &info[FS_NAME],
198 StoreInfo, (char *)&elem)) != 0) {
199 if (stat == MR_NO_MATCH)
200 Put_message(" [no members]");
202 com_err(program_name, stat, " in PrintFSInfo");
205 Loop(QueueTop(elem), (void *) PrintFSGMembers);
209 sprintf(print_buf,"%20s Filesystem: %s", " ", info[FS_NAME]);
210 Put_message(print_buf);
211 sprintf(print_buf,"Type: %-40s Machine: %-15s",
212 info[FS_TYPE], info[FS_MACHINE]);
213 Put_message(print_buf);
214 sprintf(print_buf,"Default Access: %-2s Packname: %-17s Mountpoint %s ",
215 info[FS_ACCESS], info[FS_PACK], info[FS_M_POINT]);
216 Put_message(print_buf);
217 sprintf(print_buf,"Comments: %s",info[FS_COMMENTS]);
218 Put_message(print_buf);
219 sprintf(print_buf, "User Ownership: %-30s Group Ownership: %s",
220 info[FS_OWNER], info[FS_OWNERS]);
221 Put_message(print_buf);
222 sprintf(print_buf, "Auto Create: %-34s Locker Type: %s",
223 atoi(info[FS_CREATE]) ? "ON" : "OFF",
225 Put_message(print_buf);
226 sprintf(print_buf, MOD_FORMAT, info[FS_MODBY], info[FS_MODTIME],
228 Put_message(print_buf);
230 return(info[FS_NAME]);
233 /* Function Name: AskFSInfo.
234 * Description: This function askes the user for information about a
235 * machine and saves it into a structure.
236 * Arguments: info - a pointer the the structure to put the
238 * name - add a newname field? (T/F)
243 AskFSInfo(info, name)
247 char temp_buf[BUFSIZ], *newname, access_type[32];
251 sprintf(temp_buf, "Changing Attributes of filesystem %s.",
253 Put_message(temp_buf);
257 newname = Strsave(info[FS_NAME]);
258 if (GetValueFromUser("The new name for this filesystem",
259 &newname) == SUB_ERROR)
263 if (GetTypeFromUser("Filesystem's Type", "filesys", &info[FS_TYPE]) ==
266 if (!strcasecmp(info[FS_TYPE], "FSGROUP"))
268 if (fsgroup || !strcasecmp(info[FS_TYPE], "AFS")) {
269 free(info[FS_MACHINE]);
270 info[FS_MACHINE] = Strsave(NO_MACHINE);
272 if (!strcmp(info[FS_MACHINE], NO_MACHINE_BAD)) {
273 free(info[FS_MACHINE]);
274 info[FS_MACHINE] = Strsave(NO_MACHINE);
276 if (GetValueFromUser("Filesystem's Machine", &info[FS_MACHINE]) ==
279 info[FS_MACHINE] = canonicalize_hostname(info[FS_MACHINE]);
282 if (GetValueFromUser("Filesystem's Pack Name", &info[FS_PACK]) ==
285 if (GetValueFromUser("Filesystem's Mount Point", &info[FS_M_POINT]) ==
288 sprintf(access_type, "fs_access_%s", info[FS_TYPE]);
289 if (GetTypeFromUser("Filesystem's Default Access", access_type,
290 &info[FS_ACCESS]) == SUB_ERROR)
293 if (GetValueFromUser("Comments about this Filesystem", &info[FS_COMMENTS])
296 if (GetValueFromUser("Filesystem's owner (user)", &info[FS_OWNER]) ==
299 if (GetValueFromUser("Filesystem's owners (group)", &info[FS_OWNERS]) ==
303 if (GetYesNoValueFromUser("Automatically create this filesystem",
304 &info[FS_CREATE]) == SUB_ERROR)
306 if (GetTypeFromUser("Filesystem's lockertype", "lockertype",
307 &info[FS_L_TYPE]) == SUB_ERROR)
310 FreeAndClear(&info[FS_MODTIME], TRUE);
311 FreeAndClear(&info[FS_MODBY], TRUE);
312 FreeAndClear(&info[FS_MODWITH], TRUE);
314 if (name) /* slide the newname into the #2 slot. */
315 SlipInNewName(info, newname);
320 /* --------------- Filesystem Menu ------------- */
322 /* Function Name: GetFS
323 * Description: Get Filesystem information by name.
324 * Arguments: argc, argv - name of filsys in argv[1].
325 * Returns: DM_NORMAL.
336 top = GetFSInfo(LABEL, argv[1]); /* get info. */
337 Loop(top, (void *) PrintFSInfo);
338 FreeQueue(top); /* clean the queue. */
342 /* Function Name: RealDeleteFS
343 * Description: Does the real deletion work.
344 * Arguments: info - array of char *'s containing all useful info.
345 * one_item - a Boolean that is true if only one item
346 * in queue that dumped us here.
351 RealDeleteFS(info, one_item)
356 char temp_buf[BUFSIZ];
359 * Deletetions are performed if the user hits 'y' on a list of multiple
360 * filesystem, or if the user confirms on a unique alias.
362 sprintf(temp_buf, "Are you sure that you want to delete filesystem %s",
364 if(!one_item || Confirm(temp_buf)) {
365 if ( (stat = do_mr_query("delete_filesys", 1,
366 &info[FS_NAME], Scream, NULL)) != 0)
367 com_err(program_name, stat, " filesystem not deleted.");
369 Put_message("Filesystem deleted.");
372 Put_message("Filesystem not deleted.");
375 /* Function Name: DeleteFS
376 * Description: Delete a filesystem give its name.
377 * Arguments: argc, argv - argv[1] is the name of the filesystem.
388 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
389 QueryLoop(elem, PrintFSInfo, RealDeleteFS, "Delete the Filesystem");
395 /* Function Name: RealChangeFS
396 * Description: performs the actual change to the filesys.
397 * Arguments: info - the information
398 * junk - an unused boolean.
404 RealChangeFS(info, junk)
410 extern Menu nfsphys_menu;
412 args = AskFSInfo(info, TRUE);
414 Put_message("Aborted.");
417 stat = do_mr_query("update_filesys", CountArgs(args), args,
421 Put_message("That NFS filesystem is not exported.");
422 if (YesNoQuestion("Fix this now (Y/N)", TRUE) == TRUE) {
423 Do_menu(&nfsphys_menu, 0, NULL);
424 if (YesNoQuestion("Retry filesystem update now (Y/N)", TRUE)
426 if (stat = do_mr_query("update_filesys", CountArgs(args), args,
428 com_err(program_name, stat, " filesystem not updated");
430 Put_message("filesystem sucessfully updated.");
437 com_err(program_name, stat, " in UpdateFS");
441 /* Function Name: ChangeFS
442 * Description: change the information in a filesys record.
443 * Arguments: arc, argv - value of filsys in argv[1].
444 * Returns: DM_NORMAL.
453 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
454 QueryLoop(elem, NullPrint, RealChangeFS, "Update the Filesystem");
460 /* Function Name: AddFS
461 * Description: change the information in a filesys record.
462 * Arguments: arc, argv - name of filsys in argv[1].
463 * Returns: DM_NORMAL.
472 char *info[MAX_ARGS_SIZE], **args, buf[BUFSIZ];
474 extern Menu nfsphys_menu;
476 if ( !ValidName(argv[1]) )
479 if ( (stat = do_mr_query("get_filesys_by_label", 1, argv + 1,
480 NullFunc, NULL)) == 0) {
481 Put_message ("A Filesystem by that name already exists.");
483 } else if (stat != MR_NO_MATCH) {
484 com_err(program_name, stat, " in AddFS");
488 if ((args = AskFSInfo(SetDefaults(info, argv[1]), FALSE )) == NULL) {
489 Put_message("Aborted.");
493 stat = do_mr_query("add_filesys", CountArgs(args), args, NullFunc, NULL);
496 Put_message("That NFS filesystem is not exported.");
497 if (YesNoQuestion("Fix this now (Y/N)", TRUE) == TRUE) {
498 Do_menu(&nfsphys_menu, 0, NULL);
499 if (YesNoQuestion("Retry filesystem creation now (Y/N)", TRUE)
501 if (stat = do_mr_query("add_filesys", CountArgs(args), args,
503 com_err(program_name, stat, " in AddFS");
505 Put_message("Created.");
512 com_err(program_name, stat, " in AddFS");
515 if (stat == MR_SUCCESS && !strcasecmp(info[FS_L_TYPE], "HOMEDIR")) {
516 static char *val[] = {"def_quota", NULL};
517 static char *def_quota = NULL;
518 char *argv[Q_QUOTA + 1];
519 struct qelem *top = NULL;
521 if (def_quota == NULL) {
522 stat = do_mr_query("get_value", CountArgs(val), val,
523 StoreInfo, (char *) &top);
524 if (stat != MR_SUCCESS) {
525 com_err(program_name, stat, " getting default quota");
528 def_quota = Strsave(((char **)top->q_data)[0]);
532 if (def_quota != NULL) {
533 sprintf(buf, "Give user %s a quota of %s on filesys %s (Y/N)",
534 info[FS_NAME], def_quota, info[FS_NAME]);
535 if (YesNoQuestion(buf, 1) == TRUE) {
536 argv[Q_NAME] = argv[Q_FILESYS] = info[FS_NAME];
537 argv[Q_TYPE] = "USER";
538 argv[Q_QUOTA] = def_quota;
539 if ((stat = do_mr_query("add_quota", Q_QUOTA+1, argv, Scream,
540 (char *) NULL)) != MR_SUCCESS) {
541 com_err(program_name, stat, " while adding quota");
551 /* Function Name: SortAfter
552 * Description: choose a sortkey to cause an item to be added after
553 * the count element in the queue
554 * Arguments: queue of filesys names & sortkeys, queue count pointer
555 * Returns: sort key to use.
560 SortAfter(elem, count)
564 char *prev, *next, prevnext, *key, keybuf[9];
566 /* first find the two keys we need to insert between */
568 for (; count > 0; count--) {
569 prev = ((char **)elem->q_data)[1];
578 next = ((char **)elem->q_data)[1];
580 /* now copy the matching characters */
581 for (key = keybuf; *prev && *prev == *next; next++) {
585 /* and set the last character */
587 *prev = prevnext = 'A';
594 if (*next - *prev > 1) {
595 *key++ = (*next + *prev)/2;
598 *key++ = (prevnext + 'Z')/2;
601 return(Strsave(keybuf));
604 /* Function Name: AddFSToGroup
605 * Description: add a filesystem to an FS group
606 * Arguments: arc, argv - name of group in argv[1], filesys in argv[2].
607 * Returns: DM_NORMAL.
612 AddFSToGroup(argc, argv)
617 struct qelem *elem = NULL;
618 char buf[BUFSIZ], *args[5], *bufp;
620 if ((stat = do_mr_query("get_fsgroup_members", 1, argv+1, StoreInfo,
621 (char *)&elem)) != 0) {
622 if (stat != MR_NO_MATCH)
623 com_err(program_name, stat, " in AddFSToGroup");
629 stat = do_mr_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
631 com_err(program_name, stat, " in AddFSToGroup");
634 elem = QueueTop(elem);
636 Loop(elem, (void *) PrintFSGMembers);
637 sprintf(buf, "%d", QueueCount(elem));
639 if (GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp) == SUB_ERROR)
643 args[2] = SortAfter(elem, count);
645 FreeQueue(QueueTop(elem));
648 stat = do_mr_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
649 if (stat == MR_EXISTS) {
650 Put_message("That filesystem is already a member of the group.");
651 Put_message("Use the order command if you want to change the sorting order.");
653 com_err(program_name, stat, " in AddFSToGroup");
658 /* Function Name: RemoveFSFromGroup
659 * Description: delete a filesystem from an FS group
660 * Arguments: arc, argv - name of group in argv[1].
661 * Returns: DM_NORMAL.
666 RemoveFSFromGroup(argc, argv)
673 sprintf(buf, "Delete filesystem %s from FS group %s", argv[2], argv[1]);
676 if ((stat = do_mr_query("remove_filesys_from_fsgroup", 2, argv+1,
677 Scream, NULL)) != 0) {
678 com_err(program_name, stat, ", not removed.");
683 /* Function Name: ChangeFSGroupOrder
684 * Description: change the sortkey on a filesys in an FSgroup
685 * Arguments: arc, argv - name of group in argv[1].
686 * Returns: DM_NORMAL.
691 ChangeFSGroupOrder(argc, argv)
695 int stat, src, dst, i;
696 struct qelem *elem = NULL, *top, *tmpelem;
697 char buf[BUFSIZ], *bufp, *args[3];
699 if ((stat = do_mr_query("get_fsgroup_members", 1, argv+1, StoreInfo,
700 (char *)&elem)) != 0) {
701 if (stat == MR_NO_MATCH) {
703 "Ether %s is not a filesystem group or it has no members",
707 com_err(program_name, stat, " in ChangeFSGroupOrder");
710 top = QueueTop(elem);
712 Loop(top, (void *) PrintFSGMembers);
715 if (GetValueFromUser("Enter number of the filesystem to move:",
721 Put_message("You must enter a positive number (or 0 to abort).");
723 } else if (src == 0) {
724 Put_message("Aborted.");
727 for (elem = top, i = src; i-- > 1 && elem->q_forw; elem = elem->q_forw);
729 Put_message("You entered a number that is too high");
736 if (GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp) == SUB_ERROR)
740 if (src == dst || src == dst + 1) {
741 Put_message("That has no effect on the sorting order!");
745 Put_message("You must enter a non-negative number.");
748 for (tmpelem = top, i = dst;
749 i-- > 1 && tmpelem->q_forw;
750 tmpelem = tmpelem->q_forw);
752 Put_message("You entered a number that is too high");
757 args[2] = SortAfter(top, dst);
759 args[1] = ((char **)elem->q_data)[0];
760 if ((stat = do_mr_query("remove_filesys_from_fsgroup", 2, args,
761 Scream, NULL)) != 0) {
762 com_err(program_name, stat, " in ChangeFSGroupOrder");
765 if ((stat = do_mr_query("add_filesys_to_fsgroup", 3, args,
766 Scream, NULL)) != 0) {
767 com_err(program_name, stat, " in ChangeFSGroupOrder");
773 /* -------------- Top Level Menu ---------------- */
775 /* Function Name: GetFSAlias
776 * Description: Gets the value for a Filesystem Alias.
777 * Arguments: argc, argv - name of alias in argv[1].
778 * Returns: DM_NORMAL.
779 * NOTES: There should only be one filesystem per alias, thus
780 * this will work correctly.
785 GetFSAlias(argc, argv)
791 top = GetFSInfo(ALIAS, argv[1]);
792 Put_message(" "); /* blank line. */
793 Loop(top, (void *) PrintFSAlias);
798 /* Function Name: CreateFSAlias
799 * Description: Create an alias name for a filesystem
800 * Arguments: argc, argv - name of alias in argv[1].
801 * Returns: DM_NORMAL.
802 * NOTES: This requires (name, type, transl) I get {name, translation}
803 * from the user. I provide type, which is well-known.
808 CreateFSAlias(argc, argv)
813 struct qelem *elem, *top;
814 char *args[MAX_ARGS_SIZE], buf[BUFSIZ], **info;
818 if (!ValidName(argv[1]))
821 args[ALIAS_NAME] = Strsave(argv[1]);
822 args[ALIAS_TYPE] = Strsave(FS_ALIAS_TYPE);
823 args[ALIAS_TRANS] = Strsave("*");
826 * Check to see if this alias already exists in the database, if so then
827 * print out values, free memory used and then exit.
830 if ( (stat = do_mr_query("get_alias", 3, args, StoreInfo,
831 (char *)&elem)) == 0) {
832 top = elem = QueueTop(elem);
833 while (elem != NULL) {
834 info = (char **) elem->q_data;
835 sprintf(buf,"The alias: %s currently describes the filesystem %s",
836 info[ALIAS_NAME], info[ALIAS_TRANS]);
843 else if ( stat != MR_NO_MATCH) {
844 com_err(program_name, stat, " in CreateFSAlias.");
848 args[ALIAS_TRANS]= Strsave("");
849 args[ALIAS_END] = NULL;
850 if (GetValueFromUser("Which filesystem will this alias point to?",
851 &args[ALIAS_TRANS]) == SUB_ERROR)
854 if ( (stat = do_mr_query("add_alias", 3, args, NullFunc, NULL)) != 0)
855 com_err(program_name, stat, " in CreateFSAlias.");
861 /* Function Name: RealDeleteFSAlias
862 * Description: Does the real deletion work.
863 * Arguments: info - array of char *'s containing all useful info.
864 * one_item - a Boolean that is true if only one item
865 * in queue that dumped us here.
870 RealDeleteFSAlias(info, one_item)
875 char temp_buf[BUFSIZ];
878 * Deletetions are performed if the user hits 'y' on a list of multiple
879 * filesystem, or if the user confirms on a unique alias.
882 "Are you sure that you want to delete the filesystem alias %s",
884 if(!one_item || Confirm(temp_buf)) {
885 if ( (stat = do_mr_query("delete_alias", CountArgs(info),
886 info, Scream, NULL)) != 0 )
887 com_err(program_name, stat, " filesystem alias not deleted.");
889 Put_message("Filesystem alias deleted.");
892 Put_message("Filesystem alias not deleted.");
895 /* Function Name: DeleteFSAlias
896 * Description: Delete an alias name for a filesystem
897 * Arguments: argc, argv - name of alias in argv[1].
898 * Returns: DM_NORMAL.
899 * NOTES: This requires (name, type, transl) I get {name, translation}
900 * from the user. I provide type, which is well-known.
905 DeleteFSAlias(argc, argv)
909 struct qelem *elem = GetFSInfo(ALIAS, argv[1]);
910 QueryLoop(elem, PrintFSAlias, RealDeleteFSAlias,
911 "Delete the Filesystem Alias");
916 /* Function Name: AttachHelp
917 * Description: Print help info on attachmaint.
919 * Returns: DM_NORMAL.
925 static char *message[] = {
926 "These are the options:",
928 "get - get information about a filesystem.",
929 "add - add a new filesystem to the data base.",
930 "update - update the information in the database on a filesystem.",
931 "delete - delete a filesystem from the database.",
932 "check - check information about association of a name and a filesys.",
933 "alias - associate a name with a filesystem.",
934 "unalias - disassociate a name with a filesystem.",
935 "verbose - toggle the request for delete confirmation.",
939 return(PrintHelp(message));
942 /* Function Name: FSGroupHelp
943 * Description: Print help info on fsgroups.
945 * Returns: DM_NORMAL.
951 static char *message[] = {
952 "A filesystem group is a named sorted list of filesystems.",
954 "To create, modify, or delete a group itself, use the menu above",
955 " this one, and manipulate a filesystem of type FSGROUP.",
957 " get - get info about a group and show its members",
958 " add - add a new member to a group.",
959 " remove - remove a member from a group.",
960 " order - change the sorting order of a group.",
964 return(PrintHelp(message));