1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.37 2002/04/30 11:14:52 oes 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.37 2002/04/30 11:14:52 oes
46 * Made csp the first parameter in *action_to_html
48 * Revision 1.36 2002/04/26 21:53:30 jongfoster
49 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
51 * Revision 1.35 2002/04/26 21:50:02 jongfoster
52 * Honouring default exports in edit-actions-for-url-filter template.
54 * Revision 1.34 2002/04/26 12:54:17 oes
55 * Adaptions to changes in actions.c
57 * Revision 1.33 2002/04/24 02:17:47 oes
58 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
60 * - Activated Jon's code for editing multiple AFs
61 * - cgi_edit_list_actions now provides context-sensitive
62 * help, looks up all action sets from standard.action and
63 * makes buttons for them in the catchall section
64 * - cgi_edit_action_submit now honors a p parameter, looks up
65 * the corresponding action set, and sets the catchall pattern's
66 * actions accordingly.
68 * Revision 1.32 2002/04/19 16:55:31 jongfoster
69 * Fixing newline problems. If we do our own text file newline
70 * mangling, we don't want the library to do any, so we need to
71 * open the files in *binary* mode.
73 * Revision 1.31 2002/04/18 19:21:08 jongfoster
74 * Added code to detect "conventional" action files, that start
75 * with a set of actions for all URLs (the pattern "/").
76 * These are special-cased in the "edit-actions-list" CGI, so
77 * that a special UI can be written for them.
79 * Revision 1.30 2002/04/10 13:38:35 oes
80 * load_template signature changed
82 * Revision 1.29 2002/04/08 16:59:08 oes
85 * Revision 1.28 2002/03/27 12:30:29 oes
86 * Deleted unsused variable
88 * Revision 1.27 2002/03/26 23:06:04 jongfoster
89 * Removing duplicate @ifs on the toggle page
91 * Revision 1.26 2002/03/26 22:59:17 jongfoster
92 * Fixing /toggle to display status consistently.
94 * Revision 1.25 2002/03/26 22:29:54 swa
95 * we have a new homepage!
97 * Revision 1.24 2002/03/24 15:23:33 jongfoster
100 * Revision 1.23 2002/03/24 13:32:41 swa
101 * name change related issues
103 * Revision 1.22 2002/03/24 13:25:43 swa
104 * name change related issues
106 * Revision 1.21 2002/03/22 18:02:48 jongfoster
107 * Fixing remote toggle
109 * Revision 1.20 2002/03/16 20:28:34 oes
110 * Added descriptions to the filters so users will know what they select in the cgi editor
112 * Revision 1.19 2002/03/16 18:38:14 jongfoster
113 * Stopping stupid or malicious users from breaking the actions
114 * file using the web-based editor.
116 * Revision 1.18 2002/03/16 14:57:44 jongfoster
117 * Full support for enabling/disabling modular filters.
119 * Revision 1.17 2002/03/16 14:26:42 jongfoster
120 * First version of modular filters support - READ ONLY!
121 * Fixing a double-free bug in the out-of-memory handling in map_radio().
123 * Revision 1.16 2002/03/07 03:46:17 oes
124 * Fixed compiler warnings
126 * Revision 1.15 2002/03/06 22:54:35 jongfoster
127 * Automated function-comment nitpicking.
129 * Revision 1.14 2002/03/05 00:24:51 jongfoster
130 * Patch to always edit the current actions file.
132 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
133 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
135 * Revision 1.12 2002/03/03 09:18:03 joergs
136 * Made jumbjuster work on AmigaOS again.
138 * Revision 1.11 2002/01/23 01:03:31 jongfoster
139 * Fixing gcc [CygWin] compiler warnings
141 * Revision 1.10 2002/01/23 00:22:59 jongfoster
142 * Adding new function cgi_edit_actions_section_swap(), to reorder
145 * Adding get_url_spec_param() to get a validated URL pattern.
147 * Moving edit_read_line() out of this file and into loaders.c.
149 * Adding missing html_encode() to many CGI functions.
151 * Moving the functions that #include actionlist.h to the end of the file,
152 * because the Visual C++ 97 debugger gets extremely confused if you try
153 * to debug any code that comes after them in the file.
155 * Major optimizations in cgi_edit_actions_list() to reduce the size of
156 * the generated HTML (down 40% from 550k to 304k), with major side-effects
157 * throughout the editor and templates. In particular, the length of the
158 * URLs throughout the editor has been drastically reduced, by cutting
159 * paramater names down to 1 character and CGI names down to 3-4
160 * characters, by removing all non-essential CGI paramaters even at the
161 * expense of having to re-read the actions file for the most trivial
162 * page, and by using relative rather than absolute URLs. This means
163 * that this (typical example):
165 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
166 * filename=ijb&ver=1011487572&section=12&pattern=13
167 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
171 * <a href="eau?f=ijb&v=1011487572&p=13">
173 * Revision 1.9 2002/01/17 20:56:22 jongfoster
174 * Replacing hard references to the URL of the config interface
175 * with #defines from project.h
177 * Revision 1.8 2001/11/30 23:35:51 jongfoster
178 * Renaming actionsfile to ijb.action
180 * Revision 1.7 2001/11/13 00:28:24 jongfoster
181 * - Renaming parameters from edit-actions-for-url so that they only
182 * contain legal JavaScript characters. If we wanted to write
183 * JavaScript that worked with Netscape 4, this is nessacery.
184 * (Note that at the moment the JavaScript doesn't actually work
185 * with Netscape 4, but now this is purely a template issue, not
186 * one affecting code).
187 * - Adding new CGIs for use by non-JavaScript browsers:
188 * edit-actions-url-form
189 * edit-actions-add-url-form
190 * edit-actions-remove-url-form
193 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
194 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
195 * by and __OS2__ ifdef.
197 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
198 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
199 * threads to call select() simultaneously. So, it's time to do a real, live,
200 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
201 * (native). Both versions will work, but using __OS2__ offers multi-threading.
203 * Revision 1.4 2001/10/23 21:48:19 jongfoster
204 * Cleaning up error handling in CGI functions - they now send back
205 * a HTML error page and should never cause a FATAL error. (Fixes one
206 * potential source of "denial of service" attacks).
208 * CGI actions file editor that works and is actually useful.
210 * Ability to toggle JunkBuster remotely using a CGI call.
212 * You can turn off both the above features in the main configuration
213 * file, e.g. if you are running a multi-user proxy.
215 * Revision 1.3 2001/10/14 22:12:49 jongfoster
216 * New version of CGI-based actionsfile editor.
217 * Major changes, including:
218 * - Completely new file parser and file output routines
219 * - edit-actions CGI renamed edit-actions-for-url
220 * - All CGIs now need a filename parameter, except for...
221 * - New CGI edit-actions which doesn't need a filename,
222 * to allow you to start the editor up.
223 * - edit-actions-submit now works, and now automatically
224 * redirects you back to the main edit-actions-list handler.
226 * Revision 1.2 2001/09/16 17:05:14 jongfoster
227 * Removing unused #include showarg.h
229 * Revision 1.1 2001/09/16 15:47:37 jongfoster
230 * First version of CGI-based edit interface. This is very much a
231 * work-in-progress, and you can't actually use it to edit anything
232 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
233 * to have any effect.
236 **********************************************************************/
242 * FIXME: Following includes copied from cgi.c - which are actually needed?
247 #include <sys/types.h>
251 #include <sys/stat.h>
254 #define snprintf _snprintf
255 #endif /* def _WIN32 */
260 #include "cgisimple.h"
264 #include "miscutil.h"
268 /* loadcfg.h is for g_bToggleIJB only */
269 #include "urlmatch.h"
271 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
274 #ifdef FEATURE_CGI_EDIT_ACTIONS
278 struct file_line * next;
286 struct action_spec action[1];
295 /* Add more data types here... e.g.
298 struct url_spec url[1];
302 struct action_spec action[1];
311 #define FILE_LINE_UNPROCESSED 1
312 #define FILE_LINE_BLANK 2
313 #define FILE_LINE_ALIAS_HEADER 3
314 #define FILE_LINE_ALIAS_ENTRY 4
315 #define FILE_LINE_ACTION 5
316 #define FILE_LINE_URL 6
317 #define FILE_LINE_SETTINGS_HEADER 7
318 #define FILE_LINE_SETTINGS_ENTRY 8
319 #define FILE_LINE_DESCRIPTION_HEADER 9
320 #define FILE_LINE_DESCRIPTION_ENTRY 10
325 struct file_line * lines;
326 const char * filename; /* Full pathname - e.g. "/etc/privoxy/wibble.action" */
327 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
328 /* Pre-encoded with url_encode() for ease of use. */
329 const char * version_str; /* Last modification time, as a string. For CGI param */
330 /* Can be used in URL without using url_param(). */
331 unsigned version; /* Last modification time - prevents chaos with
332 * the browser's "back" button. Note that this is a
333 * time_t cast to an unsigned. When comparing, always
334 * cast the time_t to an unsigned, and *NOT* vice-versa.
335 * This may lose the top few bits, but they're not
336 * significant anyway.
338 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
339 * Note that changing this after the file has been
340 * read in will cause a mess.
342 struct file_line * parse_error; /* On parse error, this is the offending line. */
343 const char * parse_error_text; /* On parse error, this is the problem.
344 * (Statically allocated) */
347 /* FIXME: Following non-static functions should be prototyped in .h or made static */
349 /* Functions to read and write arbitrary config files */
350 jb_err edit_read_file(struct client_state *csp,
351 const struct map *parameters,
354 struct editable_file **pfile);
355 jb_err edit_write_file(struct editable_file * file);
356 void edit_free_file(struct editable_file * file);
358 /* Functions to read and write actions files */
359 jb_err edit_parse_actions_file(struct editable_file * file);
360 jb_err edit_read_actions_file(struct client_state *csp,
361 struct http_response *rsp,
362 const struct map *parameters,
364 struct editable_file **pfile);
367 jb_err cgi_error_modified(struct client_state *csp,
368 struct http_response *rsp,
369 const char *filename);
370 jb_err cgi_error_parse(struct client_state *csp,
371 struct http_response *rsp,
372 struct editable_file *file);
373 jb_err cgi_error_file(struct client_state *csp,
374 struct http_response *rsp,
375 const char *filename);
376 jb_err cgi_error_disabled(struct client_state *csp,
377 struct http_response *rsp);
379 /* Internal arbitrary config file support functions */
380 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
381 static void edit_free_file_lines(struct file_line * first_line);
383 /* Internal actions file support functions */
384 static int match_actions_file_header_line(const char * line, const char * name);
385 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
387 /* Internal parameter parsing functions */
388 static jb_err get_file_name_param(struct client_state *csp,
389 const struct map *parameters,
390 const char *param_name,
393 const char **pparam);
395 static jb_err get_url_spec_param(struct client_state *csp,
396 const struct map *parameters,
401 /* Internal actionsfile <==> HTML conversion functions */
402 static jb_err map_radio(struct map * exports,
403 const char * optionname,
406 static jb_err actions_to_radio(struct map * exports,
407 const struct action_spec *action);
408 static jb_err actions_from_radio(const struct map * parameters,
409 struct action_spec *action);
412 static jb_err map_copy_parameter_html(struct map *out,
413 const struct map *in,
415 #if 0 /* unused function */
416 static jb_err map_copy_parameter_url(struct map *out,
417 const struct map *in,
419 #endif /* unused function */
421 /*********************************************************************
423 * Function : map_copy_parameter_html
425 * Description : Copy a CGI parameter from one map to another, HTML
429 * 1 : out = target map
430 * 2 : in = source map
431 * 3 : name = name of cgi parameter to copy
433 * Returns : JB_ERR_OK on success
434 * JB_ERR_MEMORY on out-of-memory
435 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
438 *********************************************************************/
439 static jb_err map_copy_parameter_html(struct map *out,
440 const struct map *in,
450 value = lookup(in, name);
451 err = map(out, name, 1, html_encode(value), 0);
458 else if (*value == '\0')
460 return JB_ERR_CGI_PARAMS;
469 #if 0 /* unused function */
470 /*********************************************************************
472 * Function : map_copy_parameter_url
474 * Description : Copy a CGI parameter from one map to another, URL
478 * 1 : out = target map
479 * 2 : in = source map
480 * 3 : name = name of cgi parameter to copy
482 * Returns : JB_ERR_OK on success
483 * JB_ERR_MEMORY on out-of-memory
484 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
487 *********************************************************************/
488 static jb_err map_copy_parameter_url(struct map *out,
489 const struct map *in,
499 value = lookup(in, name);
500 err = map(out, name, 1, url_encode(value), 0);
507 else if (*value == '\0')
509 return JB_ERR_CGI_PARAMS;
516 #endif /* 0 - unused function */
518 /*********************************************************************
520 * Function : cgi_edit_actions_url_form
522 * Description : CGI function that displays a form for
526 * 1 : csp = Current client state (buffers, headers, etc...)
527 * 2 : rsp = http_response data structure for output
528 * 3 : parameters = map of cgi parameters
531 * f : (filename) Identifies the file to edit
532 * v : (version) File's last-modified time
533 * p : (pattern) Line number of pattern to edit
535 * Returns : JB_ERR_OK on success
536 * JB_ERR_MEMORY on out-of-memory
537 * JB_ERR_CGI_PARAMS if the CGI parameters are not
538 * specified or not valid.
540 *********************************************************************/
541 jb_err cgi_edit_actions_url_form(struct client_state *csp,
542 struct http_response *rsp,
543 const struct map *parameters)
545 struct map * exports;
547 struct editable_file * file;
548 struct file_line * cur_line;
549 unsigned line_number;
556 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
558 return cgi_error_disabled(csp, rsp);
561 err = get_number_param(csp, parameters, "p", &patternid);
567 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
570 /* No filename specified, can't read file, modified, or out of memory. */
571 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
574 cur_line = file->lines;
576 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
578 cur_line = cur_line->next;
581 if ( (cur_line == NULL)
582 || (line_number != patternid)
584 || (cur_line->type != FILE_LINE_URL))
586 /* Invalid "patternid" parameter */
587 edit_free_file(file);
588 return JB_ERR_CGI_PARAMS;
591 if (NULL == (exports = default_exports(csp, NULL)))
593 edit_free_file(file);
594 return JB_ERR_MEMORY;
597 err = map(exports, "f", 1, file->identifier, 1);
598 if (!err) err = map(exports, "v", 1, file->version_str, 1);
599 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
600 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
602 edit_free_file(file);
610 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
614 /*********************************************************************
616 * Function : cgi_edit_actions_add_url_form
618 * Description : CGI function that displays a form for
622 * 1 : csp = Current client state (buffers, headers, etc...)
623 * 2 : rsp = http_response data structure for output
624 * 3 : parameters = map of cgi parameters
627 * f : (filename) Identifies the file to edit
628 * v : (version) File's last-modified time
629 * s : (section) Line number of section to edit
631 * Returns : JB_ERR_OK on success
632 * JB_ERR_MEMORY on out-of-memory
633 * JB_ERR_CGI_PARAMS if the CGI parameters are not
634 * specified or not valid.
636 *********************************************************************/
637 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
638 struct http_response *rsp,
639 const struct map *parameters)
648 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
650 return cgi_error_disabled(csp, rsp);
653 if (NULL == (exports = default_exports(csp, NULL)))
655 return JB_ERR_MEMORY;
658 err = map_copy_parameter_html(exports, parameters, "f");
659 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
660 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
668 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
672 /*********************************************************************
674 * Function : cgi_edit_actions_remove_url_form
676 * Description : CGI function that displays a form for
680 * 1 : csp = Current client state (buffers, headers, etc...)
681 * 2 : rsp = http_response data structure for output
682 * 3 : parameters = map of cgi parameters
685 * f : (filename) Identifies the file to edit
686 * v : (version) File's last-modified time
687 * p : (pattern) Line number of pattern to edit
689 * Returns : JB_ERR_OK on success
690 * JB_ERR_MEMORY on out-of-memory
691 * JB_ERR_CGI_PARAMS if the CGI parameters are not
692 * specified or not valid.
694 *********************************************************************/
695 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
696 struct http_response *rsp,
697 const struct map *parameters)
699 struct map * exports;
701 struct editable_file * file;
702 struct file_line * cur_line;
703 unsigned line_number;
710 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
712 return cgi_error_disabled(csp, rsp);
715 err = get_number_param(csp, parameters, "p", &patternid);
721 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
724 /* No filename specified, can't read file, modified, or out of memory. */
725 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
728 cur_line = file->lines;
730 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
732 cur_line = cur_line->next;
735 if ( (cur_line == NULL)
736 || (line_number != patternid)
738 || (cur_line->type != FILE_LINE_URL))
740 /* Invalid "patternid" parameter */
741 edit_free_file(file);
742 return JB_ERR_CGI_PARAMS;
745 if (NULL == (exports = default_exports(csp, NULL)))
747 edit_free_file(file);
748 return JB_ERR_MEMORY;
751 err = map(exports, "f", 1, file->identifier, 1);
752 if (!err) err = map(exports, "v", 1, file->version_str, 1);
753 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
754 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
756 edit_free_file(file);
764 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
768 /*********************************************************************
770 * Function : edit_write_file
772 * Description : Write a complete file to disk.
775 * 1 : filename = File to write to.
776 * 2 : file = Data structure to write.
778 * Returns : JB_ERR_OK on success
779 * JB_ERR_FILE on error writing to file.
780 * JB_ERR_MEMORY on out of memory
782 *********************************************************************/
783 jb_err edit_write_file(struct editable_file * file)
786 struct file_line * cur_line;
787 struct stat statbuf[1];
788 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
789 digits in time_t, assuming this is a 64-bit
790 machine, plus null terminator, plus one
794 assert(file->filename);
796 if (NULL == (fp = fopen(file->filename, "wb")))
801 cur_line = file->lines;
802 while (cur_line != NULL)
806 if (fputs(cur_line->raw, fp) < 0)
814 if (cur_line->prefix)
816 if (fputs(cur_line->prefix, fp) < 0)
822 if (cur_line->unprocessed)
825 if (NULL != strchr(cur_line->unprocessed, '#'))
827 /* Must quote '#' characters */
834 /* Count number of # characters, so we know length of output string */
835 src = cur_line->unprocessed;
836 while (NULL != (src = strchr(src, '#')))
843 /* Allocate new memory for string */
844 len = strlen(cur_line->unprocessed);
845 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
847 /* Uh oh, just trashed file! */
849 return JB_ERR_MEMORY;
852 /* Loop through string from end */
853 src = cur_line->unprocessed + len;
854 dest = str + len + numhash;
855 for ( ; len >= 0; len--)
857 if ((*dest-- = *src--) == '#')
861 assert(numhash >= 0);
864 assert(numhash == 0);
865 assert(src + 1 == cur_line->unprocessed);
866 assert(dest + 1 == str);
868 if (fputs(str, fp) < 0)
879 /* Can write without quoting '#' characters. */
880 if (fputs(cur_line->unprocessed, fp) < 0)
886 if (fputs(NEWLINE(file->newline), fp) < 0)
894 /* FIXME: Write data from file->data->whatever */
898 cur_line = cur_line->next;
904 /* Update the version stamp in the file structure, since we just
905 * wrote to the file & changed it's date.
907 if (stat(file->filename, statbuf) < 0)
909 /* Error, probably file not found. */
912 file->version = (unsigned)statbuf->st_mtime;
914 /* Correct file->version_str */
915 freez(file->version_str);
916 snprintf(version_buf, 22, "%u", file->version);
917 version_buf[21] = '\0';
918 file->version_str = strdup(version_buf);
919 if (version_buf == NULL)
921 return JB_ERR_MEMORY;
928 /*********************************************************************
930 * Function : edit_free_file
932 * Description : Free a complete file in memory.
935 * 1 : file = Data structure to free.
939 *********************************************************************/
940 void edit_free_file(struct editable_file * file)
944 /* Silently ignore NULL pointer */
948 edit_free_file_lines(file->lines);
949 freez(file->filename);
950 freez(file->identifier);
951 freez(file->version_str);
953 file->parse_error_text = NULL; /* Statically allocated */
954 file->parse_error = NULL;
960 /*********************************************************************
962 * Function : edit_free_file_lines
964 * Description : Free an entire linked list of file lines.
967 * 1 : first_line = Data structure to free.
971 *********************************************************************/
972 static void edit_free_file_lines(struct file_line * first_line)
974 struct file_line * next_line;
976 while (first_line != NULL)
978 next_line = first_line->next;
979 first_line->next = NULL;
980 freez(first_line->raw);
981 freez(first_line->prefix);
982 freez(first_line->unprocessed);
983 switch(first_line->type)
985 case 0: /* special case if memory zeroed */
986 case FILE_LINE_UNPROCESSED:
987 case FILE_LINE_BLANK:
988 case FILE_LINE_ALIAS_HEADER:
989 case FILE_LINE_SETTINGS_HEADER:
990 case FILE_LINE_DESCRIPTION_HEADER:
991 case FILE_LINE_DESCRIPTION_ENTRY:
992 case FILE_LINE_ALIAS_ENTRY:
994 /* No data is stored for these */
997 case FILE_LINE_ACTION:
998 free_action(first_line->data.action);
1001 case FILE_LINE_SETTINGS_ENTRY:
1002 freez(first_line->data.setting.name);
1003 freez(first_line->data.setting.svalue);
1006 /* Should never happen */
1010 first_line->type = 0; /* paranoia */
1012 first_line = next_line;
1017 /*********************************************************************
1019 * Function : match_actions_file_header_line
1021 * Description : Match an actions file {{header}} line
1024 * 1 : line = String from file
1025 * 2 : name = Header to match against
1027 * Returns : 0 iff they match.
1029 *********************************************************************/
1030 static int match_actions_file_header_line(const char * line, const char * name)
1038 if ((line[0] != '{') || (line[1] != '{'))
1044 /* Look for optional whitespace */
1045 while ( (*line == ' ') || (*line == '\t') )
1050 /* Look for the specified name (case-insensitive) */
1052 if (0 != strncmpic(line, name, len))
1058 /* Look for optional whitespace */
1059 while ( (*line == ' ') || (*line == '\t') )
1064 /* Look for "}}" and end of string*/
1065 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1075 /*********************************************************************
1077 * Function : match_actions_file_header_line
1079 * Description : Match an actions file {{header}} line
1082 * 1 : line = String from file. Must not start with
1083 * whitespace (else infinite loop!)
1084 * 2 : name = Destination for name
1085 * 2 : name = Destination for value
1087 * Returns : JB_ERR_OK on success
1088 * JB_ERR_MEMORY on out-of-memory
1089 * JB_ERR_PARSE if there's no "=" sign, or if there's
1090 * nothing before the "=" sign (but empty
1091 * values *after* the "=" sign are legal).
1093 *********************************************************************/
1094 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1096 const char * name_end;
1097 const char * value_start;
1103 assert(*line != ' ');
1104 assert(*line != '\t');
1109 value_start = strchr(line, '=');
1110 if ((value_start == NULL) || (value_start == line))
1112 return JB_ERR_PARSE;
1115 name_end = value_start - 1;
1117 /* Eat any whitespace before the '=' */
1118 while ((*name_end == ' ') || (*name_end == '\t'))
1121 * we already know we must have at least 1 non-ws char
1122 * at start of buf - no need to check
1127 name_len = name_end - line + 1; /* Length excluding \0 */
1128 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1130 return JB_ERR_MEMORY;
1132 strncpy(*pname, line, name_len);
1133 (*pname)[name_len] = '\0';
1135 /* Eat any the whitespace after the '=' */
1137 while ((*value_start == ' ') || (*value_start == '\t'))
1142 if (NULL == (*pvalue = strdup(value_start)))
1146 return JB_ERR_MEMORY;
1153 /*********************************************************************
1155 * Function : edit_parse_actions_file
1157 * Description : Parse an actions file in memory.
1159 * Passed linked list must have the "data" member
1160 * zeroed, and must contain valid "next" and
1161 * "unprocessed" fields. The "raw" and "prefix"
1162 * fields are ignored, and "type" is just overwritten.
1164 * Note that on error the file may have been
1168 * 1 : file = Actions file to be parsed in-place.
1170 * Returns : JB_ERR_OK on success
1171 * JB_ERR_MEMORY on out-of-memory
1172 * JB_ERR_PARSE on error
1174 *********************************************************************/
1175 jb_err edit_parse_actions_file(struct editable_file * file)
1177 struct file_line * cur_line;
1179 const char * text; /* Text from a line */
1180 char * name; /* For lines of the form name=value */
1181 char * value; /* For lines of the form name=value */
1182 struct action_alias * alias_list = NULL;
1183 jb_err err = JB_ERR_OK;
1185 /* alias_list contains the aliases defined in this file.
1186 * It might be better to use the "file_line.data" fields
1187 * in the relavent places instead.
1190 cur_line = file->lines;
1192 /* A note about blank line support: Blank lines should only
1193 * ever occur as the last line in the file. This function
1194 * is more forgiving than that - FILE_LINE_BLANK can occur
1198 /* Skip leading blanks. Should only happen if file is
1199 * empty (which is valid, but pointless).
1201 while ( (cur_line != NULL)
1202 && (cur_line->unprocessed[0] == '\0') )
1205 cur_line->type = FILE_LINE_BLANK;
1206 cur_line = cur_line->next;
1209 if ( (cur_line != NULL)
1210 && (cur_line->unprocessed[0] != '{') )
1212 /* File doesn't start with a header */
1213 file->parse_error = cur_line;
1214 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1215 return JB_ERR_PARSE;
1218 if ( (cur_line != NULL) && (0 ==
1219 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1221 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1223 cur_line = cur_line->next;
1224 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1226 if (cur_line->unprocessed[0])
1228 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1230 err = split_line_on_equals(cur_line->unprocessed,
1231 &cur_line->data.setting.name,
1232 &cur_line->data.setting.svalue);
1233 if (err == JB_ERR_MEMORY)
1237 else if (err != JB_ERR_OK)
1239 /* Line does not contain a name=value pair */
1240 file->parse_error = cur_line;
1241 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1242 return JB_ERR_PARSE;
1247 cur_line->type = FILE_LINE_BLANK;
1249 cur_line = cur_line->next;
1253 if ( (cur_line != NULL) && (0 ==
1254 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1256 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1258 cur_line = cur_line->next;
1259 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1261 if (cur_line->unprocessed[0])
1263 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1267 cur_line->type = FILE_LINE_BLANK;
1269 cur_line = cur_line->next;
1273 if ( (cur_line != NULL) && (0 ==
1274 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1276 cur_line->type = FILE_LINE_ALIAS_HEADER;
1278 cur_line = cur_line->next;
1279 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1281 if (cur_line->unprocessed[0])
1283 /* define an alias */
1284 struct action_alias * new_alias;
1286 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1288 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1289 if (err == JB_ERR_MEMORY)
1293 else if (err != JB_ERR_OK)
1295 /* Line does not contain a name=value pair */
1296 file->parse_error = cur_line;
1297 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1298 return JB_ERR_PARSE;
1301 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1306 free_alias_list(alias_list);
1307 return JB_ERR_MEMORY;
1310 err = get_actions(value, alias_list, new_alias->action);
1313 /* Invalid action or out of memory */
1317 free_alias_list(alias_list);
1318 if (err == JB_ERR_MEMORY)
1324 /* Line does not contain a name=value pair */
1325 file->parse_error = cur_line;
1326 file->parse_error_text = "This alias does not specify a valid set of actions.";
1327 return JB_ERR_PARSE;
1333 new_alias->name = name;
1336 new_alias->next = alias_list;
1337 alias_list = new_alias;
1341 cur_line->type = FILE_LINE_BLANK;
1343 cur_line = cur_line->next;
1347 /* Header done, process the main part of the file */
1348 while (cur_line != NULL)
1350 /* At this point, (cur_line->unprocessed[0] == '{') */
1351 assert(cur_line->unprocessed[0] == '{');
1352 text = cur_line->unprocessed + 1;
1353 len = strlen(text) - 1;
1354 if (text[len] != '}')
1356 /* No closing } on header */
1357 free_alias_list(alias_list);
1358 file->parse_error = cur_line;
1359 file->parse_error_text = "Headers starting with '{' must have a "
1360 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1361 "must close with two brackets ('}}').";
1362 return JB_ERR_PARSE;
1367 /* An invalid {{ header. */
1368 free_alias_list(alias_list);
1369 file->parse_error = cur_line;
1370 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1371 "Please remember that the system (two-bracket) headers must "
1372 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1373 "and must appear before any actions (one-bracket) headers. "
1374 "Also note that system headers may not be repeated.";
1375 return JB_ERR_PARSE;
1378 while ( (*text == ' ') || (*text == '\t') )
1384 && ( (text[len - 1] == ' ')
1385 || (text[len - 1] == '\t') ) )
1390 cur_line->type = FILE_LINE_ACTION;
1392 /* Remove {} and make copy */
1393 if (NULL == (value = (char *) malloc(len + 1)))
1396 free_alias_list(alias_list);
1397 return JB_ERR_MEMORY;
1399 strncpy(value, text, len);
1403 err = get_actions(value, alias_list, cur_line->data.action);
1406 /* Invalid action or out of memory */
1408 free_alias_list(alias_list);
1409 if (err == JB_ERR_MEMORY)
1415 /* Line does not contain a name=value pair */
1416 file->parse_error = cur_line;
1417 file->parse_error_text = "This header does not specify a valid set of actions.";
1418 return JB_ERR_PARSE;
1422 /* Done with string - it was clobbered anyway */
1425 /* Process next line */
1426 cur_line = cur_line->next;
1428 /* Loop processing URL patterns */
1429 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1431 if (cur_line->unprocessed[0])
1433 /* Could parse URL here, but this isn't currently needed */
1435 cur_line->type = FILE_LINE_URL;
1439 cur_line->type = FILE_LINE_BLANK;
1441 cur_line = cur_line->next;
1443 } /* End main while(cur_line != NULL) loop */
1445 free_alias_list(alias_list);
1451 /*********************************************************************
1453 * Function : edit_read_file_lines
1455 * Description : Read all the lines of a file into memory.
1456 * Handles whitespace, comments and line continuation.
1459 * 1 : fp = File to read from. On return, this will be
1460 * at EOF but it will not have been closed.
1461 * 2 : pfile = Destination for a linked list of file_lines.
1462 * Will be set to NULL on error.
1464 * Returns : JB_ERR_OK on success
1465 * JB_ERR_MEMORY on out-of-memory
1467 *********************************************************************/
1468 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1470 struct file_line * first_line; /* Keep for return value or to free */
1471 struct file_line * cur_line; /* Current line */
1472 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1480 cur_line = first_line = zalloc(sizeof(struct file_line));
1481 if (cur_line == NULL)
1483 return JB_ERR_MEMORY;
1486 cur_line->type = FILE_LINE_UNPROCESSED;
1488 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1491 /* Out of memory or empty file. */
1492 /* Note that empty file is not an error we propogate up */
1494 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1499 prev_line = cur_line;
1500 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1501 if (cur_line == NULL)
1504 edit_free_file_lines(first_line);
1505 return JB_ERR_MEMORY;
1508 cur_line->type = FILE_LINE_UNPROCESSED;
1510 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1511 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1514 edit_free_file_lines(first_line);
1515 return JB_ERR_MEMORY;
1519 while (rval != JB_ERR_FILE);
1523 /* We allocated one too many - free it */
1524 prev_line->next = NULL;
1527 *pfile = first_line;
1532 /*********************************************************************
1534 * Function : edit_read_file
1536 * Description : Read a complete file into memory.
1537 * Handles CGI parameter parsing. If requested, also
1538 * checks the file's modification timestamp.
1541 * 1 : csp = Current client state (buffers, headers, etc...)
1542 * 2 : parameters = map of cgi parameters.
1543 * 3 : require_version = true to check "ver" parameter.
1544 * 4 : suffix = File extension, e.g. ".action".
1545 * 5 : pfile = Destination for the file. Will be set
1549 * filename : The name of the file to read, without the
1550 * path or ".action" extension.
1551 * ver : (Only if require_version is nonzero)
1552 * Timestamp of the actions file. If wrong, this
1553 * function fails with JB_ERR_MODIFIED.
1555 * Returns : JB_ERR_OK on success
1556 * JB_ERR_MEMORY on out-of-memory
1557 * JB_ERR_CGI_PARAMS if "filename" was not specified
1559 * JB_ERR_FILE if the file cannot be opened or
1561 * JB_ERR_MODIFIED if version checking was requested and
1562 * failed - the file was modified outside
1563 * of this CGI editor instance.
1565 *********************************************************************/
1566 jb_err edit_read_file(struct client_state *csp,
1567 const struct map *parameters,
1568 int require_version,
1570 struct editable_file **pfile)
1572 struct file_line * lines;
1576 const char * identifier;
1577 struct editable_file * file;
1578 unsigned version = 0;
1579 struct stat statbuf[1];
1580 char version_buf[22];
1581 int newline = NEWLINE_UNKNOWN;
1589 err = get_file_name_param(csp, parameters, "f", suffix,
1590 &filename, &identifier);
1596 if (stat(filename, statbuf) < 0)
1598 /* Error, probably file not found. */
1602 version = (unsigned) statbuf->st_mtime;
1604 if (require_version)
1606 unsigned specified_version;
1607 err = get_number_param(csp, parameters, "v", &specified_version);
1614 if (version != specified_version)
1616 return JB_ERR_MODIFIED;
1620 if (NULL == (fp = fopen(filename,"rb")))
1626 err = edit_read_file_lines(fp, &lines, &newline);
1636 file = (struct editable_file *) zalloc(sizeof(*file));
1640 edit_free_file_lines(lines);
1644 file->lines = lines;
1645 file->newline = newline;
1646 file->filename = filename;
1647 file->version = version;
1648 file->identifier = url_encode(identifier);
1650 if (file->identifier == NULL)
1652 edit_free_file(file);
1653 return JB_ERR_MEMORY;
1656 /* Correct file->version_str */
1657 freez(file->version_str);
1658 snprintf(version_buf, 22, "%u", file->version);
1659 version_buf[21] = '\0';
1660 file->version_str = strdup(version_buf);
1661 if (version_buf == NULL)
1663 edit_free_file(file);
1664 return JB_ERR_MEMORY;
1672 /*********************************************************************
1674 * Function : edit_read_actions_file
1676 * Description : Read a complete actions file into memory.
1677 * Handles CGI parameter parsing. If requested, also
1678 * checks the file's modification timestamp.
1680 * If this function detects an error in the categories
1681 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1682 * then it handles it by filling in the specified
1683 * response structure and returning JB_ERR_FILE.
1686 * 1 : csp = Current client state (buffers, headers, etc...)
1687 * 2 : rsp = HTTP response. Only filled in on error.
1688 * 2 : parameters = map of cgi parameters.
1689 * 3 : require_version = true to check "ver" parameter.
1690 * 4 : pfile = Destination for the file. Will be set
1694 * filename : The name of the actions file to read, without the
1695 * path or ".action" extension.
1696 * ver : (Only if require_version is nonzero)
1697 * Timestamp of the actions file. If wrong, this
1698 * function fails with JB_ERR_MODIFIED.
1700 * Returns : JB_ERR_OK on success
1701 * JB_ERR_MEMORY on out-of-memory
1702 * JB_ERR_CGI_PARAMS if "filename" was not specified
1704 * JB_ERR_FILE if the file does not contain valid data,
1705 * or if file cannot be opened or
1706 * contains no data, or if version
1707 * checking was requested and failed.
1709 *********************************************************************/
1710 jb_err edit_read_actions_file(struct client_state *csp,
1711 struct http_response *rsp,
1712 const struct map *parameters,
1713 int require_version,
1714 struct editable_file **pfile)
1717 struct editable_file *file;
1725 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1728 /* Try to handle if possible */
1729 if (err == JB_ERR_FILE)
1731 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1733 else if (err == JB_ERR_MODIFIED)
1735 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1737 if (err == JB_ERR_OK)
1740 * Signal to higher-level CGI code that there was a problem but we
1741 * handled it, they should just return JB_ERR_OK.
1748 err = edit_parse_actions_file(file);
1751 if (err == JB_ERR_PARSE)
1753 err = cgi_error_parse(csp, rsp, file);
1754 if (err == JB_ERR_OK)
1757 * Signal to higher-level CGI code that there was a problem but we
1758 * handled it, they should just return JB_ERR_OK.
1763 edit_free_file(file);
1772 /*********************************************************************
1774 * Function : get_file_name_param
1776 * Description : Get the name of the file to edit from the parameters
1777 * passed to a CGI function. This function handles
1778 * security checks such as blocking urls containing
1779 * "/" or ".", prepending the config file directory,
1780 * and adding the specified suffix.
1782 * (This is an essential security check, otherwise
1783 * users may be able to pass "../../../etc/passwd"
1784 * and overwrite the password file [linux], "prn:"
1785 * and print random data [Windows], etc...)
1787 * This function only allows filenames contining the
1788 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1789 * That's probably too restrictive but at least it's
1793 * 1 : csp = Current client state (buffers, headers, etc...)
1794 * 2 : parameters = map of cgi parameters
1795 * 3 : param_name = The name of the parameter to read
1796 * 4 : suffix = File extension, e.g. ".actions"
1797 * 5 : pfilename = destination for full filename. Caller
1798 * free()s. Set to NULL on error.
1799 * 6 : pparam = destination for partial filename,
1800 * suitable for use in another URL. Allocated as part
1801 * of the map "parameters", so don't free it.
1802 * Set to NULL if not specified.
1804 * Returns : JB_ERR_OK on success
1805 * JB_ERR_MEMORY on out-of-memory
1806 * JB_ERR_CGI_PARAMS if "filename" was not specified
1809 *********************************************************************/
1810 static jb_err get_file_name_param(struct client_state *csp,
1811 const struct map *parameters,
1812 const char *param_name,
1815 const char **pparam)
1833 param = lookup(parameters, param_name);
1836 return JB_ERR_CGI_PARAMS;
1841 len = strlen(param);
1842 if (len >= FILENAME_MAX)
1845 return JB_ERR_CGI_PARAMS;
1848 /* Check every character to see if it's legal */
1850 while ((ch = *s++) != '\0')
1852 if ( ((ch < 'A') || (ch > 'Z'))
1853 && ((ch < 'a') || (ch > 'z'))
1854 && ((ch < '0') || (ch > '9'))
1858 /* Probable hack attempt. */
1859 return JB_ERR_CGI_PARAMS;
1863 /* Append extension */
1864 name = malloc(len + strlen(suffix) + 1);
1867 return JB_ERR_MEMORY;
1869 strcpy(name, param);
1870 strcpy(name + len, suffix);
1873 fullpath = make_path(csp->config->confdir, name);
1876 if (fullpath == NULL)
1878 return JB_ERR_MEMORY;
1882 *pfilename = fullpath;
1888 /*********************************************************************
1890 * Function : get_url_spec_param
1892 * Description : Get a URL pattern from the parameters
1893 * passed to a CGI function. Removes leading/trailing
1894 * spaces and validates it.
1897 * 1 : csp = Current client state (buffers, headers, etc...)
1898 * 2 : parameters = map of cgi parameters
1899 * 3 : name = Name of CGI parameter to read
1900 * 4 : pvalue = destination for value. Will be malloc()'d.
1901 * Set to NULL on error.
1903 * Returns : JB_ERR_OK on success
1904 * JB_ERR_MEMORY on out-of-memory
1905 * JB_ERR_CGI_PARAMS if the parameter was not specified
1908 *********************************************************************/
1909 static jb_err get_url_spec_param(struct client_state *csp,
1910 const struct map *parameters,
1914 const char *orig_param;
1917 struct url_spec compiled[1];
1927 orig_param = lookup(parameters, name);
1930 return JB_ERR_CGI_PARAMS;
1933 /* Copy and trim whitespace */
1934 param = strdup(orig_param);
1937 return JB_ERR_MEMORY;
1941 /* Must be non-empty, and can't allow 1st character to be '{' */
1942 if (param[0] == '\0' || param[0] == '{')
1945 return JB_ERR_CGI_PARAMS;
1948 /* Check for embedded newlines */
1949 for (s = param; *s != '\0'; s++)
1951 if ((*s == '\r') || (*s == '\n'))
1954 return JB_ERR_CGI_PARAMS;
1958 /* Check that regex is valid */
1963 return JB_ERR_MEMORY;
1965 err = create_url_spec(compiled, s);
1970 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1972 free_url_spec(compiled);
1974 if (param[strlen(param) - 1] == '\\')
1977 * Must protect trailing '\\' from becoming line continuation character.
1978 * Two methods: 1) If it's a domain only, add a trailing '/'.
1979 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1981 if (strchr(param, '/') == NULL)
1983 err = string_append(¶m, "/");
1987 err = string_append(¶m, "(?:)");
1994 /* Check that the modified regex is valid */
1999 return JB_ERR_MEMORY;
2001 err = create_url_spec(compiled, s);
2006 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2008 free_url_spec(compiled);
2015 /*********************************************************************
2017 * Function : map_radio
2019 * Description : Map a set of radio button values. E.g. if you have
2020 * 3 radio buttons, declare them as:
2021 * <option type="radio" name="xyz" @xyz-a@>
2022 * <option type="radio" name="xyz" @xyz-b@>
2023 * <option type="radio" name="xyz" @xyz-c@>
2024 * Then map one of the @xyz-?@ variables to "checked"
2025 * and all the others to empty by calling:
2026 * map_radio(exports, "xyz", "abc", sel)
2027 * Where 'sel' is 'a', 'b', or 'c'.
2030 * 1 : exports = Exports map to modify.
2031 * 2 : optionname = name for map
2032 * 3 : values = null-terminated list of values;
2033 * 4 : value = Selected value.
2035 * CGI Parameters : None
2037 * Returns : JB_ERR_OK on success
2038 * JB_ERR_MEMORY on out-of-memory
2040 *********************************************************************/
2041 static jb_err map_radio(struct map * exports,
2042 const char * optionname,
2043 const char * values,
2055 len = strlen(optionname);
2056 buf = malloc(len + 3);
2059 return JB_ERR_MEMORY;
2062 strcpy(buf, optionname);
2067 while ((c = *values++) != '\0')
2072 if (map(exports, buf, 1, "", 1))
2074 return JB_ERR_MEMORY;
2080 return map(exports, buf, 0, "checked", 1);
2084 /*********************************************************************
2086 * Function : cgi_error_modified
2088 * Description : CGI function that is called when a file is modified
2089 * outside the CGI editor.
2092 * 1 : csp = Current client state (buffers, headers, etc...)
2093 * 2 : rsp = http_response data structure for output
2094 * 3 : filename = The file that was modified.
2096 * CGI Parameters : none
2098 * Returns : JB_ERR_OK on success
2099 * JB_ERR_MEMORY on out-of-memory error.
2101 *********************************************************************/
2102 jb_err cgi_error_modified(struct client_state *csp,
2103 struct http_response *rsp,
2104 const char *filename)
2106 struct map *exports;
2113 if (NULL == (exports = default_exports(csp, NULL)))
2115 return JB_ERR_MEMORY;
2118 err = map(exports, "f", 1, html_encode(filename), 0);
2125 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2129 /*********************************************************************
2131 * Function : cgi_error_parse
2133 * Description : CGI function that is called when a file cannot
2134 * be parsed by the CGI editor.
2137 * 1 : csp = Current client state (buffers, headers, etc...)
2138 * 2 : rsp = http_response data structure for output
2139 * 3 : file = The file that was modified.
2141 * CGI Parameters : none
2143 * Returns : JB_ERR_OK on success
2144 * JB_ERR_MEMORY on out-of-memory error.
2146 *********************************************************************/
2147 jb_err cgi_error_parse(struct client_state *csp,
2148 struct http_response *rsp,
2149 struct editable_file *file)
2151 struct map *exports;
2153 struct file_line *cur_line;
2159 if (NULL == (exports = default_exports(csp, NULL)))
2161 return JB_ERR_MEMORY;
2164 err = map(exports, "f", 1, file->identifier, 1);
2165 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2167 cur_line = file->parse_error;
2170 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2171 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2179 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2183 /*********************************************************************
2185 * Function : cgi_error_file
2187 * Description : CGI function that is called when a file cannot be
2188 * opened by the CGI editor.
2191 * 1 : csp = Current client state (buffers, headers, etc...)
2192 * 2 : rsp = http_response data structure for output
2193 * 3 : filename = The file that was modified.
2195 * CGI Parameters : none
2197 * Returns : JB_ERR_OK on success
2198 * JB_ERR_MEMORY on out-of-memory error.
2200 *********************************************************************/
2201 jb_err cgi_error_file(struct client_state *csp,
2202 struct http_response *rsp,
2203 const char *filename)
2205 struct map *exports;
2212 if (NULL == (exports = default_exports(csp, NULL)))
2214 return JB_ERR_MEMORY;
2217 err = map(exports, "f", 1, html_encode(filename), 0);
2224 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2228 /*********************************************************************
2230 * Function : cgi_error_disabled
2232 * Description : CGI function that is called if the actions editor
2233 * is called although it's disabled in config
2236 * 1 : csp = Current client state (buffers, headers, etc...)
2237 * 2 : rsp = http_response data structure for output
2239 * CGI Parameters : none
2241 * Returns : JB_ERR_OK on success
2242 * JB_ERR_MEMORY on out-of-memory error.
2244 *********************************************************************/
2245 jb_err cgi_error_disabled(struct client_state *csp,
2246 struct http_response *rsp)
2248 struct map *exports;
2253 if (NULL == (exports = default_exports(csp, NULL)))
2255 return JB_ERR_MEMORY;
2258 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2262 /*********************************************************************
2264 * Function : cgi_edit_actions
2266 * Description : CGI function that allows the user to choose which
2267 * actions file to edit.
2270 * 1 : csp = Current client state (buffers, headers, etc...)
2271 * 2 : rsp = http_response data structure for output
2272 * 3 : parameters = map of cgi parameters
2274 * CGI Parameters : None
2276 * Returns : JB_ERR_OK on success
2277 * JB_ERR_MEMORY on out-of-memory error
2279 *********************************************************************/
2280 jb_err cgi_edit_actions(struct client_state *csp,
2281 struct http_response *rsp,
2282 const struct map *parameters)
2285 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2287 return cgi_error_disabled(csp, rsp);
2290 /* FIXME: Incomplete */
2291 rsp->status = strdup("302 Local Redirect from Privoxy");
2292 if (rsp->status == NULL)
2294 return JB_ERR_MEMORY;
2296 if (enlist_unique_header(rsp->headers, "Location",
2297 CGI_PREFIX "edit-actions-list?f=default"))
2301 return JB_ERR_MEMORY;
2308 /*********************************************************************
2310 * Function : cgi_edit_actions_list
2312 * Description : CGI function that edits the actions list.
2313 * FIXME: This function shouldn't FATAL ever.
2314 * FIXME: This function doesn't check the retval of map()
2316 * 1 : csp = Current client state (buffers, headers, etc...)
2317 * 2 : rsp = http_response data structure for output
2318 * 3 : parameters = map of cgi parameters
2320 * CGI Parameters : filename
2322 * Returns : JB_ERR_OK on success
2323 * JB_ERR_MEMORY on out-of-memory
2324 * JB_ERR_FILE if the file cannot be opened or
2326 * JB_ERR_CGI_PARAMS if "filename" was not specified
2329 *********************************************************************/
2330 jb_err cgi_edit_actions_list(struct client_state *csp,
2331 struct http_response *rsp,
2332 const struct map *parameters)
2334 char * section_template;
2335 char * url_template;
2340 struct map * exports;
2341 struct map * section_exports;
2342 struct map * url_exports;
2343 struct editable_file * file;
2344 struct file_line * cur_line;
2345 unsigned line_number = 0;
2346 unsigned prev_section_line_number = ((unsigned) (-1));
2348 struct file_list * fl;
2349 struct url_actions * b;
2350 char * buttons = NULL;
2353 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2355 return cgi_error_disabled(csp, rsp);
2358 if (NULL == (exports = default_exports(csp, NULL)))
2360 edit_free_file(file);
2361 return JB_ERR_MEMORY;
2364 /* Load actions file */
2365 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2368 /* No filename specified, can't read file, or out of memory. */
2369 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2372 /* Find start of actions in file */
2373 cur_line = file->lines;
2375 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2377 cur_line = cur_line->next;
2382 * Conventional actions files should have a match all block
2384 * cur_line = {...global actions...}
2385 * cur_line->next = /
2386 * cur_line->next->next = {...actions...} or EOF
2388 if ( (cur_line != NULL)
2389 && (cur_line->type == FILE_LINE_ACTION)
2390 && (cur_line->next != NULL)
2391 && (cur_line->next->type == FILE_LINE_URL)
2392 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2393 && ( (cur_line->next->next == NULL)
2394 || (cur_line->next->next->type != FILE_LINE_URL)
2398 * Generate string with buttons to set actions for "/" to
2399 * any predefined set of actions (named standard.*, probably
2400 * residing in standard.action).
2403 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2406 edit_free_file(file);
2408 if (err == JB_ERR_FILE)
2410 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2415 err = template_fill(§ion_template, exports);
2418 edit_free_file(file);
2423 buttons = strdup("");
2424 for (i = 0; i < MAX_ACTION_FILES; i++)
2426 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2428 for (b = b->next; NULL != b; b = b->next)
2430 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2432 if (err || (NULL == (section_exports = new_map())))
2435 free(section_template);
2436 edit_free_file(file);
2438 return JB_ERR_MEMORY;
2441 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2443 if (err || (NULL == (s = strdup(section_template))))
2445 free_map(section_exports);
2447 free(section_template);
2448 edit_free_file(file);
2450 return JB_ERR_MEMORY;
2453 if (!err) err = template_fill(&s, section_exports);
2454 free_map(section_exports);
2455 if (!err) err = string_join(&buttons, s);
2460 freez(section_template);
2461 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2464 * Conventional actions file, supply extra editing help.
2465 * (e.g. don't allow them to make it an unconventional one).
2467 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2469 snprintf(buf, 150, "%d", line_number);
2470 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2471 snprintf(buf, 150, "%d", line_number + 2);
2472 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2473 if (!err) err = map(exports, "all-urls-actions", 1,
2474 actions_to_html(csp, cur_line->data.action), 0);
2476 /* Skip the 2 lines */
2477 cur_line = cur_line->next->next;
2481 * Note that prev_section_line_number is NOT set here.
2482 * This is deliberate and not a bug. It stops a "Move up"
2483 * option appearing on the next section. Clicking "Move
2484 * up" would make the actions file unconventional, which
2485 * we don't want, so we hide this option.
2491 * Non-standard actions file - does not begin with
2492 * the "All URLs" section.
2494 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2497 /* Set up global exports */
2499 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2500 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2502 /* Discourage private additions to default.action */
2504 if (!err) err = map_conditional(exports, "default-action",
2505 (strcmp("default", lookup(parameters, "f")) == 0));
2508 edit_free_file(file);
2513 /* Should do all global exports above this point */
2515 /* Load templates */
2517 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2520 edit_free_file(file);
2522 if (err == JB_ERR_FILE)
2524 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2529 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2532 free(section_template);
2533 edit_free_file(file);
2535 if (err == JB_ERR_FILE)
2537 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2542 err = template_fill(§ion_template, exports);
2546 edit_free_file(file);
2552 err = template_fill(&url_template, exports);
2555 free(section_template);
2556 edit_free_file(file);
2561 if (NULL == (sections = strdup("")))
2563 free(section_template);
2565 edit_free_file(file);
2567 return JB_ERR_MEMORY;
2570 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2572 if (NULL == (section_exports = new_map()))
2575 free(section_template);
2577 edit_free_file(file);
2579 return JB_ERR_MEMORY;
2582 snprintf(buf, 150, "%d", line_number);
2583 err = map(section_exports, "s", 1, buf, 1);
2584 if (!err) err = map(section_exports, "actions", 1,
2585 actions_to_html(csp, cur_line->data.action), 0);
2588 && (cur_line->next != NULL)
2589 && (cur_line->next->type == FILE_LINE_URL))
2591 /* This section contains at least one URL, don't allow delete */
2592 err = map_block_killer(section_exports, "empty-section");
2596 if (!err) err = map_block_keep(section_exports, "empty-section");
2599 if (prev_section_line_number != ((unsigned)(-1)))
2601 /* Not last section */
2602 snprintf(buf, 150, "%d", prev_section_line_number);
2603 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2604 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2609 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2611 prev_section_line_number = line_number;
2616 free(section_template);
2618 edit_free_file(file);
2620 free_map(section_exports);
2624 /* Should do all section-specific exports above this point */
2626 if (NULL == (urls = strdup("")))
2629 free(section_template);
2631 edit_free_file(file);
2633 free_map(section_exports);
2634 return JB_ERR_MEMORY;
2639 cur_line = cur_line->next;
2642 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2644 if (NULL == (url_exports = new_map()))
2648 free(section_template);
2650 edit_free_file(file);
2652 free_map(section_exports);
2653 return JB_ERR_MEMORY;
2656 snprintf(buf, 150, "%d", line_number);
2657 err = map(url_exports, "p", 1, buf, 1);
2659 snprintf(buf, 150, "%d", url_1_2);
2660 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2662 if (!err) err = map(url_exports, "url-html", 1,
2663 html_encode(cur_line->unprocessed), 0);
2664 if (!err) err = map(url_exports, "url", 1,
2665 url_encode(cur_line->unprocessed), 0);
2671 free(section_template);
2673 edit_free_file(file);
2675 free_map(section_exports);
2676 free_map(url_exports);
2680 if (NULL == (s = strdup(url_template)))
2684 free(section_template);
2686 edit_free_file(file);
2688 free_map(section_exports);
2689 free_map(url_exports);
2690 return JB_ERR_MEMORY;
2693 err = template_fill(&s, section_exports);
2694 if (!err) err = template_fill(&s, url_exports);
2695 if (!err) err = string_append(&urls, s);
2697 free_map(url_exports);
2704 free(section_template);
2706 edit_free_file(file);
2708 free_map(section_exports);
2712 url_1_2 = 3 - url_1_2;
2714 cur_line = cur_line->next;
2718 err = map(section_exports, "urls", 1, urls, 0);
2720 /* Could also do section-specific exports here, but it wouldn't be as fast */
2722 if ( (cur_line != NULL)
2723 && (cur_line->type == FILE_LINE_ACTION))
2725 /* Not last section */
2726 snprintf(buf, 150, "%d", line_number);
2727 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2728 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2733 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2739 free(section_template);
2741 edit_free_file(file);
2743 free_map(section_exports);
2747 if (NULL == (s = strdup(section_template)))
2750 free(section_template);
2752 edit_free_file(file);
2754 free_map(section_exports);
2755 return JB_ERR_MEMORY;
2758 err = template_fill(&s, section_exports);
2759 if (!err) err = string_append(§ions, s);
2762 free_map(section_exports);
2767 free(section_template);
2769 edit_free_file(file);
2775 edit_free_file(file);
2776 free(section_template);
2779 err = map(exports, "sections", 1, sections, 0);
2786 /* Could also do global exports here, but it wouldn't be as fast */
2788 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2792 /*********************************************************************
2794 * Function : cgi_edit_actions_for_url
2796 * Description : CGI function that edits the Actions list.
2799 * 1 : csp = Current client state (buffers, headers, etc...)
2800 * 2 : rsp = http_response data structure for output
2801 * 3 : parameters = map of cgi parameters
2803 * CGI Parameters : None
2805 * Returns : JB_ERR_OK on success
2806 * JB_ERR_MEMORY on out-of-memory
2807 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2808 * specified or not valid.
2810 *********************************************************************/
2811 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2812 struct http_response *rsp,
2813 const struct map *parameters)
2815 struct map * exports;
2817 struct editable_file * file;
2818 struct file_line * cur_line;
2819 unsigned line_number;
2821 struct file_list *filter_file;
2822 struct re_filterfile_spec *filter_group;
2824 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2826 return cgi_error_disabled(csp, rsp);
2829 err = get_number_param(csp, parameters, "s", §ionid);
2835 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2838 /* No filename specified, can't read file, modified, or out of memory. */
2839 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2842 cur_line = file->lines;
2844 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2846 cur_line = cur_line->next;
2849 if ( (cur_line == NULL)
2850 || (line_number != sectionid)
2852 || (cur_line->type != FILE_LINE_ACTION))
2854 /* Invalid "sectionid" parameter */
2855 edit_free_file(file);
2856 return JB_ERR_CGI_PARAMS;
2859 if (NULL == (exports = default_exports(csp, NULL)))
2861 edit_free_file(file);
2862 return JB_ERR_MEMORY;
2865 err = map(exports, "f", 1, file->identifier, 1);
2866 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2867 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2869 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2871 filter_file = csp->rlist;
2872 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2874 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2878 edit_free_file(file);
2883 if (filter_group == NULL)
2885 err = map(exports, "filter-params", 1, "", 1);
2889 /* We have some entries in the filter list */
2892 char * filter_template;
2894 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2897 edit_free_file(file);
2899 if (err == JB_ERR_FILE)
2901 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2906 err = template_fill(&filter_template, exports);
2908 result = strdup("");
2910 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2912 char current_mode = 'x';
2913 struct list_entry *filter_name;
2915 struct map *line_exports;
2918 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2919 while ((filter_name != NULL)
2920 && (0 != strcmp(filter_group->name, filter_name->str)))
2922 filter_name = filter_name->next;
2925 if (filter_name != NULL)
2931 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2932 while ((filter_name != NULL)
2933 && (0 != strcmp(filter_group->name, filter_name->str)))
2935 filter_name = filter_name->next;
2937 if (filter_name != NULL)
2943 /* Generate a unique serial number */
2944 snprintf(number, sizeof(number), "%x", index++);
2945 number[sizeof(number) - 1] = '\0';
2947 line_exports = new_map();
2948 if (line_exports == NULL)
2950 err = JB_ERR_MEMORY;
2955 if (!err) err = map(line_exports, "index", 1, number, 1);
2956 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2957 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2958 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2963 this_line = strdup(filter_template);
2964 if (this_line == NULL) err = JB_ERR_MEMORY;
2966 if (!err) err = template_fill(&this_line, line_exports);
2967 string_join(&result, this_line);
2969 free_map(line_exports);
2973 freez(filter_template);
2977 err = map(exports, "filter-params", 1, result, 0);
2985 if (!err) err = map_radio(exports, "filter-all", "nx",
2986 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
2988 edit_free_file(file);
2996 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3000 /*********************************************************************
3002 * Function : cgi_edit_actions_submit
3004 * Description : CGI function that actually edits the Actions list.
3007 * 1 : csp = Current client state (buffers, headers, etc...)
3008 * 2 : rsp = http_response data structure for output
3009 * 3 : parameters = map of cgi parameters
3011 * CGI Parameters : None
3013 * Returns : JB_ERR_OK on success
3014 * JB_ERR_MEMORY on out-of-memory
3015 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3016 * specified or not valid.
3018 *********************************************************************/
3019 jb_err cgi_edit_actions_submit(struct client_state *csp,
3020 struct http_response *rsp,
3021 const struct map *parameters)
3027 struct editable_file * file;
3028 struct file_line * cur_line;
3029 unsigned line_number;
3033 const char * action_set_name;
3035 struct file_list * fl;
3036 struct url_actions * b;
3038 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3040 return cgi_error_disabled(csp, rsp);
3043 err = get_number_param(csp, parameters, "s", §ionid);
3049 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3052 /* No filename specified, can't read file, modified, or out of memory. */
3053 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3056 cur_line = file->lines;
3058 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3060 cur_line = cur_line->next;
3063 if ( (cur_line == NULL)
3064 || (line_number != sectionid)
3066 || (cur_line->type != FILE_LINE_ACTION))
3068 /* Invalid "sectionid" parameter */
3069 edit_free_file(file);
3070 return JB_ERR_CGI_PARAMS;
3073 get_string_param(parameters, "p", &action_set_name);
3074 if (action_set_name != NULL)
3076 for (index = 0; index < MAX_ACTION_FILES; index++)
3078 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3080 for (b = b->next; NULL != b; b = b->next)
3082 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3084 copy_action(cur_line->data.action, b->action);
3090 edit_free_file(file);
3091 return JB_ERR_CGI_PARAMS;
3097 err = actions_from_radio(parameters, cur_line->data.action);
3103 edit_free_file(file);
3107 ch = get_char_param(parameters, "filter_all");
3110 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3111 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3112 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3116 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3119 for (index = 0; !err; index++)
3126 /* Generate the keys */
3127 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3128 key_value[sizeof(key_value) - 1] = '\0';
3129 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3130 key_name[sizeof(key_name) - 1] = '\0';
3132 err = get_string_param(parameters, key_name, &name);
3141 value = get_char_param(parameters, key_value);
3144 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3145 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3146 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3148 else if (value == 'N')
3150 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3151 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3153 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3154 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3157 else if (value == 'X')
3159 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3160 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3167 edit_free_file(file);
3171 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3174 edit_free_file(file);
3175 return JB_ERR_MEMORY;
3178 len = strlen(actiontext);
3182 * Empty action - must special-case this.
3183 * Simply setting len to 1 is sufficient...
3188 if (NULL == (newtext = malloc(len + 2)))
3192 edit_free_file(file);
3193 return JB_ERR_MEMORY;
3195 strcpy(newtext, actiontext);
3199 newtext[len + 1] = '\0';
3201 freez(cur_line->raw);
3202 freez(cur_line->unprocessed);
3203 cur_line->unprocessed = newtext;
3205 err = edit_write_file(file);
3208 /* Error writing file */
3209 edit_free_file(file);
3213 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3214 string_append(&target, file->identifier);
3216 edit_free_file(file);
3221 return JB_ERR_MEMORY;
3224 rsp->status = strdup("302 Local Redirect from Privoxy");
3225 if (rsp->status == NULL)
3228 return JB_ERR_MEMORY;
3230 err = enlist_unique_header(rsp->headers, "Location", target);
3237 /*********************************************************************
3239 * Function : cgi_edit_actions_url
3241 * Description : CGI function that actually edits a URL pattern in
3245 * 1 : csp = Current client state (buffers, headers, etc...)
3246 * 2 : rsp = http_response data structure for output
3247 * 3 : parameters = map of cgi parameters
3250 * filename : Identifies the file to edit
3251 * ver : File's last-modified time
3252 * section : Line number of section to edit
3253 * pattern : Line number of pattern to edit
3254 * newval : New value for pattern
3256 * Returns : JB_ERR_OK on success
3257 * JB_ERR_MEMORY on out-of-memory
3258 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3259 * specified or not valid.
3261 *********************************************************************/
3262 jb_err cgi_edit_actions_url(struct client_state *csp,
3263 struct http_response *rsp,
3264 const struct map *parameters)
3268 struct editable_file * file;
3269 struct file_line * cur_line;
3270 unsigned line_number;
3274 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3276 return cgi_error_disabled(csp, rsp);
3279 err = get_number_param(csp, parameters, "p", &patternid);
3286 return JB_ERR_CGI_PARAMS;
3289 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3295 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3298 /* No filename specified, can't read file, modified, or out of memory. */
3300 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3304 cur_line = file->lines;
3306 while ((cur_line != NULL) && (line_number < patternid))
3308 cur_line = cur_line->next;
3312 if ( (cur_line == NULL)
3313 || (cur_line->type != FILE_LINE_URL))
3315 /* Invalid "patternid" parameter */
3317 edit_free_file(file);
3318 return JB_ERR_CGI_PARAMS;
3321 /* At this point, the line to edit is in cur_line */
3323 freez(cur_line->raw);
3324 freez(cur_line->unprocessed);
3325 cur_line->unprocessed = new_pattern;
3327 err = edit_write_file(file);
3330 /* Error writing file */
3331 edit_free_file(file);
3335 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3336 string_append(&target, file->identifier);
3338 edit_free_file(file);
3343 return JB_ERR_MEMORY;
3346 rsp->status = strdup("302 Local Redirect from Privoxy");
3347 if (rsp->status == NULL)
3350 return JB_ERR_MEMORY;
3352 err = enlist_unique_header(rsp->headers, "Location", target);
3359 /*********************************************************************
3361 * Function : cgi_edit_actions_add_url
3363 * Description : CGI function that actually adds a URL pattern to
3367 * 1 : csp = Current client state (buffers, headers, etc...)
3368 * 2 : rsp = http_response data structure for output
3369 * 3 : parameters = map of cgi parameters
3372 * filename : Identifies the file to edit
3373 * ver : File's last-modified time
3374 * section : Line number of section to edit
3375 * newval : New pattern
3377 * Returns : JB_ERR_OK on success
3378 * JB_ERR_MEMORY on out-of-memory
3379 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3380 * specified or not valid.
3382 *********************************************************************/
3383 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3384 struct http_response *rsp,
3385 const struct map *parameters)
3389 struct file_line * new_line;
3390 struct editable_file * file;
3391 struct file_line * cur_line;
3392 unsigned line_number;
3396 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3398 return cgi_error_disabled(csp, rsp);
3401 err = get_number_param(csp, parameters, "s", §ionid);
3408 return JB_ERR_CGI_PARAMS;
3411 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3417 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3420 /* No filename specified, can't read file, modified, or out of memory. */
3422 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3426 cur_line = file->lines;
3428 while ((cur_line != NULL) && (line_number < sectionid))
3430 cur_line = cur_line->next;
3434 if ( (cur_line == NULL)
3435 || (cur_line->type != FILE_LINE_ACTION))
3437 /* Invalid "sectionid" parameter */
3439 edit_free_file(file);
3440 return JB_ERR_CGI_PARAMS;
3443 /* At this point, the section header is in cur_line - add after this. */
3445 /* Allocate the new line */
3446 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3447 if (new_line == NULL)
3450 edit_free_file(file);
3451 return JB_ERR_MEMORY;
3454 /* Fill in the data members of the new line */
3455 new_line->raw = NULL;
3456 new_line->prefix = NULL;
3457 new_line->unprocessed = new_pattern;
3458 new_line->type = FILE_LINE_URL;
3460 /* Link new_line into the list, after cur_line */
3461 new_line->next = cur_line->next;
3462 cur_line->next = new_line;
3464 /* Done making changes, now commit */
3466 err = edit_write_file(file);
3469 /* Error writing file */
3470 edit_free_file(file);
3474 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3475 string_append(&target, file->identifier);
3477 edit_free_file(file);
3482 return JB_ERR_MEMORY;
3485 rsp->status = strdup("302 Local Redirect from Privoxy");
3486 if (rsp->status == NULL)
3489 return JB_ERR_MEMORY;
3491 err = enlist_unique_header(rsp->headers, "Location", target);
3498 /*********************************************************************
3500 * Function : cgi_edit_actions_remove_url
3502 * Description : CGI function that actually removes a URL pattern from
3506 * 1 : csp = Current client state (buffers, headers, etc...)
3507 * 2 : rsp = http_response data structure for output
3508 * 3 : parameters = map of cgi parameters
3511 * f : (filename) Identifies the file to edit
3512 * v : (version) File's last-modified time
3513 * p : (pattern) Line number of pattern to remove
3515 * Returns : JB_ERR_OK on success
3516 * JB_ERR_MEMORY on out-of-memory
3517 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3518 * specified or not valid.
3520 *********************************************************************/
3521 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3522 struct http_response *rsp,
3523 const struct map *parameters)
3526 struct editable_file * file;
3527 struct file_line * cur_line;
3528 struct file_line * prev_line;
3529 unsigned line_number;
3533 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3535 return cgi_error_disabled(csp, rsp);
3538 err = get_number_param(csp, parameters, "p", &patternid);
3544 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3547 /* No filename specified, can't read file, modified, or out of memory. */
3548 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3553 cur_line = file->lines;
3555 while ((cur_line != NULL) && (line_number < patternid))
3557 prev_line = cur_line;
3558 cur_line = cur_line->next;
3562 if ( (cur_line == NULL)
3563 || (prev_line == NULL)
3564 || (cur_line->type != FILE_LINE_URL))
3566 /* Invalid "patternid" parameter */
3567 edit_free_file(file);
3568 return JB_ERR_CGI_PARAMS;
3571 /* At this point, the line to remove is in cur_line, and the previous
3572 * one is in prev_line
3575 /* Unlink cur_line */
3576 prev_line->next = cur_line->next;
3577 cur_line->next = NULL;
3580 edit_free_file_lines(cur_line);
3582 err = edit_write_file(file);
3585 /* Error writing file */
3586 edit_free_file(file);
3590 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3591 string_append(&target, file->identifier);
3593 edit_free_file(file);
3598 return JB_ERR_MEMORY;
3601 rsp->status = strdup("302 Local Redirect from Privoxy");
3602 if (rsp->status == NULL)
3605 return JB_ERR_MEMORY;
3607 err = enlist_unique_header(rsp->headers, "Location", target);
3614 /*********************************************************************
3616 * Function : cgi_edit_actions_section_remove
3618 * Description : CGI function that actually removes a whole section from
3619 * the actions file. The section must be empty first
3620 * (else JB_ERR_CGI_PARAMS).
3623 * 1 : csp = Current client state (buffers, headers, etc...)
3624 * 2 : rsp = http_response data structure for output
3625 * 3 : parameters = map of cgi parameters
3628 * f : (filename) Identifies the file to edit
3629 * v : (version) File's last-modified time
3630 * s : (section) Line number of section to edit
3632 * Returns : JB_ERR_OK on success
3633 * JB_ERR_MEMORY on out-of-memory
3634 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3635 * specified or not valid.
3637 *********************************************************************/
3638 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3639 struct http_response *rsp,
3640 const struct map *parameters)
3643 struct editable_file * file;
3644 struct file_line * cur_line;
3645 struct file_line * prev_line;
3646 unsigned line_number;
3650 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3652 return cgi_error_disabled(csp, rsp);
3655 err = get_number_param(csp, parameters, "s", §ionid);
3661 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3664 /* No filename specified, can't read file, modified, or out of memory. */
3665 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3669 cur_line = file->lines;
3672 while ((cur_line != NULL) && (line_number < sectionid))
3674 prev_line = cur_line;
3675 cur_line = cur_line->next;
3679 if ( (cur_line == NULL)
3680 || (cur_line->type != FILE_LINE_ACTION) )
3682 /* Invalid "sectionid" parameter */
3683 edit_free_file(file);
3684 return JB_ERR_CGI_PARAMS;
3687 if ( (cur_line->next != NULL)
3688 && (cur_line->next->type == FILE_LINE_URL) )
3690 /* Section not empty. */
3691 edit_free_file(file);
3692 return JB_ERR_CGI_PARAMS;
3695 /* At this point, the line to remove is in cur_line, and the previous
3696 * one is in prev_line
3699 /* Unlink cur_line */
3700 if (prev_line == NULL)
3702 /* Removing the first line from the file */
3703 file->lines = cur_line->next;
3707 prev_line->next = cur_line->next;
3709 cur_line->next = NULL;
3712 edit_free_file_lines(cur_line);
3714 err = edit_write_file(file);
3717 /* Error writing file */
3718 edit_free_file(file);
3722 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3723 string_append(&target, file->identifier);
3725 edit_free_file(file);
3730 return JB_ERR_MEMORY;
3733 rsp->status = strdup("302 Local Redirect from Privoxy");
3734 if (rsp->status == NULL)
3737 return JB_ERR_MEMORY;
3739 err = enlist_unique_header(rsp->headers, "Location", target);
3746 /*********************************************************************
3748 * Function : cgi_edit_actions_section_add
3750 * Description : CGI function that adds a new empty section to
3754 * 1 : csp = Current client state (buffers, headers, etc...)
3755 * 2 : rsp = http_response data structure for output
3756 * 3 : parameters = map of cgi parameters
3759 * f : (filename) Identifies the file to edit
3760 * v : (version) File's last-modified time
3761 * s : (section) Line number of section to add after, 0 for
3764 * Returns : JB_ERR_OK on success
3765 * JB_ERR_MEMORY on out-of-memory
3766 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3767 * specified or not valid.
3769 *********************************************************************/
3770 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3771 struct http_response *rsp,
3772 const struct map *parameters)
3775 struct file_line * new_line;
3777 struct editable_file * file;
3778 struct file_line * cur_line;
3779 unsigned line_number;
3783 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3785 return cgi_error_disabled(csp, rsp);
3788 err = get_number_param(csp, parameters, "s", §ionid);
3794 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3797 /* No filename specified, can't read file, modified, or out of memory. */
3798 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3802 cur_line = file->lines;
3806 /* Add to start of file */
3807 if (cur_line != NULL)
3809 /* There's something in the file, find the line before the first
3812 while ( (cur_line->next != NULL)
3813 && (cur_line->next->type != FILE_LINE_ACTION) )
3815 cur_line = cur_line->next;
3822 /* Add after stated section. */
3823 while ((cur_line != NULL) && (line_number < sectionid))
3825 cur_line = cur_line->next;
3829 if ( (cur_line == NULL)
3830 || (cur_line->type != FILE_LINE_ACTION))
3832 /* Invalid "sectionid" parameter */
3833 edit_free_file(file);
3834 return JB_ERR_CGI_PARAMS;
3837 /* Skip through the section to find the last line in it. */
3838 while ( (cur_line->next != NULL)
3839 && (cur_line->next->type != FILE_LINE_ACTION) )
3841 cur_line = cur_line->next;
3846 /* At this point, the last line in the previous section is in cur_line
3847 * - add after this. (Or if we need to add as the first line, cur_line
3851 new_text = strdup("{}");
3852 if (NULL == new_text)
3854 edit_free_file(file);
3855 return JB_ERR_MEMORY;
3858 /* Allocate the new line */
3859 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3860 if (new_line == NULL)
3863 edit_free_file(file);
3864 return JB_ERR_MEMORY;
3867 /* Fill in the data members of the new line */
3868 new_line->raw = NULL;
3869 new_line->prefix = NULL;
3870 new_line->unprocessed = new_text;
3871 new_line->type = FILE_LINE_ACTION;
3873 if (cur_line != NULL)
3875 /* Link new_line into the list, after cur_line */
3876 new_line->next = cur_line->next;
3877 cur_line->next = new_line;
3881 /* Link new_line into the list, as first line */
3882 new_line->next = file->lines;
3883 file->lines = new_line;
3886 /* Done making changes, now commit */
3888 err = edit_write_file(file);
3891 /* Error writing file */
3892 edit_free_file(file);
3896 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3897 string_append(&target, file->identifier);
3899 edit_free_file(file);
3904 return JB_ERR_MEMORY;
3907 rsp->status = strdup("302 Local Redirect from Privoxy");
3908 if (rsp->status == NULL)
3911 return JB_ERR_MEMORY;
3913 err = enlist_unique_header(rsp->headers, "Location", target);
3920 /*********************************************************************
3922 * Function : cgi_edit_actions_section_swap
3924 * Description : CGI function that swaps the order of two sections
3925 * in the actions file. Note that this CGI can actually
3926 * swap any two arbitrary sections, but the GUI interface
3927 * currently only allows consecutive sections to be
3931 * 1 : csp = Current client state (buffers, headers, etc...)
3932 * 2 : rsp = http_response data structure for output
3933 * 3 : parameters = map of cgi parameters
3936 * f : (filename) Identifies the file to edit
3937 * v : (version) File's last-modified time
3938 * s1 : (section1) Line number of first section to swap
3939 * s2 : (section2) Line number of second section to swap
3941 * Returns : JB_ERR_OK on success
3942 * JB_ERR_MEMORY on out-of-memory
3943 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3944 * specified or not valid.
3946 *********************************************************************/
3947 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3948 struct http_response *rsp,
3949 const struct map *parameters)
3953 struct editable_file * file;
3954 struct file_line * cur_line;
3955 struct file_line * prev_line;
3956 struct file_line * line_before_section1;
3957 struct file_line * line_start_section1;
3958 struct file_line * line_end_section1;
3959 struct file_line * line_after_section1;
3960 struct file_line * line_before_section2;
3961 struct file_line * line_start_section2;
3962 struct file_line * line_end_section2;
3963 struct file_line * line_after_section2;
3964 unsigned line_number;
3968 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3970 return cgi_error_disabled(csp, rsp);
3973 err = get_number_param(csp, parameters, "s1", §ion1);
3974 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3980 if (section1 > section2)
3982 unsigned temp = section2;
3983 section2 = section1;
3987 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3990 /* No filename specified, can't read file, modified, or out of memory. */
3991 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3994 /* Start at the beginning... */
3996 cur_line = file->lines;
3999 /* ... find section1 ... */
4000 while ((cur_line != NULL) && (line_number < section1))
4002 prev_line = cur_line;
4003 cur_line = cur_line->next;
4007 if ( (cur_line == NULL)
4008 || (cur_line->type != FILE_LINE_ACTION) )
4010 /* Invalid "section1" parameter */
4011 edit_free_file(file);
4012 return JB_ERR_CGI_PARAMS;
4015 /* If no-op, we've validated params and can skip the rest. */
4016 if (section1 != section2)
4018 /* ... find the end of section1 ... */
4019 line_before_section1 = prev_line;
4020 line_start_section1 = cur_line;
4023 prev_line = cur_line;
4024 cur_line = cur_line->next;
4027 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4028 line_end_section1 = prev_line;
4029 line_after_section1 = cur_line;
4031 /* ... find section2 ... */
4032 while ((cur_line != NULL) && (line_number < section2))
4034 prev_line = cur_line;
4035 cur_line = cur_line->next;
4039 if ( (cur_line == NULL)
4040 || (cur_line->type != FILE_LINE_ACTION) )
4042 /* Invalid "section2" parameter */
4043 edit_free_file(file);
4044 return JB_ERR_CGI_PARAMS;
4047 /* ... find the end of section2 ... */
4048 line_before_section2 = prev_line;
4049 line_start_section2 = cur_line;
4052 prev_line = cur_line;
4053 cur_line = cur_line->next;
4056 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4057 line_end_section2 = prev_line;
4058 line_after_section2 = cur_line;
4060 /* Now have all the pointers we need. Do the swap. */
4062 /* Change the pointer to section1 to point to section2 instead */
4063 if (line_before_section1 == NULL)
4065 file->lines = line_start_section2;
4069 line_before_section1->next = line_start_section2;
4072 if (line_before_section2 == line_end_section1)
4074 /* Consecutive sections */
4075 line_end_section2->next = line_start_section1;
4079 line_end_section2->next = line_after_section1;
4080 line_before_section2->next = line_start_section1;
4083 /* Set the pointer from the end of section1 to the rest of the file */
4084 line_end_section1->next = line_after_section2;
4086 err = edit_write_file(file);
4089 /* Error writing file */
4090 edit_free_file(file);
4093 } /* END if (section1 != section2) */
4095 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4096 string_append(&target, file->identifier);
4098 edit_free_file(file);
4103 return JB_ERR_MEMORY;
4106 rsp->status = strdup("302 Local Redirect from Privoxy");
4107 if (rsp->status == NULL)
4110 return JB_ERR_MEMORY;
4112 err = enlist_unique_header(rsp->headers, "Location", target);
4119 /*********************************************************************
4121 * Function : cgi_toggle
4123 * Description : CGI function that adds a new empty section to
4127 * 1 : csp = Current client state (buffers, headers, etc...)
4128 * 2 : rsp = http_response data structure for output
4129 * 3 : parameters = map of cgi parameters
4132 * set : If present, how to change toggle setting:
4133 * "enable", "disable", "toggle", or none (default).
4134 * mini : If present, use mini reply template.
4136 * Returns : JB_ERR_OK on success
4137 * JB_ERR_MEMORY on out-of-memory
4139 *********************************************************************/
4140 jb_err cgi_toggle(struct client_state *csp,
4141 struct http_response *rsp,
4142 const struct map *parameters)
4144 struct map *exports;
4146 const char *template_name;
4152 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4154 return cgi_error_disabled(csp, rsp);
4157 mode = get_char_param(parameters, "set");
4164 else if (mode == 'D')
4169 else if (mode == 'T')
4172 g_bToggleIJB = !g_bToggleIJB;
4175 if (NULL == (exports = default_exports(csp, "toggle")))
4177 return JB_ERR_MEMORY;
4180 template_name = (get_char_param(parameters, "mini")
4184 return template_fill_for_cgi(csp, template_name, exports, rsp);
4188 /*********************************************************************
4190 * Function : javascriptify
4192 * Description : Converts a string into a form JavaScript will like.
4194 * Netscape 4's JavaScript sucks - it doesn't use
4195 * "id" parameters, so you have to set the "name"
4196 * used to submit a form element to something JavaScript
4197 * will like. (Or access the elements by index in an
4198 * array. That array contains >60 elements and will
4199 * be changed whenever we add a new action to the
4200 * editor, so I'm NOT going to use indexes that have
4201 * to be figured out by hand.)
4203 * Currently the only thing we have to worry about
4204 * is "-" ==> "_" conversion.
4206 * This is a length-preserving operation so it is
4207 * carried out in-place, no memory is allocated
4211 * 1 : identifier = String to make JavaScript-friendly.
4215 *********************************************************************/
4216 static void javascriptify(char * identifier)
4218 char * p = identifier;
4219 while (NULL != (p = strchr(p, '-')))
4226 /*********************************************************************
4228 * Function : actions_to_radio
4230 * Description : Converts a actionsfile entry into settings for
4231 * radio buttons and edit boxes on a HTML form.
4234 * 1 : exports = List of substitutions to add to.
4235 * 2 : action = Action to read
4237 * Returns : JB_ERR_OK on success
4238 * JB_ERR_MEMORY on out-of-memory
4240 *********************************************************************/
4241 static jb_err actions_to_radio(struct map * exports,
4242 const struct action_spec *action)
4244 unsigned mask = action->mask;
4245 unsigned add = action->add;
4253 mask = action->mask;
4256 /* sanity - prevents "-feature +feature" */
4260 #define DEFINE_ACTION_BOOL(name, bit) \
4261 if (!(mask & bit)) \
4263 current_mode = 'n'; \
4265 else if (add & bit) \
4267 current_mode = 'y'; \
4271 current_mode = 'x'; \
4273 if (map_radio(exports, name, "ynx", current_mode)) \
4275 return JB_ERR_MEMORY; \
4278 #define DEFINE_ACTION_STRING(name, bit, index) \
4279 DEFINE_ACTION_BOOL(name, bit); \
4282 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4285 checked = !strcmp(action->string[index], value); \
4289 checked = is_default; \
4291 mapped_param |= checked; \
4292 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4294 return JB_ERR_MEMORY; \
4297 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4298 if (map(exports, name "-param-custom", 1, \
4299 ((!mapped_param) ? "checked" : ""), 1)) \
4301 return JB_ERR_MEMORY; \
4303 if (map(exports, name "-param", 1, \
4304 (((add & bit) && !mapped_param) ? \
4305 action->string[index] : default_val), 1)) \
4307 return JB_ERR_MEMORY; \
4310 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4311 if (map(exports, name "-param", 1, \
4312 ((add & bit) ? action->string[index] : default_val), 1)) \
4314 return JB_ERR_MEMORY; \
4317 #define DEFINE_ACTION_MULTI(name, index) \
4318 if (action->multi_add[index]->first) \
4320 current_mode = 'y'; \
4322 else if (action->multi_remove_all[index]) \
4324 current_mode = 'n'; \
4326 else if (action->multi_remove[index]->first) \
4328 current_mode = 'y'; \
4332 current_mode = 'x'; \
4334 if (map_radio(exports, name, "ynx", current_mode)) \
4336 return JB_ERR_MEMORY; \
4339 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4341 #include "actionlist.h"
4343 #undef DEFINE_ACTION_MULTI
4344 #undef DEFINE_ACTION_STRING
4345 #undef DEFINE_ACTION_BOOL
4346 #undef DEFINE_ACTION_ALIAS
4347 #undef DEFINE_CGI_PARAM_CUSTOM
4348 #undef DEFINE_CGI_PARAM_RADIO
4349 #undef DEFINE_CGI_PARAM_NO_RADIO
4355 /*********************************************************************
4357 * Function : actions_from_radio
4359 * Description : Converts a map of parameters passed to a CGI function
4360 * into an actionsfile entry.
4363 * 1 : parameters = parameters to the CGI call
4364 * 2 : action = Action to change. Must be valid before
4365 * the call, actions not specified will be
4368 * Returns : JB_ERR_OK on success
4369 * JB_ERR_MEMORY on out-of-memory
4371 *********************************************************************/
4372 static jb_err actions_from_radio(const struct map * parameters,
4373 struct action_spec *action)
4375 static int first_time = 1;
4379 const char * js_name;
4380 jb_err err = JB_ERR_OK;
4385 /* Statics are generally a potential race condition,
4386 * but in this case we're safe and don't need semaphores.
4387 * Be careful if you modify this function.
4391 #define JAVASCRIPTIFY(dest_var, string) \
4393 static char js_name_arr[] = string; \
4396 javascriptify(js_name_arr); \
4398 dest_var = js_name_arr; \
4401 #define DEFINE_ACTION_BOOL(name, bit) \
4402 JAVASCRIPTIFY(js_name, name); \
4403 ch = get_char_param(parameters, js_name); \
4406 action->add |= bit; \
4407 action->mask |= bit; \
4409 else if (ch == 'N') \
4411 action->add &= ~bit; \
4412 action->mask &= ~bit; \
4414 else if (ch == 'X') \
4416 action->add &= ~bit; \
4417 action->mask |= bit; \
4420 #define DEFINE_ACTION_STRING(name, bit, index) \
4421 JAVASCRIPTIFY(js_name, name); \
4422 ch = get_char_param(parameters, js_name); \
4426 JAVASCRIPTIFY(js_name, name "-mode"); \
4427 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4428 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4430 JAVASCRIPTIFY(js_name, name "-param"); \
4431 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4433 if (param != NULL) \
4435 if (NULL == (param_dup = strdup(param))) \
4437 return JB_ERR_MEMORY; \
4439 freez(action->string[index]); \
4440 action->add |= bit; \
4441 action->mask |= bit; \
4442 action->string[index] = param_dup; \
4445 else if (ch == 'N') \
4447 if (action->add & bit) \
4449 freez(action->string[index]); \
4451 action->add &= ~bit; \
4452 action->mask &= ~bit; \
4454 else if (ch == 'X') \
4456 if (action->add & bit) \
4458 freez(action->string[index]); \
4460 action->add &= ~bit; \
4461 action->mask |= bit; \
4464 #define DEFINE_ACTION_MULTI(name, index) \
4465 JAVASCRIPTIFY(js_name, name); \
4466 ch = get_char_param(parameters, js_name); \
4471 else if (ch == 'N') \
4473 list_remove_all(action->multi_add[index]); \
4474 list_remove_all(action->multi_remove[index]); \
4475 action->multi_remove_all[index] = 1; \
4477 else if (ch == 'X') \
4479 list_remove_all(action->multi_add[index]); \
4480 list_remove_all(action->multi_remove[index]); \
4481 action->multi_remove_all[index] = 0; \
4484 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4486 #include "actionlist.h"
4488 #undef DEFINE_ACTION_MULTI
4489 #undef DEFINE_ACTION_STRING
4490 #undef DEFINE_ACTION_BOOL
4491 #undef DEFINE_ACTION_ALIAS
4492 #undef JAVASCRIPTIFY
4500 #endif /* def FEATURE_CGI_EDIT_ACTIONS */