1 const char actions_rcs[] = "$Id: actions.c,v 1.45 2008/03/24 11:21:02 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.45 2008/03/24 11:21:02 fabiankeil
37 * Share the action settings for multiple patterns in the same
38 * section so we waste less memory for gigantic block lists
39 * (and load them slightly faster). Reported by Franz Schwartau.
41 * Revision 1.44 2008/03/04 18:30:34 fabiankeil
42 * Remove the treat-forbidden-connects-like-blocks action. We now
43 * use the "blocked" page for forbidden CONNECT requests by default.
45 * Revision 1.43 2008/03/01 14:00:43 fabiankeil
46 * Let the block action take the reason for the block
47 * as argument and show it on the "blocked" page.
49 * Revision 1.42 2008/02/09 15:15:38 fabiankeil
50 * List active and inactive actions in the show-url-info's
51 * "Final results" section separately. Patch submitted by Lee
52 * in #1830056, modified to list active actions first.
54 * Revision 1.41 2008/01/28 20:17:40 fabiankeil
55 * - Mark some parameters as immutable.
56 * - Hide update_action_bits_for_all_tags() while it's unused.
58 * Revision 1.40 2007/05/21 10:26:50 fabiankeil
59 * - Use strlcpy() instead of strcpy().
60 * - Provide a reason why loading the actions
61 * file might have failed.
63 * Revision 1.39 2007/04/17 18:21:45 fabiankeil
64 * Split update_action_bits() into
65 * update_action_bits_for_all_tags()
66 * and update_action_bits_for_tag().
68 * Revision 1.38 2007/04/15 16:39:20 fabiankeil
69 * Introduce tags as alternative way to specify which
70 * actions apply to a request. At the moment tags can be
71 * created based on client and server headers.
73 * Revision 1.37 2007/03/11 15:56:12 fabiankeil
74 * Add kludge to log unknown aliases and actions before exiting.
76 * Revision 1.36 2006/12/28 17:15:42 fabiankeil
77 * Fix gcc43 conversion warning.
79 * Revision 1.35 2006/07/18 14:48:45 david__schmidt
80 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
81 * with what was really the latest development (the v_3_0_branch branch)
83 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
84 * Multiple filter file support
86 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
87 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
88 * this becomes important when jcc.h gets included later down the line.
90 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
91 * - Implemented Privoxy version requirement through
92 * for-privoxy-version= statement in {{settings}}
94 * - Fix for unchecked out-of-memory condition
96 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
97 * Fixed memory leak reported by Dan Price in Bug #694713
99 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
100 * Extended unload_current_actions_file() to multiple AFs.
101 * Thanks to Oliver Stoeneberg for the hint.
103 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
104 * Change unsigned to unsigned long in actions_name struct. This closes
105 * SourceForge Bug #539284.
107 * Revision 1.32 2002/05/12 21:36:29 jongfoster
108 * Correcting function comments
110 * Revision 1.31 2002/05/06 07:56:50 oes
111 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
113 * Revision 1.30 2002/04/30 11:14:52 oes
114 * Made csp the first parameter in *action_to_html
116 * Revision 1.29 2002/04/26 19:30:54 jongfoster
117 * - current_action_to_html(): Adding help link for the "-" form of
118 * one-string actions.
119 * - Some actions had "<br>-", some "<br> -" (note the space).
120 * Standardizing on no space.
121 * - Greatly simplifying some of the code by using string_join()
124 * Revision 1.28 2002/04/26 12:53:15 oes
125 * - CGI AF editor now writes action lines split into
126 * single lines with line continuation
127 * - actions_to_html now embeds each action name in
129 * - current_action_to_text is now called current_action_to_html
130 * and acts like actions_to_html
132 * Revision 1.27 2002/04/24 02:10:31 oes
133 * - Jon's patch for multiple AFs:
134 * - split load_actions_file, add load_one_actions_file
135 * - make csp->actions_list files an array
136 * - remember file id with each action
137 * - Copy_action now frees dest action before copying
139 * Revision 1.26 2002/03/26 22:29:54 swa
140 * we have a new homepage!
142 * Revision 1.25 2002/03/24 13:25:43 swa
143 * name change related issues
145 * Revision 1.24 2002/03/16 23:54:06 jongfoster
146 * Adding graceful termination feature, to help look for memory leaks.
147 * If you enable this (which, by design, has to be done by hand
148 * editing config.h) and then go to http://i.j.b/die, then the program
149 * will exit cleanly after the *next* request. It should free all the
150 * memory that was used.
152 * Revision 1.23 2002/03/07 03:46:16 oes
153 * Fixed compiler warnings
155 * Revision 1.22 2002/01/21 00:27:02 jongfoster
156 * Allowing free_action(NULL).
157 * Moving the functions that #include actionlist.h to the end of the file,
158 * because the Visual C++ 97 debugger gets extremely confused if you try
159 * to debug any code that comes after them in the file.
161 * Revision 1.21 2002/01/17 20:54:44 jongfoster
162 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
164 * Revision 1.20 2001/11/22 21:56:49 jongfoster
165 * Making action_spec->flags into an unsigned long rather than just an
167 * Fixing a bug in the display of -add-header and -wafer
169 * Revision 1.19 2001/11/13 00:14:07 jongfoster
170 * Fixing stupid bug now I've figured out what || means.
171 * (It always returns 0 or 1, not one of it's paramaters.)
173 * Revision 1.18 2001/11/07 00:06:06 steudten
174 * Add line number in error output for lineparsing for
177 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
178 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
179 * threads to call select() simultaneously. So, it's time to do a real, live,
180 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
181 * (native). Both versions will work, but using __OS2__ offers multi-threading.
183 * Revision 1.16 2001/10/23 21:30:30 jongfoster
184 * Adding error-checking to selected functions.
186 * Revision 1.15 2001/10/14 21:58:22 jongfoster
187 * Adding support for the CGI-based editor:
188 * - Exported get_actions()
189 * - Added new function free_alias_list()
190 * - Added support for {{settings}} and {{description}} blocks
191 * in the actions file. They are currently ignored.
192 * - Added restriction to only one {{alias}} block which must appear
193 * first in the file, to simplify the editor's rewriting rules.
194 * - Note that load_actions_file() is no longer used by the CGI-based
195 * editor, but some of the other routines in this file are.
197 * Revision 1.14 2001/09/22 16:36:59 jongfoster
198 * Removing unused parameter fs from read_config_line()
200 * Revision 1.13 2001/09/16 15:47:37 jongfoster
201 * First version of CGI-based edit interface. This is very much a
202 * work-in-progress, and you can't actually use it to edit anything
203 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
204 * to have any effect.
206 * Revision 1.12 2001/09/16 13:21:27 jongfoster
207 * Changes to use new list functions.
209 * Revision 1.11 2001/09/14 00:17:32 jongfoster
210 * Tidying up memory allocation. New function init_action().
212 * Revision 1.10 2001/09/10 10:14:34 oes
213 * Removing unused variable
215 * Revision 1.9 2001/07/30 22:08:36 jongfoster
216 * Tidying up #defines:
217 * - All feature #defines are now of the form FEATURE_xxx
218 * - Permanently turned off WIN_GUI_EDIT
219 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
221 * Revision 1.8 2001/06/29 13:19:52 oes
222 * Removed logentry from cancelled commit
224 * Revision 1.7 2001/06/09 10:55:28 jongfoster
225 * Changing BUFSIZ ==> BUFFER_SIZE
227 * Revision 1.6 2001/06/07 23:04:34 jongfoster
228 * Made get_actions() static.
230 * Revision 1.5 2001/06/03 19:11:48 oes
231 * adapted to new enlist_unique arg format
233 * Revision 1.4 2001/06/01 20:03:42 jongfoster
234 * Better memory management - current_action->strings[] now
235 * contains copies of the strings, not the original.
237 * Revision 1.3 2001/06/01 18:49:17 jongfoster
238 * Replaced "list_share" with "list" - the tiny memory gain was not
239 * worth the extra complexity.
241 * Revision 1.2 2001/05/31 21:40:00 jongfoster
242 * Removing some commented out, obsolete blocks of code.
244 * Revision 1.1 2001/05/31 21:16:46 jongfoster
245 * Moved functions to process the action list into this new file.
248 *********************************************************************/
258 #ifdef FEATURE_PTHREAD
266 #include "miscutil.h"
270 #include "urlmatch.h"
274 const char actions_h_rcs[] = ACTIONS_H_VERSION;
278 * We need the main list of options.
280 * First, we need a way to tell between boolean, string, and multi-string
281 * options. For string and multistring options, we also need to be
282 * able to tell the difference between a "+" and a "-". (For bools,
283 * the "+"/"-" information is encoded in "add" and "mask"). So we use
284 * an enumerated type (well, the preprocessor equivalent). Here are
287 #define AV_NONE 0 /* +opt -opt */
288 #define AV_ADD_STRING 1 /* +stropt{string} */
289 #define AV_REM_STRING 2 /* -stropt */
290 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
291 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
294 * We need a structure to hold the name, flag changes,
295 * type, and string index.
300 unsigned long mask; /* a bit set to "0" = remove action */
301 unsigned long add; /* a bit set to "1" = add action */
302 int takes_value; /* an AV_... constant */
303 int index; /* index into strings[] or multi[] */
307 * And with those building blocks in place, here's the array.
309 static const struct action_name action_names[] =
312 * Well actually there's no data here - it's in actionlist.h
313 * This keeps it together to make it easy to change.
315 * Here's the macros used to format it:
317 #define DEFINE_ACTION_MULTI(name,index) \
318 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
319 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
320 #define DEFINE_ACTION_STRING(name,flag,index) \
321 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
322 { "-" name, ~flag, 0, AV_REM_STRING, index },
323 #define DEFINE_ACTION_BOOL(name,flag) \
324 { "+" name, ACTION_MASK_ALL, flag }, \
325 { "-" name, ~flag, 0 },
326 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
328 #include "actionlist.h"
330 #undef DEFINE_ACTION_MULTI
331 #undef DEFINE_ACTION_STRING
332 #undef DEFINE_ACTION_BOOL
333 #undef DEFINE_ACTION_ALIAS
335 { NULL, 0, 0 } /* End marker */
339 static int load_one_actions_file(struct client_state *csp, int fileid);
342 /*********************************************************************
344 * Function : merge_actions
346 * Description : Merge two actions together.
347 * Similar to "dest += src".
350 * 1 : dest = Actions to modify.
351 * 2 : src = Action to add.
353 * Returns : JB_ERR_OK or JB_ERR_MEMORY
355 *********************************************************************/
356 jb_err merge_actions (struct action_spec *dest,
357 const struct action_spec *src)
362 dest->mask &= src->mask;
363 dest->add &= src->mask;
364 dest->add |= src->add;
366 for (i = 0; i < ACTION_STRING_COUNT; i++)
368 char * str = src->string[i];
371 freez(dest->string[i]);
372 dest->string[i] = strdup(str);
373 if (NULL == dest->string[i])
375 return JB_ERR_MEMORY;
380 for (i = 0; i < ACTION_MULTI_COUNT; i++)
382 if (src->multi_remove_all[i])
384 /* Remove everything from dest */
385 list_remove_all(dest->multi_remove[i]);
386 dest->multi_remove_all[i] = 1;
388 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
390 else if (dest->multi_remove_all[i])
393 * dest already removes everything, so we only need to worry
396 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
397 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
401 /* No "remove all"s to worry about. */
402 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
403 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
404 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
417 /*********************************************************************
419 * Function : copy_action
421 * Description : Copy an action_specs.
422 * Similar to "dest = src".
425 * 1 : dest = Destination of copy.
426 * 2 : src = Source for copy.
430 *********************************************************************/
431 jb_err copy_action (struct action_spec *dest,
432 const struct action_spec *src)
435 jb_err err = JB_ERR_OK;
438 memset(dest, '\0', sizeof(*dest));
440 dest->mask = src->mask;
441 dest->add = src->add;
443 for (i = 0; i < ACTION_STRING_COUNT; i++)
445 char * str = src->string[i];
451 return JB_ERR_MEMORY;
453 dest->string[i] = str;
457 for (i = 0; i < ACTION_MULTI_COUNT; i++)
459 dest->multi_remove_all[i] = src->multi_remove_all[i];
460 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
465 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
474 /*********************************************************************
476 * Function : free_action_spec
478 * Description : Frees an action_spec and the memory used by it.
481 * 1 : src = Source to free.
485 *********************************************************************/
486 void free_action_spec(struct action_spec *src)
493 /*********************************************************************
495 * Function : free_action
497 * Description : Destroy an action_spec. Frees memory used by it,
498 * except for the memory used by the struct action_spec
502 * 1 : src = Source to free.
506 *********************************************************************/
507 void free_action (struct action_spec *src)
516 for (i = 0; i < ACTION_STRING_COUNT; i++)
518 freez(src->string[i]);
521 for (i = 0; i < ACTION_MULTI_COUNT; i++)
523 destroy_list(src->multi_remove[i]);
524 destroy_list(src->multi_add[i]);
527 memset(src, '\0', sizeof(*src));
531 /*********************************************************************
533 * Function : get_action_token
535 * Description : Parses a line for the first action.
536 * Modifies it's input array, doesn't allocate memory.
538 * *line=" +abc{def} -ghi "
545 * 1 : line = [in] The line containing the action.
546 * [out] Start of next action on line, or
547 * NULL if we reached the end of line before
548 * we found an action.
549 * 2 : name = [out] Start of action name, null
550 * terminated. NULL on EOL
551 * 3 : value = [out] Start of action value, null
552 * terminated. NULL if none or EOL.
554 * Returns : JB_ERR_OK => Ok
555 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
557 *********************************************************************/
558 jb_err get_action_token(char **line, char **name, char **value)
563 /* set default returns */
568 /* Eat any leading whitespace */
569 while ((*str == ' ') || (*str == '\t'))
581 /* null name, just value is prohibited */
588 while (((ch = *str) != '\0') &&
589 (ch != ' ') && (ch != '\t') && (ch != '{'))
593 /* error, '}' without '{' */
605 /* EOL - be careful not to run off buffer */
610 /* More to parse next time. */
619 str = strchr(str, '}');
636 /*********************************************************************
638 * Function : action_used_to_valid_
640 * Description : Checks if unrecognized actions were valid in earlier
644 * 1 : action = The string containing the action to check.
646 * Returns : True if yes, otherwise false.
648 *********************************************************************/
649 static int action_used_to_be_valid(const char *action)
651 return (0 == strcmpic(action, "treat-forbidden-connects-like-blocks")
652 || 0 == strcmpic(action, "kill-popups"));
655 /*********************************************************************
657 * Function : get_actions
659 * Description : Parses a list of actions.
662 * 1 : line = The string containing the actions.
663 * Will be written to by this function.
664 * 2 : alias_list = Custom alias list, or NULL for none.
665 * 3 : cur_action = Where to store the action. Caller
668 * Returns : JB_ERR_OK => Ok
669 * JB_ERR_PARSE => Parse error (line was trashed anyway)
670 * nonzero => Out of memory (line was trashed anyway)
672 *********************************************************************/
673 jb_err get_actions(char *line,
674 struct action_alias * alias_list,
675 struct action_spec *cur_action)
678 init_action(cur_action);
679 cur_action->mask = ACTION_MASK_ALL;
683 char * option = NULL;
686 err = get_action_token(&line, &option, &value);
694 /* handle option in 'option' */
696 /* Check for standard action name */
697 const struct action_name * action = action_names;
699 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
703 if (action->name != NULL)
706 cur_action->mask &= action->mask;
707 cur_action->add &= action->mask;
708 cur_action->add |= action->add;
710 switch (action->takes_value)
713 /* ignore any option. */
717 /* add single string. */
719 if ((value == NULL) || (*value == '\0'))
721 if (0 != strcmpic(action->name, "block"))
724 * XXX: Temporary backwards compatibility hack.
726 static int complaint_shown = 0;
727 value = "No reason specified.";
728 if (!complaint_shown)
730 log_error(LOG_LEVEL_ERROR, "At least one block "
731 "without reason found. This may become a fatal "
732 "error in future versions.");
741 /* FIXME: should validate option string here */
742 freez (cur_action->string[action->index]);
743 cur_action->string[action->index] = strdup(value);
744 if (NULL == cur_action->string[action->index])
746 return JB_ERR_MEMORY;
752 /* remove single string. */
754 freez (cur_action->string[action->index]);
759 /* append multi string. */
761 struct list * remove_p = cur_action->multi_remove[action->index];
762 struct list * add_p = cur_action->multi_add[action->index];
764 if ((value == NULL) || (*value == '\0'))
769 list_remove_item(remove_p, value);
770 err = enlist_unique(add_p, value, 0);
779 /* remove multi string. */
781 struct list * remove_p = cur_action->multi_remove[action->index];
782 struct list * add_p = cur_action->multi_add[action->index];
784 if ( (value == NULL) || (*value == '\0')
785 || ((*value == '*') && (value[1] == '\0')) )
788 * no option, or option == "*".
792 list_remove_all(remove_p);
793 list_remove_all(add_p);
794 cur_action->multi_remove_all[action->index] = 1;
798 /* Valid option - remove only 1 option */
800 if ( !cur_action->multi_remove_all[action->index] )
802 /* there isn't a catch-all in the remove list already */
803 err = enlist_unique(remove_p, value, 0);
809 list_remove_item(add_p, value);
814 /* Shouldn't get here unless there's memory corruption. */
821 /* try user aliases. */
822 const struct action_alias * alias = alias_list;
824 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
831 merge_actions(cur_action, alias->action);
833 else if ((2 < strlen(option)) && action_used_to_be_valid(option+1))
835 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
836 "in this Privoxy release. Ignored.", option+1);
840 /* Bad action name */
842 * XXX: This is a fatal error and Privoxy will later on exit
843 * in load_one_actions_file() because of an "invalid line".
845 * It would be preferable to name the offending option in that
846 * error message, but currently there is no way to do that and
847 * we have to live with two error messages for basically the
850 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
861 /*********************************************************************
863 * Function : init_current_action
865 * Description : Zero out an action.
868 * 1 : dest = An uninitialized current_action_spec.
872 *********************************************************************/
873 void init_current_action (struct current_action_spec *dest)
875 memset(dest, '\0', sizeof(*dest));
877 dest->flags = ACTION_MOST_COMPATIBLE;
881 /*********************************************************************
883 * Function : init_action
885 * Description : Zero out an action.
888 * 1 : dest = An uninitialized action_spec.
892 *********************************************************************/
893 void init_action (struct action_spec *dest)
895 memset(dest, '\0', sizeof(*dest));
899 /*********************************************************************
901 * Function : merge_current_action
903 * Description : Merge two actions together.
904 * Similar to "dest += src".
905 * Differences between this and merge_actions()
906 * is that this one doesn't allocate memory for
907 * strings (so "src" better be in memory for at least
908 * as long as "dest" is, and you'd better free
909 * "dest" using "free_current_action").
910 * Also, there is no mask or remove lists in dest.
911 * (If we're applying it to a URL, we don't need them)
914 * 1 : dest = Current actions, to modify.
915 * 2 : src = Action to add.
917 * Returns 0 : no error
918 * !=0 : error, probably JB_ERR_MEMORY.
920 *********************************************************************/
921 jb_err merge_current_action (struct current_action_spec *dest,
922 const struct action_spec *src)
925 jb_err err = JB_ERR_OK;
927 dest->flags &= src->mask;
928 dest->flags |= src->add;
930 for (i = 0; i < ACTION_STRING_COUNT; i++)
932 char * str = src->string[i];
938 return JB_ERR_MEMORY;
940 freez(dest->string[i]);
941 dest->string[i] = str;
945 for (i = 0; i < ACTION_MULTI_COUNT; i++)
947 if (src->multi_remove_all[i])
949 /* Remove everything from dest, then add src->multi_add */
950 err = list_duplicate(dest->multi[i], src->multi_add[i]);
958 list_remove_list(dest->multi[i], src->multi_remove[i]);
959 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
970 /*********************************************************************
972 * Function : update_action_bits_for_all_tags
974 * Description : Updates the action bits based on all matching tags.
977 * 1 : csp = Current client state (buffers, headers, etc...)
979 * Returns : 0 if no tag matched, or
982 *********************************************************************/
983 int update_action_bits_for_all_tags(struct client_state *csp)
985 struct list_entry *tag;
988 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
990 if (update_action_bits_for_tag(csp, tag->str))
1000 /*********************************************************************
1002 * Function : update_action_bits_for_tag
1004 * Description : Updates the action bits based on the action sections
1005 * whose tag patterns match a provided tag.
1008 * 1 : csp = Current client state (buffers, headers, etc...)
1009 * 2 : tag = The tag on which the update should be based on
1011 * Returns : 0 if no tag matched, or
1014 *********************************************************************/
1015 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
1017 struct file_list *fl;
1018 struct url_actions *b;
1024 assert(list_contains_item(csp->tags, tag));
1026 /* Run through all action files, */
1027 for (i = 0; i < MAX_AF_FILES; i++)
1029 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
1031 /* Skip empty files */
1035 /* and through all the action patterns, */
1036 for (b = b->next; NULL != b; b = b->next)
1038 /* skip the URL patterns, */
1039 if (NULL == b->url->tag_regex)
1044 /* and check if one of the tag patterns matches the tag, */
1045 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
1047 /* if it does, update the action bit map, */
1048 if (merge_current_action(csp->action, b->action))
1050 log_error(LOG_LEVEL_ERROR,
1051 "Out of memorey while changing action bits");
1053 /* and signal the change. */
1063 /*********************************************************************
1065 * Function : free_current_action
1067 * Description : Free memory used by a current_action_spec.
1068 * Does not free the current_action_spec itself.
1071 * 1 : src = Source to free.
1075 *********************************************************************/
1076 void free_current_action (struct current_action_spec *src)
1080 for (i = 0; i < ACTION_STRING_COUNT; i++)
1082 freez(src->string[i]);
1085 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1087 destroy_list(src->multi[i]);
1090 memset(src, '\0', sizeof(*src));
1094 static struct file_list *current_actions_file[MAX_AF_FILES] = {
1095 NULL, NULL, NULL, NULL, NULL,
1096 NULL, NULL, NULL, NULL, NULL
1100 #ifdef FEATURE_GRACEFUL_TERMINATION
1101 /*********************************************************************
1103 * Function : unload_current_actions_file
1105 * Description : Unloads current actions file - reset to state at
1106 * beginning of program.
1112 *********************************************************************/
1113 void unload_current_actions_file(void)
1117 for (i = 0; i < MAX_AF_FILES; i++)
1119 if (current_actions_file[i])
1121 current_actions_file[i]->unloader = unload_actions_file;
1122 current_actions_file[i] = NULL;
1126 #endif /* FEATURE_GRACEFUL_TERMINATION */
1129 /*********************************************************************
1131 * Function : unload_actions_file
1133 * Description : Unloads an actions module.
1136 * 1 : file_data = the data structure associated with the
1141 *********************************************************************/
1142 void unload_actions_file(void *file_data)
1144 struct url_actions * next;
1145 struct url_actions * cur = (struct url_actions *)file_data;
1149 free_url_spec(cur->url);
1150 if ((next == NULL) || (next->action != cur->action))
1153 * As the action settings might be shared,
1154 * we can only free them if the current
1155 * url pattern is the last one, or if the
1156 * next one is using different settings.
1158 free_action_spec(cur->action);
1166 /*********************************************************************
1168 * Function : free_alias_list
1170 * Description : Free memory used by a list of aliases.
1173 * 1 : alias_list = Linked list to free.
1177 *********************************************************************/
1178 void free_alias_list(struct action_alias *alias_list)
1180 while (alias_list != NULL)
1182 struct action_alias * next = alias_list->next;
1183 alias_list->next = NULL;
1184 freez(alias_list->name);
1185 free_action(alias_list->action);
1192 /*********************************************************************
1194 * Function : load_actions_file
1196 * Description : Read and parse all the action files and add to files
1200 * 1 : csp = Current client state (buffers, headers, etc...)
1202 * Returns : 0 => Ok, everything else is an error.
1204 *********************************************************************/
1205 int load_actions_file(struct client_state *csp)
1210 for (i = 0; i < MAX_AF_FILES; i++)
1212 if (csp->config->actions_file[i])
1214 result = load_one_actions_file(csp, i);
1220 else if (current_actions_file[i])
1222 current_actions_file[i]->unloader = unload_actions_file;
1223 current_actions_file[i] = NULL;
1230 /*********************************************************************
1232 * Function : load_one_actions_file
1234 * Description : Read and parse a action file and add to files
1238 * 1 : csp = Current client state (buffers, headers, etc...)
1239 * 2 : fileid = File index to load.
1241 * Returns : 0 => Ok, everything else is an error.
1243 *********************************************************************/
1244 static int load_one_actions_file(struct client_state *csp, int fileid)
1249 * Note: Keep these in the order they occur in the file, they are
1250 * sometimes tested with <=
1252 #define MODE_START_OF_FILE 1
1253 #define MODE_SETTINGS 2
1254 #define MODE_DESCRIPTION 3
1255 #define MODE_ALIAS 4
1256 #define MODE_ACTIONS 5
1258 int mode = MODE_START_OF_FILE;
1261 struct url_actions *last_perm;
1262 struct url_actions *perm;
1263 char buf[BUFFER_SIZE];
1264 struct file_list *fs;
1265 struct action_spec * cur_action = NULL;
1266 int cur_action_used = 0;
1267 struct action_alias * alias_list = NULL;
1268 unsigned long linenum = 0;
1270 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1272 /* No need to load */
1273 csp->actions_list[fileid] = current_actions_file[fileid];
1278 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1279 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1280 "with their complete file names.", csp->config->actions_file[fileid]);
1281 return 1; /* never get here */
1284 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1285 if (last_perm == NULL)
1287 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1288 csp->config->actions_file[fileid]);
1289 return 1; /* never get here */
1292 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1294 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1295 csp->config->actions_file[fileid]);
1296 return 1; /* never get here */
1299 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1303 /* It's a header block */
1306 /* It's {{settings}} or {{alias}} */
1307 size_t len = strlen(buf);
1308 char * start = buf + 2;
1309 char * end = buf + len - 1;
1310 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1314 log_error(LOG_LEVEL_FATAL,
1315 "can't load actions file '%s': invalid line (%lu): %s",
1316 csp->config->actions_file[fileid], linenum, buf);
1317 return 1; /* never get here */
1320 /* Trim leading and trailing whitespace. */
1328 log_error(LOG_LEVEL_FATAL,
1329 "can't load actions file '%s': invalid line (%lu): {{ }}",
1330 csp->config->actions_file[fileid], linenum);
1331 return 1; /* never get here */
1335 * An actionsfile can optionally contain the following blocks.
1336 * They *MUST* be in this order, to simplify processing:
1342 * ...free text, format TBD, but no line may start with a '{'...
1347 * The actual actions must be *after* these special blocks.
1348 * None of these special blocks may be repeated.
1351 if (0 == strcmpic(start, "settings"))
1353 /* it's a {{settings}} block */
1354 if (mode >= MODE_SETTINGS)
1356 /* {{settings}} must be first thing in file and must only
1360 log_error(LOG_LEVEL_FATAL,
1361 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1362 csp->config->actions_file[fileid], linenum);
1364 mode = MODE_SETTINGS;
1366 else if (0 == strcmpic(start, "description"))
1368 /* it's a {{description}} block */
1369 if (mode >= MODE_DESCRIPTION)
1371 /* {{description}} is a singleton and only {{settings}} may proceed it
1374 log_error(LOG_LEVEL_FATAL,
1375 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1376 csp->config->actions_file[fileid], linenum);
1378 mode = MODE_DESCRIPTION;
1380 else if (0 == strcmpic(start, "alias"))
1382 /* it's an {{alias}} block */
1383 if (mode >= MODE_ALIAS)
1385 /* {{alias}} must be first thing in file, possibly after
1386 * {{settings}} and {{description}}
1388 * {{alias}} must only appear once.
1390 * Note that these are new restrictions introduced in
1391 * v2.9.10 in order to make actionsfile editing simpler.
1392 * (Otherwise, reordering actionsfile entries without
1393 * completely rewriting the file becomes non-trivial)
1396 log_error(LOG_LEVEL_FATAL,
1397 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1398 csp->config->actions_file[fileid], linenum);
1404 /* invalid {{something}} block */
1406 log_error(LOG_LEVEL_FATAL,
1407 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1408 csp->config->actions_file[fileid], linenum, start);
1409 return 1; /* never get here */
1414 /* It's an actions block */
1416 char actions_buf[BUFFER_SIZE];
1420 mode = MODE_ACTIONS;
1422 /* free old action */
1425 if (!cur_action_used)
1427 free_action_spec(cur_action);
1431 cur_action_used = 0;
1432 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1433 if (cur_action == NULL)
1436 log_error(LOG_LEVEL_FATAL,
1437 "can't load actions file '%s': out of memory",
1438 csp->config->actions_file[fileid]);
1439 return 1; /* never get here */
1441 init_action(cur_action);
1444 strlcpy(actions_buf, buf + 1, sizeof(actions_buf));
1446 /* check we have a trailing } and then trim it */
1447 end = actions_buf + strlen(actions_buf) - 1;
1452 log_error(LOG_LEVEL_FATAL,
1453 "can't load actions file '%s': invalid line (%lu): %s",
1454 csp->config->actions_file[fileid], linenum, buf);
1455 return 1; /* never get here */
1459 /* trim any whitespace immediately inside {} */
1462 if (get_actions(actions_buf, alias_list, cur_action))
1466 log_error(LOG_LEVEL_FATAL,
1467 "can't load actions file '%s': invalid line (%lu): %s",
1468 csp->config->actions_file[fileid], linenum, buf);
1469 return 1; /* never get here */
1473 else if (mode == MODE_SETTINGS)
1476 * Part of the {{settings}} block.
1477 * For now only serves to check if the file's minimum Privoxy
1478 * version requirement is met, but we may want to read & check
1479 * permissions when we go multi-user.
1481 if (!strncmp(buf, "for-privoxy-version=", 20))
1483 char *version_string, *fields[3];
1486 if ((version_string = strdup(buf + 20)) == NULL)
1489 log_error(LOG_LEVEL_FATAL,
1490 "can't load actions file '%s': out of memory!",
1491 csp->config->actions_file[fileid]);
1492 return 1; /* never get here */
1495 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1497 if (num_fields < 1 || atoi(fields[0]) == 0)
1499 log_error(LOG_LEVEL_ERROR,
1500 "While loading actions file '%s': invalid line (%lu): %s",
1501 csp->config->actions_file[fileid], linenum, buf);
1503 else if ( atoi(fields[0]) > VERSION_MAJOR
1504 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1505 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1508 log_error(LOG_LEVEL_FATAL,
1509 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1510 csp->config->actions_file[fileid], linenum, buf );
1511 return 1; /* never get here */
1513 free(version_string);
1516 else if (mode == MODE_DESCRIPTION)
1519 * Part of the {{description}} block.
1523 else if (mode == MODE_ALIAS)
1528 char actions_buf[BUFFER_SIZE];
1529 struct action_alias * new_alias;
1531 char * start = strchr(buf, '=');
1534 if ((start == NULL) || (start == buf))
1536 log_error(LOG_LEVEL_FATAL,
1537 "can't load actions file '%s': invalid alias line (%lu): %s",
1538 csp->config->actions_file[fileid], linenum, buf);
1539 return 1; /* never get here */
1542 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1545 log_error(LOG_LEVEL_FATAL,
1546 "can't load actions file '%s': out of memory!",
1547 csp->config->actions_file[fileid]);
1548 return 1; /* never get here */
1551 /* Eat any the whitespace before the '=' */
1553 while ((*end == ' ') || (*end == '\t'))
1556 * we already know we must have at least 1 non-ws char
1557 * at start of buf - no need to check
1563 /* Eat any the whitespace after the '=' */
1565 while ((*start == ' ') || (*start == '\t'))
1571 log_error(LOG_LEVEL_FATAL,
1572 "can't load actions file '%s': invalid alias line (%lu): %s",
1573 csp->config->actions_file[fileid], linenum, buf);
1574 return 1; /* never get here */
1577 if ((new_alias->name = strdup(buf)) == NULL)
1580 log_error(LOG_LEVEL_FATAL,
1581 "can't load actions file '%s': out of memory!",
1582 csp->config->actions_file[fileid]);
1583 return 1; /* never get here */
1586 strlcpy(actions_buf, start, sizeof(actions_buf));
1588 if (get_actions(actions_buf, alias_list, new_alias->action))
1592 log_error(LOG_LEVEL_FATAL,
1593 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1594 csp->config->actions_file[fileid], linenum, buf, start);
1595 return 1; /* never get here */
1599 new_alias->next = alias_list;
1600 alias_list = new_alias;
1602 else if (mode == MODE_ACTIONS)
1604 /* it's a URL pattern */
1606 /* allocate a new node */
1607 if ((perm = zalloc(sizeof(*perm))) == NULL)
1610 log_error(LOG_LEVEL_FATAL,
1611 "can't load actions file '%s': out of memory!",
1612 csp->config->actions_file[fileid]);
1613 return 1; /* never get here */
1616 perm->action = cur_action;
1617 cur_action_used = 1;
1619 /* Save the URL pattern */
1620 if (create_url_spec(perm->url, buf))
1623 log_error(LOG_LEVEL_FATAL,
1624 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1625 csp->config->actions_file[fileid], linenum, buf);
1626 return 1; /* never get here */
1629 /* add it to the list */
1630 last_perm->next = perm;
1633 else if (mode == MODE_START_OF_FILE)
1635 /* oops - please have a {} line as 1st line in file. */
1637 log_error(LOG_LEVEL_FATAL,
1638 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1639 csp->config->actions_file[fileid], linenum, buf);
1640 return 1; /* never get here */
1644 /* How did we get here? This is impossible! */
1646 log_error(LOG_LEVEL_FATAL,
1647 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1648 csp->config->actions_file[fileid], mode);
1649 return 1; /* never get here */
1655 if (!cur_action_used)
1657 free_action_spec(cur_action);
1659 free_alias_list(alias_list);
1661 /* the old one is now obsolete */
1662 if (current_actions_file[fileid])
1664 current_actions_file[fileid]->unloader = unload_actions_file;
1667 fs->next = files->next;
1669 current_actions_file[fileid] = fs;
1671 csp->actions_list[fileid] = fs;
1678 /*********************************************************************
1680 * Function : actions_to_text
1682 * Description : Converts a actionsfile entry from the internal
1683 * structure into a text line. The output is split
1684 * into one line for each action with line continuation.
1687 * 1 : action = The action to format.
1689 * Returns : A string. Caller must free it.
1690 * NULL on out-of-memory error.
1692 *********************************************************************/
1693 char * actions_to_text(const struct action_spec *action)
1695 unsigned mask = action->mask;
1696 unsigned add = action->add;
1697 char * result = strdup("");
1698 struct list_entry * lst;
1700 /* sanity - prevents "-feature +feature" */
1704 #define DEFINE_ACTION_BOOL(__name, __bit) \
1705 if (!(mask & __bit)) \
1707 string_append(&result, " -" __name " \\\n"); \
1709 else if (add & __bit) \
1711 string_append(&result, " +" __name " \\\n"); \
1714 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1715 if (!(mask & __bit)) \
1717 string_append(&result, " -" __name " \\\n"); \
1719 else if (add & __bit) \
1721 string_append(&result, " +" __name "{"); \
1722 string_append(&result, action->string[__index]); \
1723 string_append(&result, "} \\\n"); \
1726 #define DEFINE_ACTION_MULTI(__name, __index) \
1727 if (action->multi_remove_all[__index]) \
1729 string_append(&result, " -" __name " \\\n"); \
1733 lst = action->multi_remove[__index]->first; \
1736 string_append(&result, " -" __name "{"); \
1737 string_append(&result, lst->str); \
1738 string_append(&result, "} \\\n"); \
1742 lst = action->multi_add[__index]->first; \
1745 string_append(&result, " +" __name "{"); \
1746 string_append(&result, lst->str); \
1747 string_append(&result, "} \\\n"); \
1751 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1753 #include "actionlist.h"
1755 #undef DEFINE_ACTION_MULTI
1756 #undef DEFINE_ACTION_STRING
1757 #undef DEFINE_ACTION_BOOL
1758 #undef DEFINE_ACTION_ALIAS
1764 /*********************************************************************
1766 * Function : actions_to_html
1768 * Description : Converts a actionsfile entry from numeric form
1769 * ("mask" and "add") to a <br>-seperated HTML string
1770 * in which each action is linked to its chapter in
1774 * 1 : csp = Client state (for config)
1775 * 2 : action = Action spec to be converted
1777 * Returns : A string. Caller must free it.
1778 * NULL on out-of-memory error.
1780 *********************************************************************/
1781 char * actions_to_html(const struct client_state *csp,
1782 const struct action_spec *action)
1784 unsigned mask = action->mask;
1785 unsigned add = action->add;
1786 char * result = strdup("");
1787 struct list_entry * lst;
1789 /* sanity - prevents "-feature +feature" */
1793 #define DEFINE_ACTION_BOOL(__name, __bit) \
1794 if (!(mask & __bit)) \
1796 string_append(&result, "\n<br>-"); \
1797 string_join(&result, add_help_link(__name, csp->config)); \
1799 else if (add & __bit) \
1801 string_append(&result, "\n<br>+"); \
1802 string_join(&result, add_help_link(__name, csp->config)); \
1805 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1806 if (!(mask & __bit)) \
1808 string_append(&result, "\n<br>-"); \
1809 string_join(&result, add_help_link(__name, csp->config)); \
1811 else if (add & __bit) \
1813 string_append(&result, "\n<br>+"); \
1814 string_join(&result, add_help_link(__name, csp->config)); \
1815 string_append(&result, "{"); \
1816 string_join(&result, html_encode(action->string[__index])); \
1817 string_append(&result, "}"); \
1820 #define DEFINE_ACTION_MULTI(__name, __index) \
1821 if (action->multi_remove_all[__index]) \
1823 string_append(&result, "\n<br>-"); \
1824 string_join(&result, add_help_link(__name, csp->config)); \
1828 lst = action->multi_remove[__index]->first; \
1831 string_append(&result, "\n<br>-"); \
1832 string_join(&result, add_help_link(__name, csp->config)); \
1833 string_append(&result, "{"); \
1834 string_join(&result, html_encode(lst->str)); \
1835 string_append(&result, "}"); \
1839 lst = action->multi_add[__index]->first; \
1842 string_append(&result, "\n<br>+"); \
1843 string_join(&result, add_help_link(__name, csp->config)); \
1844 string_append(&result, "{"); \
1845 string_join(&result, html_encode(lst->str)); \
1846 string_append(&result, "}"); \
1850 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1852 #include "actionlist.h"
1854 #undef DEFINE_ACTION_MULTI
1855 #undef DEFINE_ACTION_STRING
1856 #undef DEFINE_ACTION_BOOL
1857 #undef DEFINE_ACTION_ALIAS
1859 /* trim leading <br> */
1860 if (result && *result)
1863 result = strdup(result + 5);
1871 /*********************************************************************
1873 * Function : current_actions_to_html
1875 * Description : Converts a curren action spec to a <br> seperated HTML
1876 * text in which each action is linked to its chapter in
1880 * 1 : csp = Client state (for config)
1881 * 2 : action = Current action spec to be converted
1883 * Returns : A string. Caller must free it.
1884 * NULL on out-of-memory error.
1886 *********************************************************************/
1887 char *current_action_to_html(const struct client_state *csp,
1888 const struct current_action_spec *action)
1890 unsigned long flags = action->flags;
1891 struct list_entry * lst;
1892 char *result = strdup("");
1893 char *active = strdup("");
1894 char *inactive = strdup("");
1896 #define DEFINE_ACTION_BOOL(__name, __bit) \
1897 if (flags & __bit) \
1899 string_append(&active, "\n<br>+"); \
1900 string_join(&active, add_help_link(__name, csp->config)); \
1904 string_append(&inactive, "\n<br>-"); \
1905 string_join(&inactive, add_help_link(__name, csp->config)); \
1908 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1909 if (flags & __bit) \
1911 string_append(&active, "\n<br>+"); \
1912 string_join(&active, add_help_link(__name, csp->config)); \
1913 string_append(&active, "{"); \
1914 string_join(&active, html_encode(action->string[__index])); \
1915 string_append(&active, "}"); \
1919 string_append(&inactive, "\n<br>-"); \
1920 string_join(&inactive, add_help_link(__name, csp->config)); \
1923 #define DEFINE_ACTION_MULTI(__name, __index) \
1924 lst = action->multi[__index]->first; \
1927 string_append(&inactive, "\n<br>-"); \
1928 string_join(&inactive, add_help_link(__name, csp->config)); \
1934 string_append(&active, "\n<br>+"); \
1935 string_join(&active, add_help_link(__name, csp->config)); \
1936 string_append(&active, "{"); \
1937 string_join(&active, html_encode(lst->str)); \
1938 string_append(&active, "}"); \
1943 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1945 #include "actionlist.h"
1947 #undef DEFINE_ACTION_MULTI
1948 #undef DEFINE_ACTION_STRING
1949 #undef DEFINE_ACTION_BOOL
1950 #undef DEFINE_ACTION_ALIAS
1954 string_append(&result, active);
1957 string_append(&result, "\n<br>");
1958 if (inactive != NULL)
1960 string_append(&result, inactive);