1 const char actions_rcs[] = "$Id: actions.c,v 1.41 2008/01/28 20:17:40 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-2007 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.41 2008/01/28 20:17:40 fabiankeil
37 * - Mark some parameters as immutable.
38 * - Hide update_action_bits_for_all_tags() while it's unused.
40 * Revision 1.40 2007/05/21 10:26:50 fabiankeil
41 * - Use strlcpy() instead of strcpy().
42 * - Provide a reason why loading the actions
43 * file might have failed.
45 * Revision 1.39 2007/04/17 18:21:45 fabiankeil
46 * Split update_action_bits() into
47 * update_action_bits_for_all_tags()
48 * and update_action_bits_for_tag().
50 * Revision 1.38 2007/04/15 16:39:20 fabiankeil
51 * Introduce tags as alternative way to specify which
52 * actions apply to a request. At the moment tags can be
53 * created based on client and server headers.
55 * Revision 1.37 2007/03/11 15:56:12 fabiankeil
56 * Add kludge to log unknown aliases and actions before exiting.
58 * Revision 1.36 2006/12/28 17:15:42 fabiankeil
59 * Fix gcc43 conversion warning.
61 * Revision 1.35 2006/07/18 14:48:45 david__schmidt
62 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
63 * with what was really the latest development (the v_3_0_branch branch)
65 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
66 * Multiple filter file support
68 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
69 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
70 * this becomes important when jcc.h gets included later down the line.
72 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
73 * - Implemented Privoxy version requirement through
74 * for-privoxy-version= statement in {{settings}}
76 * - Fix for unchecked out-of-memory condition
78 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
79 * Fixed memory leak reported by Dan Price in Bug #694713
81 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
82 * Extended unload_current_actions_file() to multiple AFs.
83 * Thanks to Oliver Stoeneberg for the hint.
85 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
86 * Change unsigned to unsigned long in actions_name struct. This closes
87 * SourceForge Bug #539284.
89 * Revision 1.32 2002/05/12 21:36:29 jongfoster
90 * Correcting function comments
92 * Revision 1.31 2002/05/06 07:56:50 oes
93 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
95 * Revision 1.30 2002/04/30 11:14:52 oes
96 * Made csp the first parameter in *action_to_html
98 * Revision 1.29 2002/04/26 19:30:54 jongfoster
99 * - current_action_to_html(): Adding help link for the "-" form of
100 * one-string actions.
101 * - Some actions had "<br>-", some "<br> -" (note the space).
102 * Standardizing on no space.
103 * - Greatly simplifying some of the code by using string_join()
106 * Revision 1.28 2002/04/26 12:53:15 oes
107 * - CGI AF editor now writes action lines split into
108 * single lines with line continuation
109 * - actions_to_html now embeds each action name in
111 * - current_action_to_text is now called current_action_to_html
112 * and acts like actions_to_html
114 * Revision 1.27 2002/04/24 02:10:31 oes
115 * - Jon's patch for multiple AFs:
116 * - split load_actions_file, add load_one_actions_file
117 * - make csp->actions_list files an array
118 * - remember file id with each action
119 * - Copy_action now frees dest action before copying
121 * Revision 1.26 2002/03/26 22:29:54 swa
122 * we have a new homepage!
124 * Revision 1.25 2002/03/24 13:25:43 swa
125 * name change related issues
127 * Revision 1.24 2002/03/16 23:54:06 jongfoster
128 * Adding graceful termination feature, to help look for memory leaks.
129 * If you enable this (which, by design, has to be done by hand
130 * editing config.h) and then go to http://i.j.b/die, then the program
131 * will exit cleanly after the *next* request. It should free all the
132 * memory that was used.
134 * Revision 1.23 2002/03/07 03:46:16 oes
135 * Fixed compiler warnings
137 * Revision 1.22 2002/01/21 00:27:02 jongfoster
138 * Allowing free_action(NULL).
139 * Moving the functions that #include actionlist.h to the end of the file,
140 * because the Visual C++ 97 debugger gets extremely confused if you try
141 * to debug any code that comes after them in the file.
143 * Revision 1.21 2002/01/17 20:54:44 jongfoster
144 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
146 * Revision 1.20 2001/11/22 21:56:49 jongfoster
147 * Making action_spec->flags into an unsigned long rather than just an
149 * Fixing a bug in the display of -add-header and -wafer
151 * Revision 1.19 2001/11/13 00:14:07 jongfoster
152 * Fixing stupid bug now I've figured out what || means.
153 * (It always returns 0 or 1, not one of it's paramaters.)
155 * Revision 1.18 2001/11/07 00:06:06 steudten
156 * Add line number in error output for lineparsing for
159 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
160 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
161 * threads to call select() simultaneously. So, it's time to do a real, live,
162 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
163 * (native). Both versions will work, but using __OS2__ offers multi-threading.
165 * Revision 1.16 2001/10/23 21:30:30 jongfoster
166 * Adding error-checking to selected functions.
168 * Revision 1.15 2001/10/14 21:58:22 jongfoster
169 * Adding support for the CGI-based editor:
170 * - Exported get_actions()
171 * - Added new function free_alias_list()
172 * - Added support for {{settings}} and {{description}} blocks
173 * in the actions file. They are currently ignored.
174 * - Added restriction to only one {{alias}} block which must appear
175 * first in the file, to simplify the editor's rewriting rules.
176 * - Note that load_actions_file() is no longer used by the CGI-based
177 * editor, but some of the other routines in this file are.
179 * Revision 1.14 2001/09/22 16:36:59 jongfoster
180 * Removing unused parameter fs from read_config_line()
182 * Revision 1.13 2001/09/16 15:47:37 jongfoster
183 * First version of CGI-based edit interface. This is very much a
184 * work-in-progress, and you can't actually use it to edit anything
185 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
186 * to have any effect.
188 * Revision 1.12 2001/09/16 13:21:27 jongfoster
189 * Changes to use new list functions.
191 * Revision 1.11 2001/09/14 00:17:32 jongfoster
192 * Tidying up memory allocation. New function init_action().
194 * Revision 1.10 2001/09/10 10:14:34 oes
195 * Removing unused variable
197 * Revision 1.9 2001/07/30 22:08:36 jongfoster
198 * Tidying up #defines:
199 * - All feature #defines are now of the form FEATURE_xxx
200 * - Permanently turned off WIN_GUI_EDIT
201 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
203 * Revision 1.8 2001/06/29 13:19:52 oes
204 * Removed logentry from cancelled commit
206 * Revision 1.7 2001/06/09 10:55:28 jongfoster
207 * Changing BUFSIZ ==> BUFFER_SIZE
209 * Revision 1.6 2001/06/07 23:04:34 jongfoster
210 * Made get_actions() static.
212 * Revision 1.5 2001/06/03 19:11:48 oes
213 * adapted to new enlist_unique arg format
215 * Revision 1.4 2001/06/01 20:03:42 jongfoster
216 * Better memory management - current_action->strings[] now
217 * contains copies of the strings, not the original.
219 * Revision 1.3 2001/06/01 18:49:17 jongfoster
220 * Replaced "list_share" with "list" - the tiny memory gain was not
221 * worth the extra complexity.
223 * Revision 1.2 2001/05/31 21:40:00 jongfoster
224 * Removing some commented out, obsolete blocks of code.
226 * Revision 1.1 2001/05/31 21:16:46 jongfoster
227 * Moved functions to process the action list into this new file.
230 *********************************************************************/
240 #ifdef FEATURE_PTHREAD
248 #include "miscutil.h"
252 #include "urlmatch.h"
256 const char actions_h_rcs[] = ACTIONS_H_VERSION;
260 * We need the main list of options.
262 * First, we need a way to tell between boolean, string, and multi-string
263 * options. For string and multistring options, we also need to be
264 * able to tell the difference between a "+" and a "-". (For bools,
265 * the "+"/"-" information is encoded in "add" and "mask"). So we use
266 * an enumerated type (well, the preprocessor equivalent). Here are
269 #define AV_NONE 0 /* +opt -opt */
270 #define AV_ADD_STRING 1 /* +stropt{string} */
271 #define AV_REM_STRING 2 /* -stropt */
272 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
273 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
276 * We need a structure to hold the name, flag changes,
277 * type, and string index.
282 unsigned long mask; /* a bit set to "0" = remove action */
283 unsigned long add; /* a bit set to "1" = add action */
284 int takes_value; /* an AV_... constant */
285 int index; /* index into strings[] or multi[] */
289 * And with those building blocks in place, here's the array.
291 static const struct action_name action_names[] =
294 * Well actually there's no data here - it's in actionlist.h
295 * This keeps it together to make it easy to change.
297 * Here's the macros used to format it:
299 #define DEFINE_ACTION_MULTI(name,index) \
300 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
301 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
302 #define DEFINE_ACTION_STRING(name,flag,index) \
303 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
304 { "-" name, ~flag, 0, AV_REM_STRING, index },
305 #define DEFINE_ACTION_BOOL(name,flag) \
306 { "+" name, ACTION_MASK_ALL, flag }, \
307 { "-" name, ~flag, 0 },
308 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
310 #include "actionlist.h"
312 #undef DEFINE_ACTION_MULTI
313 #undef DEFINE_ACTION_STRING
314 #undef DEFINE_ACTION_BOOL
315 #undef DEFINE_ACTION_ALIAS
317 { NULL, 0, 0 } /* End marker */
321 static int load_one_actions_file(struct client_state *csp, int fileid);
324 /*********************************************************************
326 * Function : merge_actions
328 * Description : Merge two actions together.
329 * Similar to "dest += src".
332 * 1 : dest = Actions to modify.
333 * 2 : src = Action to add.
335 * Returns : JB_ERR_OK or JB_ERR_MEMORY
337 *********************************************************************/
338 jb_err merge_actions (struct action_spec *dest,
339 const struct action_spec *src)
344 dest->mask &= src->mask;
345 dest->add &= src->mask;
346 dest->add |= src->add;
348 for (i = 0; i < ACTION_STRING_COUNT; i++)
350 char * str = src->string[i];
353 freez(dest->string[i]);
354 dest->string[i] = strdup(str);
355 if (NULL == dest->string[i])
357 return JB_ERR_MEMORY;
362 for (i = 0; i < ACTION_MULTI_COUNT; i++)
364 if (src->multi_remove_all[i])
366 /* Remove everything from dest */
367 list_remove_all(dest->multi_remove[i]);
368 dest->multi_remove_all[i] = 1;
370 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
372 else if (dest->multi_remove_all[i])
375 * dest already removes everything, so we only need to worry
378 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
379 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
383 /* No "remove all"s to worry about. */
384 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
385 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
386 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
399 /*********************************************************************
401 * Function : copy_action
403 * Description : Copy an action_specs.
404 * Similar to "dest = src".
407 * 1 : dest = Destination of copy.
408 * 2 : src = Source for copy.
412 *********************************************************************/
413 jb_err copy_action (struct action_spec *dest,
414 const struct action_spec *src)
417 jb_err err = JB_ERR_OK;
420 memset(dest, '\0', sizeof(*dest));
422 dest->mask = src->mask;
423 dest->add = src->add;
425 for (i = 0; i < ACTION_STRING_COUNT; i++)
427 char * str = src->string[i];
433 return JB_ERR_MEMORY;
435 dest->string[i] = str;
439 for (i = 0; i < ACTION_MULTI_COUNT; i++)
441 dest->multi_remove_all[i] = src->multi_remove_all[i];
442 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
447 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
457 /*********************************************************************
459 * Function : free_action
461 * Description : Destroy an action_spec. Frees memory used by it,
462 * except for the memory used by the struct action_spec
466 * 1 : src = Source to free.
470 *********************************************************************/
471 void free_action (struct action_spec *src)
480 for (i = 0; i < ACTION_STRING_COUNT; i++)
482 freez(src->string[i]);
485 for (i = 0; i < ACTION_MULTI_COUNT; i++)
487 destroy_list(src->multi_remove[i]);
488 destroy_list(src->multi_add[i]);
491 memset(src, '\0', sizeof(*src));
495 /*********************************************************************
497 * Function : get_action_token
499 * Description : Parses a line for the first action.
500 * Modifies it's input array, doesn't allocate memory.
502 * *line=" +abc{def} -ghi "
509 * 1 : line = [in] The line containing the action.
510 * [out] Start of next action on line, or
511 * NULL if we reached the end of line before
512 * we found an action.
513 * 2 : name = [out] Start of action name, null
514 * terminated. NULL on EOL
515 * 3 : value = [out] Start of action value, null
516 * terminated. NULL if none or EOL.
518 * Returns : JB_ERR_OK => Ok
519 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
521 *********************************************************************/
522 jb_err get_action_token(char **line, char **name, char **value)
527 /* set default returns */
532 /* Eat any leading whitespace */
533 while ((*str == ' ') || (*str == '\t'))
545 /* null name, just value is prohibited */
552 while (((ch = *str) != '\0') &&
553 (ch != ' ') && (ch != '\t') && (ch != '{'))
557 /* error, '}' without '{' */
569 /* EOL - be careful not to run off buffer */
574 /* More to parse next time. */
583 str = strchr(str, '}');
601 /*********************************************************************
603 * Function : get_actions
605 * Description : Parses a list of actions.
608 * 1 : line = The string containing the actions.
609 * Will be written to by this function.
610 * 2 : alias_list = Custom alias list, or NULL for none.
611 * 3 : cur_action = Where to store the action. Caller
614 * Returns : JB_ERR_OK => Ok
615 * JB_ERR_PARSE => Parse error (line was trashed anyway)
616 * nonzero => Out of memory (line was trashed anyway)
618 *********************************************************************/
619 jb_err get_actions(char *line,
620 struct action_alias * alias_list,
621 struct action_spec *cur_action)
624 init_action(cur_action);
625 cur_action->mask = ACTION_MASK_ALL;
629 char * option = NULL;
632 err = get_action_token(&line, &option, &value);
640 /* handle option in 'option' */
642 /* Check for standard action name */
643 const struct action_name * action = action_names;
645 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
649 if (action->name != NULL)
652 cur_action->mask &= action->mask;
653 cur_action->add &= action->mask;
654 cur_action->add |= action->add;
656 switch (action->takes_value)
659 /* ignore any option. */
663 /* add single string. */
665 if ((value == NULL) || (*value == '\0'))
669 /* FIXME: should validate option string here */
670 freez (cur_action->string[action->index]);
671 cur_action->string[action->index] = strdup(value);
672 if (NULL == cur_action->string[action->index])
674 return JB_ERR_MEMORY;
680 /* remove single string. */
682 freez (cur_action->string[action->index]);
687 /* append multi string. */
689 struct list * remove_p = cur_action->multi_remove[action->index];
690 struct list * add_p = cur_action->multi_add[action->index];
692 if ((value == NULL) || (*value == '\0'))
697 list_remove_item(remove_p, value);
698 err = enlist_unique(add_p, value, 0);
707 /* remove multi string. */
709 struct list * remove_p = cur_action->multi_remove[action->index];
710 struct list * add_p = cur_action->multi_add[action->index];
712 if ( (value == NULL) || (*value == '\0')
713 || ((*value == '*') && (value[1] == '\0')) )
716 * no option, or option == "*".
720 list_remove_all(remove_p);
721 list_remove_all(add_p);
722 cur_action->multi_remove_all[action->index] = 1;
726 /* Valid option - remove only 1 option */
728 if ( !cur_action->multi_remove_all[action->index] )
730 /* there isn't a catch-all in the remove list already */
731 err = enlist_unique(remove_p, value, 0);
737 list_remove_item(add_p, value);
742 /* Shouldn't get here unless there's memory corruption. */
749 /* try user aliases. */
750 const struct action_alias * alias = alias_list;
752 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
759 merge_actions(cur_action, alias->action);
763 /* Bad action name */
765 * XXX: This is a fatal error and Privoxy will later on exit
766 * in load_one_actions_file() because of an "invalid line".
768 * It would be preferable to name the offending option in that
769 * error message, but currently there is no way to do that and
770 * we have to live with two error messages for basically the
773 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
784 /*********************************************************************
786 * Function : init_current_action
788 * Description : Zero out an action.
791 * 1 : dest = An uninitialized current_action_spec.
795 *********************************************************************/
796 void init_current_action (struct current_action_spec *dest)
798 memset(dest, '\0', sizeof(*dest));
800 dest->flags = ACTION_MOST_COMPATIBLE;
804 /*********************************************************************
806 * Function : init_action
808 * Description : Zero out an action.
811 * 1 : dest = An uninitialized action_spec.
815 *********************************************************************/
816 void init_action (struct action_spec *dest)
818 memset(dest, '\0', sizeof(*dest));
822 /*********************************************************************
824 * Function : merge_current_action
826 * Description : Merge two actions together.
827 * Similar to "dest += src".
828 * Differences between this and merge_actions()
829 * is that this one doesn't allocate memory for
830 * strings (so "src" better be in memory for at least
831 * as long as "dest" is, and you'd better free
832 * "dest" using "free_current_action").
833 * Also, there is no mask or remove lists in dest.
834 * (If we're applying it to a URL, we don't need them)
837 * 1 : dest = Current actions, to modify.
838 * 2 : src = Action to add.
840 * Returns 0 : no error
841 * !=0 : error, probably JB_ERR_MEMORY.
843 *********************************************************************/
844 jb_err merge_current_action (struct current_action_spec *dest,
845 const struct action_spec *src)
848 jb_err err = JB_ERR_OK;
850 dest->flags &= src->mask;
851 dest->flags |= src->add;
853 for (i = 0; i < ACTION_STRING_COUNT; i++)
855 char * str = src->string[i];
861 return JB_ERR_MEMORY;
863 freez(dest->string[i]);
864 dest->string[i] = str;
868 for (i = 0; i < ACTION_MULTI_COUNT; i++)
870 if (src->multi_remove_all[i])
872 /* Remove everything from dest, then add src->multi_add */
873 err = list_duplicate(dest->multi[i], src->multi_add[i]);
881 list_remove_list(dest->multi[i], src->multi_remove[i]);
882 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
893 /*********************************************************************
895 * Function : update_action_bits_for_all_tags
897 * Description : Updates the action bits based on all matching tags.
900 * 1 : csp = Current client state (buffers, headers, etc...)
902 * Returns : 0 if no tag matched, or
905 *********************************************************************/
906 int update_action_bits_for_all_tags(struct client_state *csp)
908 struct list_entry *tag;
911 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
913 if (update_action_bits_for_tag(csp, tag->str))
923 /*********************************************************************
925 * Function : update_action_bits_for_tag
927 * Description : Updates the action bits based on the action sections
928 * whose tag patterns match a provided tag.
931 * 1 : csp = Current client state (buffers, headers, etc...)
932 * 2 : tag = The tag on which the update should be based on
934 * Returns : 0 if no tag matched, or
937 *********************************************************************/
938 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
940 struct file_list *fl;
941 struct url_actions *b;
947 assert(list_contains_item(csp->tags, tag));
949 /* Run through all action files, */
950 for (i = 0; i < MAX_AF_FILES; i++)
952 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
954 /* Skip empty files */
958 /* and through all the action patterns, */
959 for (b = b->next; NULL != b; b = b->next)
961 /* skip the URL patterns, */
962 if (NULL == b->url->tag_regex)
967 /* and check if one of the tag patterns matches the tag, */
968 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
970 /* if it does, update the action bit map, */
971 if (merge_current_action(csp->action, b->action))
973 log_error(LOG_LEVEL_ERROR,
974 "Out of memorey while changing action bits");
976 /* and signal the change. */
986 /*********************************************************************
988 * Function : free_current_action
990 * Description : Free memory used by a current_action_spec.
991 * Does not free the current_action_spec itself.
994 * 1 : src = Source to free.
998 *********************************************************************/
999 void free_current_action (struct current_action_spec *src)
1003 for (i = 0; i < ACTION_STRING_COUNT; i++)
1005 freez(src->string[i]);
1008 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1010 destroy_list(src->multi[i]);
1013 memset(src, '\0', sizeof(*src));
1017 static struct file_list *current_actions_file[MAX_AF_FILES] = {
1018 NULL, NULL, NULL, NULL, NULL,
1019 NULL, NULL, NULL, NULL, NULL
1023 #ifdef FEATURE_GRACEFUL_TERMINATION
1024 /*********************************************************************
1026 * Function : unload_current_actions_file
1028 * Description : Unloads current actions file - reset to state at
1029 * beginning of program.
1035 *********************************************************************/
1036 void unload_current_actions_file(void)
1040 for (i = 0; i < MAX_AF_FILES; i++)
1042 if (current_actions_file[i])
1044 current_actions_file[i]->unloader = unload_actions_file;
1045 current_actions_file[i] = NULL;
1049 #endif /* FEATURE_GRACEFUL_TERMINATION */
1052 /*********************************************************************
1054 * Function : unload_actions_file
1056 * Description : Unloads an actions module.
1059 * 1 : file_data = the data structure associated with the
1064 *********************************************************************/
1065 void unload_actions_file(void *file_data)
1067 struct url_actions * next;
1068 struct url_actions * cur = (struct url_actions *)file_data;
1072 free_url_spec(cur->url);
1073 free_action(cur->action);
1081 /*********************************************************************
1083 * Function : free_alias_list
1085 * Description : Free memory used by a list of aliases.
1088 * 1 : alias_list = Linked list to free.
1092 *********************************************************************/
1093 void free_alias_list(struct action_alias *alias_list)
1095 while (alias_list != NULL)
1097 struct action_alias * next = alias_list->next;
1098 alias_list->next = NULL;
1099 freez(alias_list->name);
1100 free_action(alias_list->action);
1107 /*********************************************************************
1109 * Function : load_actions_file
1111 * Description : Read and parse all the action files and add to files
1115 * 1 : csp = Current client state (buffers, headers, etc...)
1117 * Returns : 0 => Ok, everything else is an error.
1119 *********************************************************************/
1120 int load_actions_file(struct client_state *csp)
1125 for (i = 0; i < MAX_AF_FILES; i++)
1127 if (csp->config->actions_file[i])
1129 result = load_one_actions_file(csp, i);
1135 else if (current_actions_file[i])
1137 current_actions_file[i]->unloader = unload_actions_file;
1138 current_actions_file[i] = NULL;
1145 /*********************************************************************
1147 * Function : load_one_actions_file
1149 * Description : Read and parse a action file and add to files
1153 * 1 : csp = Current client state (buffers, headers, etc...)
1154 * 2 : fileid = File index to load.
1156 * Returns : 0 => Ok, everything else is an error.
1158 *********************************************************************/
1159 static int load_one_actions_file(struct client_state *csp, int fileid)
1164 * Note: Keep these in the order they occur in the file, they are
1165 * sometimes tested with <=
1167 #define MODE_START_OF_FILE 1
1168 #define MODE_SETTINGS 2
1169 #define MODE_DESCRIPTION 3
1170 #define MODE_ALIAS 4
1171 #define MODE_ACTIONS 5
1173 int mode = MODE_START_OF_FILE;
1176 struct url_actions *last_perm;
1177 struct url_actions *perm;
1178 char buf[BUFFER_SIZE];
1179 struct file_list *fs;
1180 struct action_spec * cur_action = NULL;
1181 int cur_action_used = 0;
1182 struct action_alias * alias_list = NULL;
1183 unsigned long linenum = 0;
1185 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1187 /* No need to load */
1188 csp->actions_list[fileid] = current_actions_file[fileid];
1193 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1194 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1195 "with their complete file names.", csp->config->actions_file[fileid]);
1196 return 1; /* never get here */
1199 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1200 if (last_perm == NULL)
1202 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1203 csp->config->actions_file[fileid]);
1204 return 1; /* never get here */
1207 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1209 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1210 csp->config->actions_file[fileid]);
1211 return 1; /* never get here */
1214 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1218 /* It's a header block */
1221 /* It's {{settings}} or {{alias}} */
1222 size_t len = strlen(buf);
1223 char * start = buf + 2;
1224 char * end = buf + len - 1;
1225 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1229 log_error(LOG_LEVEL_FATAL,
1230 "can't load actions file '%s': invalid line (%lu): %s",
1231 csp->config->actions_file[fileid], linenum, buf);
1232 return 1; /* never get here */
1235 /* Trim leading and trailing whitespace. */
1243 log_error(LOG_LEVEL_FATAL,
1244 "can't load actions file '%s': invalid line (%lu): {{ }}",
1245 csp->config->actions_file[fileid], linenum);
1246 return 1; /* never get here */
1250 * An actionsfile can optionally contain the following blocks.
1251 * They *MUST* be in this order, to simplify processing:
1257 * ...free text, format TBD, but no line may start with a '{'...
1262 * The actual actions must be *after* these special blocks.
1263 * None of these special blocks may be repeated.
1266 if (0 == strcmpic(start, "settings"))
1268 /* it's a {{settings}} block */
1269 if (mode >= MODE_SETTINGS)
1271 /* {{settings}} must be first thing in file and must only
1275 log_error(LOG_LEVEL_FATAL,
1276 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1277 csp->config->actions_file[fileid], linenum);
1279 mode = MODE_SETTINGS;
1281 else if (0 == strcmpic(start, "description"))
1283 /* it's a {{description}} block */
1284 if (mode >= MODE_DESCRIPTION)
1286 /* {{description}} is a singleton and only {{settings}} may proceed it
1289 log_error(LOG_LEVEL_FATAL,
1290 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1291 csp->config->actions_file[fileid], linenum);
1293 mode = MODE_DESCRIPTION;
1295 else if (0 == strcmpic(start, "alias"))
1297 /* it's an {{alias}} block */
1298 if (mode >= MODE_ALIAS)
1300 /* {{alias}} must be first thing in file, possibly after
1301 * {{settings}} and {{description}}
1303 * {{alias}} must only appear once.
1305 * Note that these are new restrictions introduced in
1306 * v2.9.10 in order to make actionsfile editing simpler.
1307 * (Otherwise, reordering actionsfile entries without
1308 * completely rewriting the file becomes non-trivial)
1311 log_error(LOG_LEVEL_FATAL,
1312 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1313 csp->config->actions_file[fileid], linenum);
1319 /* invalid {{something}} block */
1321 log_error(LOG_LEVEL_FATAL,
1322 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1323 csp->config->actions_file[fileid], linenum, start);
1324 return 1; /* never get here */
1329 /* It's an actions block */
1331 char actions_buf[BUFFER_SIZE];
1335 mode = MODE_ACTIONS;
1337 /* free old action */
1340 if (!cur_action_used)
1342 free_action(cur_action);
1347 cur_action_used = 0;
1348 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1349 if (cur_action == NULL)
1352 log_error(LOG_LEVEL_FATAL,
1353 "can't load actions file '%s': out of memory",
1354 csp->config->actions_file[fileid]);
1355 return 1; /* never get here */
1357 init_action(cur_action);
1360 strlcpy(actions_buf, buf + 1, sizeof(actions_buf));
1362 /* check we have a trailing } and then trim it */
1363 end = actions_buf + strlen(actions_buf) - 1;
1368 log_error(LOG_LEVEL_FATAL,
1369 "can't load actions file '%s': invalid line (%lu): %s",
1370 csp->config->actions_file[fileid], linenum, buf);
1371 return 1; /* never get here */
1375 /* trim any whitespace immediately inside {} */
1378 if (get_actions(actions_buf, alias_list, cur_action))
1382 log_error(LOG_LEVEL_FATAL,
1383 "can't load actions file '%s': invalid line (%lu): %s",
1384 csp->config->actions_file[fileid], linenum, buf);
1385 return 1; /* never get here */
1389 else if (mode == MODE_SETTINGS)
1392 * Part of the {{settings}} block.
1393 * For now only serves to check if the file's minimum Privoxy
1394 * version requirement is met, but we may want to read & check
1395 * permissions when we go multi-user.
1397 if (!strncmp(buf, "for-privoxy-version=", 20))
1399 char *version_string, *fields[3];
1402 if ((version_string = strdup(buf + 20)) == NULL)
1405 log_error(LOG_LEVEL_FATAL,
1406 "can't load actions file '%s': out of memory!",
1407 csp->config->actions_file[fileid]);
1408 return 1; /* never get here */
1411 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1413 if (num_fields < 1 || atoi(fields[0]) == 0)
1415 log_error(LOG_LEVEL_ERROR,
1416 "While loading actions file '%s': invalid line (%lu): %s",
1417 csp->config->actions_file[fileid], linenum, buf);
1419 else if ( atoi(fields[0]) > VERSION_MAJOR
1420 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1421 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1424 log_error(LOG_LEVEL_FATAL,
1425 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1426 csp->config->actions_file[fileid], linenum, buf );
1427 return 1; /* never get here */
1429 free(version_string);
1432 else if (mode == MODE_DESCRIPTION)
1435 * Part of the {{description}} block.
1439 else if (mode == MODE_ALIAS)
1444 char actions_buf[BUFFER_SIZE];
1445 struct action_alias * new_alias;
1447 char * start = strchr(buf, '=');
1450 if ((start == NULL) || (start == buf))
1452 log_error(LOG_LEVEL_FATAL,
1453 "can't load actions file '%s': invalid alias line (%lu): %s",
1454 csp->config->actions_file[fileid], linenum, buf);
1455 return 1; /* never get here */
1458 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1461 log_error(LOG_LEVEL_FATAL,
1462 "can't load actions file '%s': out of memory!",
1463 csp->config->actions_file[fileid]);
1464 return 1; /* never get here */
1467 /* Eat any the whitespace before the '=' */
1469 while ((*end == ' ') || (*end == '\t'))
1472 * we already know we must have at least 1 non-ws char
1473 * at start of buf - no need to check
1479 /* Eat any the whitespace after the '=' */
1481 while ((*start == ' ') || (*start == '\t'))
1487 log_error(LOG_LEVEL_FATAL,
1488 "can't load actions file '%s': invalid alias line (%lu): %s",
1489 csp->config->actions_file[fileid], linenum, buf);
1490 return 1; /* never get here */
1493 if ((new_alias->name = strdup(buf)) == NULL)
1496 log_error(LOG_LEVEL_FATAL,
1497 "can't load actions file '%s': out of memory!",
1498 csp->config->actions_file[fileid]);
1499 return 1; /* never get here */
1502 strlcpy(actions_buf, start, sizeof(actions_buf));
1504 if (get_actions(actions_buf, alias_list, new_alias->action))
1508 log_error(LOG_LEVEL_FATAL,
1509 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1510 csp->config->actions_file[fileid], linenum, buf, start);
1511 return 1; /* never get here */
1515 new_alias->next = alias_list;
1516 alias_list = new_alias;
1518 else if (mode == MODE_ACTIONS)
1520 /* it's a URL pattern */
1522 /* allocate a new node */
1523 if ((perm = zalloc(sizeof(*perm))) == NULL)
1526 log_error(LOG_LEVEL_FATAL,
1527 "can't load actions file '%s': out of memory!",
1528 csp->config->actions_file[fileid]);
1529 return 1; /* never get here */
1533 copy_action (perm->action, cur_action);
1535 /* Save the URL pattern */
1536 if (create_url_spec(perm->url, buf))
1539 log_error(LOG_LEVEL_FATAL,
1540 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1541 csp->config->actions_file[fileid], linenum, buf);
1542 return 1; /* never get here */
1545 /* add it to the list */
1546 last_perm->next = perm;
1549 else if (mode == MODE_START_OF_FILE)
1551 /* oops - please have a {} line as 1st line in file. */
1553 log_error(LOG_LEVEL_FATAL,
1554 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1555 csp->config->actions_file[fileid], linenum, buf);
1556 return 1; /* never get here */
1560 /* How did we get here? This is impossible! */
1562 log_error(LOG_LEVEL_FATAL,
1563 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1564 csp->config->actions_file[fileid], mode);
1565 return 1; /* never get here */
1571 free_action(cur_action);
1574 free_alias_list(alias_list);
1576 /* the old one is now obsolete */
1577 if (current_actions_file[fileid])
1579 current_actions_file[fileid]->unloader = unload_actions_file;
1582 fs->next = files->next;
1584 current_actions_file[fileid] = fs;
1586 csp->actions_list[fileid] = fs;
1593 /*********************************************************************
1595 * Function : actions_to_text
1597 * Description : Converts a actionsfile entry from the internal
1598 * structure into a text line. The output is split
1599 * into one line for each action with line continuation.
1602 * 1 : action = The action to format.
1604 * Returns : A string. Caller must free it.
1605 * NULL on out-of-memory error.
1607 *********************************************************************/
1608 char * actions_to_text(const struct action_spec *action)
1610 unsigned mask = action->mask;
1611 unsigned add = action->add;
1612 char * result = strdup("");
1613 struct list_entry * lst;
1615 /* sanity - prevents "-feature +feature" */
1619 #define DEFINE_ACTION_BOOL(__name, __bit) \
1620 if (!(mask & __bit)) \
1622 string_append(&result, " -" __name " \\\n"); \
1624 else if (add & __bit) \
1626 string_append(&result, " +" __name " \\\n"); \
1629 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1630 if (!(mask & __bit)) \
1632 string_append(&result, " -" __name " \\\n"); \
1634 else if (add & __bit) \
1636 string_append(&result, " +" __name "{"); \
1637 string_append(&result, action->string[__index]); \
1638 string_append(&result, "} \\\n"); \
1641 #define DEFINE_ACTION_MULTI(__name, __index) \
1642 if (action->multi_remove_all[__index]) \
1644 string_append(&result, " -" __name " \\\n"); \
1648 lst = action->multi_remove[__index]->first; \
1651 string_append(&result, " -" __name "{"); \
1652 string_append(&result, lst->str); \
1653 string_append(&result, "} \\\n"); \
1657 lst = action->multi_add[__index]->first; \
1660 string_append(&result, " +" __name "{"); \
1661 string_append(&result, lst->str); \
1662 string_append(&result, "} \\\n"); \
1666 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1668 #include "actionlist.h"
1670 #undef DEFINE_ACTION_MULTI
1671 #undef DEFINE_ACTION_STRING
1672 #undef DEFINE_ACTION_BOOL
1673 #undef DEFINE_ACTION_ALIAS
1679 /*********************************************************************
1681 * Function : actions_to_html
1683 * Description : Converts a actionsfile entry from numeric form
1684 * ("mask" and "add") to a <br>-seperated HTML string
1685 * in which each action is linked to its chapter in
1689 * 1 : csp = Client state (for config)
1690 * 2 : action = Action spec to be converted
1692 * Returns : A string. Caller must free it.
1693 * NULL on out-of-memory error.
1695 *********************************************************************/
1696 char * actions_to_html(const struct client_state *csp,
1697 const struct action_spec *action)
1699 unsigned mask = action->mask;
1700 unsigned add = action->add;
1701 char * result = strdup("");
1702 struct list_entry * lst;
1704 /* sanity - prevents "-feature +feature" */
1708 #define DEFINE_ACTION_BOOL(__name, __bit) \
1709 if (!(mask & __bit)) \
1711 string_append(&result, "\n<br>-"); \
1712 string_join(&result, add_help_link(__name, csp->config)); \
1714 else if (add & __bit) \
1716 string_append(&result, "\n<br>+"); \
1717 string_join(&result, add_help_link(__name, csp->config)); \
1720 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1721 if (!(mask & __bit)) \
1723 string_append(&result, "\n<br>-"); \
1724 string_join(&result, add_help_link(__name, csp->config)); \
1726 else if (add & __bit) \
1728 string_append(&result, "\n<br>+"); \
1729 string_join(&result, add_help_link(__name, csp->config)); \
1730 string_append(&result, "{"); \
1731 string_join(&result, html_encode(action->string[__index])); \
1732 string_append(&result, "}"); \
1735 #define DEFINE_ACTION_MULTI(__name, __index) \
1736 if (action->multi_remove_all[__index]) \
1738 string_append(&result, "\n<br>-"); \
1739 string_join(&result, add_help_link(__name, csp->config)); \
1743 lst = action->multi_remove[__index]->first; \
1746 string_append(&result, "\n<br>-"); \
1747 string_join(&result, add_help_link(__name, csp->config)); \
1748 string_append(&result, "{"); \
1749 string_join(&result, html_encode(lst->str)); \
1750 string_append(&result, "}"); \
1754 lst = action->multi_add[__index]->first; \
1757 string_append(&result, "\n<br>+"); \
1758 string_join(&result, add_help_link(__name, csp->config)); \
1759 string_append(&result, "{"); \
1760 string_join(&result, html_encode(lst->str)); \
1761 string_append(&result, "}"); \
1765 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1767 #include "actionlist.h"
1769 #undef DEFINE_ACTION_MULTI
1770 #undef DEFINE_ACTION_STRING
1771 #undef DEFINE_ACTION_BOOL
1772 #undef DEFINE_ACTION_ALIAS
1774 /* trim leading <br> */
1775 if (result && *result)
1778 result = strdup(result + 5);
1786 /*********************************************************************
1788 * Function : current_actions_to_html
1790 * Description : Converts a curren action spec to a <br> seperated HTML
1791 * text in which each action is linked to its chapter in
1795 * 1 : csp = Client state (for config)
1796 * 2 : action = Current action spec to be converted
1798 * Returns : A string. Caller must free it.
1799 * NULL on out-of-memory error.
1801 *********************************************************************/
1802 char *current_action_to_html(const struct client_state *csp,
1803 const struct current_action_spec *action)
1805 unsigned long flags = action->flags;
1806 struct list_entry * lst;
1807 char *result = strdup("");
1808 char *active = strdup("");
1809 char *inactive = strdup("");
1811 #define DEFINE_ACTION_BOOL(__name, __bit) \
1812 if (flags & __bit) \
1814 string_append(&active, "\n<br>+"); \
1815 string_join(&active, add_help_link(__name, csp->config)); \
1819 string_append(&inactive, "\n<br>-"); \
1820 string_join(&inactive, add_help_link(__name, csp->config)); \
1823 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1824 if (flags & __bit) \
1826 string_append(&active, "\n<br>+"); \
1827 string_join(&active, add_help_link(__name, csp->config)); \
1828 string_append(&active, "{"); \
1829 string_join(&active, html_encode(action->string[__index])); \
1830 string_append(&active, "}"); \
1834 string_append(&inactive, "\n<br>-"); \
1835 string_join(&inactive, add_help_link(__name, csp->config)); \
1838 #define DEFINE_ACTION_MULTI(__name, __index) \
1839 lst = action->multi[__index]->first; \
1842 string_append(&inactive, "\n<br>-"); \
1843 string_join(&inactive, add_help_link(__name, csp->config)); \
1849 string_append(&active, "\n<br>+"); \
1850 string_join(&active, add_help_link(__name, csp->config)); \
1851 string_append(&active, "{"); \
1852 string_join(&active, html_encode(lst->str)); \
1853 string_append(&active, "}"); \
1858 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1860 #include "actionlist.h"
1862 #undef DEFINE_ACTION_MULTI
1863 #undef DEFINE_ACTION_STRING
1864 #undef DEFINE_ACTION_BOOL
1865 #undef DEFINE_ACTION_ALIAS
1869 string_append(&result, active);
1872 string_append(&result, "\n<br>");
1873 if (inactive != NULL)
1875 string_append(&result, inactive);