1 const char actions_rcs[] = "$Id: actions.c,v 1.84 2012/07/27 17:39:57 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.
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 str = strchr(str, '}');
415 /*********************************************************************
417 * Function : action_used_to_be_valid
419 * Description : Checks if unrecognized actions were valid in earlier
423 * 1 : action = The string containing the action to check.
425 * Returns : True if yes, otherwise false.
427 *********************************************************************/
428 static int action_used_to_be_valid(const char *action)
430 static const char * const formerly_valid_actions[] = {
433 "send-vanilla-wafer",
435 "treat-forbidden-connects-like-blocks",
441 for (i = 0; i < SZ(formerly_valid_actions); i++)
443 if (0 == strcmpic(action, formerly_valid_actions[i]))
452 /*********************************************************************
454 * Function : get_actions
456 * Description : Parses a list of actions.
459 * 1 : line = The string containing the actions.
460 * Will be written to by this function.
461 * 2 : alias_list = Custom alias list, or NULL for none.
462 * 3 : cur_action = Where to store the action. Caller
465 * Returns : JB_ERR_OK => Ok
466 * JB_ERR_PARSE => Parse error (line was trashed anyway)
467 * nonzero => Out of memory (line was trashed anyway)
469 *********************************************************************/
470 jb_err get_actions(char *line,
471 struct action_alias * alias_list,
472 struct action_spec *cur_action)
475 init_action(cur_action);
476 cur_action->mask = ACTION_MASK_ALL;
480 char * option = NULL;
483 err = get_action_token(&line, &option, &value);
491 /* handle option in 'option' */
493 /* Check for standard action name */
494 const struct action_name * action = action_names;
496 while ((action->name != NULL) && (0 != strcmpic(action->name, option)))
500 if (action->name != NULL)
503 cur_action->mask &= action->mask;
504 cur_action->add &= action->mask;
505 cur_action->add |= action->add;
507 switch (action->value_type)
510 /* ignore any option. */
514 /* add single string. */
516 if ((value == NULL) || (*value == '\0'))
518 if (0 == strcmpic(action->name, "+block"))
521 * XXX: Temporary backwards compatibility hack.
522 * XXX: should include line number.
524 value = "No reason specified.";
525 log_error(LOG_LEVEL_ERROR,
526 "block action without reason found. This may "
527 "become a fatal error in future versions.");
534 /* FIXME: should validate option string here */
535 freez (cur_action->string[action->index]);
536 cur_action->string[action->index] = strdup(value);
537 if (NULL == cur_action->string[action->index])
539 return JB_ERR_MEMORY;
545 /* remove single string. */
547 freez (cur_action->string[action->index]);
552 /* append multi string. */
554 struct list * remove_p = cur_action->multi_remove[action->index];
555 struct list * add_p = cur_action->multi_add[action->index];
557 if ((value == NULL) || (*value == '\0'))
562 list_remove_item(remove_p, value);
563 err = enlist_unique(add_p, value, 0);
572 /* remove multi string. */
574 struct list * remove_p = cur_action->multi_remove[action->index];
575 struct list * add_p = cur_action->multi_add[action->index];
577 if ((value == NULL) || (*value == '\0')
578 || ((*value == '*') && (value[1] == '\0')))
581 * no option, or option == "*".
585 list_remove_all(remove_p);
586 list_remove_all(add_p);
587 cur_action->multi_remove_all[action->index] = 1;
591 /* Valid option - remove only 1 option */
593 if (!cur_action->multi_remove_all[action->index])
595 /* there isn't a catch-all in the remove list already */
596 err = enlist_unique(remove_p, value, 0);
602 list_remove_item(add_p, value);
607 /* Shouldn't get here unless there's memory corruption. */
614 /* try user aliases. */
615 const struct action_alias * alias = alias_list;
617 while ((alias != NULL) && (0 != strcmpic(alias->name, option)))
624 merge_actions(cur_action, alias->action);
626 else if (((size_t)2 < strlen(option)) && action_used_to_be_valid(option+1))
628 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
629 "in this Privoxy release. Ignored.", option+1);
631 else if (((size_t)2 < strlen(option)) && 0 == strcmpic(option+1, "hide-forwarded-for-headers"))
633 log_error(LOG_LEVEL_FATAL, "The action 'hide-forwarded-for-headers' "
634 "is no longer valid in this Privoxy release. "
635 "Use 'change-x-forwarded-for' instead.");
639 /* Bad action name */
641 * XXX: This is a fatal error and Privoxy will later on exit
642 * in load_one_actions_file() because of an "invalid line".
644 * It would be preferable to name the offending option in that
645 * error message, but currently there is no way to do that and
646 * we have to live with two error messages for basically the
649 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
660 /*********************************************************************
662 * Function : init_current_action
664 * Description : Zero out an action.
667 * 1 : dest = An uninitialized current_action_spec.
671 *********************************************************************/
672 void init_current_action (struct current_action_spec *dest)
674 memset(dest, '\0', sizeof(*dest));
676 dest->flags = ACTION_MOST_COMPATIBLE;
680 /*********************************************************************
682 * Function : init_action
684 * Description : Zero out an action.
687 * 1 : dest = An uninitialized action_spec.
691 *********************************************************************/
692 void init_action (struct action_spec *dest)
694 memset(dest, '\0', sizeof(*dest));
698 /*********************************************************************
700 * Function : merge_current_action
702 * Description : Merge two actions together.
703 * Similar to "dest += src".
704 * Differences between this and merge_actions()
705 * is that this one doesn't allocate memory for
706 * strings (so "src" better be in memory for at least
707 * as long as "dest" is, and you'd better free
708 * "dest" using "free_current_action").
709 * Also, there is no mask or remove lists in dest.
710 * (If we're applying it to a URL, we don't need them)
713 * 1 : dest = Current actions, to modify.
714 * 2 : src = Action to add.
716 * Returns 0 : no error
717 * !=0 : error, probably JB_ERR_MEMORY.
719 *********************************************************************/
720 jb_err merge_current_action (struct current_action_spec *dest,
721 const struct action_spec *src)
724 jb_err err = JB_ERR_OK;
726 dest->flags &= src->mask;
727 dest->flags |= src->add;
729 for (i = 0; i < ACTION_STRING_COUNT; i++)
731 char * str = src->string[i];
734 str = strdup_or_die(str);
735 freez(dest->string[i]);
736 dest->string[i] = str;
740 for (i = 0; i < ACTION_MULTI_COUNT; i++)
742 if (src->multi_remove_all[i])
744 /* Remove everything from dest, then add src->multi_add */
745 err = list_duplicate(dest->multi[i], src->multi_add[i]);
753 list_remove_list(dest->multi[i], src->multi_remove[i]);
754 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
765 /*********************************************************************
767 * Function : update_action_bits_for_tag
769 * Description : Updates the action bits based on the action sections
770 * whose tag patterns match a provided tag.
773 * 1 : csp = Current client state (buffers, headers, etc...)
774 * 2 : tag = The tag on which the update should be based on
776 * Returns : 0 if no tag matched, or
779 *********************************************************************/
780 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
782 struct file_list *fl;
783 struct url_actions *b;
789 assert(list_contains_item(csp->tags, tag));
791 /* Run through all action files, */
792 for (i = 0; i < MAX_AF_FILES; i++)
794 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
796 /* Skip empty files */
800 /* and through all the action patterns, */
801 for (b = b->next; NULL != b; b = b->next)
803 /* skip the URL patterns, */
804 if (NULL == b->url->tag_regex)
809 /* and check if one of the tag patterns matches the tag, */
810 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
812 /* if it does, update the action bit map, */
813 if (merge_current_action(csp->action, b->action))
815 log_error(LOG_LEVEL_ERROR,
816 "Out of memory while changing action bits");
818 /* and signal the change. */
828 /*********************************************************************
830 * Function : free_current_action
832 * Description : Free memory used by a current_action_spec.
833 * Does not free the current_action_spec itself.
836 * 1 : src = Source to free.
840 *********************************************************************/
841 void free_current_action(struct current_action_spec *src)
845 for (i = 0; i < ACTION_STRING_COUNT; i++)
847 freez(src->string[i]);
850 for (i = 0; i < ACTION_MULTI_COUNT; i++)
852 destroy_list(src->multi[i]);
855 memset(src, '\0', sizeof(*src));
859 static struct file_list *current_actions_file[MAX_AF_FILES] = {
860 NULL, NULL, NULL, NULL, NULL,
861 NULL, NULL, NULL, NULL, NULL
865 #ifdef FEATURE_GRACEFUL_TERMINATION
866 /*********************************************************************
868 * Function : unload_current_actions_file
870 * Description : Unloads current actions file - reset to state at
871 * beginning of program.
877 *********************************************************************/
878 void unload_current_actions_file(void)
882 for (i = 0; i < MAX_AF_FILES; i++)
884 if (current_actions_file[i])
886 current_actions_file[i]->unloader = unload_actions_file;
887 current_actions_file[i] = NULL;
891 #endif /* FEATURE_GRACEFUL_TERMINATION */
894 /*********************************************************************
896 * Function : unload_actions_file
898 * Description : Unloads an actions module.
901 * 1 : file_data = the data structure associated with the
906 *********************************************************************/
907 void unload_actions_file(void *file_data)
909 struct url_actions * next;
910 struct url_actions * cur = (struct url_actions *)file_data;
914 free_url_spec(cur->url);
915 if ((next == NULL) || (next->action != cur->action))
918 * As the action settings might be shared,
919 * we can only free them if the current
920 * url pattern is the last one, or if the
921 * next one is using different settings.
923 free_action_spec(cur->action);
931 /*********************************************************************
933 * Function : free_alias_list
935 * Description : Free memory used by a list of aliases.
938 * 1 : alias_list = Linked list to free.
942 *********************************************************************/
943 void free_alias_list(struct action_alias *alias_list)
945 while (alias_list != NULL)
947 struct action_alias * next = alias_list->next;
948 alias_list->next = NULL;
949 freez(alias_list->name);
950 free_action(alias_list->action);
957 /*********************************************************************
959 * Function : load_action_files
961 * Description : Read and parse all the action files and add to files
965 * 1 : csp = Current client state (buffers, headers, etc...)
967 * Returns : 0 => Ok, everything else is an error.
969 *********************************************************************/
970 int load_action_files(struct client_state *csp)
975 for (i = 0; i < MAX_AF_FILES; i++)
977 if (csp->config->actions_file[i])
979 result = load_one_actions_file(csp, i);
985 else if (current_actions_file[i])
987 current_actions_file[i]->unloader = unload_actions_file;
988 current_actions_file[i] = NULL;
996 /*********************************************************************
998 * Function : referenced_filters_are_missing
1000 * Description : Checks if any filters of a certain type referenced
1001 * in an action spec are missing.
1004 * 1 : csp = Current client state (buffers, headers, etc...)
1005 * 2 : cur_action = The action spec to check.
1006 * 3 : multi_index = The index where to look for the filter.
1007 * 4 : filter_type = The filter type the caller is interested in.
1009 * Returns : 0 => All referenced filters exists, everything else is an error.
1011 *********************************************************************/
1012 static int referenced_filters_are_missing(const struct client_state *csp,
1013 const struct action_spec *cur_action, int multi_index, enum filter_type filter_type)
1016 struct file_list *fl;
1017 struct re_filterfile_spec *b;
1018 struct list_entry *filtername;
1020 for (filtername = cur_action->multi_add[multi_index]->first;
1021 filtername; filtername = filtername->next)
1023 int filter_found = 0;
1024 for (i = 0; i < MAX_AF_FILES; i++)
1027 if ((NULL == fl) || (NULL == fl->f))
1032 for (b = fl->f; b; b = b->next)
1034 if (b->type != filter_type)
1038 if (strcmp(b->name, filtername->str) == 0)
1046 log_error(LOG_LEVEL_ERROR, "Missing filter '%s'", filtername->str);
1056 /*********************************************************************
1058 * Function : action_spec_is_valid
1060 * Description : Should eventually figure out if an action spec
1061 * is valid, but currently only checks that the
1062 * referenced filters are accounted for.
1065 * 1 : csp = Current client state (buffers, headers, etc...)
1066 * 2 : cur_action = The action spec to check.
1068 * Returns : 0 => No problems detected, everything else is an error.
1070 *********************************************************************/
1071 static int action_spec_is_valid(struct client_state *csp, const struct action_spec *cur_action)
1075 enum filter_type filter_type;
1077 {ACTION_MULTI_FILTER, FT_CONTENT_FILTER},
1078 {ACTION_MULTI_CLIENT_HEADER_FILTER, FT_CLIENT_HEADER_FILTER},
1079 {ACTION_MULTI_SERVER_HEADER_FILTER, FT_SERVER_HEADER_FILTER},
1080 {ACTION_MULTI_CLIENT_HEADER_TAGGER, FT_CLIENT_HEADER_TAGGER},
1081 {ACTION_MULTI_SERVER_HEADER_TAGGER, FT_SERVER_HEADER_TAGGER}
1086 for (i = 0; i < SZ(filter_map); i++)
1088 errors += referenced_filters_are_missing(csp, cur_action,
1089 filter_map[i].multi_index, filter_map[i].filter_type);
1097 /*********************************************************************
1099 * Function : load_one_actions_file
1101 * Description : Read and parse a action file and add to files
1105 * 1 : csp = Current client state (buffers, headers, etc...)
1106 * 2 : fileid = File index to load.
1108 * Returns : 0 => Ok, everything else is an error.
1110 *********************************************************************/
1111 static int load_one_actions_file(struct client_state *csp, int fileid)
1116 * Note: Keep these in the order they occur in the file, they are
1117 * sometimes tested with <=
1120 MODE_START_OF_FILE = 1,
1122 MODE_DESCRIPTION = 3,
1128 struct url_actions *last_perm;
1129 struct url_actions *perm;
1131 struct file_list *fs;
1132 struct action_spec * cur_action = NULL;
1133 int cur_action_used = 0;
1134 struct action_alias * alias_list = NULL;
1135 unsigned long linenum = 0;
1136 mode = MODE_START_OF_FILE;
1138 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1140 /* No need to load */
1141 csp->actions_list[fileid] = current_actions_file[fileid];
1146 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1147 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1148 "with their complete file names.", csp->config->actions_file[fileid]);
1149 return 1; /* never get here */
1152 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1153 if (last_perm == NULL)
1155 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1156 csp->config->actions_file[fileid]);
1157 return 1; /* never get here */
1160 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1162 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1163 csp->config->actions_file[fileid]);
1164 return 1; /* never get here */
1167 log_error(LOG_LEVEL_INFO, "Loading actions file: %s", csp->config->actions_file[fileid]);
1169 while (read_config_line(fp, &linenum, &buf) != NULL)
1173 /* It's a header block */
1176 /* It's {{settings}} or {{alias}} */
1177 size_t len = strlen(buf);
1178 char * start = buf + 2;
1179 char * end = buf + len - 1;
1180 if ((len < (size_t)5) || (*end-- != '}') || (*end-- != '}'))
1184 log_error(LOG_LEVEL_FATAL,
1185 "can't load actions file '%s': invalid line (%lu): %s",
1186 csp->config->actions_file[fileid], linenum, buf);
1187 return 1; /* never get here */
1190 /* Trim leading and trailing whitespace. */
1198 log_error(LOG_LEVEL_FATAL,
1199 "can't load actions file '%s': invalid line (%lu): {{ }}",
1200 csp->config->actions_file[fileid], linenum);
1201 return 1; /* never get here */
1205 * An actionsfile can optionally contain the following blocks.
1206 * They *MUST* be in this order, to simplify processing:
1212 * ...free text, format TBD, but no line may start with a '{'...
1217 * The actual actions must be *after* these special blocks.
1218 * None of these special blocks may be repeated.
1221 if (0 == strcmpic(start, "settings"))
1223 /* it's a {{settings}} block */
1224 if (mode >= MODE_SETTINGS)
1226 /* {{settings}} must be first thing in file and must only
1230 log_error(LOG_LEVEL_FATAL,
1231 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1232 csp->config->actions_file[fileid], linenum);
1234 mode = MODE_SETTINGS;
1236 else if (0 == strcmpic(start, "description"))
1238 /* it's a {{description}} block */
1239 if (mode >= MODE_DESCRIPTION)
1241 /* {{description}} is a singleton and only {{settings}} may proceed it
1244 log_error(LOG_LEVEL_FATAL,
1245 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1246 csp->config->actions_file[fileid], linenum);
1248 mode = MODE_DESCRIPTION;
1250 else if (0 == strcmpic(start, "alias"))
1252 /* it's an {{alias}} block */
1253 if (mode >= MODE_ALIAS)
1255 /* {{alias}} must be first thing in file, possibly after
1256 * {{settings}} and {{description}}
1258 * {{alias}} must only appear once.
1260 * Note that these are new restrictions introduced in
1261 * v2.9.10 in order to make actionsfile editing simpler.
1262 * (Otherwise, reordering actionsfile entries without
1263 * completely rewriting the file becomes non-trivial)
1266 log_error(LOG_LEVEL_FATAL,
1267 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1268 csp->config->actions_file[fileid], linenum);
1274 /* invalid {{something}} block */
1276 log_error(LOG_LEVEL_FATAL,
1277 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1278 csp->config->actions_file[fileid], linenum, start);
1279 return 1; /* never get here */
1284 /* It's an actions block */
1290 mode = MODE_ACTIONS;
1292 /* free old action */
1295 if (!cur_action_used)
1297 free_action_spec(cur_action);
1301 cur_action_used = 0;
1302 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1303 if (cur_action == NULL)
1306 log_error(LOG_LEVEL_FATAL,
1307 "can't load actions file '%s': out of memory",
1308 csp->config->actions_file[fileid]);
1309 return 1; /* never get here */
1311 init_action(cur_action);
1314 * Copy the buffer before messing with it as we may need the
1315 * unmodified version in for the fatal error messages. Given
1316 * that this is not a common event, we could instead simply
1317 * read the line again.
1319 * buf + 1 to skip the leading '{'
1321 actions_buf = strdup_or_die(buf + 1);
1323 /* check we have a trailing } and then trim it */
1324 end = actions_buf + strlen(actions_buf) - 1;
1330 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1331 "Missing trailing '}' in action section starting at line (%lu): %s",
1332 csp->config->actions_file[fileid], linenum, buf);
1333 return 1; /* never get here */
1337 /* trim any whitespace immediately inside {} */
1340 if (get_actions(actions_buf, alias_list, cur_action))
1345 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1346 "can't completely parse the action section starting at line (%lu): %s",
1347 csp->config->actions_file[fileid], linenum, buf);
1348 return 1; /* never get here */
1351 if (action_spec_is_valid(csp, cur_action))
1353 log_error(LOG_LEVEL_ERROR, "Invalid action section in file '%s', "
1354 "starting at line %lu: %s",
1355 csp->config->actions_file[fileid], linenum, buf);
1361 else if (mode == MODE_SETTINGS)
1364 * Part of the {{settings}} block.
1365 * For now only serves to check if the file's minimum Privoxy
1366 * version requirement is met, but we may want to read & check
1367 * permissions when we go multi-user.
1369 if (!strncmp(buf, "for-privoxy-version=", 20))
1371 char *version_string, *fields[3];
1374 version_string = strdup_or_die(buf + 20);
1376 num_fields = ssplit(version_string, ".", fields, SZ(fields));
1378 if (num_fields < 1 || atoi(fields[0]) == 0)
1380 log_error(LOG_LEVEL_ERROR,
1381 "While loading actions file '%s': invalid line (%lu): %s",
1382 csp->config->actions_file[fileid], linenum, buf);
1384 else if ( (atoi(fields[0]) > VERSION_MAJOR)
1385 || ((num_fields > 1) && (atoi(fields[1]) > VERSION_MINOR))
1386 || ((num_fields > 2) && (atoi(fields[2]) > VERSION_POINT)))
1389 log_error(LOG_LEVEL_FATAL,
1390 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1391 csp->config->actions_file[fileid], linenum, buf);
1392 return 1; /* never get here */
1394 free(version_string);
1397 else if (mode == MODE_DESCRIPTION)
1400 * Part of the {{description}} block.
1404 else if (mode == MODE_ALIAS)
1409 char actions_buf[BUFFER_SIZE];
1410 struct action_alias * new_alias;
1412 char * start = strchr(buf, '=');
1415 if ((start == NULL) || (start == buf))
1417 log_error(LOG_LEVEL_FATAL,
1418 "can't load actions file '%s': invalid alias line (%lu): %s",
1419 csp->config->actions_file[fileid], linenum, buf);
1420 return 1; /* never get here */
1423 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1426 log_error(LOG_LEVEL_FATAL,
1427 "can't load actions file '%s': out of memory!",
1428 csp->config->actions_file[fileid]);
1429 return 1; /* never get here */
1432 /* Eat any the whitespace before the '=' */
1434 while ((*end == ' ') || (*end == '\t'))
1437 * we already know we must have at least 1 non-ws char
1438 * at start of buf - no need to check
1444 /* Eat any the whitespace after the '=' */
1446 while ((*start == ' ') || (*start == '\t'))
1452 log_error(LOG_LEVEL_FATAL,
1453 "can't load actions file '%s': invalid alias line (%lu): %s",
1454 csp->config->actions_file[fileid], linenum, buf);
1455 return 1; /* never get here */
1458 new_alias->name = strdup_or_die(buf);
1460 strlcpy(actions_buf, start, sizeof(actions_buf));
1462 if (get_actions(actions_buf, alias_list, new_alias->action))
1466 log_error(LOG_LEVEL_FATAL,
1467 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1468 csp->config->actions_file[fileid], linenum, buf, start);
1469 return 1; /* never get here */
1473 new_alias->next = alias_list;
1474 alias_list = new_alias;
1476 else if (mode == MODE_ACTIONS)
1478 /* it's an URL pattern */
1480 /* allocate a new node */
1481 if ((perm = zalloc(sizeof(*perm))) == NULL)
1484 log_error(LOG_LEVEL_FATAL,
1485 "can't load actions file '%s': out of memory!",
1486 csp->config->actions_file[fileid]);
1487 return 1; /* never get here */
1490 perm->action = cur_action;
1491 cur_action_used = 1;
1493 /* Save the URL pattern */
1494 if (create_url_spec(perm->url, buf))
1497 log_error(LOG_LEVEL_FATAL,
1498 "can't load actions file '%s': line %lu: cannot create URL or TAG pattern from: %s",
1499 csp->config->actions_file[fileid], linenum, buf);
1500 return 1; /* never get here */
1503 /* add it to the list */
1504 last_perm->next = perm;
1507 else if (mode == MODE_START_OF_FILE)
1509 /* oops - please have a {} line as 1st line in file. */
1511 log_error(LOG_LEVEL_FATAL,
1512 "can't load actions file '%s': line %lu should begin with a '{': %s",
1513 csp->config->actions_file[fileid], linenum, buf);
1514 return 1; /* never get here */
1518 /* How did we get here? This is impossible! */
1520 log_error(LOG_LEVEL_FATAL,
1521 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1522 csp->config->actions_file[fileid], mode);
1523 return 1; /* never get here */
1530 if (!cur_action_used)
1532 free_action_spec(cur_action);
1534 free_alias_list(alias_list);
1536 /* the old one is now obsolete */
1537 if (current_actions_file[fileid])
1539 current_actions_file[fileid]->unloader = unload_actions_file;
1542 fs->next = files->next;
1544 current_actions_file[fileid] = fs;
1546 csp->actions_list[fileid] = fs;
1553 /*********************************************************************
1555 * Function : actions_to_text
1557 * Description : Converts a actionsfile entry from the internal
1558 * structure into a text line. The output is split
1559 * into one line for each action with line continuation.
1562 * 1 : action = The action to format.
1564 * Returns : A string. Caller must free it.
1565 * NULL on out-of-memory error.
1567 *********************************************************************/
1568 char * actions_to_text(const struct action_spec *action)
1570 unsigned long mask = action->mask;
1571 unsigned long add = action->add;
1572 char *result = strdup_or_die("");
1573 struct list_entry * lst;
1575 /* sanity - prevents "-feature +feature" */
1579 #define DEFINE_ACTION_BOOL(__name, __bit) \
1580 if (!(mask & __bit)) \
1582 string_append(&result, " -" __name " \\\n"); \
1584 else if (add & __bit) \
1586 string_append(&result, " +" __name " \\\n"); \
1589 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1590 if (!(mask & __bit)) \
1592 string_append(&result, " -" __name " \\\n"); \
1594 else if (add & __bit) \
1596 string_append(&result, " +" __name "{"); \
1597 string_append(&result, action->string[__index]); \
1598 string_append(&result, "} \\\n"); \
1601 #define DEFINE_ACTION_MULTI(__name, __index) \
1602 if (action->multi_remove_all[__index]) \
1604 string_append(&result, " -" __name " \\\n"); \
1608 lst = action->multi_remove[__index]->first; \
1611 string_append(&result, " -" __name "{"); \
1612 string_append(&result, lst->str); \
1613 string_append(&result, "} \\\n"); \
1617 lst = action->multi_add[__index]->first; \
1620 string_append(&result, " +" __name "{"); \
1621 string_append(&result, lst->str); \
1622 string_append(&result, "} \\\n"); \
1626 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1628 #include "actionlist.h"
1630 #undef DEFINE_ACTION_MULTI
1631 #undef DEFINE_ACTION_STRING
1632 #undef DEFINE_ACTION_BOOL
1633 #undef DEFINE_ACTION_ALIAS
1639 /*********************************************************************
1641 * Function : actions_to_html
1643 * Description : Converts a actionsfile entry from numeric form
1644 * ("mask" and "add") to a <br>-separated HTML string
1645 * in which each action is linked to its chapter in
1649 * 1 : csp = Client state (for config)
1650 * 2 : action = Action spec to be converted
1652 * Returns : A string. Caller must free it.
1653 * NULL on out-of-memory error.
1655 *********************************************************************/
1656 char * actions_to_html(const struct client_state *csp,
1657 const struct action_spec *action)
1659 unsigned long mask = action->mask;
1660 unsigned long add = action->add;
1661 char *result = strdup_or_die("");
1662 struct list_entry * lst;
1664 /* sanity - prevents "-feature +feature" */
1668 #define DEFINE_ACTION_BOOL(__name, __bit) \
1669 if (!(mask & __bit)) \
1671 string_append(&result, "\n<br>-"); \
1672 string_join(&result, add_help_link(__name, csp->config)); \
1674 else if (add & __bit) \
1676 string_append(&result, "\n<br>+"); \
1677 string_join(&result, add_help_link(__name, csp->config)); \
1680 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1681 if (!(mask & __bit)) \
1683 string_append(&result, "\n<br>-"); \
1684 string_join(&result, add_help_link(__name, csp->config)); \
1686 else if (add & __bit) \
1688 string_append(&result, "\n<br>+"); \
1689 string_join(&result, add_help_link(__name, csp->config)); \
1690 string_append(&result, "{"); \
1691 string_join(&result, html_encode(action->string[__index])); \
1692 string_append(&result, "}"); \
1695 #define DEFINE_ACTION_MULTI(__name, __index) \
1696 if (action->multi_remove_all[__index]) \
1698 string_append(&result, "\n<br>-"); \
1699 string_join(&result, add_help_link(__name, csp->config)); \
1703 lst = action->multi_remove[__index]->first; \
1706 string_append(&result, "\n<br>-"); \
1707 string_join(&result, add_help_link(__name, csp->config)); \
1708 string_append(&result, "{"); \
1709 string_join(&result, html_encode(lst->str)); \
1710 string_append(&result, "}"); \
1714 lst = action->multi_add[__index]->first; \
1717 string_append(&result, "\n<br>+"); \
1718 string_join(&result, add_help_link(__name, csp->config)); \
1719 string_append(&result, "{"); \
1720 string_join(&result, html_encode(lst->str)); \
1721 string_append(&result, "}"); \
1725 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1727 #include "actionlist.h"
1729 #undef DEFINE_ACTION_MULTI
1730 #undef DEFINE_ACTION_STRING
1731 #undef DEFINE_ACTION_BOOL
1732 #undef DEFINE_ACTION_ALIAS
1734 /* trim leading <br> */
1735 if (result && *result)
1738 result = strdup(result + 5);
1746 /*********************************************************************
1748 * Function : current_actions_to_html
1750 * Description : Converts a curren action spec to a <br> separated HTML
1751 * text in which each action is linked to its chapter in
1755 * 1 : csp = Client state (for config)
1756 * 2 : action = Current action spec to be converted
1758 * Returns : A string. Caller must free it.
1759 * NULL on out-of-memory error.
1761 *********************************************************************/
1762 char *current_action_to_html(const struct client_state *csp,
1763 const struct current_action_spec *action)
1765 unsigned long flags = action->flags;
1766 struct list_entry * lst;
1767 char *result = strdup_or_die("");
1768 char *active = strdup_or_die("");
1769 char *inactive = strdup_or_die("");
1771 #define DEFINE_ACTION_BOOL(__name, __bit) \
1772 if (flags & __bit) \
1774 string_append(&active, "\n<br>+"); \
1775 string_join(&active, add_help_link(__name, csp->config)); \
1779 string_append(&inactive, "\n<br>-"); \
1780 string_join(&inactive, add_help_link(__name, csp->config)); \
1783 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1784 if (flags & __bit) \
1786 string_append(&active, "\n<br>+"); \
1787 string_join(&active, add_help_link(__name, csp->config)); \
1788 string_append(&active, "{"); \
1789 string_join(&active, html_encode(action->string[__index])); \
1790 string_append(&active, "}"); \
1794 string_append(&inactive, "\n<br>-"); \
1795 string_join(&inactive, add_help_link(__name, csp->config)); \
1798 #define DEFINE_ACTION_MULTI(__name, __index) \
1799 lst = action->multi[__index]->first; \
1802 string_append(&inactive, "\n<br>-"); \
1803 string_join(&inactive, add_help_link(__name, csp->config)); \
1809 string_append(&active, "\n<br>+"); \
1810 string_join(&active, add_help_link(__name, csp->config)); \
1811 string_append(&active, "{"); \
1812 string_join(&active, html_encode(lst->str)); \
1813 string_append(&active, "}"); \
1818 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1820 #include "actionlist.h"
1822 #undef DEFINE_ACTION_MULTI
1823 #undef DEFINE_ACTION_STRING
1824 #undef DEFINE_ACTION_BOOL
1825 #undef DEFINE_ACTION_ALIAS
1829 string_append(&result, active);
1832 string_append(&result, "\n<br>");
1833 if (inactive != NULL)
1835 string_append(&result, inactive);
1842 /*********************************************************************
1844 * Function : action_to_line_of_text
1846 * Description : Converts a action spec to a single text line
1847 * listing the enabled actions.
1850 * 1 : action = Current action spec to be converted
1852 * Returns : A string. Caller must free it.
1853 * Out-of-memory errors are fatal.
1855 *********************************************************************/
1856 char *actions_to_line_of_text(const struct current_action_spec *action)
1859 struct list_entry *lst;
1861 const unsigned long flags = action->flags;
1863 active = strdup_or_die("");
1865 #define DEFINE_ACTION_BOOL(__name, __bit) \
1866 if (flags & __bit) \
1868 snprintf(buffer, sizeof(buffer), "+%s ", __name); \
1869 string_append(&active, buffer); \
1872 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1873 if (flags & __bit) \
1875 snprintf(buffer, sizeof(buffer), "+%s{%s} ", \
1876 __name, action->string[__index]); \
1877 string_append(&active, buffer); \
1880 #define DEFINE_ACTION_MULTI(__name, __index) \
1881 lst = action->multi[__index]->first; \
1882 while (lst != NULL) \
1884 snprintf(buffer, sizeof(buffer), "+%s{%s} ", \
1885 __name, lst->str); \
1886 string_append(&active, buffer); \
1890 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1892 #include "actionlist.h"
1894 #undef DEFINE_ACTION_MULTI
1895 #undef DEFINE_ACTION_STRING
1896 #undef DEFINE_ACTION_BOOL
1897 #undef DEFINE_ACTION_ALIAS
1901 log_error(LOG_LEVEL_FATAL, "Out of memory in action_to_line_of_text()");