1 const char actions_rcs[] = "$Id: actions.c,v 1.32.2.6 2006/01/29 23:10:56 david__schmidt Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/Attic/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.32.2.6 2006/01/29 23:10:56 david__schmidt
37 * Multiple filter file support
39 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
40 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
41 * this becomes important when jcc.h gets included later down the line.
43 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
44 * - Implemented Privoxy version requirement through
45 * for-privoxy-version= statement in {{settings}}
47 * - Fix for unchecked out-of-memory condition
49 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
50 * Fixed memory leak reported by Dan Price in Bug #694713
52 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
53 * Extended unload_current_actions_file() to multiple AFs.
54 * Thanks to Oliver Stoeneberg for the hint.
56 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
57 * Change unsigned to unsigned long in actions_name struct. This closes
58 * SourceForge Bug #539284.
60 * Revision 1.32 2002/05/12 21:36:29 jongfoster
61 * Correcting function comments
63 * Revision 1.31 2002/05/06 07:56:50 oes
64 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
66 * Revision 1.30 2002/04/30 11:14:52 oes
67 * Made csp the first parameter in *action_to_html
69 * Revision 1.29 2002/04/26 19:30:54 jongfoster
70 * - current_action_to_html(): Adding help link for the "-" form of
72 * - Some actions had "<br>-", some "<br> -" (note the space).
73 * Standardizing on no space.
74 * - Greatly simplifying some of the code by using string_join()
77 * Revision 1.28 2002/04/26 12:53:15 oes
78 * - CGI AF editor now writes action lines split into
79 * single lines with line continuation
80 * - actions_to_html now embeds each action name in
82 * - current_action_to_text is now called current_action_to_html
83 * and acts like actions_to_html
85 * Revision 1.27 2002/04/24 02:10:31 oes
86 * - Jon's patch for multiple AFs:
87 * - split load_actions_file, add load_one_actions_file
88 * - make csp->actions_list files an array
89 * - remember file id with each action
90 * - Copy_action now frees dest action before copying
92 * Revision 1.26 2002/03/26 22:29:54 swa
93 * we have a new homepage!
95 * Revision 1.25 2002/03/24 13:25:43 swa
96 * name change related issues
98 * Revision 1.24 2002/03/16 23:54:06 jongfoster
99 * Adding graceful termination feature, to help look for memory leaks.
100 * If you enable this (which, by design, has to be done by hand
101 * editing config.h) and then go to http://i.j.b/die, then the program
102 * will exit cleanly after the *next* request. It should free all the
103 * memory that was used.
105 * Revision 1.23 2002/03/07 03:46:16 oes
106 * Fixed compiler warnings
108 * Revision 1.22 2002/01/21 00:27:02 jongfoster
109 * Allowing free_action(NULL).
110 * Moving the functions that #include actionlist.h to the end of the file,
111 * because the Visual C++ 97 debugger gets extremely confused if you try
112 * to debug any code that comes after them in the file.
114 * Revision 1.21 2002/01/17 20:54:44 jongfoster
115 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
117 * Revision 1.20 2001/11/22 21:56:49 jongfoster
118 * Making action_spec->flags into an unsigned long rather than just an
120 * Fixing a bug in the display of -add-header and -wafer
122 * Revision 1.19 2001/11/13 00:14:07 jongfoster
123 * Fixing stupid bug now I've figured out what || means.
124 * (It always returns 0 or 1, not one of it's paramaters.)
126 * Revision 1.18 2001/11/07 00:06:06 steudten
127 * Add line number in error output for lineparsing for
130 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
131 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
132 * threads to call select() simultaneously. So, it's time to do a real, live,
133 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
134 * (native). Both versions will work, but using __OS2__ offers multi-threading.
136 * Revision 1.16 2001/10/23 21:30:30 jongfoster
137 * Adding error-checking to selected functions.
139 * Revision 1.15 2001/10/14 21:58:22 jongfoster
140 * Adding support for the CGI-based editor:
141 * - Exported get_actions()
142 * - Added new function free_alias_list()
143 * - Added support for {{settings}} and {{description}} blocks
144 * in the actions file. They are currently ignored.
145 * - Added restriction to only one {{alias}} block which must appear
146 * first in the file, to simplify the editor's rewriting rules.
147 * - Note that load_actions_file() is no longer used by the CGI-based
148 * editor, but some of the other routines in this file are.
150 * Revision 1.14 2001/09/22 16:36:59 jongfoster
151 * Removing unused parameter fs from read_config_line()
153 * Revision 1.13 2001/09/16 15:47:37 jongfoster
154 * First version of CGI-based edit interface. This is very much a
155 * work-in-progress, and you can't actually use it to edit anything
156 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
157 * to have any effect.
159 * Revision 1.12 2001/09/16 13:21:27 jongfoster
160 * Changes to use new list functions.
162 * Revision 1.11 2001/09/14 00:17:32 jongfoster
163 * Tidying up memory allocation. New function init_action().
165 * Revision 1.10 2001/09/10 10:14:34 oes
166 * Removing unused variable
168 * Revision 1.9 2001/07/30 22:08:36 jongfoster
169 * Tidying up #defines:
170 * - All feature #defines are now of the form FEATURE_xxx
171 * - Permanently turned off WIN_GUI_EDIT
172 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
174 * Revision 1.8 2001/06/29 13:19:52 oes
175 * Removed logentry from cancelled commit
177 * Revision 1.7 2001/06/09 10:55:28 jongfoster
178 * Changing BUFSIZ ==> BUFFER_SIZE
180 * Revision 1.6 2001/06/07 23:04:34 jongfoster
181 * Made get_actions() static.
183 * Revision 1.5 2001/06/03 19:11:48 oes
184 * adapted to new enlist_unique arg format
186 * Revision 1.4 2001/06/01 20:03:42 jongfoster
187 * Better memory management - current_action->strings[] now
188 * contains copies of the strings, not the original.
190 * Revision 1.3 2001/06/01 18:49:17 jongfoster
191 * Replaced "list_share" with "list" - the tiny memory gain was not
192 * worth the extra complexity.
194 * Revision 1.2 2001/05/31 21:40:00 jongfoster
195 * Removing some commented out, obsolete blocks of code.
197 * Revision 1.1 2001/05/31 21:16:46 jongfoster
198 * Moved functions to process the action list into this new file.
201 *********************************************************************/
211 #ifdef FEATURE_PTHREAD
219 #include "miscutil.h"
223 #include "urlmatch.h"
227 const char actions_h_rcs[] = ACTIONS_H_VERSION;
231 * We need the main list of options.
233 * First, we need a way to tell between boolean, string, and multi-string
234 * options. For string and multistring options, we also need to be
235 * able to tell the difference between a "+" and a "-". (For bools,
236 * the "+"/"-" information is encoded in "add" and "mask"). So we use
237 * an enumerated type (well, the preprocessor equivalent). Here are
240 #define AV_NONE 0 /* +opt -opt */
241 #define AV_ADD_STRING 1 /* +stropt{string} */
242 #define AV_REM_STRING 2 /* -stropt */
243 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
244 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
247 * We need a structure to hold the name, flag changes,
248 * type, and string index.
253 unsigned long mask; /* a bit set to "0" = remove action */
254 unsigned long add; /* a bit set to "1" = add action */
255 int takes_value; /* an AV_... constant */
256 int index; /* index into strings[] or multi[] */
260 * And with those building blocks in place, here's the array.
262 static const struct action_name action_names[] =
265 * Well actually there's no data here - it's in actionlist.h
266 * This keeps it together to make it easy to change.
268 * Here's the macros used to format it:
270 #define DEFINE_ACTION_MULTI(name,index) \
271 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
272 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
273 #define DEFINE_ACTION_STRING(name,flag,index) \
274 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
275 { "-" name, ~flag, 0, AV_REM_STRING, index },
276 #define DEFINE_ACTION_BOOL(name,flag) \
277 { "+" name, ACTION_MASK_ALL, flag }, \
278 { "-" name, ~flag, 0 },
279 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
281 #include "actionlist.h"
283 #undef DEFINE_ACTION_MULTI
284 #undef DEFINE_ACTION_STRING
285 #undef DEFINE_ACTION_BOOL
286 #undef DEFINE_ACTION_ALIAS
288 { NULL, 0, 0 } /* End marker */
292 static int load_one_actions_file(struct client_state *csp, int fileid);
295 /*********************************************************************
297 * Function : merge_actions
299 * Description : Merge two actions together.
300 * Similar to "dest += src".
303 * 1 : dest = Actions to modify.
304 * 2 : src = Action to add.
306 * Returns : JB_ERR_OK or JB_ERR_MEMORY
308 *********************************************************************/
309 jb_err merge_actions (struct action_spec *dest,
310 const struct action_spec *src)
315 dest->mask &= src->mask;
316 dest->add &= src->mask;
317 dest->add |= src->add;
319 for (i = 0; i < ACTION_STRING_COUNT; i++)
321 char * str = src->string[i];
324 freez(dest->string[i]);
325 dest->string[i] = strdup(str);
326 if (NULL == dest->string[i])
328 return JB_ERR_MEMORY;
333 for (i = 0; i < ACTION_MULTI_COUNT; i++)
335 if (src->multi_remove_all[i])
337 /* Remove everything from dest */
338 list_remove_all(dest->multi_remove[i]);
339 dest->multi_remove_all[i] = 1;
341 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
343 else if (dest->multi_remove_all[i])
346 * dest already removes everything, so we only need to worry
349 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
350 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
354 /* No "remove all"s to worry about. */
355 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
356 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
357 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
370 /*********************************************************************
372 * Function : copy_action
374 * Description : Copy an action_specs.
375 * Similar to "dest = src".
378 * 1 : dest = Destination of copy.
379 * 2 : src = Source for copy.
383 *********************************************************************/
384 jb_err copy_action (struct action_spec *dest,
385 const struct action_spec *src)
388 jb_err err = JB_ERR_OK;
391 memset(dest, '\0', sizeof(*dest));
393 dest->mask = src->mask;
394 dest->add = src->add;
396 for (i = 0; i < ACTION_STRING_COUNT; i++)
398 char * str = src->string[i];
404 return JB_ERR_MEMORY;
406 dest->string[i] = str;
410 for (i = 0; i < ACTION_MULTI_COUNT; i++)
412 dest->multi_remove_all[i] = src->multi_remove_all[i];
413 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
418 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
428 /*********************************************************************
430 * Function : free_action
432 * Description : Destroy an action_spec. Frees memory used by it,
433 * except for the memory used by the struct action_spec
437 * 1 : src = Source to free.
441 *********************************************************************/
442 void free_action (struct action_spec *src)
451 for (i = 0; i < ACTION_STRING_COUNT; i++)
453 freez(src->string[i]);
456 for (i = 0; i < ACTION_MULTI_COUNT; i++)
458 destroy_list(src->multi_remove[i]);
459 destroy_list(src->multi_add[i]);
462 memset(src, '\0', sizeof(*src));
466 /*********************************************************************
468 * Function : get_action_token
470 * Description : Parses a line for the first action.
471 * Modifies it's input array, doesn't allocate memory.
473 * *line=" +abc{def} -ghi "
480 * 1 : line = [in] The line containing the action.
481 * [out] Start of next action on line, or
482 * NULL if we reached the end of line before
483 * we found an action.
484 * 2 : name = [out] Start of action name, null
485 * terminated. NULL on EOL
486 * 3 : value = [out] Start of action value, null
487 * terminated. NULL if none or EOL.
489 * Returns : JB_ERR_OK => Ok
490 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
492 *********************************************************************/
493 jb_err get_action_token(char **line, char **name, char **value)
498 /* set default returns */
503 /* Eat any leading whitespace */
504 while ((*str == ' ') || (*str == '\t'))
516 /* null name, just value is prohibited */
523 while (((ch = *str) != '\0') &&
524 (ch != ' ') && (ch != '\t') && (ch != '{'))
528 /* error, '}' without '{' */
540 /* EOL - be careful not to run off buffer */
545 /* More to parse next time. */
554 str = strchr(str, '}');
572 /*********************************************************************
574 * Function : get_actions
576 * Description : Parses a list of actions.
579 * 1 : line = The string containing the actions.
580 * Will be written to by this function.
581 * 2 : alias_list = Custom alias list, or NULL for none.
582 * 3 : cur_action = Where to store the action. Caller
585 * Returns : JB_ERR_OK => Ok
586 * JB_ERR_PARSE => Parse error (line was trashed anyway)
587 * nonzero => Out of memory (line was trashed anyway)
589 *********************************************************************/
590 jb_err get_actions(char *line,
591 struct action_alias * alias_list,
592 struct action_spec *cur_action)
595 init_action(cur_action);
596 cur_action->mask = ACTION_MASK_ALL;
600 char * option = NULL;
603 err = get_action_token(&line, &option, &value);
611 /* handle option in 'option' */
613 /* Check for standard action name */
614 const struct action_name * action = action_names;
616 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
620 if (action->name != NULL)
623 cur_action->mask &= action->mask;
624 cur_action->add &= action->mask;
625 cur_action->add |= action->add;
627 switch (action->takes_value)
630 /* ignore any option. */
634 /* add single string. */
636 if ((value == NULL) || (*value == '\0'))
640 /* FIXME: should validate option string here */
641 freez (cur_action->string[action->index]);
642 cur_action->string[action->index] = strdup(value);
643 if (NULL == cur_action->string[action->index])
645 return JB_ERR_MEMORY;
651 /* remove single string. */
653 freez (cur_action->string[action->index]);
658 /* append multi string. */
660 struct list * remove_p = cur_action->multi_remove[action->index];
661 struct list * add_p = cur_action->multi_add[action->index];
663 if ((value == NULL) || (*value == '\0'))
668 list_remove_item(remove_p, value);
669 err = enlist_unique(add_p, value, 0);
678 /* remove multi string. */
680 struct list * remove_p = cur_action->multi_remove[action->index];
681 struct list * add_p = cur_action->multi_add[action->index];
683 if ( (value == NULL) || (*value == '\0')
684 || ((*value == '*') && (value[1] == '\0')) )
687 * no option, or option == "*".
691 list_remove_all(remove_p);
692 list_remove_all(add_p);
693 cur_action->multi_remove_all[action->index] = 1;
697 /* Valid option - remove only 1 option */
699 if ( !cur_action->multi_remove_all[action->index] )
701 /* there isn't a catch-all in the remove list already */
702 err = enlist_unique(remove_p, value, 0);
708 list_remove_item(add_p, value);
713 /* Shouldn't get here unless there's memory corruption. */
720 /* try user aliases. */
721 const struct action_alias * alias = alias_list;
723 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
730 merge_actions(cur_action, alias->action);
734 /* Bad action name */
745 /*********************************************************************
747 * Function : init_current_action
749 * Description : Zero out an action.
752 * 1 : dest = An uninitialized current_action_spec.
756 *********************************************************************/
757 void init_current_action (struct current_action_spec *dest)
759 memset(dest, '\0', sizeof(*dest));
761 dest->flags = ACTION_MOST_COMPATIBLE;
765 /*********************************************************************
767 * Function : init_action
769 * Description : Zero out an action.
772 * 1 : dest = An uninitialized action_spec.
776 *********************************************************************/
777 void init_action (struct action_spec *dest)
779 memset(dest, '\0', sizeof(*dest));
783 /*********************************************************************
785 * Function : merge_current_action
787 * Description : Merge two actions together.
788 * Similar to "dest += src".
789 * Differences between this and merge_actions()
790 * is that this one doesn't allocate memory for
791 * strings (so "src" better be in memory for at least
792 * as long as "dest" is, and you'd better free
793 * "dest" using "free_current_action").
794 * Also, there is no mask or remove lists in dest.
795 * (If we're applying it to a URL, we don't need them)
798 * 1 : dest = Current actions, to modify.
799 * 2 : src = Action to add.
801 * Returns 0 : no error
802 * !=0 : error, probably JB_ERR_MEMORY.
804 *********************************************************************/
805 jb_err merge_current_action (struct current_action_spec *dest,
806 const struct action_spec *src)
809 jb_err err = JB_ERR_OK;
811 dest->flags &= src->mask;
812 dest->flags |= src->add;
814 for (i = 0; i < ACTION_STRING_COUNT; i++)
816 char * str = src->string[i];
822 return JB_ERR_MEMORY;
824 freez(dest->string[i]);
825 dest->string[i] = str;
829 for (i = 0; i < ACTION_MULTI_COUNT; i++)
831 if (src->multi_remove_all[i])
833 /* Remove everything from dest, then add src->multi_add */
834 err = list_duplicate(dest->multi[i], src->multi_add[i]);
842 list_remove_list(dest->multi[i], src->multi_remove[i]);
843 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
854 /*********************************************************************
856 * Function : free_current_action
858 * Description : Free memory used by a current_action_spec.
859 * Does not free the current_action_spec itself.
862 * 1 : src = Source to free.
866 *********************************************************************/
867 void free_current_action (struct current_action_spec *src)
871 for (i = 0; i < ACTION_STRING_COUNT; i++)
873 freez(src->string[i]);
876 for (i = 0; i < ACTION_MULTI_COUNT; i++)
878 destroy_list(src->multi[i]);
881 memset(src, '\0', sizeof(*src));
885 static struct file_list *current_actions_file[MAX_AF_FILES] = {
886 NULL, NULL, NULL, NULL, NULL,
887 NULL, NULL, NULL, NULL, NULL
891 #ifdef FEATURE_GRACEFUL_TERMINATION
892 /*********************************************************************
894 * Function : unload_current_actions_file
896 * Description : Unloads current actions file - reset to state at
897 * beginning of program.
903 *********************************************************************/
904 void unload_current_actions_file(void)
908 for (i = 0; i < MAX_AF_FILES; i++)
910 if (current_actions_file[i])
912 current_actions_file[i]->unloader = unload_actions_file;
913 current_actions_file[i] = NULL;
917 #endif /* FEATURE_GRACEFUL_TERMINATION */
920 /*********************************************************************
922 * Function : unload_actions_file
924 * Description : Unloads an actions module.
927 * 1 : file_data = the data structure associated with the
932 *********************************************************************/
933 void unload_actions_file(void *file_data)
935 struct url_actions * next;
936 struct url_actions * cur = (struct url_actions *)file_data;
940 free_url_spec(cur->url);
941 free_action(cur->action);
949 /*********************************************************************
951 * Function : free_alias_list
953 * Description : Free memory used by a list of aliases.
956 * 1 : alias_list = Linked list to free.
960 *********************************************************************/
961 void free_alias_list(struct action_alias *alias_list)
963 while (alias_list != NULL)
965 struct action_alias * next = alias_list->next;
966 alias_list->next = NULL;
967 freez(alias_list->name);
968 free_action(alias_list->action);
975 /*********************************************************************
977 * Function : load_actions_file
979 * Description : Read and parse all the action files and add to files
983 * 1 : csp = Current client state (buffers, headers, etc...)
985 * Returns : 0 => Ok, everything else is an error.
987 *********************************************************************/
988 int load_actions_file(struct client_state *csp)
993 for (i = 0; i < MAX_AF_FILES; i++)
995 if (csp->config->actions_file[i])
997 result = load_one_actions_file(csp, i);
1003 else if (current_actions_file[i])
1005 current_actions_file[i]->unloader = unload_actions_file;
1006 current_actions_file[i] = NULL;
1013 /*********************************************************************
1015 * Function : load_one_actions_file
1017 * Description : Read and parse a action file and add to files
1021 * 1 : csp = Current client state (buffers, headers, etc...)
1022 * 2 : fileid = File index to load.
1024 * Returns : 0 => Ok, everything else is an error.
1026 *********************************************************************/
1027 static int load_one_actions_file(struct client_state *csp, int fileid)
1032 * Note: Keep these in the order they occur in the file, they are
1033 * sometimes tested with <=
1035 #define MODE_START_OF_FILE 1
1036 #define MODE_SETTINGS 2
1037 #define MODE_DESCRIPTION 3
1038 #define MODE_ALIAS 4
1039 #define MODE_ACTIONS 5
1041 int mode = MODE_START_OF_FILE;
1044 struct url_actions *last_perm;
1045 struct url_actions *perm;
1046 char buf[BUFFER_SIZE];
1047 struct file_list *fs;
1048 struct action_spec * cur_action = NULL;
1049 int cur_action_used = 0;
1050 struct action_alias * alias_list = NULL;
1051 unsigned long linenum = 0;
1053 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1055 /* No need to load */
1056 csp->actions_list[fileid] = current_actions_file[fileid];
1061 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1062 csp->config->actions_file[fileid]);
1063 return 1; /* never get here */
1066 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1067 if (last_perm == NULL)
1069 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1070 csp->config->actions_file[fileid]);
1071 return 1; /* never get here */
1074 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1076 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1077 csp->config->actions_file[fileid]);
1078 return 1; /* never get here */
1081 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1085 /* It's a header block */
1088 /* It's {{settings}} or {{alias}} */
1089 int len = strlen(buf);
1090 char * start = buf + 2;
1091 char * end = buf + len - 1;
1092 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1096 log_error(LOG_LEVEL_FATAL,
1097 "can't load actions file '%s': invalid line (%lu): %s",
1098 csp->config->actions_file[fileid], linenum, buf);
1099 return 1; /* never get here */
1102 /* Trim leading and trailing whitespace. */
1110 log_error(LOG_LEVEL_FATAL,
1111 "can't load actions file '%s': invalid line (%lu): {{ }}",
1112 csp->config->actions_file[fileid], linenum);
1113 return 1; /* never get here */
1117 * An actionsfile can optionally contain the following blocks.
1118 * They *MUST* be in this order, to simplify processing:
1124 * ...free text, format TBD, but no line may start with a '{'...
1129 * The actual actions must be *after* these special blocks.
1130 * None of these special blocks may be repeated.
1133 if (0 == strcmpic(start, "settings"))
1135 /* it's a {{settings}} block */
1136 if (mode >= MODE_SETTINGS)
1138 /* {{settings}} must be first thing in file and must only
1142 log_error(LOG_LEVEL_FATAL,
1143 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1144 csp->config->actions_file[fileid], linenum);
1146 mode = MODE_SETTINGS;
1148 else if (0 == strcmpic(start, "description"))
1150 /* it's a {{description}} block */
1151 if (mode >= MODE_DESCRIPTION)
1153 /* {{description}} is a singleton and only {{settings}} may proceed it
1156 log_error(LOG_LEVEL_FATAL,
1157 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1158 csp->config->actions_file[fileid], linenum);
1160 mode = MODE_DESCRIPTION;
1162 else if (0 == strcmpic(start, "alias"))
1164 /* it's an {{alias}} block */
1165 if (mode >= MODE_ALIAS)
1167 /* {{alias}} must be first thing in file, possibly after
1168 * {{settings}} and {{description}}
1170 * {{alias}} must only appear once.
1172 * Note that these are new restrictions introduced in
1173 * v2.9.10 in order to make actionsfile editing simpler.
1174 * (Otherwise, reordering actionsfile entries without
1175 * completely rewriting the file becomes non-trivial)
1178 log_error(LOG_LEVEL_FATAL,
1179 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1180 csp->config->actions_file[fileid], linenum);
1186 /* invalid {{something}} block */
1188 log_error(LOG_LEVEL_FATAL,
1189 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1190 csp->config->actions_file[fileid], linenum, start);
1191 return 1; /* never get here */
1196 /* It's an actions block */
1198 char actions_buf[BUFFER_SIZE];
1202 mode = MODE_ACTIONS;
1204 /* free old action */
1207 if (!cur_action_used)
1209 free_action(cur_action);
1214 cur_action_used = 0;
1215 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1216 if (cur_action == NULL)
1219 log_error(LOG_LEVEL_FATAL,
1220 "can't load actions file '%s': out of memory",
1221 csp->config->actions_file[fileid]);
1222 return 1; /* never get here */
1224 init_action(cur_action);
1227 strcpy(actions_buf, buf + 1);
1229 /* check we have a trailing } and then trim it */
1230 end = actions_buf + strlen(actions_buf) - 1;
1235 log_error(LOG_LEVEL_FATAL,
1236 "can't load actions file '%s': invalid line (%lu): %s",
1237 csp->config->actions_file[fileid], linenum, buf);
1238 return 1; /* never get here */
1242 /* trim any whitespace immediately inside {} */
1245 if (get_actions(actions_buf, alias_list, cur_action))
1249 log_error(LOG_LEVEL_FATAL,
1250 "can't load actions file '%s': invalid line (%lu): %s",
1251 csp->config->actions_file[fileid], linenum, buf);
1252 return 1; /* never get here */
1256 else if (mode == MODE_SETTINGS)
1259 * Part of the {{settings}} block.
1260 * For now only serves to check if the file's minimum Privoxy
1261 * version requirement is met, but we may want to read & check
1262 * permissions when we go multi-user.
1264 if (!strncmp(buf, "for-privoxy-version=", 20))
1266 char *version_string, *fields[3];
1269 if ((version_string = strdup(buf + 20)) == NULL)
1272 log_error(LOG_LEVEL_FATAL,
1273 "can't load actions file '%s': out of memory!",
1274 csp->config->actions_file[fileid]);
1275 return 1; /* never get here */
1278 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1280 if (num_fields < 1 || atoi(fields[0]) == 0)
1282 log_error(LOG_LEVEL_ERROR,
1283 "While loading actions file '%s': invalid line (%lu): %s",
1284 csp->config->actions_file[fileid], linenum, buf);
1286 else if ( atoi(fields[0]) > VERSION_MAJOR
1287 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1288 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1291 log_error(LOG_LEVEL_FATAL,
1292 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1293 csp->config->actions_file[fileid], linenum, buf );
1294 return 1; /* never get here */
1296 free(version_string);
1299 else if (mode == MODE_DESCRIPTION)
1302 * Part of the {{description}} block.
1306 else if (mode == MODE_ALIAS)
1311 char actions_buf[BUFFER_SIZE];
1312 struct action_alias * new_alias;
1314 char * start = strchr(buf, '=');
1317 if ((start == NULL) || (start == buf))
1319 log_error(LOG_LEVEL_FATAL,
1320 "can't load actions file '%s': invalid alias line (%lu): %s",
1321 csp->config->actions_file[fileid], linenum, buf);
1322 return 1; /* never get here */
1325 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1328 log_error(LOG_LEVEL_FATAL,
1329 "can't load actions file '%s': out of memory!",
1330 csp->config->actions_file[fileid]);
1331 return 1; /* never get here */
1334 /* Eat any the whitespace before the '=' */
1336 while ((*end == ' ') || (*end == '\t'))
1339 * we already know we must have at least 1 non-ws char
1340 * at start of buf - no need to check
1346 /* Eat any the whitespace after the '=' */
1348 while ((*start == ' ') || (*start == '\t'))
1354 log_error(LOG_LEVEL_FATAL,
1355 "can't load actions file '%s': invalid alias line (%lu): %s",
1356 csp->config->actions_file[fileid], linenum, buf);
1357 return 1; /* never get here */
1360 if ((new_alias->name = strdup(buf)) == NULL)
1363 log_error(LOG_LEVEL_FATAL,
1364 "can't load actions file '%s': out of memory!",
1365 csp->config->actions_file[fileid]);
1366 return 1; /* never get here */
1369 strcpy(actions_buf, start);
1371 if (get_actions(actions_buf, alias_list, new_alias->action))
1375 log_error(LOG_LEVEL_FATAL,
1376 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1377 csp->config->actions_file[fileid], linenum, buf, start);
1378 return 1; /* never get here */
1382 new_alias->next = alias_list;
1383 alias_list = new_alias;
1385 else if (mode == MODE_ACTIONS)
1387 /* it's a URL pattern */
1389 /* allocate a new node */
1390 if ((perm = zalloc(sizeof(*perm))) == NULL)
1393 log_error(LOG_LEVEL_FATAL,
1394 "can't load actions file '%s': out of memory!",
1395 csp->config->actions_file[fileid]);
1396 return 1; /* never get here */
1400 copy_action (perm->action, cur_action);
1402 /* Save the URL pattern */
1403 if (create_url_spec(perm->url, buf))
1406 log_error(LOG_LEVEL_FATAL,
1407 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1408 csp->config->actions_file[fileid], linenum, buf);
1409 return 1; /* never get here */
1412 /* add it to the list */
1413 last_perm->next = perm;
1416 else if (mode == MODE_START_OF_FILE)
1418 /* oops - please have a {} line as 1st line in file. */
1420 log_error(LOG_LEVEL_FATAL,
1421 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1422 csp->config->actions_file[fileid], linenum, buf);
1423 return 1; /* never get here */
1427 /* How did we get here? This is impossible! */
1429 log_error(LOG_LEVEL_FATAL,
1430 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1431 csp->config->actions_file[fileid], mode);
1432 return 1; /* never get here */
1438 free_action(cur_action);
1441 free_alias_list(alias_list);
1443 /* the old one is now obsolete */
1444 if (current_actions_file[fileid])
1446 current_actions_file[fileid]->unloader = unload_actions_file;
1449 fs->next = files->next;
1451 current_actions_file[fileid] = fs;
1453 csp->actions_list[fileid] = fs;
1460 /*********************************************************************
1462 * Function : actions_to_text
1464 * Description : Converts a actionsfile entry from the internal
1465 * structurt into a text line. The output is split
1466 * into one line for each action with line continuation.
1469 * 1 : action = The action to format.
1471 * Returns : A string. Caller must free it.
1472 * NULL on out-of-memory error.
1474 *********************************************************************/
1475 char * actions_to_text(struct action_spec *action)
1477 unsigned mask = action->mask;
1478 unsigned add = action->add;
1479 char * result = strdup("");
1480 struct list_entry * lst;
1482 /* sanity - prevents "-feature +feature" */
1486 #define DEFINE_ACTION_BOOL(__name, __bit) \
1487 if (!(mask & __bit)) \
1489 string_append(&result, " -" __name " \\\n"); \
1491 else if (add & __bit) \
1493 string_append(&result, " +" __name " \\\n"); \
1496 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1497 if (!(mask & __bit)) \
1499 string_append(&result, " -" __name " \\\n"); \
1501 else if (add & __bit) \
1503 string_append(&result, " +" __name "{"); \
1504 string_append(&result, action->string[__index]); \
1505 string_append(&result, "} \\\n"); \
1508 #define DEFINE_ACTION_MULTI(__name, __index) \
1509 if (action->multi_remove_all[__index]) \
1511 string_append(&result, " -" __name " \\\n"); \
1515 lst = action->multi_remove[__index]->first; \
1518 string_append(&result, " -" __name "{"); \
1519 string_append(&result, lst->str); \
1520 string_append(&result, "} \\\n"); \
1524 lst = action->multi_add[__index]->first; \
1527 string_append(&result, " +" __name "{"); \
1528 string_append(&result, lst->str); \
1529 string_append(&result, "} \\\n"); \
1533 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1535 #include "actionlist.h"
1537 #undef DEFINE_ACTION_MULTI
1538 #undef DEFINE_ACTION_STRING
1539 #undef DEFINE_ACTION_BOOL
1540 #undef DEFINE_ACTION_ALIAS
1546 /*********************************************************************
1548 * Function : actions_to_html
1550 * Description : Converts a actionsfile entry from numeric form
1551 * ("mask" and "add") to a <br>-seperated HTML string
1552 * in which each action is linked to its chapter in
1556 * 1 : csp = Client state (for config)
1557 * 2 : action = Action spec to be converted
1559 * Returns : A string. Caller must free it.
1560 * NULL on out-of-memory error.
1562 *********************************************************************/
1563 char * actions_to_html(struct client_state *csp,
1564 struct action_spec *action)
1566 unsigned mask = action->mask;
1567 unsigned add = action->add;
1568 char * result = strdup("");
1569 struct list_entry * lst;
1571 /* sanity - prevents "-feature +feature" */
1575 #define DEFINE_ACTION_BOOL(__name, __bit) \
1576 if (!(mask & __bit)) \
1578 string_append(&result, "\n<br>-"); \
1579 string_join(&result, add_help_link(__name, csp->config)); \
1581 else if (add & __bit) \
1583 string_append(&result, "\n<br>+"); \
1584 string_join(&result, add_help_link(__name, csp->config)); \
1587 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1588 if (!(mask & __bit)) \
1590 string_append(&result, "\n<br>-"); \
1591 string_join(&result, add_help_link(__name, csp->config)); \
1593 else if (add & __bit) \
1595 string_append(&result, "\n<br>+"); \
1596 string_join(&result, add_help_link(__name, csp->config)); \
1597 string_append(&result, "{"); \
1598 string_join(&result, html_encode(action->string[__index])); \
1599 string_append(&result, "}"); \
1602 #define DEFINE_ACTION_MULTI(__name, __index) \
1603 if (action->multi_remove_all[__index]) \
1605 string_append(&result, "\n<br>-"); \
1606 string_join(&result, add_help_link(__name, csp->config)); \
1610 lst = action->multi_remove[__index]->first; \
1613 string_append(&result, "\n<br>-"); \
1614 string_join(&result, add_help_link(__name, csp->config)); \
1615 string_append(&result, "{"); \
1616 string_join(&result, html_encode(lst->str)); \
1617 string_append(&result, "}"); \
1621 lst = action->multi_add[__index]->first; \
1624 string_append(&result, "\n<br>+"); \
1625 string_join(&result, add_help_link(__name, csp->config)); \
1626 string_append(&result, "{"); \
1627 string_join(&result, html_encode(lst->str)); \
1628 string_append(&result, "}"); \
1632 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1634 #include "actionlist.h"
1636 #undef DEFINE_ACTION_MULTI
1637 #undef DEFINE_ACTION_STRING
1638 #undef DEFINE_ACTION_BOOL
1639 #undef DEFINE_ACTION_ALIAS
1641 /* trim leading <br> */
1642 if (result && *result)
1645 result = strdup(result + 5);
1653 /*********************************************************************
1655 * Function : current_actions_to_html
1657 * Description : Converts a curren action spec to a <br> seperated HTML
1658 * text in which each action is linked to its chapter in
1662 * 1 : csp = Client state (for config)
1663 * 2 : action = Current action spec to be converted
1665 * Returns : A string. Caller must free it.
1666 * NULL on out-of-memory error.
1668 *********************************************************************/
1669 char *current_action_to_html(struct client_state *csp,
1670 struct current_action_spec *action)
1672 unsigned long flags = action->flags;
1673 char * result = strdup("");
1674 struct list_entry * lst;
1676 #define DEFINE_ACTION_BOOL(__name, __bit) \
1677 if (flags & __bit) \
1679 string_append(&result, "\n<br>+"); \
1680 string_join(&result, add_help_link(__name, csp->config)); \
1684 string_append(&result, "\n<br>-"); \
1685 string_join(&result, add_help_link(__name, csp->config)); \
1688 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1689 if (flags & __bit) \
1691 string_append(&result, "\n<br>+"); \
1692 string_join(&result, add_help_link(__name, csp->config)); \
1693 string_append(&result, "{"); \
1694 string_join(&result, html_encode(action->string[__index])); \
1695 string_append(&result, "}"); \
1699 string_append(&result, "\n<br>-"); \
1700 string_join(&result, add_help_link(__name, csp->config)); \
1703 #define DEFINE_ACTION_MULTI(__name, __index) \
1704 lst = action->multi[__index]->first; \
1707 string_append(&result, "\n<br>-"); \
1708 string_join(&result, add_help_link(__name, csp->config)); \
1714 string_append(&result, "\n<br>+"); \
1715 string_join(&result, add_help_link(__name, csp->config)); \
1716 string_append(&result, "{"); \
1717 string_join(&result, html_encode(lst->str)); \
1718 string_append(&result, "}"); \
1723 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1725 #include "actionlist.h"
1727 #undef DEFINE_ACTION_MULTI
1728 #undef DEFINE_ACTION_STRING
1729 #undef DEFINE_ACTION_BOOL
1730 #undef DEFINE_ACTION_ALIAS