1 const char actions_rcs[] = "$Id: actions.c,v 1.89 2013/11/24 14:24:17 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-2011 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
59 const char actions_h_rcs[] = ACTIONS_H_VERSION;
63 * We need the main list of options.
65 * First, we need a way to tell between boolean, string, and multi-string
66 * options. For string and multistring options, we also need to be
67 * able to tell the difference between a "+" and a "-". (For bools,
68 * the "+"/"-" information is encoded in "add" and "mask"). So we use
69 * an enumerated type (well, the preprocessor equivalent). Here are
72 enum action_value_type {
73 AV_NONE = 0, /* +opt -opt */
74 AV_ADD_STRING = 1, /* +stropt{string} */
75 AV_REM_STRING = 2, /* -stropt */
76 AV_ADD_MULTI = 3, /* +multiopt{string} +multiopt{string2} */
77 AV_REM_MULTI = 4 /* -multiopt{string} -multiopt */
81 * We need a structure to hold the name, flag changes,
82 * type, and string index.
87 unsigned long mask; /* a bit set to "0" = remove action */
88 unsigned long add; /* a bit set to "1" = add action */
89 enum action_value_type value_type; /* an AV_... constant */
90 int index; /* index into strings[] or multi[] */
94 * And with those building blocks in place, here's the array.
96 static const struct action_name action_names[] =
99 * Well actually there's no data here - it's in actionlist.h
100 * This keeps it together to make it easy to change.
102 * Here's the macros used to format it:
104 #define DEFINE_ACTION_MULTI(name,index) \
105 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
106 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
107 #define DEFINE_ACTION_STRING(name,flag,index) \
108 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
109 { "-" name, ~flag, 0, AV_REM_STRING, index },
110 #define DEFINE_ACTION_BOOL(name,flag) \
111 { "+" name, ACTION_MASK_ALL, flag }, \
112 { "-" name, ~flag, 0 },
113 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
115 #include "actionlist.h"
117 #undef DEFINE_ACTION_MULTI
118 #undef DEFINE_ACTION_STRING
119 #undef DEFINE_ACTION_BOOL
120 #undef DEFINE_ACTION_ALIAS
122 { NULL, 0, 0 } /* End marker */
126 static 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)
520 /* ignore any option. */
524 /* add single string. */
526 if ((value == NULL) || (*value == '\0'))
528 if (0 == strcmpic(action->name, "+block"))
531 * XXX: Temporary backwards compatibility hack.
532 * XXX: should include line number.
534 value = "No reason specified.";
535 log_error(LOG_LEVEL_ERROR,
536 "block action without reason found. This may "
537 "become a fatal error in future versions.");
544 /* FIXME: should validate option string here */
545 freez (cur_action->string[action->index]);
546 cur_action->string[action->index] = strdup(value);
547 if (NULL == cur_action->string[action->index])
549 return JB_ERR_MEMORY;
555 /* remove single string. */
557 freez (cur_action->string[action->index]);
562 /* append multi string. */
564 struct list * remove_p = cur_action->multi_remove[action->index];
565 struct list * add_p = cur_action->multi_add[action->index];
567 if ((value == NULL) || (*value == '\0'))
572 list_remove_item(remove_p, value);
573 err = enlist_unique(add_p, value, 0);
582 /* remove multi string. */
584 struct list * remove_p = cur_action->multi_remove[action->index];
585 struct list * add_p = cur_action->multi_add[action->index];
587 if ((value == NULL) || (*value == '\0')
588 || ((*value == '*') && (value[1] == '\0')))
591 * no option, or option == "*".
595 list_remove_all(remove_p);
596 list_remove_all(add_p);
597 cur_action->multi_remove_all[action->index] = 1;
601 /* Valid option - remove only 1 option */
603 if (!cur_action->multi_remove_all[action->index])
605 /* there isn't a catch-all in the remove list already */
606 err = enlist_unique(remove_p, value, 0);
612 list_remove_item(add_p, value);
617 /* Shouldn't get here unless there's memory corruption. */
624 /* try user aliases. */
625 const struct action_alias * alias = alias_list;
627 while ((alias != NULL) && (0 != strcmpic(alias->name, option)))
634 merge_actions(cur_action, alias->action);
636 else if (((size_t)2 < strlen(option)) && action_used_to_be_valid(option+1))
638 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
639 "in this Privoxy release. Ignored.", option+1);
641 else if (((size_t)2 < strlen(option)) && 0 == strcmpic(option+1, "hide-forwarded-for-headers"))
643 log_error(LOG_LEVEL_FATAL, "The action 'hide-forwarded-for-headers' "
644 "is no longer valid in this Privoxy release. "
645 "Use 'change-x-forwarded-for' instead.");
649 /* Bad action name */
651 * XXX: This is a fatal error and Privoxy will later on exit
652 * in load_one_actions_file() because of an "invalid line".
654 * It would be preferable to name the offending option in that
655 * error message, but currently there is no way to do that and
656 * we have to live with two error messages for basically the
659 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
670 /*********************************************************************
672 * Function : init_current_action
674 * Description : Zero out an action.
677 * 1 : dest = An uninitialized current_action_spec.
681 *********************************************************************/
682 void init_current_action (struct current_action_spec *dest)
684 memset(dest, '\0', sizeof(*dest));
686 dest->flags = ACTION_MOST_COMPATIBLE;
690 /*********************************************************************
692 * Function : init_action
694 * Description : Zero out an action.
697 * 1 : dest = An uninitialized action_spec.
701 *********************************************************************/
702 void init_action (struct action_spec *dest)
704 memset(dest, '\0', sizeof(*dest));
708 /*********************************************************************
710 * Function : merge_current_action
712 * Description : Merge two actions together.
713 * Similar to "dest += src".
714 * Differences between this and merge_actions()
715 * is that this one doesn't allocate memory for
716 * strings (so "src" better be in memory for at least
717 * as long as "dest" is, and you'd better free
718 * "dest" using "free_current_action").
719 * Also, there is no mask or remove lists in dest.
720 * (If we're applying it to a URL, we don't need them)
723 * 1 : dest = Current actions, to modify.
724 * 2 : src = Action to add.
726 * Returns 0 : no error
727 * !=0 : error, probably JB_ERR_MEMORY.
729 *********************************************************************/
730 jb_err merge_current_action (struct current_action_spec *dest,
731 const struct action_spec *src)
734 jb_err err = JB_ERR_OK;
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 str = strdup_or_die(str);
745 freez(dest->string[i]);
746 dest->string[i] = str;
750 for (i = 0; i < ACTION_MULTI_COUNT; i++)
752 if (src->multi_remove_all[i])
754 /* Remove everything from dest, then add src->multi_add */
755 err = list_duplicate(dest->multi[i], src->multi_add[i]);
763 list_remove_list(dest->multi[i], src->multi_remove[i]);
764 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
775 /*********************************************************************
777 * Function : update_action_bits_for_tag
779 * Description : Updates the action bits based on the action sections
780 * whose tag patterns match a provided tag.
783 * 1 : csp = Current client state (buffers, headers, etc...)
784 * 2 : tag = The tag on which the update should be based on
786 * Returns : 0 if no tag matched, or
789 *********************************************************************/
790 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
792 struct file_list *fl;
793 struct url_actions *b;
799 assert(list_contains_item(csp->tags, tag));
801 /* Run through all action files, */
802 for (i = 0; i < MAX_AF_FILES; i++)
804 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
806 /* Skip empty files */
810 /* and through all the action patterns, */
811 for (b = b->next; NULL != b; b = b->next)
813 /* skip everything but TAG patterns, */
814 if (!(b->url->flags & PATTERN_SPEC_TAG_PATTERN))
819 /* and check if one of the tag patterns matches the tag, */
820 if (0 == regexec(b->url->pattern.tag_regex, tag, 0, NULL, 0))
822 /* if it does, update the action bit map, */
823 if (merge_current_action(csp->action, b->action))
825 log_error(LOG_LEVEL_ERROR,
826 "Out of memory while changing action bits");
828 /* and signal the change. */
838 /*********************************************************************
840 * Function : check_negative_tag_patterns
842 * Description : Updates the action bits based on NO-*-TAG patterns.
845 * 1 : csp = Current client state (buffers, headers, etc...)
846 * 2 : flag = The tag pattern type
848 * Returns : JB_ERR_OK in case off success, or
849 * JB_ERR_MEMORY on out-of-memory error.
851 *********************************************************************/
852 jb_err check_negative_tag_patterns(struct client_state *csp, unsigned int flag)
854 struct list_entry *tag;
855 struct file_list *fl;
856 struct url_actions *b = NULL;
859 for (i = 0; i < MAX_AF_FILES; i++)
861 fl = csp->actions_list[i];
862 if ((fl == NULL) || ((b = fl->f) == NULL))
866 for (b = b->next; NULL != b; b = b->next)
869 if (0 == (b->url->flags & flag))
873 for (tag = csp->tags->first; NULL != tag; tag = tag->next)
875 if (0 == regexec(b->url->pattern.tag_regex, tag->str, 0, NULL, 0))
878 * The pattern matches at least one tag, thus the action
879 * section doesn't apply and we don't need to look at the
889 * The pattern doesn't match any tags,
890 * thus the action section applies.
892 if (merge_current_action(csp->action, b->action))
894 log_error(LOG_LEVEL_ERROR,
895 "Out of memory while changing action bits");
896 return JB_ERR_MEMORY;
898 log_error(LOG_LEVEL_HEADER, "Updated action bits based on: %s",
908 /*********************************************************************
910 * Function : free_current_action
912 * Description : Free memory used by a current_action_spec.
913 * Does not free the current_action_spec itself.
916 * 1 : src = Source to free.
920 *********************************************************************/
921 void free_current_action(struct current_action_spec *src)
925 for (i = 0; i < ACTION_STRING_COUNT; i++)
927 freez(src->string[i]);
930 for (i = 0; i < ACTION_MULTI_COUNT; i++)
932 destroy_list(src->multi[i]);
935 memset(src, '\0', sizeof(*src));
939 static struct file_list *current_actions_file[MAX_AF_FILES] = {
940 NULL, NULL, NULL, NULL, NULL,
941 NULL, NULL, NULL, NULL, NULL
945 #ifdef FEATURE_GRACEFUL_TERMINATION
946 /*********************************************************************
948 * Function : unload_current_actions_file
950 * Description : Unloads current actions file - reset to state at
951 * beginning of program.
957 *********************************************************************/
958 void unload_current_actions_file(void)
962 for (i = 0; i < MAX_AF_FILES; i++)
964 if (current_actions_file[i])
966 current_actions_file[i]->unloader = unload_actions_file;
967 current_actions_file[i] = NULL;
971 #endif /* FEATURE_GRACEFUL_TERMINATION */
974 /*********************************************************************
976 * Function : unload_actions_file
978 * Description : Unloads an actions module.
981 * 1 : file_data = the data structure associated with the
986 *********************************************************************/
987 void unload_actions_file(void *file_data)
989 struct url_actions * next;
990 struct url_actions * cur = (struct url_actions *)file_data;
994 free_pattern_spec(cur->url);
995 if ((next == NULL) || (next->action != cur->action))
998 * As the action settings might be shared,
999 * we can only free them if the current
1000 * url pattern is the last one, or if the
1001 * next one is using different settings.
1003 free_action_spec(cur->action);
1011 /*********************************************************************
1013 * Function : free_alias_list
1015 * Description : Free memory used by a list of aliases.
1018 * 1 : alias_list = Linked list to free.
1022 *********************************************************************/
1023 void free_alias_list(struct action_alias *alias_list)
1025 while (alias_list != NULL)
1027 struct action_alias * next = alias_list->next;
1028 alias_list->next = NULL;
1029 freez(alias_list->name);
1030 free_action(alias_list->action);
1037 /*********************************************************************
1039 * Function : load_action_files
1041 * Description : Read and parse all the action files and add to files
1045 * 1 : csp = Current client state (buffers, headers, etc...)
1047 * Returns : 0 => Ok, everything else is an error.
1049 *********************************************************************/
1050 int load_action_files(struct client_state *csp)
1055 for (i = 0; i < MAX_AF_FILES; i++)
1057 if (csp->config->actions_file[i])
1059 result = load_one_actions_file(csp, i);
1065 else if (current_actions_file[i])
1067 current_actions_file[i]->unloader = unload_actions_file;
1068 current_actions_file[i] = NULL;
1076 /*********************************************************************
1078 * Function : referenced_filters_are_missing
1080 * Description : Checks if any filters of a certain type referenced
1081 * in an action spec are missing.
1084 * 1 : csp = Current client state (buffers, headers, etc...)
1085 * 2 : cur_action = The action spec to check.
1086 * 3 : multi_index = The index where to look for the filter.
1087 * 4 : filter_type = The filter type the caller is interested in.
1089 * Returns : 0 => All referenced filters exists, everything else is an error.
1091 *********************************************************************/
1092 static int referenced_filters_are_missing(const struct client_state *csp,
1093 const struct action_spec *cur_action, int multi_index, enum filter_type filter_type)
1096 struct file_list *fl;
1097 struct re_filterfile_spec *b;
1098 struct list_entry *filtername;
1100 for (filtername = cur_action->multi_add[multi_index]->first;
1101 filtername; filtername = filtername->next)
1103 int filter_found = 0;
1104 for (i = 0; i < MAX_AF_FILES; i++)
1107 if ((NULL == fl) || (NULL == fl->f))
1112 for (b = fl->f; b; b = b->next)
1114 if (b->type != filter_type)
1118 if (strcmp(b->name, filtername->str) == 0)
1126 log_error(LOG_LEVEL_ERROR, "Missing filter '%s'", filtername->str);
1136 /*********************************************************************
1138 * Function : action_spec_is_valid
1140 * Description : Should eventually figure out if an action spec
1141 * is valid, but currently only checks that the
1142 * referenced filters are accounted for.
1145 * 1 : csp = Current client state (buffers, headers, etc...)
1146 * 2 : cur_action = The action spec to check.
1148 * Returns : 0 => No problems detected, everything else is an error.
1150 *********************************************************************/
1151 static int action_spec_is_valid(struct client_state *csp, const struct action_spec *cur_action)
1155 enum filter_type filter_type;
1157 {ACTION_MULTI_FILTER, FT_CONTENT_FILTER},
1158 {ACTION_MULTI_CLIENT_HEADER_FILTER, FT_CLIENT_HEADER_FILTER},
1159 {ACTION_MULTI_SERVER_HEADER_FILTER, FT_SERVER_HEADER_FILTER},
1160 {ACTION_MULTI_CLIENT_HEADER_TAGGER, FT_CLIENT_HEADER_TAGGER},
1161 {ACTION_MULTI_SERVER_HEADER_TAGGER, FT_SERVER_HEADER_TAGGER}
1166 for (i = 0; i < SZ(filter_map); i++)
1168 errors += referenced_filters_are_missing(csp, cur_action,
1169 filter_map[i].multi_index, filter_map[i].filter_type);
1177 /*********************************************************************
1179 * Function : load_one_actions_file
1181 * Description : Read and parse a action file and add to files
1185 * 1 : csp = Current client state (buffers, headers, etc...)
1186 * 2 : fileid = File index to load.
1188 * Returns : 0 => Ok, everything else is an error.
1190 *********************************************************************/
1191 static int load_one_actions_file(struct client_state *csp, int fileid)
1196 * Note: Keep these in the order they occur in the file, they are
1197 * sometimes tested with <=
1200 MODE_START_OF_FILE = 1,
1202 MODE_DESCRIPTION = 3,
1208 struct url_actions *last_perm;
1209 struct url_actions *perm;
1211 struct file_list *fs;
1212 struct action_spec * cur_action = NULL;
1213 int cur_action_used = 0;
1214 struct action_alias * alias_list = NULL;
1215 unsigned long linenum = 0;
1216 mode = MODE_START_OF_FILE;
1218 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1220 /* No need to load */
1221 csp->actions_list[fileid] = current_actions_file[fileid];
1226 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1227 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1228 "with their complete file names.", csp->config->actions_file[fileid]);
1229 return 1; /* never get here */
1232 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1233 if (last_perm == NULL)
1235 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1236 csp->config->actions_file[fileid]);
1237 return 1; /* never get here */
1240 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1242 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1243 csp->config->actions_file[fileid]);
1244 return 1; /* never get here */
1247 log_error(LOG_LEVEL_INFO, "Loading actions file: %s", csp->config->actions_file[fileid]);
1249 while (read_config_line(fp, &linenum, &buf) != NULL)
1253 /* It's a header block */
1256 /* It's {{settings}} or {{alias}} */
1257 size_t len = strlen(buf);
1258 char * start = buf + 2;
1259 char * end = buf + len - 1;
1260 if ((len < (size_t)5) || (*end-- != '}') || (*end-- != '}'))
1264 log_error(LOG_LEVEL_FATAL,
1265 "can't load actions file '%s': invalid line (%lu): %s",
1266 csp->config->actions_file[fileid], linenum, buf);
1267 return 1; /* never get here */
1270 /* Trim leading and trailing whitespace. */
1278 log_error(LOG_LEVEL_FATAL,
1279 "can't load actions file '%s': invalid line (%lu): {{ }}",
1280 csp->config->actions_file[fileid], linenum);
1281 return 1; /* never get here */
1285 * An actionsfile can optionally contain the following blocks.
1286 * They *MUST* be in this order, to simplify processing:
1292 * ...free text, format TBD, but no line may start with a '{'...
1297 * The actual actions must be *after* these special blocks.
1298 * None of these special blocks may be repeated.
1301 if (0 == strcmpic(start, "settings"))
1303 /* it's a {{settings}} block */
1304 if (mode >= MODE_SETTINGS)
1306 /* {{settings}} must be first thing in file and must only
1310 log_error(LOG_LEVEL_FATAL,
1311 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1312 csp->config->actions_file[fileid], linenum);
1314 mode = MODE_SETTINGS;
1316 else if (0 == strcmpic(start, "description"))
1318 /* it's a {{description}} block */
1319 if (mode >= MODE_DESCRIPTION)
1321 /* {{description}} is a singleton and only {{settings}} may proceed it
1324 log_error(LOG_LEVEL_FATAL,
1325 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1326 csp->config->actions_file[fileid], linenum);
1328 mode = MODE_DESCRIPTION;
1330 else if (0 == strcmpic(start, "alias"))
1332 /* it's an {{alias}} block */
1333 if (mode >= MODE_ALIAS)
1335 /* {{alias}} must be first thing in file, possibly after
1336 * {{settings}} and {{description}}
1338 * {{alias}} must only appear once.
1340 * Note that these are new restrictions introduced in
1341 * v2.9.10 in order to make actionsfile editing simpler.
1342 * (Otherwise, reordering actionsfile entries without
1343 * completely rewriting the file becomes non-trivial)
1346 log_error(LOG_LEVEL_FATAL,
1347 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1348 csp->config->actions_file[fileid], linenum);
1354 /* invalid {{something}} block */
1356 log_error(LOG_LEVEL_FATAL,
1357 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1358 csp->config->actions_file[fileid], linenum, start);
1359 return 1; /* never get here */
1364 /* It's an actions block */
1370 mode = MODE_ACTIONS;
1372 /* free old action */
1375 if (!cur_action_used)
1377 free_action_spec(cur_action);
1381 cur_action_used = 0;
1382 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1383 if (cur_action == NULL)
1386 log_error(LOG_LEVEL_FATAL,
1387 "can't load actions file '%s': out of memory",
1388 csp->config->actions_file[fileid]);
1389 return 1; /* never get here */
1391 init_action(cur_action);
1394 * Copy the buffer before messing with it as we may need the
1395 * unmodified version in for the fatal error messages. Given
1396 * that this is not a common event, we could instead simply
1397 * read the line again.
1399 * buf + 1 to skip the leading '{'
1401 actions_buf = strdup_or_die(buf + 1);
1403 /* check we have a trailing } and then trim it */
1404 end = actions_buf + strlen(actions_buf) - 1;
1410 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1411 "Missing trailing '}' in action section starting at line (%lu): %s",
1412 csp->config->actions_file[fileid], linenum, buf);
1413 return 1; /* never get here */
1417 /* trim any whitespace immediately inside {} */
1420 if (get_actions(actions_buf, alias_list, cur_action))
1425 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1426 "can't completely parse the action section starting at line (%lu): %s",
1427 csp->config->actions_file[fileid], linenum, buf);
1428 return 1; /* never get here */
1431 if (action_spec_is_valid(csp, cur_action))
1433 log_error(LOG_LEVEL_ERROR, "Invalid action section in file '%s', "
1434 "starting at line %lu: %s",
1435 csp->config->actions_file[fileid], linenum, buf);
1441 else if (mode == MODE_SETTINGS)
1444 * Part of the {{settings}} block.
1445 * For now only serves to check if the file's minimum Privoxy
1446 * version requirement is met, but we may want to read & check
1447 * permissions when we go multi-user.
1449 if (!strncmp(buf, "for-privoxy-version=", 20))
1451 char *version_string, *fields[3];
1454 version_string = strdup_or_die(buf + 20);
1456 num_fields = ssplit(version_string, ".", fields, SZ(fields));
1458 if (num_fields < 1 || atoi(fields[0]) == 0)
1460 log_error(LOG_LEVEL_ERROR,
1461 "While loading actions file '%s': invalid line (%lu): %s",
1462 csp->config->actions_file[fileid], linenum, buf);
1464 else if ( (atoi(fields[0]) > VERSION_MAJOR)
1465 || ((num_fields > 1) && (atoi(fields[1]) > VERSION_MINOR))
1466 || ((num_fields > 2) && (atoi(fields[2]) > VERSION_POINT)))
1469 log_error(LOG_LEVEL_FATAL,
1470 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1471 csp->config->actions_file[fileid], linenum, buf);
1472 return 1; /* never get here */
1474 free(version_string);
1477 else if (mode == MODE_DESCRIPTION)
1480 * Part of the {{description}} block.
1484 else if (mode == MODE_ALIAS)
1489 char actions_buf[BUFFER_SIZE];
1490 struct action_alias * new_alias;
1492 char * start = strchr(buf, '=');
1495 if ((start == NULL) || (start == buf))
1497 log_error(LOG_LEVEL_FATAL,
1498 "can't load actions file '%s': invalid alias line (%lu): %s",
1499 csp->config->actions_file[fileid], linenum, buf);
1500 return 1; /* never get here */
1503 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1506 log_error(LOG_LEVEL_FATAL,
1507 "can't load actions file '%s': out of memory!",
1508 csp->config->actions_file[fileid]);
1509 return 1; /* never get here */
1512 /* Eat any the whitespace before the '=' */
1514 while ((*end == ' ') || (*end == '\t'))
1517 * we already know we must have at least 1 non-ws char
1518 * at start of buf - no need to check
1524 /* Eat any the whitespace after the '=' */
1526 while ((*start == ' ') || (*start == '\t'))
1532 log_error(LOG_LEVEL_FATAL,
1533 "can't load actions file '%s': invalid alias line (%lu): %s",
1534 csp->config->actions_file[fileid], linenum, buf);
1535 return 1; /* never get here */
1538 new_alias->name = strdup_or_die(buf);
1540 strlcpy(actions_buf, start, sizeof(actions_buf));
1542 if (get_actions(actions_buf, alias_list, new_alias->action))
1546 log_error(LOG_LEVEL_FATAL,
1547 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1548 csp->config->actions_file[fileid], linenum, buf, start);
1549 return 1; /* never get here */
1553 new_alias->next = alias_list;
1554 alias_list = new_alias;
1556 else if (mode == MODE_ACTIONS)
1558 /* it's an URL pattern */
1560 /* allocate a new node */
1561 if ((perm = zalloc(sizeof(*perm))) == NULL)
1564 log_error(LOG_LEVEL_FATAL,
1565 "can't load actions file '%s': out of memory!",
1566 csp->config->actions_file[fileid]);
1567 return 1; /* never get here */
1570 perm->action = cur_action;
1571 cur_action_used = 1;
1573 /* Save the URL pattern */
1574 if (create_pattern_spec(perm->url, buf))
1577 log_error(LOG_LEVEL_FATAL,
1578 "can't load actions file '%s': line %lu: cannot create URL or TAG pattern from: %s",
1579 csp->config->actions_file[fileid], linenum, buf);
1580 return 1; /* never get here */
1583 /* add it to the list */
1584 last_perm->next = perm;
1587 else if (mode == MODE_START_OF_FILE)
1589 /* oops - please have a {} line as 1st line in file. */
1591 log_error(LOG_LEVEL_FATAL,
1592 "can't load actions file '%s': line %lu should begin with a '{': %s",
1593 csp->config->actions_file[fileid], linenum, buf);
1594 return 1; /* never get here */
1598 /* How did we get here? This is impossible! */
1600 log_error(LOG_LEVEL_FATAL,
1601 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1602 csp->config->actions_file[fileid], mode);
1603 return 1; /* never get here */
1610 if (!cur_action_used)
1612 free_action_spec(cur_action);
1614 free_alias_list(alias_list);
1616 /* the old one is now obsolete */
1617 if (current_actions_file[fileid])
1619 current_actions_file[fileid]->unloader = unload_actions_file;
1622 fs->next = files->next;
1624 current_actions_file[fileid] = fs;
1626 csp->actions_list[fileid] = fs;
1633 /*********************************************************************
1635 * Function : actions_to_text
1637 * Description : Converts a actionsfile entry from the internal
1638 * structure into a text line. The output is split
1639 * into one line for each action with line continuation.
1642 * 1 : action = The action to format.
1644 * Returns : A string. Caller must free it.
1645 * NULL on out-of-memory error.
1647 *********************************************************************/
1648 char * actions_to_text(const struct action_spec *action)
1650 unsigned long mask = action->mask;
1651 unsigned long add = action->add;
1652 char *result = strdup_or_die("");
1653 struct list_entry * lst;
1655 /* sanity - prevents "-feature +feature" */
1659 #define DEFINE_ACTION_BOOL(__name, __bit) \
1660 if (!(mask & __bit)) \
1662 string_append(&result, " -" __name " \\\n"); \
1664 else if (add & __bit) \
1666 string_append(&result, " +" __name " \\\n"); \
1669 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1670 if (!(mask & __bit)) \
1672 string_append(&result, " -" __name " \\\n"); \
1674 else if (add & __bit) \
1676 string_append(&result, " +" __name "{"); \
1677 string_append(&result, action->string[__index]); \
1678 string_append(&result, "} \\\n"); \
1681 #define DEFINE_ACTION_MULTI(__name, __index) \
1682 if (action->multi_remove_all[__index]) \
1684 string_append(&result, " -" __name " \\\n"); \
1688 lst = action->multi_remove[__index]->first; \
1691 string_append(&result, " -" __name "{"); \
1692 string_append(&result, lst->str); \
1693 string_append(&result, "} \\\n"); \
1697 lst = action->multi_add[__index]->first; \
1700 string_append(&result, " +" __name "{"); \
1701 string_append(&result, lst->str); \
1702 string_append(&result, "} \\\n"); \
1706 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1708 #include "actionlist.h"
1710 #undef DEFINE_ACTION_MULTI
1711 #undef DEFINE_ACTION_STRING
1712 #undef DEFINE_ACTION_BOOL
1713 #undef DEFINE_ACTION_ALIAS
1719 /*********************************************************************
1721 * Function : actions_to_html
1723 * Description : Converts a actionsfile entry from numeric form
1724 * ("mask" and "add") to a <br>-separated HTML string
1725 * in which each action is linked to its chapter in
1729 * 1 : csp = Client state (for config)
1730 * 2 : action = Action spec to be converted
1732 * Returns : A string. Caller must free it.
1733 * NULL on out-of-memory error.
1735 *********************************************************************/
1736 char * actions_to_html(const struct client_state *csp,
1737 const struct action_spec *action)
1739 unsigned long mask = action->mask;
1740 unsigned long add = action->add;
1741 char *result = strdup_or_die("");
1742 struct list_entry * lst;
1744 /* sanity - prevents "-feature +feature" */
1748 #define DEFINE_ACTION_BOOL(__name, __bit) \
1749 if (!(mask & __bit)) \
1751 string_append(&result, "\n<br>-"); \
1752 string_join(&result, add_help_link(__name, csp->config)); \
1754 else if (add & __bit) \
1756 string_append(&result, "\n<br>+"); \
1757 string_join(&result, add_help_link(__name, csp->config)); \
1760 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
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)); \
1770 string_append(&result, "{"); \
1771 string_join(&result, html_encode(action->string[__index])); \
1772 string_append(&result, "}"); \
1775 #define DEFINE_ACTION_MULTI(__name, __index) \
1776 if (action->multi_remove_all[__index]) \
1778 string_append(&result, "\n<br>-"); \
1779 string_join(&result, add_help_link(__name, csp->config)); \
1783 lst = action->multi_remove[__index]->first; \
1786 string_append(&result, "\n<br>-"); \
1787 string_join(&result, add_help_link(__name, csp->config)); \
1788 string_append(&result, "{"); \
1789 string_join(&result, html_encode(lst->str)); \
1790 string_append(&result, "}"); \
1794 lst = action->multi_add[__index]->first; \
1797 string_append(&result, "\n<br>+"); \
1798 string_join(&result, add_help_link(__name, csp->config)); \
1799 string_append(&result, "{"); \
1800 string_join(&result, html_encode(lst->str)); \
1801 string_append(&result, "}"); \
1805 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1807 #include "actionlist.h"
1809 #undef DEFINE_ACTION_MULTI
1810 #undef DEFINE_ACTION_STRING
1811 #undef DEFINE_ACTION_BOOL
1812 #undef DEFINE_ACTION_ALIAS
1814 /* trim leading <br> */
1815 if (result && *result)
1818 result = strdup(result + 5);
1826 /*********************************************************************
1828 * Function : current_actions_to_html
1830 * Description : Converts a curren action spec to a <br> separated HTML
1831 * text in which each action is linked to its chapter in
1835 * 1 : csp = Client state (for config)
1836 * 2 : action = Current action spec to be converted
1838 * Returns : A string. Caller must free it.
1839 * NULL on out-of-memory error.
1841 *********************************************************************/
1842 char *current_action_to_html(const struct client_state *csp,
1843 const struct current_action_spec *action)
1845 unsigned long flags = action->flags;
1846 struct list_entry * lst;
1847 char *result = strdup_or_die("");
1848 char *active = strdup_or_die("");
1849 char *inactive = strdup_or_die("");
1851 #define DEFINE_ACTION_BOOL(__name, __bit) \
1852 if (flags & __bit) \
1854 string_append(&active, "\n<br>+"); \
1855 string_join(&active, add_help_link(__name, csp->config)); \
1859 string_append(&inactive, "\n<br>-"); \
1860 string_join(&inactive, add_help_link(__name, csp->config)); \
1863 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1864 if (flags & __bit) \
1866 string_append(&active, "\n<br>+"); \
1867 string_join(&active, add_help_link(__name, csp->config)); \
1868 string_append(&active, "{"); \
1869 string_join(&active, html_encode(action->string[__index])); \
1870 string_append(&active, "}"); \
1874 string_append(&inactive, "\n<br>-"); \
1875 string_join(&inactive, add_help_link(__name, csp->config)); \
1878 #define DEFINE_ACTION_MULTI(__name, __index) \
1879 lst = action->multi[__index]->first; \
1882 string_append(&inactive, "\n<br>-"); \
1883 string_join(&inactive, add_help_link(__name, csp->config)); \
1889 string_append(&active, "\n<br>+"); \
1890 string_join(&active, add_help_link(__name, csp->config)); \
1891 string_append(&active, "{"); \
1892 string_join(&active, html_encode(lst->str)); \
1893 string_append(&active, "}"); \
1898 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1900 #include "actionlist.h"
1902 #undef DEFINE_ACTION_MULTI
1903 #undef DEFINE_ACTION_STRING
1904 #undef DEFINE_ACTION_BOOL
1905 #undef DEFINE_ACTION_ALIAS
1909 string_append(&result, active);
1912 string_append(&result, "\n<br>");
1913 if (inactive != NULL)
1915 string_append(&result, inactive);
1922 /*********************************************************************
1924 * Function : action_to_line_of_text
1926 * Description : Converts a action spec to a single text line
1927 * listing the enabled actions.
1930 * 1 : action = Current action spec to be converted
1932 * Returns : A string. Caller must free it.
1933 * Out-of-memory errors are fatal.
1935 *********************************************************************/
1936 char *actions_to_line_of_text(const struct current_action_spec *action)
1939 struct list_entry *lst;
1941 const unsigned long flags = action->flags;
1943 active = strdup_or_die("");
1945 #define DEFINE_ACTION_BOOL(__name, __bit) \
1946 if (flags & __bit) \
1948 snprintf(buffer, sizeof(buffer), "+%s ", __name); \
1949 string_append(&active, buffer); \
1952 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1953 if (flags & __bit) \
1955 snprintf(buffer, sizeof(buffer), "+%s{%s} ", \
1956 __name, action->string[__index]); \
1957 string_append(&active, buffer); \
1960 #define DEFINE_ACTION_MULTI(__name, __index) \
1961 lst = action->multi[__index]->first; \
1962 while (lst != NULL) \
1964 snprintf(buffer, sizeof(buffer), "+%s{%s} ", \
1965 __name, lst->str); \
1966 string_append(&active, buffer); \
1970 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1972 #include "actionlist.h"
1974 #undef DEFINE_ACTION_MULTI
1975 #undef DEFINE_ACTION_STRING
1976 #undef DEFINE_ACTION_BOOL
1977 #undef DEFINE_ACTION_ALIAS
1981 log_error(LOG_LEVEL_FATAL, "Out of memory in action_to_line_of_text()");