1 const char actions_rcs[] = "$Id: actions.c,v 1.7 2001/06/09 10:55:28 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.7 2001/06/09 10:55:28 jongfoster
37 * Changing BUFSIZ ==> BUFFER_SIZE
39 * Revision 1.6 2001/06/07 23:04:34 jongfoster
40 * Made get_actions() static.
42 * Revision 1.5 2001/06/03 19:11:48 oes
43 * adapted to new enlist_unique arg format
45 * Revision 1.4 2001/06/01 20:03:42 jongfoster
46 * Better memory management - current_action->strings[] now
47 * contains copies of the strings, not the original.
49 * Revision 1.3 2001/06/01 18:49:17 jongfoster
50 * Replaced "list_share" with "list" - the tiny memory gain was not
51 * worth the extra complexity.
53 * Revision 1.2 2001/05/31 21:40:00 jongfoster
54 * Removing some commented out, obsolete blocks of code.
56 * Revision 1.1 2001/05/31 21:16:46 jongfoster
57 * Moved functions to process the action list into this new file.
60 *********************************************************************/
76 const char actions_h_rcs[] = ACTIONS_H_VERSION;
79 /* Turn off everything except forwarding */
80 /* This structure is used to hold user-defined aliases */
84 struct action_spec action[1];
85 struct action_alias * next;
90 * Must declare this in this file for the above structure.
92 static int get_actions (char *line,
93 struct action_alias * alias_list,
94 struct action_spec *cur_action);
97 * We need the main list of options.
99 * First, we need a way to tell between boolean, string, and multi-string
100 * options. For string and multistring options, we also need to be
101 * able to tell the difference between a "+" and a "-". (For bools,
102 * the "+"/"-" information is encoded in "add" and "mask"). So we use
103 * an enumerated type (well, the preprocessor equivalent). Here are
106 #define AV_NONE 0 /* +opt -opt */
107 #define AV_ADD_STRING 1 /* +stropt{string} */
108 #define AV_REM_STRING 2 /* -stropt */
109 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
110 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt{*} */
113 * We need a structure to hold the name, flag changes,
114 * type, and string index.
119 unsigned mask; /* a bit set to "0" = remove action */
120 unsigned add; /* a bit set to "1" = add action */
121 int takes_value; /* an AV_... constant */
122 int index; /* index into strings[] or multi[] */
126 * And with those building blocks in place, here's the array.
128 static const struct action_name action_names[] =
131 * Well actually there's no data here - it's in actionlist.h
132 * This keeps it together to make it easy to change.
134 * Here's the macros used to format it:
136 #define DEFINE_ACTION_MULTI(name,index) \
137 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
138 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
139 #define DEFINE_ACTION_STRING(name,flag,index) \
140 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
141 { "-" name, ~flag, 0, AV_REM_STRING, index },
142 #define DEFINE_ACTION_BOOL(name,flag) \
143 { "+" name, ACTION_MASK_ALL, flag }, \
144 { "-" name, ~flag, 0 },
145 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
147 #include "actionlist.h"
149 #undef DEFINE_ACTION_MULTI
150 #undef DEFINE_ACTION_STRING
151 #undef DEFINE_ACTION_BOOL
152 #undef DEFINE_ACTION_ALIAS
154 { NULL, 0, 0 } /* End marker */
158 /*********************************************************************
160 * Function : merge_actions
162 * Description : Merge two actions together.
163 * Similar to "cur_action += new_action".
166 * 1 : cur_action = Current actions, to modify.
167 * 2 : new_action = Action to add.
171 *********************************************************************/
172 void merge_actions (struct action_spec *dest,
173 const struct action_spec *src)
177 dest->mask &= src->mask;
178 dest->add &= src->mask;
179 dest->add |= src->add;
181 for (i = 0; i < ACTION_STRING_COUNT; i++)
183 char * str = src->string[i];
186 freez(dest->string[i]);
187 dest->string[i] = strdup(str);
191 for (i = 0; i < ACTION_MULTI_COUNT; i++)
193 if (src->multi_remove_all[i])
195 /* Remove everything from dest */
196 destroy_list(dest->multi_remove[i]);
197 destroy_list(dest->multi_add[i]);
198 dest->multi_remove_all[i] = 1;
199 list_duplicate(dest->multi_add[i], src->multi_add[i]);
201 else if (dest->multi_remove_all[i])
204 * dest already removes everything, so we only need to worry
207 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
208 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
212 /* No "remove all"s to worry about. */
213 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
214 list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
215 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
221 /*********************************************************************
223 * Function : copy_action
225 * Description : Copy an action_specs.
226 * Similar to "cur_action = new_action".
229 * 1 : dest = Destination of copy.
230 * 2 : src = Source for copy.
234 *********************************************************************/
235 void copy_action (struct action_spec *dest,
236 const struct action_spec *src)
240 dest->mask = src->mask;
241 dest->add = src->add;
243 for (i = 0; i < ACTION_STRING_COUNT; i++)
245 char * str = src->string[i];
246 dest->string[i] = (str ? strdup(str) : NULL);
249 for (i = 0; i < ACTION_MULTI_COUNT; i++)
251 dest->multi_remove_all[i] = src->multi_remove_all[i];
252 list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
253 list_duplicate(dest->multi_add[i], src->multi_add[i]);
258 /*********************************************************************
260 * Function : free_action
262 * Description : Free an action_specs.
265 * 1 : src = Source to free.
269 *********************************************************************/
270 void free_action (struct action_spec *src)
274 for (i = 0; i < ACTION_STRING_COUNT; i++)
276 freez(src->string[i]);
279 for (i = 0; i < ACTION_MULTI_COUNT; i++)
281 destroy_list(src->multi_remove[i]);
282 destroy_list(src->multi_add[i]);
285 memset(src, '\0', sizeof(*src));
289 /*********************************************************************
291 * Function : get_action_token
293 * Description : Parses a line for the first action.
294 * Modifies it's input array, doesn't allocate memory.
296 * *line=" +abc{def} -ghi "
303 * 1 : line = [in] The line containing the action.
304 * [out] Start of next action on line, or
305 * NULL if we reached the end of line before
306 * we found an action.
307 * 2 : name = [out] Start of action name, null
308 * terminated. NULL on EOL
309 * 3 : value = [out] Start of action value, null
310 * terminated. NULL if none or EOL.
313 * nonzero => Mismatched {} (line was trashed anyway)
315 *********************************************************************/
316 int get_action_token(char **line, char **name, char **value)
321 /* set default returns */
326 /* Eat any leading whitespace */
327 while ((*str == ' ') || (*str == '\t'))
339 /* null name, just value is prohibited */
346 while (((ch = *str) != '\0') &&
347 (ch != ' ') && (ch != '\t') && (ch != '{'))
363 /* EOL - be careful not to run off buffer */
368 /* More to parse next time. */
377 str = strchr(str, '}');
395 /*********************************************************************
397 * Function : get_actions
399 * Description : Parses a list of actions.
402 * 1 : line = The string containing the actions.
403 * Will be written to by this function.
404 * 2 : aliases = Custom alias list, or NULL for none.
405 * 3 : cur_action = Where to store the action. Caller
409 * nonzero => Error (line was trashed anyway)
411 *********************************************************************/
412 static int get_actions(char *line,
413 struct action_alias * alias_list,
414 struct action_spec *cur_action)
416 memset(cur_action, '\0', sizeof(*cur_action));
417 cur_action->mask = ACTION_MASK_ALL;
421 char * option = NULL;
424 if (get_action_token(&line, &option, &value))
431 /* handle option in 'option' */
433 /* Check for standard action name */
434 const struct action_name * action = action_names;
436 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
440 if (action->name != NULL)
443 cur_action->mask &= action->mask;
444 cur_action->add &= action->mask;
445 cur_action->add |= action->add;
447 switch (action->takes_value)
450 /* ignore any option. */
454 /* add single string. */
456 if ((value == NULL) || (*value == '\0'))
460 /* FIXME: should validate option string here */
461 freez (cur_action->string[action->index]);
462 cur_action->string[action->index] = strdup(value);
467 /* remove single string. */
469 freez (cur_action->string[action->index]);
474 /* append multi string. */
476 struct list * remove = cur_action->multi_remove[action->index];
477 struct list * add = cur_action->multi_add[action->index];
479 if ((value == NULL) || (*value == '\0'))
484 list_remove_item(remove, value);
485 enlist_unique(add, value, 0);
490 /* remove multi string. */
492 struct list * remove = cur_action->multi_remove[action->index];
493 struct list * add = cur_action->multi_add[action->index];
495 if ( (value == NULL) || (*value == '\0')
496 || ((*value == '*') && (value[1] == '\0')) )
499 * no option, or option == "*".
503 destroy_list(remove);
505 cur_action->multi_remove_all[action->index] = 1;
509 /* Valid option - remove only 1 option */
511 if ( !cur_action->multi_remove_all[action->index] )
513 /* there isn't a catch-all in the remove list already */
514 enlist_unique(remove, value, 0);
516 list_remove_item(add, value);
521 /* Shouldn't get here unless there's memory corruption. */
527 /* try user aliases. */
528 const struct action_alias * alias = alias_list;
530 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
537 merge_actions(cur_action, alias->action);
541 /* Bad action name */
552 /*********************************************************************
554 * Function : actions_to_text
556 * Description : Converts a actionsfile entry from numeric form
557 * ("mask" and "add") to text.
560 * 1 : mask = As from struct url_actions
561 * 2 : add = As from struct url_actions
563 * Returns : A string. Caller must free it.
565 *********************************************************************/
566 char * actions_to_text(struct action_spec *action)
568 unsigned mask = action->mask;
569 unsigned add = action->add;
570 char * result = strdup("");
573 /* sanity - prevents "-feature +feature" */
577 #define DEFINE_ACTION_BOOL(__name, __bit) \
578 if (!(mask & __bit)) \
580 result = strsav(result, " -" __name); \
582 else if (add & __bit) \
584 result = strsav(result, " +" __name); \
587 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
588 if (!(mask & __bit)) \
590 result = strsav(result, " -" __name); \
592 else if (add & __bit) \
594 result = strsav(result, " +" __name "{"); \
595 result = strsav(result, action->string[__index]); \
596 result = strsav(result, "}"); \
599 #define DEFINE_ACTION_MULTI(__name, __index) \
600 if (action->multi_remove_all[__index]) \
602 result = strsav(result, " -" __name "{*}"); \
606 lst = action->multi_remove[__index]->next; \
609 result = strsav(result, " -" __name "{"); \
610 result = strsav(result, lst->str); \
611 result = strsav(result, "}"); \
615 lst = action->multi_add[__index]->next; \
618 result = strsav(result, " +" __name "{"); \
619 result = strsav(result, lst->str); \
620 result = strsav(result, "}"); \
624 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
626 #include "actionlist.h"
628 #undef DEFINE_ACTION_MULTI
629 #undef DEFINE_ACTION_STRING
630 #undef DEFINE_ACTION_BOOL
631 #undef DEFINE_ACTION_ALIAS
637 /*********************************************************************
639 * Function : current_actions_to_text
641 * Description : Converts a actionsfile entry to text.
644 * 1 : action = Action
646 * Returns : A string. Caller must free it.
648 *********************************************************************/
649 char * current_action_to_text(struct current_action_spec *action)
651 unsigned flags = action->flags;
652 char * result = strdup("");
655 #define DEFINE_ACTION_BOOL(__name, __bit) \
658 result = strsav(result, " +" __name); \
662 result = strsav(result, " -" __name); \
665 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
668 result = strsav(result, " +" __name "{"); \
669 result = strsav(result, action->string[__index]); \
670 result = strsav(result, "}"); \
674 result = strsav(result, " -" __name); \
677 #define DEFINE_ACTION_MULTI(__name, __index) \
678 lst = action->multi[__index]->next; \
681 result = strsav(result, " -" __name); \
687 result = strsav(result, " +" __name "{"); \
688 result = strsav(result, lst->str); \
689 result = strsav(result, "}"); \
694 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
696 #include "actionlist.h"
698 #undef DEFINE_ACTION_MULTI
699 #undef DEFINE_ACTION_STRING
700 #undef DEFINE_ACTION_BOOL
701 #undef DEFINE_ACTION_ALIAS
707 /*********************************************************************
709 * Function : init_current_action
711 * Description : Zero out an action.
714 * 1 : dest = An uninitialized current_action_spec.
718 *********************************************************************/
719 void init_current_action (struct current_action_spec *dest)
721 memset(dest, '\0', sizeof(*dest));
722 dest->flags = ACTION_MOST_COMPATIBLE;
726 /*********************************************************************
728 * Function : merge_current_action
730 * Description : Merge two actions together.
731 * Similar to "dest += src".
732 * Differences between this and merge_actions()
733 * is that this one doesn't allocate memory for
734 * strings (so "src" better be in memory for at least
735 * as long as "dest" is, and you'd better free
736 * "dest" using "current_free_action").
737 * Also, there is no mask or remove lists in dest.
738 * (If we're applying it to a URL, we don't need them)
741 * 1 : dest = Current actions, to modify.
742 * 2 : src = Action to add.
746 *********************************************************************/
747 void merge_current_action (struct current_action_spec *dest,
748 const struct action_spec *src)
752 dest->flags &= src->mask;
753 dest->flags |= src->add;
755 for (i = 0; i < ACTION_STRING_COUNT; i++)
757 char * str = src->string[i];
760 freez(dest->string[i]);
761 dest->string[i] = strdup(str);
765 for (i = 0; i < ACTION_MULTI_COUNT; i++)
767 if (src->multi_remove_all[i])
769 /* Remove everything from dest */
770 destroy_list(dest->multi[i]);
771 list_duplicate(dest->multi[i], src->multi_add[i]);
775 list_remove_list(dest->multi[i], src->multi_remove[i]);
776 list_append_list_unique(dest->multi[i], src->multi_add[i]);
782 /*********************************************************************
784 * Function : free_current_action
786 * Description : Free a current_action_spec.
789 * 1 : src = Source to free.
793 *********************************************************************/
794 void free_current_action (struct current_action_spec *src)
798 for (i = 0; i < ACTION_STRING_COUNT; i++)
800 freez(src->string[i]);
803 for (i = 0; i < ACTION_MULTI_COUNT; i++)
805 destroy_list(src->multi[i]);
808 memset(src, '\0', sizeof(*src));
812 /*********************************************************************
814 * Function : unload_actions_file
816 * Description : Unloads an actions module.
819 * 1 : file_data = the data structure associated with the
824 *********************************************************************/
825 void unload_actions_file(void *file_data)
827 struct url_actions * next;
828 struct url_actions * cur = (struct url_actions *)file_data;
840 /*********************************************************************
842 * Function : load_actions_file
844 * Description : Read and parse a action file and add to files
848 * 1 : csp = Current client state (buffers, headers, etc...)
850 * Returns : 0 => Ok, everything else is an error.
852 *********************************************************************/
853 int load_actions_file(struct client_state *csp)
855 static struct file_list *current_actions_file = NULL;
859 struct url_actions *last_perm;
860 struct url_actions *perm;
861 char buf[BUFFER_SIZE];
862 struct file_list *fs;
863 #define MODE_START_OF_FILE 1
864 #define MODE_ACTIONS 2
866 int mode = MODE_START_OF_FILE;
867 struct action_spec cur_action[1];
868 struct action_alias * alias_list = NULL;
870 memset(cur_action, '\0', sizeof(*cur_action));
872 if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
874 /* No need to load */
877 csp->actions_list = current_actions_file;
883 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
884 csp->config->actions_file);
885 return 1; /* never get here */
888 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
889 if (last_perm == NULL)
891 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
892 csp->config->actions_file);
893 return 1; /* never get here */
896 if ((fp = fopen(csp->config->actions_file, "r")) == NULL)
898 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
899 csp->config->actions_file);
900 return 1; /* never get here */
903 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
907 /* It's a header block */
910 /* It's {{settings}} or {{alias}} */
911 int len = strlen(buf);
912 char * start = buf + 2;
913 char * end = buf + len - 1;
914 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
918 log_error(LOG_LEVEL_FATAL,
919 "can't load actions file '%s': invalid line: %s",
920 csp->config->actions_file, buf);
921 return 1; /* never get here */
924 /* Trim leading and trailing whitespace. */
932 log_error(LOG_LEVEL_FATAL,
933 "can't load actions file '%s': invalid line: {{ }}",
934 csp->config->actions_file);
935 return 1; /* never get here */
938 if (0 == strcmpic(start, "alias"))
940 /* it's an {{alias}} block */
946 /* invalid {{something}} block */
948 log_error(LOG_LEVEL_FATAL,
949 "can't load actions file '%s': invalid line: {{%s}}",
950 csp->config->actions_file, start);
951 return 1; /* never get here */
956 /* It's an actions block */
958 char actions_buf[BUFFER_SIZE];
964 /* free old action */
965 free_action(cur_action);
968 strcpy(actions_buf, buf + 1);
970 /* check we have a trailing } and then trim it */
971 end = actions_buf + strlen(actions_buf) - 1;
976 log_error(LOG_LEVEL_FATAL,
977 "can't load actions file '%s': invalid line: %s",
978 csp->config->actions_file, buf);
979 return 1; /* never get here */
983 /* trim any whitespace immediately inside {} */
986 if (*actions_buf == '\0')
990 log_error(LOG_LEVEL_FATAL,
991 "can't load actions file '%s': invalid line: %s",
992 csp->config->actions_file, buf);
993 return 1; /* never get here */
996 if (get_actions(actions_buf, alias_list, cur_action))
1000 log_error(LOG_LEVEL_FATAL,
1001 "can't load actions file '%s': invalid line: %s",
1002 csp->config->actions_file, buf);
1003 return 1; /* never get here */
1007 else if (mode == MODE_ALIAS)
1009 /* define an alias */
1010 char actions_buf[BUFFER_SIZE];
1011 struct action_alias * new_alias;
1014 char * start = strchr(buf, '=');
1017 if ((start == NULL) || (start == buf))
1019 log_error(LOG_LEVEL_FATAL,
1020 "can't load actions file '%s': invalid alias line: %s",
1021 csp->config->actions_file, buf);
1022 return 1; /* never get here */
1025 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1028 log_error(LOG_LEVEL_FATAL,
1029 "can't load actions file '%s': out of memory!",
1030 csp->config->actions_file);
1031 return 1; /* never get here */
1034 /* Eat any the whitespace before the '=' */
1036 while ((*end == ' ') || (*end == '\t'))
1039 * we already know we must have at least 1 non-ws char
1040 * at start of buf - no need to check
1046 /* Eat any the whitespace after the '=' */
1048 while ((*start == ' ') || (*start == '\t'))
1054 log_error(LOG_LEVEL_FATAL,
1055 "can't load actions file '%s': invalid alias line: %s",
1056 csp->config->actions_file, buf);
1057 return 1; /* never get here */
1060 new_alias->name = strdup(buf);
1062 strcpy(actions_buf, start);
1064 if (get_actions(actions_buf, alias_list, new_alias->action))
1068 log_error(LOG_LEVEL_FATAL,
1069 "can't load actions file '%s': invalid alias line: %s = %s",
1070 csp->config->actions_file, buf, start);
1071 return 1; /* never get here */
1075 new_alias->next = alias_list;
1076 alias_list = new_alias;
1078 else if (mode == MODE_ACTIONS)
1080 /* it's a URL pattern */
1082 /* allocate a new node */
1083 if ((perm = zalloc(sizeof(*perm))) == NULL)
1086 log_error(LOG_LEVEL_FATAL,
1087 "can't load actions file '%s': out of memory!",
1088 csp->config->actions_file);
1089 return 1; /* never get here */
1093 copy_action (perm->action, cur_action);
1095 /* Save the URL pattern */
1096 if (create_url_spec(perm->url, buf))
1099 log_error(LOG_LEVEL_FATAL,
1100 "can't load actions file '%s': cannot create URL pattern from: %s",
1101 csp->config->actions_file, buf);
1102 return 1; /* never get here */
1105 /* add it to the list */
1106 last_perm->next = perm;
1109 else if (mode == MODE_START_OF_FILE)
1111 /* oops - please have a {} line as 1st line in file. */
1113 log_error(LOG_LEVEL_FATAL,
1114 "can't load actions file '%s': first line is invalid: %s",
1115 csp->config->actions_file, buf);
1116 return 1; /* never get here */
1120 /* How did we get here? This is impossible! */
1122 log_error(LOG_LEVEL_FATAL,
1123 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1124 csp->config->actions_file, mode);
1125 return 1; /* never get here */
1131 free_action(cur_action);
1133 while (alias_list != NULL)
1135 struct action_alias * next = alias_list->next;
1136 freez((char *)alias_list->name);
1137 free_action(alias_list->action);
1142 #ifndef SPLIT_PROXY_ARGS
1143 if (!suppress_blocklists)
1145 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
1147 #endif /* ndef SPLIT_PROXY_ARGS */
1149 /* the old one is now obsolete */
1150 if (current_actions_file)
1152 current_actions_file->unloader = unload_actions_file;
1155 fs->next = files->next;
1157 current_actions_file = fs;
1161 csp->actions_list = fs;