1 const char actions_rcs[] = "$Id: actions.c,v 1.9 2001/07/30 22:08:36 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/actions.c,v $
6 * Purpose : Declares functions to work with actions files
7 * Functions declared include: FIXME
9 * Copyright : Written by and Copyright (C) 2001 the SourceForge
10 * IJBSWA team. http://ijbswa.sourceforge.net
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will
23 * be useful, but WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A
25 * PARTICULAR PURPOSE. See the GNU General Public
26 * License for more details.
28 * The GNU General Public License should be included with
29 * this file. If not, you can view it at
30 * http://www.gnu.org/copyleft/gpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 * Revision 1.9 2001/07/30 22:08:36 jongfoster
37 * Tidying up #defines:
38 * - All feature #defines are now of the form FEATURE_xxx
39 * - Permanently turned off WIN_GUI_EDIT
40 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
42 * Revision 1.8 2001/06/29 13:19:52 oes
43 * Removed logentry from cancelled commit
45 * Revision 1.7 2001/06/09 10:55:28 jongfoster
46 * Changing BUFSIZ ==> BUFFER_SIZE
48 * Revision 1.6 2001/06/07 23:04:34 jongfoster
49 * Made get_actions() static.
51 * Revision 1.5 2001/06/03 19:11:48 oes
52 * adapted to new enlist_unique arg format
54 * Revision 1.4 2001/06/01 20:03:42 jongfoster
55 * Better memory management - current_action->strings[] now
56 * contains copies of the strings, not the original.
58 * Revision 1.3 2001/06/01 18:49:17 jongfoster
59 * Replaced "list_share" with "list" - the tiny memory gain was not
60 * worth the extra complexity.
62 * Revision 1.2 2001/05/31 21:40:00 jongfoster
63 * Removing some commented out, obsolete blocks of code.
65 * Revision 1.1 2001/05/31 21:16:46 jongfoster
66 * Moved functions to process the action list into this new file.
69 *********************************************************************/
85 const char actions_h_rcs[] = ACTIONS_H_VERSION;
88 /* Turn off everything except forwarding */
89 /* This structure is used to hold user-defined aliases */
93 struct action_spec action[1];
94 struct action_alias * next;
99 * Must declare this in this file for the above structure.
101 static int get_actions (char *line,
102 struct action_alias * alias_list,
103 struct action_spec *cur_action);
106 * We need the main list of options.
108 * First, we need a way to tell between boolean, string, and multi-string
109 * options. For string and multistring options, we also need to be
110 * able to tell the difference between a "+" and a "-". (For bools,
111 * the "+"/"-" information is encoded in "add" and "mask"). So we use
112 * an enumerated type (well, the preprocessor equivalent). Here are
115 #define AV_NONE 0 /* +opt -opt */
116 #define AV_ADD_STRING 1 /* +stropt{string} */
117 #define AV_REM_STRING 2 /* -stropt */
118 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
119 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt{*} */
122 * We need a structure to hold the name, flag changes,
123 * type, and string index.
128 unsigned mask; /* a bit set to "0" = remove action */
129 unsigned add; /* a bit set to "1" = add action */
130 int takes_value; /* an AV_... constant */
131 int index; /* index into strings[] or multi[] */
135 * And with those building blocks in place, here's the array.
137 static const struct action_name action_names[] =
140 * Well actually there's no data here - it's in actionlist.h
141 * This keeps it together to make it easy to change.
143 * Here's the macros used to format it:
145 #define DEFINE_ACTION_MULTI(name,index) \
146 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
147 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
148 #define DEFINE_ACTION_STRING(name,flag,index) \
149 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
150 { "-" name, ~flag, 0, AV_REM_STRING, index },
151 #define DEFINE_ACTION_BOOL(name,flag) \
152 { "+" name, ACTION_MASK_ALL, flag }, \
153 { "-" name, ~flag, 0 },
154 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
156 #include "actionlist.h"
158 #undef DEFINE_ACTION_MULTI
159 #undef DEFINE_ACTION_STRING
160 #undef DEFINE_ACTION_BOOL
161 #undef DEFINE_ACTION_ALIAS
163 { NULL, 0, 0 } /* End marker */
167 /*********************************************************************
169 * Function : merge_actions
171 * Description : Merge two actions together.
172 * Similar to "cur_action += new_action".
175 * 1 : cur_action = Current actions, to modify.
176 * 2 : new_action = Action to add.
180 *********************************************************************/
181 void merge_actions (struct action_spec *dest,
182 const struct action_spec *src)
186 dest->mask &= src->mask;
187 dest->add &= src->mask;
188 dest->add |= src->add;
190 for (i = 0; i < ACTION_STRING_COUNT; i++)
192 char * str = src->string[i];
195 freez(dest->string[i]);
196 dest->string[i] = strdup(str);
200 for (i = 0; i < ACTION_MULTI_COUNT; i++)
202 if (src->multi_remove_all[i])
204 /* Remove everything from dest */
205 destroy_list(dest->multi_remove[i]);
206 destroy_list(dest->multi_add[i]);
207 dest->multi_remove_all[i] = 1;
208 list_duplicate(dest->multi_add[i], src->multi_add[i]);
210 else if (dest->multi_remove_all[i])
213 * dest already removes everything, so we only need to worry
216 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
217 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
221 /* No "remove all"s to worry about. */
222 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
223 list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
224 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
230 /*********************************************************************
232 * Function : copy_action
234 * Description : Copy an action_specs.
235 * Similar to "cur_action = new_action".
238 * 1 : dest = Destination of copy.
239 * 2 : src = Source for copy.
243 *********************************************************************/
244 void copy_action (struct action_spec *dest,
245 const struct action_spec *src)
249 dest->mask = src->mask;
250 dest->add = src->add;
252 for (i = 0; i < ACTION_STRING_COUNT; i++)
254 char * str = src->string[i];
255 dest->string[i] = (str ? strdup(str) : NULL);
258 for (i = 0; i < ACTION_MULTI_COUNT; i++)
260 dest->multi_remove_all[i] = src->multi_remove_all[i];
261 list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
262 list_duplicate(dest->multi_add[i], src->multi_add[i]);
267 /*********************************************************************
269 * Function : free_action
271 * Description : Free an action_specs.
274 * 1 : src = Source to free.
278 *********************************************************************/
279 void free_action (struct action_spec *src)
283 for (i = 0; i < ACTION_STRING_COUNT; i++)
285 freez(src->string[i]);
288 for (i = 0; i < ACTION_MULTI_COUNT; i++)
290 destroy_list(src->multi_remove[i]);
291 destroy_list(src->multi_add[i]);
294 memset(src, '\0', sizeof(*src));
298 /*********************************************************************
300 * Function : get_action_token
302 * Description : Parses a line for the first action.
303 * Modifies it's input array, doesn't allocate memory.
305 * *line=" +abc{def} -ghi "
312 * 1 : line = [in] The line containing the action.
313 * [out] Start of next action on line, or
314 * NULL if we reached the end of line before
315 * we found an action.
316 * 2 : name = [out] Start of action name, null
317 * terminated. NULL on EOL
318 * 3 : value = [out] Start of action value, null
319 * terminated. NULL if none or EOL.
322 * nonzero => Mismatched {} (line was trashed anyway)
324 *********************************************************************/
325 int get_action_token(char **line, char **name, char **value)
330 /* set default returns */
335 /* Eat any leading whitespace */
336 while ((*str == ' ') || (*str == '\t'))
348 /* null name, just value is prohibited */
355 while (((ch = *str) != '\0') &&
356 (ch != ' ') && (ch != '\t') && (ch != '{'))
372 /* EOL - be careful not to run off buffer */
377 /* More to parse next time. */
386 str = strchr(str, '}');
404 /*********************************************************************
406 * Function : get_actions
408 * Description : Parses a list of actions.
411 * 1 : line = The string containing the actions.
412 * Will be written to by this function.
413 * 2 : aliases = Custom alias list, or NULL for none.
414 * 3 : cur_action = Where to store the action. Caller
418 * nonzero => Error (line was trashed anyway)
420 *********************************************************************/
421 static int get_actions(char *line,
422 struct action_alias * alias_list,
423 struct action_spec *cur_action)
425 memset(cur_action, '\0', sizeof(*cur_action));
426 cur_action->mask = ACTION_MASK_ALL;
430 char * option = NULL;
433 if (get_action_token(&line, &option, &value))
440 /* handle option in 'option' */
442 /* Check for standard action name */
443 const struct action_name * action = action_names;
445 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
449 if (action->name != NULL)
452 cur_action->mask &= action->mask;
453 cur_action->add &= action->mask;
454 cur_action->add |= action->add;
456 switch (action->takes_value)
459 /* ignore any option. */
463 /* add single string. */
465 if ((value == NULL) || (*value == '\0'))
469 /* FIXME: should validate option string here */
470 freez (cur_action->string[action->index]);
471 cur_action->string[action->index] = strdup(value);
476 /* remove single string. */
478 freez (cur_action->string[action->index]);
483 /* append multi string. */
485 struct list * remove = cur_action->multi_remove[action->index];
486 struct list * add = cur_action->multi_add[action->index];
488 if ((value == NULL) || (*value == '\0'))
493 list_remove_item(remove, value);
494 enlist_unique(add, value, 0);
499 /* remove multi string. */
501 struct list * remove = cur_action->multi_remove[action->index];
502 struct list * add = cur_action->multi_add[action->index];
504 if ( (value == NULL) || (*value == '\0')
505 || ((*value == '*') && (value[1] == '\0')) )
508 * no option, or option == "*".
512 destroy_list(remove);
514 cur_action->multi_remove_all[action->index] = 1;
518 /* Valid option - remove only 1 option */
520 if ( !cur_action->multi_remove_all[action->index] )
522 /* there isn't a catch-all in the remove list already */
523 enlist_unique(remove, value, 0);
525 list_remove_item(add, value);
530 /* Shouldn't get here unless there's memory corruption. */
536 /* try user aliases. */
537 const struct action_alias * alias = alias_list;
539 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
546 merge_actions(cur_action, alias->action);
550 /* Bad action name */
561 /*********************************************************************
563 * Function : actions_to_text
565 * Description : Converts a actionsfile entry from numeric form
566 * ("mask" and "add") to text.
569 * 1 : mask = As from struct url_actions
570 * 2 : add = As from struct url_actions
572 * Returns : A string. Caller must free it.
574 *********************************************************************/
575 char * actions_to_text(struct action_spec *action)
577 unsigned mask = action->mask;
578 unsigned add = action->add;
579 char * result = strdup("");
582 /* sanity - prevents "-feature +feature" */
586 #define DEFINE_ACTION_BOOL(__name, __bit) \
587 if (!(mask & __bit)) \
589 result = strsav(result, " -" __name); \
591 else if (add & __bit) \
593 result = strsav(result, " +" __name); \
596 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
597 if (!(mask & __bit)) \
599 result = strsav(result, " -" __name); \
601 else if (add & __bit) \
603 result = strsav(result, " +" __name "{"); \
604 result = strsav(result, action->string[__index]); \
605 result = strsav(result, "}"); \
608 #define DEFINE_ACTION_MULTI(__name, __index) \
609 if (action->multi_remove_all[__index]) \
611 result = strsav(result, " -" __name "{*}"); \
615 lst = action->multi_remove[__index]->next; \
618 result = strsav(result, " -" __name "{"); \
619 result = strsav(result, lst->str); \
620 result = strsav(result, "}"); \
624 lst = action->multi_add[__index]->next; \
627 result = strsav(result, " +" __name "{"); \
628 result = strsav(result, lst->str); \
629 result = strsav(result, "}"); \
633 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
635 #include "actionlist.h"
637 #undef DEFINE_ACTION_MULTI
638 #undef DEFINE_ACTION_STRING
639 #undef DEFINE_ACTION_BOOL
640 #undef DEFINE_ACTION_ALIAS
646 /*********************************************************************
648 * Function : current_actions_to_text
650 * Description : Converts a actionsfile entry to text.
653 * 1 : action = Action
655 * Returns : A string. Caller must free it.
657 *********************************************************************/
658 char * current_action_to_text(struct current_action_spec *action)
660 unsigned flags = action->flags;
661 char * result = strdup("");
664 #define DEFINE_ACTION_BOOL(__name, __bit) \
667 result = strsav(result, " +" __name); \
671 result = strsav(result, " -" __name); \
674 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
677 result = strsav(result, " +" __name "{"); \
678 result = strsav(result, action->string[__index]); \
679 result = strsav(result, "}"); \
683 result = strsav(result, " -" __name); \
686 #define DEFINE_ACTION_MULTI(__name, __index) \
687 lst = action->multi[__index]->next; \
690 result = strsav(result, " -" __name); \
696 result = strsav(result, " +" __name "{"); \
697 result = strsav(result, lst->str); \
698 result = strsav(result, "}"); \
703 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
705 #include "actionlist.h"
707 #undef DEFINE_ACTION_MULTI
708 #undef DEFINE_ACTION_STRING
709 #undef DEFINE_ACTION_BOOL
710 #undef DEFINE_ACTION_ALIAS
716 /*********************************************************************
718 * Function : init_current_action
720 * Description : Zero out an action.
723 * 1 : dest = An uninitialized current_action_spec.
727 *********************************************************************/
728 void init_current_action (struct current_action_spec *dest)
730 memset(dest, '\0', sizeof(*dest));
731 dest->flags = ACTION_MOST_COMPATIBLE;
735 /*********************************************************************
737 * Function : merge_current_action
739 * Description : Merge two actions together.
740 * Similar to "dest += src".
741 * Differences between this and merge_actions()
742 * is that this one doesn't allocate memory for
743 * strings (so "src" better be in memory for at least
744 * as long as "dest" is, and you'd better free
745 * "dest" using "current_free_action").
746 * Also, there is no mask or remove lists in dest.
747 * (If we're applying it to a URL, we don't need them)
750 * 1 : dest = Current actions, to modify.
751 * 2 : src = Action to add.
755 *********************************************************************/
756 void merge_current_action (struct current_action_spec *dest,
757 const struct action_spec *src)
761 dest->flags &= src->mask;
762 dest->flags |= src->add;
764 for (i = 0; i < ACTION_STRING_COUNT; i++)
766 char * str = src->string[i];
769 freez(dest->string[i]);
770 dest->string[i] = strdup(str);
774 for (i = 0; i < ACTION_MULTI_COUNT; i++)
776 if (src->multi_remove_all[i])
778 /* Remove everything from dest */
779 destroy_list(dest->multi[i]);
780 list_duplicate(dest->multi[i], src->multi_add[i]);
784 list_remove_list(dest->multi[i], src->multi_remove[i]);
785 list_append_list_unique(dest->multi[i], src->multi_add[i]);
791 /*********************************************************************
793 * Function : free_current_action
795 * Description : Free a current_action_spec.
798 * 1 : src = Source to free.
802 *********************************************************************/
803 void free_current_action (struct current_action_spec *src)
807 for (i = 0; i < ACTION_STRING_COUNT; i++)
809 freez(src->string[i]);
812 for (i = 0; i < ACTION_MULTI_COUNT; i++)
814 destroy_list(src->multi[i]);
817 memset(src, '\0', sizeof(*src));
821 /*********************************************************************
823 * Function : unload_actions_file
825 * Description : Unloads an actions module.
828 * 1 : file_data = the data structure associated with the
833 *********************************************************************/
834 void unload_actions_file(void *file_data)
836 struct url_actions * next;
837 struct url_actions * cur = (struct url_actions *)file_data;
849 /*********************************************************************
851 * Function : load_actions_file
853 * Description : Read and parse a action file and add to files
857 * 1 : csp = Current client state (buffers, headers, etc...)
859 * Returns : 0 => Ok, everything else is an error.
861 *********************************************************************/
862 int load_actions_file(struct client_state *csp)
864 static struct file_list *current_actions_file = NULL;
868 struct url_actions *last_perm;
869 struct url_actions *perm;
870 char buf[BUFFER_SIZE];
871 struct file_list *fs;
872 #define MODE_START_OF_FILE 1
873 #define MODE_ACTIONS 2
875 int mode = MODE_START_OF_FILE;
876 struct action_spec cur_action[1];
877 struct action_alias * alias_list = NULL;
879 memset(cur_action, '\0', sizeof(*cur_action));
881 if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
883 /* No need to load */
886 csp->actions_list = current_actions_file;
892 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
893 csp->config->actions_file);
894 return 1; /* never get here */
897 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
898 if (last_perm == NULL)
900 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
901 csp->config->actions_file);
902 return 1; /* never get here */
905 if ((fp = fopen(csp->config->actions_file, "r")) == NULL)
907 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
908 csp->config->actions_file);
909 return 1; /* never get here */
912 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
916 /* It's a header block */
919 /* It's {{settings}} or {{alias}} */
920 int len = strlen(buf);
921 char * start = buf + 2;
922 char * end = buf + len - 1;
923 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
927 log_error(LOG_LEVEL_FATAL,
928 "can't load actions file '%s': invalid line: %s",
929 csp->config->actions_file, buf);
930 return 1; /* never get here */
933 /* Trim leading and trailing whitespace. */
941 log_error(LOG_LEVEL_FATAL,
942 "can't load actions file '%s': invalid line: {{ }}",
943 csp->config->actions_file);
944 return 1; /* never get here */
947 if (0 == strcmpic(start, "alias"))
949 /* it's an {{alias}} block */
955 /* invalid {{something}} block */
957 log_error(LOG_LEVEL_FATAL,
958 "can't load actions file '%s': invalid line: {{%s}}",
959 csp->config->actions_file, start);
960 return 1; /* never get here */
965 /* It's an actions block */
967 char actions_buf[BUFFER_SIZE];
973 /* free old action */
974 free_action(cur_action);
977 strcpy(actions_buf, buf + 1);
979 /* check we have a trailing } and then trim it */
980 end = actions_buf + strlen(actions_buf) - 1;
985 log_error(LOG_LEVEL_FATAL,
986 "can't load actions file '%s': invalid line: %s",
987 csp->config->actions_file, buf);
988 return 1; /* never get here */
992 /* trim any whitespace immediately inside {} */
995 if (*actions_buf == '\0')
999 log_error(LOG_LEVEL_FATAL,
1000 "can't load actions file '%s': invalid line: %s",
1001 csp->config->actions_file, buf);
1002 return 1; /* never get here */
1005 if (get_actions(actions_buf, alias_list, cur_action))
1009 log_error(LOG_LEVEL_FATAL,
1010 "can't load actions file '%s': invalid line: %s",
1011 csp->config->actions_file, buf);
1012 return 1; /* never get here */
1016 else if (mode == MODE_ALIAS)
1018 /* define an alias */
1019 char actions_buf[BUFFER_SIZE];
1020 struct action_alias * new_alias;
1022 char * start = strchr(buf, '=');
1025 if ((start == NULL) || (start == buf))
1027 log_error(LOG_LEVEL_FATAL,
1028 "can't load actions file '%s': invalid alias line: %s",
1029 csp->config->actions_file, buf);
1030 return 1; /* never get here */
1033 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1036 log_error(LOG_LEVEL_FATAL,
1037 "can't load actions file '%s': out of memory!",
1038 csp->config->actions_file);
1039 return 1; /* never get here */
1042 /* Eat any the whitespace before the '=' */
1044 while ((*end == ' ') || (*end == '\t'))
1047 * we already know we must have at least 1 non-ws char
1048 * at start of buf - no need to check
1054 /* Eat any the whitespace after the '=' */
1056 while ((*start == ' ') || (*start == '\t'))
1062 log_error(LOG_LEVEL_FATAL,
1063 "can't load actions file '%s': invalid alias line: %s",
1064 csp->config->actions_file, buf);
1065 return 1; /* never get here */
1068 new_alias->name = strdup(buf);
1070 strcpy(actions_buf, start);
1072 if (get_actions(actions_buf, alias_list, new_alias->action))
1076 log_error(LOG_LEVEL_FATAL,
1077 "can't load actions file '%s': invalid alias line: %s = %s",
1078 csp->config->actions_file, buf, start);
1079 return 1; /* never get here */
1083 new_alias->next = alias_list;
1084 alias_list = new_alias;
1086 else if (mode == MODE_ACTIONS)
1088 /* it's a URL pattern */
1090 /* allocate a new node */
1091 if ((perm = zalloc(sizeof(*perm))) == NULL)
1094 log_error(LOG_LEVEL_FATAL,
1095 "can't load actions file '%s': out of memory!",
1096 csp->config->actions_file);
1097 return 1; /* never get here */
1101 copy_action (perm->action, cur_action);
1103 /* Save the URL pattern */
1104 if (create_url_spec(perm->url, buf))
1107 log_error(LOG_LEVEL_FATAL,
1108 "can't load actions file '%s': cannot create URL pattern from: %s",
1109 csp->config->actions_file, buf);
1110 return 1; /* never get here */
1113 /* add it to the list */
1114 last_perm->next = perm;
1117 else if (mode == MODE_START_OF_FILE)
1119 /* oops - please have a {} line as 1st line in file. */
1121 log_error(LOG_LEVEL_FATAL,
1122 "can't load actions file '%s': first line is invalid: %s",
1123 csp->config->actions_file, buf);
1124 return 1; /* never get here */
1128 /* How did we get here? This is impossible! */
1130 log_error(LOG_LEVEL_FATAL,
1131 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1132 csp->config->actions_file, mode);
1133 return 1; /* never get here */
1139 free_action(cur_action);
1141 while (alias_list != NULL)
1143 struct action_alias * next = alias_list->next;
1144 freez((char *)alias_list->name);
1145 free_action(alias_list->action);
1150 /* the old one is now obsolete */
1151 if (current_actions_file)
1153 current_actions_file->unloader = unload_actions_file;
1156 fs->next = files->next;
1158 current_actions_file = fs;
1162 csp->actions_list = fs;