1 const char actions_rcs[] = "$Id: actions.c,v 1.3 2001/06/01 18:49:17 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.3 2001/06/01 18:49:17 jongfoster
37 * Replaced "list_share" with "list" - the tiny memory gain was not
38 * worth the extra complexity.
40 * Revision 1.2 2001/05/31 21:40:00 jongfoster
41 * Removing some commented out, obsolete blocks of code.
43 * Revision 1.1 2001/05/31 21:16:46 jongfoster
44 * Moved functions to process the action list into this new file.
47 *********************************************************************/
63 const char actions_h_rcs[] = ACTIONS_H_VERSION;
65 /* This structure is used to hold user-defined aliases */
69 struct action_spec action[1];
70 struct action_alias * next;
75 * Must declare this in this file for the above structure.
76 * FIXME: Make this static or put structure in header.
78 extern int get_actions (char *line, struct action_alias * alias_list,
79 struct action_spec *cur_action);
82 * We need the main list of options.
84 * First, we need a way to tell between boolean, string, and multi-string
85 * options. For string and multistring options, we also need to be
86 * able to tell the difference between a "+" and a "-". (For bools,
87 * the "+"/"-" information is encoded in "add" and "mask"). So we use
88 * an enumerated type (well, the preprocessor equivalent). Here are
91 #define AV_NONE 0 /* +opt -opt */
92 #define AV_ADD_STRING 1 /* +stropt{string} */
93 #define AV_REM_STRING 2 /* -stropt */
94 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
95 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt{*} */
98 * We need a structure to hold the name, flag changes,
99 * type, and string index.
104 unsigned mask; /* a bit set to "0" = remove action */
105 unsigned add; /* a bit set to "1" = add action */
106 int takes_value; /* an AV_... constant */
107 int index; /* index into strings[] or multi[] */
111 * And with those building blocks in place, here's the array.
113 static const struct action_name action_names[] =
116 * Well actually there's no data here - it's in actionlist.h
117 * This keeps it together to make it easy to change.
119 * Here's the macros used to format it:
121 #define DEFINE_ACTION_MULTI(name,index) \
122 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
123 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
124 #define DEFINE_ACTION_STRING(name,flag,index) \
125 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
126 { "-" name, ~flag, 0, AV_REM_STRING, index },
127 #define DEFINE_ACTION_BOOL(name,flag) \
128 { "+" name, ACTION_MASK_ALL, flag }, \
129 { "-" name, ~flag, 0 },
130 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
132 #include "actionlist.h"
134 #undef DEFINE_ACTION_MULTI
135 #undef DEFINE_ACTION_STRING
136 #undef DEFINE_ACTION_BOOL
137 #undef DEFINE_ACTION_ALIAS
139 { NULL, 0, 0 } /* End marker */
143 /*********************************************************************
145 * Function : merge_actions
147 * Description : Merge two actions together.
148 * Similar to "cur_action += new_action".
151 * 1 : cur_action = Current actions, to modify.
152 * 2 : new_action = Action to add.
156 *********************************************************************/
157 void merge_actions (struct action_spec *dest,
158 const struct action_spec *src)
162 dest->mask &= src->mask;
163 dest->add &= src->mask;
164 dest->add |= src->add;
166 for (i = 0; i < ACTION_STRING_COUNT; i++)
168 char * str = src->string[i];
171 freez(dest->string[i]);
172 dest->string[i] = strdup(str);
176 for (i = 0; i < ACTION_MULTI_COUNT; i++)
178 if (src->multi_remove_all[i])
180 /* Remove everything from dest */
181 destroy_list(dest->multi_remove[i]);
182 destroy_list(dest->multi_add[i]);
183 dest->multi_remove_all[i] = 1;
184 list_duplicate(dest->multi_add[i], src->multi_add[i]);
186 else if (dest->multi_remove_all[i])
189 * dest already removes everything, so we only need to worry
192 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
193 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
197 /* No "remove all"s to worry about. */
198 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
199 list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
200 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
206 /*********************************************************************
208 * Function : copy_action
210 * Description : Copy an action_specs.
211 * Similar to "cur_action = new_action".
214 * 1 : dest = Destination of copy.
215 * 2 : src = Source for copy.
219 *********************************************************************/
220 void copy_action (struct action_spec *dest,
221 const struct action_spec *src)
225 dest->mask = src->mask;
226 dest->add = src->add;
228 for (i = 0; i < ACTION_STRING_COUNT; i++)
230 char * str = src->string[i];
231 dest->string[i] = (str ? strdup(str) : NULL);
234 for (i = 0; i < ACTION_MULTI_COUNT; i++)
236 dest->multi_remove_all[i] = src->multi_remove_all[i];
237 list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
238 list_duplicate(dest->multi_add[i], src->multi_add[i]);
243 /*********************************************************************
245 * Function : free_action
247 * Description : Free an action_specs.
250 * 1 : src = Source to free.
254 *********************************************************************/
255 void free_action (struct action_spec *src)
259 for (i = 0; i < ACTION_STRING_COUNT; i++)
261 freez(src->string[i]);
264 for (i = 0; i < ACTION_MULTI_COUNT; i++)
266 destroy_list(src->multi_remove[i]);
267 destroy_list(src->multi_add[i]);
270 memset(src, '\0', sizeof(*src));
274 /*********************************************************************
276 * Function : get_action_token
278 * Description : Parses a line for the first action.
279 * Modifies it's input array, doesn't allocate memory.
281 * *line=" +abc{def} -ghi "
288 * 1 : line = [in] The line containing the action.
289 * [out] Start of next action on line, or
290 * NULL if we reached the end of line before
291 * we found an action.
292 * 2 : name = [out] Start of action name, null
293 * terminated. NULL on EOL
294 * 3 : value = [out] Start of action value, null
295 * terminated. NULL if none or EOL.
298 * nonzero => Mismatched {} (line was trashed anyway)
300 *********************************************************************/
301 int get_action_token(char **line, char **name, char **value)
306 /* set default returns */
311 /* Eat any leading whitespace */
312 while ((*str == ' ') || (*str == '\t'))
324 /* null name, just value is prohibited */
331 while (((ch = *str) != '\0') &&
332 (ch != ' ') && (ch != '\t') && (ch != '{'))
348 /* EOL - be careful not to run off buffer */
353 /* More to parse next time. */
362 str = strchr(str, '}');
380 /*********************************************************************
382 * Function : get_actions
384 * Description : Parses a list of actions.
387 * 1 : line = The string containing the actions.
388 * Will be written to by this function.
389 * 2 : aliases = Custom alias list, or NULL for none.
390 * 3 : cur_action = Where to store the action. Caller
394 * nonzero => Error (line was trashed anyway)
396 *********************************************************************/
397 int get_actions(char *line, struct action_alias * alias_list,
398 struct action_spec *cur_action)
400 memset(cur_action, '\0', sizeof(*cur_action));
401 cur_action->mask = ACTION_MASK_ALL;
405 char * option = NULL;
408 if (get_action_token(&line, &option, &value))
415 /* handle option in 'option' */
417 /* Check for standard action name */
418 const struct action_name * action = action_names;
420 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
424 if (action->name != NULL)
427 cur_action->mask &= action->mask;
428 cur_action->add &= action->mask;
429 cur_action->add |= action->add;
431 switch (action->takes_value)
434 /* ignore any option. */
438 /* add single string. */
440 if ((value == NULL) || (*value == '\0'))
444 /* FIXME: should validate option string here */
445 freez (cur_action->string[action->index]);
446 cur_action->string[action->index] = strdup(value);
451 /* remove single string. */
453 freez (cur_action->string[action->index]);
458 /* append multi string. */
460 struct list * remove = cur_action->multi_remove[action->index];
461 struct list * add = cur_action->multi_add[action->index];
463 if ((value == NULL) || (*value == '\0'))
468 list_remove_item(remove, value);
469 enlist_unique(add, value);
474 /* remove 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')
480 || ((*value == '*') && (value[1] == '\0')) )
483 * no option, or option == "*".
487 destroy_list(remove);
489 cur_action->multi_remove_all[action->index] = 1;
493 /* Valid option - remove only 1 option */
495 if ( !cur_action->multi_remove_all[action->index] )
497 /* there isn't a catch-all in the remove list already */
498 enlist_unique(remove, value);
500 list_remove_item(add, value);
505 /* Shouldn't get here unless there's memory corruption. */
511 /* try user aliases. */
512 const struct action_alias * alias = alias_list;
514 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
521 merge_actions(cur_action, alias->action);
525 /* Bad action name */
536 /*********************************************************************
538 * Function : actions_to_text
540 * Description : Converts a actionsfile entry from numeric form
541 * ("mask" and "add") to text.
544 * 1 : mask = As from struct url_actions
545 * 2 : add = As from struct url_actions
547 * Returns : A string. Caller must free it.
549 *********************************************************************/
550 char * actions_to_text(struct action_spec *action)
552 unsigned mask = action->mask;
553 unsigned add = action->add;
554 char * result = strdup("");
557 /* sanity - prevents "-feature +feature" */
561 #define DEFINE_ACTION_BOOL(__name, __bit) \
562 if (!(mask & __bit)) \
564 result = strsav(result, " -" __name); \
566 else if (add & __bit) \
568 result = strsav(result, " +" __name); \
571 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
572 if (!(mask & __bit)) \
574 result = strsav(result, " -" __name); \
576 else if (add & __bit) \
578 result = strsav(result, " +" __name "{"); \
579 result = strsav(result, action->string[__index]); \
580 result = strsav(result, "}"); \
583 #define DEFINE_ACTION_MULTI(__name, __index) \
584 if (action->multi_remove_all[__index]) \
586 result = strsav(result, " -" __name "{*}"); \
590 lst = action->multi_remove[__index]->next; \
593 result = strsav(result, " -" __name "{"); \
594 result = strsav(result, lst->str); \
595 result = strsav(result, "}"); \
599 lst = action->multi_add[__index]->next; \
602 result = strsav(result, " +" __name "{"); \
603 result = strsav(result, lst->str); \
604 result = strsav(result, "}"); \
608 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
610 #include "actionlist.h"
612 #undef DEFINE_ACTION_MULTI
613 #undef DEFINE_ACTION_STRING
614 #undef DEFINE_ACTION_BOOL
615 #undef DEFINE_ACTION_ALIAS
621 /*********************************************************************
623 * Function : current_actions_to_text
625 * Description : Converts a actionsfile entry to text.
628 * 1 : action = Action
630 * Returns : A string. Caller must free it.
632 *********************************************************************/
633 char * current_action_to_text(struct current_action_spec *action)
635 unsigned flags = action->flags;
636 char * result = strdup("");
639 #define DEFINE_ACTION_BOOL(__name, __bit) \
642 result = strsav(result, " +" __name); \
646 result = strsav(result, " -" __name); \
649 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
652 result = strsav(result, " +" __name "{"); \
653 result = strsav(result, action->string[__index]); \
654 result = strsav(result, "}"); \
658 result = strsav(result, " -" __name); \
661 #define DEFINE_ACTION_MULTI(__name, __index) \
662 lst = action->multi[__index]->next; \
665 result = strsav(result, " -" __name); \
671 result = strsav(result, " +" __name "{"); \
672 result = strsav(result, lst->str); \
673 result = strsav(result, "}"); \
678 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
680 #include "actionlist.h"
682 #undef DEFINE_ACTION_MULTI
683 #undef DEFINE_ACTION_STRING
684 #undef DEFINE_ACTION_BOOL
685 #undef DEFINE_ACTION_ALIAS
691 /*********************************************************************
693 * Function : init_current_action
695 * Description : Zero out an action.
698 * 1 : dest = An uninitialized current_action_spec.
702 *********************************************************************/
703 void init_current_action (struct current_action_spec *dest)
705 memset(dest, '\0', sizeof(*dest));
706 dest->flags = ACTION_MOST_COMPATIBLE;
710 /*********************************************************************
712 * Function : merge_current_action
714 * Description : Merge two actions together.
715 * Similar to "dest += src".
716 * Differences between this and merge_actions()
717 * is that this one doesn't allocate memory for
718 * strings (so "src" better be in memory for at least
719 * as long as "dest" is, and you'd better free
720 * "dest" using "current_free_action").
721 * Also, there is no mask or remove lists in dest.
722 * (If we're applying it to a URL, we don't need them)
725 * 1 : dest = Current actions, to modify.
726 * 2 : src = Action to add.
730 *********************************************************************/
731 void merge_current_action (struct current_action_spec *dest,
732 const struct action_spec *src)
736 dest->flags &= src->mask;
737 dest->flags |= src->add;
739 for (i = 0; i < ACTION_STRING_COUNT; i++)
741 char * str = src->string[i];
744 freez(dest->string[i]);
745 dest->string[i] = strdup(str);
749 for (i = 0; i < ACTION_MULTI_COUNT; i++)
751 if (src->multi_remove_all[i])
753 /* Remove everything from dest */
754 destroy_list(dest->multi[i]);
755 list_duplicate(dest->multi[i], src->multi_add[i]);
759 list_remove_list(dest->multi[i], src->multi_remove[i]);
760 list_append_list_unique(dest->multi[i], src->multi_add[i]);
766 /*********************************************************************
768 * Function : free_current_action
770 * Description : Free a current_action_spec.
773 * 1 : src = Source to free.
777 *********************************************************************/
778 void free_current_action (struct current_action_spec *src)
782 for (i = 0; i < ACTION_STRING_COUNT; i++)
784 freez(src->string[i]);
787 for (i = 0; i < ACTION_MULTI_COUNT; i++)
789 destroy_list(src->multi[i]);
792 memset(src, '\0', sizeof(*src));
796 /*********************************************************************
798 * Function : unload_actions_file
800 * Description : Unloads an actions module.
803 * 1 : file_data = the data structure associated with the
808 *********************************************************************/
809 void unload_actions_file(void *file_data)
811 struct url_actions * next;
812 struct url_actions * cur = (struct url_actions *)file_data;
824 /*********************************************************************
826 * Function : load_actions_file
828 * Description : Read and parse a action file and add to files
832 * 1 : csp = Current client state (buffers, headers, etc...)
834 * Returns : 0 => Ok, everything else is an error.
836 *********************************************************************/
837 int load_actions_file(struct client_state *csp)
839 static struct file_list *current_actions_file = NULL;
843 struct url_actions *last_perm;
844 struct url_actions *perm;
846 struct file_list *fs;
847 #define MODE_START_OF_FILE 1
848 #define MODE_ACTIONS 2
850 int mode = MODE_START_OF_FILE;
851 struct action_spec cur_action[1];
852 struct action_alias * alias_list = NULL;
854 memset(cur_action, '\0', sizeof(*cur_action));
856 if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
858 /* No need to load */
861 csp->actions_list = current_actions_file;
867 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
868 csp->config->actions_file);
869 return 1; /* never get here */
872 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
873 if (last_perm == NULL)
875 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
876 csp->config->actions_file);
877 return 1; /* never get here */
880 if ((fp = fopen(csp->config->actions_file, "r")) == NULL)
882 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
883 csp->config->actions_file);
884 return 1; /* never get here */
887 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
891 /* It's a header block */
894 /* It's {{settings}} or {{alias}} */
895 int len = strlen(buf);
896 char * start = buf + 2;
897 char * end = buf + len - 1;
898 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
902 log_error(LOG_LEVEL_FATAL,
903 "can't load actions file '%s': invalid line: %s",
904 csp->config->actions_file, buf);
905 return 1; /* never get here */
908 /* Trim leading and trailing whitespace. */
916 log_error(LOG_LEVEL_FATAL,
917 "can't load actions file '%s': invalid line: {{ }}",
918 csp->config->actions_file);
919 return 1; /* never get here */
922 if (0 == strcmpic(start, "alias"))
924 /* it's an {{alias}} block */
930 /* invalid {{something}} block */
932 log_error(LOG_LEVEL_FATAL,
933 "can't load actions file '%s': invalid line: {{%s}}",
934 csp->config->actions_file, start);
935 return 1; /* never get here */
940 /* It's an actions block */
942 char actions_buf[BUFSIZ];
948 /* free old action */
949 free_action(cur_action);
952 strcpy(actions_buf, buf + 1);
954 /* check we have a trailing } and then trim it */
955 end = actions_buf + strlen(actions_buf) - 1;
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 */
967 /* trim any whitespace immediately inside {} */
970 if (*actions_buf == '\0')
974 log_error(LOG_LEVEL_FATAL,
975 "can't load actions file '%s': invalid line: %s",
976 csp->config->actions_file, buf);
977 return 1; /* never get here */
980 if (get_actions(actions_buf, alias_list, cur_action))
984 log_error(LOG_LEVEL_FATAL,
985 "can't load actions file '%s': invalid line: %s",
986 csp->config->actions_file, buf);
987 return 1; /* never get here */
991 else if (mode == MODE_ALIAS)
993 /* define an alias */
994 char actions_buf[BUFSIZ];
995 struct action_alias * new_alias;
998 char * start = strchr(buf, '=');
1001 if ((start == NULL) || (start == buf))
1003 log_error(LOG_LEVEL_FATAL,
1004 "can't load actions file '%s': invalid alias line: %s",
1005 csp->config->actions_file, buf);
1006 return 1; /* never get here */
1009 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1012 log_error(LOG_LEVEL_FATAL,
1013 "can't load actions file '%s': out of memory!",
1014 csp->config->actions_file);
1015 return 1; /* never get here */
1018 /* Eat any the whitespace before the '=' */
1020 while ((*end == ' ') || (*end == '\t'))
1023 * we already know we must have at least 1 non-ws char
1024 * at start of buf - no need to check
1030 /* Eat any the whitespace after the '=' */
1032 while ((*start == ' ') || (*start == '\t'))
1038 log_error(LOG_LEVEL_FATAL,
1039 "can't load actions file '%s': invalid alias line: %s",
1040 csp->config->actions_file, buf);
1041 return 1; /* never get here */
1044 new_alias->name = strdup(buf);
1046 strcpy(actions_buf, start);
1048 if (get_actions(actions_buf, alias_list, new_alias->action))
1052 log_error(LOG_LEVEL_FATAL,
1053 "can't load actions file '%s': invalid alias line: %s = %s",
1054 csp->config->actions_file, buf, start);
1055 return 1; /* never get here */
1059 new_alias->next = alias_list;
1060 alias_list = new_alias;
1062 else if (mode == MODE_ACTIONS)
1064 /* it's a URL pattern */
1066 /* allocate a new node */
1067 if ((perm = zalloc(sizeof(*perm))) == 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 */
1077 copy_action (perm->action, cur_action);
1079 /* Save the URL pattern */
1080 if (create_url_spec(perm->url, buf))
1083 log_error(LOG_LEVEL_FATAL,
1084 "can't load actions file '%s': cannot create URL pattern from: %s",
1085 csp->config->actions_file, buf);
1086 return 1; /* never get here */
1089 /* add it to the list */
1090 last_perm->next = perm;
1093 else if (mode == MODE_START_OF_FILE)
1095 /* oops - please have a {} line as 1st line in file. */
1097 log_error(LOG_LEVEL_FATAL,
1098 "can't load actions file '%s': first line is invalid: %s",
1099 csp->config->actions_file, buf);
1100 return 1; /* never get here */
1104 /* How did we get here? This is impossible! */
1106 log_error(LOG_LEVEL_FATAL,
1107 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1108 csp->config->actions_file, mode);
1109 return 1; /* never get here */
1115 free_action(cur_action);
1117 while (alias_list != NULL)
1119 struct action_alias * next = alias_list->next;
1120 freez((char *)alias_list->name);
1121 free_action(alias_list->action);
1126 #ifndef SPLIT_PROXY_ARGS
1127 if (!suppress_blocklists)
1129 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
1131 #endif /* ndef SPLIT_PROXY_ARGS */
1133 /* the old one is now obsolete */
1134 if (current_actions_file)
1136 current_actions_file->unloader = unload_actions_file;
1139 fs->next = files->next;
1141 current_actions_file = fs;
1145 csp->actions_list = fs;