1 const char actions_rcs[] = "$Id: actions.c,v 1.39 2007/04/17 18:21:45 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.39 2007/04/17 18:21:45 fabiankeil
37 * Split update_action_bits() into
38 * update_action_bits_for_all_tags()
39 * and update_action_bits_for_tag().
41 * Revision 1.38 2007/04/15 16:39:20 fabiankeil
42 * Introduce tags as alternative way to specify which
43 * actions apply to a request. At the moment tags can be
44 * created based on client and server headers.
46 * Revision 1.37 2007/03/11 15:56:12 fabiankeil
47 * Add kludge to log unknown aliases and actions before exiting.
49 * Revision 1.36 2006/12/28 17:15:42 fabiankeil
50 * Fix gcc43 conversion warning.
52 * Revision 1.35 2006/07/18 14:48:45 david__schmidt
53 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
54 * with what was really the latest development (the v_3_0_branch branch)
56 * Revision 1.32.2.6 2006/01/29 23:10:56 david__schmidt
57 * Multiple filter file support
59 * Revision 1.32.2.5 2005/06/09 01:18:41 david__schmidt
60 * Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
61 * this becomes important when jcc.h gets included later down the line.
63 * Revision 1.32.2.4 2003/12/03 10:33:11 oes
64 * - Implemented Privoxy version requirement through
65 * for-privoxy-version= statement in {{settings}}
67 * - Fix for unchecked out-of-memory condition
69 * Revision 1.32.2.3 2003/02/28 12:52:10 oes
70 * Fixed memory leak reported by Dan Price in Bug #694713
72 * Revision 1.32.2.2 2002/11/20 14:36:55 oes
73 * Extended unload_current_actions_file() to multiple AFs.
74 * Thanks to Oliver Stoeneberg for the hint.
76 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
77 * Change unsigned to unsigned long in actions_name struct. This closes
78 * SourceForge Bug #539284.
80 * Revision 1.32 2002/05/12 21:36:29 jongfoster
81 * Correcting function comments
83 * Revision 1.31 2002/05/06 07:56:50 oes
84 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
86 * Revision 1.30 2002/04/30 11:14:52 oes
87 * Made csp the first parameter in *action_to_html
89 * Revision 1.29 2002/04/26 19:30:54 jongfoster
90 * - current_action_to_html(): Adding help link for the "-" form of
92 * - Some actions had "<br>-", some "<br> -" (note the space).
93 * Standardizing on no space.
94 * - Greatly simplifying some of the code by using string_join()
97 * Revision 1.28 2002/04/26 12:53:15 oes
98 * - CGI AF editor now writes action lines split into
99 * single lines with line continuation
100 * - actions_to_html now embeds each action name in
102 * - current_action_to_text is now called current_action_to_html
103 * and acts like actions_to_html
105 * Revision 1.27 2002/04/24 02:10:31 oes
106 * - Jon's patch for multiple AFs:
107 * - split load_actions_file, add load_one_actions_file
108 * - make csp->actions_list files an array
109 * - remember file id with each action
110 * - Copy_action now frees dest action before copying
112 * Revision 1.26 2002/03/26 22:29:54 swa
113 * we have a new homepage!
115 * Revision 1.25 2002/03/24 13:25:43 swa
116 * name change related issues
118 * Revision 1.24 2002/03/16 23:54:06 jongfoster
119 * Adding graceful termination feature, to help look for memory leaks.
120 * If you enable this (which, by design, has to be done by hand
121 * editing config.h) and then go to http://i.j.b/die, then the program
122 * will exit cleanly after the *next* request. It should free all the
123 * memory that was used.
125 * Revision 1.23 2002/03/07 03:46:16 oes
126 * Fixed compiler warnings
128 * Revision 1.22 2002/01/21 00:27:02 jongfoster
129 * Allowing free_action(NULL).
130 * Moving the functions that #include actionlist.h to the end of the file,
131 * because the Visual C++ 97 debugger gets extremely confused if you try
132 * to debug any code that comes after them in the file.
134 * Revision 1.21 2002/01/17 20:54:44 jongfoster
135 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
137 * Revision 1.20 2001/11/22 21:56:49 jongfoster
138 * Making action_spec->flags into an unsigned long rather than just an
140 * Fixing a bug in the display of -add-header and -wafer
142 * Revision 1.19 2001/11/13 00:14:07 jongfoster
143 * Fixing stupid bug now I've figured out what || means.
144 * (It always returns 0 or 1, not one of it's paramaters.)
146 * Revision 1.18 2001/11/07 00:06:06 steudten
147 * Add line number in error output for lineparsing for
150 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
151 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
152 * threads to call select() simultaneously. So, it's time to do a real, live,
153 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
154 * (native). Both versions will work, but using __OS2__ offers multi-threading.
156 * Revision 1.16 2001/10/23 21:30:30 jongfoster
157 * Adding error-checking to selected functions.
159 * Revision 1.15 2001/10/14 21:58:22 jongfoster
160 * Adding support for the CGI-based editor:
161 * - Exported get_actions()
162 * - Added new function free_alias_list()
163 * - Added support for {{settings}} and {{description}} blocks
164 * in the actions file. They are currently ignored.
165 * - Added restriction to only one {{alias}} block which must appear
166 * first in the file, to simplify the editor's rewriting rules.
167 * - Note that load_actions_file() is no longer used by the CGI-based
168 * editor, but some of the other routines in this file are.
170 * Revision 1.14 2001/09/22 16:36:59 jongfoster
171 * Removing unused parameter fs from read_config_line()
173 * Revision 1.13 2001/09/16 15:47:37 jongfoster
174 * First version of CGI-based edit interface. This is very much a
175 * work-in-progress, and you can't actually use it to edit anything
176 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
177 * to have any effect.
179 * Revision 1.12 2001/09/16 13:21:27 jongfoster
180 * Changes to use new list functions.
182 * Revision 1.11 2001/09/14 00:17:32 jongfoster
183 * Tidying up memory allocation. New function init_action().
185 * Revision 1.10 2001/09/10 10:14:34 oes
186 * Removing unused variable
188 * Revision 1.9 2001/07/30 22:08:36 jongfoster
189 * Tidying up #defines:
190 * - All feature #defines are now of the form FEATURE_xxx
191 * - Permanently turned off WIN_GUI_EDIT
192 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
194 * Revision 1.8 2001/06/29 13:19:52 oes
195 * Removed logentry from cancelled commit
197 * Revision 1.7 2001/06/09 10:55:28 jongfoster
198 * Changing BUFSIZ ==> BUFFER_SIZE
200 * Revision 1.6 2001/06/07 23:04:34 jongfoster
201 * Made get_actions() static.
203 * Revision 1.5 2001/06/03 19:11:48 oes
204 * adapted to new enlist_unique arg format
206 * Revision 1.4 2001/06/01 20:03:42 jongfoster
207 * Better memory management - current_action->strings[] now
208 * contains copies of the strings, not the original.
210 * Revision 1.3 2001/06/01 18:49:17 jongfoster
211 * Replaced "list_share" with "list" - the tiny memory gain was not
212 * worth the extra complexity.
214 * Revision 1.2 2001/05/31 21:40:00 jongfoster
215 * Removing some commented out, obsolete blocks of code.
217 * Revision 1.1 2001/05/31 21:16:46 jongfoster
218 * Moved functions to process the action list into this new file.
221 *********************************************************************/
231 #ifdef FEATURE_PTHREAD
239 #include "miscutil.h"
243 #include "urlmatch.h"
247 const char actions_h_rcs[] = ACTIONS_H_VERSION;
251 * We need the main list of options.
253 * First, we need a way to tell between boolean, string, and multi-string
254 * options. For string and multistring options, we also need to be
255 * able to tell the difference between a "+" and a "-". (For bools,
256 * the "+"/"-" information is encoded in "add" and "mask"). So we use
257 * an enumerated type (well, the preprocessor equivalent). Here are
260 #define AV_NONE 0 /* +opt -opt */
261 #define AV_ADD_STRING 1 /* +stropt{string} */
262 #define AV_REM_STRING 2 /* -stropt */
263 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
264 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
267 * We need a structure to hold the name, flag changes,
268 * type, and string index.
273 unsigned long mask; /* a bit set to "0" = remove action */
274 unsigned long add; /* a bit set to "1" = add action */
275 int takes_value; /* an AV_... constant */
276 int index; /* index into strings[] or multi[] */
280 * And with those building blocks in place, here's the array.
282 static const struct action_name action_names[] =
285 * Well actually there's no data here - it's in actionlist.h
286 * This keeps it together to make it easy to change.
288 * Here's the macros used to format it:
290 #define DEFINE_ACTION_MULTI(name,index) \
291 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
292 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
293 #define DEFINE_ACTION_STRING(name,flag,index) \
294 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
295 { "-" name, ~flag, 0, AV_REM_STRING, index },
296 #define DEFINE_ACTION_BOOL(name,flag) \
297 { "+" name, ACTION_MASK_ALL, flag }, \
298 { "-" name, ~flag, 0 },
299 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
301 #include "actionlist.h"
303 #undef DEFINE_ACTION_MULTI
304 #undef DEFINE_ACTION_STRING
305 #undef DEFINE_ACTION_BOOL
306 #undef DEFINE_ACTION_ALIAS
308 { NULL, 0, 0 } /* End marker */
312 static int load_one_actions_file(struct client_state *csp, int fileid);
315 /*********************************************************************
317 * Function : merge_actions
319 * Description : Merge two actions together.
320 * Similar to "dest += src".
323 * 1 : dest = Actions to modify.
324 * 2 : src = Action to add.
326 * Returns : JB_ERR_OK or JB_ERR_MEMORY
328 *********************************************************************/
329 jb_err merge_actions (struct action_spec *dest,
330 const struct action_spec *src)
335 dest->mask &= src->mask;
336 dest->add &= src->mask;
337 dest->add |= src->add;
339 for (i = 0; i < ACTION_STRING_COUNT; i++)
341 char * str = src->string[i];
344 freez(dest->string[i]);
345 dest->string[i] = strdup(str);
346 if (NULL == dest->string[i])
348 return JB_ERR_MEMORY;
353 for (i = 0; i < ACTION_MULTI_COUNT; i++)
355 if (src->multi_remove_all[i])
357 /* Remove everything from dest */
358 list_remove_all(dest->multi_remove[i]);
359 dest->multi_remove_all[i] = 1;
361 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
363 else if (dest->multi_remove_all[i])
366 * dest already removes everything, so we only need to worry
369 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
370 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
374 /* No "remove all"s to worry about. */
375 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
376 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
377 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
390 /*********************************************************************
392 * Function : copy_action
394 * Description : Copy an action_specs.
395 * Similar to "dest = src".
398 * 1 : dest = Destination of copy.
399 * 2 : src = Source for copy.
403 *********************************************************************/
404 jb_err copy_action (struct action_spec *dest,
405 const struct action_spec *src)
408 jb_err err = JB_ERR_OK;
411 memset(dest, '\0', sizeof(*dest));
413 dest->mask = src->mask;
414 dest->add = src->add;
416 for (i = 0; i < ACTION_STRING_COUNT; i++)
418 char * str = src->string[i];
424 return JB_ERR_MEMORY;
426 dest->string[i] = str;
430 for (i = 0; i < ACTION_MULTI_COUNT; i++)
432 dest->multi_remove_all[i] = src->multi_remove_all[i];
433 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
438 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
448 /*********************************************************************
450 * Function : free_action
452 * Description : Destroy an action_spec. Frees memory used by it,
453 * except for the memory used by the struct action_spec
457 * 1 : src = Source to free.
461 *********************************************************************/
462 void free_action (struct action_spec *src)
471 for (i = 0; i < ACTION_STRING_COUNT; i++)
473 freez(src->string[i]);
476 for (i = 0; i < ACTION_MULTI_COUNT; i++)
478 destroy_list(src->multi_remove[i]);
479 destroy_list(src->multi_add[i]);
482 memset(src, '\0', sizeof(*src));
486 /*********************************************************************
488 * Function : get_action_token
490 * Description : Parses a line for the first action.
491 * Modifies it's input array, doesn't allocate memory.
493 * *line=" +abc{def} -ghi "
500 * 1 : line = [in] The line containing the action.
501 * [out] Start of next action on line, or
502 * NULL if we reached the end of line before
503 * we found an action.
504 * 2 : name = [out] Start of action name, null
505 * terminated. NULL on EOL
506 * 3 : value = [out] Start of action value, null
507 * terminated. NULL if none or EOL.
509 * Returns : JB_ERR_OK => Ok
510 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
512 *********************************************************************/
513 jb_err get_action_token(char **line, char **name, char **value)
518 /* set default returns */
523 /* Eat any leading whitespace */
524 while ((*str == ' ') || (*str == '\t'))
536 /* null name, just value is prohibited */
543 while (((ch = *str) != '\0') &&
544 (ch != ' ') && (ch != '\t') && (ch != '{'))
548 /* error, '}' without '{' */
560 /* EOL - be careful not to run off buffer */
565 /* More to parse next time. */
574 str = strchr(str, '}');
592 /*********************************************************************
594 * Function : get_actions
596 * Description : Parses a list of actions.
599 * 1 : line = The string containing the actions.
600 * Will be written to by this function.
601 * 2 : alias_list = Custom alias list, or NULL for none.
602 * 3 : cur_action = Where to store the action. Caller
605 * Returns : JB_ERR_OK => Ok
606 * JB_ERR_PARSE => Parse error (line was trashed anyway)
607 * nonzero => Out of memory (line was trashed anyway)
609 *********************************************************************/
610 jb_err get_actions(char *line,
611 struct action_alias * alias_list,
612 struct action_spec *cur_action)
615 init_action(cur_action);
616 cur_action->mask = ACTION_MASK_ALL;
620 char * option = NULL;
623 err = get_action_token(&line, &option, &value);
631 /* handle option in 'option' */
633 /* Check for standard action name */
634 const struct action_name * action = action_names;
636 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
640 if (action->name != NULL)
643 cur_action->mask &= action->mask;
644 cur_action->add &= action->mask;
645 cur_action->add |= action->add;
647 switch (action->takes_value)
650 /* ignore any option. */
654 /* add single string. */
656 if ((value == NULL) || (*value == '\0'))
660 /* FIXME: should validate option string here */
661 freez (cur_action->string[action->index]);
662 cur_action->string[action->index] = strdup(value);
663 if (NULL == cur_action->string[action->index])
665 return JB_ERR_MEMORY;
671 /* remove single string. */
673 freez (cur_action->string[action->index]);
678 /* append 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'))
688 list_remove_item(remove_p, value);
689 err = enlist_unique(add_p, value, 0);
698 /* remove multi string. */
700 struct list * remove_p = cur_action->multi_remove[action->index];
701 struct list * add_p = cur_action->multi_add[action->index];
703 if ( (value == NULL) || (*value == '\0')
704 || ((*value == '*') && (value[1] == '\0')) )
707 * no option, or option == "*".
711 list_remove_all(remove_p);
712 list_remove_all(add_p);
713 cur_action->multi_remove_all[action->index] = 1;
717 /* Valid option - remove only 1 option */
719 if ( !cur_action->multi_remove_all[action->index] )
721 /* there isn't a catch-all in the remove list already */
722 err = enlist_unique(remove_p, value, 0);
728 list_remove_item(add_p, value);
733 /* Shouldn't get here unless there's memory corruption. */
740 /* try user aliases. */
741 const struct action_alias * alias = alias_list;
743 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
750 merge_actions(cur_action, alias->action);
754 /* Bad action name */
756 * XXX: This is a fatal error and Privoxy will later on exit
757 * in load_one_actions_file() because of an "invalid line".
759 * It would be preferable to name the offending option in that
760 * error message, but currently there is no way to do that and
761 * we have to live with two error messages for basically the
764 log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
775 /*********************************************************************
777 * Function : init_current_action
779 * Description : Zero out an action.
782 * 1 : dest = An uninitialized current_action_spec.
786 *********************************************************************/
787 void init_current_action (struct current_action_spec *dest)
789 memset(dest, '\0', sizeof(*dest));
791 dest->flags = ACTION_MOST_COMPATIBLE;
795 /*********************************************************************
797 * Function : init_action
799 * Description : Zero out an action.
802 * 1 : dest = An uninitialized action_spec.
806 *********************************************************************/
807 void init_action (struct action_spec *dest)
809 memset(dest, '\0', sizeof(*dest));
813 /*********************************************************************
815 * Function : merge_current_action
817 * Description : Merge two actions together.
818 * Similar to "dest += src".
819 * Differences between this and merge_actions()
820 * is that this one doesn't allocate memory for
821 * strings (so "src" better be in memory for at least
822 * as long as "dest" is, and you'd better free
823 * "dest" using "free_current_action").
824 * Also, there is no mask or remove lists in dest.
825 * (If we're applying it to a URL, we don't need them)
828 * 1 : dest = Current actions, to modify.
829 * 2 : src = Action to add.
831 * Returns 0 : no error
832 * !=0 : error, probably JB_ERR_MEMORY.
834 *********************************************************************/
835 jb_err merge_current_action (struct current_action_spec *dest,
836 const struct action_spec *src)
839 jb_err err = JB_ERR_OK;
841 dest->flags &= src->mask;
842 dest->flags |= src->add;
844 for (i = 0; i < ACTION_STRING_COUNT; i++)
846 char * str = src->string[i];
852 return JB_ERR_MEMORY;
854 freez(dest->string[i]);
855 dest->string[i] = str;
859 for (i = 0; i < ACTION_MULTI_COUNT; i++)
861 if (src->multi_remove_all[i])
863 /* Remove everything from dest, then add src->multi_add */
864 err = list_duplicate(dest->multi[i], src->multi_add[i]);
872 list_remove_list(dest->multi[i], src->multi_remove[i]);
873 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
884 /*********************************************************************
886 * Function : update_action_bits_for_all_tags
888 * Description : Updates the action bits based on all matching tags.
891 * 1 : csp = Current client state (buffers, headers, etc...)
893 * Returns : 0 if no tag matched, or
896 *********************************************************************/
897 int update_action_bits_for_all_tags(struct client_state *csp)
899 struct list_entry *tag;
902 for (tag = csp->tags->first; tag != NULL; tag = tag->next)
904 if (update_action_bits_for_tag(csp, tag->str))
914 /*********************************************************************
916 * Function : update_action_bits_for_tag
918 * Description : Updates the action bits based on the action sections
919 * whose tag patterns match a provided tag.
922 * 1 : csp = Current client state (buffers, headers, etc...)
923 * 2 : tag = The tag on which the update should be based on
925 * Returns : 0 if no tag matched, or
928 *********************************************************************/
929 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
931 struct file_list *fl;
932 struct url_actions *b;
938 assert(list_contains_item(csp->tags, tag));
940 /* Run through all action files, */
941 for (i = 0; i < MAX_AF_FILES; i++)
943 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
945 /* Skip empty files */
949 /* and through all the action patterns, */
950 for (b = b->next; NULL != b; b = b->next)
952 /* skip the URL patterns, */
953 if (NULL == b->url->tag_regex)
958 /* and check if one of the tag patterns matches the tag, */
959 if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
961 /* if it does, update the action bit map, */
962 if (merge_current_action(csp->action, b->action))
964 log_error(LOG_LEVEL_ERROR,
965 "Out of memorey while changing action bits");
967 /* and signal the change. */
977 /*********************************************************************
979 * Function : free_current_action
981 * Description : Free memory used by a current_action_spec.
982 * Does not free the current_action_spec itself.
985 * 1 : src = Source to free.
989 *********************************************************************/
990 void free_current_action (struct current_action_spec *src)
994 for (i = 0; i < ACTION_STRING_COUNT; i++)
996 freez(src->string[i]);
999 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1001 destroy_list(src->multi[i]);
1004 memset(src, '\0', sizeof(*src));
1008 static struct file_list *current_actions_file[MAX_AF_FILES] = {
1009 NULL, NULL, NULL, NULL, NULL,
1010 NULL, NULL, NULL, NULL, NULL
1014 #ifdef FEATURE_GRACEFUL_TERMINATION
1015 /*********************************************************************
1017 * Function : unload_current_actions_file
1019 * Description : Unloads current actions file - reset to state at
1020 * beginning of program.
1026 *********************************************************************/
1027 void unload_current_actions_file(void)
1031 for (i = 0; i < MAX_AF_FILES; i++)
1033 if (current_actions_file[i])
1035 current_actions_file[i]->unloader = unload_actions_file;
1036 current_actions_file[i] = NULL;
1040 #endif /* FEATURE_GRACEFUL_TERMINATION */
1043 /*********************************************************************
1045 * Function : unload_actions_file
1047 * Description : Unloads an actions module.
1050 * 1 : file_data = the data structure associated with the
1055 *********************************************************************/
1056 void unload_actions_file(void *file_data)
1058 struct url_actions * next;
1059 struct url_actions * cur = (struct url_actions *)file_data;
1063 free_url_spec(cur->url);
1064 free_action(cur->action);
1072 /*********************************************************************
1074 * Function : free_alias_list
1076 * Description : Free memory used by a list of aliases.
1079 * 1 : alias_list = Linked list to free.
1083 *********************************************************************/
1084 void free_alias_list(struct action_alias *alias_list)
1086 while (alias_list != NULL)
1088 struct action_alias * next = alias_list->next;
1089 alias_list->next = NULL;
1090 freez(alias_list->name);
1091 free_action(alias_list->action);
1098 /*********************************************************************
1100 * Function : load_actions_file
1102 * Description : Read and parse all the action files and add to files
1106 * 1 : csp = Current client state (buffers, headers, etc...)
1108 * Returns : 0 => Ok, everything else is an error.
1110 *********************************************************************/
1111 int load_actions_file(struct client_state *csp)
1116 for (i = 0; i < MAX_AF_FILES; i++)
1118 if (csp->config->actions_file[i])
1120 result = load_one_actions_file(csp, i);
1126 else if (current_actions_file[i])
1128 current_actions_file[i]->unloader = unload_actions_file;
1129 current_actions_file[i] = NULL;
1136 /*********************************************************************
1138 * Function : load_one_actions_file
1140 * Description : Read and parse a action file and add to files
1144 * 1 : csp = Current client state (buffers, headers, etc...)
1145 * 2 : fileid = File index to load.
1147 * Returns : 0 => Ok, everything else is an error.
1149 *********************************************************************/
1150 static int load_one_actions_file(struct client_state *csp, int fileid)
1155 * Note: Keep these in the order they occur in the file, they are
1156 * sometimes tested with <=
1158 #define MODE_START_OF_FILE 1
1159 #define MODE_SETTINGS 2
1160 #define MODE_DESCRIPTION 3
1161 #define MODE_ALIAS 4
1162 #define MODE_ACTIONS 5
1164 int mode = MODE_START_OF_FILE;
1167 struct url_actions *last_perm;
1168 struct url_actions *perm;
1169 char buf[BUFFER_SIZE];
1170 struct file_list *fs;
1171 struct action_spec * cur_action = NULL;
1172 int cur_action_used = 0;
1173 struct action_alias * alias_list = NULL;
1174 unsigned long linenum = 0;
1176 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1178 /* No need to load */
1179 csp->actions_list[fileid] = current_actions_file[fileid];
1184 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1185 "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1186 "with their complete file names.", csp->config->actions_file[fileid]);
1187 return 1; /* never get here */
1190 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1191 if (last_perm == NULL)
1193 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1194 csp->config->actions_file[fileid]);
1195 return 1; /* never get here */
1198 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1200 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1201 csp->config->actions_file[fileid]);
1202 return 1; /* never get here */
1205 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1209 /* It's a header block */
1212 /* It's {{settings}} or {{alias}} */
1213 size_t len = strlen(buf);
1214 char * start = buf + 2;
1215 char * end = buf + len - 1;
1216 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1220 log_error(LOG_LEVEL_FATAL,
1221 "can't load actions file '%s': invalid line (%lu): %s",
1222 csp->config->actions_file[fileid], linenum, buf);
1223 return 1; /* never get here */
1226 /* Trim leading and trailing whitespace. */
1234 log_error(LOG_LEVEL_FATAL,
1235 "can't load actions file '%s': invalid line (%lu): {{ }}",
1236 csp->config->actions_file[fileid], linenum);
1237 return 1; /* never get here */
1241 * An actionsfile can optionally contain the following blocks.
1242 * They *MUST* be in this order, to simplify processing:
1248 * ...free text, format TBD, but no line may start with a '{'...
1253 * The actual actions must be *after* these special blocks.
1254 * None of these special blocks may be repeated.
1257 if (0 == strcmpic(start, "settings"))
1259 /* it's a {{settings}} block */
1260 if (mode >= MODE_SETTINGS)
1262 /* {{settings}} must be first thing in file and must only
1266 log_error(LOG_LEVEL_FATAL,
1267 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1268 csp->config->actions_file[fileid], linenum);
1270 mode = MODE_SETTINGS;
1272 else if (0 == strcmpic(start, "description"))
1274 /* it's a {{description}} block */
1275 if (mode >= MODE_DESCRIPTION)
1277 /* {{description}} is a singleton and only {{settings}} may proceed it
1280 log_error(LOG_LEVEL_FATAL,
1281 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1282 csp->config->actions_file[fileid], linenum);
1284 mode = MODE_DESCRIPTION;
1286 else if (0 == strcmpic(start, "alias"))
1288 /* it's an {{alias}} block */
1289 if (mode >= MODE_ALIAS)
1291 /* {{alias}} must be first thing in file, possibly after
1292 * {{settings}} and {{description}}
1294 * {{alias}} must only appear once.
1296 * Note that these are new restrictions introduced in
1297 * v2.9.10 in order to make actionsfile editing simpler.
1298 * (Otherwise, reordering actionsfile entries without
1299 * completely rewriting the file becomes non-trivial)
1302 log_error(LOG_LEVEL_FATAL,
1303 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1304 csp->config->actions_file[fileid], linenum);
1310 /* invalid {{something}} block */
1312 log_error(LOG_LEVEL_FATAL,
1313 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1314 csp->config->actions_file[fileid], linenum, start);
1315 return 1; /* never get here */
1320 /* It's an actions block */
1322 char actions_buf[BUFFER_SIZE];
1326 mode = MODE_ACTIONS;
1328 /* free old action */
1331 if (!cur_action_used)
1333 free_action(cur_action);
1338 cur_action_used = 0;
1339 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1340 if (cur_action == NULL)
1343 log_error(LOG_LEVEL_FATAL,
1344 "can't load actions file '%s': out of memory",
1345 csp->config->actions_file[fileid]);
1346 return 1; /* never get here */
1348 init_action(cur_action);
1351 strlcpy(actions_buf, buf + 1, sizeof(actions_buf));
1353 /* check we have a trailing } and then trim it */
1354 end = actions_buf + strlen(actions_buf) - 1;
1359 log_error(LOG_LEVEL_FATAL,
1360 "can't load actions file '%s': invalid line (%lu): %s",
1361 csp->config->actions_file[fileid], linenum, buf);
1362 return 1; /* never get here */
1366 /* trim any whitespace immediately inside {} */
1369 if (get_actions(actions_buf, alias_list, cur_action))
1373 log_error(LOG_LEVEL_FATAL,
1374 "can't load actions file '%s': invalid line (%lu): %s",
1375 csp->config->actions_file[fileid], linenum, buf);
1376 return 1; /* never get here */
1380 else if (mode == MODE_SETTINGS)
1383 * Part of the {{settings}} block.
1384 * For now only serves to check if the file's minimum Privoxy
1385 * version requirement is met, but we may want to read & check
1386 * permissions when we go multi-user.
1388 if (!strncmp(buf, "for-privoxy-version=", 20))
1390 char *version_string, *fields[3];
1393 if ((version_string = strdup(buf + 20)) == NULL)
1396 log_error(LOG_LEVEL_FATAL,
1397 "can't load actions file '%s': out of memory!",
1398 csp->config->actions_file[fileid]);
1399 return 1; /* never get here */
1402 num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1404 if (num_fields < 1 || atoi(fields[0]) == 0)
1406 log_error(LOG_LEVEL_ERROR,
1407 "While loading actions file '%s': invalid line (%lu): %s",
1408 csp->config->actions_file[fileid], linenum, buf);
1410 else if ( atoi(fields[0]) > VERSION_MAJOR
1411 || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1412 || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1415 log_error(LOG_LEVEL_FATAL,
1416 "Actions file '%s', line %lu requires newer Privoxy version: %s",
1417 csp->config->actions_file[fileid], linenum, buf );
1418 return 1; /* never get here */
1420 free(version_string);
1423 else if (mode == MODE_DESCRIPTION)
1426 * Part of the {{description}} block.
1430 else if (mode == MODE_ALIAS)
1435 char actions_buf[BUFFER_SIZE];
1436 struct action_alias * new_alias;
1438 char * start = strchr(buf, '=');
1441 if ((start == NULL) || (start == buf))
1443 log_error(LOG_LEVEL_FATAL,
1444 "can't load actions file '%s': invalid alias line (%lu): %s",
1445 csp->config->actions_file[fileid], linenum, buf);
1446 return 1; /* never get here */
1449 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1452 log_error(LOG_LEVEL_FATAL,
1453 "can't load actions file '%s': out of memory!",
1454 csp->config->actions_file[fileid]);
1455 return 1; /* never get here */
1458 /* Eat any the whitespace before the '=' */
1460 while ((*end == ' ') || (*end == '\t'))
1463 * we already know we must have at least 1 non-ws char
1464 * at start of buf - no need to check
1470 /* Eat any the whitespace after the '=' */
1472 while ((*start == ' ') || (*start == '\t'))
1478 log_error(LOG_LEVEL_FATAL,
1479 "can't load actions file '%s': invalid alias line (%lu): %s",
1480 csp->config->actions_file[fileid], linenum, buf);
1481 return 1; /* never get here */
1484 if ((new_alias->name = strdup(buf)) == NULL)
1487 log_error(LOG_LEVEL_FATAL,
1488 "can't load actions file '%s': out of memory!",
1489 csp->config->actions_file[fileid]);
1490 return 1; /* never get here */
1493 strlcpy(actions_buf, start, sizeof(actions_buf));
1495 if (get_actions(actions_buf, alias_list, new_alias->action))
1499 log_error(LOG_LEVEL_FATAL,
1500 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1501 csp->config->actions_file[fileid], linenum, buf, start);
1502 return 1; /* never get here */
1506 new_alias->next = alias_list;
1507 alias_list = new_alias;
1509 else if (mode == MODE_ACTIONS)
1511 /* it's a URL pattern */
1513 /* allocate a new node */
1514 if ((perm = zalloc(sizeof(*perm))) == NULL)
1517 log_error(LOG_LEVEL_FATAL,
1518 "can't load actions file '%s': out of memory!",
1519 csp->config->actions_file[fileid]);
1520 return 1; /* never get here */
1524 copy_action (perm->action, cur_action);
1526 /* Save the URL pattern */
1527 if (create_url_spec(perm->url, buf))
1530 log_error(LOG_LEVEL_FATAL,
1531 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1532 csp->config->actions_file[fileid], linenum, buf);
1533 return 1; /* never get here */
1536 /* add it to the list */
1537 last_perm->next = perm;
1540 else if (mode == MODE_START_OF_FILE)
1542 /* oops - please have a {} line as 1st line in file. */
1544 log_error(LOG_LEVEL_FATAL,
1545 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1546 csp->config->actions_file[fileid], linenum, buf);
1547 return 1; /* never get here */
1551 /* How did we get here? This is impossible! */
1553 log_error(LOG_LEVEL_FATAL,
1554 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1555 csp->config->actions_file[fileid], mode);
1556 return 1; /* never get here */
1562 free_action(cur_action);
1565 free_alias_list(alias_list);
1567 /* the old one is now obsolete */
1568 if (current_actions_file[fileid])
1570 current_actions_file[fileid]->unloader = unload_actions_file;
1573 fs->next = files->next;
1575 current_actions_file[fileid] = fs;
1577 csp->actions_list[fileid] = fs;
1584 /*********************************************************************
1586 * Function : actions_to_text
1588 * Description : Converts a actionsfile entry from the internal
1589 * structure into a text line. The output is split
1590 * into one line for each action with line continuation.
1593 * 1 : action = The action to format.
1595 * Returns : A string. Caller must free it.
1596 * NULL on out-of-memory error.
1598 *********************************************************************/
1599 char * actions_to_text(struct action_spec *action)
1601 unsigned mask = action->mask;
1602 unsigned add = action->add;
1603 char * result = strdup("");
1604 struct list_entry * lst;
1606 /* sanity - prevents "-feature +feature" */
1610 #define DEFINE_ACTION_BOOL(__name, __bit) \
1611 if (!(mask & __bit)) \
1613 string_append(&result, " -" __name " \\\n"); \
1615 else if (add & __bit) \
1617 string_append(&result, " +" __name " \\\n"); \
1620 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1621 if (!(mask & __bit)) \
1623 string_append(&result, " -" __name " \\\n"); \
1625 else if (add & __bit) \
1627 string_append(&result, " +" __name "{"); \
1628 string_append(&result, action->string[__index]); \
1629 string_append(&result, "} \\\n"); \
1632 #define DEFINE_ACTION_MULTI(__name, __index) \
1633 if (action->multi_remove_all[__index]) \
1635 string_append(&result, " -" __name " \\\n"); \
1639 lst = action->multi_remove[__index]->first; \
1642 string_append(&result, " -" __name "{"); \
1643 string_append(&result, lst->str); \
1644 string_append(&result, "} \\\n"); \
1648 lst = action->multi_add[__index]->first; \
1651 string_append(&result, " +" __name "{"); \
1652 string_append(&result, lst->str); \
1653 string_append(&result, "} \\\n"); \
1657 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1659 #include "actionlist.h"
1661 #undef DEFINE_ACTION_MULTI
1662 #undef DEFINE_ACTION_STRING
1663 #undef DEFINE_ACTION_BOOL
1664 #undef DEFINE_ACTION_ALIAS
1670 /*********************************************************************
1672 * Function : actions_to_html
1674 * Description : Converts a actionsfile entry from numeric form
1675 * ("mask" and "add") to a <br>-seperated HTML string
1676 * in which each action is linked to its chapter in
1680 * 1 : csp = Client state (for config)
1681 * 2 : action = Action spec to be converted
1683 * Returns : A string. Caller must free it.
1684 * NULL on out-of-memory error.
1686 *********************************************************************/
1687 char * actions_to_html(struct client_state *csp,
1688 struct action_spec *action)
1690 unsigned mask = action->mask;
1691 unsigned add = action->add;
1692 char * result = strdup("");
1693 struct list_entry * lst;
1695 /* sanity - prevents "-feature +feature" */
1699 #define DEFINE_ACTION_BOOL(__name, __bit) \
1700 if (!(mask & __bit)) \
1702 string_append(&result, "\n<br>-"); \
1703 string_join(&result, add_help_link(__name, csp->config)); \
1705 else if (add & __bit) \
1707 string_append(&result, "\n<br>+"); \
1708 string_join(&result, add_help_link(__name, csp->config)); \
1711 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1712 if (!(mask & __bit)) \
1714 string_append(&result, "\n<br>-"); \
1715 string_join(&result, add_help_link(__name, csp->config)); \
1717 else if (add & __bit) \
1719 string_append(&result, "\n<br>+"); \
1720 string_join(&result, add_help_link(__name, csp->config)); \
1721 string_append(&result, "{"); \
1722 string_join(&result, html_encode(action->string[__index])); \
1723 string_append(&result, "}"); \
1726 #define DEFINE_ACTION_MULTI(__name, __index) \
1727 if (action->multi_remove_all[__index]) \
1729 string_append(&result, "\n<br>-"); \
1730 string_join(&result, add_help_link(__name, csp->config)); \
1734 lst = action->multi_remove[__index]->first; \
1737 string_append(&result, "\n<br>-"); \
1738 string_join(&result, add_help_link(__name, csp->config)); \
1739 string_append(&result, "{"); \
1740 string_join(&result, html_encode(lst->str)); \
1741 string_append(&result, "}"); \
1745 lst = action->multi_add[__index]->first; \
1748 string_append(&result, "\n<br>+"); \
1749 string_join(&result, add_help_link(__name, csp->config)); \
1750 string_append(&result, "{"); \
1751 string_join(&result, html_encode(lst->str)); \
1752 string_append(&result, "}"); \
1756 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1758 #include "actionlist.h"
1760 #undef DEFINE_ACTION_MULTI
1761 #undef DEFINE_ACTION_STRING
1762 #undef DEFINE_ACTION_BOOL
1763 #undef DEFINE_ACTION_ALIAS
1765 /* trim leading <br> */
1766 if (result && *result)
1769 result = strdup(result + 5);
1777 /*********************************************************************
1779 * Function : current_actions_to_html
1781 * Description : Converts a curren action spec to a <br> seperated HTML
1782 * text in which each action is linked to its chapter in
1786 * 1 : csp = Client state (for config)
1787 * 2 : action = Current action spec to be converted
1789 * Returns : A string. Caller must free it.
1790 * NULL on out-of-memory error.
1792 *********************************************************************/
1793 char *current_action_to_html(struct client_state *csp,
1794 struct current_action_spec *action)
1796 unsigned long flags = action->flags;
1797 char * result = strdup("");
1798 struct list_entry * lst;
1800 #define DEFINE_ACTION_BOOL(__name, __bit) \
1801 if (flags & __bit) \
1803 string_append(&result, "\n<br>+"); \
1804 string_join(&result, add_help_link(__name, csp->config)); \
1808 string_append(&result, "\n<br>-"); \
1809 string_join(&result, add_help_link(__name, csp->config)); \
1812 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1813 if (flags & __bit) \
1815 string_append(&result, "\n<br>+"); \
1816 string_join(&result, add_help_link(__name, csp->config)); \
1817 string_append(&result, "{"); \
1818 string_join(&result, html_encode(action->string[__index])); \
1819 string_append(&result, "}"); \
1823 string_append(&result, "\n<br>-"); \
1824 string_join(&result, add_help_link(__name, csp->config)); \
1827 #define DEFINE_ACTION_MULTI(__name, __index) \
1828 lst = action->multi[__index]->first; \
1831 string_append(&result, "\n<br>-"); \
1832 string_join(&result, add_help_link(__name, csp->config)); \
1838 string_append(&result, "\n<br>+"); \
1839 string_join(&result, add_help_link(__name, csp->config)); \
1840 string_append(&result, "{"); \
1841 string_join(&result, html_encode(lst->str)); \
1842 string_append(&result, "}"); \
1847 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1849 #include "actionlist.h"
1851 #undef DEFINE_ACTION_MULTI
1852 #undef DEFINE_ACTION_STRING
1853 #undef DEFINE_ACTION_BOOL
1854 #undef DEFINE_ACTION_ALIAS