1 const char actions_rcs[] = "$Id: actions.c,v 1.94 2016/01/16 12:29:30 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 */
127 static int load_one_actions_file(struct client_state *csp, int fileid);
130 /*********************************************************************
132 * Function : merge_actions
134 * Description : Merge two actions together.
135 * Similar to "dest += src".
138 * 1 : dest = Actions to modify.
139 * 2 : src = Action to add.
141 * Returns : JB_ERR_OK or JB_ERR_MEMORY
143 *********************************************************************/
144 jb_err merge_actions (struct action_spec *dest,
145 const struct action_spec *src)
150 dest->mask &= src->mask;
151 dest->add &= src->mask;
152 dest->add |= src->add;
154 for (i = 0; i < ACTION_STRING_COUNT; i++)
156 char * str = src->string[i];
159 freez(dest->string[i]);
160 dest->string[i] = strdup_or_die(str);
164 for (i = 0; i < ACTION_MULTI_COUNT; i++)
166 if (src->multi_remove_all[i])
168 /* Remove everything from dest */
169 list_remove_all(dest->multi_remove[i]);
170 dest->multi_remove_all[i] = 1;
172 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
174 else if (dest->multi_remove_all[i])
177 * dest already removes everything, so we only need to worry
180 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
181 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
185 /* No "remove all"s to worry about. */
186 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
187 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
188 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
201 /*********************************************************************
203 * Function : copy_action
205 * Description : Copy an action_specs.
206 * Similar to "dest = src".
209 * 1 : dest = Destination of copy.
210 * 2 : src = Source for copy.
212 * Returns : JB_ERR_OK or JB_ERR_MEMORY
214 *********************************************************************/
215 jb_err copy_action (struct action_spec *dest,
216 const struct action_spec *src)
219 jb_err err = JB_ERR_OK;
222 memset(dest, '\0', sizeof(*dest));
224 dest->mask = src->mask;
225 dest->add = src->add;
227 for (i = 0; i < ACTION_STRING_COUNT; i++)
229 char * str = src->string[i];
232 str = strdup_or_die(str);
233 dest->string[i] = str;
237 for (i = 0; i < ACTION_MULTI_COUNT; i++)
239 dest->multi_remove_all[i] = src->multi_remove_all[i];
240 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
245 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
254 /*********************************************************************
256 * Function : free_action_spec
258 * Description : Frees an action_spec and the memory used by it.
261 * 1 : src = Source to free.
265 *********************************************************************/
266 void free_action_spec(struct action_spec *src)
273 /*********************************************************************
275 * Function : free_action
277 * Description : Destroy an action_spec. Frees memory used by it,
278 * except for the memory used by the struct action_spec
282 * 1 : src = Source to free.
286 *********************************************************************/
287 void free_action (struct action_spec *src)
296 for (i = 0; i < ACTION_STRING_COUNT; i++)
298 freez(src->string[i]);
301 for (i = 0; i < ACTION_MULTI_COUNT; i++)
303 destroy_list(src->multi_remove[i]);
304 destroy_list(src->multi_add[i]);
307 memset(src, '\0', sizeof(*src));
311 /*********************************************************************
313 * Function : get_action_token
315 * Description : Parses a line for the first action.
316 * Modifies its input array, doesn't allocate memory.
318 * *line=" +abc{def} -ghi "
325 * 1 : line = [in] The line containing the action.
326 * [out] Start of next action on line, or
327 * NULL if we reached the end of line before
328 * we found an action.
329 * 2 : name = [out] Start of action name, null
330 * terminated. NULL on EOL
331 * 3 : value = [out] Start of action value, null
332 * terminated. NULL if none or EOL.
334 * Returns : JB_ERR_OK => Ok
335 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
337 *********************************************************************/
338 jb_err get_action_token(char **line, char **name, char **value)
343 /* set default returns */
348 /* Eat any leading whitespace */
349 while ((*str == ' ') || (*str == '\t'))
361 /* null name, just value is prohibited */
368 while (((ch = *str) != '\0') &&
369 (ch != ' ') && (ch != '\t') && (ch != '{'))
373 /* error, '}' without '{' */
385 /* EOL - be careful not to run off buffer */
390 /* More to parse next time. */
399 /* The value ends with the first non-escaped closing curly brace */
400 while ((str = strchr(str, '}')) != NULL)
404 /* Overwrite the '\' so the action doesn't see it. */
405 string_move(str-1, str);
426 /*********************************************************************
428 * Function : action_used_to_be_valid
430 * Description : Checks if unrecognized actions were valid in earlier
434 * 1 : action = The string containing the action to check.
436 * Returns : True if yes, otherwise false.
438 *********************************************************************/
439 static int action_used_to_be_valid(const char *action)
441 static const char * const formerly_valid_actions[] = {
444 "send-vanilla-wafer",
446 "treat-forbidden-connects-like-blocks",
452 for (i = 0; i < SZ(formerly_valid_actions); i++)
454 if (0 == strcmpic(action, formerly_valid_actions[i]))
463 /*********************************************************************
465 * Function : get_actions
467 * Description : Parses a list of actions.
470 * 1 : line = The string containing the actions.
471 * Will be written to by this function.
472 * 2 : alias_list = Custom alias list, or NULL for none.
473 * 3 : cur_action = Where to store the action. Caller
476 * Returns : JB_ERR_OK => Ok
477 * JB_ERR_PARSE => Parse error (line was trashed anyway)
478 * nonzero => Out of memory (line was trashed anyway)
480 *********************************************************************/
481 jb_err get_actions(char *line,
482 struct action_alias * alias_list,
483 struct action_spec *cur_action)
486 init_action(cur_action);
487 cur_action->mask = ACTION_MASK_ALL;
491 char * option = NULL;
494 err = get_action_token(&line, &option, &value);
502 /* handle option in 'option' */
504 /* Check for standard action name */
505 const struct action_name * action = action_names;
507 while ((action->name != NULL) && (0 != strcmpic(action->name, option)))
511 if (action->name != NULL)
514 cur_action->mask &= action->mask;
515 cur_action->add &= action->mask;
516 cur_action->add |= action->add;
518 switch (action->value_type)
523 log_error(LOG_LEVEL_ERROR,
524 "Action %s does not take parameters but %s was given.",
525 action->name, value);
531 /* add single string. */
533 if ((value == NULL) || (*value == '\0'))
535 if (0 == strcmpic(action->name, "+block"))
538 * XXX: Temporary backwards compatibility hack.
539 * XXX: should include line number.
541 value = "No reason specified.";
542 log_error(LOG_LEVEL_ERROR,
543 "block action without reason found. This may "
544 "become a fatal error in future versions.");
551 /* FIXME: should validate option string here */
552 freez (cur_action->string[action->index]);
553 cur_action->string[action->index] = strdup(value);
554 if (NULL == cur_action->string[action->index])
556 return JB_ERR_MEMORY;
562 /* remove single string. */
564 freez (cur_action->string[action->index]);
569 /* append multi string. */
571 struct list * remove_p = cur_action->multi_remove[action->index];
572 struct list * add_p = cur_action->multi_add[action->index];
574 if ((value == NULL) || (*value == '\0'))
579 list_remove_item(remove_p, value);
580 err = enlist_unique(add_p, value, 0);
589 /* remove multi string. */
591 struct list * remove_p = cur_action->multi_remove[action->index];
592 struct list * add_p = cur_action->multi_add[action->index];
594 if ((value == NULL) || (*value == '\0')
595 || ((*value == '*') && (value[1] == '\0')))
598 * no option, or option == "*".
602 list_remove_all(remove_p);
603 list_remove_all(add_p);
604 cur_action->multi_remove_all[action->index] = 1;
608 /* Valid option - remove only 1 option */
610 if (!cur_action->multi_remove_all[action->index])
612 /* there isn't a catch-all in the remove list already */
613 err = enlist_unique(remove_p, value, 0);
619 list_remove_item(add_p, value);
624 /* Shouldn't get here unless there's memory corruption. */
631 /* try user aliases. */
632 const struct action_alias * alias = alias_list;
634 while ((alias != NULL) && (0 != strcmpic(alias->name, option)))
641 merge_actions(cur_action, alias->action);
643 else if (((size_t)2 < strlen(option)) && action_used_to_be_valid(option+1))
645 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
646 "in this Privoxy release. Ignored.", option+1);
648 else if (((size_t)2 < strlen(option)) && 0 == strcmpic(option+1, "hide-forwarded-for-headers"))
650 log_error(LOG_LEVEL_FATAL, "The action 'hide-forwarded-for-headers' "
651 "is no longer valid in this Privoxy release. "
652 "Use 'change-x-forwarded-for' instead.");
656 /* Bad action name */
658 * XXX: This is a fatal error and Privoxy will later on exit
659 * in load_one_actions_file() because of an "invalid line".
661 * It would be preferable to name the offending option in that
662 * error message, but currently there is no way to do that and
663 * we have to live with two error messages for basically the
666 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
677 /*********************************************************************
679 * Function : init_current_action
681 * Description : Zero out an action.
684 * 1 : dest = An uninitialized current_action_spec.
688 *********************************************************************/
689 void init_current_action (struct current_action_spec *dest)
691 memset(dest, '\0', sizeof(*dest));
693 dest->flags = ACTION_MOST_COMPATIBLE;
697 /*********************************************************************
699 * Function : init_action
701 * Description : Zero out an action.
704 * 1 : dest = An uninitialized action_spec.
708 *********************************************************************/
709 void init_action (struct action_spec *dest)
711 memset(dest, '\0', sizeof(*dest));
715 /*********************************************************************
717 * Function : merge_current_action
719 * Description : Merge two actions together.
720 * Similar to "dest += src".
721 * Differences between this and merge_actions()
722 * is that this one doesn't allocate memory for
723 * strings (so "src" better be in memory for at least
724 * as long as "dest" is, and you'd better free
725 * "dest" using "free_current_action").
726 * Also, there is no mask or remove lists in dest.
727 * (If we're applying it to a URL, we don't need them)
730 * 1 : dest = Current actions, to modify.
731 * 2 : src = Action to add.
733 * Returns 0 : no error
734 * !=0 : error, probably JB_ERR_MEMORY.
736 *********************************************************************/
737 jb_err merge_current_action (struct current_action_spec *dest,
738 const struct action_spec *src)
741 jb_err err = JB_ERR_OK;
743 dest->flags &= src->mask;
744 dest->flags |= src->add;
746 for (i = 0; i < ACTION_STRING_COUNT; i++)
748 char * str = src->string[i];
751 str = strdup_or_die(str);
752 freez(dest->string[i]);
753 dest->string[i] = str;
757 for (i = 0; i < ACTION_MULTI_COUNT; i++)
759 if (src->multi_remove_all[i])
761 /* Remove everything from dest, then add src->multi_add */
762 err = list_duplicate(dest->multi[i], src->multi_add[i]);
770 list_remove_list(dest->multi[i], src->multi_remove[i]);
771 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
782 /*********************************************************************
784 * Function : update_action_bits_for_tag
786 * Description : Updates the action bits based on the action sections
787 * whose tag patterns match a provided tag.
790 * 1 : csp = Current client state (buffers, headers, etc...)
791 * 2 : tag = The tag on which the update should be based on
793 * Returns : 0 if no tag matched, or
796 *********************************************************************/
797 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
799 struct file_list *fl;
800 struct url_actions *b;
806 assert(list_contains_item(csp->tags, tag));
808 /* Run through all action files, */
809 for (i = 0; i < MAX_AF_FILES; i++)
811 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
813 /* Skip empty files */
817 /* and through all the action patterns, */
818 for (b = b->next; NULL != b; b = b->next)
820 /* skip everything but TAG patterns, */
821 if (!(b->url->flags & PATTERN_SPEC_TAG_PATTERN))
826 /* and check if one of the tag patterns matches the tag, */
827 if (0 == regexec(b->url->pattern.tag_regex, tag, 0, NULL, 0))
829 /* if it does, update the action bit map, */
830 if (merge_current_action(csp->action, b->action))
832 log_error(LOG_LEVEL_ERROR,
833 "Out of memory while changing action bits");
835 /* and signal the change. */
845 /*********************************************************************
847 * Function : check_negative_tag_patterns
849 * Description : Updates the action bits based on NO-*-TAG patterns.
852 * 1 : csp = Current client state (buffers, headers, etc...)
853 * 2 : flag = The tag pattern type
855 * Returns : JB_ERR_OK in case off success, or
856 * JB_ERR_MEMORY on out-of-memory error.
858 *********************************************************************/
859 jb_err check_negative_tag_patterns(struct client_state *csp, unsigned int flag)
861 struct list_entry *tag;
862 struct file_list *fl;
863 struct url_actions *b = NULL;
866 for (i = 0; i < MAX_AF_FILES; i++)
868 fl = csp->actions_list[i];
869 if ((fl == NULL) || ((b = fl->f) == NULL))
873 for (b = b->next; NULL != b; b = b->next)
876 if (0 == (b->url->flags & flag))
880 for (tag = csp->tags->first; NULL != tag; tag = tag->next)
882 if (0 == regexec(b->url->pattern.tag_regex, tag->str, 0, NULL, 0))
885 * The pattern matches at least one tag, thus the action
886 * section doesn't apply and we don't need to look at the
896 * The pattern doesn't match any tags,
897 * thus the action section applies.
899 if (merge_current_action(csp->action, b->action))
901 log_error(LOG_LEVEL_ERROR,
902 "Out of memory while changing action bits");
903 return JB_ERR_MEMORY;
905 log_error(LOG_LEVEL_HEADER, "Updated action bits based on: %s",
915 /*********************************************************************
917 * Function : free_current_action
919 * Description : Free memory used by a current_action_spec.
920 * Does not free the current_action_spec itself.
923 * 1 : src = Source to free.
927 *********************************************************************/
928 void free_current_action(struct current_action_spec *src)
932 for (i = 0; i < ACTION_STRING_COUNT; i++)
934 freez(src->string[i]);
937 for (i = 0; i < ACTION_MULTI_COUNT; i++)
939 destroy_list(src->multi[i]);
942 memset(src, '\0', sizeof(*src));
946 static struct file_list *current_actions_file[MAX_AF_FILES] = {
947 NULL, NULL, NULL, NULL, NULL,
948 NULL, NULL, NULL, NULL, NULL
952 #ifdef FEATURE_GRACEFUL_TERMINATION
953 /*********************************************************************
955 * Function : unload_current_actions_file
957 * Description : Unloads current actions file - reset to state at
958 * beginning of program.
964 *********************************************************************/
965 void unload_current_actions_file(void)
969 for (i = 0; i < MAX_AF_FILES; i++)
971 if (current_actions_file[i])
973 current_actions_file[i]->unloader = unload_actions_file;
974 current_actions_file[i] = NULL;
978 #endif /* FEATURE_GRACEFUL_TERMINATION */
981 /*********************************************************************
983 * Function : unload_actions_file
985 * Description : Unloads an actions module.
988 * 1 : file_data = the data structure associated with the
993 *********************************************************************/
994 void unload_actions_file(void *file_data)
996 struct url_actions * next;
997 struct url_actions * cur = (struct url_actions *)file_data;
1001 free_pattern_spec(cur->url);
1002 if ((next == NULL) || (next->action != cur->action))
1005 * As the action settings might be shared,
1006 * we can only free them if the current
1007 * url pattern is the last one, or if the
1008 * next one is using different settings.
1010 free_action_spec(cur->action);
1018 /*********************************************************************
1020 * Function : free_alias_list
1022 * Description : Free memory used by a list of aliases.
1025 * 1 : alias_list = Linked list to free.
1029 *********************************************************************/
1030 void free_alias_list(struct action_alias *alias_list)
1032 while (alias_list != NULL)
1034 struct action_alias * next = alias_list->next;
1035 alias_list->next = NULL;
1036 freez(alias_list->name);
1037 free_action(alias_list->action);
1044 /*********************************************************************
1046 * Function : load_action_files
1048 * Description : Read and parse all the action files and add to files
1052 * 1 : csp = Current client state (buffers, headers, etc...)
1054 * Returns : 0 => Ok, everything else is an error.
1056 *********************************************************************/
1057 int load_action_files(struct client_state *csp)
1062 for (i = 0; i < MAX_AF_FILES; i++)
1064 if (csp->config->actions_file[i])
1066 result = load_one_actions_file(csp, i);
1072 else if (current_actions_file[i])
1074 current_actions_file[i]->unloader = unload_actions_file;
1075 current_actions_file[i] = NULL;
1083 /*********************************************************************
1085 * Function : referenced_filters_are_missing
1087 * Description : Checks if any filters of a certain type referenced
1088 * in an action spec are missing.
1091 * 1 : csp = Current client state (buffers, headers, etc...)
1092 * 2 : cur_action = The action spec to check.
1093 * 3 : multi_index = The index where to look for the filter.
1094 * 4 : filter_type = The filter type the caller is interested in.
1096 * Returns : 0 => All referenced filters exist, everything else is an error.
1098 *********************************************************************/
1099 static int referenced_filters_are_missing(const struct client_state *csp,
1100 const struct action_spec *cur_action, int multi_index, enum filter_type filter_type)
1102 struct list_entry *filtername;
1104 for (filtername = cur_action->multi_add[multi_index]->first;
1105 filtername; filtername = filtername->next)
1107 if (NULL == get_filter(csp, filtername->str, filter_type))
1109 log_error(LOG_LEVEL_ERROR, "Missing filter '%s'", filtername->str);
1119 /*********************************************************************
1121 * Function : action_spec_is_valid
1123 * Description : Should eventually figure out if an action spec
1124 * is valid, but currently only checks that the
1125 * referenced filters are accounted for.
1128 * 1 : csp = Current client state (buffers, headers, etc...)
1129 * 2 : cur_action = The action spec to check.
1131 * Returns : 0 => No problems detected, everything else is an error.
1133 *********************************************************************/
1134 static int action_spec_is_valid(struct client_state *csp, const struct action_spec *cur_action)
1138 enum filter_type filter_type;
1140 {ACTION_MULTI_FILTER, FT_CONTENT_FILTER},
1141 {ACTION_MULTI_CLIENT_HEADER_FILTER, FT_CLIENT_HEADER_FILTER},
1142 {ACTION_MULTI_SERVER_HEADER_FILTER, FT_SERVER_HEADER_FILTER},
1143 {ACTION_MULTI_CLIENT_HEADER_TAGGER, FT_CLIENT_HEADER_TAGGER},
1144 {ACTION_MULTI_SERVER_HEADER_TAGGER, FT_SERVER_HEADER_TAGGER}
1149 for (i = 0; i < SZ(filter_map); i++)
1151 errors += referenced_filters_are_missing(csp, cur_action,
1152 filter_map[i].multi_index, filter_map[i].filter_type);
1160 /*********************************************************************
1162 * Function : load_one_actions_file
1164 * Description : Read and parse a action file and add to files
1168 * 1 : csp = Current client state (buffers, headers, etc...)
1169 * 2 : fileid = File index to load.
1171 * Returns : 0 => Ok, everything else is an error.
1173 *********************************************************************/
1174 static int load_one_actions_file(struct client_state *csp, int fileid)
1179 * Note: Keep these in the order they occur in the file, they are
1180 * sometimes tested with <=
1183 MODE_START_OF_FILE = 1,
1185 MODE_DESCRIPTION = 3,
1191 struct url_actions *last_perm;
1192 struct url_actions *perm;
1194 struct file_list *fs;
1195 struct action_spec * cur_action = NULL;
1196 int cur_action_used = 0;
1197 struct action_alias * alias_list = NULL;
1198 unsigned long linenum = 0;
1199 mode = MODE_START_OF_FILE;
1201 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1203 /* No need to load */
1204 csp->actions_list[fileid] = current_actions_file[fileid];
1209 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1210 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1211 "with their complete file names.", csp->config->actions_file[fileid]);
1212 return 1; /* never get here */
1215 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1216 if (last_perm == NULL)
1218 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1219 csp->config->actions_file[fileid]);
1220 return 1; /* never get here */
1223 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1225 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1226 csp->config->actions_file[fileid]);
1227 return 1; /* never get here */
1230 log_error(LOG_LEVEL_INFO, "Loading actions file: %s", csp->config->actions_file[fileid]);
1232 while (read_config_line(fp, &linenum, &buf) != NULL)
1236 /* It's a header block */
1239 /* It's {{settings}} or {{alias}} */
1240 size_t len = strlen(buf);
1241 char * start = buf + 2;
1242 char * end = buf + len - 1;
1243 if ((len < (size_t)5) || (*end-- != '}') || (*end-- != '}'))
1247 log_error(LOG_LEVEL_FATAL,
1248 "can't load actions file '%s': invalid line (%lu): %s",
1249 csp->config->actions_file[fileid], linenum, buf);
1250 return 1; /* never get here */
1253 /* Trim leading and trailing whitespace. */
1261 log_error(LOG_LEVEL_FATAL,
1262 "can't load actions file '%s': invalid line (%lu): {{ }}",
1263 csp->config->actions_file[fileid], linenum);
1264 return 1; /* never get here */
1268 * An actionsfile can optionally contain the following blocks.
1269 * They *MUST* be in this order, to simplify processing:
1275 * ...free text, format TBD, but no line may start with a '{'...
1280 * The actual actions must be *after* these special blocks.
1281 * None of these special blocks may be repeated.
1284 if (0 == strcmpic(start, "settings"))
1286 /* it's a {{settings}} block */
1287 if (mode >= MODE_SETTINGS)
1289 /* {{settings}} must be first thing in file and must only
1293 log_error(LOG_LEVEL_FATAL,
1294 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1295 csp->config->actions_file[fileid], linenum);
1297 mode = MODE_SETTINGS;
1299 else if (0 == strcmpic(start, "description"))
1301 /* it's a {{description}} block */
1302 if (mode >= MODE_DESCRIPTION)
1304 /* {{description}} is a singleton and only {{settings}} may proceed it
1307 log_error(LOG_LEVEL_FATAL,
1308 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1309 csp->config->actions_file[fileid], linenum);
1311 mode = MODE_DESCRIPTION;
1313 else if (0 == strcmpic(start, "alias"))
1315 /* it's an {{alias}} block */
1316 if (mode >= MODE_ALIAS)
1318 /* {{alias}} must be first thing in file, possibly after
1319 * {{settings}} and {{description}}
1321 * {{alias}} must only appear once.
1323 * Note that these are new restrictions introduced in
1324 * v2.9.10 in order to make actionsfile editing simpler.
1325 * (Otherwise, reordering actionsfile entries without
1326 * completely rewriting the file becomes non-trivial)
1329 log_error(LOG_LEVEL_FATAL,
1330 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1331 csp->config->actions_file[fileid], linenum);
1337 /* invalid {{something}} block */
1339 log_error(LOG_LEVEL_FATAL,
1340 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1341 csp->config->actions_file[fileid], linenum, start);
1342 return 1; /* never get here */
1347 /* It's an actions block */
1353 mode = MODE_ACTIONS;
1355 /* free old action */
1358 if (!cur_action_used)
1360 free_action_spec(cur_action);
1364 cur_action_used = 0;
1365 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1366 if (cur_action == NULL)
1369 log_error(LOG_LEVEL_FATAL,
1370 "can't load actions file '%s': out of memory",
1371 csp->config->actions_file[fileid]);
1372 return 1; /* never get here */
1374 init_action(cur_action);
1377 * Copy the buffer before messing with it as we may need the
1378 * unmodified version in for the fatal error messages. Given
1379 * that this is not a common event, we could instead simply
1380 * read the line again.
1382 * buf + 1 to skip the leading '{'
1384 actions_buf = end = strdup_or_die(buf + 1);
1386 /* check we have a trailing } and then trim it */
1387 if (strlen(actions_buf))
1389 end += strlen(actions_buf) - 1;
1396 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1397 "Missing trailing '}' in action section starting at line (%lu): %s",
1398 csp->config->actions_file[fileid], linenum, buf);
1399 return 1; /* never get here */
1403 /* trim any whitespace immediately inside {} */
1406 if (get_actions(actions_buf, alias_list, cur_action))
1411 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1412 "can't completely parse the action section starting at line (%lu): %s",
1413 csp->config->actions_file[fileid], linenum, buf);
1414 return 1; /* never get here */
1417 if (action_spec_is_valid(csp, cur_action))
1419 log_error(LOG_LEVEL_ERROR, "Invalid action section in file '%s', "
1420 "starting at line %lu: %s",
1421 csp->config->actions_file[fileid], linenum, buf);
1427 else if (mode == MODE_SETTINGS)
1430 * Part of the {{settings}} block.
1431 * For now only serves to check if the file's minimum Privoxy
1432 * version requirement is met, but we may want to read & check
1433 * permissions when we go multi-user.
1435 if (!strncmp(buf, "for-privoxy-version=", 20))
1437 char *version_string, *fields[3];
1440 version_string = strdup_or_die(buf + 20);
1442 num_fields = ssplit(version_string, ".", fields, SZ(fields));
1444 if (num_fields < 1 || atoi(fields[0]) == 0)
1446 log_error(LOG_LEVEL_ERROR,
1447 "While loading actions file '%s': invalid line (%lu): %s",
1448 csp->config->actions_file[fileid], linenum, buf);
1450 else if ( (atoi(fields[0]) > VERSION_MAJOR)
1451 || ((num_fields > 1) && (atoi(fields[1]) > VERSION_MINOR))
1452 || ((num_fields > 2) && (atoi(fields[2]) > VERSION_POINT)))
1455 log_error(LOG_LEVEL_FATAL,
1456 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1457 csp->config->actions_file[fileid], linenum, buf);
1458 return 1; /* never get here */
1460 free(version_string);
1463 else if (mode == MODE_DESCRIPTION)
1466 * Part of the {{description}} block.
1470 else if (mode == MODE_ALIAS)
1475 char actions_buf[BUFFER_SIZE];
1476 struct action_alias * new_alias;
1478 char * start = strchr(buf, '=');
1481 if ((start == NULL) || (start == buf))
1483 log_error(LOG_LEVEL_FATAL,
1484 "can't load actions file '%s': invalid alias line (%lu): %s",
1485 csp->config->actions_file[fileid], linenum, buf);
1486 return 1; /* never get here */
1489 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1492 log_error(LOG_LEVEL_FATAL,
1493 "can't load actions file '%s': out of memory!",
1494 csp->config->actions_file[fileid]);
1495 return 1; /* never get here */
1498 /* Eat any the whitespace before the '=' */
1500 while ((*end == ' ') || (*end == '\t'))
1503 * we already know we must have at least 1 non-ws char
1504 * at start of buf - no need to check
1510 /* Eat any the whitespace after the '=' */
1512 while ((*start == ' ') || (*start == '\t'))
1518 log_error(LOG_LEVEL_FATAL,
1519 "can't load actions file '%s': invalid alias line (%lu): %s",
1520 csp->config->actions_file[fileid], linenum, buf);
1521 return 1; /* never get here */
1524 new_alias->name = strdup_or_die(buf);
1526 strlcpy(actions_buf, start, sizeof(actions_buf));
1528 if (get_actions(actions_buf, alias_list, new_alias->action))
1532 log_error(LOG_LEVEL_FATAL,
1533 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1534 csp->config->actions_file[fileid], linenum, buf, start);
1535 return 1; /* never get here */
1539 new_alias->next = alias_list;
1540 alias_list = new_alias;
1542 else if (mode == MODE_ACTIONS)
1544 /* it's an URL pattern */
1546 /* allocate a new node */
1547 if ((perm = zalloc(sizeof(*perm))) == NULL)
1550 log_error(LOG_LEVEL_FATAL,
1551 "can't load actions file '%s': out of memory!",
1552 csp->config->actions_file[fileid]);
1553 return 1; /* never get here */
1556 perm->action = cur_action;
1557 cur_action_used = 1;
1559 /* Save the URL pattern */
1560 if (create_pattern_spec(perm->url, buf))
1563 log_error(LOG_LEVEL_FATAL,
1564 "can't load actions file '%s': line %lu: cannot create URL or TAG pattern from: %s",
1565 csp->config->actions_file[fileid], linenum, buf);
1566 return 1; /* never get here */
1569 /* add it to the list */
1570 last_perm->next = perm;
1573 else if (mode == MODE_START_OF_FILE)
1575 /* oops - please have a {} line as 1st line in file. */
1577 log_error(LOG_LEVEL_FATAL,
1578 "can't load actions file '%s': line %lu should begin with a '{': %s",
1579 csp->config->actions_file[fileid], linenum, buf);
1580 return 1; /* never get here */
1584 /* How did we get here? This is impossible! */
1586 log_error(LOG_LEVEL_FATAL,
1587 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1588 csp->config->actions_file[fileid], mode);
1589 return 1; /* never get here */
1596 if (!cur_action_used)
1598 free_action_spec(cur_action);
1600 free_alias_list(alias_list);
1602 /* the old one is now obsolete */
1603 if (current_actions_file[fileid])
1605 current_actions_file[fileid]->unloader = unload_actions_file;
1608 fs->next = files->next;
1610 current_actions_file[fileid] = fs;
1612 csp->actions_list[fileid] = fs;
1619 /*********************************************************************
1621 * Function : actions_to_text
1623 * Description : Converts a actionsfile entry from the internal
1624 * structure into a text line. The output is split
1625 * into one line for each action with line continuation.
1628 * 1 : action = The action to format.
1630 * Returns : A string. Caller must free it.
1631 * NULL on out-of-memory error.
1633 *********************************************************************/
1634 char * actions_to_text(const struct action_spec *action)
1636 unsigned long mask = action->mask;
1637 unsigned long add = action->add;
1638 char *result = strdup_or_die("");
1639 struct list_entry * lst;
1641 /* sanity - prevents "-feature +feature" */
1645 #define DEFINE_ACTION_BOOL(__name, __bit) \
1646 if (!(mask & __bit)) \
1648 string_append(&result, " -" __name " \\\n"); \
1650 else if (add & __bit) \
1652 string_append(&result, " +" __name " \\\n"); \
1655 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1656 if (!(mask & __bit)) \
1658 string_append(&result, " -" __name " \\\n"); \
1660 else if (add & __bit) \
1662 string_append(&result, " +" __name "{"); \
1663 string_append(&result, action->string[__index]); \
1664 string_append(&result, "} \\\n"); \
1667 #define DEFINE_ACTION_MULTI(__name, __index) \
1668 if (action->multi_remove_all[__index]) \
1670 string_append(&result, " -" __name " \\\n"); \
1674 lst = action->multi_remove[__index]->first; \
1677 string_append(&result, " -" __name "{"); \
1678 string_append(&result, lst->str); \
1679 string_append(&result, "} \\\n"); \
1683 lst = action->multi_add[__index]->first; \
1686 string_append(&result, " +" __name "{"); \
1687 string_append(&result, lst->str); \
1688 string_append(&result, "} \\\n"); \
1692 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1694 #include "actionlist.h"
1696 #undef DEFINE_ACTION_MULTI
1697 #undef DEFINE_ACTION_STRING
1698 #undef DEFINE_ACTION_BOOL
1699 #undef DEFINE_ACTION_ALIAS
1705 /*********************************************************************
1707 * Function : actions_to_html
1709 * Description : Converts a actionsfile entry from numeric form
1710 * ("mask" and "add") to a <br>-separated HTML string
1711 * in which each action is linked to its chapter in
1715 * 1 : csp = Client state (for config)
1716 * 2 : action = Action spec to be converted
1718 * Returns : A string. Caller must free it.
1719 * NULL on out-of-memory error.
1721 *********************************************************************/
1722 char * actions_to_html(const struct client_state *csp,
1723 const struct action_spec *action)
1725 unsigned long mask = action->mask;
1726 unsigned long add = action->add;
1727 char *result = strdup_or_die("");
1728 struct list_entry * lst;
1730 /* sanity - prevents "-feature +feature" */
1734 #define DEFINE_ACTION_BOOL(__name, __bit) \
1735 if (!(mask & __bit)) \
1737 string_append(&result, "\n<br>-"); \
1738 string_join(&result, add_help_link(__name, csp->config)); \
1740 else if (add & __bit) \
1742 string_append(&result, "\n<br>+"); \
1743 string_join(&result, add_help_link(__name, csp->config)); \
1746 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1747 if (!(mask & __bit)) \
1749 string_append(&result, "\n<br>-"); \
1750 string_join(&result, add_help_link(__name, csp->config)); \
1752 else if (add & __bit) \
1754 string_append(&result, "\n<br>+"); \
1755 string_join(&result, add_help_link(__name, csp->config)); \
1756 string_append(&result, "{"); \
1757 string_join(&result, html_encode(action->string[__index])); \
1758 string_append(&result, "}"); \
1761 #define DEFINE_ACTION_MULTI(__name, __index) \
1762 if (action->multi_remove_all[__index]) \
1764 string_append(&result, "\n<br>-"); \
1765 string_join(&result, add_help_link(__name, csp->config)); \
1769 lst = action->multi_remove[__index]->first; \
1772 string_append(&result, "\n<br>-"); \
1773 string_join(&result, add_help_link(__name, csp->config)); \
1774 string_append(&result, "{"); \
1775 string_join(&result, html_encode(lst->str)); \
1776 string_append(&result, "}"); \
1780 lst = action->multi_add[__index]->first; \
1783 string_append(&result, "\n<br>+"); \
1784 string_join(&result, add_help_link(__name, csp->config)); \
1785 string_append(&result, "{"); \
1786 string_join(&result, html_encode(lst->str)); \
1787 string_append(&result, "}"); \
1791 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1793 #include "actionlist.h"
1795 #undef DEFINE_ACTION_MULTI
1796 #undef DEFINE_ACTION_STRING
1797 #undef DEFINE_ACTION_BOOL
1798 #undef DEFINE_ACTION_ALIAS
1800 /* trim leading <br> */
1801 if (result && *result)
1804 result = strdup(result + 5);
1812 /*********************************************************************
1814 * Function : current_actions_to_html
1816 * Description : Converts a curren action spec to a <br> separated HTML
1817 * text in which each action is linked to its chapter in
1821 * 1 : csp = Client state (for config)
1822 * 2 : action = Current action spec to be converted
1824 * Returns : A string. Caller must free it.
1825 * NULL on out-of-memory error.
1827 *********************************************************************/
1828 char *current_action_to_html(const struct client_state *csp,
1829 const struct current_action_spec *action)
1831 unsigned long flags = action->flags;
1832 struct list_entry * lst;
1833 char *result = strdup_or_die("");
1834 char *active = strdup_or_die("");
1835 char *inactive = strdup_or_die("");
1837 #define DEFINE_ACTION_BOOL(__name, __bit) \
1838 if (flags & __bit) \
1840 string_append(&active, "\n<br>+"); \
1841 string_join(&active, add_help_link(__name, csp->config)); \
1845 string_append(&inactive, "\n<br>-"); \
1846 string_join(&inactive, add_help_link(__name, csp->config)); \
1849 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1850 if (flags & __bit) \
1852 string_append(&active, "\n<br>+"); \
1853 string_join(&active, add_help_link(__name, csp->config)); \
1854 string_append(&active, "{"); \
1855 string_join(&active, html_encode(action->string[__index])); \
1856 string_append(&active, "}"); \
1860 string_append(&inactive, "\n<br>-"); \
1861 string_join(&inactive, add_help_link(__name, csp->config)); \
1864 #define DEFINE_ACTION_MULTI(__name, __index) \
1865 lst = action->multi[__index]->first; \
1868 string_append(&inactive, "\n<br>-"); \
1869 string_join(&inactive, add_help_link(__name, csp->config)); \
1875 string_append(&active, "\n<br>+"); \
1876 string_join(&active, add_help_link(__name, csp->config)); \
1877 string_append(&active, "{"); \
1878 string_join(&active, html_encode(lst->str)); \
1879 string_append(&active, "}"); \
1884 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1886 #include "actionlist.h"
1888 #undef DEFINE_ACTION_MULTI
1889 #undef DEFINE_ACTION_STRING
1890 #undef DEFINE_ACTION_BOOL
1891 #undef DEFINE_ACTION_ALIAS
1895 string_append(&result, active);
1898 string_append(&result, "\n<br>");
1899 if (inactive != NULL)
1901 string_append(&result, inactive);
1908 /*********************************************************************
1910 * Function : action_to_line_of_text
1912 * Description : Converts a action spec to a single text line
1913 * listing the enabled actions.
1916 * 1 : action = Current action spec to be converted
1918 * Returns : A string. Caller must free it.
1919 * Out-of-memory errors are fatal.
1921 *********************************************************************/
1922 char *actions_to_line_of_text(const struct current_action_spec *action)
1925 struct list_entry *lst;
1927 const unsigned long flags = action->flags;
1929 active = strdup_or_die("");
1931 #define DEFINE_ACTION_BOOL(__name, __bit) \
1932 if (flags & __bit) \
1934 snprintf(buffer, sizeof(buffer), "+%s ", __name); \
1935 string_append(&active, buffer); \
1938 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1939 if (flags & __bit) \
1941 snprintf(buffer, sizeof(buffer), "+%s{%s} ", \
1942 __name, action->string[__index]); \
1943 string_append(&active, buffer); \
1946 #define DEFINE_ACTION_MULTI(__name, __index) \
1947 lst = action->multi[__index]->first; \
1948 while (lst != NULL) \
1950 snprintf(buffer, sizeof(buffer), "+%s{%s} ", \
1951 __name, lst->str); \
1952 string_append(&active, buffer); \
1956 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1958 #include "actionlist.h"
1960 #undef DEFINE_ACTION_MULTI
1961 #undef DEFINE_ACTION_STRING
1962 #undef DEFINE_ACTION_BOOL
1963 #undef DEFINE_ACTION_ALIAS
1967 log_error(LOG_LEVEL_FATAL, "Out of memory in action_to_line_of_text()");