1 const char actions_rcs[] = "$Id: actions.c,v 1.43 2008/03/01 14:00:43 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.43 2008/03/01 14:00:43 fabiankeil
37 * Let the block action take the reason for the block
38 * as argument and show it on the "blocked" page.
40 * Revision 1.42 2008/02/09 15:15:38 fabiankeil
41 * List active and inactive actions in the show-url-info's
42 * "Final results" section separately. Patch submitted by Lee
43 * in #1830056, modified to list active actions first.
45 * Revision 1.41 2008/01/28 20:17:40 fabiankeil
46 * - Mark some parameters as immutable.
47 * - Hide update_action_bits_for_all_tags() while it's unused.
49 * Revision 1.40 2007/05/21 10:26:50 fabiankeil
50 * - Use strlcpy() instead of strcpy().
51 * - Provide a reason why loading the actions
52 * file might have failed.
54 * Revision 1.39 2007/04/17 18:21:45 fabiankeil
55 * Split update_action_bits() into
56 * update_action_bits_for_all_tags()
57 * and update_action_bits_for_tag().
59 * Revision 1.38 2007/04/15 16:39:20 fabiankeil
60 * Introduce tags as alternative way to specify which
61 * actions apply to a request. At the moment tags can be
62 * created based on client and server headers.
64 * Revision 1.37 2007/03/11 15:56:12 fabiankeil
65 * Add kludge to log unknown aliases and actions before exiting.
67 * Revision 1.36 2006/12/28 17:15:42 fabiankeil
68 * Fix gcc43 conversion warning.
70 * Revision 1.35 2006/07/18 14:48:45 david__schmidt
71 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
72 * with what was really the latest development (the v_3_0_branch branch)
74 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
75 * Multiple filter file support
77 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
78 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
79 * this becomes important when jcc.h gets included later down the line.
81 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
82 * - Implemented Privoxy version requirement through
83 * for-privoxy-version= statement in {{settings}}
85 * - Fix for unchecked out-of-memory condition
87 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
88 * Fixed memory leak reported by Dan Price in Bug #694713
90 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
91 * Extended unload_current_actions_file() to multiple AFs.
92 * Thanks to Oliver Stoeneberg for the hint.
94 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
95 * Change unsigned to unsigned long in actions_name struct. This closes
96 * SourceForge Bug #539284.
98 * Revision 1.32 2002/05/12 21:36:29 jongfoster
99 * Correcting function comments
101 * Revision 1.31 2002/05/06 07:56:50 oes
102 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
104 * Revision 1.30 2002/04/30 11:14:52 oes
105 * Made csp the first parameter in *action_to_html
107 * Revision 1.29 2002/04/26 19:30:54 jongfoster
108 * - current_action_to_html(): Adding help link for the "-" form of
109 * one-string actions.
110 * - Some actions had "<br>-", some "<br> -" (note the space).
111 * Standardizing on no space.
112 * - Greatly simplifying some of the code by using string_join()
115 * Revision 1.28 2002/04/26 12:53:15 oes
116 * - CGI AF editor now writes action lines split into
117 * single lines with line continuation
118 * - actions_to_html now embeds each action name in
120 * - current_action_to_text is now called current_action_to_html
121 * and acts like actions_to_html
123 * Revision 1.27 2002/04/24 02:10:31 oes
124 * - Jon's patch for multiple AFs:
125 * - split load_actions_file, add load_one_actions_file
126 * - make csp->actions_list files an array
127 * - remember file id with each action
128 * - Copy_action now frees dest action before copying
130 * Revision 1.26 2002/03/26 22:29:54 swa
131 * we have a new homepage!
133 * Revision 1.25 2002/03/24 13:25:43 swa
134 * name change related issues
136 * Revision 1.24 2002/03/16 23:54:06 jongfoster
137 * Adding graceful termination feature, to help look for memory leaks.
138 * If you enable this (which, by design, has to be done by hand
139 * editing config.h) and then go to http://i.j.b/die, then the program
140 * will exit cleanly after the *next* request. It should free all the
141 * memory that was used.
143 * Revision 1.23 2002/03/07 03:46:16 oes
144 * Fixed compiler warnings
146 * Revision 1.22 2002/01/21 00:27:02 jongfoster
147 * Allowing free_action(NULL).
148 * Moving the functions that #include actionlist.h to the end of the file,
149 * because the Visual C++ 97 debugger gets extremely confused if you try
150 * to debug any code that comes after them in the file.
152 * Revision 1.21 2002/01/17 20:54:44 jongfoster
153 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
155 * Revision 1.20 2001/11/22 21:56:49 jongfoster
156 * Making action_spec->flags into an unsigned long rather than just an
158 * Fixing a bug in the display of -add-header and -wafer
160 * Revision 1.19 2001/11/13 00:14:07 jongfoster
161 * Fixing stupid bug now I've figured out what || means.
162 * (It always returns 0 or 1, not one of it's paramaters.)
164 * Revision 1.18 2001/11/07 00:06:06 steudten
165 * Add line number in error output for lineparsing for
168 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
169 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
170 * threads to call select() simultaneously. So, it's time to do a real, live,
171 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
172 * (native). Both versions will work, but using __OS2__ offers multi-threading.
174 * Revision 1.16 2001/10/23 21:30:30 jongfoster
175 * Adding error-checking to selected functions.
177 * Revision 1.15 2001/10/14 21:58:22 jongfoster
178 * Adding support for the CGI-based editor:
179 * - Exported get_actions()
180 * - Added new function free_alias_list()
181 * - Added support for {{settings}} and {{description}} blocks
182 * in the actions file. They are currently ignored.
183 * - Added restriction to only one {{alias}} block which must appear
184 * first in the file, to simplify the editor's rewriting rules.
185 * - Note that load_actions_file() is no longer used by the CGI-based
186 * editor, but some of the other routines in this file are.
188 * Revision 1.14 2001/09/22 16:36:59 jongfoster
189 * Removing unused parameter fs from read_config_line()
191 * Revision 1.13 2001/09/16 15:47:37 jongfoster
192 * First version of CGI-based edit interface. This is very much a
193 * work-in-progress, and you can't actually use it to edit anything
194 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
195 * to have any effect.
197 * Revision 1.12 2001/09/16 13:21:27 jongfoster
198 * Changes to use new list functions.
200 * Revision 1.11 2001/09/14 00:17:32 jongfoster
201 * Tidying up memory allocation. New function init_action().
203 * Revision 1.10 2001/09/10 10:14:34 oes
204 * Removing unused variable
206 * Revision 1.9 2001/07/30 22:08:36 jongfoster
207 * Tidying up #defines:
208 * - All feature #defines are now of the form FEATURE_xxx
209 * - Permanently turned off WIN_GUI_EDIT
210 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
212 * Revision 1.8 2001/06/29 13:19:52 oes
213 * Removed logentry from cancelled commit
215 * Revision 1.7 2001/06/09 10:55:28 jongfoster
216 * Changing BUFSIZ ==> BUFFER_SIZE
218 * Revision 1.6 2001/06/07 23:04:34 jongfoster
219 * Made get_actions() static.
221 * Revision 1.5 2001/06/03 19:11:48 oes
222 * adapted to new enlist_unique arg format
224 * Revision 1.4 2001/06/01 20:03:42 jongfoster
225 * Better memory management - current_action->strings[] now
226 * contains copies of the strings, not the original.
228 * Revision 1.3 2001/06/01 18:49:17 jongfoster
229 * Replaced "list_share" with "list" - the tiny memory gain was not
230 * worth the extra complexity.
232 * Revision 1.2 2001/05/31 21:40:00 jongfoster
233 * Removing some commented out, obsolete blocks of code.
235 * Revision 1.1 2001/05/31 21:16:46 jongfoster
236 * Moved functions to process the action list into this new file.
239 *********************************************************************/
249 #ifdef FEATURE_PTHREAD
257 #include "miscutil.h"
261 #include "urlmatch.h"
265 const char actions_h_rcs[] = ACTIONS_H_VERSION;
269 * We need the main list of options.
271 * First, we need a way to tell between boolean, string, and multi-string
272 * options. For string and multistring options, we also need to be
273 * able to tell the difference between a "+" and a "-". (For bools,
274 * the "+"/"-" information is encoded in "add" and "mask"). So we use
275 * an enumerated type (well, the preprocessor equivalent). Here are
278 #define AV_NONE 0 /* +opt -opt */
279 #define AV_ADD_STRING 1 /* +stropt{string} */
280 #define AV_REM_STRING 2 /* -stropt */
281 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
282 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
285 * We need a structure to hold the name, flag changes,
286 * type, and string index.
291 unsigned long mask; /* a bit set to "0" = remove action */
292 unsigned long add; /* a bit set to "1" = add action */
293 int takes_value; /* an AV_... constant */
294 int index; /* index into strings[] or multi[] */
298 * And with those building blocks in place, here's the array.
300 static const struct action_name action_names[] =
303 * Well actually there's no data here - it's in actionlist.h
304 * This keeps it together to make it easy to change.
306 * Here's the macros used to format it:
308 #define DEFINE_ACTION_MULTI(name,index) \
309 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
310 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
311 #define DEFINE_ACTION_STRING(name,flag,index) \
312 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
313 { "-" name, ~flag, 0, AV_REM_STRING, index },
314 #define DEFINE_ACTION_BOOL(name,flag) \
315 { "+" name, ACTION_MASK_ALL, flag }, \
316 { "-" name, ~flag, 0 },
317 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
319 #include "actionlist.h"
321 #undef DEFINE_ACTION_MULTI
322 #undef DEFINE_ACTION_STRING
323 #undef DEFINE_ACTION_BOOL
324 #undef DEFINE_ACTION_ALIAS
326 { NULL, 0, 0 } /* End marker */
330 static int load_one_actions_file(struct client_state *csp, int fileid);
333 /*********************************************************************
335 * Function : merge_actions
337 * Description : Merge two actions together.
338 * Similar to "dest += src".
341 * 1 : dest = Actions to modify.
342 * 2 : src = Action to add.
344 * Returns : JB_ERR_OK or JB_ERR_MEMORY
346 *********************************************************************/
347 jb_err merge_actions (struct action_spec *dest,
348 const struct action_spec *src)
353 dest->mask &= src->mask;
354 dest->add &= src->mask;
355 dest->add |= src->add;
357 for (i = 0; i < ACTION_STRING_COUNT; i++)
359 char * str = src->string[i];
362 freez(dest->string[i]);
363 dest->string[i] = strdup(str);
364 if (NULL == dest->string[i])
366 return JB_ERR_MEMORY;
371 for (i = 0; i < ACTION_MULTI_COUNT; i++)
373 if (src->multi_remove_all[i])
375 /* Remove everything from dest */
376 list_remove_all(dest->multi_remove[i]);
377 dest->multi_remove_all[i] = 1;
379 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
381 else if (dest->multi_remove_all[i])
384 * dest already removes everything, so we only need to worry
387 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
388 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
392 /* No "remove all"s to worry about. */
393 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
394 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
395 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
408 /*********************************************************************
410 * Function : copy_action
412 * Description : Copy an action_specs.
413 * Similar to "dest = src".
416 * 1 : dest = Destination of copy.
417 * 2 : src = Source for copy.
421 *********************************************************************/
422 jb_err copy_action (struct action_spec *dest,
423 const struct action_spec *src)
426 jb_err err = JB_ERR_OK;
429 memset(dest, '\0', sizeof(*dest));
431 dest->mask = src->mask;
432 dest->add = src->add;
434 for (i = 0; i < ACTION_STRING_COUNT; i++)
436 char * str = src->string[i];
442 return JB_ERR_MEMORY;
444 dest->string[i] = str;
448 for (i = 0; i < ACTION_MULTI_COUNT; i++)
450 dest->multi_remove_all[i] = src->multi_remove_all[i];
451 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
456 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
466 /*********************************************************************
468 * Function : free_action
470 * Description : Destroy an action_spec. Frees memory used by it,
471 * except for the memory used by the struct action_spec
475 * 1 : src = Source to free.
479 *********************************************************************/
480 void free_action (struct action_spec *src)
489 for (i = 0; i < ACTION_STRING_COUNT; i++)
491 freez(src->string[i]);
494 for (i = 0; i < ACTION_MULTI_COUNT; i++)
496 destroy_list(src->multi_remove[i]);
497 destroy_list(src->multi_add[i]);
500 memset(src, '\0', sizeof(*src));
504 /*********************************************************************
506 * Function : get_action_token
508 * Description : Parses a line for the first action.
509 * Modifies it's input array, doesn't allocate memory.
511 * *line=" +abc{def} -ghi "
518 * 1 : line = [in] The line containing the action.
519 * [out] Start of next action on line, or
520 * NULL if we reached the end of line before
521 * we found an action.
522 * 2 : name = [out] Start of action name, null
523 * terminated. NULL on EOL
524 * 3 : value = [out] Start of action value, null
525 * terminated. NULL if none or EOL.
527 * Returns : JB_ERR_OK => Ok
528 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
530 *********************************************************************/
531 jb_err get_action_token(char **line, char **name, char **value)
536 /* set default returns */
541 /* Eat any leading whitespace */
542 while ((*str == ' ') || (*str == '\t'))
554 /* null name, just value is prohibited */
561 while (((ch = *str) != '\0') &&
562 (ch != ' ') && (ch != '\t') && (ch != '{'))
566 /* error, '}' without '{' */
578 /* EOL - be careful not to run off buffer */
583 /* More to parse next time. */
592 str = strchr(str, '}');
609 /*********************************************************************
611 * Function : action_used_to_valid_
613 * Description : Checks if unrecognized actions were valid in earlier
617 * 1 : action = The string containing the action to check.
619 * Returns : True if yes, otherwise false.
621 *********************************************************************/
622 static int action_used_to_be_valid(const char *action)
624 return (0 == strcmpic(action, "treat-forbidden-connects-like-blocks"));
627 /*********************************************************************
629 * Function : get_actions
631 * Description : Parses a list of actions.
634 * 1 : line = The string containing the actions.
635 * Will be written to by this function.
636 * 2 : alias_list = Custom alias list, or NULL for none.
637 * 3 : cur_action = Where to store the action. Caller
640 * Returns : JB_ERR_OK => Ok
641 * JB_ERR_PARSE => Parse error (line was trashed anyway)
642 * nonzero => Out of memory (line was trashed anyway)
644 *********************************************************************/
645 jb_err get_actions(char *line,
646 struct action_alias * alias_list,
647 struct action_spec *cur_action)
650 init_action(cur_action);
651 cur_action->mask = ACTION_MASK_ALL;
655 char * option = NULL;
658 err = get_action_token(&line, &option, &value);
666 /* handle option in 'option' */
668 /* Check for standard action name */
669 const struct action_name * action = action_names;
671 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
675 if (action->name != NULL)
678 cur_action->mask &= action->mask;
679 cur_action->add &= action->mask;
680 cur_action->add |= action->add;
682 switch (action->takes_value)
685 /* ignore any option. */
689 /* add single string. */
691 if ((value == NULL) || (*value == '\0'))
693 if (0 != strcmpic(action->name, "block"))
696 * XXX: Temporary backwards compatibility hack.
698 static int complaint_shown = 0;
699 value = "No reason specified.";
700 if (!complaint_shown)
702 log_error(LOG_LEVEL_ERROR, "At least one block "
703 "without reason found. This may become a fatal "
704 "error in future versions.");
713 /* FIXME: should validate option string here */
714 freez (cur_action->string[action->index]);
715 cur_action->string[action->index] = strdup(value);
716 if (NULL == cur_action->string[action->index])
718 return JB_ERR_MEMORY;
724 /* remove single string. */
726 freez (cur_action->string[action->index]);
731 /* append multi string. */
733 struct list * remove_p = cur_action->multi_remove[action->index];
734 struct list * add_p = cur_action->multi_add[action->index];
736 if ((value == NULL) || (*value == '\0'))
741 list_remove_item(remove_p, value);
742 err = enlist_unique(add_p, value, 0);
751 /* remove multi string. */
753 struct list * remove_p = cur_action->multi_remove[action->index];
754 struct list * add_p = cur_action->multi_add[action->index];
756 if ( (value == NULL) || (*value == '\0')
757 || ((*value == '*') && (value[1] == '\0')) )
760 * no option, or option == "*".
764 list_remove_all(remove_p);
765 list_remove_all(add_p);
766 cur_action->multi_remove_all[action->index] = 1;
770 /* Valid option - remove only 1 option */
772 if ( !cur_action->multi_remove_all[action->index] )
774 /* there isn't a catch-all in the remove list already */
775 err = enlist_unique(remove_p, value, 0);
781 list_remove_item(add_p, value);
786 /* Shouldn't get here unless there's memory corruption. */
793 /* try user aliases. */
794 const struct action_alias * alias = alias_list;
796 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
803 merge_actions(cur_action, alias->action);
805 else if ((2 < strlen(option)) && action_used_to_be_valid(option+1))
807 log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
808 "in this Privoxy release. Ignored.", option+1);
812 /* Bad action name */
814 * XXX: This is a fatal error and Privoxy will later on exit
815 * in load_one_actions_file() because of an "invalid line".
817 * It would be preferable to name the offending option in that
818 * error message, but currently there is no way to do that and
819 * we have to live with two error messages for basically the
822 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
833 /*********************************************************************
835 * Function : init_current_action
837 * Description : Zero out an action.
840 * 1 : dest = An uninitialized current_action_spec.
844 *********************************************************************/
845 void init_current_action (struct current_action_spec *dest)
847 memset(dest, '\0', sizeof(*dest));
849 dest->flags = ACTION_MOST_COMPATIBLE;
853 /*********************************************************************
855 * Function : init_action
857 * Description : Zero out an action.
860 * 1 : dest = An uninitialized action_spec.
864 *********************************************************************/
865 void init_action (struct action_spec *dest)
867 memset(dest, '\0', sizeof(*dest));
871 /*********************************************************************
873 * Function : merge_current_action
875 * Description : Merge two actions together.
876 * Similar to "dest += src".
877 * Differences between this and merge_actions()
878 * is that this one doesn't allocate memory for
879 * strings (so "src" better be in memory for at least
880 * as long as "dest" is, and you'd better free
881 * "dest" using "free_current_action").
882 * Also, there is no mask or remove lists in dest.
883 * (If we're applying it to a URL, we don't need them)
886 * 1 : dest = Current actions, to modify.
887 * 2 : src = Action to add.
889 * Returns 0 : no error
890 * !=0 : error, probably JB_ERR_MEMORY.
892 *********************************************************************/
893 jb_err merge_current_action (struct current_action_spec *dest,
894 const struct action_spec *src)
897 jb_err err = JB_ERR_OK;
899 dest->flags &= src->mask;
900 dest->flags |= src->add;
902 for (i = 0; i < ACTION_STRING_COUNT; i++)
904 char * str = src->string[i];
910 return JB_ERR_MEMORY;
912 freez(dest->string[i]);
913 dest->string[i] = str;
917 for (i = 0; i < ACTION_MULTI_COUNT; i++)
919 if (src->multi_remove_all[i])
921 /* Remove everything from dest, then add src->multi_add */
922 err = list_duplicate(dest->multi[i], src->multi_add[i]);
930 list_remove_list(dest->multi[i], src->multi_remove[i]);
931 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
942 /*********************************************************************
944 * Function : update_action_bits_for_all_tags
946 * Description : Updates the action bits based on all matching tags.
949 * 1 : csp = Current client state (buffers, headers, etc...)
951 * Returns : 0 if no tag matched, or
954 *********************************************************************/
955 int update_action_bits_for_all_tags(struct client_state *csp)
957 struct list_entry *tag;
960 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
962 if (update_action_bits_for_tag(csp, tag->str))
972 /*********************************************************************
974 * Function : update_action_bits_for_tag
976 * Description : Updates the action bits based on the action sections
977 * whose tag patterns match a provided tag.
980 * 1 : csp = Current client state (buffers, headers, etc...)
981 * 2 : tag = The tag on which the update should be based on
983 * Returns : 0 if no tag matched, or
986 *********************************************************************/
987 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
989 struct file_list *fl;
990 struct url_actions *b;
996 assert(list_contains_item(csp->tags, tag));
998 /* Run through all action files, */
999 for (i = 0; i < MAX_AF_FILES; i++)
1001 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
1003 /* Skip empty files */
1007 /* and through all the action patterns, */
1008 for (b = b->next; NULL != b; b = b->next)
1010 /* skip the URL patterns, */
1011 if (NULL == b->url->tag_regex)
1016 /* and check if one of the tag patterns matches the tag, */
1017 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
1019 /* if it does, update the action bit map, */
1020 if (merge_current_action(csp->action, b->action))
1022 log_error(LOG_LEVEL_ERROR,
1023 "Out of memorey while changing action bits");
1025 /* and signal the change. */
1035 /*********************************************************************
1037 * Function : free_current_action
1039 * Description : Free memory used by a current_action_spec.
1040 * Does not free the current_action_spec itself.
1043 * 1 : src = Source to free.
1047 *********************************************************************/
1048 void free_current_action (struct current_action_spec *src)
1052 for (i = 0; i < ACTION_STRING_COUNT; i++)
1054 freez(src->string[i]);
1057 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1059 destroy_list(src->multi[i]);
1062 memset(src, '\0', sizeof(*src));
1066 static struct file_list *current_actions_file[MAX_AF_FILES] = {
1067 NULL, NULL, NULL, NULL, NULL,
1068 NULL, NULL, NULL, NULL, NULL
1072 #ifdef FEATURE_GRACEFUL_TERMINATION
1073 /*********************************************************************
1075 * Function : unload_current_actions_file
1077 * Description : Unloads current actions file - reset to state at
1078 * beginning of program.
1084 *********************************************************************/
1085 void unload_current_actions_file(void)
1089 for (i = 0; i < MAX_AF_FILES; i++)
1091 if (current_actions_file[i])
1093 current_actions_file[i]->unloader = unload_actions_file;
1094 current_actions_file[i] = NULL;
1098 #endif /* FEATURE_GRACEFUL_TERMINATION */
1101 /*********************************************************************
1103 * Function : unload_actions_file
1105 * Description : Unloads an actions module.
1108 * 1 : file_data = the data structure associated with the
1113 *********************************************************************/
1114 void unload_actions_file(void *file_data)
1116 struct url_actions * next;
1117 struct url_actions * cur = (struct url_actions *)file_data;
1121 free_url_spec(cur->url);
1122 free_action(cur->action);
1130 /*********************************************************************
1132 * Function : free_alias_list
1134 * Description : Free memory used by a list of aliases.
1137 * 1 : alias_list = Linked list to free.
1141 *********************************************************************/
1142 void free_alias_list(struct action_alias *alias_list)
1144 while (alias_list != NULL)
1146 struct action_alias * next = alias_list->next;
1147 alias_list->next = NULL;
1148 freez(alias_list->name);
1149 free_action(alias_list->action);
1156 /*********************************************************************
1158 * Function : load_actions_file
1160 * Description : Read and parse all the action files and add to files
1164 * 1 : csp = Current client state (buffers, headers, etc...)
1166 * Returns : 0 => Ok, everything else is an error.
1168 *********************************************************************/
1169 int load_actions_file(struct client_state *csp)
1174 for (i = 0; i < MAX_AF_FILES; i++)
1176 if (csp->config->actions_file[i])
1178 result = load_one_actions_file(csp, i);
1184 else if (current_actions_file[i])
1186 current_actions_file[i]->unloader = unload_actions_file;
1187 current_actions_file[i] = NULL;
1194 /*********************************************************************
1196 * Function : load_one_actions_file
1198 * Description : Read and parse a action file and add to files
1202 * 1 : csp = Current client state (buffers, headers, etc...)
1203 * 2 : fileid = File index to load.
1205 * Returns : 0 => Ok, everything else is an error.
1207 *********************************************************************/
1208 static int load_one_actions_file(struct client_state *csp, int fileid)
1213 * Note: Keep these in the order they occur in the file, they are
1214 * sometimes tested with <=
1216 #define MODE_START_OF_FILE 1
1217 #define MODE_SETTINGS 2
1218 #define MODE_DESCRIPTION 3
1219 #define MODE_ALIAS 4
1220 #define MODE_ACTIONS 5
1222 int mode = MODE_START_OF_FILE;
1225 struct url_actions *last_perm;
1226 struct url_actions *perm;
1227 char buf[BUFFER_SIZE];
1228 struct file_list *fs;
1229 struct action_spec * cur_action = NULL;
1230 int cur_action_used = 0;
1231 struct action_alias * alias_list = NULL;
1232 unsigned long linenum = 0;
1234 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1236 /* No need to load */
1237 csp->actions_list[fileid] = current_actions_file[fileid];
1242 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1243 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1244 "with their complete file names.", csp->config->actions_file[fileid]);
1245 return 1; /* never get here */
1248 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1249 if (last_perm == NULL)
1251 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1252 csp->config->actions_file[fileid]);
1253 return 1; /* never get here */
1256 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1258 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1259 csp->config->actions_file[fileid]);
1260 return 1; /* never get here */
1263 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1267 /* It's a header block */
1270 /* It's {{settings}} or {{alias}} */
1271 size_t len = strlen(buf);
1272 char * start = buf + 2;
1273 char * end = buf + len - 1;
1274 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1278 log_error(LOG_LEVEL_FATAL,
1279 "can't load actions file '%s': invalid line (%lu): %s",
1280 csp->config->actions_file[fileid], linenum, buf);
1281 return 1; /* never get here */
1284 /* Trim leading and trailing whitespace. */
1292 log_error(LOG_LEVEL_FATAL,
1293 "can't load actions file '%s': invalid line (%lu): {{ }}",
1294 csp->config->actions_file[fileid], linenum);
1295 return 1; /* never get here */
1299 * An actionsfile can optionally contain the following blocks.
1300 * They *MUST* be in this order, to simplify processing:
1306 * ...free text, format TBD, but no line may start with a '{'...
1311 * The actual actions must be *after* these special blocks.
1312 * None of these special blocks may be repeated.
1315 if (0 == strcmpic(start, "settings"))
1317 /* it's a {{settings}} block */
1318 if (mode >= MODE_SETTINGS)
1320 /* {{settings}} must be first thing in file and must only
1324 log_error(LOG_LEVEL_FATAL,
1325 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1326 csp->config->actions_file[fileid], linenum);
1328 mode = MODE_SETTINGS;
1330 else if (0 == strcmpic(start, "description"))
1332 /* it's a {{description}} block */
1333 if (mode >= MODE_DESCRIPTION)
1335 /* {{description}} is a singleton and only {{settings}} may proceed it
1338 log_error(LOG_LEVEL_FATAL,
1339 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1340 csp->config->actions_file[fileid], linenum);
1342 mode = MODE_DESCRIPTION;
1344 else if (0 == strcmpic(start, "alias"))
1346 /* it's an {{alias}} block */
1347 if (mode >= MODE_ALIAS)
1349 /* {{alias}} must be first thing in file, possibly after
1350 * {{settings}} and {{description}}
1352 * {{alias}} must only appear once.
1354 * Note that these are new restrictions introduced in
1355 * v2.9.10 in order to make actionsfile editing simpler.
1356 * (Otherwise, reordering actionsfile entries without
1357 * completely rewriting the file becomes non-trivial)
1360 log_error(LOG_LEVEL_FATAL,
1361 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1362 csp->config->actions_file[fileid], linenum);
1368 /* invalid {{something}} block */
1370 log_error(LOG_LEVEL_FATAL,
1371 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1372 csp->config->actions_file[fileid], linenum, start);
1373 return 1; /* never get here */
1378 /* It's an actions block */
1380 char actions_buf[BUFFER_SIZE];
1384 mode = MODE_ACTIONS;
1386 /* free old action */
1389 if (!cur_action_used)
1391 free_action(cur_action);
1396 cur_action_used = 0;
1397 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1398 if (cur_action == NULL)
1401 log_error(LOG_LEVEL_FATAL,
1402 "can't load actions file '%s': out of memory",
1403 csp->config->actions_file[fileid]);
1404 return 1; /* never get here */
1406 init_action(cur_action);
1409 strlcpy(actions_buf, buf + 1, sizeof(actions_buf));
1411 /* check we have a trailing } and then trim it */
1412 end = actions_buf + strlen(actions_buf) - 1;
1417 log_error(LOG_LEVEL_FATAL,
1418 "can't load actions file '%s': invalid line (%lu): %s",
1419 csp->config->actions_file[fileid], linenum, buf);
1420 return 1; /* never get here */
1424 /* trim any whitespace immediately inside {} */
1427 if (get_actions(actions_buf, alias_list, cur_action))
1431 log_error(LOG_LEVEL_FATAL,
1432 "can't load actions file '%s': invalid line (%lu): %s",
1433 csp->config->actions_file[fileid], linenum, buf);
1434 return 1; /* never get here */
1438 else if (mode == MODE_SETTINGS)
1441 * Part of the {{settings}} block.
1442 * For now only serves to check if the file's minimum Privoxy
1443 * version requirement is met, but we may want to read & check
1444 * permissions when we go multi-user.
1446 if (!strncmp(buf, "for-privoxy-version=", 20))
1448 char *version_string, *fields[3];
1451 if ((version_string = strdup(buf + 20)) == NULL)
1454 log_error(LOG_LEVEL_FATAL,
1455 "can't load actions file '%s': out of memory!",
1456 csp->config->actions_file[fileid]);
1457 return 1; /* never get here */
1460 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1462 if (num_fields < 1 || atoi(fields[0]) == 0)
1464 log_error(LOG_LEVEL_ERROR,
1465 "While loading actions file '%s': invalid line (%lu): %s",
1466 csp->config->actions_file[fileid], linenum, buf);
1468 else if ( atoi(fields[0]) > VERSION_MAJOR
1469 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1470 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1473 log_error(LOG_LEVEL_FATAL,
1474 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1475 csp->config->actions_file[fileid], linenum, buf );
1476 return 1; /* never get here */
1478 free(version_string);
1481 else if (mode == MODE_DESCRIPTION)
1484 * Part of the {{description}} block.
1488 else if (mode == MODE_ALIAS)
1493 char actions_buf[BUFFER_SIZE];
1494 struct action_alias * new_alias;
1496 char * start = strchr(buf, '=');
1499 if ((start == NULL) || (start == buf))
1501 log_error(LOG_LEVEL_FATAL,
1502 "can't load actions file '%s': invalid alias line (%lu): %s",
1503 csp->config->actions_file[fileid], linenum, buf);
1504 return 1; /* never get here */
1507 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1510 log_error(LOG_LEVEL_FATAL,
1511 "can't load actions file '%s': out of memory!",
1512 csp->config->actions_file[fileid]);
1513 return 1; /* never get here */
1516 /* Eat any the whitespace before the '=' */
1518 while ((*end == ' ') || (*end == '\t'))
1521 * we already know we must have at least 1 non-ws char
1522 * at start of buf - no need to check
1528 /* Eat any the whitespace after the '=' */
1530 while ((*start == ' ') || (*start == '\t'))
1536 log_error(LOG_LEVEL_FATAL,
1537 "can't load actions file '%s': invalid alias line (%lu): %s",
1538 csp->config->actions_file[fileid], linenum, buf);
1539 return 1; /* never get here */
1542 if ((new_alias->name = strdup(buf)) == NULL)
1545 log_error(LOG_LEVEL_FATAL,
1546 "can't load actions file '%s': out of memory!",
1547 csp->config->actions_file[fileid]);
1548 return 1; /* never get here */
1551 strlcpy(actions_buf, start, sizeof(actions_buf));
1553 if (get_actions(actions_buf, alias_list, new_alias->action))
1557 log_error(LOG_LEVEL_FATAL,
1558 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1559 csp->config->actions_file[fileid], linenum, buf, start);
1560 return 1; /* never get here */
1564 new_alias->next = alias_list;
1565 alias_list = new_alias;
1567 else if (mode == MODE_ACTIONS)
1569 /* it's a URL pattern */
1571 /* allocate a new node */
1572 if ((perm = zalloc(sizeof(*perm))) == NULL)
1575 log_error(LOG_LEVEL_FATAL,
1576 "can't load actions file '%s': out of memory!",
1577 csp->config->actions_file[fileid]);
1578 return 1; /* never get here */
1582 copy_action (perm->action, cur_action);
1584 /* Save the URL pattern */
1585 if (create_url_spec(perm->url, buf))
1588 log_error(LOG_LEVEL_FATAL,
1589 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1590 csp->config->actions_file[fileid], linenum, buf);
1591 return 1; /* never get here */
1594 /* add it to the list */
1595 last_perm->next = perm;
1598 else if (mode == MODE_START_OF_FILE)
1600 /* oops - please have a {} line as 1st line in file. */
1602 log_error(LOG_LEVEL_FATAL,
1603 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1604 csp->config->actions_file[fileid], linenum, buf);
1605 return 1; /* never get here */
1609 /* How did we get here? This is impossible! */
1611 log_error(LOG_LEVEL_FATAL,
1612 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1613 csp->config->actions_file[fileid], mode);
1614 return 1; /* never get here */
1620 free_action(cur_action);
1623 free_alias_list(alias_list);
1625 /* the old one is now obsolete */
1626 if (current_actions_file[fileid])
1628 current_actions_file[fileid]->unloader = unload_actions_file;
1631 fs->next = files->next;
1633 current_actions_file[fileid] = fs;
1635 csp->actions_list[fileid] = fs;
1642 /*********************************************************************
1644 * Function : actions_to_text
1646 * Description : Converts a actionsfile entry from the internal
1647 * structure into a text line. The output is split
1648 * into one line for each action with line continuation.
1651 * 1 : action = The action to format.
1653 * Returns : A string. Caller must free it.
1654 * NULL on out-of-memory error.
1656 *********************************************************************/
1657 char * actions_to_text(const struct action_spec *action)
1659 unsigned mask = action->mask;
1660 unsigned add = action->add;
1661 char * result = strdup("");
1662 struct list_entry * lst;
1664 /* sanity - prevents "-feature +feature" */
1668 #define DEFINE_ACTION_BOOL(__name, __bit) \
1669 if (!(mask & __bit)) \
1671 string_append(&result, " -" __name " \\\n"); \
1673 else if (add & __bit) \
1675 string_append(&result, " +" __name " \\\n"); \
1678 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1679 if (!(mask & __bit)) \
1681 string_append(&result, " -" __name " \\\n"); \
1683 else if (add & __bit) \
1685 string_append(&result, " +" __name "{"); \
1686 string_append(&result, action->string[__index]); \
1687 string_append(&result, "} \\\n"); \
1690 #define DEFINE_ACTION_MULTI(__name, __index) \
1691 if (action->multi_remove_all[__index]) \
1693 string_append(&result, " -" __name " \\\n"); \
1697 lst = action->multi_remove[__index]->first; \
1700 string_append(&result, " -" __name "{"); \
1701 string_append(&result, lst->str); \
1702 string_append(&result, "} \\\n"); \
1706 lst = action->multi_add[__index]->first; \
1709 string_append(&result, " +" __name "{"); \
1710 string_append(&result, lst->str); \
1711 string_append(&result, "} \\\n"); \
1715 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1717 #include "actionlist.h"
1719 #undef DEFINE_ACTION_MULTI
1720 #undef DEFINE_ACTION_STRING
1721 #undef DEFINE_ACTION_BOOL
1722 #undef DEFINE_ACTION_ALIAS
1728 /*********************************************************************
1730 * Function : actions_to_html
1732 * Description : Converts a actionsfile entry from numeric form
1733 * ("mask" and "add") to a <br>-seperated HTML string
1734 * in which each action is linked to its chapter in
1738 * 1 : csp = Client state (for config)
1739 * 2 : action = Action spec to be converted
1741 * Returns : A string. Caller must free it.
1742 * NULL on out-of-memory error.
1744 *********************************************************************/
1745 char * actions_to_html(const struct client_state *csp,
1746 const struct action_spec *action)
1748 unsigned mask = action->mask;
1749 unsigned add = action->add;
1750 char * result = strdup("");
1751 struct list_entry * lst;
1753 /* sanity - prevents "-feature +feature" */
1757 #define DEFINE_ACTION_BOOL(__name, __bit) \
1758 if (!(mask & __bit)) \
1760 string_append(&result, "\n<br>-"); \
1761 string_join(&result, add_help_link(__name, csp->config)); \
1763 else if (add & __bit) \
1765 string_append(&result, "\n<br>+"); \
1766 string_join(&result, add_help_link(__name, csp->config)); \
1769 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1770 if (!(mask & __bit)) \
1772 string_append(&result, "\n<br>-"); \
1773 string_join(&result, add_help_link(__name, csp->config)); \
1775 else if (add & __bit) \
1777 string_append(&result, "\n<br>+"); \
1778 string_join(&result, add_help_link(__name, csp->config)); \
1779 string_append(&result, "{"); \
1780 string_join(&result, html_encode(action->string[__index])); \
1781 string_append(&result, "}"); \
1784 #define DEFINE_ACTION_MULTI(__name, __index) \
1785 if (action->multi_remove_all[__index]) \
1787 string_append(&result, "\n<br>-"); \
1788 string_join(&result, add_help_link(__name, csp->config)); \
1792 lst = action->multi_remove[__index]->first; \
1795 string_append(&result, "\n<br>-"); \
1796 string_join(&result, add_help_link(__name, csp->config)); \
1797 string_append(&result, "{"); \
1798 string_join(&result, html_encode(lst->str)); \
1799 string_append(&result, "}"); \
1803 lst = action->multi_add[__index]->first; \
1806 string_append(&result, "\n<br>+"); \
1807 string_join(&result, add_help_link(__name, csp->config)); \
1808 string_append(&result, "{"); \
1809 string_join(&result, html_encode(lst->str)); \
1810 string_append(&result, "}"); \
1814 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1816 #include "actionlist.h"
1818 #undef DEFINE_ACTION_MULTI
1819 #undef DEFINE_ACTION_STRING
1820 #undef DEFINE_ACTION_BOOL
1821 #undef DEFINE_ACTION_ALIAS
1823 /* trim leading <br> */
1824 if (result && *result)
1827 result = strdup(result + 5);
1835 /*********************************************************************
1837 * Function : current_actions_to_html
1839 * Description : Converts a curren action spec to a <br> seperated HTML
1840 * text in which each action is linked to its chapter in
1844 * 1 : csp = Client state (for config)
1845 * 2 : action = Current action spec to be converted
1847 * Returns : A string. Caller must free it.
1848 * NULL on out-of-memory error.
1850 *********************************************************************/
1851 char *current_action_to_html(const struct client_state *csp,
1852 const struct current_action_spec *action)
1854 unsigned long flags = action->flags;
1855 struct list_entry * lst;
1856 char *result = strdup("");
1857 char *active = strdup("");
1858 char *inactive = strdup("");
1860 #define DEFINE_ACTION_BOOL(__name, __bit) \
1861 if (flags & __bit) \
1863 string_append(&active, "\n<br>+"); \
1864 string_join(&active, add_help_link(__name, csp->config)); \
1868 string_append(&inactive, "\n<br>-"); \
1869 string_join(&inactive, add_help_link(__name, csp->config)); \
1872 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1873 if (flags & __bit) \
1875 string_append(&active, "\n<br>+"); \
1876 string_join(&active, add_help_link(__name, csp->config)); \
1877 string_append(&active, "{"); \
1878 string_join(&active, html_encode(action->string[__index])); \
1879 string_append(&active, "}"); \
1883 string_append(&inactive, "\n<br>-"); \
1884 string_join(&inactive, add_help_link(__name, csp->config)); \
1887 #define DEFINE_ACTION_MULTI(__name, __index) \
1888 lst = action->multi[__index]->first; \
1891 string_append(&inactive, "\n<br>-"); \
1892 string_join(&inactive, add_help_link(__name, csp->config)); \
1898 string_append(&active, "\n<br>+"); \
1899 string_join(&active, add_help_link(__name, csp->config)); \
1900 string_append(&active, "{"); \
1901 string_join(&active, html_encode(lst->str)); \
1902 string_append(&active, "}"); \
1907 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1909 #include "actionlist.h"
1911 #undef DEFINE_ACTION_MULTI
1912 #undef DEFINE_ACTION_STRING
1913 #undef DEFINE_ACTION_BOOL
1914 #undef DEFINE_ACTION_ALIAS
1918 string_append(&result, active);
1921 string_append(&result, "\n<br>");
1922 if (inactive != NULL)
1924 string_append(&result, inactive);