better service description. This text will be shown when the user configures the...
[privoxy.git] / cgiedit.c
1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.41.2.2 2002/08/05 20:02:59 oes Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/Attic/cgiedit.c,v $
5  *
6  * Purpose     :  CGI-based actionsfile editor.
7  *
8  *                Functions declared include: cgi_edit_*
9  *
10  *                NOTE: The CGIs in this file use parameter names
11  *                such as "f" and "s" which are really *BAD* choices.
12  *                However, I'm trying to save bytes in the
13  *                edit-actions-list HTML page - the standard actions
14  *                file generated a 550kbyte page, which is ridiculous.
15  *
16  *                Stick to the short names in this file for consistency.
17  *
18  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
19  *                Privoxy team. http://www.privoxy.org/
20  *
21  *                Based on the Internet Junkbuster originally written
22  *                by and Copyright (C) 1997 Anonymous Coders and
23  *                Junkbusters Corporation.  http://www.junkbusters.com
24  *
25  *                This program is free software; you can redistribute it
26  *                and/or modify it under the terms of the GNU General
27  *                Public License as published by the Free Software
28  *                Foundation; either version 2 of the License, or (at
29  *                your option) any later version.
30  *
31  *                This program is distributed in the hope that it will
32  *                be useful, but WITHOUT ANY WARRANTY; without even the
33  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
34  *                PARTICULAR PURPOSE.  See the GNU General Public
35  *                License for more details.
36  *
37  *                The GNU General Public License should be included with
38  *                this file.  If not, you can view it at
39  *                http://www.gnu.org/copyleft/gpl.html
40  *                or write to the Free Software Foundation, Inc., 59
41  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
42  *
43  * Revisions   :
44  *    $Log: cgiedit.c,v $
45  *    Revision 1.41.2.2  2002/08/05 20:02:59  oes
46  *    Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
47  *
48  *    Revision 1.41.2.1  2002/08/02 12:43:14  oes
49  *    Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
50  *
51  *    Revision 1.41  2002/05/21 19:09:45  oes
52  *     - Made Add/Edit/Remove URL Submit and Cancel
53  *       buttons jump back to relevant section in eal
54  *     - Bugfix: remove-url-form needs p export
55  *
56  *    Revision 1.40  2002/05/19 11:34:35  jongfoster
57  *    Handling read-only actions files better - report the actual
58  *    error, not "Out of memory"!
59  *
60  *    Bug report:
61  *    http://sourceforge.net/tracker/index.php?func=detail
62  *    &aid=557905&group_id=11118&atid=111118
63  *
64  *    Revision 1.39  2002/05/12 21:39:15  jongfoster
65  *    - Adding Doxygen-style comments to structures and #defines.
66  *    - Correcting function comments
67  *
68  *    Revision 1.38  2002/05/03 23:00:38  jongfoster
69  *    Support for templates for "standard actions" buttons.
70  *    See bug #549871
71  *
72  *    Revision 1.37  2002/04/30 11:14:52  oes
73  *    Made csp the first parameter in *action_to_html
74  *
75  *    Revision 1.36  2002/04/26 21:53:30  jongfoster
76  *    Fixing a memory leak.  (Near, but not caused by, my earlier commit).
77  *
78  *    Revision 1.35  2002/04/26 21:50:02  jongfoster
79  *    Honouring default exports in edit-actions-for-url-filter template.
80  *
81  *    Revision 1.34  2002/04/26 12:54:17  oes
82  *    Adaptions to changes in actions.c
83  *
84  *    Revision 1.33  2002/04/24 02:17:47  oes
85  *     - Moved get_char_param, get_string_param and get_number_param to cgi.c
86  *     - Comments
87  *     - Activated Jon's code for editing multiple AFs
88  *     - cgi_edit_list_actions now provides context-sensitive
89  *       help, looks up all action sets from standard.action and
90  *       makes buttons for them in the catchall section
91  *     - cgi_edit_action_submit now honors a p parameter, looks up
92  *       the corresponding action set, and sets the catchall pattern's
93  *       actions accordingly.
94  *
95  *    Revision 1.32  2002/04/19 16:55:31  jongfoster
96  *    Fixing newline problems.  If we do our own text file newline
97  *    mangling, we don't want the library to do any, so we need to
98  *    open the files in *binary* mode.
99  *
100  *    Revision 1.31  2002/04/18 19:21:08  jongfoster
101  *    Added code to detect "conventional" action files, that start
102  *    with a set of actions for all URLs (the pattern "/").
103  *    These are special-cased in the "edit-actions-list" CGI, so
104  *    that a special UI can be written for them.
105  *
106  *    Revision 1.30  2002/04/10 13:38:35  oes
107  *    load_template signature changed
108  *
109  *    Revision 1.29  2002/04/08 16:59:08  oes
110  *    Fixed comment
111  *
112  *    Revision 1.28  2002/03/27 12:30:29  oes
113  *    Deleted unsused variable
114  *
115  *    Revision 1.27  2002/03/26 23:06:04  jongfoster
116  *    Removing duplicate @ifs on the toggle page
117  *
118  *    Revision 1.26  2002/03/26 22:59:17  jongfoster
119  *    Fixing /toggle to display status consistently.
120  *
121  *    Revision 1.25  2002/03/26 22:29:54  swa
122  *    we have a new homepage!
123  *
124  *    Revision 1.24  2002/03/24 15:23:33  jongfoster
125  *    Name changes
126  *
127  *    Revision 1.23  2002/03/24 13:32:41  swa
128  *    name change related issues
129  *
130  *    Revision 1.22  2002/03/24 13:25:43  swa
131  *    name change related issues
132  *
133  *    Revision 1.21  2002/03/22 18:02:48  jongfoster
134  *    Fixing remote toggle
135  *
136  *    Revision 1.20  2002/03/16 20:28:34  oes
137  *    Added descriptions to the filters so users will know what they select in the cgi editor
138  *
139  *    Revision 1.19  2002/03/16 18:38:14  jongfoster
140  *    Stopping stupid or malicious users from breaking the actions
141  *    file using the web-based editor.
142  *
143  *    Revision 1.18  2002/03/16 14:57:44  jongfoster
144  *    Full support for enabling/disabling modular filters.
145  *
146  *    Revision 1.17  2002/03/16 14:26:42  jongfoster
147  *    First version of modular filters support - READ ONLY!
148  *    Fixing a double-free bug in the out-of-memory handling in map_radio().
149  *
150  *    Revision 1.16  2002/03/07 03:46:17  oes
151  *    Fixed compiler warnings
152  *
153  *    Revision 1.15  2002/03/06 22:54:35  jongfoster
154  *    Automated function-comment nitpicking.
155  *
156  *    Revision 1.14  2002/03/05 00:24:51  jongfoster
157  *    Patch to always edit the current actions file.
158  *
159  *    Revision 1.13  2002/03/04 02:07:59  david__schmidt
160  *    Enable web editing of actions file on OS/2 (it had been broken all this time!)
161  *
162  *    Revision 1.12  2002/03/03 09:18:03  joergs
163  *    Made jumbjuster work on AmigaOS again.
164  *
165  *    Revision 1.11  2002/01/23 01:03:31  jongfoster
166  *    Fixing gcc [CygWin] compiler warnings
167  *
168  *    Revision 1.10  2002/01/23 00:22:59  jongfoster
169  *    Adding new function cgi_edit_actions_section_swap(), to reorder
170  *    the actions file.
171  *
172  *    Adding get_url_spec_param() to get a validated URL pattern.
173  *
174  *    Moving edit_read_line() out of this file and into loaders.c.
175  *
176  *    Adding missing html_encode() to many CGI functions.
177  *
178  *    Moving the functions that #include actionlist.h to the end of the file,
179  *    because the Visual C++ 97 debugger gets extremely confused if you try
180  *    to debug any code that comes after them in the file.
181  *
182  *    Major optimizations in cgi_edit_actions_list() to reduce the size of
183  *    the generated HTML (down 40% from 550k to 304k), with major side-effects
184  *    throughout the editor and templates.  In particular, the length of the
185  *    URLs throughout the editor has been drastically reduced, by cutting
186  *    paramater names down to 1 character and CGI names down to 3-4
187  *    characters, by removing all non-essential CGI paramaters even at the
188  *    expense of having to re-read the actions file for the most trivial
189  *    page, and by using relative rather than absolute URLs.  This means
190  *    that this (typical example):
191  *
192  *    <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
193  *    filename=ijb&amp;ver=1011487572&amp;section=12&amp;pattern=13
194  *    &amp;oldval=www.oesterhelt.org%2Fdeanimate-demo">
195  *
196  *    is now this:
197  *
198  *    <a href="eau?f=ijb&amp;v=1011487572&amp;p=13">
199  *
200  *    Revision 1.9  2002/01/17 20:56:22  jongfoster
201  *    Replacing hard references to the URL of the config interface
202  *    with #defines from project.h
203  *
204  *    Revision 1.8  2001/11/30 23:35:51  jongfoster
205  *    Renaming actionsfile to ijb.action
206  *
207  *    Revision 1.7  2001/11/13 00:28:24  jongfoster
208  *    - Renaming parameters from edit-actions-for-url so that they only
209  *      contain legal JavaScript characters.  If we wanted to write
210  *      JavaScript that worked with Netscape 4, this is nessacery.
211  *      (Note that at the moment the JavaScript doesn't actually work
212  *      with Netscape 4, but now this is purely a template issue, not
213  *      one affecting code).
214  *    - Adding new CGIs for use by non-JavaScript browsers:
215  *        edit-actions-url-form
216  *        edit-actions-add-url-form
217  *        edit-actions-remove-url-form
218  *    - Fixing || bug.
219  *
220  *    Revision 1.6  2001/10/29 03:48:09  david__schmidt
221  *    OS/2 native needed a snprintf() routine.  Added one to miscutil, brackedted
222  *    by and __OS2__ ifdef.
223  *
224  *    Revision 1.5  2001/10/25 03:40:48  david__schmidt
225  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
226  *    threads to call select() simultaneously.  So, it's time to do a real, live,
227  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
228  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
229  *
230  *    Revision 1.4  2001/10/23 21:48:19  jongfoster
231  *    Cleaning up error handling in CGI functions - they now send back
232  *    a HTML error page and should never cause a FATAL error.  (Fixes one
233  *    potential source of "denial of service" attacks).
234  *
235  *    CGI actions file editor that works and is actually useful.
236  *
237  *    Ability to toggle JunkBuster remotely using a CGI call.
238  *
239  *    You can turn off both the above features in the main configuration
240  *    file, e.g. if you are running a multi-user proxy.
241  *
242  *    Revision 1.3  2001/10/14 22:12:49  jongfoster
243  *    New version of CGI-based actionsfile editor.
244  *    Major changes, including:
245  *    - Completely new file parser and file output routines
246  *    - edit-actions CGI renamed edit-actions-for-url
247  *    - All CGIs now need a filename parameter, except for...
248  *    - New CGI edit-actions which doesn't need a filename,
249  *      to allow you to start the editor up.
250  *    - edit-actions-submit now works, and now automatically
251  *      redirects you back to the main edit-actions-list handler.
252  *
253  *    Revision 1.2  2001/09/16 17:05:14  jongfoster
254  *    Removing unused #include showarg.h
255  *
256  *    Revision 1.1  2001/09/16 15:47:37  jongfoster
257  *    First version of CGI-based edit interface.  This is very much a
258  *    work-in-progress, and you can't actually use it to edit anything
259  *    yet.  You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
260  *    to have any effect.
261  *
262  *
263  **********************************************************************/
264 \f
265
266 #include "config.h"
267
268 /*
269  * FIXME: Following includes copied from cgi.c - which are actually needed?
270  */
271
272 #include <stdio.h>
273 #include <stdlib.h>
274 #include <sys/types.h>
275 #include <ctype.h>
276 #include <string.h>
277 #include <assert.h>
278 #include <sys/stat.h>
279
280 #ifdef _WIN32
281 #define snprintf _snprintf
282 #endif /* def _WIN32 */
283
284 #include "project.h"
285 #include "cgi.h"
286 #include "cgiedit.h"
287 #include "cgisimple.h"
288 #include "list.h"
289 #include "encode.h"
290 #include "actions.h"
291 #include "miscutil.h"
292 #include "errlog.h"
293 #include "loaders.h"
294 #include "loadcfg.h"
295 /* loadcfg.h is for g_bToggleIJB only */
296 #include "urlmatch.h"
297
298 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
299
300
301 #ifdef FEATURE_CGI_EDIT_ACTIONS
302
303 /**
304  * A line in an editable_file.
305  */
306 struct file_line
307 {
308    /** Next entry in the linked list */
309    struct file_line * next;
310    
311    /** The raw data, to write out if this line is unmodified. */
312    char * raw;
313    
314    /** Comments and/or whitespace to put before this line if it's modified
315        and then written out. */
316    char * prefix;
317
318    /** The actual data, as a string.  Line continuation and comment removal
319        are performed on the data read from file before it's stored here, so
320        it will be a single line of data.  */
321    char * unprocessed;
322    
323    /** The type of data on this line.  One of the FILE_LINE_xxx constants. */
324    int type;
325
326    /** The actual data, processed into some sensible data type. */
327    union
328    {
329
330       /** An action specification. */
331       struct action_spec action[1];
332
333       /** A name=value pair. */
334       struct
335       {
336
337          /** The name in the name=value pair. */
338          char * name;
339
340          /** The value in the name=value pair, as a string. */
341          char * svalue;
342
343          /** The value in the name=value pair, as an integer. */
344          int ivalue;
345
346       } setting;
347
348       /* Add more data types here... e.g.
349
350
351       struct url_spec url[1];
352
353       struct
354       {
355          struct action_spec action[1];
356          const char * name;
357       } alias;
358
359       */
360
361    } data;
362
363 };
364
365 /** This file_line has not been processed yet. */
366 #define FILE_LINE_UNPROCESSED           1
367
368 /** This file_line is blank. Can only appear at the end of a file, due to
369     the way the parser works. */
370 #define FILE_LINE_BLANK                 2
371
372 /** This file_line says {{alias}}. */
373 #define FILE_LINE_ALIAS_HEADER          3
374
375 /** This file_line defines an alias. */
376 #define FILE_LINE_ALIAS_ENTRY           4
377
378 /** This file_line defines an {action}. */
379 #define FILE_LINE_ACTION                5
380
381 /** This file_line specifies a URL pattern. */
382 #define FILE_LINE_URL                   6
383
384 /** This file_line says {{settings}}. */
385 #define FILE_LINE_SETTINGS_HEADER       7
386
387 /** This file_line is in a {{settings}} block. */
388 #define FILE_LINE_SETTINGS_ENTRY        8
389
390 /** This file_line says {{description}}. */
391 #define FILE_LINE_DESCRIPTION_HEADER    9
392
393 /** This file_line is in a {{description}} block. */
394 #define FILE_LINE_DESCRIPTION_ENTRY    10
395
396
397 /**
398  * A configuration file, in a format that can be edited and written back to
399  * disk.
400  */
401 struct editable_file
402 {
403    struct file_line * lines;  /**< The contents of the file.  A linked list of lines. */
404    const char * filename;     /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
405    const char * identifier;   /**< Filename stub - e.g. "wibble".  Use for CGI param. */
406                               /**< Pre-encoded with url_encode() for ease of use. */
407    const char * version_str;  /**< Last modification time, as a string.  For CGI param. */
408                               /**< Can be used in URL without using url_param(). */
409    unsigned version;          /**< Last modification time - prevents chaos with
410                                    the browser's "back" button.  Note that this is a
411                                    time_t cast to an unsigned.  When comparing, always
412                                    cast the time_t to an unsigned, and *NOT* vice-versa.
413                                    This may lose the top few bits, but they're not
414                                    significant anyway. */
415    int newline;               /**< Newline convention - one of the NEWLINE_xxx constants.
416                                    Note that changing this after the file has been
417                                    read in will cause a mess. */
418    struct file_line * parse_error; /**< On parse error, this is the offending line. */
419    const char * parse_error_text;  /**< On parse error, this is the problem.
420                                         (Statically allocated) */
421 };
422
423 /* FIXME: Following non-static functions should be prototyped in .h or made static */
424
425 /* Functions to read and write arbitrary config files */
426 jb_err edit_read_file(struct client_state *csp,
427                       const struct map *parameters,
428                       int require_version,
429                       const char *suffix,
430                       struct editable_file **pfile);
431 jb_err edit_write_file(struct editable_file * file);
432 void   edit_free_file(struct editable_file * file);
433
434 /* Functions to read and write actions files */
435 jb_err edit_parse_actions_file(struct editable_file * file);
436 jb_err edit_read_actions_file(struct client_state *csp,
437                               struct http_response *rsp,
438                               const struct map *parameters,
439                               int require_version,
440                               struct editable_file **pfile);
441
442 /* Error handlers */
443 jb_err cgi_error_modified(struct client_state *csp,
444                           struct http_response *rsp,
445                           const char *filename);
446 jb_err cgi_error_parse(struct client_state *csp,
447                        struct http_response *rsp,
448                        struct editable_file *file);
449 jb_err cgi_error_file(struct client_state *csp,
450                       struct http_response *rsp,
451                       const char *filename);
452 jb_err cgi_error_file_read_only(struct client_state *csp,
453                                 struct http_response *rsp,
454                                 const char *filename);
455 jb_err cgi_error_disabled(struct client_state *csp,
456                           struct http_response *rsp);
457
458 /* Internal arbitrary config file support functions */
459 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
460 static void edit_free_file_lines(struct file_line * first_line);
461
462 /* Internal actions file support functions */
463 static int match_actions_file_header_line(const char * line, const char * name);
464 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
465
466 /* Internal parameter parsing functions */
467 static jb_err get_file_name_param(struct client_state *csp,
468                                   const struct map *parameters,
469                                   const char *param_name,
470                                   const char *suffix,
471                                   char **pfilename,
472                                   const char **pparam);
473
474 static jb_err get_url_spec_param(struct client_state *csp,
475                                  const struct map *parameters,
476                                  const char *name,
477                                  char **pvalue);
478
479
480 /* Internal actionsfile <==> HTML conversion functions */
481 static jb_err map_radio(struct map * exports,
482                         const char * optionname,
483                         const char * values,
484                         int value);
485 static jb_err actions_to_radio(struct map * exports,
486                                const struct action_spec *action);
487 static jb_err actions_from_radio(const struct map * parameters,
488                                  struct action_spec *action);
489
490
491 static jb_err map_copy_parameter_html(struct map *out,
492                                       const struct map *in,
493                                       const char *name);
494 #if 0 /* unused function */
495 static jb_err map_copy_parameter_url(struct map *out,
496                                      const struct map *in,
497                                      const char *name);
498 #endif /* unused function */
499
500 /* Internal convenience functions */
501 static char *section_target(const unsigned sectionid);
502
503 /*********************************************************************
504  *
505  * Function    :  section_target
506  *
507  * Description :  Given an unsigned (section id) n, produce a dynamically
508  *                allocated string of the form #l<n>, for use in link
509  *                targets.
510  *
511  * Parameters  :
512  *          1  :  sectionid = start line number of section
513  *
514  * Returns     :  String with link target, or NULL if out of
515  *                memory
516  *
517  *********************************************************************/
518 static char *section_target(const unsigned sectionid)
519 {
520    char buf[30];
521
522    snprintf(buf, 30, "#l%d", sectionid);
523    return(strdup(buf));
524
525 }
526
527
528 /*********************************************************************
529  *
530  * Function    :  map_copy_parameter_html
531  *
532  * Description :  Copy a CGI parameter from one map to another, HTML
533  *                encoding it.
534  *
535  * Parameters  :
536  *          1  :  out = target map
537  *          2  :  in = source map
538  *          3  :  name = name of cgi parameter to copy
539  *
540  * Returns     :  JB_ERR_OK on success
541  *                JB_ERR_MEMORY on out-of-memory
542  *                JB_ERR_CGI_PARAMS if the parameter doesn't exist
543  *                                  in the source map
544  *
545  *********************************************************************/
546 static jb_err map_copy_parameter_html(struct map *out,
547                                       const struct map *in,
548                                       const char *name)
549 {
550    const char * value;
551    jb_err err;
552
553    assert(out);
554    assert(in);
555    assert(name);
556
557    value = lookup(in, name);
558    err = map(out, name, 1, html_encode(value), 0);
559
560    if (err)
561    {
562       /* Out of memory */
563       return err;
564    }
565    else if (*value == '\0')
566    {
567       return JB_ERR_CGI_PARAMS;
568    }
569    else
570    {
571       return JB_ERR_OK;
572    }
573 }
574
575
576 #if 0 /* unused function */
577 /*********************************************************************
578  *
579  * Function    :  map_copy_parameter_url
580  *
581  * Description :  Copy a CGI parameter from one map to another, URL
582  *                encoding it.
583  *
584  * Parameters  :
585  *          1  :  out = target map
586  *          2  :  in = source map
587  *          3  :  name = name of cgi parameter to copy
588  *
589  * Returns     :  JB_ERR_OK on success
590  *                JB_ERR_MEMORY on out-of-memory
591  *                JB_ERR_CGI_PARAMS if the parameter doesn't exist
592  *                                  in the source map
593  *
594  *********************************************************************/
595 static jb_err map_copy_parameter_url(struct map *out,
596                                      const struct map *in,
597                                      const char *name)
598 {
599    const char * value;
600    jb_err err;
601
602    assert(out);
603    assert(in);
604    assert(name);
605
606    value = lookup(in, name);
607    err = map(out, name, 1, url_encode(value), 0);
608
609    if (err)
610    {
611       /* Out of memory */
612       return err;
613    }
614    else if (*value == '\0')
615    {
616       return JB_ERR_CGI_PARAMS;
617    }
618    else
619    {
620       return JB_ERR_OK;
621    }
622 }
623 #endif /* 0 - unused function */
624
625 /*********************************************************************
626  *
627  * Function    :  cgi_edit_actions_url_form
628  *
629  * Description :  CGI function that displays a form for
630  *                edit-actions-url
631  *
632  * Parameters  :
633  *          1  :  csp = Current client state (buffers, headers, etc...)
634  *          2  :  rsp = http_response data structure for output
635  *          3  :  parameters = map of cgi parameters
636  *
637  * CGI Parameters
638  *           f : (filename) Identifies the file to edit
639  *           v : (version) File's last-modified time
640  *           p : (pattern) Line number of pattern to edit
641  *
642  * Returns     :  JB_ERR_OK on success
643  *                JB_ERR_MEMORY on out-of-memory
644  *                JB_ERR_CGI_PARAMS if the CGI parameters are not
645  *                                  specified or not valid.
646  *
647  *********************************************************************/
648 jb_err cgi_edit_actions_url_form(struct client_state *csp,
649                                  struct http_response *rsp,
650                                  const struct map *parameters)
651 {
652    struct map * exports;
653    unsigned patternid;
654    struct editable_file * file;
655    struct file_line * cur_line;
656    unsigned line_number;
657    unsigned section_start_line_number = 0;
658    jb_err err;
659
660    assert(csp);
661    assert(rsp);
662    assert(parameters);
663
664    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
665    {
666       return cgi_error_disabled(csp, rsp);
667    }
668
669    err = get_number_param(csp, parameters, "p", &patternid);
670    if (err)
671    {
672       return err;
673    }
674
675    err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
676    if (err)
677    {
678       /* No filename specified, can't read file, modified, or out of memory. */
679       return (err == JB_ERR_FILE ? JB_ERR_OK : err);
680    }
681
682    cur_line = file->lines;
683
684    for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
685    {
686       if (cur_line->type == FILE_LINE_ACTION)
687       {
688          section_start_line_number = line_number;
689       }
690       cur_line = cur_line->next;
691    }
692
693    if ( (cur_line == NULL)
694      || (line_number != patternid)
695      || (patternid < 1)
696      || (cur_line->type != FILE_LINE_URL))
697    {
698       /* Invalid "patternid" parameter */
699       edit_free_file(file);
700       return JB_ERR_CGI_PARAMS;
701    }
702
703    if (NULL == (exports = default_exports(csp, NULL)))
704    {
705       edit_free_file(file);
706       return JB_ERR_MEMORY;
707    }
708
709    err = map(exports, "f", 1, file->identifier, 1);
710    if (!err) err = map(exports, "v", 1, file->version_str, 1);
711    if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
712    if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
713    if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
714
715    edit_free_file(file);
716
717    if (err)
718    {
719       free_map(exports);
720       return err;
721    }
722
723    return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
724 }
725
726
727 /*********************************************************************
728  *
729  * Function    :  cgi_edit_actions_add_url_form
730  *
731  * Description :  CGI function that displays a form for
732  *                edit-actions-url
733  *
734  * Parameters  :
735  *          1  :  csp = Current client state (buffers, headers, etc...)
736  *          2  :  rsp = http_response data structure for output
737  *          3  :  parameters = map of cgi parameters
738  *
739  * CGI Parameters :
740  *           f : (filename) Identifies the file to edit
741  *           v : (version) File's last-modified time
742  *           s : (section) Line number of section to edit
743  *
744  * Returns     :  JB_ERR_OK on success
745  *                JB_ERR_MEMORY on out-of-memory
746  *                JB_ERR_CGI_PARAMS if the CGI parameters are not
747  *                                  specified or not valid.
748  *
749  *********************************************************************/
750 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
751                                      struct http_response *rsp,
752                                      const struct map *parameters)
753 {
754    struct map *exports;
755    jb_err err;
756
757    assert(csp);
758    assert(rsp);
759    assert(parameters);
760
761    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
762    {
763       return cgi_error_disabled(csp, rsp);
764    }
765
766    if (NULL == (exports = default_exports(csp, NULL)))
767    {
768       return JB_ERR_MEMORY;
769    }
770
771    err = map_copy_parameter_html(exports, parameters, "f");
772    if (!err) err = map_copy_parameter_html(exports, parameters, "v");
773    if (!err) err = map_copy_parameter_html(exports, parameters, "s");
774
775    if (err)
776    {
777       free_map(exports);
778       return err;
779    }
780
781    return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
782 }
783
784
785 /*********************************************************************
786  *
787  * Function    :  cgi_edit_actions_remove_url_form
788  *
789  * Description :  CGI function that displays a form for
790  *                edit-actions-url
791  *
792  * Parameters  :
793  *          1  :  csp = Current client state (buffers, headers, etc...)
794  *          2  :  rsp = http_response data structure for output
795  *          3  :  parameters = map of cgi parameters
796  *
797  * CGI Parameters :
798  *           f : (filename) Identifies the file to edit
799  *           v : (version) File's last-modified time
800  *           p : (pattern) Line number of pattern to edit
801  *
802  * Returns     :  JB_ERR_OK on success
803  *                JB_ERR_MEMORY on out-of-memory
804  *                JB_ERR_CGI_PARAMS if the CGI parameters are not
805  *                                  specified or not valid.
806  *
807  *********************************************************************/
808 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
809                                      struct http_response *rsp,
810                                      const struct map *parameters)
811 {
812    struct map * exports;
813    unsigned patternid;
814    struct editable_file * file;
815    struct file_line * cur_line;
816    unsigned line_number;
817    unsigned section_start_line_number = 0;
818    jb_err err;
819
820    assert(csp);
821    assert(rsp);
822    assert(parameters);
823
824    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
825    {
826       return cgi_error_disabled(csp, rsp);
827    }
828
829    err = get_number_param(csp, parameters, "p", &patternid);
830    if (err)
831    {
832       return err;
833    }
834
835    err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
836    if (err)
837    {
838       /* No filename specified, can't read file, modified, or out of memory. */
839       return (err == JB_ERR_FILE ? JB_ERR_OK : err);
840    }
841
842    cur_line = file->lines;
843
844    for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
845    {
846       if (cur_line->type == FILE_LINE_ACTION)
847       {
848          section_start_line_number = line_number;
849       }      
850       cur_line = cur_line->next;
851    }
852
853    if ( (cur_line == NULL)
854      || (line_number != patternid)
855      || (patternid < 1)
856      || (cur_line->type != FILE_LINE_URL))
857    {
858       /* Invalid "patternid" parameter */
859       edit_free_file(file);
860       return JB_ERR_CGI_PARAMS;
861    }
862
863    if (NULL == (exports = default_exports(csp, NULL)))
864    {
865       edit_free_file(file);
866       return JB_ERR_MEMORY;
867    }
868
869    err = map(exports, "f", 1, file->identifier, 1);
870    if (!err) err = map(exports, "v", 1, file->version_str, 1);
871    if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
872    if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
873    if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
874
875    edit_free_file(file);
876
877    if (err)
878    {
879       free_map(exports);
880       return err;
881    }
882
883    return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
884 }
885
886
887 /*********************************************************************
888  *
889  * Function    :  edit_write_file
890  *
891  * Description :  Write a complete file to disk.
892  *
893  * Parameters  :
894  *          1  :  file = File to write.
895  *
896  * Returns     :  JB_ERR_OK     on success
897  *                JB_ERR_FILE   on error writing to file.
898  *                JB_ERR_MEMORY on out of memory
899  *
900  *********************************************************************/
901 jb_err edit_write_file(struct editable_file * file)
902 {
903    FILE * fp;
904    struct file_line * cur_line;
905    struct stat statbuf[1];
906    char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
907                             digits in time_t, assuming this is a 64-bit
908                             machine, plus null terminator, plus one
909                             for paranoia */
910
911    assert(file);
912    assert(file->filename);
913
914    if (NULL == (fp = fopen(file->filename, "wb")))
915    {
916       return JB_ERR_FILE;
917    }
918
919    cur_line = file->lines;
920    while (cur_line != NULL)
921    {
922       if (cur_line->raw)
923       {
924          if (fputs(cur_line->raw, fp) < 0)
925          {
926             fclose(fp);
927             return JB_ERR_FILE;
928          }
929       }
930       else
931       {
932          if (cur_line->prefix)
933          {
934             if (fputs(cur_line->prefix, fp) < 0)
935             {
936                fclose(fp);
937                return JB_ERR_FILE;
938             }
939          }
940          if (cur_line->unprocessed)
941          {
942
943             if (NULL != strchr(cur_line->unprocessed, '#'))
944             {
945                /* Must quote '#' characters */
946                int numhash = 0;
947                int len;
948                char * src;
949                char * dest;
950                char * str;
951
952                /* Count number of # characters, so we know length of output string */
953                src = cur_line->unprocessed;
954                while (NULL != (src = strchr(src, '#')))
955                {
956                   numhash++;
957                   src++;
958                }
959                assert(numhash > 0);
960
961                /* Allocate new memory for string */
962                len = strlen(cur_line->unprocessed);
963                if (NULL == (str = malloc((size_t) len + 1 + numhash)))
964                {
965                   /* Uh oh, just trashed file! */
966                   fclose(fp);
967                   return JB_ERR_MEMORY;
968                }
969
970                /* Loop through string from end */
971                src  = cur_line->unprocessed + len;
972                dest = str + len + numhash;
973                for ( ; len >= 0; len--)
974                {
975                   if ((*dest-- = *src--) == '#')
976                   {
977                      *dest-- = '\\';
978                      numhash--;
979                      assert(numhash >= 0);
980                   }
981                }
982                assert(numhash == 0);
983                assert(src  + 1 == cur_line->unprocessed);
984                assert(dest + 1 == str);
985
986                if (fputs(str, fp) < 0)
987                {
988                   free(str);
989                   fclose(fp);
990                   return JB_ERR_FILE;
991                }
992
993                free(str);
994             }
995             else
996             {
997                /* Can write without quoting '#' characters. */
998                if (fputs(cur_line->unprocessed, fp) < 0)
999                {
1000                   fclose(fp);
1001                   return JB_ERR_FILE;
1002                }
1003             }
1004             if (fputs(NEWLINE(file->newline), fp) < 0)
1005             {
1006                fclose(fp);
1007                return JB_ERR_FILE;
1008             }
1009          }
1010          else
1011          {
1012             /* FIXME: Write data from file->data->whatever */
1013             assert(0);
1014          }
1015       }
1016       cur_line = cur_line->next;
1017    }
1018
1019    fclose(fp);
1020
1021
1022    /* Update the version stamp in the file structure, since we just
1023     * wrote to the file & changed it's date.
1024     */
1025    if (stat(file->filename, statbuf) < 0)
1026    {
1027       /* Error, probably file not found. */
1028       return JB_ERR_FILE;
1029    }
1030    file->version = (unsigned)statbuf->st_mtime;
1031
1032    /* Correct file->version_str */
1033    freez(file->version_str);
1034    snprintf(version_buf, 22, "%u", file->version);
1035    version_buf[21] = '\0';
1036    file->version_str = strdup(version_buf);
1037    if (version_buf == NULL)
1038    {
1039       return JB_ERR_MEMORY;
1040    }
1041
1042    return JB_ERR_OK;
1043 }
1044
1045
1046 /*********************************************************************
1047  *
1048  * Function    :  edit_free_file
1049  *
1050  * Description :  Free a complete file in memory.
1051  *
1052  * Parameters  :
1053  *          1  :  file = Data structure to free.
1054  *
1055  * Returns     :  N/A
1056  *
1057  *********************************************************************/
1058 void edit_free_file(struct editable_file * file)
1059 {
1060    if (!file)
1061    {
1062       /* Silently ignore NULL pointer */
1063       return;
1064    }
1065
1066    edit_free_file_lines(file->lines);
1067    freez(file->filename);
1068    freez(file->identifier);
1069    freez(file->version_str);
1070    file->version = 0;
1071    file->parse_error_text = NULL; /* Statically allocated */
1072    file->parse_error = NULL;
1073
1074    free(file);
1075 }
1076
1077
1078 /*********************************************************************
1079  *
1080  * Function    :  edit_free_file_lines
1081  *
1082  * Description :  Free an entire linked list of file lines.
1083  *
1084  * Parameters  :
1085  *          1  :  first_line = Data structure to free.
1086  *
1087  * Returns     :  N/A
1088  *
1089  *********************************************************************/
1090 static void edit_free_file_lines(struct file_line * first_line)
1091 {
1092    struct file_line * next_line;
1093
1094    while (first_line != NULL)
1095    {
1096       next_line = first_line->next;
1097       first_line->next = NULL;
1098       freez(first_line->raw);
1099       freez(first_line->prefix);
1100       freez(first_line->unprocessed);
1101       switch(first_line->type)
1102       {
1103          case 0: /* special case if memory zeroed */
1104          case FILE_LINE_UNPROCESSED:
1105          case FILE_LINE_BLANK:
1106          case FILE_LINE_ALIAS_HEADER:
1107          case FILE_LINE_SETTINGS_HEADER:
1108          case FILE_LINE_DESCRIPTION_HEADER:
1109          case FILE_LINE_DESCRIPTION_ENTRY:
1110          case FILE_LINE_ALIAS_ENTRY:
1111          case FILE_LINE_URL:
1112             /* No data is stored for these */
1113             break;
1114
1115          case FILE_LINE_ACTION:
1116             free_action(first_line->data.action);
1117             break;
1118
1119          case FILE_LINE_SETTINGS_ENTRY:
1120             freez(first_line->data.setting.name);
1121             freez(first_line->data.setting.svalue);
1122             break;
1123          default:
1124             /* Should never happen */
1125             assert(0);
1126             break;
1127       }
1128       first_line->type = 0; /* paranoia */
1129       free(first_line);
1130       first_line = next_line;
1131    }
1132 }
1133
1134
1135 /*********************************************************************
1136  *
1137  * Function    :  match_actions_file_header_line
1138  *
1139  * Description :  Match an actions file {{header}} line
1140  *
1141  * Parameters  :
1142  *          1  :  line = String from file
1143  *          2  :  name = Header to match against
1144  *
1145  * Returns     :  0 iff they match.
1146  *
1147  *********************************************************************/
1148 static int match_actions_file_header_line(const char * line, const char * name)
1149 {
1150    size_t len;
1151
1152    assert(line);
1153    assert(name);
1154
1155    /* Look for "{{" */
1156    if ((line[0] != '{') || (line[1] != '{'))
1157    {
1158       return 1;
1159    }
1160    line += 2;
1161
1162    /* Look for optional whitespace */
1163    while ( (*line == ' ') || (*line == '\t') )
1164    {
1165       line++;
1166    }
1167
1168    /* Look for the specified name (case-insensitive) */
1169    len = strlen(name);
1170    if (0 != strncmpic(line, name, len))
1171    {
1172       return 1;
1173    }
1174    line += len;
1175
1176    /* Look for optional whitespace */
1177    while ( (*line == ' ') || (*line == '\t') )
1178    {
1179       line++;
1180    }
1181
1182    /* Look for "}}" and end of string*/
1183    if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1184    {
1185       return 1;
1186    }
1187
1188    /* It matched!! */
1189    return 0;
1190 }
1191
1192
1193 /*********************************************************************
1194  *
1195  * Function    :  match_actions_file_header_line
1196  *
1197  * Description :  Match an actions file {{header}} line
1198  *
1199  * Parameters  :
1200  *          1  :  line = String from file.  Must not start with
1201  *                       whitespace (else infinite loop!)
1202  *          2  :  pname = Destination for name
1203  *          2  :  pvalue = Destination for value
1204  *
1205  * Returns     :  JB_ERR_OK     on success
1206  *                JB_ERR_MEMORY on out-of-memory
1207  *                JB_ERR_PARSE  if there's no "=" sign, or if there's
1208  *                              nothing before the "=" sign (but empty
1209  *                              values *after* the "=" sign are legal).
1210  *
1211  *********************************************************************/
1212 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1213 {
1214    const char * name_end;
1215    const char * value_start;
1216    size_t name_len;
1217
1218    assert(line);
1219    assert(pname);
1220    assert(pvalue);
1221    assert(*line != ' ');
1222    assert(*line != '\t');
1223
1224    *pname = NULL;
1225    *pvalue = NULL;
1226
1227    value_start = strchr(line, '=');
1228    if ((value_start == NULL) || (value_start == line))
1229    {
1230       return JB_ERR_PARSE;
1231    }
1232
1233    name_end = value_start - 1;
1234
1235    /* Eat any whitespace before the '=' */
1236    while ((*name_end == ' ') || (*name_end == '\t'))
1237    {
1238       /*
1239        * we already know we must have at least 1 non-ws char
1240        * at start of buf - no need to check
1241        */
1242       name_end--;
1243    }
1244
1245    name_len = name_end - line + 1; /* Length excluding \0 */
1246    if (NULL == (*pname = (char *) malloc(name_len + 1)))
1247    {
1248       return JB_ERR_MEMORY;
1249    }
1250    strncpy(*pname, line, name_len);
1251    (*pname)[name_len] = '\0';
1252
1253    /* Eat any the whitespace after the '=' */
1254    value_start++;
1255    while ((*value_start == ' ') || (*value_start == '\t'))
1256    {
1257       value_start++;
1258    }
1259
1260    if (NULL == (*pvalue = strdup(value_start)))
1261    {
1262       free(*pname);
1263       *pname = NULL;
1264       return JB_ERR_MEMORY;
1265    }
1266
1267    return JB_ERR_OK;
1268 }
1269
1270
1271 /*********************************************************************
1272  *
1273  * Function    :  edit_parse_actions_file
1274  *
1275  * Description :  Parse an actions file in memory.
1276  *
1277  *                Passed linked list must have the "data" member
1278  *                zeroed, and must contain valid "next" and
1279  *                "unprocessed" fields.  The "raw" and "prefix"
1280  *                fields are ignored, and "type" is just overwritten.
1281  *
1282  *                Note that on error the file may have been
1283  *                partially parsed.
1284  *
1285  * Parameters  :
1286  *          1  :  file = Actions file to be parsed in-place.
1287  *
1288  * Returns     :  JB_ERR_OK     on success
1289  *                JB_ERR_MEMORY on out-of-memory
1290  *                JB_ERR_PARSE  on error
1291  *
1292  *********************************************************************/
1293 jb_err edit_parse_actions_file(struct editable_file * file)
1294 {
1295    struct file_line * cur_line;
1296    size_t len;
1297    const char * text; /* Text from a line */
1298    char * name;  /* For lines of the form name=value */
1299    char * value; /* For lines of the form name=value */
1300    struct action_alias * alias_list = NULL;
1301    jb_err err = JB_ERR_OK;
1302
1303    /* alias_list contains the aliases defined in this file.
1304     * It might be better to use the "file_line.data" fields
1305     * in the relavent places instead.
1306     */
1307
1308    cur_line = file->lines;
1309
1310    /* A note about blank line support: Blank lines should only
1311     * ever occur as the last line in the file.  This function
1312     * is more forgiving than that - FILE_LINE_BLANK can occur
1313     * anywhere.
1314     */
1315
1316    /* Skip leading blanks.  Should only happen if file is
1317     * empty (which is valid, but pointless).
1318     */
1319    while ( (cur_line != NULL)
1320         && (cur_line->unprocessed[0] == '\0') )
1321    {
1322       /* Blank line */
1323       cur_line->type = FILE_LINE_BLANK;
1324       cur_line = cur_line->next;
1325    }
1326
1327    if ( (cur_line != NULL)
1328      && (cur_line->unprocessed[0] != '{') )
1329    {
1330       /* File doesn't start with a header */
1331       file->parse_error = cur_line;
1332       file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1333       return JB_ERR_PARSE;
1334    }
1335
1336    if ( (cur_line != NULL) && (0 ==
1337       match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1338    {
1339       cur_line->type = FILE_LINE_SETTINGS_HEADER;
1340
1341       cur_line = cur_line->next;
1342       while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1343       {
1344          if (cur_line->unprocessed[0])
1345          {
1346             cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1347
1348             err = split_line_on_equals(cur_line->unprocessed,
1349                      &cur_line->data.setting.name,
1350                      &cur_line->data.setting.svalue);
1351             if (err == JB_ERR_MEMORY)
1352             {
1353                return err;
1354             }
1355             else if (err != JB_ERR_OK)
1356             {
1357                /* Line does not contain a name=value pair */
1358                file->parse_error = cur_line;
1359                file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1360                return JB_ERR_PARSE;
1361             }
1362          }
1363          else
1364          {
1365             cur_line->type = FILE_LINE_BLANK;
1366          }
1367          cur_line = cur_line->next;
1368       }
1369    }
1370
1371    if ( (cur_line != NULL) && (0 ==
1372       match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1373    {
1374       cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1375
1376       cur_line = cur_line->next;
1377       while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1378       {
1379          if (cur_line->unprocessed[0])
1380          {
1381             cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1382          }
1383          else
1384          {
1385             cur_line->type = FILE_LINE_BLANK;
1386          }
1387          cur_line = cur_line->next;
1388       }
1389    }
1390
1391    if ( (cur_line != NULL) && (0 ==
1392       match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1393    {
1394       cur_line->type = FILE_LINE_ALIAS_HEADER;
1395
1396       cur_line = cur_line->next;
1397       while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1398       {
1399          if (cur_line->unprocessed[0])
1400          {
1401             /* define an alias */
1402             struct action_alias * new_alias;
1403
1404             cur_line->type = FILE_LINE_ALIAS_ENTRY;
1405
1406             err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1407             if (err == JB_ERR_MEMORY)
1408             {
1409                return err;
1410             }
1411             else if (err != JB_ERR_OK)
1412             {
1413                /* Line does not contain a name=value pair */
1414                file->parse_error = cur_line;
1415                file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1416                return JB_ERR_PARSE;
1417             }
1418
1419             if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1420             {
1421                /* Out of memory */
1422                free(name);
1423                free(value);
1424                free_alias_list(alias_list);
1425                return JB_ERR_MEMORY;
1426             }
1427
1428             err = get_actions(value, alias_list, new_alias->action);
1429             if (err)
1430             {
1431                /* Invalid action or out of memory */
1432                free(name);
1433                free(value);
1434                free(new_alias);
1435                free_alias_list(alias_list);
1436                if (err == JB_ERR_MEMORY)
1437                {
1438                   return err;
1439                }
1440                else
1441                {
1442                   /* Line does not contain a name=value pair */
1443                   file->parse_error = cur_line;
1444                   file->parse_error_text = "This alias does not specify a valid set of actions.";
1445                   return JB_ERR_PARSE;
1446                }
1447             }
1448
1449             free(value);
1450
1451             new_alias->name = name;
1452
1453             /* add to list */
1454             new_alias->next = alias_list;
1455             alias_list = new_alias;
1456          }
1457          else
1458          {
1459             cur_line->type = FILE_LINE_BLANK;
1460          }
1461          cur_line = cur_line->next;
1462       }
1463    }
1464
1465    /* Header done, process the main part of the file */
1466    while (cur_line != NULL)
1467    {
1468       /* At this point, (cur_line->unprocessed[0] == '{') */
1469       assert(cur_line->unprocessed[0] == '{');
1470       text = cur_line->unprocessed + 1;
1471       len = strlen(text) - 1;
1472       if (text[len] != '}')
1473       {
1474          /* No closing } on header */
1475          free_alias_list(alias_list);
1476          file->parse_error = cur_line;
1477          file->parse_error_text = "Headers starting with '{' must have a "
1478             "closing bracket ('}').  Headers starting with two brackets ('{{') "
1479             "must close with two brackets ('}}').";
1480          return JB_ERR_PARSE;
1481       }
1482
1483       if (text[0] == '{')
1484       {
1485          /* An invalid {{ header.  */
1486          free_alias_list(alias_list);
1487          file->parse_error = cur_line;
1488          file->parse_error_text = "Unknown or unexpected two-bracket header.  "
1489             "Please remember that the system (two-bracket) headers must "
1490             "appear in the order {{settings}}, {{description}}, {{alias}}, "
1491             "and must appear before any actions (one-bracket) headers.  "
1492             "Also note that system headers may not be repeated.";
1493          return JB_ERR_PARSE;
1494       }
1495
1496       while ( (*text == ' ') || (*text == '\t') )
1497       {
1498          text++;
1499          len--;
1500       }
1501       while ( (len > 0)
1502            && ( (text[len - 1] == ' ')
1503              || (text[len - 1] == '\t') ) )
1504       {
1505          len--;
1506       }
1507
1508       cur_line->type = FILE_LINE_ACTION;
1509
1510       /* Remove {} and make copy */
1511       if (NULL == (value = (char *) malloc(len + 1)))
1512       {
1513          /* Out of memory */
1514          free_alias_list(alias_list);
1515          return JB_ERR_MEMORY;
1516       }
1517       strncpy(value, text, len);
1518       value[len] = '\0';
1519
1520       /* Get actions */
1521       err = get_actions(value, alias_list, cur_line->data.action);
1522       if (err)
1523       {
1524          /* Invalid action or out of memory */
1525          free(value);
1526          free_alias_list(alias_list);
1527          if (err == JB_ERR_MEMORY)
1528          {
1529             return err;
1530          }
1531          else
1532          {
1533             /* Line does not contain a name=value pair */
1534             file->parse_error = cur_line;
1535             file->parse_error_text = "This header does not specify a valid set of actions.";
1536             return JB_ERR_PARSE;
1537          }
1538       }
1539
1540       /* Done with string - it was clobbered anyway */
1541       free(value);
1542
1543       /* Process next line */
1544       cur_line = cur_line->next;
1545
1546       /* Loop processing URL patterns */
1547       while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1548       {
1549          if (cur_line->unprocessed[0])
1550          {
1551             /* Could parse URL here, but this isn't currently needed */
1552
1553             cur_line->type = FILE_LINE_URL;
1554          }
1555          else
1556          {
1557             cur_line->type = FILE_LINE_BLANK;
1558          }
1559          cur_line = cur_line->next;
1560       }
1561    } /* End main while(cur_line != NULL) loop */
1562
1563    free_alias_list(alias_list);
1564
1565    return JB_ERR_OK;
1566 }
1567
1568
1569 /*********************************************************************
1570  *
1571  * Function    :  edit_read_file_lines
1572  *
1573  * Description :  Read all the lines of a file into memory.
1574  *                Handles whitespace, comments and line continuation.
1575  *
1576  * Parameters  :
1577  *          1  :  fp = File to read from.  On return, this will be
1578  *                     at EOF but it will not have been closed.
1579  *          2  :  pfile = Destination for a linked list of file_lines.
1580  *                        Will be set to NULL on error.
1581  *          3  :  newline = How to handle newlines.
1582  *
1583  * Returns     :  JB_ERR_OK     on success
1584  *                JB_ERR_MEMORY on out-of-memory
1585  *
1586  *********************************************************************/
1587 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1588 {
1589    struct file_line * first_line; /* Keep for return value or to free */
1590    struct file_line * cur_line;   /* Current line */
1591    struct file_line * prev_line;  /* Entry with prev_line->next = cur_line */
1592    jb_err rval;
1593
1594    assert(fp);
1595    assert(pfile);
1596
1597    *pfile = NULL;
1598
1599    cur_line = first_line = zalloc(sizeof(struct file_line));
1600    if (cur_line == NULL)
1601    {
1602       return JB_ERR_MEMORY;
1603    }
1604
1605    cur_line->type = FILE_LINE_UNPROCESSED;
1606
1607    rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1608    if (rval)
1609    {
1610       /* Out of memory or empty file. */
1611       /* Note that empty file is not an error we propogate up */
1612       free(cur_line);
1613       return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1614    }
1615
1616    do
1617    {
1618       prev_line = cur_line;
1619       cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1620       if (cur_line == NULL)
1621       {
1622          /* Out of memory */
1623          edit_free_file_lines(first_line);
1624          return JB_ERR_MEMORY;
1625       }
1626
1627       cur_line->type = FILE_LINE_UNPROCESSED;
1628
1629       rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1630       if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1631       {
1632          /* Out of memory */
1633          edit_free_file_lines(first_line);
1634          return JB_ERR_MEMORY;
1635       }
1636
1637    }
1638    while (rval != JB_ERR_FILE);
1639
1640    /* EOF */
1641
1642    /* We allocated one too many - free it */
1643    prev_line->next = NULL;
1644    free(cur_line);
1645
1646    *pfile = first_line;
1647    return JB_ERR_OK;
1648 }
1649
1650
1651 /*********************************************************************
1652  *
1653  * Function    :  edit_read_file
1654  *
1655  * Description :  Read a complete file into memory.
1656  *                Handles CGI parameter parsing.  If requested, also
1657  *                checks the file's modification timestamp.
1658  *
1659  * Parameters  :
1660  *          1  :  csp = Current client state (buffers, headers, etc...)
1661  *          2  :  parameters = map of cgi parameters.
1662  *          3  :  require_version = true to check "ver" parameter.
1663  *          4  :  suffix = File extension, e.g. ".action".
1664  *          5  :  pfile = Destination for the file.  Will be set
1665  *                        to NULL on error.
1666  *
1667  * CGI Parameters :
1668  *    filename :  The name of the file to read, without the
1669  *                path or ".action" extension.
1670  *         ver :  (Only if require_version is nonzero)
1671  *                Timestamp of the actions file.  If wrong, this
1672  *                function fails with JB_ERR_MODIFIED.
1673  *
1674  * Returns     :  JB_ERR_OK     on success
1675  *                JB_ERR_MEMORY on out-of-memory
1676  *                JB_ERR_CGI_PARAMS if "filename" was not specified
1677  *                                  or is not valid.
1678  *                JB_ERR_FILE   if the file cannot be opened or
1679  *                              contains no data
1680  *                JB_ERR_MODIFIED if version checking was requested and
1681  *                                failed - the file was modified outside
1682  *                                of this CGI editor instance.
1683  *
1684  *********************************************************************/
1685 jb_err edit_read_file(struct client_state *csp,
1686                       const struct map *parameters,
1687                       int require_version,
1688                       const char *suffix,
1689                       struct editable_file **pfile)
1690 {
1691    struct file_line * lines;
1692    FILE * fp;
1693    jb_err err;
1694    char * filename;
1695    const char * identifier;
1696    struct editable_file * file;
1697    unsigned version = 0;
1698    struct stat statbuf[1];
1699    char version_buf[22];
1700    int newline = NEWLINE_UNKNOWN;
1701
1702    assert(csp);
1703    assert(parameters);
1704    assert(pfile);
1705
1706    *pfile = NULL;
1707
1708    err = get_file_name_param(csp, parameters, "f", suffix,
1709                              &filename, &identifier);
1710    if (err)
1711    {
1712       return err;
1713    }
1714
1715    if (stat(filename, statbuf) < 0)
1716    {
1717       /* Error, probably file not found. */
1718       free(filename);
1719       return JB_ERR_FILE;
1720    }
1721    version = (unsigned) statbuf->st_mtime;
1722
1723    if (require_version)
1724    {
1725       unsigned specified_version;
1726       err = get_number_param(csp, parameters, "v", &specified_version);
1727       if (err)
1728       {
1729          free(filename);
1730          return err;
1731       }
1732
1733       if (version != specified_version)
1734       {
1735          return JB_ERR_MODIFIED;
1736       }
1737    }
1738
1739    if (NULL == (fp = fopen(filename,"rb")))
1740    {
1741       free(filename);
1742       return JB_ERR_FILE;
1743    }
1744
1745    err = edit_read_file_lines(fp, &lines, &newline);
1746
1747    fclose(fp);
1748
1749    if (err)
1750    {
1751       free(filename);
1752       return err;
1753    }
1754
1755    file = (struct editable_file *) zalloc(sizeof(*file));
1756    if (err)
1757    {
1758       free(filename);
1759       edit_free_file_lines(lines);
1760       return err;
1761    }
1762
1763    file->lines = lines;
1764    file->newline = newline;
1765    file->filename = filename;
1766    file->version = version;
1767    file->identifier = url_encode(identifier);
1768
1769    if (file->identifier == NULL)
1770    {
1771       edit_free_file(file);
1772       return JB_ERR_MEMORY;
1773    }
1774
1775    /* Correct file->version_str */
1776    freez(file->version_str);
1777    snprintf(version_buf, 22, "%u", file->version);
1778    version_buf[21] = '\0';
1779    file->version_str = strdup(version_buf);
1780    if (version_buf == NULL)
1781    {
1782       edit_free_file(file);
1783       return JB_ERR_MEMORY;
1784    }
1785
1786    *pfile = file;
1787    return JB_ERR_OK;
1788 }
1789
1790
1791 /*********************************************************************
1792  *
1793  * Function    :  edit_read_actions_file
1794  *
1795  * Description :  Read a complete actions file into memory.
1796  *                Handles CGI parameter parsing.  If requested, also
1797  *                checks the file's modification timestamp.
1798  *
1799  *                If this function detects an error in the categories
1800  *                JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1801  *                then it handles it by filling in the specified
1802  *                response structure and returning JB_ERR_FILE.
1803  *
1804  * Parameters  :
1805  *          1  :  csp = Current client state (buffers, headers, etc...)
1806  *          2  :  rsp = HTTP response.  Only filled in on error.
1807  *          2  :  parameters = map of cgi parameters.
1808  *          3  :  require_version = true to check "ver" parameter.
1809  *          4  :  pfile = Destination for the file.  Will be set
1810  *                        to NULL on error.
1811  *
1812  * CGI Parameters :
1813  *    filename :  The name of the actions file to read, without the
1814  *                path or ".action" extension.
1815  *         ver :  (Only if require_version is nonzero)
1816  *                Timestamp of the actions file.  If wrong, this
1817  *                function fails with JB_ERR_MODIFIED.
1818  *
1819  * Returns     :  JB_ERR_OK     on success
1820  *                JB_ERR_MEMORY on out-of-memory
1821  *                JB_ERR_CGI_PARAMS if "filename" was not specified
1822  *                                  or is not valid.
1823  *                JB_ERR_FILE  if the file does not contain valid data,
1824  *                             or if file cannot be opened or
1825  *                             contains no data, or if version
1826  *                             checking was requested and failed.
1827  *
1828  *********************************************************************/
1829 jb_err edit_read_actions_file(struct client_state *csp,
1830                               struct http_response *rsp,
1831                               const struct map *parameters,
1832                               int require_version,
1833                               struct editable_file **pfile)
1834 {
1835    jb_err err;
1836    struct editable_file *file;
1837
1838    assert(csp);
1839    assert(parameters);
1840    assert(pfile);
1841
1842    *pfile = NULL;
1843
1844    err = edit_read_file(csp, parameters, require_version, ".action", &file);
1845    if (err)
1846    {
1847       /* Try to handle if possible */
1848       if (err == JB_ERR_FILE)
1849       {
1850          err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1851       }
1852       else if (err == JB_ERR_MODIFIED)
1853       {
1854          err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1855       }
1856       if (err == JB_ERR_OK)
1857       {
1858          /*
1859           * Signal to higher-level CGI code that there was a problem but we
1860           * handled it, they should just return JB_ERR_OK.
1861           */
1862          err = JB_ERR_FILE;
1863       }
1864       return err;
1865    }
1866
1867    err = edit_parse_actions_file(file);
1868    if (err)
1869    {
1870       if (err == JB_ERR_PARSE)
1871       {
1872          err = cgi_error_parse(csp, rsp, file);
1873          if (err == JB_ERR_OK)
1874          {
1875             /*
1876              * Signal to higher-level CGI code that there was a problem but we
1877              * handled it, they should just return JB_ERR_OK.
1878              */
1879             err = JB_ERR_FILE;
1880          }
1881       }
1882       edit_free_file(file);
1883       return err;
1884    }
1885
1886    *pfile = file;
1887    return JB_ERR_OK;
1888 }
1889
1890
1891 /*********************************************************************
1892  *
1893  * Function    :  get_file_name_param
1894  *
1895  * Description :  Get the name of the file to edit from the parameters
1896  *                passed to a CGI function.  This function handles
1897  *                security checks such as blocking urls containing
1898  *                "/" or ".", prepending the config file directory,
1899  *                and adding the specified suffix.
1900  *
1901  *                (This is an essential security check, otherwise
1902  *                users may be able to pass "../../../etc/passwd"
1903  *                and overwrite the password file [linux], "prn:"
1904  *                and print random data [Windows], etc...)
1905  *
1906  *                This function only allows filenames contining the
1907  *                characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1908  *                That's probably too restrictive but at least it's
1909  *                secure.
1910  *
1911  * Parameters  :
1912  *          1  :  csp = Current client state (buffers, headers, etc...)
1913  *          2  :  parameters = map of cgi parameters
1914  *          3  :  param_name = The name of the parameter to read
1915  *          4  :  suffix = File extension, e.g. ".actions"
1916  *          5  :  pfilename = destination for full filename.  Caller
1917  *                free()s.  Set to NULL on error.
1918  *          6  :  pparam = destination for partial filename,
1919  *                suitable for use in another URL.  Allocated as part
1920  *                of the map "parameters", so don't free it.
1921  *                Set to NULL if not specified.
1922  *
1923  * Returns     :  JB_ERR_OK         on success
1924  *                JB_ERR_MEMORY     on out-of-memory
1925  *                JB_ERR_CGI_PARAMS if "filename" was not specified
1926  *                                  or is not valid.
1927  *
1928  *********************************************************************/
1929 static jb_err get_file_name_param(struct client_state *csp,
1930                                   const struct map *parameters,
1931                                   const char *param_name,
1932                                   const char *suffix,
1933                                   char **pfilename,
1934                                   const char **pparam)
1935 {
1936    const char *param;
1937    const char *s;
1938    char *name;
1939    char *fullpath;
1940    char ch;
1941    int len;
1942
1943    assert(csp);
1944    assert(parameters);
1945    assert(suffix);
1946    assert(pfilename);
1947    assert(pparam);
1948
1949    *pfilename = NULL;
1950    *pparam = NULL;
1951
1952    param = lookup(parameters, param_name);
1953    if (!*param)
1954    {
1955       return JB_ERR_CGI_PARAMS;
1956    }
1957
1958    *pparam = param;
1959
1960    len = strlen(param);
1961    if (len >= FILENAME_MAX)
1962    {
1963       /* Too long. */
1964       return JB_ERR_CGI_PARAMS;
1965    }
1966
1967    /* Check every character to see if it's legal */
1968    s = param;
1969    while ((ch = *s++) != '\0')
1970    {
1971       if ( ((ch < 'A') || (ch > 'Z'))
1972         && ((ch < 'a') || (ch > 'z'))
1973         && ((ch < '0') || (ch > '9'))
1974         && (ch != '-')
1975         && (ch != '_') )
1976       {
1977          /* Probable hack attempt. */
1978          return JB_ERR_CGI_PARAMS;
1979       }
1980    }
1981
1982    /* Append extension */
1983    name = malloc(len + strlen(suffix) + 1);
1984    if (name == NULL)
1985    {
1986       return JB_ERR_MEMORY;
1987    }
1988    strcpy(name, param);
1989    strcpy(name + len, suffix);
1990
1991    /* Prepend path */
1992    fullpath = make_path(csp->config->confdir, name);
1993    free(name);
1994
1995    if (fullpath == NULL)
1996    {
1997       return JB_ERR_MEMORY;
1998    }
1999
2000    /* Success */
2001    *pfilename = fullpath;
2002
2003    return JB_ERR_OK;
2004 }
2005
2006
2007 /*********************************************************************
2008  *
2009  * Function    :  get_url_spec_param
2010  *
2011  * Description :  Get a URL pattern from the parameters
2012  *                passed to a CGI function.  Removes leading/trailing
2013  *                spaces and validates it.
2014  *
2015  * Parameters  :
2016  *          1  :  csp = Current client state (buffers, headers, etc...)
2017  *          2  :  parameters = map of cgi parameters
2018  *          3  :  name = Name of CGI parameter to read
2019  *          4  :  pvalue = destination for value.  Will be malloc()'d.
2020  *                         Set to NULL on error.
2021  *
2022  * Returns     :  JB_ERR_OK         on success
2023  *                JB_ERR_MEMORY     on out-of-memory
2024  *                JB_ERR_CGI_PARAMS if the parameter was not specified
2025  *                                  or is not valid.
2026  *
2027  *********************************************************************/
2028 static jb_err get_url_spec_param(struct client_state *csp,
2029                                  const struct map *parameters,
2030                                  const char *name,
2031                                  char **pvalue)
2032 {
2033    const char *orig_param;
2034    char *param;
2035    char *s;
2036    struct url_spec compiled[1];
2037    jb_err err;
2038
2039    assert(csp);
2040    assert(parameters);
2041    assert(name);
2042    assert(pvalue);
2043
2044    *pvalue = NULL;
2045
2046    orig_param = lookup(parameters, name);
2047    if (!*orig_param)
2048    {
2049       return JB_ERR_CGI_PARAMS;
2050    }
2051
2052    /* Copy and trim whitespace */
2053    param = strdup(orig_param);
2054    if (param == NULL)
2055    {
2056       return JB_ERR_MEMORY;
2057    }
2058    chomp(param);
2059
2060    /* Must be non-empty, and can't allow 1st character to be '{' */
2061    if (param[0] == '\0' || param[0] == '{')
2062    {
2063       free(param);
2064       return JB_ERR_CGI_PARAMS;
2065    }
2066
2067    /* Check for embedded newlines */
2068    for (s = param; *s != '\0'; s++)
2069    {
2070       if ((*s == '\r') || (*s == '\n'))
2071       {
2072          free(param);
2073          return JB_ERR_CGI_PARAMS;
2074       }
2075    }
2076
2077    /* Check that regex is valid */
2078    s = strdup(param);
2079    if (s == NULL)
2080    {
2081       free(param);
2082       return JB_ERR_MEMORY;
2083    }
2084    err = create_url_spec(compiled, s);
2085    free(s);
2086    if (err)
2087    {
2088       free(param);
2089       return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2090    }
2091    free_url_spec(compiled);
2092
2093    if (param[strlen(param) - 1] == '\\')
2094    {
2095       /*
2096        * Must protect trailing '\\' from becoming line continuation character.
2097        * Two methods: 1) If it's a domain only, add a trailing '/'.
2098        * 2) For path, add the do-nothing PCRE expression (?:) to the end
2099        */
2100       if (strchr(param, '/') == NULL)
2101       {
2102          err = string_append(&param, "/");
2103       }
2104       else
2105       {
2106          err = string_append(&param, "(?:)");
2107       }
2108       if (err)
2109       {
2110          return err;
2111       }
2112
2113       /* Check that the modified regex is valid */
2114       s = strdup(param);
2115       if (s == NULL)
2116       {
2117          free(param);
2118          return JB_ERR_MEMORY;
2119       }
2120       err = create_url_spec(compiled, s);
2121       free(s);
2122       if (err)
2123       {
2124          free(param);
2125          return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2126       }
2127       free_url_spec(compiled);
2128    }
2129
2130    *pvalue = param;
2131    return JB_ERR_OK;
2132 }
2133
2134 /*********************************************************************
2135  *
2136  * Function    :  map_radio
2137  *
2138  * Description :  Map a set of radio button values.  E.g. if you have
2139  *                3 radio buttons, declare them as:
2140  *                  <option type="radio" name="xyz" @xyz-a@>
2141  *                  <option type="radio" name="xyz" @xyz-b@>
2142  *                  <option type="radio" name="xyz" @xyz-c@>
2143  *                Then map one of the @xyz-?@ variables to "checked"
2144  *                and all the others to empty by calling:
2145  *                map_radio(exports, "xyz", "abc", sel)
2146  *                Where 'sel' is 'a', 'b', or 'c'.
2147  *
2148  * Parameters  :
2149  *          1  :  exports = Exports map to modify.
2150  *          2  :  optionname = name for map
2151  *          3  :  values = null-terminated list of values;
2152  *          4  :  value = Selected value.
2153  *
2154  * CGI Parameters : None
2155  *
2156  * Returns     :  JB_ERR_OK     on success
2157  *                JB_ERR_MEMORY on out-of-memory
2158  *
2159  *********************************************************************/
2160 static jb_err map_radio(struct map * exports,
2161                         const char * optionname,
2162                         const char * values,
2163                         int value)
2164 {
2165    size_t len;
2166    char * buf;
2167    char * p;
2168    char c;
2169
2170    assert(exports);
2171    assert(optionname);
2172    assert(values);
2173
2174    len = strlen(optionname);
2175    buf = malloc(len + 3);
2176    if (buf == NULL)
2177    {
2178       return JB_ERR_MEMORY;
2179    }
2180
2181    strcpy(buf, optionname);
2182    p = buf + len;
2183    *p++ = '-';
2184    p[1] = '\0';
2185
2186    while ((c = *values++) != '\0')
2187    {
2188       if (c != value)
2189       {
2190          *p = c;
2191          if (map(exports, buf, 1, "", 1))
2192          {
2193             return JB_ERR_MEMORY;
2194          }
2195       }
2196    }
2197
2198    *p = value;
2199    return map(exports, buf, 0, "checked", 1);
2200 }
2201
2202
2203 /*********************************************************************
2204  *
2205  * Function    :  cgi_error_modified
2206  *
2207  * Description :  CGI function that is called when a file is modified
2208  *                outside the CGI editor.
2209  *
2210  * Parameters  :
2211  *          1  :  csp = Current client state (buffers, headers, etc...)
2212  *          2  :  rsp = http_response data structure for output
2213  *          3  :  filename = The file that was modified.
2214  *
2215  * CGI Parameters : none
2216  *
2217  * Returns     :  JB_ERR_OK on success
2218  *                JB_ERR_MEMORY on out-of-memory error.
2219  *
2220  *********************************************************************/
2221 jb_err cgi_error_modified(struct client_state *csp,
2222                           struct http_response *rsp,
2223                           const char *filename)
2224 {
2225    struct map *exports;
2226    jb_err err;
2227
2228    assert(csp);
2229    assert(rsp);
2230    assert(filename);
2231
2232    if (NULL == (exports = default_exports(csp, NULL)))
2233    {
2234       return JB_ERR_MEMORY;
2235    }
2236
2237    err = map(exports, "f", 1, html_encode(filename), 0);
2238    if (err)
2239    {
2240       free_map(exports);
2241       return err;
2242    }
2243
2244    return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2245 }
2246
2247
2248 /*********************************************************************
2249  *
2250  * Function    :  cgi_error_parse
2251  *
2252  * Description :  CGI function that is called when a file cannot
2253  *                be parsed by the CGI editor.
2254  *
2255  * Parameters  :
2256  *          1  :  csp = Current client state (buffers, headers, etc...)
2257  *          2  :  rsp = http_response data structure for output
2258  *          3  :  file = The file that was modified.
2259  *
2260  * CGI Parameters : none
2261  *
2262  * Returns     :  JB_ERR_OK on success
2263  *                JB_ERR_MEMORY on out-of-memory error.
2264  *
2265  *********************************************************************/
2266 jb_err cgi_error_parse(struct client_state *csp,
2267                        struct http_response *rsp,
2268                        struct editable_file *file)
2269 {
2270    struct map *exports;
2271    jb_err err;
2272    struct file_line *cur_line;
2273
2274    assert(csp);
2275    assert(rsp);
2276    assert(file);
2277
2278    if (NULL == (exports = default_exports(csp, NULL)))
2279    {
2280       return JB_ERR_MEMORY;
2281    }
2282
2283    err = map(exports, "f", 1, file->identifier, 1);
2284    if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2285
2286    cur_line = file->parse_error;
2287    assert(cur_line);
2288
2289    if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2290    if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2291
2292    if (err)
2293    {
2294       free_map(exports);
2295       return err;
2296    }
2297
2298    return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2299 }
2300
2301
2302 /*********************************************************************
2303  *
2304  * Function    :  cgi_error_file
2305  *
2306  * Description :  CGI function that is called when a file cannot be
2307  *                opened by the CGI editor.
2308  *
2309  * Parameters  :
2310  *          1  :  csp = Current client state (buffers, headers, etc...)
2311  *          2  :  rsp = http_response data structure for output
2312  *          3  :  filename = The file that was modified.
2313  *
2314  * CGI Parameters : none
2315  *
2316  * Returns     :  JB_ERR_OK on success
2317  *                JB_ERR_MEMORY on out-of-memory error.
2318  *
2319  *********************************************************************/
2320 jb_err cgi_error_file(struct client_state *csp,
2321                       struct http_response *rsp,
2322                       const char *filename)
2323 {
2324    struct map *exports;
2325    jb_err err;
2326
2327    assert(csp);
2328    assert(rsp);
2329    assert(filename);
2330
2331    if (NULL == (exports = default_exports(csp, NULL)))
2332    {
2333       return JB_ERR_MEMORY;
2334    }
2335
2336    err = map(exports, "f", 1, html_encode(filename), 0);
2337    if (err)
2338    {
2339       free_map(exports);
2340       return err;
2341    }
2342
2343    return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2344 }
2345
2346
2347 /*********************************************************************
2348  *
2349  * Function    :  cgi_error_file
2350  *
2351  * Description :  CGI function that is called when a file cannot be
2352  *                opened for writing by the CGI editor.
2353  *
2354  * Parameters  :
2355  *          1  :  csp = Current client state (buffers, headers, etc...)
2356  *          2  :  rsp = http_response data structure for output
2357  *          3  :  filename = The file that we can't write to
2358  *
2359  * CGI Parameters : none
2360  *
2361  * Returns     :  JB_ERR_OK on success
2362  *                JB_ERR_MEMORY on out-of-memory error.
2363  *
2364  *********************************************************************/
2365 jb_err cgi_error_file_read_only(struct client_state *csp,
2366                                 struct http_response *rsp,
2367                                 const char *filename)
2368 {
2369    struct map *exports;
2370    jb_err err;
2371
2372    assert(csp);
2373    assert(rsp);
2374    assert(filename);
2375
2376    if (NULL == (exports = default_exports(csp, NULL)))
2377    {
2378       return JB_ERR_MEMORY;
2379    }
2380
2381    err = map(exports, "f", 1, html_encode(filename), 0);
2382    if (err)
2383    {
2384       free_map(exports);
2385       return err;
2386    }
2387
2388    return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2389 }
2390
2391
2392 /*********************************************************************
2393  *
2394  * Function    :  cgi_error_disabled
2395  *
2396  * Description :  CGI function that is called if the actions editor
2397  *                is called although it's disabled in config
2398  *
2399  * Parameters  :
2400  *          1  :  csp = Current client state (buffers, headers, etc...)
2401  *          2  :  rsp = http_response data structure for output
2402  *
2403  * CGI Parameters : none
2404  *
2405  * Returns     :  JB_ERR_OK on success
2406  *                JB_ERR_MEMORY on out-of-memory error.
2407  *
2408  *********************************************************************/
2409 jb_err cgi_error_disabled(struct client_state *csp,
2410                           struct http_response *rsp)
2411 {
2412    struct map *exports;
2413
2414    assert(csp);
2415    assert(rsp);
2416
2417    if (NULL == (exports = default_exports(csp, NULL)))
2418    {
2419       return JB_ERR_MEMORY;
2420    }
2421
2422    return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2423 }
2424
2425
2426 /*********************************************************************
2427  *
2428  * Function    :  cgi_edit_actions
2429  *
2430  * Description :  CGI function that allows the user to choose which
2431  *                actions file to edit.
2432  *
2433  * Parameters  :
2434  *          1  :  csp = Current client state (buffers, headers, etc...)
2435  *          2  :  rsp = http_response data structure for output
2436  *          3  :  parameters = map of cgi parameters
2437  *
2438  * CGI Parameters : None
2439  *
2440  * Returns     :  JB_ERR_OK on success
2441  *                JB_ERR_MEMORY on out-of-memory error
2442  *
2443  *********************************************************************/
2444 jb_err cgi_edit_actions(struct client_state *csp,
2445                         struct http_response *rsp,
2446                         const struct map *parameters)
2447 {
2448
2449    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2450    {
2451       return cgi_error_disabled(csp, rsp);
2452    }
2453
2454    /* FIXME: Incomplete */
2455    rsp->status = strdup("302 Local Redirect from Privoxy");
2456    if (rsp->status == NULL)
2457    {
2458       return JB_ERR_MEMORY;
2459    }
2460    if (enlist_unique_header(rsp->headers, "Location",
2461       CGI_PREFIX "edit-actions-list?f=default"))
2462    {
2463       free(rsp->status);
2464       rsp->status = NULL;
2465       return JB_ERR_MEMORY;
2466    }
2467
2468    return JB_ERR_OK;
2469 }
2470
2471
2472 /*********************************************************************
2473  *
2474  * Function    :  cgi_edit_actions_list
2475  *
2476  * Description :  CGI function that edits the actions list.
2477  *                FIXME: This function shouldn't FATAL ever.
2478  *                FIXME: This function doesn't check the retval of map()
2479  * Parameters  :
2480  *          1  :  csp = Current client state (buffers, headers, etc...)
2481  *          2  :  rsp = http_response data structure for output
2482  *          3  :  parameters = map of cgi parameters
2483  *
2484  * CGI Parameters : filename
2485  *
2486  * Returns     :  JB_ERR_OK     on success
2487  *                JB_ERR_MEMORY on out-of-memory
2488  *                JB_ERR_FILE   if the file cannot be opened or
2489  *                              contains no data
2490  *                JB_ERR_CGI_PARAMS if "filename" was not specified
2491  *                                  or is not valid.
2492  *
2493  *********************************************************************/
2494 jb_err cgi_edit_actions_list(struct client_state *csp,
2495                              struct http_response *rsp,
2496                              const struct map *parameters)
2497 {
2498    char * section_template;
2499    char * url_template;
2500    char * sections;
2501    char * urls;
2502    char buf[150];
2503    char * s;
2504    struct map * exports;
2505    struct map * section_exports;
2506    struct map * url_exports;
2507    struct editable_file * file;
2508    struct file_line * cur_line;
2509    unsigned line_number = 0;
2510    unsigned prev_section_line_number = ((unsigned) (-1));
2511    int i, url_1_2;
2512    struct file_list * fl;
2513    struct url_actions * b;
2514    char * buttons = NULL;
2515    jb_err err;
2516
2517    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2518    {
2519       return cgi_error_disabled(csp, rsp);
2520    }
2521
2522    if (NULL == (exports = default_exports(csp, NULL)))
2523    {
2524       return JB_ERR_MEMORY;
2525    }
2526
2527    /* Load actions file */
2528    err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2529    if (err)
2530    {
2531       /* No filename specified, can't read file, or out of memory. */
2532       return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2533    }
2534
2535    /* Find start of actions in file */
2536    cur_line = file->lines;
2537    line_number = 1;
2538    while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2539    {
2540       cur_line = cur_line->next;
2541       line_number++;
2542    }
2543
2544    /*
2545     * Conventional actions files should have a match all block
2546     * at the start:
2547     * cur_line             = {...global actions...}
2548     * cur_line->next       = /
2549     * cur_line->next->next = {...actions...} or EOF
2550     */
2551    if ( (cur_line != NULL)
2552      && (cur_line->type == FILE_LINE_ACTION)
2553      && (cur_line->next != NULL)
2554      && (cur_line->next->type == FILE_LINE_URL)
2555      && (0 == strcmp(cur_line->next->unprocessed, "/"))
2556      && ( (cur_line->next->next == NULL)
2557        || (cur_line->next->next->type != FILE_LINE_URL)
2558       ) )
2559    {
2560       /*
2561        * Generate string with buttons to set actions for "/" to
2562        * any predefined set of actions (named standard.*, probably
2563        * residing in standard.action).
2564        */
2565
2566       err = template_load(csp, &section_template, "edit-actions-list-button", 0);
2567       if (err)
2568       {
2569          edit_free_file(file);
2570          free_map(exports);
2571          if (err == JB_ERR_FILE)
2572          {
2573             return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2574          }
2575          return err;
2576       }
2577
2578       err = template_fill(&section_template, exports);
2579       if (err)
2580       {
2581          edit_free_file(file);
2582          free_map(exports);
2583          return err;
2584       }
2585
2586       buttons = strdup("");
2587       for (i = 0; i < MAX_ACTION_FILES; i++)
2588       {
2589          if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2590          {
2591             for (b = b->next; NULL != b; b = b->next)
2592             {
2593                if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2594                {
2595                   if (err || (NULL == (section_exports = new_map())))
2596                   {
2597                      freez(buttons);
2598                      free(section_template);
2599                      edit_free_file(file);
2600                      free_map(exports);
2601                      return JB_ERR_MEMORY;
2602                   }
2603
2604                   err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2605
2606                   if (err || (NULL == (s = strdup(section_template))))
2607                   {
2608                      free_map(section_exports);
2609                      freez(buttons);
2610                      free(section_template);
2611                      edit_free_file(file);
2612                      free_map(exports);
2613                      return JB_ERR_MEMORY;
2614                   }
2615
2616                   if (!err) err = template_fill(&s, section_exports);
2617                   free_map(section_exports);
2618                   if (!err) err = string_join(&buttons, s);
2619                }
2620             }
2621          }
2622       }
2623       freez(section_template);
2624       if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2625
2626       /*
2627        * Conventional actions file, supply extra editing help.
2628        * (e.g. don't allow them to make it an unconventional one).
2629        */
2630       if (!err) err = map_conditional(exports, "all-urls-present", 1);
2631
2632       snprintf(buf, 150, "%d", line_number);
2633       if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2634       snprintf(buf, 150, "%d", line_number + 2);
2635       if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2636       if (!err) err = map(exports, "all-urls-actions", 1,
2637                           actions_to_html(csp, cur_line->data.action), 0);
2638
2639        /* Skip the 2 lines */
2640       cur_line = cur_line->next->next;
2641       line_number += 2;
2642
2643       /*
2644        * Note that prev_section_line_number is NOT set here.
2645        * This is deliberate and not a bug.  It stops a "Move up"
2646        * option appearing on the next section.  Clicking "Move
2647        * up" would make the actions file unconventional, which
2648        * we don't want, so we hide this option.
2649        */
2650    }
2651    else
2652    {
2653       /*
2654        * Non-standard actions file - does not begin with
2655        * the "All URLs" section.
2656        */
2657       if (!err) err = map_conditional(exports, "all-urls-present", 0);
2658    }
2659
2660    /* Set up global exports */
2661
2662    if (!err) err = map(exports, "f", 1, file->identifier, 1);
2663    if (!err) err = map(exports, "v", 1, file->version_str, 1);
2664
2665    /* Discourage private additions to default.action */
2666
2667    if (!err) err = map_conditional(exports, "default-action",
2668                                    (strcmp("default", lookup(parameters, "f")) == 0));
2669    if (err)
2670    {
2671       edit_free_file(file);
2672       free_map(exports);
2673       return err;
2674    }
2675
2676    /* Should do all global exports above this point */
2677
2678    /* Load templates */
2679
2680    err = template_load(csp, &section_template, "edit-actions-list-section", 0);
2681    if (err)
2682    {
2683       edit_free_file(file);
2684       free_map(exports);
2685       if (err == JB_ERR_FILE)
2686       {
2687          return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2688       }
2689       return err;
2690    }
2691
2692    err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2693    if (err)
2694    {
2695       free(section_template);
2696       edit_free_file(file);
2697       free_map(exports);
2698       if (err == JB_ERR_FILE)
2699       {
2700          return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2701       }
2702       return err;
2703    }
2704
2705    err = template_fill(&section_template, exports);
2706    if (err)
2707    {
2708       free(url_template);
2709       edit_free_file(file);
2710       free_map(exports);
2711       free(url_template);
2712       return err;
2713    }
2714
2715    err = template_fill(&url_template, exports);
2716    if (err)
2717    {
2718       free(section_template);
2719       edit_free_file(file);
2720       free_map(exports);
2721       return err;
2722    }
2723
2724    if (NULL == (sections = strdup("")))
2725    {
2726       free(section_template);
2727       free(url_template);
2728       edit_free_file(file);
2729       free_map(exports);
2730       return JB_ERR_MEMORY;
2731    }
2732
2733    while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2734    {
2735       if (NULL == (section_exports = new_map()))
2736       {
2737          free(sections);
2738          free(section_template);
2739          free(url_template);
2740          edit_free_file(file);
2741          free_map(exports);
2742          return JB_ERR_MEMORY;
2743       }
2744
2745       snprintf(buf, 150, "%d", line_number);
2746       err = map(section_exports, "s", 1, buf, 1);
2747       if (!err) err = map(section_exports, "actions", 1,
2748                           actions_to_html(csp, cur_line->data.action), 0);
2749
2750       if ( (!err)
2751         && (cur_line->next != NULL)
2752         && (cur_line->next->type == FILE_LINE_URL))
2753       {
2754          /* This section contains at least one URL, don't allow delete */
2755          err = map_block_killer(section_exports, "empty-section");
2756       }
2757       else
2758       {
2759          if (!err) err = map_block_keep(section_exports, "empty-section");
2760       }
2761
2762       if (prev_section_line_number != ((unsigned)(-1)))
2763       {
2764          /* Not last section */
2765          snprintf(buf, 150, "%d", prev_section_line_number);
2766          if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2767          if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2768       }
2769       else
2770       {
2771          /* Last section */
2772          if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2773       }
2774       prev_section_line_number = line_number;
2775
2776       if (err)
2777       {
2778          free(sections);
2779          free(section_template);
2780          free(url_template);
2781          edit_free_file(file);
2782          free_map(exports);
2783          free_map(section_exports);
2784          return err;
2785       }
2786
2787       /* Should do all section-specific exports above this point */
2788
2789       if (NULL == (urls = strdup("")))
2790       {
2791          free(sections);
2792          free(section_template);
2793          free(url_template);
2794          edit_free_file(file);
2795          free_map(exports);
2796          free_map(section_exports);
2797          return JB_ERR_MEMORY;
2798       }
2799
2800       url_1_2 = 2;
2801
2802       cur_line = cur_line->next;
2803       line_number++;
2804
2805       while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2806       {
2807          if (NULL == (url_exports = new_map()))
2808          {
2809             free(urls);
2810             free(sections);
2811             free(section_template);
2812             free(url_template);
2813             edit_free_file(file);
2814             free_map(exports);
2815             free_map(section_exports);
2816             return JB_ERR_MEMORY;
2817          }
2818
2819          snprintf(buf, 150, "%d", line_number);
2820          err = map(url_exports, "p", 1, buf, 1);
2821
2822          snprintf(buf, 150, "%d", url_1_2);
2823          if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2824
2825          if (!err) err = map(url_exports, "url-html", 1,
2826                              html_encode(cur_line->unprocessed), 0);
2827          if (!err) err = map(url_exports, "url", 1,
2828                              url_encode(cur_line->unprocessed), 0);
2829
2830          if (err)
2831          {
2832             free(urls);
2833             free(sections);
2834             free(section_template);
2835             free(url_template);
2836             edit_free_file(file);
2837             free_map(exports);
2838             free_map(section_exports);
2839             free_map(url_exports);
2840             return err;
2841          }
2842
2843          if (NULL == (s = strdup(url_template)))
2844          {
2845             free(urls);
2846             free(sections);
2847             free(section_template);
2848             free(url_template);
2849             edit_free_file(file);
2850             free_map(exports);
2851             free_map(section_exports);
2852             free_map(url_exports);
2853             return JB_ERR_MEMORY;
2854          }
2855
2856          err = template_fill(&s, section_exports);
2857          if (!err) err = template_fill(&s, url_exports);
2858          if (!err) err = string_append(&urls, s);
2859
2860          free_map(url_exports);
2861          freez(s);
2862
2863          if (err)
2864          {
2865             freez(urls);
2866             free(sections);
2867             free(section_template);
2868             free(url_template);
2869             edit_free_file(file);
2870             free_map(exports);
2871             free_map(section_exports);
2872             return err;
2873          }
2874
2875          url_1_2 = 3 - url_1_2;
2876
2877          cur_line = cur_line->next;
2878          line_number++;
2879       }
2880
2881       err = map(section_exports, "urls", 1, urls, 0);
2882
2883       /* Could also do section-specific exports here, but it wouldn't be as fast */
2884
2885       snprintf(buf, 150, "%d", line_number);
2886       if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2887
2888       if ( (cur_line != NULL)
2889         && (cur_line->type == FILE_LINE_ACTION))
2890       {
2891          /* Not last section */
2892          if (!err) err = map_block_keep(section_exports, "s-next-exists");
2893       }
2894       else
2895       {
2896          /* Last section */
2897          if (!err) err = map_block_killer(section_exports, "s-next-exists");
2898       }
2899
2900       if (err)
2901       {
2902          free(sections);
2903          free(section_template);
2904          free(url_template);
2905          edit_free_file(file);
2906          free_map(exports);
2907          free_map(section_exports);
2908          return err;
2909       }
2910
2911       if (NULL == (s = strdup(section_template)))
2912       {
2913          free(sections);
2914          free(section_template);
2915          free(url_template);
2916          edit_free_file(file);
2917          free_map(exports);
2918          free_map(section_exports);
2919          return JB_ERR_MEMORY;
2920       }
2921
2922       err = template_fill(&s, section_exports);
2923       if (!err) err = string_append(&sections, s);
2924
2925       freez(s);
2926       free_map(section_exports);
2927
2928       if (err)
2929       {
2930          freez(sections);
2931          free(section_template);
2932          free(url_template);
2933          edit_free_file(file);
2934          free_map(exports);
2935          return err;
2936       }
2937    }
2938
2939    edit_free_file(file);
2940    free(section_template);
2941    free(url_template);
2942
2943    err = map(exports, "sections", 1, sections, 0);
2944    if (err)
2945    {
2946       free_map(exports);
2947       return err;
2948    }
2949
2950    /* Could also do global exports here, but it wouldn't be as fast */
2951
2952    return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2953 }
2954
2955
2956 /*********************************************************************
2957  *
2958  * Function    :  cgi_edit_actions_for_url
2959  *
2960  * Description :  CGI function that edits the Actions list.
2961  *
2962  * Parameters  :
2963  *          1  :  csp = Current client state (buffers, headers, etc...)
2964  *          2  :  rsp = http_response data structure for output
2965  *          3  :  parameters = map of cgi parameters
2966  *
2967  * CGI Parameters : None
2968  *
2969  * Returns     :  JB_ERR_OK     on success
2970  *                JB_ERR_MEMORY on out-of-memory
2971  *                JB_ERR_CGI_PARAMS if the CGI parameters are not
2972  *                                  specified or not valid.
2973  *
2974  *********************************************************************/
2975 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2976                                 struct http_response *rsp,
2977                                 const struct map *parameters)
2978 {
2979    struct map * exports;
2980    unsigned sectionid;
2981    struct editable_file * file;
2982    struct file_line * cur_line;
2983    unsigned line_number;
2984    jb_err err;
2985    struct file_list *filter_file;
2986    struct re_filterfile_spec *filter_group;
2987
2988    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2989    {
2990       return cgi_error_disabled(csp, rsp);
2991    }
2992
2993    err = get_number_param(csp, parameters, "s", &sectionid);
2994    if (err)
2995    {
2996       return err;
2997    }
2998
2999    err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3000    if (err)
3001    {
3002       /* No filename specified, can't read file, modified, or out of memory. */
3003       return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3004    }
3005
3006    cur_line = file->lines;
3007
3008    for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3009    {
3010       cur_line = cur_line->next;
3011    }
3012
3013    if ( (cur_line == NULL)
3014      || (line_number != sectionid)
3015      || (sectionid < 1)
3016      || (cur_line->type != FILE_LINE_ACTION))
3017    {
3018       /* Invalid "sectionid" parameter */
3019       edit_free_file(file);
3020       return JB_ERR_CGI_PARAMS;
3021    }
3022
3023    if (NULL == (exports = default_exports(csp, NULL)))
3024    {
3025       edit_free_file(file);
3026       return JB_ERR_MEMORY;
3027    }
3028
3029    err = map(exports, "f", 1, file->identifier, 1);
3030    if (!err) err = map(exports, "v", 1, file->version_str, 1);
3031    if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3032
3033    if (!err) err = actions_to_radio(exports, cur_line->data.action);
3034
3035    filter_file = csp->rlist;
3036    filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
3037
3038    if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
3039
3040    if (err)
3041    {
3042       edit_free_file(file);
3043       free_map(exports);
3044       return err;
3045    }
3046
3047    if (filter_group == NULL)
3048    {
3049       err = map(exports, "filter-params", 1, "", 1);
3050    }
3051    else
3052    {
3053       /* We have some entries in the filter list */
3054       char * result;
3055       int index = 0;
3056       char * filter_template;
3057
3058       err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3059       if (err)
3060       {
3061          edit_free_file(file);
3062          free_map(exports);
3063          if (err == JB_ERR_FILE)
3064          {
3065             return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3066          }
3067          return err;
3068       }
3069
3070       err = template_fill(&filter_template, exports);
3071
3072       result = strdup("");
3073
3074       for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3075       {
3076          char current_mode = 'x';
3077          struct list_entry *filter_name;
3078          char * this_line;
3079          struct map *line_exports;
3080          char number[20];
3081
3082          filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
3083          while ((filter_name != NULL)
3084              && (0 != strcmp(filter_group->name, filter_name->str)))
3085          {
3086               filter_name = filter_name->next;
3087          }
3088
3089          if (filter_name != NULL)
3090          {
3091             current_mode = 'y';
3092          }
3093          else
3094          {
3095             filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
3096             while ((filter_name != NULL)
3097                 && (0 != strcmp(filter_group->name, filter_name->str)))
3098             {
3099                  filter_name = filter_name->next;
3100             }
3101             if (filter_name != NULL)
3102             {
3103                current_mode = 'n';
3104             }
3105          }
3106
3107          /* Generate a unique serial number */
3108          snprintf(number, sizeof(number), "%x", index++);
3109          number[sizeof(number) - 1] = '\0';
3110
3111          line_exports = new_map();
3112          if (line_exports == NULL)
3113          {
3114             err = JB_ERR_MEMORY;
3115             freez(result);
3116          }
3117          else
3118          {
3119             if (!err) err = map(line_exports, "index", 1, number, 1);
3120             if (!err) err = map(line_exports, "name",  1, filter_group->name, 1);
3121             if (!err) err = map(line_exports, "description",  1, filter_group->description, 1);
3122             if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3123
3124             this_line = NULL;
3125             if (!err)
3126             {
3127                this_line = strdup(filter_template);
3128                if (this_line == NULL) err = JB_ERR_MEMORY;
3129             }
3130             if (!err) err = template_fill(&this_line, line_exports);
3131             string_join(&result, this_line);
3132
3133             free_map(line_exports);
3134          }
3135       }
3136
3137       freez(filter_template);
3138
3139       if (!err)
3140       {
3141          err = map(exports, "filter-params", 1, result, 0);
3142       }
3143       else
3144       {
3145          freez(result);
3146       }
3147    }
3148
3149    if (!err) err = map_radio(exports, "filter-all", "nx",
3150       (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3151
3152    edit_free_file(file);
3153
3154    if (err)
3155    {
3156       free_map(exports);
3157       return err;
3158    }
3159
3160    return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3161 }
3162
3163
3164 /*********************************************************************
3165  *
3166  * Function    :  cgi_edit_actions_submit
3167  *
3168  * Description :  CGI function that actually edits the Actions list.
3169  *
3170  * Parameters  :
3171  *          1  :  csp = Current client state (buffers, headers, etc...)
3172  *          2  :  rsp = http_response data structure for output
3173  *          3  :  parameters = map of cgi parameters
3174  *
3175  * CGI Parameters : None
3176  *
3177  * Returns     :  JB_ERR_OK     on success
3178  *                JB_ERR_MEMORY on out-of-memory
3179  *                JB_ERR_CGI_PARAMS if the CGI parameters are not
3180  *                                  specified or not valid.
3181  *
3182  *********************************************************************/
3183 jb_err cgi_edit_actions_submit(struct client_state *csp,
3184                                struct http_response *rsp,
3185                                const struct map *parameters)
3186 {
3187    unsigned sectionid;
3188    char * actiontext;
3189    char * newtext;
3190    size_t len;
3191    struct editable_file * file;
3192    struct file_line * cur_line;
3193    unsigned line_number;
3194    char * target;
3195    jb_err err;
3196    int index;
3197    const char * action_set_name;
3198    char ch;
3199    struct file_list * fl;
3200    struct url_actions * b;
3201
3202    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3203    {
3204       return cgi_error_disabled(csp, rsp);
3205    }
3206
3207    err = get_number_param(csp, parameters, "s", &sectionid);
3208    if (err)
3209    {
3210       return err;
3211    }
3212
3213    err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3214    if (err)
3215    {
3216       /* No filename specified, can't read file, modified, or out of memory. */
3217       return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3218    }
3219
3220    cur_line = file->lines;
3221
3222    for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3223    {
3224       cur_line = cur_line->next;
3225    }
3226
3227    if ( (cur_line == NULL)
3228      || (line_number != sectionid)
3229      || (sectionid < 1)
3230      || (cur_line->type != FILE_LINE_ACTION))
3231    {
3232       /* Invalid "sectionid" parameter */
3233       edit_free_file(file);
3234       return JB_ERR_CGI_PARAMS;
3235    }
3236
3237    get_string_param(parameters, "p", &action_set_name);
3238    if (action_set_name != NULL)
3239    {
3240       for (index = 0; index < MAX_ACTION_FILES; index++)
3241       {
3242          if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3243          {
3244             for (b = b->next; NULL != b; b = b->next)
3245             {
3246                if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3247                {
3248                   copy_action(cur_line->data.action, b->action); 
3249                   goto found;
3250                }
3251             }
3252          }
3253       }
3254       edit_free_file(file);
3255       return JB_ERR_CGI_PARAMS;
3256
3257       found: ;
3258    }
3259    else
3260    {
3261       err = actions_from_radio(parameters, cur_line->data.action);
3262    }
3263
3264    if(err)
3265    {
3266       /* Out of memory */
3267       edit_free_file(file);
3268       return err;
3269    }
3270
3271    ch = get_char_param(parameters, "filter_all");
3272    if (ch == 'N')
3273    {
3274       list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3275       list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3276       cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3277    }
3278    else if (ch == 'X')
3279    {
3280       cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3281    }
3282
3283    for (index = 0; !err; index++)
3284    {
3285       char key_value[30];
3286       char key_name[30];
3287       const char *name;
3288       char value;
3289
3290       /* Generate the keys */
3291       snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3292       key_value[sizeof(key_value) - 1] = '\0';
3293       snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3294       key_name[sizeof(key_name) - 1] = '\0';
3295
3296       err = get_string_param(parameters, key_name, &name);
3297       if (err) break;
3298
3299       if (name == NULL)
3300       {
3301          /* End of list */
3302          break;
3303       }
3304
3305       value = get_char_param(parameters, key_value);
3306       if (value == 'Y')
3307       {
3308          list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3309          if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3310          list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3311       }
3312       else if (value == 'N')
3313       {
3314          list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3315          if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3316          {
3317             list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3318             if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3319          }
3320       }
3321       else if (value == 'X')
3322       {
3323          list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3324          list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3325       }
3326    }
3327
3328    if(err)
3329    {
3330       /* Out of memory */
3331       edit_free_file(file);
3332       return err;
3333    }
3334
3335    if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3336    {
3337       /* Out of memory */
3338       edit_free_file(file);
3339       return JB_ERR_MEMORY;
3340    }
3341
3342    len = strlen(actiontext);
3343    if (len == 0)
3344    {
3345       /*
3346        * Empty action - must special-case this.
3347        * Simply setting len to 1 is sufficient...
3348        */
3349       len = 1;
3350    }
3351
3352    if (NULL == (newtext = malloc(len + 2)))
3353    {
3354       /* Out of memory */
3355       free(actiontext);
3356       edit_free_file(file);
3357       return JB_ERR_MEMORY;
3358    }
3359    strcpy(newtext, actiontext);
3360    free(actiontext);
3361    newtext[0]       = '{';
3362    newtext[len]     = '}';
3363    newtext[len + 1] = '\0';
3364
3365    freez(cur_line->raw);
3366    freez(cur_line->unprocessed);
3367    cur_line->unprocessed = newtext;
3368
3369    err = edit_write_file(file);
3370    if (err)
3371    {
3372       /* Error writing file */
3373       if (err == JB_ERR_FILE)
3374       {
3375          /* Read-only file. */
3376          err = cgi_error_file_read_only(csp, rsp, file->identifier);
3377       }
3378       edit_free_file(file);
3379       return err;
3380    }
3381
3382    target = strdup(CGI_PREFIX "edit-actions-list?f=");
3383    string_append(&target, file->identifier);
3384    string_join(&target, section_target(sectionid));
3385
3386
3387    edit_free_file(file);
3388
3389    if (target == NULL)
3390    {
3391       /* Out of memory */
3392       return JB_ERR_MEMORY;
3393    }
3394
3395    rsp->status = strdup("302 Local Redirect from Privoxy");
3396    if (rsp->status == NULL)
3397    {
3398       free(target);
3399       return JB_ERR_MEMORY;
3400    }
3401    err = enlist_unique_header(rsp->headers, "Location", target);
3402    free(target);
3403
3404    return err;
3405 }
3406
3407
3408 /*********************************************************************
3409  *
3410  * Function    :  cgi_edit_actions_url
3411  *
3412  * Description :  CGI function that actually edits a URL pattern in
3413  *                an actions file.
3414  *
3415  * Parameters  :
3416  *          1  :  csp = Current client state (buffers, headers, etc...)
3417  *          2  :  rsp = http_response data structure for output
3418  *          3  :  parameters = map of cgi parameters
3419  *
3420  * CGI Parameters :
3421  *    filename : Identifies the file to edit
3422  *         ver : File's last-modified time
3423  *     section : Line number of section to edit
3424  *     pattern : Line number of pattern to edit
3425  *      newval : New value for pattern
3426  *
3427  * Returns     :  JB_ERR_OK     on success
3428  *                JB_ERR_MEMORY on out-of-memory
3429  *                JB_ERR_CGI_PARAMS if the CGI parameters are not
3430  *                                  specified or not valid.
3431  *
3432  *********************************************************************/
3433 jb_err cgi_edit_actions_url(struct client_state *csp,
3434                             struct http_response *rsp,
3435                             const struct map *parameters)
3436 {
3437    unsigned patternid;
3438    char * new_pattern;
3439    struct editable_file * file;
3440    struct file_line * cur_line;
3441    unsigned line_number;
3442    unsigned section_start_line_number = 0;
3443    char * target;
3444    jb_err err;
3445
3446    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3447    {
3448       return cgi_error_disabled(csp, rsp);
3449    }
3450
3451    err = get_number_param(csp, parameters, "p", &patternid);
3452    if (err)
3453    {
3454       return err;
3455    }
3456    if (patternid < 1U)
3457    {
3458       return JB_ERR_CGI_PARAMS;
3459    }
3460
3461    err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3462    if (err)
3463    {
3464       return err;
3465    }
3466
3467    err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3468    if (err)
3469    {
3470       /* No filename specified, can't read file, modified, or out of memory. */
3471       free(new_pattern);
3472       return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3473    }
3474
3475    line_number = 1;
3476    cur_line = file->lines;
3477
3478    while ((cur_line != NULL) && (line_number < patternid))
3479    {
3480       if (cur_line->type == FILE_LINE_ACTION)
3481       {
3482          section_start_line_number = line_number;
3483       }      
3484       cur_line = cur_line->next;
3485       line_number++;
3486    }
3487
3488    if ( (cur_line == NULL)
3489      || (cur_line->type != FILE_LINE_URL))
3490    {
3491       /* Invalid "patternid" parameter */
3492       free(new_pattern);
3493       edit_free_file(file);
3494       return JB_ERR_CGI_PARAMS;
3495    }
3496
3497    /* At this point, the line to edit is in cur_line */
3498
3499    freez(cur_line->raw);
3500    freez(cur_line->unprocessed);
3501    cur_line->unprocessed = new_pattern;
3502
3503    err = edit_write_file(file);
3504    if (err)
3505    {
3506       /* Error writing file */
3507       if (err == JB_ERR_FILE)
3508       {
3509          /* Read-only file. */
3510          err = cgi_error_file_read_only(csp, rsp, file->identifier);
3511       }
3512       edit_free_file(file);
3513       return err;
3514    }
3515
3516    target = strdup(CGI_PREFIX "edit-actions-list?f=");
3517    string_append(&target, file->identifier);
3518    string_join(&target, section_target(section_start_line_number));
3519
3520    edit_free_file(file);
3521
3522    if (target == NULL)
3523    {
3524       /* Out of memory */
3525       return JB_ERR_MEMORY;
3526    }
3527
3528    rsp->status = strdup("302 Local Redirect from Privoxy");
3529    if (rsp->status == NULL)
3530    {
3531       free(target);
3532       return JB_ERR_MEMORY;
3533    }
3534    err = enlist_unique_header(rsp->headers, "Location", target);
3535    free(target);
3536
3537    return err;
3538 }
3539
3540
3541 /*********************************************************************
3542  *
3543  * Function    :  cgi_edit_actions_add_url
3544  *
3545  * Description :  CGI function that actually adds a URL pattern to
3546  *                an actions file.
3547  *
3548  * Parameters  :
3549  *          1  :  csp = Current client state (buffers, headers, etc...)
3550  *          2  :  rsp = http_response data structure for output
3551  *          3  :  parameters = map of cgi parameters
3552  *
3553  * CGI Parameters :
3554  *    filename : Identifies the file to edit
3555  *         ver : File's last-modified time
3556  *     section : Line number of section to edit
3557  *      newval : New pattern
3558  *
3559  * Returns     :  JB_ERR_OK     on success
3560  *                JB_ERR_MEMORY on out-of-memory
3561  *                JB_ERR_CGI_PARAMS if the CGI parameters are not
3562  *                                  specified or not valid.
3563  *
3564  *********************************************************************/
3565 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3566                                 struct http_response *rsp,
3567                                 const struct map *parameters)
3568 {
3569    unsigned sectionid;
3570    char * new_pattern;
3571    struct file_line * new_line;
3572    struct editable_file * file;
3573    struct file_line * cur_line;
3574    unsigned line_number;
3575    char * target;
3576    jb_err err;
3577
3578    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3579    {
3580       return cgi_error_disabled(csp, rsp);
3581    }
3582
3583    err = get_number_param(csp, parameters, "s", &sectionid);
3584    if (err)
3585    {
3586       return err;
3587    }
3588    if (sectionid < 1U)
3589    {
3590       return JB_ERR_CGI_PARAMS;
3591    }
3592
3593    err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3594    if (err)
3595    {
3596       return err;
3597    }
3598
3599    err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3600    if (err)
3601    {
3602       /* No filename specified, can't read file, modified, or out of memory. */
3603       free(new_pattern);
3604       return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3605    }
3606
3607    line_number = 1;
3608    cur_line = file->lines;
3609
3610    while ((cur_line != NULL) && (line_number < sectionid))
3611    {
3612       cur_line = cur_line->next;
3613       line_number++;
3614    }
3615
3616    if ( (cur_line == NULL)
3617      || (cur_line->type != FILE_LINE_ACTION))
3618    {
3619       /* Invalid "sectionid" parameter */
3620       free(new_pattern);
3621       edit_free_file(file);
3622       return JB_ERR_CGI_PARAMS;
3623    }
3624
3625    /* At this point, the section header is in cur_line - add after this. */
3626
3627    /* Allocate the new line */
3628    new_line = (struct file_line *)zalloc(sizeof(*new_line));
3629    if (new_line == NULL)
3630    {
3631       free(new_pattern);
3632       edit_free_file(file);
3633       return JB_ERR_MEMORY;
3634    }
3635
3636    /* Fill in the data members of the new line */
3637    new_line->raw = NULL;
3638    new_line->prefix = NULL;
3639    new_line->unprocessed = new_pattern;
3640    new_line->type = FILE_LINE_URL;
3641
3642    /* Link new_line into the list, after cur_line */
3643    new_line->next = cur_line->next;
3644    cur_line->next = new_line;
3645
3646    /* Done making changes, now commit */
3647
3648    err = edit_write_file(file);
3649    if (err)
3650    {
3651       /* Error writing file */
3652       if (err == JB_ERR_FILE)
3653       {
3654          /* Read-only file. */
3655          err = cgi_error_file_read_only(csp, rsp, file->identifier);
3656       }
3657       edit_free_file(file);
3658       return err;
3659    }
3660
3661    target = strdup(CGI_PREFIX "edit-actions-list?f=");
3662    string_append(&target, file->identifier);
3663    string_join(&target, section_target(sectionid));
3664
3665    edit_free_file(file);
3666
3667    if (target == NULL)
3668    {
3669       /* Out of memory */
3670       return JB_ERR_MEMORY;
3671    }
3672
3673    rsp->status = strdup("302 Local Redirect from Privoxy");
3674    if (rsp->status == NULL)
3675    {
3676       free(target);
3677       return JB_ERR_MEMORY;
3678    }
3679    err = enlist_unique_header(rsp->headers, "Location", target);
3680    free(target);
3681
3682    return err;
3683 }
3684
3685
3686 /*********************************************************************
3687  *
3688  * Function    :  cgi_edit_actions_remove_url
3689  *
3690  * Description :  CGI function that actually removes a URL pattern from
3691  *                the actions file.
3692  *
3693  * Parameters  :
3694  *          1  :  csp = Current client state (buffers, headers, etc...)
3695  *          2  :  rsp = http_response data structure for output
3696  *          3  :  parameters = map of cgi parameters
3697  *
3698  * CGI Parameters :
3699  *           f : (filename) Identifies the file to edit
3700  *           v : (version) File's last-modified time
3701  *           p : (pattern) Line number of pattern to remove
3702  *
3703  * Returns     :  JB_ERR_OK     on success
3704  *                JB_ERR_MEMORY on out-of-memory
3705  *                JB_ERR_CGI_PARAMS if the CGI parameters are not
3706  *                                  specified or not valid.
3707  *
3708  *********************************************************************/
3709 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3710                                    struct http_response *rsp,
3711                                    const struct map *parameters)
3712 {
3713    unsigned patternid;
3714    struct editable_file * file;
3715    struct file_line * cur_line;
3716    struct file_line * prev_line;
3717    unsigned line_number;
3718    unsigned section_start_line_number = 0;
3719    char * target;
3720    jb_err err;
3721
3722    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3723    {
3724       return cgi_error_disabled(csp, rsp);
3725    }
3726
3727    err = get_number_param(csp, parameters, "p", &patternid);
3728    if (err)
3729    {
3730       return err;
3731    }
3732
3733    err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3734    if (err)
3735    {
3736       /* No filename specified, can't read file, modified, or out of memory. */
3737       return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3738    }
3739
3740    line_number = 1;
3741    prev_line = NULL;
3742    cur_line = file->lines;
3743
3744    while ((cur_line != NULL) && (line_number < patternid))
3745    {
3746       if (cur_line->type == FILE_LINE_ACTION)
3747       {
3748          section_start_line_number = line_number;
3749       }
3750       prev_line = cur_line;
3751       cur_line = cur_line->next;
3752       line_number++;
3753    }
3754
3755    if ( (cur_line == NULL)
3756      || (prev_line == NULL)
3757      || (cur_line->type != FILE_LINE_URL))
3758    {
3759       /* Invalid "patternid" parameter */
3760       edit_free_file(file);
3761       return JB_ERR_CGI_PARAMS;
3762    }
3763
3764    /* At this point, the line to remove is in cur_line, and the previous
3765     * one is in prev_line
3766     */
3767
3768    /* Unlink cur_line */
3769    prev_line->next = cur_line->next;
3770    cur_line->next = NULL;
3771
3772    /* Free cur_line */
3773    edit_free_file_lines(cur_line);
3774
3775    err = edit_write_file(file);
3776    if (err)
3777    {
3778       /* Error writing file */
3779       if (err == JB_ERR_FILE)
3780       {
3781          /* Read-only file. */
3782          err = cgi_error_file_read_only(csp, rsp, file->identifier);
3783       }
3784       edit_free_file(file);
3785       return err;
3786    }
3787
3788    target = strdup(CGI_PREFIX "edit-actions-list?f=");
3789    string_append(&target, file->identifier);
3790    string_join(&target, section_target(section_start_line_number));
3791
3792    edit_free_file(file);
3793
3794    if (target == NULL)
3795    {
3796       /* Out of memory */
3797       return JB_ERR_MEMORY;
3798    }
3799
3800    rsp->status = strdup("302 Local Redirect from Privoxy");
3801    if (rsp->status == NULL)
3802    {
3803       free(target);
3804       return JB_ERR_MEMORY;
3805    }
3806    err = enlist_unique_header(rsp->headers, "Location", target);
3807    free(target);
3808
3809    return err;
3810 }
3811
3812
3813 /*********************************************************************
3814  *
3815  * Function    :  cgi_edit_actions_section_remove
3816  *
3817  * Description :  CGI function that actually removes a whole section from
3818  *                the actions file.  The section must be empty first
3819  *                (else JB_ERR_CGI_PARAMS).
3820  *
3821  * Parameters  :
3822  *          1  :  csp = Current client state (buffers, headers, etc...)
3823  *          2  :  rsp = http_response data structure for output
3824  *          3  :  parameters = map of cgi parameters
3825  *
3826  * CGI Parameters :
3827  *           f : (filename) Identifies the file to edit
3828  *           v : (version) File's last-modified time
3829  *           s : (section) Line number of section to edit
3830  *
3831  * Returns     :  JB_ERR_OK     on success
3832  *                JB_ERR_MEMORY on out-of-memory
3833  *                JB_ERR_CGI_PARAMS if the CGI parameters are not
3834  *                                  specified or not valid.
3835  *
3836  *********************************************************************/
3837 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3838                                        struct http_response *rsp,
3839                                        const struct map *parameters)
3840 {
3841    unsigned sectionid;
3842    struct editable_file * file;
3843    struct file_line * cur_line;
3844    struct file_line * prev_line;
3845    unsigned line_number;
3846    char * target;
3847    jb_err err;
3848
3849    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3850    {
3851       return cgi_error_disabled(csp, rsp);
3852    }
3853
3854    err = get_number_param(csp, parameters, "s", &sectionid);
3855    if (err)
3856    {
3857       return err;
3858    }
3859
3860    err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3861    if (err)
3862    {
3863       /* No filename specified, can't read file, modified, or out of memory. */
3864       return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3865    }
3866
3867    line_number = 1;
3868    cur_line = file->lines;
3869
3870    prev_line = NULL;
3871    while ((cur_line != NULL) && (line_number < sectionid))
3872    {
3873       prev_line = cur_line;
3874       cur_line = cur_line->next;
3875       line_number++;
3876    }
3877
3878    if ( (cur_line == NULL)
3879      || (cur_line->type != FILE_LINE_ACTION) )
3880    {
3881       /* Invalid "sectionid" parameter */
3882       edit_free_file(file);
3883       return JB_ERR_CGI_PARAMS;
3884    }
3885
3886    if ( (cur_line->next != NULL)
3887      && (cur_line->next->type == FILE_LINE_URL) )
3888    {
3889       /* Section not empty. */
3890       edit_free_file(file);
3891       return JB_ERR_CGI_PARAMS;
3892    }
3893
3894    /* At this point, the line to remove is in cur_line, and the previous
3895     * one is in prev_line
3896     */
3897
3898    /* Unlink cur_line */
3899    if (prev_line == NULL)
3900    {
3901       /* Removing the first line from the file */
3902       file->lines = cur_line->next;
3903    }
3904    else
3905    {
3906       prev_line->next = cur_line->next;
3907    }
3908    cur_line->next = NULL;
3909
3910    /* Free cur_line */
3911    edit_free_file_lines(cur_line);
3912
3913    err = edit_write_file(file);
3914    if (err)
3915    {
3916       /* Error writing file */
3917       if (err == JB_ERR_FILE)
3918       {
3919          /* Read-only file. */
3920          err = cgi_error_file_read_only(csp, rsp, file->identifier);
3921       }
3922       edit_free_file(file);
3923       return err;
3924    }
3925
3926    target = strdup(CGI_PREFIX "edit-actions-list?f=");
3927    string_append(&target, file->identifier);
3928
3929    edit_free_file(file);
3930
3931    if (target == NULL)
3932    {
3933       /* Out of memory */
3934       return JB_ERR_MEMORY;
3935    }
3936
3937    rsp->status = strdup("302 Local Redirect from Privoxy");
3938    if (rsp->status == NULL)
3939    {
3940       free(target);
3941       return JB_ERR_MEMORY;
3942    }
3943    err = enlist_unique_header(rsp->headers, "Location", target);
3944    free(target);
3945
3946    return err;
3947 }
3948
3949
3950 /*********************************************************************
3951  *
3952  * Function    :  cgi_edit_actions_section_add
3953  *
3954  * Description :  CGI function that adds a new empty section to
3955  *                an actions file.
3956  *
3957  * Parameters  :
3958  *          1  :  csp = Current client state (buffers, headers, etc...)
3959  *          2  :  rsp = http_response data structure for output
3960  *          3  :  parameters = map of cgi parameters
3961  *
3962  * CGI Parameters :
3963  *           f : (filename) Identifies the file to edit
3964  *           v : (version) File's last-modified time
3965  *           s : (section) Line number of section to add after, 0 for
3966  *               start of file.
3967  *
3968  * Returns     :  JB_ERR_OK     on success
3969  *                JB_ERR_MEMORY on out-of-memory
3970  *                JB_ERR_CGI_PARAMS if the CGI parameters are not
3971  *                                  specified or not valid.
3972  *
3973  *********************************************************************/
3974 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3975                                     struct http_response *rsp,
3976                                     const struct map *parameters)
3977 {
3978    unsigned sectionid;
3979    struct file_line * new_line;
3980    char * new_text;
3981    struct editable_file * file;
3982    struct file_line * cur_line;
3983    unsigned line_number;
3984    char * target;
3985    jb_err err;
3986
3987    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3988    {
3989       return cgi_error_disabled(csp, rsp);
3990    }
3991
3992    err = get_number_param(csp, parameters, "s", &sectionid);
3993    if (err)
3994    {
3995       return err;
3996    }
3997
3998    err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3999    if (err)
4000    {
4001       /* No filename specified, can't read file, modified, or out of memory. */
4002       return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4003    }
4004
4005    line_number = 1;
4006    cur_line = file->lines;
4007
4008    if (sectionid <= 1U)
4009    {
4010       /* Add to start of file */
4011       if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4012       {
4013          /* There's something in the file, find the line before the first
4014           * action.
4015           */
4016          while ( (cur_line->next != NULL)
4017               && (cur_line->next->type != FILE_LINE_ACTION) )
4018          {
4019             cur_line = cur_line->next;
4020             line_number++;
4021          }
4022       }
4023       else
4024       {
4025          /* File starts with action line, so insert at top */
4026          cur_line = NULL;
4027       }
4028    }
4029    else
4030    {
4031       /* Add after stated section. */
4032       while ((cur_line != NULL) && (line_number < sectionid))
4033       {
4034          cur_line = cur_line->next;
4035          line_number++;
4036       }
4037
4038       if ( (cur_line == NULL)
4039         || (cur_line->type != FILE_LINE_ACTION))
4040       {
4041          /* Invalid "sectionid" parameter */
4042          edit_free_file(file);
4043          return JB_ERR_CGI_PARAMS;
4044       }
4045
4046       /* Skip through the section to find the last line in it. */
4047       while ( (cur_line->next != NULL)
4048            && (cur_line->next->type != FILE_LINE_ACTION) )
4049       {
4050          cur_line = cur_line->next;
4051          line_number++;
4052       }
4053    }
4054
4055    /* At this point, the last line in the previous section is in cur_line
4056     * - add after this.  (Or if we need to add as the first line, cur_line
4057     * will be NULL).
4058     */
4059
4060    new_text = strdup("{}");
4061    if (NULL == new_text)
4062    {
4063       edit_free_file(file);
4064       return JB_ERR_MEMORY;
4065    }
4066
4067    /* Allocate the new line */
4068    new_line = (struct file_line *)zalloc(sizeof(*new_line));
4069    if (new_line == NULL)
4070    {
4071       free(new_text);
4072       edit_free_file(file);
4073       return JB_ERR_MEMORY;
4074    }
4075
4076    /* Fill in the data members of the new line */
4077    new_line->raw = NULL;
4078    new_line->prefix = NULL;
4079    new_line->unprocessed = new_text;
4080    new_line->type = FILE_LINE_ACTION;
4081
4082    if (cur_line != NULL)
4083    {
4084       /* Link new_line into the list, after cur_line */
4085       new_line->next = cur_line->next;
4086       cur_line->next = new_line;
4087    }
4088    else
4089    {
4090       /* Link new_line into the list, as first line */
4091       new_line->next = file->lines;
4092       file->lines = new_line;
4093    }
4094
4095    /* Done making changes, now commit */
4096
4097    err = edit_write_file(file);
4098    if (err)
4099    {
4100       /* Error writing file */
4101       if (err == JB_ERR_FILE)
4102       {
4103          /* Read-only file. */
4104          err = cgi_error_file_read_only(csp, rsp, file->identifier);
4105       }
4106       edit_free_file(file);
4107       return err;
4108    }
4109
4110    target = strdup(CGI_PREFIX "edit-actions-list?f=");
4111    string_append(&target, file->identifier);
4112
4113    edit_free_file(file);
4114
4115    if (target == NULL)
4116    {
4117       /* Out of memory */
4118       return JB_ERR_MEMORY;
4119    }
4120
4121    rsp->status = strdup("302 Local Redirect from Privoxy");
4122    if (rsp->status == NULL)
4123    {
4124       free(target);
4125       return JB_ERR_MEMORY;
4126    }
4127    err = enlist_unique_header(rsp->headers, "Location", target);
4128    free(target);
4129
4130    return err;
4131 }
4132
4133
4134 /*********************************************************************
4135  *
4136  * Function    :  cgi_edit_actions_section_swap
4137  *
4138  * Description :  CGI function that swaps the order of two sections
4139  *                in the actions file.  Note that this CGI can actually
4140  *                swap any two arbitrary sections, but the GUI interface
4141  *                currently only allows consecutive sections to be
4142  *                specified.
4143  *
4144  * Parameters  :
4145  *          1  :  csp = Current client state (buffers, headers, etc...)
4146  *          2  :  rsp = http_response data structure for output
4147  *          3  :  parameters = map of cgi parameters
4148  *
4149  * CGI Parameters :
4150  *           f : (filename) Identifies the file to edit
4151  *           v : (version) File's last-modified time
4152  *          s1 : (section1) Line number of first section to swap
4153  *          s2 : (section2) Line number of second section to swap
4154  *
4155  * Returns     :  JB_ERR_OK     on success
4156  *                JB_ERR_MEMORY on out-of-memory
4157  *                JB_ERR_CGI_PARAMS if the CGI parameters are not
4158  *                                  specified or not valid.
4159  *
4160  *********************************************************************/
4161 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4162                                      struct http_response *rsp,
4163                                      const struct map *parameters)
4164 {
4165    unsigned section1;
4166    unsigned section2;
4167    struct editable_file * file;
4168    struct file_line * cur_line;
4169    struct file_line * prev_line;
4170    struct file_line * line_before_section1;
4171    struct file_line * line_start_section1;
4172    struct file_line * line_end_section1;
4173    struct file_line * line_after_section1;
4174    struct file_line * line_before_section2;
4175    struct file_line * line_start_section2;
4176    struct file_line * line_end_section2;
4177    struct file_line * line_after_section2;
4178    unsigned line_number;
4179    char * target;
4180    jb_err err;
4181
4182    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4183    {
4184       return cgi_error_disabled(csp, rsp);
4185    }
4186
4187    err = get_number_param(csp, parameters, "s1", &section1);
4188    if (!err) err = get_number_param(csp, parameters, "s2", &section2);
4189    if (err)
4190    {
4191       return err;
4192    }
4193
4194    if (section1 > section2)
4195    {
4196       unsigned temp = section2;
4197       section2 = section1;
4198       section1 = temp;
4199    }
4200
4201    err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4202    if (err)
4203    {
4204       /* No filename specified, can't read file, modified, or out of memory. */
4205       return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4206    }
4207
4208    /* Start at the beginning... */
4209    line_number = 1;
4210    cur_line = file->lines;
4211    prev_line = NULL;
4212
4213    /* ... find section1 ... */
4214    while ((cur_line != NULL) && (line_number < section1))
4215    {
4216       prev_line = cur_line;
4217       cur_line = cur_line->next;
4218       line_number++;
4219    }
4220
4221    if ( (cur_line == NULL)
4222      || (cur_line->type != FILE_LINE_ACTION) )
4223    {
4224       /* Invalid "section1" parameter */
4225       edit_free_file(file);
4226       return JB_ERR_CGI_PARAMS;
4227    }
4228
4229    /* If no-op, we've validated params and can skip the rest. */
4230    if (section1 != section2)
4231    {
4232       /* ... find the end of section1 ... */
4233       line_before_section1 = prev_line;
4234       line_start_section1 = cur_line;
4235       do
4236       {
4237          prev_line = cur_line;
4238          cur_line = cur_line->next;
4239          line_number++;
4240       }
4241       while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4242       line_end_section1 = prev_line;
4243       line_after_section1 = cur_line;
4244
4245       /* ... find section2 ... */
4246       while ((cur_line != NULL) && (line_number < section2))
4247       {
4248          prev_line = cur_line;
4249          cur_line = cur_line->next;
4250          line_number++;
4251       }
4252
4253       if ( (cur_line == NULL)
4254         || (cur_line->type != FILE_LINE_ACTION) )
4255       {
4256          /* Invalid "section2" parameter */
4257          edit_free_file(file);
4258          return JB_ERR_CGI_PARAMS;
4259       }
4260
4261       /* ... find the end of section2 ... */
4262       line_before_section2 = prev_line;
4263       line_start_section2 = cur_line;
4264       do
4265       {
4266          prev_line = cur_line;
4267          cur_line = cur_line->next;
4268          line_number++;
4269       }
4270       while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4271       line_end_section2 = prev_line;
4272       line_after_section2 = cur_line;
4273
4274       /* Now have all the pointers we need. Do the swap. */
4275
4276       /* Change the pointer to section1 to point to section2 instead */
4277       if (line_before_section1 == NULL)
4278       {
4279          file->lines = line_start_section2;
4280       }
4281       else
4282       {
4283          line_before_section1->next = line_start_section2;
4284       }
4285
4286       if (line_before_section2 == line_end_section1)
4287       {
4288          /* Consecutive sections */
4289          line_end_section2->next = line_start_section1;
4290       }
4291       else
4292       {
4293          line_end_section2->next = line_after_section1;
4294          line_before_section2->next = line_start_section1;
4295       }
4296
4297       /* Set the pointer from the end of section1 to the rest of the file */
4298       line_end_section1->next = line_after_section2;
4299
4300       err = edit_write_file(file);
4301       if (err)
4302       {
4303          /* Error writing file */
4304          if (err == JB_ERR_FILE)
4305          {
4306             /* Read-only file. */
4307             err = cgi_error_file_read_only(csp, rsp, file->identifier);
4308          }
4309          edit_free_file(file);
4310          return err;
4311       }
4312    } /* END if (section1 != section2) */
4313
4314    target = strdup(CGI_PREFIX "edit-actions-list?f=");
4315    string_append(&target, file->identifier);
4316
4317    edit_free_file(file);
4318
4319    if (target == NULL)
4320    {
4321       /* Out of memory */
4322       return JB_ERR_MEMORY;
4323    }
4324
4325    rsp->status = strdup("302 Local Redirect from Privoxy");
4326    if (rsp->status == NULL)
4327    {
4328       free(target);
4329       return JB_ERR_MEMORY;
4330    }
4331    err = enlist_unique_header(rsp->headers, "Location", target);
4332    free(target);
4333
4334    return err;
4335 }
4336
4337
4338 /*********************************************************************
4339  *
4340  * Function    :  cgi_toggle
4341  *
4342  * Description :  CGI function that adds a new empty section to
4343  *                an actions file.
4344  *
4345  * Parameters  :
4346  *          1  :  csp = Current client state (buffers, headers, etc...)
4347  *          2  :  rsp = http_response data structure for output
4348  *          3  :  parameters = map of cgi parameters
4349  *
4350  * CGI Parameters :
4351  *         set : If present, how to change toggle setting:
4352  *               "enable", "disable", "toggle", or none (default).
4353  *        mini : If present, use mini reply template.
4354  *
4355  * Returns     :  JB_ERR_OK     on success
4356  *                JB_ERR_MEMORY on out-of-memory
4357  *
4358  *********************************************************************/
4359 jb_err cgi_toggle(struct client_state *csp,
4360                   struct http_response *rsp,
4361                   const struct map *parameters)
4362 {
4363    struct map *exports;
4364    char mode;
4365    const char *template_name;
4366
4367    assert(csp);
4368    assert(rsp);
4369    assert(parameters);
4370
4371    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4372    {
4373       return cgi_error_disabled(csp, rsp);
4374    }
4375
4376    mode = get_char_param(parameters, "set");
4377
4378    if (mode == 'E')
4379    {
4380       /* Enable */
4381       g_bToggleIJB = 1;
4382    }
4383    else if (mode == 'D')
4384    {
4385       /* Disable */
4386       g_bToggleIJB = 0;
4387    }
4388    else if (mode == 'T')
4389    {
4390       /* Toggle */
4391       g_bToggleIJB = !g_bToggleIJB;
4392    }
4393
4394    if (NULL == (exports = default_exports(csp, "toggle")))
4395    {
4396       return JB_ERR_MEMORY;
4397    }
4398
4399    template_name = (get_char_param(parameters, "mini")
4400                  ? "toggle-mini"
4401                  : "toggle");
4402
4403    return template_fill_for_cgi(csp, template_name, exports, rsp);
4404 }
4405
4406
4407 /*********************************************************************
4408  *
4409  * Function    :  javascriptify
4410  *
4411  * Description :  Converts a string into a form JavaScript will like.
4412  *
4413  *                Netscape 4's JavaScript sucks - it doesn't use 
4414  *                "id" parameters, so you have to set the "name"
4415  *                used to submit a form element to something JavaScript
4416  *                will like.  (Or access the elements by index in an
4417  *                array.  That array contains >60 elements and will
4418  *                be changed whenever we add a new action to the
4419  *                editor, so I'm NOT going to use indexes that have
4420  *                to be figured out by hand.)
4421  *
4422  *                Currently the only thing we have to worry about
4423  *                is "-" ==> "_" conversion.
4424  *
4425  *                This is a length-preserving operation so it is
4426  *                carried out in-place, no memory is allocated
4427  *                or freed.
4428  *
4429  * Parameters  :
4430  *          1  :  identifier = String to make JavaScript-friendly.
4431  *
4432  * Returns     :  N/A
4433  *
4434  *********************************************************************/
4435 static void javascriptify(char * identifier)
4436 {
4437    char * p = identifier;
4438    while (NULL != (p = strchr(p, '-')))
4439    {
4440       *p++ = '_';
4441    }
4442 }
4443
4444
4445 /*********************************************************************
4446  *
4447  * Function    :  actions_to_radio
4448  *
4449  * Description :  Converts a actionsfile entry into settings for
4450  *                radio buttons and edit boxes on a HTML form.
4451  *
4452  * Parameters  :
4453  *          1  :  exports = List of substitutions to add to.
4454  *          2  :  action  = Action to read
4455  *
4456  * Returns     :  JB_ERR_OK     on success
4457  *                JB_ERR_MEMORY on out-of-memory
4458  *
4459  *********************************************************************/
4460 static jb_err actions_to_radio(struct map * exports,
4461                                const struct action_spec *action)
4462 {
4463    unsigned mask = action->mask;
4464    unsigned add  = action->add;
4465    int mapped_param;
4466    int checked;
4467    char current_mode;
4468
4469    assert(exports);
4470    assert(action);
4471
4472    mask = action->mask;
4473    add  = action->add;
4474
4475    /* sanity - prevents "-feature +feature" */
4476    mask |= add;
4477
4478
4479 #define DEFINE_ACTION_BOOL(name, bit)                 \
4480    if (!(mask & bit))                                 \
4481    {                                                  \
4482       current_mode = 'n';                             \
4483    }                                                  \
4484    else if (add & bit)                                \
4485    {                                                  \
4486       current_mode = 'y';                             \
4487    }                                                  \
4488    else                                               \
4489    {                                                  \
4490       current_mode = 'x';                             \
4491    }                                                  \
4492    if (map_radio(exports, name, "ynx", current_mode)) \
4493    {                                                  \
4494       return JB_ERR_MEMORY;                           \
4495    }
4496
4497 #define DEFINE_ACTION_STRING(name, bit, index)        \
4498    DEFINE_ACTION_BOOL(name, bit);                     \
4499    mapped_param = 0;
4500
4501 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default)  \
4502    if (add & bit)                                                    \
4503    {                                                                 \
4504       checked = !strcmp(action->string[index], value);               \
4505    }                                                                 \
4506    else                                                              \
4507    {                                                                 \
4508       checked = is_default;                                          \
4509    }                                                                 \
4510    mapped_param |= checked;                                          \
4511    if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4512    {                                                                 \
4513       return JB_ERR_MEMORY;                                          \
4514    }
4515
4516 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val)       \
4517    if (map(exports, name "-param-custom", 1,                         \
4518            ((!mapped_param) ? "checked" : ""), 1))                   \
4519    {                                                                 \
4520       return JB_ERR_MEMORY;                                          \
4521    }                                                                 \
4522    if (map(exports, name "-param", 1,                                \
4523            (((add & bit) && !mapped_param) ?                         \
4524            action->string[index] : default_val), 1))                 \
4525    {                                                                 \
4526       return JB_ERR_MEMORY;                                          \
4527    }
4528
4529 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val)     \
4530    if (map(exports, name "-param", 1,                                \
4531            ((add & bit) ? action->string[index] : default_val), 1))  \
4532    {                                                                 \
4533       return JB_ERR_MEMORY;                                          \
4534    }
4535
4536 #define DEFINE_ACTION_MULTI(name, index)              \
4537    if (action->multi_add[index]->first)               \
4538    {                                                  \
4539       current_mode = 'y';                             \
4540    }                                                  \
4541    else if (action->multi_remove_all[index])          \
4542    {                                                  \
4543       current_mode = 'n';                             \
4544    }                                                  \
4545    else if (action->multi_remove[index]->first)       \
4546    {                                                  \
4547       current_mode = 'y';                             \
4548    }                                                  \
4549    else                                               \
4550    {                                                  \
4551       current_mode = 'x';                             \
4552    }                                                  \
4553    if (map_radio(exports, name, "ynx", current_mode)) \
4554    {                                                  \
4555       return JB_ERR_MEMORY;                           \
4556    }
4557
4558 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4559
4560 #include "actionlist.h"
4561
4562 #undef DEFINE_ACTION_MULTI
4563 #undef DEFINE_ACTION_STRING
4564 #undef DEFINE_ACTION_BOOL
4565 #undef DEFINE_ACTION_ALIAS
4566 #undef DEFINE_CGI_PARAM_CUSTOM
4567 #undef DEFINE_CGI_PARAM_RADIO
4568 #undef DEFINE_CGI_PARAM_NO_RADIO
4569
4570    return JB_ERR_OK;
4571 }
4572
4573
4574 /*********************************************************************
4575  *
4576  * Function    :  actions_from_radio
4577  *
4578  * Description :  Converts a map of parameters passed to a CGI function
4579  *                into an actionsfile entry.
4580  *
4581  * Parameters  :
4582  *          1  :  parameters = parameters to the CGI call
4583  *          2  :  action  = Action to change.  Must be valid before
4584  *                          the call, actions not specified will be
4585  *                          left unchanged.
4586  *
4587  * Returns     :  JB_ERR_OK     on success
4588  *                JB_ERR_MEMORY on out-of-memory
4589  *
4590  *********************************************************************/
4591 static jb_err actions_from_radio(const struct map * parameters,
4592                                  struct action_spec *action)
4593 {
4594    const char * param;
4595    char * param_dup;
4596    char ch;
4597    const char * js_name;
4598    jb_err err = JB_ERR_OK;
4599
4600    assert(parameters);
4601    assert(action);
4602
4603    /* Statics are generally a potential race condition,
4604     * but in this case we're safe and don't need semaphores.
4605     * Be careful if you modify this function.
4606     * - Jon
4607     * The js_name_arr's are never free()d, but this is no
4608     * problem, since they will only be created once and
4609     * used by all threads thereafter. -oes
4610     */
4611
4612 #define JAVASCRIPTIFY(dest_var, string)               \
4613    {                                                  \
4614      static int first_time = 1;                       \
4615      static char *js_name_arr;                        \
4616       if (first_time)                                 \
4617       {                                               \
4618          js_name_arr = strdup(string);                \
4619          javascriptify(js_name_arr);                  \
4620       }                                               \
4621       dest_var = js_name_arr;                         \
4622       first_time = 0;                                 \
4623    }                                                  \
4624
4625 #define DEFINE_ACTION_BOOL(name, bit)                 \
4626    JAVASCRIPTIFY(js_name, name);                      \
4627    ch = get_char_param(parameters, js_name);          \
4628    if (ch == 'Y')                                     \
4629    {                                                  \
4630       action->add  |= bit;                            \
4631       action->mask |= bit;                            \
4632    }                                                  \
4633    else if (ch == 'N')                                \
4634    {                                                  \
4635       action->add  &= ~bit;                           \
4636       action->mask &= ~bit;                           \
4637    }                                                  \
4638    else if (ch == 'X')                                \
4639    {                                                  \
4640       action->add  &= ~bit;                           \
4641       action->mask |= bit;                            \
4642    }                                                  \
4643
4644 #define DEFINE_ACTION_STRING(name, bit, index)                 \
4645    JAVASCRIPTIFY(js_name, name);                               \
4646    ch = get_char_param(parameters, js_name);                   \
4647    if (ch == 'Y')                                              \
4648    {                                                           \
4649       param = NULL;                                            \
4650       JAVASCRIPTIFY(js_name, name "-mode");                    \
4651       if (!err) err = get_string_param(parameters, js_name, &param);    \
4652       if ((param == NULL) || (0 == strcmp(param, "CUSTOM")))            \
4653       {                                                                 \
4654          JAVASCRIPTIFY(js_name, name "-param");                         \
4655          if (!err) err = get_string_param(parameters, js_name, &param); \
4656       }                                                        \
4657       if (param != NULL)                                       \
4658       {                                                        \
4659          if (NULL == (param_dup = strdup(param)))              \
4660          {                                                     \
4661             return JB_ERR_MEMORY;                              \
4662          }                                                     \
4663          freez(action->string[index]);                         \
4664          action->add  |= bit;                                  \
4665          action->mask |= bit;                                  \
4666          action->string[index] = param_dup;                    \
4667       }                                                        \
4668    }                                                           \
4669    else if (ch == 'N')                                         \
4670    {                                                           \
4671       if (action->add & bit)                                   \
4672       {                                                        \
4673          freez(action->string[index]);                         \
4674       }                                                        \
4675       action->add  &= ~bit;                                    \
4676       action->mask &= ~bit;                                    \
4677    }                                                           \
4678    else if (ch == 'X')                                         \
4679    {                                                           \
4680       if (action->add & bit)                                   \
4681       {                                                        \
4682          freez(action->string[index]);                         \
4683       }                                                        \
4684       action->add  &= ~bit;                                    \
4685       action->mask |= bit;                                     \
4686    }                                                           \
4687
4688 #define DEFINE_ACTION_MULTI(name, index)                       \
4689    JAVASCRIPTIFY(js_name, name);                               \
4690    ch = get_char_param(parameters, js_name);                   \
4691    if (ch == 'Y')                                              \
4692    {                                                           \
4693       /* FIXME */                                              \
4694    }                                                           \
4695    else if (ch == 'N')                                         \
4696    {                                                           \
4697       list_remove_all(action->multi_add[index]);               \
4698       list_remove_all(action->multi_remove[index]);            \
4699       action->multi_remove_all[index] = 1;                     \
4700    }                                                           \
4701    else if (ch == 'X')                                         \
4702    {                                                           \
4703       list_remove_all(action->multi_add[index]);               \
4704       list_remove_all(action->multi_remove[index]);            \
4705       action->multi_remove_all[index] = 0;                     \
4706    }                                                           \
4707
4708 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4709
4710 #include "actionlist.h"
4711
4712 #undef DEFINE_ACTION_MULTI
4713 #undef DEFINE_ACTION_STRING
4714 #undef DEFINE_ACTION_BOOL
4715 #undef DEFINE_ACTION_ALIAS
4716 #undef JAVASCRIPTIFY
4717
4718    return err;
4719 }
4720
4721
4722 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4723
4724
4725 /*
4726   Local Variables:
4727   tab-width: 3
4728   end:
4729 */