1 const char actions_rcs[] = "$Id: actions.c,v 1.97 2016/05/03 13:20:37 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/actions.c,v $
6 * Purpose : Declares functions to work with actions files
8 * Copyright : Written by and Copyright (C) 2001-2016 the
9 * Privoxy team. http://www.privoxy.org/
11 * Based on the Internet Junkbuster originally written
12 * by and Copyright (C) 1997 Anonymous Coders and
13 * Junkbusters Corporation. http://www.junkbusters.com
15 * This program is free software; you can redistribute it
16 * and/or modify it under the terms of the GNU General
17 * Public License as published by the Free Software
18 * Foundation; either version 2 of the License, or (at
19 * your option) any later version.
21 * This program is distributed in the hope that it will
22 * be useful, but WITHOUT ANY WARRANTY; without even the
23 * implied warranty of MERCHANTABILITY or FITNESS FOR A
24 * PARTICULAR PURPOSE. See the GNU General Public
25 * License for more details.
27 * The GNU General Public License should be included with
28 * this file. If not, you can view it at
29 * http://www.gnu.org/copyleft/gpl.html
30 * or write to the Free Software Foundation, Inc., 59
31 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 *********************************************************************/
43 #ifdef FEATURE_PTHREAD
60 const char actions_h_rcs[] = ACTIONS_H_VERSION;
64 * We need the main list of options.
66 * First, we need a way to tell between boolean, string, and multi-string
67 * options. For string and multistring options, we also need to be
68 * able to tell the difference between a "+" and a "-". (For bools,
69 * the "+"/"-" information is encoded in "add" and "mask"). So we use
70 * an enumerated type (well, the preprocessor equivalent). Here are
73 enum action_value_type {
74 AV_NONE = 0, /* +opt -opt */
75 AV_ADD_STRING = 1, /* +stropt{string} */
76 AV_REM_STRING = 2, /* -stropt */
77 AV_ADD_MULTI = 3, /* +multiopt{string} +multiopt{string2} */
78 AV_REM_MULTI = 4 /* -multiopt{string} -multiopt */
82 * We need a structure to hold the name, flag changes,
83 * type, and string index.
88 unsigned long mask; /* a bit set to "0" = remove action */
89 unsigned long add; /* a bit set to "1" = add action */
90 enum action_value_type value_type; /* an AV_... constant */
91 int index; /* index into strings[] or multi[] */
95 * And with those building blocks in place, here's the array.
97 static const struct action_name action_names[] =
100 * Well actually there's no data here - it's in actionlist.h
101 * This keeps it together to make it easy to change.
103 * Here's the macros used to format it:
105 #define DEFINE_ACTION_MULTI(name,index) \
106 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
107 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
108 #define DEFINE_ACTION_STRING(name,flag,index) \
109 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
110 { "-" name, ~flag, 0, AV_REM_STRING, index },
111 #define DEFINE_ACTION_BOOL(name,flag) \
112 { "+" name, ACTION_MASK_ALL, flag }, \
113 { "-" name, ~flag, 0 },
114 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
116 #include "actionlist.h"
118 #undef DEFINE_ACTION_MULTI
119 #undef DEFINE_ACTION_STRING
120 #undef DEFINE_ACTION_BOOL
121 #undef DEFINE_ACTION_ALIAS
123 { NULL, 0, 0 } /* End marker */
130 int load_one_actions_file(struct client_state *csp, int fileid);
133 /*********************************************************************
135 * Function : merge_actions
137 * Description : Merge two actions together.
138 * Similar to "dest += src".
141 * 1 : dest = Actions to modify.
142 * 2 : src = Action to add.
144 * Returns : JB_ERR_OK or JB_ERR_MEMORY
146 *********************************************************************/
147 jb_err merge_actions (struct action_spec *dest,
148 const struct action_spec *src)
153 dest->mask &= src->mask;
154 dest->add &= src->mask;
155 dest->add |= src->add;
157 for (i = 0; i < ACTION_STRING_COUNT; i++)
159 char * str = src->string[i];
162 freez(dest->string[i]);
163 dest->string[i] = strdup_or_die(str);
167 for (i = 0; i < ACTION_MULTI_COUNT; i++)
169 if (src->multi_remove_all[i])
171 /* Remove everything from dest */
172 list_remove_all(dest->multi_remove[i]);
173 dest->multi_remove_all[i] = 1;
175 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
177 else if (dest->multi_remove_all[i])
180 * dest already removes everything, so we only need to worry
183 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
184 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
188 /* No "remove all"s to worry about. */
189 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
190 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
191 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
204 /*********************************************************************
206 * Function : copy_action
208 * Description : Copy an action_specs.
209 * Similar to "dest = src".
212 * 1 : dest = Destination of copy.
213 * 2 : src = Source for copy.
215 * Returns : JB_ERR_OK or JB_ERR_MEMORY
217 *********************************************************************/
218 jb_err copy_action (struct action_spec *dest,
219 const struct action_spec *src)
222 jb_err err = JB_ERR_OK;
225 memset(dest, '\0', sizeof(*dest));
227 dest->mask = src->mask;
228 dest->add = src->add;
230 for (i = 0; i < ACTION_STRING_COUNT; i++)
232 char * str = src->string[i];
235 str = strdup_or_die(str);
236 dest->string[i] = str;
240 for (i = 0; i < ACTION_MULTI_COUNT; i++)
242 dest->multi_remove_all[i] = src->multi_remove_all[i];
243 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
248 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
257 /*********************************************************************
259 * Function : free_action_spec
261 * Description : Frees an action_spec and the memory used by it.
264 * 1 : src = Source to free.
268 *********************************************************************/
269 void free_action_spec(struct action_spec *src)
276 /*********************************************************************
278 * Function : free_action
280 * Description : Destroy an action_spec. Frees memory used by it,
281 * except for the memory used by the struct action_spec
285 * 1 : src = Source to free.
289 *********************************************************************/
290 void free_action (struct action_spec *src)
299 for (i = 0; i < ACTION_STRING_COUNT; i++)
301 freez(src->string[i]);
304 for (i = 0; i < ACTION_MULTI_COUNT; i++)
306 destroy_list(src->multi_remove[i]);
307 destroy_list(src->multi_add[i]);
310 memset(src, '\0', sizeof(*src));
314 /*********************************************************************
316 * Function : get_action_token
318 * Description : Parses a line for the first action.
319 * Modifies its input array, doesn't allocate memory.
321 * *line=" +abc{def} -ghi "
328 * 1 : line = [in] The line containing the action.
329 * [out] Start of next action on line, or
330 * NULL if we reached the end of line before
331 * we found an action.
332 * 2 : name = [out] Start of action name, null
333 * terminated. NULL on EOL
334 * 3 : value = [out] Start of action value, null
335 * terminated. NULL if none or EOL.
337 * Returns : JB_ERR_OK => Ok
338 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
340 *********************************************************************/
341 jb_err get_action_token(char **line, char **name, char **value)
346 /* set default returns */
351 /* Eat any leading whitespace */
352 while ((*str == ' ') || (*str == '\t'))
364 /* null name, just value is prohibited */
371 while (((ch = *str) != '\0') &&
372 (ch != ' ') && (ch != '\t') && (ch != '{'))
376 /* error, '}' without '{' */
388 /* EOL - be careful not to run off buffer */
393 /* More to parse next time. */
402 /* The value ends with the first non-escaped closing curly brace */
403 while ((str = strchr(str, '}')) != NULL)
407 /* Overwrite the '\' so the action doesn't see it. */
408 string_move(str-1, str);
429 /*********************************************************************
431 * Function : action_used_to_be_valid
433 * Description : Checks if unrecognized actions were valid in earlier
437 * 1 : action = The string containing the action to check.
439 * Returns : True if yes, otherwise false.
441 *********************************************************************/
442 static int action_used_to_be_valid(const char *action)
444 static const char * const formerly_valid_actions[] = {
447 "send-vanilla-wafer",
449 "treat-forbidden-connects-like-blocks",
455 for (i = 0; i < SZ(formerly_valid_actions); i++)
457 if (0 == strcmpic(action, formerly_valid_actions[i]))
466 /*********************************************************************
468 * Function : get_actions
470 * Description : Parses a list of actions.
473 * 1 : line = The string containing the actions.
474 * Will be written to by this function.
475 * 2 : alias_list = Custom alias list, or NULL for none.
476 * 3 : cur_action = Where to store the action. Caller
479 * Returns : JB_ERR_OK => Ok
480 * JB_ERR_PARSE => Parse error (line was trashed anyway)
481 * nonzero => Out of memory (line was trashed anyway)
483 *********************************************************************/
484 jb_err get_actions(char *line,
485 struct action_alias * alias_list,
486 struct action_spec *cur_action)
489 init_action(cur_action);
490 cur_action->mask = ACTION_MASK_ALL;
494 char * option = NULL;
497 err = get_action_token(&line, &option, &value);
505 /* handle option in 'option' */
507 /* Check for standard action name */
508 const struct action_name * action = action_names;
510 while ((action->name != NULL) && (0 != strcmpic(action->name, option)))
514 if (action->name != NULL)
517 cur_action->mask &= action->mask;
518 cur_action->add &= action->mask;
519 cur_action->add |= action->add;
521 switch (action->value_type)
526 log_error(LOG_LEVEL_ERROR,
527 "Action %s does not take parameters but %s was given.",
528 action->name, value);
534 /* add single string. */
536 if ((value == NULL) || (*value == '\0'))
538 if (0 == strcmpic(action->name, "+block"))
541 * XXX: Temporary backwards compatibility hack.
542 * XXX: should include line number.
544 value = "No reason specified.";
545 log_error(LOG_LEVEL_ERROR,
546 "block action without reason found. This may "
547 "become a fatal error in future versions.");
554 /* FIXME: should validate option string here */
555 freez (cur_action->string[action->index]);
556 cur_action->string[action->index] = strdup(value);
557 if (NULL == cur_action->string[action->index])
559 return JB_ERR_MEMORY;
565 /* remove single string. */
567 freez (cur_action->string[action->index]);
572 /* append multi string. */
574 struct list * remove_p = cur_action->multi_remove[action->index];
575 struct list * add_p = cur_action->multi_add[action->index];
577 if ((value == NULL) || (*value == '\0'))
582 list_remove_item(remove_p, value);
583 err = enlist_unique(add_p, value, 0);
592 /* remove multi string. */
594 struct list * remove_p = cur_action->multi_remove[action->index];
595 struct list * add_p = cur_action->multi_add[action->index];
597 if ((value == NULL) || (*value == '\0')
598 || ((*value == '*') && (value[1] == '\0')))
601 * no option, or option == "*".
605 list_remove_all(remove_p);
606 list_remove_all(add_p);
607 cur_action->multi_remove_all[action->index] = 1;
611 /* Valid option - remove only 1 option */
613 if (!cur_action->multi_remove_all[action->index])
615 /* there isn't a catch-all in the remove list already */
616 err = enlist_unique(remove_p, value, 0);
622 list_remove_item(add_p, value);
627 /* Shouldn't get here unless there's memory corruption. */
634 /* try user aliases. */
635 const struct action_alias * alias = alias_list;
637 while ((alias != NULL) && (0 != strcmpic(alias->name, option)))
644 merge_actions(cur_action, alias->action);
646 else if (((size_t)2 < strlen(option)) && action_used_to_be_valid(option+1))
648 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
649 "in this Privoxy release. Ignored.", option+1);
651 else if (((size_t)2 < strlen(option)) && 0 == strcmpic(option+1, "hide-forwarded-for-headers"))
653 log_error(LOG_LEVEL_FATAL, "The action 'hide-forwarded-for-headers' "
654 "is no longer valid in this Privoxy release. "
655 "Use 'change-x-forwarded-for' instead.");
659 /* Bad action name */
661 * XXX: This is a fatal error and Privoxy will later on exit
662 * in load_one_actions_file() because of an "invalid line".
664 * It would be preferable to name the offending option in that
665 * error message, but currently there is no way to do that and
666 * we have to live with two error messages for basically the
669 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
680 /*********************************************************************
682 * Function : init_current_action
684 * Description : Zero out an action.
687 * 1 : dest = An uninitialized current_action_spec.
691 *********************************************************************/
692 void init_current_action (struct current_action_spec *dest)
694 memset(dest, '\0', sizeof(*dest));
696 dest->flags = ACTION_MOST_COMPATIBLE;
700 /*********************************************************************
702 * Function : init_action
704 * Description : Zero out an action.
707 * 1 : dest = An uninitialized action_spec.
711 *********************************************************************/
712 void init_action (struct action_spec *dest)
714 memset(dest, '\0', sizeof(*dest));
718 /*********************************************************************
720 * Function : merge_current_action
722 * Description : Merge two actions together.
723 * Similar to "dest += src".
724 * Differences between this and merge_actions()
725 * is that this one doesn't allocate memory for
726 * strings (so "src" better be in memory for at least
727 * as long as "dest" is, and you'd better free
728 * "dest" using "free_current_action").
729 * Also, there is no mask or remove lists in dest.
730 * (If we're applying it to a URL, we don't need them)
733 * 1 : dest = Current actions, to modify.
734 * 2 : src = Action to add.
736 * Returns 0 : no error
737 * !=0 : error, probably JB_ERR_MEMORY.
739 *********************************************************************/
740 jb_err merge_current_action (struct current_action_spec *dest,
741 const struct action_spec *src)
744 jb_err err = JB_ERR_OK;
746 dest->flags &= src->mask;
747 dest->flags |= src->add;
749 for (i = 0; i < ACTION_STRING_COUNT; i++)
751 char * str = src->string[i];
754 str = strdup_or_die(str);
755 freez(dest->string[i]);
756 dest->string[i] = str;
760 for (i = 0; i < ACTION_MULTI_COUNT; i++)
762 if (src->multi_remove_all[i])
764 /* Remove everything from dest, then add src->multi_add */
765 err = list_duplicate(dest->multi[i], src->multi_add[i]);
773 list_remove_list(dest->multi[i], src->multi_remove[i]);
774 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
785 /*********************************************************************
787 * Function : update_action_bits_for_tag
789 * Description : Updates the action bits based on the action sections
790 * whose tag patterns match a provided tag.
793 * 1 : csp = Current client state (buffers, headers, etc...)
794 * 2 : tag = The tag on which the update should be based on
796 * Returns : 0 if no tag matched, or
799 *********************************************************************/
800 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
802 struct file_list *fl;
803 struct url_actions *b;
809 assert(list_contains_item(csp->tags, tag));
811 /* Run through all action files, */
812 for (i = 0; i < MAX_AF_FILES; i++)
814 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
816 /* Skip empty files */
820 /* and through all the action patterns, */
821 for (b = b->next; NULL != b; b = b->next)
823 /* skip everything but TAG patterns, */
824 if (!(b->url->flags & PATTERN_SPEC_TAG_PATTERN))
829 /* and check if one of the tag patterns matches the tag, */
830 if (0 == regexec(b->url->pattern.tag_regex, tag, 0, NULL, 0))
832 /* if it does, update the action bit map, */
833 if (merge_current_action(csp->action, b->action))
835 log_error(LOG_LEVEL_ERROR,
836 "Out of memory while changing action bits");
838 /* and signal the change. */
848 /*********************************************************************
850 * Function : check_negative_tag_patterns
852 * Description : Updates the action bits based on NO-*-TAG patterns.
855 * 1 : csp = Current client state (buffers, headers, etc...)
856 * 2 : flag = The tag pattern type
858 * Returns : JB_ERR_OK in case off success, or
859 * JB_ERR_MEMORY on out-of-memory error.
861 *********************************************************************/
862 jb_err check_negative_tag_patterns(struct client_state *csp, unsigned int flag)
864 struct list_entry *tag;
865 struct file_list *fl;
866 struct url_actions *b = NULL;
869 for (i = 0; i < MAX_AF_FILES; i++)
871 fl = csp->actions_list[i];
872 if ((fl == NULL) || ((b = fl->f) == NULL))
876 for (b = b->next; NULL != b; b = b->next)
879 if (0 == (b->url->flags & flag))
883 for (tag = csp->tags->first; NULL != tag; tag = tag->next)
885 if (0 == regexec(b->url->pattern.tag_regex, tag->str, 0, NULL, 0))
888 * The pattern matches at least one tag, thus the action
889 * section doesn't apply and we don't need to look at the
899 * The pattern doesn't match any tags,
900 * thus the action section applies.
902 if (merge_current_action(csp->action, b->action))
904 log_error(LOG_LEVEL_ERROR,
905 "Out of memory while changing action bits");
906 return JB_ERR_MEMORY;
908 log_error(LOG_LEVEL_HEADER, "Updated action bits based on: %s",
918 /*********************************************************************
920 * Function : free_current_action
922 * Description : Free memory used by a current_action_spec.
923 * Does not free the current_action_spec itself.
926 * 1 : src = Source to free.
930 *********************************************************************/
931 void free_current_action(struct current_action_spec *src)
935 for (i = 0; i < ACTION_STRING_COUNT; i++)
937 freez(src->string[i]);
940 for (i = 0; i < ACTION_MULTI_COUNT; i++)
942 destroy_list(src->multi[i]);
945 memset(src, '\0', sizeof(*src));
949 static struct file_list *current_actions_file[MAX_AF_FILES] = {
950 NULL, NULL, NULL, NULL, NULL,
951 NULL, NULL, NULL, NULL, NULL
955 #ifdef FEATURE_GRACEFUL_TERMINATION
956 /*********************************************************************
958 * Function : unload_current_actions_file
960 * Description : Unloads current actions file - reset to state at
961 * beginning of program.
967 *********************************************************************/
968 void unload_current_actions_file(void)
972 for (i = 0; i < MAX_AF_FILES; i++)
974 if (current_actions_file[i])
976 current_actions_file[i]->unloader = unload_actions_file;
977 current_actions_file[i] = NULL;
981 #endif /* FEATURE_GRACEFUL_TERMINATION */
984 /*********************************************************************
986 * Function : unload_actions_file
988 * Description : Unloads an actions module.
991 * 1 : file_data = the data structure associated with the
996 *********************************************************************/
997 void unload_actions_file(void *file_data)
999 struct url_actions * next;
1000 struct url_actions * cur = (struct url_actions *)file_data;
1004 free_pattern_spec(cur->url);
1005 if ((next == NULL) || (next->action != cur->action))
1008 * As the action settings might be shared,
1009 * we can only free them if the current
1010 * url pattern is the last one, or if the
1011 * next one is using different settings.
1013 free_action_spec(cur->action);
1021 /*********************************************************************
1023 * Function : free_alias_list
1025 * Description : Free memory used by a list of aliases.
1028 * 1 : alias_list = Linked list to free.
1032 *********************************************************************/
1033 void free_alias_list(struct action_alias *alias_list)
1035 while (alias_list != NULL)
1037 struct action_alias * next = alias_list->next;
1038 alias_list->next = NULL;
1039 freez(alias_list->name);
1040 free_action(alias_list->action);
1047 /*********************************************************************
1049 * Function : load_action_files
1051 * Description : Read and parse all the action files and add to files
1055 * 1 : csp = Current client state (buffers, headers, etc...)
1057 * Returns : 0 => Ok, everything else is an error.
1059 *********************************************************************/
1060 int load_action_files(struct client_state *csp)
1065 for (i = 0; i < MAX_AF_FILES; i++)
1067 if (csp->config->actions_file[i])
1069 result = load_one_actions_file(csp, i);
1075 else if (current_actions_file[i])
1077 current_actions_file[i]->unloader = unload_actions_file;
1078 current_actions_file[i] = NULL;
1086 /*********************************************************************
1088 * Function : filter_type_to_string
1090 * Description : Converts a filter type enum into a string.
1093 * 1 : filter_type = filter_type as enum
1095 * Returns : Pointer to static string.
1097 *********************************************************************/
1098 static const char *filter_type_to_string(enum filter_type filter_type)
1100 switch (filter_type)
1102 case FT_CONTENT_FILTER:
1103 return "content filter";
1104 case FT_CLIENT_HEADER_FILTER:
1105 return "client-header filter";
1106 case FT_SERVER_HEADER_FILTER:
1107 return "server-header filter";
1108 case FT_CLIENT_HEADER_TAGGER:
1109 return "client-header tagger";
1110 case FT_SERVER_HEADER_TAGGER:
1111 return "server-header tagger";
1112 #ifdef FEATURE_EXTERNAL_FILTERS
1113 case FT_EXTERNAL_CONTENT_FILTER:
1114 return "external content filter";
1116 case FT_INVALID_FILTER:
1117 return "invalid filter type";
1120 return "unknown filter type";
1124 /*********************************************************************
1126 * Function : referenced_filters_are_missing
1128 * Description : Checks if any filters of a certain type referenced
1129 * in an action spec are missing.
1132 * 1 : csp = Current client state (buffers, headers, etc...)
1133 * 2 : cur_action = The action spec to check.
1134 * 3 : multi_index = The index where to look for the filter.
1135 * 4 : filter_type = The filter type the caller is interested in.
1137 * Returns : 0 => All referenced filters exist, everything else is an error.
1139 *********************************************************************/
1140 static int referenced_filters_are_missing(const struct client_state *csp,
1141 const struct action_spec *cur_action, int multi_index, enum filter_type filter_type)
1143 struct list_entry *filtername;
1145 for (filtername = cur_action->multi_add[multi_index]->first;
1146 filtername; filtername = filtername->next)
1148 if (NULL == get_filter(csp, filtername->str, filter_type))
1150 log_error(LOG_LEVEL_ERROR, "Missing %s '%s'",
1151 filter_type_to_string(filter_type), filtername->str);
1161 /*********************************************************************
1163 * Function : action_spec_is_valid
1165 * Description : Should eventually figure out if an action spec
1166 * is valid, but currently only checks that the
1167 * referenced filters are accounted for.
1170 * 1 : csp = Current client state (buffers, headers, etc...)
1171 * 2 : cur_action = The action spec to check.
1173 * Returns : 0 => No problems detected, everything else is an error.
1175 *********************************************************************/
1176 static int action_spec_is_valid(struct client_state *csp, const struct action_spec *cur_action)
1180 enum filter_type filter_type;
1182 {ACTION_MULTI_FILTER, FT_CONTENT_FILTER},
1183 {ACTION_MULTI_CLIENT_HEADER_FILTER, FT_CLIENT_HEADER_FILTER},
1184 {ACTION_MULTI_SERVER_HEADER_FILTER, FT_SERVER_HEADER_FILTER},
1185 {ACTION_MULTI_CLIENT_HEADER_TAGGER, FT_CLIENT_HEADER_TAGGER},
1186 {ACTION_MULTI_SERVER_HEADER_TAGGER, FT_SERVER_HEADER_TAGGER}
1191 for (i = 0; i < SZ(filter_map); i++)
1193 errors += referenced_filters_are_missing(csp, cur_action,
1194 filter_map[i].multi_index, filter_map[i].filter_type);
1202 /*********************************************************************
1204 * Function : load_one_actions_file
1206 * Description : Read and parse a action file and add to files
1210 * 1 : csp = Current client state (buffers, headers, etc...)
1211 * 2 : fileid = File index to load.
1213 * Returns : 0 => Ok, everything else is an error.
1215 *********************************************************************/
1219 int load_one_actions_file(struct client_state *csp, int fileid)
1224 * Note: Keep these in the order they occur in the file, they are
1225 * sometimes tested with <=
1228 MODE_START_OF_FILE = 1,
1230 MODE_DESCRIPTION = 3,
1236 struct url_actions *last_perm;
1237 struct url_actions *perm;
1239 struct file_list *fs;
1240 struct action_spec * cur_action = NULL;
1241 int cur_action_used = 0;
1242 struct action_alias * alias_list = NULL;
1243 unsigned long linenum = 0;
1244 mode = MODE_START_OF_FILE;
1246 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1248 /* No need to load */
1249 csp->actions_list[fileid] = current_actions_file[fileid];
1254 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1255 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1256 "with their complete file names.", csp->config->actions_file[fileid]);
1257 return 1; /* never get here */
1260 fs->f = last_perm = zalloc_or_die(sizeof(*last_perm));
1262 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1264 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1265 csp->config->actions_file[fileid]);
1266 return 1; /* never get here */
1269 log_error(LOG_LEVEL_INFO, "Loading actions file: %s", csp->config->actions_file[fileid]);
1271 while (read_config_line(fp, &linenum, &buf) != NULL)
1275 /* It's a header block */
1278 /* It's {{settings}} or {{alias}} */
1279 size_t len = strlen(buf);
1280 char * start = buf + 2;
1281 char * end = buf + len - 1;
1282 if ((len < (size_t)5) || (*end-- != '}') || (*end-- != '}'))
1286 log_error(LOG_LEVEL_FATAL,
1287 "can't load actions file '%s': invalid line (%lu): %s",
1288 csp->config->actions_file[fileid], linenum, buf);
1289 return 1; /* never get here */
1292 /* Trim leading and trailing whitespace. */
1300 log_error(LOG_LEVEL_FATAL,
1301 "can't load actions file '%s': invalid line (%lu): {{ }}",
1302 csp->config->actions_file[fileid], linenum);
1303 return 1; /* never get here */
1307 * An actionsfile can optionally contain the following blocks.
1308 * They *MUST* be in this order, to simplify processing:
1314 * ...free text, format TBD, but no line may start with a '{'...
1319 * The actual actions must be *after* these special blocks.
1320 * None of these special blocks may be repeated.
1323 if (0 == strcmpic(start, "settings"))
1325 /* it's a {{settings}} block */
1326 if (mode >= MODE_SETTINGS)
1328 /* {{settings}} must be first thing in file and must only
1332 log_error(LOG_LEVEL_FATAL,
1333 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1334 csp->config->actions_file[fileid], linenum);
1336 mode = MODE_SETTINGS;
1338 else if (0 == strcmpic(start, "description"))
1340 /* it's a {{description}} block */
1341 if (mode >= MODE_DESCRIPTION)
1343 /* {{description}} is a singleton and only {{settings}} may proceed it
1346 log_error(LOG_LEVEL_FATAL,
1347 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1348 csp->config->actions_file[fileid], linenum);
1350 mode = MODE_DESCRIPTION;
1352 else if (0 == strcmpic(start, "alias"))
1354 /* it's an {{alias}} block */
1355 if (mode >= MODE_ALIAS)
1357 /* {{alias}} must be first thing in file, possibly after
1358 * {{settings}} and {{description}}
1360 * {{alias}} must only appear once.
1362 * Note that these are new restrictions introduced in
1363 * v2.9.10 in order to make actionsfile editing simpler.
1364 * (Otherwise, reordering actionsfile entries without
1365 * completely rewriting the file becomes non-trivial)
1368 log_error(LOG_LEVEL_FATAL,
1369 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1370 csp->config->actions_file[fileid], linenum);
1376 /* invalid {{something}} block */
1378 log_error(LOG_LEVEL_FATAL,
1379 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1380 csp->config->actions_file[fileid], linenum, start);
1381 return 1; /* never get here */
1386 /* It's an actions block */
1392 mode = MODE_ACTIONS;
1394 /* free old action */
1397 if (!cur_action_used)
1399 free_action_spec(cur_action);
1403 cur_action_used = 0;
1404 cur_action = zalloc_or_die(sizeof(*cur_action));
1405 init_action(cur_action);
1408 * Copy the buffer before messing with it as we may need the
1409 * unmodified version in for the fatal error messages. Given
1410 * that this is not a common event, we could instead simply
1411 * read the line again.
1413 * buf + 1 to skip the leading '{'
1415 actions_buf = end = strdup_or_die(buf + 1);
1417 /* check we have a trailing } and then trim it */
1418 if (strlen(actions_buf))
1420 end += strlen(actions_buf) - 1;
1427 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1428 "Missing trailing '}' in action section starting at line (%lu): %s",
1429 csp->config->actions_file[fileid], linenum, buf);
1430 return 1; /* never get here */
1434 /* trim any whitespace immediately inside {} */
1437 if (get_actions(actions_buf, alias_list, cur_action))
1442 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1443 "can't completely parse the action section starting at line (%lu): %s",
1444 csp->config->actions_file[fileid], linenum, buf);
1445 return 1; /* never get here */
1448 if (action_spec_is_valid(csp, cur_action))
1450 log_error(LOG_LEVEL_ERROR, "Invalid action section in file '%s', "
1451 "starting at line %lu: %s",
1452 csp->config->actions_file[fileid], linenum, buf);
1458 else if (mode == MODE_SETTINGS)
1461 * Part of the {{settings}} block.
1462 * For now only serves to check if the file's minimum Privoxy
1463 * version requirement is met, but we may want to read & check
1464 * permissions when we go multi-user.
1466 if (!strncmp(buf, "for-privoxy-version=", 20))
1468 char *version_string, *fields[3];
1471 version_string = strdup_or_die(buf + 20);
1473 num_fields = ssplit(version_string, ".", fields, SZ(fields));
1475 if (num_fields < 1 || atoi(fields[0]) == 0)
1477 log_error(LOG_LEVEL_ERROR,
1478 "While loading actions file '%s': invalid line (%lu): %s",
1479 csp->config->actions_file[fileid], linenum, buf);
1481 else if ( (atoi(fields[0]) > VERSION_MAJOR)
1482 || ((num_fields > 1) && (atoi(fields[1]) > VERSION_MINOR))
1483 || ((num_fields > 2) && (atoi(fields[2]) > VERSION_POINT)))
1486 log_error(LOG_LEVEL_FATAL,
1487 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1488 csp->config->actions_file[fileid], linenum, buf);
1489 return 1; /* never get here */
1491 free(version_string);
1494 else if (mode == MODE_DESCRIPTION)
1497 * Part of the {{description}} block.
1501 else if (mode == MODE_ALIAS)
1506 char actions_buf[BUFFER_SIZE];
1507 struct action_alias * new_alias;
1509 char * start = strchr(buf, '=');
1512 if ((start == NULL) || (start == buf))
1514 log_error(LOG_LEVEL_FATAL,
1515 "can't load actions file '%s': invalid alias line (%lu): %s",
1516 csp->config->actions_file[fileid], linenum, buf);
1517 return 1; /* never get here */
1520 new_alias = zalloc_or_die(sizeof(*new_alias));
1522 /* Eat any the whitespace before the '=' */
1524 while ((*end == ' ') || (*end == '\t'))
1527 * we already know we must have at least 1 non-ws char
1528 * at start of buf - no need to check
1534 /* Eat any the whitespace after the '=' */
1536 while ((*start == ' ') || (*start == '\t'))
1542 log_error(LOG_LEVEL_FATAL,
1543 "can't load actions file '%s': invalid alias line (%lu): %s",
1544 csp->config->actions_file[fileid], linenum, buf);
1545 return 1; /* never get here */
1548 new_alias->name = strdup_or_die(buf);
1550 strlcpy(actions_buf, start, sizeof(actions_buf));
1552 if (get_actions(actions_buf, alias_list, new_alias->action))
1556 log_error(LOG_LEVEL_FATAL,
1557 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1558 csp->config->actions_file[fileid], linenum, buf, start);
1559 return 1; /* never get here */
1563 new_alias->next = alias_list;
1564 alias_list = new_alias;
1566 else if (mode == MODE_ACTIONS)
1568 /* it's an URL pattern */
1570 /* allocate a new node */
1571 perm = zalloc_or_die(sizeof(*perm));
1573 perm->action = cur_action;
1574 cur_action_used = 1;
1576 /* Save the URL pattern */
1577 if (create_pattern_spec(perm->url, buf))
1580 log_error(LOG_LEVEL_FATAL,
1581 "can't load actions file '%s': line %lu: cannot create URL or TAG pattern from: %s",
1582 csp->config->actions_file[fileid], linenum, buf);
1583 return 1; /* never get here */
1586 /* add it to the list */
1587 last_perm->next = perm;
1590 else if (mode == MODE_START_OF_FILE)
1592 /* oops - please have a {} line as 1st line in file. */
1594 log_error(LOG_LEVEL_FATAL,
1595 "can't load actions file '%s': line %lu should begin with a '{': %s",
1596 csp->config->actions_file[fileid], linenum, buf);
1597 return 1; /* never get here */
1601 /* How did we get here? This is impossible! */
1603 log_error(LOG_LEVEL_FATAL,
1604 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1605 csp->config->actions_file[fileid], mode);
1606 return 1; /* never get here */
1613 if (!cur_action_used)
1615 free_action_spec(cur_action);
1617 free_alias_list(alias_list);
1619 /* the old one is now obsolete */
1620 if (current_actions_file[fileid])
1622 current_actions_file[fileid]->unloader = unload_actions_file;
1625 fs->next = files->next;
1627 current_actions_file[fileid] = fs;
1629 csp->actions_list[fileid] = fs;
1636 /*********************************************************************
1638 * Function : actions_to_text
1640 * Description : Converts a actionsfile entry from the internal
1641 * structure into a text line. The output is split
1642 * into one line for each action with line continuation.
1645 * 1 : action = The action to format.
1647 * Returns : A string. Caller must free it.
1648 * NULL on out-of-memory error.
1650 *********************************************************************/
1651 char * actions_to_text(const struct action_spec *action)
1653 unsigned long mask = action->mask;
1654 unsigned long add = action->add;
1655 char *result = strdup_or_die("");
1656 struct list_entry * lst;
1658 /* sanity - prevents "-feature +feature" */
1662 #define DEFINE_ACTION_BOOL(__name, __bit) \
1663 if (!(mask & __bit)) \
1665 string_append(&result, " -" __name " \\\n"); \
1667 else if (add & __bit) \
1669 string_append(&result, " +" __name " \\\n"); \
1672 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1673 if (!(mask & __bit)) \
1675 string_append(&result, " -" __name " \\\n"); \
1677 else if (add & __bit) \
1679 string_append(&result, " +" __name "{"); \
1680 string_append(&result, action->string[__index]); \
1681 string_append(&result, "} \\\n"); \
1684 #define DEFINE_ACTION_MULTI(__name, __index) \
1685 if (action->multi_remove_all[__index]) \
1687 string_append(&result, " -" __name " \\\n"); \
1691 lst = action->multi_remove[__index]->first; \
1694 string_append(&result, " -" __name "{"); \
1695 string_append(&result, lst->str); \
1696 string_append(&result, "} \\\n"); \
1700 lst = action->multi_add[__index]->first; \
1703 string_append(&result, " +" __name "{"); \
1704 string_append(&result, lst->str); \
1705 string_append(&result, "} \\\n"); \
1709 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1711 #include "actionlist.h"
1713 #undef DEFINE_ACTION_MULTI
1714 #undef DEFINE_ACTION_STRING
1715 #undef DEFINE_ACTION_BOOL
1716 #undef DEFINE_ACTION_ALIAS
1722 /*********************************************************************
1724 * Function : actions_to_html
1726 * Description : Converts a actionsfile entry from numeric form
1727 * ("mask" and "add") to a <br>-separated HTML string
1728 * in which each action is linked to its chapter in
1732 * 1 : csp = Client state (for config)
1733 * 2 : action = Action spec to be converted
1735 * Returns : A string. Caller must free it.
1736 * NULL on out-of-memory error.
1738 *********************************************************************/
1739 char * actions_to_html(const struct client_state *csp,
1740 const struct action_spec *action)
1742 unsigned long mask = action->mask;
1743 unsigned long add = action->add;
1744 char *result = strdup_or_die("");
1745 struct list_entry * lst;
1747 /* sanity - prevents "-feature +feature" */
1751 #define DEFINE_ACTION_BOOL(__name, __bit) \
1752 if (!(mask & __bit)) \
1754 string_append(&result, "\n<br>-"); \
1755 string_join(&result, add_help_link(__name, csp->config)); \
1757 else if (add & __bit) \
1759 string_append(&result, "\n<br>+"); \
1760 string_join(&result, add_help_link(__name, csp->config)); \
1763 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1764 if (!(mask & __bit)) \
1766 string_append(&result, "\n<br>-"); \
1767 string_join(&result, add_help_link(__name, csp->config)); \
1769 else if (add & __bit) \
1771 string_append(&result, "\n<br>+"); \
1772 string_join(&result, add_help_link(__name, csp->config)); \
1773 string_append(&result, "{"); \
1774 string_join(&result, html_encode(action->string[__index])); \
1775 string_append(&result, "}"); \
1778 #define DEFINE_ACTION_MULTI(__name, __index) \
1779 if (action->multi_remove_all[__index]) \
1781 string_append(&result, "\n<br>-"); \
1782 string_join(&result, add_help_link(__name, csp->config)); \
1786 lst = action->multi_remove[__index]->first; \
1789 string_append(&result, "\n<br>-"); \
1790 string_join(&result, add_help_link(__name, csp->config)); \
1791 string_append(&result, "{"); \
1792 string_join(&result, html_encode(lst->str)); \
1793 string_append(&result, "}"); \
1797 lst = action->multi_add[__index]->first; \
1800 string_append(&result, "\n<br>+"); \
1801 string_join(&result, add_help_link(__name, csp->config)); \
1802 string_append(&result, "{"); \
1803 string_join(&result, html_encode(lst->str)); \
1804 string_append(&result, "}"); \
1808 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1810 #include "actionlist.h"
1812 #undef DEFINE_ACTION_MULTI
1813 #undef DEFINE_ACTION_STRING
1814 #undef DEFINE_ACTION_BOOL
1815 #undef DEFINE_ACTION_ALIAS
1817 /* trim leading <br> */
1818 if (result && *result)
1821 result = strdup(result + 5);
1829 /*********************************************************************
1831 * Function : current_actions_to_html
1833 * Description : Converts a curren action spec to a <br> separated HTML
1834 * text in which each action is linked to its chapter in
1838 * 1 : csp = Client state (for config)
1839 * 2 : action = Current action spec to be converted
1841 * Returns : A string. Caller must free it.
1842 * NULL on out-of-memory error.
1844 *********************************************************************/
1845 char *current_action_to_html(const struct client_state *csp,
1846 const struct current_action_spec *action)
1848 unsigned long flags = action->flags;
1849 struct list_entry * lst;
1850 char *result = strdup_or_die("");
1851 char *active = strdup_or_die("");
1852 char *inactive = strdup_or_die("");
1854 #define DEFINE_ACTION_BOOL(__name, __bit) \
1855 if (flags & __bit) \
1857 string_append(&active, "\n<br>+"); \
1858 string_join(&active, add_help_link(__name, csp->config)); \
1862 string_append(&inactive, "\n<br>-"); \
1863 string_join(&inactive, add_help_link(__name, csp->config)); \
1866 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1867 if (flags & __bit) \
1869 string_append(&active, "\n<br>+"); \
1870 string_join(&active, add_help_link(__name, csp->config)); \
1871 string_append(&active, "{"); \
1872 string_join(&active, html_encode(action->string[__index])); \
1873 string_append(&active, "}"); \
1877 string_append(&inactive, "\n<br>-"); \
1878 string_join(&inactive, add_help_link(__name, csp->config)); \
1881 #define DEFINE_ACTION_MULTI(__name, __index) \
1882 lst = action->multi[__index]->first; \
1885 string_append(&inactive, "\n<br>-"); \
1886 string_join(&inactive, add_help_link(__name, csp->config)); \
1892 string_append(&active, "\n<br>+"); \
1893 string_join(&active, add_help_link(__name, csp->config)); \
1894 string_append(&active, "{"); \
1895 string_join(&active, html_encode(lst->str)); \
1896 string_append(&active, "}"); \
1901 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1903 #include "actionlist.h"
1905 #undef DEFINE_ACTION_MULTI
1906 #undef DEFINE_ACTION_STRING
1907 #undef DEFINE_ACTION_BOOL
1908 #undef DEFINE_ACTION_ALIAS
1912 string_append(&result, active);
1915 string_append(&result, "\n<br>");
1916 if (inactive != NULL)
1918 string_append(&result, inactive);
1925 /*********************************************************************
1927 * Function : action_to_line_of_text
1929 * Description : Converts a action spec to a single text line
1930 * listing the enabled actions.
1933 * 1 : action = Current action spec to be converted
1935 * Returns : A string. Caller must free it.
1936 * Out-of-memory errors are fatal.
1938 *********************************************************************/
1939 char *actions_to_line_of_text(const struct current_action_spec *action)
1942 struct list_entry *lst;
1944 const unsigned long flags = action->flags;
1946 active = strdup_or_die("");
1948 #define DEFINE_ACTION_BOOL(__name, __bit) \
1949 if (flags & __bit) \
1951 snprintf(buffer, sizeof(buffer), "+%s ", __name); \
1952 string_append(&active, buffer); \
1955 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1956 if (flags & __bit) \
1958 snprintf(buffer, sizeof(buffer), "+%s{%s} ", \
1959 __name, action->string[__index]); \
1960 string_append(&active, buffer); \
1963 #define DEFINE_ACTION_MULTI(__name, __index) \
1964 lst = action->multi[__index]->first; \
1965 while (lst != NULL) \
1967 snprintf(buffer, sizeof(buffer), "+%s{%s} ", \
1968 __name, lst->str); \
1969 string_append(&active, buffer); \
1973 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1975 #include "actionlist.h"
1977 #undef DEFINE_ACTION_MULTI
1978 #undef DEFINE_ACTION_STRING
1979 #undef DEFINE_ACTION_BOOL
1980 #undef DEFINE_ACTION_ALIAS
1984 log_error(LOG_LEVEL_FATAL, "Out of memory in action_to_line_of_text()");