1 const char actions_rcs[] = "$Id: actions.c,v 1.38 2007/04/15 16:39:20 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.38 2007/04/15 16:39:20 fabiankeil
37 * Introduce tags as alternative way to specify which
38 * actions apply to a request. At the moment tags can be
39 * created based on client and server headers.
41 * Revision 1.37 2007/03/11 15:56:12 fabiankeil
42 * Add kludge to log unknown aliases and actions before exiting.
44 * Revision 1.36 2006/12/28 17:15:42 fabiankeil
45 * Fix gcc43 conversion warning.
47 * Revision 1.35 2006/07/18 14:48:45 david__schmidt
48 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
49 * with what was really the latest development (the v_3_0_branch branch)
51 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
52 * Multiple filter file support
54 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
55 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
56 * this becomes important when jcc.h gets included later down the line.
58 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
59 * - Implemented Privoxy version requirement through
60 * for-privoxy-version= statement in {{settings}}
62 * - Fix for unchecked out-of-memory condition
64 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
65 * Fixed memory leak reported by Dan Price in Bug #694713
67 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
68 * Extended unload_current_actions_file() to multiple AFs.
69 * Thanks to Oliver Stoeneberg for the hint.
71 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
72 * Change unsigned to unsigned long in actions_name struct. This closes
73 * SourceForge Bug #539284.
75 * Revision 1.32 2002/05/12 21:36:29 jongfoster
76 * Correcting function comments
78 * Revision 1.31 2002/05/06 07:56:50 oes
79 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
81 * Revision 1.30 2002/04/30 11:14:52 oes
82 * Made csp the first parameter in *action_to_html
84 * Revision 1.29 2002/04/26 19:30:54 jongfoster
85 * - current_action_to_html(): Adding help link for the "-" form of
87 * - Some actions had "<br>-", some "<br> -" (note the space).
88 * Standardizing on no space.
89 * - Greatly simplifying some of the code by using string_join()
92 * Revision 1.28 2002/04/26 12:53:15 oes
93 * - CGI AF editor now writes action lines split into
94 * single lines with line continuation
95 * - actions_to_html now embeds each action name in
97 * - current_action_to_text is now called current_action_to_html
98 * and acts like actions_to_html
100 * Revision 1.27 2002/04/24 02:10:31 oes
101 * - Jon's patch for multiple AFs:
102 * - split load_actions_file, add load_one_actions_file
103 * - make csp->actions_list files an array
104 * - remember file id with each action
105 * - Copy_action now frees dest action before copying
107 * Revision 1.26 2002/03/26 22:29:54 swa
108 * we have a new homepage!
110 * Revision 1.25 2002/03/24 13:25:43 swa
111 * name change related issues
113 * Revision 1.24 2002/03/16 23:54:06 jongfoster
114 * Adding graceful termination feature, to help look for memory leaks.
115 * If you enable this (which, by design, has to be done by hand
116 * editing config.h) and then go to http://i.j.b/die, then the program
117 * will exit cleanly after the *next* request. It should free all the
118 * memory that was used.
120 * Revision 1.23 2002/03/07 03:46:16 oes
121 * Fixed compiler warnings
123 * Revision 1.22 2002/01/21 00:27:02 jongfoster
124 * Allowing free_action(NULL).
125 * Moving the functions that #include actionlist.h to the end of the file,
126 * because the Visual C++ 97 debugger gets extremely confused if you try
127 * to debug any code that comes after them in the file.
129 * Revision 1.21 2002/01/17 20:54:44 jongfoster
130 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
132 * Revision 1.20 2001/11/22 21:56:49 jongfoster
133 * Making action_spec->flags into an unsigned long rather than just an
135 * Fixing a bug in the display of -add-header and -wafer
137 * Revision 1.19 2001/11/13 00:14:07 jongfoster
138 * Fixing stupid bug now I've figured out what || means.
139 * (It always returns 0 or 1, not one of it's paramaters.)
141 * Revision 1.18 2001/11/07 00:06:06 steudten
142 * Add line number in error output for lineparsing for
145 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
146 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
147 * threads to call select() simultaneously. So, it's time to do a real, live,
148 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
149 * (native). Both versions will work, but using __OS2__ offers multi-threading.
151 * Revision 1.16 2001/10/23 21:30:30 jongfoster
152 * Adding error-checking to selected functions.
154 * Revision 1.15 2001/10/14 21:58:22 jongfoster
155 * Adding support for the CGI-based editor:
156 * - Exported get_actions()
157 * - Added new function free_alias_list()
158 * - Added support for {{settings}} and {{description}} blocks
159 * in the actions file. They are currently ignored.
160 * - Added restriction to only one {{alias}} block which must appear
161 * first in the file, to simplify the editor's rewriting rules.
162 * - Note that load_actions_file() is no longer used by the CGI-based
163 * editor, but some of the other routines in this file are.
165 * Revision 1.14 2001/09/22 16:36:59 jongfoster
166 * Removing unused parameter fs from read_config_line()
168 * Revision 1.13 2001/09/16 15:47:37 jongfoster
169 * First version of CGI-based edit interface. This is very much a
170 * work-in-progress, and you can't actually use it to edit anything
171 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
172 * to have any effect.
174 * Revision 1.12 2001/09/16 13:21:27 jongfoster
175 * Changes to use new list functions.
177 * Revision 1.11 2001/09/14 00:17:32 jongfoster
178 * Tidying up memory allocation. New function init_action().
180 * Revision 1.10 2001/09/10 10:14:34 oes
181 * Removing unused variable
183 * Revision 1.9 2001/07/30 22:08:36 jongfoster
184 * Tidying up #defines:
185 * - All feature #defines are now of the form FEATURE_xxx
186 * - Permanently turned off WIN_GUI_EDIT
187 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
189 * Revision 1.8 2001/06/29 13:19:52 oes
190 * Removed logentry from cancelled commit
192 * Revision 1.7 2001/06/09 10:55:28 jongfoster
193 * Changing BUFSIZ ==> BUFFER_SIZE
195 * Revision 1.6 2001/06/07 23:04:34 jongfoster
196 * Made get_actions() static.
198 * Revision 1.5 2001/06/03 19:11:48 oes
199 * adapted to new enlist_unique arg format
201 * Revision 1.4 2001/06/01 20:03:42 jongfoster
202 * Better memory management - current_action->strings[] now
203 * contains copies of the strings, not the original.
205 * Revision 1.3 2001/06/01 18:49:17 jongfoster
206 * Replaced "list_share" with "list" - the tiny memory gain was not
207 * worth the extra complexity.
209 * Revision 1.2 2001/05/31 21:40:00 jongfoster
210 * Removing some commented out, obsolete blocks of code.
212 * Revision 1.1 2001/05/31 21:16:46 jongfoster
213 * Moved functions to process the action list into this new file.
216 *********************************************************************/
226 #ifdef FEATURE_PTHREAD
234 #include "miscutil.h"
238 #include "urlmatch.h"
242 const char actions_h_rcs[] = ACTIONS_H_VERSION;
246 * We need the main list of options.
248 * First, we need a way to tell between boolean, string, and multi-string
249 * options. For string and multistring options, we also need to be
250 * able to tell the difference between a "+" and a "-". (For bools,
251 * the "+"/"-" information is encoded in "add" and "mask"). So we use
252 * an enumerated type (well, the preprocessor equivalent). Here are
255 #define AV_NONE 0 /* +opt -opt */
256 #define AV_ADD_STRING 1 /* +stropt{string} */
257 #define AV_REM_STRING 2 /* -stropt */
258 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
259 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
262 * We need a structure to hold the name, flag changes,
263 * type, and string index.
268 unsigned long mask; /* a bit set to "0" = remove action */
269 unsigned long add; /* a bit set to "1" = add action */
270 int takes_value; /* an AV_... constant */
271 int index; /* index into strings[] or multi[] */
275 * And with those building blocks in place, here's the array.
277 static const struct action_name action_names[] =
280 * Well actually there's no data here - it's in actionlist.h
281 * This keeps it together to make it easy to change.
283 * Here's the macros used to format it:
285 #define DEFINE_ACTION_MULTI(name,index) \
286 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
287 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
288 #define DEFINE_ACTION_STRING(name,flag,index) \
289 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
290 { "-" name, ~flag, 0, AV_REM_STRING, index },
291 #define DEFINE_ACTION_BOOL(name,flag) \
292 { "+" name, ACTION_MASK_ALL, flag }, \
293 { "-" name, ~flag, 0 },
294 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
296 #include "actionlist.h"
298 #undef DEFINE_ACTION_MULTI
299 #undef DEFINE_ACTION_STRING
300 #undef DEFINE_ACTION_BOOL
301 #undef DEFINE_ACTION_ALIAS
303 { NULL, 0, 0 } /* End marker */
307 static int load_one_actions_file(struct client_state *csp, int fileid);
310 /*********************************************************************
312 * Function : merge_actions
314 * Description : Merge two actions together.
315 * Similar to "dest += src".
318 * 1 : dest = Actions to modify.
319 * 2 : src = Action to add.
321 * Returns : JB_ERR_OK or JB_ERR_MEMORY
323 *********************************************************************/
324 jb_err merge_actions (struct action_spec *dest,
325 const struct action_spec *src)
330 dest->mask &= src->mask;
331 dest->add &= src->mask;
332 dest->add |= src->add;
334 for (i = 0; i < ACTION_STRING_COUNT; i++)
336 char * str = src->string[i];
339 freez(dest->string[i]);
340 dest->string[i] = strdup(str);
341 if (NULL == dest->string[i])
343 return JB_ERR_MEMORY;
348 for (i = 0; i < ACTION_MULTI_COUNT; i++)
350 if (src->multi_remove_all[i])
352 /* Remove everything from dest */
353 list_remove_all(dest->multi_remove[i]);
354 dest->multi_remove_all[i] = 1;
356 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
358 else if (dest->multi_remove_all[i])
361 * dest already removes everything, so we only need to worry
364 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
365 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
369 /* No "remove all"s to worry about. */
370 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
371 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
372 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
385 /*********************************************************************
387 * Function : copy_action
389 * Description : Copy an action_specs.
390 * Similar to "dest = src".
393 * 1 : dest = Destination of copy.
394 * 2 : src = Source for copy.
398 *********************************************************************/
399 jb_err copy_action (struct action_spec *dest,
400 const struct action_spec *src)
403 jb_err err = JB_ERR_OK;
406 memset(dest, '\0', sizeof(*dest));
408 dest->mask = src->mask;
409 dest->add = src->add;
411 for (i = 0; i < ACTION_STRING_COUNT; i++)
413 char * str = src->string[i];
419 return JB_ERR_MEMORY;
421 dest->string[i] = str;
425 for (i = 0; i < ACTION_MULTI_COUNT; i++)
427 dest->multi_remove_all[i] = src->multi_remove_all[i];
428 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
433 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
443 /*********************************************************************
445 * Function : free_action
447 * Description : Destroy an action_spec. Frees memory used by it,
448 * except for the memory used by the struct action_spec
452 * 1 : src = Source to free.
456 *********************************************************************/
457 void free_action (struct action_spec *src)
466 for (i = 0; i < ACTION_STRING_COUNT; i++)
468 freez(src->string[i]);
471 for (i = 0; i < ACTION_MULTI_COUNT; i++)
473 destroy_list(src->multi_remove[i]);
474 destroy_list(src->multi_add[i]);
477 memset(src, '\0', sizeof(*src));
481 /*********************************************************************
483 * Function : get_action_token
485 * Description : Parses a line for the first action.
486 * Modifies it's input array, doesn't allocate memory.
488 * *line=" +abc{def} -ghi "
495 * 1 : line = [in] The line containing the action.
496 * [out] Start of next action on line, or
497 * NULL if we reached the end of line before
498 * we found an action.
499 * 2 : name = [out] Start of action name, null
500 * terminated. NULL on EOL
501 * 3 : value = [out] Start of action value, null
502 * terminated. NULL if none or EOL.
504 * Returns : JB_ERR_OK => Ok
505 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
507 *********************************************************************/
508 jb_err get_action_token(char **line, char **name, char **value)
513 /* set default returns */
518 /* Eat any leading whitespace */
519 while ((*str == ' ') || (*str == '\t'))
531 /* null name, just value is prohibited */
538 while (((ch = *str) != '\0') &&
539 (ch != ' ') && (ch != '\t') && (ch != '{'))
543 /* error, '}' without '{' */
555 /* EOL - be careful not to run off buffer */
560 /* More to parse next time. */
569 str = strchr(str, '}');
587 /*********************************************************************
589 * Function : get_actions
591 * Description : Parses a list of actions.
594 * 1 : line = The string containing the actions.
595 * Will be written to by this function.
596 * 2 : alias_list = Custom alias list, or NULL for none.
597 * 3 : cur_action = Where to store the action. Caller
600 * Returns : JB_ERR_OK => Ok
601 * JB_ERR_PARSE => Parse error (line was trashed anyway)
602 * nonzero => Out of memory (line was trashed anyway)
604 *********************************************************************/
605 jb_err get_actions(char *line,
606 struct action_alias * alias_list,
607 struct action_spec *cur_action)
610 init_action(cur_action);
611 cur_action->mask = ACTION_MASK_ALL;
615 char * option = NULL;
618 err = get_action_token(&line, &option, &value);
626 /* handle option in 'option' */
628 /* Check for standard action name */
629 const struct action_name * action = action_names;
631 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
635 if (action->name != NULL)
638 cur_action->mask &= action->mask;
639 cur_action->add &= action->mask;
640 cur_action->add |= action->add;
642 switch (action->takes_value)
645 /* ignore any option. */
649 /* add single string. */
651 if ((value == NULL) || (*value == '\0'))
655 /* FIXME: should validate option string here */
656 freez (cur_action->string[action->index]);
657 cur_action->string[action->index] = strdup(value);
658 if (NULL == cur_action->string[action->index])
660 return JB_ERR_MEMORY;
666 /* remove single string. */
668 freez (cur_action->string[action->index]);
673 /* append multi string. */
675 struct list * remove_p = cur_action->multi_remove[action->index];
676 struct list * add_p = cur_action->multi_add[action->index];
678 if ((value == NULL) || (*value == '\0'))
683 list_remove_item(remove_p, value);
684 err = enlist_unique(add_p, value, 0);
693 /* remove multi string. */
695 struct list * remove_p = cur_action->multi_remove[action->index];
696 struct list * add_p = cur_action->multi_add[action->index];
698 if ( (value == NULL) || (*value == '\0')
699 || ((*value == '*') && (value[1] == '\0')) )
702 * no option, or option == "*".
706 list_remove_all(remove_p);
707 list_remove_all(add_p);
708 cur_action->multi_remove_all[action->index] = 1;
712 /* Valid option - remove only 1 option */
714 if ( !cur_action->multi_remove_all[action->index] )
716 /* there isn't a catch-all in the remove list already */
717 err = enlist_unique(remove_p, value, 0);
723 list_remove_item(add_p, value);
728 /* Shouldn't get here unless there's memory corruption. */
735 /* try user aliases. */
736 const struct action_alias * alias = alias_list;
738 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
745 merge_actions(cur_action, alias->action);
749 /* Bad action name */
751 * XXX: This is a fatal error and Privoxy will later on exit
752 * in load_one_actions_file() because of an "invalid line".
754 * It would be preferable to name the offending option in that
755 * error message, but currently there is no way to do that and
756 * we have to live with two error messages for basically the
759 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
770 /*********************************************************************
772 * Function : init_current_action
774 * Description : Zero out an action.
777 * 1 : dest = An uninitialized current_action_spec.
781 *********************************************************************/
782 void init_current_action (struct current_action_spec *dest)
784 memset(dest, '\0', sizeof(*dest));
786 dest->flags = ACTION_MOST_COMPATIBLE;
790 /*********************************************************************
792 * Function : init_action
794 * Description : Zero out an action.
797 * 1 : dest = An uninitialized action_spec.
801 *********************************************************************/
802 void init_action (struct action_spec *dest)
804 memset(dest, '\0', sizeof(*dest));
808 /*********************************************************************
810 * Function : merge_current_action
812 * Description : Merge two actions together.
813 * Similar to "dest += src".
814 * Differences between this and merge_actions()
815 * is that this one doesn't allocate memory for
816 * strings (so "src" better be in memory for at least
817 * as long as "dest" is, and you'd better free
818 * "dest" using "free_current_action").
819 * Also, there is no mask or remove lists in dest.
820 * (If we're applying it to a URL, we don't need them)
823 * 1 : dest = Current actions, to modify.
824 * 2 : src = Action to add.
826 * Returns 0 : no error
827 * !=0 : error, probably JB_ERR_MEMORY.
829 *********************************************************************/
830 jb_err merge_current_action (struct current_action_spec *dest,
831 const struct action_spec *src)
834 jb_err err = JB_ERR_OK;
836 dest->flags &= src->mask;
837 dest->flags |= src->add;
839 for (i = 0; i < ACTION_STRING_COUNT; i++)
841 char * str = src->string[i];
847 return JB_ERR_MEMORY;
849 freez(dest->string[i]);
850 dest->string[i] = str;
854 for (i = 0; i < ACTION_MULTI_COUNT; i++)
856 if (src->multi_remove_all[i])
858 /* Remove everything from dest, then add src->multi_add */
859 err = list_duplicate(dest->multi[i], src->multi_add[i]);
867 list_remove_list(dest->multi[i], src->multi_remove[i]);
868 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
879 /*********************************************************************
881 * Function : update_action_bits_for_all_tags
883 * Description : Updates the action bits based on all matching tags.
886 * 1 : csp = Current client state (buffers, headers, etc...)
888 * Returns : 0 if no tag matched, or
891 *********************************************************************/
892 int update_action_bits_for_all_tags(struct client_state *csp)
894 struct list_entry *tag;
897 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
899 if (update_action_bits_for_tag(csp, tag->str))
909 /*********************************************************************
911 * Function : update_action_bits_for_tag
913 * Description : Updates the action bits based on the action sections
914 * whose tag patterns match a provided tag.
917 * 1 : csp = Current client state (buffers, headers, etc...)
918 * 2 : tag = The tag on which the update should be based on
920 * Returns : 0 if no tag matched, or
923 *********************************************************************/
924 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
926 struct file_list *fl;
927 struct url_actions *b;
933 assert(list_contains_item(csp->tags, tag));
935 /* Run through all action files, */
936 for (i = 0; i < MAX_AF_FILES; i++)
938 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
940 /* Skip empty files */
944 /* and through all the action patterns, */
945 for (b = b->next; NULL != b; b = b->next)
947 /* skip the URL patterns, */
948 if (NULL == b->url->tag_regex)
953 /* and check if one of the tag patterns matches the tag, */
954 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
956 /* if it does, update the action bit map, */
957 if (merge_current_action(csp->action, b->action))
959 log_error(LOG_LEVEL_ERROR,
960 "Out of memorey while changing action bits");
962 /* and signal the change. */
972 /*********************************************************************
974 * Function : free_current_action
976 * Description : Free memory used by a current_action_spec.
977 * Does not free the current_action_spec itself.
980 * 1 : src = Source to free.
984 *********************************************************************/
985 void free_current_action (struct current_action_spec *src)
989 for (i = 0; i < ACTION_STRING_COUNT; i++)
991 freez(src->string[i]);
994 for (i = 0; i < ACTION_MULTI_COUNT; i++)
996 destroy_list(src->multi[i]);
999 memset(src, '\0', sizeof(*src));
1003 static struct file_list *current_actions_file[MAX_AF_FILES] = {
1004 NULL, NULL, NULL, NULL, NULL,
1005 NULL, NULL, NULL, NULL, NULL
1009 #ifdef FEATURE_GRACEFUL_TERMINATION
1010 /*********************************************************************
1012 * Function : unload_current_actions_file
1014 * Description : Unloads current actions file - reset to state at
1015 * beginning of program.
1021 *********************************************************************/
1022 void unload_current_actions_file(void)
1026 for (i = 0; i < MAX_AF_FILES; i++)
1028 if (current_actions_file[i])
1030 current_actions_file[i]->unloader = unload_actions_file;
1031 current_actions_file[i] = NULL;
1035 #endif /* FEATURE_GRACEFUL_TERMINATION */
1038 /*********************************************************************
1040 * Function : unload_actions_file
1042 * Description : Unloads an actions module.
1045 * 1 : file_data = the data structure associated with the
1050 *********************************************************************/
1051 void unload_actions_file(void *file_data)
1053 struct url_actions * next;
1054 struct url_actions * cur = (struct url_actions *)file_data;
1058 free_url_spec(cur->url);
1059 free_action(cur->action);
1067 /*********************************************************************
1069 * Function : free_alias_list
1071 * Description : Free memory used by a list of aliases.
1074 * 1 : alias_list = Linked list to free.
1078 *********************************************************************/
1079 void free_alias_list(struct action_alias *alias_list)
1081 while (alias_list != NULL)
1083 struct action_alias * next = alias_list->next;
1084 alias_list->next = NULL;
1085 freez(alias_list->name);
1086 free_action(alias_list->action);
1093 /*********************************************************************
1095 * Function : load_actions_file
1097 * Description : Read and parse all the action files and add to files
1101 * 1 : csp = Current client state (buffers, headers, etc...)
1103 * Returns : 0 => Ok, everything else is an error.
1105 *********************************************************************/
1106 int load_actions_file(struct client_state *csp)
1111 for (i = 0; i < MAX_AF_FILES; i++)
1113 if (csp->config->actions_file[i])
1115 result = load_one_actions_file(csp, i);
1121 else if (current_actions_file[i])
1123 current_actions_file[i]->unloader = unload_actions_file;
1124 current_actions_file[i] = NULL;
1131 /*********************************************************************
1133 * Function : load_one_actions_file
1135 * Description : Read and parse a action file and add to files
1139 * 1 : csp = Current client state (buffers, headers, etc...)
1140 * 2 : fileid = File index to load.
1142 * Returns : 0 => Ok, everything else is an error.
1144 *********************************************************************/
1145 static int load_one_actions_file(struct client_state *csp, int fileid)
1150 * Note: Keep these in the order they occur in the file, they are
1151 * sometimes tested with <=
1153 #define MODE_START_OF_FILE 1
1154 #define MODE_SETTINGS 2
1155 #define MODE_DESCRIPTION 3
1156 #define MODE_ALIAS 4
1157 #define MODE_ACTIONS 5
1159 int mode = MODE_START_OF_FILE;
1162 struct url_actions *last_perm;
1163 struct url_actions *perm;
1164 char buf[BUFFER_SIZE];
1165 struct file_list *fs;
1166 struct action_spec * cur_action = NULL;
1167 int cur_action_used = 0;
1168 struct action_alias * alias_list = NULL;
1169 unsigned long linenum = 0;
1171 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1173 /* No need to load */
1174 csp->actions_list[fileid] = current_actions_file[fileid];
1179 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1180 csp->config->actions_file[fileid]);
1181 return 1; /* never get here */
1184 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1185 if (last_perm == NULL)
1187 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1188 csp->config->actions_file[fileid]);
1189 return 1; /* never get here */
1192 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1194 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1195 csp->config->actions_file[fileid]);
1196 return 1; /* never get here */
1199 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1203 /* It's a header block */
1206 /* It's {{settings}} or {{alias}} */
1207 size_t len = strlen(buf);
1208 char * start = buf + 2;
1209 char * end = buf + len - 1;
1210 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1214 log_error(LOG_LEVEL_FATAL,
1215 "can't load actions file '%s': invalid line (%lu): %s",
1216 csp->config->actions_file[fileid], linenum, buf);
1217 return 1; /* never get here */
1220 /* Trim leading and trailing whitespace. */
1228 log_error(LOG_LEVEL_FATAL,
1229 "can't load actions file '%s': invalid line (%lu): {{ }}",
1230 csp->config->actions_file[fileid], linenum);
1231 return 1; /* never get here */
1235 * An actionsfile can optionally contain the following blocks.
1236 * They *MUST* be in this order, to simplify processing:
1242 * ...free text, format TBD, but no line may start with a '{'...
1247 * The actual actions must be *after* these special blocks.
1248 * None of these special blocks may be repeated.
1251 if (0 == strcmpic(start, "settings"))
1253 /* it's a {{settings}} block */
1254 if (mode >= MODE_SETTINGS)
1256 /* {{settings}} must be first thing in file and must only
1260 log_error(LOG_LEVEL_FATAL,
1261 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1262 csp->config->actions_file[fileid], linenum);
1264 mode = MODE_SETTINGS;
1266 else if (0 == strcmpic(start, "description"))
1268 /* it's a {{description}} block */
1269 if (mode >= MODE_DESCRIPTION)
1271 /* {{description}} is a singleton and only {{settings}} may proceed it
1274 log_error(LOG_LEVEL_FATAL,
1275 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1276 csp->config->actions_file[fileid], linenum);
1278 mode = MODE_DESCRIPTION;
1280 else if (0 == strcmpic(start, "alias"))
1282 /* it's an {{alias}} block */
1283 if (mode >= MODE_ALIAS)
1285 /* {{alias}} must be first thing in file, possibly after
1286 * {{settings}} and {{description}}
1288 * {{alias}} must only appear once.
1290 * Note that these are new restrictions introduced in
1291 * v2.9.10 in order to make actionsfile editing simpler.
1292 * (Otherwise, reordering actionsfile entries without
1293 * completely rewriting the file becomes non-trivial)
1296 log_error(LOG_LEVEL_FATAL,
1297 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1298 csp->config->actions_file[fileid], linenum);
1304 /* invalid {{something}} block */
1306 log_error(LOG_LEVEL_FATAL,
1307 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1308 csp->config->actions_file[fileid], linenum, start);
1309 return 1; /* never get here */
1314 /* It's an actions block */
1316 char actions_buf[BUFFER_SIZE];
1320 mode = MODE_ACTIONS;
1322 /* free old action */
1325 if (!cur_action_used)
1327 free_action(cur_action);
1332 cur_action_used = 0;
1333 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1334 if (cur_action == NULL)
1337 log_error(LOG_LEVEL_FATAL,
1338 "can't load actions file '%s': out of memory",
1339 csp->config->actions_file[fileid]);
1340 return 1; /* never get here */
1342 init_action(cur_action);
1345 strcpy(actions_buf, buf + 1);
1347 /* check we have a trailing } and then trim it */
1348 end = actions_buf + strlen(actions_buf) - 1;
1353 log_error(LOG_LEVEL_FATAL,
1354 "can't load actions file '%s': invalid line (%lu): %s",
1355 csp->config->actions_file[fileid], linenum, buf);
1356 return 1; /* never get here */
1360 /* trim any whitespace immediately inside {} */
1363 if (get_actions(actions_buf, alias_list, cur_action))
1367 log_error(LOG_LEVEL_FATAL,
1368 "can't load actions file '%s': invalid line (%lu): %s",
1369 csp->config->actions_file[fileid], linenum, buf);
1370 return 1; /* never get here */
1374 else if (mode == MODE_SETTINGS)
1377 * Part of the {{settings}} block.
1378 * For now only serves to check if the file's minimum Privoxy
1379 * version requirement is met, but we may want to read & check
1380 * permissions when we go multi-user.
1382 if (!strncmp(buf, "for-privoxy-version=", 20))
1384 char *version_string, *fields[3];
1387 if ((version_string = strdup(buf + 20)) == NULL)
1390 log_error(LOG_LEVEL_FATAL,
1391 "can't load actions file '%s': out of memory!",
1392 csp->config->actions_file[fileid]);
1393 return 1; /* never get here */
1396 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1398 if (num_fields < 1 || atoi(fields[0]) == 0)
1400 log_error(LOG_LEVEL_ERROR,
1401 "While loading actions file '%s': invalid line (%lu): %s",
1402 csp->config->actions_file[fileid], linenum, buf);
1404 else if ( atoi(fields[0]) > VERSION_MAJOR
1405 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1406 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1409 log_error(LOG_LEVEL_FATAL,
1410 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1411 csp->config->actions_file[fileid], linenum, buf );
1412 return 1; /* never get here */
1414 free(version_string);
1417 else if (mode == MODE_DESCRIPTION)
1420 * Part of the {{description}} block.
1424 else if (mode == MODE_ALIAS)
1429 char actions_buf[BUFFER_SIZE];
1430 struct action_alias * new_alias;
1432 char * start = strchr(buf, '=');
1435 if ((start == NULL) || (start == buf))
1437 log_error(LOG_LEVEL_FATAL,
1438 "can't load actions file '%s': invalid alias line (%lu): %s",
1439 csp->config->actions_file[fileid], linenum, buf);
1440 return 1; /* never get here */
1443 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1446 log_error(LOG_LEVEL_FATAL,
1447 "can't load actions file '%s': out of memory!",
1448 csp->config->actions_file[fileid]);
1449 return 1; /* never get here */
1452 /* Eat any the whitespace before the '=' */
1454 while ((*end == ' ') || (*end == '\t'))
1457 * we already know we must have at least 1 non-ws char
1458 * at start of buf - no need to check
1464 /* Eat any the whitespace after the '=' */
1466 while ((*start == ' ') || (*start == '\t'))
1472 log_error(LOG_LEVEL_FATAL,
1473 "can't load actions file '%s': invalid alias line (%lu): %s",
1474 csp->config->actions_file[fileid], linenum, buf);
1475 return 1; /* never get here */
1478 if ((new_alias->name = strdup(buf)) == NULL)
1481 log_error(LOG_LEVEL_FATAL,
1482 "can't load actions file '%s': out of memory!",
1483 csp->config->actions_file[fileid]);
1484 return 1; /* never get here */
1487 strcpy(actions_buf, start);
1489 if (get_actions(actions_buf, alias_list, new_alias->action))
1493 log_error(LOG_LEVEL_FATAL,
1494 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1495 csp->config->actions_file[fileid], linenum, buf, start);
1496 return 1; /* never get here */
1500 new_alias->next = alias_list;
1501 alias_list = new_alias;
1503 else if (mode == MODE_ACTIONS)
1505 /* it's a URL pattern */
1507 /* allocate a new node */
1508 if ((perm = zalloc(sizeof(*perm))) == NULL)
1511 log_error(LOG_LEVEL_FATAL,
1512 "can't load actions file '%s': out of memory!",
1513 csp->config->actions_file[fileid]);
1514 return 1; /* never get here */
1518 copy_action (perm->action, cur_action);
1520 /* Save the URL pattern */
1521 if (create_url_spec(perm->url, buf))
1524 log_error(LOG_LEVEL_FATAL,
1525 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1526 csp->config->actions_file[fileid], linenum, buf);
1527 return 1; /* never get here */
1530 /* add it to the list */
1531 last_perm->next = perm;
1534 else if (mode == MODE_START_OF_FILE)
1536 /* oops - please have a {} line as 1st line in file. */
1538 log_error(LOG_LEVEL_FATAL,
1539 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1540 csp->config->actions_file[fileid], linenum, buf);
1541 return 1; /* never get here */
1545 /* How did we get here? This is impossible! */
1547 log_error(LOG_LEVEL_FATAL,
1548 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1549 csp->config->actions_file[fileid], mode);
1550 return 1; /* never get here */
1556 free_action(cur_action);
1559 free_alias_list(alias_list);
1561 /* the old one is now obsolete */
1562 if (current_actions_file[fileid])
1564 current_actions_file[fileid]->unloader = unload_actions_file;
1567 fs->next = files->next;
1569 current_actions_file[fileid] = fs;
1571 csp->actions_list[fileid] = fs;
1578 /*********************************************************************
1580 * Function : actions_to_text
1582 * Description : Converts a actionsfile entry from the internal
1583 * structure into a text line. The output is split
1584 * into one line for each action with line continuation.
1587 * 1 : action = The action to format.
1589 * Returns : A string. Caller must free it.
1590 * NULL on out-of-memory error.
1592 *********************************************************************/
1593 char * actions_to_text(struct action_spec *action)
1595 unsigned mask = action->mask;
1596 unsigned add = action->add;
1597 char * result = strdup("");
1598 struct list_entry * lst;
1600 /* sanity - prevents "-feature +feature" */
1604 #define DEFINE_ACTION_BOOL(__name, __bit) \
1605 if (!(mask & __bit)) \
1607 string_append(&result, " -" __name " \\\n"); \
1609 else if (add & __bit) \
1611 string_append(&result, " +" __name " \\\n"); \
1614 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1615 if (!(mask & __bit)) \
1617 string_append(&result, " -" __name " \\\n"); \
1619 else if (add & __bit) \
1621 string_append(&result, " +" __name "{"); \
1622 string_append(&result, action->string[__index]); \
1623 string_append(&result, "} \\\n"); \
1626 #define DEFINE_ACTION_MULTI(__name, __index) \
1627 if (action->multi_remove_all[__index]) \
1629 string_append(&result, " -" __name " \\\n"); \
1633 lst = action->multi_remove[__index]->first; \
1636 string_append(&result, " -" __name "{"); \
1637 string_append(&result, lst->str); \
1638 string_append(&result, "} \\\n"); \
1642 lst = action->multi_add[__index]->first; \
1645 string_append(&result, " +" __name "{"); \
1646 string_append(&result, lst->str); \
1647 string_append(&result, "} \\\n"); \
1651 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1653 #include "actionlist.h"
1655 #undef DEFINE_ACTION_MULTI
1656 #undef DEFINE_ACTION_STRING
1657 #undef DEFINE_ACTION_BOOL
1658 #undef DEFINE_ACTION_ALIAS
1664 /*********************************************************************
1666 * Function : actions_to_html
1668 * Description : Converts a actionsfile entry from numeric form
1669 * ("mask" and "add") to a <br>-seperated HTML string
1670 * in which each action is linked to its chapter in
1674 * 1 : csp = Client state (for config)
1675 * 2 : action = Action spec to be converted
1677 * Returns : A string. Caller must free it.
1678 * NULL on out-of-memory error.
1680 *********************************************************************/
1681 char * actions_to_html(struct client_state *csp,
1682 struct action_spec *action)
1684 unsigned mask = action->mask;
1685 unsigned add = action->add;
1686 char * result = strdup("");
1687 struct list_entry * lst;
1689 /* sanity - prevents "-feature +feature" */
1693 #define DEFINE_ACTION_BOOL(__name, __bit) \
1694 if (!(mask & __bit)) \
1696 string_append(&result, "\n<br>-"); \
1697 string_join(&result, add_help_link(__name, csp->config)); \
1699 else if (add & __bit) \
1701 string_append(&result, "\n<br>+"); \
1702 string_join(&result, add_help_link(__name, csp->config)); \
1705 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1706 if (!(mask & __bit)) \
1708 string_append(&result, "\n<br>-"); \
1709 string_join(&result, add_help_link(__name, csp->config)); \
1711 else if (add & __bit) \
1713 string_append(&result, "\n<br>+"); \
1714 string_join(&result, add_help_link(__name, csp->config)); \
1715 string_append(&result, "{"); \
1716 string_join(&result, html_encode(action->string[__index])); \
1717 string_append(&result, "}"); \
1720 #define DEFINE_ACTION_MULTI(__name, __index) \
1721 if (action->multi_remove_all[__index]) \
1723 string_append(&result, "\n<br>-"); \
1724 string_join(&result, add_help_link(__name, csp->config)); \
1728 lst = action->multi_remove[__index]->first; \
1731 string_append(&result, "\n<br>-"); \
1732 string_join(&result, add_help_link(__name, csp->config)); \
1733 string_append(&result, "{"); \
1734 string_join(&result, html_encode(lst->str)); \
1735 string_append(&result, "}"); \
1739 lst = action->multi_add[__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 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1752 #include "actionlist.h"
1754 #undef DEFINE_ACTION_MULTI
1755 #undef DEFINE_ACTION_STRING
1756 #undef DEFINE_ACTION_BOOL
1757 #undef DEFINE_ACTION_ALIAS
1759 /* trim leading <br> */
1760 if (result && *result)
1763 result = strdup(result + 5);
1771 /*********************************************************************
1773 * Function : current_actions_to_html
1775 * Description : Converts a curren action spec to a <br> seperated HTML
1776 * text in which each action is linked to its chapter in
1780 * 1 : csp = Client state (for config)
1781 * 2 : action = Current action spec to be converted
1783 * Returns : A string. Caller must free it.
1784 * NULL on out-of-memory error.
1786 *********************************************************************/
1787 char *current_action_to_html(struct client_state *csp,
1788 struct current_action_spec *action)
1790 unsigned long flags = action->flags;
1791 char * result = strdup("");
1792 struct list_entry * lst;
1794 #define DEFINE_ACTION_BOOL(__name, __bit) \
1795 if (flags & __bit) \
1797 string_append(&result, "\n<br>+"); \
1798 string_join(&result, add_help_link(__name, csp->config)); \
1802 string_append(&result, "\n<br>-"); \
1803 string_join(&result, add_help_link(__name, csp->config)); \
1806 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1807 if (flags & __bit) \
1809 string_append(&result, "\n<br>+"); \
1810 string_join(&result, add_help_link(__name, csp->config)); \
1811 string_append(&result, "{"); \
1812 string_join(&result, html_encode(action->string[__index])); \
1813 string_append(&result, "}"); \
1817 string_append(&result, "\n<br>-"); \
1818 string_join(&result, add_help_link(__name, csp->config)); \
1821 #define DEFINE_ACTION_MULTI(__name, __index) \
1822 lst = action->multi[__index]->first; \
1825 string_append(&result, "\n<br>-"); \
1826 string_join(&result, add_help_link(__name, csp->config)); \
1832 string_append(&result, "\n<br>+"); \
1833 string_join(&result, add_help_link(__name, csp->config)); \
1834 string_append(&result, "{"); \
1835 string_join(&result, html_encode(lst->str)); \
1836 string_append(&result, "}"); \
1841 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1843 #include "actionlist.h"
1845 #undef DEFINE_ACTION_MULTI
1846 #undef DEFINE_ACTION_STRING
1847 #undef DEFINE_ACTION_BOOL
1848 #undef DEFINE_ACTION_ALIAS