Remove kill-popups action.
[privoxy.git] / actions.c
1 const char actions_rcs[] = "$Id: actions.c,v 1.45 2008/03/24 11:21:02 fabiankeil Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/actions.c,v $
5  *
6  * Purpose     :  Declares functions to work with actions files
7  *                Functions declared include: FIXME
8  *
9  * Copyright   :  Written by and Copyright (C) 2001-2008 the SourceForge
10  *                Privoxy team. http://www.privoxy.org/
11  *
12  *                Based on the Internet Junkbuster originally written
13  *                by and Copyright (C) 1997 Anonymous Coders and
14  *                Junkbusters Corporation.  http://www.junkbusters.com
15  *
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.
21  *
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.
27  *
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.
33  *
34  * Revisions   :
35  *    $Log: actions.c,v $
36  *    Revision 1.45  2008/03/24 11:21:02  fabiankeil
37  *    Share the action settings for multiple patterns in the same
38  *    section so we waste less memory for gigantic block lists
39  *    (and load them slightly faster). Reported by Franz Schwartau.
40  *
41  *    Revision 1.44  2008/03/04 18:30:34  fabiankeil
42  *    Remove the treat-forbidden-connects-like-blocks action. We now
43  *    use the "blocked" page for forbidden CONNECT requests by default.
44  *
45  *    Revision 1.43  2008/03/01 14:00:43  fabiankeil
46  *    Let the block action take the reason for the block
47  *    as argument and show it on the "blocked" page.
48  *
49  *    Revision 1.42  2008/02/09 15:15:38  fabiankeil
50  *    List active and inactive actions in the show-url-info's
51  *    "Final results" section separately. Patch submitted by Lee
52  *    in #1830056, modified to list active actions first.
53  *
54  *    Revision 1.41  2008/01/28 20:17:40  fabiankeil
55  *    - Mark some parameters as immutable.
56  *    - Hide update_action_bits_for_all_tags() while it's unused.
57  *
58  *    Revision 1.40  2007/05/21 10:26:50  fabiankeil
59  *    - Use strlcpy() instead of strcpy().
60  *    - Provide a reason why loading the actions
61  *      file might have failed.
62  *
63  *    Revision 1.39  2007/04/17 18:21:45  fabiankeil
64  *    Split update_action_bits() into
65  *    update_action_bits_for_all_tags()
66  *    and update_action_bits_for_tag().
67  *
68  *    Revision 1.38  2007/04/15 16:39:20  fabiankeil
69  *    Introduce tags as alternative way to specify which
70  *    actions apply to a request. At the moment tags can be
71  *    created based on client and server headers.
72  *
73  *    Revision 1.37  2007/03/11 15:56:12  fabiankeil
74  *    Add kludge to log unknown aliases and actions before exiting.
75  *
76  *    Revision 1.36  2006/12/28 17:15:42  fabiankeil
77  *    Fix gcc43 conversion warning.
78  *
79  *    Revision 1.35  2006/07/18 14:48:45  david__schmidt
80  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
81  *    with what was really the latest development (the v_3_0_branch branch)
82  *
83  *    Revision 1.32.2.6  2006/01/29 23:10:56  david__schmidt
84  *    Multiple filter file support
85  *
86  *    Revision 1.32.2.5  2005/06/09 01:18:41  david__schmidt
87  *    Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
88  *    this becomes important when jcc.h gets included later down the line.
89  *
90  *    Revision 1.32.2.4  2003/12/03 10:33:11  oes
91  *    - Implemented Privoxy version requirement through
92  *      for-privoxy-version= statement in {{settings}}
93  *      block
94  *    - Fix for unchecked out-of-memory condition
95  *
96  *    Revision 1.32.2.3  2003/02/28 12:52:10  oes
97  *    Fixed memory leak reported by Dan Price in Bug #694713
98  *
99  *    Revision 1.32.2.2  2002/11/20 14:36:55  oes
100  *    Extended unload_current_actions_file() to multiple AFs.
101  *    Thanks to Oliver Stoeneberg for the hint.
102  *
103  *    Revision 1.32.2.1  2002/05/26 12:13:16  roro
104  *    Change unsigned to unsigned long in actions_name struct.  This closes
105  *    SourceForge Bug #539284.
106  *
107  *    Revision 1.32  2002/05/12 21:36:29  jongfoster
108  *    Correcting function comments
109  *
110  *    Revision 1.31  2002/05/06 07:56:50  oes
111  *    Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
112  *
113  *    Revision 1.30  2002/04/30 11:14:52  oes
114  *    Made csp the first parameter in *action_to_html
115  *
116  *    Revision 1.29  2002/04/26 19:30:54  jongfoster
117  *    - current_action_to_html(): Adding help link for the "-" form of
118  *      one-string actions.
119  *    - Some actions had "<br>-", some "<br> -" (note the space).
120  *      Standardizing on no space.
121  *    - Greatly simplifying some of the code by using string_join()
122  *      where appropriate.
123  *
124  *    Revision 1.28  2002/04/26 12:53:15  oes
125  *     - CGI AF editor now writes action lines split into
126  *       single lines with line continuation
127  *     - actions_to_html now embeds each action name in
128  *       link to chapter
129  *     - current_action_to_text is now called current_action_to_html
130  *       and acts like actions_to_html
131  *
132  *    Revision 1.27  2002/04/24 02:10:31  oes
133  *     - Jon's patch for multiple AFs:
134  *       - split load_actions_file, add load_one_actions_file
135  *       - make csp->actions_list files an array
136  *       - remember file id with each action
137  *     - Copy_action now frees dest action before copying
138  *
139  *    Revision 1.26  2002/03/26 22:29:54  swa
140  *    we have a new homepage!
141  *
142  *    Revision 1.25  2002/03/24 13:25:43  swa
143  *    name change related issues
144  *
145  *    Revision 1.24  2002/03/16 23:54:06  jongfoster
146  *    Adding graceful termination feature, to help look for memory leaks.
147  *    If you enable this (which, by design, has to be done by hand
148  *    editing config.h) and then go to http://i.j.b/die, then the program
149  *    will exit cleanly after the *next* request.  It should free all the
150  *    memory that was used.
151  *
152  *    Revision 1.23  2002/03/07 03:46:16  oes
153  *    Fixed compiler warnings
154  *
155  *    Revision 1.22  2002/01/21 00:27:02  jongfoster
156  *    Allowing free_action(NULL).
157  *    Moving the functions that #include actionlist.h to the end of the file,
158  *    because the Visual C++ 97 debugger gets extremely confused if you try
159  *    to debug any code that comes after them in the file.
160  *
161  *    Revision 1.21  2002/01/17 20:54:44  jongfoster
162  *    Renaming free_url to free_url_spec, since it frees a struct url_spec.
163  *
164  *    Revision 1.20  2001/11/22 21:56:49  jongfoster
165  *    Making action_spec->flags into an unsigned long rather than just an
166  *    unsigned int.
167  *    Fixing a bug in the display of -add-header and -wafer
168  *
169  *    Revision 1.19  2001/11/13 00:14:07  jongfoster
170  *    Fixing stupid bug now I've figured out what || means.
171  *    (It always returns 0 or 1, not one of it's paramaters.)
172  *
173  *    Revision 1.18  2001/11/07 00:06:06  steudten
174  *    Add line number in error output for lineparsing for
175  *    actionsfile.
176  *
177  *    Revision 1.17  2001/10/25 03:40:47  david__schmidt
178  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
179  *    threads to call select() simultaneously.  So, it's time to do a real, live,
180  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
181  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
182  *
183  *    Revision 1.16  2001/10/23 21:30:30  jongfoster
184  *    Adding error-checking to selected functions.
185  *
186  *    Revision 1.15  2001/10/14 21:58:22  jongfoster
187  *    Adding support for the CGI-based editor:
188  *    - Exported get_actions()
189  *    - Added new function free_alias_list()
190  *    - Added support for {{settings}} and {{description}} blocks
191  *      in the actions file.  They are currently ignored.
192  *    - Added restriction to only one {{alias}} block which must appear
193  *      first in the file, to simplify the editor's rewriting rules.
194  *    - Note that load_actions_file() is no longer used by the CGI-based
195  *      editor, but some of the other routines in this file are.
196  *
197  *    Revision 1.14  2001/09/22 16:36:59  jongfoster
198  *    Removing unused parameter fs from read_config_line()
199  *
200  *    Revision 1.13  2001/09/16 15:47:37  jongfoster
201  *    First version of CGI-based edit interface.  This is very much a
202  *    work-in-progress, and you can't actually use it to edit anything
203  *    yet.  You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
204  *    to have any effect.
205  *
206  *    Revision 1.12  2001/09/16 13:21:27  jongfoster
207  *    Changes to use new list functions.
208  *
209  *    Revision 1.11  2001/09/14 00:17:32  jongfoster
210  *    Tidying up memory allocation. New function init_action().
211  *
212  *    Revision 1.10  2001/09/10 10:14:34  oes
213  *    Removing unused variable
214  *
215  *    Revision 1.9  2001/07/30 22:08:36  jongfoster
216  *    Tidying up #defines:
217  *    - All feature #defines are now of the form FEATURE_xxx
218  *    - Permanently turned off WIN_GUI_EDIT
219  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
220  *
221  *    Revision 1.8  2001/06/29 13:19:52  oes
222  *    Removed logentry from cancelled commit
223  *
224  *    Revision 1.7  2001/06/09 10:55:28  jongfoster
225  *    Changing BUFSIZ ==> BUFFER_SIZE
226  *
227  *    Revision 1.6  2001/06/07 23:04:34  jongfoster
228  *    Made get_actions() static.
229  *
230  *    Revision 1.5  2001/06/03 19:11:48  oes
231  *    adapted to new enlist_unique arg format
232  *
233  *    Revision 1.4  2001/06/01 20:03:42  jongfoster
234  *    Better memory management - current_action->strings[] now
235  *    contains copies of the strings, not the original.
236  *
237  *    Revision 1.3  2001/06/01 18:49:17  jongfoster
238  *    Replaced "list_share" with "list" - the tiny memory gain was not
239  *    worth the extra complexity.
240  *
241  *    Revision 1.2  2001/05/31 21:40:00  jongfoster
242  *    Removing some commented out, obsolete blocks of code.
243  *
244  *    Revision 1.1  2001/05/31 21:16:46  jongfoster
245  *    Moved functions to process the action list into this new file.
246  *
247  *
248  *********************************************************************/
249 \f
250
251 #include "config.h"
252
253 #include <stdio.h>
254 #include <string.h>
255 #include <assert.h>
256 #include <stdlib.h>
257
258 #ifdef FEATURE_PTHREAD
259 #include <pthread.h>
260 #endif
261
262 #include "project.h"
263 #include "jcc.h"
264 #include "list.h"
265 #include "actions.h"
266 #include "miscutil.h"
267 #include "errlog.h"
268 #include "loaders.h"
269 #include "encode.h"
270 #include "urlmatch.h"
271 #include "cgi.h"
272 #include "ssplit.h"
273
274 const char actions_h_rcs[] = ACTIONS_H_VERSION;
275
276
277 /*
278  * We need the main list of options.
279  *
280  * First, we need a way to tell between boolean, string, and multi-string
281  * options.  For string and multistring options, we also need to be
282  * able to tell the difference between a "+" and a "-".  (For bools,
283  * the "+"/"-" information is encoded in "add" and "mask").  So we use
284  * an enumerated type (well, the preprocessor equivalent).  Here are
285  * the values:
286  */
287 #define AV_NONE       0 /* +opt -opt */
288 #define AV_ADD_STRING 1 /* +stropt{string} */
289 #define AV_REM_STRING 2 /* -stropt */
290 #define AV_ADD_MULTI  3 /* +multiopt{string} +multiopt{string2} */
291 #define AV_REM_MULTI  4 /* -multiopt{string} -multiopt          */
292
293 /*
294  * We need a structure to hold the name, flag changes,
295  * type, and string index.
296  */
297 struct action_name
298 {
299    const char * name;
300    unsigned long mask;   /* a bit set to "0" = remove action */
301    unsigned long add;    /* a bit set to "1" = add action */
302    int takes_value;      /* an AV_... constant */
303    int index;            /* index into strings[] or multi[] */
304 };
305
306 /*
307  * And with those building blocks in place, here's the array.
308  */
309 static const struct action_name action_names[] =
310 {
311    /*
312     * Well actually there's no data here - it's in actionlist.h
313     * This keeps it together to make it easy to change.
314     *
315     * Here's the macros used to format it:
316     */
317 #define DEFINE_ACTION_MULTI(name,index)                   \
318    { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
319    { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
320 #define DEFINE_ACTION_STRING(name,flag,index)                 \
321    { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
322    { "-" name, ~flag, 0, AV_REM_STRING, index },
323 #define DEFINE_ACTION_BOOL(name,flag)   \
324    { "+" name, ACTION_MASK_ALL, flag }, \
325    { "-" name, ~flag, 0 },
326 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
327
328 #include "actionlist.h"
329
330 #undef DEFINE_ACTION_MULTI
331 #undef DEFINE_ACTION_STRING
332 #undef DEFINE_ACTION_BOOL
333 #undef DEFINE_ACTION_ALIAS
334
335    { NULL, 0, 0 } /* End marker */
336 };
337
338
339 static int load_one_actions_file(struct client_state *csp, int fileid);
340
341
342 /*********************************************************************
343  *
344  * Function    :  merge_actions
345  *
346  * Description :  Merge two actions together.
347  *                Similar to "dest += src".
348  *
349  * Parameters  :
350  *          1  :  dest = Actions to modify.
351  *          2  :  src = Action to add.
352  *
353  * Returns     :  JB_ERR_OK or JB_ERR_MEMORY
354  *
355  *********************************************************************/
356 jb_err merge_actions (struct action_spec *dest,
357                       const struct action_spec *src)
358 {
359    int i;
360    jb_err err;
361
362    dest->mask &= src->mask;
363    dest->add  &= src->mask;
364    dest->add  |= src->add;
365
366    for (i = 0; i < ACTION_STRING_COUNT; i++)
367    {
368       char * str = src->string[i];
369       if (str)
370       {
371          freez(dest->string[i]);
372          dest->string[i] = strdup(str);
373          if (NULL == dest->string[i])
374          {
375             return JB_ERR_MEMORY;
376          }
377       }
378    }
379
380    for (i = 0; i < ACTION_MULTI_COUNT; i++)
381    {
382       if (src->multi_remove_all[i])
383       {
384          /* Remove everything from dest */
385          list_remove_all(dest->multi_remove[i]);
386          dest->multi_remove_all[i] = 1;
387
388          err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
389       }
390       else if (dest->multi_remove_all[i])
391       {
392          /*
393           * dest already removes everything, so we only need to worry
394           * about what we add.
395           */
396          list_remove_list(dest->multi_add[i], src->multi_remove[i]);
397          err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
398       }
399       else
400       {
401          /* No "remove all"s to worry about. */
402          list_remove_list(dest->multi_add[i], src->multi_remove[i]);
403          err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
404          if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
405       }
406
407       if (err)
408       {
409          return err;
410       }
411    }
412
413    return JB_ERR_OK;
414 }
415
416
417 /*********************************************************************
418  *
419  * Function    :  copy_action
420  *
421  * Description :  Copy an action_specs.
422  *                Similar to "dest = src".
423  *
424  * Parameters  :
425  *          1  :  dest = Destination of copy.
426  *          2  :  src = Source for copy.
427  *
428  * Returns     :  N/A
429  *
430  *********************************************************************/
431 jb_err copy_action (struct action_spec *dest,
432                     const struct action_spec *src)
433 {
434    int i;
435    jb_err err = JB_ERR_OK;
436
437    free_action(dest);
438    memset(dest, '\0', sizeof(*dest));
439
440    dest->mask = src->mask;
441    dest->add  = src->add;
442
443    for (i = 0; i < ACTION_STRING_COUNT; i++)
444    {
445       char * str = src->string[i];
446       if (str)
447       {
448          str = strdup(str);
449          if (!str)
450          {
451             return JB_ERR_MEMORY;
452          }
453          dest->string[i] = str;
454       }
455    }
456
457    for (i = 0; i < ACTION_MULTI_COUNT; i++)
458    {
459       dest->multi_remove_all[i] = src->multi_remove_all[i];
460       err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
461       if (err)
462       {
463          return err;
464       }
465       err = list_duplicate(dest->multi_add[i],    src->multi_add[i]);
466       if (err)
467       {
468          return err;
469       }
470    }
471    return err;
472 }
473
474 /*********************************************************************
475  *
476  * Function    :  free_action_spec
477  *
478  * Description :  Frees an action_spec and the memory used by it.
479  *
480  * Parameters  :
481  *          1  :  src = Source to free.
482  *
483  * Returns     :  N/A
484  *
485  *********************************************************************/
486 void free_action_spec(struct action_spec *src)
487 {
488    free_action(src);
489    freez(src);
490 }
491
492
493 /*********************************************************************
494  *
495  * Function    :  free_action
496  *
497  * Description :  Destroy an action_spec.  Frees memory used by it,
498  *                except for the memory used by the struct action_spec
499  *                itself.
500  *
501  * Parameters  :
502  *          1  :  src = Source to free.
503  *
504  * Returns     :  N/A
505  *
506  *********************************************************************/
507 void free_action (struct action_spec *src)
508 {
509    int i;
510
511    if (src == NULL)
512    {
513       return;
514    }
515
516    for (i = 0; i < ACTION_STRING_COUNT; i++)
517    {
518       freez(src->string[i]);
519    }
520
521    for (i = 0; i < ACTION_MULTI_COUNT; i++)
522    {
523       destroy_list(src->multi_remove[i]);
524       destroy_list(src->multi_add[i]);
525    }
526
527    memset(src, '\0', sizeof(*src));
528 }
529
530
531 /*********************************************************************
532  *
533  * Function    :  get_action_token
534  *
535  * Description :  Parses a line for the first action.
536  *                Modifies it's input array, doesn't allocate memory.
537  *                e.g. given:
538  *                *line="  +abc{def}  -ghi "
539  *                Returns:
540  *                *line="  -ghi "
541  *                *name="+abc"
542  *                *value="def"
543  *
544  * Parameters  :
545  *          1  :  line = [in] The line containing the action.
546  *                       [out] Start of next action on line, or
547  *                       NULL if we reached the end of line before
548  *                       we found an action.
549  *          2  :  name = [out] Start of action name, null
550  *                       terminated.  NULL on EOL
551  *          3  :  value = [out] Start of action value, null
552  *                        terminated.  NULL if none or EOL.
553  *
554  * Returns     :  JB_ERR_OK => Ok
555  *                JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
556  *
557  *********************************************************************/
558 jb_err get_action_token(char **line, char **name, char **value)
559 {
560    char * str = *line;
561    char ch;
562
563    /* set default returns */
564    *line = NULL;
565    *name = NULL;
566    *value = NULL;
567
568    /* Eat any leading whitespace */
569    while ((*str == ' ') || (*str == '\t'))
570    {
571       str++;
572    }
573
574    if (*str == '\0')
575    {
576       return 0;
577    }
578
579    if (*str == '{')
580    {
581       /* null name, just value is prohibited */
582       return JB_ERR_PARSE;
583    }
584
585    *name = str;
586
587    /* parse option */
588    while (((ch = *str) != '\0') &&
589           (ch != ' ') && (ch != '\t') && (ch != '{'))
590    {
591       if (ch == '}')
592       {
593          /* error, '}' without '{' */
594          return JB_ERR_PARSE;
595       }
596       str++;
597    }
598    *str = '\0';
599
600    if (ch != '{')
601    {
602       /* no value */
603       if (ch == '\0')
604       {
605          /* EOL - be careful not to run off buffer */
606          *line = str;
607       }
608       else
609       {
610          /* More to parse next time. */
611          *line = str + 1;
612       }
613       return JB_ERR_OK;
614    }
615
616    str++;
617    *value = str;
618
619    str = strchr(str, '}');
620    if (str == NULL)
621    {
622       /* error */
623       *value = NULL;
624       return JB_ERR_PARSE;
625    }
626
627    /* got value */
628    *str = '\0';
629    *line = str + 1;
630
631    chomp(*value);
632
633    return JB_ERR_OK;
634 }
635
636 /*********************************************************************
637  *
638  * Function    :  action_used_to_valid_
639  *
640  * Description :  Checks if unrecognized actions were valid in earlier
641  *                releases.
642  *
643  * Parameters  :
644  *          1  :  action = The string containing the action to check.
645  *
646  * Returns     :  True if yes, otherwise false.
647  *
648  *********************************************************************/
649 static int action_used_to_be_valid(const char *action)
650 {
651    return (0 == strcmpic(action, "treat-forbidden-connects-like-blocks")
652         || 0 == strcmpic(action, "kill-popups"));
653 }
654
655 /*********************************************************************
656  *
657  * Function    :  get_actions
658  *
659  * Description :  Parses a list of actions.
660  *
661  * Parameters  :
662  *          1  :  line = The string containing the actions.
663  *                       Will be written to by this function.
664  *          2  :  alias_list = Custom alias list, or NULL for none.
665  *          3  :  cur_action = Where to store the action.  Caller
666  *                             allocates memory.
667  *
668  * Returns     :  JB_ERR_OK => Ok
669  *                JB_ERR_PARSE => Parse error (line was trashed anyway)
670  *                nonzero => Out of memory (line was trashed anyway)
671  *
672  *********************************************************************/
673 jb_err get_actions(char *line,
674                    struct action_alias * alias_list,
675                    struct action_spec *cur_action)
676 {
677    jb_err err;
678    init_action(cur_action);
679    cur_action->mask = ACTION_MASK_ALL;
680
681    while (line)
682    {
683       char * option = NULL;
684       char * value = NULL;
685
686       err = get_action_token(&line, &option, &value);
687       if (err)
688       {
689          return err;
690       }
691
692       if (option)
693       {
694          /* handle option in 'option' */
695
696          /* Check for standard action name */
697          const struct action_name * action = action_names;
698
699          while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
700          {
701             action++;
702          }
703          if (action->name != NULL)
704          {
705             /* Found it */
706             cur_action->mask &= action->mask;
707             cur_action->add  &= action->mask;
708             cur_action->add  |= action->add;
709
710             switch (action->takes_value)
711             {
712             case AV_NONE:
713                /* ignore any option. */
714                break;
715             case AV_ADD_STRING:
716                {
717                   /* add single string. */
718
719                   if ((value == NULL) || (*value == '\0'))
720                   {
721                      if (0 != strcmpic(action->name, "block"))
722                      {
723                         /*
724                          * XXX: Temporary backwards compatibility hack.
725                          */
726                         static int complaint_shown = 0;
727                         value = "No reason specified.";
728                         if (!complaint_shown)
729                         {
730                            log_error(LOG_LEVEL_ERROR, "At least one block "
731                               "without reason found. This may become a fatal "
732                               "error in future versions.");
733                            complaint_shown = 1;
734                         }
735                      }
736                      else
737                      {
738                         return JB_ERR_PARSE;
739                      }
740                   }
741                   /* FIXME: should validate option string here */
742                   freez (cur_action->string[action->index]);
743                   cur_action->string[action->index] = strdup(value);
744                   if (NULL == cur_action->string[action->index])
745                   {
746                      return JB_ERR_MEMORY;
747                   }
748                   break;
749                }
750             case AV_REM_STRING:
751                {
752                   /* remove single string. */
753
754                   freez (cur_action->string[action->index]);
755                   break;
756                }
757             case AV_ADD_MULTI:
758                {
759                   /* append multi string. */
760
761                   struct list * remove_p = cur_action->multi_remove[action->index];
762                   struct list * add_p    = cur_action->multi_add[action->index];
763
764                   if ((value == NULL) || (*value == '\0'))
765                   {
766                      return JB_ERR_PARSE;
767                   }
768
769                   list_remove_item(remove_p, value);
770                   err = enlist_unique(add_p, value, 0);
771                   if (err)
772                   {
773                      return err;
774                   }
775                   break;
776                }
777             case AV_REM_MULTI:
778                {
779                   /* remove multi string. */
780
781                   struct list * remove_p = cur_action->multi_remove[action->index];
782                   struct list * add_p    = cur_action->multi_add[action->index];
783
784                   if ( (value == NULL) || (*value == '\0')
785                      || ((*value == '*') && (value[1] == '\0')) )
786                   {
787                      /*
788                       * no option, or option == "*".
789                       *
790                       * Remove *ALL*.
791                       */
792                      list_remove_all(remove_p);
793                      list_remove_all(add_p);
794                      cur_action->multi_remove_all[action->index] = 1;
795                   }
796                   else
797                   {
798                      /* Valid option - remove only 1 option */
799
800                      if ( !cur_action->multi_remove_all[action->index] )
801                      {
802                         /* there isn't a catch-all in the remove list already */
803                         err = enlist_unique(remove_p, value, 0);
804                         if (err)
805                         {
806                            return err;
807                         }
808                      }
809                      list_remove_item(add_p, value);
810                   }
811                   break;
812                }
813             default:
814                /* Shouldn't get here unless there's memory corruption. */
815                assert(0);
816                return JB_ERR_PARSE;
817             }
818          }
819          else
820          {
821             /* try user aliases. */
822             const struct action_alias * alias = alias_list;
823
824             while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
825             {
826                alias = alias->next;
827             }
828             if (alias != NULL)
829             {
830                /* Found it */
831                merge_actions(cur_action, alias->action);
832             }
833             else if ((2 < strlen(option)) && action_used_to_be_valid(option+1))
834             {
835                log_error(LOG_LEVEL_ERROR, "Action '%s' is no longer valid "
836                   "in this Privoxy release. Ignored.", option+1);
837             }
838             else
839             {
840                /* Bad action name */
841                /*
842                 * XXX: This is a fatal error and Privoxy will later on exit
843                 * in load_one_actions_file() because of an "invalid line".
844                 *
845                 * It would be preferable to name the offending option in that
846                 * error message, but currently there is no way to do that and
847                 * we have to live with two error messages for basically the
848                 * same reason.
849                 */
850                log_error(LOG_LEVEL_ERROR, "Unknown action or alias: %s", option);
851                return JB_ERR_PARSE;
852             }
853          }
854       }
855    }
856
857    return JB_ERR_OK;
858 }
859
860
861 /*********************************************************************
862  *
863  * Function    :  init_current_action
864  *
865  * Description :  Zero out an action.
866  *
867  * Parameters  :
868  *          1  :  dest = An uninitialized current_action_spec.
869  *
870  * Returns     :  N/A
871  *
872  *********************************************************************/
873 void init_current_action (struct current_action_spec *dest)
874 {
875    memset(dest, '\0', sizeof(*dest));
876
877    dest->flags = ACTION_MOST_COMPATIBLE;
878 }
879
880
881 /*********************************************************************
882  *
883  * Function    :  init_action
884  *
885  * Description :  Zero out an action.
886  *
887  * Parameters  :
888  *          1  :  dest = An uninitialized action_spec.
889  *
890  * Returns     :  N/A
891  *
892  *********************************************************************/
893 void init_action (struct action_spec *dest)
894 {
895    memset(dest, '\0', sizeof(*dest));
896 }
897
898
899 /*********************************************************************
900  *
901  * Function    :  merge_current_action
902  *
903  * Description :  Merge two actions together.
904  *                Similar to "dest += src".
905  *                Differences between this and merge_actions()
906  *                is that this one doesn't allocate memory for
907  *                strings (so "src" better be in memory for at least
908  *                as long as "dest" is, and you'd better free
909  *                "dest" using "free_current_action").
910  *                Also, there is no  mask or remove lists in dest.
911  *                (If we're applying it to a URL, we don't need them)
912  *
913  * Parameters  :
914  *          1  :  dest = Current actions, to modify.
915  *          2  :  src = Action to add.
916  *
917  * Returns  0  :  no error
918  *        !=0  :  error, probably JB_ERR_MEMORY.
919  *
920  *********************************************************************/
921 jb_err merge_current_action (struct current_action_spec *dest,
922                              const struct action_spec *src)
923 {
924    int i;
925    jb_err err = JB_ERR_OK;
926
927    dest->flags  &= src->mask;
928    dest->flags  |= src->add;
929
930    for (i = 0; i < ACTION_STRING_COUNT; i++)
931    {
932       char * str = src->string[i];
933       if (str)
934       {
935          str = strdup(str);
936          if (!str)
937          {
938             return JB_ERR_MEMORY;
939          }
940          freez(dest->string[i]);
941          dest->string[i] = str;
942       }
943    }
944
945    for (i = 0; i < ACTION_MULTI_COUNT; i++)
946    {
947       if (src->multi_remove_all[i])
948       {
949          /* Remove everything from dest, then add src->multi_add */
950          err = list_duplicate(dest->multi[i], src->multi_add[i]);
951          if (err)
952          {
953             return err;
954          }
955       }
956       else
957       {
958          list_remove_list(dest->multi[i], src->multi_remove[i]);
959          err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
960          if (err)
961          {
962             return err;
963          }
964       }
965    }
966    return err;
967 }
968
969 #if 0
970 /*********************************************************************
971  *
972  * Function    :  update_action_bits_for_all_tags
973  *
974  * Description :  Updates the action bits based on all matching tags.
975  *
976  * Parameters  :
977  *          1  :  csp = Current client state (buffers, headers, etc...)
978  *
979  * Returns     :  0 if no tag matched, or
980  *                1 otherwise
981  *
982  *********************************************************************/
983 int update_action_bits_for_all_tags(struct client_state *csp)
984 {
985    struct list_entry *tag;
986    int updated = 0;
987
988    for (tag = csp->tags->first; tag != NULL; tag = tag->next)
989    {
990       if (update_action_bits_for_tag(csp, tag->str))
991       {
992          updated = 1;
993       }
994    }
995
996    return updated;
997 }
998 #endif
999
1000 /*********************************************************************
1001  *
1002  * Function    :  update_action_bits_for_tag
1003  *
1004  * Description :  Updates the action bits based on the action sections
1005  *                whose tag patterns match a provided tag.
1006  *
1007  * Parameters  :
1008  *          1  :  csp = Current client state (buffers, headers, etc...)
1009  *          2  :  tag = The tag on which the update should be based on
1010  *
1011  * Returns     :  0 if no tag matched, or
1012  *                1 otherwise
1013  *
1014  *********************************************************************/
1015 int update_action_bits_for_tag(struct client_state *csp, const char *tag)
1016 {
1017    struct file_list *fl;
1018    struct url_actions *b;
1019
1020    int updated = 0;
1021    int i;
1022
1023    assert(tag);
1024    assert(list_contains_item(csp->tags, tag));
1025
1026    /* Run through all action files, */
1027    for (i = 0; i < MAX_AF_FILES; i++)
1028    {
1029       if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
1030       {
1031          /* Skip empty files */
1032          continue;
1033       }
1034
1035       /* and through all the action patterns, */
1036       for (b = b->next; NULL != b; b = b->next)
1037       {
1038          /* skip the URL patterns, */
1039          if (NULL == b->url->tag_regex)
1040          {
1041             continue;
1042          }
1043
1044          /* and check if one of the tag patterns matches the tag, */
1045          if (0 == regexec(b->url->tag_regex, tag, 0, NULL, 0))
1046          {
1047             /* if it does, update the action bit map, */
1048             if (merge_current_action(csp->action, b->action))
1049             {
1050                log_error(LOG_LEVEL_ERROR,
1051                   "Out of memorey while changing action bits");
1052             }
1053             /* and signal the change. */
1054             updated = 1;
1055          }
1056       }
1057    }
1058
1059    return updated;
1060 }
1061
1062
1063 /*********************************************************************
1064  *
1065  * Function    :  free_current_action
1066  *
1067  * Description :  Free memory used by a current_action_spec.
1068  *                Does not free the current_action_spec itself.
1069  *
1070  * Parameters  :
1071  *          1  :  src = Source to free.
1072  *
1073  * Returns     :  N/A
1074  *
1075  *********************************************************************/
1076 void free_current_action (struct current_action_spec *src)
1077 {
1078    int i;
1079
1080    for (i = 0; i < ACTION_STRING_COUNT; i++)
1081    {
1082       freez(src->string[i]);
1083    }
1084
1085    for (i = 0; i < ACTION_MULTI_COUNT; i++)
1086    {
1087       destroy_list(src->multi[i]);
1088    }
1089
1090    memset(src, '\0', sizeof(*src));
1091 }
1092
1093
1094 static struct file_list *current_actions_file[MAX_AF_FILES]  = {
1095    NULL, NULL, NULL, NULL, NULL,
1096    NULL, NULL, NULL, NULL, NULL
1097 };
1098
1099
1100 #ifdef FEATURE_GRACEFUL_TERMINATION
1101 /*********************************************************************
1102  *
1103  * Function    :  unload_current_actions_file
1104  *
1105  * Description :  Unloads current actions file - reset to state at
1106  *                beginning of program.
1107  *
1108  * Parameters  :  None
1109  *
1110  * Returns     :  N/A
1111  *
1112  *********************************************************************/
1113 void unload_current_actions_file(void)
1114 {
1115    int i;
1116
1117    for (i = 0; i < MAX_AF_FILES; i++)
1118    {
1119       if (current_actions_file[i])
1120       {
1121          current_actions_file[i]->unloader = unload_actions_file;
1122          current_actions_file[i] = NULL;
1123       }
1124    }
1125 }
1126 #endif /* FEATURE_GRACEFUL_TERMINATION */
1127
1128
1129 /*********************************************************************
1130  *
1131  * Function    :  unload_actions_file
1132  *
1133  * Description :  Unloads an actions module.
1134  *
1135  * Parameters  :
1136  *          1  :  file_data = the data structure associated with the
1137  *                            actions file.
1138  *
1139  * Returns     :  N/A
1140  *
1141  *********************************************************************/
1142 void unload_actions_file(void *file_data)
1143 {
1144    struct url_actions * next;
1145    struct url_actions * cur = (struct url_actions *)file_data;
1146    while (cur != NULL)
1147    {
1148       next = cur->next;
1149       free_url_spec(cur->url);
1150       if ((next == NULL) || (next->action != cur->action))
1151       {
1152          /*
1153           * As the action settings might be shared,
1154           * we can only free them if the current
1155           * url pattern is the last one, or if the
1156           * next one is using different settings.
1157           */
1158          free_action_spec(cur->action);
1159       }
1160       freez(cur);
1161       cur = next;
1162    }
1163 }
1164
1165
1166 /*********************************************************************
1167  *
1168  * Function    :  free_alias_list
1169  *
1170  * Description :  Free memory used by a list of aliases.
1171  *
1172  * Parameters  :
1173  *          1  :  alias_list = Linked list to free.
1174  *
1175  * Returns     :  N/A
1176  *
1177  *********************************************************************/
1178 void free_alias_list(struct action_alias *alias_list)
1179 {
1180    while (alias_list != NULL)
1181    {
1182       struct action_alias * next = alias_list->next;
1183       alias_list->next = NULL;
1184       freez(alias_list->name);
1185       free_action(alias_list->action);
1186       free(alias_list);
1187       alias_list = next;
1188    }
1189 }
1190
1191
1192 /*********************************************************************
1193  *
1194  * Function    :  load_actions_file
1195  *
1196  * Description :  Read and parse all the action files and add to files
1197  *                list.
1198  *
1199  * Parameters  :
1200  *          1  :  csp = Current client state (buffers, headers, etc...)
1201  *
1202  * Returns     :  0 => Ok, everything else is an error.
1203  *
1204  *********************************************************************/
1205 int load_actions_file(struct client_state *csp)
1206 {
1207    int i;
1208    int result;
1209
1210    for (i = 0; i < MAX_AF_FILES; i++)
1211    {
1212       if (csp->config->actions_file[i])
1213       {
1214          result = load_one_actions_file(csp, i);
1215          if (result)
1216          {
1217             return result;
1218          }
1219       }
1220       else if (current_actions_file[i])
1221       {
1222          current_actions_file[i]->unloader = unload_actions_file;
1223          current_actions_file[i] = NULL;
1224       }
1225    }
1226
1227    return 0;
1228 }
1229
1230 /*********************************************************************
1231  *
1232  * Function    :  load_one_actions_file
1233  *
1234  * Description :  Read and parse a action file and add to files
1235  *                list.
1236  *
1237  * Parameters  :
1238  *          1  :  csp = Current client state (buffers, headers, etc...)
1239  *          2  :  fileid = File index to load.
1240  *
1241  * Returns     :  0 => Ok, everything else is an error.
1242  *
1243  *********************************************************************/
1244 static int load_one_actions_file(struct client_state *csp, int fileid)
1245 {
1246
1247    /*
1248     * Parser mode.
1249     * Note: Keep these in the order they occur in the file, they are
1250     * sometimes tested with <=
1251     */
1252 #define MODE_START_OF_FILE 1
1253 #define MODE_SETTINGS      2
1254 #define MODE_DESCRIPTION   3
1255 #define MODE_ALIAS         4
1256 #define MODE_ACTIONS       5
1257
1258    int mode = MODE_START_OF_FILE;
1259
1260    FILE *fp;
1261    struct url_actions *last_perm;
1262    struct url_actions *perm;
1263    char  buf[BUFFER_SIZE];
1264    struct file_list *fs;
1265    struct action_spec * cur_action = NULL;
1266    int cur_action_used = 0;
1267    struct action_alias * alias_list = NULL;
1268    unsigned long linenum = 0;
1269
1270    if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1271    {
1272       /* No need to load */
1273       csp->actions_list[fileid] = current_actions_file[fileid];
1274       return 0;
1275    }
1276    if (!fs)
1277    {
1278       log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': %E. "
1279          "Note that beginning with Privoxy 3.0.7, actions files have to be specified "
1280          "with their complete file names.", csp->config->actions_file[fileid]);
1281       return 1; /* never get here */
1282    }
1283
1284    fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1285    if (last_perm == NULL)
1286    {
1287       log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1288                 csp->config->actions_file[fileid]);
1289       return 1; /* never get here */
1290    }
1291
1292    if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1293    {
1294       log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1295                 csp->config->actions_file[fileid]);
1296       return 1; /* never get here */
1297    }
1298
1299    while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1300    {
1301       if (*buf == '{')
1302       {
1303          /* It's a header block */
1304          if (buf[1] == '{')
1305          {
1306             /* It's {{settings}} or {{alias}} */
1307             size_t len = strlen(buf);
1308             char * start = buf + 2;
1309             char * end = buf + len - 1;
1310             if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1311             {
1312                /* too short */
1313                fclose(fp);
1314                log_error(LOG_LEVEL_FATAL,
1315                   "can't load actions file '%s': invalid line (%lu): %s", 
1316                   csp->config->actions_file[fileid], linenum, buf);
1317                return 1; /* never get here */
1318             }
1319
1320             /* Trim leading and trailing whitespace. */
1321             end[1] = '\0';
1322             chomp(start);
1323
1324             if (*start == '\0')
1325             {
1326                /* too short */
1327                fclose(fp);
1328                log_error(LOG_LEVEL_FATAL,
1329                   "can't load actions file '%s': invalid line (%lu): {{ }}",
1330                   csp->config->actions_file[fileid], linenum);
1331                return 1; /* never get here */
1332             }
1333
1334             /*
1335              * An actionsfile can optionally contain the following blocks.
1336              * They *MUST* be in this order, to simplify processing:
1337              *
1338              * {{settings}}
1339              * name=value...
1340              *
1341              * {{description}}
1342              * ...free text, format TBD, but no line may start with a '{'...
1343              *
1344              * {{alias}}
1345              * name=actions...
1346              *
1347              * The actual actions must be *after* these special blocks.
1348              * None of these special blocks may be repeated.
1349              *
1350              */
1351             if (0 == strcmpic(start, "settings"))
1352             {
1353                /* it's a {{settings}} block */
1354                if (mode >= MODE_SETTINGS)
1355                {
1356                   /* {{settings}} must be first thing in file and must only
1357                    * appear once.
1358                    */
1359                   fclose(fp);
1360                   log_error(LOG_LEVEL_FATAL,
1361                      "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1362                      csp->config->actions_file[fileid], linenum);
1363                }
1364                mode = MODE_SETTINGS;
1365             }
1366             else if (0 == strcmpic(start, "description"))
1367             {
1368                /* it's a {{description}} block */
1369                if (mode >= MODE_DESCRIPTION)
1370                {
1371                   /* {{description}} is a singleton and only {{settings}} may proceed it
1372                    */
1373                   fclose(fp);
1374                   log_error(LOG_LEVEL_FATAL,
1375                      "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1376                      csp->config->actions_file[fileid], linenum);
1377                }
1378                mode = MODE_DESCRIPTION;
1379             }
1380             else if (0 == strcmpic(start, "alias"))
1381             {
1382                /* it's an {{alias}} block */
1383                if (mode >= MODE_ALIAS)
1384                {
1385                   /* {{alias}} must be first thing in file, possibly after
1386                    * {{settings}} and {{description}}
1387                    *
1388                    * {{alias}} must only appear once.
1389                    *
1390                    * Note that these are new restrictions introduced in
1391                    * v2.9.10 in order to make actionsfile editing simpler.
1392                    * (Otherwise, reordering actionsfile entries without
1393                    * completely rewriting the file becomes non-trivial)
1394                    */
1395                   fclose(fp);
1396                   log_error(LOG_LEVEL_FATAL,
1397                      "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1398                      csp->config->actions_file[fileid], linenum);
1399                }
1400                mode = MODE_ALIAS;
1401             }
1402             else
1403             {
1404                /* invalid {{something}} block */
1405                fclose(fp);
1406                log_error(LOG_LEVEL_FATAL,
1407                   "can't load actions file '%s': invalid line (%lu): {{%s}}",
1408                   csp->config->actions_file[fileid], linenum, start);
1409                return 1; /* never get here */
1410             }
1411          }
1412          else
1413          {
1414             /* It's an actions block */
1415
1416             char  actions_buf[BUFFER_SIZE];
1417             char * end;
1418
1419             /* set mode */
1420             mode    = MODE_ACTIONS;
1421
1422             /* free old action */
1423             if (cur_action)
1424             {
1425                if (!cur_action_used)
1426                {
1427                   free_action_spec(cur_action);
1428                }
1429                cur_action = NULL;
1430             }
1431             cur_action_used = 0;
1432             cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1433             if (cur_action == NULL)
1434             {
1435                fclose(fp);
1436                log_error(LOG_LEVEL_FATAL,
1437                   "can't load actions file '%s': out of memory",
1438                   csp->config->actions_file[fileid]);
1439                return 1; /* never get here */
1440             }
1441             init_action(cur_action);
1442
1443             /* trim { */
1444             strlcpy(actions_buf, buf + 1, sizeof(actions_buf));
1445
1446             /* check we have a trailing } and then trim it */
1447             end = actions_buf + strlen(actions_buf) - 1;
1448             if (*end != '}')
1449             {
1450                /* No closing } */
1451                fclose(fp);
1452                log_error(LOG_LEVEL_FATAL,
1453                   "can't load actions file '%s': invalid line (%lu): %s",
1454                   csp->config->actions_file[fileid], linenum, buf);
1455                return 1; /* never get here */
1456             }
1457             *end = '\0';
1458
1459             /* trim any whitespace immediately inside {} */
1460             chomp(actions_buf);
1461
1462             if (get_actions(actions_buf, alias_list, cur_action))
1463             {
1464                /* error */
1465                fclose(fp);
1466                log_error(LOG_LEVEL_FATAL,
1467                   "can't load actions file '%s': invalid line (%lu): %s",
1468                   csp->config->actions_file[fileid], linenum, buf);
1469                return 1; /* never get here */
1470             }
1471          }
1472       }
1473       else if (mode == MODE_SETTINGS)
1474       {
1475          /*
1476           * Part of the {{settings}} block.
1477           * For now only serves to check if the file's minimum Privoxy
1478           * version requirement is met, but we may want to read & check
1479           * permissions when we go multi-user.
1480           */
1481          if (!strncmp(buf, "for-privoxy-version=", 20))
1482          {
1483             char *version_string, *fields[3];
1484             int num_fields;
1485
1486             if ((version_string = strdup(buf + 20)) == NULL)
1487             {
1488                fclose(fp);
1489                log_error(LOG_LEVEL_FATAL,
1490                          "can't load actions file '%s': out of memory!",
1491                          csp->config->actions_file[fileid]);
1492                return 1; /* never get here */
1493             }
1494             
1495             num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1496
1497             if (num_fields < 1 || atoi(fields[0]) == 0)
1498             {
1499                log_error(LOG_LEVEL_ERROR,
1500                  "While loading actions file '%s': invalid line (%lu): %s",
1501                   csp->config->actions_file[fileid], linenum, buf);
1502             }
1503             else if (                      atoi(fields[0]) > VERSION_MAJOR
1504                      || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1505                      || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1506             {
1507                fclose(fp);
1508                log_error(LOG_LEVEL_FATAL,
1509                          "Actions file '%s', line %lu requires newer Privoxy version: %s",
1510                          csp->config->actions_file[fileid], linenum, buf );
1511                return 1; /* never get here */
1512             }
1513             free(version_string);
1514          }
1515       }
1516       else if (mode == MODE_DESCRIPTION)
1517       {
1518          /*
1519           * Part of the {{description}} block.
1520           * Ignore for now.
1521           */
1522       }
1523       else if (mode == MODE_ALIAS)
1524       {
1525          /*
1526           * define an alias
1527           */
1528          char  actions_buf[BUFFER_SIZE];
1529          struct action_alias * new_alias;
1530
1531          char * start = strchr(buf, '=');
1532          char * end = start;
1533
1534          if ((start == NULL) || (start == buf))
1535          {
1536             log_error(LOG_LEVEL_FATAL,
1537                "can't load actions file '%s': invalid alias line (%lu): %s",
1538                csp->config->actions_file[fileid], linenum, buf);
1539             return 1; /* never get here */
1540          }
1541
1542          if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1543          {
1544             fclose(fp);
1545             log_error(LOG_LEVEL_FATAL,
1546                "can't load actions file '%s': out of memory!",
1547                csp->config->actions_file[fileid]);
1548             return 1; /* never get here */
1549          }
1550
1551          /* Eat any the whitespace before the '=' */
1552          end--;
1553          while ((*end == ' ') || (*end == '\t'))
1554          {
1555             /*
1556              * we already know we must have at least 1 non-ws char
1557              * at start of buf - no need to check
1558              */
1559             end--;
1560          }
1561          end[1] = '\0';
1562
1563          /* Eat any the whitespace after the '=' */
1564          start++;
1565          while ((*start == ' ') || (*start == '\t'))
1566          {
1567             start++;
1568          }
1569          if (*start == '\0')
1570          {
1571             log_error(LOG_LEVEL_FATAL,
1572                "can't load actions file '%s': invalid alias line (%lu): %s",
1573                csp->config->actions_file[fileid], linenum, buf);
1574             return 1; /* never get here */
1575          }
1576
1577          if ((new_alias->name = strdup(buf)) == NULL)
1578          {
1579             fclose(fp);
1580             log_error(LOG_LEVEL_FATAL,
1581                "can't load actions file '%s': out of memory!",
1582                csp->config->actions_file[fileid]);
1583             return 1; /* never get here */
1584          }
1585
1586          strlcpy(actions_buf, start, sizeof(actions_buf));
1587
1588          if (get_actions(actions_buf, alias_list, new_alias->action))
1589          {
1590             /* error */
1591             fclose(fp);
1592             log_error(LOG_LEVEL_FATAL,
1593                "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1594                csp->config->actions_file[fileid], linenum, buf, start);
1595             return 1; /* never get here */
1596          }
1597
1598          /* add to list */
1599          new_alias->next = alias_list;
1600          alias_list = new_alias;
1601       }
1602       else if (mode == MODE_ACTIONS)
1603       {
1604          /* it's a URL pattern */
1605
1606          /* allocate a new node */
1607          if ((perm = zalloc(sizeof(*perm))) == NULL)
1608          {
1609             fclose(fp);
1610             log_error(LOG_LEVEL_FATAL,
1611                "can't load actions file '%s': out of memory!",
1612                csp->config->actions_file[fileid]);
1613             return 1; /* never get here */
1614          }
1615
1616          perm->action = cur_action;
1617          cur_action_used = 1;
1618
1619          /* Save the URL pattern */
1620          if (create_url_spec(perm->url, buf))
1621          {
1622             fclose(fp);
1623             log_error(LOG_LEVEL_FATAL,
1624                "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1625                csp->config->actions_file[fileid], linenum, buf);
1626             return 1; /* never get here */
1627          }
1628
1629          /* add it to the list */
1630          last_perm->next = perm;
1631          last_perm = perm;
1632       }
1633       else if (mode == MODE_START_OF_FILE)
1634       {
1635          /* oops - please have a {} line as 1st line in file. */
1636          fclose(fp);
1637          log_error(LOG_LEVEL_FATAL,
1638             "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1639             csp->config->actions_file[fileid], linenum, buf);
1640          return 1; /* never get here */
1641       }
1642       else
1643       {
1644          /* How did we get here? This is impossible! */
1645          fclose(fp);
1646          log_error(LOG_LEVEL_FATAL,
1647             "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1648             csp->config->actions_file[fileid], mode);
1649          return 1; /* never get here */
1650       }
1651    }
1652
1653    fclose(fp);
1654
1655    if (!cur_action_used)
1656    {
1657       free_action_spec(cur_action);
1658    }
1659    free_alias_list(alias_list);
1660
1661    /* the old one is now obsolete */
1662    if (current_actions_file[fileid])
1663    {
1664       current_actions_file[fileid]->unloader = unload_actions_file;
1665    }
1666
1667    fs->next    = files->next;
1668    files->next = fs;
1669    current_actions_file[fileid] = fs;
1670
1671    csp->actions_list[fileid] = fs;
1672
1673    return(0);
1674
1675 }
1676
1677
1678 /*********************************************************************
1679  *
1680  * Function    :  actions_to_text
1681  *
1682  * Description :  Converts a actionsfile entry from the internal
1683  *                structure into a text line.  The output is split
1684  *                into one line for each action with line continuation. 
1685  *
1686  * Parameters  :
1687  *          1  :  action = The action to format.
1688  *
1689  * Returns     :  A string.  Caller must free it.
1690  *                NULL on out-of-memory error.
1691  *
1692  *********************************************************************/
1693 char * actions_to_text(const struct action_spec *action)
1694 {
1695    unsigned mask = action->mask;
1696    unsigned add  = action->add;
1697    char * result = strdup("");
1698    struct list_entry * lst;
1699
1700    /* sanity - prevents "-feature +feature" */
1701    mask |= add;
1702
1703
1704 #define DEFINE_ACTION_BOOL(__name, __bit)          \
1705    if (!(mask & __bit))                            \
1706    {                                               \
1707       string_append(&result, " -" __name " \\\n"); \
1708    }                                               \
1709    else if (add & __bit)                           \
1710    {                                               \
1711       string_append(&result, " +" __name " \\\n"); \
1712    }
1713
1714 #define DEFINE_ACTION_STRING(__name, __bit, __index)   \
1715    if (!(mask & __bit))                                \
1716    {                                                   \
1717       string_append(&result, " -" __name " \\\n");     \
1718    }                                                   \
1719    else if (add & __bit)                               \
1720    {                                                   \
1721       string_append(&result, " +" __name "{");         \
1722       string_append(&result, action->string[__index]); \
1723       string_append(&result, "} \\\n");                \
1724    }
1725
1726 #define DEFINE_ACTION_MULTI(__name, __index)         \
1727    if (action->multi_remove_all[__index])            \
1728    {                                                 \
1729       string_append(&result, " -" __name " \\\n");   \
1730    }                                                 \
1731    else                                              \
1732    {                                                 \
1733       lst = action->multi_remove[__index]->first;    \
1734       while (lst)                                    \
1735       {                                              \
1736          string_append(&result, " -" __name "{");    \
1737          string_append(&result, lst->str);           \
1738          string_append(&result, "} \\\n");           \
1739          lst = lst->next;                            \
1740       }                                              \
1741    }                                                 \
1742    lst = action->multi_add[__index]->first;          \
1743    while (lst)                                       \
1744    {                                                 \
1745       string_append(&result, " +" __name "{");       \
1746       string_append(&result, lst->str);              \
1747       string_append(&result, "} \\\n");              \
1748       lst = lst->next;                               \
1749    }
1750
1751 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1752
1753 #include "actionlist.h"
1754
1755 #undef DEFINE_ACTION_MULTI
1756 #undef DEFINE_ACTION_STRING
1757 #undef DEFINE_ACTION_BOOL
1758 #undef DEFINE_ACTION_ALIAS
1759
1760    return result;
1761 }
1762
1763
1764 /*********************************************************************
1765  *
1766  * Function    :  actions_to_html
1767  *
1768  * Description :  Converts a actionsfile entry from numeric form
1769  *                ("mask" and "add") to a <br>-seperated HTML string
1770  *                in which each action is linked to its chapter in
1771  *                the user manual.
1772  *
1773  * Parameters  :
1774  *          1  :  csp    = Client state (for config)
1775  *          2  :  action = Action spec to be converted
1776  *
1777  * Returns     :  A string.  Caller must free it.
1778  *                NULL on out-of-memory error.
1779  *
1780  *********************************************************************/
1781 char * actions_to_html(const struct client_state *csp,
1782                        const struct action_spec *action)
1783 {
1784    unsigned mask = action->mask;
1785    unsigned add  = action->add;
1786    char * result = strdup("");
1787    struct list_entry * lst;
1788
1789    /* sanity - prevents "-feature +feature" */
1790    mask |= add;
1791
1792
1793 #define DEFINE_ACTION_BOOL(__name, __bit)       \
1794    if (!(mask & __bit))                         \
1795    {                                            \
1796       string_append(&result, "\n<br>-");        \
1797       string_join(&result, add_help_link(__name, csp->config)); \
1798    }                                            \
1799    else if (add & __bit)                        \
1800    {                                            \
1801       string_append(&result, "\n<br>+");        \
1802       string_join(&result, add_help_link(__name, csp->config)); \
1803    }
1804
1805 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1806    if (!(mask & __bit))                              \
1807    {                                                 \
1808       string_append(&result, "\n<br>-");             \
1809       string_join(&result, add_help_link(__name, csp->config)); \
1810    }                                                 \
1811    else if (add & __bit)                             \
1812    {                                                 \
1813       string_append(&result, "\n<br>+");             \
1814       string_join(&result, add_help_link(__name, csp->config)); \
1815       string_append(&result, "{");                   \
1816       string_join(&result, html_encode(action->string[__index])); \
1817       string_append(&result, "}");                   \
1818    }
1819
1820 #define DEFINE_ACTION_MULTI(__name, __index)          \
1821    if (action->multi_remove_all[__index])             \
1822    {                                                  \
1823       string_append(&result, "\n<br>-");              \
1824       string_join(&result, add_help_link(__name, csp->config)); \
1825    }                                                  \
1826    else                                               \
1827    {                                                  \
1828       lst = action->multi_remove[__index]->first;     \
1829       while (lst)                                     \
1830       {                                               \
1831          string_append(&result, "\n<br>-");           \
1832          string_join(&result, add_help_link(__name, csp->config)); \
1833          string_append(&result, "{");                 \
1834          string_join(&result, html_encode(lst->str)); \
1835          string_append(&result, "}");                 \
1836          lst = lst->next;                             \
1837       }                                               \
1838    }                                                  \
1839    lst = action->multi_add[__index]->first;           \
1840    while (lst)                                        \
1841    {                                                  \
1842       string_append(&result, "\n<br>+");              \
1843       string_join(&result, add_help_link(__name, csp->config)); \
1844       string_append(&result, "{");                    \
1845       string_join(&result, html_encode(lst->str));    \
1846       string_append(&result, "}");                    \
1847       lst = lst->next;                                \
1848    }
1849
1850 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1851
1852 #include "actionlist.h"
1853
1854 #undef DEFINE_ACTION_MULTI
1855 #undef DEFINE_ACTION_STRING
1856 #undef DEFINE_ACTION_BOOL
1857 #undef DEFINE_ACTION_ALIAS
1858
1859    /* trim leading <br> */
1860    if (result && *result)
1861    {
1862       char * s = result;
1863       result = strdup(result + 5);
1864       free(s);
1865    }
1866
1867    return result;
1868 }
1869
1870
1871 /*********************************************************************
1872  *
1873  * Function    :  current_actions_to_html
1874  *
1875  * Description :  Converts a curren action spec to a <br> seperated HTML
1876  *                text in which each action is linked to its chapter in
1877  *                the user manual.
1878  *
1879  * Parameters  :
1880  *          1  :  csp    = Client state (for config) 
1881  *          2  :  action = Current action spec to be converted
1882  *
1883  * Returns     :  A string.  Caller must free it.
1884  *                NULL on out-of-memory error.
1885  *
1886  *********************************************************************/
1887 char *current_action_to_html(const struct client_state *csp,
1888                              const struct current_action_spec *action)
1889 {
1890    unsigned long flags  = action->flags;
1891    struct list_entry * lst;
1892    char *result   = strdup("");
1893    char *active   = strdup("");
1894    char *inactive = strdup("");
1895
1896 #define DEFINE_ACTION_BOOL(__name, __bit)  \
1897    if (flags & __bit)                      \
1898    {                                       \
1899       string_append(&active, "\n<br>+");   \
1900       string_join(&active, add_help_link(__name, csp->config)); \
1901    }                                       \
1902    else                                    \
1903    {                                       \
1904       string_append(&inactive, "\n<br>-"); \
1905       string_join(&inactive, add_help_link(__name, csp->config)); \
1906    }
1907
1908 #define DEFINE_ACTION_STRING(__name, __bit, __index)   \
1909    if (flags & __bit)                                  \
1910    {                                                   \
1911       string_append(&active, "\n<br>+");               \
1912       string_join(&active, add_help_link(__name, csp->config)); \
1913       string_append(&active, "{");                     \
1914       string_join(&active, html_encode(action->string[__index])); \
1915       string_append(&active, "}");                     \
1916    }                                                   \
1917    else                                                \
1918    {                                                   \
1919       string_append(&inactive, "\n<br>-");             \
1920       string_join(&inactive, add_help_link(__name, csp->config)); \
1921    }
1922
1923 #define DEFINE_ACTION_MULTI(__name, __index)           \
1924    lst = action->multi[__index]->first;                \
1925    if (lst == NULL)                                    \
1926    {                                                   \
1927       string_append(&inactive, "\n<br>-");             \
1928       string_join(&inactive, add_help_link(__name, csp->config)); \
1929    }                                                   \
1930    else                                                \
1931    {                                                   \
1932       while (lst)                                      \
1933       {                                                \
1934          string_append(&active, "\n<br>+");            \
1935          string_join(&active, add_help_link(__name, csp->config)); \
1936          string_append(&active, "{");                  \
1937          string_join(&active, html_encode(lst->str));  \
1938          string_append(&active, "}");                  \
1939          lst = lst->next;                              \
1940       }                                                \
1941    }
1942
1943 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1944
1945 #include "actionlist.h"
1946
1947 #undef DEFINE_ACTION_MULTI
1948 #undef DEFINE_ACTION_STRING
1949 #undef DEFINE_ACTION_BOOL
1950 #undef DEFINE_ACTION_ALIAS
1951
1952    if (active != NULL)
1953    {
1954       string_append(&result, active);
1955       freez(active);
1956    }
1957    string_append(&result, "\n<br>");
1958    if (inactive != NULL)
1959    {
1960       string_append(&result, inactive);
1961       freez(inactive);
1962    }
1963    return result;
1964 }