1 const char actions_rcs[] = "$Id: actions.c,v 1.50 2008/03/30 14:52:00 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.50 2008/03/30 14:52:00 fabiankeil
37 * Rename load_actions_file() and load_re_filterfile()
38 * as they load multiple files "now".
40 * Revision 1.49 2008/03/29 12:13:45 fabiankeil
41 * Remove send-wafer and send-vanilla-wafer actions.
43 * Revision 1.48 2008/03/28 18:17:14 fabiankeil
44 * In action_used_to_be_valid(), loop through an array of formerly
45 * valid actions instead of using an OR-chain of strcmpic() calls.
47 * Revision 1.47 2008/03/28 15:13:37 fabiankeil
48 * Remove inspect-jpegs action.
50 * Revision 1.46 2008/03/27 18:27:20 fabiankeil
51 * Remove kill-popups action.
53 * Revision 1.45 2008/03/24 11:21:02 fabiankeil
54 * Share the action settings for multiple patterns in the same
55 * section so we waste less memory for gigantic block lists
56 * (and load them slightly faster). Reported by Franz Schwartau.
58 * Revision 1.44 2008/03/04 18:30:34 fabiankeil
59 * Remove the treat-forbidden-connects-like-blocks action. We now
60 * use the "blocked" page for forbidden CONNECT requests by default.
62 * Revision 1.43 2008/03/01 14:00:43 fabiankeil
63 * Let the block action take the reason for the block
64 * as argument and show it on the "blocked" page.
66 * Revision 1.42 2008/02/09 15:15:38 fabiankeil
67 * List active and inactive actions in the show-url-info's
68 * "Final results" section separately. Patch submitted by Lee
69 * in #1830056, modified to list active actions first.
71 * Revision 1.41 2008/01/28 20:17:40 fabiankeil
72 * - Mark some parameters as immutable.
73 * - Hide update_action_bits_for_all_tags() while it's unused.
75 * Revision 1.40 2007/05/21 10:26:50 fabiankeil
76 * - Use strlcpy() instead of strcpy().
77 * - Provide a reason why loading the actions
78 * file might have failed.
80 * Revision 1.39 2007/04/17 18:21:45 fabiankeil
81 * Split update_action_bits() into
82 * update_action_bits_for_all_tags()
83 * and update_action_bits_for_tag().
85 * Revision 1.38 2007/04/15 16:39:20 fabiankeil
86 * Introduce tags as alternative way to specify which
87 * actions apply to a request. At the moment tags can be
88 * created based on client and server headers.
90 * Revision 1.37 2007/03/11 15:56:12 fabiankeil
91 * Add kludge to log unknown aliases and actions before exiting.
93 * Revision 1.36 2006/12/28 17:15:42 fabiankeil
94 * Fix gcc43 conversion warning.
96 * Revision 1.35 2006/07/18 14:48:45 david__schmidt
97 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
98 * with what was really the latest development (the v_3_0_branch branch)
100 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
101 * Multiple filter file support
103 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
104 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
105 * this becomes important when jcc.h gets included later down the line.
107 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
108 * - Implemented Privoxy version requirement through
109 * for-privoxy-version= statement in {{settings}}
111 * - Fix for unchecked out-of-memory condition
113 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
114 * Fixed memory leak reported by Dan Price in Bug #694713
116 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
117 * Extended unload_current_actions_file() to multiple AFs.
118 * Thanks to Oliver Stoeneberg for the hint.
120 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
121 * Change unsigned to unsigned long in actions_name struct. This closes
122 * SourceForge Bug #539284.
124 * Revision 1.32 2002/05/12 21:36:29 jongfoster
125 * Correcting function comments
127 * Revision 1.31 2002/05/06 07:56:50 oes
128 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
130 * Revision 1.30 2002/04/30 11:14:52 oes
131 * Made csp the first parameter in *action_to_html
133 * Revision 1.29 2002/04/26 19:30:54 jongfoster
134 * - current_action_to_html(): Adding help link for the "-" form of
135 * one-string actions.
136 * - Some actions had "<br>-", some "<br> -" (note the space).
137 * Standardizing on no space.
138 * - Greatly simplifying some of the code by using string_join()
141 * Revision 1.28 2002/04/26 12:53:15 oes
142 * - CGI AF editor now writes action lines split into
143 * single lines with line continuation
144 * - actions_to_html now embeds each action name in
146 * - current_action_to_text is now called current_action_to_html
147 * and acts like actions_to_html
149 * Revision 1.27 2002/04/24 02:10:31 oes
150 * - Jon's patch for multiple AFs:
151 * - split load_actions_file, add load_one_actions_file
152 * - make csp->actions_list files an array
153 * - remember file id with each action
154 * - Copy_action now frees dest action before copying
156 * Revision 1.26 2002/03/26 22:29:54 swa
157 * we have a new homepage!
159 * Revision 1.25 2002/03/24 13:25:43 swa
160 * name change related issues
162 * Revision 1.24 2002/03/16 23:54:06 jongfoster
163 * Adding graceful termination feature, to help look for memory leaks.
164 * If you enable this (which, by design, has to be done by hand
165 * editing config.h) and then go to http://i.j.b/die, then the program
166 * will exit cleanly after the *next* request. It should free all the
167 * memory that was used.
169 * Revision 1.23 2002/03/07 03:46:16 oes
170 * Fixed compiler warnings
172 * Revision 1.22 2002/01/21 00:27:02 jongfoster
173 * Allowing free_action(NULL).
174 * Moving the functions that #include actionlist.h to the end of the file,
175 * because the Visual C++ 97 debugger gets extremely confused if you try
176 * to debug any code that comes after them in the file.
178 * Revision 1.21 2002/01/17 20:54:44 jongfoster
179 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
181 * Revision 1.20 2001/11/22 21:56:49 jongfoster
182 * Making action_spec->flags into an unsigned long rather than just an
184 * Fixing a bug in the display of -add-header and -wafer
186 * Revision 1.19 2001/11/13 00:14:07 jongfoster
187 * Fixing stupid bug now I've figured out what || means.
188 * (It always returns 0 or 1, not one of it's paramaters.)
190 * Revision 1.18 2001/11/07 00:06:06 steudten
191 * Add line number in error output for lineparsing for
194 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
195 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
196 * threads to call select() simultaneously. So, it's time to do a real, live,
197 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
198 * (native). Both versions will work, but using __OS2__ offers multi-threading.
200 * Revision 1.16 2001/10/23 21:30:30 jongfoster
201 * Adding error-checking to selected functions.
203 * Revision 1.15 2001/10/14 21:58:22 jongfoster
204 * Adding support for the CGI-based editor:
205 * - Exported get_actions()
206 * - Added new function free_alias_list()
207 * - Added support for {{settings}} and {{description}} blocks
208 * in the actions file. They are currently ignored.
209 * - Added restriction to only one {{alias}} block which must appear
210 * first in the file, to simplify the editor's rewriting rules.
211 * - Note that load_actions_file() is no longer used by the CGI-based
212 * editor, but some of the other routines in this file are.
214 * Revision 1.14 2001/09/22 16:36:59 jongfoster
215 * Removing unused parameter fs from read_config_line()
217 * Revision 1.13 2001/09/16 15:47:37 jongfoster
218 * First version of CGI-based edit interface. This is very much a
219 * work-in-progress, and you can't actually use it to edit anything
220 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
221 * to have any effect.
223 * Revision 1.12 2001/09/16 13:21:27 jongfoster
224 * Changes to use new list functions.
226 * Revision 1.11 2001/09/14 00:17:32 jongfoster
227 * Tidying up memory allocation. New function init_action().
229 * Revision 1.10 2001/09/10 10:14:34 oes
230 * Removing unused variable
232 * Revision 1.9 2001/07/30 22:08:36 jongfoster
233 * Tidying up #defines:
234 * - All feature #defines are now of the form FEATURE_xxx
235 * - Permanently turned off WIN_GUI_EDIT
236 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
238 * Revision 1.8 2001/06/29 13:19:52 oes
239 * Removed logentry from cancelled commit
241 * Revision 1.7 2001/06/09 10:55:28 jongfoster
242 * Changing BUFSIZ ==> BUFFER_SIZE
244 * Revision 1.6 2001/06/07 23:04:34 jongfoster
245 * Made get_actions() static.
247 * Revision 1.5 2001/06/03 19:11:48 oes
248 * adapted to new enlist_unique arg format
250 * Revision 1.4 2001/06/01 20:03:42 jongfoster
251 * Better memory management - current_action->strings[] now
252 * contains copies of the strings, not the original.
254 * Revision 1.3 2001/06/01 18:49:17 jongfoster
255 * Replaced "list_share" with "list" - the tiny memory gain was not
256 * worth the extra complexity.
258 * Revision 1.2 2001/05/31 21:40:00 jongfoster
259 * Removing some commented out, obsolete blocks of code.
261 * Revision 1.1 2001/05/31 21:16:46 jongfoster
262 * Moved functions to process the action list into this new file.
265 *********************************************************************/
275 #ifdef FEATURE_PTHREAD
283 #include "miscutil.h"
287 #include "urlmatch.h"
291 const char actions_h_rcs[] = ACTIONS_H_VERSION;
295 * We need the main list of options.
297 * First, we need a way to tell between boolean, string, and multi-string
298 * options. For string and multistring options, we also need to be
299 * able to tell the difference between a "+" and a "-". (For bools,
300 * the "+"/"-" information is encoded in "add" and "mask"). So we use
301 * an enumerated type (well, the preprocessor equivalent). Here are
304 #define AV_NONE 0 /* +opt -opt */
305 #define AV_ADD_STRING 1 /* +stropt{string} */
306 #define AV_REM_STRING 2 /* -stropt */
307 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
308 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
311 * We need a structure to hold the name, flag changes,
312 * type, and string index.
317 unsigned long mask; /* a bit set to "0" = remove action */
318 unsigned long add; /* a bit set to "1" = add action */
319 int takes_value; /* an AV_... constant */
320 int index; /* index into strings[] or multi[] */
324 * And with those building blocks in place, here's the array.
326 static const struct action_name action_names[] =
329 * Well actually there's no data here - it's in actionlist.h
330 * This keeps it together to make it easy to change.
332 * Here's the macros used to format it:
334 #define DEFINE_ACTION_MULTI(name,index) \
335 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
336 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
337 #define DEFINE_ACTION_STRING(name,flag,index) \
338 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
339 { "-" name, ~flag, 0, AV_REM_STRING, index },
340 #define DEFINE_ACTION_BOOL(name,flag) \
341 { "+" name, ACTION_MASK_ALL, flag }, \
342 { "-" name, ~flag, 0 },
343 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
345 #include "actionlist.h"
347 #undef DEFINE_ACTION_MULTI
348 #undef DEFINE_ACTION_STRING
349 #undef DEFINE_ACTION_BOOL
350 #undef DEFINE_ACTION_ALIAS
352 { NULL, 0, 0 } /* End marker */
356 static int load_one_actions_file(struct client_state *csp, int fileid);
359 /*********************************************************************
361 * Function : merge_actions
363 * Description : Merge two actions together.
364 * Similar to "dest += src".
367 * 1 : dest = Actions to modify.
368 * 2 : src = Action to add.
370 * Returns : JB_ERR_OK or JB_ERR_MEMORY
372 *********************************************************************/
373 jb_err merge_actions (struct action_spec *dest,
374 const struct action_spec *src)
379 dest->mask &= src->mask;
380 dest->add &= src->mask;
381 dest->add |= src->add;
383 for (i = 0; i < ACTION_STRING_COUNT; i++)
385 char * str = src->string[i];
388 freez(dest->string[i]);
389 dest->string[i] = strdup(str);
390 if (NULL == dest->string[i])
392 return JB_ERR_MEMORY;
397 for (i = 0; i < ACTION_MULTI_COUNT; i++)
399 if (src->multi_remove_all[i])
401 /* Remove everything from dest */
402 list_remove_all(dest->multi_remove[i]);
403 dest->multi_remove_all[i] = 1;
405 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
407 else if (dest->multi_remove_all[i])
410 * dest already removes everything, so we only need to worry
413 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
414 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
418 /* No "remove all"s to worry about. */
419 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
420 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
421 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
434 /*********************************************************************
436 * Function : copy_action
438 * Description : Copy an action_specs.
439 * Similar to "dest = src".
442 * 1 : dest = Destination of copy.
443 * 2 : src = Source for copy.
447 *********************************************************************/
448 jb_err copy_action (struct action_spec *dest,
449 const struct action_spec *src)
452 jb_err err = JB_ERR_OK;
455 memset(dest, '\0', sizeof(*dest));
457 dest->mask = src->mask;
458 dest->add = src->add;
460 for (i = 0; i < ACTION_STRING_COUNT; i++)
462 char * str = src->string[i];
468 return JB_ERR_MEMORY;
470 dest->string[i] = str;
474 for (i = 0; i < ACTION_MULTI_COUNT; i++)
476 dest->multi_remove_all[i] = src->multi_remove_all[i];
477 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
482 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
491 /*********************************************************************
493 * Function : free_action_spec
495 * Description : Frees an action_spec and the memory used by it.
498 * 1 : src = Source to free.
502 *********************************************************************/
503 void free_action_spec(struct action_spec *src)
510 /*********************************************************************
512 * Function : free_action
514 * Description : Destroy an action_spec. Frees memory used by it,
515 * except for the memory used by the struct action_spec
519 * 1 : src = Source to free.
523 *********************************************************************/
524 void free_action (struct action_spec *src)
533 for (i = 0; i < ACTION_STRING_COUNT; i++)
535 freez(src->string[i]);
538 for (i = 0; i < ACTION_MULTI_COUNT; i++)
540 destroy_list(src->multi_remove[i]);
541 destroy_list(src->multi_add[i]);
544 memset(src, '\0', sizeof(*src));
548 /*********************************************************************
550 * Function : get_action_token
552 * Description : Parses a line for the first action.
553 * Modifies it's input array, doesn't allocate memory.
555 * *line=" +abc{def} -ghi "
562 * 1 : line = [in] The line containing the action.
563 * [out] Start of next action on line, or
564 * NULL if we reached the end of line before
565 * we found an action.
566 * 2 : name = [out] Start of action name, null
567 * terminated. NULL on EOL
568 * 3 : value = [out] Start of action value, null
569 * terminated. NULL if none or EOL.
571 * Returns : JB_ERR_OK => Ok
572 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
574 *********************************************************************/
575 jb_err get_action_token(char **line, char **name, char **value)
580 /* set default returns */
585 /* Eat any leading whitespace */
586 while ((*str == ' ') || (*str == '\t'))
598 /* null name, just value is prohibited */
605 while (((ch = *str) != '\0') &&
606 (ch != ' ') && (ch != '\t') && (ch != '{'))
610 /* error, '}' without '{' */
622 /* EOL - be careful not to run off buffer */
627 /* More to parse next time. */
636 str = strchr(str, '}');
653 /*********************************************************************
655 * Function : action_used_to_be_valid
657 * Description : Checks if unrecognized actions were valid in earlier
661 * 1 : action = The string containing the action to check.
663 * Returns : True if yes, otherwise false.
665 *********************************************************************/
666 static int action_used_to_be_valid(const char *action)
668 static const char *formerly_valid_actions[] = {
671 "send-vanilla-wafer",
673 "treat-forbidden-connects-like-blocks",
679 for (i = 0; i < SZ(formerly_valid_actions); i++)
681 if (0 == strcmpic(action, formerly_valid_actions[i]))
690 /*********************************************************************
692 * Function : get_actions
694 * Description : Parses a list of actions.
697 * 1 : line = The string containing the actions.
698 * Will be written to by this function.
699 * 2 : alias_list = Custom alias list, or NULL for none.
700 * 3 : cur_action = Where to store the action. Caller
703 * Returns : JB_ERR_OK => Ok
704 * JB_ERR_PARSE => Parse error (line was trashed anyway)
705 * nonzero => Out of memory (line was trashed anyway)
707 *********************************************************************/
708 jb_err get_actions(char *line,
709 struct action_alias * alias_list,
710 struct action_spec *cur_action)
713 init_action(cur_action);
714 cur_action->mask = ACTION_MASK_ALL;
718 char * option = NULL;
721 err = get_action_token(&line, &option, &value);
729 /* handle option in 'option' */
731 /* Check for standard action name */
732 const struct action_name * action = action_names;
734 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
738 if (action->name != NULL)
741 cur_action->mask &= action->mask;
742 cur_action->add &= action->mask;
743 cur_action->add |= action->add;
745 switch (action->takes_value)
748 /* ignore any option. */
752 /* add single string. */
754 if ((value == NULL) || (*value == '\0'))
756 if (0 != strcmpic(action->name, "block"))
759 * XXX: Temporary backwards compatibility hack.
760 * XXX: should include line number.
762 value = "No reason specified.";
763 log_error(LOG_LEVEL_ERROR, "block action without "
764 "reason found. This may become a fatal error"
765 "in future versions.");
772 /* FIXME: should validate option string here */
773 freez (cur_action->string[action->index]);
774 cur_action->string[action->index] = strdup(value);
775 if (NULL == cur_action->string[action->index])
777 return JB_ERR_MEMORY;
783 /* remove single string. */
785 freez (cur_action->string[action->index]);
790 /* append multi string. */
792 struct list * remove_p = cur_action->multi_remove[action->index];
793 struct list * add_p = cur_action->multi_add[action->index];
795 if ((value == NULL) || (*value == '\0'))
800 list_remove_item(remove_p, value);
801 err = enlist_unique(add_p, value, 0);
810 /* remove multi string. */
812 struct list * remove_p = cur_action->multi_remove[action->index];
813 struct list * add_p = cur_action->multi_add[action->index];
815 if ( (value == NULL) || (*value == '\0')
816 || ((*value == '*') && (value[1] == '\0')) )
819 * no option, or option == "*".
823 list_remove_all(remove_p);
824 list_remove_all(add_p);
825 cur_action->multi_remove_all[action->index] = 1;
829 /* Valid option - remove only 1 option */
831 if ( !cur_action->multi_remove_all[action->index] )
833 /* there isn't a catch-all in the remove list already */
834 err = enlist_unique(remove_p, value, 0);
840 list_remove_item(add_p, value);
845 /* Shouldn't get here unless there's memory corruption. */
852 /* try user aliases. */
853 const struct action_alias * alias = alias_list;
855 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
862 merge_actions(cur_action, alias->action);
864 else if ((2 < strlen(option)) && action_used_to_be_valid(option+1))
866 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
867 "in this Privoxy release. Ignored.", option+1);
871 /* Bad action name */
873 * XXX: This is a fatal error and Privoxy will later on exit
874 * in load_one_actions_file() because of an "invalid line".
876 * It would be preferable to name the offending option in that
877 * error message, but currently there is no way to do that and
878 * we have to live with two error messages for basically the
881 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
892 /*********************************************************************
894 * Function : init_current_action
896 * Description : Zero out an action.
899 * 1 : dest = An uninitialized current_action_spec.
903 *********************************************************************/
904 void init_current_action (struct current_action_spec *dest)
906 memset(dest, '\0', sizeof(*dest));
908 dest->flags = ACTION_MOST_COMPATIBLE;
912 /*********************************************************************
914 * Function : init_action
916 * Description : Zero out an action.
919 * 1 : dest = An uninitialized action_spec.
923 *********************************************************************/
924 void init_action (struct action_spec *dest)
926 memset(dest, '\0', sizeof(*dest));
930 /*********************************************************************
932 * Function : merge_current_action
934 * Description : Merge two actions together.
935 * Similar to "dest += src".
936 * Differences between this and merge_actions()
937 * is that this one doesn't allocate memory for
938 * strings (so "src" better be in memory for at least
939 * as long as "dest" is, and you'd better free
940 * "dest" using "free_current_action").
941 * Also, there is no mask or remove lists in dest.
942 * (If we're applying it to a URL, we don't need them)
945 * 1 : dest = Current actions, to modify.
946 * 2 : src = Action to add.
948 * Returns 0 : no error
949 * !=0 : error, probably JB_ERR_MEMORY.
951 *********************************************************************/
952 jb_err merge_current_action (struct current_action_spec *dest,
953 const struct action_spec *src)
956 jb_err err = JB_ERR_OK;
958 dest->flags &= src->mask;
959 dest->flags |= src->add;
961 for (i = 0; i < ACTION_STRING_COUNT; i++)
963 char * str = src->string[i];
969 return JB_ERR_MEMORY;
971 freez(dest->string[i]);
972 dest->string[i] = str;
976 for (i = 0; i < ACTION_MULTI_COUNT; i++)
978 if (src->multi_remove_all[i])
980 /* Remove everything from dest, then add src->multi_add */
981 err = list_duplicate(dest->multi[i], src->multi_add[i]);
989 list_remove_list(dest->multi[i], src->multi_remove[i]);
990 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
1001 /*********************************************************************
1003 * Function : update_action_bits_for_all_tags
1005 * Description : Updates the action bits based on all matching tags.
1008 * 1 : csp = Current client state (buffers, headers, etc...)
1010 * Returns : 0 if no tag matched, or
1013 *********************************************************************/
1014 int update_action_bits_for_all_tags(struct client_state *csp)
1016 struct list_entry *tag;
1019 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
1021 if (update_action_bits_for_tag(csp, tag->str))
1031 /*********************************************************************
1033 * Function : update_action_bits_for_tag
1035 * Description : Updates the action bits based on the action sections
1036 * whose tag patterns match a provided tag.
1039 * 1 : csp = Current client state (buffers, headers, etc...)
1040 * 2 : tag = The tag on which the update should be based on
1042 * Returns : 0 if no tag matched, or
1045 *********************************************************************/
1046 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
1048 struct file_list *fl;
1049 struct url_actions *b;
1055 assert(list_contains_item(csp->tags, tag));
1057 /* Run through all action files, */
1058 for (i = 0; i < MAX_AF_FILES; i++)
1060 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
1062 /* Skip empty files */
1066 /* and through all the action patterns, */
1067 for (b = b->next; NULL != b; b = b->next)
1069 /* skip the URL patterns, */
1070 if (NULL == b->url->tag_regex)
1075 /* and check if one of the tag patterns matches the tag, */
1076 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
1078 /* if it does, update the action bit map, */
1079 if (merge_current_action(csp->action, b->action))
1081 log_error(LOG_LEVEL_ERROR,
1082 "Out of memorey while changing action bits");
1084 /* and signal the change. */
1094 /*********************************************************************
1096 * Function : free_current_action
1098 * Description : Free memory used by a current_action_spec.
1099 * Does not free the current_action_spec itself.
1102 * 1 : src = Source to free.
1106 *********************************************************************/
1107 void free_current_action (struct current_action_spec *src)
1111 for (i = 0; i < ACTION_STRING_COUNT; i++)
1113 freez(src->string[i]);
1116 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1118 destroy_list(src->multi[i]);
1121 memset(src, '\0', sizeof(*src));
1125 static struct file_list *current_actions_file[MAX_AF_FILES] = {
1126 NULL, NULL, NULL, NULL, NULL,
1127 NULL, NULL, NULL, NULL, NULL
1131 #ifdef FEATURE_GRACEFUL_TERMINATION
1132 /*********************************************************************
1134 * Function : unload_current_actions_file
1136 * Description : Unloads current actions file - reset to state at
1137 * beginning of program.
1143 *********************************************************************/
1144 void unload_current_actions_file(void)
1148 for (i = 0; i < MAX_AF_FILES; i++)
1150 if (current_actions_file[i])
1152 current_actions_file[i]->unloader = unload_actions_file;
1153 current_actions_file[i] = NULL;
1157 #endif /* FEATURE_GRACEFUL_TERMINATION */
1160 /*********************************************************************
1162 * Function : unload_actions_file
1164 * Description : Unloads an actions module.
1167 * 1 : file_data = the data structure associated with the
1172 *********************************************************************/
1173 void unload_actions_file(void *file_data)
1175 struct url_actions * next;
1176 struct url_actions * cur = (struct url_actions *)file_data;
1180 free_url_spec(cur->url);
1181 if ((next == NULL) || (next->action != cur->action))
1184 * As the action settings might be shared,
1185 * we can only free them if the current
1186 * url pattern is the last one, or if the
1187 * next one is using different settings.
1189 free_action_spec(cur->action);
1197 /*********************************************************************
1199 * Function : free_alias_list
1201 * Description : Free memory used by a list of aliases.
1204 * 1 : alias_list = Linked list to free.
1208 *********************************************************************/
1209 void free_alias_list(struct action_alias *alias_list)
1211 while (alias_list != NULL)
1213 struct action_alias * next = alias_list->next;
1214 alias_list->next = NULL;
1215 freez(alias_list->name);
1216 free_action(alias_list->action);
1223 /*********************************************************************
1225 * Function : load_action_files
1227 * Description : Read and parse all the action files and add to files
1231 * 1 : csp = Current client state (buffers, headers, etc...)
1233 * Returns : 0 => Ok, everything else is an error.
1235 *********************************************************************/
1236 int load_action_files(struct client_state *csp)
1241 for (i = 0; i < MAX_AF_FILES; i++)
1243 if (csp->config->actions_file[i])
1245 result = load_one_actions_file(csp, i);
1251 else if (current_actions_file[i])
1253 current_actions_file[i]->unloader = unload_actions_file;
1254 current_actions_file[i] = NULL;
1261 /*********************************************************************
1263 * Function : load_one_actions_file
1265 * Description : Read and parse a action file and add to files
1269 * 1 : csp = Current client state (buffers, headers, etc...)
1270 * 2 : fileid = File index to load.
1272 * Returns : 0 => Ok, everything else is an error.
1274 *********************************************************************/
1275 static int load_one_actions_file(struct client_state *csp, int fileid)
1280 * Note: Keep these in the order they occur in the file, they are
1281 * sometimes tested with <=
1283 #define MODE_START_OF_FILE 1
1284 #define MODE_SETTINGS 2
1285 #define MODE_DESCRIPTION 3
1286 #define MODE_ALIAS 4
1287 #define MODE_ACTIONS 5
1289 int mode = MODE_START_OF_FILE;
1292 struct url_actions *last_perm;
1293 struct url_actions *perm;
1294 char buf[BUFFER_SIZE];
1295 struct file_list *fs;
1296 struct action_spec * cur_action = NULL;
1297 int cur_action_used = 0;
1298 struct action_alias * alias_list = NULL;
1299 unsigned long linenum = 0;
1301 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1303 /* No need to load */
1304 csp->actions_list[fileid] = current_actions_file[fileid];
1309 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1310 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1311 "with their complete file names.", csp->config->actions_file[fileid]);
1312 return 1; /* never get here */
1315 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1316 if (last_perm == NULL)
1318 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1319 csp->config->actions_file[fileid]);
1320 return 1; /* never get here */
1323 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1325 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1326 csp->config->actions_file[fileid]);
1327 return 1; /* never get here */
1330 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1334 /* It's a header block */
1337 /* It's {{settings}} or {{alias}} */
1338 size_t len = strlen(buf);
1339 char * start = buf + 2;
1340 char * end = buf + len - 1;
1341 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1345 log_error(LOG_LEVEL_FATAL,
1346 "can't load actions file '%s': invalid line (%lu): %s",
1347 csp->config->actions_file[fileid], linenum, buf);
1348 return 1; /* never get here */
1351 /* Trim leading and trailing whitespace. */
1359 log_error(LOG_LEVEL_FATAL,
1360 "can't load actions file '%s': invalid line (%lu): {{ }}",
1361 csp->config->actions_file[fileid], linenum);
1362 return 1; /* never get here */
1366 * An actionsfile can optionally contain the following blocks.
1367 * They *MUST* be in this order, to simplify processing:
1373 * ...free text, format TBD, but no line may start with a '{'...
1378 * The actual actions must be *after* these special blocks.
1379 * None of these special blocks may be repeated.
1382 if (0 == strcmpic(start, "settings"))
1384 /* it's a {{settings}} block */
1385 if (mode >= MODE_SETTINGS)
1387 /* {{settings}} must be first thing in file and must only
1391 log_error(LOG_LEVEL_FATAL,
1392 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1393 csp->config->actions_file[fileid], linenum);
1395 mode = MODE_SETTINGS;
1397 else if (0 == strcmpic(start, "description"))
1399 /* it's a {{description}} block */
1400 if (mode >= MODE_DESCRIPTION)
1402 /* {{description}} is a singleton and only {{settings}} may proceed it
1405 log_error(LOG_LEVEL_FATAL,
1406 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1407 csp->config->actions_file[fileid], linenum);
1409 mode = MODE_DESCRIPTION;
1411 else if (0 == strcmpic(start, "alias"))
1413 /* it's an {{alias}} block */
1414 if (mode >= MODE_ALIAS)
1416 /* {{alias}} must be first thing in file, possibly after
1417 * {{settings}} and {{description}}
1419 * {{alias}} must only appear once.
1421 * Note that these are new restrictions introduced in
1422 * v2.9.10 in order to make actionsfile editing simpler.
1423 * (Otherwise, reordering actionsfile entries without
1424 * completely rewriting the file becomes non-trivial)
1427 log_error(LOG_LEVEL_FATAL,
1428 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1429 csp->config->actions_file[fileid], linenum);
1435 /* invalid {{something}} block */
1437 log_error(LOG_LEVEL_FATAL,
1438 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1439 csp->config->actions_file[fileid], linenum, start);
1440 return 1; /* never get here */
1445 /* It's an actions block */
1447 char actions_buf[BUFFER_SIZE];
1451 mode = MODE_ACTIONS;
1453 /* free old action */
1456 if (!cur_action_used)
1458 free_action_spec(cur_action);
1462 cur_action_used = 0;
1463 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1464 if (cur_action == NULL)
1467 log_error(LOG_LEVEL_FATAL,
1468 "can't load actions file '%s': out of memory",
1469 csp->config->actions_file[fileid]);
1470 return 1; /* never get here */
1472 init_action(cur_action);
1475 strlcpy(actions_buf, buf + 1, sizeof(actions_buf));
1477 /* check we have a trailing } and then trim it */
1478 end = actions_buf + strlen(actions_buf) - 1;
1483 log_error(LOG_LEVEL_FATAL,
1484 "can't load actions file '%s': invalid line (%lu): %s",
1485 csp->config->actions_file[fileid], linenum, buf);
1486 return 1; /* never get here */
1490 /* trim any whitespace immediately inside {} */
1493 if (get_actions(actions_buf, alias_list, cur_action))
1497 log_error(LOG_LEVEL_FATAL,
1498 "can't load actions file '%s': invalid line (%lu): %s",
1499 csp->config->actions_file[fileid], linenum, buf);
1500 return 1; /* never get here */
1504 else if (mode == MODE_SETTINGS)
1507 * Part of the {{settings}} block.
1508 * For now only serves to check if the file's minimum Privoxy
1509 * version requirement is met, but we may want to read & check
1510 * permissions when we go multi-user.
1512 if (!strncmp(buf, "for-privoxy-version=", 20))
1514 char *version_string, *fields[3];
1517 if ((version_string = strdup(buf + 20)) == NULL)
1520 log_error(LOG_LEVEL_FATAL,
1521 "can't load actions file '%s': out of memory!",
1522 csp->config->actions_file[fileid]);
1523 return 1; /* never get here */
1526 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1528 if (num_fields < 1 || atoi(fields[0]) == 0)
1530 log_error(LOG_LEVEL_ERROR,
1531 "While loading actions file '%s': invalid line (%lu): %s",
1532 csp->config->actions_file[fileid], linenum, buf);
1534 else if ( atoi(fields[0]) > VERSION_MAJOR
1535 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1536 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1539 log_error(LOG_LEVEL_FATAL,
1540 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1541 csp->config->actions_file[fileid], linenum, buf );
1542 return 1; /* never get here */
1544 free(version_string);
1547 else if (mode == MODE_DESCRIPTION)
1550 * Part of the {{description}} block.
1554 else if (mode == MODE_ALIAS)
1559 char actions_buf[BUFFER_SIZE];
1560 struct action_alias * new_alias;
1562 char * start = strchr(buf, '=');
1565 if ((start == NULL) || (start == buf))
1567 log_error(LOG_LEVEL_FATAL,
1568 "can't load actions file '%s': invalid alias line (%lu): %s",
1569 csp->config->actions_file[fileid], linenum, buf);
1570 return 1; /* never get here */
1573 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1576 log_error(LOG_LEVEL_FATAL,
1577 "can't load actions file '%s': out of memory!",
1578 csp->config->actions_file[fileid]);
1579 return 1; /* never get here */
1582 /* Eat any the whitespace before the '=' */
1584 while ((*end == ' ') || (*end == '\t'))
1587 * we already know we must have at least 1 non-ws char
1588 * at start of buf - no need to check
1594 /* Eat any the whitespace after the '=' */
1596 while ((*start == ' ') || (*start == '\t'))
1602 log_error(LOG_LEVEL_FATAL,
1603 "can't load actions file '%s': invalid alias line (%lu): %s",
1604 csp->config->actions_file[fileid], linenum, buf);
1605 return 1; /* never get here */
1608 if ((new_alias->name = strdup(buf)) == NULL)
1611 log_error(LOG_LEVEL_FATAL,
1612 "can't load actions file '%s': out of memory!",
1613 csp->config->actions_file[fileid]);
1614 return 1; /* never get here */
1617 strlcpy(actions_buf, start, sizeof(actions_buf));
1619 if (get_actions(actions_buf, alias_list, new_alias->action))
1623 log_error(LOG_LEVEL_FATAL,
1624 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1625 csp->config->actions_file[fileid], linenum, buf, start);
1626 return 1; /* never get here */
1630 new_alias->next = alias_list;
1631 alias_list = new_alias;
1633 else if (mode == MODE_ACTIONS)
1635 /* it's a URL pattern */
1637 /* allocate a new node */
1638 if ((perm = zalloc(sizeof(*perm))) == NULL)
1641 log_error(LOG_LEVEL_FATAL,
1642 "can't load actions file '%s': out of memory!",
1643 csp->config->actions_file[fileid]);
1644 return 1; /* never get here */
1647 perm->action = cur_action;
1648 cur_action_used = 1;
1650 /* Save the URL pattern */
1651 if (create_url_spec(perm->url, buf))
1654 log_error(LOG_LEVEL_FATAL,
1655 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1656 csp->config->actions_file[fileid], linenum, buf);
1657 return 1; /* never get here */
1660 /* add it to the list */
1661 last_perm->next = perm;
1664 else if (mode == MODE_START_OF_FILE)
1666 /* oops - please have a {} line as 1st line in file. */
1668 log_error(LOG_LEVEL_FATAL,
1669 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1670 csp->config->actions_file[fileid], linenum, buf);
1671 return 1; /* never get here */
1675 /* How did we get here? This is impossible! */
1677 log_error(LOG_LEVEL_FATAL,
1678 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1679 csp->config->actions_file[fileid], mode);
1680 return 1; /* never get here */
1686 if (!cur_action_used)
1688 free_action_spec(cur_action);
1690 free_alias_list(alias_list);
1692 /* the old one is now obsolete */
1693 if (current_actions_file[fileid])
1695 current_actions_file[fileid]->unloader = unload_actions_file;
1698 fs->next = files->next;
1700 current_actions_file[fileid] = fs;
1702 csp->actions_list[fileid] = fs;
1709 /*********************************************************************
1711 * Function : actions_to_text
1713 * Description : Converts a actionsfile entry from the internal
1714 * structure into a text line. The output is split
1715 * into one line for each action with line continuation.
1718 * 1 : action = The action to format.
1720 * Returns : A string. Caller must free it.
1721 * NULL on out-of-memory error.
1723 *********************************************************************/
1724 char * actions_to_text(const struct action_spec *action)
1726 unsigned mask = action->mask;
1727 unsigned add = action->add;
1728 char * result = strdup("");
1729 struct list_entry * lst;
1731 /* sanity - prevents "-feature +feature" */
1735 #define DEFINE_ACTION_BOOL(__name, __bit) \
1736 if (!(mask & __bit)) \
1738 string_append(&result, " -" __name " \\\n"); \
1740 else if (add & __bit) \
1742 string_append(&result, " +" __name " \\\n"); \
1745 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1746 if (!(mask & __bit)) \
1748 string_append(&result, " -" __name " \\\n"); \
1750 else if (add & __bit) \
1752 string_append(&result, " +" __name "{"); \
1753 string_append(&result, action->string[__index]); \
1754 string_append(&result, "} \\\n"); \
1757 #define DEFINE_ACTION_MULTI(__name, __index) \
1758 if (action->multi_remove_all[__index]) \
1760 string_append(&result, " -" __name " \\\n"); \
1764 lst = action->multi_remove[__index]->first; \
1767 string_append(&result, " -" __name "{"); \
1768 string_append(&result, lst->str); \
1769 string_append(&result, "} \\\n"); \
1773 lst = action->multi_add[__index]->first; \
1776 string_append(&result, " +" __name "{"); \
1777 string_append(&result, lst->str); \
1778 string_append(&result, "} \\\n"); \
1782 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1784 #include "actionlist.h"
1786 #undef DEFINE_ACTION_MULTI
1787 #undef DEFINE_ACTION_STRING
1788 #undef DEFINE_ACTION_BOOL
1789 #undef DEFINE_ACTION_ALIAS
1795 /*********************************************************************
1797 * Function : actions_to_html
1799 * Description : Converts a actionsfile entry from numeric form
1800 * ("mask" and "add") to a <br>-seperated HTML string
1801 * in which each action is linked to its chapter in
1805 * 1 : csp = Client state (for config)
1806 * 2 : action = Action spec to be converted
1808 * Returns : A string. Caller must free it.
1809 * NULL on out-of-memory error.
1811 *********************************************************************/
1812 char * actions_to_html(const struct client_state *csp,
1813 const struct action_spec *action)
1815 unsigned mask = action->mask;
1816 unsigned add = action->add;
1817 char * result = strdup("");
1818 struct list_entry * lst;
1820 /* sanity - prevents "-feature +feature" */
1824 #define DEFINE_ACTION_BOOL(__name, __bit) \
1825 if (!(mask & __bit)) \
1827 string_append(&result, "\n<br>-"); \
1828 string_join(&result, add_help_link(__name, csp->config)); \
1830 else if (add & __bit) \
1832 string_append(&result, "\n<br>+"); \
1833 string_join(&result, add_help_link(__name, csp->config)); \
1836 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1837 if (!(mask & __bit)) \
1839 string_append(&result, "\n<br>-"); \
1840 string_join(&result, add_help_link(__name, csp->config)); \
1842 else if (add & __bit) \
1844 string_append(&result, "\n<br>+"); \
1845 string_join(&result, add_help_link(__name, csp->config)); \
1846 string_append(&result, "{"); \
1847 string_join(&result, html_encode(action->string[__index])); \
1848 string_append(&result, "}"); \
1851 #define DEFINE_ACTION_MULTI(__name, __index) \
1852 if (action->multi_remove_all[__index]) \
1854 string_append(&result, "\n<br>-"); \
1855 string_join(&result, add_help_link(__name, csp->config)); \
1859 lst = action->multi_remove[__index]->first; \
1862 string_append(&result, "\n<br>-"); \
1863 string_join(&result, add_help_link(__name, csp->config)); \
1864 string_append(&result, "{"); \
1865 string_join(&result, html_encode(lst->str)); \
1866 string_append(&result, "}"); \
1870 lst = action->multi_add[__index]->first; \
1873 string_append(&result, "\n<br>+"); \
1874 string_join(&result, add_help_link(__name, csp->config)); \
1875 string_append(&result, "{"); \
1876 string_join(&result, html_encode(lst->str)); \
1877 string_append(&result, "}"); \
1881 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1883 #include "actionlist.h"
1885 #undef DEFINE_ACTION_MULTI
1886 #undef DEFINE_ACTION_STRING
1887 #undef DEFINE_ACTION_BOOL
1888 #undef DEFINE_ACTION_ALIAS
1890 /* trim leading <br> */
1891 if (result && *result)
1894 result = strdup(result + 5);
1902 /*********************************************************************
1904 * Function : current_actions_to_html
1906 * Description : Converts a curren action spec to a <br> seperated HTML
1907 * text in which each action is linked to its chapter in
1911 * 1 : csp = Client state (for config)
1912 * 2 : action = Current action spec to be converted
1914 * Returns : A string. Caller must free it.
1915 * NULL on out-of-memory error.
1917 *********************************************************************/
1918 char *current_action_to_html(const struct client_state *csp,
1919 const struct current_action_spec *action)
1921 unsigned long flags = action->flags;
1922 struct list_entry * lst;
1923 char *result = strdup("");
1924 char *active = strdup("");
1925 char *inactive = strdup("");
1927 #define DEFINE_ACTION_BOOL(__name, __bit) \
1928 if (flags & __bit) \
1930 string_append(&active, "\n<br>+"); \
1931 string_join(&active, add_help_link(__name, csp->config)); \
1935 string_append(&inactive, "\n<br>-"); \
1936 string_join(&inactive, add_help_link(__name, csp->config)); \
1939 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1940 if (flags & __bit) \
1942 string_append(&active, "\n<br>+"); \
1943 string_join(&active, add_help_link(__name, csp->config)); \
1944 string_append(&active, "{"); \
1945 string_join(&active, html_encode(action->string[__index])); \
1946 string_append(&active, "}"); \
1950 string_append(&inactive, "\n<br>-"); \
1951 string_join(&inactive, add_help_link(__name, csp->config)); \
1954 #define DEFINE_ACTION_MULTI(__name, __index) \
1955 lst = action->multi[__index]->first; \
1958 string_append(&inactive, "\n<br>-"); \
1959 string_join(&inactive, add_help_link(__name, csp->config)); \
1965 string_append(&active, "\n<br>+"); \
1966 string_join(&active, add_help_link(__name, csp->config)); \
1967 string_append(&active, "{"); \
1968 string_join(&active, html_encode(lst->str)); \
1969 string_append(&active, "}"); \
1974 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1976 #include "actionlist.h"
1978 #undef DEFINE_ACTION_MULTI
1979 #undef DEFINE_ACTION_STRING
1980 #undef DEFINE_ACTION_BOOL
1981 #undef DEFINE_ACTION_ALIAS
1985 string_append(&result, active);
1988 string_append(&result, "\n<br>");
1989 if (inactive != NULL)
1991 string_append(&result, inactive);