1 const char actions_rcs[] = "$Id: actions.c,v 1.35 2006/07/18 14:48:45 david__schmidt 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 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.35 2006/07/18 14:48:45 david__schmidt
37 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
38 * with what was really the latest development (the v_3_0_branch branch)
40 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
41 * Multiple filter file support
43 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
44 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
45 * this becomes important when jcc.h gets included later down the line.
47 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
48 * - Implemented Privoxy version requirement through
49 * for-privoxy-version= statement in {{settings}}
51 * - Fix for unchecked out-of-memory condition
53 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
54 * Fixed memory leak reported by Dan Price in Bug #694713
56 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
57 * Extended unload_current_actions_file() to multiple AFs.
58 * Thanks to Oliver Stoeneberg for the hint.
60 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
61 * Change unsigned to unsigned long in actions_name struct. This closes
62 * SourceForge Bug #539284.
64 * Revision 1.32 2002/05/12 21:36:29 jongfoster
65 * Correcting function comments
67 * Revision 1.31 2002/05/06 07:56:50 oes
68 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
70 * Revision 1.30 2002/04/30 11:14:52 oes
71 * Made csp the first parameter in *action_to_html
73 * Revision 1.29 2002/04/26 19:30:54 jongfoster
74 * - current_action_to_html(): Adding help link for the "-" form of
76 * - Some actions had "<br>-", some "<br> -" (note the space).
77 * Standardizing on no space.
78 * - Greatly simplifying some of the code by using string_join()
81 * Revision 1.28 2002/04/26 12:53:15 oes
82 * - CGI AF editor now writes action lines split into
83 * single lines with line continuation
84 * - actions_to_html now embeds each action name in
86 * - current_action_to_text is now called current_action_to_html
87 * and acts like actions_to_html
89 * Revision 1.27 2002/04/24 02:10:31 oes
90 * - Jon's patch for multiple AFs:
91 * - split load_actions_file, add load_one_actions_file
92 * - make csp->actions_list files an array
93 * - remember file id with each action
94 * - Copy_action now frees dest action before copying
96 * Revision 1.26 2002/03/26 22:29:54 swa
97 * we have a new homepage!
99 * Revision 1.25 2002/03/24 13:25:43 swa
100 * name change related issues
102 * Revision 1.24 2002/03/16 23:54:06 jongfoster
103 * Adding graceful termination feature, to help look for memory leaks.
104 * If you enable this (which, by design, has to be done by hand
105 * editing config.h) and then go to http://i.j.b/die, then the program
106 * will exit cleanly after the *next* request. It should free all the
107 * memory that was used.
109 * Revision 1.23 2002/03/07 03:46:16 oes
110 * Fixed compiler warnings
112 * Revision 1.22 2002/01/21 00:27:02 jongfoster
113 * Allowing free_action(NULL).
114 * Moving the functions that #include actionlist.h to the end of the file,
115 * because the Visual C++ 97 debugger gets extremely confused if you try
116 * to debug any code that comes after them in the file.
118 * Revision 1.21 2002/01/17 20:54:44 jongfoster
119 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
121 * Revision 1.20 2001/11/22 21:56:49 jongfoster
122 * Making action_spec->flags into an unsigned long rather than just an
124 * Fixing a bug in the display of -add-header and -wafer
126 * Revision 1.19 2001/11/13 00:14:07 jongfoster
127 * Fixing stupid bug now I've figured out what || means.
128 * (It always returns 0 or 1, not one of it's paramaters.)
130 * Revision 1.18 2001/11/07 00:06:06 steudten
131 * Add line number in error output for lineparsing for
134 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
135 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
136 * threads to call select() simultaneously. So, it's time to do a real, live,
137 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
138 * (native). Both versions will work, but using __OS2__ offers multi-threading.
140 * Revision 1.16 2001/10/23 21:30:30 jongfoster
141 * Adding error-checking to selected functions.
143 * Revision 1.15 2001/10/14 21:58:22 jongfoster
144 * Adding support for the CGI-based editor:
145 * - Exported get_actions()
146 * - Added new function free_alias_list()
147 * - Added support for {{settings}} and {{description}} blocks
148 * in the actions file. They are currently ignored.
149 * - Added restriction to only one {{alias}} block which must appear
150 * first in the file, to simplify the editor's rewriting rules.
151 * - Note that load_actions_file() is no longer used by the CGI-based
152 * editor, but some of the other routines in this file are.
154 * Revision 1.14 2001/09/22 16:36:59 jongfoster
155 * Removing unused parameter fs from read_config_line()
157 * Revision 1.13 2001/09/16 15:47:37 jongfoster
158 * First version of CGI-based edit interface. This is very much a
159 * work-in-progress, and you can't actually use it to edit anything
160 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
161 * to have any effect.
163 * Revision 1.12 2001/09/16 13:21:27 jongfoster
164 * Changes to use new list functions.
166 * Revision 1.11 2001/09/14 00:17:32 jongfoster
167 * Tidying up memory allocation. New function init_action().
169 * Revision 1.10 2001/09/10 10:14:34 oes
170 * Removing unused variable
172 * Revision 1.9 2001/07/30 22:08:36 jongfoster
173 * Tidying up #defines:
174 * - All feature #defines are now of the form FEATURE_xxx
175 * - Permanently turned off WIN_GUI_EDIT
176 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
178 * Revision 1.8 2001/06/29 13:19:52 oes
179 * Removed logentry from cancelled commit
181 * Revision 1.7 2001/06/09 10:55:28 jongfoster
182 * Changing BUFSIZ ==> BUFFER_SIZE
184 * Revision 1.6 2001/06/07 23:04:34 jongfoster
185 * Made get_actions() static.
187 * Revision 1.5 2001/06/03 19:11:48 oes
188 * adapted to new enlist_unique arg format
190 * Revision 1.4 2001/06/01 20:03:42 jongfoster
191 * Better memory management - current_action->strings[] now
192 * contains copies of the strings, not the original.
194 * Revision 1.3 2001/06/01 18:49:17 jongfoster
195 * Replaced "list_share" with "list" - the tiny memory gain was not
196 * worth the extra complexity.
198 * Revision 1.2 2001/05/31 21:40:00 jongfoster
199 * Removing some commented out, obsolete blocks of code.
201 * Revision 1.1 2001/05/31 21:16:46 jongfoster
202 * Moved functions to process the action list into this new file.
205 *********************************************************************/
215 #ifdef FEATURE_PTHREAD
223 #include "miscutil.h"
227 #include "urlmatch.h"
231 const char actions_h_rcs[] = ACTIONS_H_VERSION;
235 * We need the main list of options.
237 * First, we need a way to tell between boolean, string, and multi-string
238 * options. For string and multistring options, we also need to be
239 * able to tell the difference between a "+" and a "-". (For bools,
240 * the "+"/"-" information is encoded in "add" and "mask"). So we use
241 * an enumerated type (well, the preprocessor equivalent). Here are
244 #define AV_NONE 0 /* +opt -opt */
245 #define AV_ADD_STRING 1 /* +stropt{string} */
246 #define AV_REM_STRING 2 /* -stropt */
247 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
248 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
251 * We need a structure to hold the name, flag changes,
252 * type, and string index.
257 unsigned long mask; /* a bit set to "0" = remove action */
258 unsigned long add; /* a bit set to "1" = add action */
259 int takes_value; /* an AV_... constant */
260 int index; /* index into strings[] or multi[] */
264 * And with those building blocks in place, here's the array.
266 static const struct action_name action_names[] =
269 * Well actually there's no data here - it's in actionlist.h
270 * This keeps it together to make it easy to change.
272 * Here's the macros used to format it:
274 #define DEFINE_ACTION_MULTI(name,index) \
275 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
276 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
277 #define DEFINE_ACTION_STRING(name,flag,index) \
278 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
279 { "-" name, ~flag, 0, AV_REM_STRING, index },
280 #define DEFINE_ACTION_BOOL(name,flag) \
281 { "+" name, ACTION_MASK_ALL, flag }, \
282 { "-" name, ~flag, 0 },
283 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
285 #include "actionlist.h"
287 #undef DEFINE_ACTION_MULTI
288 #undef DEFINE_ACTION_STRING
289 #undef DEFINE_ACTION_BOOL
290 #undef DEFINE_ACTION_ALIAS
292 { NULL, 0, 0 } /* End marker */
296 static int load_one_actions_file(struct client_state *csp, int fileid);
299 /*********************************************************************
301 * Function : merge_actions
303 * Description : Merge two actions together.
304 * Similar to "dest += src".
307 * 1 : dest = Actions to modify.
308 * 2 : src = Action to add.
310 * Returns : JB_ERR_OK or JB_ERR_MEMORY
312 *********************************************************************/
313 jb_err merge_actions (struct action_spec *dest,
314 const struct action_spec *src)
319 dest->mask &= src->mask;
320 dest->add &= src->mask;
321 dest->add |= src->add;
323 for (i = 0; i < ACTION_STRING_COUNT; i++)
325 char * str = src->string[i];
328 freez(dest->string[i]);
329 dest->string[i] = strdup(str);
330 if (NULL == dest->string[i])
332 return JB_ERR_MEMORY;
337 for (i = 0; i < ACTION_MULTI_COUNT; i++)
339 if (src->multi_remove_all[i])
341 /* Remove everything from dest */
342 list_remove_all(dest->multi_remove[i]);
343 dest->multi_remove_all[i] = 1;
345 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
347 else if (dest->multi_remove_all[i])
350 * dest already removes everything, so we only need to worry
353 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
354 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
358 /* No "remove all"s to worry about. */
359 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
360 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
361 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
374 /*********************************************************************
376 * Function : copy_action
378 * Description : Copy an action_specs.
379 * Similar to "dest = src".
382 * 1 : dest = Destination of copy.
383 * 2 : src = Source for copy.
387 *********************************************************************/
388 jb_err copy_action (struct action_spec *dest,
389 const struct action_spec *src)
392 jb_err err = JB_ERR_OK;
395 memset(dest, '\0', sizeof(*dest));
397 dest->mask = src->mask;
398 dest->add = src->add;
400 for (i = 0; i < ACTION_STRING_COUNT; i++)
402 char * str = src->string[i];
408 return JB_ERR_MEMORY;
410 dest->string[i] = str;
414 for (i = 0; i < ACTION_MULTI_COUNT; i++)
416 dest->multi_remove_all[i] = src->multi_remove_all[i];
417 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
422 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
432 /*********************************************************************
434 * Function : free_action
436 * Description : Destroy an action_spec. Frees memory used by it,
437 * except for the memory used by the struct action_spec
441 * 1 : src = Source to free.
445 *********************************************************************/
446 void free_action (struct action_spec *src)
455 for (i = 0; i < ACTION_STRING_COUNT; i++)
457 freez(src->string[i]);
460 for (i = 0; i < ACTION_MULTI_COUNT; i++)
462 destroy_list(src->multi_remove[i]);
463 destroy_list(src->multi_add[i]);
466 memset(src, '\0', sizeof(*src));
470 /*********************************************************************
472 * Function : get_action_token
474 * Description : Parses a line for the first action.
475 * Modifies it's input array, doesn't allocate memory.
477 * *line=" +abc{def} -ghi "
484 * 1 : line = [in] The line containing the action.
485 * [out] Start of next action on line, or
486 * NULL if we reached the end of line before
487 * we found an action.
488 * 2 : name = [out] Start of action name, null
489 * terminated. NULL on EOL
490 * 3 : value = [out] Start of action value, null
491 * terminated. NULL if none or EOL.
493 * Returns : JB_ERR_OK => Ok
494 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
496 *********************************************************************/
497 jb_err get_action_token(char **line, char **name, char **value)
502 /* set default returns */
507 /* Eat any leading whitespace */
508 while ((*str == ' ') || (*str == '\t'))
520 /* null name, just value is prohibited */
527 while (((ch = *str) != '\0') &&
528 (ch != ' ') && (ch != '\t') && (ch != '{'))
532 /* error, '}' without '{' */
544 /* EOL - be careful not to run off buffer */
549 /* More to parse next time. */
558 str = strchr(str, '}');
576 /*********************************************************************
578 * Function : get_actions
580 * Description : Parses a list of actions.
583 * 1 : line = The string containing the actions.
584 * Will be written to by this function.
585 * 2 : alias_list = Custom alias list, or NULL for none.
586 * 3 : cur_action = Where to store the action. Caller
589 * Returns : JB_ERR_OK => Ok
590 * JB_ERR_PARSE => Parse error (line was trashed anyway)
591 * nonzero => Out of memory (line was trashed anyway)
593 *********************************************************************/
594 jb_err get_actions(char *line,
595 struct action_alias * alias_list,
596 struct action_spec *cur_action)
599 init_action(cur_action);
600 cur_action->mask = ACTION_MASK_ALL;
604 char * option = NULL;
607 err = get_action_token(&line, &option, &value);
615 /* handle option in 'option' */
617 /* Check for standard action name */
618 const struct action_name * action = action_names;
620 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
624 if (action->name != NULL)
627 cur_action->mask &= action->mask;
628 cur_action->add &= action->mask;
629 cur_action->add |= action->add;
631 switch (action->takes_value)
634 /* ignore any option. */
638 /* add single string. */
640 if ((value == NULL) || (*value == '\0'))
644 /* FIXME: should validate option string here */
645 freez (cur_action->string[action->index]);
646 cur_action->string[action->index] = strdup(value);
647 if (NULL == cur_action->string[action->index])
649 return JB_ERR_MEMORY;
655 /* remove single string. */
657 freez (cur_action->string[action->index]);
662 /* append multi string. */
664 struct list * remove_p = cur_action->multi_remove[action->index];
665 struct list * add_p = cur_action->multi_add[action->index];
667 if ((value == NULL) || (*value == '\0'))
672 list_remove_item(remove_p, value);
673 err = enlist_unique(add_p, value, 0);
682 /* remove multi string. */
684 struct list * remove_p = cur_action->multi_remove[action->index];
685 struct list * add_p = cur_action->multi_add[action->index];
687 if ( (value == NULL) || (*value == '\0')
688 || ((*value == '*') && (value[1] == '\0')) )
691 * no option, or option == "*".
695 list_remove_all(remove_p);
696 list_remove_all(add_p);
697 cur_action->multi_remove_all[action->index] = 1;
701 /* Valid option - remove only 1 option */
703 if ( !cur_action->multi_remove_all[action->index] )
705 /* there isn't a catch-all in the remove list already */
706 err = enlist_unique(remove_p, value, 0);
712 list_remove_item(add_p, value);
717 /* Shouldn't get here unless there's memory corruption. */
724 /* try user aliases. */
725 const struct action_alias * alias = alias_list;
727 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
734 merge_actions(cur_action, alias->action);
738 /* Bad action name */
749 /*********************************************************************
751 * Function : init_current_action
753 * Description : Zero out an action.
756 * 1 : dest = An uninitialized current_action_spec.
760 *********************************************************************/
761 void init_current_action (struct current_action_spec *dest)
763 memset(dest, '\0', sizeof(*dest));
765 dest->flags = ACTION_MOST_COMPATIBLE;
769 /*********************************************************************
771 * Function : init_action
773 * Description : Zero out an action.
776 * 1 : dest = An uninitialized action_spec.
780 *********************************************************************/
781 void init_action (struct action_spec *dest)
783 memset(dest, '\0', sizeof(*dest));
787 /*********************************************************************
789 * Function : merge_current_action
791 * Description : Merge two actions together.
792 * Similar to "dest += src".
793 * Differences between this and merge_actions()
794 * is that this one doesn't allocate memory for
795 * strings (so "src" better be in memory for at least
796 * as long as "dest" is, and you'd better free
797 * "dest" using "free_current_action").
798 * Also, there is no mask or remove lists in dest.
799 * (If we're applying it to a URL, we don't need them)
802 * 1 : dest = Current actions, to modify.
803 * 2 : src = Action to add.
805 * Returns 0 : no error
806 * !=0 : error, probably JB_ERR_MEMORY.
808 *********************************************************************/
809 jb_err merge_current_action (struct current_action_spec *dest,
810 const struct action_spec *src)
813 jb_err err = JB_ERR_OK;
815 dest->flags &= src->mask;
816 dest->flags |= src->add;
818 for (i = 0; i < ACTION_STRING_COUNT; i++)
820 char * str = src->string[i];
826 return JB_ERR_MEMORY;
828 freez(dest->string[i]);
829 dest->string[i] = str;
833 for (i = 0; i < ACTION_MULTI_COUNT; i++)
835 if (src->multi_remove_all[i])
837 /* Remove everything from dest, then add src->multi_add */
838 err = list_duplicate(dest->multi[i], src->multi_add[i]);
846 list_remove_list(dest->multi[i], src->multi_remove[i]);
847 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
858 /*********************************************************************
860 * Function : free_current_action
862 * Description : Free memory used by a current_action_spec.
863 * Does not free the current_action_spec itself.
866 * 1 : src = Source to free.
870 *********************************************************************/
871 void free_current_action (struct current_action_spec *src)
875 for (i = 0; i < ACTION_STRING_COUNT; i++)
877 freez(src->string[i]);
880 for (i = 0; i < ACTION_MULTI_COUNT; i++)
882 destroy_list(src->multi[i]);
885 memset(src, '\0', sizeof(*src));
889 static struct file_list *current_actions_file[MAX_AF_FILES] = {
890 NULL, NULL, NULL, NULL, NULL,
891 NULL, NULL, NULL, NULL, NULL
895 #ifdef FEATURE_GRACEFUL_TERMINATION
896 /*********************************************************************
898 * Function : unload_current_actions_file
900 * Description : Unloads current actions file - reset to state at
901 * beginning of program.
907 *********************************************************************/
908 void unload_current_actions_file(void)
912 for (i = 0; i < MAX_AF_FILES; i++)
914 if (current_actions_file[i])
916 current_actions_file[i]->unloader = unload_actions_file;
917 current_actions_file[i] = NULL;
921 #endif /* FEATURE_GRACEFUL_TERMINATION */
924 /*********************************************************************
926 * Function : unload_actions_file
928 * Description : Unloads an actions module.
931 * 1 : file_data = the data structure associated with the
936 *********************************************************************/
937 void unload_actions_file(void *file_data)
939 struct url_actions * next;
940 struct url_actions * cur = (struct url_actions *)file_data;
944 free_url_spec(cur->url);
945 free_action(cur->action);
953 /*********************************************************************
955 * Function : free_alias_list
957 * Description : Free memory used by a list of aliases.
960 * 1 : alias_list = Linked list to free.
964 *********************************************************************/
965 void free_alias_list(struct action_alias *alias_list)
967 while (alias_list != NULL)
969 struct action_alias * next = alias_list->next;
970 alias_list->next = NULL;
971 freez(alias_list->name);
972 free_action(alias_list->action);
979 /*********************************************************************
981 * Function : load_actions_file
983 * Description : Read and parse all the action files and add to files
987 * 1 : csp = Current client state (buffers, headers, etc...)
989 * Returns : 0 => Ok, everything else is an error.
991 *********************************************************************/
992 int load_actions_file(struct client_state *csp)
997 for (i = 0; i < MAX_AF_FILES; i++)
999 if (csp->config->actions_file[i])
1001 result = load_one_actions_file(csp, i);
1007 else if (current_actions_file[i])
1009 current_actions_file[i]->unloader = unload_actions_file;
1010 current_actions_file[i] = NULL;
1017 /*********************************************************************
1019 * Function : load_one_actions_file
1021 * Description : Read and parse a action file and add to files
1025 * 1 : csp = Current client state (buffers, headers, etc...)
1026 * 2 : fileid = File index to load.
1028 * Returns : 0 => Ok, everything else is an error.
1030 *********************************************************************/
1031 static int load_one_actions_file(struct client_state *csp, int fileid)
1036 * Note: Keep these in the order they occur in the file, they are
1037 * sometimes tested with <=
1039 #define MODE_START_OF_FILE 1
1040 #define MODE_SETTINGS 2
1041 #define MODE_DESCRIPTION 3
1042 #define MODE_ALIAS 4
1043 #define MODE_ACTIONS 5
1045 int mode = MODE_START_OF_FILE;
1048 struct url_actions *last_perm;
1049 struct url_actions *perm;
1050 char buf[BUFFER_SIZE];
1051 struct file_list *fs;
1052 struct action_spec * cur_action = NULL;
1053 int cur_action_used = 0;
1054 struct action_alias * alias_list = NULL;
1055 unsigned long linenum = 0;
1057 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1059 /* No need to load */
1060 csp->actions_list[fileid] = current_actions_file[fileid];
1065 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1066 csp->config->actions_file[fileid]);
1067 return 1; /* never get here */
1070 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1071 if (last_perm == NULL)
1073 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1074 csp->config->actions_file[fileid]);
1075 return 1; /* never get here */
1078 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1080 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1081 csp->config->actions_file[fileid]);
1082 return 1; /* never get here */
1085 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1089 /* It's a header block */
1092 /* It's {{settings}} or {{alias}} */
1093 size_t len = strlen(buf);
1094 char * start = buf + 2;
1095 char * end = buf + len - 1;
1096 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1100 log_error(LOG_LEVEL_FATAL,
1101 "can't load actions file '%s': invalid line (%lu): %s",
1102 csp->config->actions_file[fileid], linenum, buf);
1103 return 1; /* never get here */
1106 /* Trim leading and trailing whitespace. */
1114 log_error(LOG_LEVEL_FATAL,
1115 "can't load actions file '%s': invalid line (%lu): {{ }}",
1116 csp->config->actions_file[fileid], linenum);
1117 return 1; /* never get here */
1121 * An actionsfile can optionally contain the following blocks.
1122 * They *MUST* be in this order, to simplify processing:
1128 * ...free text, format TBD, but no line may start with a '{'...
1133 * The actual actions must be *after* these special blocks.
1134 * None of these special blocks may be repeated.
1137 if (0 == strcmpic(start, "settings"))
1139 /* it's a {{settings}} block */
1140 if (mode >= MODE_SETTINGS)
1142 /* {{settings}} must be first thing in file and must only
1146 log_error(LOG_LEVEL_FATAL,
1147 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1148 csp->config->actions_file[fileid], linenum);
1150 mode = MODE_SETTINGS;
1152 else if (0 == strcmpic(start, "description"))
1154 /* it's a {{description}} block */
1155 if (mode >= MODE_DESCRIPTION)
1157 /* {{description}} is a singleton and only {{settings}} may proceed it
1160 log_error(LOG_LEVEL_FATAL,
1161 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1162 csp->config->actions_file[fileid], linenum);
1164 mode = MODE_DESCRIPTION;
1166 else if (0 == strcmpic(start, "alias"))
1168 /* it's an {{alias}} block */
1169 if (mode >= MODE_ALIAS)
1171 /* {{alias}} must be first thing in file, possibly after
1172 * {{settings}} and {{description}}
1174 * {{alias}} must only appear once.
1176 * Note that these are new restrictions introduced in
1177 * v2.9.10 in order to make actionsfile editing simpler.
1178 * (Otherwise, reordering actionsfile entries without
1179 * completely rewriting the file becomes non-trivial)
1182 log_error(LOG_LEVEL_FATAL,
1183 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1184 csp->config->actions_file[fileid], linenum);
1190 /* invalid {{something}} block */
1192 log_error(LOG_LEVEL_FATAL,
1193 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1194 csp->config->actions_file[fileid], linenum, start);
1195 return 1; /* never get here */
1200 /* It's an actions block */
1202 char actions_buf[BUFFER_SIZE];
1206 mode = MODE_ACTIONS;
1208 /* free old action */
1211 if (!cur_action_used)
1213 free_action(cur_action);
1218 cur_action_used = 0;
1219 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1220 if (cur_action == NULL)
1223 log_error(LOG_LEVEL_FATAL,
1224 "can't load actions file '%s': out of memory",
1225 csp->config->actions_file[fileid]);
1226 return 1; /* never get here */
1228 init_action(cur_action);
1231 strcpy(actions_buf, buf + 1);
1233 /* check we have a trailing } and then trim it */
1234 end = actions_buf + strlen(actions_buf) - 1;
1239 log_error(LOG_LEVEL_FATAL,
1240 "can't load actions file '%s': invalid line (%lu): %s",
1241 csp->config->actions_file[fileid], linenum, buf);
1242 return 1; /* never get here */
1246 /* trim any whitespace immediately inside {} */
1249 if (get_actions(actions_buf, alias_list, cur_action))
1253 log_error(LOG_LEVEL_FATAL,
1254 "can't load actions file '%s': invalid line (%lu): %s",
1255 csp->config->actions_file[fileid], linenum, buf);
1256 return 1; /* never get here */
1260 else if (mode == MODE_SETTINGS)
1263 * Part of the {{settings}} block.
1264 * For now only serves to check if the file's minimum Privoxy
1265 * version requirement is met, but we may want to read & check
1266 * permissions when we go multi-user.
1268 if (!strncmp(buf, "for-privoxy-version=", 20))
1270 char *version_string, *fields[3];
1273 if ((version_string = strdup(buf + 20)) == NULL)
1276 log_error(LOG_LEVEL_FATAL,
1277 "can't load actions file '%s': out of memory!",
1278 csp->config->actions_file[fileid]);
1279 return 1; /* never get here */
1282 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1284 if (num_fields < 1 || atoi(fields[0]) == 0)
1286 log_error(LOG_LEVEL_ERROR,
1287 "While loading actions file '%s': invalid line (%lu): %s",
1288 csp->config->actions_file[fileid], linenum, buf);
1290 else if ( atoi(fields[0]) > VERSION_MAJOR
1291 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1292 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1295 log_error(LOG_LEVEL_FATAL,
1296 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1297 csp->config->actions_file[fileid], linenum, buf );
1298 return 1; /* never get here */
1300 free(version_string);
1303 else if (mode == MODE_DESCRIPTION)
1306 * Part of the {{description}} block.
1310 else if (mode == MODE_ALIAS)
1315 char actions_buf[BUFFER_SIZE];
1316 struct action_alias * new_alias;
1318 char * start = strchr(buf, '=');
1321 if ((start == NULL) || (start == buf))
1323 log_error(LOG_LEVEL_FATAL,
1324 "can't load actions file '%s': invalid alias line (%lu): %s",
1325 csp->config->actions_file[fileid], linenum, buf);
1326 return 1; /* never get here */
1329 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1332 log_error(LOG_LEVEL_FATAL,
1333 "can't load actions file '%s': out of memory!",
1334 csp->config->actions_file[fileid]);
1335 return 1; /* never get here */
1338 /* Eat any the whitespace before the '=' */
1340 while ((*end == ' ') || (*end == '\t'))
1343 * we already know we must have at least 1 non-ws char
1344 * at start of buf - no need to check
1350 /* Eat any the whitespace after the '=' */
1352 while ((*start == ' ') || (*start == '\t'))
1358 log_error(LOG_LEVEL_FATAL,
1359 "can't load actions file '%s': invalid alias line (%lu): %s",
1360 csp->config->actions_file[fileid], linenum, buf);
1361 return 1; /* never get here */
1364 if ((new_alias->name = strdup(buf)) == NULL)
1367 log_error(LOG_LEVEL_FATAL,
1368 "can't load actions file '%s': out of memory!",
1369 csp->config->actions_file[fileid]);
1370 return 1; /* never get here */
1373 strcpy(actions_buf, start);
1375 if (get_actions(actions_buf, alias_list, new_alias->action))
1379 log_error(LOG_LEVEL_FATAL,
1380 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1381 csp->config->actions_file[fileid], linenum, buf, start);
1382 return 1; /* never get here */
1386 new_alias->next = alias_list;
1387 alias_list = new_alias;
1389 else if (mode == MODE_ACTIONS)
1391 /* it's a URL pattern */
1393 /* allocate a new node */
1394 if ((perm = zalloc(sizeof(*perm))) == NULL)
1397 log_error(LOG_LEVEL_FATAL,
1398 "can't load actions file '%s': out of memory!",
1399 csp->config->actions_file[fileid]);
1400 return 1; /* never get here */
1404 copy_action (perm->action, cur_action);
1406 /* Save the URL pattern */
1407 if (create_url_spec(perm->url, buf))
1410 log_error(LOG_LEVEL_FATAL,
1411 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1412 csp->config->actions_file[fileid], linenum, buf);
1413 return 1; /* never get here */
1416 /* add it to the list */
1417 last_perm->next = perm;
1420 else if (mode == MODE_START_OF_FILE)
1422 /* oops - please have a {} line as 1st line in file. */
1424 log_error(LOG_LEVEL_FATAL,
1425 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1426 csp->config->actions_file[fileid], linenum, buf);
1427 return 1; /* never get here */
1431 /* How did we get here? This is impossible! */
1433 log_error(LOG_LEVEL_FATAL,
1434 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1435 csp->config->actions_file[fileid], mode);
1436 return 1; /* never get here */
1442 free_action(cur_action);
1445 free_alias_list(alias_list);
1447 /* the old one is now obsolete */
1448 if (current_actions_file[fileid])
1450 current_actions_file[fileid]->unloader = unload_actions_file;
1453 fs->next = files->next;
1455 current_actions_file[fileid] = fs;
1457 csp->actions_list[fileid] = fs;
1464 /*********************************************************************
1466 * Function : actions_to_text
1468 * Description : Converts a actionsfile entry from the internal
1469 * structurt into a text line. The output is split
1470 * into one line for each action with line continuation.
1473 * 1 : action = The action to format.
1475 * Returns : A string. Caller must free it.
1476 * NULL on out-of-memory error.
1478 *********************************************************************/
1479 char * actions_to_text(struct action_spec *action)
1481 unsigned mask = action->mask;
1482 unsigned add = action->add;
1483 char * result = strdup("");
1484 struct list_entry * lst;
1486 /* sanity - prevents "-feature +feature" */
1490 #define DEFINE_ACTION_BOOL(__name, __bit) \
1491 if (!(mask & __bit)) \
1493 string_append(&result, " -" __name " \\\n"); \
1495 else if (add & __bit) \
1497 string_append(&result, " +" __name " \\\n"); \
1500 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1501 if (!(mask & __bit)) \
1503 string_append(&result, " -" __name " \\\n"); \
1505 else if (add & __bit) \
1507 string_append(&result, " +" __name "{"); \
1508 string_append(&result, action->string[__index]); \
1509 string_append(&result, "} \\\n"); \
1512 #define DEFINE_ACTION_MULTI(__name, __index) \
1513 if (action->multi_remove_all[__index]) \
1515 string_append(&result, " -" __name " \\\n"); \
1519 lst = action->multi_remove[__index]->first; \
1522 string_append(&result, " -" __name "{"); \
1523 string_append(&result, lst->str); \
1524 string_append(&result, "} \\\n"); \
1528 lst = action->multi_add[__index]->first; \
1531 string_append(&result, " +" __name "{"); \
1532 string_append(&result, lst->str); \
1533 string_append(&result, "} \\\n"); \
1537 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1539 #include "actionlist.h"
1541 #undef DEFINE_ACTION_MULTI
1542 #undef DEFINE_ACTION_STRING
1543 #undef DEFINE_ACTION_BOOL
1544 #undef DEFINE_ACTION_ALIAS
1550 /*********************************************************************
1552 * Function : actions_to_html
1554 * Description : Converts a actionsfile entry from numeric form
1555 * ("mask" and "add") to a <br>-seperated HTML string
1556 * in which each action is linked to its chapter in
1560 * 1 : csp = Client state (for config)
1561 * 2 : action = Action spec to be converted
1563 * Returns : A string. Caller must free it.
1564 * NULL on out-of-memory error.
1566 *********************************************************************/
1567 char * actions_to_html(struct client_state *csp,
1568 struct action_spec *action)
1570 unsigned mask = action->mask;
1571 unsigned add = action->add;
1572 char * result = strdup("");
1573 struct list_entry * lst;
1575 /* sanity - prevents "-feature +feature" */
1579 #define DEFINE_ACTION_BOOL(__name, __bit) \
1580 if (!(mask & __bit)) \
1582 string_append(&result, "\n<br>-"); \
1583 string_join(&result, add_help_link(__name, csp->config)); \
1585 else if (add & __bit) \
1587 string_append(&result, "\n<br>+"); \
1588 string_join(&result, add_help_link(__name, csp->config)); \
1591 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1592 if (!(mask & __bit)) \
1594 string_append(&result, "\n<br>-"); \
1595 string_join(&result, add_help_link(__name, csp->config)); \
1597 else if (add & __bit) \
1599 string_append(&result, "\n<br>+"); \
1600 string_join(&result, add_help_link(__name, csp->config)); \
1601 string_append(&result, "{"); \
1602 string_join(&result, html_encode(action->string[__index])); \
1603 string_append(&result, "}"); \
1606 #define DEFINE_ACTION_MULTI(__name, __index) \
1607 if (action->multi_remove_all[__index]) \
1609 string_append(&result, "\n<br>-"); \
1610 string_join(&result, add_help_link(__name, csp->config)); \
1614 lst = action->multi_remove[__index]->first; \
1617 string_append(&result, "\n<br>-"); \
1618 string_join(&result, add_help_link(__name, csp->config)); \
1619 string_append(&result, "{"); \
1620 string_join(&result, html_encode(lst->str)); \
1621 string_append(&result, "}"); \
1625 lst = action->multi_add[__index]->first; \
1628 string_append(&result, "\n<br>+"); \
1629 string_join(&result, add_help_link(__name, csp->config)); \
1630 string_append(&result, "{"); \
1631 string_join(&result, html_encode(lst->str)); \
1632 string_append(&result, "}"); \
1636 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1638 #include "actionlist.h"
1640 #undef DEFINE_ACTION_MULTI
1641 #undef DEFINE_ACTION_STRING
1642 #undef DEFINE_ACTION_BOOL
1643 #undef DEFINE_ACTION_ALIAS
1645 /* trim leading <br> */
1646 if (result && *result)
1649 result = strdup(result + 5);
1657 /*********************************************************************
1659 * Function : current_actions_to_html
1661 * Description : Converts a curren action spec to a <br> seperated HTML
1662 * text in which each action is linked to its chapter in
1666 * 1 : csp = Client state (for config)
1667 * 2 : action = Current action spec to be converted
1669 * Returns : A string. Caller must free it.
1670 * NULL on out-of-memory error.
1672 *********************************************************************/
1673 char *current_action_to_html(struct client_state *csp,
1674 struct current_action_spec *action)
1676 unsigned long flags = action->flags;
1677 char * result = strdup("");
1678 struct list_entry * lst;
1680 #define DEFINE_ACTION_BOOL(__name, __bit) \
1681 if (flags & __bit) \
1683 string_append(&result, "\n<br>+"); \
1684 string_join(&result, add_help_link(__name, csp->config)); \
1688 string_append(&result, "\n<br>-"); \
1689 string_join(&result, add_help_link(__name, csp->config)); \
1692 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1693 if (flags & __bit) \
1695 string_append(&result, "\n<br>+"); \
1696 string_join(&result, add_help_link(__name, csp->config)); \
1697 string_append(&result, "{"); \
1698 string_join(&result, html_encode(action->string[__index])); \
1699 string_append(&result, "}"); \
1703 string_append(&result, "\n<br>-"); \
1704 string_join(&result, add_help_link(__name, csp->config)); \
1707 #define DEFINE_ACTION_MULTI(__name, __index) \
1708 lst = action->multi[__index]->first; \
1711 string_append(&result, "\n<br>-"); \
1712 string_join(&result, add_help_link(__name, csp->config)); \
1718 string_append(&result, "\n<br>+"); \
1719 string_join(&result, add_help_link(__name, csp->config)); \
1720 string_append(&result, "{"); \
1721 string_join(&result, html_encode(lst->str)); \
1722 string_append(&result, "}"); \
1727 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1729 #include "actionlist.h"
1731 #undef DEFINE_ACTION_MULTI
1732 #undef DEFINE_ACTION_STRING
1733 #undef DEFINE_ACTION_BOOL
1734 #undef DEFINE_ACTION_ALIAS