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, access_type[32];
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 sprintf(access_type, "fs_access_%s", info[FS_TYPE]);
280 GetTypeFromUser("Filesystem's Default Access", access_type,
283 GetValueFromUser("Comments about this Filesystem", &info[FS_COMMENTS]);
284 GetValueFromUser("Filesystem's owner (user)", &info[FS_OWNER]);
285 GetValueFromUser("Filesystem's owners (group)", &info[FS_OWNERS]);
287 GetYesNoValueFromUser("Automatically create this filesystem",
289 GetTypeFromUser("Filesystem's lockertype", "lockertype", &info[FS_L_TYPE]);
291 FreeAndClear(&info[FS_MODTIME], TRUE);
292 FreeAndClear(&info[FS_MODBY], TRUE);
293 FreeAndClear(&info[FS_MODWITH], TRUE);
295 if (name) /* slide the newname into the #2 slot. */
296 SlipInNewName(info, newname);
301 /* --------------- Filesystem Menu ------------- */
303 /* Function Name: GetFS
304 * Description: Get Filesystem information by name.
305 * Arguments: argc, argv - name of filsys in argv[1].
306 * Returns: DM_NORMAL.
317 top = GetFSInfo(LABEL, argv[1]); /* get info. */
318 Loop(top, (void *) PrintFSInfo);
319 FreeQueue(top); /* clean the queue. */
323 /* Function Name: RealDeleteFS
324 * Description: Does the real deletion work.
325 * Arguments: info - array of char *'s containing all useful info.
326 * one_item - a Boolean that is true if only one item
327 * in queue that dumped us here.
332 RealDeleteFS(info, one_item)
337 char temp_buf[BUFSIZ];
340 * Deletetions are performed if the user hits 'y' on a list of multiple
341 * filesystem, or if the user confirms on a unique alias.
343 sprintf(temp_buf, "Are you sure that you want to delete filesystem %s",
345 if(!one_item || Confirm(temp_buf)) {
346 if ( (stat = do_mr_query("delete_filesys", 1,
347 &info[FS_NAME], Scream, NULL)) != 0)
348 com_err(program_name, stat, " filesystem not deleted.");
350 Put_message("Filesystem deleted.");
353 Put_message("Filesystem not deleted.");
356 /* Function Name: DeleteFS
357 * Description: Delete a filesystem give its name.
358 * Arguments: argc, argv - argv[1] is the name of the filesystem.
369 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
370 QueryLoop(elem, PrintFSInfo, RealDeleteFS, "Delete the Filesystem");
376 /* Function Name: RealChangeFS
377 * Description: performs the actual change to the filesys.
378 * Arguments: info - the information
379 * junk - an unused boolean.
385 RealChangeFS(info, junk)
390 char ** args = AskFSInfo(info, TRUE);
391 extern Menu nfsphys_menu;
393 stat = do_mr_query("update_filesys", CountArgs(args), args,
397 Put_message("That NFS filesystem is not exported.");
398 if (YesNoQuestion("Fix this now (Y/N)")) {
399 Do_menu(&nfsphys_menu, 0, NULL);
400 if (YesNoQuestion("Retry filesystem update now (Y/N)")) {
401 if (stat = do_mr_query("update_filesys", CountArgs(args), args,
403 com_err(program_name, stat, " filesystem not updated");
405 Put_message("filesystem sucessfully updated.");
412 com_err(program_name, stat, " in UpdateFS");
416 /* Function Name: ChangeFS
417 * Description: change the information in a filesys record.
418 * Arguments: arc, argv - value of filsys in argv[1].
419 * Returns: DM_NORMAL.
428 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
429 QueryLoop(elem, NullPrint, RealChangeFS, "Update the Filesystem");
435 /* Function Name: AddFS
436 * Description: change the information in a filesys record.
437 * Arguments: arc, argv - name of filsys in argv[1].
438 * Returns: DM_NORMAL.
447 char *info[MAX_ARGS_SIZE], **args, buf[BUFSIZ];
449 extern Menu nfsphys_menu;
451 if ( !ValidName(argv[1]) )
454 if ( (stat = do_mr_query("get_filesys_by_label", 1, argv + 1,
455 NullFunc, NULL)) == 0) {
456 Put_message ("A Filesystem by that name already exists.");
458 } else if (stat != MR_NO_MATCH) {
459 com_err(program_name, stat, " in AddFS");
463 args = AskFSInfo(SetDefaults(info, argv[1]), FALSE );
465 stat = do_mr_query("add_filesys", CountArgs(args), args, NullFunc, NULL);
468 Put_message("That NFS filesystem is not exported.");
469 if (YesNoQuestion("Fix this now (Y/N)")) {
470 Do_menu(&nfsphys_menu, 0, NULL);
471 if (YesNoQuestion("Retry filesystem creation now (Y/N)")) {
472 if (stat = do_mr_query("add_filesys", CountArgs(args), args,
474 com_err(program_name, stat, " in AddFS");
476 Put_message("Created.");
483 com_err(program_name, stat, " in AddFS");
486 if (stat == MR_SUCCESS && !strcasecmp(info[FS_L_TYPE], "HOMEDIR")) {
487 static char *val[] = {"def_quota", NULL};
488 static char *def_quota = NULL;
489 char *argv[Q_QUOTA + 1];
490 struct qelem *top = NULL;
492 if (def_quota == NULL) {
493 stat = do_mr_query("get_value", CountArgs(val), val,
494 StoreInfo, (char *) &top);
495 if (stat != MR_SUCCESS) {
496 com_err(program_name, stat, " getting default quota");
499 def_quota = Strsave(((char **)top->q_data)[0]);
503 if (def_quota != NULL) {
504 sprintf(buf, "Give user %s a quota of %s on filesys %s (Y/N)",
505 info[FS_NAME], def_quota, info[FS_NAME]);
506 if (YesNoQuestion(buf, 1)) {
507 argv[Q_NAME] = argv[Q_FILESYS] = info[FS_NAME];
508 argv[Q_TYPE] = "USER";
509 argv[Q_QUOTA] = def_quota;
510 if ((stat = do_mr_query("add_quota", Q_QUOTA+1, argv, Scream,
511 (char *) NULL)) != MR_SUCCESS) {
512 com_err(program_name, stat, " while adding quota");
522 /* Function Name: SortAfter
523 * Description: choose a sortkey to cause an item to be added after
524 * the count element in the queue
525 * Arguments: queue of filesys names & sortkeys, queue count pointer
526 * Returns: sort key to use.
531 SortAfter(elem, count)
535 char *prev, *next, prevnext, *key, keybuf[9];
537 /* first find the two keys we need to insert between */
539 for (; count > 0; count--) {
540 prev = ((char **)elem->q_data)[1];
549 next = ((char **)elem->q_data)[1];
551 /* now copy the matching characters */
552 for (key = keybuf; *prev && *prev == *next; next++) {
556 /* and set the last character */
558 *prev = prevnext = 'A';
565 if (*next - *prev > 1) {
566 *key++ = (*next + *prev)/2;
569 *key++ = (prevnext + 'Z')/2;
572 return(Strsave(keybuf));
575 /* Function Name: AddFSToGroup
576 * Description: add a filesystem to an FS group
577 * Arguments: arc, argv - name of group in argv[1], filesys in argv[2].
578 * Returns: DM_NORMAL.
583 AddFSToGroup(argc, argv)
588 struct qelem *elem = NULL;
589 char buf[BUFSIZ], *args[5], *bufp;
591 if ((stat = do_mr_query("get_fsgroup_members", 1, argv+1, StoreInfo,
592 (char *)&elem)) != 0) {
593 if (stat != MR_NO_MATCH)
594 com_err(program_name, stat, " in AddFSToGroup");
600 stat = do_mr_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
602 com_err(program_name, stat, " in AddFSToGroup");
605 elem = QueueTop(elem);
607 Loop(elem, (void *) PrintFSGMembers);
608 sprintf(buf, "%d", QueueCount(elem));
610 stat = GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp);
613 args[2] = SortAfter(elem, count);
615 FreeQueue(QueueTop(elem));
618 stat = do_mr_query("add_filesys_to_fsgroup", 3, args, Scream, NULL);
619 if (stat == MR_EXISTS) {
620 Put_message("That filesystem is already a member of the group.");
621 Put_message("Use the order command if you want to change the sorting order.");
623 com_err(program_name, stat, " in AddFSToGroup");
628 /* Function Name: RemoveFSFromGroup
629 * Description: delete a filesystem from an FS group
630 * Arguments: arc, argv - name of group in argv[1].
631 * Returns: DM_NORMAL.
636 RemoveFSFromGroup(argc, argv)
643 sprintf(buf, "Delete filesystem %s from FS group %s", argv[2], argv[1]);
646 if ((stat = do_mr_query("remove_filesys_from_fsgroup", 2, argv+1,
647 Scream, NULL)) != 0) {
648 com_err(program_name, stat, ", not removed.");
653 /* Function Name: ChangeFSGroupOrder
654 * Description: change the sortkey on a filesys in an FSgroup
655 * Arguments: arc, argv - name of group in argv[1].
656 * Returns: DM_NORMAL.
661 ChangeFSGroupOrder(argc, argv)
665 int stat, src, dst, i;
666 struct qelem *elem = NULL, *top, *tmpelem;
667 char buf[BUFSIZ], *bufp, *args[3];
669 if ((stat = do_mr_query("get_fsgroup_members", 1, argv+1, StoreInfo,
670 (char *)&elem)) != 0) {
671 if (stat == MR_NO_MATCH) {
672 sprintf(buf, "Ether %s is not a filesystem group or it has no members", argv[1]);
675 com_err(program_name, stat, " in ChangeFSGroupOrder");
678 top = QueueTop(elem);
680 Loop(top, (void *) PrintFSGMembers);
683 stat = GetValueFromUser("Enter number of the filesystem to move:",
688 Put_message("You must enter a positive number (or 0 to abort).");
690 } else if (src == 0) {
691 Put_message("Aborted.");
694 for (elem = top, i = src; i-- > 1 && elem->q_forw; elem = elem->q_forw);
696 Put_message("You entered a number that is too high");
703 stat = GetValueFromUser("Enter number of filesystem it should follow (0 to make it first):", &bufp);
706 if (src == dst || src == dst + 1) {
707 Put_message("That has no effect on the sorting order!");
711 Put_message("You must enter a non-negative number.");
714 for (tmpelem = top, i = dst;
715 i-- > 1 && tmpelem->q_forw;
716 tmpelem = tmpelem->q_forw);
718 Put_message("You entered a number that is too high");
723 args[2] = SortAfter(top, dst);
725 args[1] = ((char **)elem->q_data)[0];
726 if ((stat = do_mr_query("remove_filesys_from_fsgroup", 2, args,
727 Scream, NULL)) != 0) {
728 com_err(program_name, stat, " in ChangeFSGroupOrder");
731 if ((stat = do_mr_query("add_filesys_to_fsgroup", 3, args,
732 Scream, NULL)) != 0) {
733 com_err(program_name, stat, " in ChangeFSGroupOrder");
739 /* -------------- Top Level Menu ---------------- */
741 /* Function Name: GetFSAlias
742 * Description: Gets the value for a Filesystem Alias.
743 * Arguments: argc, argv - name of alias in argv[1].
744 * Returns: DM_NORMAL.
745 * NOTES: There should only be one filesystem per alias, thus
746 * this will work correctly.
751 GetFSAlias(argc, argv)
757 top = GetFSInfo(ALIAS, argv[1]);
758 Put_message(" "); /* blank line. */
759 Loop(top, (void *) PrintFSAlias);
764 /* Function Name: CreateFSAlias
765 * Description: Create an alias name for a filesystem
766 * Arguments: argc, argv - name of alias in argv[1].
767 * Returns: DM_NORMAL.
768 * NOTES: This requires (name, type, transl) I get {name, translation}
769 * from the user. I provide type, which is well-known.
774 CreateFSAlias(argc, argv)
779 struct qelem *elem, *top;
780 char *args[MAX_ARGS_SIZE], buf[BUFSIZ], **info;
784 if (!ValidName(argv[1]))
787 args[ALIAS_NAME] = Strsave(argv[1]);
788 args[ALIAS_TYPE] = Strsave(FS_ALIAS_TYPE);
789 args[ALIAS_TRANS] = Strsave("*");
792 * Check to see if this alias already exists in the database, if so then
793 * print out values, free memory used and then exit.
796 if ( (stat = do_mr_query("get_alias", 3, args, StoreInfo,
797 (char *)&elem)) == 0) {
798 top = elem = QueueTop(elem);
799 while (elem != NULL) {
800 info = (char **) elem->q_data;
801 sprintf(buf,"The alias: %s currently describes the filesystem %s",
802 info[ALIAS_NAME], info[ALIAS_TRANS]);
809 else if ( stat != MR_NO_MATCH) {
810 com_err(program_name, stat, " in CreateFSAlias.");
814 args[ALIAS_TRANS]= args[ALIAS_END] = NULL; /* set to NULL initially. */
815 GetValueFromUser("Which filesystem will this alias point to?",
818 if ( (stat = do_mr_query("add_alias", 3, args, NullFunc, NULL)) != 0)
819 com_err(program_name, stat, " in CreateFSAlias.");
825 /* Function Name: RealDeleteFSAlias
826 * Description: Does the real deletion work.
827 * Arguments: info - array of char *'s containing all useful info.
828 * one_item - a Boolean that is true if only one item
829 * in queue that dumped us here.
834 RealDeleteFSAlias(info, one_item)
839 char temp_buf[BUFSIZ];
842 * Deletetions are performed if the user hits 'y' on a list of multiple
843 * filesystem, or if the user confirms on a unique alias.
846 "Are you sure that you want to delete the filesystem alias %s",
848 if(!one_item || Confirm(temp_buf)) {
849 if ( (stat = do_mr_query("delete_alias", CountArgs(info),
850 info, Scream, NULL)) != 0 )
851 com_err(program_name, stat, " filesystem alias not deleted.");
853 Put_message("Filesystem alias deleted.");
856 Put_message("Filesystem alias not deleted.");
859 /* Function Name: DeleteFSAlias
860 * Description: Delete an alias name for a filesystem
861 * Arguments: argc, argv - name of alias in argv[1].
862 * Returns: DM_NORMAL.
863 * NOTES: This requires (name, type, transl) I get {name, translation}
864 * from the user. I provide type, which is well-known.
869 DeleteFSAlias(argc, argv)
873 struct qelem *elem = GetFSInfo(ALIAS, argv[1]);
874 QueryLoop(elem, PrintFSAlias, RealDeleteFSAlias,
875 "Delete the Filesystem Alias");
880 /* Function Name: AttachHelp
881 * Description: Print help info on attachmaint.
883 * Returns: DM_NORMAL.
889 static char *message[] = {
890 "These are the options:",
892 "get - get information about a filesystem.",
893 "add - add a new filesystem to the data base.",
894 "update - update the information in the database on a filesystem.",
895 "delete - delete a filesystem from the database.",
896 "check - check information about association of a name and a filesys.",
897 "alias - associate a name with a filesystem.",
898 "unalias - disassociate a name with a filesystem.",
899 "verbose - toggle the request for delete confirmation.",
903 return(PrintHelp(message));
906 /* Function Name: FSGroupHelp
907 * Description: Print help info on fsgroups.
909 * Returns: DM_NORMAL.
915 static char *message[] = {
916 "A filesystem group is a named sorted list of filesystems.",
918 "To create, modify, or delete a group itself, use the menu above",
919 " this one, and manipulate a filesystem of type FSGROUP.",
921 " get - get info about a group and show its members",
922 " add - add a new member to a group.",
923 " remove - remove a member from a group.",
924 " order - change the sorting order of a group.",
928 return(PrintHelp(message));