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, NULL);
111 if ( (stat = do_mr_query("get_filesys_by_machine", 1, &name,
112 StoreInfo, (char *)&elem)) != 0) {
113 com_err(program_name, stat, NULL);
118 if ( (stat = do_mr_query("get_filesys_by_group", 1, &name,
119 StoreInfo, (char *)&elem)) != 0) {
120 com_err(program_name, stat, NULL);
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, NULL);
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;
251 sprintf(temp_buf, "Changing Attributes of filesystem %s.",
253 Put_message(temp_buf);
257 newname = Strsave(info[FS_NAME]);
258 GetValueFromUser("The new name for this filesystem",
262 GetTypeFromUser("Filesystem's Type", "filesys", &info[FS_TYPE]);
263 if (!strcasecmp(info[FS_TYPE], "FSGROUP"))
265 if (fsgroup || !strcasecmp(info[FS_TYPE], "AFS")) {
266 free(info[FS_MACHINE]);
267 info[FS_MACHINE] = Strsave(NO_MACHINE);
269 if (!strcmp(info[FS_MACHINE], NO_MACHINE_BAD)) {
270 free(info[FS_MACHINE]);
271 info[FS_MACHINE] = Strsave(NO_MACHINE);
273 GetValueFromUser("Filesystem's Machine", &info[FS_MACHINE]);
274 info[FS_MACHINE] = canonicalize_hostname(info[FS_MACHINE]);
277 GetValueFromUser("Filesystem's Pack Name", &info[FS_PACK]);
278 GetValueFromUser("Filesystem's Mount Point", &info[FS_M_POINT]);
279 GetValueFromUser("Filesystem's Default Access", &info[FS_ACCESS]);
281 GetValueFromUser("Comments about this Filesystem", &info[FS_COMMENTS]);
282 GetValueFromUser("Filesystem's owner (user)", &info[FS_OWNER]);
283 GetValueFromUser("Filesystem's owners (group)", &info[FS_OWNERS]);
285 GetYesNoValueFromUser("Automatically create this filesystem",
287 GetTypeFromUser("Filesystem's lockertype", "lockertype", &info[FS_L_TYPE]);
289 FreeAndClear(&info[FS_MODTIME], TRUE);
290 FreeAndClear(&info[FS_MODBY], TRUE);
291 FreeAndClear(&info[FS_MODWITH], TRUE);
293 if (name) /* slide the newname into the #2 slot. */
294 SlipInNewName(info, newname);
299 /* --------------- Filesystem Menu ------------- */
301 /* Function Name: GetFS
302 * Description: Get Filesystem information by name.
303 * Arguments: argc, argv - name of filsys in argv[1].
304 * Returns: DM_NORMAL.
315 top = GetFSInfo(LABEL, argv[1]); /* get info. */
316 Loop(top, (void *) PrintFSInfo);
317 FreeQueue(top); /* clean the queue. */
321 /* Function Name: RealDeleteFS
322 * Description: Does the real deletion work.
323 * Arguments: info - array of char *'s containing all useful info.
324 * one_item - a Boolean that is true if only one item
325 * in queue that dumped us here.
330 RealDeleteFS(info, one_item)
335 char temp_buf[BUFSIZ];
338 * Deletetions are performed if the user hits 'y' on a list of multiple
339 * filesystem, or if the user confirms on a unique alias.
341 sprintf(temp_buf, "Are you sure that you want to delete filesystem %s",
343 if(!one_item || Confirm(temp_buf)) {
344 if ( (stat = do_mr_query("delete_filesys", 1,
345 &info[FS_NAME], Scream, NULL)) != 0)
346 com_err(program_name, stat, " filesystem not deleted.");
348 Put_message("Filesystem deleted.");
351 Put_message("Filesystem not deleted.");
354 /* Function Name: DeleteFS
355 * Description: Delete a filesystem give its name.
356 * Arguments: argc, argv - argv[1] is the name of the filesystem.
367 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
368 QueryLoop(elem, PrintFSInfo, RealDeleteFS, "Delete the Filesystem");
374 /* Function Name: RealChangeFS
375 * Description: performs the actual change to the filesys.
376 * Arguments: info - the information
377 * junk - an unused boolean.
383 RealChangeFS(info, junk)
388 char ** args = AskFSInfo(info, TRUE);
389 extern Menu nfsphys_menu;
391 stat = do_mr_query("update_filesys", CountArgs(args), args,
395 Put_message("That NFS filesystem is not exported.");
396 if (YesNoQuestion("Fix this now (Y/N)")) {
397 Do_menu(&nfsphys_menu, 0, NULL);
398 if (YesNoQuestion("Retry filesystem update now (Y/N)")) {
399 if (stat = do_mr_query("update_filesys", CountArgs(args), args,
401 com_err(program_name, stat, " filesystem not updated");
403 Put_message("filesystem sucessfully updated.");
410 com_err(program_name, stat, " in UpdateFS");
414 /* Function Name: ChangeFS
415 * Description: change the information in a filesys record.
416 * Arguments: arc, argv - value of filsys in argv[1].
417 * Returns: DM_NORMAL.
426 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
427 QueryLoop(elem, NullPrint, RealChangeFS, "Update the Filesystem");
433 /* Function Name: AddFS
434 * Description: change the information in a filesys record.
435 * Arguments: arc, argv - name of filsys in argv[1].
436 * Returns: DM_NORMAL.
445 char *info[MAX_ARGS_SIZE], **args, buf[BUFSIZ];
447 extern Menu nfsphys_menu;
449 if ( !ValidName(argv[1]) )
452 if ( (stat = do_mr_query("get_filesys_by_label", 1, argv + 1,
453 NullFunc, NULL)) == 0) {
454 Put_message ("A Filesystem by that name already exists.");
456 } else if (stat != MR_NO_MATCH) {
457 com_err(program_name, stat, " in AddFS");
461 args = AskFSInfo(SetDefaults(info, argv[1]), FALSE );
463 stat = do_mr_query("add_filesys", CountArgs(args), args, NullFunc, NULL);
466 Put_message("That NFS filesystem is not exported.");
467 if (YesNoQuestion("Fix this now (Y/N)")) {
468 Do_menu(&nfsphys_menu, 0, NULL);
469 if (YesNoQuestion("Retry filesystem creation now (Y/N)")) {
470 if (stat = do_mr_query("add_filesys", CountArgs(args), args,
472 com_err(program_name, stat, " in AddFS");
474 Put_message("Created.");
481 com_err(program_name, stat, " in AddFS");
484 if (stat == MR_SUCCESS && !strcasecmp(info[FS_L_TYPE], "HOMEDIR")) {
485 static char *val[] = {"def_quota", NULL};
486 static char *def_quota = NULL;
488 struct qelem *top = NULL;
490 if (def_quota == NULL) {
491 stat = do_mr_query("get_value", CountArgs(val), val,
492 StoreInfo, (char *) &top);
493 if (stat != MR_SUCCESS) {
494 com_err(program_name, stat, " getting default quota");
497 def_quota = Strsave(((char **)top->q_data)[0]);
501 if (def_quota != NULL) {
502 sprintf(buf, "Give user %s a quota of %s on filesys %s (Y/N)",
503 info[FS_NAME], def_quota, info[FS_NAME]);
504 if (YesNoQuestion(buf, 1)) {
505 argv[Q_LOGIN] = argv[Q_FILESYS] = info[FS_NAME];
506 argv[Q_QUOTA] = def_quota;
507 if ((stat = do_mr_query("add_nfs_quota", 3, argv, Scream,
508 (char *) NULL)) != MR_SUCCESS) {
509 com_err(program_name, stat, " while adding quota");
519 /* Function Name: SortAfter
520 * Description: choose a sortkey to cause an item to be added after
521 * the count element in the queue
522 * Arguments: queue of filesys names & sortkeys, queue count pointer
523 * Returns: sort key to use.
528 SortAfter(elem, count)
532 char *prev, *next, prevnext, *key, keybuf[9];
534 /* first find the two keys we need to insert between */
536 for (; count > 0; count--) {
537 prev = ((char **)elem->q_data)[1];
546 next = ((char **)elem->q_data)[1];
548 /* now copy the matching characters */
549 for (key = keybuf; *prev && *prev == *next; next++) {
553 /* and set the last character */
555 *prev = prevnext = 'A';
562 if (*next - *prev > 1) {
563 *key++ = (*next + *prev)/2;
566 *key++ = (prevnext + 'Z')/2;
569 return(Strsave(keybuf));
572 /* Function Name: AddFSToGroup
573 * Description: add a filesystem to an FS group
574 * Arguments: arc, argv - name of group in argv[1], filesys in argv[2].
575 * Returns: DM_NORMAL.
580 AddFSToGroup(argc, argv)
585 struct qelem *elem = NULL;
586 char buf[BUFSIZ], *args[5], *bufp;
588 if ((stat = do_mr_query("get_fsgroup_members", 1, argv+1, StoreInfo,
589 (char *)&elem)) != 0) {
590 if (stat != MR_NO_MATCH)
591 com_err(program_name, stat, " in AddFSToGroup");
597 stat = do_mr_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
599 com_err(program_name, stat, " in AddFSToGroup");
602 elem = QueueTop(elem);
604 Loop(elem, (void *) PrintFSGMembers);
605 sprintf(buf, "%d", QueueCount(elem));
607 stat = GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp);
610 args[2] = SortAfter(elem, count);
612 FreeQueue(QueueTop(elem));
615 stat = do_mr_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
616 if (stat == MR_EXISTS) {
617 Put_message("That filesystem is already a member of the group.");
618 Put_message("Use the order command if you want to change the sorting order.");
620 com_err(program_name, stat, " in AddFSToGroup");
625 /* Function Name: RemoveFSFromGroup
626 * Description: delete a filesystem from an FS group
627 * Arguments: arc, argv - name of group in argv[1].
628 * Returns: DM_NORMAL.
633 RemoveFSFromGroup(argc, argv)
640 sprintf(buf, "Delete filesystem %s from FS group %s", argv[2], argv[1]);
643 if ((stat = do_mr_query("remove_filesys_from_fsgroup", 2, argv+1,
644 Scream, NULL)) != 0) {
645 com_err(program_name, stat, ", not removed.");
650 /* Function Name: ChangeFSGroupOrder
651 * Description: change the sortkey on a filesys in an FSgroup
652 * Arguments: arc, argv - name of group in argv[1].
653 * Returns: DM_NORMAL.
658 ChangeFSGroupOrder(argc, argv)
663 struct qelem *elem = NULL, *top;
664 char buf[BUFSIZ], *bufp, *args[3];
666 if ((stat = do_mr_query("get_fsgroup_members", 1, argv+1, StoreInfo,
667 (char *)&elem)) != 0) {
668 if (stat == MR_NO_MATCH) {
669 sprintf(buf, "Ether %s is not a filesystem group or it has no members", argv[1]);
672 com_err(program_name, stat, " in ChangeFSGroupOrder");
675 top = QueueTop(elem);
677 Loop(top, (void *) PrintFSGMembers);
680 stat = GetValueFromUser("Enter number of the filesystem to move:",
685 Put_message("You must enter a positive number (or 0 to abort).");
687 } else if (src == 0) {
688 Put_message("Aborted.");
691 for (elem = top; src-- > 1 && elem->q_forw; elem = elem->q_forw);
693 Put_message("You entered a number that is too high");
699 stat = GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp);
702 if (src == dst || src == dst + 1) {
703 Put_message("That has no effect on the sorting order!");
706 args[2] = SortAfter(top, dst);
708 args[1] = ((char **)elem->q_data)[0];
709 if ((stat = do_mr_query("remove_filesys_from_fsgroup", 2, args,
710 Scream, NULL)) != 0) {
711 com_err(program_name, stat, " in ChangeFSGroupOrder");
714 if ((stat = do_mr_query("add_filesys_to_fsgroup", 3, args,
715 Scream, NULL)) != 0) {
716 com_err(program_name, stat, " in ChangeFSGroupOrder");
722 /* -------------- Top Level Menu ---------------- */
724 /* Function Name: GetFSAlias
725 * Description: Gets the value for a Filesystem Alias.
726 * Arguments: argc, argv - name of alias in argv[1].
727 * Returns: DM_NORMAL.
728 * NOTES: There should only be one filesystem per alias, thus
729 * this will work correctly.
734 GetFSAlias(argc, argv)
740 top = GetFSInfo(ALIAS, argv[1]);
741 Put_message(" "); /* blank line. */
742 Loop(top, (void *) PrintFSAlias);
747 /* Function Name: CreateFSAlias
748 * Description: Create an alias name for a filesystem
749 * Arguments: argc, argv - name of alias in argv[1].
750 * Returns: DM_NORMAL.
751 * NOTES: This requires (name, type, transl) I get {name, translation}
752 * from the user. I provide type, which is well-known.
757 CreateFSAlias(argc, argv)
762 struct qelem *elem, *top;
763 char *args[MAX_ARGS_SIZE], buf[BUFSIZ], **info;
767 if (!ValidName(argv[1]))
770 args[ALIAS_NAME] = Strsave(argv[1]);
771 args[ALIAS_TYPE] = Strsave(FS_ALIAS_TYPE);
772 args[ALIAS_TRANS] = Strsave("*");
775 * Check to see if this alias already exists in the database, if so then
776 * print out values, free memory used and then exit.
779 if ( (stat = do_mr_query("get_alias", 3, args, StoreInfo,
780 (char *)&elem)) == 0) {
781 top = elem = QueueTop(elem);
782 while (elem != NULL) {
783 info = (char **) elem->q_data;
784 sprintf(buf,"The alias: %s currently describes the filesystem %s",
785 info[ALIAS_NAME], info[ALIAS_TRANS]);
792 else if ( stat != MR_NO_MATCH) {
793 com_err(program_name, stat, " in CreateFSAlias.");
797 args[ALIAS_TRANS]= args[ALIAS_END] = NULL; /* set to NULL initially. */
798 GetValueFromUser("Which filesystem will this alias point to?",
801 if ( (stat = do_mr_query("add_alias", 3, args, NullFunc, NULL)) != 0)
802 com_err(program_name, stat, " in CreateFSAlias.");
808 /* Function Name: RealDeleteFSAlias
809 * Description: Does the real deletion work.
810 * Arguments: info - array of char *'s containing all useful info.
811 * one_item - a Boolean that is true if only one item
812 * in queue that dumped us here.
817 RealDeleteFSAlias(info, one_item)
822 char temp_buf[BUFSIZ];
825 * Deletetions are performed if the user hits 'y' on a list of multiple
826 * filesystem, or if the user confirms on a unique alias.
829 "Are you sure that you want to delete the filesystem alias %s",
831 if(!one_item || Confirm(temp_buf)) {
832 if ( (stat = do_mr_query("delete_alias", CountArgs(info),
833 info, Scream, NULL)) != 0 )
834 com_err(program_name, stat, " filesystem alias not deleted.");
836 Put_message("Filesystem alias deleted.");
839 Put_message("Filesystem alias not deleted.");
842 /* Function Name: DeleteFSAlias
843 * Description: Delete an alias name for a filesystem
844 * Arguments: argc, argv - name of alias in argv[1].
845 * Returns: DM_NORMAL.
846 * NOTES: This requires (name, type, transl) I get {name, translation}
847 * from the user. I provide type, which is well-known.
852 DeleteFSAlias(argc, argv)
856 struct qelem *elem = GetFSInfo(ALIAS, argv[1]);
857 QueryLoop(elem, PrintFSAlias, RealDeleteFSAlias,
858 "Delete the Filesystem Alias");
863 /* Function Name: AttachHelp
864 * Description: Print help info on attachmaint.
866 * Returns: DM_NORMAL.
872 static char *message[] = {
873 "These are the options:",
875 "get - get information about a filesystem.",
876 "add - add a new filesystem to the data base.",
877 "update - update the information in the database on a filesystem.",
878 "delete - delete a filesystem from the database.",
879 "check - check information about association of a name and a filesys.",
880 "alias - associate a name with a filesystem.",
881 "unalias - disassociate a name with a filesystem.",
882 "verbose - toggle the request for delete confirmation.",
886 return(PrintHelp(message));
889 /* Function Name: FSGroupHelp
890 * Description: Print help info on fsgroups.
892 * Returns: DM_NORMAL.
898 static char *message[] = {
899 "A filesystem group is a named sorted list of filesystems.",
901 "To create, modify, or delete a group itself, use the menu above",
902 " this one, and manipulate a filesystem of type FSGROUP.",
904 " get - get info about a group and show its members",
905 " add - add a new member to a group.",
906 " remove - remove a member from a group.",
907 " order - change the sorting order of a group.",
911 return(PrintHelp(message));