1 const char actions_rcs[] = "$Id: actions.c,v 1.5 2001/06/03 11:03:48 oes 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.5 2001/06/03 11:03:48 oes
43 * adapted to new enlist_unique arg format
47 * introduced confdir option
49 * filters.c filtrers.h
51 * extracted-CGI relevant stuff
59 * support for new cgi mechansim
63 * functions for new list type: "map"
64 * extended enlist_unique
71 * deleted const struct interceptors
79 * added struct http_response
80 * changes struct interceptors to struct cgi_dispatcher
81 * moved HTML stuff to cgi.h
90 * Revision 1.4 2001/06/01 20:03:42 jongfoster
91 * Better memory management - current_action->strings[] now
92 * contains copies of the strings, not the original.
94 * Revision 1.3 2001/06/01 18:49:17 jongfoster
95 * Replaced "list_share" with "list" - the tiny memory gain was not
96 * worth the extra complexity.
98 * Revision 1.2 2001/05/31 21:40:00 jongfoster
99 * Removing some commented out, obsolete blocks of code.
101 * Revision 1.1 2001/05/31 21:16:46 jongfoster
102 * Moved functions to process the action list into this new file.
105 *********************************************************************/
117 #include "miscutil.h"
121 const char actions_h_rcs[] = ACTIONS_H_VERSION;
123 /* This structure is used to hold user-defined aliases */
127 struct action_spec action[1];
128 struct action_alias * next;
133 * Must declare this in this file for the above structure.
134 * FIXME: Make this static or put structure in header.
136 extern int get_actions (char *line, struct action_alias * alias_list,
137 struct action_spec *cur_action);
140 * We need the main list of options.
142 * First, we need a way to tell between boolean, string, and multi-string
143 * options. For string and multistring options, we also need to be
144 * able to tell the difference between a "+" and a "-". (For bools,
145 * the "+"/"-" information is encoded in "add" and "mask"). So we use
146 * an enumerated type (well, the preprocessor equivalent). Here are
149 #define AV_NONE 0 /* +opt -opt */
150 #define AV_ADD_STRING 1 /* +stropt{string} */
151 #define AV_REM_STRING 2 /* -stropt */
152 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
153 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt{*} */
156 * We need a structure to hold the name, flag changes,
157 * type, and string index.
162 unsigned mask; /* a bit set to "0" = remove action */
163 unsigned add; /* a bit set to "1" = add action */
164 int takes_value; /* an AV_... constant */
165 int index; /* index into strings[] or multi[] */
169 * And with those building blocks in place, here's the array.
171 static const struct action_name action_names[] =
174 * Well actually there's no data here - it's in actionlist.h
175 * This keeps it together to make it easy to change.
177 * Here's the macros used to format it:
179 #define DEFINE_ACTION_MULTI(name,index) \
180 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
181 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
182 #define DEFINE_ACTION_STRING(name,flag,index) \
183 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
184 { "-" name, ~flag, 0, AV_REM_STRING, index },
185 #define DEFINE_ACTION_BOOL(name,flag) \
186 { "+" name, ACTION_MASK_ALL, flag }, \
187 { "-" name, ~flag, 0 },
188 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
190 #include "actionlist.h"
192 #undef DEFINE_ACTION_MULTI
193 #undef DEFINE_ACTION_STRING
194 #undef DEFINE_ACTION_BOOL
195 #undef DEFINE_ACTION_ALIAS
197 { NULL, 0, 0 } /* End marker */
201 /*********************************************************************
203 * Function : merge_actions
205 * Description : Merge two actions together.
206 * Similar to "cur_action += new_action".
209 * 1 : cur_action = Current actions, to modify.
210 * 2 : new_action = Action to add.
214 *********************************************************************/
215 void merge_actions (struct action_spec *dest,
216 const struct action_spec *src)
220 dest->mask &= src->mask;
221 dest->add &= src->mask;
222 dest->add |= src->add;
224 for (i = 0; i < ACTION_STRING_COUNT; i++)
226 char * str = src->string[i];
229 freez(dest->string[i]);
230 dest->string[i] = strdup(str);
234 for (i = 0; i < ACTION_MULTI_COUNT; i++)
236 if (src->multi_remove_all[i])
238 /* Remove everything from dest */
239 destroy_list(dest->multi_remove[i]);
240 destroy_list(dest->multi_add[i]);
241 dest->multi_remove_all[i] = 1;
242 list_duplicate(dest->multi_add[i], src->multi_add[i]);
244 else if (dest->multi_remove_all[i])
247 * dest already removes everything, so we only need to worry
250 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
251 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
255 /* No "remove all"s to worry about. */
256 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
257 list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
258 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
264 /*********************************************************************
266 * Function : copy_action
268 * Description : Copy an action_specs.
269 * Similar to "cur_action = new_action".
272 * 1 : dest = Destination of copy.
273 * 2 : src = Source for copy.
277 *********************************************************************/
278 void copy_action (struct action_spec *dest,
279 const struct action_spec *src)
283 dest->mask = src->mask;
284 dest->add = src->add;
286 for (i = 0; i < ACTION_STRING_COUNT; i++)
288 char * str = src->string[i];
289 dest->string[i] = (str ? strdup(str) : NULL);
292 for (i = 0; i < ACTION_MULTI_COUNT; i++)
294 dest->multi_remove_all[i] = src->multi_remove_all[i];
295 list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
296 list_duplicate(dest->multi_add[i], src->multi_add[i]);
301 /*********************************************************************
303 * Function : free_action
305 * Description : Free an action_specs.
308 * 1 : src = Source to free.
312 *********************************************************************/
313 void free_action (struct action_spec *src)
317 for (i = 0; i < ACTION_STRING_COUNT; i++)
319 freez(src->string[i]);
322 for (i = 0; i < ACTION_MULTI_COUNT; i++)
324 destroy_list(src->multi_remove[i]);
325 destroy_list(src->multi_add[i]);
328 memset(src, '\0', sizeof(*src));
332 /*********************************************************************
334 * Function : get_action_token
336 * Description : Parses a line for the first action.
337 * Modifies it's input array, doesn't allocate memory.
339 * *line=" +abc{def} -ghi "
346 * 1 : line = [in] The line containing the action.
347 * [out] Start of next action on line, or
348 * NULL if we reached the end of line before
349 * we found an action.
350 * 2 : name = [out] Start of action name, null
351 * terminated. NULL on EOL
352 * 3 : value = [out] Start of action value, null
353 * terminated. NULL if none or EOL.
356 * nonzero => Mismatched {} (line was trashed anyway)
358 *********************************************************************/
359 int get_action_token(char **line, char **name, char **value)
364 /* set default returns */
369 /* Eat any leading whitespace */
370 while ((*str == ' ') || (*str == '\t'))
382 /* null name, just value is prohibited */
389 while (((ch = *str) != '\0') &&
390 (ch != ' ') && (ch != '\t') && (ch != '{'))
406 /* EOL - be careful not to run off buffer */
411 /* More to parse next time. */
420 str = strchr(str, '}');
438 /*********************************************************************
440 * Function : get_actions
442 * Description : Parses a list of actions.
445 * 1 : line = The string containing the actions.
446 * Will be written to by this function.
447 * 2 : aliases = Custom alias list, or NULL for none.
448 * 3 : cur_action = Where to store the action. Caller
452 * nonzero => Error (line was trashed anyway)
454 *********************************************************************/
455 int get_actions(char *line, struct action_alias * alias_list,
456 struct action_spec *cur_action)
458 memset(cur_action, '\0', sizeof(*cur_action));
459 cur_action->mask = ACTION_MASK_ALL;
463 char * option = NULL;
466 if (get_action_token(&line, &option, &value))
473 /* handle option in 'option' */
475 /* Check for standard action name */
476 const struct action_name * action = action_names;
478 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
482 if (action->name != NULL)
485 cur_action->mask &= action->mask;
486 cur_action->add &= action->mask;
487 cur_action->add |= action->add;
489 switch (action->takes_value)
492 /* ignore any option. */
496 /* add single string. */
498 if ((value == NULL) || (*value == '\0'))
502 /* FIXME: should validate option string here */
503 freez (cur_action->string[action->index]);
504 cur_action->string[action->index] = strdup(value);
509 /* remove single string. */
511 freez (cur_action->string[action->index]);
516 /* append multi string. */
518 struct list * remove = cur_action->multi_remove[action->index];
519 struct list * add = cur_action->multi_add[action->index];
521 if ((value == NULL) || (*value == '\0'))
526 list_remove_item(remove, value);
527 enlist_unique(add, value, 0);
532 /* remove multi string. */
534 struct list * remove = cur_action->multi_remove[action->index];
535 struct list * add = cur_action->multi_add[action->index];
537 if ( (value == NULL) || (*value == '\0')
538 || ((*value == '*') && (value[1] == '\0')) )
541 * no option, or option == "*".
545 destroy_list(remove);
547 cur_action->multi_remove_all[action->index] = 1;
551 /* Valid option - remove only 1 option */
553 if ( !cur_action->multi_remove_all[action->index] )
555 /* there isn't a catch-all in the remove list already */
556 enlist_unique(remove, value, 0);
558 list_remove_item(add, value);
563 /* Shouldn't get here unless there's memory corruption. */
569 /* try user aliases. */
570 const struct action_alias * alias = alias_list;
572 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
579 merge_actions(cur_action, alias->action);
583 /* Bad action name */
594 /*********************************************************************
596 * Function : actions_to_text
598 * Description : Converts a actionsfile entry from numeric form
599 * ("mask" and "add") to text.
602 * 1 : mask = As from struct url_actions
603 * 2 : add = As from struct url_actions
605 * Returns : A string. Caller must free it.
607 *********************************************************************/
608 char * actions_to_text(struct action_spec *action)
610 unsigned mask = action->mask;
611 unsigned add = action->add;
612 char * result = strdup("");
615 /* sanity - prevents "-feature +feature" */
619 #define DEFINE_ACTION_BOOL(__name, __bit) \
620 if (!(mask & __bit)) \
622 result = strsav(result, " -" __name); \
624 else if (add & __bit) \
626 result = strsav(result, " +" __name); \
629 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
630 if (!(mask & __bit)) \
632 result = strsav(result, " -" __name); \
634 else if (add & __bit) \
636 result = strsav(result, " +" __name "{"); \
637 result = strsav(result, action->string[__index]); \
638 result = strsav(result, "}"); \
641 #define DEFINE_ACTION_MULTI(__name, __index) \
642 if (action->multi_remove_all[__index]) \
644 result = strsav(result, " -" __name "{*}"); \
648 lst = action->multi_remove[__index]->next; \
651 result = strsav(result, " -" __name "{"); \
652 result = strsav(result, lst->str); \
653 result = strsav(result, "}"); \
657 lst = action->multi_add[__index]->next; \
660 result = strsav(result, " +" __name "{"); \
661 result = strsav(result, lst->str); \
662 result = strsav(result, "}"); \
666 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
668 #include "actionlist.h"
670 #undef DEFINE_ACTION_MULTI
671 #undef DEFINE_ACTION_STRING
672 #undef DEFINE_ACTION_BOOL
673 #undef DEFINE_ACTION_ALIAS
679 /*********************************************************************
681 * Function : current_actions_to_text
683 * Description : Converts a actionsfile entry to text.
686 * 1 : action = Action
688 * Returns : A string. Caller must free it.
690 *********************************************************************/
691 char * current_action_to_text(struct current_action_spec *action)
693 unsigned flags = action->flags;
694 char * result = strdup("");
697 #define DEFINE_ACTION_BOOL(__name, __bit) \
700 result = strsav(result, " +" __name); \
704 result = strsav(result, " -" __name); \
707 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
710 result = strsav(result, " +" __name "{"); \
711 result = strsav(result, action->string[__index]); \
712 result = strsav(result, "}"); \
716 result = strsav(result, " -" __name); \
719 #define DEFINE_ACTION_MULTI(__name, __index) \
720 lst = action->multi[__index]->next; \
723 result = strsav(result, " -" __name); \
729 result = strsav(result, " +" __name "{"); \
730 result = strsav(result, lst->str); \
731 result = strsav(result, "}"); \
736 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
738 #include "actionlist.h"
740 #undef DEFINE_ACTION_MULTI
741 #undef DEFINE_ACTION_STRING
742 #undef DEFINE_ACTION_BOOL
743 #undef DEFINE_ACTION_ALIAS
749 /*********************************************************************
751 * Function : init_current_action
753 * Description : Zero out an action.
756 * 1 : dest = An uninitialized current_action_spec.
760 *********************************************************************/
761 void init_current_action (struct current_action_spec *dest)
763 memset(dest, '\0', sizeof(*dest));
764 dest->flags = ACTION_MOST_COMPATIBLE;
768 /*********************************************************************
770 * Function : merge_current_action
772 * Description : Merge two actions together.
773 * Similar to "dest += src".
774 * Differences between this and merge_actions()
775 * is that this one doesn't allocate memory for
776 * strings (so "src" better be in memory for at least
777 * as long as "dest" is, and you'd better free
778 * "dest" using "current_free_action").
779 * Also, there is no mask or remove lists in dest.
780 * (If we're applying it to a URL, we don't need them)
783 * 1 : dest = Current actions, to modify.
784 * 2 : src = Action to add.
788 *********************************************************************/
789 void merge_current_action (struct current_action_spec *dest,
790 const struct action_spec *src)
794 dest->flags &= src->mask;
795 dest->flags |= src->add;
797 for (i = 0; i < ACTION_STRING_COUNT; i++)
799 char * str = src->string[i];
802 freez(dest->string[i]);
803 dest->string[i] = strdup(str);
807 for (i = 0; i < ACTION_MULTI_COUNT; i++)
809 if (src->multi_remove_all[i])
811 /* Remove everything from dest */
812 destroy_list(dest->multi[i]);
813 list_duplicate(dest->multi[i], src->multi_add[i]);
817 list_remove_list(dest->multi[i], src->multi_remove[i]);
818 list_append_list_unique(dest->multi[i], src->multi_add[i]);
824 /*********************************************************************
826 * Function : free_current_action
828 * Description : Free a current_action_spec.
831 * 1 : src = Source to free.
835 *********************************************************************/
836 void free_current_action (struct current_action_spec *src)
840 for (i = 0; i < ACTION_STRING_COUNT; i++)
842 freez(src->string[i]);
845 for (i = 0; i < ACTION_MULTI_COUNT; i++)
847 destroy_list(src->multi[i]);
850 memset(src, '\0', sizeof(*src));
854 /*********************************************************************
856 * Function : unload_actions_file
858 * Description : Unloads an actions module.
861 * 1 : file_data = the data structure associated with the
866 *********************************************************************/
867 void unload_actions_file(void *file_data)
869 struct url_actions * next;
870 struct url_actions * cur = (struct url_actions *)file_data;
882 /*********************************************************************
884 * Function : load_actions_file
886 * Description : Read and parse a action file and add to files
890 * 1 : csp = Current client state (buffers, headers, etc...)
892 * Returns : 0 => Ok, everything else is an error.
894 *********************************************************************/
895 int load_actions_file(struct client_state *csp)
897 static struct file_list *current_actions_file = NULL;
901 struct url_actions *last_perm;
902 struct url_actions *perm;
904 struct file_list *fs;
905 #define MODE_START_OF_FILE 1
906 #define MODE_ACTIONS 2
908 int mode = MODE_START_OF_FILE;
909 struct action_spec cur_action[1];
910 struct action_alias * alias_list = NULL;
912 memset(cur_action, '\0', sizeof(*cur_action));
914 if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
916 /* No need to load */
919 csp->actions_list = current_actions_file;
925 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
926 csp->config->actions_file);
927 return 1; /* never get here */
930 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
931 if (last_perm == NULL)
933 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
934 csp->config->actions_file);
935 return 1; /* never get here */
938 if ((fp = fopen(csp->config->actions_file, "r")) == NULL)
940 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
941 csp->config->actions_file);
942 return 1; /* never get here */
945 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
949 /* It's a header block */
952 /* It's {{settings}} or {{alias}} */
953 int len = strlen(buf);
954 char * start = buf + 2;
955 char * end = buf + len - 1;
956 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
960 log_error(LOG_LEVEL_FATAL,
961 "can't load actions file '%s': invalid line: %s",
962 csp->config->actions_file, buf);
963 return 1; /* never get here */
966 /* Trim leading and trailing whitespace. */
974 log_error(LOG_LEVEL_FATAL,
975 "can't load actions file '%s': invalid line: {{ }}",
976 csp->config->actions_file);
977 return 1; /* never get here */
980 if (0 == strcmpic(start, "alias"))
982 /* it's an {{alias}} block */
988 /* invalid {{something}} block */
990 log_error(LOG_LEVEL_FATAL,
991 "can't load actions file '%s': invalid line: {{%s}}",
992 csp->config->actions_file, start);
993 return 1; /* never get here */
998 /* It's an actions block */
1000 char actions_buf[BUFSIZ];
1004 mode = MODE_ACTIONS;
1006 /* free old action */
1007 free_action(cur_action);
1010 strcpy(actions_buf, buf + 1);
1012 /* check we have a trailing } and then trim it */
1013 end = actions_buf + strlen(actions_buf) - 1;
1018 log_error(LOG_LEVEL_FATAL,
1019 "can't load actions file '%s': invalid line: %s",
1020 csp->config->actions_file, buf);
1021 return 1; /* never get here */
1025 /* trim any whitespace immediately inside {} */
1028 if (*actions_buf == '\0')
1032 log_error(LOG_LEVEL_FATAL,
1033 "can't load actions file '%s': invalid line: %s",
1034 csp->config->actions_file, buf);
1035 return 1; /* never get here */
1038 if (get_actions(actions_buf, alias_list, cur_action))
1042 log_error(LOG_LEVEL_FATAL,
1043 "can't load actions file '%s': invalid line: %s",
1044 csp->config->actions_file, buf);
1045 return 1; /* never get here */
1049 else if (mode == MODE_ALIAS)
1051 /* define an alias */
1052 char actions_buf[BUFSIZ];
1053 struct action_alias * new_alias;
1056 char * start = strchr(buf, '=');
1059 if ((start == NULL) || (start == buf))
1061 log_error(LOG_LEVEL_FATAL,
1062 "can't load actions file '%s': invalid alias line: %s",
1063 csp->config->actions_file, buf);
1064 return 1; /* never get here */
1067 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1070 log_error(LOG_LEVEL_FATAL,
1071 "can't load actions file '%s': out of memory!",
1072 csp->config->actions_file);
1073 return 1; /* never get here */
1076 /* Eat any the whitespace before the '=' */
1078 while ((*end == ' ') || (*end == '\t'))
1081 * we already know we must have at least 1 non-ws char
1082 * at start of buf - no need to check
1088 /* Eat any the whitespace after the '=' */
1090 while ((*start == ' ') || (*start == '\t'))
1096 log_error(LOG_LEVEL_FATAL,
1097 "can't load actions file '%s': invalid alias line: %s",
1098 csp->config->actions_file, buf);
1099 return 1; /* never get here */
1102 new_alias->name = strdup(buf);
1104 strcpy(actions_buf, start);
1106 if (get_actions(actions_buf, alias_list, new_alias->action))
1110 log_error(LOG_LEVEL_FATAL,
1111 "can't load actions file '%s': invalid alias line: %s = %s",
1112 csp->config->actions_file, buf, start);
1113 return 1; /* never get here */
1117 new_alias->next = alias_list;
1118 alias_list = new_alias;
1120 else if (mode == MODE_ACTIONS)
1122 /* it's a URL pattern */
1124 /* allocate a new node */
1125 if ((perm = zalloc(sizeof(*perm))) == NULL)
1128 log_error(LOG_LEVEL_FATAL,
1129 "can't load actions file '%s': out of memory!",
1130 csp->config->actions_file);
1131 return 1; /* never get here */
1135 copy_action (perm->action, cur_action);
1137 /* Save the URL pattern */
1138 if (create_url_spec(perm->url, buf))
1141 log_error(LOG_LEVEL_FATAL,
1142 "can't load actions file '%s': cannot create URL pattern from: %s",
1143 csp->config->actions_file, buf);
1144 return 1; /* never get here */
1147 /* add it to the list */
1148 last_perm->next = perm;
1151 else if (mode == MODE_START_OF_FILE)
1153 /* oops - please have a {} line as 1st line in file. */
1155 log_error(LOG_LEVEL_FATAL,
1156 "can't load actions file '%s': first line is invalid: %s",
1157 csp->config->actions_file, buf);
1158 return 1; /* never get here */
1162 /* How did we get here? This is impossible! */
1164 log_error(LOG_LEVEL_FATAL,
1165 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1166 csp->config->actions_file, mode);
1167 return 1; /* never get here */
1173 free_action(cur_action);
1175 while (alias_list != NULL)
1177 struct action_alias * next = alias_list->next;
1178 freez((char *)alias_list->name);
1179 free_action(alias_list->action);
1184 #ifndef SPLIT_PROXY_ARGS
1185 if (!suppress_blocklists)
1187 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
1189 #endif /* ndef SPLIT_PROXY_ARGS */
1191 /* the old one is now obsolete */
1192 if (current_actions_file)
1194 current_actions_file->unloader = unload_actions_file;
1197 fs->next = files->next;
1199 current_actions_file = fs;
1203 csp->actions_list = fs;