1 const char actions_rcs[] = "$Id: actions.c,v 1.40 2007/05/21 10:26:50 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.40 2007/05/21 10:26:50 fabiankeil
37 * - Use strlcpy() instead of strcpy().
38 * - Provide a reason why loading the actions
39 * file might have failed.
41 * Revision 1.39 2007/04/17 18:21:45 fabiankeil
42 * Split update_action_bits() into
43 * update_action_bits_for_all_tags()
44 * and update_action_bits_for_tag().
46 * Revision 1.38 2007/04/15 16:39:20 fabiankeil
47 * Introduce tags as alternative way to specify which
48 * actions apply to a request. At the moment tags can be
49 * created based on client and server headers.
51 * Revision 1.37 2007/03/11 15:56:12 fabiankeil
52 * Add kludge to log unknown aliases and actions before exiting.
54 * Revision 1.36 2006/12/28 17:15:42 fabiankeil
55 * Fix gcc43 conversion warning.
57 * Revision 1.35 2006/07/18 14:48:45 david__schmidt
58 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
59 * with what was really the latest development (the v_3_0_branch branch)
61 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
62 * Multiple filter file support
64 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
65 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
66 * this becomes important when jcc.h gets included later down the line.
68 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
69 * - Implemented Privoxy version requirement through
70 * for-privoxy-version= statement in {{settings}}
72 * - Fix for unchecked out-of-memory condition
74 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
75 * Fixed memory leak reported by Dan Price in Bug #694713
77 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
78 * Extended unload_current_actions_file() to multiple AFs.
79 * Thanks to Oliver Stoeneberg for the hint.
81 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
82 * Change unsigned to unsigned long in actions_name struct. This closes
83 * SourceForge Bug #539284.
85 * Revision 1.32 2002/05/12 21:36:29 jongfoster
86 * Correcting function comments
88 * Revision 1.31 2002/05/06 07:56:50 oes
89 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
91 * Revision 1.30 2002/04/30 11:14:52 oes
92 * Made csp the first parameter in *action_to_html
94 * Revision 1.29 2002/04/26 19:30:54 jongfoster
95 * - current_action_to_html(): Adding help link for the "-" form of
97 * - Some actions had "<br>-", some "<br> -" (note the space).
98 * Standardizing on no space.
99 * - Greatly simplifying some of the code by using string_join()
102 * Revision 1.28 2002/04/26 12:53:15 oes
103 * - CGI AF editor now writes action lines split into
104 * single lines with line continuation
105 * - actions_to_html now embeds each action name in
107 * - current_action_to_text is now called current_action_to_html
108 * and acts like actions_to_html
110 * Revision 1.27 2002/04/24 02:10:31 oes
111 * - Jon's patch for multiple AFs:
112 * - split load_actions_file, add load_one_actions_file
113 * - make csp->actions_list files an array
114 * - remember file id with each action
115 * - Copy_action now frees dest action before copying
117 * Revision 1.26 2002/03/26 22:29:54 swa
118 * we have a new homepage!
120 * Revision 1.25 2002/03/24 13:25:43 swa
121 * name change related issues
123 * Revision 1.24 2002/03/16 23:54:06 jongfoster
124 * Adding graceful termination feature, to help look for memory leaks.
125 * If you enable this (which, by design, has to be done by hand
126 * editing config.h) and then go to http://i.j.b/die, then the program
127 * will exit cleanly after the *next* request. It should free all the
128 * memory that was used.
130 * Revision 1.23 2002/03/07 03:46:16 oes
131 * Fixed compiler warnings
133 * Revision 1.22 2002/01/21 00:27:02 jongfoster
134 * Allowing free_action(NULL).
135 * Moving the functions that #include actionlist.h to the end of the file,
136 * because the Visual C++ 97 debugger gets extremely confused if you try
137 * to debug any code that comes after them in the file.
139 * Revision 1.21 2002/01/17 20:54:44 jongfoster
140 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
142 * Revision 1.20 2001/11/22 21:56:49 jongfoster
143 * Making action_spec->flags into an unsigned long rather than just an
145 * Fixing a bug in the display of -add-header and -wafer
147 * Revision 1.19 2001/11/13 00:14:07 jongfoster
148 * Fixing stupid bug now I've figured out what || means.
149 * (It always returns 0 or 1, not one of it's paramaters.)
151 * Revision 1.18 2001/11/07 00:06:06 steudten
152 * Add line number in error output for lineparsing for
155 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
156 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
157 * threads to call select() simultaneously. So, it's time to do a real, live,
158 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
159 * (native). Both versions will work, but using __OS2__ offers multi-threading.
161 * Revision 1.16 2001/10/23 21:30:30 jongfoster
162 * Adding error-checking to selected functions.
164 * Revision 1.15 2001/10/14 21:58:22 jongfoster
165 * Adding support for the CGI-based editor:
166 * - Exported get_actions()
167 * - Added new function free_alias_list()
168 * - Added support for {{settings}} and {{description}} blocks
169 * in the actions file. They are currently ignored.
170 * - Added restriction to only one {{alias}} block which must appear
171 * first in the file, to simplify the editor's rewriting rules.
172 * - Note that load_actions_file() is no longer used by the CGI-based
173 * editor, but some of the other routines in this file are.
175 * Revision 1.14 2001/09/22 16:36:59 jongfoster
176 * Removing unused parameter fs from read_config_line()
178 * Revision 1.13 2001/09/16 15:47:37 jongfoster
179 * First version of CGI-based edit interface. This is very much a
180 * work-in-progress, and you can't actually use it to edit anything
181 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
182 * to have any effect.
184 * Revision 1.12 2001/09/16 13:21:27 jongfoster
185 * Changes to use new list functions.
187 * Revision 1.11 2001/09/14 00:17:32 jongfoster
188 * Tidying up memory allocation. New function init_action().
190 * Revision 1.10 2001/09/10 10:14:34 oes
191 * Removing unused variable
193 * Revision 1.9 2001/07/30 22:08:36 jongfoster
194 * Tidying up #defines:
195 * - All feature #defines are now of the form FEATURE_xxx
196 * - Permanently turned off WIN_GUI_EDIT
197 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
199 * Revision 1.8 2001/06/29 13:19:52 oes
200 * Removed logentry from cancelled commit
202 * Revision 1.7 2001/06/09 10:55:28 jongfoster
203 * Changing BUFSIZ ==> BUFFER_SIZE
205 * Revision 1.6 2001/06/07 23:04:34 jongfoster
206 * Made get_actions() static.
208 * Revision 1.5 2001/06/03 19:11:48 oes
209 * adapted to new enlist_unique arg format
211 * Revision 1.4 2001/06/01 20:03:42 jongfoster
212 * Better memory management - current_action->strings[] now
213 * contains copies of the strings, not the original.
215 * Revision 1.3 2001/06/01 18:49:17 jongfoster
216 * Replaced "list_share" with "list" - the tiny memory gain was not
217 * worth the extra complexity.
219 * Revision 1.2 2001/05/31 21:40:00 jongfoster
220 * Removing some commented out, obsolete blocks of code.
222 * Revision 1.1 2001/05/31 21:16:46 jongfoster
223 * Moved functions to process the action list into this new file.
226 *********************************************************************/
236 #ifdef FEATURE_PTHREAD
244 #include "miscutil.h"
248 #include "urlmatch.h"
252 const char actions_h_rcs[] = ACTIONS_H_VERSION;
256 * We need the main list of options.
258 * First, we need a way to tell between boolean, string, and multi-string
259 * options. For string and multistring options, we also need to be
260 * able to tell the difference between a "+" and a "-". (For bools,
261 * the "+"/"-" information is encoded in "add" and "mask"). So we use
262 * an enumerated type (well, the preprocessor equivalent). Here are
265 #define AV_NONE 0 /* +opt -opt */
266 #define AV_ADD_STRING 1 /* +stropt{string} */
267 #define AV_REM_STRING 2 /* -stropt */
268 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
269 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
272 * We need a structure to hold the name, flag changes,
273 * type, and string index.
278 unsigned long mask; /* a bit set to "0" = remove action */
279 unsigned long add; /* a bit set to "1" = add action */
280 int takes_value; /* an AV_... constant */
281 int index; /* index into strings[] or multi[] */
285 * And with those building blocks in place, here's the array.
287 static const struct action_name action_names[] =
290 * Well actually there's no data here - it's in actionlist.h
291 * This keeps it together to make it easy to change.
293 * Here's the macros used to format it:
295 #define DEFINE_ACTION_MULTI(name,index) \
296 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
297 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
298 #define DEFINE_ACTION_STRING(name,flag,index) \
299 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
300 { "-" name, ~flag, 0, AV_REM_STRING, index },
301 #define DEFINE_ACTION_BOOL(name,flag) \
302 { "+" name, ACTION_MASK_ALL, flag }, \
303 { "-" name, ~flag, 0 },
304 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
306 #include "actionlist.h"
308 #undef DEFINE_ACTION_MULTI
309 #undef DEFINE_ACTION_STRING
310 #undef DEFINE_ACTION_BOOL
311 #undef DEFINE_ACTION_ALIAS
313 { NULL, 0, 0 } /* End marker */
317 static int load_one_actions_file(struct client_state *csp, int fileid);
320 /*********************************************************************
322 * Function : merge_actions
324 * Description : Merge two actions together.
325 * Similar to "dest += src".
328 * 1 : dest = Actions to modify.
329 * 2 : src = Action to add.
331 * Returns : JB_ERR_OK or JB_ERR_MEMORY
333 *********************************************************************/
334 jb_err merge_actions (struct action_spec *dest,
335 const struct action_spec *src)
340 dest->mask &= src->mask;
341 dest->add &= src->mask;
342 dest->add |= src->add;
344 for (i = 0; i < ACTION_STRING_COUNT; i++)
346 char * str = src->string[i];
349 freez(dest->string[i]);
350 dest->string[i] = strdup(str);
351 if (NULL == dest->string[i])
353 return JB_ERR_MEMORY;
358 for (i = 0; i < ACTION_MULTI_COUNT; i++)
360 if (src->multi_remove_all[i])
362 /* Remove everything from dest */
363 list_remove_all(dest->multi_remove[i]);
364 dest->multi_remove_all[i] = 1;
366 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
368 else if (dest->multi_remove_all[i])
371 * dest already removes everything, so we only need to worry
374 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
375 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
379 /* No "remove all"s to worry about. */
380 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
381 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
382 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
395 /*********************************************************************
397 * Function : copy_action
399 * Description : Copy an action_specs.
400 * Similar to "dest = src".
403 * 1 : dest = Destination of copy.
404 * 2 : src = Source for copy.
408 *********************************************************************/
409 jb_err copy_action (struct action_spec *dest,
410 const struct action_spec *src)
413 jb_err err = JB_ERR_OK;
416 memset(dest, '\0', sizeof(*dest));
418 dest->mask = src->mask;
419 dest->add = src->add;
421 for (i = 0; i < ACTION_STRING_COUNT; i++)
423 char * str = src->string[i];
429 return JB_ERR_MEMORY;
431 dest->string[i] = str;
435 for (i = 0; i < ACTION_MULTI_COUNT; i++)
437 dest->multi_remove_all[i] = src->multi_remove_all[i];
438 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
443 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
453 /*********************************************************************
455 * Function : free_action
457 * Description : Destroy an action_spec. Frees memory used by it,
458 * except for the memory used by the struct action_spec
462 * 1 : src = Source to free.
466 *********************************************************************/
467 void free_action (struct action_spec *src)
476 for (i = 0; i < ACTION_STRING_COUNT; i++)
478 freez(src->string[i]);
481 for (i = 0; i < ACTION_MULTI_COUNT; i++)
483 destroy_list(src->multi_remove[i]);
484 destroy_list(src->multi_add[i]);
487 memset(src, '\0', sizeof(*src));
491 /*********************************************************************
493 * Function : get_action_token
495 * Description : Parses a line for the first action.
496 * Modifies it's input array, doesn't allocate memory.
498 * *line=" +abc{def} -ghi "
505 * 1 : line = [in] The line containing the action.
506 * [out] Start of next action on line, or
507 * NULL if we reached the end of line before
508 * we found an action.
509 * 2 : name = [out] Start of action name, null
510 * terminated. NULL on EOL
511 * 3 : value = [out] Start of action value, null
512 * terminated. NULL if none or EOL.
514 * Returns : JB_ERR_OK => Ok
515 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
517 *********************************************************************/
518 jb_err get_action_token(char **line, char **name, char **value)
523 /* set default returns */
528 /* Eat any leading whitespace */
529 while ((*str == ' ') || (*str == '\t'))
541 /* null name, just value is prohibited */
548 while (((ch = *str) != '\0') &&
549 (ch != ' ') && (ch != '\t') && (ch != '{'))
553 /* error, '}' without '{' */
565 /* EOL - be careful not to run off buffer */
570 /* More to parse next time. */
579 str = strchr(str, '}');
597 /*********************************************************************
599 * Function : get_actions
601 * Description : Parses a list of actions.
604 * 1 : line = The string containing the actions.
605 * Will be written to by this function.
606 * 2 : alias_list = Custom alias list, or NULL for none.
607 * 3 : cur_action = Where to store the action. Caller
610 * Returns : JB_ERR_OK => Ok
611 * JB_ERR_PARSE => Parse error (line was trashed anyway)
612 * nonzero => Out of memory (line was trashed anyway)
614 *********************************************************************/
615 jb_err get_actions(char *line,
616 struct action_alias * alias_list,
617 struct action_spec *cur_action)
620 init_action(cur_action);
621 cur_action->mask = ACTION_MASK_ALL;
625 char * option = NULL;
628 err = get_action_token(&line, &option, &value);
636 /* handle option in 'option' */
638 /* Check for standard action name */
639 const struct action_name * action = action_names;
641 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
645 if (action->name != NULL)
648 cur_action->mask &= action->mask;
649 cur_action->add &= action->mask;
650 cur_action->add |= action->add;
652 switch (action->takes_value)
655 /* ignore any option. */
659 /* add single string. */
661 if ((value == NULL) || (*value == '\0'))
665 /* FIXME: should validate option string here */
666 freez (cur_action->string[action->index]);
667 cur_action->string[action->index] = strdup(value);
668 if (NULL == cur_action->string[action->index])
670 return JB_ERR_MEMORY;
676 /* remove single string. */
678 freez (cur_action->string[action->index]);
683 /* append multi string. */
685 struct list * remove_p = cur_action->multi_remove[action->index];
686 struct list * add_p = cur_action->multi_add[action->index];
688 if ((value == NULL) || (*value == '\0'))
693 list_remove_item(remove_p, value);
694 err = enlist_unique(add_p, value, 0);
703 /* remove multi string. */
705 struct list * remove_p = cur_action->multi_remove[action->index];
706 struct list * add_p = cur_action->multi_add[action->index];
708 if ( (value == NULL) || (*value == '\0')
709 || ((*value == '*') && (value[1] == '\0')) )
712 * no option, or option == "*".
716 list_remove_all(remove_p);
717 list_remove_all(add_p);
718 cur_action->multi_remove_all[action->index] = 1;
722 /* Valid option - remove only 1 option */
724 if ( !cur_action->multi_remove_all[action->index] )
726 /* there isn't a catch-all in the remove list already */
727 err = enlist_unique(remove_p, value, 0);
733 list_remove_item(add_p, value);
738 /* Shouldn't get here unless there's memory corruption. */
745 /* try user aliases. */
746 const struct action_alias * alias = alias_list;
748 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
755 merge_actions(cur_action, alias->action);
759 /* Bad action name */
761 * XXX: This is a fatal error and Privoxy will later on exit
762 * in load_one_actions_file() because of an "invalid line".
764 * It would be preferable to name the offending option in that
765 * error message, but currently there is no way to do that and
766 * we have to live with two error messages for basically the
769 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
780 /*********************************************************************
782 * Function : init_current_action
784 * Description : Zero out an action.
787 * 1 : dest = An uninitialized current_action_spec.
791 *********************************************************************/
792 void init_current_action (struct current_action_spec *dest)
794 memset(dest, '\0', sizeof(*dest));
796 dest->flags = ACTION_MOST_COMPATIBLE;
800 /*********************************************************************
802 * Function : init_action
804 * Description : Zero out an action.
807 * 1 : dest = An uninitialized action_spec.
811 *********************************************************************/
812 void init_action (struct action_spec *dest)
814 memset(dest, '\0', sizeof(*dest));
818 /*********************************************************************
820 * Function : merge_current_action
822 * Description : Merge two actions together.
823 * Similar to "dest += src".
824 * Differences between this and merge_actions()
825 * is that this one doesn't allocate memory for
826 * strings (so "src" better be in memory for at least
827 * as long as "dest" is, and you'd better free
828 * "dest" using "free_current_action").
829 * Also, there is no mask or remove lists in dest.
830 * (If we're applying it to a URL, we don't need them)
833 * 1 : dest = Current actions, to modify.
834 * 2 : src = Action to add.
836 * Returns 0 : no error
837 * !=0 : error, probably JB_ERR_MEMORY.
839 *********************************************************************/
840 jb_err merge_current_action (struct current_action_spec *dest,
841 const struct action_spec *src)
844 jb_err err = JB_ERR_OK;
846 dest->flags &= src->mask;
847 dest->flags |= src->add;
849 for (i = 0; i < ACTION_STRING_COUNT; i++)
851 char * str = src->string[i];
857 return JB_ERR_MEMORY;
859 freez(dest->string[i]);
860 dest->string[i] = str;
864 for (i = 0; i < ACTION_MULTI_COUNT; i++)
866 if (src->multi_remove_all[i])
868 /* Remove everything from dest, then add src->multi_add */
869 err = list_duplicate(dest->multi[i], src->multi_add[i]);
877 list_remove_list(dest->multi[i], src->multi_remove[i]);
878 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
889 /*********************************************************************
891 * Function : update_action_bits_for_all_tags
893 * Description : Updates the action bits based on all matching tags.
896 * 1 : csp = Current client state (buffers, headers, etc...)
898 * Returns : 0 if no tag matched, or
901 *********************************************************************/
902 int update_action_bits_for_all_tags(struct client_state *csp)
904 struct list_entry *tag;
907 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
909 if (update_action_bits_for_tag(csp, tag->str))
919 /*********************************************************************
921 * Function : update_action_bits_for_tag
923 * Description : Updates the action bits based on the action sections
924 * whose tag patterns match a provided tag.
927 * 1 : csp = Current client state (buffers, headers, etc...)
928 * 2 : tag = The tag on which the update should be based on
930 * Returns : 0 if no tag matched, or
933 *********************************************************************/
934 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
936 struct file_list *fl;
937 struct url_actions *b;
943 assert(list_contains_item(csp->tags, tag));
945 /* Run through all action files, */
946 for (i = 0; i < MAX_AF_FILES; i++)
948 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
950 /* Skip empty files */
954 /* and through all the action patterns, */
955 for (b = b->next; NULL != b; b = b->next)
957 /* skip the URL patterns, */
958 if (NULL == b->url->tag_regex)
963 /* and check if one of the tag patterns matches the tag, */
964 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
966 /* if it does, update the action bit map, */
967 if (merge_current_action(csp->action, b->action))
969 log_error(LOG_LEVEL_ERROR,
970 "Out of memorey while changing action bits");
972 /* and signal the change. */
982 /*********************************************************************
984 * Function : free_current_action
986 * Description : Free memory used by a current_action_spec.
987 * Does not free the current_action_spec itself.
990 * 1 : src = Source to free.
994 *********************************************************************/
995 void free_current_action (struct current_action_spec *src)
999 for (i = 0; i < ACTION_STRING_COUNT; i++)
1001 freez(src->string[i]);
1004 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1006 destroy_list(src->multi[i]);
1009 memset(src, '\0', sizeof(*src));
1013 static struct file_list *current_actions_file[MAX_AF_FILES] = {
1014 NULL, NULL, NULL, NULL, NULL,
1015 NULL, NULL, NULL, NULL, NULL
1019 #ifdef FEATURE_GRACEFUL_TERMINATION
1020 /*********************************************************************
1022 * Function : unload_current_actions_file
1024 * Description : Unloads current actions file - reset to state at
1025 * beginning of program.
1031 *********************************************************************/
1032 void unload_current_actions_file(void)
1036 for (i = 0; i < MAX_AF_FILES; i++)
1038 if (current_actions_file[i])
1040 current_actions_file[i]->unloader = unload_actions_file;
1041 current_actions_file[i] = NULL;
1045 #endif /* FEATURE_GRACEFUL_TERMINATION */
1048 /*********************************************************************
1050 * Function : unload_actions_file
1052 * Description : Unloads an actions module.
1055 * 1 : file_data = the data structure associated with the
1060 *********************************************************************/
1061 void unload_actions_file(void *file_data)
1063 struct url_actions * next;
1064 struct url_actions * cur = (struct url_actions *)file_data;
1068 free_url_spec(cur->url);
1069 free_action(cur->action);
1077 /*********************************************************************
1079 * Function : free_alias_list
1081 * Description : Free memory used by a list of aliases.
1084 * 1 : alias_list = Linked list to free.
1088 *********************************************************************/
1089 void free_alias_list(struct action_alias *alias_list)
1091 while (alias_list != NULL)
1093 struct action_alias * next = alias_list->next;
1094 alias_list->next = NULL;
1095 freez(alias_list->name);
1096 free_action(alias_list->action);
1103 /*********************************************************************
1105 * Function : load_actions_file
1107 * Description : Read and parse all the action files and add to files
1111 * 1 : csp = Current client state (buffers, headers, etc...)
1113 * Returns : 0 => Ok, everything else is an error.
1115 *********************************************************************/
1116 int load_actions_file(struct client_state *csp)
1121 for (i = 0; i < MAX_AF_FILES; i++)
1123 if (csp->config->actions_file[i])
1125 result = load_one_actions_file(csp, i);
1131 else if (current_actions_file[i])
1133 current_actions_file[i]->unloader = unload_actions_file;
1134 current_actions_file[i] = NULL;
1141 /*********************************************************************
1143 * Function : load_one_actions_file
1145 * Description : Read and parse a action file and add to files
1149 * 1 : csp = Current client state (buffers, headers, etc...)
1150 * 2 : fileid = File index to load.
1152 * Returns : 0 => Ok, everything else is an error.
1154 *********************************************************************/
1155 static int load_one_actions_file(struct client_state *csp, int fileid)
1160 * Note: Keep these in the order they occur in the file, they are
1161 * sometimes tested with <=
1163 #define MODE_START_OF_FILE 1
1164 #define MODE_SETTINGS 2
1165 #define MODE_DESCRIPTION 3
1166 #define MODE_ALIAS 4
1167 #define MODE_ACTIONS 5
1169 int mode = MODE_START_OF_FILE;
1172 struct url_actions *last_perm;
1173 struct url_actions *perm;
1174 char buf[BUFFER_SIZE];
1175 struct file_list *fs;
1176 struct action_spec * cur_action = NULL;
1177 int cur_action_used = 0;
1178 struct action_alias * alias_list = NULL;
1179 unsigned long linenum = 0;
1181 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1183 /* No need to load */
1184 csp->actions_list[fileid] = current_actions_file[fileid];
1189 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1190 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1191 "with their complete file names.", csp->config->actions_file[fileid]);
1192 return 1; /* never get here */
1195 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1196 if (last_perm == NULL)
1198 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1199 csp->config->actions_file[fileid]);
1200 return 1; /* never get here */
1203 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1205 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1206 csp->config->actions_file[fileid]);
1207 return 1; /* never get here */
1210 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1214 /* It's a header block */
1217 /* It's {{settings}} or {{alias}} */
1218 size_t len = strlen(buf);
1219 char * start = buf + 2;
1220 char * end = buf + len - 1;
1221 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1225 log_error(LOG_LEVEL_FATAL,
1226 "can't load actions file '%s': invalid line (%lu): %s",
1227 csp->config->actions_file[fileid], linenum, buf);
1228 return 1; /* never get here */
1231 /* Trim leading and trailing whitespace. */
1239 log_error(LOG_LEVEL_FATAL,
1240 "can't load actions file '%s': invalid line (%lu): {{ }}",
1241 csp->config->actions_file[fileid], linenum);
1242 return 1; /* never get here */
1246 * An actionsfile can optionally contain the following blocks.
1247 * They *MUST* be in this order, to simplify processing:
1253 * ...free text, format TBD, but no line may start with a '{'...
1258 * The actual actions must be *after* these special blocks.
1259 * None of these special blocks may be repeated.
1262 if (0 == strcmpic(start, "settings"))
1264 /* it's a {{settings}} block */
1265 if (mode >= MODE_SETTINGS)
1267 /* {{settings}} must be first thing in file and must only
1271 log_error(LOG_LEVEL_FATAL,
1272 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1273 csp->config->actions_file[fileid], linenum);
1275 mode = MODE_SETTINGS;
1277 else if (0 == strcmpic(start, "description"))
1279 /* it's a {{description}} block */
1280 if (mode >= MODE_DESCRIPTION)
1282 /* {{description}} is a singleton and only {{settings}} may proceed it
1285 log_error(LOG_LEVEL_FATAL,
1286 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1287 csp->config->actions_file[fileid], linenum);
1289 mode = MODE_DESCRIPTION;
1291 else if (0 == strcmpic(start, "alias"))
1293 /* it's an {{alias}} block */
1294 if (mode >= MODE_ALIAS)
1296 /* {{alias}} must be first thing in file, possibly after
1297 * {{settings}} and {{description}}
1299 * {{alias}} must only appear once.
1301 * Note that these are new restrictions introduced in
1302 * v2.9.10 in order to make actionsfile editing simpler.
1303 * (Otherwise, reordering actionsfile entries without
1304 * completely rewriting the file becomes non-trivial)
1307 log_error(LOG_LEVEL_FATAL,
1308 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1309 csp->config->actions_file[fileid], linenum);
1315 /* invalid {{something}} block */
1317 log_error(LOG_LEVEL_FATAL,
1318 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1319 csp->config->actions_file[fileid], linenum, start);
1320 return 1; /* never get here */
1325 /* It's an actions block */
1327 char actions_buf[BUFFER_SIZE];
1331 mode = MODE_ACTIONS;
1333 /* free old action */
1336 if (!cur_action_used)
1338 free_action(cur_action);
1343 cur_action_used = 0;
1344 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1345 if (cur_action == NULL)
1348 log_error(LOG_LEVEL_FATAL,
1349 "can't load actions file '%s': out of memory",
1350 csp->config->actions_file[fileid]);
1351 return 1; /* never get here */
1353 init_action(cur_action);
1356 strlcpy(actions_buf, buf + 1, sizeof(actions_buf));
1358 /* check we have a trailing } and then trim it */
1359 end = actions_buf + strlen(actions_buf) - 1;
1364 log_error(LOG_LEVEL_FATAL,
1365 "can't load actions file '%s': invalid line (%lu): %s",
1366 csp->config->actions_file[fileid], linenum, buf);
1367 return 1; /* never get here */
1371 /* trim any whitespace immediately inside {} */
1374 if (get_actions(actions_buf, alias_list, cur_action))
1378 log_error(LOG_LEVEL_FATAL,
1379 "can't load actions file '%s': invalid line (%lu): %s",
1380 csp->config->actions_file[fileid], linenum, buf);
1381 return 1; /* never get here */
1385 else if (mode == MODE_SETTINGS)
1388 * Part of the {{settings}} block.
1389 * For now only serves to check if the file's minimum Privoxy
1390 * version requirement is met, but we may want to read & check
1391 * permissions when we go multi-user.
1393 if (!strncmp(buf, "for-privoxy-version=", 20))
1395 char *version_string, *fields[3];
1398 if ((version_string = strdup(buf + 20)) == 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 */
1407 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1409 if (num_fields < 1 || atoi(fields[0]) == 0)
1411 log_error(LOG_LEVEL_ERROR,
1412 "While loading actions file '%s': invalid line (%lu): %s",
1413 csp->config->actions_file[fileid], linenum, buf);
1415 else if ( atoi(fields[0]) > VERSION_MAJOR
1416 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1417 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1420 log_error(LOG_LEVEL_FATAL,
1421 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1422 csp->config->actions_file[fileid], linenum, buf );
1423 return 1; /* never get here */
1425 free(version_string);
1428 else if (mode == MODE_DESCRIPTION)
1431 * Part of the {{description}} block.
1435 else if (mode == MODE_ALIAS)
1440 char actions_buf[BUFFER_SIZE];
1441 struct action_alias * new_alias;
1443 char * start = strchr(buf, '=');
1446 if ((start == NULL) || (start == buf))
1448 log_error(LOG_LEVEL_FATAL,
1449 "can't load actions file '%s': invalid alias line (%lu): %s",
1450 csp->config->actions_file[fileid], linenum, buf);
1451 return 1; /* never get here */
1454 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1457 log_error(LOG_LEVEL_FATAL,
1458 "can't load actions file '%s': out of memory!",
1459 csp->config->actions_file[fileid]);
1460 return 1; /* never get here */
1463 /* Eat any the whitespace before the '=' */
1465 while ((*end == ' ') || (*end == '\t'))
1468 * we already know we must have at least 1 non-ws char
1469 * at start of buf - no need to check
1475 /* Eat any the whitespace after the '=' */
1477 while ((*start == ' ') || (*start == '\t'))
1483 log_error(LOG_LEVEL_FATAL,
1484 "can't load actions file '%s': invalid alias line (%lu): %s",
1485 csp->config->actions_file[fileid], linenum, buf);
1486 return 1; /* never get here */
1489 if ((new_alias->name = strdup(buf)) == NULL)
1492 log_error(LOG_LEVEL_FATAL,
1493 "can't load actions file '%s': out of memory!",
1494 csp->config->actions_file[fileid]);
1495 return 1; /* never get here */
1498 strlcpy(actions_buf, start, sizeof(actions_buf));
1500 if (get_actions(actions_buf, alias_list, new_alias->action))
1504 log_error(LOG_LEVEL_FATAL,
1505 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1506 csp->config->actions_file[fileid], linenum, buf, start);
1507 return 1; /* never get here */
1511 new_alias->next = alias_list;
1512 alias_list = new_alias;
1514 else if (mode == MODE_ACTIONS)
1516 /* it's a URL pattern */
1518 /* allocate a new node */
1519 if ((perm = zalloc(sizeof(*perm))) == NULL)
1522 log_error(LOG_LEVEL_FATAL,
1523 "can't load actions file '%s': out of memory!",
1524 csp->config->actions_file[fileid]);
1525 return 1; /* never get here */
1529 copy_action (perm->action, cur_action);
1531 /* Save the URL pattern */
1532 if (create_url_spec(perm->url, buf))
1535 log_error(LOG_LEVEL_FATAL,
1536 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1537 csp->config->actions_file[fileid], linenum, buf);
1538 return 1; /* never get here */
1541 /* add it to the list */
1542 last_perm->next = perm;
1545 else if (mode == MODE_START_OF_FILE)
1547 /* oops - please have a {} line as 1st line in file. */
1549 log_error(LOG_LEVEL_FATAL,
1550 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1551 csp->config->actions_file[fileid], linenum, buf);
1552 return 1; /* never get here */
1556 /* How did we get here? This is impossible! */
1558 log_error(LOG_LEVEL_FATAL,
1559 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1560 csp->config->actions_file[fileid], mode);
1561 return 1; /* never get here */
1567 free_action(cur_action);
1570 free_alias_list(alias_list);
1572 /* the old one is now obsolete */
1573 if (current_actions_file[fileid])
1575 current_actions_file[fileid]->unloader = unload_actions_file;
1578 fs->next = files->next;
1580 current_actions_file[fileid] = fs;
1582 csp->actions_list[fileid] = fs;
1589 /*********************************************************************
1591 * Function : actions_to_text
1593 * Description : Converts a actionsfile entry from the internal
1594 * structure into a text line. The output is split
1595 * into one line for each action with line continuation.
1598 * 1 : action = The action to format.
1600 * Returns : A string. Caller must free it.
1601 * NULL on out-of-memory error.
1603 *********************************************************************/
1604 char * actions_to_text(const struct action_spec *action)
1606 unsigned mask = action->mask;
1607 unsigned add = action->add;
1608 char * result = strdup("");
1609 struct list_entry * lst;
1611 /* sanity - prevents "-feature +feature" */
1615 #define DEFINE_ACTION_BOOL(__name, __bit) \
1616 if (!(mask & __bit)) \
1618 string_append(&result, " -" __name " \\\n"); \
1620 else if (add & __bit) \
1622 string_append(&result, " +" __name " \\\n"); \
1625 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1626 if (!(mask & __bit)) \
1628 string_append(&result, " -" __name " \\\n"); \
1630 else if (add & __bit) \
1632 string_append(&result, " +" __name "{"); \
1633 string_append(&result, action->string[__index]); \
1634 string_append(&result, "} \\\n"); \
1637 #define DEFINE_ACTION_MULTI(__name, __index) \
1638 if (action->multi_remove_all[__index]) \
1640 string_append(&result, " -" __name " \\\n"); \
1644 lst = action->multi_remove[__index]->first; \
1647 string_append(&result, " -" __name "{"); \
1648 string_append(&result, lst->str); \
1649 string_append(&result, "} \\\n"); \
1653 lst = action->multi_add[__index]->first; \
1656 string_append(&result, " +" __name "{"); \
1657 string_append(&result, lst->str); \
1658 string_append(&result, "} \\\n"); \
1662 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1664 #include "actionlist.h"
1666 #undef DEFINE_ACTION_MULTI
1667 #undef DEFINE_ACTION_STRING
1668 #undef DEFINE_ACTION_BOOL
1669 #undef DEFINE_ACTION_ALIAS
1675 /*********************************************************************
1677 * Function : actions_to_html
1679 * Description : Converts a actionsfile entry from numeric form
1680 * ("mask" and "add") to a <br>-seperated HTML string
1681 * in which each action is linked to its chapter in
1685 * 1 : csp = Client state (for config)
1686 * 2 : action = Action spec to be converted
1688 * Returns : A string. Caller must free it.
1689 * NULL on out-of-memory error.
1691 *********************************************************************/
1692 char * actions_to_html(const struct client_state *csp,
1693 const struct action_spec *action)
1695 unsigned mask = action->mask;
1696 unsigned add = action->add;
1697 char * result = strdup("");
1698 struct list_entry * lst;
1700 /* sanity - prevents "-feature +feature" */
1704 #define DEFINE_ACTION_BOOL(__name, __bit) \
1705 if (!(mask & __bit)) \
1707 string_append(&result, "\n<br>-"); \
1708 string_join(&result, add_help_link(__name, csp->config)); \
1710 else if (add & __bit) \
1712 string_append(&result, "\n<br>+"); \
1713 string_join(&result, add_help_link(__name, csp->config)); \
1716 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1717 if (!(mask & __bit)) \
1719 string_append(&result, "\n<br>-"); \
1720 string_join(&result, add_help_link(__name, csp->config)); \
1722 else if (add & __bit) \
1724 string_append(&result, "\n<br>+"); \
1725 string_join(&result, add_help_link(__name, csp->config)); \
1726 string_append(&result, "{"); \
1727 string_join(&result, html_encode(action->string[__index])); \
1728 string_append(&result, "}"); \
1731 #define DEFINE_ACTION_MULTI(__name, __index) \
1732 if (action->multi_remove_all[__index]) \
1734 string_append(&result, "\n<br>-"); \
1735 string_join(&result, add_help_link(__name, csp->config)); \
1739 lst = action->multi_remove[__index]->first; \
1742 string_append(&result, "\n<br>-"); \
1743 string_join(&result, add_help_link(__name, csp->config)); \
1744 string_append(&result, "{"); \
1745 string_join(&result, html_encode(lst->str)); \
1746 string_append(&result, "}"); \
1750 lst = action->multi_add[__index]->first; \
1753 string_append(&result, "\n<br>+"); \
1754 string_join(&result, add_help_link(__name, csp->config)); \
1755 string_append(&result, "{"); \
1756 string_join(&result, html_encode(lst->str)); \
1757 string_append(&result, "}"); \
1761 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1763 #include "actionlist.h"
1765 #undef DEFINE_ACTION_MULTI
1766 #undef DEFINE_ACTION_STRING
1767 #undef DEFINE_ACTION_BOOL
1768 #undef DEFINE_ACTION_ALIAS
1770 /* trim leading <br> */
1771 if (result && *result)
1774 result = strdup(result + 5);
1782 /*********************************************************************
1784 * Function : current_actions_to_html
1786 * Description : Converts a curren action spec to a <br> seperated HTML
1787 * text in which each action is linked to its chapter in
1791 * 1 : csp = Client state (for config)
1792 * 2 : action = Current action spec to be converted
1794 * Returns : A string. Caller must free it.
1795 * NULL on out-of-memory error.
1797 *********************************************************************/
1798 char *current_action_to_html(const struct client_state *csp,
1799 const struct current_action_spec *action)
1801 unsigned long flags = action->flags;
1802 char * result = strdup("");
1803 struct list_entry * lst;
1805 #define DEFINE_ACTION_BOOL(__name, __bit) \
1806 if (flags & __bit) \
1808 string_append(&result, "\n<br>+"); \
1809 string_join(&result, add_help_link(__name, csp->config)); \
1813 string_append(&result, "\n<br>-"); \
1814 string_join(&result, add_help_link(__name, csp->config)); \
1817 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1818 if (flags & __bit) \
1820 string_append(&result, "\n<br>+"); \
1821 string_join(&result, add_help_link(__name, csp->config)); \
1822 string_append(&result, "{"); \
1823 string_join(&result, html_encode(action->string[__index])); \
1824 string_append(&result, "}"); \
1828 string_append(&result, "\n<br>-"); \
1829 string_join(&result, add_help_link(__name, csp->config)); \
1832 #define DEFINE_ACTION_MULTI(__name, __index) \
1833 lst = action->multi[__index]->first; \
1836 string_append(&result, "\n<br>-"); \
1837 string_join(&result, add_help_link(__name, csp->config)); \
1843 string_append(&result, "\n<br>+"); \
1844 string_join(&result, add_help_link(__name, csp->config)); \
1845 string_append(&result, "{"); \
1846 string_join(&result, html_encode(lst->str)); \
1847 string_append(&result, "}"); \
1852 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1854 #include "actionlist.h"
1856 #undef DEFINE_ACTION_MULTI
1857 #undef DEFINE_ACTION_STRING
1858 #undef DEFINE_ACTION_BOOL
1859 #undef DEFINE_ACTION_ALIAS