1 const char actions_rcs[] = "$Id: actions.c,v 1.54 2008/09/20 10:04:33 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.54 2008/09/20 10:04:33 fabiankeil
37 * Remove hide-forwarded-for-headers action which has
38 * been obsoleted by change-x-forwarded-for{block}.
40 * Revision 1.53 2008/05/26 16:04:04 fabiankeil
43 * Revision 1.52 2008/04/27 16:26:59 fabiankeil
44 * White space fix for the last commit.
46 * Revision 1.51 2008/04/27 16:20:19 fabiankeil
47 * Complain about every block action without reason found.
49 * Revision 1.50 2008/03/30 14:52:00 fabiankeil
50 * Rename load_actions_file() and load_re_filterfile()
51 * as they load multiple files "now".
53 * Revision 1.49 2008/03/29 12:13:45 fabiankeil
54 * Remove send-wafer and send-vanilla-wafer actions.
56 * Revision 1.48 2008/03/28 18:17:14 fabiankeil
57 * In action_used_to_be_valid(), loop through an array of formerly
58 * valid actions instead of using an OR-chain of strcmpic() calls.
60 * Revision 1.47 2008/03/28 15:13:37 fabiankeil
61 * Remove inspect-jpegs action.
63 * Revision 1.46 2008/03/27 18:27:20 fabiankeil
64 * Remove kill-popups action.
66 * Revision 1.45 2008/03/24 11:21:02 fabiankeil
67 * Share the action settings for multiple patterns in the same
68 * section so we waste less memory for gigantic block lists
69 * (and load them slightly faster). Reported by Franz Schwartau.
71 * Revision 1.44 2008/03/04 18:30:34 fabiankeil
72 * Remove the treat-forbidden-connects-like-blocks action. We now
73 * use the "blocked" page for forbidden CONNECT requests by default.
75 * Revision 1.43 2008/03/01 14:00:43 fabiankeil
76 * Let the block action take the reason for the block
77 * as argument and show it on the "blocked" page.
79 * Revision 1.42 2008/02/09 15:15:38 fabiankeil
80 * List active and inactive actions in the show-url-info's
81 * "Final results" section separately. Patch submitted by Lee
82 * in #1830056, modified to list active actions first.
84 * Revision 1.41 2008/01/28 20:17:40 fabiankeil
85 * - Mark some parameters as immutable.
86 * - Hide update_action_bits_for_all_tags() while it's unused.
88 * Revision 1.40 2007/05/21 10:26:50 fabiankeil
89 * - Use strlcpy() instead of strcpy().
90 * - Provide a reason why loading the actions
91 * file might have failed.
93 * Revision 1.39 2007/04/17 18:21:45 fabiankeil
94 * Split update_action_bits() into
95 * update_action_bits_for_all_tags()
96 * and update_action_bits_for_tag().
98 * Revision 1.38 2007/04/15 16:39:20 fabiankeil
99 * Introduce tags as alternative way to specify which
100 * actions apply to a request. At the moment tags can be
101 * created based on client and server headers.
103 * Revision 1.37 2007/03/11 15:56:12 fabiankeil
104 * Add kludge to log unknown aliases and actions before exiting.
106 * Revision 1.36 2006/12/28 17:15:42 fabiankeil
107 * Fix gcc43 conversion warning.
109 * Revision 1.35 2006/07/18 14:48:45 david__schmidt
110 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
111 * with what was really the latest development (the v_3_0_branch branch)
113 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
114 * Multiple filter file support
116 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
117 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
118 * this becomes important when jcc.h gets included later down the line.
120 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
121 * - Implemented Privoxy version requirement through
122 * for-privoxy-version= statement in {{settings}}
124 * - Fix for unchecked out-of-memory condition
126 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
127 * Fixed memory leak reported by Dan Price in Bug #694713
129 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
130 * Extended unload_current_actions_file() to multiple AFs.
131 * Thanks to Oliver Stoeneberg for the hint.
133 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
134 * Change unsigned to unsigned long in actions_name struct. This closes
135 * SourceForge Bug #539284.
137 * Revision 1.32 2002/05/12 21:36:29 jongfoster
138 * Correcting function comments
140 * Revision 1.31 2002/05/06 07:56:50 oes
141 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
143 * Revision 1.30 2002/04/30 11:14:52 oes
144 * Made csp the first parameter in *action_to_html
146 * Revision 1.29 2002/04/26 19:30:54 jongfoster
147 * - current_action_to_html(): Adding help link for the "-" form of
148 * one-string actions.
149 * - Some actions had "<br>-", some "<br> -" (note the space).
150 * Standardizing on no space.
151 * - Greatly simplifying some of the code by using string_join()
154 * Revision 1.28 2002/04/26 12:53:15 oes
155 * - CGI AF editor now writes action lines split into
156 * single lines with line continuation
157 * - actions_to_html now embeds each action name in
159 * - current_action_to_text is now called current_action_to_html
160 * and acts like actions_to_html
162 * Revision 1.27 2002/04/24 02:10:31 oes
163 * - Jon's patch for multiple AFs:
164 * - split load_actions_file, add load_one_actions_file
165 * - make csp->actions_list files an array
166 * - remember file id with each action
167 * - Copy_action now frees dest action before copying
169 * Revision 1.26 2002/03/26 22:29:54 swa
170 * we have a new homepage!
172 * Revision 1.25 2002/03/24 13:25:43 swa
173 * name change related issues
175 * Revision 1.24 2002/03/16 23:54:06 jongfoster
176 * Adding graceful termination feature, to help look for memory leaks.
177 * If you enable this (which, by design, has to be done by hand
178 * editing config.h) and then go to http://i.j.b/die, then the program
179 * will exit cleanly after the *next* request. It should free all the
180 * memory that was used.
182 * Revision 1.23 2002/03/07 03:46:16 oes
183 * Fixed compiler warnings
185 * Revision 1.22 2002/01/21 00:27:02 jongfoster
186 * Allowing free_action(NULL).
187 * Moving the functions that #include actionlist.h to the end of the file,
188 * because the Visual C++ 97 debugger gets extremely confused if you try
189 * to debug any code that comes after them in the file.
191 * Revision 1.21 2002/01/17 20:54:44 jongfoster
192 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
194 * Revision 1.20 2001/11/22 21:56:49 jongfoster
195 * Making action_spec->flags into an unsigned long rather than just an
197 * Fixing a bug in the display of -add-header and -wafer
199 * Revision 1.19 2001/11/13 00:14:07 jongfoster
200 * Fixing stupid bug now I've figured out what || means.
201 * (It always returns 0 or 1, not one of it's paramaters.)
203 * Revision 1.18 2001/11/07 00:06:06 steudten
204 * Add line number in error output for lineparsing for
207 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
208 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
209 * threads to call select() simultaneously. So, it's time to do a real, live,
210 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
211 * (native). Both versions will work, but using __OS2__ offers multi-threading.
213 * Revision 1.16 2001/10/23 21:30:30 jongfoster
214 * Adding error-checking to selected functions.
216 * Revision 1.15 2001/10/14 21:58:22 jongfoster
217 * Adding support for the CGI-based editor:
218 * - Exported get_actions()
219 * - Added new function free_alias_list()
220 * - Added support for {{settings}} and {{description}} blocks
221 * in the actions file. They are currently ignored.
222 * - Added restriction to only one {{alias}} block which must appear
223 * first in the file, to simplify the editor's rewriting rules.
224 * - Note that load_actions_file() is no longer used by the CGI-based
225 * editor, but some of the other routines in this file are.
227 * Revision 1.14 2001/09/22 16:36:59 jongfoster
228 * Removing unused parameter fs from read_config_line()
230 * Revision 1.13 2001/09/16 15:47:37 jongfoster
231 * First version of CGI-based edit interface. This is very much a
232 * work-in-progress, and you can't actually use it to edit anything
233 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
234 * to have any effect.
236 * Revision 1.12 2001/09/16 13:21:27 jongfoster
237 * Changes to use new list functions.
239 * Revision 1.11 2001/09/14 00:17:32 jongfoster
240 * Tidying up memory allocation. New function init_action().
242 * Revision 1.10 2001/09/10 10:14:34 oes
243 * Removing unused variable
245 * Revision 1.9 2001/07/30 22:08:36 jongfoster
246 * Tidying up #defines:
247 * - All feature #defines are now of the form FEATURE_xxx
248 * - Permanently turned off WIN_GUI_EDIT
249 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
251 * Revision 1.8 2001/06/29 13:19:52 oes
252 * Removed logentry from cancelled commit
254 * Revision 1.7 2001/06/09 10:55:28 jongfoster
255 * Changing BUFSIZ ==> BUFFER_SIZE
257 * Revision 1.6 2001/06/07 23:04:34 jongfoster
258 * Made get_actions() static.
260 * Revision 1.5 2001/06/03 19:11:48 oes
261 * adapted to new enlist_unique arg format
263 * Revision 1.4 2001/06/01 20:03:42 jongfoster
264 * Better memory management - current_action->strings[] now
265 * contains copies of the strings, not the original.
267 * Revision 1.3 2001/06/01 18:49:17 jongfoster
268 * Replaced "list_share" with "list" - the tiny memory gain was not
269 * worth the extra complexity.
271 * Revision 1.2 2001/05/31 21:40:00 jongfoster
272 * Removing some commented out, obsolete blocks of code.
274 * Revision 1.1 2001/05/31 21:16:46 jongfoster
275 * Moved functions to process the action list into this new file.
278 *********************************************************************/
288 #ifdef FEATURE_PTHREAD
296 #include "miscutil.h"
300 #include "urlmatch.h"
304 const char actions_h_rcs[] = ACTIONS_H_VERSION;
308 * We need the main list of options.
310 * First, we need a way to tell between boolean, string, and multi-string
311 * options. For string and multistring options, we also need to be
312 * able to tell the difference between a "+" and a "-". (For bools,
313 * the "+"/"-" information is encoded in "add" and "mask"). So we use
314 * an enumerated type (well, the preprocessor equivalent). Here are
317 #define AV_NONE 0 /* +opt -opt */
318 #define AV_ADD_STRING 1 /* +stropt{string} */
319 #define AV_REM_STRING 2 /* -stropt */
320 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
321 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
324 * We need a structure to hold the name, flag changes,
325 * type, and string index.
330 unsigned long mask; /* a bit set to "0" = remove action */
331 unsigned long add; /* a bit set to "1" = add action */
332 int takes_value; /* an AV_... constant */
333 int index; /* index into strings[] or multi[] */
337 * And with those building blocks in place, here's the array.
339 static const struct action_name action_names[] =
342 * Well actually there's no data here - it's in actionlist.h
343 * This keeps it together to make it easy to change.
345 * Here's the macros used to format it:
347 #define DEFINE_ACTION_MULTI(name,index) \
348 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
349 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
350 #define DEFINE_ACTION_STRING(name,flag,index) \
351 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
352 { "-" name, ~flag, 0, AV_REM_STRING, index },
353 #define DEFINE_ACTION_BOOL(name,flag) \
354 { "+" name, ACTION_MASK_ALL, flag }, \
355 { "-" name, ~flag, 0 },
356 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
358 #include "actionlist.h"
360 #undef DEFINE_ACTION_MULTI
361 #undef DEFINE_ACTION_STRING
362 #undef DEFINE_ACTION_BOOL
363 #undef DEFINE_ACTION_ALIAS
365 { NULL, 0, 0 } /* End marker */
369 static int load_one_actions_file(struct client_state *csp, int fileid);
372 /*********************************************************************
374 * Function : merge_actions
376 * Description : Merge two actions together.
377 * Similar to "dest += src".
380 * 1 : dest = Actions to modify.
381 * 2 : src = Action to add.
383 * Returns : JB_ERR_OK or JB_ERR_MEMORY
385 *********************************************************************/
386 jb_err merge_actions (struct action_spec *dest,
387 const struct action_spec *src)
392 dest->mask &= src->mask;
393 dest->add &= src->mask;
394 dest->add |= src->add;
396 for (i = 0; i < ACTION_STRING_COUNT; i++)
398 char * str = src->string[i];
401 freez(dest->string[i]);
402 dest->string[i] = strdup(str);
403 if (NULL == dest->string[i])
405 return JB_ERR_MEMORY;
410 for (i = 0; i < ACTION_MULTI_COUNT; i++)
412 if (src->multi_remove_all[i])
414 /* Remove everything from dest */
415 list_remove_all(dest->multi_remove[i]);
416 dest->multi_remove_all[i] = 1;
418 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
420 else if (dest->multi_remove_all[i])
423 * dest already removes everything, so we only need to worry
426 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
427 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
431 /* No "remove all"s to worry about. */
432 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
433 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
434 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
447 /*********************************************************************
449 * Function : copy_action
451 * Description : Copy an action_specs.
452 * Similar to "dest = src".
455 * 1 : dest = Destination of copy.
456 * 2 : src = Source for copy.
460 *********************************************************************/
461 jb_err copy_action (struct action_spec *dest,
462 const struct action_spec *src)
465 jb_err err = JB_ERR_OK;
468 memset(dest, '\0', sizeof(*dest));
470 dest->mask = src->mask;
471 dest->add = src->add;
473 for (i = 0; i < ACTION_STRING_COUNT; i++)
475 char * str = src->string[i];
481 return JB_ERR_MEMORY;
483 dest->string[i] = str;
487 for (i = 0; i < ACTION_MULTI_COUNT; i++)
489 dest->multi_remove_all[i] = src->multi_remove_all[i];
490 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
495 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
504 /*********************************************************************
506 * Function : free_action_spec
508 * Description : Frees an action_spec and the memory used by it.
511 * 1 : src = Source to free.
515 *********************************************************************/
516 void free_action_spec(struct action_spec *src)
523 /*********************************************************************
525 * Function : free_action
527 * Description : Destroy an action_spec. Frees memory used by it,
528 * except for the memory used by the struct action_spec
532 * 1 : src = Source to free.
536 *********************************************************************/
537 void free_action (struct action_spec *src)
546 for (i = 0; i < ACTION_STRING_COUNT; i++)
548 freez(src->string[i]);
551 for (i = 0; i < ACTION_MULTI_COUNT; i++)
553 destroy_list(src->multi_remove[i]);
554 destroy_list(src->multi_add[i]);
557 memset(src, '\0', sizeof(*src));
561 /*********************************************************************
563 * Function : get_action_token
565 * Description : Parses a line for the first action.
566 * Modifies it's input array, doesn't allocate memory.
568 * *line=" +abc{def} -ghi "
575 * 1 : line = [in] The line containing the action.
576 * [out] Start of next action on line, or
577 * NULL if we reached the end of line before
578 * we found an action.
579 * 2 : name = [out] Start of action name, null
580 * terminated. NULL on EOL
581 * 3 : value = [out] Start of action value, null
582 * terminated. NULL if none or EOL.
584 * Returns : JB_ERR_OK => Ok
585 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
587 *********************************************************************/
588 jb_err get_action_token(char **line, char **name, char **value)
593 /* set default returns */
598 /* Eat any leading whitespace */
599 while ((*str == ' ') || (*str == '\t'))
611 /* null name, just value is prohibited */
618 while (((ch = *str) != '\0') &&
619 (ch != ' ') && (ch != '\t') && (ch != '{'))
623 /* error, '}' without '{' */
635 /* EOL - be careful not to run off buffer */
640 /* More to parse next time. */
649 str = strchr(str, '}');
666 /*********************************************************************
668 * Function : action_used_to_be_valid
670 * Description : Checks if unrecognized actions were valid in earlier
674 * 1 : action = The string containing the action to check.
676 * Returns : True if yes, otherwise false.
678 *********************************************************************/
679 static int action_used_to_be_valid(const char *action)
681 static const char *formerly_valid_actions[] = {
684 "send-vanilla-wafer",
686 "treat-forbidden-connects-like-blocks",
692 for (i = 0; i < SZ(formerly_valid_actions); i++)
694 if (0 == strcmpic(action, formerly_valid_actions[i]))
703 /*********************************************************************
705 * Function : get_actions
707 * Description : Parses a list of actions.
710 * 1 : line = The string containing the actions.
711 * Will be written to by this function.
712 * 2 : alias_list = Custom alias list, or NULL for none.
713 * 3 : cur_action = Where to store the action. Caller
716 * Returns : JB_ERR_OK => Ok
717 * JB_ERR_PARSE => Parse error (line was trashed anyway)
718 * nonzero => Out of memory (line was trashed anyway)
720 *********************************************************************/
721 jb_err get_actions(char *line,
722 struct action_alias * alias_list,
723 struct action_spec *cur_action)
726 init_action(cur_action);
727 cur_action->mask = ACTION_MASK_ALL;
731 char * option = NULL;
734 err = get_action_token(&line, &option, &value);
742 /* handle option in 'option' */
744 /* Check for standard action name */
745 const struct action_name * action = action_names;
747 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
751 if (action->name != NULL)
754 cur_action->mask &= action->mask;
755 cur_action->add &= action->mask;
756 cur_action->add |= action->add;
758 switch (action->takes_value)
761 /* ignore any option. */
765 /* add single string. */
767 if ((value == NULL) || (*value == '\0'))
769 if (0 != strcmpic(action->name, "block"))
772 * XXX: Temporary backwards compatibility hack.
773 * XXX: should include line number.
775 value = "No reason specified.";
776 log_error(LOG_LEVEL_ERROR,
777 "block action without reason found. This may "
778 "become a fatal error in future versions.");
785 /* FIXME: should validate option string here */
786 freez (cur_action->string[action->index]);
787 cur_action->string[action->index] = strdup(value);
788 if (NULL == cur_action->string[action->index])
790 return JB_ERR_MEMORY;
796 /* remove single string. */
798 freez (cur_action->string[action->index]);
803 /* append multi string. */
805 struct list * remove_p = cur_action->multi_remove[action->index];
806 struct list * add_p = cur_action->multi_add[action->index];
808 if ((value == NULL) || (*value == '\0'))
813 list_remove_item(remove_p, value);
814 err = enlist_unique(add_p, value, 0);
823 /* remove multi string. */
825 struct list * remove_p = cur_action->multi_remove[action->index];
826 struct list * add_p = cur_action->multi_add[action->index];
828 if ( (value == NULL) || (*value == '\0')
829 || ((*value == '*') && (value[1] == '\0')) )
832 * no option, or option == "*".
836 list_remove_all(remove_p);
837 list_remove_all(add_p);
838 cur_action->multi_remove_all[action->index] = 1;
842 /* Valid option - remove only 1 option */
844 if ( !cur_action->multi_remove_all[action->index] )
846 /* there isn't a catch-all in the remove list already */
847 err = enlist_unique(remove_p, value, 0);
853 list_remove_item(add_p, value);
858 /* Shouldn't get here unless there's memory corruption. */
865 /* try user aliases. */
866 const struct action_alias * alias = alias_list;
868 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
875 merge_actions(cur_action, alias->action);
877 else if (((size_t)2 < strlen(option)) && action_used_to_be_valid(option+1))
879 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
880 "in this Privoxy release. Ignored.", option+1);
882 else if (((size_t)2 < strlen(option)) && 0 == strcmpic(option+1, "hide-forwarded-for-headers"))
884 log_error(LOG_LEVEL_FATAL, "The action 'hide-forwarded-for-headers' "
885 "is no longer valid in this Privoxy release. "
886 "Use 'change-x-forwarded-for' instead.");
890 /* Bad action name */
892 * XXX: This is a fatal error and Privoxy will later on exit
893 * in load_one_actions_file() because of an "invalid line".
895 * It would be preferable to name the offending option in that
896 * error message, but currently there is no way to do that and
897 * we have to live with two error messages for basically the
900 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
911 /*********************************************************************
913 * Function : init_current_action
915 * Description : Zero out an action.
918 * 1 : dest = An uninitialized current_action_spec.
922 *********************************************************************/
923 void init_current_action (struct current_action_spec *dest)
925 memset(dest, '\0', sizeof(*dest));
927 dest->flags = ACTION_MOST_COMPATIBLE;
931 /*********************************************************************
933 * Function : init_action
935 * Description : Zero out an action.
938 * 1 : dest = An uninitialized action_spec.
942 *********************************************************************/
943 void init_action (struct action_spec *dest)
945 memset(dest, '\0', sizeof(*dest));
949 /*********************************************************************
951 * Function : merge_current_action
953 * Description : Merge two actions together.
954 * Similar to "dest += src".
955 * Differences between this and merge_actions()
956 * is that this one doesn't allocate memory for
957 * strings (so "src" better be in memory for at least
958 * as long as "dest" is, and you'd better free
959 * "dest" using "free_current_action").
960 * Also, there is no mask or remove lists in dest.
961 * (If we're applying it to a URL, we don't need them)
964 * 1 : dest = Current actions, to modify.
965 * 2 : src = Action to add.
967 * Returns 0 : no error
968 * !=0 : error, probably JB_ERR_MEMORY.
970 *********************************************************************/
971 jb_err merge_current_action (struct current_action_spec *dest,
972 const struct action_spec *src)
975 jb_err err = JB_ERR_OK;
977 dest->flags &= src->mask;
978 dest->flags |= src->add;
980 for (i = 0; i < ACTION_STRING_COUNT; i++)
982 char * str = src->string[i];
988 return JB_ERR_MEMORY;
990 freez(dest->string[i]);
991 dest->string[i] = str;
995 for (i = 0; i < ACTION_MULTI_COUNT; i++)
997 if (src->multi_remove_all[i])
999 /* Remove everything from dest, then add src->multi_add */
1000 err = list_duplicate(dest->multi[i], src->multi_add[i]);
1008 list_remove_list(dest->multi[i], src->multi_remove[i]);
1009 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
1020 /*********************************************************************
1022 * Function : update_action_bits_for_all_tags
1024 * Description : Updates the action bits based on all matching tags.
1027 * 1 : csp = Current client state (buffers, headers, etc...)
1029 * Returns : 0 if no tag matched, or
1032 *********************************************************************/
1033 int update_action_bits_for_all_tags(struct client_state *csp)
1035 struct list_entry *tag;
1038 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
1040 if (update_action_bits_for_tag(csp, tag->str))
1050 /*********************************************************************
1052 * Function : update_action_bits_for_tag
1054 * Description : Updates the action bits based on the action sections
1055 * whose tag patterns match a provided tag.
1058 * 1 : csp = Current client state (buffers, headers, etc...)
1059 * 2 : tag = The tag on which the update should be based on
1061 * Returns : 0 if no tag matched, or
1064 *********************************************************************/
1065 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
1067 struct file_list *fl;
1068 struct url_actions *b;
1074 assert(list_contains_item(csp->tags, tag));
1076 /* Run through all action files, */
1077 for (i = 0; i < MAX_AF_FILES; i++)
1079 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
1081 /* Skip empty files */
1085 /* and through all the action patterns, */
1086 for (b = b->next; NULL != b; b = b->next)
1088 /* skip the URL patterns, */
1089 if (NULL == b->url->tag_regex)
1094 /* and check if one of the tag patterns matches the tag, */
1095 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
1097 /* if it does, update the action bit map, */
1098 if (merge_current_action(csp->action, b->action))
1100 log_error(LOG_LEVEL_ERROR,
1101 "Out of memory while changing action bits");
1103 /* and signal the change. */
1113 /*********************************************************************
1115 * Function : free_current_action
1117 * Description : Free memory used by a current_action_spec.
1118 * Does not free the current_action_spec itself.
1121 * 1 : src = Source to free.
1125 *********************************************************************/
1126 void free_current_action (struct current_action_spec *src)
1130 for (i = 0; i < ACTION_STRING_COUNT; i++)
1132 freez(src->string[i]);
1135 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1137 destroy_list(src->multi[i]);
1140 memset(src, '\0', sizeof(*src));
1144 static struct file_list *current_actions_file[MAX_AF_FILES] = {
1145 NULL, NULL, NULL, NULL, NULL,
1146 NULL, NULL, NULL, NULL, NULL
1150 #ifdef FEATURE_GRACEFUL_TERMINATION
1151 /*********************************************************************
1153 * Function : unload_current_actions_file
1155 * Description : Unloads current actions file - reset to state at
1156 * beginning of program.
1162 *********************************************************************/
1163 void unload_current_actions_file(void)
1167 for (i = 0; i < MAX_AF_FILES; i++)
1169 if (current_actions_file[i])
1171 current_actions_file[i]->unloader = unload_actions_file;
1172 current_actions_file[i] = NULL;
1176 #endif /* FEATURE_GRACEFUL_TERMINATION */
1179 /*********************************************************************
1181 * Function : unload_actions_file
1183 * Description : Unloads an actions module.
1186 * 1 : file_data = the data structure associated with the
1191 *********************************************************************/
1192 void unload_actions_file(void *file_data)
1194 struct url_actions * next;
1195 struct url_actions * cur = (struct url_actions *)file_data;
1199 free_url_spec(cur->url);
1200 if ((next == NULL) || (next->action != cur->action))
1203 * As the action settings might be shared,
1204 * we can only free them if the current
1205 * url pattern is the last one, or if the
1206 * next one is using different settings.
1208 free_action_spec(cur->action);
1216 /*********************************************************************
1218 * Function : free_alias_list
1220 * Description : Free memory used by a list of aliases.
1223 * 1 : alias_list = Linked list to free.
1227 *********************************************************************/
1228 void free_alias_list(struct action_alias *alias_list)
1230 while (alias_list != NULL)
1232 struct action_alias * next = alias_list->next;
1233 alias_list->next = NULL;
1234 freez(alias_list->name);
1235 free_action(alias_list->action);
1242 /*********************************************************************
1244 * Function : load_action_files
1246 * Description : Read and parse all the action files and add to files
1250 * 1 : csp = Current client state (buffers, headers, etc...)
1252 * Returns : 0 => Ok, everything else is an error.
1254 *********************************************************************/
1255 int load_action_files(struct client_state *csp)
1260 for (i = 0; i < MAX_AF_FILES; i++)
1262 if (csp->config->actions_file[i])
1264 result = load_one_actions_file(csp, i);
1270 else if (current_actions_file[i])
1272 current_actions_file[i]->unloader = unload_actions_file;
1273 current_actions_file[i] = NULL;
1280 /*********************************************************************
1282 * Function : load_one_actions_file
1284 * Description : Read and parse a action file and add to files
1288 * 1 : csp = Current client state (buffers, headers, etc...)
1289 * 2 : fileid = File index to load.
1291 * Returns : 0 => Ok, everything else is an error.
1293 *********************************************************************/
1294 static int load_one_actions_file(struct client_state *csp, int fileid)
1299 * Note: Keep these in the order they occur in the file, they are
1300 * sometimes tested with <=
1302 #define MODE_START_OF_FILE 1
1303 #define MODE_SETTINGS 2
1304 #define MODE_DESCRIPTION 3
1305 #define MODE_ALIAS 4
1306 #define MODE_ACTIONS 5
1308 int mode = MODE_START_OF_FILE;
1311 struct url_actions *last_perm;
1312 struct url_actions *perm;
1313 char buf[BUFFER_SIZE];
1314 struct file_list *fs;
1315 struct action_spec * cur_action = NULL;
1316 int cur_action_used = 0;
1317 struct action_alias * alias_list = NULL;
1318 unsigned long linenum = 0;
1320 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1322 /* No need to load */
1323 csp->actions_list[fileid] = current_actions_file[fileid];
1328 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1329 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1330 "with their complete file names.", csp->config->actions_file[fileid]);
1331 return 1; /* never get here */
1334 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1335 if (last_perm == NULL)
1337 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1338 csp->config->actions_file[fileid]);
1339 return 1; /* never get here */
1342 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1344 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1345 csp->config->actions_file[fileid]);
1346 return 1; /* never get here */
1349 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1353 /* It's a header block */
1356 /* It's {{settings}} or {{alias}} */
1357 size_t len = strlen(buf);
1358 char * start = buf + 2;
1359 char * end = buf + len - 1;
1360 if ((len < (size_t)5) || (*end-- != '}') || (*end-- != '}'))
1364 log_error(LOG_LEVEL_FATAL,
1365 "can't load actions file '%s': invalid line (%lu): %s",
1366 csp->config->actions_file[fileid], linenum, buf);
1367 return 1; /* never get here */
1370 /* Trim leading and trailing whitespace. */
1378 log_error(LOG_LEVEL_FATAL,
1379 "can't load actions file '%s': invalid line (%lu): {{ }}",
1380 csp->config->actions_file[fileid], linenum);
1381 return 1; /* never get here */
1385 * An actionsfile can optionally contain the following blocks.
1386 * They *MUST* be in this order, to simplify processing:
1392 * ...free text, format TBD, but no line may start with a '{'...
1397 * The actual actions must be *after* these special blocks.
1398 * None of these special blocks may be repeated.
1401 if (0 == strcmpic(start, "settings"))
1403 /* it's a {{settings}} block */
1404 if (mode >= MODE_SETTINGS)
1406 /* {{settings}} must be first thing in file and must only
1410 log_error(LOG_LEVEL_FATAL,
1411 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1412 csp->config->actions_file[fileid], linenum);
1414 mode = MODE_SETTINGS;
1416 else if (0 == strcmpic(start, "description"))
1418 /* it's a {{description}} block */
1419 if (mode >= MODE_DESCRIPTION)
1421 /* {{description}} is a singleton and only {{settings}} may proceed it
1424 log_error(LOG_LEVEL_FATAL,
1425 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1426 csp->config->actions_file[fileid], linenum);
1428 mode = MODE_DESCRIPTION;
1430 else if (0 == strcmpic(start, "alias"))
1432 /* it's an {{alias}} block */
1433 if (mode >= MODE_ALIAS)
1435 /* {{alias}} must be first thing in file, possibly after
1436 * {{settings}} and {{description}}
1438 * {{alias}} must only appear once.
1440 * Note that these are new restrictions introduced in
1441 * v2.9.10 in order to make actionsfile editing simpler.
1442 * (Otherwise, reordering actionsfile entries without
1443 * completely rewriting the file becomes non-trivial)
1446 log_error(LOG_LEVEL_FATAL,
1447 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1448 csp->config->actions_file[fileid], linenum);
1454 /* invalid {{something}} block */
1456 log_error(LOG_LEVEL_FATAL,
1457 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1458 csp->config->actions_file[fileid], linenum, start);
1459 return 1; /* never get here */
1464 /* It's an actions block */
1466 char actions_buf[BUFFER_SIZE];
1470 mode = MODE_ACTIONS;
1472 /* free old action */
1475 if (!cur_action_used)
1477 free_action_spec(cur_action);
1481 cur_action_used = 0;
1482 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1483 if (cur_action == NULL)
1486 log_error(LOG_LEVEL_FATAL,
1487 "can't load actions file '%s': out of memory",
1488 csp->config->actions_file[fileid]);
1489 return 1; /* never get here */
1491 init_action(cur_action);
1494 strlcpy(actions_buf, buf + 1, sizeof(actions_buf));
1496 /* check we have a trailing } and then trim it */
1497 end = actions_buf + strlen(actions_buf) - 1;
1502 log_error(LOG_LEVEL_FATAL,
1503 "can't load actions file '%s': invalid line (%lu): %s",
1504 csp->config->actions_file[fileid], linenum, buf);
1505 return 1; /* never get here */
1509 /* trim any whitespace immediately inside {} */
1512 if (get_actions(actions_buf, alias_list, cur_action))
1516 log_error(LOG_LEVEL_FATAL,
1517 "can't load actions file '%s': invalid line (%lu): %s",
1518 csp->config->actions_file[fileid], linenum, buf);
1519 return 1; /* never get here */
1523 else if (mode == MODE_SETTINGS)
1526 * Part of the {{settings}} block.
1527 * For now only serves to check if the file's minimum Privoxy
1528 * version requirement is met, but we may want to read & check
1529 * permissions when we go multi-user.
1531 if (!strncmp(buf, "for-privoxy-version=", 20))
1533 char *version_string, *fields[3];
1536 if ((version_string = strdup(buf + 20)) == NULL)
1539 log_error(LOG_LEVEL_FATAL,
1540 "can't load actions file '%s': out of memory!",
1541 csp->config->actions_file[fileid]);
1542 return 1; /* never get here */
1545 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1547 if (num_fields < 1 || atoi(fields[0]) == 0)
1549 log_error(LOG_LEVEL_ERROR,
1550 "While loading actions file '%s': invalid line (%lu): %s",
1551 csp->config->actions_file[fileid], linenum, buf);
1553 else if ( atoi(fields[0]) > VERSION_MAJOR
1554 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1555 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1558 log_error(LOG_LEVEL_FATAL,
1559 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1560 csp->config->actions_file[fileid], linenum, buf );
1561 return 1; /* never get here */
1563 free(version_string);
1566 else if (mode == MODE_DESCRIPTION)
1569 * Part of the {{description}} block.
1573 else if (mode == MODE_ALIAS)
1578 char actions_buf[BUFFER_SIZE];
1579 struct action_alias * new_alias;
1581 char * start = strchr(buf, '=');
1584 if ((start == NULL) || (start == buf))
1586 log_error(LOG_LEVEL_FATAL,
1587 "can't load actions file '%s': invalid alias line (%lu): %s",
1588 csp->config->actions_file[fileid], linenum, buf);
1589 return 1; /* never get here */
1592 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1595 log_error(LOG_LEVEL_FATAL,
1596 "can't load actions file '%s': out of memory!",
1597 csp->config->actions_file[fileid]);
1598 return 1; /* never get here */
1601 /* Eat any the whitespace before the '=' */
1603 while ((*end == ' ') || (*end == '\t'))
1606 * we already know we must have at least 1 non-ws char
1607 * at start of buf - no need to check
1613 /* Eat any the whitespace after the '=' */
1615 while ((*start == ' ') || (*start == '\t'))
1621 log_error(LOG_LEVEL_FATAL,
1622 "can't load actions file '%s': invalid alias line (%lu): %s",
1623 csp->config->actions_file[fileid], linenum, buf);
1624 return 1; /* never get here */
1627 if ((new_alias->name = strdup(buf)) == NULL)
1630 log_error(LOG_LEVEL_FATAL,
1631 "can't load actions file '%s': out of memory!",
1632 csp->config->actions_file[fileid]);
1633 return 1; /* never get here */
1636 strlcpy(actions_buf, start, sizeof(actions_buf));
1638 if (get_actions(actions_buf, alias_list, new_alias->action))
1642 log_error(LOG_LEVEL_FATAL,
1643 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1644 csp->config->actions_file[fileid], linenum, buf, start);
1645 return 1; /* never get here */
1649 new_alias->next = alias_list;
1650 alias_list = new_alias;
1652 else if (mode == MODE_ACTIONS)
1654 /* it's a URL pattern */
1656 /* allocate a new node */
1657 if ((perm = zalloc(sizeof(*perm))) == NULL)
1660 log_error(LOG_LEVEL_FATAL,
1661 "can't load actions file '%s': out of memory!",
1662 csp->config->actions_file[fileid]);
1663 return 1; /* never get here */
1666 perm->action = cur_action;
1667 cur_action_used = 1;
1669 /* Save the URL pattern */
1670 if (create_url_spec(perm->url, buf))
1673 log_error(LOG_LEVEL_FATAL,
1674 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1675 csp->config->actions_file[fileid], linenum, buf);
1676 return 1; /* never get here */
1679 /* add it to the list */
1680 last_perm->next = perm;
1683 else if (mode == MODE_START_OF_FILE)
1685 /* oops - please have a {} line as 1st line in file. */
1687 log_error(LOG_LEVEL_FATAL,
1688 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1689 csp->config->actions_file[fileid], linenum, buf);
1690 return 1; /* never get here */
1694 /* How did we get here? This is impossible! */
1696 log_error(LOG_LEVEL_FATAL,
1697 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1698 csp->config->actions_file[fileid], mode);
1699 return 1; /* never get here */
1705 if (!cur_action_used)
1707 free_action_spec(cur_action);
1709 free_alias_list(alias_list);
1711 /* the old one is now obsolete */
1712 if (current_actions_file[fileid])
1714 current_actions_file[fileid]->unloader = unload_actions_file;
1717 fs->next = files->next;
1719 current_actions_file[fileid] = fs;
1721 csp->actions_list[fileid] = fs;
1728 /*********************************************************************
1730 * Function : actions_to_text
1732 * Description : Converts a actionsfile entry from the internal
1733 * structure into a text line. The output is split
1734 * into one line for each action with line continuation.
1737 * 1 : action = The action to format.
1739 * Returns : A string. Caller must free it.
1740 * NULL on out-of-memory error.
1742 *********************************************************************/
1743 char * actions_to_text(const struct action_spec *action)
1745 unsigned mask = action->mask;
1746 unsigned add = action->add;
1747 char * result = strdup("");
1748 struct list_entry * lst;
1750 /* sanity - prevents "-feature +feature" */
1754 #define DEFINE_ACTION_BOOL(__name, __bit) \
1755 if (!(mask & __bit)) \
1757 string_append(&result, " -" __name " \\\n"); \
1759 else if (add & __bit) \
1761 string_append(&result, " +" __name " \\\n"); \
1764 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1765 if (!(mask & __bit)) \
1767 string_append(&result, " -" __name " \\\n"); \
1769 else if (add & __bit) \
1771 string_append(&result, " +" __name "{"); \
1772 string_append(&result, action->string[__index]); \
1773 string_append(&result, "} \\\n"); \
1776 #define DEFINE_ACTION_MULTI(__name, __index) \
1777 if (action->multi_remove_all[__index]) \
1779 string_append(&result, " -" __name " \\\n"); \
1783 lst = action->multi_remove[__index]->first; \
1786 string_append(&result, " -" __name "{"); \
1787 string_append(&result, lst->str); \
1788 string_append(&result, "} \\\n"); \
1792 lst = action->multi_add[__index]->first; \
1795 string_append(&result, " +" __name "{"); \
1796 string_append(&result, lst->str); \
1797 string_append(&result, "} \\\n"); \
1801 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1803 #include "actionlist.h"
1805 #undef DEFINE_ACTION_MULTI
1806 #undef DEFINE_ACTION_STRING
1807 #undef DEFINE_ACTION_BOOL
1808 #undef DEFINE_ACTION_ALIAS
1814 /*********************************************************************
1816 * Function : actions_to_html
1818 * Description : Converts a actionsfile entry from numeric form
1819 * ("mask" and "add") to a <br>-seperated HTML string
1820 * in which each action is linked to its chapter in
1824 * 1 : csp = Client state (for config)
1825 * 2 : action = Action spec to be converted
1827 * Returns : A string. Caller must free it.
1828 * NULL on out-of-memory error.
1830 *********************************************************************/
1831 char * actions_to_html(const struct client_state *csp,
1832 const struct action_spec *action)
1834 unsigned mask = action->mask;
1835 unsigned add = action->add;
1836 char * result = strdup("");
1837 struct list_entry * lst;
1839 /* sanity - prevents "-feature +feature" */
1843 #define DEFINE_ACTION_BOOL(__name, __bit) \
1844 if (!(mask & __bit)) \
1846 string_append(&result, "\n<br>-"); \
1847 string_join(&result, add_help_link(__name, csp->config)); \
1849 else if (add & __bit) \
1851 string_append(&result, "\n<br>+"); \
1852 string_join(&result, add_help_link(__name, csp->config)); \
1855 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1856 if (!(mask & __bit)) \
1858 string_append(&result, "\n<br>-"); \
1859 string_join(&result, add_help_link(__name, csp->config)); \
1861 else if (add & __bit) \
1863 string_append(&result, "\n<br>+"); \
1864 string_join(&result, add_help_link(__name, csp->config)); \
1865 string_append(&result, "{"); \
1866 string_join(&result, html_encode(action->string[__index])); \
1867 string_append(&result, "}"); \
1870 #define DEFINE_ACTION_MULTI(__name, __index) \
1871 if (action->multi_remove_all[__index]) \
1873 string_append(&result, "\n<br>-"); \
1874 string_join(&result, add_help_link(__name, csp->config)); \
1878 lst = action->multi_remove[__index]->first; \
1881 string_append(&result, "\n<br>-"); \
1882 string_join(&result, add_help_link(__name, csp->config)); \
1883 string_append(&result, "{"); \
1884 string_join(&result, html_encode(lst->str)); \
1885 string_append(&result, "}"); \
1889 lst = action->multi_add[__index]->first; \
1892 string_append(&result, "\n<br>+"); \
1893 string_join(&result, add_help_link(__name, csp->config)); \
1894 string_append(&result, "{"); \
1895 string_join(&result, html_encode(lst->str)); \
1896 string_append(&result, "}"); \
1900 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1902 #include "actionlist.h"
1904 #undef DEFINE_ACTION_MULTI
1905 #undef DEFINE_ACTION_STRING
1906 #undef DEFINE_ACTION_BOOL
1907 #undef DEFINE_ACTION_ALIAS
1909 /* trim leading <br> */
1910 if (result && *result)
1913 result = strdup(result + 5);
1921 /*********************************************************************
1923 * Function : current_actions_to_html
1925 * Description : Converts a curren action spec to a <br> seperated HTML
1926 * text in which each action is linked to its chapter in
1930 * 1 : csp = Client state (for config)
1931 * 2 : action = Current action spec to be converted
1933 * Returns : A string. Caller must free it.
1934 * NULL on out-of-memory error.
1936 *********************************************************************/
1937 char *current_action_to_html(const struct client_state *csp,
1938 const struct current_action_spec *action)
1940 unsigned long flags = action->flags;
1941 struct list_entry * lst;
1942 char *result = strdup("");
1943 char *active = strdup("");
1944 char *inactive = strdup("");
1946 #define DEFINE_ACTION_BOOL(__name, __bit) \
1947 if (flags & __bit) \
1949 string_append(&active, "\n<br>+"); \
1950 string_join(&active, add_help_link(__name, csp->config)); \
1954 string_append(&inactive, "\n<br>-"); \
1955 string_join(&inactive, add_help_link(__name, csp->config)); \
1958 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1959 if (flags & __bit) \
1961 string_append(&active, "\n<br>+"); \
1962 string_join(&active, add_help_link(__name, csp->config)); \
1963 string_append(&active, "{"); \
1964 string_join(&active, html_encode(action->string[__index])); \
1965 string_append(&active, "}"); \
1969 string_append(&inactive, "\n<br>-"); \
1970 string_join(&inactive, add_help_link(__name, csp->config)); \
1973 #define DEFINE_ACTION_MULTI(__name, __index) \
1974 lst = action->multi[__index]->first; \
1977 string_append(&inactive, "\n<br>-"); \
1978 string_join(&inactive, add_help_link(__name, csp->config)); \
1984 string_append(&active, "\n<br>+"); \
1985 string_join(&active, add_help_link(__name, csp->config)); \
1986 string_append(&active, "{"); \
1987 string_join(&active, html_encode(lst->str)); \
1988 string_append(&active, "}"); \
1993 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1995 #include "actionlist.h"
1997 #undef DEFINE_ACTION_MULTI
1998 #undef DEFINE_ACTION_STRING
1999 #undef DEFINE_ACTION_BOOL
2000 #undef DEFINE_ACTION_ALIAS
2004 string_append(&result, active);
2007 string_append(&result, "\n<br>");
2008 if (inactive != NULL)
2010 string_append(&result, inactive);