1 const char actions_rcs[] = "$Id: actions.c,v 1.48 2008/03/28 18:17:14 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.48 2008/03/28 18:17:14 fabiankeil
37 * In action_used_to_be_valid(), loop through an array of formerly
38 * valid actions instead of using an OR-chain of strcmpic() calls.
40 * Revision 1.47 2008/03/28 15:13:37 fabiankeil
41 * Remove inspect-jpegs action.
43 * Revision 1.46 2008/03/27 18:27:20 fabiankeil
44 * Remove kill-popups action.
46 * Revision 1.45 2008/03/24 11:21:02 fabiankeil
47 * Share the action settings for multiple patterns in the same
48 * section so we waste less memory for gigantic block lists
49 * (and load them slightly faster). Reported by Franz Schwartau.
51 * Revision 1.44 2008/03/04 18:30:34 fabiankeil
52 * Remove the treat-forbidden-connects-like-blocks action. We now
53 * use the "blocked" page for forbidden CONNECT requests by default.
55 * Revision 1.43 2008/03/01 14:00:43 fabiankeil
56 * Let the block action take the reason for the block
57 * as argument and show it on the "blocked" page.
59 * Revision 1.42 2008/02/09 15:15:38 fabiankeil
60 * List active and inactive actions in the show-url-info's
61 * "Final results" section separately. Patch submitted by Lee
62 * in #1830056, modified to list active actions first.
64 * Revision 1.41 2008/01/28 20:17:40 fabiankeil
65 * - Mark some parameters as immutable.
66 * - Hide update_action_bits_for_all_tags() while it's unused.
68 * Revision 1.40 2007/05/21 10:26:50 fabiankeil
69 * - Use strlcpy() instead of strcpy().
70 * - Provide a reason why loading the actions
71 * file might have failed.
73 * Revision 1.39 2007/04/17 18:21:45 fabiankeil
74 * Split update_action_bits() into
75 * update_action_bits_for_all_tags()
76 * and update_action_bits_for_tag().
78 * Revision 1.38 2007/04/15 16:39:20 fabiankeil
79 * Introduce tags as alternative way to specify which
80 * actions apply to a request. At the moment tags can be
81 * created based on client and server headers.
83 * Revision 1.37 2007/03/11 15:56:12 fabiankeil
84 * Add kludge to log unknown aliases and actions before exiting.
86 * Revision 1.36 2006/12/28 17:15:42 fabiankeil
87 * Fix gcc43 conversion warning.
89 * Revision 1.35 2006/07/18 14:48:45 david__schmidt
90 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
91 * with what was really the latest development (the v_3_0_branch branch)
93 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
94 * Multiple filter file support
96 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
97 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
98 * this becomes important when jcc.h gets included later down the line.
100 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
101 * - Implemented Privoxy version requirement through
102 * for-privoxy-version= statement in {{settings}}
104 * - Fix for unchecked out-of-memory condition
106 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
107 * Fixed memory leak reported by Dan Price in Bug #694713
109 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
110 * Extended unload_current_actions_file() to multiple AFs.
111 * Thanks to Oliver Stoeneberg for the hint.
113 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
114 * Change unsigned to unsigned long in actions_name struct. This closes
115 * SourceForge Bug #539284.
117 * Revision 1.32 2002/05/12 21:36:29 jongfoster
118 * Correcting function comments
120 * Revision 1.31 2002/05/06 07:56:50 oes
121 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
123 * Revision 1.30 2002/04/30 11:14:52 oes
124 * Made csp the first parameter in *action_to_html
126 * Revision 1.29 2002/04/26 19:30:54 jongfoster
127 * - current_action_to_html(): Adding help link for the "-" form of
128 * one-string actions.
129 * - Some actions had "<br>-", some "<br> -" (note the space).
130 * Standardizing on no space.
131 * - Greatly simplifying some of the code by using string_join()
134 * Revision 1.28 2002/04/26 12:53:15 oes
135 * - CGI AF editor now writes action lines split into
136 * single lines with line continuation
137 * - actions_to_html now embeds each action name in
139 * - current_action_to_text is now called current_action_to_html
140 * and acts like actions_to_html
142 * Revision 1.27 2002/04/24 02:10:31 oes
143 * - Jon's patch for multiple AFs:
144 * - split load_actions_file, add load_one_actions_file
145 * - make csp->actions_list files an array
146 * - remember file id with each action
147 * - Copy_action now frees dest action before copying
149 * Revision 1.26 2002/03/26 22:29:54 swa
150 * we have a new homepage!
152 * Revision 1.25 2002/03/24 13:25:43 swa
153 * name change related issues
155 * Revision 1.24 2002/03/16 23:54:06 jongfoster
156 * Adding graceful termination feature, to help look for memory leaks.
157 * If you enable this (which, by design, has to be done by hand
158 * editing config.h) and then go to http://i.j.b/die, then the program
159 * will exit cleanly after the *next* request. It should free all the
160 * memory that was used.
162 * Revision 1.23 2002/03/07 03:46:16 oes
163 * Fixed compiler warnings
165 * Revision 1.22 2002/01/21 00:27:02 jongfoster
166 * Allowing free_action(NULL).
167 * Moving the functions that #include actionlist.h to the end of the file,
168 * because the Visual C++ 97 debugger gets extremely confused if you try
169 * to debug any code that comes after them in the file.
171 * Revision 1.21 2002/01/17 20:54:44 jongfoster
172 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
174 * Revision 1.20 2001/11/22 21:56:49 jongfoster
175 * Making action_spec->flags into an unsigned long rather than just an
177 * Fixing a bug in the display of -add-header and -wafer
179 * Revision 1.19 2001/11/13 00:14:07 jongfoster
180 * Fixing stupid bug now I've figured out what || means.
181 * (It always returns 0 or 1, not one of it's paramaters.)
183 * Revision 1.18 2001/11/07 00:06:06 steudten
184 * Add line number in error output for lineparsing for
187 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
188 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
189 * threads to call select() simultaneously. So, it's time to do a real, live,
190 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
191 * (native). Both versions will work, but using __OS2__ offers multi-threading.
193 * Revision 1.16 2001/10/23 21:30:30 jongfoster
194 * Adding error-checking to selected functions.
196 * Revision 1.15 2001/10/14 21:58:22 jongfoster
197 * Adding support for the CGI-based editor:
198 * - Exported get_actions()
199 * - Added new function free_alias_list()
200 * - Added support for {{settings}} and {{description}} blocks
201 * in the actions file. They are currently ignored.
202 * - Added restriction to only one {{alias}} block which must appear
203 * first in the file, to simplify the editor's rewriting rules.
204 * - Note that load_actions_file() is no longer used by the CGI-based
205 * editor, but some of the other routines in this file are.
207 * Revision 1.14 2001/09/22 16:36:59 jongfoster
208 * Removing unused parameter fs from read_config_line()
210 * Revision 1.13 2001/09/16 15:47:37 jongfoster
211 * First version of CGI-based edit interface. This is very much a
212 * work-in-progress, and you can't actually use it to edit anything
213 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
214 * to have any effect.
216 * Revision 1.12 2001/09/16 13:21:27 jongfoster
217 * Changes to use new list functions.
219 * Revision 1.11 2001/09/14 00:17:32 jongfoster
220 * Tidying up memory allocation. New function init_action().
222 * Revision 1.10 2001/09/10 10:14:34 oes
223 * Removing unused variable
225 * Revision 1.9 2001/07/30 22:08:36 jongfoster
226 * Tidying up #defines:
227 * - All feature #defines are now of the form FEATURE_xxx
228 * - Permanently turned off WIN_GUI_EDIT
229 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
231 * Revision 1.8 2001/06/29 13:19:52 oes
232 * Removed logentry from cancelled commit
234 * Revision 1.7 2001/06/09 10:55:28 jongfoster
235 * Changing BUFSIZ ==> BUFFER_SIZE
237 * Revision 1.6 2001/06/07 23:04:34 jongfoster
238 * Made get_actions() static.
240 * Revision 1.5 2001/06/03 19:11:48 oes
241 * adapted to new enlist_unique arg format
243 * Revision 1.4 2001/06/01 20:03:42 jongfoster
244 * Better memory management - current_action->strings[] now
245 * contains copies of the strings, not the original.
247 * Revision 1.3 2001/06/01 18:49:17 jongfoster
248 * Replaced "list_share" with "list" - the tiny memory gain was not
249 * worth the extra complexity.
251 * Revision 1.2 2001/05/31 21:40:00 jongfoster
252 * Removing some commented out, obsolete blocks of code.
254 * Revision 1.1 2001/05/31 21:16:46 jongfoster
255 * Moved functions to process the action list into this new file.
258 *********************************************************************/
268 #ifdef FEATURE_PTHREAD
276 #include "miscutil.h"
280 #include "urlmatch.h"
284 const char actions_h_rcs[] = ACTIONS_H_VERSION;
288 * We need the main list of options.
290 * First, we need a way to tell between boolean, string, and multi-string
291 * options. For string and multistring options, we also need to be
292 * able to tell the difference between a "+" and a "-". (For bools,
293 * the "+"/"-" information is encoded in "add" and "mask"). So we use
294 * an enumerated type (well, the preprocessor equivalent). Here are
297 #define AV_NONE 0 /* +opt -opt */
298 #define AV_ADD_STRING 1 /* +stropt{string} */
299 #define AV_REM_STRING 2 /* -stropt */
300 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
301 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
304 * We need a structure to hold the name, flag changes,
305 * type, and string index.
310 unsigned long mask; /* a bit set to "0" = remove action */
311 unsigned long add; /* a bit set to "1" = add action */
312 int takes_value; /* an AV_... constant */
313 int index; /* index into strings[] or multi[] */
317 * And with those building blocks in place, here's the array.
319 static const struct action_name action_names[] =
322 * Well actually there's no data here - it's in actionlist.h
323 * This keeps it together to make it easy to change.
325 * Here's the macros used to format it:
327 #define DEFINE_ACTION_MULTI(name,index) \
328 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
329 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
330 #define DEFINE_ACTION_STRING(name,flag,index) \
331 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
332 { "-" name, ~flag, 0, AV_REM_STRING, index },
333 #define DEFINE_ACTION_BOOL(name,flag) \
334 { "+" name, ACTION_MASK_ALL, flag }, \
335 { "-" name, ~flag, 0 },
336 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
338 #include "actionlist.h"
340 #undef DEFINE_ACTION_MULTI
341 #undef DEFINE_ACTION_STRING
342 #undef DEFINE_ACTION_BOOL
343 #undef DEFINE_ACTION_ALIAS
345 { NULL, 0, 0 } /* End marker */
349 static int load_one_actions_file(struct client_state *csp, int fileid);
352 /*********************************************************************
354 * Function : merge_actions
356 * Description : Merge two actions together.
357 * Similar to "dest += src".
360 * 1 : dest = Actions to modify.
361 * 2 : src = Action to add.
363 * Returns : JB_ERR_OK or JB_ERR_MEMORY
365 *********************************************************************/
366 jb_err merge_actions (struct action_spec *dest,
367 const struct action_spec *src)
372 dest->mask &= src->mask;
373 dest->add &= src->mask;
374 dest->add |= src->add;
376 for (i = 0; i < ACTION_STRING_COUNT; i++)
378 char * str = src->string[i];
381 freez(dest->string[i]);
382 dest->string[i] = strdup(str);
383 if (NULL == dest->string[i])
385 return JB_ERR_MEMORY;
390 for (i = 0; i < ACTION_MULTI_COUNT; i++)
392 if (src->multi_remove_all[i])
394 /* Remove everything from dest */
395 list_remove_all(dest->multi_remove[i]);
396 dest->multi_remove_all[i] = 1;
398 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
400 else if (dest->multi_remove_all[i])
403 * dest already removes everything, so we only need to worry
406 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
407 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
411 /* No "remove all"s to worry about. */
412 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
413 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
414 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
427 /*********************************************************************
429 * Function : copy_action
431 * Description : Copy an action_specs.
432 * Similar to "dest = src".
435 * 1 : dest = Destination of copy.
436 * 2 : src = Source for copy.
440 *********************************************************************/
441 jb_err copy_action (struct action_spec *dest,
442 const struct action_spec *src)
445 jb_err err = JB_ERR_OK;
448 memset(dest, '\0', sizeof(*dest));
450 dest->mask = src->mask;
451 dest->add = src->add;
453 for (i = 0; i < ACTION_STRING_COUNT; i++)
455 char * str = src->string[i];
461 return JB_ERR_MEMORY;
463 dest->string[i] = str;
467 for (i = 0; i < ACTION_MULTI_COUNT; i++)
469 dest->multi_remove_all[i] = src->multi_remove_all[i];
470 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
475 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
484 /*********************************************************************
486 * Function : free_action_spec
488 * Description : Frees an action_spec and the memory used by it.
491 * 1 : src = Source to free.
495 *********************************************************************/
496 void free_action_spec(struct action_spec *src)
503 /*********************************************************************
505 * Function : free_action
507 * Description : Destroy an action_spec. Frees memory used by it,
508 * except for the memory used by the struct action_spec
512 * 1 : src = Source to free.
516 *********************************************************************/
517 void free_action (struct action_spec *src)
526 for (i = 0; i < ACTION_STRING_COUNT; i++)
528 freez(src->string[i]);
531 for (i = 0; i < ACTION_MULTI_COUNT; i++)
533 destroy_list(src->multi_remove[i]);
534 destroy_list(src->multi_add[i]);
537 memset(src, '\0', sizeof(*src));
541 /*********************************************************************
543 * Function : get_action_token
545 * Description : Parses a line for the first action.
546 * Modifies it's input array, doesn't allocate memory.
548 * *line=" +abc{def} -ghi "
555 * 1 : line = [in] The line containing the action.
556 * [out] Start of next action on line, or
557 * NULL if we reached the end of line before
558 * we found an action.
559 * 2 : name = [out] Start of action name, null
560 * terminated. NULL on EOL
561 * 3 : value = [out] Start of action value, null
562 * terminated. NULL if none or EOL.
564 * Returns : JB_ERR_OK => Ok
565 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
567 *********************************************************************/
568 jb_err get_action_token(char **line, char **name, char **value)
573 /* set default returns */
578 /* Eat any leading whitespace */
579 while ((*str == ' ') || (*str == '\t'))
591 /* null name, just value is prohibited */
598 while (((ch = *str) != '\0') &&
599 (ch != ' ') && (ch != '\t') && (ch != '{'))
603 /* error, '}' without '{' */
615 /* EOL - be careful not to run off buffer */
620 /* More to parse next time. */
629 str = strchr(str, '}');
646 /*********************************************************************
648 * Function : action_used_to_be_valid
650 * Description : Checks if unrecognized actions were valid in earlier
654 * 1 : action = The string containing the action to check.
656 * Returns : True if yes, otherwise false.
658 *********************************************************************/
659 static int action_used_to_be_valid(const char *action)
661 static const char *formerly_valid_actions[] = {
664 "send-vanilla-wafer",
666 "treat-forbidden-connects-like-blocks",
672 for (i = 0; i < SZ(formerly_valid_actions); i++)
674 if (0 == strcmpic(action, formerly_valid_actions[i]))
683 /*********************************************************************
685 * Function : get_actions
687 * Description : Parses a list of actions.
690 * 1 : line = The string containing the actions.
691 * Will be written to by this function.
692 * 2 : alias_list = Custom alias list, or NULL for none.
693 * 3 : cur_action = Where to store the action. Caller
696 * Returns : JB_ERR_OK => Ok
697 * JB_ERR_PARSE => Parse error (line was trashed anyway)
698 * nonzero => Out of memory (line was trashed anyway)
700 *********************************************************************/
701 jb_err get_actions(char *line,
702 struct action_alias * alias_list,
703 struct action_spec *cur_action)
706 init_action(cur_action);
707 cur_action->mask = ACTION_MASK_ALL;
711 char * option = NULL;
714 err = get_action_token(&line, &option, &value);
722 /* handle option in 'option' */
724 /* Check for standard action name */
725 const struct action_name * action = action_names;
727 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
731 if (action->name != NULL)
734 cur_action->mask &= action->mask;
735 cur_action->add &= action->mask;
736 cur_action->add |= action->add;
738 switch (action->takes_value)
741 /* ignore any option. */
745 /* add single string. */
747 if ((value == NULL) || (*value == '\0'))
749 if (0 != strcmpic(action->name, "block"))
752 * XXX: Temporary backwards compatibility hack.
754 static int complaint_shown = 0;
755 value = "No reason specified.";
756 if (!complaint_shown)
758 log_error(LOG_LEVEL_ERROR, "At least one block "
759 "without reason found. This may become a fatal "
760 "error in future versions.");
769 /* FIXME: should validate option string here */
770 freez (cur_action->string[action->index]);
771 cur_action->string[action->index] = strdup(value);
772 if (NULL == cur_action->string[action->index])
774 return JB_ERR_MEMORY;
780 /* remove single string. */
782 freez (cur_action->string[action->index]);
787 /* append multi string. */
789 struct list * remove_p = cur_action->multi_remove[action->index];
790 struct list * add_p = cur_action->multi_add[action->index];
792 if ((value == NULL) || (*value == '\0'))
797 list_remove_item(remove_p, value);
798 err = enlist_unique(add_p, value, 0);
807 /* remove multi string. */
809 struct list * remove_p = cur_action->multi_remove[action->index];
810 struct list * add_p = cur_action->multi_add[action->index];
812 if ( (value == NULL) || (*value == '\0')
813 || ((*value == '*') && (value[1] == '\0')) )
816 * no option, or option == "*".
820 list_remove_all(remove_p);
821 list_remove_all(add_p);
822 cur_action->multi_remove_all[action->index] = 1;
826 /* Valid option - remove only 1 option */
828 if ( !cur_action->multi_remove_all[action->index] )
830 /* there isn't a catch-all in the remove list already */
831 err = enlist_unique(remove_p, value, 0);
837 list_remove_item(add_p, value);
842 /* Shouldn't get here unless there's memory corruption. */
849 /* try user aliases. */
850 const struct action_alias * alias = alias_list;
852 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
859 merge_actions(cur_action, alias->action);
861 else if ((2 < strlen(option)) && action_used_to_be_valid(option+1))
863 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
864 "in this Privoxy release. Ignored.", option+1);
868 /* Bad action name */
870 * XXX: This is a fatal error and Privoxy will later on exit
871 * in load_one_actions_file() because of an "invalid line".
873 * It would be preferable to name the offending option in that
874 * error message, but currently there is no way to do that and
875 * we have to live with two error messages for basically the
878 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
889 /*********************************************************************
891 * Function : init_current_action
893 * Description : Zero out an action.
896 * 1 : dest = An uninitialized current_action_spec.
900 *********************************************************************/
901 void init_current_action (struct current_action_spec *dest)
903 memset(dest, '\0', sizeof(*dest));
905 dest->flags = ACTION_MOST_COMPATIBLE;
909 /*********************************************************************
911 * Function : init_action
913 * Description : Zero out an action.
916 * 1 : dest = An uninitialized action_spec.
920 *********************************************************************/
921 void init_action (struct action_spec *dest)
923 memset(dest, '\0', sizeof(*dest));
927 /*********************************************************************
929 * Function : merge_current_action
931 * Description : Merge two actions together.
932 * Similar to "dest += src".
933 * Differences between this and merge_actions()
934 * is that this one doesn't allocate memory for
935 * strings (so "src" better be in memory for at least
936 * as long as "dest" is, and you'd better free
937 * "dest" using "free_current_action").
938 * Also, there is no mask or remove lists in dest.
939 * (If we're applying it to a URL, we don't need them)
942 * 1 : dest = Current actions, to modify.
943 * 2 : src = Action to add.
945 * Returns 0 : no error
946 * !=0 : error, probably JB_ERR_MEMORY.
948 *********************************************************************/
949 jb_err merge_current_action (struct current_action_spec *dest,
950 const struct action_spec *src)
953 jb_err err = JB_ERR_OK;
955 dest->flags &= src->mask;
956 dest->flags |= src->add;
958 for (i = 0; i < ACTION_STRING_COUNT; i++)
960 char * str = src->string[i];
966 return JB_ERR_MEMORY;
968 freez(dest->string[i]);
969 dest->string[i] = str;
973 for (i = 0; i < ACTION_MULTI_COUNT; i++)
975 if (src->multi_remove_all[i])
977 /* Remove everything from dest, then add src->multi_add */
978 err = list_duplicate(dest->multi[i], src->multi_add[i]);
986 list_remove_list(dest->multi[i], src->multi_remove[i]);
987 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
998 /*********************************************************************
1000 * Function : update_action_bits_for_all_tags
1002 * Description : Updates the action bits based on all matching tags.
1005 * 1 : csp = Current client state (buffers, headers, etc...)
1007 * Returns : 0 if no tag matched, or
1010 *********************************************************************/
1011 int update_action_bits_for_all_tags(struct client_state *csp)
1013 struct list_entry *tag;
1016 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
1018 if (update_action_bits_for_tag(csp, tag->str))
1028 /*********************************************************************
1030 * Function : update_action_bits_for_tag
1032 * Description : Updates the action bits based on the action sections
1033 * whose tag patterns match a provided tag.
1036 * 1 : csp = Current client state (buffers, headers, etc...)
1037 * 2 : tag = The tag on which the update should be based on
1039 * Returns : 0 if no tag matched, or
1042 *********************************************************************/
1043 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
1045 struct file_list *fl;
1046 struct url_actions *b;
1052 assert(list_contains_item(csp->tags, tag));
1054 /* Run through all action files, */
1055 for (i = 0; i < MAX_AF_FILES; i++)
1057 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
1059 /* Skip empty files */
1063 /* and through all the action patterns, */
1064 for (b = b->next; NULL != b; b = b->next)
1066 /* skip the URL patterns, */
1067 if (NULL == b->url->tag_regex)
1072 /* and check if one of the tag patterns matches the tag, */
1073 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
1075 /* if it does, update the action bit map, */
1076 if (merge_current_action(csp->action, b->action))
1078 log_error(LOG_LEVEL_ERROR,
1079 "Out of memorey while changing action bits");
1081 /* and signal the change. */
1091 /*********************************************************************
1093 * Function : free_current_action
1095 * Description : Free memory used by a current_action_spec.
1096 * Does not free the current_action_spec itself.
1099 * 1 : src = Source to free.
1103 *********************************************************************/
1104 void free_current_action (struct current_action_spec *src)
1108 for (i = 0; i < ACTION_STRING_COUNT; i++)
1110 freez(src->string[i]);
1113 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1115 destroy_list(src->multi[i]);
1118 memset(src, '\0', sizeof(*src));
1122 static struct file_list *current_actions_file[MAX_AF_FILES] = {
1123 NULL, NULL, NULL, NULL, NULL,
1124 NULL, NULL, NULL, NULL, NULL
1128 #ifdef FEATURE_GRACEFUL_TERMINATION
1129 /*********************************************************************
1131 * Function : unload_current_actions_file
1133 * Description : Unloads current actions file - reset to state at
1134 * beginning of program.
1140 *********************************************************************/
1141 void unload_current_actions_file(void)
1145 for (i = 0; i < MAX_AF_FILES; i++)
1147 if (current_actions_file[i])
1149 current_actions_file[i]->unloader = unload_actions_file;
1150 current_actions_file[i] = NULL;
1154 #endif /* FEATURE_GRACEFUL_TERMINATION */
1157 /*********************************************************************
1159 * Function : unload_actions_file
1161 * Description : Unloads an actions module.
1164 * 1 : file_data = the data structure associated with the
1169 *********************************************************************/
1170 void unload_actions_file(void *file_data)
1172 struct url_actions * next;
1173 struct url_actions * cur = (struct url_actions *)file_data;
1177 free_url_spec(cur->url);
1178 if ((next == NULL) || (next->action != cur->action))
1181 * As the action settings might be shared,
1182 * we can only free them if the current
1183 * url pattern is the last one, or if the
1184 * next one is using different settings.
1186 free_action_spec(cur->action);
1194 /*********************************************************************
1196 * Function : free_alias_list
1198 * Description : Free memory used by a list of aliases.
1201 * 1 : alias_list = Linked list to free.
1205 *********************************************************************/
1206 void free_alias_list(struct action_alias *alias_list)
1208 while (alias_list != NULL)
1210 struct action_alias * next = alias_list->next;
1211 alias_list->next = NULL;
1212 freez(alias_list->name);
1213 free_action(alias_list->action);
1220 /*********************************************************************
1222 * Function : load_actions_file
1224 * Description : Read and parse all the action files and add to files
1228 * 1 : csp = Current client state (buffers, headers, etc...)
1230 * Returns : 0 => Ok, everything else is an error.
1232 *********************************************************************/
1233 int load_actions_file(struct client_state *csp)
1238 for (i = 0; i < MAX_AF_FILES; i++)
1240 if (csp->config->actions_file[i])
1242 result = load_one_actions_file(csp, i);
1248 else if (current_actions_file[i])
1250 current_actions_file[i]->unloader = unload_actions_file;
1251 current_actions_file[i] = NULL;
1258 /*********************************************************************
1260 * Function : load_one_actions_file
1262 * Description : Read and parse a action file and add to files
1266 * 1 : csp = Current client state (buffers, headers, etc...)
1267 * 2 : fileid = File index to load.
1269 * Returns : 0 => Ok, everything else is an error.
1271 *********************************************************************/
1272 static int load_one_actions_file(struct client_state *csp, int fileid)
1277 * Note: Keep these in the order they occur in the file, they are
1278 * sometimes tested with <=
1280 #define MODE_START_OF_FILE 1
1281 #define MODE_SETTINGS 2
1282 #define MODE_DESCRIPTION 3
1283 #define MODE_ALIAS 4
1284 #define MODE_ACTIONS 5
1286 int mode = MODE_START_OF_FILE;
1289 struct url_actions *last_perm;
1290 struct url_actions *perm;
1291 char buf[BUFFER_SIZE];
1292 struct file_list *fs;
1293 struct action_spec * cur_action = NULL;
1294 int cur_action_used = 0;
1295 struct action_alias * alias_list = NULL;
1296 unsigned long linenum = 0;
1298 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1300 /* No need to load */
1301 csp->actions_list[fileid] = current_actions_file[fileid];
1306 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1307 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1308 "with their complete file names.", csp->config->actions_file[fileid]);
1309 return 1; /* never get here */
1312 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1313 if (last_perm == NULL)
1315 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1316 csp->config->actions_file[fileid]);
1317 return 1; /* never get here */
1320 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1322 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1323 csp->config->actions_file[fileid]);
1324 return 1; /* never get here */
1327 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1331 /* It's a header block */
1334 /* It's {{settings}} or {{alias}} */
1335 size_t len = strlen(buf);
1336 char * start = buf + 2;
1337 char * end = buf + len - 1;
1338 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1342 log_error(LOG_LEVEL_FATAL,
1343 "can't load actions file '%s': invalid line (%lu): %s",
1344 csp->config->actions_file[fileid], linenum, buf);
1345 return 1; /* never get here */
1348 /* Trim leading and trailing whitespace. */
1356 log_error(LOG_LEVEL_FATAL,
1357 "can't load actions file '%s': invalid line (%lu): {{ }}",
1358 csp->config->actions_file[fileid], linenum);
1359 return 1; /* never get here */
1363 * An actionsfile can optionally contain the following blocks.
1364 * They *MUST* be in this order, to simplify processing:
1370 * ...free text, format TBD, but no line may start with a '{'...
1375 * The actual actions must be *after* these special blocks.
1376 * None of these special blocks may be repeated.
1379 if (0 == strcmpic(start, "settings"))
1381 /* it's a {{settings}} block */
1382 if (mode >= MODE_SETTINGS)
1384 /* {{settings}} must be first thing in file and must only
1388 log_error(LOG_LEVEL_FATAL,
1389 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1390 csp->config->actions_file[fileid], linenum);
1392 mode = MODE_SETTINGS;
1394 else if (0 == strcmpic(start, "description"))
1396 /* it's a {{description}} block */
1397 if (mode >= MODE_DESCRIPTION)
1399 /* {{description}} is a singleton and only {{settings}} may proceed it
1402 log_error(LOG_LEVEL_FATAL,
1403 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1404 csp->config->actions_file[fileid], linenum);
1406 mode = MODE_DESCRIPTION;
1408 else if (0 == strcmpic(start, "alias"))
1410 /* it's an {{alias}} block */
1411 if (mode >= MODE_ALIAS)
1413 /* {{alias}} must be first thing in file, possibly after
1414 * {{settings}} and {{description}}
1416 * {{alias}} must only appear once.
1418 * Note that these are new restrictions introduced in
1419 * v2.9.10 in order to make actionsfile editing simpler.
1420 * (Otherwise, reordering actionsfile entries without
1421 * completely rewriting the file becomes non-trivial)
1424 log_error(LOG_LEVEL_FATAL,
1425 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1426 csp->config->actions_file[fileid], linenum);
1432 /* invalid {{something}} block */
1434 log_error(LOG_LEVEL_FATAL,
1435 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1436 csp->config->actions_file[fileid], linenum, start);
1437 return 1; /* never get here */
1442 /* It's an actions block */
1444 char actions_buf[BUFFER_SIZE];
1448 mode = MODE_ACTIONS;
1450 /* free old action */
1453 if (!cur_action_used)
1455 free_action_spec(cur_action);
1459 cur_action_used = 0;
1460 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1461 if (cur_action == NULL)
1464 log_error(LOG_LEVEL_FATAL,
1465 "can't load actions file '%s': out of memory",
1466 csp->config->actions_file[fileid]);
1467 return 1; /* never get here */
1469 init_action(cur_action);
1472 strlcpy(actions_buf, buf + 1, sizeof(actions_buf));
1474 /* check we have a trailing } and then trim it */
1475 end = actions_buf + strlen(actions_buf) - 1;
1480 log_error(LOG_LEVEL_FATAL,
1481 "can't load actions file '%s': invalid line (%lu): %s",
1482 csp->config->actions_file[fileid], linenum, buf);
1483 return 1; /* never get here */
1487 /* trim any whitespace immediately inside {} */
1490 if (get_actions(actions_buf, alias_list, cur_action))
1494 log_error(LOG_LEVEL_FATAL,
1495 "can't load actions file '%s': invalid line (%lu): %s",
1496 csp->config->actions_file[fileid], linenum, buf);
1497 return 1; /* never get here */
1501 else if (mode == MODE_SETTINGS)
1504 * Part of the {{settings}} block.
1505 * For now only serves to check if the file's minimum Privoxy
1506 * version requirement is met, but we may want to read & check
1507 * permissions when we go multi-user.
1509 if (!strncmp(buf, "for-privoxy-version=", 20))
1511 char *version_string, *fields[3];
1514 if ((version_string = strdup(buf + 20)) == NULL)
1517 log_error(LOG_LEVEL_FATAL,
1518 "can't load actions file '%s': out of memory!",
1519 csp->config->actions_file[fileid]);
1520 return 1; /* never get here */
1523 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1525 if (num_fields < 1 || atoi(fields[0]) == 0)
1527 log_error(LOG_LEVEL_ERROR,
1528 "While loading actions file '%s': invalid line (%lu): %s",
1529 csp->config->actions_file[fileid], linenum, buf);
1531 else if ( atoi(fields[0]) > VERSION_MAJOR
1532 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1533 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1536 log_error(LOG_LEVEL_FATAL,
1537 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1538 csp->config->actions_file[fileid], linenum, buf );
1539 return 1; /* never get here */
1541 free(version_string);
1544 else if (mode == MODE_DESCRIPTION)
1547 * Part of the {{description}} block.
1551 else if (mode == MODE_ALIAS)
1556 char actions_buf[BUFFER_SIZE];
1557 struct action_alias * new_alias;
1559 char * start = strchr(buf, '=');
1562 if ((start == NULL) || (start == buf))
1564 log_error(LOG_LEVEL_FATAL,
1565 "can't load actions file '%s': invalid alias line (%lu): %s",
1566 csp->config->actions_file[fileid], linenum, buf);
1567 return 1; /* never get here */
1570 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1573 log_error(LOG_LEVEL_FATAL,
1574 "can't load actions file '%s': out of memory!",
1575 csp->config->actions_file[fileid]);
1576 return 1; /* never get here */
1579 /* Eat any the whitespace before the '=' */
1581 while ((*end == ' ') || (*end == '\t'))
1584 * we already know we must have at least 1 non-ws char
1585 * at start of buf - no need to check
1591 /* Eat any the whitespace after the '=' */
1593 while ((*start == ' ') || (*start == '\t'))
1599 log_error(LOG_LEVEL_FATAL,
1600 "can't load actions file '%s': invalid alias line (%lu): %s",
1601 csp->config->actions_file[fileid], linenum, buf);
1602 return 1; /* never get here */
1605 if ((new_alias->name = strdup(buf)) == NULL)
1608 log_error(LOG_LEVEL_FATAL,
1609 "can't load actions file '%s': out of memory!",
1610 csp->config->actions_file[fileid]);
1611 return 1; /* never get here */
1614 strlcpy(actions_buf, start, sizeof(actions_buf));
1616 if (get_actions(actions_buf, alias_list, new_alias->action))
1620 log_error(LOG_LEVEL_FATAL,
1621 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1622 csp->config->actions_file[fileid], linenum, buf, start);
1623 return 1; /* never get here */
1627 new_alias->next = alias_list;
1628 alias_list = new_alias;
1630 else if (mode == MODE_ACTIONS)
1632 /* it's a URL pattern */
1634 /* allocate a new node */
1635 if ((perm = zalloc(sizeof(*perm))) == NULL)
1638 log_error(LOG_LEVEL_FATAL,
1639 "can't load actions file '%s': out of memory!",
1640 csp->config->actions_file[fileid]);
1641 return 1; /* never get here */
1644 perm->action = cur_action;
1645 cur_action_used = 1;
1647 /* Save the URL pattern */
1648 if (create_url_spec(perm->url, buf))
1651 log_error(LOG_LEVEL_FATAL,
1652 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1653 csp->config->actions_file[fileid], linenum, buf);
1654 return 1; /* never get here */
1657 /* add it to the list */
1658 last_perm->next = perm;
1661 else if (mode == MODE_START_OF_FILE)
1663 /* oops - please have a {} line as 1st line in file. */
1665 log_error(LOG_LEVEL_FATAL,
1666 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1667 csp->config->actions_file[fileid], linenum, buf);
1668 return 1; /* never get here */
1672 /* How did we get here? This is impossible! */
1674 log_error(LOG_LEVEL_FATAL,
1675 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1676 csp->config->actions_file[fileid], mode);
1677 return 1; /* never get here */
1683 if (!cur_action_used)
1685 free_action_spec(cur_action);
1687 free_alias_list(alias_list);
1689 /* the old one is now obsolete */
1690 if (current_actions_file[fileid])
1692 current_actions_file[fileid]->unloader = unload_actions_file;
1695 fs->next = files->next;
1697 current_actions_file[fileid] = fs;
1699 csp->actions_list[fileid] = fs;
1706 /*********************************************************************
1708 * Function : actions_to_text
1710 * Description : Converts a actionsfile entry from the internal
1711 * structure into a text line. The output is split
1712 * into one line for each action with line continuation.
1715 * 1 : action = The action to format.
1717 * Returns : A string. Caller must free it.
1718 * NULL on out-of-memory error.
1720 *********************************************************************/
1721 char * actions_to_text(const struct action_spec *action)
1723 unsigned mask = action->mask;
1724 unsigned add = action->add;
1725 char * result = strdup("");
1726 struct list_entry * lst;
1728 /* sanity - prevents "-feature +feature" */
1732 #define DEFINE_ACTION_BOOL(__name, __bit) \
1733 if (!(mask & __bit)) \
1735 string_append(&result, " -" __name " \\\n"); \
1737 else if (add & __bit) \
1739 string_append(&result, " +" __name " \\\n"); \
1742 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1743 if (!(mask & __bit)) \
1745 string_append(&result, " -" __name " \\\n"); \
1747 else if (add & __bit) \
1749 string_append(&result, " +" __name "{"); \
1750 string_append(&result, action->string[__index]); \
1751 string_append(&result, "} \\\n"); \
1754 #define DEFINE_ACTION_MULTI(__name, __index) \
1755 if (action->multi_remove_all[__index]) \
1757 string_append(&result, " -" __name " \\\n"); \
1761 lst = action->multi_remove[__index]->first; \
1764 string_append(&result, " -" __name "{"); \
1765 string_append(&result, lst->str); \
1766 string_append(&result, "} \\\n"); \
1770 lst = action->multi_add[__index]->first; \
1773 string_append(&result, " +" __name "{"); \
1774 string_append(&result, lst->str); \
1775 string_append(&result, "} \\\n"); \
1779 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1781 #include "actionlist.h"
1783 #undef DEFINE_ACTION_MULTI
1784 #undef DEFINE_ACTION_STRING
1785 #undef DEFINE_ACTION_BOOL
1786 #undef DEFINE_ACTION_ALIAS
1792 /*********************************************************************
1794 * Function : actions_to_html
1796 * Description : Converts a actionsfile entry from numeric form
1797 * ("mask" and "add") to a <br>-seperated HTML string
1798 * in which each action is linked to its chapter in
1802 * 1 : csp = Client state (for config)
1803 * 2 : action = Action spec to be converted
1805 * Returns : A string. Caller must free it.
1806 * NULL on out-of-memory error.
1808 *********************************************************************/
1809 char * actions_to_html(const struct client_state *csp,
1810 const struct action_spec *action)
1812 unsigned mask = action->mask;
1813 unsigned add = action->add;
1814 char * result = strdup("");
1815 struct list_entry * lst;
1817 /* sanity - prevents "-feature +feature" */
1821 #define DEFINE_ACTION_BOOL(__name, __bit) \
1822 if (!(mask & __bit)) \
1824 string_append(&result, "\n<br>-"); \
1825 string_join(&result, add_help_link(__name, csp->config)); \
1827 else if (add & __bit) \
1829 string_append(&result, "\n<br>+"); \
1830 string_join(&result, add_help_link(__name, csp->config)); \
1833 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1834 if (!(mask & __bit)) \
1836 string_append(&result, "\n<br>-"); \
1837 string_join(&result, add_help_link(__name, csp->config)); \
1839 else if (add & __bit) \
1841 string_append(&result, "\n<br>+"); \
1842 string_join(&result, add_help_link(__name, csp->config)); \
1843 string_append(&result, "{"); \
1844 string_join(&result, html_encode(action->string[__index])); \
1845 string_append(&result, "}"); \
1848 #define DEFINE_ACTION_MULTI(__name, __index) \
1849 if (action->multi_remove_all[__index]) \
1851 string_append(&result, "\n<br>-"); \
1852 string_join(&result, add_help_link(__name, csp->config)); \
1856 lst = action->multi_remove[__index]->first; \
1859 string_append(&result, "\n<br>-"); \
1860 string_join(&result, add_help_link(__name, csp->config)); \
1861 string_append(&result, "{"); \
1862 string_join(&result, html_encode(lst->str)); \
1863 string_append(&result, "}"); \
1867 lst = action->multi_add[__index]->first; \
1870 string_append(&result, "\n<br>+"); \
1871 string_join(&result, add_help_link(__name, csp->config)); \
1872 string_append(&result, "{"); \
1873 string_join(&result, html_encode(lst->str)); \
1874 string_append(&result, "}"); \
1878 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1880 #include "actionlist.h"
1882 #undef DEFINE_ACTION_MULTI
1883 #undef DEFINE_ACTION_STRING
1884 #undef DEFINE_ACTION_BOOL
1885 #undef DEFINE_ACTION_ALIAS
1887 /* trim leading <br> */
1888 if (result && *result)
1891 result = strdup(result + 5);
1899 /*********************************************************************
1901 * Function : current_actions_to_html
1903 * Description : Converts a curren action spec to a <br> seperated HTML
1904 * text in which each action is linked to its chapter in
1908 * 1 : csp = Client state (for config)
1909 * 2 : action = Current action spec to be converted
1911 * Returns : A string. Caller must free it.
1912 * NULL on out-of-memory error.
1914 *********************************************************************/
1915 char *current_action_to_html(const struct client_state *csp,
1916 const struct current_action_spec *action)
1918 unsigned long flags = action->flags;
1919 struct list_entry * lst;
1920 char *result = strdup("");
1921 char *active = strdup("");
1922 char *inactive = strdup("");
1924 #define DEFINE_ACTION_BOOL(__name, __bit) \
1925 if (flags & __bit) \
1927 string_append(&active, "\n<br>+"); \
1928 string_join(&active, add_help_link(__name, csp->config)); \
1932 string_append(&inactive, "\n<br>-"); \
1933 string_join(&inactive, add_help_link(__name, csp->config)); \
1936 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1937 if (flags & __bit) \
1939 string_append(&active, "\n<br>+"); \
1940 string_join(&active, add_help_link(__name, csp->config)); \
1941 string_append(&active, "{"); \
1942 string_join(&active, html_encode(action->string[__index])); \
1943 string_append(&active, "}"); \
1947 string_append(&inactive, "\n<br>-"); \
1948 string_join(&inactive, add_help_link(__name, csp->config)); \
1951 #define DEFINE_ACTION_MULTI(__name, __index) \
1952 lst = action->multi[__index]->first; \
1955 string_append(&inactive, "\n<br>-"); \
1956 string_join(&inactive, add_help_link(__name, csp->config)); \
1962 string_append(&active, "\n<br>+"); \
1963 string_join(&active, add_help_link(__name, csp->config)); \
1964 string_append(&active, "{"); \
1965 string_join(&active, html_encode(lst->str)); \
1966 string_append(&active, "}"); \
1971 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1973 #include "actionlist.h"
1975 #undef DEFINE_ACTION_MULTI
1976 #undef DEFINE_ACTION_STRING
1977 #undef DEFINE_ACTION_BOOL
1978 #undef DEFINE_ACTION_ALIAS
1982 string_append(&result, active);
1985 string_append(&result, "\n<br>");
1986 if (inactive != NULL)
1988 string_append(&result, inactive);