1 const char actions_rcs[] = "$Id: actions.c,v 1.52 2008/04/27 16:26:59 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.52 2008/04/27 16:26:59 fabiankeil
37 * White space fix for the last commit.
39 * Revision 1.51 2008/04/27 16:20:19 fabiankeil
40 * Complain about every block action without reason found.
42 * Revision 1.50 2008/03/30 14:52:00 fabiankeil
43 * Rename load_actions_file() and load_re_filterfile()
44 * as they load multiple files "now".
46 * Revision 1.49 2008/03/29 12:13:45 fabiankeil
47 * Remove send-wafer and send-vanilla-wafer actions.
49 * Revision 1.48 2008/03/28 18:17:14 fabiankeil
50 * In action_used_to_be_valid(), loop through an array of formerly
51 * valid actions instead of using an OR-chain of strcmpic() calls.
53 * Revision 1.47 2008/03/28 15:13:37 fabiankeil
54 * Remove inspect-jpegs action.
56 * Revision 1.46 2008/03/27 18:27:20 fabiankeil
57 * Remove kill-popups action.
59 * Revision 1.45 2008/03/24 11:21:02 fabiankeil
60 * Share the action settings for multiple patterns in the same
61 * section so we waste less memory for gigantic block lists
62 * (and load them slightly faster). Reported by Franz Schwartau.
64 * Revision 1.44 2008/03/04 18:30:34 fabiankeil
65 * Remove the treat-forbidden-connects-like-blocks action. We now
66 * use the "blocked" page for forbidden CONNECT requests by default.
68 * Revision 1.43 2008/03/01 14:00:43 fabiankeil
69 * Let the block action take the reason for the block
70 * as argument and show it on the "blocked" page.
72 * Revision 1.42 2008/02/09 15:15:38 fabiankeil
73 * List active and inactive actions in the show-url-info's
74 * "Final results" section separately. Patch submitted by Lee
75 * in #1830056, modified to list active actions first.
77 * Revision 1.41 2008/01/28 20:17:40 fabiankeil
78 * - Mark some parameters as immutable.
79 * - Hide update_action_bits_for_all_tags() while it's unused.
81 * Revision 1.40 2007/05/21 10:26:50 fabiankeil
82 * - Use strlcpy() instead of strcpy().
83 * - Provide a reason why loading the actions
84 * file might have failed.
86 * Revision 1.39 2007/04/17 18:21:45 fabiankeil
87 * Split update_action_bits() into
88 * update_action_bits_for_all_tags()
89 * and update_action_bits_for_tag().
91 * Revision 1.38 2007/04/15 16:39:20 fabiankeil
92 * Introduce tags as alternative way to specify which
93 * actions apply to a request. At the moment tags can be
94 * created based on client and server headers.
96 * Revision 1.37 2007/03/11 15:56:12 fabiankeil
97 * Add kludge to log unknown aliases and actions before exiting.
99 * Revision 1.36 2006/12/28 17:15:42 fabiankeil
100 * Fix gcc43 conversion warning.
102 * Revision 1.35 2006/07/18 14:48:45 david__schmidt
103 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
104 * with what was really the latest development (the v_3_0_branch branch)
106 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
107 * Multiple filter file support
109 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
110 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
111 * this becomes important when jcc.h gets included later down the line.
113 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
114 * - Implemented Privoxy version requirement through
115 * for-privoxy-version= statement in {{settings}}
117 * - Fix for unchecked out-of-memory condition
119 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
120 * Fixed memory leak reported by Dan Price in Bug #694713
122 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
123 * Extended unload_current_actions_file() to multiple AFs.
124 * Thanks to Oliver Stoeneberg for the hint.
126 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
127 * Change unsigned to unsigned long in actions_name struct. This closes
128 * SourceForge Bug #539284.
130 * Revision 1.32 2002/05/12 21:36:29 jongfoster
131 * Correcting function comments
133 * Revision 1.31 2002/05/06 07:56:50 oes
134 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
136 * Revision 1.30 2002/04/30 11:14:52 oes
137 * Made csp the first parameter in *action_to_html
139 * Revision 1.29 2002/04/26 19:30:54 jongfoster
140 * - current_action_to_html(): Adding help link for the "-" form of
141 * one-string actions.
142 * - Some actions had "<br>-", some "<br> -" (note the space).
143 * Standardizing on no space.
144 * - Greatly simplifying some of the code by using string_join()
147 * Revision 1.28 2002/04/26 12:53:15 oes
148 * - CGI AF editor now writes action lines split into
149 * single lines with line continuation
150 * - actions_to_html now embeds each action name in
152 * - current_action_to_text is now called current_action_to_html
153 * and acts like actions_to_html
155 * Revision 1.27 2002/04/24 02:10:31 oes
156 * - Jon's patch for multiple AFs:
157 * - split load_actions_file, add load_one_actions_file
158 * - make csp->actions_list files an array
159 * - remember file id with each action
160 * - Copy_action now frees dest action before copying
162 * Revision 1.26 2002/03/26 22:29:54 swa
163 * we have a new homepage!
165 * Revision 1.25 2002/03/24 13:25:43 swa
166 * name change related issues
168 * Revision 1.24 2002/03/16 23:54:06 jongfoster
169 * Adding graceful termination feature, to help look for memory leaks.
170 * If you enable this (which, by design, has to be done by hand
171 * editing config.h) and then go to http://i.j.b/die, then the program
172 * will exit cleanly after the *next* request. It should free all the
173 * memory that was used.
175 * Revision 1.23 2002/03/07 03:46:16 oes
176 * Fixed compiler warnings
178 * Revision 1.22 2002/01/21 00:27:02 jongfoster
179 * Allowing free_action(NULL).
180 * Moving the functions that #include actionlist.h to the end of the file,
181 * because the Visual C++ 97 debugger gets extremely confused if you try
182 * to debug any code that comes after them in the file.
184 * Revision 1.21 2002/01/17 20:54:44 jongfoster
185 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
187 * Revision 1.20 2001/11/22 21:56:49 jongfoster
188 * Making action_spec->flags into an unsigned long rather than just an
190 * Fixing a bug in the display of -add-header and -wafer
192 * Revision 1.19 2001/11/13 00:14:07 jongfoster
193 * Fixing stupid bug now I've figured out what || means.
194 * (It always returns 0 or 1, not one of it's paramaters.)
196 * Revision 1.18 2001/11/07 00:06:06 steudten
197 * Add line number in error output for lineparsing for
200 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
201 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
202 * threads to call select() simultaneously. So, it's time to do a real, live,
203 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
204 * (native). Both versions will work, but using __OS2__ offers multi-threading.
206 * Revision 1.16 2001/10/23 21:30:30 jongfoster
207 * Adding error-checking to selected functions.
209 * Revision 1.15 2001/10/14 21:58:22 jongfoster
210 * Adding support for the CGI-based editor:
211 * - Exported get_actions()
212 * - Added new function free_alias_list()
213 * - Added support for {{settings}} and {{description}} blocks
214 * in the actions file. They are currently ignored.
215 * - Added restriction to only one {{alias}} block which must appear
216 * first in the file, to simplify the editor's rewriting rules.
217 * - Note that load_actions_file() is no longer used by the CGI-based
218 * editor, but some of the other routines in this file are.
220 * Revision 1.14 2001/09/22 16:36:59 jongfoster
221 * Removing unused parameter fs from read_config_line()
223 * Revision 1.13 2001/09/16 15:47:37 jongfoster
224 * First version of CGI-based edit interface. This is very much a
225 * work-in-progress, and you can't actually use it to edit anything
226 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
227 * to have any effect.
229 * Revision 1.12 2001/09/16 13:21:27 jongfoster
230 * Changes to use new list functions.
232 * Revision 1.11 2001/09/14 00:17:32 jongfoster
233 * Tidying up memory allocation. New function init_action().
235 * Revision 1.10 2001/09/10 10:14:34 oes
236 * Removing unused variable
238 * Revision 1.9 2001/07/30 22:08:36 jongfoster
239 * Tidying up #defines:
240 * - All feature #defines are now of the form FEATURE_xxx
241 * - Permanently turned off WIN_GUI_EDIT
242 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
244 * Revision 1.8 2001/06/29 13:19:52 oes
245 * Removed logentry from cancelled commit
247 * Revision 1.7 2001/06/09 10:55:28 jongfoster
248 * Changing BUFSIZ ==> BUFFER_SIZE
250 * Revision 1.6 2001/06/07 23:04:34 jongfoster
251 * Made get_actions() static.
253 * Revision 1.5 2001/06/03 19:11:48 oes
254 * adapted to new enlist_unique arg format
256 * Revision 1.4 2001/06/01 20:03:42 jongfoster
257 * Better memory management - current_action->strings[] now
258 * contains copies of the strings, not the original.
260 * Revision 1.3 2001/06/01 18:49:17 jongfoster
261 * Replaced "list_share" with "list" - the tiny memory gain was not
262 * worth the extra complexity.
264 * Revision 1.2 2001/05/31 21:40:00 jongfoster
265 * Removing some commented out, obsolete blocks of code.
267 * Revision 1.1 2001/05/31 21:16:46 jongfoster
268 * Moved functions to process the action list into this new file.
271 *********************************************************************/
281 #ifdef FEATURE_PTHREAD
289 #include "miscutil.h"
293 #include "urlmatch.h"
297 const char actions_h_rcs[] = ACTIONS_H_VERSION;
301 * We need the main list of options.
303 * First, we need a way to tell between boolean, string, and multi-string
304 * options. For string and multistring options, we also need to be
305 * able to tell the difference between a "+" and a "-". (For bools,
306 * the "+"/"-" information is encoded in "add" and "mask"). So we use
307 * an enumerated type (well, the preprocessor equivalent). Here are
310 #define AV_NONE 0 /* +opt -opt */
311 #define AV_ADD_STRING 1 /* +stropt{string} */
312 #define AV_REM_STRING 2 /* -stropt */
313 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
314 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
317 * We need a structure to hold the name, flag changes,
318 * type, and string index.
323 unsigned long mask; /* a bit set to "0" = remove action */
324 unsigned long add; /* a bit set to "1" = add action */
325 int takes_value; /* an AV_... constant */
326 int index; /* index into strings[] or multi[] */
330 * And with those building blocks in place, here's the array.
332 static const struct action_name action_names[] =
335 * Well actually there's no data here - it's in actionlist.h
336 * This keeps it together to make it easy to change.
338 * Here's the macros used to format it:
340 #define DEFINE_ACTION_MULTI(name,index) \
341 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
342 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
343 #define DEFINE_ACTION_STRING(name,flag,index) \
344 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
345 { "-" name, ~flag, 0, AV_REM_STRING, index },
346 #define DEFINE_ACTION_BOOL(name,flag) \
347 { "+" name, ACTION_MASK_ALL, flag }, \
348 { "-" name, ~flag, 0 },
349 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
351 #include "actionlist.h"
353 #undef DEFINE_ACTION_MULTI
354 #undef DEFINE_ACTION_STRING
355 #undef DEFINE_ACTION_BOOL
356 #undef DEFINE_ACTION_ALIAS
358 { NULL, 0, 0 } /* End marker */
362 static int load_one_actions_file(struct client_state *csp, int fileid);
365 /*********************************************************************
367 * Function : merge_actions
369 * Description : Merge two actions together.
370 * Similar to "dest += src".
373 * 1 : dest = Actions to modify.
374 * 2 : src = Action to add.
376 * Returns : JB_ERR_OK or JB_ERR_MEMORY
378 *********************************************************************/
379 jb_err merge_actions (struct action_spec *dest,
380 const struct action_spec *src)
385 dest->mask &= src->mask;
386 dest->add &= src->mask;
387 dest->add |= src->add;
389 for (i = 0; i < ACTION_STRING_COUNT; i++)
391 char * str = src->string[i];
394 freez(dest->string[i]);
395 dest->string[i] = strdup(str);
396 if (NULL == dest->string[i])
398 return JB_ERR_MEMORY;
403 for (i = 0; i < ACTION_MULTI_COUNT; i++)
405 if (src->multi_remove_all[i])
407 /* Remove everything from dest */
408 list_remove_all(dest->multi_remove[i]);
409 dest->multi_remove_all[i] = 1;
411 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
413 else if (dest->multi_remove_all[i])
416 * dest already removes everything, so we only need to worry
419 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
420 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
424 /* No "remove all"s to worry about. */
425 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
426 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
427 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
440 /*********************************************************************
442 * Function : copy_action
444 * Description : Copy an action_specs.
445 * Similar to "dest = src".
448 * 1 : dest = Destination of copy.
449 * 2 : src = Source for copy.
453 *********************************************************************/
454 jb_err copy_action (struct action_spec *dest,
455 const struct action_spec *src)
458 jb_err err = JB_ERR_OK;
461 memset(dest, '\0', sizeof(*dest));
463 dest->mask = src->mask;
464 dest->add = src->add;
466 for (i = 0; i < ACTION_STRING_COUNT; i++)
468 char * str = src->string[i];
474 return JB_ERR_MEMORY;
476 dest->string[i] = str;
480 for (i = 0; i < ACTION_MULTI_COUNT; i++)
482 dest->multi_remove_all[i] = src->multi_remove_all[i];
483 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
488 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
497 /*********************************************************************
499 * Function : free_action_spec
501 * Description : Frees an action_spec and the memory used by it.
504 * 1 : src = Source to free.
508 *********************************************************************/
509 void free_action_spec(struct action_spec *src)
516 /*********************************************************************
518 * Function : free_action
520 * Description : Destroy an action_spec. Frees memory used by it,
521 * except for the memory used by the struct action_spec
525 * 1 : src = Source to free.
529 *********************************************************************/
530 void free_action (struct action_spec *src)
539 for (i = 0; i < ACTION_STRING_COUNT; i++)
541 freez(src->string[i]);
544 for (i = 0; i < ACTION_MULTI_COUNT; i++)
546 destroy_list(src->multi_remove[i]);
547 destroy_list(src->multi_add[i]);
550 memset(src, '\0', sizeof(*src));
554 /*********************************************************************
556 * Function : get_action_token
558 * Description : Parses a line for the first action.
559 * Modifies it's input array, doesn't allocate memory.
561 * *line=" +abc{def} -ghi "
568 * 1 : line = [in] The line containing the action.
569 * [out] Start of next action on line, or
570 * NULL if we reached the end of line before
571 * we found an action.
572 * 2 : name = [out] Start of action name, null
573 * terminated. NULL on EOL
574 * 3 : value = [out] Start of action value, null
575 * terminated. NULL if none or EOL.
577 * Returns : JB_ERR_OK => Ok
578 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
580 *********************************************************************/
581 jb_err get_action_token(char **line, char **name, char **value)
586 /* set default returns */
591 /* Eat any leading whitespace */
592 while ((*str == ' ') || (*str == '\t'))
604 /* null name, just value is prohibited */
611 while (((ch = *str) != '\0') &&
612 (ch != ' ') && (ch != '\t') && (ch != '{'))
616 /* error, '}' without '{' */
628 /* EOL - be careful not to run off buffer */
633 /* More to parse next time. */
642 str = strchr(str, '}');
659 /*********************************************************************
661 * Function : action_used_to_be_valid
663 * Description : Checks if unrecognized actions were valid in earlier
667 * 1 : action = The string containing the action to check.
669 * Returns : True if yes, otherwise false.
671 *********************************************************************/
672 static int action_used_to_be_valid(const char *action)
674 static const char *formerly_valid_actions[] = {
677 "send-vanilla-wafer",
679 "treat-forbidden-connects-like-blocks",
685 for (i = 0; i < SZ(formerly_valid_actions); i++)
687 if (0 == strcmpic(action, formerly_valid_actions[i]))
696 /*********************************************************************
698 * Function : get_actions
700 * Description : Parses a list of actions.
703 * 1 : line = The string containing the actions.
704 * Will be written to by this function.
705 * 2 : alias_list = Custom alias list, or NULL for none.
706 * 3 : cur_action = Where to store the action. Caller
709 * Returns : JB_ERR_OK => Ok
710 * JB_ERR_PARSE => Parse error (line was trashed anyway)
711 * nonzero => Out of memory (line was trashed anyway)
713 *********************************************************************/
714 jb_err get_actions(char *line,
715 struct action_alias * alias_list,
716 struct action_spec *cur_action)
719 init_action(cur_action);
720 cur_action->mask = ACTION_MASK_ALL;
724 char * option = NULL;
727 err = get_action_token(&line, &option, &value);
735 /* handle option in 'option' */
737 /* Check for standard action name */
738 const struct action_name * action = action_names;
740 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
744 if (action->name != NULL)
747 cur_action->mask &= action->mask;
748 cur_action->add &= action->mask;
749 cur_action->add |= action->add;
751 switch (action->takes_value)
754 /* ignore any option. */
758 /* add single string. */
760 if ((value == NULL) || (*value == '\0'))
762 if (0 != strcmpic(action->name, "block"))
765 * XXX: Temporary backwards compatibility hack.
766 * XXX: should include line number.
768 value = "No reason specified.";
769 log_error(LOG_LEVEL_ERROR,
770 "block action without reason found. This may "
771 "become a fatal error in future versions.");
778 /* FIXME: should validate option string here */
779 freez (cur_action->string[action->index]);
780 cur_action->string[action->index] = strdup(value);
781 if (NULL == cur_action->string[action->index])
783 return JB_ERR_MEMORY;
789 /* remove single string. */
791 freez (cur_action->string[action->index]);
796 /* append multi string. */
798 struct list * remove_p = cur_action->multi_remove[action->index];
799 struct list * add_p = cur_action->multi_add[action->index];
801 if ((value == NULL) || (*value == '\0'))
806 list_remove_item(remove_p, value);
807 err = enlist_unique(add_p, value, 0);
816 /* remove multi string. */
818 struct list * remove_p = cur_action->multi_remove[action->index];
819 struct list * add_p = cur_action->multi_add[action->index];
821 if ( (value == NULL) || (*value == '\0')
822 || ((*value == '*') && (value[1] == '\0')) )
825 * no option, or option == "*".
829 list_remove_all(remove_p);
830 list_remove_all(add_p);
831 cur_action->multi_remove_all[action->index] = 1;
835 /* Valid option - remove only 1 option */
837 if ( !cur_action->multi_remove_all[action->index] )
839 /* there isn't a catch-all in the remove list already */
840 err = enlist_unique(remove_p, value, 0);
846 list_remove_item(add_p, value);
851 /* Shouldn't get here unless there's memory corruption. */
858 /* try user aliases. */
859 const struct action_alias * alias = alias_list;
861 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
868 merge_actions(cur_action, alias->action);
870 else if ((2 < strlen(option)) && action_used_to_be_valid(option+1))
872 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
873 "in this Privoxy release. Ignored.", option+1);
877 /* Bad action name */
879 * XXX: This is a fatal error and Privoxy will later on exit
880 * in load_one_actions_file() because of an "invalid line".
882 * It would be preferable to name the offending option in that
883 * error message, but currently there is no way to do that and
884 * we have to live with two error messages for basically the
887 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
898 /*********************************************************************
900 * Function : init_current_action
902 * Description : Zero out an action.
905 * 1 : dest = An uninitialized current_action_spec.
909 *********************************************************************/
910 void init_current_action (struct current_action_spec *dest)
912 memset(dest, '\0', sizeof(*dest));
914 dest->flags = ACTION_MOST_COMPATIBLE;
918 /*********************************************************************
920 * Function : init_action
922 * Description : Zero out an action.
925 * 1 : dest = An uninitialized action_spec.
929 *********************************************************************/
930 void init_action (struct action_spec *dest)
932 memset(dest, '\0', sizeof(*dest));
936 /*********************************************************************
938 * Function : merge_current_action
940 * Description : Merge two actions together.
941 * Similar to "dest += src".
942 * Differences between this and merge_actions()
943 * is that this one doesn't allocate memory for
944 * strings (so "src" better be in memory for at least
945 * as long as "dest" is, and you'd better free
946 * "dest" using "free_current_action").
947 * Also, there is no mask or remove lists in dest.
948 * (If we're applying it to a URL, we don't need them)
951 * 1 : dest = Current actions, to modify.
952 * 2 : src = Action to add.
954 * Returns 0 : no error
955 * !=0 : error, probably JB_ERR_MEMORY.
957 *********************************************************************/
958 jb_err merge_current_action (struct current_action_spec *dest,
959 const struct action_spec *src)
962 jb_err err = JB_ERR_OK;
964 dest->flags &= src->mask;
965 dest->flags |= src->add;
967 for (i = 0; i < ACTION_STRING_COUNT; i++)
969 char * str = src->string[i];
975 return JB_ERR_MEMORY;
977 freez(dest->string[i]);
978 dest->string[i] = str;
982 for (i = 0; i < ACTION_MULTI_COUNT; i++)
984 if (src->multi_remove_all[i])
986 /* Remove everything from dest, then add src->multi_add */
987 err = list_duplicate(dest->multi[i], src->multi_add[i]);
995 list_remove_list(dest->multi[i], src->multi_remove[i]);
996 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
1007 /*********************************************************************
1009 * Function : update_action_bits_for_all_tags
1011 * Description : Updates the action bits based on all matching tags.
1014 * 1 : csp = Current client state (buffers, headers, etc...)
1016 * Returns : 0 if no tag matched, or
1019 *********************************************************************/
1020 int update_action_bits_for_all_tags(struct client_state *csp)
1022 struct list_entry *tag;
1025 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
1027 if (update_action_bits_for_tag(csp, tag->str))
1037 /*********************************************************************
1039 * Function : update_action_bits_for_tag
1041 * Description : Updates the action bits based on the action sections
1042 * whose tag patterns match a provided tag.
1045 * 1 : csp = Current client state (buffers, headers, etc...)
1046 * 2 : tag = The tag on which the update should be based on
1048 * Returns : 0 if no tag matched, or
1051 *********************************************************************/
1052 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
1054 struct file_list *fl;
1055 struct url_actions *b;
1061 assert(list_contains_item(csp->tags, tag));
1063 /* Run through all action files, */
1064 for (i = 0; i < MAX_AF_FILES; i++)
1066 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
1068 /* Skip empty files */
1072 /* and through all the action patterns, */
1073 for (b = b->next; NULL != b; b = b->next)
1075 /* skip the URL patterns, */
1076 if (NULL == b->url->tag_regex)
1081 /* and check if one of the tag patterns matches the tag, */
1082 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
1084 /* if it does, update the action bit map, */
1085 if (merge_current_action(csp->action, b->action))
1087 log_error(LOG_LEVEL_ERROR,
1088 "Out of memory while changing action bits");
1090 /* and signal the change. */
1100 /*********************************************************************
1102 * Function : free_current_action
1104 * Description : Free memory used by a current_action_spec.
1105 * Does not free the current_action_spec itself.
1108 * 1 : src = Source to free.
1112 *********************************************************************/
1113 void free_current_action (struct current_action_spec *src)
1117 for (i = 0; i < ACTION_STRING_COUNT; i++)
1119 freez(src->string[i]);
1122 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1124 destroy_list(src->multi[i]);
1127 memset(src, '\0', sizeof(*src));
1131 static struct file_list *current_actions_file[MAX_AF_FILES] = {
1132 NULL, NULL, NULL, NULL, NULL,
1133 NULL, NULL, NULL, NULL, NULL
1137 #ifdef FEATURE_GRACEFUL_TERMINATION
1138 /*********************************************************************
1140 * Function : unload_current_actions_file
1142 * Description : Unloads current actions file - reset to state at
1143 * beginning of program.
1149 *********************************************************************/
1150 void unload_current_actions_file(void)
1154 for (i = 0; i < MAX_AF_FILES; i++)
1156 if (current_actions_file[i])
1158 current_actions_file[i]->unloader = unload_actions_file;
1159 current_actions_file[i] = NULL;
1163 #endif /* FEATURE_GRACEFUL_TERMINATION */
1166 /*********************************************************************
1168 * Function : unload_actions_file
1170 * Description : Unloads an actions module.
1173 * 1 : file_data = the data structure associated with the
1178 *********************************************************************/
1179 void unload_actions_file(void *file_data)
1181 struct url_actions * next;
1182 struct url_actions * cur = (struct url_actions *)file_data;
1186 free_url_spec(cur->url);
1187 if ((next == NULL) || (next->action != cur->action))
1190 * As the action settings might be shared,
1191 * we can only free them if the current
1192 * url pattern is the last one, or if the
1193 * next one is using different settings.
1195 free_action_spec(cur->action);
1203 /*********************************************************************
1205 * Function : free_alias_list
1207 * Description : Free memory used by a list of aliases.
1210 * 1 : alias_list = Linked list to free.
1214 *********************************************************************/
1215 void free_alias_list(struct action_alias *alias_list)
1217 while (alias_list != NULL)
1219 struct action_alias * next = alias_list->next;
1220 alias_list->next = NULL;
1221 freez(alias_list->name);
1222 free_action(alias_list->action);
1229 /*********************************************************************
1231 * Function : load_action_files
1233 * Description : Read and parse all the action files and add to files
1237 * 1 : csp = Current client state (buffers, headers, etc...)
1239 * Returns : 0 => Ok, everything else is an error.
1241 *********************************************************************/
1242 int load_action_files(struct client_state *csp)
1247 for (i = 0; i < MAX_AF_FILES; i++)
1249 if (csp->config->actions_file[i])
1251 result = load_one_actions_file(csp, i);
1257 else if (current_actions_file[i])
1259 current_actions_file[i]->unloader = unload_actions_file;
1260 current_actions_file[i] = NULL;
1267 /*********************************************************************
1269 * Function : load_one_actions_file
1271 * Description : Read and parse a action file and add to files
1275 * 1 : csp = Current client state (buffers, headers, etc...)
1276 * 2 : fileid = File index to load.
1278 * Returns : 0 => Ok, everything else is an error.
1280 *********************************************************************/
1281 static int load_one_actions_file(struct client_state *csp, int fileid)
1286 * Note: Keep these in the order they occur in the file, they are
1287 * sometimes tested with <=
1289 #define MODE_START_OF_FILE 1
1290 #define MODE_SETTINGS 2
1291 #define MODE_DESCRIPTION 3
1292 #define MODE_ALIAS 4
1293 #define MODE_ACTIONS 5
1295 int mode = MODE_START_OF_FILE;
1298 struct url_actions *last_perm;
1299 struct url_actions *perm;
1300 char buf[BUFFER_SIZE];
1301 struct file_list *fs;
1302 struct action_spec * cur_action = NULL;
1303 int cur_action_used = 0;
1304 struct action_alias * alias_list = NULL;
1305 unsigned long linenum = 0;
1307 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1309 /* No need to load */
1310 csp->actions_list[fileid] = current_actions_file[fileid];
1315 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1316 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1317 "with their complete file names.", csp->config->actions_file[fileid]);
1318 return 1; /* never get here */
1321 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1322 if (last_perm == NULL)
1324 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1325 csp->config->actions_file[fileid]);
1326 return 1; /* never get here */
1329 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1331 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1332 csp->config->actions_file[fileid]);
1333 return 1; /* never get here */
1336 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1340 /* It's a header block */
1343 /* It's {{settings}} or {{alias}} */
1344 size_t len = strlen(buf);
1345 char * start = buf + 2;
1346 char * end = buf + len - 1;
1347 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1351 log_error(LOG_LEVEL_FATAL,
1352 "can't load actions file '%s': invalid line (%lu): %s",
1353 csp->config->actions_file[fileid], linenum, buf);
1354 return 1; /* never get here */
1357 /* Trim leading and trailing whitespace. */
1365 log_error(LOG_LEVEL_FATAL,
1366 "can't load actions file '%s': invalid line (%lu): {{ }}",
1367 csp->config->actions_file[fileid], linenum);
1368 return 1; /* never get here */
1372 * An actionsfile can optionally contain the following blocks.
1373 * They *MUST* be in this order, to simplify processing:
1379 * ...free text, format TBD, but no line may start with a '{'...
1384 * The actual actions must be *after* these special blocks.
1385 * None of these special blocks may be repeated.
1388 if (0 == strcmpic(start, "settings"))
1390 /* it's a {{settings}} block */
1391 if (mode >= MODE_SETTINGS)
1393 /* {{settings}} must be first thing in file and must only
1397 log_error(LOG_LEVEL_FATAL,
1398 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1399 csp->config->actions_file[fileid], linenum);
1401 mode = MODE_SETTINGS;
1403 else if (0 == strcmpic(start, "description"))
1405 /* it's a {{description}} block */
1406 if (mode >= MODE_DESCRIPTION)
1408 /* {{description}} is a singleton and only {{settings}} may proceed it
1411 log_error(LOG_LEVEL_FATAL,
1412 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1413 csp->config->actions_file[fileid], linenum);
1415 mode = MODE_DESCRIPTION;
1417 else if (0 == strcmpic(start, "alias"))
1419 /* it's an {{alias}} block */
1420 if (mode >= MODE_ALIAS)
1422 /* {{alias}} must be first thing in file, possibly after
1423 * {{settings}} and {{description}}
1425 * {{alias}} must only appear once.
1427 * Note that these are new restrictions introduced in
1428 * v2.9.10 in order to make actionsfile editing simpler.
1429 * (Otherwise, reordering actionsfile entries without
1430 * completely rewriting the file becomes non-trivial)
1433 log_error(LOG_LEVEL_FATAL,
1434 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1435 csp->config->actions_file[fileid], linenum);
1441 /* invalid {{something}} block */
1443 log_error(LOG_LEVEL_FATAL,
1444 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1445 csp->config->actions_file[fileid], linenum, start);
1446 return 1; /* never get here */
1451 /* It's an actions block */
1453 char actions_buf[BUFFER_SIZE];
1457 mode = MODE_ACTIONS;
1459 /* free old action */
1462 if (!cur_action_used)
1464 free_action_spec(cur_action);
1468 cur_action_used = 0;
1469 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1470 if (cur_action == NULL)
1473 log_error(LOG_LEVEL_FATAL,
1474 "can't load actions file '%s': out of memory",
1475 csp->config->actions_file[fileid]);
1476 return 1; /* never get here */
1478 init_action(cur_action);
1481 strlcpy(actions_buf, buf + 1, sizeof(actions_buf));
1483 /* check we have a trailing } and then trim it */
1484 end = actions_buf + strlen(actions_buf) - 1;
1489 log_error(LOG_LEVEL_FATAL,
1490 "can't load actions file '%s': invalid line (%lu): %s",
1491 csp->config->actions_file[fileid], linenum, buf);
1492 return 1; /* never get here */
1496 /* trim any whitespace immediately inside {} */
1499 if (get_actions(actions_buf, alias_list, cur_action))
1503 log_error(LOG_LEVEL_FATAL,
1504 "can't load actions file '%s': invalid line (%lu): %s",
1505 csp->config->actions_file[fileid], linenum, buf);
1506 return 1; /* never get here */
1510 else if (mode == MODE_SETTINGS)
1513 * Part of the {{settings}} block.
1514 * For now only serves to check if the file's minimum Privoxy
1515 * version requirement is met, but we may want to read & check
1516 * permissions when we go multi-user.
1518 if (!strncmp(buf, "for-privoxy-version=", 20))
1520 char *version_string, *fields[3];
1523 if ((version_string = strdup(buf + 20)) == NULL)
1526 log_error(LOG_LEVEL_FATAL,
1527 "can't load actions file '%s': out of memory!",
1528 csp->config->actions_file[fileid]);
1529 return 1; /* never get here */
1532 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1534 if (num_fields < 1 || atoi(fields[0]) == 0)
1536 log_error(LOG_LEVEL_ERROR,
1537 "While loading actions file '%s': invalid line (%lu): %s",
1538 csp->config->actions_file[fileid], linenum, buf);
1540 else if ( atoi(fields[0]) > VERSION_MAJOR
1541 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1542 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1545 log_error(LOG_LEVEL_FATAL,
1546 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1547 csp->config->actions_file[fileid], linenum, buf );
1548 return 1; /* never get here */
1550 free(version_string);
1553 else if (mode == MODE_DESCRIPTION)
1556 * Part of the {{description}} block.
1560 else if (mode == MODE_ALIAS)
1565 char actions_buf[BUFFER_SIZE];
1566 struct action_alias * new_alias;
1568 char * start = strchr(buf, '=');
1571 if ((start == NULL) || (start == buf))
1573 log_error(LOG_LEVEL_FATAL,
1574 "can't load actions file '%s': invalid alias line (%lu): %s",
1575 csp->config->actions_file[fileid], linenum, buf);
1576 return 1; /* never get here */
1579 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1582 log_error(LOG_LEVEL_FATAL,
1583 "can't load actions file '%s': out of memory!",
1584 csp->config->actions_file[fileid]);
1585 return 1; /* never get here */
1588 /* Eat any the whitespace before the '=' */
1590 while ((*end == ' ') || (*end == '\t'))
1593 * we already know we must have at least 1 non-ws char
1594 * at start of buf - no need to check
1600 /* Eat any the whitespace after the '=' */
1602 while ((*start == ' ') || (*start == '\t'))
1608 log_error(LOG_LEVEL_FATAL,
1609 "can't load actions file '%s': invalid alias line (%lu): %s",
1610 csp->config->actions_file[fileid], linenum, buf);
1611 return 1; /* never get here */
1614 if ((new_alias->name = strdup(buf)) == NULL)
1617 log_error(LOG_LEVEL_FATAL,
1618 "can't load actions file '%s': out of memory!",
1619 csp->config->actions_file[fileid]);
1620 return 1; /* never get here */
1623 strlcpy(actions_buf, start, sizeof(actions_buf));
1625 if (get_actions(actions_buf, alias_list, new_alias->action))
1629 log_error(LOG_LEVEL_FATAL,
1630 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1631 csp->config->actions_file[fileid], linenum, buf, start);
1632 return 1; /* never get here */
1636 new_alias->next = alias_list;
1637 alias_list = new_alias;
1639 else if (mode == MODE_ACTIONS)
1641 /* it's a URL pattern */
1643 /* allocate a new node */
1644 if ((perm = zalloc(sizeof(*perm))) == NULL)
1647 log_error(LOG_LEVEL_FATAL,
1648 "can't load actions file '%s': out of memory!",
1649 csp->config->actions_file[fileid]);
1650 return 1; /* never get here */
1653 perm->action = cur_action;
1654 cur_action_used = 1;
1656 /* Save the URL pattern */
1657 if (create_url_spec(perm->url, buf))
1660 log_error(LOG_LEVEL_FATAL,
1661 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1662 csp->config->actions_file[fileid], linenum, buf);
1663 return 1; /* never get here */
1666 /* add it to the list */
1667 last_perm->next = perm;
1670 else if (mode == MODE_START_OF_FILE)
1672 /* oops - please have a {} line as 1st line in file. */
1674 log_error(LOG_LEVEL_FATAL,
1675 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1676 csp->config->actions_file[fileid], linenum, buf);
1677 return 1; /* never get here */
1681 /* How did we get here? This is impossible! */
1683 log_error(LOG_LEVEL_FATAL,
1684 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1685 csp->config->actions_file[fileid], mode);
1686 return 1; /* never get here */
1692 if (!cur_action_used)
1694 free_action_spec(cur_action);
1696 free_alias_list(alias_list);
1698 /* the old one is now obsolete */
1699 if (current_actions_file[fileid])
1701 current_actions_file[fileid]->unloader = unload_actions_file;
1704 fs->next = files->next;
1706 current_actions_file[fileid] = fs;
1708 csp->actions_list[fileid] = fs;
1715 /*********************************************************************
1717 * Function : actions_to_text
1719 * Description : Converts a actionsfile entry from the internal
1720 * structure into a text line. The output is split
1721 * into one line for each action with line continuation.
1724 * 1 : action = The action to format.
1726 * Returns : A string. Caller must free it.
1727 * NULL on out-of-memory error.
1729 *********************************************************************/
1730 char * actions_to_text(const struct action_spec *action)
1732 unsigned mask = action->mask;
1733 unsigned add = action->add;
1734 char * result = strdup("");
1735 struct list_entry * lst;
1737 /* sanity - prevents "-feature +feature" */
1741 #define DEFINE_ACTION_BOOL(__name, __bit) \
1742 if (!(mask & __bit)) \
1744 string_append(&result, " -" __name " \\\n"); \
1746 else if (add & __bit) \
1748 string_append(&result, " +" __name " \\\n"); \
1751 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1752 if (!(mask & __bit)) \
1754 string_append(&result, " -" __name " \\\n"); \
1756 else if (add & __bit) \
1758 string_append(&result, " +" __name "{"); \
1759 string_append(&result, action->string[__index]); \
1760 string_append(&result, "} \\\n"); \
1763 #define DEFINE_ACTION_MULTI(__name, __index) \
1764 if (action->multi_remove_all[__index]) \
1766 string_append(&result, " -" __name " \\\n"); \
1770 lst = action->multi_remove[__index]->first; \
1773 string_append(&result, " -" __name "{"); \
1774 string_append(&result, lst->str); \
1775 string_append(&result, "} \\\n"); \
1779 lst = action->multi_add[__index]->first; \
1782 string_append(&result, " +" __name "{"); \
1783 string_append(&result, lst->str); \
1784 string_append(&result, "} \\\n"); \
1788 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1790 #include "actionlist.h"
1792 #undef DEFINE_ACTION_MULTI
1793 #undef DEFINE_ACTION_STRING
1794 #undef DEFINE_ACTION_BOOL
1795 #undef DEFINE_ACTION_ALIAS
1801 /*********************************************************************
1803 * Function : actions_to_html
1805 * Description : Converts a actionsfile entry from numeric form
1806 * ("mask" and "add") to a <br>-seperated HTML string
1807 * in which each action is linked to its chapter in
1811 * 1 : csp = Client state (for config)
1812 * 2 : action = Action spec to be converted
1814 * Returns : A string. Caller must free it.
1815 * NULL on out-of-memory error.
1817 *********************************************************************/
1818 char * actions_to_html(const struct client_state *csp,
1819 const struct action_spec *action)
1821 unsigned mask = action->mask;
1822 unsigned add = action->add;
1823 char * result = strdup("");
1824 struct list_entry * lst;
1826 /* sanity - prevents "-feature +feature" */
1830 #define DEFINE_ACTION_BOOL(__name, __bit) \
1831 if (!(mask & __bit)) \
1833 string_append(&result, "\n<br>-"); \
1834 string_join(&result, add_help_link(__name, csp->config)); \
1836 else if (add & __bit) \
1838 string_append(&result, "\n<br>+"); \
1839 string_join(&result, add_help_link(__name, csp->config)); \
1842 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1843 if (!(mask & __bit)) \
1845 string_append(&result, "\n<br>-"); \
1846 string_join(&result, add_help_link(__name, csp->config)); \
1848 else if (add & __bit) \
1850 string_append(&result, "\n<br>+"); \
1851 string_join(&result, add_help_link(__name, csp->config)); \
1852 string_append(&result, "{"); \
1853 string_join(&result, html_encode(action->string[__index])); \
1854 string_append(&result, "}"); \
1857 #define DEFINE_ACTION_MULTI(__name, __index) \
1858 if (action->multi_remove_all[__index]) \
1860 string_append(&result, "\n<br>-"); \
1861 string_join(&result, add_help_link(__name, csp->config)); \
1865 lst = action->multi_remove[__index]->first; \
1868 string_append(&result, "\n<br>-"); \
1869 string_join(&result, add_help_link(__name, csp->config)); \
1870 string_append(&result, "{"); \
1871 string_join(&result, html_encode(lst->str)); \
1872 string_append(&result, "}"); \
1876 lst = action->multi_add[__index]->first; \
1879 string_append(&result, "\n<br>+"); \
1880 string_join(&result, add_help_link(__name, csp->config)); \
1881 string_append(&result, "{"); \
1882 string_join(&result, html_encode(lst->str)); \
1883 string_append(&result, "}"); \
1887 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1889 #include "actionlist.h"
1891 #undef DEFINE_ACTION_MULTI
1892 #undef DEFINE_ACTION_STRING
1893 #undef DEFINE_ACTION_BOOL
1894 #undef DEFINE_ACTION_ALIAS
1896 /* trim leading <br> */
1897 if (result && *result)
1900 result = strdup(result + 5);
1908 /*********************************************************************
1910 * Function : current_actions_to_html
1912 * Description : Converts a curren action spec to a <br> seperated HTML
1913 * text in which each action is linked to its chapter in
1917 * 1 : csp = Client state (for config)
1918 * 2 : action = Current action spec to be converted
1920 * Returns : A string. Caller must free it.
1921 * NULL on out-of-memory error.
1923 *********************************************************************/
1924 char *current_action_to_html(const struct client_state *csp,
1925 const struct current_action_spec *action)
1927 unsigned long flags = action->flags;
1928 struct list_entry * lst;
1929 char *result = strdup("");
1930 char *active = strdup("");
1931 char *inactive = strdup("");
1933 #define DEFINE_ACTION_BOOL(__name, __bit) \
1934 if (flags & __bit) \
1936 string_append(&active, "\n<br>+"); \
1937 string_join(&active, add_help_link(__name, csp->config)); \
1941 string_append(&inactive, "\n<br>-"); \
1942 string_join(&inactive, add_help_link(__name, csp->config)); \
1945 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1946 if (flags & __bit) \
1948 string_append(&active, "\n<br>+"); \
1949 string_join(&active, add_help_link(__name, csp->config)); \
1950 string_append(&active, "{"); \
1951 string_join(&active, html_encode(action->string[__index])); \
1952 string_append(&active, "}"); \
1956 string_append(&inactive, "\n<br>-"); \
1957 string_join(&inactive, add_help_link(__name, csp->config)); \
1960 #define DEFINE_ACTION_MULTI(__name, __index) \
1961 lst = action->multi[__index]->first; \
1964 string_append(&inactive, "\n<br>-"); \
1965 string_join(&inactive, add_help_link(__name, csp->config)); \
1971 string_append(&active, "\n<br>+"); \
1972 string_join(&active, add_help_link(__name, csp->config)); \
1973 string_append(&active, "{"); \
1974 string_join(&active, html_encode(lst->str)); \
1975 string_append(&active, "}"); \
1980 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1982 #include "actionlist.h"
1984 #undef DEFINE_ACTION_MULTI
1985 #undef DEFINE_ACTION_STRING
1986 #undef DEFINE_ACTION_BOOL
1987 #undef DEFINE_ACTION_ALIAS
1991 string_append(&result, active);
1994 string_append(&result, "\n<br>");
1995 if (inactive != NULL)
1997 string_append(&result, inactive);