1 const char actions_rcs[] = "$Id: actions.c,v 1.44 2008/03/04 18:30:34 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.44 2008/03/04 18:30:34 fabiankeil
37 * Remove the treat-forbidden-connects-like-blocks action. We now
38 * use the "blocked" page for forbidden CONNECT requests by default.
40 * Revision 1.43 2008/03/01 14:00:43 fabiankeil
41 * Let the block action take the reason for the block
42 * as argument and show it on the "blocked" page.
44 * Revision 1.42 2008/02/09 15:15:38 fabiankeil
45 * List active and inactive actions in the show-url-info's
46 * "Final results" section separately. Patch submitted by Lee
47 * in #1830056, modified to list active actions first.
49 * Revision 1.41 2008/01/28 20:17:40 fabiankeil
50 * - Mark some parameters as immutable.
51 * - Hide update_action_bits_for_all_tags() while it's unused.
53 * Revision 1.40 2007/05/21 10:26:50 fabiankeil
54 * - Use strlcpy() instead of strcpy().
55 * - Provide a reason why loading the actions
56 * file might have failed.
58 * Revision 1.39 2007/04/17 18:21:45 fabiankeil
59 * Split update_action_bits() into
60 * update_action_bits_for_all_tags()
61 * and update_action_bits_for_tag().
63 * Revision 1.38 2007/04/15 16:39:20 fabiankeil
64 * Introduce tags as alternative way to specify which
65 * actions apply to a request. At the moment tags can be
66 * created based on client and server headers.
68 * Revision 1.37 2007/03/11 15:56:12 fabiankeil
69 * Add kludge to log unknown aliases and actions before exiting.
71 * Revision 1.36 2006/12/28 17:15:42 fabiankeil
72 * Fix gcc43 conversion warning.
74 * Revision 1.35 2006/07/18 14:48:45 david__schmidt
75 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
76 * with what was really the latest development (the v_3_0_branch branch)
78 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
79 * Multiple filter file support
81 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
82 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
83 * this becomes important when jcc.h gets included later down the line.
85 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
86 * - Implemented Privoxy version requirement through
87 * for-privoxy-version= statement in {{settings}}
89 * - Fix for unchecked out-of-memory condition
91 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
92 * Fixed memory leak reported by Dan Price in Bug #694713
94 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
95 * Extended unload_current_actions_file() to multiple AFs.
96 * Thanks to Oliver Stoeneberg for the hint.
98 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
99 * Change unsigned to unsigned long in actions_name struct. This closes
100 * SourceForge Bug #539284.
102 * Revision 1.32 2002/05/12 21:36:29 jongfoster
103 * Correcting function comments
105 * Revision 1.31 2002/05/06 07:56:50 oes
106 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
108 * Revision 1.30 2002/04/30 11:14:52 oes
109 * Made csp the first parameter in *action_to_html
111 * Revision 1.29 2002/04/26 19:30:54 jongfoster
112 * - current_action_to_html(): Adding help link for the "-" form of
113 * one-string actions.
114 * - Some actions had "<br>-", some "<br> -" (note the space).
115 * Standardizing on no space.
116 * - Greatly simplifying some of the code by using string_join()
119 * Revision 1.28 2002/04/26 12:53:15 oes
120 * - CGI AF editor now writes action lines split into
121 * single lines with line continuation
122 * - actions_to_html now embeds each action name in
124 * - current_action_to_text is now called current_action_to_html
125 * and acts like actions_to_html
127 * Revision 1.27 2002/04/24 02:10:31 oes
128 * - Jon's patch for multiple AFs:
129 * - split load_actions_file, add load_one_actions_file
130 * - make csp->actions_list files an array
131 * - remember file id with each action
132 * - Copy_action now frees dest action before copying
134 * Revision 1.26 2002/03/26 22:29:54 swa
135 * we have a new homepage!
137 * Revision 1.25 2002/03/24 13:25:43 swa
138 * name change related issues
140 * Revision 1.24 2002/03/16 23:54:06 jongfoster
141 * Adding graceful termination feature, to help look for memory leaks.
142 * If you enable this (which, by design, has to be done by hand
143 * editing config.h) and then go to http://i.j.b/die, then the program
144 * will exit cleanly after the *next* request. It should free all the
145 * memory that was used.
147 * Revision 1.23 2002/03/07 03:46:16 oes
148 * Fixed compiler warnings
150 * Revision 1.22 2002/01/21 00:27:02 jongfoster
151 * Allowing free_action(NULL).
152 * Moving the functions that #include actionlist.h to the end of the file,
153 * because the Visual C++ 97 debugger gets extremely confused if you try
154 * to debug any code that comes after them in the file.
156 * Revision 1.21 2002/01/17 20:54:44 jongfoster
157 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
159 * Revision 1.20 2001/11/22 21:56:49 jongfoster
160 * Making action_spec->flags into an unsigned long rather than just an
162 * Fixing a bug in the display of -add-header and -wafer
164 * Revision 1.19 2001/11/13 00:14:07 jongfoster
165 * Fixing stupid bug now I've figured out what || means.
166 * (It always returns 0 or 1, not one of it's paramaters.)
168 * Revision 1.18 2001/11/07 00:06:06 steudten
169 * Add line number in error output for lineparsing for
172 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
173 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
174 * threads to call select() simultaneously. So, it's time to do a real, live,
175 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
176 * (native). Both versions will work, but using __OS2__ offers multi-threading.
178 * Revision 1.16 2001/10/23 21:30:30 jongfoster
179 * Adding error-checking to selected functions.
181 * Revision 1.15 2001/10/14 21:58:22 jongfoster
182 * Adding support for the CGI-based editor:
183 * - Exported get_actions()
184 * - Added new function free_alias_list()
185 * - Added support for {{settings}} and {{description}} blocks
186 * in the actions file. They are currently ignored.
187 * - Added restriction to only one {{alias}} block which must appear
188 * first in the file, to simplify the editor's rewriting rules.
189 * - Note that load_actions_file() is no longer used by the CGI-based
190 * editor, but some of the other routines in this file are.
192 * Revision 1.14 2001/09/22 16:36:59 jongfoster
193 * Removing unused parameter fs from read_config_line()
195 * Revision 1.13 2001/09/16 15:47:37 jongfoster
196 * First version of CGI-based edit interface. This is very much a
197 * work-in-progress, and you can't actually use it to edit anything
198 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
199 * to have any effect.
201 * Revision 1.12 2001/09/16 13:21:27 jongfoster
202 * Changes to use new list functions.
204 * Revision 1.11 2001/09/14 00:17:32 jongfoster
205 * Tidying up memory allocation. New function init_action().
207 * Revision 1.10 2001/09/10 10:14:34 oes
208 * Removing unused variable
210 * Revision 1.9 2001/07/30 22:08:36 jongfoster
211 * Tidying up #defines:
212 * - All feature #defines are now of the form FEATURE_xxx
213 * - Permanently turned off WIN_GUI_EDIT
214 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
216 * Revision 1.8 2001/06/29 13:19:52 oes
217 * Removed logentry from cancelled commit
219 * Revision 1.7 2001/06/09 10:55:28 jongfoster
220 * Changing BUFSIZ ==> BUFFER_SIZE
222 * Revision 1.6 2001/06/07 23:04:34 jongfoster
223 * Made get_actions() static.
225 * Revision 1.5 2001/06/03 19:11:48 oes
226 * adapted to new enlist_unique arg format
228 * Revision 1.4 2001/06/01 20:03:42 jongfoster
229 * Better memory management - current_action->strings[] now
230 * contains copies of the strings, not the original.
232 * Revision 1.3 2001/06/01 18:49:17 jongfoster
233 * Replaced "list_share" with "list" - the tiny memory gain was not
234 * worth the extra complexity.
236 * Revision 1.2 2001/05/31 21:40:00 jongfoster
237 * Removing some commented out, obsolete blocks of code.
239 * Revision 1.1 2001/05/31 21:16:46 jongfoster
240 * Moved functions to process the action list into this new file.
243 *********************************************************************/
253 #ifdef FEATURE_PTHREAD
261 #include "miscutil.h"
265 #include "urlmatch.h"
269 const char actions_h_rcs[] = ACTIONS_H_VERSION;
273 * We need the main list of options.
275 * First, we need a way to tell between boolean, string, and multi-string
276 * options. For string and multistring options, we also need to be
277 * able to tell the difference between a "+" and a "-". (For bools,
278 * the "+"/"-" information is encoded in "add" and "mask"). So we use
279 * an enumerated type (well, the preprocessor equivalent). Here are
282 #define AV_NONE 0 /* +opt -opt */
283 #define AV_ADD_STRING 1 /* +stropt{string} */
284 #define AV_REM_STRING 2 /* -stropt */
285 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
286 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
289 * We need a structure to hold the name, flag changes,
290 * type, and string index.
295 unsigned long mask; /* a bit set to "0" = remove action */
296 unsigned long add; /* a bit set to "1" = add action */
297 int takes_value; /* an AV_... constant */
298 int index; /* index into strings[] or multi[] */
302 * And with those building blocks in place, here's the array.
304 static const struct action_name action_names[] =
307 * Well actually there's no data here - it's in actionlist.h
308 * This keeps it together to make it easy to change.
310 * Here's the macros used to format it:
312 #define DEFINE_ACTION_MULTI(name,index) \
313 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
314 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
315 #define DEFINE_ACTION_STRING(name,flag,index) \
316 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
317 { "-" name, ~flag, 0, AV_REM_STRING, index },
318 #define DEFINE_ACTION_BOOL(name,flag) \
319 { "+" name, ACTION_MASK_ALL, flag }, \
320 { "-" name, ~flag, 0 },
321 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
323 #include "actionlist.h"
325 #undef DEFINE_ACTION_MULTI
326 #undef DEFINE_ACTION_STRING
327 #undef DEFINE_ACTION_BOOL
328 #undef DEFINE_ACTION_ALIAS
330 { NULL, 0, 0 } /* End marker */
334 static int load_one_actions_file(struct client_state *csp, int fileid);
337 /*********************************************************************
339 * Function : merge_actions
341 * Description : Merge two actions together.
342 * Similar to "dest += src".
345 * 1 : dest = Actions to modify.
346 * 2 : src = Action to add.
348 * Returns : JB_ERR_OK or JB_ERR_MEMORY
350 *********************************************************************/
351 jb_err merge_actions (struct action_spec *dest,
352 const struct action_spec *src)
357 dest->mask &= src->mask;
358 dest->add &= src->mask;
359 dest->add |= src->add;
361 for (i = 0; i < ACTION_STRING_COUNT; i++)
363 char * str = src->string[i];
366 freez(dest->string[i]);
367 dest->string[i] = strdup(str);
368 if (NULL == dest->string[i])
370 return JB_ERR_MEMORY;
375 for (i = 0; i < ACTION_MULTI_COUNT; i++)
377 if (src->multi_remove_all[i])
379 /* Remove everything from dest */
380 list_remove_all(dest->multi_remove[i]);
381 dest->multi_remove_all[i] = 1;
383 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
385 else if (dest->multi_remove_all[i])
388 * dest already removes everything, so we only need to worry
391 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
392 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
396 /* No "remove all"s to worry about. */
397 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
398 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
399 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
412 /*********************************************************************
414 * Function : copy_action
416 * Description : Copy an action_specs.
417 * Similar to "dest = src".
420 * 1 : dest = Destination of copy.
421 * 2 : src = Source for copy.
425 *********************************************************************/
426 jb_err copy_action (struct action_spec *dest,
427 const struct action_spec *src)
430 jb_err err = JB_ERR_OK;
433 memset(dest, '\0', sizeof(*dest));
435 dest->mask = src->mask;
436 dest->add = src->add;
438 for (i = 0; i < ACTION_STRING_COUNT; i++)
440 char * str = src->string[i];
446 return JB_ERR_MEMORY;
448 dest->string[i] = str;
452 for (i = 0; i < ACTION_MULTI_COUNT; i++)
454 dest->multi_remove_all[i] = src->multi_remove_all[i];
455 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
460 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
469 /*********************************************************************
471 * Function : free_action_spec
473 * Description : Frees an action_spec and the memory used by it.
476 * 1 : src = Source to free.
480 *********************************************************************/
481 void free_action_spec(struct action_spec *src)
488 /*********************************************************************
490 * Function : free_action
492 * Description : Destroy an action_spec. Frees memory used by it,
493 * except for the memory used by the struct action_spec
497 * 1 : src = Source to free.
501 *********************************************************************/
502 void free_action (struct action_spec *src)
511 for (i = 0; i < ACTION_STRING_COUNT; i++)
513 freez(src->string[i]);
516 for (i = 0; i < ACTION_MULTI_COUNT; i++)
518 destroy_list(src->multi_remove[i]);
519 destroy_list(src->multi_add[i]);
522 memset(src, '\0', sizeof(*src));
526 /*********************************************************************
528 * Function : get_action_token
530 * Description : Parses a line for the first action.
531 * Modifies it's input array, doesn't allocate memory.
533 * *line=" +abc{def} -ghi "
540 * 1 : line = [in] The line containing the action.
541 * [out] Start of next action on line, or
542 * NULL if we reached the end of line before
543 * we found an action.
544 * 2 : name = [out] Start of action name, null
545 * terminated. NULL on EOL
546 * 3 : value = [out] Start of action value, null
547 * terminated. NULL if none or EOL.
549 * Returns : JB_ERR_OK => Ok
550 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
552 *********************************************************************/
553 jb_err get_action_token(char **line, char **name, char **value)
558 /* set default returns */
563 /* Eat any leading whitespace */
564 while ((*str == ' ') || (*str == '\t'))
576 /* null name, just value is prohibited */
583 while (((ch = *str) != '\0') &&
584 (ch != ' ') && (ch != '\t') && (ch != '{'))
588 /* error, '}' without '{' */
600 /* EOL - be careful not to run off buffer */
605 /* More to parse next time. */
614 str = strchr(str, '}');
631 /*********************************************************************
633 * Function : action_used_to_valid_
635 * Description : Checks if unrecognized actions were valid in earlier
639 * 1 : action = The string containing the action to check.
641 * Returns : True if yes, otherwise false.
643 *********************************************************************/
644 static int action_used_to_be_valid(const char *action)
646 return (0 == strcmpic(action, "treat-forbidden-connects-like-blocks"));
649 /*********************************************************************
651 * Function : get_actions
653 * Description : Parses a list of actions.
656 * 1 : line = The string containing the actions.
657 * Will be written to by this function.
658 * 2 : alias_list = Custom alias list, or NULL for none.
659 * 3 : cur_action = Where to store the action. Caller
662 * Returns : JB_ERR_OK => Ok
663 * JB_ERR_PARSE => Parse error (line was trashed anyway)
664 * nonzero => Out of memory (line was trashed anyway)
666 *********************************************************************/
667 jb_err get_actions(char *line,
668 struct action_alias * alias_list,
669 struct action_spec *cur_action)
672 init_action(cur_action);
673 cur_action->mask = ACTION_MASK_ALL;
677 char * option = NULL;
680 err = get_action_token(&line, &option, &value);
688 /* handle option in 'option' */
690 /* Check for standard action name */
691 const struct action_name * action = action_names;
693 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
697 if (action->name != NULL)
700 cur_action->mask &= action->mask;
701 cur_action->add &= action->mask;
702 cur_action->add |= action->add;
704 switch (action->takes_value)
707 /* ignore any option. */
711 /* add single string. */
713 if ((value == NULL) || (*value == '\0'))
715 if (0 != strcmpic(action->name, "block"))
718 * XXX: Temporary backwards compatibility hack.
720 static int complaint_shown = 0;
721 value = "No reason specified.";
722 if (!complaint_shown)
724 log_error(LOG_LEVEL_ERROR, "At least one block "
725 "without reason found. This may become a fatal "
726 "error in future versions.");
735 /* FIXME: should validate option string here */
736 freez (cur_action->string[action->index]);
737 cur_action->string[action->index] = strdup(value);
738 if (NULL == cur_action->string[action->index])
740 return JB_ERR_MEMORY;
746 /* remove single string. */
748 freez (cur_action->string[action->index]);
753 /* append multi string. */
755 struct list * remove_p = cur_action->multi_remove[action->index];
756 struct list * add_p = cur_action->multi_add[action->index];
758 if ((value == NULL) || (*value == '\0'))
763 list_remove_item(remove_p, value);
764 err = enlist_unique(add_p, value, 0);
773 /* remove multi string. */
775 struct list * remove_p = cur_action->multi_remove[action->index];
776 struct list * add_p = cur_action->multi_add[action->index];
778 if ( (value == NULL) || (*value == '\0')
779 || ((*value == '*') && (value[1] == '\0')) )
782 * no option, or option == "*".
786 list_remove_all(remove_p);
787 list_remove_all(add_p);
788 cur_action->multi_remove_all[action->index] = 1;
792 /* Valid option - remove only 1 option */
794 if ( !cur_action->multi_remove_all[action->index] )
796 /* there isn't a catch-all in the remove list already */
797 err = enlist_unique(remove_p, value, 0);
803 list_remove_item(add_p, value);
808 /* Shouldn't get here unless there's memory corruption. */
815 /* try user aliases. */
816 const struct action_alias * alias = alias_list;
818 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
825 merge_actions(cur_action, alias->action);
827 else if ((2 < strlen(option)) && action_used_to_be_valid(option+1))
829 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
830 "in this Privoxy release. Ignored.", option+1);
834 /* Bad action name */
836 * XXX: This is a fatal error and Privoxy will later on exit
837 * in load_one_actions_file() because of an "invalid line".
839 * It would be preferable to name the offending option in that
840 * error message, but currently there is no way to do that and
841 * we have to live with two error messages for basically the
844 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
855 /*********************************************************************
857 * Function : init_current_action
859 * Description : Zero out an action.
862 * 1 : dest = An uninitialized current_action_spec.
866 *********************************************************************/
867 void init_current_action (struct current_action_spec *dest)
869 memset(dest, '\0', sizeof(*dest));
871 dest->flags = ACTION_MOST_COMPATIBLE;
875 /*********************************************************************
877 * Function : init_action
879 * Description : Zero out an action.
882 * 1 : dest = An uninitialized action_spec.
886 *********************************************************************/
887 void init_action (struct action_spec *dest)
889 memset(dest, '\0', sizeof(*dest));
893 /*********************************************************************
895 * Function : merge_current_action
897 * Description : Merge two actions together.
898 * Similar to "dest += src".
899 * Differences between this and merge_actions()
900 * is that this one doesn't allocate memory for
901 * strings (so "src" better be in memory for at least
902 * as long as "dest" is, and you'd better free
903 * "dest" using "free_current_action").
904 * Also, there is no mask or remove lists in dest.
905 * (If we're applying it to a URL, we don't need them)
908 * 1 : dest = Current actions, to modify.
909 * 2 : src = Action to add.
911 * Returns 0 : no error
912 * !=0 : error, probably JB_ERR_MEMORY.
914 *********************************************************************/
915 jb_err merge_current_action (struct current_action_spec *dest,
916 const struct action_spec *src)
919 jb_err err = JB_ERR_OK;
921 dest->flags &= src->mask;
922 dest->flags |= src->add;
924 for (i = 0; i < ACTION_STRING_COUNT; i++)
926 char * str = src->string[i];
932 return JB_ERR_MEMORY;
934 freez(dest->string[i]);
935 dest->string[i] = str;
939 for (i = 0; i < ACTION_MULTI_COUNT; i++)
941 if (src->multi_remove_all[i])
943 /* Remove everything from dest, then add src->multi_add */
944 err = list_duplicate(dest->multi[i], src->multi_add[i]);
952 list_remove_list(dest->multi[i], src->multi_remove[i]);
953 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
964 /*********************************************************************
966 * Function : update_action_bits_for_all_tags
968 * Description : Updates the action bits based on all matching tags.
971 * 1 : csp = Current client state (buffers, headers, etc...)
973 * Returns : 0 if no tag matched, or
976 *********************************************************************/
977 int update_action_bits_for_all_tags(struct client_state *csp)
979 struct list_entry *tag;
982 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
984 if (update_action_bits_for_tag(csp, tag->str))
994 /*********************************************************************
996 * Function : update_action_bits_for_tag
998 * Description : Updates the action bits based on the action sections
999 * whose tag patterns match a provided tag.
1002 * 1 : csp = Current client state (buffers, headers, etc...)
1003 * 2 : tag = The tag on which the update should be based on
1005 * Returns : 0 if no tag matched, or
1008 *********************************************************************/
1009 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
1011 struct file_list *fl;
1012 struct url_actions *b;
1018 assert(list_contains_item(csp->tags, tag));
1020 /* Run through all action files, */
1021 for (i = 0; i < MAX_AF_FILES; i++)
1023 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
1025 /* Skip empty files */
1029 /* and through all the action patterns, */
1030 for (b = b->next; NULL != b; b = b->next)
1032 /* skip the URL patterns, */
1033 if (NULL == b->url->tag_regex)
1038 /* and check if one of the tag patterns matches the tag, */
1039 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
1041 /* if it does, update the action bit map, */
1042 if (merge_current_action(csp->action, b->action))
1044 log_error(LOG_LEVEL_ERROR,
1045 "Out of memorey while changing action bits");
1047 /* and signal the change. */
1057 /*********************************************************************
1059 * Function : free_current_action
1061 * Description : Free memory used by a current_action_spec.
1062 * Does not free the current_action_spec itself.
1065 * 1 : src = Source to free.
1069 *********************************************************************/
1070 void free_current_action (struct current_action_spec *src)
1074 for (i = 0; i < ACTION_STRING_COUNT; i++)
1076 freez(src->string[i]);
1079 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1081 destroy_list(src->multi[i]);
1084 memset(src, '\0', sizeof(*src));
1088 static struct file_list *current_actions_file[MAX_AF_FILES] = {
1089 NULL, NULL, NULL, NULL, NULL,
1090 NULL, NULL, NULL, NULL, NULL
1094 #ifdef FEATURE_GRACEFUL_TERMINATION
1095 /*********************************************************************
1097 * Function : unload_current_actions_file
1099 * Description : Unloads current actions file - reset to state at
1100 * beginning of program.
1106 *********************************************************************/
1107 void unload_current_actions_file(void)
1111 for (i = 0; i < MAX_AF_FILES; i++)
1113 if (current_actions_file[i])
1115 current_actions_file[i]->unloader = unload_actions_file;
1116 current_actions_file[i] = NULL;
1120 #endif /* FEATURE_GRACEFUL_TERMINATION */
1123 /*********************************************************************
1125 * Function : unload_actions_file
1127 * Description : Unloads an actions module.
1130 * 1 : file_data = the data structure associated with the
1135 *********************************************************************/
1136 void unload_actions_file(void *file_data)
1138 struct url_actions * next;
1139 struct url_actions * cur = (struct url_actions *)file_data;
1143 free_url_spec(cur->url);
1144 if ((next == NULL) || (next->action != cur->action))
1147 * As the action settings might be shared,
1148 * we can only free them if the current
1149 * url pattern is the last one, or if the
1150 * next one is using different settings.
1152 free_action_spec(cur->action);
1160 /*********************************************************************
1162 * Function : free_alias_list
1164 * Description : Free memory used by a list of aliases.
1167 * 1 : alias_list = Linked list to free.
1171 *********************************************************************/
1172 void free_alias_list(struct action_alias *alias_list)
1174 while (alias_list != NULL)
1176 struct action_alias * next = alias_list->next;
1177 alias_list->next = NULL;
1178 freez(alias_list->name);
1179 free_action(alias_list->action);
1186 /*********************************************************************
1188 * Function : load_actions_file
1190 * Description : Read and parse all the action files and add to files
1194 * 1 : csp = Current client state (buffers, headers, etc...)
1196 * Returns : 0 => Ok, everything else is an error.
1198 *********************************************************************/
1199 int load_actions_file(struct client_state *csp)
1204 for (i = 0; i < MAX_AF_FILES; i++)
1206 if (csp->config->actions_file[i])
1208 result = load_one_actions_file(csp, i);
1214 else if (current_actions_file[i])
1216 current_actions_file[i]->unloader = unload_actions_file;
1217 current_actions_file[i] = NULL;
1224 /*********************************************************************
1226 * Function : load_one_actions_file
1228 * Description : Read and parse a action file and add to files
1232 * 1 : csp = Current client state (buffers, headers, etc...)
1233 * 2 : fileid = File index to load.
1235 * Returns : 0 => Ok, everything else is an error.
1237 *********************************************************************/
1238 static int load_one_actions_file(struct client_state *csp, int fileid)
1243 * Note: Keep these in the order they occur in the file, they are
1244 * sometimes tested with <=
1246 #define MODE_START_OF_FILE 1
1247 #define MODE_SETTINGS 2
1248 #define MODE_DESCRIPTION 3
1249 #define MODE_ALIAS 4
1250 #define MODE_ACTIONS 5
1252 int mode = MODE_START_OF_FILE;
1255 struct url_actions *last_perm;
1256 struct url_actions *perm;
1257 char buf[BUFFER_SIZE];
1258 struct file_list *fs;
1259 struct action_spec * cur_action = NULL;
1260 int cur_action_used = 0;
1261 struct action_alias * alias_list = NULL;
1262 unsigned long linenum = 0;
1264 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1266 /* No need to load */
1267 csp->actions_list[fileid] = current_actions_file[fileid];
1272 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1273 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1274 "with their complete file names.", csp->config->actions_file[fileid]);
1275 return 1; /* never get here */
1278 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1279 if (last_perm == NULL)
1281 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1282 csp->config->actions_file[fileid]);
1283 return 1; /* never get here */
1286 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1288 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1289 csp->config->actions_file[fileid]);
1290 return 1; /* never get here */
1293 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1297 /* It's a header block */
1300 /* It's {{settings}} or {{alias}} */
1301 size_t len = strlen(buf);
1302 char * start = buf + 2;
1303 char * end = buf + len - 1;
1304 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1308 log_error(LOG_LEVEL_FATAL,
1309 "can't load actions file '%s': invalid line (%lu): %s",
1310 csp->config->actions_file[fileid], linenum, buf);
1311 return 1; /* never get here */
1314 /* Trim leading and trailing whitespace. */
1322 log_error(LOG_LEVEL_FATAL,
1323 "can't load actions file '%s': invalid line (%lu): {{ }}",
1324 csp->config->actions_file[fileid], linenum);
1325 return 1; /* never get here */
1329 * An actionsfile can optionally contain the following blocks.
1330 * They *MUST* be in this order, to simplify processing:
1336 * ...free text, format TBD, but no line may start with a '{'...
1341 * The actual actions must be *after* these special blocks.
1342 * None of these special blocks may be repeated.
1345 if (0 == strcmpic(start, "settings"))
1347 /* it's a {{settings}} block */
1348 if (mode >= MODE_SETTINGS)
1350 /* {{settings}} must be first thing in file and must only
1354 log_error(LOG_LEVEL_FATAL,
1355 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1356 csp->config->actions_file[fileid], linenum);
1358 mode = MODE_SETTINGS;
1360 else if (0 == strcmpic(start, "description"))
1362 /* it's a {{description}} block */
1363 if (mode >= MODE_DESCRIPTION)
1365 /* {{description}} is a singleton and only {{settings}} may proceed it
1368 log_error(LOG_LEVEL_FATAL,
1369 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1370 csp->config->actions_file[fileid], linenum);
1372 mode = MODE_DESCRIPTION;
1374 else if (0 == strcmpic(start, "alias"))
1376 /* it's an {{alias}} block */
1377 if (mode >= MODE_ALIAS)
1379 /* {{alias}} must be first thing in file, possibly after
1380 * {{settings}} and {{description}}
1382 * {{alias}} must only appear once.
1384 * Note that these are new restrictions introduced in
1385 * v2.9.10 in order to make actionsfile editing simpler.
1386 * (Otherwise, reordering actionsfile entries without
1387 * completely rewriting the file becomes non-trivial)
1390 log_error(LOG_LEVEL_FATAL,
1391 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1392 csp->config->actions_file[fileid], linenum);
1398 /* invalid {{something}} block */
1400 log_error(LOG_LEVEL_FATAL,
1401 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1402 csp->config->actions_file[fileid], linenum, start);
1403 return 1; /* never get here */
1408 /* It's an actions block */
1410 char actions_buf[BUFFER_SIZE];
1414 mode = MODE_ACTIONS;
1416 /* free old action */
1419 if (!cur_action_used)
1421 free_action_spec(cur_action);
1425 cur_action_used = 0;
1426 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1427 if (cur_action == NULL)
1430 log_error(LOG_LEVEL_FATAL,
1431 "can't load actions file '%s': out of memory",
1432 csp->config->actions_file[fileid]);
1433 return 1; /* never get here */
1435 init_action(cur_action);
1438 strlcpy(actions_buf, buf + 1, sizeof(actions_buf));
1440 /* check we have a trailing } and then trim it */
1441 end = actions_buf + strlen(actions_buf) - 1;
1446 log_error(LOG_LEVEL_FATAL,
1447 "can't load actions file '%s': invalid line (%lu): %s",
1448 csp->config->actions_file[fileid], linenum, buf);
1449 return 1; /* never get here */
1453 /* trim any whitespace immediately inside {} */
1456 if (get_actions(actions_buf, alias_list, cur_action))
1460 log_error(LOG_LEVEL_FATAL,
1461 "can't load actions file '%s': invalid line (%lu): %s",
1462 csp->config->actions_file[fileid], linenum, buf);
1463 return 1; /* never get here */
1467 else if (mode == MODE_SETTINGS)
1470 * Part of the {{settings}} block.
1471 * For now only serves to check if the file's minimum Privoxy
1472 * version requirement is met, but we may want to read & check
1473 * permissions when we go multi-user.
1475 if (!strncmp(buf, "for-privoxy-version=", 20))
1477 char *version_string, *fields[3];
1480 if ((version_string = strdup(buf + 20)) == NULL)
1483 log_error(LOG_LEVEL_FATAL,
1484 "can't load actions file '%s': out of memory!",
1485 csp->config->actions_file[fileid]);
1486 return 1; /* never get here */
1489 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1491 if (num_fields < 1 || atoi(fields[0]) == 0)
1493 log_error(LOG_LEVEL_ERROR,
1494 "While loading actions file '%s': invalid line (%lu): %s",
1495 csp->config->actions_file[fileid], linenum, buf);
1497 else if ( atoi(fields[0]) > VERSION_MAJOR
1498 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1499 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1502 log_error(LOG_LEVEL_FATAL,
1503 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1504 csp->config->actions_file[fileid], linenum, buf );
1505 return 1; /* never get here */
1507 free(version_string);
1510 else if (mode == MODE_DESCRIPTION)
1513 * Part of the {{description}} block.
1517 else if (mode == MODE_ALIAS)
1522 char actions_buf[BUFFER_SIZE];
1523 struct action_alias * new_alias;
1525 char * start = strchr(buf, '=');
1528 if ((start == NULL) || (start == buf))
1530 log_error(LOG_LEVEL_FATAL,
1531 "can't load actions file '%s': invalid alias line (%lu): %s",
1532 csp->config->actions_file[fileid], linenum, buf);
1533 return 1; /* never get here */
1536 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1539 log_error(LOG_LEVEL_FATAL,
1540 "can't load actions file '%s': out of memory!",
1541 csp->config->actions_file[fileid]);
1542 return 1; /* never get here */
1545 /* Eat any the whitespace before the '=' */
1547 while ((*end == ' ') || (*end == '\t'))
1550 * we already know we must have at least 1 non-ws char
1551 * at start of buf - no need to check
1557 /* Eat any the whitespace after the '=' */
1559 while ((*start == ' ') || (*start == '\t'))
1565 log_error(LOG_LEVEL_FATAL,
1566 "can't load actions file '%s': invalid alias line (%lu): %s",
1567 csp->config->actions_file[fileid], linenum, buf);
1568 return 1; /* never get here */
1571 if ((new_alias->name = strdup(buf)) == NULL)
1574 log_error(LOG_LEVEL_FATAL,
1575 "can't load actions file '%s': out of memory!",
1576 csp->config->actions_file[fileid]);
1577 return 1; /* never get here */
1580 strlcpy(actions_buf, start, sizeof(actions_buf));
1582 if (get_actions(actions_buf, alias_list, new_alias->action))
1586 log_error(LOG_LEVEL_FATAL,
1587 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1588 csp->config->actions_file[fileid], linenum, buf, start);
1589 return 1; /* never get here */
1593 new_alias->next = alias_list;
1594 alias_list = new_alias;
1596 else if (mode == MODE_ACTIONS)
1598 /* it's a URL pattern */
1600 /* allocate a new node */
1601 if ((perm = zalloc(sizeof(*perm))) == NULL)
1604 log_error(LOG_LEVEL_FATAL,
1605 "can't load actions file '%s': out of memory!",
1606 csp->config->actions_file[fileid]);
1607 return 1; /* never get here */
1610 perm->action = cur_action;
1611 cur_action_used = 1;
1613 /* Save the URL pattern */
1614 if (create_url_spec(perm->url, buf))
1617 log_error(LOG_LEVEL_FATAL,
1618 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1619 csp->config->actions_file[fileid], linenum, buf);
1620 return 1; /* never get here */
1623 /* add it to the list */
1624 last_perm->next = perm;
1627 else if (mode == MODE_START_OF_FILE)
1629 /* oops - please have a {} line as 1st line in file. */
1631 log_error(LOG_LEVEL_FATAL,
1632 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1633 csp->config->actions_file[fileid], linenum, buf);
1634 return 1; /* never get here */
1638 /* How did we get here? This is impossible! */
1640 log_error(LOG_LEVEL_FATAL,
1641 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1642 csp->config->actions_file[fileid], mode);
1643 return 1; /* never get here */
1649 if (!cur_action_used)
1651 free_action_spec(cur_action);
1653 free_alias_list(alias_list);
1655 /* the old one is now obsolete */
1656 if (current_actions_file[fileid])
1658 current_actions_file[fileid]->unloader = unload_actions_file;
1661 fs->next = files->next;
1663 current_actions_file[fileid] = fs;
1665 csp->actions_list[fileid] = fs;
1672 /*********************************************************************
1674 * Function : actions_to_text
1676 * Description : Converts a actionsfile entry from the internal
1677 * structure into a text line. The output is split
1678 * into one line for each action with line continuation.
1681 * 1 : action = The action to format.
1683 * Returns : A string. Caller must free it.
1684 * NULL on out-of-memory error.
1686 *********************************************************************/
1687 char * actions_to_text(const struct action_spec *action)
1689 unsigned mask = action->mask;
1690 unsigned add = action->add;
1691 char * result = strdup("");
1692 struct list_entry * lst;
1694 /* sanity - prevents "-feature +feature" */
1698 #define DEFINE_ACTION_BOOL(__name, __bit) \
1699 if (!(mask & __bit)) \
1701 string_append(&result, " -" __name " \\\n"); \
1703 else if (add & __bit) \
1705 string_append(&result, " +" __name " \\\n"); \
1708 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1709 if (!(mask & __bit)) \
1711 string_append(&result, " -" __name " \\\n"); \
1713 else if (add & __bit) \
1715 string_append(&result, " +" __name "{"); \
1716 string_append(&result, action->string[__index]); \
1717 string_append(&result, "} \\\n"); \
1720 #define DEFINE_ACTION_MULTI(__name, __index) \
1721 if (action->multi_remove_all[__index]) \
1723 string_append(&result, " -" __name " \\\n"); \
1727 lst = action->multi_remove[__index]->first; \
1730 string_append(&result, " -" __name "{"); \
1731 string_append(&result, lst->str); \
1732 string_append(&result, "} \\\n"); \
1736 lst = action->multi_add[__index]->first; \
1739 string_append(&result, " +" __name "{"); \
1740 string_append(&result, lst->str); \
1741 string_append(&result, "} \\\n"); \
1745 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1747 #include "actionlist.h"
1749 #undef DEFINE_ACTION_MULTI
1750 #undef DEFINE_ACTION_STRING
1751 #undef DEFINE_ACTION_BOOL
1752 #undef DEFINE_ACTION_ALIAS
1758 /*********************************************************************
1760 * Function : actions_to_html
1762 * Description : Converts a actionsfile entry from numeric form
1763 * ("mask" and "add") to a <br>-seperated HTML string
1764 * in which each action is linked to its chapter in
1768 * 1 : csp = Client state (for config)
1769 * 2 : action = Action spec to be converted
1771 * Returns : A string. Caller must free it.
1772 * NULL on out-of-memory error.
1774 *********************************************************************/
1775 char * actions_to_html(const struct client_state *csp,
1776 const struct action_spec *action)
1778 unsigned mask = action->mask;
1779 unsigned add = action->add;
1780 char * result = strdup("");
1781 struct list_entry * lst;
1783 /* sanity - prevents "-feature +feature" */
1787 #define DEFINE_ACTION_BOOL(__name, __bit) \
1788 if (!(mask & __bit)) \
1790 string_append(&result, "\n<br>-"); \
1791 string_join(&result, add_help_link(__name, csp->config)); \
1793 else if (add & __bit) \
1795 string_append(&result, "\n<br>+"); \
1796 string_join(&result, add_help_link(__name, csp->config)); \
1799 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1800 if (!(mask & __bit)) \
1802 string_append(&result, "\n<br>-"); \
1803 string_join(&result, add_help_link(__name, csp->config)); \
1805 else if (add & __bit) \
1807 string_append(&result, "\n<br>+"); \
1808 string_join(&result, add_help_link(__name, csp->config)); \
1809 string_append(&result, "{"); \
1810 string_join(&result, html_encode(action->string[__index])); \
1811 string_append(&result, "}"); \
1814 #define DEFINE_ACTION_MULTI(__name, __index) \
1815 if (action->multi_remove_all[__index]) \
1817 string_append(&result, "\n<br>-"); \
1818 string_join(&result, add_help_link(__name, csp->config)); \
1822 lst = action->multi_remove[__index]->first; \
1825 string_append(&result, "\n<br>-"); \
1826 string_join(&result, add_help_link(__name, csp->config)); \
1827 string_append(&result, "{"); \
1828 string_join(&result, html_encode(lst->str)); \
1829 string_append(&result, "}"); \
1833 lst = action->multi_add[__index]->first; \
1836 string_append(&result, "\n<br>+"); \
1837 string_join(&result, add_help_link(__name, csp->config)); \
1838 string_append(&result, "{"); \
1839 string_join(&result, html_encode(lst->str)); \
1840 string_append(&result, "}"); \
1844 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1846 #include "actionlist.h"
1848 #undef DEFINE_ACTION_MULTI
1849 #undef DEFINE_ACTION_STRING
1850 #undef DEFINE_ACTION_BOOL
1851 #undef DEFINE_ACTION_ALIAS
1853 /* trim leading <br> */
1854 if (result && *result)
1857 result = strdup(result + 5);
1865 /*********************************************************************
1867 * Function : current_actions_to_html
1869 * Description : Converts a curren action spec to a <br> seperated HTML
1870 * text in which each action is linked to its chapter in
1874 * 1 : csp = Client state (for config)
1875 * 2 : action = Current action spec to be converted
1877 * Returns : A string. Caller must free it.
1878 * NULL on out-of-memory error.
1880 *********************************************************************/
1881 char *current_action_to_html(const struct client_state *csp,
1882 const struct current_action_spec *action)
1884 unsigned long flags = action->flags;
1885 struct list_entry * lst;
1886 char *result = strdup("");
1887 char *active = strdup("");
1888 char *inactive = strdup("");
1890 #define DEFINE_ACTION_BOOL(__name, __bit) \
1891 if (flags & __bit) \
1893 string_append(&active, "\n<br>+"); \
1894 string_join(&active, add_help_link(__name, csp->config)); \
1898 string_append(&inactive, "\n<br>-"); \
1899 string_join(&inactive, add_help_link(__name, csp->config)); \
1902 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1903 if (flags & __bit) \
1905 string_append(&active, "\n<br>+"); \
1906 string_join(&active, add_help_link(__name, csp->config)); \
1907 string_append(&active, "{"); \
1908 string_join(&active, html_encode(action->string[__index])); \
1909 string_append(&active, "}"); \
1913 string_append(&inactive, "\n<br>-"); \
1914 string_join(&inactive, add_help_link(__name, csp->config)); \
1917 #define DEFINE_ACTION_MULTI(__name, __index) \
1918 lst = action->multi[__index]->first; \
1921 string_append(&inactive, "\n<br>-"); \
1922 string_join(&inactive, add_help_link(__name, csp->config)); \
1928 string_append(&active, "\n<br>+"); \
1929 string_join(&active, add_help_link(__name, csp->config)); \
1930 string_append(&active, "{"); \
1931 string_join(&active, html_encode(lst->str)); \
1932 string_append(&active, "}"); \
1937 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1939 #include "actionlist.h"
1941 #undef DEFINE_ACTION_MULTI
1942 #undef DEFINE_ACTION_STRING
1943 #undef DEFINE_ACTION_BOOL
1944 #undef DEFINE_ACTION_ALIAS
1948 string_append(&result, active);
1951 string_append(&result, "\n<br>");
1952 if (inactive != NULL)
1954 string_append(&result, inactive);