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