1 const char actions_rcs[] = "$Id: actions.c,v 1.46 2008/03/27 18:27:20 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-2008 the SourceForge
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.
36 * Revision 1.46 2008/03/27 18:27:20 fabiankeil
37 * Remove kill-popups action.
39 * Revision 1.45 2008/03/24 11:21:02 fabiankeil
40 * Share the action settings for multiple patterns in the same
41 * section so we waste less memory for gigantic block lists
42 * (and load them slightly faster). Reported by Franz Schwartau.
44 * Revision 1.44 2008/03/04 18:30:34 fabiankeil
45 * Remove the treat-forbidden-connects-like-blocks action. We now
46 * use the "blocked" page for forbidden CONNECT requests by default.
48 * Revision 1.43 2008/03/01 14:00:43 fabiankeil
49 * Let the block action take the reason for the block
50 * as argument and show it on the "blocked" page.
52 * Revision 1.42 2008/02/09 15:15:38 fabiankeil
53 * List active and inactive actions in the show-url-info's
54 * "Final results" section separately. Patch submitted by Lee
55 * in #1830056, modified to list active actions first.
57 * Revision 1.41 2008/01/28 20:17:40 fabiankeil
58 * - Mark some parameters as immutable.
59 * - Hide update_action_bits_for_all_tags() while it's unused.
61 * Revision 1.40 2007/05/21 10:26:50 fabiankeil
62 * - Use strlcpy() instead of strcpy().
63 * - Provide a reason why loading the actions
64 * file might have failed.
66 * Revision 1.39 2007/04/17 18:21:45 fabiankeil
67 * Split update_action_bits() into
68 * update_action_bits_for_all_tags()
69 * and update_action_bits_for_tag().
71 * Revision 1.38 2007/04/15 16:39:20 fabiankeil
72 * Introduce tags as alternative way to specify which
73 * actions apply to a request. At the moment tags can be
74 * created based on client and server headers.
76 * Revision 1.37 2007/03/11 15:56:12 fabiankeil
77 * Add kludge to log unknown aliases and actions before exiting.
79 * Revision 1.36 2006/12/28 17:15:42 fabiankeil
80 * Fix gcc43 conversion warning.
82 * Revision 1.35 2006/07/18 14:48:45 david__schmidt
83 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
84 * with what was really the latest development (the v_3_0_branch branch)
86 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
87 * Multiple filter file support
89 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
90 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
91 * this becomes important when jcc.h gets included later down the line.
93 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
94 * - Implemented Privoxy version requirement through
95 * for-privoxy-version= statement in {{settings}}
97 * - Fix for unchecked out-of-memory condition
99 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
100 * Fixed memory leak reported by Dan Price in Bug #694713
102 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
103 * Extended unload_current_actions_file() to multiple AFs.
104 * Thanks to Oliver Stoeneberg for the hint.
106 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
107 * Change unsigned to unsigned long in actions_name struct. This closes
108 * SourceForge Bug #539284.
110 * Revision 1.32 2002/05/12 21:36:29 jongfoster
111 * Correcting function comments
113 * Revision 1.31 2002/05/06 07:56:50 oes
114 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
116 * Revision 1.30 2002/04/30 11:14:52 oes
117 * Made csp the first parameter in *action_to_html
119 * Revision 1.29 2002/04/26 19:30:54 jongfoster
120 * - current_action_to_html(): Adding help link for the "-" form of
121 * one-string actions.
122 * - Some actions had "<br>-", some "<br> -" (note the space).
123 * Standardizing on no space.
124 * - Greatly simplifying some of the code by using string_join()
127 * Revision 1.28 2002/04/26 12:53:15 oes
128 * - CGI AF editor now writes action lines split into
129 * single lines with line continuation
130 * - actions_to_html now embeds each action name in
132 * - current_action_to_text is now called current_action_to_html
133 * and acts like actions_to_html
135 * Revision 1.27 2002/04/24 02:10:31 oes
136 * - Jon's patch for multiple AFs:
137 * - split load_actions_file, add load_one_actions_file
138 * - make csp->actions_list files an array
139 * - remember file id with each action
140 * - Copy_action now frees dest action before copying
142 * Revision 1.26 2002/03/26 22:29:54 swa
143 * we have a new homepage!
145 * Revision 1.25 2002/03/24 13:25:43 swa
146 * name change related issues
148 * Revision 1.24 2002/03/16 23:54:06 jongfoster
149 * Adding graceful termination feature, to help look for memory leaks.
150 * If you enable this (which, by design, has to be done by hand
151 * editing config.h) and then go to http://i.j.b/die, then the program
152 * will exit cleanly after the *next* request. It should free all the
153 * memory that was used.
155 * Revision 1.23 2002/03/07 03:46:16 oes
156 * Fixed compiler warnings
158 * Revision 1.22 2002/01/21 00:27:02 jongfoster
159 * Allowing free_action(NULL).
160 * Moving the functions that #include actionlist.h to the end of the file,
161 * because the Visual C++ 97 debugger gets extremely confused if you try
162 * to debug any code that comes after them in the file.
164 * Revision 1.21 2002/01/17 20:54:44 jongfoster
165 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
167 * Revision 1.20 2001/11/22 21:56:49 jongfoster
168 * Making action_spec->flags into an unsigned long rather than just an
170 * Fixing a bug in the display of -add-header and -wafer
172 * Revision 1.19 2001/11/13 00:14:07 jongfoster
173 * Fixing stupid bug now I've figured out what || means.
174 * (It always returns 0 or 1, not one of it's paramaters.)
176 * Revision 1.18 2001/11/07 00:06:06 steudten
177 * Add line number in error output for lineparsing for
180 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
181 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
182 * threads to call select() simultaneously. So, it's time to do a real, live,
183 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
184 * (native). Both versions will work, but using __OS2__ offers multi-threading.
186 * Revision 1.16 2001/10/23 21:30:30 jongfoster
187 * Adding error-checking to selected functions.
189 * Revision 1.15 2001/10/14 21:58:22 jongfoster
190 * Adding support for the CGI-based editor:
191 * - Exported get_actions()
192 * - Added new function free_alias_list()
193 * - Added support for {{settings}} and {{description}} blocks
194 * in the actions file. They are currently ignored.
195 * - Added restriction to only one {{alias}} block which must appear
196 * first in the file, to simplify the editor's rewriting rules.
197 * - Note that load_actions_file() is no longer used by the CGI-based
198 * editor, but some of the other routines in this file are.
200 * Revision 1.14 2001/09/22 16:36:59 jongfoster
201 * Removing unused parameter fs from read_config_line()
203 * Revision 1.13 2001/09/16 15:47:37 jongfoster
204 * First version of CGI-based edit interface. This is very much a
205 * work-in-progress, and you can't actually use it to edit anything
206 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
207 * to have any effect.
209 * Revision 1.12 2001/09/16 13:21:27 jongfoster
210 * Changes to use new list functions.
212 * Revision 1.11 2001/09/14 00:17:32 jongfoster
213 * Tidying up memory allocation. New function init_action().
215 * Revision 1.10 2001/09/10 10:14:34 oes
216 * Removing unused variable
218 * Revision 1.9 2001/07/30 22:08:36 jongfoster
219 * Tidying up #defines:
220 * - All feature #defines are now of the form FEATURE_xxx
221 * - Permanently turned off WIN_GUI_EDIT
222 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
224 * Revision 1.8 2001/06/29 13:19:52 oes
225 * Removed logentry from cancelled commit
227 * Revision 1.7 2001/06/09 10:55:28 jongfoster
228 * Changing BUFSIZ ==> BUFFER_SIZE
230 * Revision 1.6 2001/06/07 23:04:34 jongfoster
231 * Made get_actions() static.
233 * Revision 1.5 2001/06/03 19:11:48 oes
234 * adapted to new enlist_unique arg format
236 * Revision 1.4 2001/06/01 20:03:42 jongfoster
237 * Better memory management - current_action->strings[] now
238 * contains copies of the strings, not the original.
240 * Revision 1.3 2001/06/01 18:49:17 jongfoster
241 * Replaced "list_share" with "list" - the tiny memory gain was not
242 * worth the extra complexity.
244 * Revision 1.2 2001/05/31 21:40:00 jongfoster
245 * Removing some commented out, obsolete blocks of code.
247 * Revision 1.1 2001/05/31 21:16:46 jongfoster
248 * Moved functions to process the action list into this new file.
251 *********************************************************************/
261 #ifdef FEATURE_PTHREAD
269 #include "miscutil.h"
273 #include "urlmatch.h"
277 const char actions_h_rcs[] = ACTIONS_H_VERSION;
281 * We need the main list of options.
283 * First, we need a way to tell between boolean, string, and multi-string
284 * options. For string and multistring options, we also need to be
285 * able to tell the difference between a "+" and a "-". (For bools,
286 * the "+"/"-" information is encoded in "add" and "mask"). So we use
287 * an enumerated type (well, the preprocessor equivalent). Here are
290 #define AV_NONE 0 /* +opt -opt */
291 #define AV_ADD_STRING 1 /* +stropt{string} */
292 #define AV_REM_STRING 2 /* -stropt */
293 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
294 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
297 * We need a structure to hold the name, flag changes,
298 * type, and string index.
303 unsigned long mask; /* a bit set to "0" = remove action */
304 unsigned long add; /* a bit set to "1" = add action */
305 int takes_value; /* an AV_... constant */
306 int index; /* index into strings[] or multi[] */
310 * And with those building blocks in place, here's the array.
312 static const struct action_name action_names[] =
315 * Well actually there's no data here - it's in actionlist.h
316 * This keeps it together to make it easy to change.
318 * Here's the macros used to format it:
320 #define DEFINE_ACTION_MULTI(name,index) \
321 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
322 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
323 #define DEFINE_ACTION_STRING(name,flag,index) \
324 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
325 { "-" name, ~flag, 0, AV_REM_STRING, index },
326 #define DEFINE_ACTION_BOOL(name,flag) \
327 { "+" name, ACTION_MASK_ALL, flag }, \
328 { "-" name, ~flag, 0 },
329 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
331 #include "actionlist.h"
333 #undef DEFINE_ACTION_MULTI
334 #undef DEFINE_ACTION_STRING
335 #undef DEFINE_ACTION_BOOL
336 #undef DEFINE_ACTION_ALIAS
338 { NULL, 0, 0 } /* End marker */
342 static int load_one_actions_file(struct client_state *csp, int fileid);
345 /*********************************************************************
347 * Function : merge_actions
349 * Description : Merge two actions together.
350 * Similar to "dest += src".
353 * 1 : dest = Actions to modify.
354 * 2 : src = Action to add.
356 * Returns : JB_ERR_OK or JB_ERR_MEMORY
358 *********************************************************************/
359 jb_err merge_actions (struct action_spec *dest,
360 const struct action_spec *src)
365 dest->mask &= src->mask;
366 dest->add &= src->mask;
367 dest->add |= src->add;
369 for (i = 0; i < ACTION_STRING_COUNT; i++)
371 char * str = src->string[i];
374 freez(dest->string[i]);
375 dest->string[i] = strdup(str);
376 if (NULL == dest->string[i])
378 return JB_ERR_MEMORY;
383 for (i = 0; i < ACTION_MULTI_COUNT; i++)
385 if (src->multi_remove_all[i])
387 /* Remove everything from dest */
388 list_remove_all(dest->multi_remove[i]);
389 dest->multi_remove_all[i] = 1;
391 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
393 else if (dest->multi_remove_all[i])
396 * dest already removes everything, so we only need to worry
399 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
400 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
404 /* No "remove all"s to worry about. */
405 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
406 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
407 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
420 /*********************************************************************
422 * Function : copy_action
424 * Description : Copy an action_specs.
425 * Similar to "dest = src".
428 * 1 : dest = Destination of copy.
429 * 2 : src = Source for copy.
433 *********************************************************************/
434 jb_err copy_action (struct action_spec *dest,
435 const struct action_spec *src)
438 jb_err err = JB_ERR_OK;
441 memset(dest, '\0', sizeof(*dest));
443 dest->mask = src->mask;
444 dest->add = src->add;
446 for (i = 0; i < ACTION_STRING_COUNT; i++)
448 char * str = src->string[i];
454 return JB_ERR_MEMORY;
456 dest->string[i] = str;
460 for (i = 0; i < ACTION_MULTI_COUNT; i++)
462 dest->multi_remove_all[i] = src->multi_remove_all[i];
463 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
468 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
477 /*********************************************************************
479 * Function : free_action_spec
481 * Description : Frees an action_spec and the memory used by it.
484 * 1 : src = Source to free.
488 *********************************************************************/
489 void free_action_spec(struct action_spec *src)
496 /*********************************************************************
498 * Function : free_action
500 * Description : Destroy an action_spec. Frees memory used by it,
501 * except for the memory used by the struct action_spec
505 * 1 : src = Source to free.
509 *********************************************************************/
510 void free_action (struct action_spec *src)
519 for (i = 0; i < ACTION_STRING_COUNT; i++)
521 freez(src->string[i]);
524 for (i = 0; i < ACTION_MULTI_COUNT; i++)
526 destroy_list(src->multi_remove[i]);
527 destroy_list(src->multi_add[i]);
530 memset(src, '\0', sizeof(*src));
534 /*********************************************************************
536 * Function : get_action_token
538 * Description : Parses a line for the first action.
539 * Modifies it's input array, doesn't allocate memory.
541 * *line=" +abc{def} -ghi "
548 * 1 : line = [in] The line containing the action.
549 * [out] Start of next action on line, or
550 * NULL if we reached the end of line before
551 * we found an action.
552 * 2 : name = [out] Start of action name, null
553 * terminated. NULL on EOL
554 * 3 : value = [out] Start of action value, null
555 * terminated. NULL if none or EOL.
557 * Returns : JB_ERR_OK => Ok
558 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
560 *********************************************************************/
561 jb_err get_action_token(char **line, char **name, char **value)
566 /* set default returns */
571 /* Eat any leading whitespace */
572 while ((*str == ' ') || (*str == '\t'))
584 /* null name, just value is prohibited */
591 while (((ch = *str) != '\0') &&
592 (ch != ' ') && (ch != '\t') && (ch != '{'))
596 /* error, '}' without '{' */
608 /* EOL - be careful not to run off buffer */
613 /* More to parse next time. */
622 str = strchr(str, '}');
639 /*********************************************************************
641 * Function : action_used_to_valid_
643 * Description : Checks if unrecognized actions were valid in earlier
647 * 1 : action = The string containing the action to check.
649 * Returns : True if yes, otherwise false.
651 *********************************************************************/
652 static int action_used_to_be_valid(const char *action)
654 return ((0 == strcmpic(action, "treat-forbidden-connects-like-blocks"))
655 || (0 == strcmpic(action, "kill-popups"))
656 || (0 == strcmpic(action, "inspect-jpegs")));
659 /*********************************************************************
661 * Function : get_actions
663 * Description : Parses a list of actions.
666 * 1 : line = The string containing the actions.
667 * Will be written to by this function.
668 * 2 : alias_list = Custom alias list, or NULL for none.
669 * 3 : cur_action = Where to store the action. Caller
672 * Returns : JB_ERR_OK => Ok
673 * JB_ERR_PARSE => Parse error (line was trashed anyway)
674 * nonzero => Out of memory (line was trashed anyway)
676 *********************************************************************/
677 jb_err get_actions(char *line,
678 struct action_alias * alias_list,
679 struct action_spec *cur_action)
682 init_action(cur_action);
683 cur_action->mask = ACTION_MASK_ALL;
687 char * option = NULL;
690 err = get_action_token(&line, &option, &value);
698 /* handle option in 'option' */
700 /* Check for standard action name */
701 const struct action_name * action = action_names;
703 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
707 if (action->name != NULL)
710 cur_action->mask &= action->mask;
711 cur_action->add &= action->mask;
712 cur_action->add |= action->add;
714 switch (action->takes_value)
717 /* ignore any option. */
721 /* add single string. */
723 if ((value == NULL) || (*value == '\0'))
725 if (0 != strcmpic(action->name, "block"))
728 * XXX: Temporary backwards compatibility hack.
730 static int complaint_shown = 0;
731 value = "No reason specified.";
732 if (!complaint_shown)
734 log_error(LOG_LEVEL_ERROR, "At least one block "
735 "without reason found. This may become a fatal "
736 "error in future versions.");
745 /* FIXME: should validate option string here */
746 freez (cur_action->string[action->index]);
747 cur_action->string[action->index] = strdup(value);
748 if (NULL == cur_action->string[action->index])
750 return JB_ERR_MEMORY;
756 /* remove single string. */
758 freez (cur_action->string[action->index]);
763 /* append multi string. */
765 struct list * remove_p = cur_action->multi_remove[action->index];
766 struct list * add_p = cur_action->multi_add[action->index];
768 if ((value == NULL) || (*value == '\0'))
773 list_remove_item(remove_p, value);
774 err = enlist_unique(add_p, value, 0);
783 /* remove multi string. */
785 struct list * remove_p = cur_action->multi_remove[action->index];
786 struct list * add_p = cur_action->multi_add[action->index];
788 if ( (value == NULL) || (*value == '\0')
789 || ((*value == '*') && (value[1] == '\0')) )
792 * no option, or option == "*".
796 list_remove_all(remove_p);
797 list_remove_all(add_p);
798 cur_action->multi_remove_all[action->index] = 1;
802 /* Valid option - remove only 1 option */
804 if ( !cur_action->multi_remove_all[action->index] )
806 /* there isn't a catch-all in the remove list already */
807 err = enlist_unique(remove_p, value, 0);
813 list_remove_item(add_p, value);
818 /* Shouldn't get here unless there's memory corruption. */
825 /* try user aliases. */
826 const struct action_alias * alias = alias_list;
828 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
835 merge_actions(cur_action, alias->action);
837 else if ((2 < strlen(option)) && action_used_to_be_valid(option+1))
839 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
840 "in this Privoxy release. Ignored.", option+1);
844 /* Bad action name */
846 * XXX: This is a fatal error and Privoxy will later on exit
847 * in load_one_actions_file() because of an "invalid line".
849 * It would be preferable to name the offending option in that
850 * error message, but currently there is no way to do that and
851 * we have to live with two error messages for basically the
854 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
865 /*********************************************************************
867 * Function : init_current_action
869 * Description : Zero out an action.
872 * 1 : dest = An uninitialized current_action_spec.
876 *********************************************************************/
877 void init_current_action (struct current_action_spec *dest)
879 memset(dest, '\0', sizeof(*dest));
881 dest->flags = ACTION_MOST_COMPATIBLE;
885 /*********************************************************************
887 * Function : init_action
889 * Description : Zero out an action.
892 * 1 : dest = An uninitialized action_spec.
896 *********************************************************************/
897 void init_action (struct action_spec *dest)
899 memset(dest, '\0', sizeof(*dest));
903 /*********************************************************************
905 * Function : merge_current_action
907 * Description : Merge two actions together.
908 * Similar to "dest += src".
909 * Differences between this and merge_actions()
910 * is that this one doesn't allocate memory for
911 * strings (so "src" better be in memory for at least
912 * as long as "dest" is, and you'd better free
913 * "dest" using "free_current_action").
914 * Also, there is no mask or remove lists in dest.
915 * (If we're applying it to a URL, we don't need them)
918 * 1 : dest = Current actions, to modify.
919 * 2 : src = Action to add.
921 * Returns 0 : no error
922 * !=0 : error, probably JB_ERR_MEMORY.
924 *********************************************************************/
925 jb_err merge_current_action (struct current_action_spec *dest,
926 const struct action_spec *src)
929 jb_err err = JB_ERR_OK;
931 dest->flags &= src->mask;
932 dest->flags |= src->add;
934 for (i = 0; i < ACTION_STRING_COUNT; i++)
936 char * str = src->string[i];
942 return JB_ERR_MEMORY;
944 freez(dest->string[i]);
945 dest->string[i] = str;
949 for (i = 0; i < ACTION_MULTI_COUNT; i++)
951 if (src->multi_remove_all[i])
953 /* Remove everything from dest, then add src->multi_add */
954 err = list_duplicate(dest->multi[i], src->multi_add[i]);
962 list_remove_list(dest->multi[i], src->multi_remove[i]);
963 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
974 /*********************************************************************
976 * Function : update_action_bits_for_all_tags
978 * Description : Updates the action bits based on all matching tags.
981 * 1 : csp = Current client state (buffers, headers, etc...)
983 * Returns : 0 if no tag matched, or
986 *********************************************************************/
987 int update_action_bits_for_all_tags(struct client_state *csp)
989 struct list_entry *tag;
992 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
994 if (update_action_bits_for_tag(csp, tag->str))
1004 /*********************************************************************
1006 * Function : update_action_bits_for_tag
1008 * Description : Updates the action bits based on the action sections
1009 * whose tag patterns match a provided tag.
1012 * 1 : csp = Current client state (buffers, headers, etc...)
1013 * 2 : tag = The tag on which the update should be based on
1015 * Returns : 0 if no tag matched, or
1018 *********************************************************************/
1019 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
1021 struct file_list *fl;
1022 struct url_actions *b;
1028 assert(list_contains_item(csp->tags, tag));
1030 /* Run through all action files, */
1031 for (i = 0; i < MAX_AF_FILES; i++)
1033 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
1035 /* Skip empty files */
1039 /* and through all the action patterns, */
1040 for (b = b->next; NULL != b; b = b->next)
1042 /* skip the URL patterns, */
1043 if (NULL == b->url->tag_regex)
1048 /* and check if one of the tag patterns matches the tag, */
1049 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
1051 /* if it does, update the action bit map, */
1052 if (merge_current_action(csp->action, b->action))
1054 log_error(LOG_LEVEL_ERROR,
1055 "Out of memorey while changing action bits");
1057 /* and signal the change. */
1067 /*********************************************************************
1069 * Function : free_current_action
1071 * Description : Free memory used by a current_action_spec.
1072 * Does not free the current_action_spec itself.
1075 * 1 : src = Source to free.
1079 *********************************************************************/
1080 void free_current_action (struct current_action_spec *src)
1084 for (i = 0; i < ACTION_STRING_COUNT; i++)
1086 freez(src->string[i]);
1089 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1091 destroy_list(src->multi[i]);
1094 memset(src, '\0', sizeof(*src));
1098 static struct file_list *current_actions_file[MAX_AF_FILES] = {
1099 NULL, NULL, NULL, NULL, NULL,
1100 NULL, NULL, NULL, NULL, NULL
1104 #ifdef FEATURE_GRACEFUL_TERMINATION
1105 /*********************************************************************
1107 * Function : unload_current_actions_file
1109 * Description : Unloads current actions file - reset to state at
1110 * beginning of program.
1116 *********************************************************************/
1117 void unload_current_actions_file(void)
1121 for (i = 0; i < MAX_AF_FILES; i++)
1123 if (current_actions_file[i])
1125 current_actions_file[i]->unloader = unload_actions_file;
1126 current_actions_file[i] = NULL;
1130 #endif /* FEATURE_GRACEFUL_TERMINATION */
1133 /*********************************************************************
1135 * Function : unload_actions_file
1137 * Description : Unloads an actions module.
1140 * 1 : file_data = the data structure associated with the
1145 *********************************************************************/
1146 void unload_actions_file(void *file_data)
1148 struct url_actions * next;
1149 struct url_actions * cur = (struct url_actions *)file_data;
1153 free_url_spec(cur->url);
1154 if ((next == NULL) || (next->action != cur->action))
1157 * As the action settings might be shared,
1158 * we can only free them if the current
1159 * url pattern is the last one, or if the
1160 * next one is using different settings.
1162 free_action_spec(cur->action);
1170 /*********************************************************************
1172 * Function : free_alias_list
1174 * Description : Free memory used by a list of aliases.
1177 * 1 : alias_list = Linked list to free.
1181 *********************************************************************/
1182 void free_alias_list(struct action_alias *alias_list)
1184 while (alias_list != NULL)
1186 struct action_alias * next = alias_list->next;
1187 alias_list->next = NULL;
1188 freez(alias_list->name);
1189 free_action(alias_list->action);
1196 /*********************************************************************
1198 * Function : load_actions_file
1200 * Description : Read and parse all the action files and add to files
1204 * 1 : csp = Current client state (buffers, headers, etc...)
1206 * Returns : 0 => Ok, everything else is an error.
1208 *********************************************************************/
1209 int load_actions_file(struct client_state *csp)
1214 for (i = 0; i < MAX_AF_FILES; i++)
1216 if (csp->config->actions_file[i])
1218 result = load_one_actions_file(csp, i);
1224 else if (current_actions_file[i])
1226 current_actions_file[i]->unloader = unload_actions_file;
1227 current_actions_file[i] = NULL;
1234 /*********************************************************************
1236 * Function : load_one_actions_file
1238 * Description : Read and parse a action file and add to files
1242 * 1 : csp = Current client state (buffers, headers, etc...)
1243 * 2 : fileid = File index to load.
1245 * Returns : 0 => Ok, everything else is an error.
1247 *********************************************************************/
1248 static int load_one_actions_file(struct client_state *csp, int fileid)
1253 * Note: Keep these in the order they occur in the file, they are
1254 * sometimes tested with <=
1256 #define MODE_START_OF_FILE 1
1257 #define MODE_SETTINGS 2
1258 #define MODE_DESCRIPTION 3
1259 #define MODE_ALIAS 4
1260 #define MODE_ACTIONS 5
1262 int mode = MODE_START_OF_FILE;
1265 struct url_actions *last_perm;
1266 struct url_actions *perm;
1267 char buf[BUFFER_SIZE];
1268 struct file_list *fs;
1269 struct action_spec * cur_action = NULL;
1270 int cur_action_used = 0;
1271 struct action_alias * alias_list = NULL;
1272 unsigned long linenum = 0;
1274 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1276 /* No need to load */
1277 csp->actions_list[fileid] = current_actions_file[fileid];
1282 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1283 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1284 "with their complete file names.", csp->config->actions_file[fileid]);
1285 return 1; /* never get here */
1288 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1289 if (last_perm == NULL)
1291 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1292 csp->config->actions_file[fileid]);
1293 return 1; /* never get here */
1296 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1298 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1299 csp->config->actions_file[fileid]);
1300 return 1; /* never get here */
1303 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1307 /* It's a header block */
1310 /* It's {{settings}} or {{alias}} */
1311 size_t len = strlen(buf);
1312 char * start = buf + 2;
1313 char * end = buf + len - 1;
1314 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1318 log_error(LOG_LEVEL_FATAL,
1319 "can't load actions file '%s': invalid line (%lu): %s",
1320 csp->config->actions_file[fileid], linenum, buf);
1321 return 1; /* never get here */
1324 /* Trim leading and trailing whitespace. */
1332 log_error(LOG_LEVEL_FATAL,
1333 "can't load actions file '%s': invalid line (%lu): {{ }}",
1334 csp->config->actions_file[fileid], linenum);
1335 return 1; /* never get here */
1339 * An actionsfile can optionally contain the following blocks.
1340 * They *MUST* be in this order, to simplify processing:
1346 * ...free text, format TBD, but no line may start with a '{'...
1351 * The actual actions must be *after* these special blocks.
1352 * None of these special blocks may be repeated.
1355 if (0 == strcmpic(start, "settings"))
1357 /* it's a {{settings}} block */
1358 if (mode >= MODE_SETTINGS)
1360 /* {{settings}} must be first thing in file and must only
1364 log_error(LOG_LEVEL_FATAL,
1365 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1366 csp->config->actions_file[fileid], linenum);
1368 mode = MODE_SETTINGS;
1370 else if (0 == strcmpic(start, "description"))
1372 /* it's a {{description}} block */
1373 if (mode >= MODE_DESCRIPTION)
1375 /* {{description}} is a singleton and only {{settings}} may proceed it
1378 log_error(LOG_LEVEL_FATAL,
1379 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1380 csp->config->actions_file[fileid], linenum);
1382 mode = MODE_DESCRIPTION;
1384 else if (0 == strcmpic(start, "alias"))
1386 /* it's an {{alias}} block */
1387 if (mode >= MODE_ALIAS)
1389 /* {{alias}} must be first thing in file, possibly after
1390 * {{settings}} and {{description}}
1392 * {{alias}} must only appear once.
1394 * Note that these are new restrictions introduced in
1395 * v2.9.10 in order to make actionsfile editing simpler.
1396 * (Otherwise, reordering actionsfile entries without
1397 * completely rewriting the file becomes non-trivial)
1400 log_error(LOG_LEVEL_FATAL,
1401 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1402 csp->config->actions_file[fileid], linenum);
1408 /* invalid {{something}} block */
1410 log_error(LOG_LEVEL_FATAL,
1411 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1412 csp->config->actions_file[fileid], linenum, start);
1413 return 1; /* never get here */
1418 /* It's an actions block */
1420 char actions_buf[BUFFER_SIZE];
1424 mode = MODE_ACTIONS;
1426 /* free old action */
1429 if (!cur_action_used)
1431 free_action_spec(cur_action);
1435 cur_action_used = 0;
1436 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1437 if (cur_action == NULL)
1440 log_error(LOG_LEVEL_FATAL,
1441 "can't load actions file '%s': out of memory",
1442 csp->config->actions_file[fileid]);
1443 return 1; /* never get here */
1445 init_action(cur_action);
1448 strlcpy(actions_buf, buf + 1, sizeof(actions_buf));
1450 /* check we have a trailing } and then trim it */
1451 end = actions_buf + strlen(actions_buf) - 1;
1456 log_error(LOG_LEVEL_FATAL,
1457 "can't load actions file '%s': invalid line (%lu): %s",
1458 csp->config->actions_file[fileid], linenum, buf);
1459 return 1; /* never get here */
1463 /* trim any whitespace immediately inside {} */
1466 if (get_actions(actions_buf, alias_list, cur_action))
1470 log_error(LOG_LEVEL_FATAL,
1471 "can't load actions file '%s': invalid line (%lu): %s",
1472 csp->config->actions_file[fileid], linenum, buf);
1473 return 1; /* never get here */
1477 else if (mode == MODE_SETTINGS)
1480 * Part of the {{settings}} block.
1481 * For now only serves to check if the file's minimum Privoxy
1482 * version requirement is met, but we may want to read & check
1483 * permissions when we go multi-user.
1485 if (!strncmp(buf, "for-privoxy-version=", 20))
1487 char *version_string, *fields[3];
1490 if ((version_string = strdup(buf + 20)) == NULL)
1493 log_error(LOG_LEVEL_FATAL,
1494 "can't load actions file '%s': out of memory!",
1495 csp->config->actions_file[fileid]);
1496 return 1; /* never get here */
1499 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1501 if (num_fields < 1 || atoi(fields[0]) == 0)
1503 log_error(LOG_LEVEL_ERROR,
1504 "While loading actions file '%s': invalid line (%lu): %s",
1505 csp->config->actions_file[fileid], linenum, buf);
1507 else if ( atoi(fields[0]) > VERSION_MAJOR
1508 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1509 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1512 log_error(LOG_LEVEL_FATAL,
1513 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1514 csp->config->actions_file[fileid], linenum, buf );
1515 return 1; /* never get here */
1517 free(version_string);
1520 else if (mode == MODE_DESCRIPTION)
1523 * Part of the {{description}} block.
1527 else if (mode == MODE_ALIAS)
1532 char actions_buf[BUFFER_SIZE];
1533 struct action_alias * new_alias;
1535 char * start = strchr(buf, '=');
1538 if ((start == NULL) || (start == buf))
1540 log_error(LOG_LEVEL_FATAL,
1541 "can't load actions file '%s': invalid alias line (%lu): %s",
1542 csp->config->actions_file[fileid], linenum, buf);
1543 return 1; /* never get here */
1546 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1549 log_error(LOG_LEVEL_FATAL,
1550 "can't load actions file '%s': out of memory!",
1551 csp->config->actions_file[fileid]);
1552 return 1; /* never get here */
1555 /* Eat any the whitespace before the '=' */
1557 while ((*end == ' ') || (*end == '\t'))
1560 * we already know we must have at least 1 non-ws char
1561 * at start of buf - no need to check
1567 /* Eat any the whitespace after the '=' */
1569 while ((*start == ' ') || (*start == '\t'))
1575 log_error(LOG_LEVEL_FATAL,
1576 "can't load actions file '%s': invalid alias line (%lu): %s",
1577 csp->config->actions_file[fileid], linenum, buf);
1578 return 1; /* never get here */
1581 if ((new_alias->name = strdup(buf)) == NULL)
1584 log_error(LOG_LEVEL_FATAL,
1585 "can't load actions file '%s': out of memory!",
1586 csp->config->actions_file[fileid]);
1587 return 1; /* never get here */
1590 strlcpy(actions_buf, start, sizeof(actions_buf));
1592 if (get_actions(actions_buf, alias_list, new_alias->action))
1596 log_error(LOG_LEVEL_FATAL,
1597 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1598 csp->config->actions_file[fileid], linenum, buf, start);
1599 return 1; /* never get here */
1603 new_alias->next = alias_list;
1604 alias_list = new_alias;
1606 else if (mode == MODE_ACTIONS)
1608 /* it's a URL pattern */
1610 /* allocate a new node */
1611 if ((perm = zalloc(sizeof(*perm))) == NULL)
1614 log_error(LOG_LEVEL_FATAL,
1615 "can't load actions file '%s': out of memory!",
1616 csp->config->actions_file[fileid]);
1617 return 1; /* never get here */
1620 perm->action = cur_action;
1621 cur_action_used = 1;
1623 /* Save the URL pattern */
1624 if (create_url_spec(perm->url, buf))
1627 log_error(LOG_LEVEL_FATAL,
1628 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1629 csp->config->actions_file[fileid], linenum, buf);
1630 return 1; /* never get here */
1633 /* add it to the list */
1634 last_perm->next = perm;
1637 else if (mode == MODE_START_OF_FILE)
1639 /* oops - please have a {} line as 1st line in file. */
1641 log_error(LOG_LEVEL_FATAL,
1642 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1643 csp->config->actions_file[fileid], linenum, buf);
1644 return 1; /* never get here */
1648 /* How did we get here? This is impossible! */
1650 log_error(LOG_LEVEL_FATAL,
1651 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1652 csp->config->actions_file[fileid], mode);
1653 return 1; /* never get here */
1659 if (!cur_action_used)
1661 free_action_spec(cur_action);
1663 free_alias_list(alias_list);
1665 /* the old one is now obsolete */
1666 if (current_actions_file[fileid])
1668 current_actions_file[fileid]->unloader = unload_actions_file;
1671 fs->next = files->next;
1673 current_actions_file[fileid] = fs;
1675 csp->actions_list[fileid] = fs;
1682 /*********************************************************************
1684 * Function : actions_to_text
1686 * Description : Converts a actionsfile entry from the internal
1687 * structure into a text line. The output is split
1688 * into one line for each action with line continuation.
1691 * 1 : action = The action to format.
1693 * Returns : A string. Caller must free it.
1694 * NULL on out-of-memory error.
1696 *********************************************************************/
1697 char * actions_to_text(const struct action_spec *action)
1699 unsigned mask = action->mask;
1700 unsigned add = action->add;
1701 char * result = strdup("");
1702 struct list_entry * lst;
1704 /* sanity - prevents "-feature +feature" */
1708 #define DEFINE_ACTION_BOOL(__name, __bit) \
1709 if (!(mask & __bit)) \
1711 string_append(&result, " -" __name " \\\n"); \
1713 else if (add & __bit) \
1715 string_append(&result, " +" __name " \\\n"); \
1718 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1719 if (!(mask & __bit)) \
1721 string_append(&result, " -" __name " \\\n"); \
1723 else if (add & __bit) \
1725 string_append(&result, " +" __name "{"); \
1726 string_append(&result, action->string[__index]); \
1727 string_append(&result, "} \\\n"); \
1730 #define DEFINE_ACTION_MULTI(__name, __index) \
1731 if (action->multi_remove_all[__index]) \
1733 string_append(&result, " -" __name " \\\n"); \
1737 lst = action->multi_remove[__index]->first; \
1740 string_append(&result, " -" __name "{"); \
1741 string_append(&result, lst->str); \
1742 string_append(&result, "} \\\n"); \
1746 lst = action->multi_add[__index]->first; \
1749 string_append(&result, " +" __name "{"); \
1750 string_append(&result, lst->str); \
1751 string_append(&result, "} \\\n"); \
1755 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1757 #include "actionlist.h"
1759 #undef DEFINE_ACTION_MULTI
1760 #undef DEFINE_ACTION_STRING
1761 #undef DEFINE_ACTION_BOOL
1762 #undef DEFINE_ACTION_ALIAS
1768 /*********************************************************************
1770 * Function : actions_to_html
1772 * Description : Converts a actionsfile entry from numeric form
1773 * ("mask" and "add") to a <br>-seperated HTML string
1774 * in which each action is linked to its chapter in
1778 * 1 : csp = Client state (for config)
1779 * 2 : action = Action spec to be converted
1781 * Returns : A string. Caller must free it.
1782 * NULL on out-of-memory error.
1784 *********************************************************************/
1785 char * actions_to_html(const struct client_state *csp,
1786 const struct action_spec *action)
1788 unsigned mask = action->mask;
1789 unsigned add = action->add;
1790 char * result = strdup("");
1791 struct list_entry * lst;
1793 /* sanity - prevents "-feature +feature" */
1797 #define DEFINE_ACTION_BOOL(__name, __bit) \
1798 if (!(mask & __bit)) \
1800 string_append(&result, "\n<br>-"); \
1801 string_join(&result, add_help_link(__name, csp->config)); \
1803 else if (add & __bit) \
1805 string_append(&result, "\n<br>+"); \
1806 string_join(&result, add_help_link(__name, csp->config)); \
1809 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1810 if (!(mask & __bit)) \
1812 string_append(&result, "\n<br>-"); \
1813 string_join(&result, add_help_link(__name, csp->config)); \
1815 else if (add & __bit) \
1817 string_append(&result, "\n<br>+"); \
1818 string_join(&result, add_help_link(__name, csp->config)); \
1819 string_append(&result, "{"); \
1820 string_join(&result, html_encode(action->string[__index])); \
1821 string_append(&result, "}"); \
1824 #define DEFINE_ACTION_MULTI(__name, __index) \
1825 if (action->multi_remove_all[__index]) \
1827 string_append(&result, "\n<br>-"); \
1828 string_join(&result, add_help_link(__name, csp->config)); \
1832 lst = action->multi_remove[__index]->first; \
1835 string_append(&result, "\n<br>-"); \
1836 string_join(&result, add_help_link(__name, csp->config)); \
1837 string_append(&result, "{"); \
1838 string_join(&result, html_encode(lst->str)); \
1839 string_append(&result, "}"); \
1843 lst = action->multi_add[__index]->first; \
1846 string_append(&result, "\n<br>+"); \
1847 string_join(&result, add_help_link(__name, csp->config)); \
1848 string_append(&result, "{"); \
1849 string_join(&result, html_encode(lst->str)); \
1850 string_append(&result, "}"); \
1854 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1856 #include "actionlist.h"
1858 #undef DEFINE_ACTION_MULTI
1859 #undef DEFINE_ACTION_STRING
1860 #undef DEFINE_ACTION_BOOL
1861 #undef DEFINE_ACTION_ALIAS
1863 /* trim leading <br> */
1864 if (result && *result)
1867 result = strdup(result + 5);
1875 /*********************************************************************
1877 * Function : current_actions_to_html
1879 * Description : Converts a curren action spec to a <br> seperated HTML
1880 * text in which each action is linked to its chapter in
1884 * 1 : csp = Client state (for config)
1885 * 2 : action = Current action spec to be converted
1887 * Returns : A string. Caller must free it.
1888 * NULL on out-of-memory error.
1890 *********************************************************************/
1891 char *current_action_to_html(const struct client_state *csp,
1892 const struct current_action_spec *action)
1894 unsigned long flags = action->flags;
1895 struct list_entry * lst;
1896 char *result = strdup("");
1897 char *active = strdup("");
1898 char *inactive = strdup("");
1900 #define DEFINE_ACTION_BOOL(__name, __bit) \
1901 if (flags & __bit) \
1903 string_append(&active, "\n<br>+"); \
1904 string_join(&active, add_help_link(__name, csp->config)); \
1908 string_append(&inactive, "\n<br>-"); \
1909 string_join(&inactive, add_help_link(__name, csp->config)); \
1912 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1913 if (flags & __bit) \
1915 string_append(&active, "\n<br>+"); \
1916 string_join(&active, add_help_link(__name, csp->config)); \
1917 string_append(&active, "{"); \
1918 string_join(&active, html_encode(action->string[__index])); \
1919 string_append(&active, "}"); \
1923 string_append(&inactive, "\n<br>-"); \
1924 string_join(&inactive, add_help_link(__name, csp->config)); \
1927 #define DEFINE_ACTION_MULTI(__name, __index) \
1928 lst = action->multi[__index]->first; \
1931 string_append(&inactive, "\n<br>-"); \
1932 string_join(&inactive, add_help_link(__name, csp->config)); \
1938 string_append(&active, "\n<br>+"); \
1939 string_join(&active, add_help_link(__name, csp->config)); \
1940 string_append(&active, "{"); \
1941 string_join(&active, html_encode(lst->str)); \
1942 string_append(&active, "}"); \
1947 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1949 #include "actionlist.h"
1951 #undef DEFINE_ACTION_MULTI
1952 #undef DEFINE_ACTION_STRING
1953 #undef DEFINE_ACTION_BOOL
1954 #undef DEFINE_ACTION_ALIAS
1958 string_append(&result, active);
1961 string_append(&result, "\n<br>");
1962 if (inactive != NULL)
1964 string_append(&result, inactive);