1 const char actions_rcs[] = "$Id: actions.c,v 1.47 2008/03/28 15:13:37 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.47 2008/03/28 15:13:37 fabiankeil
37 * Remove inspect-jpegs action.
39 * Revision 1.46 2008/03/27 18:27:20 fabiankeil
40 * Remove kill-popups action.
42 * Revision 1.45 2008/03/24 11:21:02 fabiankeil
43 * Share the action settings for multiple patterns in the same
44 * section so we waste less memory for gigantic block lists
45 * (and load them slightly faster). Reported by Franz Schwartau.
47 * Revision 1.44 2008/03/04 18:30:34 fabiankeil
48 * Remove the treat-forbidden-connects-like-blocks action. We now
49 * use the "blocked" page for forbidden CONNECT requests by default.
51 * Revision 1.43 2008/03/01 14:00:43 fabiankeil
52 * Let the block action take the reason for the block
53 * as argument and show it on the "blocked" page.
55 * Revision 1.42 2008/02/09 15:15:38 fabiankeil
56 * List active and inactive actions in the show-url-info's
57 * "Final results" section separately. Patch submitted by Lee
58 * in #1830056, modified to list active actions first.
60 * Revision 1.41 2008/01/28 20:17:40 fabiankeil
61 * - Mark some parameters as immutable.
62 * - Hide update_action_bits_for_all_tags() while it's unused.
64 * Revision 1.40 2007/05/21 10:26:50 fabiankeil
65 * - Use strlcpy() instead of strcpy().
66 * - Provide a reason why loading the actions
67 * file might have failed.
69 * Revision 1.39 2007/04/17 18:21:45 fabiankeil
70 * Split update_action_bits() into
71 * update_action_bits_for_all_tags()
72 * and update_action_bits_for_tag().
74 * Revision 1.38 2007/04/15 16:39:20 fabiankeil
75 * Introduce tags as alternative way to specify which
76 * actions apply to a request. At the moment tags can be
77 * created based on client and server headers.
79 * Revision 1.37 2007/03/11 15:56:12 fabiankeil
80 * Add kludge to log unknown aliases and actions before exiting.
82 * Revision 1.36 2006/12/28 17:15:42 fabiankeil
83 * Fix gcc43 conversion warning.
85 * Revision 1.35 2006/07/18 14:48:45 david__schmidt
86 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
87 * with what was really the latest development (the v_3_0_branch branch)
89 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
90 * Multiple filter file support
92 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
93 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
94 * this becomes important when jcc.h gets included later down the line.
96 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
97 * - Implemented Privoxy version requirement through
98 * for-privoxy-version= statement in {{settings}}
100 * - Fix for unchecked out-of-memory condition
102 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
103 * Fixed memory leak reported by Dan Price in Bug #694713
105 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
106 * Extended unload_current_actions_file() to multiple AFs.
107 * Thanks to Oliver Stoeneberg for the hint.
109 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
110 * Change unsigned to unsigned long in actions_name struct. This closes
111 * SourceForge Bug #539284.
113 * Revision 1.32 2002/05/12 21:36:29 jongfoster
114 * Correcting function comments
116 * Revision 1.31 2002/05/06 07:56:50 oes
117 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
119 * Revision 1.30 2002/04/30 11:14:52 oes
120 * Made csp the first parameter in *action_to_html
122 * Revision 1.29 2002/04/26 19:30:54 jongfoster
123 * - current_action_to_html(): Adding help link for the "-" form of
124 * one-string actions.
125 * - Some actions had "<br>-", some "<br> -" (note the space).
126 * Standardizing on no space.
127 * - Greatly simplifying some of the code by using string_join()
130 * Revision 1.28 2002/04/26 12:53:15 oes
131 * - CGI AF editor now writes action lines split into
132 * single lines with line continuation
133 * - actions_to_html now embeds each action name in
135 * - current_action_to_text is now called current_action_to_html
136 * and acts like actions_to_html
138 * Revision 1.27 2002/04/24 02:10:31 oes
139 * - Jon's patch for multiple AFs:
140 * - split load_actions_file, add load_one_actions_file
141 * - make csp->actions_list files an array
142 * - remember file id with each action
143 * - Copy_action now frees dest action before copying
145 * Revision 1.26 2002/03/26 22:29:54 swa
146 * we have a new homepage!
148 * Revision 1.25 2002/03/24 13:25:43 swa
149 * name change related issues
151 * Revision 1.24 2002/03/16 23:54:06 jongfoster
152 * Adding graceful termination feature, to help look for memory leaks.
153 * If you enable this (which, by design, has to be done by hand
154 * editing config.h) and then go to http://i.j.b/die, then the program
155 * will exit cleanly after the *next* request. It should free all the
156 * memory that was used.
158 * Revision 1.23 2002/03/07 03:46:16 oes
159 * Fixed compiler warnings
161 * Revision 1.22 2002/01/21 00:27:02 jongfoster
162 * Allowing free_action(NULL).
163 * Moving the functions that #include actionlist.h to the end of the file,
164 * because the Visual C++ 97 debugger gets extremely confused if you try
165 * to debug any code that comes after them in the file.
167 * Revision 1.21 2002/01/17 20:54:44 jongfoster
168 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
170 * Revision 1.20 2001/11/22 21:56:49 jongfoster
171 * Making action_spec->flags into an unsigned long rather than just an
173 * Fixing a bug in the display of -add-header and -wafer
175 * Revision 1.19 2001/11/13 00:14:07 jongfoster
176 * Fixing stupid bug now I've figured out what || means.
177 * (It always returns 0 or 1, not one of it's paramaters.)
179 * Revision 1.18 2001/11/07 00:06:06 steudten
180 * Add line number in error output for lineparsing for
183 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
184 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
185 * threads to call select() simultaneously. So, it's time to do a real, live,
186 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
187 * (native). Both versions will work, but using __OS2__ offers multi-threading.
189 * Revision 1.16 2001/10/23 21:30:30 jongfoster
190 * Adding error-checking to selected functions.
192 * Revision 1.15 2001/10/14 21:58:22 jongfoster
193 * Adding support for the CGI-based editor:
194 * - Exported get_actions()
195 * - Added new function free_alias_list()
196 * - Added support for {{settings}} and {{description}} blocks
197 * in the actions file. They are currently ignored.
198 * - Added restriction to only one {{alias}} block which must appear
199 * first in the file, to simplify the editor's rewriting rules.
200 * - Note that load_actions_file() is no longer used by the CGI-based
201 * editor, but some of the other routines in this file are.
203 * Revision 1.14 2001/09/22 16:36:59 jongfoster
204 * Removing unused parameter fs from read_config_line()
206 * Revision 1.13 2001/09/16 15:47:37 jongfoster
207 * First version of CGI-based edit interface. This is very much a
208 * work-in-progress, and you can't actually use it to edit anything
209 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
210 * to have any effect.
212 * Revision 1.12 2001/09/16 13:21:27 jongfoster
213 * Changes to use new list functions.
215 * Revision 1.11 2001/09/14 00:17:32 jongfoster
216 * Tidying up memory allocation. New function init_action().
218 * Revision 1.10 2001/09/10 10:14:34 oes
219 * Removing unused variable
221 * Revision 1.9 2001/07/30 22:08:36 jongfoster
222 * Tidying up #defines:
223 * - All feature #defines are now of the form FEATURE_xxx
224 * - Permanently turned off WIN_GUI_EDIT
225 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
227 * Revision 1.8 2001/06/29 13:19:52 oes
228 * Removed logentry from cancelled commit
230 * Revision 1.7 2001/06/09 10:55:28 jongfoster
231 * Changing BUFSIZ ==> BUFFER_SIZE
233 * Revision 1.6 2001/06/07 23:04:34 jongfoster
234 * Made get_actions() static.
236 * Revision 1.5 2001/06/03 19:11:48 oes
237 * adapted to new enlist_unique arg format
239 * Revision 1.4 2001/06/01 20:03:42 jongfoster
240 * Better memory management - current_action->strings[] now
241 * contains copies of the strings, not the original.
243 * Revision 1.3 2001/06/01 18:49:17 jongfoster
244 * Replaced "list_share" with "list" - the tiny memory gain was not
245 * worth the extra complexity.
247 * Revision 1.2 2001/05/31 21:40:00 jongfoster
248 * Removing some commented out, obsolete blocks of code.
250 * Revision 1.1 2001/05/31 21:16:46 jongfoster
251 * Moved functions to process the action list into this new file.
254 *********************************************************************/
264 #ifdef FEATURE_PTHREAD
272 #include "miscutil.h"
276 #include "urlmatch.h"
280 const char actions_h_rcs[] = ACTIONS_H_VERSION;
284 * We need the main list of options.
286 * First, we need a way to tell between boolean, string, and multi-string
287 * options. For string and multistring options, we also need to be
288 * able to tell the difference between a "+" and a "-". (For bools,
289 * the "+"/"-" information is encoded in "add" and "mask"). So we use
290 * an enumerated type (well, the preprocessor equivalent). Here are
293 #define AV_NONE 0 /* +opt -opt */
294 #define AV_ADD_STRING 1 /* +stropt{string} */
295 #define AV_REM_STRING 2 /* -stropt */
296 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
297 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
300 * We need a structure to hold the name, flag changes,
301 * type, and string index.
306 unsigned long mask; /* a bit set to "0" = remove action */
307 unsigned long add; /* a bit set to "1" = add action */
308 int takes_value; /* an AV_... constant */
309 int index; /* index into strings[] or multi[] */
313 * And with those building blocks in place, here's the array.
315 static const struct action_name action_names[] =
318 * Well actually there's no data here - it's in actionlist.h
319 * This keeps it together to make it easy to change.
321 * Here's the macros used to format it:
323 #define DEFINE_ACTION_MULTI(name,index) \
324 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
325 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
326 #define DEFINE_ACTION_STRING(name,flag,index) \
327 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
328 { "-" name, ~flag, 0, AV_REM_STRING, index },
329 #define DEFINE_ACTION_BOOL(name,flag) \
330 { "+" name, ACTION_MASK_ALL, flag }, \
331 { "-" name, ~flag, 0 },
332 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
334 #include "actionlist.h"
336 #undef DEFINE_ACTION_MULTI
337 #undef DEFINE_ACTION_STRING
338 #undef DEFINE_ACTION_BOOL
339 #undef DEFINE_ACTION_ALIAS
341 { NULL, 0, 0 } /* End marker */
345 static int load_one_actions_file(struct client_state *csp, int fileid);
348 /*********************************************************************
350 * Function : merge_actions
352 * Description : Merge two actions together.
353 * Similar to "dest += src".
356 * 1 : dest = Actions to modify.
357 * 2 : src = Action to add.
359 * Returns : JB_ERR_OK or JB_ERR_MEMORY
361 *********************************************************************/
362 jb_err merge_actions (struct action_spec *dest,
363 const struct action_spec *src)
368 dest->mask &= src->mask;
369 dest->add &= src->mask;
370 dest->add |= src->add;
372 for (i = 0; i < ACTION_STRING_COUNT; i++)
374 char * str = src->string[i];
377 freez(dest->string[i]);
378 dest->string[i] = strdup(str);
379 if (NULL == dest->string[i])
381 return JB_ERR_MEMORY;
386 for (i = 0; i < ACTION_MULTI_COUNT; i++)
388 if (src->multi_remove_all[i])
390 /* Remove everything from dest */
391 list_remove_all(dest->multi_remove[i]);
392 dest->multi_remove_all[i] = 1;
394 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
396 else if (dest->multi_remove_all[i])
399 * dest already removes everything, so we only need to worry
402 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
403 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
407 /* No "remove all"s to worry about. */
408 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
409 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
410 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
423 /*********************************************************************
425 * Function : copy_action
427 * Description : Copy an action_specs.
428 * Similar to "dest = src".
431 * 1 : dest = Destination of copy.
432 * 2 : src = Source for copy.
436 *********************************************************************/
437 jb_err copy_action (struct action_spec *dest,
438 const struct action_spec *src)
441 jb_err err = JB_ERR_OK;
444 memset(dest, '\0', sizeof(*dest));
446 dest->mask = src->mask;
447 dest->add = src->add;
449 for (i = 0; i < ACTION_STRING_COUNT; i++)
451 char * str = src->string[i];
457 return JB_ERR_MEMORY;
459 dest->string[i] = str;
463 for (i = 0; i < ACTION_MULTI_COUNT; i++)
465 dest->multi_remove_all[i] = src->multi_remove_all[i];
466 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
471 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
480 /*********************************************************************
482 * Function : free_action_spec
484 * Description : Frees an action_spec and the memory used by it.
487 * 1 : src = Source to free.
491 *********************************************************************/
492 void free_action_spec(struct action_spec *src)
499 /*********************************************************************
501 * Function : free_action
503 * Description : Destroy an action_spec. Frees memory used by it,
504 * except for the memory used by the struct action_spec
508 * 1 : src = Source to free.
512 *********************************************************************/
513 void free_action (struct action_spec *src)
522 for (i = 0; i < ACTION_STRING_COUNT; i++)
524 freez(src->string[i]);
527 for (i = 0; i < ACTION_MULTI_COUNT; i++)
529 destroy_list(src->multi_remove[i]);
530 destroy_list(src->multi_add[i]);
533 memset(src, '\0', sizeof(*src));
537 /*********************************************************************
539 * Function : get_action_token
541 * Description : Parses a line for the first action.
542 * Modifies it's input array, doesn't allocate memory.
544 * *line=" +abc{def} -ghi "
551 * 1 : line = [in] The line containing the action.
552 * [out] Start of next action on line, or
553 * NULL if we reached the end of line before
554 * we found an action.
555 * 2 : name = [out] Start of action name, null
556 * terminated. NULL on EOL
557 * 3 : value = [out] Start of action value, null
558 * terminated. NULL if none or EOL.
560 * Returns : JB_ERR_OK => Ok
561 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
563 *********************************************************************/
564 jb_err get_action_token(char **line, char **name, char **value)
569 /* set default returns */
574 /* Eat any leading whitespace */
575 while ((*str == ' ') || (*str == '\t'))
587 /* null name, just value is prohibited */
594 while (((ch = *str) != '\0') &&
595 (ch != ' ') && (ch != '\t') && (ch != '{'))
599 /* error, '}' without '{' */
611 /* EOL - be careful not to run off buffer */
616 /* More to parse next time. */
625 str = strchr(str, '}');
642 /*********************************************************************
644 * Function : action_used_to_be_valid
646 * Description : Checks if unrecognized actions were valid in earlier
650 * 1 : action = The string containing the action to check.
652 * Returns : True if yes, otherwise false.
654 *********************************************************************/
655 static int action_used_to_be_valid(const char *action)
657 static const char *formerly_valid_actions[] = {
660 "treat-forbidden-connects-like-blocks"
664 for (i = 0; i < SZ(formerly_valid_actions); i++)
666 if (0 == strcmpic(action, formerly_valid_actions[i]))
675 /*********************************************************************
677 * Function : get_actions
679 * Description : Parses a list of actions.
682 * 1 : line = The string containing the actions.
683 * Will be written to by this function.
684 * 2 : alias_list = Custom alias list, or NULL for none.
685 * 3 : cur_action = Where to store the action. Caller
688 * Returns : JB_ERR_OK => Ok
689 * JB_ERR_PARSE => Parse error (line was trashed anyway)
690 * nonzero => Out of memory (line was trashed anyway)
692 *********************************************************************/
693 jb_err get_actions(char *line,
694 struct action_alias * alias_list,
695 struct action_spec *cur_action)
698 init_action(cur_action);
699 cur_action->mask = ACTION_MASK_ALL;
703 char * option = NULL;
706 err = get_action_token(&line, &option, &value);
714 /* handle option in 'option' */
716 /* Check for standard action name */
717 const struct action_name * action = action_names;
719 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
723 if (action->name != NULL)
726 cur_action->mask &= action->mask;
727 cur_action->add &= action->mask;
728 cur_action->add |= action->add;
730 switch (action->takes_value)
733 /* ignore any option. */
737 /* add single string. */
739 if ((value == NULL) || (*value == '\0'))
741 if (0 != strcmpic(action->name, "block"))
744 * XXX: Temporary backwards compatibility hack.
746 static int complaint_shown = 0;
747 value = "No reason specified.";
748 if (!complaint_shown)
750 log_error(LOG_LEVEL_ERROR, "At least one block "
751 "without reason found. This may become a fatal "
752 "error in future versions.");
761 /* FIXME: should validate option string here */
762 freez (cur_action->string[action->index]);
763 cur_action->string[action->index] = strdup(value);
764 if (NULL == cur_action->string[action->index])
766 return JB_ERR_MEMORY;
772 /* remove single string. */
774 freez (cur_action->string[action->index]);
779 /* append 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'))
789 list_remove_item(remove_p, value);
790 err = enlist_unique(add_p, value, 0);
799 /* remove multi string. */
801 struct list * remove_p = cur_action->multi_remove[action->index];
802 struct list * add_p = cur_action->multi_add[action->index];
804 if ( (value == NULL) || (*value == '\0')
805 || ((*value == '*') && (value[1] == '\0')) )
808 * no option, or option == "*".
812 list_remove_all(remove_p);
813 list_remove_all(add_p);
814 cur_action->multi_remove_all[action->index] = 1;
818 /* Valid option - remove only 1 option */
820 if ( !cur_action->multi_remove_all[action->index] )
822 /* there isn't a catch-all in the remove list already */
823 err = enlist_unique(remove_p, value, 0);
829 list_remove_item(add_p, value);
834 /* Shouldn't get here unless there's memory corruption. */
841 /* try user aliases. */
842 const struct action_alias * alias = alias_list;
844 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
851 merge_actions(cur_action, alias->action);
853 else if ((2 < strlen(option)) && action_used_to_be_valid(option+1))
855 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
856 "in this Privoxy release. Ignored.", option+1);
860 /* Bad action name */
862 * XXX: This is a fatal error and Privoxy will later on exit
863 * in load_one_actions_file() because of an "invalid line".
865 * It would be preferable to name the offending option in that
866 * error message, but currently there is no way to do that and
867 * we have to live with two error messages for basically the
870 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
881 /*********************************************************************
883 * Function : init_current_action
885 * Description : Zero out an action.
888 * 1 : dest = An uninitialized current_action_spec.
892 *********************************************************************/
893 void init_current_action (struct current_action_spec *dest)
895 memset(dest, '\0', sizeof(*dest));
897 dest->flags = ACTION_MOST_COMPATIBLE;
901 /*********************************************************************
903 * Function : init_action
905 * Description : Zero out an action.
908 * 1 : dest = An uninitialized action_spec.
912 *********************************************************************/
913 void init_action (struct action_spec *dest)
915 memset(dest, '\0', sizeof(*dest));
919 /*********************************************************************
921 * Function : merge_current_action
923 * Description : Merge two actions together.
924 * Similar to "dest += src".
925 * Differences between this and merge_actions()
926 * is that this one doesn't allocate memory for
927 * strings (so "src" better be in memory for at least
928 * as long as "dest" is, and you'd better free
929 * "dest" using "free_current_action").
930 * Also, there is no mask or remove lists in dest.
931 * (If we're applying it to a URL, we don't need them)
934 * 1 : dest = Current actions, to modify.
935 * 2 : src = Action to add.
937 * Returns 0 : no error
938 * !=0 : error, probably JB_ERR_MEMORY.
940 *********************************************************************/
941 jb_err merge_current_action (struct current_action_spec *dest,
942 const struct action_spec *src)
945 jb_err err = JB_ERR_OK;
947 dest->flags &= src->mask;
948 dest->flags |= src->add;
950 for (i = 0; i < ACTION_STRING_COUNT; i++)
952 char * str = src->string[i];
958 return JB_ERR_MEMORY;
960 freez(dest->string[i]);
961 dest->string[i] = str;
965 for (i = 0; i < ACTION_MULTI_COUNT; i++)
967 if (src->multi_remove_all[i])
969 /* Remove everything from dest, then add src->multi_add */
970 err = list_duplicate(dest->multi[i], src->multi_add[i]);
978 list_remove_list(dest->multi[i], src->multi_remove[i]);
979 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
990 /*********************************************************************
992 * Function : update_action_bits_for_all_tags
994 * Description : Updates the action bits based on all matching tags.
997 * 1 : csp = Current client state (buffers, headers, etc...)
999 * Returns : 0 if no tag matched, or
1002 *********************************************************************/
1003 int update_action_bits_for_all_tags(struct client_state *csp)
1005 struct list_entry *tag;
1008 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
1010 if (update_action_bits_for_tag(csp, tag->str))
1020 /*********************************************************************
1022 * Function : update_action_bits_for_tag
1024 * Description : Updates the action bits based on the action sections
1025 * whose tag patterns match a provided tag.
1028 * 1 : csp = Current client state (buffers, headers, etc...)
1029 * 2 : tag = The tag on which the update should be based on
1031 * Returns : 0 if no tag matched, or
1034 *********************************************************************/
1035 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
1037 struct file_list *fl;
1038 struct url_actions *b;
1044 assert(list_contains_item(csp->tags, tag));
1046 /* Run through all action files, */
1047 for (i = 0; i < MAX_AF_FILES; i++)
1049 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
1051 /* Skip empty files */
1055 /* and through all the action patterns, */
1056 for (b = b->next; NULL != b; b = b->next)
1058 /* skip the URL patterns, */
1059 if (NULL == b->url->tag_regex)
1064 /* and check if one of the tag patterns matches the tag, */
1065 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
1067 /* if it does, update the action bit map, */
1068 if (merge_current_action(csp->action, b->action))
1070 log_error(LOG_LEVEL_ERROR,
1071 "Out of memorey while changing action bits");
1073 /* and signal the change. */
1083 /*********************************************************************
1085 * Function : free_current_action
1087 * Description : Free memory used by a current_action_spec.
1088 * Does not free the current_action_spec itself.
1091 * 1 : src = Source to free.
1095 *********************************************************************/
1096 void free_current_action (struct current_action_spec *src)
1100 for (i = 0; i < ACTION_STRING_COUNT; i++)
1102 freez(src->string[i]);
1105 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1107 destroy_list(src->multi[i]);
1110 memset(src, '\0', sizeof(*src));
1114 static struct file_list *current_actions_file[MAX_AF_FILES] = {
1115 NULL, NULL, NULL, NULL, NULL,
1116 NULL, NULL, NULL, NULL, NULL
1120 #ifdef FEATURE_GRACEFUL_TERMINATION
1121 /*********************************************************************
1123 * Function : unload_current_actions_file
1125 * Description : Unloads current actions file - reset to state at
1126 * beginning of program.
1132 *********************************************************************/
1133 void unload_current_actions_file(void)
1137 for (i = 0; i < MAX_AF_FILES; i++)
1139 if (current_actions_file[i])
1141 current_actions_file[i]->unloader = unload_actions_file;
1142 current_actions_file[i] = NULL;
1146 #endif /* FEATURE_GRACEFUL_TERMINATION */
1149 /*********************************************************************
1151 * Function : unload_actions_file
1153 * Description : Unloads an actions module.
1156 * 1 : file_data = the data structure associated with the
1161 *********************************************************************/
1162 void unload_actions_file(void *file_data)
1164 struct url_actions * next;
1165 struct url_actions * cur = (struct url_actions *)file_data;
1169 free_url_spec(cur->url);
1170 if ((next == NULL) || (next->action != cur->action))
1173 * As the action settings might be shared,
1174 * we can only free them if the current
1175 * url pattern is the last one, or if the
1176 * next one is using different settings.
1178 free_action_spec(cur->action);
1186 /*********************************************************************
1188 * Function : free_alias_list
1190 * Description : Free memory used by a list of aliases.
1193 * 1 : alias_list = Linked list to free.
1197 *********************************************************************/
1198 void free_alias_list(struct action_alias *alias_list)
1200 while (alias_list != NULL)
1202 struct action_alias * next = alias_list->next;
1203 alias_list->next = NULL;
1204 freez(alias_list->name);
1205 free_action(alias_list->action);
1212 /*********************************************************************
1214 * Function : load_actions_file
1216 * Description : Read and parse all the action files and add to files
1220 * 1 : csp = Current client state (buffers, headers, etc...)
1222 * Returns : 0 => Ok, everything else is an error.
1224 *********************************************************************/
1225 int load_actions_file(struct client_state *csp)
1230 for (i = 0; i < MAX_AF_FILES; i++)
1232 if (csp->config->actions_file[i])
1234 result = load_one_actions_file(csp, i);
1240 else if (current_actions_file[i])
1242 current_actions_file[i]->unloader = unload_actions_file;
1243 current_actions_file[i] = NULL;
1250 /*********************************************************************
1252 * Function : load_one_actions_file
1254 * Description : Read and parse a action file and add to files
1258 * 1 : csp = Current client state (buffers, headers, etc...)
1259 * 2 : fileid = File index to load.
1261 * Returns : 0 => Ok, everything else is an error.
1263 *********************************************************************/
1264 static int load_one_actions_file(struct client_state *csp, int fileid)
1269 * Note: Keep these in the order they occur in the file, they are
1270 * sometimes tested with <=
1272 #define MODE_START_OF_FILE 1
1273 #define MODE_SETTINGS 2
1274 #define MODE_DESCRIPTION 3
1275 #define MODE_ALIAS 4
1276 #define MODE_ACTIONS 5
1278 int mode = MODE_START_OF_FILE;
1281 struct url_actions *last_perm;
1282 struct url_actions *perm;
1283 char buf[BUFFER_SIZE];
1284 struct file_list *fs;
1285 struct action_spec * cur_action = NULL;
1286 int cur_action_used = 0;
1287 struct action_alias * alias_list = NULL;
1288 unsigned long linenum = 0;
1290 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1292 /* No need to load */
1293 csp->actions_list[fileid] = current_actions_file[fileid];
1298 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1299 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1300 "with their complete file names.", csp->config->actions_file[fileid]);
1301 return 1; /* never get here */
1304 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1305 if (last_perm == NULL)
1307 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1308 csp->config->actions_file[fileid]);
1309 return 1; /* never get here */
1312 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1314 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1315 csp->config->actions_file[fileid]);
1316 return 1; /* never get here */
1319 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1323 /* It's a header block */
1326 /* It's {{settings}} or {{alias}} */
1327 size_t len = strlen(buf);
1328 char * start = buf + 2;
1329 char * end = buf + len - 1;
1330 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1334 log_error(LOG_LEVEL_FATAL,
1335 "can't load actions file '%s': invalid line (%lu): %s",
1336 csp->config->actions_file[fileid], linenum, buf);
1337 return 1; /* never get here */
1340 /* Trim leading and trailing whitespace. */
1348 log_error(LOG_LEVEL_FATAL,
1349 "can't load actions file '%s': invalid line (%lu): {{ }}",
1350 csp->config->actions_file[fileid], linenum);
1351 return 1; /* never get here */
1355 * An actionsfile can optionally contain the following blocks.
1356 * They *MUST* be in this order, to simplify processing:
1362 * ...free text, format TBD, but no line may start with a '{'...
1367 * The actual actions must be *after* these special blocks.
1368 * None of these special blocks may be repeated.
1371 if (0 == strcmpic(start, "settings"))
1373 /* it's a {{settings}} block */
1374 if (mode >= MODE_SETTINGS)
1376 /* {{settings}} must be first thing in file and must only
1380 log_error(LOG_LEVEL_FATAL,
1381 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1382 csp->config->actions_file[fileid], linenum);
1384 mode = MODE_SETTINGS;
1386 else if (0 == strcmpic(start, "description"))
1388 /* it's a {{description}} block */
1389 if (mode >= MODE_DESCRIPTION)
1391 /* {{description}} is a singleton and only {{settings}} may proceed it
1394 log_error(LOG_LEVEL_FATAL,
1395 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1396 csp->config->actions_file[fileid], linenum);
1398 mode = MODE_DESCRIPTION;
1400 else if (0 == strcmpic(start, "alias"))
1402 /* it's an {{alias}} block */
1403 if (mode >= MODE_ALIAS)
1405 /* {{alias}} must be first thing in file, possibly after
1406 * {{settings}} and {{description}}
1408 * {{alias}} must only appear once.
1410 * Note that these are new restrictions introduced in
1411 * v2.9.10 in order to make actionsfile editing simpler.
1412 * (Otherwise, reordering actionsfile entries without
1413 * completely rewriting the file becomes non-trivial)
1416 log_error(LOG_LEVEL_FATAL,
1417 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1418 csp->config->actions_file[fileid], linenum);
1424 /* invalid {{something}} block */
1426 log_error(LOG_LEVEL_FATAL,
1427 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1428 csp->config->actions_file[fileid], linenum, start);
1429 return 1; /* never get here */
1434 /* It's an actions block */
1436 char actions_buf[BUFFER_SIZE];
1440 mode = MODE_ACTIONS;
1442 /* free old action */
1445 if (!cur_action_used)
1447 free_action_spec(cur_action);
1451 cur_action_used = 0;
1452 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1453 if (cur_action == NULL)
1456 log_error(LOG_LEVEL_FATAL,
1457 "can't load actions file '%s': out of memory",
1458 csp->config->actions_file[fileid]);
1459 return 1; /* never get here */
1461 init_action(cur_action);
1464 strlcpy(actions_buf, buf + 1, sizeof(actions_buf));
1466 /* check we have a trailing } and then trim it */
1467 end = actions_buf + strlen(actions_buf) - 1;
1472 log_error(LOG_LEVEL_FATAL,
1473 "can't load actions file '%s': invalid line (%lu): %s",
1474 csp->config->actions_file[fileid], linenum, buf);
1475 return 1; /* never get here */
1479 /* trim any whitespace immediately inside {} */
1482 if (get_actions(actions_buf, alias_list, cur_action))
1486 log_error(LOG_LEVEL_FATAL,
1487 "can't load actions file '%s': invalid line (%lu): %s",
1488 csp->config->actions_file[fileid], linenum, buf);
1489 return 1; /* never get here */
1493 else if (mode == MODE_SETTINGS)
1496 * Part of the {{settings}} block.
1497 * For now only serves to check if the file's minimum Privoxy
1498 * version requirement is met, but we may want to read & check
1499 * permissions when we go multi-user.
1501 if (!strncmp(buf, "for-privoxy-version=", 20))
1503 char *version_string, *fields[3];
1506 if ((version_string = strdup(buf + 20)) == NULL)
1509 log_error(LOG_LEVEL_FATAL,
1510 "can't load actions file '%s': out of memory!",
1511 csp->config->actions_file[fileid]);
1512 return 1; /* never get here */
1515 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1517 if (num_fields < 1 || atoi(fields[0]) == 0)
1519 log_error(LOG_LEVEL_ERROR,
1520 "While loading actions file '%s': invalid line (%lu): %s",
1521 csp->config->actions_file[fileid], linenum, buf);
1523 else if ( atoi(fields[0]) > VERSION_MAJOR
1524 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1525 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1528 log_error(LOG_LEVEL_FATAL,
1529 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1530 csp->config->actions_file[fileid], linenum, buf );
1531 return 1; /* never get here */
1533 free(version_string);
1536 else if (mode == MODE_DESCRIPTION)
1539 * Part of the {{description}} block.
1543 else if (mode == MODE_ALIAS)
1548 char actions_buf[BUFFER_SIZE];
1549 struct action_alias * new_alias;
1551 char * start = strchr(buf, '=');
1554 if ((start == NULL) || (start == buf))
1556 log_error(LOG_LEVEL_FATAL,
1557 "can't load actions file '%s': invalid alias line (%lu): %s",
1558 csp->config->actions_file[fileid], linenum, buf);
1559 return 1; /* never get here */
1562 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1565 log_error(LOG_LEVEL_FATAL,
1566 "can't load actions file '%s': out of memory!",
1567 csp->config->actions_file[fileid]);
1568 return 1; /* never get here */
1571 /* Eat any the whitespace before the '=' */
1573 while ((*end == ' ') || (*end == '\t'))
1576 * we already know we must have at least 1 non-ws char
1577 * at start of buf - no need to check
1583 /* Eat any the whitespace after the '=' */
1585 while ((*start == ' ') || (*start == '\t'))
1591 log_error(LOG_LEVEL_FATAL,
1592 "can't load actions file '%s': invalid alias line (%lu): %s",
1593 csp->config->actions_file[fileid], linenum, buf);
1594 return 1; /* never get here */
1597 if ((new_alias->name = strdup(buf)) == NULL)
1600 log_error(LOG_LEVEL_FATAL,
1601 "can't load actions file '%s': out of memory!",
1602 csp->config->actions_file[fileid]);
1603 return 1; /* never get here */
1606 strlcpy(actions_buf, start, sizeof(actions_buf));
1608 if (get_actions(actions_buf, alias_list, new_alias->action))
1612 log_error(LOG_LEVEL_FATAL,
1613 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1614 csp->config->actions_file[fileid], linenum, buf, start);
1615 return 1; /* never get here */
1619 new_alias->next = alias_list;
1620 alias_list = new_alias;
1622 else if (mode == MODE_ACTIONS)
1624 /* it's a URL pattern */
1626 /* allocate a new node */
1627 if ((perm = zalloc(sizeof(*perm))) == NULL)
1630 log_error(LOG_LEVEL_FATAL,
1631 "can't load actions file '%s': out of memory!",
1632 csp->config->actions_file[fileid]);
1633 return 1; /* never get here */
1636 perm->action = cur_action;
1637 cur_action_used = 1;
1639 /* Save the URL pattern */
1640 if (create_url_spec(perm->url, buf))
1643 log_error(LOG_LEVEL_FATAL,
1644 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1645 csp->config->actions_file[fileid], linenum, buf);
1646 return 1; /* never get here */
1649 /* add it to the list */
1650 last_perm->next = perm;
1653 else if (mode == MODE_START_OF_FILE)
1655 /* oops - please have a {} line as 1st line in file. */
1657 log_error(LOG_LEVEL_FATAL,
1658 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1659 csp->config->actions_file[fileid], linenum, buf);
1660 return 1; /* never get here */
1664 /* How did we get here? This is impossible! */
1666 log_error(LOG_LEVEL_FATAL,
1667 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1668 csp->config->actions_file[fileid], mode);
1669 return 1; /* never get here */
1675 if (!cur_action_used)
1677 free_action_spec(cur_action);
1679 free_alias_list(alias_list);
1681 /* the old one is now obsolete */
1682 if (current_actions_file[fileid])
1684 current_actions_file[fileid]->unloader = unload_actions_file;
1687 fs->next = files->next;
1689 current_actions_file[fileid] = fs;
1691 csp->actions_list[fileid] = fs;
1698 /*********************************************************************
1700 * Function : actions_to_text
1702 * Description : Converts a actionsfile entry from the internal
1703 * structure into a text line. The output is split
1704 * into one line for each action with line continuation.
1707 * 1 : action = The action to format.
1709 * Returns : A string. Caller must free it.
1710 * NULL on out-of-memory error.
1712 *********************************************************************/
1713 char * actions_to_text(const struct action_spec *action)
1715 unsigned mask = action->mask;
1716 unsigned add = action->add;
1717 char * result = strdup("");
1718 struct list_entry * lst;
1720 /* sanity - prevents "-feature +feature" */
1724 #define DEFINE_ACTION_BOOL(__name, __bit) \
1725 if (!(mask & __bit)) \
1727 string_append(&result, " -" __name " \\\n"); \
1729 else if (add & __bit) \
1731 string_append(&result, " +" __name " \\\n"); \
1734 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1735 if (!(mask & __bit)) \
1737 string_append(&result, " -" __name " \\\n"); \
1739 else if (add & __bit) \
1741 string_append(&result, " +" __name "{"); \
1742 string_append(&result, action->string[__index]); \
1743 string_append(&result, "} \\\n"); \
1746 #define DEFINE_ACTION_MULTI(__name, __index) \
1747 if (action->multi_remove_all[__index]) \
1749 string_append(&result, " -" __name " \\\n"); \
1753 lst = action->multi_remove[__index]->first; \
1756 string_append(&result, " -" __name "{"); \
1757 string_append(&result, lst->str); \
1758 string_append(&result, "} \\\n"); \
1762 lst = action->multi_add[__index]->first; \
1765 string_append(&result, " +" __name "{"); \
1766 string_append(&result, lst->str); \
1767 string_append(&result, "} \\\n"); \
1771 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1773 #include "actionlist.h"
1775 #undef DEFINE_ACTION_MULTI
1776 #undef DEFINE_ACTION_STRING
1777 #undef DEFINE_ACTION_BOOL
1778 #undef DEFINE_ACTION_ALIAS
1784 /*********************************************************************
1786 * Function : actions_to_html
1788 * Description : Converts a actionsfile entry from numeric form
1789 * ("mask" and "add") to a <br>-seperated HTML string
1790 * in which each action is linked to its chapter in
1794 * 1 : csp = Client state (for config)
1795 * 2 : action = Action spec to be converted
1797 * Returns : A string. Caller must free it.
1798 * NULL on out-of-memory error.
1800 *********************************************************************/
1801 char * actions_to_html(const struct client_state *csp,
1802 const struct action_spec *action)
1804 unsigned mask = action->mask;
1805 unsigned add = action->add;
1806 char * result = strdup("");
1807 struct list_entry * lst;
1809 /* sanity - prevents "-feature +feature" */
1813 #define DEFINE_ACTION_BOOL(__name, __bit) \
1814 if (!(mask & __bit)) \
1816 string_append(&result, "\n<br>-"); \
1817 string_join(&result, add_help_link(__name, csp->config)); \
1819 else if (add & __bit) \
1821 string_append(&result, "\n<br>+"); \
1822 string_join(&result, add_help_link(__name, csp->config)); \
1825 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1826 if (!(mask & __bit)) \
1828 string_append(&result, "\n<br>-"); \
1829 string_join(&result, add_help_link(__name, csp->config)); \
1831 else if (add & __bit) \
1833 string_append(&result, "\n<br>+"); \
1834 string_join(&result, add_help_link(__name, csp->config)); \
1835 string_append(&result, "{"); \
1836 string_join(&result, html_encode(action->string[__index])); \
1837 string_append(&result, "}"); \
1840 #define DEFINE_ACTION_MULTI(__name, __index) \
1841 if (action->multi_remove_all[__index]) \
1843 string_append(&result, "\n<br>-"); \
1844 string_join(&result, add_help_link(__name, csp->config)); \
1848 lst = action->multi_remove[__index]->first; \
1851 string_append(&result, "\n<br>-"); \
1852 string_join(&result, add_help_link(__name, csp->config)); \
1853 string_append(&result, "{"); \
1854 string_join(&result, html_encode(lst->str)); \
1855 string_append(&result, "}"); \
1859 lst = action->multi_add[__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 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1872 #include "actionlist.h"
1874 #undef DEFINE_ACTION_MULTI
1875 #undef DEFINE_ACTION_STRING
1876 #undef DEFINE_ACTION_BOOL
1877 #undef DEFINE_ACTION_ALIAS
1879 /* trim leading <br> */
1880 if (result && *result)
1883 result = strdup(result + 5);
1891 /*********************************************************************
1893 * Function : current_actions_to_html
1895 * Description : Converts a curren action spec to a <br> seperated HTML
1896 * text in which each action is linked to its chapter in
1900 * 1 : csp = Client state (for config)
1901 * 2 : action = Current action spec to be converted
1903 * Returns : A string. Caller must free it.
1904 * NULL on out-of-memory error.
1906 *********************************************************************/
1907 char *current_action_to_html(const struct client_state *csp,
1908 const struct current_action_spec *action)
1910 unsigned long flags = action->flags;
1911 struct list_entry * lst;
1912 char *result = strdup("");
1913 char *active = strdup("");
1914 char *inactive = strdup("");
1916 #define DEFINE_ACTION_BOOL(__name, __bit) \
1917 if (flags & __bit) \
1919 string_append(&active, "\n<br>+"); \
1920 string_join(&active, add_help_link(__name, csp->config)); \
1924 string_append(&inactive, "\n<br>-"); \
1925 string_join(&inactive, add_help_link(__name, csp->config)); \
1928 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1929 if (flags & __bit) \
1931 string_append(&active, "\n<br>+"); \
1932 string_join(&active, add_help_link(__name, csp->config)); \
1933 string_append(&active, "{"); \
1934 string_join(&active, html_encode(action->string[__index])); \
1935 string_append(&active, "}"); \
1939 string_append(&inactive, "\n<br>-"); \
1940 string_join(&inactive, add_help_link(__name, csp->config)); \
1943 #define DEFINE_ACTION_MULTI(__name, __index) \
1944 lst = action->multi[__index]->first; \
1947 string_append(&inactive, "\n<br>-"); \
1948 string_join(&inactive, add_help_link(__name, csp->config)); \
1954 string_append(&active, "\n<br>+"); \
1955 string_join(&active, add_help_link(__name, csp->config)); \
1956 string_append(&active, "{"); \
1957 string_join(&active, html_encode(lst->str)); \
1958 string_append(&active, "}"); \
1963 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1965 #include "actionlist.h"
1967 #undef DEFINE_ACTION_MULTI
1968 #undef DEFINE_ACTION_STRING
1969 #undef DEFINE_ACTION_BOOL
1970 #undef DEFINE_ACTION_ALIAS
1974 string_append(&result, active);
1977 string_append(&result, "\n<br>");
1978 if (inactive != NULL)
1980 string_append(&result, inactive);