1 const char actions_rcs[] = "$Id: actions.c,v 1.64 2011/03/03 14:39:13 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/actions.c,v $
6 * Purpose : Declares functions to work with actions files
7 * Functions declared include: FIXME
9 * Copyright : Written by and Copyright (C) 2001-2011 the
10 * Privoxy team. http://www.privoxy.org/
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will
23 * be useful, but WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A
25 * PARTICULAR PURPOSE. See the GNU General Public
26 * License for more details.
28 * The GNU General Public License should be included with
29 * this file. If not, you can view it at
30 * http://www.gnu.org/copyleft/gpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 *********************************************************************/
44 #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 #define AV_NONE 0 /* +opt -opt */
74 #define AV_ADD_STRING 1 /* +stropt{string} */
75 #define AV_REM_STRING 2 /* -stropt */
76 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
77 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
80 * We need a structure to hold the name, flag changes,
81 * type, and string index.
86 unsigned long mask; /* a bit set to "0" = remove action */
87 unsigned long add; /* a bit set to "1" = add action */
88 int takes_value; /* an AV_... constant */
89 int index; /* index into strings[] or multi[] */
93 * And with those building blocks in place, here's the array.
95 static const struct action_name action_names[] =
98 * Well actually there's no data here - it's in actionlist.h
99 * This keeps it together to make it easy to change.
101 * Here's the macros used to format it:
103 #define DEFINE_ACTION_MULTI(name,index) \
104 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
105 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
106 #define DEFINE_ACTION_STRING(name,flag,index) \
107 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
108 { "-" name, ~flag, 0, AV_REM_STRING, index },
109 #define DEFINE_ACTION_BOOL(name,flag) \
110 { "+" name, ACTION_MASK_ALL, flag }, \
111 { "-" name, ~flag, 0 },
112 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
114 #include "actionlist.h"
116 #undef DEFINE_ACTION_MULTI
117 #undef DEFINE_ACTION_STRING
118 #undef DEFINE_ACTION_BOOL
119 #undef DEFINE_ACTION_ALIAS
121 { NULL, 0, 0 } /* End marker */
125 static int load_one_actions_file(struct client_state *csp, int fileid);
128 /*********************************************************************
130 * Function : merge_actions
132 * Description : Merge two actions together.
133 * Similar to "dest += src".
136 * 1 : dest = Actions to modify.
137 * 2 : src = Action to add.
139 * Returns : JB_ERR_OK or JB_ERR_MEMORY
141 *********************************************************************/
142 jb_err merge_actions (struct action_spec *dest,
143 const struct action_spec *src)
148 dest->mask &= src->mask;
149 dest->add &= src->mask;
150 dest->add |= src->add;
152 for (i = 0; i < ACTION_STRING_COUNT; i++)
154 char * str = src->string[i];
157 freez(dest->string[i]);
158 dest->string[i] = strdup(str);
159 if (NULL == dest->string[i])
161 return JB_ERR_MEMORY;
166 for (i = 0; i < ACTION_MULTI_COUNT; i++)
168 if (src->multi_remove_all[i])
170 /* Remove everything from dest */
171 list_remove_all(dest->multi_remove[i]);
172 dest->multi_remove_all[i] = 1;
174 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
176 else if (dest->multi_remove_all[i])
179 * dest already removes everything, so we only need to worry
182 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
183 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
187 /* No "remove all"s to worry about. */
188 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
189 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
190 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
203 /*********************************************************************
205 * Function : copy_action
207 * Description : Copy an action_specs.
208 * Similar to "dest = src".
211 * 1 : dest = Destination of copy.
212 * 2 : src = Source for copy.
216 *********************************************************************/
217 jb_err copy_action (struct action_spec *dest,
218 const struct action_spec *src)
221 jb_err err = JB_ERR_OK;
224 memset(dest, '\0', sizeof(*dest));
226 dest->mask = src->mask;
227 dest->add = src->add;
229 for (i = 0; i < ACTION_STRING_COUNT; i++)
231 char * str = src->string[i];
237 return JB_ERR_MEMORY;
239 dest->string[i] = str;
243 for (i = 0; i < ACTION_MULTI_COUNT; i++)
245 dest->multi_remove_all[i] = src->multi_remove_all[i];
246 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
251 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
260 /*********************************************************************
262 * Function : free_action_spec
264 * Description : Frees an action_spec and the memory used by it.
267 * 1 : src = Source to free.
271 *********************************************************************/
272 void free_action_spec(struct action_spec *src)
279 /*********************************************************************
281 * Function : free_action
283 * Description : Destroy an action_spec. Frees memory used by it,
284 * except for the memory used by the struct action_spec
288 * 1 : src = Source to free.
292 *********************************************************************/
293 void free_action (struct action_spec *src)
302 for (i = 0; i < ACTION_STRING_COUNT; i++)
304 freez(src->string[i]);
307 for (i = 0; i < ACTION_MULTI_COUNT; i++)
309 destroy_list(src->multi_remove[i]);
310 destroy_list(src->multi_add[i]);
313 memset(src, '\0', sizeof(*src));
317 /*********************************************************************
319 * Function : get_action_token
321 * Description : Parses a line for the first action.
322 * Modifies it's input array, doesn't allocate memory.
324 * *line=" +abc{def} -ghi "
331 * 1 : line = [in] The line containing the action.
332 * [out] Start of next action on line, or
333 * NULL if we reached the end of line before
334 * we found an action.
335 * 2 : name = [out] Start of action name, null
336 * terminated. NULL on EOL
337 * 3 : value = [out] Start of action value, null
338 * terminated. NULL if none or EOL.
340 * Returns : JB_ERR_OK => Ok
341 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
343 *********************************************************************/
344 jb_err get_action_token(char **line, char **name, char **value)
349 /* set default returns */
354 /* Eat any leading whitespace */
355 while ((*str == ' ') || (*str == '\t'))
367 /* null name, just value is prohibited */
374 while (((ch = *str) != '\0') &&
375 (ch != ' ') && (ch != '\t') && (ch != '{'))
379 /* error, '}' without '{' */
391 /* EOL - be careful not to run off buffer */
396 /* More to parse next time. */
405 str = strchr(str, '}');
422 /*********************************************************************
424 * Function : action_used_to_be_valid
426 * Description : Checks if unrecognized actions were valid in earlier
430 * 1 : action = The string containing the action to check.
432 * Returns : True if yes, otherwise false.
434 *********************************************************************/
435 static int action_used_to_be_valid(const char *action)
437 static const char *formerly_valid_actions[] = {
440 "send-vanilla-wafer",
442 "treat-forbidden-connects-like-blocks",
448 for (i = 0; i < SZ(formerly_valid_actions); i++)
450 if (0 == strcmpic(action, formerly_valid_actions[i]))
459 /*********************************************************************
461 * Function : get_actions
463 * Description : Parses a list of actions.
466 * 1 : line = The string containing the actions.
467 * Will be written to by this function.
468 * 2 : alias_list = Custom alias list, or NULL for none.
469 * 3 : cur_action = Where to store the action. Caller
472 * Returns : JB_ERR_OK => Ok
473 * JB_ERR_PARSE => Parse error (line was trashed anyway)
474 * nonzero => Out of memory (line was trashed anyway)
476 *********************************************************************/
477 jb_err get_actions(char *line,
478 struct action_alias * alias_list,
479 struct action_spec *cur_action)
482 init_action(cur_action);
483 cur_action->mask = ACTION_MASK_ALL;
487 char * option = NULL;
490 err = get_action_token(&line, &option, &value);
498 /* handle option in 'option' */
500 /* Check for standard action name */
501 const struct action_name * action = action_names;
503 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
507 if (action->name != NULL)
510 cur_action->mask &= action->mask;
511 cur_action->add &= action->mask;
512 cur_action->add |= action->add;
514 switch (action->takes_value)
517 /* ignore any option. */
521 /* add single string. */
523 if ((value == NULL) || (*value == '\0'))
525 if (0 == strcmpic(action->name, "+block"))
528 * XXX: Temporary backwards compatibility hack.
529 * XXX: should include line number.
531 value = "No reason specified.";
532 log_error(LOG_LEVEL_ERROR,
533 "block action without reason found. This may "
534 "become a fatal error in future versions.");
541 /* FIXME: should validate option string here */
542 freez (cur_action->string[action->index]);
543 cur_action->string[action->index] = strdup(value);
544 if (NULL == cur_action->string[action->index])
546 return JB_ERR_MEMORY;
552 /* remove single string. */
554 freez (cur_action->string[action->index]);
559 /* append multi string. */
561 struct list * remove_p = cur_action->multi_remove[action->index];
562 struct list * add_p = cur_action->multi_add[action->index];
564 if ((value == NULL) || (*value == '\0'))
569 list_remove_item(remove_p, value);
570 err = enlist_unique(add_p, value, 0);
579 /* remove multi string. */
581 struct list * remove_p = cur_action->multi_remove[action->index];
582 struct list * add_p = cur_action->multi_add[action->index];
584 if ( (value == NULL) || (*value == '\0')
585 || ((*value == '*') && (value[1] == '\0')) )
588 * no option, or option == "*".
592 list_remove_all(remove_p);
593 list_remove_all(add_p);
594 cur_action->multi_remove_all[action->index] = 1;
598 /* Valid option - remove only 1 option */
600 if ( !cur_action->multi_remove_all[action->index] )
602 /* there isn't a catch-all in the remove list already */
603 err = enlist_unique(remove_p, value, 0);
609 list_remove_item(add_p, value);
614 /* Shouldn't get here unless there's memory corruption. */
621 /* try user aliases. */
622 const struct action_alias * alias = alias_list;
624 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
631 merge_actions(cur_action, alias->action);
633 else if (((size_t)2 < strlen(option)) && action_used_to_be_valid(option+1))
635 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
636 "in this Privoxy release. Ignored.", option+1);
638 else if (((size_t)2 < strlen(option)) && 0 == strcmpic(option+1, "hide-forwarded-for-headers"))
640 log_error(LOG_LEVEL_FATAL, "The action 'hide-forwarded-for-headers' "
641 "is no longer valid in this Privoxy release. "
642 "Use 'change-x-forwarded-for' instead.");
646 /* Bad action name */
648 * XXX: This is a fatal error and Privoxy will later on exit
649 * in load_one_actions_file() because of an "invalid line".
651 * It would be preferable to name the offending option in that
652 * error message, but currently there is no way to do that and
653 * we have to live with two error messages for basically the
656 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
667 /*********************************************************************
669 * Function : init_current_action
671 * Description : Zero out an action.
674 * 1 : dest = An uninitialized current_action_spec.
678 *********************************************************************/
679 void init_current_action (struct current_action_spec *dest)
681 memset(dest, '\0', sizeof(*dest));
683 dest->flags = ACTION_MOST_COMPATIBLE;
687 /*********************************************************************
689 * Function : init_action
691 * Description : Zero out an action.
694 * 1 : dest = An uninitialized action_spec.
698 *********************************************************************/
699 void init_action (struct action_spec *dest)
701 memset(dest, '\0', sizeof(*dest));
705 /*********************************************************************
707 * Function : merge_current_action
709 * Description : Merge two actions together.
710 * Similar to "dest += src".
711 * Differences between this and merge_actions()
712 * is that this one doesn't allocate memory for
713 * strings (so "src" better be in memory for at least
714 * as long as "dest" is, and you'd better free
715 * "dest" using "free_current_action").
716 * Also, there is no mask or remove lists in dest.
717 * (If we're applying it to a URL, we don't need them)
720 * 1 : dest = Current actions, to modify.
721 * 2 : src = Action to add.
723 * Returns 0 : no error
724 * !=0 : error, probably JB_ERR_MEMORY.
726 *********************************************************************/
727 jb_err merge_current_action (struct current_action_spec *dest,
728 const struct action_spec *src)
731 jb_err err = JB_ERR_OK;
733 dest->flags &= src->mask;
734 dest->flags |= src->add;
736 for (i = 0; i < ACTION_STRING_COUNT; i++)
738 char * str = src->string[i];
744 return JB_ERR_MEMORY;
746 freez(dest->string[i]);
747 dest->string[i] = str;
751 for (i = 0; i < ACTION_MULTI_COUNT; i++)
753 if (src->multi_remove_all[i])
755 /* Remove everything from dest, then add src->multi_add */
756 err = list_duplicate(dest->multi[i], src->multi_add[i]);
764 list_remove_list(dest->multi[i], src->multi_remove[i]);
765 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
776 /*********************************************************************
778 * Function : update_action_bits_for_all_tags
780 * Description : Updates the action bits based on all matching tags.
783 * 1 : csp = Current client state (buffers, headers, etc...)
785 * Returns : 0 if no tag matched, or
788 *********************************************************************/
789 int update_action_bits_for_all_tags(struct client_state *csp)
791 struct list_entry *tag;
794 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
796 if (update_action_bits_for_tag(csp, tag->str))
806 /*********************************************************************
808 * Function : update_action_bits_for_tag
810 * Description : Updates the action bits based on the action sections
811 * whose tag patterns match a provided tag.
814 * 1 : csp = Current client state (buffers, headers, etc...)
815 * 2 : tag = The tag on which the update should be based on
817 * Returns : 0 if no tag matched, or
820 *********************************************************************/
821 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
823 struct file_list *fl;
824 struct url_actions *b;
830 assert(list_contains_item(csp->tags, tag));
832 /* Run through all action files, */
833 for (i = 0; i < MAX_AF_FILES; i++)
835 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
837 /* Skip empty files */
841 /* and through all the action patterns, */
842 for (b = b->next; NULL != b; b = b->next)
844 /* skip the URL patterns, */
845 if (NULL == b->url->tag_regex)
850 /* and check if one of the tag patterns matches the tag, */
851 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
853 /* if it does, update the action bit map, */
854 if (merge_current_action(csp->action, b->action))
856 log_error(LOG_LEVEL_ERROR,
857 "Out of memory while changing action bits");
859 /* and signal the change. */
869 /*********************************************************************
871 * Function : free_current_action
873 * Description : Free memory used by a current_action_spec.
874 * Does not free the current_action_spec itself.
877 * 1 : src = Source to free.
881 *********************************************************************/
882 void free_current_action(struct current_action_spec *src)
886 for (i = 0; i < ACTION_STRING_COUNT; i++)
888 freez(src->string[i]);
891 for (i = 0; i < ACTION_MULTI_COUNT; i++)
893 destroy_list(src->multi[i]);
896 memset(src, '\0', sizeof(*src));
900 static struct file_list *current_actions_file[MAX_AF_FILES] = {
901 NULL, NULL, NULL, NULL, NULL,
902 NULL, NULL, NULL, NULL, NULL
906 #ifdef FEATURE_GRACEFUL_TERMINATION
907 /*********************************************************************
909 * Function : unload_current_actions_file
911 * Description : Unloads current actions file - reset to state at
912 * beginning of program.
918 *********************************************************************/
919 void unload_current_actions_file(void)
923 for (i = 0; i < MAX_AF_FILES; i++)
925 if (current_actions_file[i])
927 current_actions_file[i]->unloader = unload_actions_file;
928 current_actions_file[i] = NULL;
932 #endif /* FEATURE_GRACEFUL_TERMINATION */
935 /*********************************************************************
937 * Function : unload_actions_file
939 * Description : Unloads an actions module.
942 * 1 : file_data = the data structure associated with the
947 *********************************************************************/
948 void unload_actions_file(void *file_data)
950 struct url_actions * next;
951 struct url_actions * cur = (struct url_actions *)file_data;
955 free_url_spec(cur->url);
956 if ((next == NULL) || (next->action != cur->action))
959 * As the action settings might be shared,
960 * we can only free them if the current
961 * url pattern is the last one, or if the
962 * next one is using different settings.
964 free_action_spec(cur->action);
972 /*********************************************************************
974 * Function : free_alias_list
976 * Description : Free memory used by a list of aliases.
979 * 1 : alias_list = Linked list to free.
983 *********************************************************************/
984 void free_alias_list(struct action_alias *alias_list)
986 while (alias_list != NULL)
988 struct action_alias * next = alias_list->next;
989 alias_list->next = NULL;
990 freez(alias_list->name);
991 free_action(alias_list->action);
998 /*********************************************************************
1000 * Function : load_action_files
1002 * Description : Read and parse all the action files and add to files
1006 * 1 : csp = Current client state (buffers, headers, etc...)
1008 * Returns : 0 => Ok, everything else is an error.
1010 *********************************************************************/
1011 int load_action_files(struct client_state *csp)
1016 for (i = 0; i < MAX_AF_FILES; i++)
1018 if (csp->config->actions_file[i])
1020 result = load_one_actions_file(csp, i);
1026 else if (current_actions_file[i])
1028 current_actions_file[i]->unloader = unload_actions_file;
1029 current_actions_file[i] = NULL;
1036 /*********************************************************************
1038 * Function : load_one_actions_file
1040 * Description : Read and parse a action file and add to files
1044 * 1 : csp = Current client state (buffers, headers, etc...)
1045 * 2 : fileid = File index to load.
1047 * Returns : 0 => Ok, everything else is an error.
1049 *********************************************************************/
1050 static int load_one_actions_file(struct client_state *csp, int fileid)
1055 * Note: Keep these in the order they occur in the file, they are
1056 * sometimes tested with <=
1059 MODE_START_OF_FILE = 1,
1061 MODE_DESCRIPTION = 3,
1067 struct url_actions *last_perm;
1068 struct url_actions *perm;
1070 struct file_list *fs;
1071 struct action_spec * cur_action = NULL;
1072 int cur_action_used = 0;
1073 struct action_alias * alias_list = NULL;
1074 unsigned long linenum = 0;
1075 mode = MODE_START_OF_FILE;
1077 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1079 /* No need to load */
1080 csp->actions_list[fileid] = current_actions_file[fileid];
1085 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1086 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1087 "with their complete file names.", csp->config->actions_file[fileid]);
1088 return 1; /* never get here */
1091 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1092 if (last_perm == NULL)
1094 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1095 csp->config->actions_file[fileid]);
1096 return 1; /* never get here */
1099 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1101 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1102 csp->config->actions_file[fileid]);
1103 return 1; /* never get here */
1106 log_error(LOG_LEVEL_INFO, "Loading actions file: %s", csp->config->actions_file[fileid]);
1108 while (read_config_line(fp, &linenum, &buf) != NULL)
1112 /* It's a header block */
1115 /* It's {{settings}} or {{alias}} */
1116 size_t len = strlen(buf);
1117 char * start = buf + 2;
1118 char * end = buf + len - 1;
1119 if ((len < (size_t)5) || (*end-- != '}') || (*end-- != '}'))
1123 log_error(LOG_LEVEL_FATAL,
1124 "can't load actions file '%s': invalid line (%lu): %s",
1125 csp->config->actions_file[fileid], linenum, buf);
1126 return 1; /* never get here */
1129 /* Trim leading and trailing whitespace. */
1137 log_error(LOG_LEVEL_FATAL,
1138 "can't load actions file '%s': invalid line (%lu): {{ }}",
1139 csp->config->actions_file[fileid], linenum);
1140 return 1; /* never get here */
1144 * An actionsfile can optionally contain the following blocks.
1145 * They *MUST* be in this order, to simplify processing:
1151 * ...free text, format TBD, but no line may start with a '{'...
1156 * The actual actions must be *after* these special blocks.
1157 * None of these special blocks may be repeated.
1160 if (0 == strcmpic(start, "settings"))
1162 /* it's a {{settings}} block */
1163 if (mode >= MODE_SETTINGS)
1165 /* {{settings}} must be first thing in file and must only
1169 log_error(LOG_LEVEL_FATAL,
1170 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1171 csp->config->actions_file[fileid], linenum);
1173 mode = MODE_SETTINGS;
1175 else if (0 == strcmpic(start, "description"))
1177 /* it's a {{description}} block */
1178 if (mode >= MODE_DESCRIPTION)
1180 /* {{description}} is a singleton and only {{settings}} may proceed it
1183 log_error(LOG_LEVEL_FATAL,
1184 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1185 csp->config->actions_file[fileid], linenum);
1187 mode = MODE_DESCRIPTION;
1189 else if (0 == strcmpic(start, "alias"))
1191 /* it's an {{alias}} block */
1192 if (mode >= MODE_ALIAS)
1194 /* {{alias}} must be first thing in file, possibly after
1195 * {{settings}} and {{description}}
1197 * {{alias}} must only appear once.
1199 * Note that these are new restrictions introduced in
1200 * v2.9.10 in order to make actionsfile editing simpler.
1201 * (Otherwise, reordering actionsfile entries without
1202 * completely rewriting the file becomes non-trivial)
1205 log_error(LOG_LEVEL_FATAL,
1206 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1207 csp->config->actions_file[fileid], linenum);
1213 /* invalid {{something}} block */
1215 log_error(LOG_LEVEL_FATAL,
1216 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1217 csp->config->actions_file[fileid], linenum, start);
1218 return 1; /* never get here */
1223 /* It's an actions block */
1229 mode = MODE_ACTIONS;
1231 /* free old action */
1234 if (!cur_action_used)
1236 free_action_spec(cur_action);
1240 cur_action_used = 0;
1241 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1242 if (cur_action == NULL)
1245 log_error(LOG_LEVEL_FATAL,
1246 "can't load actions file '%s': out of memory",
1247 csp->config->actions_file[fileid]);
1248 return 1; /* never get here */
1250 init_action(cur_action);
1253 actions_buf = strdup(buf + 1);
1254 if (actions_buf == NULL)
1257 log_error(LOG_LEVEL_FATAL,
1258 "can't load actions file '%s': out of memory",
1259 csp->config->actions_file[fileid]);
1260 return 1; /* never get here */
1263 /* check we have a trailing } and then trim it */
1264 end = actions_buf + strlen(actions_buf) - 1;
1270 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1271 "Missing trailing '}' in action section starting at line (%lu): %s",
1272 csp->config->actions_file[fileid], linenum, buf);
1273 return 1; /* never get here */
1277 /* trim any whitespace immediately inside {} */
1280 if (get_actions(actions_buf, alias_list, cur_action))
1285 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': "
1286 "can't completely parse the action section starting at line (%lu): %s",
1287 csp->config->actions_file[fileid], linenum, buf);
1288 return 1; /* never get here */
1293 else if (mode == MODE_SETTINGS)
1296 * Part of the {{settings}} block.
1297 * For now only serves to check if the file's minimum Privoxy
1298 * version requirement is met, but we may want to read & check
1299 * permissions when we go multi-user.
1301 if (!strncmp(buf, "for-privoxy-version=", 20))
1303 char *version_string, *fields[3];
1306 if ((version_string = strdup(buf + 20)) == NULL)
1309 log_error(LOG_LEVEL_FATAL,
1310 "can't load actions file '%s': out of memory!",
1311 csp->config->actions_file[fileid]);
1312 return 1; /* never get here */
1315 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1317 if (num_fields < 1 || atoi(fields[0]) == 0)
1319 log_error(LOG_LEVEL_ERROR,
1320 "While loading actions file '%s': invalid line (%lu): %s",
1321 csp->config->actions_file[fileid], linenum, buf);
1323 else if ( atoi(fields[0]) > VERSION_MAJOR
1324 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1325 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1328 log_error(LOG_LEVEL_FATAL,
1329 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1330 csp->config->actions_file[fileid], linenum, buf );
1331 return 1; /* never get here */
1333 free(version_string);
1336 else if (mode == MODE_DESCRIPTION)
1339 * Part of the {{description}} block.
1343 else if (mode == MODE_ALIAS)
1348 char actions_buf[BUFFER_SIZE];
1349 struct action_alias * new_alias;
1351 char * start = strchr(buf, '=');
1354 if ((start == NULL) || (start == buf))
1356 log_error(LOG_LEVEL_FATAL,
1357 "can't load actions file '%s': invalid alias line (%lu): %s",
1358 csp->config->actions_file[fileid], linenum, buf);
1359 return 1; /* never get here */
1362 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1365 log_error(LOG_LEVEL_FATAL,
1366 "can't load actions file '%s': out of memory!",
1367 csp->config->actions_file[fileid]);
1368 return 1; /* never get here */
1371 /* Eat any the whitespace before the '=' */
1373 while ((*end == ' ') || (*end == '\t'))
1376 * we already know we must have at least 1 non-ws char
1377 * at start of buf - no need to check
1383 /* Eat any the whitespace after the '=' */
1385 while ((*start == ' ') || (*start == '\t'))
1391 log_error(LOG_LEVEL_FATAL,
1392 "can't load actions file '%s': invalid alias line (%lu): %s",
1393 csp->config->actions_file[fileid], linenum, buf);
1394 return 1; /* never get here */
1397 if ((new_alias->name = strdup(buf)) == NULL)
1400 log_error(LOG_LEVEL_FATAL,
1401 "can't load actions file '%s': out of memory!",
1402 csp->config->actions_file[fileid]);
1403 return 1; /* never get here */
1406 strlcpy(actions_buf, start, sizeof(actions_buf));
1408 if (get_actions(actions_buf, alias_list, new_alias->action))
1412 log_error(LOG_LEVEL_FATAL,
1413 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1414 csp->config->actions_file[fileid], linenum, buf, start);
1415 return 1; /* never get here */
1419 new_alias->next = alias_list;
1420 alias_list = new_alias;
1422 else if (mode == MODE_ACTIONS)
1424 /* it's an URL pattern */
1426 /* allocate a new node */
1427 if ((perm = zalloc(sizeof(*perm))) == NULL)
1430 log_error(LOG_LEVEL_FATAL,
1431 "can't load actions file '%s': out of memory!",
1432 csp->config->actions_file[fileid]);
1433 return 1; /* never get here */
1436 perm->action = cur_action;
1437 cur_action_used = 1;
1439 /* Save the URL pattern */
1440 if (create_url_spec(perm->url, buf))
1443 log_error(LOG_LEVEL_FATAL,
1444 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1445 csp->config->actions_file[fileid], linenum, buf);
1446 return 1; /* never get here */
1449 /* add it to the list */
1450 last_perm->next = perm;
1453 else if (mode == MODE_START_OF_FILE)
1455 /* oops - please have a {} line as 1st line in file. */
1457 log_error(LOG_LEVEL_FATAL,
1458 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1459 csp->config->actions_file[fileid], linenum, buf);
1460 return 1; /* never get here */
1464 /* How did we get here? This is impossible! */
1466 log_error(LOG_LEVEL_FATAL,
1467 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1468 csp->config->actions_file[fileid], mode);
1469 return 1; /* never get here */
1476 if (!cur_action_used)
1478 free_action_spec(cur_action);
1480 free_alias_list(alias_list);
1482 /* the old one is now obsolete */
1483 if (current_actions_file[fileid])
1485 current_actions_file[fileid]->unloader = unload_actions_file;
1488 fs->next = files->next;
1490 current_actions_file[fileid] = fs;
1492 csp->actions_list[fileid] = fs;
1499 /*********************************************************************
1501 * Function : actions_to_text
1503 * Description : Converts a actionsfile entry from the internal
1504 * structure into a text line. The output is split
1505 * into one line for each action with line continuation.
1508 * 1 : action = The action to format.
1510 * Returns : A string. Caller must free it.
1511 * NULL on out-of-memory error.
1513 *********************************************************************/
1514 char * actions_to_text(const struct action_spec *action)
1516 unsigned long mask = action->mask;
1517 unsigned long add = action->add;
1518 char *result = strdup("");
1519 struct list_entry * lst;
1521 /* sanity - prevents "-feature +feature" */
1525 #define DEFINE_ACTION_BOOL(__name, __bit) \
1526 if (!(mask & __bit)) \
1528 string_append(&result, " -" __name " \\\n"); \
1530 else if (add & __bit) \
1532 string_append(&result, " +" __name " \\\n"); \
1535 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1536 if (!(mask & __bit)) \
1538 string_append(&result, " -" __name " \\\n"); \
1540 else if (add & __bit) \
1542 string_append(&result, " +" __name "{"); \
1543 string_append(&result, action->string[__index]); \
1544 string_append(&result, "} \\\n"); \
1547 #define DEFINE_ACTION_MULTI(__name, __index) \
1548 if (action->multi_remove_all[__index]) \
1550 string_append(&result, " -" __name " \\\n"); \
1554 lst = action->multi_remove[__index]->first; \
1557 string_append(&result, " -" __name "{"); \
1558 string_append(&result, lst->str); \
1559 string_append(&result, "} \\\n"); \
1563 lst = action->multi_add[__index]->first; \
1566 string_append(&result, " +" __name "{"); \
1567 string_append(&result, lst->str); \
1568 string_append(&result, "} \\\n"); \
1572 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1574 #include "actionlist.h"
1576 #undef DEFINE_ACTION_MULTI
1577 #undef DEFINE_ACTION_STRING
1578 #undef DEFINE_ACTION_BOOL
1579 #undef DEFINE_ACTION_ALIAS
1585 /*********************************************************************
1587 * Function : actions_to_html
1589 * Description : Converts a actionsfile entry from numeric form
1590 * ("mask" and "add") to a <br>-seperated HTML string
1591 * in which each action is linked to its chapter in
1595 * 1 : csp = Client state (for config)
1596 * 2 : action = Action spec to be converted
1598 * Returns : A string. Caller must free it.
1599 * NULL on out-of-memory error.
1601 *********************************************************************/
1602 char * actions_to_html(const struct client_state *csp,
1603 const struct action_spec *action)
1605 unsigned long mask = action->mask;
1606 unsigned long add = action->add;
1607 char *result = strdup("");
1608 struct list_entry * lst;
1610 /* sanity - prevents "-feature +feature" */
1614 #define DEFINE_ACTION_BOOL(__name, __bit) \
1615 if (!(mask & __bit)) \
1617 string_append(&result, "\n<br>-"); \
1618 string_join(&result, add_help_link(__name, csp->config)); \
1620 else if (add & __bit) \
1622 string_append(&result, "\n<br>+"); \
1623 string_join(&result, add_help_link(__name, csp->config)); \
1626 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1627 if (!(mask & __bit)) \
1629 string_append(&result, "\n<br>-"); \
1630 string_join(&result, add_help_link(__name, csp->config)); \
1632 else if (add & __bit) \
1634 string_append(&result, "\n<br>+"); \
1635 string_join(&result, add_help_link(__name, csp->config)); \
1636 string_append(&result, "{"); \
1637 string_join(&result, html_encode(action->string[__index])); \
1638 string_append(&result, "}"); \
1641 #define DEFINE_ACTION_MULTI(__name, __index) \
1642 if (action->multi_remove_all[__index]) \
1644 string_append(&result, "\n<br>-"); \
1645 string_join(&result, add_help_link(__name, csp->config)); \
1649 lst = action->multi_remove[__index]->first; \
1652 string_append(&result, "\n<br>-"); \
1653 string_join(&result, add_help_link(__name, csp->config)); \
1654 string_append(&result, "{"); \
1655 string_join(&result, html_encode(lst->str)); \
1656 string_append(&result, "}"); \
1660 lst = action->multi_add[__index]->first; \
1663 string_append(&result, "\n<br>+"); \
1664 string_join(&result, add_help_link(__name, csp->config)); \
1665 string_append(&result, "{"); \
1666 string_join(&result, html_encode(lst->str)); \
1667 string_append(&result, "}"); \
1671 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1673 #include "actionlist.h"
1675 #undef DEFINE_ACTION_MULTI
1676 #undef DEFINE_ACTION_STRING
1677 #undef DEFINE_ACTION_BOOL
1678 #undef DEFINE_ACTION_ALIAS
1680 /* trim leading <br> */
1681 if (result && *result)
1684 result = strdup(result + 5);
1692 /*********************************************************************
1694 * Function : current_actions_to_html
1696 * Description : Converts a curren action spec to a <br> seperated HTML
1697 * text in which each action is linked to its chapter in
1701 * 1 : csp = Client state (for config)
1702 * 2 : action = Current action spec to be converted
1704 * Returns : A string. Caller must free it.
1705 * NULL on out-of-memory error.
1707 *********************************************************************/
1708 char *current_action_to_html(const struct client_state *csp,
1709 const struct current_action_spec *action)
1711 unsigned long flags = action->flags;
1712 struct list_entry * lst;
1713 char *result = strdup("");
1714 char *active = strdup("");
1715 char *inactive = strdup("");
1717 #define DEFINE_ACTION_BOOL(__name, __bit) \
1718 if (flags & __bit) \
1720 string_append(&active, "\n<br>+"); \
1721 string_join(&active, add_help_link(__name, csp->config)); \
1725 string_append(&inactive, "\n<br>-"); \
1726 string_join(&inactive, add_help_link(__name, csp->config)); \
1729 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1730 if (flags & __bit) \
1732 string_append(&active, "\n<br>+"); \
1733 string_join(&active, add_help_link(__name, csp->config)); \
1734 string_append(&active, "{"); \
1735 string_join(&active, html_encode(action->string[__index])); \
1736 string_append(&active, "}"); \
1740 string_append(&inactive, "\n<br>-"); \
1741 string_join(&inactive, add_help_link(__name, csp->config)); \
1744 #define DEFINE_ACTION_MULTI(__name, __index) \
1745 lst = action->multi[__index]->first; \
1748 string_append(&inactive, "\n<br>-"); \
1749 string_join(&inactive, add_help_link(__name, csp->config)); \
1755 string_append(&active, "\n<br>+"); \
1756 string_join(&active, add_help_link(__name, csp->config)); \
1757 string_append(&active, "{"); \
1758 string_join(&active, html_encode(lst->str)); \
1759 string_append(&active, "}"); \
1764 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1766 #include "actionlist.h"
1768 #undef DEFINE_ACTION_MULTI
1769 #undef DEFINE_ACTION_STRING
1770 #undef DEFINE_ACTION_BOOL
1771 #undef DEFINE_ACTION_ALIAS
1775 string_append(&result, active);
1778 string_append(&result, "\n<br>");
1779 if (inactive != NULL)
1781 string_append(&result, inactive);