1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.35 2002/04/26 21:50:02 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
10 * NOTE: The CGIs in this file use parameter names
11 * such as "f" and "s" which are really *BAD* choices.
12 * However, I'm trying to save bytes in the
13 * edit-actions-list HTML page - the standard actions
14 * file generated a 550kbyte page, which is ridiculous.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001 the SourceForge
19 * Privoxy team. http://www.privoxy.org/
21 * Based on the Internet Junkbuster originally written
22 * by and Copyright (C) 1997 Anonymous Coders and
23 * Junkbusters Corporation. http://www.junkbusters.com
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General
27 * Public License as published by the Free Software
28 * Foundation; either version 2 of the License, or (at
29 * your option) any later version.
31 * This program is distributed in the hope that it will
32 * be useful, but WITHOUT ANY WARRANTY; without even the
33 * implied warranty of MERCHANTABILITY or FITNESS FOR A
34 * PARTICULAR PURPOSE. See the GNU General Public
35 * License for more details.
37 * The GNU General Public License should be included with
38 * this file. If not, you can view it at
39 * http://www.gnu.org/copyleft/gpl.html
40 * or write to the Free Software Foundation, Inc., 59
41 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 * Revision 1.35 2002/04/26 21:50:02 jongfoster
46 * Honouring default exports in edit-actions-for-url-filter template.
48 * Revision 1.34 2002/04/26 12:54:17 oes
49 * Adaptions to changes in actions.c
51 * Revision 1.33 2002/04/24 02:17:47 oes
52 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
54 * - Activated Jon's code for editing multiple AFs
55 * - cgi_edit_list_actions now provides context-sensitive
56 * help, looks up all action sets from standard.action and
57 * makes buttons for them in the catchall section
58 * - cgi_edit_action_submit now honors a p parameter, looks up
59 * the corresponding action set, and sets the catchall pattern's
60 * actions accordingly.
62 * Revision 1.32 2002/04/19 16:55:31 jongfoster
63 * Fixing newline problems. If we do our own text file newline
64 * mangling, we don't want the library to do any, so we need to
65 * open the files in *binary* mode.
67 * Revision 1.31 2002/04/18 19:21:08 jongfoster
68 * Added code to detect "conventional" action files, that start
69 * with a set of actions for all URLs (the pattern "/").
70 * These are special-cased in the "edit-actions-list" CGI, so
71 * that a special UI can be written for them.
73 * Revision 1.30 2002/04/10 13:38:35 oes
74 * load_template signature changed
76 * Revision 1.29 2002/04/08 16:59:08 oes
79 * Revision 1.28 2002/03/27 12:30:29 oes
80 * Deleted unsused variable
82 * Revision 1.27 2002/03/26 23:06:04 jongfoster
83 * Removing duplicate @ifs on the toggle page
85 * Revision 1.26 2002/03/26 22:59:17 jongfoster
86 * Fixing /toggle to display status consistently.
88 * Revision 1.25 2002/03/26 22:29:54 swa
89 * we have a new homepage!
91 * Revision 1.24 2002/03/24 15:23:33 jongfoster
94 * Revision 1.23 2002/03/24 13:32:41 swa
95 * name change related issues
97 * Revision 1.22 2002/03/24 13:25:43 swa
98 * name change related issues
100 * Revision 1.21 2002/03/22 18:02:48 jongfoster
101 * Fixing remote toggle
103 * Revision 1.20 2002/03/16 20:28:34 oes
104 * Added descriptions to the filters so users will know what they select in the cgi editor
106 * Revision 1.19 2002/03/16 18:38:14 jongfoster
107 * Stopping stupid or malicious users from breaking the actions
108 * file using the web-based editor.
110 * Revision 1.18 2002/03/16 14:57:44 jongfoster
111 * Full support for enabling/disabling modular filters.
113 * Revision 1.17 2002/03/16 14:26:42 jongfoster
114 * First version of modular filters support - READ ONLY!
115 * Fixing a double-free bug in the out-of-memory handling in map_radio().
117 * Revision 1.16 2002/03/07 03:46:17 oes
118 * Fixed compiler warnings
120 * Revision 1.15 2002/03/06 22:54:35 jongfoster
121 * Automated function-comment nitpicking.
123 * Revision 1.14 2002/03/05 00:24:51 jongfoster
124 * Patch to always edit the current actions file.
126 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
127 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
129 * Revision 1.12 2002/03/03 09:18:03 joergs
130 * Made jumbjuster work on AmigaOS again.
132 * Revision 1.11 2002/01/23 01:03:31 jongfoster
133 * Fixing gcc [CygWin] compiler warnings
135 * Revision 1.10 2002/01/23 00:22:59 jongfoster
136 * Adding new function cgi_edit_actions_section_swap(), to reorder
139 * Adding get_url_spec_param() to get a validated URL pattern.
141 * Moving edit_read_line() out of this file and into loaders.c.
143 * Adding missing html_encode() to many CGI functions.
145 * Moving the functions that #include actionlist.h to the end of the file,
146 * because the Visual C++ 97 debugger gets extremely confused if you try
147 * to debug any code that comes after them in the file.
149 * Major optimizations in cgi_edit_actions_list() to reduce the size of
150 * the generated HTML (down 40% from 550k to 304k), with major side-effects
151 * throughout the editor and templates. In particular, the length of the
152 * URLs throughout the editor has been drastically reduced, by cutting
153 * paramater names down to 1 character and CGI names down to 3-4
154 * characters, by removing all non-essential CGI paramaters even at the
155 * expense of having to re-read the actions file for the most trivial
156 * page, and by using relative rather than absolute URLs. This means
157 * that this (typical example):
159 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
160 * filename=ijb&ver=1011487572&section=12&pattern=13
161 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
165 * <a href="eau?f=ijb&v=1011487572&p=13">
167 * Revision 1.9 2002/01/17 20:56:22 jongfoster
168 * Replacing hard references to the URL of the config interface
169 * with #defines from project.h
171 * Revision 1.8 2001/11/30 23:35:51 jongfoster
172 * Renaming actionsfile to ijb.action
174 * Revision 1.7 2001/11/13 00:28:24 jongfoster
175 * - Renaming parameters from edit-actions-for-url so that they only
176 * contain legal JavaScript characters. If we wanted to write
177 * JavaScript that worked with Netscape 4, this is nessacery.
178 * (Note that at the moment the JavaScript doesn't actually work
179 * with Netscape 4, but now this is purely a template issue, not
180 * one affecting code).
181 * - Adding new CGIs for use by non-JavaScript browsers:
182 * edit-actions-url-form
183 * edit-actions-add-url-form
184 * edit-actions-remove-url-form
187 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
188 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
189 * by and __OS2__ ifdef.
191 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
192 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
193 * threads to call select() simultaneously. So, it's time to do a real, live,
194 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
195 * (native). Both versions will work, but using __OS2__ offers multi-threading.
197 * Revision 1.4 2001/10/23 21:48:19 jongfoster
198 * Cleaning up error handling in CGI functions - they now send back
199 * a HTML error page and should never cause a FATAL error. (Fixes one
200 * potential source of "denial of service" attacks).
202 * CGI actions file editor that works and is actually useful.
204 * Ability to toggle JunkBuster remotely using a CGI call.
206 * You can turn off both the above features in the main configuration
207 * file, e.g. if you are running a multi-user proxy.
209 * Revision 1.3 2001/10/14 22:12:49 jongfoster
210 * New version of CGI-based actionsfile editor.
211 * Major changes, including:
212 * - Completely new file parser and file output routines
213 * - edit-actions CGI renamed edit-actions-for-url
214 * - All CGIs now need a filename parameter, except for...
215 * - New CGI edit-actions which doesn't need a filename,
216 * to allow you to start the editor up.
217 * - edit-actions-submit now works, and now automatically
218 * redirects you back to the main edit-actions-list handler.
220 * Revision 1.2 2001/09/16 17:05:14 jongfoster
221 * Removing unused #include showarg.h
223 * Revision 1.1 2001/09/16 15:47:37 jongfoster
224 * First version of CGI-based edit interface. This is very much a
225 * work-in-progress, and you can't actually use it to edit anything
226 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
227 * to have any effect.
230 **********************************************************************/
236 * FIXME: Following includes copied from cgi.c - which are actually needed?
241 #include <sys/types.h>
245 #include <sys/stat.h>
248 #define snprintf _snprintf
249 #endif /* def _WIN32 */
254 #include "cgisimple.h"
258 #include "miscutil.h"
262 /* loadcfg.h is for g_bToggleIJB only */
263 #include "urlmatch.h"
265 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
268 #ifdef FEATURE_CGI_EDIT_ACTIONS
272 struct file_line * next;
280 struct action_spec action[1];
289 /* Add more data types here... e.g.
292 struct url_spec url[1];
296 struct action_spec action[1];
305 #define FILE_LINE_UNPROCESSED 1
306 #define FILE_LINE_BLANK 2
307 #define FILE_LINE_ALIAS_HEADER 3
308 #define FILE_LINE_ALIAS_ENTRY 4
309 #define FILE_LINE_ACTION 5
310 #define FILE_LINE_URL 6
311 #define FILE_LINE_SETTINGS_HEADER 7
312 #define FILE_LINE_SETTINGS_ENTRY 8
313 #define FILE_LINE_DESCRIPTION_HEADER 9
314 #define FILE_LINE_DESCRIPTION_ENTRY 10
319 struct file_line * lines;
320 const char * filename; /* Full pathname - e.g. "/etc/privoxy/wibble.action" */
321 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
322 /* Pre-encoded with url_encode() for ease of use. */
323 const char * version_str; /* Last modification time, as a string. For CGI param */
324 /* Can be used in URL without using url_param(). */
325 unsigned version; /* Last modification time - prevents chaos with
326 * the browser's "back" button. Note that this is a
327 * time_t cast to an unsigned. When comparing, always
328 * cast the time_t to an unsigned, and *NOT* vice-versa.
329 * This may lose the top few bits, but they're not
330 * significant anyway.
332 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
333 * Note that changing this after the file has been
334 * read in will cause a mess.
336 struct file_line * parse_error; /* On parse error, this is the offending line. */
337 const char * parse_error_text; /* On parse error, this is the problem.
338 * (Statically allocated) */
341 /* FIXME: Following non-static functions should be prototyped in .h or made static */
343 /* Functions to read and write arbitrary config files */
344 jb_err edit_read_file(struct client_state *csp,
345 const struct map *parameters,
348 struct editable_file **pfile);
349 jb_err edit_write_file(struct editable_file * file);
350 void edit_free_file(struct editable_file * file);
352 /* Functions to read and write actions files */
353 jb_err edit_parse_actions_file(struct editable_file * file);
354 jb_err edit_read_actions_file(struct client_state *csp,
355 struct http_response *rsp,
356 const struct map *parameters,
358 struct editable_file **pfile);
361 jb_err cgi_error_modified(struct client_state *csp,
362 struct http_response *rsp,
363 const char *filename);
364 jb_err cgi_error_parse(struct client_state *csp,
365 struct http_response *rsp,
366 struct editable_file *file);
367 jb_err cgi_error_file(struct client_state *csp,
368 struct http_response *rsp,
369 const char *filename);
370 jb_err cgi_error_disabled(struct client_state *csp,
371 struct http_response *rsp);
373 /* Internal arbitrary config file support functions */
374 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
375 static void edit_free_file_lines(struct file_line * first_line);
377 /* Internal actions file support functions */
378 static int match_actions_file_header_line(const char * line, const char * name);
379 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
381 /* Internal parameter parsing functions */
382 static jb_err get_file_name_param(struct client_state *csp,
383 const struct map *parameters,
384 const char *param_name,
387 const char **pparam);
389 static jb_err get_url_spec_param(struct client_state *csp,
390 const struct map *parameters,
395 /* Internal actionsfile <==> HTML conversion functions */
396 static jb_err map_radio(struct map * exports,
397 const char * optionname,
400 static jb_err actions_to_radio(struct map * exports,
401 const struct action_spec *action);
402 static jb_err actions_from_radio(const struct map * parameters,
403 struct action_spec *action);
406 static jb_err map_copy_parameter_html(struct map *out,
407 const struct map *in,
409 #if 0 /* unused function */
410 static jb_err map_copy_parameter_url(struct map *out,
411 const struct map *in,
413 #endif /* unused function */
415 /*********************************************************************
417 * Function : map_copy_parameter_html
419 * Description : Copy a CGI parameter from one map to another, HTML
423 * 1 : out = target map
424 * 2 : in = source map
425 * 3 : name = name of cgi parameter to copy
427 * Returns : JB_ERR_OK on success
428 * JB_ERR_MEMORY on out-of-memory
429 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
432 *********************************************************************/
433 static jb_err map_copy_parameter_html(struct map *out,
434 const struct map *in,
444 value = lookup(in, name);
445 err = map(out, name, 1, html_encode(value), 0);
452 else if (*value == '\0')
454 return JB_ERR_CGI_PARAMS;
463 #if 0 /* unused function */
464 /*********************************************************************
466 * Function : map_copy_parameter_url
468 * Description : Copy a CGI parameter from one map to another, URL
472 * 1 : out = target map
473 * 2 : in = source map
474 * 3 : name = name of cgi parameter to copy
476 * Returns : JB_ERR_OK on success
477 * JB_ERR_MEMORY on out-of-memory
478 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
481 *********************************************************************/
482 static jb_err map_copy_parameter_url(struct map *out,
483 const struct map *in,
493 value = lookup(in, name);
494 err = map(out, name, 1, url_encode(value), 0);
501 else if (*value == '\0')
503 return JB_ERR_CGI_PARAMS;
510 #endif /* 0 - unused function */
512 /*********************************************************************
514 * Function : cgi_edit_actions_url_form
516 * Description : CGI function that displays a form for
520 * 1 : csp = Current client state (buffers, headers, etc...)
521 * 2 : rsp = http_response data structure for output
522 * 3 : parameters = map of cgi parameters
525 * f : (filename) Identifies the file to edit
526 * v : (version) File's last-modified time
527 * p : (pattern) Line number of pattern to edit
529 * Returns : JB_ERR_OK on success
530 * JB_ERR_MEMORY on out-of-memory
531 * JB_ERR_CGI_PARAMS if the CGI parameters are not
532 * specified or not valid.
534 *********************************************************************/
535 jb_err cgi_edit_actions_url_form(struct client_state *csp,
536 struct http_response *rsp,
537 const struct map *parameters)
539 struct map * exports;
541 struct editable_file * file;
542 struct file_line * cur_line;
543 unsigned line_number;
550 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
552 return cgi_error_disabled(csp, rsp);
555 err = get_number_param(csp, parameters, "p", &patternid);
561 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
564 /* No filename specified, can't read file, modified, or out of memory. */
565 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
568 cur_line = file->lines;
570 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
572 cur_line = cur_line->next;
575 if ( (cur_line == NULL)
576 || (line_number != patternid)
578 || (cur_line->type != FILE_LINE_URL))
580 /* Invalid "patternid" parameter */
581 edit_free_file(file);
582 return JB_ERR_CGI_PARAMS;
585 if (NULL == (exports = default_exports(csp, NULL)))
587 edit_free_file(file);
588 return JB_ERR_MEMORY;
591 err = map(exports, "f", 1, file->identifier, 1);
592 if (!err) err = map(exports, "v", 1, file->version_str, 1);
593 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
594 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
596 edit_free_file(file);
604 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
608 /*********************************************************************
610 * Function : cgi_edit_actions_add_url_form
612 * Description : CGI function that displays a form for
616 * 1 : csp = Current client state (buffers, headers, etc...)
617 * 2 : rsp = http_response data structure for output
618 * 3 : parameters = map of cgi parameters
621 * f : (filename) Identifies the file to edit
622 * v : (version) File's last-modified time
623 * s : (section) Line number of section to edit
625 * Returns : JB_ERR_OK on success
626 * JB_ERR_MEMORY on out-of-memory
627 * JB_ERR_CGI_PARAMS if the CGI parameters are not
628 * specified or not valid.
630 *********************************************************************/
631 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
632 struct http_response *rsp,
633 const struct map *parameters)
642 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
644 return cgi_error_disabled(csp, rsp);
647 if (NULL == (exports = default_exports(csp, NULL)))
649 return JB_ERR_MEMORY;
652 err = map_copy_parameter_html(exports, parameters, "f");
653 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
654 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
662 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
666 /*********************************************************************
668 * Function : cgi_edit_actions_remove_url_form
670 * Description : CGI function that displays a form for
674 * 1 : csp = Current client state (buffers, headers, etc...)
675 * 2 : rsp = http_response data structure for output
676 * 3 : parameters = map of cgi parameters
679 * f : (filename) Identifies the file to edit
680 * v : (version) File's last-modified time
681 * p : (pattern) Line number of pattern to edit
683 * Returns : JB_ERR_OK on success
684 * JB_ERR_MEMORY on out-of-memory
685 * JB_ERR_CGI_PARAMS if the CGI parameters are not
686 * specified or not valid.
688 *********************************************************************/
689 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
690 struct http_response *rsp,
691 const struct map *parameters)
693 struct map * exports;
695 struct editable_file * file;
696 struct file_line * cur_line;
697 unsigned line_number;
704 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
706 return cgi_error_disabled(csp, rsp);
709 err = get_number_param(csp, parameters, "p", &patternid);
715 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
718 /* No filename specified, can't read file, modified, or out of memory. */
719 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
722 cur_line = file->lines;
724 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
726 cur_line = cur_line->next;
729 if ( (cur_line == NULL)
730 || (line_number != patternid)
732 || (cur_line->type != FILE_LINE_URL))
734 /* Invalid "patternid" parameter */
735 edit_free_file(file);
736 return JB_ERR_CGI_PARAMS;
739 if (NULL == (exports = default_exports(csp, NULL)))
741 edit_free_file(file);
742 return JB_ERR_MEMORY;
745 err = map(exports, "f", 1, file->identifier, 1);
746 if (!err) err = map(exports, "v", 1, file->version_str, 1);
747 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
748 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
750 edit_free_file(file);
758 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
762 /*********************************************************************
764 * Function : edit_write_file
766 * Description : Write a complete file to disk.
769 * 1 : filename = File to write to.
770 * 2 : file = Data structure to write.
772 * Returns : JB_ERR_OK on success
773 * JB_ERR_FILE on error writing to file.
774 * JB_ERR_MEMORY on out of memory
776 *********************************************************************/
777 jb_err edit_write_file(struct editable_file * file)
780 struct file_line * cur_line;
781 struct stat statbuf[1];
782 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
783 digits in time_t, assuming this is a 64-bit
784 machine, plus null terminator, plus one
788 assert(file->filename);
790 if (NULL == (fp = fopen(file->filename, "wb")))
795 cur_line = file->lines;
796 while (cur_line != NULL)
800 if (fputs(cur_line->raw, fp) < 0)
808 if (cur_line->prefix)
810 if (fputs(cur_line->prefix, fp) < 0)
816 if (cur_line->unprocessed)
819 if (NULL != strchr(cur_line->unprocessed, '#'))
821 /* Must quote '#' characters */
828 /* Count number of # characters, so we know length of output string */
829 src = cur_line->unprocessed;
830 while (NULL != (src = strchr(src, '#')))
837 /* Allocate new memory for string */
838 len = strlen(cur_line->unprocessed);
839 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
841 /* Uh oh, just trashed file! */
843 return JB_ERR_MEMORY;
846 /* Loop through string from end */
847 src = cur_line->unprocessed + len;
848 dest = str + len + numhash;
849 for ( ; len >= 0; len--)
851 if ((*dest-- = *src--) == '#')
855 assert(numhash >= 0);
858 assert(numhash == 0);
859 assert(src + 1 == cur_line->unprocessed);
860 assert(dest + 1 == str);
862 if (fputs(str, fp) < 0)
873 /* Can write without quoting '#' characters. */
874 if (fputs(cur_line->unprocessed, fp) < 0)
880 if (fputs(NEWLINE(file->newline), fp) < 0)
888 /* FIXME: Write data from file->data->whatever */
892 cur_line = cur_line->next;
898 /* Update the version stamp in the file structure, since we just
899 * wrote to the file & changed it's date.
901 if (stat(file->filename, statbuf) < 0)
903 /* Error, probably file not found. */
906 file->version = (unsigned)statbuf->st_mtime;
908 /* Correct file->version_str */
909 freez(file->version_str);
910 snprintf(version_buf, 22, "%u", file->version);
911 version_buf[21] = '\0';
912 file->version_str = strdup(version_buf);
913 if (version_buf == NULL)
915 return JB_ERR_MEMORY;
922 /*********************************************************************
924 * Function : edit_free_file
926 * Description : Free a complete file in memory.
929 * 1 : file = Data structure to free.
933 *********************************************************************/
934 void edit_free_file(struct editable_file * file)
938 /* Silently ignore NULL pointer */
942 edit_free_file_lines(file->lines);
943 freez(file->filename);
944 freez(file->identifier);
945 freez(file->version_str);
947 file->parse_error_text = NULL; /* Statically allocated */
948 file->parse_error = NULL;
954 /*********************************************************************
956 * Function : edit_free_file_lines
958 * Description : Free an entire linked list of file lines.
961 * 1 : first_line = Data structure to free.
965 *********************************************************************/
966 static void edit_free_file_lines(struct file_line * first_line)
968 struct file_line * next_line;
970 while (first_line != NULL)
972 next_line = first_line->next;
973 first_line->next = NULL;
974 freez(first_line->raw);
975 freez(first_line->prefix);
976 freez(first_line->unprocessed);
977 switch(first_line->type)
979 case 0: /* special case if memory zeroed */
980 case FILE_LINE_UNPROCESSED:
981 case FILE_LINE_BLANK:
982 case FILE_LINE_ALIAS_HEADER:
983 case FILE_LINE_SETTINGS_HEADER:
984 case FILE_LINE_DESCRIPTION_HEADER:
985 case FILE_LINE_DESCRIPTION_ENTRY:
986 case FILE_LINE_ALIAS_ENTRY:
988 /* No data is stored for these */
991 case FILE_LINE_ACTION:
992 free_action(first_line->data.action);
995 case FILE_LINE_SETTINGS_ENTRY:
996 freez(first_line->data.setting.name);
997 freez(first_line->data.setting.svalue);
1000 /* Should never happen */
1004 first_line->type = 0; /* paranoia */
1006 first_line = next_line;
1011 /*********************************************************************
1013 * Function : match_actions_file_header_line
1015 * Description : Match an actions file {{header}} line
1018 * 1 : line = String from file
1019 * 2 : name = Header to match against
1021 * Returns : 0 iff they match.
1023 *********************************************************************/
1024 static int match_actions_file_header_line(const char * line, const char * name)
1032 if ((line[0] != '{') || (line[1] != '{'))
1038 /* Look for optional whitespace */
1039 while ( (*line == ' ') || (*line == '\t') )
1044 /* Look for the specified name (case-insensitive) */
1046 if (0 != strncmpic(line, name, len))
1052 /* Look for optional whitespace */
1053 while ( (*line == ' ') || (*line == '\t') )
1058 /* Look for "}}" and end of string*/
1059 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1069 /*********************************************************************
1071 * Function : match_actions_file_header_line
1073 * Description : Match an actions file {{header}} line
1076 * 1 : line = String from file. Must not start with
1077 * whitespace (else infinite loop!)
1078 * 2 : name = Destination for name
1079 * 2 : name = Destination for value
1081 * Returns : JB_ERR_OK on success
1082 * JB_ERR_MEMORY on out-of-memory
1083 * JB_ERR_PARSE if there's no "=" sign, or if there's
1084 * nothing before the "=" sign (but empty
1085 * values *after* the "=" sign are legal).
1087 *********************************************************************/
1088 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1090 const char * name_end;
1091 const char * value_start;
1097 assert(*line != ' ');
1098 assert(*line != '\t');
1103 value_start = strchr(line, '=');
1104 if ((value_start == NULL) || (value_start == line))
1106 return JB_ERR_PARSE;
1109 name_end = value_start - 1;
1111 /* Eat any whitespace before the '=' */
1112 while ((*name_end == ' ') || (*name_end == '\t'))
1115 * we already know we must have at least 1 non-ws char
1116 * at start of buf - no need to check
1121 name_len = name_end - line + 1; /* Length excluding \0 */
1122 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1124 return JB_ERR_MEMORY;
1126 strncpy(*pname, line, name_len);
1127 (*pname)[name_len] = '\0';
1129 /* Eat any the whitespace after the '=' */
1131 while ((*value_start == ' ') || (*value_start == '\t'))
1136 if (NULL == (*pvalue = strdup(value_start)))
1140 return JB_ERR_MEMORY;
1147 /*********************************************************************
1149 * Function : edit_parse_actions_file
1151 * Description : Parse an actions file in memory.
1153 * Passed linked list must have the "data" member
1154 * zeroed, and must contain valid "next" and
1155 * "unprocessed" fields. The "raw" and "prefix"
1156 * fields are ignored, and "type" is just overwritten.
1158 * Note that on error the file may have been
1162 * 1 : file = Actions file to be parsed in-place.
1164 * Returns : JB_ERR_OK on success
1165 * JB_ERR_MEMORY on out-of-memory
1166 * JB_ERR_PARSE on error
1168 *********************************************************************/
1169 jb_err edit_parse_actions_file(struct editable_file * file)
1171 struct file_line * cur_line;
1173 const char * text; /* Text from a line */
1174 char * name; /* For lines of the form name=value */
1175 char * value; /* For lines of the form name=value */
1176 struct action_alias * alias_list = NULL;
1177 jb_err err = JB_ERR_OK;
1179 /* alias_list contains the aliases defined in this file.
1180 * It might be better to use the "file_line.data" fields
1181 * in the relavent places instead.
1184 cur_line = file->lines;
1186 /* A note about blank line support: Blank lines should only
1187 * ever occur as the last line in the file. This function
1188 * is more forgiving than that - FILE_LINE_BLANK can occur
1192 /* Skip leading blanks. Should only happen if file is
1193 * empty (which is valid, but pointless).
1195 while ( (cur_line != NULL)
1196 && (cur_line->unprocessed[0] == '\0') )
1199 cur_line->type = FILE_LINE_BLANK;
1200 cur_line = cur_line->next;
1203 if ( (cur_line != NULL)
1204 && (cur_line->unprocessed[0] != '{') )
1206 /* File doesn't start with a header */
1207 file->parse_error = cur_line;
1208 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1209 return JB_ERR_PARSE;
1212 if ( (cur_line != NULL) && (0 ==
1213 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1215 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1217 cur_line = cur_line->next;
1218 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1220 if (cur_line->unprocessed[0])
1222 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1224 err = split_line_on_equals(cur_line->unprocessed,
1225 &cur_line->data.setting.name,
1226 &cur_line->data.setting.svalue);
1227 if (err == JB_ERR_MEMORY)
1231 else if (err != JB_ERR_OK)
1233 /* Line does not contain a name=value pair */
1234 file->parse_error = cur_line;
1235 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1236 return JB_ERR_PARSE;
1241 cur_line->type = FILE_LINE_BLANK;
1243 cur_line = cur_line->next;
1247 if ( (cur_line != NULL) && (0 ==
1248 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1250 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1252 cur_line = cur_line->next;
1253 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1255 if (cur_line->unprocessed[0])
1257 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1261 cur_line->type = FILE_LINE_BLANK;
1263 cur_line = cur_line->next;
1267 if ( (cur_line != NULL) && (0 ==
1268 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1270 cur_line->type = FILE_LINE_ALIAS_HEADER;
1272 cur_line = cur_line->next;
1273 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1275 if (cur_line->unprocessed[0])
1277 /* define an alias */
1278 struct action_alias * new_alias;
1280 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1282 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1283 if (err == JB_ERR_MEMORY)
1287 else if (err != JB_ERR_OK)
1289 /* Line does not contain a name=value pair */
1290 file->parse_error = cur_line;
1291 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1292 return JB_ERR_PARSE;
1295 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1300 free_alias_list(alias_list);
1301 return JB_ERR_MEMORY;
1304 err = get_actions(value, alias_list, new_alias->action);
1307 /* Invalid action or out of memory */
1311 free_alias_list(alias_list);
1312 if (err == JB_ERR_MEMORY)
1318 /* Line does not contain a name=value pair */
1319 file->parse_error = cur_line;
1320 file->parse_error_text = "This alias does not specify a valid set of actions.";
1321 return JB_ERR_PARSE;
1327 new_alias->name = name;
1330 new_alias->next = alias_list;
1331 alias_list = new_alias;
1335 cur_line->type = FILE_LINE_BLANK;
1337 cur_line = cur_line->next;
1341 /* Header done, process the main part of the file */
1342 while (cur_line != NULL)
1344 /* At this point, (cur_line->unprocessed[0] == '{') */
1345 assert(cur_line->unprocessed[0] == '{');
1346 text = cur_line->unprocessed + 1;
1347 len = strlen(text) - 1;
1348 if (text[len] != '}')
1350 /* No closing } on header */
1351 free_alias_list(alias_list);
1352 file->parse_error = cur_line;
1353 file->parse_error_text = "Headers starting with '{' must have a "
1354 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1355 "must close with two brackets ('}}').";
1356 return JB_ERR_PARSE;
1361 /* An invalid {{ header. */
1362 free_alias_list(alias_list);
1363 file->parse_error = cur_line;
1364 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1365 "Please remember that the system (two-bracket) headers must "
1366 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1367 "and must appear before any actions (one-bracket) headers. "
1368 "Also note that system headers may not be repeated.";
1369 return JB_ERR_PARSE;
1372 while ( (*text == ' ') || (*text == '\t') )
1378 && ( (text[len - 1] == ' ')
1379 || (text[len - 1] == '\t') ) )
1384 cur_line->type = FILE_LINE_ACTION;
1386 /* Remove {} and make copy */
1387 if (NULL == (value = (char *) malloc(len + 1)))
1390 free_alias_list(alias_list);
1391 return JB_ERR_MEMORY;
1393 strncpy(value, text, len);
1397 err = get_actions(value, alias_list, cur_line->data.action);
1400 /* Invalid action or out of memory */
1402 free_alias_list(alias_list);
1403 if (err == JB_ERR_MEMORY)
1409 /* Line does not contain a name=value pair */
1410 file->parse_error = cur_line;
1411 file->parse_error_text = "This header does not specify a valid set of actions.";
1412 return JB_ERR_PARSE;
1416 /* Done with string - it was clobbered anyway */
1419 /* Process next line */
1420 cur_line = cur_line->next;
1422 /* Loop processing URL patterns */
1423 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1425 if (cur_line->unprocessed[0])
1427 /* Could parse URL here, but this isn't currently needed */
1429 cur_line->type = FILE_LINE_URL;
1433 cur_line->type = FILE_LINE_BLANK;
1435 cur_line = cur_line->next;
1437 } /* End main while(cur_line != NULL) loop */
1439 free_alias_list(alias_list);
1445 /*********************************************************************
1447 * Function : edit_read_file_lines
1449 * Description : Read all the lines of a file into memory.
1450 * Handles whitespace, comments and line continuation.
1453 * 1 : fp = File to read from. On return, this will be
1454 * at EOF but it will not have been closed.
1455 * 2 : pfile = Destination for a linked list of file_lines.
1456 * Will be set to NULL on error.
1458 * Returns : JB_ERR_OK on success
1459 * JB_ERR_MEMORY on out-of-memory
1461 *********************************************************************/
1462 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1464 struct file_line * first_line; /* Keep for return value or to free */
1465 struct file_line * cur_line; /* Current line */
1466 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1474 cur_line = first_line = zalloc(sizeof(struct file_line));
1475 if (cur_line == NULL)
1477 return JB_ERR_MEMORY;
1480 cur_line->type = FILE_LINE_UNPROCESSED;
1482 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1485 /* Out of memory or empty file. */
1486 /* Note that empty file is not an error we propogate up */
1488 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1493 prev_line = cur_line;
1494 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1495 if (cur_line == NULL)
1498 edit_free_file_lines(first_line);
1499 return JB_ERR_MEMORY;
1502 cur_line->type = FILE_LINE_UNPROCESSED;
1504 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1505 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1508 edit_free_file_lines(first_line);
1509 return JB_ERR_MEMORY;
1513 while (rval != JB_ERR_FILE);
1517 /* We allocated one too many - free it */
1518 prev_line->next = NULL;
1521 *pfile = first_line;
1526 /*********************************************************************
1528 * Function : edit_read_file
1530 * Description : Read a complete file into memory.
1531 * Handles CGI parameter parsing. If requested, also
1532 * checks the file's modification timestamp.
1535 * 1 : csp = Current client state (buffers, headers, etc...)
1536 * 2 : parameters = map of cgi parameters.
1537 * 3 : require_version = true to check "ver" parameter.
1538 * 4 : suffix = File extension, e.g. ".action".
1539 * 5 : pfile = Destination for the file. Will be set
1543 * filename : The name of the file to read, without the
1544 * path or ".action" extension.
1545 * ver : (Only if require_version is nonzero)
1546 * Timestamp of the actions file. If wrong, this
1547 * function fails with JB_ERR_MODIFIED.
1549 * Returns : JB_ERR_OK on success
1550 * JB_ERR_MEMORY on out-of-memory
1551 * JB_ERR_CGI_PARAMS if "filename" was not specified
1553 * JB_ERR_FILE if the file cannot be opened or
1555 * JB_ERR_MODIFIED if version checking was requested and
1556 * failed - the file was modified outside
1557 * of this CGI editor instance.
1559 *********************************************************************/
1560 jb_err edit_read_file(struct client_state *csp,
1561 const struct map *parameters,
1562 int require_version,
1564 struct editable_file **pfile)
1566 struct file_line * lines;
1570 const char * identifier;
1571 struct editable_file * file;
1572 unsigned version = 0;
1573 struct stat statbuf[1];
1574 char version_buf[22];
1575 int newline = NEWLINE_UNKNOWN;
1583 err = get_file_name_param(csp, parameters, "f", suffix,
1584 &filename, &identifier);
1590 if (stat(filename, statbuf) < 0)
1592 /* Error, probably file not found. */
1596 version = (unsigned) statbuf->st_mtime;
1598 if (require_version)
1600 unsigned specified_version;
1601 err = get_number_param(csp, parameters, "v", &specified_version);
1608 if (version != specified_version)
1610 return JB_ERR_MODIFIED;
1614 if (NULL == (fp = fopen(filename,"rb")))
1620 err = edit_read_file_lines(fp, &lines, &newline);
1630 file = (struct editable_file *) zalloc(sizeof(*file));
1634 edit_free_file_lines(lines);
1638 file->lines = lines;
1639 file->newline = newline;
1640 file->filename = filename;
1641 file->version = version;
1642 file->identifier = url_encode(identifier);
1644 if (file->identifier == NULL)
1646 edit_free_file(file);
1647 return JB_ERR_MEMORY;
1650 /* Correct file->version_str */
1651 freez(file->version_str);
1652 snprintf(version_buf, 22, "%u", file->version);
1653 version_buf[21] = '\0';
1654 file->version_str = strdup(version_buf);
1655 if (version_buf == NULL)
1657 edit_free_file(file);
1658 return JB_ERR_MEMORY;
1666 /*********************************************************************
1668 * Function : edit_read_actions_file
1670 * Description : Read a complete actions file into memory.
1671 * Handles CGI parameter parsing. If requested, also
1672 * checks the file's modification timestamp.
1674 * If this function detects an error in the categories
1675 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1676 * then it handles it by filling in the specified
1677 * response structure and returning JB_ERR_FILE.
1680 * 1 : csp = Current client state (buffers, headers, etc...)
1681 * 2 : rsp = HTTP response. Only filled in on error.
1682 * 2 : parameters = map of cgi parameters.
1683 * 3 : require_version = true to check "ver" parameter.
1684 * 4 : pfile = Destination for the file. Will be set
1688 * filename : The name of the actions file to read, without the
1689 * path or ".action" extension.
1690 * ver : (Only if require_version is nonzero)
1691 * Timestamp of the actions file. If wrong, this
1692 * function fails with JB_ERR_MODIFIED.
1694 * Returns : JB_ERR_OK on success
1695 * JB_ERR_MEMORY on out-of-memory
1696 * JB_ERR_CGI_PARAMS if "filename" was not specified
1698 * JB_ERR_FILE if the file does not contain valid data,
1699 * or if file cannot be opened or
1700 * contains no data, or if version
1701 * checking was requested and failed.
1703 *********************************************************************/
1704 jb_err edit_read_actions_file(struct client_state *csp,
1705 struct http_response *rsp,
1706 const struct map *parameters,
1707 int require_version,
1708 struct editable_file **pfile)
1711 struct editable_file *file;
1719 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1722 /* Try to handle if possible */
1723 if (err == JB_ERR_FILE)
1725 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1727 else if (err == JB_ERR_MODIFIED)
1729 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1731 if (err == JB_ERR_OK)
1734 * Signal to higher-level CGI code that there was a problem but we
1735 * handled it, they should just return JB_ERR_OK.
1742 err = edit_parse_actions_file(file);
1745 if (err == JB_ERR_PARSE)
1747 err = cgi_error_parse(csp, rsp, file);
1748 if (err == JB_ERR_OK)
1751 * Signal to higher-level CGI code that there was a problem but we
1752 * handled it, they should just return JB_ERR_OK.
1757 edit_free_file(file);
1766 /*********************************************************************
1768 * Function : get_file_name_param
1770 * Description : Get the name of the file to edit from the parameters
1771 * passed to a CGI function. This function handles
1772 * security checks such as blocking urls containing
1773 * "/" or ".", prepending the config file directory,
1774 * and adding the specified suffix.
1776 * (This is an essential security check, otherwise
1777 * users may be able to pass "../../../etc/passwd"
1778 * and overwrite the password file [linux], "prn:"
1779 * and print random data [Windows], etc...)
1781 * This function only allows filenames contining the
1782 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1783 * That's probably too restrictive but at least it's
1787 * 1 : csp = Current client state (buffers, headers, etc...)
1788 * 2 : parameters = map of cgi parameters
1789 * 3 : param_name = The name of the parameter to read
1790 * 4 : suffix = File extension, e.g. ".actions"
1791 * 5 : pfilename = destination for full filename. Caller
1792 * free()s. Set to NULL on error.
1793 * 6 : pparam = destination for partial filename,
1794 * suitable for use in another URL. Allocated as part
1795 * of the map "parameters", so don't free it.
1796 * Set to NULL if not specified.
1798 * Returns : JB_ERR_OK on success
1799 * JB_ERR_MEMORY on out-of-memory
1800 * JB_ERR_CGI_PARAMS if "filename" was not specified
1803 *********************************************************************/
1804 static jb_err get_file_name_param(struct client_state *csp,
1805 const struct map *parameters,
1806 const char *param_name,
1809 const char **pparam)
1827 param = lookup(parameters, param_name);
1830 return JB_ERR_CGI_PARAMS;
1835 len = strlen(param);
1836 if (len >= FILENAME_MAX)
1839 return JB_ERR_CGI_PARAMS;
1842 /* Check every character to see if it's legal */
1844 while ((ch = *s++) != '\0')
1846 if ( ((ch < 'A') || (ch > 'Z'))
1847 && ((ch < 'a') || (ch > 'z'))
1848 && ((ch < '0') || (ch > '9'))
1852 /* Probable hack attempt. */
1853 return JB_ERR_CGI_PARAMS;
1857 /* Append extension */
1858 name = malloc(len + strlen(suffix) + 1);
1861 return JB_ERR_MEMORY;
1863 strcpy(name, param);
1864 strcpy(name + len, suffix);
1867 fullpath = make_path(csp->config->confdir, name);
1870 if (fullpath == NULL)
1872 return JB_ERR_MEMORY;
1876 *pfilename = fullpath;
1882 /*********************************************************************
1884 * Function : get_url_spec_param
1886 * Description : Get a URL pattern from the parameters
1887 * passed to a CGI function. Removes leading/trailing
1888 * spaces and validates it.
1891 * 1 : csp = Current client state (buffers, headers, etc...)
1892 * 2 : parameters = map of cgi parameters
1893 * 3 : name = Name of CGI parameter to read
1894 * 4 : pvalue = destination for value. Will be malloc()'d.
1895 * Set to NULL on error.
1897 * Returns : JB_ERR_OK on success
1898 * JB_ERR_MEMORY on out-of-memory
1899 * JB_ERR_CGI_PARAMS if the parameter was not specified
1902 *********************************************************************/
1903 static jb_err get_url_spec_param(struct client_state *csp,
1904 const struct map *parameters,
1908 const char *orig_param;
1911 struct url_spec compiled[1];
1921 orig_param = lookup(parameters, name);
1924 return JB_ERR_CGI_PARAMS;
1927 /* Copy and trim whitespace */
1928 param = strdup(orig_param);
1931 return JB_ERR_MEMORY;
1935 /* Must be non-empty, and can't allow 1st character to be '{' */
1936 if (param[0] == '\0' || param[0] == '{')
1939 return JB_ERR_CGI_PARAMS;
1942 /* Check for embedded newlines */
1943 for (s = param; *s != '\0'; s++)
1945 if ((*s == '\r') || (*s == '\n'))
1948 return JB_ERR_CGI_PARAMS;
1952 /* Check that regex is valid */
1957 return JB_ERR_MEMORY;
1959 err = create_url_spec(compiled, s);
1964 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1966 free_url_spec(compiled);
1968 if (param[strlen(param) - 1] == '\\')
1971 * Must protect trailing '\\' from becoming line continuation character.
1972 * Two methods: 1) If it's a domain only, add a trailing '/'.
1973 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1975 if (strchr(param, '/') == NULL)
1977 err = string_append(¶m, "/");
1981 err = string_append(¶m, "(?:)");
1988 /* Check that the modified regex is valid */
1993 return JB_ERR_MEMORY;
1995 err = create_url_spec(compiled, s);
2000 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2002 free_url_spec(compiled);
2009 /*********************************************************************
2011 * Function : map_radio
2013 * Description : Map a set of radio button values. E.g. if you have
2014 * 3 radio buttons, declare them as:
2015 * <option type="radio" name="xyz" @xyz-a@>
2016 * <option type="radio" name="xyz" @xyz-b@>
2017 * <option type="radio" name="xyz" @xyz-c@>
2018 * Then map one of the @xyz-?@ variables to "checked"
2019 * and all the others to empty by calling:
2020 * map_radio(exports, "xyz", "abc", sel)
2021 * Where 'sel' is 'a', 'b', or 'c'.
2024 * 1 : exports = Exports map to modify.
2025 * 2 : optionname = name for map
2026 * 3 : values = null-terminated list of values;
2027 * 4 : value = Selected value.
2029 * CGI Parameters : None
2031 * Returns : JB_ERR_OK on success
2032 * JB_ERR_MEMORY on out-of-memory
2034 *********************************************************************/
2035 static jb_err map_radio(struct map * exports,
2036 const char * optionname,
2037 const char * values,
2049 len = strlen(optionname);
2050 buf = malloc(len + 3);
2053 return JB_ERR_MEMORY;
2056 strcpy(buf, optionname);
2061 while ((c = *values++) != '\0')
2066 if (map(exports, buf, 1, "", 1))
2068 return JB_ERR_MEMORY;
2074 return map(exports, buf, 0, "checked", 1);
2078 /*********************************************************************
2080 * Function : cgi_error_modified
2082 * Description : CGI function that is called when a file is modified
2083 * outside the CGI editor.
2086 * 1 : csp = Current client state (buffers, headers, etc...)
2087 * 2 : rsp = http_response data structure for output
2088 * 3 : filename = The file that was modified.
2090 * CGI Parameters : none
2092 * Returns : JB_ERR_OK on success
2093 * JB_ERR_MEMORY on out-of-memory error.
2095 *********************************************************************/
2096 jb_err cgi_error_modified(struct client_state *csp,
2097 struct http_response *rsp,
2098 const char *filename)
2100 struct map *exports;
2107 if (NULL == (exports = default_exports(csp, NULL)))
2109 return JB_ERR_MEMORY;
2112 err = map(exports, "f", 1, html_encode(filename), 0);
2119 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2123 /*********************************************************************
2125 * Function : cgi_error_parse
2127 * Description : CGI function that is called when a file cannot
2128 * be parsed by the CGI editor.
2131 * 1 : csp = Current client state (buffers, headers, etc...)
2132 * 2 : rsp = http_response data structure for output
2133 * 3 : file = The file that was modified.
2135 * CGI Parameters : none
2137 * Returns : JB_ERR_OK on success
2138 * JB_ERR_MEMORY on out-of-memory error.
2140 *********************************************************************/
2141 jb_err cgi_error_parse(struct client_state *csp,
2142 struct http_response *rsp,
2143 struct editable_file *file)
2145 struct map *exports;
2147 struct file_line *cur_line;
2153 if (NULL == (exports = default_exports(csp, NULL)))
2155 return JB_ERR_MEMORY;
2158 err = map(exports, "f", 1, file->identifier, 1);
2159 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2161 cur_line = file->parse_error;
2164 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2165 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2173 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2177 /*********************************************************************
2179 * Function : cgi_error_file
2181 * Description : CGI function that is called when a file cannot be
2182 * opened by the CGI editor.
2185 * 1 : csp = Current client state (buffers, headers, etc...)
2186 * 2 : rsp = http_response data structure for output
2187 * 3 : filename = The file that was modified.
2189 * CGI Parameters : none
2191 * Returns : JB_ERR_OK on success
2192 * JB_ERR_MEMORY on out-of-memory error.
2194 *********************************************************************/
2195 jb_err cgi_error_file(struct client_state *csp,
2196 struct http_response *rsp,
2197 const char *filename)
2199 struct map *exports;
2206 if (NULL == (exports = default_exports(csp, NULL)))
2208 return JB_ERR_MEMORY;
2211 err = map(exports, "f", 1, html_encode(filename), 0);
2218 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2222 /*********************************************************************
2224 * Function : cgi_error_disabled
2226 * Description : CGI function that is called if the actions editor
2227 * is called although it's disabled in config
2230 * 1 : csp = Current client state (buffers, headers, etc...)
2231 * 2 : rsp = http_response data structure for output
2233 * CGI Parameters : none
2235 * Returns : JB_ERR_OK on success
2236 * JB_ERR_MEMORY on out-of-memory error.
2238 *********************************************************************/
2239 jb_err cgi_error_disabled(struct client_state *csp,
2240 struct http_response *rsp)
2242 struct map *exports;
2247 if (NULL == (exports = default_exports(csp, NULL)))
2249 return JB_ERR_MEMORY;
2252 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2256 /*********************************************************************
2258 * Function : cgi_edit_actions
2260 * Description : CGI function that allows the user to choose which
2261 * actions file to edit.
2264 * 1 : csp = Current client state (buffers, headers, etc...)
2265 * 2 : rsp = http_response data structure for output
2266 * 3 : parameters = map of cgi parameters
2268 * CGI Parameters : None
2270 * Returns : JB_ERR_OK on success
2271 * JB_ERR_MEMORY on out-of-memory error
2273 *********************************************************************/
2274 jb_err cgi_edit_actions(struct client_state *csp,
2275 struct http_response *rsp,
2276 const struct map *parameters)
2279 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2281 return cgi_error_disabled(csp, rsp);
2284 /* FIXME: Incomplete */
2285 rsp->status = strdup("302 Local Redirect from Privoxy");
2286 if (rsp->status == NULL)
2288 return JB_ERR_MEMORY;
2290 if (enlist_unique_header(rsp->headers, "Location",
2291 CGI_PREFIX "edit-actions-list?f=default"))
2295 return JB_ERR_MEMORY;
2302 /*********************************************************************
2304 * Function : cgi_edit_actions_list
2306 * Description : CGI function that edits the actions list.
2307 * FIXME: This function shouldn't FATAL ever.
2308 * FIXME: This function doesn't check the retval of map()
2310 * 1 : csp = Current client state (buffers, headers, etc...)
2311 * 2 : rsp = http_response data structure for output
2312 * 3 : parameters = map of cgi parameters
2314 * CGI Parameters : filename
2316 * Returns : JB_ERR_OK on success
2317 * JB_ERR_MEMORY on out-of-memory
2318 * JB_ERR_FILE if the file cannot be opened or
2320 * JB_ERR_CGI_PARAMS if "filename" was not specified
2323 *********************************************************************/
2324 jb_err cgi_edit_actions_list(struct client_state *csp,
2325 struct http_response *rsp,
2326 const struct map *parameters)
2328 char * section_template;
2329 char * url_template;
2334 struct map * exports;
2335 struct map * section_exports;
2336 struct map * url_exports;
2337 struct editable_file * file;
2338 struct file_line * cur_line;
2339 unsigned line_number = 0;
2340 unsigned prev_section_line_number = ((unsigned) (-1));
2342 struct file_list * fl;
2343 struct url_actions * b;
2344 char * buttons = NULL;
2347 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2349 return cgi_error_disabled(csp, rsp);
2352 if (NULL == (exports = default_exports(csp, NULL)))
2354 edit_free_file(file);
2355 return JB_ERR_MEMORY;
2358 /* Load actions file */
2359 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2362 /* No filename specified, can't read file, or out of memory. */
2363 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2366 /* Find start of actions in file */
2367 cur_line = file->lines;
2369 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2371 cur_line = cur_line->next;
2376 * Conventional actions files should have a match all block
2378 * cur_line = {...global actions...}
2379 * cur_line->next = /
2380 * cur_line->next->next = {...actions...} or EOF
2382 if ( (cur_line != NULL)
2383 && (cur_line->type == FILE_LINE_ACTION)
2384 && (cur_line->next != NULL)
2385 && (cur_line->next->type == FILE_LINE_URL)
2386 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2387 && ( (cur_line->next->next == NULL)
2388 || (cur_line->next->next->type != FILE_LINE_URL)
2392 * Generate string with buttons to set actions for "/" to
2393 * any predefined set of actions (named standard.*, propably
2394 * residing in standard.action).
2395 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
2398 buttons = strdup(" ");
2399 for (i = 0; i < MAX_ACTION_FILES; i++)
2401 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2403 for (b = b->next; NULL != b; b = b->next)
2405 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2407 snprintf(buf, 150, "<p><a name=\"l@s@\" href=\"eas?f=@f@&v=@v@&s=@all-urls-s@&p=%s\">Set"
2408 " to %s</a></p>", b->url->spec + 9, b->url->spec + 9);
2409 if (!err) err = string_append(&buttons, buf);
2414 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2417 * Conventional actions file, supply extra editing help.
2418 * (e.g. don't allow them to make it an unconventional one).
2420 err = map_conditional(exports, "all-urls-present", 1);
2422 snprintf(buf, 150, "%d", line_number);
2423 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2424 snprintf(buf, 150, "%d", line_number + 2);
2425 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2426 if (!err) err = map(exports, "all-urls-actions", 1,
2427 actions_to_html(cur_line->data.action, csp), 0);
2429 /* Skip the 2 lines */
2430 cur_line = cur_line->next->next;
2434 * Note that prev_section_line_number is NOT set here.
2435 * This is deliberate and not a bug. It stops a "Move up"
2436 * option appearing on the next section. Clicking "Move
2437 * up" would make the actions file unconventional, which
2438 * we don't want, so we hide this option.
2444 * Non-standard actions file - does not begin with
2445 * the "All URLs" section.
2447 err = map_conditional(exports, "all-urls-present", 0);
2450 /* Set up global exports */
2452 err = map(exports, "f", 1, file->identifier, 1);
2453 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2455 /* Discourage private additions to default.action */
2457 if (!err) err = map_conditional(exports, "default-action",
2458 (strcmp("default", lookup(parameters, "f")) == 0));
2461 edit_free_file(file);
2466 /* Should do all global exports above this point */
2468 /* Load templates */
2470 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2473 edit_free_file(file);
2475 if (err == JB_ERR_FILE)
2477 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2482 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2485 free(section_template);
2486 edit_free_file(file);
2488 if (err == JB_ERR_FILE)
2490 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2495 err = template_fill(§ion_template, exports);
2499 edit_free_file(file);
2505 err = template_fill(&url_template, exports);
2508 free(section_template);
2509 edit_free_file(file);
2514 if (NULL == (sections = strdup("")))
2516 free(section_template);
2518 edit_free_file(file);
2520 return JB_ERR_MEMORY;
2523 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2525 if (NULL == (section_exports = new_map()))
2528 free(section_template);
2530 edit_free_file(file);
2532 return JB_ERR_MEMORY;
2535 snprintf(buf, 150, "%d", line_number);
2536 err = map(section_exports, "s", 1, buf, 1);
2537 if (!err) err = map(section_exports, "actions", 1,
2538 actions_to_html(cur_line->data.action, csp), 0);
2541 && (cur_line->next != NULL)
2542 && (cur_line->next->type == FILE_LINE_URL))
2544 /* This section contains at least one URL, don't allow delete */
2545 err = map_block_killer(section_exports, "empty-section");
2549 if (!err) err = map_block_keep(section_exports, "empty-section");
2552 if (prev_section_line_number != ((unsigned)(-1)))
2554 /* Not last section */
2555 snprintf(buf, 150, "%d", prev_section_line_number);
2556 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2557 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2562 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2564 prev_section_line_number = line_number;
2569 free(section_template);
2571 edit_free_file(file);
2573 free_map(section_exports);
2577 /* Should do all section-specific exports above this point */
2579 if (NULL == (urls = strdup("")))
2582 free(section_template);
2584 edit_free_file(file);
2586 free_map(section_exports);
2587 return JB_ERR_MEMORY;
2592 cur_line = cur_line->next;
2595 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2597 if (NULL == (url_exports = new_map()))
2601 free(section_template);
2603 edit_free_file(file);
2605 free_map(section_exports);
2606 return JB_ERR_MEMORY;
2609 snprintf(buf, 150, "%d", line_number);
2610 err = map(url_exports, "p", 1, buf, 1);
2612 snprintf(buf, 150, "%d", url_1_2);
2613 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2615 if (!err) err = map(url_exports, "url-html", 1,
2616 html_encode(cur_line->unprocessed), 0);
2617 if (!err) err = map(url_exports, "url", 1,
2618 url_encode(cur_line->unprocessed), 0);
2624 free(section_template);
2626 edit_free_file(file);
2628 free_map(section_exports);
2629 free_map(url_exports);
2633 if (NULL == (s = strdup(url_template)))
2637 free(section_template);
2639 edit_free_file(file);
2641 free_map(section_exports);
2642 free_map(url_exports);
2643 return JB_ERR_MEMORY;
2646 err = template_fill(&s, section_exports);
2647 if (!err) err = template_fill(&s, url_exports);
2648 if (!err) err = string_append(&urls, s);
2650 free_map(url_exports);
2657 free(section_template);
2659 edit_free_file(file);
2661 free_map(section_exports);
2665 url_1_2 = 3 - url_1_2;
2667 cur_line = cur_line->next;
2671 err = map(section_exports, "urls", 1, urls, 0);
2673 /* Could also do section-specific exports here, but it wouldn't be as fast */
2675 if ( (cur_line != NULL)
2676 && (cur_line->type == FILE_LINE_ACTION))
2678 /* Not last section */
2679 snprintf(buf, 150, "%d", line_number);
2680 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2681 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2686 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2692 free(section_template);
2694 edit_free_file(file);
2696 free_map(section_exports);
2700 if (NULL == (s = strdup(section_template)))
2703 free(section_template);
2705 edit_free_file(file);
2707 free_map(section_exports);
2708 return JB_ERR_MEMORY;
2711 err = template_fill(&s, section_exports);
2712 if (!err) err = string_append(§ions, s);
2715 free_map(section_exports);
2720 free(section_template);
2722 edit_free_file(file);
2728 edit_free_file(file);
2729 free(section_template);
2732 err = map(exports, "sections", 1, sections, 0);
2739 /* Could also do global exports here, but it wouldn't be as fast */
2741 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2745 /*********************************************************************
2747 * Function : cgi_edit_actions_for_url
2749 * Description : CGI function that edits the Actions list.
2752 * 1 : csp = Current client state (buffers, headers, etc...)
2753 * 2 : rsp = http_response data structure for output
2754 * 3 : parameters = map of cgi parameters
2756 * CGI Parameters : None
2758 * Returns : JB_ERR_OK on success
2759 * JB_ERR_MEMORY on out-of-memory
2760 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2761 * specified or not valid.
2763 *********************************************************************/
2764 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2765 struct http_response *rsp,
2766 const struct map *parameters)
2768 struct map * exports;
2770 struct editable_file * file;
2771 struct file_line * cur_line;
2772 unsigned line_number;
2774 struct file_list *filter_file;
2775 struct re_filterfile_spec *filter_group;
2777 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2779 return cgi_error_disabled(csp, rsp);
2782 err = get_number_param(csp, parameters, "s", §ionid);
2788 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2791 /* No filename specified, can't read file, modified, or out of memory. */
2792 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2795 cur_line = file->lines;
2797 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2799 cur_line = cur_line->next;
2802 if ( (cur_line == NULL)
2803 || (line_number != sectionid)
2805 || (cur_line->type != FILE_LINE_ACTION))
2807 /* Invalid "sectionid" parameter */
2808 edit_free_file(file);
2809 return JB_ERR_CGI_PARAMS;
2812 if (NULL == (exports = default_exports(csp, NULL)))
2814 edit_free_file(file);
2815 return JB_ERR_MEMORY;
2818 err = map(exports, "f", 1, file->identifier, 1);
2819 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2820 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2822 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2824 filter_file = csp->rlist;
2825 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2827 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2831 edit_free_file(file);
2836 if (filter_group == NULL)
2838 err = map(exports, "filter-params", 1, "", 1);
2842 /* We have some entries in the filter list */
2845 char * filter_template;
2847 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2850 edit_free_file(file);
2852 if (err == JB_ERR_FILE)
2854 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2859 err = template_fill(&filter_template, exports);
2861 result = strdup("");
2863 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2865 char current_mode = 'x';
2866 struct list_entry *filter_name;
2868 struct map *line_exports;
2871 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2872 while ((filter_name != NULL)
2873 && (0 != strcmp(filter_group->name, filter_name->str)))
2875 filter_name = filter_name->next;
2878 if (filter_name != NULL)
2884 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2885 while ((filter_name != NULL)
2886 && (0 != strcmp(filter_group->name, filter_name->str)))
2888 filter_name = filter_name->next;
2890 if (filter_name != NULL)
2896 /* Generate a unique serial number */
2897 snprintf(number, sizeof(number), "%x", index++);
2898 number[sizeof(number) - 1] = '\0';
2900 line_exports = new_map();
2901 if (line_exports == NULL)
2903 err = JB_ERR_MEMORY;
2908 if (!err) err = map(line_exports, "index", 1, number, 1);
2909 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2910 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2911 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2916 this_line = strdup(filter_template);
2917 if (this_line == NULL) err = JB_ERR_MEMORY;
2919 if (!err) err = template_fill(&this_line, line_exports);
2920 string_join(&result, this_line);
2922 free_map(line_exports);
2926 freez(filter_template);
2930 err = map(exports, "filter-params", 1, result, 0);
2938 if (!err) err = map_radio(exports, "filter-all", "nx",
2939 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
2941 edit_free_file(file);
2949 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2953 /*********************************************************************
2955 * Function : cgi_edit_actions_submit
2957 * Description : CGI function that actually edits the Actions list.
2960 * 1 : csp = Current client state (buffers, headers, etc...)
2961 * 2 : rsp = http_response data structure for output
2962 * 3 : parameters = map of cgi parameters
2964 * CGI Parameters : None
2966 * Returns : JB_ERR_OK on success
2967 * JB_ERR_MEMORY on out-of-memory
2968 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2969 * specified or not valid.
2971 *********************************************************************/
2972 jb_err cgi_edit_actions_submit(struct client_state *csp,
2973 struct http_response *rsp,
2974 const struct map *parameters)
2980 struct editable_file * file;
2981 struct file_line * cur_line;
2982 unsigned line_number;
2986 const char * action_set_name;
2988 struct file_list * fl;
2989 struct url_actions * b;
2991 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2993 return cgi_error_disabled(csp, rsp);
2996 err = get_number_param(csp, parameters, "s", §ionid);
3002 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3005 /* No filename specified, can't read file, modified, or out of memory. */
3006 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3009 cur_line = file->lines;
3011 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3013 cur_line = cur_line->next;
3016 if ( (cur_line == NULL)
3017 || (line_number != sectionid)
3019 || (cur_line->type != FILE_LINE_ACTION))
3021 /* Invalid "sectionid" parameter */
3022 edit_free_file(file);
3023 return JB_ERR_CGI_PARAMS;
3026 get_string_param(parameters, "p", &action_set_name);
3027 if (action_set_name != NULL)
3029 for (index = 0; index < MAX_ACTION_FILES; index++)
3031 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3033 for (b = b->next; NULL != b; b = b->next)
3035 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3037 copy_action(cur_line->data.action, b->action);
3043 edit_free_file(file);
3044 return JB_ERR_CGI_PARAMS;
3050 err = actions_from_radio(parameters, cur_line->data.action);
3056 edit_free_file(file);
3060 ch = get_char_param(parameters, "filter_all");
3063 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3064 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3065 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3069 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3072 for (index = 0; !err; index++)
3079 /* Generate the keys */
3080 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3081 key_value[sizeof(key_value) - 1] = '\0';
3082 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3083 key_name[sizeof(key_name) - 1] = '\0';
3085 err = get_string_param(parameters, key_name, &name);
3094 value = get_char_param(parameters, key_value);
3097 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3098 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3099 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3101 else if (value == 'N')
3103 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3104 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3106 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3107 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3110 else if (value == 'X')
3112 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3113 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3120 edit_free_file(file);
3124 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3127 edit_free_file(file);
3128 return JB_ERR_MEMORY;
3131 len = strlen(actiontext);
3135 * Empty action - must special-case this.
3136 * Simply setting len to 1 is sufficient...
3141 if (NULL == (newtext = malloc(len + 2)))
3145 edit_free_file(file);
3146 return JB_ERR_MEMORY;
3148 strcpy(newtext, actiontext);
3152 newtext[len + 1] = '\0';
3154 freez(cur_line->raw);
3155 freez(cur_line->unprocessed);
3156 cur_line->unprocessed = newtext;
3158 err = edit_write_file(file);
3161 /* Error writing file */
3162 edit_free_file(file);
3166 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3167 string_append(&target, file->identifier);
3169 edit_free_file(file);
3174 return JB_ERR_MEMORY;
3177 rsp->status = strdup("302 Local Redirect from Privoxy");
3178 if (rsp->status == NULL)
3181 return JB_ERR_MEMORY;
3183 err = enlist_unique_header(rsp->headers, "Location", target);
3190 /*********************************************************************
3192 * Function : cgi_edit_actions_url
3194 * Description : CGI function that actually edits a URL pattern in
3198 * 1 : csp = Current client state (buffers, headers, etc...)
3199 * 2 : rsp = http_response data structure for output
3200 * 3 : parameters = map of cgi parameters
3203 * filename : Identifies the file to edit
3204 * ver : File's last-modified time
3205 * section : Line number of section to edit
3206 * pattern : Line number of pattern to edit
3207 * newval : New value for pattern
3209 * Returns : JB_ERR_OK on success
3210 * JB_ERR_MEMORY on out-of-memory
3211 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3212 * specified or not valid.
3214 *********************************************************************/
3215 jb_err cgi_edit_actions_url(struct client_state *csp,
3216 struct http_response *rsp,
3217 const struct map *parameters)
3221 struct editable_file * file;
3222 struct file_line * cur_line;
3223 unsigned line_number;
3227 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3229 return cgi_error_disabled(csp, rsp);
3232 err = get_number_param(csp, parameters, "p", &patternid);
3239 return JB_ERR_CGI_PARAMS;
3242 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3248 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3251 /* No filename specified, can't read file, modified, or out of memory. */
3253 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3257 cur_line = file->lines;
3259 while ((cur_line != NULL) && (line_number < patternid))
3261 cur_line = cur_line->next;
3265 if ( (cur_line == NULL)
3266 || (cur_line->type != FILE_LINE_URL))
3268 /* Invalid "patternid" parameter */
3270 edit_free_file(file);
3271 return JB_ERR_CGI_PARAMS;
3274 /* At this point, the line to edit is in cur_line */
3276 freez(cur_line->raw);
3277 freez(cur_line->unprocessed);
3278 cur_line->unprocessed = new_pattern;
3280 err = edit_write_file(file);
3283 /* Error writing file */
3284 edit_free_file(file);
3288 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3289 string_append(&target, file->identifier);
3291 edit_free_file(file);
3296 return JB_ERR_MEMORY;
3299 rsp->status = strdup("302 Local Redirect from Privoxy");
3300 if (rsp->status == NULL)
3303 return JB_ERR_MEMORY;
3305 err = enlist_unique_header(rsp->headers, "Location", target);
3312 /*********************************************************************
3314 * Function : cgi_edit_actions_add_url
3316 * Description : CGI function that actually adds a URL pattern to
3320 * 1 : csp = Current client state (buffers, headers, etc...)
3321 * 2 : rsp = http_response data structure for output
3322 * 3 : parameters = map of cgi parameters
3325 * filename : Identifies the file to edit
3326 * ver : File's last-modified time
3327 * section : Line number of section to edit
3328 * newval : New pattern
3330 * Returns : JB_ERR_OK on success
3331 * JB_ERR_MEMORY on out-of-memory
3332 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3333 * specified or not valid.
3335 *********************************************************************/
3336 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3337 struct http_response *rsp,
3338 const struct map *parameters)
3342 struct file_line * new_line;
3343 struct editable_file * file;
3344 struct file_line * cur_line;
3345 unsigned line_number;
3349 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3351 return cgi_error_disabled(csp, rsp);
3354 err = get_number_param(csp, parameters, "s", §ionid);
3361 return JB_ERR_CGI_PARAMS;
3364 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3370 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3373 /* No filename specified, can't read file, modified, or out of memory. */
3375 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3379 cur_line = file->lines;
3381 while ((cur_line != NULL) && (line_number < sectionid))
3383 cur_line = cur_line->next;
3387 if ( (cur_line == NULL)
3388 || (cur_line->type != FILE_LINE_ACTION))
3390 /* Invalid "sectionid" parameter */
3392 edit_free_file(file);
3393 return JB_ERR_CGI_PARAMS;
3396 /* At this point, the section header is in cur_line - add after this. */
3398 /* Allocate the new line */
3399 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3400 if (new_line == NULL)
3403 edit_free_file(file);
3404 return JB_ERR_MEMORY;
3407 /* Fill in the data members of the new line */
3408 new_line->raw = NULL;
3409 new_line->prefix = NULL;
3410 new_line->unprocessed = new_pattern;
3411 new_line->type = FILE_LINE_URL;
3413 /* Link new_line into the list, after cur_line */
3414 new_line->next = cur_line->next;
3415 cur_line->next = new_line;
3417 /* Done making changes, now commit */
3419 err = edit_write_file(file);
3422 /* Error writing file */
3423 edit_free_file(file);
3427 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3428 string_append(&target, file->identifier);
3430 edit_free_file(file);
3435 return JB_ERR_MEMORY;
3438 rsp->status = strdup("302 Local Redirect from Privoxy");
3439 if (rsp->status == NULL)
3442 return JB_ERR_MEMORY;
3444 err = enlist_unique_header(rsp->headers, "Location", target);
3451 /*********************************************************************
3453 * Function : cgi_edit_actions_remove_url
3455 * Description : CGI function that actually removes a URL pattern from
3459 * 1 : csp = Current client state (buffers, headers, etc...)
3460 * 2 : rsp = http_response data structure for output
3461 * 3 : parameters = map of cgi parameters
3464 * f : (filename) Identifies the file to edit
3465 * v : (version) File's last-modified time
3466 * p : (pattern) Line number of pattern to remove
3468 * Returns : JB_ERR_OK on success
3469 * JB_ERR_MEMORY on out-of-memory
3470 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3471 * specified or not valid.
3473 *********************************************************************/
3474 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3475 struct http_response *rsp,
3476 const struct map *parameters)
3479 struct editable_file * file;
3480 struct file_line * cur_line;
3481 struct file_line * prev_line;
3482 unsigned line_number;
3486 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3488 return cgi_error_disabled(csp, rsp);
3491 err = get_number_param(csp, parameters, "p", &patternid);
3497 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3500 /* No filename specified, can't read file, modified, or out of memory. */
3501 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3506 cur_line = file->lines;
3508 while ((cur_line != NULL) && (line_number < patternid))
3510 prev_line = cur_line;
3511 cur_line = cur_line->next;
3515 if ( (cur_line == NULL)
3516 || (prev_line == NULL)
3517 || (cur_line->type != FILE_LINE_URL))
3519 /* Invalid "patternid" parameter */
3520 edit_free_file(file);
3521 return JB_ERR_CGI_PARAMS;
3524 /* At this point, the line to remove is in cur_line, and the previous
3525 * one is in prev_line
3528 /* Unlink cur_line */
3529 prev_line->next = cur_line->next;
3530 cur_line->next = NULL;
3533 edit_free_file_lines(cur_line);
3535 err = edit_write_file(file);
3538 /* Error writing file */
3539 edit_free_file(file);
3543 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3544 string_append(&target, file->identifier);
3546 edit_free_file(file);
3551 return JB_ERR_MEMORY;
3554 rsp->status = strdup("302 Local Redirect from Privoxy");
3555 if (rsp->status == NULL)
3558 return JB_ERR_MEMORY;
3560 err = enlist_unique_header(rsp->headers, "Location", target);
3567 /*********************************************************************
3569 * Function : cgi_edit_actions_section_remove
3571 * Description : CGI function that actually removes a whole section from
3572 * the actions file. The section must be empty first
3573 * (else JB_ERR_CGI_PARAMS).
3576 * 1 : csp = Current client state (buffers, headers, etc...)
3577 * 2 : rsp = http_response data structure for output
3578 * 3 : parameters = map of cgi parameters
3581 * f : (filename) Identifies the file to edit
3582 * v : (version) File's last-modified time
3583 * s : (section) Line number of section to edit
3585 * Returns : JB_ERR_OK on success
3586 * JB_ERR_MEMORY on out-of-memory
3587 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3588 * specified or not valid.
3590 *********************************************************************/
3591 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3592 struct http_response *rsp,
3593 const struct map *parameters)
3596 struct editable_file * file;
3597 struct file_line * cur_line;
3598 struct file_line * prev_line;
3599 unsigned line_number;
3603 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3605 return cgi_error_disabled(csp, rsp);
3608 err = get_number_param(csp, parameters, "s", §ionid);
3614 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3617 /* No filename specified, can't read file, modified, or out of memory. */
3618 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3622 cur_line = file->lines;
3625 while ((cur_line != NULL) && (line_number < sectionid))
3627 prev_line = cur_line;
3628 cur_line = cur_line->next;
3632 if ( (cur_line == NULL)
3633 || (cur_line->type != FILE_LINE_ACTION) )
3635 /* Invalid "sectionid" parameter */
3636 edit_free_file(file);
3637 return JB_ERR_CGI_PARAMS;
3640 if ( (cur_line->next != NULL)
3641 && (cur_line->next->type == FILE_LINE_URL) )
3643 /* Section not empty. */
3644 edit_free_file(file);
3645 return JB_ERR_CGI_PARAMS;
3648 /* At this point, the line to remove is in cur_line, and the previous
3649 * one is in prev_line
3652 /* Unlink cur_line */
3653 if (prev_line == NULL)
3655 /* Removing the first line from the file */
3656 file->lines = cur_line->next;
3660 prev_line->next = cur_line->next;
3662 cur_line->next = NULL;
3665 edit_free_file_lines(cur_line);
3667 err = edit_write_file(file);
3670 /* Error writing file */
3671 edit_free_file(file);
3675 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3676 string_append(&target, file->identifier);
3678 edit_free_file(file);
3683 return JB_ERR_MEMORY;
3686 rsp->status = strdup("302 Local Redirect from Privoxy");
3687 if (rsp->status == NULL)
3690 return JB_ERR_MEMORY;
3692 err = enlist_unique_header(rsp->headers, "Location", target);
3699 /*********************************************************************
3701 * Function : cgi_edit_actions_section_add
3703 * Description : CGI function that adds a new empty section to
3707 * 1 : csp = Current client state (buffers, headers, etc...)
3708 * 2 : rsp = http_response data structure for output
3709 * 3 : parameters = map of cgi parameters
3712 * f : (filename) Identifies the file to edit
3713 * v : (version) File's last-modified time
3714 * s : (section) Line number of section to add after, 0 for
3717 * Returns : JB_ERR_OK on success
3718 * JB_ERR_MEMORY on out-of-memory
3719 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3720 * specified or not valid.
3722 *********************************************************************/
3723 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3724 struct http_response *rsp,
3725 const struct map *parameters)
3728 struct file_line * new_line;
3730 struct editable_file * file;
3731 struct file_line * cur_line;
3732 unsigned line_number;
3736 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3738 return cgi_error_disabled(csp, rsp);
3741 err = get_number_param(csp, parameters, "s", §ionid);
3747 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3750 /* No filename specified, can't read file, modified, or out of memory. */
3751 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3755 cur_line = file->lines;
3759 /* Add to start of file */
3760 if (cur_line != NULL)
3762 /* There's something in the file, find the line before the first
3765 while ( (cur_line->next != NULL)
3766 && (cur_line->next->type != FILE_LINE_ACTION) )
3768 cur_line = cur_line->next;
3775 /* Add after stated section. */
3776 while ((cur_line != NULL) && (line_number < sectionid))
3778 cur_line = cur_line->next;
3782 if ( (cur_line == NULL)
3783 || (cur_line->type != FILE_LINE_ACTION))
3785 /* Invalid "sectionid" parameter */
3786 edit_free_file(file);
3787 return JB_ERR_CGI_PARAMS;
3790 /* Skip through the section to find the last line in it. */
3791 while ( (cur_line->next != NULL)
3792 && (cur_line->next->type != FILE_LINE_ACTION) )
3794 cur_line = cur_line->next;
3799 /* At this point, the last line in the previous section is in cur_line
3800 * - add after this. (Or if we need to add as the first line, cur_line
3804 new_text = strdup("{}");
3805 if (NULL == new_text)
3807 edit_free_file(file);
3808 return JB_ERR_MEMORY;
3811 /* Allocate the new line */
3812 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3813 if (new_line == NULL)
3816 edit_free_file(file);
3817 return JB_ERR_MEMORY;
3820 /* Fill in the data members of the new line */
3821 new_line->raw = NULL;
3822 new_line->prefix = NULL;
3823 new_line->unprocessed = new_text;
3824 new_line->type = FILE_LINE_ACTION;
3826 if (cur_line != NULL)
3828 /* Link new_line into the list, after cur_line */
3829 new_line->next = cur_line->next;
3830 cur_line->next = new_line;
3834 /* Link new_line into the list, as first line */
3835 new_line->next = file->lines;
3836 file->lines = new_line;
3839 /* Done making changes, now commit */
3841 err = edit_write_file(file);
3844 /* Error writing file */
3845 edit_free_file(file);
3849 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3850 string_append(&target, file->identifier);
3852 edit_free_file(file);
3857 return JB_ERR_MEMORY;
3860 rsp->status = strdup("302 Local Redirect from Privoxy");
3861 if (rsp->status == NULL)
3864 return JB_ERR_MEMORY;
3866 err = enlist_unique_header(rsp->headers, "Location", target);
3873 /*********************************************************************
3875 * Function : cgi_edit_actions_section_swap
3877 * Description : CGI function that swaps the order of two sections
3878 * in the actions file. Note that this CGI can actually
3879 * swap any two arbitrary sections, but the GUI interface
3880 * currently only allows consecutive sections to be
3884 * 1 : csp = Current client state (buffers, headers, etc...)
3885 * 2 : rsp = http_response data structure for output
3886 * 3 : parameters = map of cgi parameters
3889 * f : (filename) Identifies the file to edit
3890 * v : (version) File's last-modified time
3891 * s1 : (section1) Line number of first section to swap
3892 * s2 : (section2) Line number of second section to swap
3894 * Returns : JB_ERR_OK on success
3895 * JB_ERR_MEMORY on out-of-memory
3896 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3897 * specified or not valid.
3899 *********************************************************************/
3900 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3901 struct http_response *rsp,
3902 const struct map *parameters)
3906 struct editable_file * file;
3907 struct file_line * cur_line;
3908 struct file_line * prev_line;
3909 struct file_line * line_before_section1;
3910 struct file_line * line_start_section1;
3911 struct file_line * line_end_section1;
3912 struct file_line * line_after_section1;
3913 struct file_line * line_before_section2;
3914 struct file_line * line_start_section2;
3915 struct file_line * line_end_section2;
3916 struct file_line * line_after_section2;
3917 unsigned line_number;
3921 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3923 return cgi_error_disabled(csp, rsp);
3926 err = get_number_param(csp, parameters, "s1", §ion1);
3927 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3933 if (section1 > section2)
3935 unsigned temp = section2;
3936 section2 = section1;
3940 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3943 /* No filename specified, can't read file, modified, or out of memory. */
3944 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3947 /* Start at the beginning... */
3949 cur_line = file->lines;
3952 /* ... find section1 ... */
3953 while ((cur_line != NULL) && (line_number < section1))
3955 prev_line = cur_line;
3956 cur_line = cur_line->next;
3960 if ( (cur_line == NULL)
3961 || (cur_line->type != FILE_LINE_ACTION) )
3963 /* Invalid "section1" parameter */
3964 edit_free_file(file);
3965 return JB_ERR_CGI_PARAMS;
3968 /* If no-op, we've validated params and can skip the rest. */
3969 if (section1 != section2)
3971 /* ... find the end of section1 ... */
3972 line_before_section1 = prev_line;
3973 line_start_section1 = cur_line;
3976 prev_line = cur_line;
3977 cur_line = cur_line->next;
3980 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3981 line_end_section1 = prev_line;
3982 line_after_section1 = cur_line;
3984 /* ... find section2 ... */
3985 while ((cur_line != NULL) && (line_number < section2))
3987 prev_line = cur_line;
3988 cur_line = cur_line->next;
3992 if ( (cur_line == NULL)
3993 || (cur_line->type != FILE_LINE_ACTION) )
3995 /* Invalid "section2" parameter */
3996 edit_free_file(file);
3997 return JB_ERR_CGI_PARAMS;
4000 /* ... find the end of section2 ... */
4001 line_before_section2 = prev_line;
4002 line_start_section2 = cur_line;
4005 prev_line = cur_line;
4006 cur_line = cur_line->next;
4009 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4010 line_end_section2 = prev_line;
4011 line_after_section2 = cur_line;
4013 /* Now have all the pointers we need. Do the swap. */
4015 /* Change the pointer to section1 to point to section2 instead */
4016 if (line_before_section1 == NULL)
4018 file->lines = line_start_section2;
4022 line_before_section1->next = line_start_section2;
4025 if (line_before_section2 == line_end_section1)
4027 /* Consecutive sections */
4028 line_end_section2->next = line_start_section1;
4032 line_end_section2->next = line_after_section1;
4033 line_before_section2->next = line_start_section1;
4036 /* Set the pointer from the end of section1 to the rest of the file */
4037 line_end_section1->next = line_after_section2;
4039 err = edit_write_file(file);
4042 /* Error writing file */
4043 edit_free_file(file);
4046 } /* END if (section1 != section2) */
4048 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4049 string_append(&target, file->identifier);
4051 edit_free_file(file);
4056 return JB_ERR_MEMORY;
4059 rsp->status = strdup("302 Local Redirect from Privoxy");
4060 if (rsp->status == NULL)
4063 return JB_ERR_MEMORY;
4065 err = enlist_unique_header(rsp->headers, "Location", target);
4072 /*********************************************************************
4074 * Function : cgi_toggle
4076 * Description : CGI function that adds a new empty section to
4080 * 1 : csp = Current client state (buffers, headers, etc...)
4081 * 2 : rsp = http_response data structure for output
4082 * 3 : parameters = map of cgi parameters
4085 * set : If present, how to change toggle setting:
4086 * "enable", "disable", "toggle", or none (default).
4087 * mini : If present, use mini reply template.
4089 * Returns : JB_ERR_OK on success
4090 * JB_ERR_MEMORY on out-of-memory
4092 *********************************************************************/
4093 jb_err cgi_toggle(struct client_state *csp,
4094 struct http_response *rsp,
4095 const struct map *parameters)
4097 struct map *exports;
4099 const char *template_name;
4105 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4107 return cgi_error_disabled(csp, rsp);
4110 mode = get_char_param(parameters, "set");
4117 else if (mode == 'D')
4122 else if (mode == 'T')
4125 g_bToggleIJB = !g_bToggleIJB;
4128 if (NULL == (exports = default_exports(csp, "toggle")))
4130 return JB_ERR_MEMORY;
4133 template_name = (get_char_param(parameters, "mini")
4137 return template_fill_for_cgi(csp, template_name, exports, rsp);
4141 /*********************************************************************
4143 * Function : javascriptify
4145 * Description : Converts a string into a form JavaScript will like.
4147 * Netscape 4's JavaScript sucks - it doesn't use
4148 * "id" parameters, so you have to set the "name"
4149 * used to submit a form element to something JavaScript
4150 * will like. (Or access the elements by index in an
4151 * array. That array contains >60 elements and will
4152 * be changed whenever we add a new action to the
4153 * editor, so I'm NOT going to use indexes that have
4154 * to be figured out by hand.)
4156 * Currently the only thing we have to worry about
4157 * is "-" ==> "_" conversion.
4159 * This is a length-preserving operation so it is
4160 * carried out in-place, no memory is allocated
4164 * 1 : identifier = String to make JavaScript-friendly.
4168 *********************************************************************/
4169 static void javascriptify(char * identifier)
4171 char * p = identifier;
4172 while (NULL != (p = strchr(p, '-')))
4179 /*********************************************************************
4181 * Function : actions_to_radio
4183 * Description : Converts a actionsfile entry into settings for
4184 * radio buttons and edit boxes on a HTML form.
4187 * 1 : exports = List of substitutions to add to.
4188 * 2 : action = Action to read
4190 * Returns : JB_ERR_OK on success
4191 * JB_ERR_MEMORY on out-of-memory
4193 *********************************************************************/
4194 static jb_err actions_to_radio(struct map * exports,
4195 const struct action_spec *action)
4197 unsigned mask = action->mask;
4198 unsigned add = action->add;
4206 mask = action->mask;
4209 /* sanity - prevents "-feature +feature" */
4213 #define DEFINE_ACTION_BOOL(name, bit) \
4214 if (!(mask & bit)) \
4216 current_mode = 'n'; \
4218 else if (add & bit) \
4220 current_mode = 'y'; \
4224 current_mode = 'x'; \
4226 if (map_radio(exports, name, "ynx", current_mode)) \
4228 return JB_ERR_MEMORY; \
4231 #define DEFINE_ACTION_STRING(name, bit, index) \
4232 DEFINE_ACTION_BOOL(name, bit); \
4235 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4238 checked = !strcmp(action->string[index], value); \
4242 checked = is_default; \
4244 mapped_param |= checked; \
4245 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4247 return JB_ERR_MEMORY; \
4250 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4251 if (map(exports, name "-param-custom", 1, \
4252 ((!mapped_param) ? "checked" : ""), 1)) \
4254 return JB_ERR_MEMORY; \
4256 if (map(exports, name "-param", 1, \
4257 (((add & bit) && !mapped_param) ? \
4258 action->string[index] : default_val), 1)) \
4260 return JB_ERR_MEMORY; \
4263 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4264 if (map(exports, name "-param", 1, \
4265 ((add & bit) ? action->string[index] : default_val), 1)) \
4267 return JB_ERR_MEMORY; \
4270 #define DEFINE_ACTION_MULTI(name, index) \
4271 if (action->multi_add[index]->first) \
4273 current_mode = 'y'; \
4275 else if (action->multi_remove_all[index]) \
4277 current_mode = 'n'; \
4279 else if (action->multi_remove[index]->first) \
4281 current_mode = 'y'; \
4285 current_mode = 'x'; \
4287 if (map_radio(exports, name, "ynx", current_mode)) \
4289 return JB_ERR_MEMORY; \
4292 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4294 #include "actionlist.h"
4296 #undef DEFINE_ACTION_MULTI
4297 #undef DEFINE_ACTION_STRING
4298 #undef DEFINE_ACTION_BOOL
4299 #undef DEFINE_ACTION_ALIAS
4300 #undef DEFINE_CGI_PARAM_CUSTOM
4301 #undef DEFINE_CGI_PARAM_RADIO
4302 #undef DEFINE_CGI_PARAM_NO_RADIO
4308 /*********************************************************************
4310 * Function : actions_from_radio
4312 * Description : Converts a map of parameters passed to a CGI function
4313 * into an actionsfile entry.
4316 * 1 : parameters = parameters to the CGI call
4317 * 2 : action = Action to change. Must be valid before
4318 * the call, actions not specified will be
4321 * Returns : JB_ERR_OK on success
4322 * JB_ERR_MEMORY on out-of-memory
4324 *********************************************************************/
4325 static jb_err actions_from_radio(const struct map * parameters,
4326 struct action_spec *action)
4328 static int first_time = 1;
4332 const char * js_name;
4333 jb_err err = JB_ERR_OK;
4338 /* Statics are generally a potential race condition,
4339 * but in this case we're safe and don't need semaphores.
4340 * Be careful if you modify this function.
4344 #define JAVASCRIPTIFY(dest_var, string) \
4346 static char js_name_arr[] = string; \
4349 javascriptify(js_name_arr); \
4351 dest_var = js_name_arr; \
4354 #define DEFINE_ACTION_BOOL(name, bit) \
4355 JAVASCRIPTIFY(js_name, name); \
4356 ch = get_char_param(parameters, js_name); \
4359 action->add |= bit; \
4360 action->mask |= bit; \
4362 else if (ch == 'N') \
4364 action->add &= ~bit; \
4365 action->mask &= ~bit; \
4367 else if (ch == 'X') \
4369 action->add &= ~bit; \
4370 action->mask |= bit; \
4373 #define DEFINE_ACTION_STRING(name, bit, index) \
4374 JAVASCRIPTIFY(js_name, name); \
4375 ch = get_char_param(parameters, js_name); \
4379 JAVASCRIPTIFY(js_name, name "-mode"); \
4380 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4381 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4383 JAVASCRIPTIFY(js_name, name "-param"); \
4384 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4386 if (param != NULL) \
4388 if (NULL == (param_dup = strdup(param))) \
4390 return JB_ERR_MEMORY; \
4392 freez(action->string[index]); \
4393 action->add |= bit; \
4394 action->mask |= bit; \
4395 action->string[index] = param_dup; \
4398 else if (ch == 'N') \
4400 if (action->add & bit) \
4402 freez(action->string[index]); \
4404 action->add &= ~bit; \
4405 action->mask &= ~bit; \
4407 else if (ch == 'X') \
4409 if (action->add & bit) \
4411 freez(action->string[index]); \
4413 action->add &= ~bit; \
4414 action->mask |= bit; \
4417 #define DEFINE_ACTION_MULTI(name, index) \
4418 JAVASCRIPTIFY(js_name, name); \
4419 ch = get_char_param(parameters, js_name); \
4424 else if (ch == 'N') \
4426 list_remove_all(action->multi_add[index]); \
4427 list_remove_all(action->multi_remove[index]); \
4428 action->multi_remove_all[index] = 1; \
4430 else if (ch == 'X') \
4432 list_remove_all(action->multi_add[index]); \
4433 list_remove_all(action->multi_remove[index]); \
4434 action->multi_remove_all[index] = 0; \
4437 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4439 #include "actionlist.h"
4441 #undef DEFINE_ACTION_MULTI
4442 #undef DEFINE_ACTION_STRING
4443 #undef DEFINE_ACTION_BOOL
4444 #undef DEFINE_ACTION_ALIAS
4445 #undef JAVASCRIPTIFY
4453 #endif /* def FEATURE_CGI_EDIT_ACTIONS */