1 const char actions_rcs[] = "$Id: actions.c,v 1.83 2012/06/08 15:15:11 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(str);
160 if (NULL == dest->string[i])
162 return JB_ERR_MEMORY;
167 for (i = 0; i < ACTION_MULTI_COUNT; i++)
169 if (src->multi_remove_all[i])
171 /* Remove everything from dest */
172 list_remove_all(dest->multi_remove[i]);
173 dest->multi_remove_all[i] = 1;
175 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
177 else if (dest->multi_remove_all[i])
180 * dest already removes everything, so we only need to worry
183 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
184 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
188 /* No "remove all"s to worry about. */
189 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
190 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
191 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
204 /*********************************************************************
206 * Function : copy_action
208 * Description : Copy an action_specs.
209 * Similar to "dest = src".
212 * 1 : dest = Destination of copy.
213 * 2 : src = Source for copy.
217 *********************************************************************/
218 jb_err copy_action (struct action_spec *dest,
219 const struct action_spec *src)
222 jb_err err = JB_ERR_OK;
225 memset(dest, '\0', sizeof(*dest));
227 dest->mask = src->mask;
228 dest->add = src->add;
230 for (i = 0; i < ACTION_STRING_COUNT; i++)
232 char * str = src->string[i];
238 return JB_ERR_MEMORY;
240 dest->string[i] = str;
244 for (i = 0; i < ACTION_MULTI_COUNT; i++)
246 dest->multi_remove_all[i] = src->multi_remove_all[i];
247 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
252 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
261 /*********************************************************************
263 * Function : free_action_spec
265 * Description : Frees an action_spec and the memory used by it.
268 * 1 : src = Source to free.
272 *********************************************************************/
273 void free_action_spec(struct action_spec *src)
280 /*********************************************************************
282 * Function : free_action
284 * Description : Destroy an action_spec. Frees memory used by it,
285 * except for the memory used by the struct action_spec
289 * 1 : src = Source to free.
293 *********************************************************************/
294 void free_action (struct action_spec *src)
303 for (i = 0; i < ACTION_STRING_COUNT; i++)
305 freez(src->string[i]);
308 for (i = 0; i < ACTION_MULTI_COUNT; i++)
310 destroy_list(src->multi_remove[i]);
311 destroy_list(src->multi_add[i]);
314 memset(src, '\0', sizeof(*src));
318 /*********************************************************************
320 * Function : get_action_token
322 * Description : Parses a line for the first action.
323 * Modifies its input array, doesn't allocate memory.
325 * *line=" +abc{def} -ghi "
332 * 1 : line = [in] The line containing the action.
333 * [out] Start of next action on line, or
334 * NULL if we reached the end of line before
335 * we found an action.
336 * 2 : name = [out] Start of action name, null
337 * terminated. NULL on EOL
338 * 3 : value = [out] Start of action value, null
339 * terminated. NULL if none or EOL.
341 * Returns : JB_ERR_OK => Ok
342 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
344 *********************************************************************/
345 jb_err get_action_token(char **line, char **name, char **value)
350 /* set default returns */
355 /* Eat any leading whitespace */
356 while ((*str == ' ') || (*str == '\t'))
368 /* null name, just value is prohibited */
375 while (((ch = *str) != '\0') &&
376 (ch != ' ') && (ch != '\t') && (ch != '{'))
380 /* error, '}' without '{' */
392 /* EOL - be careful not to run off buffer */
397 /* More to parse next time. */
406 str = strchr(str, '}');
423 /*********************************************************************
425 * Function : action_used_to_be_valid
427 * Description : Checks if unrecognized actions were valid in earlier
431 * 1 : action = The string containing the action to check.
433 * Returns : True if yes, otherwise false.
435 *********************************************************************/
436 static int action_used_to_be_valid(const char *action)
438 static const char * const formerly_valid_actions[] = {
441 "send-vanilla-wafer",
443 "treat-forbidden-connects-like-blocks",
449 for (i = 0; i < SZ(formerly_valid_actions); i++)
451 if (0 == strcmpic(action, formerly_valid_actions[i]))
460 /*********************************************************************
462 * Function : get_actions
464 * Description : Parses a list of actions.
467 * 1 : line = The string containing the actions.
468 * Will be written to by this function.
469 * 2 : alias_list = Custom alias list, or NULL for none.
470 * 3 : cur_action = Where to store the action. Caller
473 * Returns : JB_ERR_OK => Ok
474 * JB_ERR_PARSE => Parse error (line was trashed anyway)
475 * nonzero => Out of memory (line was trashed anyway)
477 *********************************************************************/
478 jb_err get_actions(char *line,
479 struct action_alias * alias_list,
480 struct action_spec *cur_action)
483 init_action(cur_action);
484 cur_action->mask = ACTION_MASK_ALL;
488 char * option = NULL;
491 err = get_action_token(&line, &option, &value);
499 /* handle option in 'option' */
501 /* Check for standard action name */
502 const struct action_name * action = action_names;
504 while ((action->name != NULL) && (0 != strcmpic(action->name, option)))
508 if (action->name != NULL)
511 cur_action->mask &= action->mask;
512 cur_action->add &= action->mask;
513 cur_action->add |= action->add;
515 switch (action->value_type)
518 /* ignore any option. */
522 /* add single string. */
524 if ((value == NULL) || (*value == '\0'))
526 if (0 == strcmpic(action->name, "+block"))
529 * XXX: Temporary backwards compatibility hack.
530 * XXX: should include line number.
532 value = "No reason specified.";
533 log_error(LOG_LEVEL_ERROR,
534 "block action without reason found. This may "
535 "become a fatal error in future versions.");
542 /* FIXME: should validate option string here */
543 freez (cur_action->string[action->index]);
544 cur_action->string[action->index] = strdup(value);
545 if (NULL == cur_action->string[action->index])
547 return JB_ERR_MEMORY;
553 /* remove single string. */
555 freez (cur_action->string[action->index]);
560 /* append multi string. */
562 struct list * remove_p = cur_action->multi_remove[action->index];
563 struct list * add_p = cur_action->multi_add[action->index];
565 if ((value == NULL) || (*value == '\0'))
570 list_remove_item(remove_p, value);
571 err = enlist_unique(add_p, value, 0);
580 /* remove multi string. */
582 struct list * remove_p = cur_action->multi_remove[action->index];
583 struct list * add_p = cur_action->multi_add[action->index];
585 if ((value == NULL) || (*value == '\0')
586 || ((*value == '*') && (value[1] == '\0')))
589 * no option, or option == "*".
593 list_remove_all(remove_p);
594 list_remove_all(add_p);
595 cur_action->multi_remove_all[action->index] = 1;
599 /* Valid option - remove only 1 option */
601 if (!cur_action->multi_remove_all[action->index])
603 /* there isn't a catch-all in the remove list already */
604 err = enlist_unique(remove_p, value, 0);
610 list_remove_item(add_p, value);
615 /* Shouldn't get here unless there's memory corruption. */
622 /* try user aliases. */
623 const struct action_alias * alias = alias_list;
625 while ((alias != NULL) && (0 != strcmpic(alias->name, option)))
632 merge_actions(cur_action, alias->action);
634 else if (((size_t)2 < strlen(option)) && action_used_to_be_valid(option+1))
636 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
637 "in this Privoxy release. Ignored.", option+1);
639 else if (((size_t)2 < strlen(option)) && 0 == strcmpic(option+1, "hide-forwarded-for-headers"))
641 log_error(LOG_LEVEL_FATAL, "The action 'hide-forwarded-for-headers' "
642 "is no longer valid in this Privoxy release. "
643 "Use 'change-x-forwarded-for' instead.");
647 /* Bad action name */
649 * XXX: This is a fatal error and Privoxy will later on exit
650 * in load_one_actions_file() because of an "invalid line".
652 * It would be preferable to name the offending option in that
653 * error message, but currently there is no way to do that and
654 * we have to live with two error messages for basically the
657 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
668 /*********************************************************************
670 * Function : init_current_action
672 * Description : Zero out an action.
675 * 1 : dest = An uninitialized current_action_spec.
679 *********************************************************************/
680 void init_current_action (struct current_action_spec *dest)
682 memset(dest, '\0', sizeof(*dest));
684 dest->flags = ACTION_MOST_COMPATIBLE;
688 /*********************************************************************
690 * Function : init_action
692 * Description : Zero out an action.
695 * 1 : dest = An uninitialized action_spec.
699 *********************************************************************/
700 void init_action (struct action_spec *dest)
702 memset(dest, '\0', sizeof(*dest));
706 /*********************************************************************
708 * Function : merge_current_action
710 * Description : Merge two actions together.
711 * Similar to "dest += src".
712 * Differences between this and merge_actions()
713 * is that this one doesn't allocate memory for
714 * strings (so "src" better be in memory for at least
715 * as long as "dest" is, and you'd better free
716 * "dest" using "free_current_action").
717 * Also, there is no mask or remove lists in dest.
718 * (If we're applying it to a URL, we don't need them)
721 * 1 : dest = Current actions, to modify.
722 * 2 : src = Action to add.
724 * Returns 0 : no error
725 * !=0 : error, probably JB_ERR_MEMORY.
727 *********************************************************************/
728 jb_err merge_current_action (struct current_action_spec *dest,
729 const struct action_spec *src)
732 jb_err err = JB_ERR_OK;
734 dest->flags &= src->mask;
735 dest->flags |= src->add;
737 for (i = 0; i < ACTION_STRING_COUNT; i++)
739 char * str = src->string[i];
745 return JB_ERR_MEMORY;
747 freez(dest->string[i]);
748 dest->string[i] = str;
752 for (i = 0; i < ACTION_MULTI_COUNT; i++)
754 if (src->multi_remove_all[i])
756 /* Remove everything from dest, then add src->multi_add */
757 err = list_duplicate(dest->multi[i], src->multi_add[i]);
765 list_remove_list(dest->multi[i], src->multi_remove[i]);
766 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
777 /*********************************************************************
779 * Function : update_action_bits_for_tag
781 * Description : Updates the action bits based on the action sections
782 * whose tag patterns match a provided tag.
785 * 1 : csp = Current client state (buffers, headers, etc...)
786 * 2 : tag = The tag on which the update should be based on
788 * Returns : 0 if no tag matched, or
791 *********************************************************************/
792 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
794 struct file_list *fl;
795 struct url_actions *b;
801 assert(list_contains_item(csp->tags, tag));
803 /* Run through all action files, */
804 for (i = 0; i < MAX_AF_FILES; i++)
806 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
808 /* Skip empty files */
812 /* and through all the action patterns, */
813 for (b = b->next; NULL != b; b = b->next)
815 /* skip the URL patterns, */
816 if (NULL == b->url->tag_regex)
821 /* and check if one of the tag patterns matches the tag, */
822 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
824 /* if it does, update the action bit map, */
825 if (merge_current_action(csp->action, b->action))
827 log_error(LOG_LEVEL_ERROR,
828 "Out of memory while changing action bits");
830 /* and signal the change. */
840 /*********************************************************************
842 * Function : free_current_action
844 * Description : Free memory used by a current_action_spec.
845 * Does not free the current_action_spec itself.
848 * 1 : src = Source to free.
852 *********************************************************************/
853 void free_current_action(struct current_action_spec *src)
857 for (i = 0; i < ACTION_STRING_COUNT; i++)
859 freez(src->string[i]);
862 for (i = 0; i < ACTION_MULTI_COUNT; i++)
864 destroy_list(src->multi[i]);
867 memset(src, '\0', sizeof(*src));
871 static struct file_list *current_actions_file[MAX_AF_FILES] = {
872 NULL, NULL, NULL, NULL, NULL,
873 NULL, NULL, NULL, NULL, NULL
877 #ifdef FEATURE_GRACEFUL_TERMINATION
878 /*********************************************************************
880 * Function : unload_current_actions_file
882 * Description : Unloads current actions file - reset to state at
883 * beginning of program.
889 *********************************************************************/
890 void unload_current_actions_file(void)
894 for (i = 0; i < MAX_AF_FILES; i++)
896 if (current_actions_file[i])
898 current_actions_file[i]->unloader = unload_actions_file;
899 current_actions_file[i] = NULL;
903 #endif /* FEATURE_GRACEFUL_TERMINATION */
906 /*********************************************************************
908 * Function : unload_actions_file
910 * Description : Unloads an actions module.
913 * 1 : file_data = the data structure associated with the
918 *********************************************************************/
919 void unload_actions_file(void *file_data)
921 struct url_actions * next;
922 struct url_actions * cur = (struct url_actions *)file_data;
926 free_url_spec(cur->url);
927 if ((next == NULL) || (next->action != cur->action))
930 * As the action settings might be shared,
931 * we can only free them if the current
932 * url pattern is the last one, or if the
933 * next one is using different settings.
935 free_action_spec(cur->action);
943 /*********************************************************************
945 * Function : free_alias_list
947 * Description : Free memory used by a list of aliases.
950 * 1 : alias_list = Linked list to free.
954 *********************************************************************/
955 void free_alias_list(struct action_alias *alias_list)
957 while (alias_list != NULL)
959 struct action_alias * next = alias_list->next;
960 alias_list->next = NULL;
961 freez(alias_list->name);
962 free_action(alias_list->action);
969 /*********************************************************************
971 * Function : load_action_files
973 * Description : Read and parse all the action files and add to files
977 * 1 : csp = Current client state (buffers, headers, etc...)
979 * Returns : 0 => Ok, everything else is an error.
981 *********************************************************************/
982 int load_action_files(struct client_state *csp)
987 for (i = 0; i < MAX_AF_FILES; i++)
989 if (csp->config->actions_file[i])
991 result = load_one_actions_file(csp, i);
997 else if (current_actions_file[i])
999 current_actions_file[i]->unloader = unload_actions_file;
1000 current_actions_file[i] = NULL;
1008 /*********************************************************************
1010 * Function : referenced_filters_are_missing
1012 * Description : Checks if any filters of a certain type referenced
1013 * in an action spec are missing.
1016 * 1 : csp = Current client state (buffers, headers, etc...)
1017 * 2 : cur_action = The action spec to check.
1018 * 3 : multi_index = The index where to look for the filter.
1019 * 4 : filter_type = The filter type the caller is interested in.
1021 * Returns : 0 => All referenced filters exists, everything else is an error.
1023 *********************************************************************/
1024 static int referenced_filters_are_missing(const struct client_state *csp,
1025 const struct action_spec *cur_action, int multi_index, enum filter_type filter_type)
1028 struct file_list *fl;
1029 struct re_filterfile_spec *b;
1030 struct list_entry *filtername;
1032 for (filtername = cur_action->multi_add[multi_index]->first;
1033 filtername; filtername = filtername->next)
1035 int filter_found = 0;
1036 for (i = 0; i < MAX_AF_FILES; i++)
1039 if ((NULL == fl) || (NULL == fl->f))
1044 for (b = fl->f; b; b = b->next)
1046 if (b->type != filter_type)
1050 if (strcmp(b->name, filtername->str) == 0)
1058 log_error(LOG_LEVEL_ERROR, "Missing filter '%s'", filtername->str);
1068 /*********************************************************************
1070 * Function : action_spec_is_valid
1072 * Description : Should eventually figure out if an action spec
1073 * is valid, but currently only checks that the
1074 * referenced filters are accounted for.
1077 * 1 : csp = Current client state (buffers, headers, etc...)
1078 * 2 : cur_action = The action spec to check.
1080 * Returns : 0 => No problems detected, everything else is an error.
1082 *********************************************************************/
1083 static int action_spec_is_valid(struct client_state *csp, const struct action_spec *cur_action)
1087 enum filter_type filter_type;
1089 {ACTION_MULTI_FILTER, FT_CONTENT_FILTER},
1090 {ACTION_MULTI_CLIENT_HEADER_FILTER, FT_CLIENT_HEADER_FILTER},
1091 {ACTION_MULTI_SERVER_HEADER_FILTER, FT_SERVER_HEADER_FILTER},
1092 {ACTION_MULTI_CLIENT_HEADER_TAGGER, FT_CLIENT_HEADER_TAGGER},
1093 {ACTION_MULTI_SERVER_HEADER_TAGGER, FT_SERVER_HEADER_TAGGER}
1098 for (i = 0; i < SZ(filter_map); i++)
1100 errors += referenced_filters_are_missing(csp, cur_action,
1101 filter_map[i].multi_index, filter_map[i].filter_type);
1109 /*********************************************************************
1111 * Function : load_one_actions_file
1113 * Description : Read and parse a action file and add to files
1117 * 1 : csp = Current client state (buffers, headers, etc...)
1118 * 2 : fileid = File index to load.
1120 * Returns : 0 => Ok, everything else is an error.
1122 *********************************************************************/
1123 static int load_one_actions_file(struct client_state *csp, int fileid)
1128 * Note: Keep these in the order they occur in the file, they are
1129 * sometimes tested with <=
1132 MODE_START_OF_FILE = 1,
1134 MODE_DESCRIPTION = 3,
1140 struct url_actions *last_perm;
1141 struct url_actions *perm;
1143 struct file_list *fs;
1144 struct action_spec * cur_action = NULL;
1145 int cur_action_used = 0;
1146 struct action_alias * alias_list = NULL;
1147 unsigned long linenum = 0;
1148 mode = MODE_START_OF_FILE;
1150 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1152 /* No need to load */
1153 csp->actions_list[fileid] = current_actions_file[fileid];
1158 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1159 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1160 "with their complete file names.", csp->config->actions_file[fileid]);
1161 return 1; /* never get here */
1164 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1165 if (last_perm == NULL)
1167 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1168 csp->config->actions_file[fileid]);
1169 return 1; /* never get here */
1172 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1174 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1175 csp->config->actions_file[fileid]);
1176 return 1; /* never get here */
1179 log_error(LOG_LEVEL_INFO, "Loading actions file: %s", csp->config->actions_file[fileid]);
1181 while (read_config_line(fp, &linenum, &buf) != NULL)
1185 /* It's a header block */
1188 /* It's {{settings}} or {{alias}} */
1189 size_t len = strlen(buf);
1190 char * start = buf + 2;
1191 char * end = buf + len - 1;
1192 if ((len < (size_t)5) || (*end-- != '}') || (*end-- != '}'))
1196 log_error(LOG_LEVEL_FATAL,
1197 "can't load actions file '%s': invalid line (%lu): %s",
1198 csp->config->actions_file[fileid], linenum, buf);
1199 return 1; /* never get here */
1202 /* Trim leading and trailing whitespace. */
1210 log_error(LOG_LEVEL_FATAL,
1211 "can't load actions file '%s': invalid line (%lu): {{ }}",
1212 csp->config->actions_file[fileid], linenum);
1213 return 1; /* never get here */
1217 * An actionsfile can optionally contain the following blocks.
1218 * They *MUST* be in this order, to simplify processing:
1224 * ...free text, format TBD, but no line may start with a '{'...
1229 * The actual actions must be *after* these special blocks.
1230 * None of these special blocks may be repeated.
1233 if (0 == strcmpic(start, "settings"))
1235 /* it's a {{settings}} block */
1236 if (mode >= MODE_SETTINGS)
1238 /* {{settings}} must be first thing in file and must only
1242 log_error(LOG_LEVEL_FATAL,
1243 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1244 csp->config->actions_file[fileid], linenum);
1246 mode = MODE_SETTINGS;
1248 else if (0 == strcmpic(start, "description"))
1250 /* it's a {{description}} block */
1251 if (mode >= MODE_DESCRIPTION)
1253 /* {{description}} is a singleton and only {{settings}} may proceed it
1256 log_error(LOG_LEVEL_FATAL,
1257 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1258 csp->config->actions_file[fileid], linenum);
1260 mode = MODE_DESCRIPTION;
1262 else if (0 == strcmpic(start, "alias"))
1264 /* it's an {{alias}} block */
1265 if (mode >= MODE_ALIAS)
1267 /* {{alias}} must be first thing in file, possibly after
1268 * {{settings}} and {{description}}
1270 * {{alias}} must only appear once.
1272 * Note that these are new restrictions introduced in
1273 * v2.9.10 in order to make actionsfile editing simpler.
1274 * (Otherwise, reordering actionsfile entries without
1275 * completely rewriting the file becomes non-trivial)
1278 log_error(LOG_LEVEL_FATAL,
1279 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1280 csp->config->actions_file[fileid], linenum);
1286 /* invalid {{something}} block */
1288 log_error(LOG_LEVEL_FATAL,
1289 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1290 csp->config->actions_file[fileid], linenum, start);
1291 return 1; /* never get here */
1296 /* It's an actions block */
1302 mode = MODE_ACTIONS;
1304 /* free old action */
1307 if (!cur_action_used)
1309 free_action_spec(cur_action);
1313 cur_action_used = 0;
1314 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1315 if (cur_action == NULL)
1318 log_error(LOG_LEVEL_FATAL,
1319 "can't load actions file '%s': out of memory",
1320 csp->config->actions_file[fileid]);
1321 return 1; /* never get here */
1323 init_action(cur_action);
1326 * Copy the buffer before messing with it as we may need the
1327 * unmodified version in for the fatal error messages. Given
1328 * that this is not a common event, we could instead simply
1329 * read the line again.
1331 * buf + 1 to skip the leading '{'
1333 actions_buf = strdup(buf + 1);
1334 if (actions_buf == NULL)
1337 log_error(LOG_LEVEL_FATAL,
1338 "can't load actions file '%s': out of memory",
1339 csp->config->actions_file[fileid]);
1340 return 1; /* never get here */
1343 /* check we have a trailing } and then trim it */
1344 end = actions_buf + strlen(actions_buf) - 1;
1350 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1351 "Missing trailing '}' in action section starting at line (%lu): %s",
1352 csp->config->actions_file[fileid], linenum, buf);
1353 return 1; /* never get here */
1357 /* trim any whitespace immediately inside {} */
1360 if (get_actions(actions_buf, alias_list, cur_action))
1365 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1366 "can't completely parse the action section starting at line (%lu): %s",
1367 csp->config->actions_file[fileid], linenum, buf);
1368 return 1; /* never get here */
1371 if (action_spec_is_valid(csp, cur_action))
1373 log_error(LOG_LEVEL_ERROR, "Invalid action section in file '%s', "
1374 "starting at line %lu: %s",
1375 csp->config->actions_file[fileid], linenum, buf);
1381 else if (mode == MODE_SETTINGS)
1384 * Part of the {{settings}} block.
1385 * For now only serves to check if the file's minimum Privoxy
1386 * version requirement is met, but we may want to read & check
1387 * permissions when we go multi-user.
1389 if (!strncmp(buf, "for-privoxy-version=", 20))
1391 char *version_string, *fields[3];
1394 if ((version_string = strdup(buf + 20)) == NULL)
1397 log_error(LOG_LEVEL_FATAL,
1398 "can't load actions file '%s': out of memory!",
1399 csp->config->actions_file[fileid]);
1400 return 1; /* never get here */
1403 num_fields = ssplit(version_string, ".", fields, SZ(fields));
1405 if (num_fields < 1 || atoi(fields[0]) == 0)
1407 log_error(LOG_LEVEL_ERROR,
1408 "While loading actions file '%s': invalid line (%lu): %s",
1409 csp->config->actions_file[fileid], linenum, buf);
1411 else if ( (atoi(fields[0]) > VERSION_MAJOR)
1412 || ((num_fields > 1) && (atoi(fields[1]) > VERSION_MINOR))
1413 || ((num_fields > 2) && (atoi(fields[2]) > VERSION_POINT)))
1416 log_error(LOG_LEVEL_FATAL,
1417 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1418 csp->config->actions_file[fileid], linenum, buf);
1419 return 1; /* never get here */
1421 free(version_string);
1424 else if (mode == MODE_DESCRIPTION)
1427 * Part of the {{description}} block.
1431 else if (mode == MODE_ALIAS)
1436 char actions_buf[BUFFER_SIZE];
1437 struct action_alias * new_alias;
1439 char * start = strchr(buf, '=');
1442 if ((start == NULL) || (start == buf))
1444 log_error(LOG_LEVEL_FATAL,
1445 "can't load actions file '%s': invalid alias line (%lu): %s",
1446 csp->config->actions_file[fileid], linenum, buf);
1447 return 1; /* never get here */
1450 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1453 log_error(LOG_LEVEL_FATAL,
1454 "can't load actions file '%s': out of memory!",
1455 csp->config->actions_file[fileid]);
1456 return 1; /* never get here */
1459 /* Eat any the whitespace before the '=' */
1461 while ((*end == ' ') || (*end == '\t'))
1464 * we already know we must have at least 1 non-ws char
1465 * at start of buf - no need to check
1471 /* Eat any the whitespace after the '=' */
1473 while ((*start == ' ') || (*start == '\t'))
1479 log_error(LOG_LEVEL_FATAL,
1480 "can't load actions file '%s': invalid alias line (%lu): %s",
1481 csp->config->actions_file[fileid], linenum, buf);
1482 return 1; /* never get here */
1485 if ((new_alias->name = strdup(buf)) == NULL)
1488 log_error(LOG_LEVEL_FATAL,
1489 "can't load actions file '%s': out of memory!",
1490 csp->config->actions_file[fileid]);
1491 return 1; /* never get here */
1494 strlcpy(actions_buf, start, sizeof(actions_buf));
1496 if (get_actions(actions_buf, alias_list, new_alias->action))
1500 log_error(LOG_LEVEL_FATAL,
1501 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1502 csp->config->actions_file[fileid], linenum, buf, start);
1503 return 1; /* never get here */
1507 new_alias->next = alias_list;
1508 alias_list = new_alias;
1510 else if (mode == MODE_ACTIONS)
1512 /* it's an URL pattern */
1514 /* allocate a new node */
1515 if ((perm = zalloc(sizeof(*perm))) == NULL)
1518 log_error(LOG_LEVEL_FATAL,
1519 "can't load actions file '%s': out of memory!",
1520 csp->config->actions_file[fileid]);
1521 return 1; /* never get here */
1524 perm->action = cur_action;
1525 cur_action_used = 1;
1527 /* Save the URL pattern */
1528 if (create_url_spec(perm->url, buf))
1531 log_error(LOG_LEVEL_FATAL,
1532 "can't load actions file '%s': line %lu: cannot create URL or TAG pattern from: %s",
1533 csp->config->actions_file[fileid], linenum, buf);
1534 return 1; /* never get here */
1537 /* add it to the list */
1538 last_perm->next = perm;
1541 else if (mode == MODE_START_OF_FILE)
1543 /* oops - please have a {} line as 1st line in file. */
1545 log_error(LOG_LEVEL_FATAL,
1546 "can't load actions file '%s': line %lu should begin with a '{': %s",
1547 csp->config->actions_file[fileid], linenum, buf);
1548 return 1; /* never get here */
1552 /* How did we get here? This is impossible! */
1554 log_error(LOG_LEVEL_FATAL,
1555 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1556 csp->config->actions_file[fileid], mode);
1557 return 1; /* never get here */
1564 if (!cur_action_used)
1566 free_action_spec(cur_action);
1568 free_alias_list(alias_list);
1570 /* the old one is now obsolete */
1571 if (current_actions_file[fileid])
1573 current_actions_file[fileid]->unloader = unload_actions_file;
1576 fs->next = files->next;
1578 current_actions_file[fileid] = fs;
1580 csp->actions_list[fileid] = fs;
1587 /*********************************************************************
1589 * Function : actions_to_text
1591 * Description : Converts a actionsfile entry from the internal
1592 * structure into a text line. The output is split
1593 * into one line for each action with line continuation.
1596 * 1 : action = The action to format.
1598 * Returns : A string. Caller must free it.
1599 * NULL on out-of-memory error.
1601 *********************************************************************/
1602 char * actions_to_text(const struct action_spec *action)
1604 unsigned long mask = action->mask;
1605 unsigned long add = action->add;
1606 char *result = strdup("");
1607 struct list_entry * lst;
1609 /* sanity - prevents "-feature +feature" */
1613 #define DEFINE_ACTION_BOOL(__name, __bit) \
1614 if (!(mask & __bit)) \
1616 string_append(&result, " -" __name " \\\n"); \
1618 else if (add & __bit) \
1620 string_append(&result, " +" __name " \\\n"); \
1623 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1624 if (!(mask & __bit)) \
1626 string_append(&result, " -" __name " \\\n"); \
1628 else if (add & __bit) \
1630 string_append(&result, " +" __name "{"); \
1631 string_append(&result, action->string[__index]); \
1632 string_append(&result, "} \\\n"); \
1635 #define DEFINE_ACTION_MULTI(__name, __index) \
1636 if (action->multi_remove_all[__index]) \
1638 string_append(&result, " -" __name " \\\n"); \
1642 lst = action->multi_remove[__index]->first; \
1645 string_append(&result, " -" __name "{"); \
1646 string_append(&result, lst->str); \
1647 string_append(&result, "} \\\n"); \
1651 lst = action->multi_add[__index]->first; \
1654 string_append(&result, " +" __name "{"); \
1655 string_append(&result, lst->str); \
1656 string_append(&result, "} \\\n"); \
1660 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1662 #include "actionlist.h"
1664 #undef DEFINE_ACTION_MULTI
1665 #undef DEFINE_ACTION_STRING
1666 #undef DEFINE_ACTION_BOOL
1667 #undef DEFINE_ACTION_ALIAS
1673 /*********************************************************************
1675 * Function : actions_to_html
1677 * Description : Converts a actionsfile entry from numeric form
1678 * ("mask" and "add") to a <br>-separated HTML string
1679 * in which each action is linked to its chapter in
1683 * 1 : csp = Client state (for config)
1684 * 2 : action = Action spec to be converted
1686 * Returns : A string. Caller must free it.
1687 * NULL on out-of-memory error.
1689 *********************************************************************/
1690 char * actions_to_html(const struct client_state *csp,
1691 const struct action_spec *action)
1693 unsigned long mask = action->mask;
1694 unsigned long add = action->add;
1695 char *result = strdup("");
1696 struct list_entry * lst;
1698 /* sanity - prevents "-feature +feature" */
1702 #define DEFINE_ACTION_BOOL(__name, __bit) \
1703 if (!(mask & __bit)) \
1705 string_append(&result, "\n<br>-"); \
1706 string_join(&result, add_help_link(__name, csp->config)); \
1708 else if (add & __bit) \
1710 string_append(&result, "\n<br>+"); \
1711 string_join(&result, add_help_link(__name, csp->config)); \
1714 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1715 if (!(mask & __bit)) \
1717 string_append(&result, "\n<br>-"); \
1718 string_join(&result, add_help_link(__name, csp->config)); \
1720 else if (add & __bit) \
1722 string_append(&result, "\n<br>+"); \
1723 string_join(&result, add_help_link(__name, csp->config)); \
1724 string_append(&result, "{"); \
1725 string_join(&result, html_encode(action->string[__index])); \
1726 string_append(&result, "}"); \
1729 #define DEFINE_ACTION_MULTI(__name, __index) \
1730 if (action->multi_remove_all[__index]) \
1732 string_append(&result, "\n<br>-"); \
1733 string_join(&result, add_help_link(__name, csp->config)); \
1737 lst = action->multi_remove[__index]->first; \
1740 string_append(&result, "\n<br>-"); \
1741 string_join(&result, add_help_link(__name, csp->config)); \
1742 string_append(&result, "{"); \
1743 string_join(&result, html_encode(lst->str)); \
1744 string_append(&result, "}"); \
1748 lst = action->multi_add[__index]->first; \
1751 string_append(&result, "\n<br>+"); \
1752 string_join(&result, add_help_link(__name, csp->config)); \
1753 string_append(&result, "{"); \
1754 string_join(&result, html_encode(lst->str)); \
1755 string_append(&result, "}"); \
1759 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1761 #include "actionlist.h"
1763 #undef DEFINE_ACTION_MULTI
1764 #undef DEFINE_ACTION_STRING
1765 #undef DEFINE_ACTION_BOOL
1766 #undef DEFINE_ACTION_ALIAS
1768 /* trim leading <br> */
1769 if (result && *result)
1772 result = strdup(result + 5);
1780 /*********************************************************************
1782 * Function : current_actions_to_html
1784 * Description : Converts a curren action spec to a <br> separated HTML
1785 * text in which each action is linked to its chapter in
1789 * 1 : csp = Client state (for config)
1790 * 2 : action = Current action spec to be converted
1792 * Returns : A string. Caller must free it.
1793 * NULL on out-of-memory error.
1795 *********************************************************************/
1796 char *current_action_to_html(const struct client_state *csp,
1797 const struct current_action_spec *action)
1799 unsigned long flags = action->flags;
1800 struct list_entry * lst;
1801 char *result = strdup("");
1802 char *active = strdup("");
1803 char *inactive = strdup("");
1805 #define DEFINE_ACTION_BOOL(__name, __bit) \
1806 if (flags & __bit) \
1808 string_append(&active, "\n<br>+"); \
1809 string_join(&active, add_help_link(__name, csp->config)); \
1813 string_append(&inactive, "\n<br>-"); \
1814 string_join(&inactive, add_help_link(__name, csp->config)); \
1817 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1818 if (flags & __bit) \
1820 string_append(&active, "\n<br>+"); \
1821 string_join(&active, add_help_link(__name, csp->config)); \
1822 string_append(&active, "{"); \
1823 string_join(&active, html_encode(action->string[__index])); \
1824 string_append(&active, "}"); \
1828 string_append(&inactive, "\n<br>-"); \
1829 string_join(&inactive, add_help_link(__name, csp->config)); \
1832 #define DEFINE_ACTION_MULTI(__name, __index) \
1833 lst = action->multi[__index]->first; \
1836 string_append(&inactive, "\n<br>-"); \
1837 string_join(&inactive, add_help_link(__name, csp->config)); \
1843 string_append(&active, "\n<br>+"); \
1844 string_join(&active, add_help_link(__name, csp->config)); \
1845 string_append(&active, "{"); \
1846 string_join(&active, html_encode(lst->str)); \
1847 string_append(&active, "}"); \
1852 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1854 #include "actionlist.h"
1856 #undef DEFINE_ACTION_MULTI
1857 #undef DEFINE_ACTION_STRING
1858 #undef DEFINE_ACTION_BOOL
1859 #undef DEFINE_ACTION_ALIAS
1863 string_append(&result, active);
1866 string_append(&result, "\n<br>");
1867 if (inactive != NULL)
1869 string_append(&result, inactive);
1876 /*********************************************************************
1878 * Function : action_to_line_of_text
1880 * Description : Converts a action spec to a single text line
1881 * listing the enabled actions.
1884 * 1 : action = Current action spec to be converted
1886 * Returns : A string. Caller must free it.
1887 * Out-of-memory errors are fatal.
1889 *********************************************************************/
1890 char *actions_to_line_of_text(const struct current_action_spec *action)
1893 struct list_entry *lst;
1895 const unsigned long flags = action->flags;
1897 active = strdup_or_die("");
1899 #define DEFINE_ACTION_BOOL(__name, __bit) \
1900 if (flags & __bit) \
1902 snprintf(buffer, sizeof(buffer), "+%s ", __name); \
1903 string_append(&active, buffer); \
1906 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1907 if (flags & __bit) \
1909 snprintf(buffer, sizeof(buffer), "+%s{%s} ", \
1910 __name, action->string[__index]); \
1911 string_append(&active, buffer); \
1914 #define DEFINE_ACTION_MULTI(__name, __index) \
1915 lst = action->multi[__index]->first; \
1916 while (lst != NULL) \
1918 snprintf(buffer, sizeof(buffer), "+%s{%s} ", \
1919 __name, lst->str); \
1920 string_append(&active, buffer); \
1924 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1926 #include "actionlist.h"
1928 #undef DEFINE_ACTION_MULTI
1929 #undef DEFINE_ACTION_STRING
1930 #undef DEFINE_ACTION_BOOL
1931 #undef DEFINE_ACTION_ALIAS
1935 log_error(LOG_LEVEL_FATAL, "Out of memory in action_to_line_of_text()");