1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/actions.c,v $
5 * Purpose : Declares functions to work with actions files
7 * Copyright : Written by and Copyright (C) 2001-2016 the
8 * Privoxy team. https://www.privoxy.org/
10 * Based on the Internet Junkbuster originally written
11 * by and Copyright (C) 1997 Anonymous Coders and
12 * Junkbusters Corporation. http://www.junkbusters.com
14 * This program is free software; you can redistribute it
15 * and/or modify it under the terms of the GNU General
16 * Public License as published by the Free Software
17 * Foundation; either version 2 of the License, or (at
18 * your option) any later version.
20 * This program is distributed in the hope that it will
21 * be useful, but WITHOUT ANY WARRANTY; without even the
22 * implied warranty of MERCHANTABILITY or FITNESS FOR A
23 * PARTICULAR PURPOSE. See the GNU General Public
24 * License for more details.
26 * The GNU General Public License should be included with
27 * this file. If not, you can view it at
28 * http://www.gnu.org/copyleft/gpl.html
29 * or write to the Free Software Foundation, Inc., 59
30 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 *********************************************************************/
42 #ifdef FEATURE_PTHREAD
60 * We need the main list of options.
62 * First, we need a way to tell between boolean, string, and multi-string
63 * options. For string and multistring options, we also need to be
64 * able to tell the difference between a "+" and a "-". (For bools,
65 * the "+"/"-" information is encoded in "add" and "mask"). So we use
66 * an enumerated type (well, the preprocessor equivalent). Here are
69 enum action_value_type {
70 AV_NONE = 0, /* +opt -opt */
71 AV_ADD_STRING = 1, /* +stropt{string} */
72 AV_REM_STRING = 2, /* -stropt */
73 AV_ADD_MULTI = 3, /* +multiopt{string} +multiopt{string2} */
74 AV_REM_MULTI = 4 /* -multiopt{string} -multiopt */
78 * We need a structure to hold the name, flag changes,
79 * type, and string index.
84 unsigned long mask; /* a bit set to "0" = remove action */
85 unsigned long add; /* a bit set to "1" = add action */
86 enum action_value_type value_type; /* an AV_... constant */
87 int index; /* index into strings[] or multi[] */
91 * And with those building blocks in place, here's the array.
93 static const struct action_name action_names[] =
96 * Well actually there's no data here - it's in actionlist.h
97 * This keeps it together to make it easy to change.
99 * Here's the macros used to format it:
101 #define DEFINE_ACTION_MULTI(name,index) \
102 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
103 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
104 #define DEFINE_ACTION_STRING(name,flag,index) \
105 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
106 { "-" name, ~flag, 0, AV_REM_STRING, index },
107 #define DEFINE_ACTION_BOOL(name,flag) \
108 { "+" name, ACTION_MASK_ALL, flag }, \
109 { "-" name, ~flag, 0 },
110 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
112 #include "actionlist.h"
114 #undef DEFINE_ACTION_MULTI
115 #undef DEFINE_ACTION_STRING
116 #undef DEFINE_ACTION_BOOL
117 #undef DEFINE_ACTION_ALIAS
119 { NULL, 0, 0 } /* End marker */
126 int load_one_actions_file(struct client_state *csp, int fileid);
129 /*********************************************************************
131 * Function : merge_actions
133 * Description : Merge two actions together.
134 * Similar to "dest += src".
137 * 1 : dest = Actions to modify.
138 * 2 : src = Action to add.
140 * Returns : JB_ERR_OK or JB_ERR_MEMORY
142 *********************************************************************/
143 jb_err merge_actions (struct action_spec *dest,
144 const struct action_spec *src)
149 dest->mask &= src->mask;
150 dest->add &= src->mask;
151 dest->add |= src->add;
153 for (i = 0; i < ACTION_STRING_COUNT; i++)
155 char * str = src->string[i];
158 freez(dest->string[i]);
159 dest->string[i] = strdup_or_die(str);
163 for (i = 0; i < ACTION_MULTI_COUNT; i++)
165 if (src->multi_remove_all[i])
167 /* Remove everything from dest */
168 list_remove_all(dest->multi_remove[i]);
169 dest->multi_remove_all[i] = 1;
171 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
173 else if (dest->multi_remove_all[i])
176 * dest already removes everything, so we only need to worry
179 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
180 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
184 /* No "remove all"s to worry about. */
185 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
186 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
187 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
200 /*********************************************************************
202 * Function : copy_action
204 * Description : Copy an action_specs.
205 * Similar to "dest = src".
208 * 1 : dest = Destination of copy.
209 * 2 : src = Source for copy.
211 * Returns : JB_ERR_OK or JB_ERR_MEMORY
213 *********************************************************************/
214 jb_err copy_action (struct action_spec *dest,
215 const struct action_spec *src)
218 jb_err err = JB_ERR_OK;
221 memset(dest, '\0', sizeof(*dest));
223 dest->mask = src->mask;
224 dest->add = src->add;
226 for (i = 0; i < ACTION_STRING_COUNT; i++)
228 char * str = src->string[i];
231 str = strdup_or_die(str);
232 dest->string[i] = str;
236 for (i = 0; i < ACTION_MULTI_COUNT; i++)
238 dest->multi_remove_all[i] = src->multi_remove_all[i];
239 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
244 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
253 /*********************************************************************
255 * Function : free_action_spec
257 * Description : Frees an action_spec and the memory used by it.
260 * 1 : src = Source to free.
264 *********************************************************************/
265 void free_action_spec(struct action_spec *src)
272 /*********************************************************************
274 * Function : free_action
276 * Description : Destroy an action_spec. Frees memory used by it,
277 * except for the memory used by the struct action_spec
281 * 1 : src = Source to free.
285 *********************************************************************/
286 void free_action (struct action_spec *src)
295 for (i = 0; i < ACTION_STRING_COUNT; i++)
297 freez(src->string[i]);
300 for (i = 0; i < ACTION_MULTI_COUNT; i++)
302 destroy_list(src->multi_remove[i]);
303 destroy_list(src->multi_add[i]);
306 memset(src, '\0', sizeof(*src));
310 /*********************************************************************
312 * Function : get_action_token
314 * Description : Parses a line for the first action.
315 * Modifies its input array, doesn't allocate memory.
317 * *line=" +abc{def} -ghi "
324 * 1 : line = [in] The line containing the action.
325 * [out] Start of next action on line, or
326 * NULL if we reached the end of line before
327 * we found an action.
328 * 2 : name = [out] Start of action name, null
329 * terminated. NULL on EOL
330 * 3 : value = [out] Start of action value, null
331 * terminated. NULL if none or EOL.
333 * Returns : JB_ERR_OK => Ok
334 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
336 *********************************************************************/
337 jb_err get_action_token(char **line, char **name, char **value)
342 /* set default returns */
347 /* Eat any leading whitespace */
348 while ((*str == ' ') || (*str == '\t'))
360 /* null name, just value is prohibited */
367 while (((ch = *str) != '\0') &&
368 (ch != ' ') && (ch != '\t') && (ch != '{'))
372 /* error, '}' without '{' */
384 /* EOL - be careful not to run off buffer */
389 /* More to parse next time. */
398 /* The value ends with the first non-escaped closing curly brace */
399 while ((str = strchr(str, '}')) != NULL)
403 /* Overwrite the '\' so the action doesn't see it. */
404 string_move(str-1, str);
425 /*********************************************************************
427 * Function : action_used_to_be_valid
429 * Description : Checks if unrecognized actions were valid in earlier
433 * 1 : action = The string containing the action to check.
435 * Returns : True if yes, otherwise false.
437 *********************************************************************/
438 static int action_used_to_be_valid(const char *action)
440 static const char * const formerly_valid_actions[] = {
443 "send-vanilla-wafer",
445 "treat-forbidden-connects-like-blocks",
451 for (i = 0; i < SZ(formerly_valid_actions); i++)
453 if (0 == strcmpic(action, formerly_valid_actions[i]))
462 /*********************************************************************
464 * Function : get_actions
466 * Description : Parses a list of actions.
469 * 1 : line = The string containing the actions.
470 * Will be written to by this function.
471 * 2 : alias_list = Custom alias list, or NULL for none.
472 * 3 : cur_action = Where to store the action. Caller
475 * Returns : JB_ERR_OK => Ok
476 * JB_ERR_PARSE => Parse error (line was trashed anyway)
477 * nonzero => Out of memory (line was trashed anyway)
479 *********************************************************************/
480 jb_err get_actions(char *line,
481 struct action_alias * alias_list,
482 struct action_spec *cur_action)
485 init_action(cur_action);
486 cur_action->mask = ACTION_MASK_ALL;
490 char * option = NULL;
493 err = get_action_token(&line, &option, &value);
501 /* handle option in 'option' */
503 /* Check for standard action name */
504 const struct action_name * action = action_names;
506 while ((action->name != NULL) && (0 != strcmpic(action->name, option)))
510 if (action->name != NULL)
513 cur_action->mask &= action->mask;
514 cur_action->add &= action->mask;
515 cur_action->add |= action->add;
517 switch (action->value_type)
522 log_error(LOG_LEVEL_ERROR,
523 "Action %s does not take parameters but %s was given.",
524 action->name, value);
530 /* add single string. */
532 if ((value == NULL) || (*value == '\0'))
534 if (0 == strcmpic(action->name, "+block"))
537 * XXX: Temporary backwards compatibility hack.
538 * XXX: should include line number.
540 value = "No reason specified.";
541 log_error(LOG_LEVEL_ERROR,
542 "block action without reason found. This may "
543 "become a fatal error in future versions.");
550 #ifdef FEATURE_EXTENDED_STATISTICS
551 if (0 == strcmpic(action->name, "+block"))
553 register_block_reason_for_statistics(value);
556 /* FIXME: should validate option string here */
557 freez (cur_action->string[action->index]);
558 cur_action->string[action->index] = strdup(value);
559 if (NULL == cur_action->string[action->index])
561 return JB_ERR_MEMORY;
567 /* remove single string. */
569 freez (cur_action->string[action->index]);
574 /* append multi string. */
576 struct list * remove_p = cur_action->multi_remove[action->index];
577 struct list * add_p = cur_action->multi_add[action->index];
579 if ((value == NULL) || (*value == '\0'))
584 list_remove_item(remove_p, value);
585 err = enlist_unique(add_p, value, 0);
594 /* remove multi string. */
596 struct list * remove_p = cur_action->multi_remove[action->index];
597 struct list * add_p = cur_action->multi_add[action->index];
599 if ((value == NULL) || (*value == '\0')
600 || ((*value == '*') && (value[1] == '\0')))
603 * no option, or option == "*".
607 list_remove_all(remove_p);
608 list_remove_all(add_p);
609 cur_action->multi_remove_all[action->index] = 1;
613 /* Valid option - remove only 1 option */
615 if (!cur_action->multi_remove_all[action->index])
617 /* there isn't a catch-all in the remove list already */
618 err = enlist_unique(remove_p, value, 0);
624 list_remove_item(add_p, value);
629 /* Shouldn't get here unless there's memory corruption. */
636 /* try user aliases. */
637 const struct action_alias * alias = alias_list;
639 while ((alias != NULL) && (0 != strcmpic(alias->name, option)))
646 merge_actions(cur_action, alias->action);
648 else if (((size_t)2 < strlen(option)) && action_used_to_be_valid(option+1))
650 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
651 "in this Privoxy release. Ignored.", option+1);
653 else if (((size_t)2 < strlen(option)) && 0 == strcmpic(option+1, "hide-forwarded-for-headers"))
655 log_error(LOG_LEVEL_FATAL, "The action 'hide-forwarded-for-headers' "
656 "is no longer valid in this Privoxy release. "
657 "Use 'change-x-forwarded-for' instead.");
661 /* Bad action name */
663 * XXX: This is a fatal error and Privoxy will later on exit
664 * in load_one_actions_file() because of an "invalid line".
666 * It would be preferable to name the offending option in that
667 * error message, but currently there is no way to do that and
668 * we have to live with two error messages for basically the
671 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
682 /*********************************************************************
684 * Function : init_current_action
686 * Description : Zero out an action.
689 * 1 : dest = An uninitialized current_action_spec.
693 *********************************************************************/
694 void init_current_action (struct current_action_spec *dest)
696 memset(dest, '\0', sizeof(*dest));
698 dest->flags = ACTION_MOST_COMPATIBLE;
702 /*********************************************************************
704 * Function : init_action
706 * Description : Zero out an action.
709 * 1 : dest = An uninitialized action_spec.
713 *********************************************************************/
714 void init_action (struct action_spec *dest)
716 memset(dest, '\0', sizeof(*dest));
720 /*********************************************************************
722 * Function : merge_current_action
724 * Description : Merge two actions together.
725 * Similar to "dest += src".
726 * Differences between this and merge_actions()
727 * is that this one doesn't allocate memory for
728 * strings (so "src" better be in memory for at least
729 * as long as "dest" is, and you'd better free
730 * "dest" using "free_current_action").
731 * Also, there is no mask or remove lists in dest.
732 * (If we're applying it to a URL, we don't need them)
735 * 1 : dest = Current actions, to modify.
736 * 2 : src = Action to add.
738 * Returns 0 : no error
739 * !=0 : error, probably JB_ERR_MEMORY.
741 *********************************************************************/
742 jb_err merge_current_action (struct current_action_spec *dest,
743 const struct action_spec *src)
746 jb_err err = JB_ERR_OK;
748 dest->flags &= src->mask;
749 dest->flags |= src->add;
751 for (i = 0; i < ACTION_STRING_COUNT; i++)
753 char * str = src->string[i];
756 str = strdup_or_die(str);
757 freez(dest->string[i]);
758 dest->string[i] = str;
762 for (i = 0; i < ACTION_MULTI_COUNT; i++)
764 if (src->multi_remove_all[i])
766 /* Remove everything from dest, then add src->multi_add */
767 err = list_duplicate(dest->multi[i], src->multi_add[i]);
775 list_remove_list(dest->multi[i], src->multi_remove[i]);
776 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
787 /*********************************************************************
789 * Function : update_action_bits_for_tag
791 * Description : Updates the action bits based on the action sections
792 * whose tag patterns match a provided tag.
795 * 1 : csp = Current client state (buffers, headers, etc...)
796 * 2 : tag = The tag on which the update should be based on
798 * Returns : 0 if no tag matched, or
801 *********************************************************************/
802 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
804 struct file_list *fl;
805 struct url_actions *b;
811 assert(list_contains_item(csp->tags, tag));
813 /* Run through all action files, */
814 for (i = 0; i < MAX_AF_FILES; i++)
816 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
818 /* Skip empty files */
822 /* and through all the action patterns, */
823 for (b = b->next; NULL != b; b = b->next)
825 /* skip everything but TAG patterns, */
826 if (!(b->url->flags & PATTERN_SPEC_TAG_PATTERN))
831 /* and check if one of the tag patterns matches the tag, */
832 if (0 == regexec(b->url->pattern.tag_regex, tag, 0, NULL, 0))
834 /* if it does, update the action bit map, */
835 if (merge_current_action(csp->action, b->action))
837 log_error(LOG_LEVEL_ERROR,
838 "Out of memory while changing action bits");
840 /* and signal the change. */
850 /*********************************************************************
852 * Function : check_negative_tag_patterns
854 * Description : Updates the action bits based on NO-*-TAG patterns.
857 * 1 : csp = Current client state (buffers, headers, etc...)
858 * 2 : flag = The tag pattern type
860 * Returns : JB_ERR_OK in case off success, or
861 * JB_ERR_MEMORY on out-of-memory error.
863 *********************************************************************/
864 jb_err check_negative_tag_patterns(struct client_state *csp, unsigned int flag)
866 struct list_entry *tag;
867 struct file_list *fl;
868 struct url_actions *b = NULL;
871 for (i = 0; i < MAX_AF_FILES; i++)
873 fl = csp->actions_list[i];
874 if ((fl == NULL) || ((b = fl->f) == NULL))
878 for (b = b->next; NULL != b; b = b->next)
881 if (0 == (b->url->flags & flag))
885 for (tag = csp->tags->first; NULL != tag; tag = tag->next)
887 if (0 == regexec(b->url->pattern.tag_regex, tag->str, 0, NULL, 0))
890 * The pattern matches at least one tag, thus the action
891 * section doesn't apply and we don't need to look at the
901 * The pattern doesn't match any tags,
902 * thus the action section applies.
904 if (merge_current_action(csp->action, b->action))
906 log_error(LOG_LEVEL_ERROR,
907 "Out of memory while changing action bits");
908 return JB_ERR_MEMORY;
910 log_error(LOG_LEVEL_HEADER, "Updated action bits based on: %s",
920 /*********************************************************************
922 * Function : free_current_action
924 * Description : Free memory used by a current_action_spec.
925 * Does not free the current_action_spec itself.
928 * 1 : src = Source to free.
932 *********************************************************************/
933 void free_current_action(struct current_action_spec *src)
937 for (i = 0; i < ACTION_STRING_COUNT; i++)
939 freez(src->string[i]);
942 for (i = 0; i < ACTION_MULTI_COUNT; i++)
944 destroy_list(src->multi[i]);
947 memset(src, '\0', sizeof(*src));
951 static struct file_list *current_actions_file[MAX_AF_FILES] = {
952 NULL, NULL, NULL, NULL, NULL,
953 NULL, NULL, NULL, NULL, NULL
957 #ifdef FEATURE_GRACEFUL_TERMINATION
958 /*********************************************************************
960 * Function : unload_current_actions_file
962 * Description : Unloads current actions file - reset to state at
963 * beginning of program.
969 *********************************************************************/
970 void unload_current_actions_file(void)
974 for (i = 0; i < MAX_AF_FILES; i++)
976 if (current_actions_file[i])
978 current_actions_file[i]->unloader = unload_actions_file;
979 current_actions_file[i] = NULL;
983 #endif /* FEATURE_GRACEFUL_TERMINATION */
986 /*********************************************************************
988 * Function : unload_actions_file
990 * Description : Unloads an actions module.
993 * 1 : file_data = the data structure associated with the
998 *********************************************************************/
999 void unload_actions_file(void *file_data)
1001 struct url_actions * next;
1002 struct url_actions * cur = (struct url_actions *)file_data;
1006 free_pattern_spec(cur->url);
1007 if ((next == NULL) || (next->action != cur->action))
1010 * As the action settings might be shared,
1011 * we can only free them if the current
1012 * url pattern is the last one, or if the
1013 * next one is using different settings.
1015 free_action_spec(cur->action);
1023 /*********************************************************************
1025 * Function : free_alias_list
1027 * Description : Free memory used by a list of aliases.
1030 * 1 : alias_list = Linked list to free.
1034 *********************************************************************/
1035 void free_alias_list(struct action_alias *alias_list)
1037 while (alias_list != NULL)
1039 struct action_alias * next = alias_list->next;
1040 alias_list->next = NULL;
1041 freez(alias_list->name);
1042 free_action(alias_list->action);
1049 /*********************************************************************
1051 * Function : load_action_files
1053 * Description : Read and parse all the action files and add to files
1057 * 1 : csp = Current client state (buffers, headers, etc...)
1059 * Returns : 0 => Ok, everything else is an error.
1061 *********************************************************************/
1062 int load_action_files(struct client_state *csp)
1067 for (i = 0; i < MAX_AF_FILES; i++)
1069 if (csp->config->actions_file[i])
1071 result = load_one_actions_file(csp, i);
1077 else if (current_actions_file[i])
1079 current_actions_file[i]->unloader = unload_actions_file;
1080 current_actions_file[i] = NULL;
1088 /*********************************************************************
1090 * Function : filter_type_to_string
1092 * Description : Converts a filter type enum into a string.
1095 * 1 : filter_type = filter_type as enum
1097 * Returns : Pointer to static string.
1099 *********************************************************************/
1100 static const char *filter_type_to_string(enum filter_type filter_type)
1102 switch (filter_type)
1104 case FT_CONTENT_FILTER:
1105 return "content filter";
1106 case FT_CLIENT_HEADER_FILTER:
1107 return "client-header filter";
1108 case FT_SERVER_HEADER_FILTER:
1109 return "server-header filter";
1110 case FT_CLIENT_HEADER_TAGGER:
1111 return "client-header tagger";
1112 case FT_SERVER_HEADER_TAGGER:
1113 return "server-header tagger";
1114 case FT_SUPPRESS_TAG:
1115 return "suppress tag filter";
1116 case FT_CLIENT_BODY_FILTER:
1117 return "client body filter";
1119 return "add-header action";
1120 #ifdef FEATURE_EXTERNAL_FILTERS
1121 case FT_EXTERNAL_CONTENT_FILTER:
1122 return "external content filter";
1124 case FT_INVALID_FILTER:
1125 return "invalid filter type";
1128 return "unknown filter type";
1132 /*********************************************************************
1134 * Function : referenced_filters_are_missing
1136 * Description : Checks if any filters of a certain type referenced
1137 * in an action spec are missing.
1140 * 1 : csp = Current client state (buffers, headers, etc...)
1141 * 2 : cur_action = The action spec to check.
1142 * 3 : multi_index = The index where to look for the filter.
1143 * 4 : filter_type = The filter type the caller is interested in.
1145 * Returns : 0 => All referenced filters exist, everything else is an error.
1147 *********************************************************************/
1148 static int referenced_filters_are_missing(const struct client_state *csp,
1149 const struct action_spec *cur_action, int multi_index, enum filter_type filter_type)
1151 struct list_entry *filtername;
1153 for (filtername = cur_action->multi_add[multi_index]->first;
1154 filtername; filtername = filtername->next)
1156 if (NULL == get_filter(csp, filtername->str, filter_type))
1158 log_error(LOG_LEVEL_ERROR, "Missing %s '%s'",
1159 filter_type_to_string(filter_type), filtername->str);
1169 /*********************************************************************
1171 * Function : action_spec_is_valid
1173 * Description : Should eventually figure out if an action spec
1174 * is valid, but currently only checks that the
1175 * referenced filters are accounted for.
1178 * 1 : csp = Current client state (buffers, headers, etc...)
1179 * 2 : cur_action = The action spec to check.
1181 * Returns : 0 => No problems detected, everything else is an error.
1183 *********************************************************************/
1184 static int action_spec_is_valid(struct client_state *csp, const struct action_spec *cur_action)
1188 enum filter_type filter_type;
1190 {ACTION_MULTI_FILTER, FT_CONTENT_FILTER},
1191 {ACTION_MULTI_CLIENT_HEADER_FILTER, FT_CLIENT_HEADER_FILTER},
1192 {ACTION_MULTI_SERVER_HEADER_FILTER, FT_SERVER_HEADER_FILTER},
1193 {ACTION_MULTI_CLIENT_HEADER_TAGGER, FT_CLIENT_HEADER_TAGGER},
1194 {ACTION_MULTI_SERVER_HEADER_TAGGER, FT_SERVER_HEADER_TAGGER},
1195 {ACTION_MULTI_CLIENT_BODY_FILTER, FT_CLIENT_BODY_FILTER}
1200 for (i = 0; i < SZ(filter_map); i++)
1202 errors += referenced_filters_are_missing(csp, cur_action,
1203 filter_map[i].multi_index, filter_map[i].filter_type);
1211 /*********************************************************************
1213 * Function : load_one_actions_file
1215 * Description : Read and parse an action file and add to files
1219 * 1 : csp = Current client state (buffers, headers, etc...)
1220 * 2 : fileid = File index to load.
1222 * Returns : 0 => Ok, everything else is an error.
1224 *********************************************************************/
1228 int load_one_actions_file(struct client_state *csp, int fileid)
1233 * Note: Keep these in the order they occur in the file, they are
1234 * sometimes tested with <=
1237 MODE_START_OF_FILE = 1,
1239 MODE_DESCRIPTION = 3,
1245 struct url_actions *last_perm;
1246 struct url_actions *perm;
1248 struct file_list *fs;
1249 struct action_spec * cur_action = NULL;
1250 int cur_action_used = 0;
1251 struct action_alias * alias_list = NULL;
1252 unsigned long linenum = 0;
1253 mode = MODE_START_OF_FILE;
1255 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1257 /* No need to load */
1258 csp->actions_list[fileid] = current_actions_file[fileid];
1263 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1264 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1265 "with their complete file names.", csp->config->actions_file[fileid]);
1266 return 1; /* never get here */
1269 fs->f = last_perm = zalloc_or_die(sizeof(*last_perm));
1271 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1273 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1274 csp->config->actions_file[fileid]);
1275 return 1; /* never get here */
1278 log_error(LOG_LEVEL_INFO, "Loading actions file: %s", csp->config->actions_file[fileid]);
1280 while (read_config_line(fp, &linenum, &buf) != NULL)
1284 /* It's a header block */
1287 /* It's {{settings}} or {{alias}} */
1288 size_t len = strlen(buf);
1289 char * start = buf + 2;
1290 char * end = buf + len - 1;
1291 if ((len < (size_t)5) || (*end-- != '}') || (*end-- != '}'))
1295 log_error(LOG_LEVEL_FATAL,
1296 "can't load actions file '%s': invalid line (%lu): %s",
1297 csp->config->actions_file[fileid], linenum, buf);
1298 return 1; /* never get here */
1301 /* Trim leading and trailing whitespace. */
1309 log_error(LOG_LEVEL_FATAL,
1310 "can't load actions file '%s': invalid line (%lu): {{ }}",
1311 csp->config->actions_file[fileid], linenum);
1312 return 1; /* never get here */
1316 * An actionsfile can optionally contain the following blocks.
1317 * They *MUST* be in this order, to simplify processing:
1323 * ...free text, format TBD, but no line may start with a '{'...
1328 * The actual actions must be *after* these special blocks.
1329 * None of these special blocks may be repeated.
1332 if (0 == strcmpic(start, "settings"))
1334 /* it's a {{settings}} block */
1335 if (mode >= MODE_SETTINGS)
1337 /* {{settings}} must be first thing in file and must only
1341 log_error(LOG_LEVEL_FATAL,
1342 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1343 csp->config->actions_file[fileid], linenum);
1345 mode = MODE_SETTINGS;
1347 else if (0 == strcmpic(start, "description"))
1349 /* it's a {{description}} block */
1350 if (mode >= MODE_DESCRIPTION)
1352 /* {{description}} is a singleton and only {{settings}} may proceed it
1355 log_error(LOG_LEVEL_FATAL,
1356 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1357 csp->config->actions_file[fileid], linenum);
1359 mode = MODE_DESCRIPTION;
1361 else if (0 == strcmpic(start, "alias"))
1363 /* it's an {{alias}} block */
1364 if (mode >= MODE_ALIAS)
1366 /* {{alias}} must be first thing in file, possibly after
1367 * {{settings}} and {{description}}
1369 * {{alias}} must only appear once.
1371 * Note that these are new restrictions introduced in
1372 * v2.9.10 in order to make actionsfile editing simpler.
1373 * (Otherwise, reordering actionsfile entries without
1374 * completely rewriting the file becomes non-trivial)
1377 log_error(LOG_LEVEL_FATAL,
1378 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1379 csp->config->actions_file[fileid], linenum);
1385 /* invalid {{something}} block */
1387 log_error(LOG_LEVEL_FATAL,
1388 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1389 csp->config->actions_file[fileid], linenum, start);
1390 return 1; /* never get here */
1395 /* It's an actions block */
1401 mode = MODE_ACTIONS;
1403 /* free old action */
1406 if (!cur_action_used)
1408 free_action_spec(cur_action);
1412 cur_action_used = 0;
1413 cur_action = zalloc_or_die(sizeof(*cur_action));
1414 init_action(cur_action);
1417 * Copy the buffer before messing with it as we may need the
1418 * unmodified version in for the fatal error messages. Given
1419 * that this is not a common event, we could instead simply
1420 * read the line again.
1422 * buf + 1 to skip the leading '{'
1424 actions_buf = end = strdup_or_die(buf + 1);
1426 /* check we have a trailing } and then trim it */
1427 if (strlen(actions_buf))
1429 end += strlen(actions_buf) - 1;
1436 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1437 "Missing trailing '}' in action section starting at line (%lu): %s",
1438 csp->config->actions_file[fileid], linenum, buf);
1439 return 1; /* never get here */
1443 /* trim any whitespace immediately inside {} */
1446 if (get_actions(actions_buf, alias_list, cur_action))
1451 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1452 "can't completely parse the action section starting at line (%lu): %s",
1453 csp->config->actions_file[fileid], linenum, buf);
1454 return 1; /* never get here */
1457 if (action_spec_is_valid(csp, cur_action))
1459 log_error(LOG_LEVEL_ERROR, "Invalid action section in file '%s', "
1460 "starting at line %lu: %s",
1461 csp->config->actions_file[fileid], linenum, buf);
1467 else if (mode == MODE_SETTINGS)
1470 * Part of the {{settings}} block.
1471 * For now only serves to check if the file's minimum Privoxy
1472 * version requirement is met, but we may want to read & check
1473 * permissions when we go multi-user.
1475 if (!strncmp(buf, "for-privoxy-version=", 20))
1477 char *version_string, *fields[3];
1480 version_string = strdup_or_die(buf + 20);
1482 num_fields = ssplit(version_string, ".", fields, SZ(fields));
1484 if (num_fields < 1 || atoi(fields[0]) == 0)
1486 log_error(LOG_LEVEL_ERROR,
1487 "While loading actions file '%s': invalid line (%lu): %s",
1488 csp->config->actions_file[fileid], linenum, buf);
1490 else if ( (atoi(fields[0]) > VERSION_MAJOR)
1491 || ((num_fields > 1) && (atoi(fields[1]) > VERSION_MINOR))
1492 || ((num_fields > 2) && (atoi(fields[2]) > VERSION_POINT)))
1495 log_error(LOG_LEVEL_FATAL,
1496 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1497 csp->config->actions_file[fileid], linenum, buf);
1498 return 1; /* never get here */
1500 free(version_string);
1503 else if (mode == MODE_DESCRIPTION)
1506 * Part of the {{description}} block.
1510 else if (mode == MODE_ALIAS)
1515 char actions_buf[BUFFER_SIZE];
1516 struct action_alias * new_alias;
1518 char * start = strchr(buf, '=');
1521 if ((start == NULL) || (start == buf))
1523 log_error(LOG_LEVEL_FATAL,
1524 "can't load actions file '%s': invalid alias line (%lu): %s",
1525 csp->config->actions_file[fileid], linenum, buf);
1526 return 1; /* never get here */
1529 new_alias = zalloc_or_die(sizeof(*new_alias));
1531 /* Eat any the whitespace before the '=' */
1533 while ((*end == ' ') || (*end == '\t'))
1536 * we already know we must have at least 1 non-ws char
1537 * at start of buf - no need to check
1543 /* Eat any the whitespace after the '=' */
1545 while ((*start == ' ') || (*start == '\t'))
1551 log_error(LOG_LEVEL_FATAL,
1552 "can't load actions file '%s': invalid alias line (%lu): %s",
1553 csp->config->actions_file[fileid], linenum, buf);
1554 return 1; /* never get here */
1557 new_alias->name = strdup_or_die(buf);
1559 strlcpy(actions_buf, start, sizeof(actions_buf));
1561 if (get_actions(actions_buf, alias_list, new_alias->action))
1565 log_error(LOG_LEVEL_FATAL,
1566 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1567 csp->config->actions_file[fileid], linenum, buf, start);
1568 return 1; /* never get here */
1572 new_alias->next = alias_list;
1573 alias_list = new_alias;
1575 else if (mode == MODE_ACTIONS)
1577 /* it's an URL pattern */
1579 /* allocate a new node */
1580 perm = zalloc_or_die(sizeof(*perm));
1582 perm->action = cur_action;
1583 cur_action_used = 1;
1585 /* Save the URL pattern */
1586 if (create_pattern_spec(perm->url, buf))
1589 log_error(LOG_LEVEL_FATAL,
1590 "can't load actions file '%s': line %lu: cannot create URL or TAG pattern from: %s",
1591 csp->config->actions_file[fileid], linenum, buf);
1592 return 1; /* never get here */
1595 /* add it to the list */
1596 last_perm->next = perm;
1599 else if (mode == MODE_START_OF_FILE)
1601 /* oops - please have a {} line as 1st line in file. */
1603 log_error(LOG_LEVEL_FATAL,
1604 "can't load actions file '%s': line %lu should begin with a '{': %s",
1605 csp->config->actions_file[fileid], linenum, buf);
1606 return 1; /* never get here */
1610 /* How did we get here? This is impossible! */
1612 log_error(LOG_LEVEL_FATAL,
1613 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1614 csp->config->actions_file[fileid], mode);
1615 return 1; /* never get here */
1622 if (!cur_action_used)
1624 free_action_spec(cur_action);
1626 free_alias_list(alias_list);
1628 /* the old one is now obsolete */
1629 if (current_actions_file[fileid])
1631 current_actions_file[fileid]->unloader = unload_actions_file;
1634 fs->next = files->next;
1636 current_actions_file[fileid] = fs;
1638 csp->actions_list[fileid] = fs;
1645 /*********************************************************************
1647 * Function : actions_to_text
1649 * Description : Converts an actionsfile entry from the internal
1650 * structure into a text line. The output is split
1651 * into one line for each action with line continuation.
1654 * 1 : action = The action to format.
1656 * Returns : A string. Caller must free it.
1657 * NULL on out-of-memory error.
1659 *********************************************************************/
1660 char * actions_to_text(const struct action_spec *action)
1662 unsigned long mask = action->mask;
1663 unsigned long add = action->add;
1664 char *result = strdup_or_die("");
1665 struct list_entry * lst;
1667 /* sanity - prevents "-feature +feature" */
1671 #define DEFINE_ACTION_BOOL(__name, __bit) \
1672 if (!(mask & __bit)) \
1674 string_append(&result, " -" __name " \\\n"); \
1676 else if (add & __bit) \
1678 string_append(&result, " +" __name " \\\n"); \
1681 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1682 if (!(mask & __bit)) \
1684 string_append(&result, " -" __name " \\\n"); \
1686 else if (add & __bit) \
1688 string_append(&result, " +" __name "{"); \
1689 string_append(&result, action->string[__index]); \
1690 string_append(&result, "} \\\n"); \
1693 #define DEFINE_ACTION_MULTI(__name, __index) \
1694 if (action->multi_remove_all[__index]) \
1696 string_append(&result, " -" __name " \\\n"); \
1700 lst = action->multi_remove[__index]->first; \
1703 string_append(&result, " -" __name "{"); \
1704 string_append(&result, lst->str); \
1705 string_append(&result, "} \\\n"); \
1709 lst = action->multi_add[__index]->first; \
1712 string_append(&result, " +" __name "{"); \
1713 string_append(&result, lst->str); \
1714 string_append(&result, "} \\\n"); \
1718 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1720 #include "actionlist.h"
1722 #undef DEFINE_ACTION_MULTI
1723 #undef DEFINE_ACTION_STRING
1724 #undef DEFINE_ACTION_BOOL
1725 #undef DEFINE_ACTION_ALIAS
1731 /*********************************************************************
1733 * Function : actions_to_html
1735 * Description : Converts an actionsfile entry from numeric form
1736 * ("mask" and "add") to a <br>-separated HTML string
1737 * in which each action is linked to its chapter in
1741 * 1 : csp = Client state (for config)
1742 * 2 : action = Action spec to be converted
1744 * Returns : A string. Caller must free it.
1745 * NULL on out-of-memory error.
1747 *********************************************************************/
1748 char * actions_to_html(const struct client_state *csp,
1749 const struct action_spec *action)
1751 unsigned long mask = action->mask;
1752 unsigned long add = action->add;
1753 char *result = strdup_or_die("");
1754 struct list_entry * lst;
1756 /* sanity - prevents "-feature +feature" */
1760 #define DEFINE_ACTION_BOOL(__name, __bit) \
1761 if (!(mask & __bit)) \
1763 string_append(&result, "\n<br>-"); \
1764 string_join(&result, add_help_link(__name, csp->config)); \
1766 else if (add & __bit) \
1768 string_append(&result, "\n<br>+"); \
1769 string_join(&result, add_help_link(__name, csp->config)); \
1772 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1773 if (!(mask & __bit)) \
1775 string_append(&result, "\n<br>-"); \
1776 string_join(&result, add_help_link(__name, csp->config)); \
1778 else if (add & __bit) \
1780 string_append(&result, "\n<br>+"); \
1781 string_join(&result, add_help_link(__name, csp->config)); \
1782 string_append(&result, "{"); \
1783 string_join(&result, html_encode(action->string[__index])); \
1784 string_append(&result, "}"); \
1787 #define DEFINE_ACTION_MULTI(__name, __index) \
1788 if (action->multi_remove_all[__index]) \
1790 string_append(&result, "\n<br>-"); \
1791 string_join(&result, add_help_link(__name, csp->config)); \
1795 lst = action->multi_remove[__index]->first; \
1798 string_append(&result, "\n<br>-"); \
1799 string_join(&result, add_help_link(__name, csp->config)); \
1800 string_append(&result, "{"); \
1801 string_join(&result, html_encode(lst->str)); \
1802 string_append(&result, "}"); \
1806 lst = action->multi_add[__index]->first; \
1809 string_append(&result, "\n<br>+"); \
1810 string_join(&result, add_help_link(__name, csp->config)); \
1811 string_append(&result, "{"); \
1812 string_join(&result, html_encode(lst->str)); \
1813 string_append(&result, "}"); \
1817 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1819 #include "actionlist.h"
1821 #undef DEFINE_ACTION_MULTI
1822 #undef DEFINE_ACTION_STRING
1823 #undef DEFINE_ACTION_BOOL
1824 #undef DEFINE_ACTION_ALIAS
1826 /* trim leading <br> */
1827 if (result && *result)
1830 result = strdup(result + 5);
1838 /*********************************************************************
1840 * Function : current_actions_to_html
1842 * Description : Converts a current action spec to a <br> separated HTML
1843 * text in which each action is linked to its chapter in
1847 * 1 : csp = Client state (for config)
1848 * 2 : action = Current action spec to be converted
1850 * Returns : A string. Caller must free it.
1851 * NULL on out-of-memory error.
1853 *********************************************************************/
1854 char *current_action_to_html(const struct client_state *csp,
1855 const struct current_action_spec *action)
1857 unsigned long flags = action->flags;
1858 struct list_entry * lst;
1859 char *result = strdup_or_die("");
1860 char *active = strdup_or_die("");
1861 char *inactive = strdup_or_die("");
1863 #define DEFINE_ACTION_BOOL(__name, __bit) \
1864 if (flags & __bit) \
1866 string_append(&active, "\n<br>+"); \
1867 string_join(&active, add_help_link(__name, csp->config)); \
1871 string_append(&inactive, "\n<br>-"); \
1872 string_join(&inactive, add_help_link(__name, csp->config)); \
1875 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1876 if (flags & __bit) \
1878 string_append(&active, "\n<br>+"); \
1879 string_join(&active, add_help_link(__name, csp->config)); \
1880 string_append(&active, "{"); \
1881 string_join(&active, html_encode(action->string[__index])); \
1882 string_append(&active, "}"); \
1886 string_append(&inactive, "\n<br>-"); \
1887 string_join(&inactive, add_help_link(__name, csp->config)); \
1890 #define DEFINE_ACTION_MULTI(__name, __index) \
1891 lst = action->multi[__index]->first; \
1894 string_append(&inactive, "\n<br>-"); \
1895 string_join(&inactive, add_help_link(__name, csp->config)); \
1901 string_append(&active, "\n<br>+"); \
1902 string_join(&active, add_help_link(__name, csp->config)); \
1903 string_append(&active, "{"); \
1904 string_join(&active, html_encode(lst->str)); \
1905 string_append(&active, "}"); \
1910 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1912 #include "actionlist.h"
1914 #undef DEFINE_ACTION_MULTI
1915 #undef DEFINE_ACTION_STRING
1916 #undef DEFINE_ACTION_BOOL
1917 #undef DEFINE_ACTION_ALIAS
1921 string_append(&result, active);
1924 string_append(&result, "\n<br>");
1925 if (inactive != NULL)
1927 string_append(&result, inactive);
1934 /*********************************************************************
1936 * Function : action_to_line_of_text
1938 * Description : Converts an action spec to a single text line
1939 * listing the enabled actions.
1942 * 1 : action = Current action spec to be converted
1944 * Returns : A string. Caller must free it.
1945 * Out-of-memory errors are fatal.
1947 *********************************************************************/
1948 char *actions_to_line_of_text(const struct current_action_spec *action)
1951 struct list_entry *lst;
1953 const unsigned long flags = action->flags;
1955 active = strdup_or_die("");
1957 #define DEFINE_ACTION_BOOL(__name, __bit) \
1958 if (flags & __bit) \
1960 snprintf(buffer, sizeof(buffer), "+%s ", __name); \
1961 string_append(&active, buffer); \
1964 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1965 if (flags & __bit) \
1967 snprintf(buffer, sizeof(buffer), "+%s{%s} ", \
1968 __name, action->string[__index]); \
1969 string_append(&active, buffer); \
1972 #define DEFINE_ACTION_MULTI(__name, __index) \
1973 lst = action->multi[__index]->first; \
1974 while (lst != NULL) \
1976 snprintf(buffer, sizeof(buffer), "+%s{%s} ", \
1977 __name, lst->str); \
1978 string_append(&active, buffer); \
1982 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1984 #include "actionlist.h"
1986 #undef DEFINE_ACTION_MULTI
1987 #undef DEFINE_ACTION_STRING
1988 #undef DEFINE_ACTION_BOOL
1989 #undef DEFINE_ACTION_ALIAS
1993 log_error(LOG_LEVEL_FATAL, "Out of memory in action_to_line_of_text()");