1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.36 2002/04/26 21:53:30 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
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.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001 the SourceForge
19 * Privoxy team. http://www.privoxy.org/
21 * Based on the Internet Junkbuster originally written
22 * by and Copyright (C) 1997 Anonymous Coders and
23 * Junkbusters Corporation. http://www.junkbusters.com
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.
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.
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.
45 * Revision 1.36 2002/04/26 21:53:30 jongfoster
46 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
48 * Revision 1.35 2002/04/26 21:50:02 jongfoster
49 * Honouring default exports in edit-actions-for-url-filter template.
51 * Revision 1.34 2002/04/26 12:54:17 oes
52 * Adaptions to changes in actions.c
54 * Revision 1.33 2002/04/24 02:17:47 oes
55 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
57 * - Activated Jon's code for editing multiple AFs
58 * - cgi_edit_list_actions now provides context-sensitive
59 * help, looks up all action sets from standard.action and
60 * makes buttons for them in the catchall section
61 * - cgi_edit_action_submit now honors a p parameter, looks up
62 * the corresponding action set, and sets the catchall pattern's
63 * actions accordingly.
65 * Revision 1.32 2002/04/19 16:55:31 jongfoster
66 * Fixing newline problems. If we do our own text file newline
67 * mangling, we don't want the library to do any, so we need to
68 * open the files in *binary* mode.
70 * Revision 1.31 2002/04/18 19:21:08 jongfoster
71 * Added code to detect "conventional" action files, that start
72 * with a set of actions for all URLs (the pattern "/").
73 * These are special-cased in the "edit-actions-list" CGI, so
74 * that a special UI can be written for them.
76 * Revision 1.30 2002/04/10 13:38:35 oes
77 * load_template signature changed
79 * Revision 1.29 2002/04/08 16:59:08 oes
82 * Revision 1.28 2002/03/27 12:30:29 oes
83 * Deleted unsused variable
85 * Revision 1.27 2002/03/26 23:06:04 jongfoster
86 * Removing duplicate @ifs on the toggle page
88 * Revision 1.26 2002/03/26 22:59:17 jongfoster
89 * Fixing /toggle to display status consistently.
91 * Revision 1.25 2002/03/26 22:29:54 swa
92 * we have a new homepage!
94 * Revision 1.24 2002/03/24 15:23:33 jongfoster
97 * Revision 1.23 2002/03/24 13:32:41 swa
98 * name change related issues
100 * Revision 1.22 2002/03/24 13:25:43 swa
101 * name change related issues
103 * Revision 1.21 2002/03/22 18:02:48 jongfoster
104 * Fixing remote toggle
106 * Revision 1.20 2002/03/16 20:28:34 oes
107 * Added descriptions to the filters so users will know what they select in the cgi editor
109 * Revision 1.19 2002/03/16 18:38:14 jongfoster
110 * Stopping stupid or malicious users from breaking the actions
111 * file using the web-based editor.
113 * Revision 1.18 2002/03/16 14:57:44 jongfoster
114 * Full support for enabling/disabling modular filters.
116 * Revision 1.17 2002/03/16 14:26:42 jongfoster
117 * First version of modular filters support - READ ONLY!
118 * Fixing a double-free bug in the out-of-memory handling in map_radio().
120 * Revision 1.16 2002/03/07 03:46:17 oes
121 * Fixed compiler warnings
123 * Revision 1.15 2002/03/06 22:54:35 jongfoster
124 * Automated function-comment nitpicking.
126 * Revision 1.14 2002/03/05 00:24:51 jongfoster
127 * Patch to always edit the current actions file.
129 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
130 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
132 * Revision 1.12 2002/03/03 09:18:03 joergs
133 * Made jumbjuster work on AmigaOS again.
135 * Revision 1.11 2002/01/23 01:03:31 jongfoster
136 * Fixing gcc [CygWin] compiler warnings
138 * Revision 1.10 2002/01/23 00:22:59 jongfoster
139 * Adding new function cgi_edit_actions_section_swap(), to reorder
142 * Adding get_url_spec_param() to get a validated URL pattern.
144 * Moving edit_read_line() out of this file and into loaders.c.
146 * Adding missing html_encode() to many CGI functions.
148 * Moving the functions that #include actionlist.h to the end of the file,
149 * because the Visual C++ 97 debugger gets extremely confused if you try
150 * to debug any code that comes after them in the file.
152 * Major optimizations in cgi_edit_actions_list() to reduce the size of
153 * the generated HTML (down 40% from 550k to 304k), with major side-effects
154 * throughout the editor and templates. In particular, the length of the
155 * URLs throughout the editor has been drastically reduced, by cutting
156 * paramater names down to 1 character and CGI names down to 3-4
157 * characters, by removing all non-essential CGI paramaters even at the
158 * expense of having to re-read the actions file for the most trivial
159 * page, and by using relative rather than absolute URLs. This means
160 * that this (typical example):
162 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
163 * filename=ijb&ver=1011487572&section=12&pattern=13
164 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
168 * <a href="eau?f=ijb&v=1011487572&p=13">
170 * Revision 1.9 2002/01/17 20:56:22 jongfoster
171 * Replacing hard references to the URL of the config interface
172 * with #defines from project.h
174 * Revision 1.8 2001/11/30 23:35:51 jongfoster
175 * Renaming actionsfile to ijb.action
177 * Revision 1.7 2001/11/13 00:28:24 jongfoster
178 * - Renaming parameters from edit-actions-for-url so that they only
179 * contain legal JavaScript characters. If we wanted to write
180 * JavaScript that worked with Netscape 4, this is nessacery.
181 * (Note that at the moment the JavaScript doesn't actually work
182 * with Netscape 4, but now this is purely a template issue, not
183 * one affecting code).
184 * - Adding new CGIs for use by non-JavaScript browsers:
185 * edit-actions-url-form
186 * edit-actions-add-url-form
187 * edit-actions-remove-url-form
190 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
191 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
192 * by and __OS2__ ifdef.
194 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
195 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
196 * threads to call select() simultaneously. So, it's time to do a real, live,
197 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
198 * (native). Both versions will work, but using __OS2__ offers multi-threading.
200 * Revision 1.4 2001/10/23 21:48:19 jongfoster
201 * Cleaning up error handling in CGI functions - they now send back
202 * a HTML error page and should never cause a FATAL error. (Fixes one
203 * potential source of "denial of service" attacks).
205 * CGI actions file editor that works and is actually useful.
207 * Ability to toggle JunkBuster remotely using a CGI call.
209 * You can turn off both the above features in the main configuration
210 * file, e.g. if you are running a multi-user proxy.
212 * Revision 1.3 2001/10/14 22:12:49 jongfoster
213 * New version of CGI-based actionsfile editor.
214 * Major changes, including:
215 * - Completely new file parser and file output routines
216 * - edit-actions CGI renamed edit-actions-for-url
217 * - All CGIs now need a filename parameter, except for...
218 * - New CGI edit-actions which doesn't need a filename,
219 * to allow you to start the editor up.
220 * - edit-actions-submit now works, and now automatically
221 * redirects you back to the main edit-actions-list handler.
223 * Revision 1.2 2001/09/16 17:05:14 jongfoster
224 * Removing unused #include showarg.h
226 * Revision 1.1 2001/09/16 15:47:37 jongfoster
227 * First version of CGI-based edit interface. This is very much a
228 * work-in-progress, and you can't actually use it to edit anything
229 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
230 * to have any effect.
233 **********************************************************************/
239 * FIXME: Following includes copied from cgi.c - which are actually needed?
244 #include <sys/types.h>
248 #include <sys/stat.h>
251 #define snprintf _snprintf
252 #endif /* def _WIN32 */
257 #include "cgisimple.h"
261 #include "miscutil.h"
265 /* loadcfg.h is for g_bToggleIJB only */
266 #include "urlmatch.h"
268 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
271 #ifdef FEATURE_CGI_EDIT_ACTIONS
275 struct file_line * next;
283 struct action_spec action[1];
292 /* Add more data types here... e.g.
295 struct url_spec url[1];
299 struct action_spec action[1];
308 #define FILE_LINE_UNPROCESSED 1
309 #define FILE_LINE_BLANK 2
310 #define FILE_LINE_ALIAS_HEADER 3
311 #define FILE_LINE_ALIAS_ENTRY 4
312 #define FILE_LINE_ACTION 5
313 #define FILE_LINE_URL 6
314 #define FILE_LINE_SETTINGS_HEADER 7
315 #define FILE_LINE_SETTINGS_ENTRY 8
316 #define FILE_LINE_DESCRIPTION_HEADER 9
317 #define FILE_LINE_DESCRIPTION_ENTRY 10
322 struct file_line * lines;
323 const char * filename; /* Full pathname - e.g. "/etc/privoxy/wibble.action" */
324 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
325 /* Pre-encoded with url_encode() for ease of use. */
326 const char * version_str; /* Last modification time, as a string. For CGI param */
327 /* Can be used in URL without using url_param(). */
328 unsigned version; /* Last modification time - prevents chaos with
329 * the browser's "back" button. Note that this is a
330 * time_t cast to an unsigned. When comparing, always
331 * cast the time_t to an unsigned, and *NOT* vice-versa.
332 * This may lose the top few bits, but they're not
333 * significant anyway.
335 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
336 * Note that changing this after the file has been
337 * read in will cause a mess.
339 struct file_line * parse_error; /* On parse error, this is the offending line. */
340 const char * parse_error_text; /* On parse error, this is the problem.
341 * (Statically allocated) */
344 /* FIXME: Following non-static functions should be prototyped in .h or made static */
346 /* Functions to read and write arbitrary config files */
347 jb_err edit_read_file(struct client_state *csp,
348 const struct map *parameters,
351 struct editable_file **pfile);
352 jb_err edit_write_file(struct editable_file * file);
353 void edit_free_file(struct editable_file * file);
355 /* Functions to read and write actions files */
356 jb_err edit_parse_actions_file(struct editable_file * file);
357 jb_err edit_read_actions_file(struct client_state *csp,
358 struct http_response *rsp,
359 const struct map *parameters,
361 struct editable_file **pfile);
364 jb_err cgi_error_modified(struct client_state *csp,
365 struct http_response *rsp,
366 const char *filename);
367 jb_err cgi_error_parse(struct client_state *csp,
368 struct http_response *rsp,
369 struct editable_file *file);
370 jb_err cgi_error_file(struct client_state *csp,
371 struct http_response *rsp,
372 const char *filename);
373 jb_err cgi_error_disabled(struct client_state *csp,
374 struct http_response *rsp);
376 /* Internal arbitrary config file support functions */
377 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
378 static void edit_free_file_lines(struct file_line * first_line);
380 /* Internal actions file support functions */
381 static int match_actions_file_header_line(const char * line, const char * name);
382 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
384 /* Internal parameter parsing functions */
385 static jb_err get_file_name_param(struct client_state *csp,
386 const struct map *parameters,
387 const char *param_name,
390 const char **pparam);
392 static jb_err get_url_spec_param(struct client_state *csp,
393 const struct map *parameters,
398 /* Internal actionsfile <==> HTML conversion functions */
399 static jb_err map_radio(struct map * exports,
400 const char * optionname,
403 static jb_err actions_to_radio(struct map * exports,
404 const struct action_spec *action);
405 static jb_err actions_from_radio(const struct map * parameters,
406 struct action_spec *action);
409 static jb_err map_copy_parameter_html(struct map *out,
410 const struct map *in,
412 #if 0 /* unused function */
413 static jb_err map_copy_parameter_url(struct map *out,
414 const struct map *in,
416 #endif /* unused function */
418 /*********************************************************************
420 * Function : map_copy_parameter_html
422 * Description : Copy a CGI parameter from one map to another, HTML
426 * 1 : out = target map
427 * 2 : in = source map
428 * 3 : name = name of cgi parameter to copy
430 * Returns : JB_ERR_OK on success
431 * JB_ERR_MEMORY on out-of-memory
432 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
435 *********************************************************************/
436 static jb_err map_copy_parameter_html(struct map *out,
437 const struct map *in,
447 value = lookup(in, name);
448 err = map(out, name, 1, html_encode(value), 0);
455 else if (*value == '\0')
457 return JB_ERR_CGI_PARAMS;
466 #if 0 /* unused function */
467 /*********************************************************************
469 * Function : map_copy_parameter_url
471 * Description : Copy a CGI parameter from one map to another, URL
475 * 1 : out = target map
476 * 2 : in = source map
477 * 3 : name = name of cgi parameter to copy
479 * Returns : JB_ERR_OK on success
480 * JB_ERR_MEMORY on out-of-memory
481 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
484 *********************************************************************/
485 static jb_err map_copy_parameter_url(struct map *out,
486 const struct map *in,
496 value = lookup(in, name);
497 err = map(out, name, 1, url_encode(value), 0);
504 else if (*value == '\0')
506 return JB_ERR_CGI_PARAMS;
513 #endif /* 0 - unused function */
515 /*********************************************************************
517 * Function : cgi_edit_actions_url_form
519 * Description : CGI function that displays a form for
523 * 1 : csp = Current client state (buffers, headers, etc...)
524 * 2 : rsp = http_response data structure for output
525 * 3 : parameters = map of cgi parameters
528 * f : (filename) Identifies the file to edit
529 * v : (version) File's last-modified time
530 * p : (pattern) Line number of pattern to edit
532 * Returns : JB_ERR_OK on success
533 * JB_ERR_MEMORY on out-of-memory
534 * JB_ERR_CGI_PARAMS if the CGI parameters are not
535 * specified or not valid.
537 *********************************************************************/
538 jb_err cgi_edit_actions_url_form(struct client_state *csp,
539 struct http_response *rsp,
540 const struct map *parameters)
542 struct map * exports;
544 struct editable_file * file;
545 struct file_line * cur_line;
546 unsigned line_number;
553 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
555 return cgi_error_disabled(csp, rsp);
558 err = get_number_param(csp, parameters, "p", &patternid);
564 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
567 /* No filename specified, can't read file, modified, or out of memory. */
568 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
571 cur_line = file->lines;
573 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
575 cur_line = cur_line->next;
578 if ( (cur_line == NULL)
579 || (line_number != patternid)
581 || (cur_line->type != FILE_LINE_URL))
583 /* Invalid "patternid" parameter */
584 edit_free_file(file);
585 return JB_ERR_CGI_PARAMS;
588 if (NULL == (exports = default_exports(csp, NULL)))
590 edit_free_file(file);
591 return JB_ERR_MEMORY;
594 err = map(exports, "f", 1, file->identifier, 1);
595 if (!err) err = map(exports, "v", 1, file->version_str, 1);
596 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
597 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
599 edit_free_file(file);
607 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
611 /*********************************************************************
613 * Function : cgi_edit_actions_add_url_form
615 * Description : CGI function that displays a form for
619 * 1 : csp = Current client state (buffers, headers, etc...)
620 * 2 : rsp = http_response data structure for output
621 * 3 : parameters = map of cgi parameters
624 * f : (filename) Identifies the file to edit
625 * v : (version) File's last-modified time
626 * s : (section) Line number of section to edit
628 * Returns : JB_ERR_OK on success
629 * JB_ERR_MEMORY on out-of-memory
630 * JB_ERR_CGI_PARAMS if the CGI parameters are not
631 * specified or not valid.
633 *********************************************************************/
634 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
635 struct http_response *rsp,
636 const struct map *parameters)
645 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
647 return cgi_error_disabled(csp, rsp);
650 if (NULL == (exports = default_exports(csp, NULL)))
652 return JB_ERR_MEMORY;
655 err = map_copy_parameter_html(exports, parameters, "f");
656 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
657 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
665 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
669 /*********************************************************************
671 * Function : cgi_edit_actions_remove_url_form
673 * Description : CGI function that displays a form for
677 * 1 : csp = Current client state (buffers, headers, etc...)
678 * 2 : rsp = http_response data structure for output
679 * 3 : parameters = map of cgi parameters
682 * f : (filename) Identifies the file to edit
683 * v : (version) File's last-modified time
684 * p : (pattern) Line number of pattern to edit
686 * Returns : JB_ERR_OK on success
687 * JB_ERR_MEMORY on out-of-memory
688 * JB_ERR_CGI_PARAMS if the CGI parameters are not
689 * specified or not valid.
691 *********************************************************************/
692 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
693 struct http_response *rsp,
694 const struct map *parameters)
696 struct map * exports;
698 struct editable_file * file;
699 struct file_line * cur_line;
700 unsigned line_number;
707 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
709 return cgi_error_disabled(csp, rsp);
712 err = get_number_param(csp, parameters, "p", &patternid);
718 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
721 /* No filename specified, can't read file, modified, or out of memory. */
722 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
725 cur_line = file->lines;
727 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
729 cur_line = cur_line->next;
732 if ( (cur_line == NULL)
733 || (line_number != patternid)
735 || (cur_line->type != FILE_LINE_URL))
737 /* Invalid "patternid" parameter */
738 edit_free_file(file);
739 return JB_ERR_CGI_PARAMS;
742 if (NULL == (exports = default_exports(csp, NULL)))
744 edit_free_file(file);
745 return JB_ERR_MEMORY;
748 err = map(exports, "f", 1, file->identifier, 1);
749 if (!err) err = map(exports, "v", 1, file->version_str, 1);
750 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
751 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
753 edit_free_file(file);
761 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
765 /*********************************************************************
767 * Function : edit_write_file
769 * Description : Write a complete file to disk.
772 * 1 : filename = File to write to.
773 * 2 : file = Data structure to write.
775 * Returns : JB_ERR_OK on success
776 * JB_ERR_FILE on error writing to file.
777 * JB_ERR_MEMORY on out of memory
779 *********************************************************************/
780 jb_err edit_write_file(struct editable_file * file)
783 struct file_line * cur_line;
784 struct stat statbuf[1];
785 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
786 digits in time_t, assuming this is a 64-bit
787 machine, plus null terminator, plus one
791 assert(file->filename);
793 if (NULL == (fp = fopen(file->filename, "wb")))
798 cur_line = file->lines;
799 while (cur_line != NULL)
803 if (fputs(cur_line->raw, fp) < 0)
811 if (cur_line->prefix)
813 if (fputs(cur_line->prefix, fp) < 0)
819 if (cur_line->unprocessed)
822 if (NULL != strchr(cur_line->unprocessed, '#'))
824 /* Must quote '#' characters */
831 /* Count number of # characters, so we know length of output string */
832 src = cur_line->unprocessed;
833 while (NULL != (src = strchr(src, '#')))
840 /* Allocate new memory for string */
841 len = strlen(cur_line->unprocessed);
842 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
844 /* Uh oh, just trashed file! */
846 return JB_ERR_MEMORY;
849 /* Loop through string from end */
850 src = cur_line->unprocessed + len;
851 dest = str + len + numhash;
852 for ( ; len >= 0; len--)
854 if ((*dest-- = *src--) == '#')
858 assert(numhash >= 0);
861 assert(numhash == 0);
862 assert(src + 1 == cur_line->unprocessed);
863 assert(dest + 1 == str);
865 if (fputs(str, fp) < 0)
876 /* Can write without quoting '#' characters. */
877 if (fputs(cur_line->unprocessed, fp) < 0)
883 if (fputs(NEWLINE(file->newline), fp) < 0)
891 /* FIXME: Write data from file->data->whatever */
895 cur_line = cur_line->next;
901 /* Update the version stamp in the file structure, since we just
902 * wrote to the file & changed it's date.
904 if (stat(file->filename, statbuf) < 0)
906 /* Error, probably file not found. */
909 file->version = (unsigned)statbuf->st_mtime;
911 /* Correct file->version_str */
912 freez(file->version_str);
913 snprintf(version_buf, 22, "%u", file->version);
914 version_buf[21] = '\0';
915 file->version_str = strdup(version_buf);
916 if (version_buf == NULL)
918 return JB_ERR_MEMORY;
925 /*********************************************************************
927 * Function : edit_free_file
929 * Description : Free a complete file in memory.
932 * 1 : file = Data structure to free.
936 *********************************************************************/
937 void edit_free_file(struct editable_file * file)
941 /* Silently ignore NULL pointer */
945 edit_free_file_lines(file->lines);
946 freez(file->filename);
947 freez(file->identifier);
948 freez(file->version_str);
950 file->parse_error_text = NULL; /* Statically allocated */
951 file->parse_error = NULL;
957 /*********************************************************************
959 * Function : edit_free_file_lines
961 * Description : Free an entire linked list of file lines.
964 * 1 : first_line = Data structure to free.
968 *********************************************************************/
969 static void edit_free_file_lines(struct file_line * first_line)
971 struct file_line * next_line;
973 while (first_line != NULL)
975 next_line = first_line->next;
976 first_line->next = NULL;
977 freez(first_line->raw);
978 freez(first_line->prefix);
979 freez(first_line->unprocessed);
980 switch(first_line->type)
982 case 0: /* special case if memory zeroed */
983 case FILE_LINE_UNPROCESSED:
984 case FILE_LINE_BLANK:
985 case FILE_LINE_ALIAS_HEADER:
986 case FILE_LINE_SETTINGS_HEADER:
987 case FILE_LINE_DESCRIPTION_HEADER:
988 case FILE_LINE_DESCRIPTION_ENTRY:
989 case FILE_LINE_ALIAS_ENTRY:
991 /* No data is stored for these */
994 case FILE_LINE_ACTION:
995 free_action(first_line->data.action);
998 case FILE_LINE_SETTINGS_ENTRY:
999 freez(first_line->data.setting.name);
1000 freez(first_line->data.setting.svalue);
1003 /* Should never happen */
1007 first_line->type = 0; /* paranoia */
1009 first_line = next_line;
1014 /*********************************************************************
1016 * Function : match_actions_file_header_line
1018 * Description : Match an actions file {{header}} line
1021 * 1 : line = String from file
1022 * 2 : name = Header to match against
1024 * Returns : 0 iff they match.
1026 *********************************************************************/
1027 static int match_actions_file_header_line(const char * line, const char * name)
1035 if ((line[0] != '{') || (line[1] != '{'))
1041 /* Look for optional whitespace */
1042 while ( (*line == ' ') || (*line == '\t') )
1047 /* Look for the specified name (case-insensitive) */
1049 if (0 != strncmpic(line, name, len))
1055 /* Look for optional whitespace */
1056 while ( (*line == ' ') || (*line == '\t') )
1061 /* Look for "}}" and end of string*/
1062 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1072 /*********************************************************************
1074 * Function : match_actions_file_header_line
1076 * Description : Match an actions file {{header}} line
1079 * 1 : line = String from file. Must not start with
1080 * whitespace (else infinite loop!)
1081 * 2 : name = Destination for name
1082 * 2 : name = Destination for value
1084 * Returns : JB_ERR_OK on success
1085 * JB_ERR_MEMORY on out-of-memory
1086 * JB_ERR_PARSE if there's no "=" sign, or if there's
1087 * nothing before the "=" sign (but empty
1088 * values *after* the "=" sign are legal).
1090 *********************************************************************/
1091 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1093 const char * name_end;
1094 const char * value_start;
1100 assert(*line != ' ');
1101 assert(*line != '\t');
1106 value_start = strchr(line, '=');
1107 if ((value_start == NULL) || (value_start == line))
1109 return JB_ERR_PARSE;
1112 name_end = value_start - 1;
1114 /* Eat any whitespace before the '=' */
1115 while ((*name_end == ' ') || (*name_end == '\t'))
1118 * we already know we must have at least 1 non-ws char
1119 * at start of buf - no need to check
1124 name_len = name_end - line + 1; /* Length excluding \0 */
1125 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1127 return JB_ERR_MEMORY;
1129 strncpy(*pname, line, name_len);
1130 (*pname)[name_len] = '\0';
1132 /* Eat any the whitespace after the '=' */
1134 while ((*value_start == ' ') || (*value_start == '\t'))
1139 if (NULL == (*pvalue = strdup(value_start)))
1143 return JB_ERR_MEMORY;
1150 /*********************************************************************
1152 * Function : edit_parse_actions_file
1154 * Description : Parse an actions file in memory.
1156 * Passed linked list must have the "data" member
1157 * zeroed, and must contain valid "next" and
1158 * "unprocessed" fields. The "raw" and "prefix"
1159 * fields are ignored, and "type" is just overwritten.
1161 * Note that on error the file may have been
1165 * 1 : file = Actions file to be parsed in-place.
1167 * Returns : JB_ERR_OK on success
1168 * JB_ERR_MEMORY on out-of-memory
1169 * JB_ERR_PARSE on error
1171 *********************************************************************/
1172 jb_err edit_parse_actions_file(struct editable_file * file)
1174 struct file_line * cur_line;
1176 const char * text; /* Text from a line */
1177 char * name; /* For lines of the form name=value */
1178 char * value; /* For lines of the form name=value */
1179 struct action_alias * alias_list = NULL;
1180 jb_err err = JB_ERR_OK;
1182 /* alias_list contains the aliases defined in this file.
1183 * It might be better to use the "file_line.data" fields
1184 * in the relavent places instead.
1187 cur_line = file->lines;
1189 /* A note about blank line support: Blank lines should only
1190 * ever occur as the last line in the file. This function
1191 * is more forgiving than that - FILE_LINE_BLANK can occur
1195 /* Skip leading blanks. Should only happen if file is
1196 * empty (which is valid, but pointless).
1198 while ( (cur_line != NULL)
1199 && (cur_line->unprocessed[0] == '\0') )
1202 cur_line->type = FILE_LINE_BLANK;
1203 cur_line = cur_line->next;
1206 if ( (cur_line != NULL)
1207 && (cur_line->unprocessed[0] != '{') )
1209 /* File doesn't start with a header */
1210 file->parse_error = cur_line;
1211 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1212 return JB_ERR_PARSE;
1215 if ( (cur_line != NULL) && (0 ==
1216 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1218 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1220 cur_line = cur_line->next;
1221 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1223 if (cur_line->unprocessed[0])
1225 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1227 err = split_line_on_equals(cur_line->unprocessed,
1228 &cur_line->data.setting.name,
1229 &cur_line->data.setting.svalue);
1230 if (err == JB_ERR_MEMORY)
1234 else if (err != JB_ERR_OK)
1236 /* Line does not contain a name=value pair */
1237 file->parse_error = cur_line;
1238 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1239 return JB_ERR_PARSE;
1244 cur_line->type = FILE_LINE_BLANK;
1246 cur_line = cur_line->next;
1250 if ( (cur_line != NULL) && (0 ==
1251 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1253 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1255 cur_line = cur_line->next;
1256 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1258 if (cur_line->unprocessed[0])
1260 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1264 cur_line->type = FILE_LINE_BLANK;
1266 cur_line = cur_line->next;
1270 if ( (cur_line != NULL) && (0 ==
1271 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1273 cur_line->type = FILE_LINE_ALIAS_HEADER;
1275 cur_line = cur_line->next;
1276 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1278 if (cur_line->unprocessed[0])
1280 /* define an alias */
1281 struct action_alias * new_alias;
1283 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1285 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1286 if (err == JB_ERR_MEMORY)
1290 else if (err != JB_ERR_OK)
1292 /* Line does not contain a name=value pair */
1293 file->parse_error = cur_line;
1294 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1295 return JB_ERR_PARSE;
1298 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1303 free_alias_list(alias_list);
1304 return JB_ERR_MEMORY;
1307 err = get_actions(value, alias_list, new_alias->action);
1310 /* Invalid action or out of memory */
1314 free_alias_list(alias_list);
1315 if (err == JB_ERR_MEMORY)
1321 /* Line does not contain a name=value pair */
1322 file->parse_error = cur_line;
1323 file->parse_error_text = "This alias does not specify a valid set of actions.";
1324 return JB_ERR_PARSE;
1330 new_alias->name = name;
1333 new_alias->next = alias_list;
1334 alias_list = new_alias;
1338 cur_line->type = FILE_LINE_BLANK;
1340 cur_line = cur_line->next;
1344 /* Header done, process the main part of the file */
1345 while (cur_line != NULL)
1347 /* At this point, (cur_line->unprocessed[0] == '{') */
1348 assert(cur_line->unprocessed[0] == '{');
1349 text = cur_line->unprocessed + 1;
1350 len = strlen(text) - 1;
1351 if (text[len] != '}')
1353 /* No closing } on header */
1354 free_alias_list(alias_list);
1355 file->parse_error = cur_line;
1356 file->parse_error_text = "Headers starting with '{' must have a "
1357 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1358 "must close with two brackets ('}}').";
1359 return JB_ERR_PARSE;
1364 /* An invalid {{ header. */
1365 free_alias_list(alias_list);
1366 file->parse_error = cur_line;
1367 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1368 "Please remember that the system (two-bracket) headers must "
1369 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1370 "and must appear before any actions (one-bracket) headers. "
1371 "Also note that system headers may not be repeated.";
1372 return JB_ERR_PARSE;
1375 while ( (*text == ' ') || (*text == '\t') )
1381 && ( (text[len - 1] == ' ')
1382 || (text[len - 1] == '\t') ) )
1387 cur_line->type = FILE_LINE_ACTION;
1389 /* Remove {} and make copy */
1390 if (NULL == (value = (char *) malloc(len + 1)))
1393 free_alias_list(alias_list);
1394 return JB_ERR_MEMORY;
1396 strncpy(value, text, len);
1400 err = get_actions(value, alias_list, cur_line->data.action);
1403 /* Invalid action or out of memory */
1405 free_alias_list(alias_list);
1406 if (err == JB_ERR_MEMORY)
1412 /* Line does not contain a name=value pair */
1413 file->parse_error = cur_line;
1414 file->parse_error_text = "This header does not specify a valid set of actions.";
1415 return JB_ERR_PARSE;
1419 /* Done with string - it was clobbered anyway */
1422 /* Process next line */
1423 cur_line = cur_line->next;
1425 /* Loop processing URL patterns */
1426 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1428 if (cur_line->unprocessed[0])
1430 /* Could parse URL here, but this isn't currently needed */
1432 cur_line->type = FILE_LINE_URL;
1436 cur_line->type = FILE_LINE_BLANK;
1438 cur_line = cur_line->next;
1440 } /* End main while(cur_line != NULL) loop */
1442 free_alias_list(alias_list);
1448 /*********************************************************************
1450 * Function : edit_read_file_lines
1452 * Description : Read all the lines of a file into memory.
1453 * Handles whitespace, comments and line continuation.
1456 * 1 : fp = File to read from. On return, this will be
1457 * at EOF but it will not have been closed.
1458 * 2 : pfile = Destination for a linked list of file_lines.
1459 * Will be set to NULL on error.
1461 * Returns : JB_ERR_OK on success
1462 * JB_ERR_MEMORY on out-of-memory
1464 *********************************************************************/
1465 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1467 struct file_line * first_line; /* Keep for return value or to free */
1468 struct file_line * cur_line; /* Current line */
1469 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1477 cur_line = first_line = zalloc(sizeof(struct file_line));
1478 if (cur_line == NULL)
1480 return JB_ERR_MEMORY;
1483 cur_line->type = FILE_LINE_UNPROCESSED;
1485 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1488 /* Out of memory or empty file. */
1489 /* Note that empty file is not an error we propogate up */
1491 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1496 prev_line = cur_line;
1497 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1498 if (cur_line == NULL)
1501 edit_free_file_lines(first_line);
1502 return JB_ERR_MEMORY;
1505 cur_line->type = FILE_LINE_UNPROCESSED;
1507 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1508 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1511 edit_free_file_lines(first_line);
1512 return JB_ERR_MEMORY;
1516 while (rval != JB_ERR_FILE);
1520 /* We allocated one too many - free it */
1521 prev_line->next = NULL;
1524 *pfile = first_line;
1529 /*********************************************************************
1531 * Function : edit_read_file
1533 * Description : Read a complete file into memory.
1534 * Handles CGI parameter parsing. If requested, also
1535 * checks the file's modification timestamp.
1538 * 1 : csp = Current client state (buffers, headers, etc...)
1539 * 2 : parameters = map of cgi parameters.
1540 * 3 : require_version = true to check "ver" parameter.
1541 * 4 : suffix = File extension, e.g. ".action".
1542 * 5 : pfile = Destination for the file. Will be set
1546 * filename : The name of the file to read, without the
1547 * path or ".action" extension.
1548 * ver : (Only if require_version is nonzero)
1549 * Timestamp of the actions file. If wrong, this
1550 * function fails with JB_ERR_MODIFIED.
1552 * Returns : JB_ERR_OK on success
1553 * JB_ERR_MEMORY on out-of-memory
1554 * JB_ERR_CGI_PARAMS if "filename" was not specified
1556 * JB_ERR_FILE if the file cannot be opened or
1558 * JB_ERR_MODIFIED if version checking was requested and
1559 * failed - the file was modified outside
1560 * of this CGI editor instance.
1562 *********************************************************************/
1563 jb_err edit_read_file(struct client_state *csp,
1564 const struct map *parameters,
1565 int require_version,
1567 struct editable_file **pfile)
1569 struct file_line * lines;
1573 const char * identifier;
1574 struct editable_file * file;
1575 unsigned version = 0;
1576 struct stat statbuf[1];
1577 char version_buf[22];
1578 int newline = NEWLINE_UNKNOWN;
1586 err = get_file_name_param(csp, parameters, "f", suffix,
1587 &filename, &identifier);
1593 if (stat(filename, statbuf) < 0)
1595 /* Error, probably file not found. */
1599 version = (unsigned) statbuf->st_mtime;
1601 if (require_version)
1603 unsigned specified_version;
1604 err = get_number_param(csp, parameters, "v", &specified_version);
1611 if (version != specified_version)
1613 return JB_ERR_MODIFIED;
1617 if (NULL == (fp = fopen(filename,"rb")))
1623 err = edit_read_file_lines(fp, &lines, &newline);
1633 file = (struct editable_file *) zalloc(sizeof(*file));
1637 edit_free_file_lines(lines);
1641 file->lines = lines;
1642 file->newline = newline;
1643 file->filename = filename;
1644 file->version = version;
1645 file->identifier = url_encode(identifier);
1647 if (file->identifier == NULL)
1649 edit_free_file(file);
1650 return JB_ERR_MEMORY;
1653 /* Correct file->version_str */
1654 freez(file->version_str);
1655 snprintf(version_buf, 22, "%u", file->version);
1656 version_buf[21] = '\0';
1657 file->version_str = strdup(version_buf);
1658 if (version_buf == NULL)
1660 edit_free_file(file);
1661 return JB_ERR_MEMORY;
1669 /*********************************************************************
1671 * Function : edit_read_actions_file
1673 * Description : Read a complete actions file into memory.
1674 * Handles CGI parameter parsing. If requested, also
1675 * checks the file's modification timestamp.
1677 * If this function detects an error in the categories
1678 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1679 * then it handles it by filling in the specified
1680 * response structure and returning JB_ERR_FILE.
1683 * 1 : csp = Current client state (buffers, headers, etc...)
1684 * 2 : rsp = HTTP response. Only filled in on error.
1685 * 2 : parameters = map of cgi parameters.
1686 * 3 : require_version = true to check "ver" parameter.
1687 * 4 : pfile = Destination for the file. Will be set
1691 * filename : The name of the actions file to read, without the
1692 * path or ".action" extension.
1693 * ver : (Only if require_version is nonzero)
1694 * Timestamp of the actions file. If wrong, this
1695 * function fails with JB_ERR_MODIFIED.
1697 * Returns : JB_ERR_OK on success
1698 * JB_ERR_MEMORY on out-of-memory
1699 * JB_ERR_CGI_PARAMS if "filename" was not specified
1701 * JB_ERR_FILE if the file does not contain valid data,
1702 * or if file cannot be opened or
1703 * contains no data, or if version
1704 * checking was requested and failed.
1706 *********************************************************************/
1707 jb_err edit_read_actions_file(struct client_state *csp,
1708 struct http_response *rsp,
1709 const struct map *parameters,
1710 int require_version,
1711 struct editable_file **pfile)
1714 struct editable_file *file;
1722 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1725 /* Try to handle if possible */
1726 if (err == JB_ERR_FILE)
1728 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1730 else if (err == JB_ERR_MODIFIED)
1732 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1734 if (err == JB_ERR_OK)
1737 * Signal to higher-level CGI code that there was a problem but we
1738 * handled it, they should just return JB_ERR_OK.
1745 err = edit_parse_actions_file(file);
1748 if (err == JB_ERR_PARSE)
1750 err = cgi_error_parse(csp, rsp, file);
1751 if (err == JB_ERR_OK)
1754 * Signal to higher-level CGI code that there was a problem but we
1755 * handled it, they should just return JB_ERR_OK.
1760 edit_free_file(file);
1769 /*********************************************************************
1771 * Function : get_file_name_param
1773 * Description : Get the name of the file to edit from the parameters
1774 * passed to a CGI function. This function handles
1775 * security checks such as blocking urls containing
1776 * "/" or ".", prepending the config file directory,
1777 * and adding the specified suffix.
1779 * (This is an essential security check, otherwise
1780 * users may be able to pass "../../../etc/passwd"
1781 * and overwrite the password file [linux], "prn:"
1782 * and print random data [Windows], etc...)
1784 * This function only allows filenames contining the
1785 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1786 * That's probably too restrictive but at least it's
1790 * 1 : csp = Current client state (buffers, headers, etc...)
1791 * 2 : parameters = map of cgi parameters
1792 * 3 : param_name = The name of the parameter to read
1793 * 4 : suffix = File extension, e.g. ".actions"
1794 * 5 : pfilename = destination for full filename. Caller
1795 * free()s. Set to NULL on error.
1796 * 6 : pparam = destination for partial filename,
1797 * suitable for use in another URL. Allocated as part
1798 * of the map "parameters", so don't free it.
1799 * Set to NULL if not specified.
1801 * Returns : JB_ERR_OK on success
1802 * JB_ERR_MEMORY on out-of-memory
1803 * JB_ERR_CGI_PARAMS if "filename" was not specified
1806 *********************************************************************/
1807 static jb_err get_file_name_param(struct client_state *csp,
1808 const struct map *parameters,
1809 const char *param_name,
1812 const char **pparam)
1830 param = lookup(parameters, param_name);
1833 return JB_ERR_CGI_PARAMS;
1838 len = strlen(param);
1839 if (len >= FILENAME_MAX)
1842 return JB_ERR_CGI_PARAMS;
1845 /* Check every character to see if it's legal */
1847 while ((ch = *s++) != '\0')
1849 if ( ((ch < 'A') || (ch > 'Z'))
1850 && ((ch < 'a') || (ch > 'z'))
1851 && ((ch < '0') || (ch > '9'))
1855 /* Probable hack attempt. */
1856 return JB_ERR_CGI_PARAMS;
1860 /* Append extension */
1861 name = malloc(len + strlen(suffix) + 1);
1864 return JB_ERR_MEMORY;
1866 strcpy(name, param);
1867 strcpy(name + len, suffix);
1870 fullpath = make_path(csp->config->confdir, name);
1873 if (fullpath == NULL)
1875 return JB_ERR_MEMORY;
1879 *pfilename = fullpath;
1885 /*********************************************************************
1887 * Function : get_url_spec_param
1889 * Description : Get a URL pattern from the parameters
1890 * passed to a CGI function. Removes leading/trailing
1891 * spaces and validates it.
1894 * 1 : csp = Current client state (buffers, headers, etc...)
1895 * 2 : parameters = map of cgi parameters
1896 * 3 : name = Name of CGI parameter to read
1897 * 4 : pvalue = destination for value. Will be malloc()'d.
1898 * Set to NULL on error.
1900 * Returns : JB_ERR_OK on success
1901 * JB_ERR_MEMORY on out-of-memory
1902 * JB_ERR_CGI_PARAMS if the parameter was not specified
1905 *********************************************************************/
1906 static jb_err get_url_spec_param(struct client_state *csp,
1907 const struct map *parameters,
1911 const char *orig_param;
1914 struct url_spec compiled[1];
1924 orig_param = lookup(parameters, name);
1927 return JB_ERR_CGI_PARAMS;
1930 /* Copy and trim whitespace */
1931 param = strdup(orig_param);
1934 return JB_ERR_MEMORY;
1938 /* Must be non-empty, and can't allow 1st character to be '{' */
1939 if (param[0] == '\0' || param[0] == '{')
1942 return JB_ERR_CGI_PARAMS;
1945 /* Check for embedded newlines */
1946 for (s = param; *s != '\0'; s++)
1948 if ((*s == '\r') || (*s == '\n'))
1951 return JB_ERR_CGI_PARAMS;
1955 /* Check that regex is valid */
1960 return JB_ERR_MEMORY;
1962 err = create_url_spec(compiled, s);
1967 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1969 free_url_spec(compiled);
1971 if (param[strlen(param) - 1] == '\\')
1974 * Must protect trailing '\\' from becoming line continuation character.
1975 * Two methods: 1) If it's a domain only, add a trailing '/'.
1976 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1978 if (strchr(param, '/') == NULL)
1980 err = string_append(¶m, "/");
1984 err = string_append(¶m, "(?:)");
1991 /* Check that the modified regex is valid */
1996 return JB_ERR_MEMORY;
1998 err = create_url_spec(compiled, s);
2003 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2005 free_url_spec(compiled);
2012 /*********************************************************************
2014 * Function : map_radio
2016 * Description : Map a set of radio button values. E.g. if you have
2017 * 3 radio buttons, declare them as:
2018 * <option type="radio" name="xyz" @xyz-a@>
2019 * <option type="radio" name="xyz" @xyz-b@>
2020 * <option type="radio" name="xyz" @xyz-c@>
2021 * Then map one of the @xyz-?@ variables to "checked"
2022 * and all the others to empty by calling:
2023 * map_radio(exports, "xyz", "abc", sel)
2024 * Where 'sel' is 'a', 'b', or 'c'.
2027 * 1 : exports = Exports map to modify.
2028 * 2 : optionname = name for map
2029 * 3 : values = null-terminated list of values;
2030 * 4 : value = Selected value.
2032 * CGI Parameters : None
2034 * Returns : JB_ERR_OK on success
2035 * JB_ERR_MEMORY on out-of-memory
2037 *********************************************************************/
2038 static jb_err map_radio(struct map * exports,
2039 const char * optionname,
2040 const char * values,
2052 len = strlen(optionname);
2053 buf = malloc(len + 3);
2056 return JB_ERR_MEMORY;
2059 strcpy(buf, optionname);
2064 while ((c = *values++) != '\0')
2069 if (map(exports, buf, 1, "", 1))
2071 return JB_ERR_MEMORY;
2077 return map(exports, buf, 0, "checked", 1);
2081 /*********************************************************************
2083 * Function : cgi_error_modified
2085 * Description : CGI function that is called when a file is modified
2086 * outside the CGI editor.
2089 * 1 : csp = Current client state (buffers, headers, etc...)
2090 * 2 : rsp = http_response data structure for output
2091 * 3 : filename = The file that was modified.
2093 * CGI Parameters : none
2095 * Returns : JB_ERR_OK on success
2096 * JB_ERR_MEMORY on out-of-memory error.
2098 *********************************************************************/
2099 jb_err cgi_error_modified(struct client_state *csp,
2100 struct http_response *rsp,
2101 const char *filename)
2103 struct map *exports;
2110 if (NULL == (exports = default_exports(csp, NULL)))
2112 return JB_ERR_MEMORY;
2115 err = map(exports, "f", 1, html_encode(filename), 0);
2122 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2126 /*********************************************************************
2128 * Function : cgi_error_parse
2130 * Description : CGI function that is called when a file cannot
2131 * be parsed by the CGI editor.
2134 * 1 : csp = Current client state (buffers, headers, etc...)
2135 * 2 : rsp = http_response data structure for output
2136 * 3 : file = The file that was modified.
2138 * CGI Parameters : none
2140 * Returns : JB_ERR_OK on success
2141 * JB_ERR_MEMORY on out-of-memory error.
2143 *********************************************************************/
2144 jb_err cgi_error_parse(struct client_state *csp,
2145 struct http_response *rsp,
2146 struct editable_file *file)
2148 struct map *exports;
2150 struct file_line *cur_line;
2156 if (NULL == (exports = default_exports(csp, NULL)))
2158 return JB_ERR_MEMORY;
2161 err = map(exports, "f", 1, file->identifier, 1);
2162 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2164 cur_line = file->parse_error;
2167 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2168 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2176 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2180 /*********************************************************************
2182 * Function : cgi_error_file
2184 * Description : CGI function that is called when a file cannot be
2185 * opened by the CGI editor.
2188 * 1 : csp = Current client state (buffers, headers, etc...)
2189 * 2 : rsp = http_response data structure for output
2190 * 3 : filename = The file that was modified.
2192 * CGI Parameters : none
2194 * Returns : JB_ERR_OK on success
2195 * JB_ERR_MEMORY on out-of-memory error.
2197 *********************************************************************/
2198 jb_err cgi_error_file(struct client_state *csp,
2199 struct http_response *rsp,
2200 const char *filename)
2202 struct map *exports;
2209 if (NULL == (exports = default_exports(csp, NULL)))
2211 return JB_ERR_MEMORY;
2214 err = map(exports, "f", 1, html_encode(filename), 0);
2221 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2225 /*********************************************************************
2227 * Function : cgi_error_disabled
2229 * Description : CGI function that is called if the actions editor
2230 * is called although it's disabled in config
2233 * 1 : csp = Current client state (buffers, headers, etc...)
2234 * 2 : rsp = http_response data structure for output
2236 * CGI Parameters : none
2238 * Returns : JB_ERR_OK on success
2239 * JB_ERR_MEMORY on out-of-memory error.
2241 *********************************************************************/
2242 jb_err cgi_error_disabled(struct client_state *csp,
2243 struct http_response *rsp)
2245 struct map *exports;
2250 if (NULL == (exports = default_exports(csp, NULL)))
2252 return JB_ERR_MEMORY;
2255 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2259 /*********************************************************************
2261 * Function : cgi_edit_actions
2263 * Description : CGI function that allows the user to choose which
2264 * actions file to edit.
2267 * 1 : csp = Current client state (buffers, headers, etc...)
2268 * 2 : rsp = http_response data structure for output
2269 * 3 : parameters = map of cgi parameters
2271 * CGI Parameters : None
2273 * Returns : JB_ERR_OK on success
2274 * JB_ERR_MEMORY on out-of-memory error
2276 *********************************************************************/
2277 jb_err cgi_edit_actions(struct client_state *csp,
2278 struct http_response *rsp,
2279 const struct map *parameters)
2282 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2284 return cgi_error_disabled(csp, rsp);
2287 /* FIXME: Incomplete */
2288 rsp->status = strdup("302 Local Redirect from Privoxy");
2289 if (rsp->status == NULL)
2291 return JB_ERR_MEMORY;
2293 if (enlist_unique_header(rsp->headers, "Location",
2294 CGI_PREFIX "edit-actions-list?f=default"))
2298 return JB_ERR_MEMORY;
2305 /*********************************************************************
2307 * Function : cgi_edit_actions_list
2309 * Description : CGI function that edits the actions list.
2310 * FIXME: This function shouldn't FATAL ever.
2311 * FIXME: This function doesn't check the retval of map()
2313 * 1 : csp = Current client state (buffers, headers, etc...)
2314 * 2 : rsp = http_response data structure for output
2315 * 3 : parameters = map of cgi parameters
2317 * CGI Parameters : filename
2319 * Returns : JB_ERR_OK on success
2320 * JB_ERR_MEMORY on out-of-memory
2321 * JB_ERR_FILE if the file cannot be opened or
2323 * JB_ERR_CGI_PARAMS if "filename" was not specified
2326 *********************************************************************/
2327 jb_err cgi_edit_actions_list(struct client_state *csp,
2328 struct http_response *rsp,
2329 const struct map *parameters)
2331 char * section_template;
2332 char * url_template;
2337 struct map * exports;
2338 struct map * section_exports;
2339 struct map * url_exports;
2340 struct editable_file * file;
2341 struct file_line * cur_line;
2342 unsigned line_number = 0;
2343 unsigned prev_section_line_number = ((unsigned) (-1));
2345 struct file_list * fl;
2346 struct url_actions * b;
2347 char * buttons = NULL;
2350 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2352 return cgi_error_disabled(csp, rsp);
2355 if (NULL == (exports = default_exports(csp, NULL)))
2357 edit_free_file(file);
2358 return JB_ERR_MEMORY;
2361 /* Load actions file */
2362 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2365 /* No filename specified, can't read file, or out of memory. */
2366 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2369 /* Find start of actions in file */
2370 cur_line = file->lines;
2372 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2374 cur_line = cur_line->next;
2379 * Conventional actions files should have a match all block
2381 * cur_line = {...global actions...}
2382 * cur_line->next = /
2383 * cur_line->next->next = {...actions...} or EOF
2385 if ( (cur_line != NULL)
2386 && (cur_line->type == FILE_LINE_ACTION)
2387 && (cur_line->next != NULL)
2388 && (cur_line->next->type == FILE_LINE_URL)
2389 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2390 && ( (cur_line->next->next == NULL)
2391 || (cur_line->next->next->type != FILE_LINE_URL)
2395 * Generate string with buttons to set actions for "/" to
2396 * any predefined set of actions (named standard.*, propably
2397 * residing in standard.action).
2398 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
2401 buttons = strdup(" ");
2402 for (i = 0; i < MAX_ACTION_FILES; i++)
2404 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2406 for (b = b->next; NULL != b; b = b->next)
2408 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2410 snprintf(buf, 150, "<p><a name=\"l@s@\" href=\"eas?f=@f@&v=@v@&s=@all-urls-s@&p=%s\">Set"
2411 " to %s</a></p>", b->url->spec + 9, b->url->spec + 9);
2412 if (!err) err = string_append(&buttons, buf);
2417 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2420 * Conventional actions file, supply extra editing help.
2421 * (e.g. don't allow them to make it an unconventional one).
2423 err = map_conditional(exports, "all-urls-present", 1);
2425 snprintf(buf, 150, "%d", line_number);
2426 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2427 snprintf(buf, 150, "%d", line_number + 2);
2428 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2429 if (!err) err = map(exports, "all-urls-actions", 1,
2430 actions_to_html(csp, cur_line->data.action), 0);
2432 /* Skip the 2 lines */
2433 cur_line = cur_line->next->next;
2437 * Note that prev_section_line_number is NOT set here.
2438 * This is deliberate and not a bug. It stops a "Move up"
2439 * option appearing on the next section. Clicking "Move
2440 * up" would make the actions file unconventional, which
2441 * we don't want, so we hide this option.
2447 * Non-standard actions file - does not begin with
2448 * the "All URLs" section.
2450 err = map_conditional(exports, "all-urls-present", 0);
2453 /* Set up global exports */
2455 err = map(exports, "f", 1, file->identifier, 1);
2456 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2458 /* Discourage private additions to default.action */
2460 if (!err) err = map_conditional(exports, "default-action",
2461 (strcmp("default", lookup(parameters, "f")) == 0));
2464 edit_free_file(file);
2469 /* Should do all global exports above this point */
2471 /* Load templates */
2473 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2476 edit_free_file(file);
2478 if (err == JB_ERR_FILE)
2480 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2485 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2488 free(section_template);
2489 edit_free_file(file);
2491 if (err == JB_ERR_FILE)
2493 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2498 err = template_fill(§ion_template, exports);
2502 edit_free_file(file);
2508 err = template_fill(&url_template, exports);
2511 free(section_template);
2512 edit_free_file(file);
2517 if (NULL == (sections = strdup("")))
2519 free(section_template);
2521 edit_free_file(file);
2523 return JB_ERR_MEMORY;
2526 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2528 if (NULL == (section_exports = new_map()))
2531 free(section_template);
2533 edit_free_file(file);
2535 return JB_ERR_MEMORY;
2538 snprintf(buf, 150, "%d", line_number);
2539 err = map(section_exports, "s", 1, buf, 1);
2540 if (!err) err = map(section_exports, "actions", 1,
2541 actions_to_html(csp, cur_line->data.action), 0);
2544 && (cur_line->next != NULL)
2545 && (cur_line->next->type == FILE_LINE_URL))
2547 /* This section contains at least one URL, don't allow delete */
2548 err = map_block_killer(section_exports, "empty-section");
2552 if (!err) err = map_block_keep(section_exports, "empty-section");
2555 if (prev_section_line_number != ((unsigned)(-1)))
2557 /* Not last section */
2558 snprintf(buf, 150, "%d", prev_section_line_number);
2559 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2560 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2565 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2567 prev_section_line_number = line_number;
2572 free(section_template);
2574 edit_free_file(file);
2576 free_map(section_exports);
2580 /* Should do all section-specific exports above this point */
2582 if (NULL == (urls = strdup("")))
2585 free(section_template);
2587 edit_free_file(file);
2589 free_map(section_exports);
2590 return JB_ERR_MEMORY;
2595 cur_line = cur_line->next;
2598 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2600 if (NULL == (url_exports = new_map()))
2604 free(section_template);
2606 edit_free_file(file);
2608 free_map(section_exports);
2609 return JB_ERR_MEMORY;
2612 snprintf(buf, 150, "%d", line_number);
2613 err = map(url_exports, "p", 1, buf, 1);
2615 snprintf(buf, 150, "%d", url_1_2);
2616 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2618 if (!err) err = map(url_exports, "url-html", 1,
2619 html_encode(cur_line->unprocessed), 0);
2620 if (!err) err = map(url_exports, "url", 1,
2621 url_encode(cur_line->unprocessed), 0);
2627 free(section_template);
2629 edit_free_file(file);
2631 free_map(section_exports);
2632 free_map(url_exports);
2636 if (NULL == (s = strdup(url_template)))
2640 free(section_template);
2642 edit_free_file(file);
2644 free_map(section_exports);
2645 free_map(url_exports);
2646 return JB_ERR_MEMORY;
2649 err = template_fill(&s, section_exports);
2650 if (!err) err = template_fill(&s, url_exports);
2651 if (!err) err = string_append(&urls, s);
2653 free_map(url_exports);
2660 free(section_template);
2662 edit_free_file(file);
2664 free_map(section_exports);
2668 url_1_2 = 3 - url_1_2;
2670 cur_line = cur_line->next;
2674 err = map(section_exports, "urls", 1, urls, 0);
2676 /* Could also do section-specific exports here, but it wouldn't be as fast */
2678 if ( (cur_line != NULL)
2679 && (cur_line->type == FILE_LINE_ACTION))
2681 /* Not last section */
2682 snprintf(buf, 150, "%d", line_number);
2683 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2684 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2689 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2695 free(section_template);
2697 edit_free_file(file);
2699 free_map(section_exports);
2703 if (NULL == (s = strdup(section_template)))
2706 free(section_template);
2708 edit_free_file(file);
2710 free_map(section_exports);
2711 return JB_ERR_MEMORY;
2714 err = template_fill(&s, section_exports);
2715 if (!err) err = string_append(§ions, s);
2718 free_map(section_exports);
2723 free(section_template);
2725 edit_free_file(file);
2731 edit_free_file(file);
2732 free(section_template);
2735 err = map(exports, "sections", 1, sections, 0);
2742 /* Could also do global exports here, but it wouldn't be as fast */
2744 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2748 /*********************************************************************
2750 * Function : cgi_edit_actions_for_url
2752 * Description : CGI function that edits the Actions list.
2755 * 1 : csp = Current client state (buffers, headers, etc...)
2756 * 2 : rsp = http_response data structure for output
2757 * 3 : parameters = map of cgi parameters
2759 * CGI Parameters : None
2761 * Returns : JB_ERR_OK on success
2762 * JB_ERR_MEMORY on out-of-memory
2763 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2764 * specified or not valid.
2766 *********************************************************************/
2767 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2768 struct http_response *rsp,
2769 const struct map *parameters)
2771 struct map * exports;
2773 struct editable_file * file;
2774 struct file_line * cur_line;
2775 unsigned line_number;
2777 struct file_list *filter_file;
2778 struct re_filterfile_spec *filter_group;
2780 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2782 return cgi_error_disabled(csp, rsp);
2785 err = get_number_param(csp, parameters, "s", §ionid);
2791 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2794 /* No filename specified, can't read file, modified, or out of memory. */
2795 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2798 cur_line = file->lines;
2800 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2802 cur_line = cur_line->next;
2805 if ( (cur_line == NULL)
2806 || (line_number != sectionid)
2808 || (cur_line->type != FILE_LINE_ACTION))
2810 /* Invalid "sectionid" parameter */
2811 edit_free_file(file);
2812 return JB_ERR_CGI_PARAMS;
2815 if (NULL == (exports = default_exports(csp, NULL)))
2817 edit_free_file(file);
2818 return JB_ERR_MEMORY;
2821 err = map(exports, "f", 1, file->identifier, 1);
2822 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2823 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2825 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2827 filter_file = csp->rlist;
2828 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2830 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2834 edit_free_file(file);
2839 if (filter_group == NULL)
2841 err = map(exports, "filter-params", 1, "", 1);
2845 /* We have some entries in the filter list */
2848 char * filter_template;
2850 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2853 edit_free_file(file);
2855 if (err == JB_ERR_FILE)
2857 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2862 err = template_fill(&filter_template, exports);
2864 result = strdup("");
2866 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2868 char current_mode = 'x';
2869 struct list_entry *filter_name;
2871 struct map *line_exports;
2874 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2875 while ((filter_name != NULL)
2876 && (0 != strcmp(filter_group->name, filter_name->str)))
2878 filter_name = filter_name->next;
2881 if (filter_name != NULL)
2887 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2888 while ((filter_name != NULL)
2889 && (0 != strcmp(filter_group->name, filter_name->str)))
2891 filter_name = filter_name->next;
2893 if (filter_name != NULL)
2899 /* Generate a unique serial number */
2900 snprintf(number, sizeof(number), "%x", index++);
2901 number[sizeof(number) - 1] = '\0';
2903 line_exports = new_map();
2904 if (line_exports == NULL)
2906 err = JB_ERR_MEMORY;
2911 if (!err) err = map(line_exports, "index", 1, number, 1);
2912 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2913 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2914 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2919 this_line = strdup(filter_template);
2920 if (this_line == NULL) err = JB_ERR_MEMORY;
2922 if (!err) err = template_fill(&this_line, line_exports);
2923 string_join(&result, this_line);
2925 free_map(line_exports);
2929 freez(filter_template);
2933 err = map(exports, "filter-params", 1, result, 0);
2941 if (!err) err = map_radio(exports, "filter-all", "nx",
2942 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
2944 edit_free_file(file);
2952 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2956 /*********************************************************************
2958 * Function : cgi_edit_actions_submit
2960 * Description : CGI function that actually edits the Actions list.
2963 * 1 : csp = Current client state (buffers, headers, etc...)
2964 * 2 : rsp = http_response data structure for output
2965 * 3 : parameters = map of cgi parameters
2967 * CGI Parameters : None
2969 * Returns : JB_ERR_OK on success
2970 * JB_ERR_MEMORY on out-of-memory
2971 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2972 * specified or not valid.
2974 *********************************************************************/
2975 jb_err cgi_edit_actions_submit(struct client_state *csp,
2976 struct http_response *rsp,
2977 const struct map *parameters)
2983 struct editable_file * file;
2984 struct file_line * cur_line;
2985 unsigned line_number;
2989 const char * action_set_name;
2991 struct file_list * fl;
2992 struct url_actions * b;
2994 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2996 return cgi_error_disabled(csp, rsp);
2999 err = get_number_param(csp, parameters, "s", §ionid);
3005 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3008 /* No filename specified, can't read file, modified, or out of memory. */
3009 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3012 cur_line = file->lines;
3014 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3016 cur_line = cur_line->next;
3019 if ( (cur_line == NULL)
3020 || (line_number != sectionid)
3022 || (cur_line->type != FILE_LINE_ACTION))
3024 /* Invalid "sectionid" parameter */
3025 edit_free_file(file);
3026 return JB_ERR_CGI_PARAMS;
3029 get_string_param(parameters, "p", &action_set_name);
3030 if (action_set_name != NULL)
3032 for (index = 0; index < MAX_ACTION_FILES; index++)
3034 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3036 for (b = b->next; NULL != b; b = b->next)
3038 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3040 copy_action(cur_line->data.action, b->action);
3046 edit_free_file(file);
3047 return JB_ERR_CGI_PARAMS;
3053 err = actions_from_radio(parameters, cur_line->data.action);
3059 edit_free_file(file);
3063 ch = get_char_param(parameters, "filter_all");
3066 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3067 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3068 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3072 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3075 for (index = 0; !err; index++)
3082 /* Generate the keys */
3083 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3084 key_value[sizeof(key_value) - 1] = '\0';
3085 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3086 key_name[sizeof(key_name) - 1] = '\0';
3088 err = get_string_param(parameters, key_name, &name);
3097 value = get_char_param(parameters, key_value);
3100 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3101 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3102 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3104 else if (value == 'N')
3106 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3107 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3109 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3110 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3113 else if (value == 'X')
3115 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3116 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3123 edit_free_file(file);
3127 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3130 edit_free_file(file);
3131 return JB_ERR_MEMORY;
3134 len = strlen(actiontext);
3138 * Empty action - must special-case this.
3139 * Simply setting len to 1 is sufficient...
3144 if (NULL == (newtext = malloc(len + 2)))
3148 edit_free_file(file);
3149 return JB_ERR_MEMORY;
3151 strcpy(newtext, actiontext);
3155 newtext[len + 1] = '\0';
3157 freez(cur_line->raw);
3158 freez(cur_line->unprocessed);
3159 cur_line->unprocessed = newtext;
3161 err = edit_write_file(file);
3164 /* Error writing file */
3165 edit_free_file(file);
3169 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3170 string_append(&target, file->identifier);
3172 edit_free_file(file);
3177 return JB_ERR_MEMORY;
3180 rsp->status = strdup("302 Local Redirect from Privoxy");
3181 if (rsp->status == NULL)
3184 return JB_ERR_MEMORY;
3186 err = enlist_unique_header(rsp->headers, "Location", target);
3193 /*********************************************************************
3195 * Function : cgi_edit_actions_url
3197 * Description : CGI function that actually edits a URL pattern in
3201 * 1 : csp = Current client state (buffers, headers, etc...)
3202 * 2 : rsp = http_response data structure for output
3203 * 3 : parameters = map of cgi parameters
3206 * filename : Identifies the file to edit
3207 * ver : File's last-modified time
3208 * section : Line number of section to edit
3209 * pattern : Line number of pattern to edit
3210 * newval : New value for pattern
3212 * Returns : JB_ERR_OK on success
3213 * JB_ERR_MEMORY on out-of-memory
3214 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3215 * specified or not valid.
3217 *********************************************************************/
3218 jb_err cgi_edit_actions_url(struct client_state *csp,
3219 struct http_response *rsp,
3220 const struct map *parameters)
3224 struct editable_file * file;
3225 struct file_line * cur_line;
3226 unsigned line_number;
3230 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3232 return cgi_error_disabled(csp, rsp);
3235 err = get_number_param(csp, parameters, "p", &patternid);
3242 return JB_ERR_CGI_PARAMS;
3245 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3251 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3254 /* No filename specified, can't read file, modified, or out of memory. */
3256 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3260 cur_line = file->lines;
3262 while ((cur_line != NULL) && (line_number < patternid))
3264 cur_line = cur_line->next;
3268 if ( (cur_line == NULL)
3269 || (cur_line->type != FILE_LINE_URL))
3271 /* Invalid "patternid" parameter */
3273 edit_free_file(file);
3274 return JB_ERR_CGI_PARAMS;
3277 /* At this point, the line to edit is in cur_line */
3279 freez(cur_line->raw);
3280 freez(cur_line->unprocessed);
3281 cur_line->unprocessed = new_pattern;
3283 err = edit_write_file(file);
3286 /* Error writing file */
3287 edit_free_file(file);
3291 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3292 string_append(&target, file->identifier);
3294 edit_free_file(file);
3299 return JB_ERR_MEMORY;
3302 rsp->status = strdup("302 Local Redirect from Privoxy");
3303 if (rsp->status == NULL)
3306 return JB_ERR_MEMORY;
3308 err = enlist_unique_header(rsp->headers, "Location", target);
3315 /*********************************************************************
3317 * Function : cgi_edit_actions_add_url
3319 * Description : CGI function that actually adds a URL pattern to
3323 * 1 : csp = Current client state (buffers, headers, etc...)
3324 * 2 : rsp = http_response data structure for output
3325 * 3 : parameters = map of cgi parameters
3328 * filename : Identifies the file to edit
3329 * ver : File's last-modified time
3330 * section : Line number of section to edit
3331 * newval : New pattern
3333 * Returns : JB_ERR_OK on success
3334 * JB_ERR_MEMORY on out-of-memory
3335 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3336 * specified or not valid.
3338 *********************************************************************/
3339 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3340 struct http_response *rsp,
3341 const struct map *parameters)
3345 struct file_line * new_line;
3346 struct editable_file * file;
3347 struct file_line * cur_line;
3348 unsigned line_number;
3352 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3354 return cgi_error_disabled(csp, rsp);
3357 err = get_number_param(csp, parameters, "s", §ionid);
3364 return JB_ERR_CGI_PARAMS;
3367 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3373 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3376 /* No filename specified, can't read file, modified, or out of memory. */
3378 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3382 cur_line = file->lines;
3384 while ((cur_line != NULL) && (line_number < sectionid))
3386 cur_line = cur_line->next;
3390 if ( (cur_line == NULL)
3391 || (cur_line->type != FILE_LINE_ACTION))
3393 /* Invalid "sectionid" parameter */
3395 edit_free_file(file);
3396 return JB_ERR_CGI_PARAMS;
3399 /* At this point, the section header is in cur_line - add after this. */
3401 /* Allocate the new line */
3402 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3403 if (new_line == NULL)
3406 edit_free_file(file);
3407 return JB_ERR_MEMORY;
3410 /* Fill in the data members of the new line */
3411 new_line->raw = NULL;
3412 new_line->prefix = NULL;
3413 new_line->unprocessed = new_pattern;
3414 new_line->type = FILE_LINE_URL;
3416 /* Link new_line into the list, after cur_line */
3417 new_line->next = cur_line->next;
3418 cur_line->next = new_line;
3420 /* Done making changes, now commit */
3422 err = edit_write_file(file);
3425 /* Error writing file */
3426 edit_free_file(file);
3430 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3431 string_append(&target, file->identifier);
3433 edit_free_file(file);
3438 return JB_ERR_MEMORY;
3441 rsp->status = strdup("302 Local Redirect from Privoxy");
3442 if (rsp->status == NULL)
3445 return JB_ERR_MEMORY;
3447 err = enlist_unique_header(rsp->headers, "Location", target);
3454 /*********************************************************************
3456 * Function : cgi_edit_actions_remove_url
3458 * Description : CGI function that actually removes a URL pattern from
3462 * 1 : csp = Current client state (buffers, headers, etc...)
3463 * 2 : rsp = http_response data structure for output
3464 * 3 : parameters = map of cgi parameters
3467 * f : (filename) Identifies the file to edit
3468 * v : (version) File's last-modified time
3469 * p : (pattern) Line number of pattern to remove
3471 * Returns : JB_ERR_OK on success
3472 * JB_ERR_MEMORY on out-of-memory
3473 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3474 * specified or not valid.
3476 *********************************************************************/
3477 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3478 struct http_response *rsp,
3479 const struct map *parameters)
3482 struct editable_file * file;
3483 struct file_line * cur_line;
3484 struct file_line * prev_line;
3485 unsigned line_number;
3489 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3491 return cgi_error_disabled(csp, rsp);
3494 err = get_number_param(csp, parameters, "p", &patternid);
3500 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3503 /* No filename specified, can't read file, modified, or out of memory. */
3504 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3509 cur_line = file->lines;
3511 while ((cur_line != NULL) && (line_number < patternid))
3513 prev_line = cur_line;
3514 cur_line = cur_line->next;
3518 if ( (cur_line == NULL)
3519 || (prev_line == NULL)
3520 || (cur_line->type != FILE_LINE_URL))
3522 /* Invalid "patternid" parameter */
3523 edit_free_file(file);
3524 return JB_ERR_CGI_PARAMS;
3527 /* At this point, the line to remove is in cur_line, and the previous
3528 * one is in prev_line
3531 /* Unlink cur_line */
3532 prev_line->next = cur_line->next;
3533 cur_line->next = NULL;
3536 edit_free_file_lines(cur_line);
3538 err = edit_write_file(file);
3541 /* Error writing file */
3542 edit_free_file(file);
3546 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3547 string_append(&target, file->identifier);
3549 edit_free_file(file);
3554 return JB_ERR_MEMORY;
3557 rsp->status = strdup("302 Local Redirect from Privoxy");
3558 if (rsp->status == NULL)
3561 return JB_ERR_MEMORY;
3563 err = enlist_unique_header(rsp->headers, "Location", target);
3570 /*********************************************************************
3572 * Function : cgi_edit_actions_section_remove
3574 * Description : CGI function that actually removes a whole section from
3575 * the actions file. The section must be empty first
3576 * (else JB_ERR_CGI_PARAMS).
3579 * 1 : csp = Current client state (buffers, headers, etc...)
3580 * 2 : rsp = http_response data structure for output
3581 * 3 : parameters = map of cgi parameters
3584 * f : (filename) Identifies the file to edit
3585 * v : (version) File's last-modified time
3586 * s : (section) Line number of section to edit
3588 * Returns : JB_ERR_OK on success
3589 * JB_ERR_MEMORY on out-of-memory
3590 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3591 * specified or not valid.
3593 *********************************************************************/
3594 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3595 struct http_response *rsp,
3596 const struct map *parameters)
3599 struct editable_file * file;
3600 struct file_line * cur_line;
3601 struct file_line * prev_line;
3602 unsigned line_number;
3606 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3608 return cgi_error_disabled(csp, rsp);
3611 err = get_number_param(csp, parameters, "s", §ionid);
3617 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3620 /* No filename specified, can't read file, modified, or out of memory. */
3621 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3625 cur_line = file->lines;
3628 while ((cur_line != NULL) && (line_number < sectionid))
3630 prev_line = cur_line;
3631 cur_line = cur_line->next;
3635 if ( (cur_line == NULL)
3636 || (cur_line->type != FILE_LINE_ACTION) )
3638 /* Invalid "sectionid" parameter */
3639 edit_free_file(file);
3640 return JB_ERR_CGI_PARAMS;
3643 if ( (cur_line->next != NULL)
3644 && (cur_line->next->type == FILE_LINE_URL) )
3646 /* Section not empty. */
3647 edit_free_file(file);
3648 return JB_ERR_CGI_PARAMS;
3651 /* At this point, the line to remove is in cur_line, and the previous
3652 * one is in prev_line
3655 /* Unlink cur_line */
3656 if (prev_line == NULL)
3658 /* Removing the first line from the file */
3659 file->lines = cur_line->next;
3663 prev_line->next = cur_line->next;
3665 cur_line->next = NULL;
3668 edit_free_file_lines(cur_line);
3670 err = edit_write_file(file);
3673 /* Error writing file */
3674 edit_free_file(file);
3678 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3679 string_append(&target, file->identifier);
3681 edit_free_file(file);
3686 return JB_ERR_MEMORY;
3689 rsp->status = strdup("302 Local Redirect from Privoxy");
3690 if (rsp->status == NULL)
3693 return JB_ERR_MEMORY;
3695 err = enlist_unique_header(rsp->headers, "Location", target);
3702 /*********************************************************************
3704 * Function : cgi_edit_actions_section_add
3706 * Description : CGI function that adds a new empty section to
3710 * 1 : csp = Current client state (buffers, headers, etc...)
3711 * 2 : rsp = http_response data structure for output
3712 * 3 : parameters = map of cgi parameters
3715 * f : (filename) Identifies the file to edit
3716 * v : (version) File's last-modified time
3717 * s : (section) Line number of section to add after, 0 for
3720 * Returns : JB_ERR_OK on success
3721 * JB_ERR_MEMORY on out-of-memory
3722 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3723 * specified or not valid.
3725 *********************************************************************/
3726 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3727 struct http_response *rsp,
3728 const struct map *parameters)
3731 struct file_line * new_line;
3733 struct editable_file * file;
3734 struct file_line * cur_line;
3735 unsigned line_number;
3739 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3741 return cgi_error_disabled(csp, rsp);
3744 err = get_number_param(csp, parameters, "s", §ionid);
3750 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3753 /* No filename specified, can't read file, modified, or out of memory. */
3754 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3758 cur_line = file->lines;
3762 /* Add to start of file */
3763 if (cur_line != NULL)
3765 /* There's something in the file, find the line before the first
3768 while ( (cur_line->next != NULL)
3769 && (cur_line->next->type != FILE_LINE_ACTION) )
3771 cur_line = cur_line->next;
3778 /* Add after stated section. */
3779 while ((cur_line != NULL) && (line_number < sectionid))
3781 cur_line = cur_line->next;
3785 if ( (cur_line == NULL)
3786 || (cur_line->type != FILE_LINE_ACTION))
3788 /* Invalid "sectionid" parameter */
3789 edit_free_file(file);
3790 return JB_ERR_CGI_PARAMS;
3793 /* Skip through the section to find the last line in it. */
3794 while ( (cur_line->next != NULL)
3795 && (cur_line->next->type != FILE_LINE_ACTION) )
3797 cur_line = cur_line->next;
3802 /* At this point, the last line in the previous section is in cur_line
3803 * - add after this. (Or if we need to add as the first line, cur_line
3807 new_text = strdup("{}");
3808 if (NULL == new_text)
3810 edit_free_file(file);
3811 return JB_ERR_MEMORY;
3814 /* Allocate the new line */
3815 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3816 if (new_line == NULL)
3819 edit_free_file(file);
3820 return JB_ERR_MEMORY;
3823 /* Fill in the data members of the new line */
3824 new_line->raw = NULL;
3825 new_line->prefix = NULL;
3826 new_line->unprocessed = new_text;
3827 new_line->type = FILE_LINE_ACTION;
3829 if (cur_line != NULL)
3831 /* Link new_line into the list, after cur_line */
3832 new_line->next = cur_line->next;
3833 cur_line->next = new_line;
3837 /* Link new_line into the list, as first line */
3838 new_line->next = file->lines;
3839 file->lines = new_line;
3842 /* Done making changes, now commit */
3844 err = edit_write_file(file);
3847 /* Error writing file */
3848 edit_free_file(file);
3852 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3853 string_append(&target, file->identifier);
3855 edit_free_file(file);
3860 return JB_ERR_MEMORY;
3863 rsp->status = strdup("302 Local Redirect from Privoxy");
3864 if (rsp->status == NULL)
3867 return JB_ERR_MEMORY;
3869 err = enlist_unique_header(rsp->headers, "Location", target);
3876 /*********************************************************************
3878 * Function : cgi_edit_actions_section_swap
3880 * Description : CGI function that swaps the order of two sections
3881 * in the actions file. Note that this CGI can actually
3882 * swap any two arbitrary sections, but the GUI interface
3883 * currently only allows consecutive sections to be
3887 * 1 : csp = Current client state (buffers, headers, etc...)
3888 * 2 : rsp = http_response data structure for output
3889 * 3 : parameters = map of cgi parameters
3892 * f : (filename) Identifies the file to edit
3893 * v : (version) File's last-modified time
3894 * s1 : (section1) Line number of first section to swap
3895 * s2 : (section2) Line number of second section to swap
3897 * Returns : JB_ERR_OK on success
3898 * JB_ERR_MEMORY on out-of-memory
3899 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3900 * specified or not valid.
3902 *********************************************************************/
3903 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3904 struct http_response *rsp,
3905 const struct map *parameters)
3909 struct editable_file * file;
3910 struct file_line * cur_line;
3911 struct file_line * prev_line;
3912 struct file_line * line_before_section1;
3913 struct file_line * line_start_section1;
3914 struct file_line * line_end_section1;
3915 struct file_line * line_after_section1;
3916 struct file_line * line_before_section2;
3917 struct file_line * line_start_section2;
3918 struct file_line * line_end_section2;
3919 struct file_line * line_after_section2;
3920 unsigned line_number;
3924 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3926 return cgi_error_disabled(csp, rsp);
3929 err = get_number_param(csp, parameters, "s1", §ion1);
3930 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3936 if (section1 > section2)
3938 unsigned temp = section2;
3939 section2 = section1;
3943 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3946 /* No filename specified, can't read file, modified, or out of memory. */
3947 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3950 /* Start at the beginning... */
3952 cur_line = file->lines;
3955 /* ... find section1 ... */
3956 while ((cur_line != NULL) && (line_number < section1))
3958 prev_line = cur_line;
3959 cur_line = cur_line->next;
3963 if ( (cur_line == NULL)
3964 || (cur_line->type != FILE_LINE_ACTION) )
3966 /* Invalid "section1" parameter */
3967 edit_free_file(file);
3968 return JB_ERR_CGI_PARAMS;
3971 /* If no-op, we've validated params and can skip the rest. */
3972 if (section1 != section2)
3974 /* ... find the end of section1 ... */
3975 line_before_section1 = prev_line;
3976 line_start_section1 = cur_line;
3979 prev_line = cur_line;
3980 cur_line = cur_line->next;
3983 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3984 line_end_section1 = prev_line;
3985 line_after_section1 = cur_line;
3987 /* ... find section2 ... */
3988 while ((cur_line != NULL) && (line_number < section2))
3990 prev_line = cur_line;
3991 cur_line = cur_line->next;
3995 if ( (cur_line == NULL)
3996 || (cur_line->type != FILE_LINE_ACTION) )
3998 /* Invalid "section2" parameter */
3999 edit_free_file(file);
4000 return JB_ERR_CGI_PARAMS;
4003 /* ... find the end of section2 ... */
4004 line_before_section2 = prev_line;
4005 line_start_section2 = cur_line;
4008 prev_line = cur_line;
4009 cur_line = cur_line->next;
4012 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4013 line_end_section2 = prev_line;
4014 line_after_section2 = cur_line;
4016 /* Now have all the pointers we need. Do the swap. */
4018 /* Change the pointer to section1 to point to section2 instead */
4019 if (line_before_section1 == NULL)
4021 file->lines = line_start_section2;
4025 line_before_section1->next = line_start_section2;
4028 if (line_before_section2 == line_end_section1)
4030 /* Consecutive sections */
4031 line_end_section2->next = line_start_section1;
4035 line_end_section2->next = line_after_section1;
4036 line_before_section2->next = line_start_section1;
4039 /* Set the pointer from the end of section1 to the rest of the file */
4040 line_end_section1->next = line_after_section2;
4042 err = edit_write_file(file);
4045 /* Error writing file */
4046 edit_free_file(file);
4049 } /* END if (section1 != section2) */
4051 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4052 string_append(&target, file->identifier);
4054 edit_free_file(file);
4059 return JB_ERR_MEMORY;
4062 rsp->status = strdup("302 Local Redirect from Privoxy");
4063 if (rsp->status == NULL)
4066 return JB_ERR_MEMORY;
4068 err = enlist_unique_header(rsp->headers, "Location", target);
4075 /*********************************************************************
4077 * Function : cgi_toggle
4079 * Description : CGI function that adds a new empty section to
4083 * 1 : csp = Current client state (buffers, headers, etc...)
4084 * 2 : rsp = http_response data structure for output
4085 * 3 : parameters = map of cgi parameters
4088 * set : If present, how to change toggle setting:
4089 * "enable", "disable", "toggle", or none (default).
4090 * mini : If present, use mini reply template.
4092 * Returns : JB_ERR_OK on success
4093 * JB_ERR_MEMORY on out-of-memory
4095 *********************************************************************/
4096 jb_err cgi_toggle(struct client_state *csp,
4097 struct http_response *rsp,
4098 const struct map *parameters)
4100 struct map *exports;
4102 const char *template_name;
4108 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4110 return cgi_error_disabled(csp, rsp);
4113 mode = get_char_param(parameters, "set");
4120 else if (mode == 'D')
4125 else if (mode == 'T')
4128 g_bToggleIJB = !g_bToggleIJB;
4131 if (NULL == (exports = default_exports(csp, "toggle")))
4133 return JB_ERR_MEMORY;
4136 template_name = (get_char_param(parameters, "mini")
4140 return template_fill_for_cgi(csp, template_name, exports, rsp);
4144 /*********************************************************************
4146 * Function : javascriptify
4148 * Description : Converts a string into a form JavaScript will like.
4150 * Netscape 4's JavaScript sucks - it doesn't use
4151 * "id" parameters, so you have to set the "name"
4152 * used to submit a form element to something JavaScript
4153 * will like. (Or access the elements by index in an
4154 * array. That array contains >60 elements and will
4155 * be changed whenever we add a new action to the
4156 * editor, so I'm NOT going to use indexes that have
4157 * to be figured out by hand.)
4159 * Currently the only thing we have to worry about
4160 * is "-" ==> "_" conversion.
4162 * This is a length-preserving operation so it is
4163 * carried out in-place, no memory is allocated
4167 * 1 : identifier = String to make JavaScript-friendly.
4171 *********************************************************************/
4172 static void javascriptify(char * identifier)
4174 char * p = identifier;
4175 while (NULL != (p = strchr(p, '-')))
4182 /*********************************************************************
4184 * Function : actions_to_radio
4186 * Description : Converts a actionsfile entry into settings for
4187 * radio buttons and edit boxes on a HTML form.
4190 * 1 : exports = List of substitutions to add to.
4191 * 2 : action = Action to read
4193 * Returns : JB_ERR_OK on success
4194 * JB_ERR_MEMORY on out-of-memory
4196 *********************************************************************/
4197 static jb_err actions_to_radio(struct map * exports,
4198 const struct action_spec *action)
4200 unsigned mask = action->mask;
4201 unsigned add = action->add;
4209 mask = action->mask;
4212 /* sanity - prevents "-feature +feature" */
4216 #define DEFINE_ACTION_BOOL(name, bit) \
4217 if (!(mask & bit)) \
4219 current_mode = 'n'; \
4221 else if (add & bit) \
4223 current_mode = 'y'; \
4227 current_mode = 'x'; \
4229 if (map_radio(exports, name, "ynx", current_mode)) \
4231 return JB_ERR_MEMORY; \
4234 #define DEFINE_ACTION_STRING(name, bit, index) \
4235 DEFINE_ACTION_BOOL(name, bit); \
4238 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4241 checked = !strcmp(action->string[index], value); \
4245 checked = is_default; \
4247 mapped_param |= checked; \
4248 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4250 return JB_ERR_MEMORY; \
4253 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4254 if (map(exports, name "-param-custom", 1, \
4255 ((!mapped_param) ? "checked" : ""), 1)) \
4257 return JB_ERR_MEMORY; \
4259 if (map(exports, name "-param", 1, \
4260 (((add & bit) && !mapped_param) ? \
4261 action->string[index] : default_val), 1)) \
4263 return JB_ERR_MEMORY; \
4266 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4267 if (map(exports, name "-param", 1, \
4268 ((add & bit) ? action->string[index] : default_val), 1)) \
4270 return JB_ERR_MEMORY; \
4273 #define DEFINE_ACTION_MULTI(name, index) \
4274 if (action->multi_add[index]->first) \
4276 current_mode = 'y'; \
4278 else if (action->multi_remove_all[index]) \
4280 current_mode = 'n'; \
4282 else if (action->multi_remove[index]->first) \
4284 current_mode = 'y'; \
4288 current_mode = 'x'; \
4290 if (map_radio(exports, name, "ynx", current_mode)) \
4292 return JB_ERR_MEMORY; \
4295 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4297 #include "actionlist.h"
4299 #undef DEFINE_ACTION_MULTI
4300 #undef DEFINE_ACTION_STRING
4301 #undef DEFINE_ACTION_BOOL
4302 #undef DEFINE_ACTION_ALIAS
4303 #undef DEFINE_CGI_PARAM_CUSTOM
4304 #undef DEFINE_CGI_PARAM_RADIO
4305 #undef DEFINE_CGI_PARAM_NO_RADIO
4311 /*********************************************************************
4313 * Function : actions_from_radio
4315 * Description : Converts a map of parameters passed to a CGI function
4316 * into an actionsfile entry.
4319 * 1 : parameters = parameters to the CGI call
4320 * 2 : action = Action to change. Must be valid before
4321 * the call, actions not specified will be
4324 * Returns : JB_ERR_OK on success
4325 * JB_ERR_MEMORY on out-of-memory
4327 *********************************************************************/
4328 static jb_err actions_from_radio(const struct map * parameters,
4329 struct action_spec *action)
4331 static int first_time = 1;
4335 const char * js_name;
4336 jb_err err = JB_ERR_OK;
4341 /* Statics are generally a potential race condition,
4342 * but in this case we're safe and don't need semaphores.
4343 * Be careful if you modify this function.
4347 #define JAVASCRIPTIFY(dest_var, string) \
4349 static char js_name_arr[] = string; \
4352 javascriptify(js_name_arr); \
4354 dest_var = js_name_arr; \
4357 #define DEFINE_ACTION_BOOL(name, bit) \
4358 JAVASCRIPTIFY(js_name, name); \
4359 ch = get_char_param(parameters, js_name); \
4362 action->add |= bit; \
4363 action->mask |= bit; \
4365 else if (ch == 'N') \
4367 action->add &= ~bit; \
4368 action->mask &= ~bit; \
4370 else if (ch == 'X') \
4372 action->add &= ~bit; \
4373 action->mask |= bit; \
4376 #define DEFINE_ACTION_STRING(name, bit, index) \
4377 JAVASCRIPTIFY(js_name, name); \
4378 ch = get_char_param(parameters, js_name); \
4382 JAVASCRIPTIFY(js_name, name "-mode"); \
4383 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4384 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4386 JAVASCRIPTIFY(js_name, name "-param"); \
4387 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4389 if (param != NULL) \
4391 if (NULL == (param_dup = strdup(param))) \
4393 return JB_ERR_MEMORY; \
4395 freez(action->string[index]); \
4396 action->add |= bit; \
4397 action->mask |= bit; \
4398 action->string[index] = param_dup; \
4401 else if (ch == 'N') \
4403 if (action->add & bit) \
4405 freez(action->string[index]); \
4407 action->add &= ~bit; \
4408 action->mask &= ~bit; \
4410 else if (ch == 'X') \
4412 if (action->add & bit) \
4414 freez(action->string[index]); \
4416 action->add &= ~bit; \
4417 action->mask |= bit; \
4420 #define DEFINE_ACTION_MULTI(name, index) \
4421 JAVASCRIPTIFY(js_name, name); \
4422 ch = get_char_param(parameters, js_name); \
4427 else if (ch == 'N') \
4429 list_remove_all(action->multi_add[index]); \
4430 list_remove_all(action->multi_remove[index]); \
4431 action->multi_remove_all[index] = 1; \
4433 else if (ch == 'X') \
4435 list_remove_all(action->multi_add[index]); \
4436 list_remove_all(action->multi_remove[index]); \
4437 action->multi_remove_all[index] = 0; \
4440 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4442 #include "actionlist.h"
4444 #undef DEFINE_ACTION_MULTI
4445 #undef DEFINE_ACTION_STRING
4446 #undef DEFINE_ACTION_BOOL
4447 #undef DEFINE_ACTION_ALIAS
4448 #undef JAVASCRIPTIFY
4456 #endif /* def FEATURE_CGI_EDIT_ACTIONS */