1 #if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
5 /* This is the file utils.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: Many useful utility functions.
10 * By: Chris D. Peterson
16 * Copyright 1988 by the Massachusetts Institute of Technology.
18 * For further information on copyright and distribution
19 * see the file mit-copyright.h
25 #include <moira_site.h>
29 #include "mit-copyright.h"
33 #include <netdb.h> /* for gethostbyname. */
34 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
39 /* Function Name: AddQueue
40 * Description: Adds an element to a queue
41 * Arguments: elem, pred - element and its predecessor.
47 struct qelem * elem, *pred;
55 elem->q_forw = pred->q_forw;
59 /* Function Name: RemoveQueue
60 * Description: removes an element from a queue.
69 if (elem->q_forw != NULL)
70 (elem->q_forw)->q_back = elem->q_back;
71 if (elem->q_back != NULL)
72 (elem->q_back)->q_forw = elem->q_forw;
76 /* CopyInfo: allocates memory for a copy of a NULL terminated array of
77 * strings <and returns a pointer to the copy.
86 ret = (char **) malloc(sizeof(char *) * (CountArgs(info) + 1));
89 for (i = 0; info[i]; i++)
90 ret[i] = Strsave(info[i]);
96 /* Function Name: FreeInfo
97 * Description: Frees all elements of a NULL terminated arrary of char*'s
98 * Arguments: info - array who's elements we are to free.
106 while (*info != NULL)
107 FreeAndClear(info++, TRUE);
110 /* Function Name: FreeAndClear - I couldn't resist the name.
111 * Description: Clears pointer and optionially frees it.
112 * Arguments: pointer - pointer to work with.
113 * free_it - if TRUE then free pointer.
118 FreeAndClear(pointer, free_it)
122 if (*pointer == NULL)
129 /* Function Name: QueueTop
130 * Description: returns a qelem pointer that points to the top of
132 * Arguments: elem - any element of a queue.
133 * Returns: top element of a queue.
140 if (elem == NULL) /* NULL returns NULL. */
142 while (elem->q_back != NULL)
147 /* Function Name: FreeQueueElem
148 * Description: Frees one element of the queue.
149 * Arguments: elem - the elem to free.
157 char ** info = (char **) elem->q_data;
159 if (info != (char **) NULL) {
160 FreeInfo( info ); /* free info fields */
161 free(elem->q_data); /* free info array itself. */
163 RemoveQueue(elem); /* remove this element from the queue */
164 free(elem); /* free its space. */
167 /* Function Name: FreeQueue
168 * Description: Cleans up the queue
169 * Arguments: elem - any element of the queue.
177 struct qelem *temp, *local = QueueTop(elem);
179 while(local != NULL) {
180 temp = local->q_forw;
181 FreeQueueElem(local);
186 /* Function Name: QueueCount
187 * Description: Counts the number of elements in a queue
188 * Arguments: elem - any element in the queue.
197 elem = QueueTop(elem);
198 while (elem != NULL) {
205 /* Function Name: StoreInfo
206 * Description: Stores information from an moira query into a queue.
207 * Arguments: argc, argv, - information returned from the query returned
209 * data - the previous element on the queue, this data will be
210 * stored in a qelem struct immediatly after this elem.
211 * If NULL then a new queue will be created. This value
212 * is updated to the current element at the end off the
214 * Returns: MR_CONT, or MR_ABORT if it has problems.
218 StoreInfo(argc, argv, data)
223 char ** info = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *));
224 struct qelem ** old_elem = (struct qelem **) data;
225 struct qelem * new_elem = (struct qelem *) malloc (sizeof (struct qelem));
228 if ( (new_elem == (struct qelem *) NULL) || (info == (char **) NULL) ) {
229 Put_message("Could Not allocate more memory.");
230 FreeQueue(*old_elem);
231 *old_elem = (struct qelem *) NULL;
235 for (count = 0; count < argc; count++)
236 info[count] = Strsave(argv[count]);
237 info[count] = NULL; /* NULL terminate this sucker. */
239 new_elem->q_data = (char *) info;
240 AddQueue(new_elem, *old_elem);
242 *old_elem = new_elem;
246 /* Function Name: CountArgs
247 * Description: Retrieve the number of args in a null terminated
249 * Arguments: info - the argument list.
250 * Returns: number if args in the list.
259 while (*info != NULL) {
267 /* Function Name: Scream
268 * Description: Bitch Loudly and exit, it is intended as a callback
269 * function for queries that should never return a value.
271 * Returns: doesn't exit.
277 com_err(program_name, 0,
278 "\nA Moira update returned a value -- programmer botch\n");
285 /* Function Name: PromptWithDefault
286 * Description: allows a user to be prompted for input, and given a
288 * Arguments: prompt - the prompt string.
289 * buf, buflen - buffer to be returned and its MAX size?
290 * default value for the answer.
291 * Returns: zero on failure
295 PromptWithDefault(prompt, buf, buflen, def)
303 if (parsed_argc > 0) {
305 strncpy(buf, parsed_argv[0], buflen);
306 sprintf(tmp, "%s: %s", prompt, buf);
312 (void) sprintf(tmp, "%s [%s]: ", prompt, def ? def : "");
313 ans = Prompt_input(tmp, buf, buflen);
315 (void) strcpy(buf, def);
316 else if (!strcmp(buf, "\"\""))
321 /* Function Name: YesNoQuestion
322 * Description: This prompts the user for the answer to a yes-no or
323 * true-false question.
324 * Arguments: prompt - the prompt for the user.
325 * bool_def - the default value either TRUE or FALSE.
326 * Returns: TRUE or FALSE or -1 on error
330 YesNoQuestion(prompt, bool_def)
337 if (!PromptWithDefault(prompt, ans, 2, bool_def ? "y" : "n"))
347 Put_message("Please answer 'y' or 'n'.");
352 /* Function Name: YesNoQuitQuestion
353 * Description: This prompts the user for the answer to a yes-no or
354 * true-false question, with a quit option.
355 * Arguments: prompt - the prompt for the user.
356 * bool_def - the default value either TRUE or FALSE.
357 * Returns: TRUE or FALSE or -1 on error or QUIT
358 * NOTE: It is not possible to have quit the default, but then I don't
359 * seem to need this functionality.
363 YesNoQuitQuestion(prompt, bool_def)
370 if (!PromptWithDefault(prompt, ans, 2, bool_def ? "y" : "n"))
383 Put_message("Please answer 'y', 'n' or 'q'.");
389 /* Function Name: Confirm
390 * Description: This function asks the user to confirm the action
391 * he is about to take, used for things like deleting.
392 * Arguments: prompt - the prompt string.
393 * Returns: TRUE/FALSE - wether or not the confirmation occured.
400 return( !verbose || (YesNoQuestion(prompt,FALSE) == TRUE) );
403 /* Function Name: ValidName
404 * Description: This function checks to see if we have a valid list name.
405 * Arguments: s - the list name.
406 * Returns: TRUE if Valid.
414 Put_message("Please use a non-empty name.");
415 else if (strchr(s, ' '))
416 Put_message("You cannot use space (' ') in this name.");
417 else if (strchr(s, '*') || strchr(s, '?') || strchr(s, '['))
418 Put_message("Wildcards not accepted here.");
424 /* Function Name: ToggleVerboseMode
425 * Description: This function toggles the verbose mode.
427 * Returns: DM_NORMAL.
437 Put_message("Delete functions will first confirm\n");
439 Put_message("Delete functions will be silent\n");
444 /* Function Name: NullFunc
445 * Description: dummy callback routine
456 /* Function Name: SlipInNewName
457 * Description: Slips the new name into the number 2 slot of a list, and
458 * returns a pointer to the new list.
459 * Arguments: info - list that needs this name slipped into it.
460 * name - the name to slip into the list.
461 * Returns: a pointer to the new list.
462 * NOTE: This screws up the numbers of the elements of the array in a
467 SlipInNewName(info, name)
473 /* This also pushes the NULL down. */
474 for (i = CountArgs(info); i > 0; i--) {
477 info[1] = name; /* now slip in the name. */
480 /* Function Name: GetValueFromUser
481 * Description: This function gets a value from a user for the field
483 * Arguments: prompt - prompt for user.
484 * pointer - pointer to default value, will be returned
486 * Returns: SUB_ERROR if break hit (^C).
490 GetValueFromUser(prompt, pointer)
491 char * prompt, ** pointer;
495 if (PromptWithDefault(prompt, buf, BUFSIZ, *pointer) == 0)
499 * If these are the same then there is no need to allocate a new string.
501 * a difference that makes no difference, IS no difference.
504 if (*pointer != NULL) {
505 if (strcmp(buf, *pointer) != 0) {
507 *pointer = Strsave(buf);
513 /* Function Name: GetYesNoValueFromUser
514 * Description: This function gets a value from a user for the field
516 * Arguments: prompt - prompt for user.
517 * pointer - pointer to default value, will be returned
519 * Returns: SUB_ERROR if break hit (^C).
523 GetYesNoValueFromUser(prompt, pointer)
524 char * prompt, ** pointer;
526 char user_prompt[BUFSIZ];
529 if ( strcmp (*pointer, DEFAULT_YES) == 0 )
534 sprintf(user_prompt, "%s (y/n)", prompt);
536 switch (YesNoQuestion(user_prompt, default_val)) {
538 if (*pointer != NULL)
540 *pointer = Strsave(DEFAULT_YES);
543 if (*pointer != NULL)
545 *pointer = Strsave(DEFAULT_NO);
554 /* Function Name: GetFSVal
555 * Description: asks about a specific filesystem value.
556 * Arguments: name - string for this type of filesystem.
557 * mask - mask for this type of filesystem.
558 * current - current filesystem state. (for defaults).
559 * new - new filesystem state.
560 * Returns: TRUE if successful.
564 GetFSVal(name, mask, current, new)
566 int mask, current, *new;
568 char temp_buf[BUFSIZ];
569 sprintf(temp_buf, "Is this a %s filsystem", name);
570 switch (YesNoQuestion(temp_buf, ( (mask & current) == mask) )) {
575 break; /* zero by default. */
582 /* Function Name: GetFSTypes
583 * Description: Allows user to specify filsystem types.
584 * Arguments: current - current value of filsystem, freed here.
585 * Returns: SUB_ERROR on ^C.
589 GetFSTypes(current, options)
593 int c_value, new_val = 0; /* current value of filesys type (int). */
594 char ret_value[BUFSIZ];
596 if (*current == NULL)
599 c_value = atoi(*current);
601 if (GetFSVal("student", MR_FS_STUDENT, c_value, &new_val) == FALSE)
603 if (GetFSVal("faculty", MR_FS_FACULTY, c_value, &new_val) == FALSE)
605 if (GetFSVal("staff", MR_FS_STAFF, c_value, &new_val) == FALSE)
607 if (GetFSVal("miscellaneous", MR_FS_MISC, c_value, &new_val) == FALSE)
610 if (GetFSVal("Group Quotas", MR_FS_GROUPQUOTA, c_value, &new_val) ==
615 FreeAndClear(current, TRUE);
616 sprintf(ret_value, "%d", new_val);
617 *current = Strsave(ret_value);
621 /* Function Name: Strsave
622 * Description: save a string.
623 * Arguments: string - the string to save.
624 * Returns: The malloced string, now safely saved, or NULL.
631 register char *newstr = malloc((unsigned) strlen(str) + 1);
633 if (newstr == (char *) NULL)
634 return ((char *) NULL);
636 return (strcpy(newstr, str));
640 /* atot: convert ASCII integer unix time into human readable date string */
648 time = (time_t)atoi(itime);
655 /* Function Name: Print
656 * Description: prints out all the arguments on a single line.
657 * Arguments: argc, argv - the standard MR arguments.
658 * callback - the callback function - NOT USED.
664 Print(argc, argv, callback)
666 char **argv, *callback;
672 (void) strcpy(buf,argv[0]); /* no newline 'cause Put_message adds one */
673 for (i = 1; i < argc; i++)
674 (void) sprintf(buf,"%s %s",buf,argv[i]);
675 (void) Put_message(buf);
680 /* Function Name: PrintByType
681 * Description: This function prints all members of the type specified
682 * by the callback arg, unless the callback is NULL, in which
683 * case it prints all members.
684 * Arguments: argc, argc - normal arguments for mr_callback function.
685 * callback - either a type of member or NULL.
686 * Returns: MR_CONT or MR_QUIT.
691 PrintByType(argc, argv, callback)
693 char **argv, *callback;
695 if (callback == NULL)
696 return( Print(argc, argv, callback) );
697 if (strcmp(argv[0], callback) == 0)
698 return( Print(argc, argv, callback) );
702 /* Function Name: PrintHelp
703 * Description: Prints Help Information in a NULL terminated
705 * Arguments: message.
706 * Returns: DM_NORMAL.
715 for (i = 0; i < CountArgs(message); i++)
716 Put_message(message[i]);
721 /* Function Name: Loop
722 * Description: This function goes through the entire queue, and
723 * and executes the given function on each element.
724 * Arguments: elem - top element of the queue.
725 * func - the function to execute.
734 while (elem != NULL) {
735 char ** info = (char **) elem->q_data;
742 /* Function Name: QueryLoop
743 * Description: This functions loops through a queue containing
744 * information about some item that we want to perform
745 * an operation on, and then calls the correct routine
746 * perform that operation.
747 * Arguments: top - top of the queue of information.
748 * print_func - print function.
749 * op_function - operation to be performed.
750 * query_string - string the prompts the user whether or not
751 * to perform this operation.
754 * print_opt - should expect one arguent, the info array
756 * is expected to return the name of the item.
757 * op_func - should expect two arguments.
758 * 1) the info array of char *'s.
759 * 2) a boolean the is true if there only
760 * one item in this queue, used for delete
762 * query_string - this should be of such a form that when the
763 * name of the object and '(y/n/q) ?' are appended
764 * then it should still make sense, an example is
769 QueryLoop(elem, print_func, op_func, query_string)
772 FCharStar print_func;
776 char temp_buf[BUFSIZ], *name;
778 elem = QueueTop(elem);
779 one_item = (QueueCount(elem) == 1);
780 while (elem != NULL) {
781 char **info = (char **) elem->q_data;
784 (*op_func) (info, one_item);
786 name = (*print_func) (info); /* call print function. */
787 sprintf(temp_buf,"%s %s (y/n/q)", query_string, name);
788 switch(YesNoQuitQuestion(temp_buf, FALSE)) {
790 (*op_func) (info, one_item);
795 Put_message("Aborting...");
803 /* Function Name: NullPrint
804 * Description: print function that returns nothing.
805 * Arguments: info - a pointer to the info array - Not used.
817 /* Function Name: GetTypeValues
818 * Description: gets legal values for a typed object, keeping a cache
819 * Arguments: type name
820 * Returns: argv of values
828 char *argv[3], *p, **pp, *strsave();
829 struct qelem *elem, *oelem;
830 static struct qelem *cache = NULL;
831 struct cache_elem { char *cache_name; struct qelem *cache_data; } *ce;
833 for (elem = cache; elem; elem = elem->q_forw) {
834 ce = (struct cache_elem *)elem->q_data;
835 if (!strcmp(ce->cache_name, tname))
836 return(ce->cache_data);
843 if (stat = do_mr_query("get_alias", 3, argv, StoreInfo, (char *)&elem)) {
844 com_err(program_name, stat, " in GetTypeValues");
848 for (elem = QueueTop(elem); elem; elem = elem->q_forw) {
849 pp = (char **) elem->q_data;
854 elem = (struct qelem *) malloc(sizeof(struct qelem));
855 ce = (struct cache_elem *) malloc(sizeof(struct cache_elem));
856 ce->cache_name = strsave(tname);
857 ce->cache_data = QueueTop(oelem);
858 elem->q_data = (char *)ce;
859 AddQueue(elem, cache);
860 cache = QueueTop(elem);
861 return(ce->cache_data);
865 /* Function Name: GetTypeFromUser
866 * Description: gets a typed value from the user
867 * Arguments: prompt string, type name, buffer pointer
868 * Returns: SUB_ERROR if ^C, SUB_NORMAL otherwise
871 int GetTypeFromUser(prompt, tname, pointer)
876 char def[BUFSIZ], buffer[BUFSIZ], *p, *argv[3];
880 strcpy(def, *pointer);
881 strcpy(buffer, prompt);
882 strcat(buffer, " (");
883 for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) {
884 strcat(buffer, elem->q_data);
886 strcat(buffer, ", ");
889 if (strlen(buffer) > 64)
890 sprintf(buffer, "%s (? for help)", prompt);
891 if (GetValueFromUser(buffer, pointer) == SUB_ERROR)
893 if (**pointer == '?') {
894 sprintf(buffer, "Type %s is one of:", tname);
896 for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) {
897 Put_message(elem->q_data);
899 *pointer = strsave(def);
900 return(GetTypeFromUser(prompt, tname, pointer));
902 for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) {
903 if (!strcasecmp(elem->q_data, *pointer)) {
904 strcpy(*pointer, elem->q_data);
908 sprintf(buffer, "\"%s\" is not a legal value for %s. Use one of:",
911 for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) {
912 Put_message(elem->q_data);
914 sprintf(buffer, "Are you sure you want \"%s\" to be a legal %s",
916 if (YesNoQuestion("Do you want this to be a new legal value", 0) == TRUE &&
917 YesNoQuestion(buffer, 0) == TRUE) {
921 /* don't uppercase access flags. Do uppercase everything else */
922 if (strncmp(tname, "fs_access", 9))
923 for (p = argv[2]; *p; p++)
926 if (stat = do_mr_query("add_alias", 3, argv, Scream, NULL)) {
927 com_err(program_name, stat, " in add_alias");
929 elem = (struct qelem *) malloc(sizeof(struct qelem));
930 elem->q_data = strsave(*pointer);
931 AddQueue(elem, GetTypeValues(tname));
932 Put_message("Done.");
935 *pointer = strsave(def);
936 return(GetTypeFromUser(prompt, tname, pointer));
940 /* Function Name: GetAddressFromUser
941 * Description: gets an IP address from the user
942 * Arguments: prompt string, buffer pointer
943 * buffer contains default value as long int
944 * Returns: SUB_ERROR if ^C, SUB_NORMAL otherwise
947 int GetAddressFromUser(prompt, pointer)
951 char *value, buf[256];
955 addr.s_addr = htonl(atoi(*pointer));
956 value = strsave(inet_ntoa(addr));
957 ret = GetValueFromUser(prompt, &value);
958 if (ret == SUB_ERROR) return(SUB_ERROR);
959 addr.s_addr = inet_addr(value);
961 sprintf(buf, "%d", ntohl(addr.s_addr));
962 *pointer = strsave(buf);
967 do_mr_query(name, argc, argv, proc, hint)
975 extern char *whoami, *moira_server;
978 status = mr_query(name, argc, argv, proc, hint);
979 if (status != MR_ABORTED && status != MR_NOT_CONNECTED)
981 status = mr_connect(moira_server);
983 com_err(whoami, status, " while re-connecting to server %s",
987 status = mr_auth(whoami);
989 com_err(whoami, status, " while re-authenticating to server %s",
994 status = mr_query(name, argc, argv, proc, hint);