1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.29 2002/04/08 16:59:08 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
10 * NOTE: The CGIs in this file use parameter names
11 * such as "f" and "s" which are really *BAD* choices.
12 * However, I'm trying to save bytes in the
13 * edit-actions-list HTML page - the standard actions
14 * file generated a 550kbyte page, which is ridiculous.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001 the SourceForge
19 * Privoxy team. http://www.privoxy.org/
21 * Based on the Internet Junkbuster originally written
22 * by and Copyright (C) 1997 Anonymous Coders and
23 * Junkbusters Corporation. http://www.junkbusters.com
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General
27 * Public License as published by the Free Software
28 * Foundation; either version 2 of the License, or (at
29 * your option) any later version.
31 * This program is distributed in the hope that it will
32 * be useful, but WITHOUT ANY WARRANTY; without even the
33 * implied warranty of MERCHANTABILITY or FITNESS FOR A
34 * PARTICULAR PURPOSE. See the GNU General Public
35 * License for more details.
37 * The GNU General Public License should be included with
38 * this file. If not, you can view it at
39 * http://www.gnu.org/copyleft/gpl.html
40 * or write to the Free Software Foundation, Inc., 59
41 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 * Revision 1.29 2002/04/08 16:59:08 oes
48 * Revision 1.28 2002/03/27 12:30:29 oes
49 * Deleted unsused variable
51 * Revision 1.27 2002/03/26 23:06:04 jongfoster
52 * Removing duplicate @ifs on the toggle page
54 * Revision 1.26 2002/03/26 22:59:17 jongfoster
55 * Fixing /toggle to display status consistently.
57 * Revision 1.25 2002/03/26 22:29:54 swa
58 * we have a new homepage!
60 * Revision 1.24 2002/03/24 15:23:33 jongfoster
63 * Revision 1.23 2002/03/24 13:32:41 swa
64 * name change related issues
66 * Revision 1.22 2002/03/24 13:25:43 swa
67 * name change related issues
69 * Revision 1.21 2002/03/22 18:02:48 jongfoster
70 * Fixing remote toggle
72 * Revision 1.20 2002/03/16 20:28:34 oes
73 * Added descriptions to the filters so users will know what they select in the cgi editor
75 * Revision 1.19 2002/03/16 18:38:14 jongfoster
76 * Stopping stupid or malicious users from breaking the actions
77 * file using the web-based editor.
79 * Revision 1.18 2002/03/16 14:57:44 jongfoster
80 * Full support for enabling/disabling modular filters.
82 * Revision 1.17 2002/03/16 14:26:42 jongfoster
83 * First version of modular filters support - READ ONLY!
84 * Fixing a double-free bug in the out-of-memory handling in map_radio().
86 * Revision 1.16 2002/03/07 03:46:17 oes
87 * Fixed compiler warnings
89 * Revision 1.15 2002/03/06 22:54:35 jongfoster
90 * Automated function-comment nitpicking.
92 * Revision 1.14 2002/03/05 00:24:51 jongfoster
93 * Patch to always edit the current actions file.
95 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
96 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
98 * Revision 1.12 2002/03/03 09:18:03 joergs
99 * Made jumbjuster work on AmigaOS again.
101 * Revision 1.11 2002/01/23 01:03:31 jongfoster
102 * Fixing gcc [CygWin] compiler warnings
104 * Revision 1.10 2002/01/23 00:22:59 jongfoster
105 * Adding new function cgi_edit_actions_section_swap(), to reorder
108 * Adding get_url_spec_param() to get a validated URL pattern.
110 * Moving edit_read_line() out of this file and into loaders.c.
112 * Adding missing html_encode() to many CGI functions.
114 * Moving the functions that #include actionlist.h to the end of the file,
115 * because the Visual C++ 97 debugger gets extremely confused if you try
116 * to debug any code that comes after them in the file.
118 * Major optimizations in cgi_edit_actions_list() to reduce the size of
119 * the generated HTML (down 40% from 550k to 304k), with major side-effects
120 * throughout the editor and templates. In particular, the length of the
121 * URLs throughout the editor has been drastically reduced, by cutting
122 * paramater names down to 1 character and CGI names down to 3-4
123 * characters, by removing all non-essential CGI paramaters even at the
124 * expense of having to re-read the actions file for the most trivial
125 * page, and by using relative rather than absolute URLs. This means
126 * that this (typical example):
128 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
129 * filename=ijb&ver=1011487572&section=12&pattern=13
130 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
134 * <a href="eau?f=ijb&v=1011487572&p=13">
136 * Revision 1.9 2002/01/17 20:56:22 jongfoster
137 * Replacing hard references to the URL of the config interface
138 * with #defines from project.h
140 * Revision 1.8 2001/11/30 23:35:51 jongfoster
141 * Renaming actionsfile to ijb.action
143 * Revision 1.7 2001/11/13 00:28:24 jongfoster
144 * - Renaming parameters from edit-actions-for-url so that they only
145 * contain legal JavaScript characters. If we wanted to write
146 * JavaScript that worked with Netscape 4, this is nessacery.
147 * (Note that at the moment the JavaScript doesn't actually work
148 * with Netscape 4, but now this is purely a template issue, not
149 * one affecting code).
150 * - Adding new CGIs for use by non-JavaScript browsers:
151 * edit-actions-url-form
152 * edit-actions-add-url-form
153 * edit-actions-remove-url-form
156 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
157 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
158 * by and __OS2__ ifdef.
160 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
161 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
162 * threads to call select() simultaneously. So, it's time to do a real, live,
163 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
164 * (native). Both versions will work, but using __OS2__ offers multi-threading.
166 * Revision 1.4 2001/10/23 21:48:19 jongfoster
167 * Cleaning up error handling in CGI functions - they now send back
168 * a HTML error page and should never cause a FATAL error. (Fixes one
169 * potential source of "denial of service" attacks).
171 * CGI actions file editor that works and is actually useful.
173 * Ability to toggle JunkBuster remotely using a CGI call.
175 * You can turn off both the above features in the main configuration
176 * file, e.g. if you are running a multi-user proxy.
178 * Revision 1.3 2001/10/14 22:12:49 jongfoster
179 * New version of CGI-based actionsfile editor.
180 * Major changes, including:
181 * - Completely new file parser and file output routines
182 * - edit-actions CGI renamed edit-actions-for-url
183 * - All CGIs now need a filename parameter, except for...
184 * - New CGI edit-actions which doesn't need a filename,
185 * to allow you to start the editor up.
186 * - edit-actions-submit now works, and now automatically
187 * redirects you back to the main edit-actions-list handler.
189 * Revision 1.2 2001/09/16 17:05:14 jongfoster
190 * Removing unused #include showarg.h
192 * Revision 1.1 2001/09/16 15:47:37 jongfoster
193 * First version of CGI-based edit interface. This is very much a
194 * work-in-progress, and you can't actually use it to edit anything
195 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
196 * to have any effect.
199 **********************************************************************/
205 * FIXME: Following includes copied from cgi.c - which are actually needed?
210 #include <sys/types.h>
215 #include <sys/stat.h>
218 #define snprintf _snprintf
219 #endif /* def _WIN32 */
224 #include "cgisimple.h"
228 #include "miscutil.h"
232 /* loadcfg.h is for g_bToggleIJB only */
233 #include "urlmatch.h"
235 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
238 #ifdef FEATURE_CGI_EDIT_ACTIONS
242 struct file_line * next;
250 struct action_spec action[1];
259 /* Add more data types here... e.g.
262 struct url_spec url[1];
266 struct action_spec action[1];
275 #define FILE_LINE_UNPROCESSED 1
276 #define FILE_LINE_BLANK 2
277 #define FILE_LINE_ALIAS_HEADER 3
278 #define FILE_LINE_ALIAS_ENTRY 4
279 #define FILE_LINE_ACTION 5
280 #define FILE_LINE_URL 6
281 #define FILE_LINE_SETTINGS_HEADER 7
282 #define FILE_LINE_SETTINGS_ENTRY 8
283 #define FILE_LINE_DESCRIPTION_HEADER 9
284 #define FILE_LINE_DESCRIPTION_ENTRY 10
289 struct file_line * lines;
290 const char * filename; /* Full pathname - e.g. "/etc/privoxy/wibble.action" */
291 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
292 /* Pre-encoded with url_encode() for ease of use. */
293 const char * version_str; /* Last modification time, as a string. For CGI param */
294 /* Can be used in URL without using url_param(). */
295 unsigned version; /* Last modification time - prevents chaos with
296 * the browser's "back" button. Note that this is a
297 * time_t cast to an unsigned. When comparing, always
298 * cast the time_t to an unsigned, and *NOT* vice-versa.
299 * This may lose the top few bits, but they're not
300 * significant anyway.
302 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
303 * Note that changing this after the file has been
304 * read in will cause a mess.
306 struct file_line * parse_error; /* On parse error, this is the offending line. */
307 const char * parse_error_text; /* On parse error, this is the problem.
308 * (Statically allocated) */
311 #define CGI_ACTION_PARAM_LEN_MAX 500
313 /* FIXME: Following non-static functions should be prototyped in .h or made static */
315 /* Functions to read and write arbitrary config files */
316 jb_err edit_read_file(struct client_state *csp,
317 const struct map *parameters,
320 struct editable_file **pfile);
321 jb_err edit_write_file(struct editable_file * file);
322 void edit_free_file(struct editable_file * file);
324 /* Functions to read and write actions files */
325 jb_err edit_parse_actions_file(struct editable_file * file);
326 jb_err edit_read_actions_file(struct client_state *csp,
327 struct http_response *rsp,
328 const struct map *parameters,
330 struct editable_file **pfile);
333 jb_err cgi_error_modified(struct client_state *csp,
334 struct http_response *rsp,
335 const char *filename);
336 jb_err cgi_error_parse(struct client_state *csp,
337 struct http_response *rsp,
338 struct editable_file *file);
339 jb_err cgi_error_file(struct client_state *csp,
340 struct http_response *rsp,
341 const char *filename);
342 jb_err cgi_error_disabled(struct client_state *csp,
343 struct http_response *rsp);
345 /* Internal arbitrary config file support functions */
346 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
347 static void edit_free_file_lines(struct file_line * first_line);
349 /* Internal actions file support functions */
350 static int match_actions_file_header_line(const char * line, const char * name);
351 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
353 /* Internal parameter parsing functions */
354 static jb_err get_file_name_param(struct client_state *csp,
355 const struct map *parameters,
356 const char *param_name,
359 const char **pparam);
360 static jb_err get_number_param(struct client_state *csp,
361 const struct map *parameters,
364 static jb_err get_url_spec_param(struct client_state *csp,
365 const struct map *parameters,
368 static jb_err get_string_param(const struct map *parameters,
369 const char *param_name,
370 const char **pparam);
372 /* Internal actionsfile <==> HTML conversion functions */
373 static jb_err map_radio(struct map * exports,
374 const char * optionname,
377 static jb_err actions_to_radio(struct map * exports,
378 const struct action_spec *action);
379 static jb_err actions_from_radio(const struct map * parameters,
380 struct action_spec *action);
383 static jb_err map_copy_parameter_html(struct map *out,
384 const struct map *in,
386 #if 0 /* unused function */
387 static jb_err map_copy_parameter_url(struct map *out,
388 const struct map *in,
390 #endif /* unused function */
392 /*********************************************************************
394 * Function : map_copy_parameter_html
396 * Description : Copy a CGI parameter from one map to another, HTML
400 * 1 : out = target map
401 * 2 : in = source map
402 * 3 : name = name of cgi parameter to copy
404 * Returns : JB_ERR_OK on success
405 * JB_ERR_MEMORY on out-of-memory
406 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
409 *********************************************************************/
410 static jb_err map_copy_parameter_html(struct map *out,
411 const struct map *in,
421 value = lookup(in, name);
422 err = map(out, name, 1, html_encode(value), 0);
429 else if (*value == '\0')
431 return JB_ERR_CGI_PARAMS;
440 #if 0 /* unused function */
441 /*********************************************************************
443 * Function : map_copy_parameter_html
445 * Description : Copy a CGI parameter from one map to another, URL
449 * 1 : out = target map
450 * 2 : in = source map
451 * 3 : name = name of cgi parameter to copy
453 * Returns : JB_ERR_OK on success
454 * JB_ERR_MEMORY on out-of-memory
455 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
458 *********************************************************************/
459 static jb_err map_copy_parameter_url(struct map *out,
460 const struct map *in,
470 value = lookup(in, name);
471 err = map(out, name, 1, url_encode(value), 0);
478 else if (*value == '\0')
480 return JB_ERR_CGI_PARAMS;
487 #endif /* 0 - unused function */
489 /*********************************************************************
491 * Function : cgi_edit_actions_url_form
493 * Description : CGI function that displays a form for
497 * 1 : csp = Current client state (buffers, headers, etc...)
498 * 2 : rsp = http_response data structure for output
499 * 3 : parameters = map of cgi parameters
502 * f : (filename) Identifies the file to edit
503 * v : (version) File's last-modified time
504 * p : (pattern) Line number of pattern to edit
506 * Returns : JB_ERR_OK on success
507 * JB_ERR_MEMORY on out-of-memory
508 * JB_ERR_CGI_PARAMS if the CGI parameters are not
509 * specified or not valid.
511 *********************************************************************/
512 jb_err cgi_edit_actions_url_form(struct client_state *csp,
513 struct http_response *rsp,
514 const struct map *parameters)
516 struct map * exports;
518 struct editable_file * file;
519 struct file_line * cur_line;
520 unsigned line_number;
527 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
529 return cgi_error_disabled(csp, rsp);
532 err = get_number_param(csp, parameters, "p", &patternid);
538 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
541 /* No filename specified, can't read file, modified, or out of memory. */
542 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
545 cur_line = file->lines;
547 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
549 cur_line = cur_line->next;
552 if ( (cur_line == NULL)
553 || (line_number != patternid)
555 || (cur_line->type != FILE_LINE_URL))
557 /* Invalid "patternid" parameter */
558 edit_free_file(file);
559 return JB_ERR_CGI_PARAMS;
562 if (NULL == (exports = default_exports(csp, NULL)))
564 edit_free_file(file);
565 return JB_ERR_MEMORY;
568 err = map(exports, "f", 1, file->identifier, 1);
569 if (!err) err = map(exports, "v", 1, file->version_str, 1);
570 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
571 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
573 edit_free_file(file);
581 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
585 /*********************************************************************
587 * Function : cgi_edit_actions_add_url_form
589 * Description : CGI function that displays a form for
593 * 1 : csp = Current client state (buffers, headers, etc...)
594 * 2 : rsp = http_response data structure for output
595 * 3 : parameters = map of cgi parameters
598 * f : (filename) Identifies the file to edit
599 * v : (version) File's last-modified time
600 * s : (section) Line number of section to edit
602 * Returns : JB_ERR_OK on success
603 * JB_ERR_MEMORY on out-of-memory
604 * JB_ERR_CGI_PARAMS if the CGI parameters are not
605 * specified or not valid.
607 *********************************************************************/
608 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
609 struct http_response *rsp,
610 const struct map *parameters)
619 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
621 return cgi_error_disabled(csp, rsp);
624 if (NULL == (exports = default_exports(csp, NULL)))
626 return JB_ERR_MEMORY;
629 err = map_copy_parameter_html(exports, parameters, "f");
630 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
631 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
639 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
643 /*********************************************************************
645 * Function : cgi_edit_actions_remove_url_form
647 * Description : CGI function that displays a form for
651 * 1 : csp = Current client state (buffers, headers, etc...)
652 * 2 : rsp = http_response data structure for output
653 * 3 : parameters = map of cgi parameters
656 * f : (filename) Identifies the file to edit
657 * v : (version) File's last-modified time
658 * p : (pattern) Line number of pattern to edit
660 * Returns : JB_ERR_OK on success
661 * JB_ERR_MEMORY on out-of-memory
662 * JB_ERR_CGI_PARAMS if the CGI parameters are not
663 * specified or not valid.
665 *********************************************************************/
666 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
667 struct http_response *rsp,
668 const struct map *parameters)
670 struct map * exports;
672 struct editable_file * file;
673 struct file_line * cur_line;
674 unsigned line_number;
681 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
683 return cgi_error_disabled(csp, rsp);
686 err = get_number_param(csp, parameters, "p", &patternid);
692 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
695 /* No filename specified, can't read file, modified, or out of memory. */
696 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
699 cur_line = file->lines;
701 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
703 cur_line = cur_line->next;
706 if ( (cur_line == NULL)
707 || (line_number != patternid)
709 || (cur_line->type != FILE_LINE_URL))
711 /* Invalid "patternid" parameter */
712 edit_free_file(file);
713 return JB_ERR_CGI_PARAMS;
716 if (NULL == (exports = default_exports(csp, NULL)))
718 edit_free_file(file);
719 return JB_ERR_MEMORY;
722 err = map(exports, "f", 1, file->identifier, 1);
723 if (!err) err = map(exports, "v", 1, file->version_str, 1);
724 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
725 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
727 edit_free_file(file);
735 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
739 /*********************************************************************
741 * Function : edit_write_file
743 * Description : Write a complete file to disk.
746 * 1 : filename = File to write to.
747 * 2 : file = Data structure to write.
749 * Returns : JB_ERR_OK on success
750 * JB_ERR_FILE on error writing to file.
751 * JB_ERR_MEMORY on out of memory
753 *********************************************************************/
754 jb_err edit_write_file(struct editable_file * file)
757 struct file_line * cur_line;
758 struct stat statbuf[1];
759 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
760 digits in time_t, assuming this is a 64-bit
761 machine, plus null terminator, plus one
765 assert(file->filename);
767 #if defined(AMIGA) || defined(__OS2__)
768 if (NULL == (fp = fopen(file->filename, "w")))
770 if (NULL == (fp = fopen(file->filename, "wt")))
771 #endif /* def AMIGA */
776 cur_line = file->lines;
777 while (cur_line != NULL)
781 if (fputs(cur_line->raw, fp) < 0)
789 if (cur_line->prefix)
791 if (fputs(cur_line->prefix, fp) < 0)
797 if (cur_line->unprocessed)
799 /* This should be a single line - sanity check. */
800 assert(NULL == strchr(cur_line->unprocessed, '\r'));
801 assert(NULL == strchr(cur_line->unprocessed, '\n'));
803 if (NULL != strchr(cur_line->unprocessed, '#'))
805 /* Must quote '#' characters */
812 /* Count number of # characters, so we know length of output string */
813 src = cur_line->unprocessed;
814 while (NULL != (src = strchr(src, '#')))
821 /* Allocate new memory for string */
822 len = strlen(cur_line->unprocessed);
823 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
825 /* Uh oh, just trashed file! */
827 return JB_ERR_MEMORY;
830 /* Loop through string from end */
831 src = cur_line->unprocessed + len;
832 dest = str + len + numhash;
833 for ( ; len >= 0; len--)
835 if ((*dest-- = *src--) == '#')
839 assert(numhash >= 0);
842 assert(numhash == 0);
843 assert(src + 1 == cur_line->unprocessed);
844 assert(dest + 1 == str);
846 if (fputs(str, fp) < 0)
857 /* Can write without quoting '#' characters. */
858 if (fputs(cur_line->unprocessed, fp) < 0)
864 if (fputs(NEWLINE(file->newline), fp) < 0)
872 /* FIXME: Write data from file->data->whatever */
876 cur_line = cur_line->next;
882 /* Update the version stamp in the file structure, since we just
883 * wrote to the file & changed it's date.
885 if (stat(file->filename, statbuf) < 0)
887 /* Error, probably file not found. */
890 file->version = (unsigned)statbuf->st_mtime;
892 /* Correct file->version_str */
893 freez(file->version_str);
894 snprintf(version_buf, 22, "%u", file->version);
895 version_buf[21] = '\0';
896 file->version_str = strdup(version_buf);
897 if (version_buf == NULL)
899 return JB_ERR_MEMORY;
906 /*********************************************************************
908 * Function : edit_free_file
910 * Description : Free a complete file in memory.
913 * 1 : file = Data structure to free.
917 *********************************************************************/
918 void edit_free_file(struct editable_file * file)
922 /* Silently ignore NULL pointer */
926 edit_free_file_lines(file->lines);
927 freez(file->filename);
928 freez(file->identifier);
929 freez(file->version_str);
931 file->parse_error_text = NULL; /* Statically allocated */
932 file->parse_error = NULL;
938 /*********************************************************************
940 * Function : edit_free_file
942 * Description : Free an entire linked list of file lines.
945 * 1 : first_line = Data structure to free.
949 *********************************************************************/
950 static void edit_free_file_lines(struct file_line * first_line)
952 struct file_line * next_line;
954 while (first_line != NULL)
956 next_line = first_line->next;
957 first_line->next = NULL;
958 freez(first_line->raw);
959 freez(first_line->prefix);
960 freez(first_line->unprocessed);
961 switch(first_line->type)
963 case 0: /* special case if memory zeroed */
964 case FILE_LINE_UNPROCESSED:
965 case FILE_LINE_BLANK:
966 case FILE_LINE_ALIAS_HEADER:
967 case FILE_LINE_SETTINGS_HEADER:
968 case FILE_LINE_DESCRIPTION_HEADER:
969 case FILE_LINE_DESCRIPTION_ENTRY:
970 case FILE_LINE_ALIAS_ENTRY:
972 /* No data is stored for these */
975 case FILE_LINE_ACTION:
976 free_action(first_line->data.action);
979 case FILE_LINE_SETTINGS_ENTRY:
980 freez(first_line->data.setting.name);
981 freez(first_line->data.setting.svalue);
984 /* Should never happen */
988 first_line->type = 0; /* paranoia */
990 first_line = next_line;
995 /*********************************************************************
997 * Function : match_actions_file_header_line
999 * Description : Match an actions file {{header}} line
1002 * 1 : line = String from file
1003 * 2 : name = Header to match against
1005 * Returns : 0 iff they match.
1007 *********************************************************************/
1008 static int match_actions_file_header_line(const char * line, const char * name)
1016 if ((line[0] != '{') || (line[1] != '{'))
1022 /* Look for optional whitespace */
1023 while ( (*line == ' ') || (*line == '\t') )
1028 /* Look for the specified name (case-insensitive) */
1030 if (0 != strncmpic(line, name, len))
1036 /* Look for optional whitespace */
1037 while ( (*line == ' ') || (*line == '\t') )
1042 /* Look for "}}" and end of string*/
1043 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1053 /*********************************************************************
1055 * Function : match_actions_file_header_line
1057 * Description : Match an actions file {{header}} line
1060 * 1 : line = String from file. Must not start with
1061 * whitespace (else infinite loop!)
1062 * 2 : name = Destination for name
1063 * 2 : name = Destination for value
1065 * Returns : JB_ERR_OK on success
1066 * JB_ERR_MEMORY on out-of-memory
1067 * JB_ERR_PARSE if there's no "=" sign, or if there's
1068 * nothing before the "=" sign (but empty
1069 * values *after* the "=" sign are legal).
1071 *********************************************************************/
1072 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1074 const char * name_end;
1075 const char * value_start;
1081 assert(*line != ' ');
1082 assert(*line != '\t');
1087 value_start = strchr(line, '=');
1088 if ((value_start == NULL) || (value_start == line))
1090 return JB_ERR_PARSE;
1093 name_end = value_start - 1;
1095 /* Eat any whitespace before the '=' */
1096 while ((*name_end == ' ') || (*name_end == '\t'))
1099 * we already know we must have at least 1 non-ws char
1100 * at start of buf - no need to check
1105 name_len = name_end - line + 1; /* Length excluding \0 */
1106 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1108 return JB_ERR_MEMORY;
1110 strncpy(*pname, line, name_len);
1111 (*pname)[name_len] = '\0';
1113 /* Eat any the whitespace after the '=' */
1115 while ((*value_start == ' ') || (*value_start == '\t'))
1120 if (NULL == (*pvalue = strdup(value_start)))
1124 return JB_ERR_MEMORY;
1131 /*********************************************************************
1133 * Function : edit_parse_actions_file
1135 * Description : Parse an actions file in memory.
1137 * Passed linked list must have the "data" member
1138 * zeroed, and must contain valid "next" and
1139 * "unprocessed" fields. The "raw" and "prefix"
1140 * fields are ignored, and "type" is just overwritten.
1142 * Note that on error the file may have been
1146 * 1 : file = Actions file to be parsed in-place.
1148 * Returns : JB_ERR_OK on success
1149 * JB_ERR_MEMORY on out-of-memory
1150 * JB_ERR_PARSE on error
1152 *********************************************************************/
1153 jb_err edit_parse_actions_file(struct editable_file * file)
1155 struct file_line * cur_line;
1157 const char * text; /* Text from a line */
1158 char * name; /* For lines of the form name=value */
1159 char * value; /* For lines of the form name=value */
1160 struct action_alias * alias_list = NULL;
1161 jb_err err = JB_ERR_OK;
1163 /* alias_list contains the aliases defined in this file.
1164 * It might be better to use the "file_line.data" fields
1165 * in the relavent places instead.
1168 cur_line = file->lines;
1170 /* A note about blank line support: Blank lines should only
1171 * ever occur as the last line in the file. This function
1172 * is more forgiving than that - FILE_LINE_BLANK can occur
1176 /* Skip leading blanks. Should only happen if file is
1177 * empty (which is valid, but pointless).
1179 while ( (cur_line != NULL)
1180 && (cur_line->unprocessed[0] == '\0') )
1183 cur_line->type = FILE_LINE_BLANK;
1184 cur_line = cur_line->next;
1187 if ( (cur_line != NULL)
1188 && (cur_line->unprocessed[0] != '{') )
1190 /* File doesn't start with a header */
1191 file->parse_error = cur_line;
1192 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1193 return JB_ERR_PARSE;
1196 if ( (cur_line != NULL) && (0 ==
1197 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1199 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1201 cur_line = cur_line->next;
1202 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1204 if (cur_line->unprocessed[0])
1206 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1208 err = split_line_on_equals(cur_line->unprocessed,
1209 &cur_line->data.setting.name,
1210 &cur_line->data.setting.svalue);
1211 if (err == JB_ERR_MEMORY)
1215 else if (err != JB_ERR_OK)
1217 /* Line does not contain a name=value pair */
1218 file->parse_error = cur_line;
1219 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1220 return JB_ERR_PARSE;
1225 cur_line->type = FILE_LINE_BLANK;
1227 cur_line = cur_line->next;
1231 if ( (cur_line != NULL) && (0 ==
1232 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1234 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1236 cur_line = cur_line->next;
1237 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1239 if (cur_line->unprocessed[0])
1241 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1245 cur_line->type = FILE_LINE_BLANK;
1247 cur_line = cur_line->next;
1251 if ( (cur_line != NULL) && (0 ==
1252 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1254 cur_line->type = FILE_LINE_ALIAS_HEADER;
1256 cur_line = cur_line->next;
1257 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1259 if (cur_line->unprocessed[0])
1261 /* define an alias */
1262 struct action_alias * new_alias;
1264 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1266 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1267 if (err == JB_ERR_MEMORY)
1271 else if (err != JB_ERR_OK)
1273 /* Line does not contain a name=value pair */
1274 file->parse_error = cur_line;
1275 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1276 return JB_ERR_PARSE;
1279 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1284 free_alias_list(alias_list);
1285 return JB_ERR_MEMORY;
1288 err = get_actions(value, alias_list, new_alias->action);
1291 /* Invalid action or out of memory */
1295 free_alias_list(alias_list);
1296 if (err == JB_ERR_MEMORY)
1302 /* Line does not contain a name=value pair */
1303 file->parse_error = cur_line;
1304 file->parse_error_text = "This alias does not specify a valid set of actions.";
1305 return JB_ERR_PARSE;
1311 new_alias->name = name;
1314 new_alias->next = alias_list;
1315 alias_list = new_alias;
1319 cur_line->type = FILE_LINE_BLANK;
1321 cur_line = cur_line->next;
1325 /* Header done, process the main part of the file */
1326 while (cur_line != NULL)
1328 /* At this point, (cur_line->unprocessed[0] == '{') */
1329 assert(cur_line->unprocessed[0] == '{');
1330 text = cur_line->unprocessed + 1;
1331 len = strlen(text) - 1;
1332 if (text[len] != '}')
1334 /* No closing } on header */
1335 free_alias_list(alias_list);
1336 file->parse_error = cur_line;
1337 file->parse_error_text = "Headers starting with '{' must have a "
1338 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1339 "must close with two brackets ('}}').";
1340 return JB_ERR_PARSE;
1345 /* An invalid {{ header. */
1346 free_alias_list(alias_list);
1347 file->parse_error = cur_line;
1348 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1349 "Please remember that the system (two-bracket) headers must "
1350 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1351 "and must appear before any actions (one-bracket) headers. "
1352 "Also note that system headers may not be repeated.";
1353 return JB_ERR_PARSE;
1356 while ( (*text == ' ') || (*text == '\t') )
1362 && ( (text[len - 1] == ' ')
1363 || (text[len - 1] == '\t') ) )
1368 cur_line->type = FILE_LINE_ACTION;
1370 /* Remove {} and make copy */
1371 if (NULL == (value = (char *) malloc(len + 1)))
1374 free_alias_list(alias_list);
1375 return JB_ERR_MEMORY;
1377 strncpy(value, text, len);
1381 err = get_actions(value, alias_list, cur_line->data.action);
1384 /* Invalid action or out of memory */
1386 free_alias_list(alias_list);
1387 if (err == JB_ERR_MEMORY)
1393 /* Line does not contain a name=value pair */
1394 file->parse_error = cur_line;
1395 file->parse_error_text = "This header does not specify a valid set of actions.";
1396 return JB_ERR_PARSE;
1400 /* Done with string - it was clobbered anyway */
1403 /* Process next line */
1404 cur_line = cur_line->next;
1406 /* Loop processing URL patterns */
1407 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1409 if (cur_line->unprocessed[0])
1411 /* Could parse URL here, but this isn't currently needed */
1413 cur_line->type = FILE_LINE_URL;
1417 cur_line->type = FILE_LINE_BLANK;
1419 cur_line = cur_line->next;
1421 } /* End main while(cur_line != NULL) loop */
1423 free_alias_list(alias_list);
1429 /*********************************************************************
1431 * Function : edit_read_file_lines
1433 * Description : Read all the lines of a file into memory.
1434 * Handles whitespace, comments and line continuation.
1437 * 1 : fp = File to read from. On return, this will be
1438 * at EOF but it will not have been closed.
1439 * 2 : pfile = Destination for a linked list of file_lines.
1440 * Will be set to NULL on error.
1442 * Returns : JB_ERR_OK on success
1443 * JB_ERR_MEMORY on out-of-memory
1445 *********************************************************************/
1446 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1448 struct file_line * first_line; /* Keep for return value or to free */
1449 struct file_line * cur_line; /* Current line */
1450 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1458 cur_line = first_line = zalloc(sizeof(struct file_line));
1459 if (cur_line == NULL)
1461 return JB_ERR_MEMORY;
1464 cur_line->type = FILE_LINE_UNPROCESSED;
1466 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1469 /* Out of memory or empty file. */
1470 /* Note that empty file is not an error we propogate up */
1472 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1477 prev_line = cur_line;
1478 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1479 if (cur_line == NULL)
1482 edit_free_file_lines(first_line);
1483 return JB_ERR_MEMORY;
1486 cur_line->type = FILE_LINE_UNPROCESSED;
1488 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1489 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1492 edit_free_file_lines(first_line);
1493 return JB_ERR_MEMORY;
1497 while (rval != JB_ERR_FILE);
1501 /* We allocated one too many - free it */
1502 prev_line->next = NULL;
1505 *pfile = first_line;
1510 /*********************************************************************
1512 * Function : edit_read_file
1514 * Description : Read a complete file into memory.
1515 * Handles CGI parameter parsing. If requested, also
1516 * checks the file's modification timestamp.
1519 * 1 : csp = Current client state (buffers, headers, etc...)
1520 * 2 : parameters = map of cgi parameters.
1521 * 3 : require_version = true to check "ver" parameter.
1522 * 4 : suffix = File extension, e.g. ".action".
1523 * 5 : pfile = Destination for the file. Will be set
1527 * filename : The name of the file to read, without the
1528 * path or ".action" extension.
1529 * ver : (Only if require_version is nonzero)
1530 * Timestamp of the actions file. If wrong, this
1531 * function fails with JB_ERR_MODIFIED.
1533 * Returns : JB_ERR_OK on success
1534 * JB_ERR_MEMORY on out-of-memory
1535 * JB_ERR_CGI_PARAMS if "filename" was not specified
1537 * JB_ERR_FILE if the file cannot be opened or
1539 * JB_ERR_MODIFIED if version checking was requested and
1540 * failed - the file was modified outside
1541 * of this CGI editor instance.
1543 *********************************************************************/
1544 jb_err edit_read_file(struct client_state *csp,
1545 const struct map *parameters,
1546 int require_version,
1548 struct editable_file **pfile)
1550 struct file_line * lines;
1554 const char * identifier;
1555 struct editable_file * file;
1556 unsigned version = 0;
1557 struct stat statbuf[1];
1558 char version_buf[22];
1559 int newline = NEWLINE_UNKNOWN;
1567 err = get_file_name_param(csp, parameters, "f", suffix,
1568 &filename, &identifier);
1574 if (stat(filename, statbuf) < 0)
1576 /* Error, probably file not found. */
1580 version = (unsigned) statbuf->st_mtime;
1582 if (require_version)
1584 unsigned specified_version;
1585 err = get_number_param(csp, parameters, "v", &specified_version);
1592 if (version != specified_version)
1594 return JB_ERR_MODIFIED;
1598 #if defined(AMIGA) || defined(__OS2__)
1599 if (NULL == (fp = fopen(filename,"r")))
1601 if (NULL == (fp = fopen(filename,"rt")))
1602 #endif /* def AMIGA */
1608 err = edit_read_file_lines(fp, &lines, &newline);
1618 file = (struct editable_file *) zalloc(sizeof(*file));
1622 edit_free_file_lines(lines);
1626 file->lines = lines;
1627 file->newline = newline;
1628 file->filename = filename;
1629 file->version = version;
1630 file->identifier = url_encode(identifier);
1632 if (file->identifier == NULL)
1634 edit_free_file(file);
1635 return JB_ERR_MEMORY;
1638 /* Correct file->version_str */
1639 freez(file->version_str);
1640 snprintf(version_buf, 22, "%u", file->version);
1641 version_buf[21] = '\0';
1642 file->version_str = strdup(version_buf);
1643 if (version_buf == NULL)
1645 edit_free_file(file);
1646 return JB_ERR_MEMORY;
1654 /*********************************************************************
1656 * Function : edit_read_actions_file
1658 * Description : Read a complete actions file into memory.
1659 * Handles CGI parameter parsing. If requested, also
1660 * checks the file's modification timestamp.
1662 * If this function detects an error in the categories
1663 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1664 * then it handles it by filling in the specified
1665 * response structure and returning JB_ERR_FILE.
1668 * 1 : csp = Current client state (buffers, headers, etc...)
1669 * 2 : rsp = HTTP response. Only filled in on error.
1670 * 2 : parameters = map of cgi parameters.
1671 * 3 : require_version = true to check "ver" parameter.
1672 * 4 : pfile = Destination for the file. Will be set
1676 * filename : The name of the actions file to read, without the
1677 * path or ".action" extension.
1678 * ver : (Only if require_version is nonzero)
1679 * Timestamp of the actions file. If wrong, this
1680 * function fails with JB_ERR_MODIFIED.
1682 * Returns : JB_ERR_OK on success
1683 * JB_ERR_MEMORY on out-of-memory
1684 * JB_ERR_CGI_PARAMS if "filename" was not specified
1686 * JB_ERR_FILE if the file does not contain valid data,
1687 * or if file cannot be opened or
1688 * contains no data, or if version
1689 * checking was requested and failed.
1691 *********************************************************************/
1692 jb_err edit_read_actions_file(struct client_state *csp,
1693 struct http_response *rsp,
1694 const struct map *parameters,
1695 int require_version,
1696 struct editable_file **pfile)
1699 struct editable_file *file;
1707 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1710 /* Try to handle if possible */
1711 if (err == JB_ERR_FILE)
1713 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1715 else if (err == JB_ERR_MODIFIED)
1717 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1719 if (err == JB_ERR_OK)
1722 * Signal to higher-level CGI code that there was a problem but we
1723 * handled it, they should just return JB_ERR_OK.
1730 err = edit_parse_actions_file(file);
1733 if (err == JB_ERR_PARSE)
1735 err = cgi_error_parse(csp, rsp, file);
1736 if (err == JB_ERR_OK)
1739 * Signal to higher-level CGI code that there was a problem but we
1740 * handled it, they should just return JB_ERR_OK.
1745 edit_free_file(file);
1754 /*********************************************************************
1756 * Function : get_file_name_param
1758 * Description : Get the name of the file to edit from the parameters
1759 * passed to a CGI function. This function handles
1760 * security checks such as blocking urls containing
1761 * "/" or ".", prepending the config file directory,
1762 * and adding the specified suffix.
1764 * (This is an essential security check, otherwise
1765 * users may be able to pass "../../../etc/passwd"
1766 * and overwrite the password file [linux], "prn:"
1767 * and print random data [Windows], etc...)
1769 * This function only allows filenames contining the
1770 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1771 * That's probably too restrictive but at least it's
1775 * 1 : csp = Current client state (buffers, headers, etc...)
1776 * 2 : parameters = map of cgi parameters
1777 * 3 : param_name = The name of the parameter to read
1778 * 4 : suffix = File extension, e.g. ".actions"
1779 * 5 : pfilename = destination for full filename. Caller
1780 * free()s. Set to NULL on error.
1781 * 6 : pparam = destination for partial filename,
1782 * suitable for use in another URL. Allocated as part
1783 * of the map "parameters", so don't free it.
1784 * Set to NULL if not specified.
1786 * Returns : JB_ERR_OK on success
1787 * JB_ERR_MEMORY on out-of-memory
1788 * JB_ERR_CGI_PARAMS if "filename" was not specified
1791 *********************************************************************/
1792 static jb_err get_file_name_param(struct client_state *csp,
1793 const struct map *parameters,
1794 const char *param_name,
1797 const char **pparam)
1801 #if 0 /* Patch to make 3.0.0 work properly. */
1803 #endif /* 0 - Patch to make 3.0.0 work properly. */
1817 param = lookup(parameters, param_name);
1820 return JB_ERR_CGI_PARAMS;
1825 len = strlen(param);
1826 if (len >= FILENAME_MAX)
1829 return JB_ERR_CGI_PARAMS;
1832 /* Check every character to see if it's legal */
1834 while ((ch = *s++) != '\0')
1836 if ( ((ch < 'A') || (ch > 'Z'))
1837 && ((ch < 'a') || (ch > 'z'))
1838 && ((ch < '0') || (ch > '9'))
1842 /* Probable hack attempt. */
1843 return JB_ERR_CGI_PARAMS;
1848 * FIXME Following is a hack to make 3.0.0 work properly.
1849 * Change "#if 0" --> "#if 1" below when we have modular action
1853 #if 0 /* Patch to make 3.0.0 work properly. */
1854 /* Append extension */
1855 name = malloc(len + strlen(suffix) + 1);
1858 return JB_ERR_MEMORY;
1860 strcpy(name, param);
1861 strcpy(name + len, suffix);
1864 fullpath = make_path(csp->config->confdir, name);
1866 #else /* 1 - Patch to make 3.0.0 work properly. */
1867 if ((csp->actions_list == NULL)
1868 || (csp->actions_list->filename == NULL))
1870 return JB_ERR_CGI_PARAMS;
1873 fullpath = ( (csp->actions_list && csp->actions_list->filename)
1874 ? strdup(csp->actions_list->filename) : NULL);
1875 #endif /* 1 - Patch to make 3.0.0 work properly. */
1876 if (fullpath == NULL)
1878 return JB_ERR_MEMORY;
1882 *pfilename = fullpath;
1888 /*********************************************************************
1890 * Function : get_char_param
1892 * Description : Get a single-character parameter passed to a CGI
1896 * 1 : parameters = map of cgi parameters
1897 * 2 : param_name = The name of the parameter to read
1899 * Returns : Uppercase character on success, '\0' on error.
1901 *********************************************************************/
1902 static char get_char_param(const struct map *parameters,
1903 const char *param_name)
1910 ch = *(lookup(parameters, param_name));
1911 if ((ch >= 'a') && (ch <= 'z'))
1913 ch = ch - 'a' + 'A';
1920 /*********************************************************************
1922 * Function : get_string_param
1924 * Description : Get a string paramater, to be used as an
1925 * ACTION_STRING or ACTION_MULTI paramater.
1926 * Validates the input to prevent stupid/malicious
1927 * users from corrupting their action file.
1930 * 1 : parameters = map of cgi parameters
1931 * 2 : param_name = The name of the parameter to read
1932 * 3 : pparam = destination for paramater. Allocated as
1933 * part of the map "parameters", so don't free it.
1934 * Set to NULL if not specified.
1936 * Returns : JB_ERR_OK on success, or if the paramater
1937 * was not specified.
1938 * JB_ERR_MEMORY on out-of-memory.
1939 * JB_ERR_CGI_PARAMS if the paramater is not valid.
1941 *********************************************************************/
1942 static jb_err get_string_param(const struct map *parameters,
1943 const char *param_name,
1944 const char **pparam)
1956 param = lookup(parameters, param_name);
1962 if (strlen(param) >= CGI_ACTION_PARAM_LEN_MAX)
1967 * Note that the length limit is arbitrary, it just seems
1968 * sensible to limit it to *something*. There's no
1969 * technical reason for any limit at all.
1971 return JB_ERR_CGI_PARAMS;
1974 /* Check every character to see if it's legal */
1976 while ((ch = *s++) != '\0')
1978 if ( ((unsigned char)ch < (unsigned char)' ')
1981 /* Probable hack attempt, or user accidentally used '}'. */
1982 return JB_ERR_CGI_PARAMS;
1993 /*********************************************************************
1995 * Function : get_number_param
1997 * Description : Get a non-negative integer from the parameters
1998 * passed to a CGI function.
2001 * 1 : csp = Current client state (buffers, headers, etc...)
2002 * 2 : parameters = map of cgi parameters
2003 * 3 : name = Name of CGI parameter to read
2004 * 4 : pvalue = destination for value.
2005 * Set to -1 on error.
2007 * Returns : JB_ERR_OK on success
2008 * JB_ERR_MEMORY on out-of-memory
2009 * JB_ERR_CGI_PARAMS if the parameter was not specified
2012 *********************************************************************/
2013 static jb_err get_number_param(struct client_state *csp,
2014 const struct map *parameters,
2029 param = lookup(parameters, name);
2032 return JB_ERR_CGI_PARAMS;
2035 /* We don't use atoi because I want to check this carefully... */
2038 while ((ch = *param++) != '\0')
2040 if ((ch < '0') || (ch > '9'))
2042 return JB_ERR_CGI_PARAMS;
2049 * <limits.h> defines UINT_MAX
2051 * (UINT_MAX - ch) / 10 is the largest number that
2052 * can be safely multiplied by 10 then have ch added.
2054 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
2056 return JB_ERR_CGI_PARAMS;
2059 value = value * 10 + ch;
2070 /*********************************************************************
2072 * Function : get_url_spec_param
2074 * Description : Get a URL pattern from the parameters
2075 * passed to a CGI function. Removes leading/trailing
2076 * spaces and validates it.
2079 * 1 : csp = Current client state (buffers, headers, etc...)
2080 * 2 : parameters = map of cgi parameters
2081 * 3 : name = Name of CGI parameter to read
2082 * 4 : pvalue = destination for value. Will be malloc()'d.
2083 * Set to NULL on error.
2085 * Returns : JB_ERR_OK on success
2086 * JB_ERR_MEMORY on out-of-memory
2087 * JB_ERR_CGI_PARAMS if the parameter was not specified
2090 *********************************************************************/
2091 static jb_err get_url_spec_param(struct client_state *csp,
2092 const struct map *parameters,
2096 const char *orig_param;
2099 struct url_spec compiled[1];
2109 orig_param = lookup(parameters, name);
2112 return JB_ERR_CGI_PARAMS;
2115 /* Copy and trim whitespace */
2116 param = strdup(orig_param);
2119 return JB_ERR_MEMORY;
2123 /* Must be non-empty, and can't allow 1st character to be '{' */
2124 if (param[0] == '\0' || param[0] == '{')
2127 return JB_ERR_CGI_PARAMS;
2130 /* Check for embedded newlines */
2131 for (s = param; *s != '\0'; s++)
2133 if ((*s == '\r') || (*s == '\n'))
2136 return JB_ERR_CGI_PARAMS;
2140 /* Check that regex is valid */
2145 return JB_ERR_MEMORY;
2147 err = create_url_spec(compiled, s);
2152 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2154 free_url_spec(compiled);
2156 if (param[strlen(param) - 1] == '\\')
2159 * Must protect trailing '\\' from becoming line continuation character.
2160 * Two methods: 1) If it's a domain only, add a trailing '/'.
2161 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2163 if (strchr(param, '/') == NULL)
2165 err = string_append(¶m, "/");
2169 err = string_append(¶m, "(?:)");
2176 /* Check that the modified regex is valid */
2181 return JB_ERR_MEMORY;
2183 err = create_url_spec(compiled, s);
2188 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2190 free_url_spec(compiled);
2197 /*********************************************************************
2199 * Function : map_radio
2201 * Description : Map a set of radio button values. E.g. if you have
2202 * 3 radio buttons, declare them as:
2203 * <option type="radio" name="xyz" @xyz-a@>
2204 * <option type="radio" name="xyz" @xyz-b@>
2205 * <option type="radio" name="xyz" @xyz-c@>
2206 * Then map one of the @xyz-?@ variables to "checked"
2207 * and all the others to empty by calling:
2208 * map_radio(exports, "xyz", "abc", sel)
2209 * Where 'sel' is 'a', 'b', or 'c'.
2212 * 1 : exports = Exports map to modify.
2213 * 2 : optionname = name for map
2214 * 3 : values = null-terminated list of values;
2215 * 4 : value = Selected value.
2217 * CGI Parameters : None
2219 * Returns : JB_ERR_OK on success
2220 * JB_ERR_MEMORY on out-of-memory
2222 *********************************************************************/
2223 static jb_err map_radio(struct map * exports,
2224 const char * optionname,
2225 const char * values,
2237 len = strlen(optionname);
2238 buf = malloc(len + 3);
2241 return JB_ERR_MEMORY;
2244 strcpy(buf, optionname);
2249 while ((c = *values++) != '\0')
2254 if (map(exports, buf, 1, "", 1))
2256 return JB_ERR_MEMORY;
2262 return map(exports, buf, 0, "checked", 1);
2266 /*********************************************************************
2268 * Function : cgi_error_modified
2270 * Description : CGI function that is called when a file is modified
2271 * outside the CGI editor.
2274 * 1 : csp = Current client state (buffers, headers, etc...)
2275 * 2 : rsp = http_response data structure for output
2276 * 3 : filename = The file that was modified.
2278 * CGI Parameters : none
2280 * Returns : JB_ERR_OK on success
2281 * JB_ERR_MEMORY on out-of-memory error.
2283 *********************************************************************/
2284 jb_err cgi_error_modified(struct client_state *csp,
2285 struct http_response *rsp,
2286 const char *filename)
2288 struct map *exports;
2295 if (NULL == (exports = default_exports(csp, NULL)))
2297 return JB_ERR_MEMORY;
2300 err = map(exports, "f", 1, html_encode(filename), 0);
2307 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2311 /*********************************************************************
2313 * Function : cgi_error_parse
2315 * Description : CGI function that is called when a file cannot
2316 * be parsed by the CGI editor.
2319 * 1 : csp = Current client state (buffers, headers, etc...)
2320 * 2 : rsp = http_response data structure for output
2321 * 3 : file = The file that was modified.
2323 * CGI Parameters : none
2325 * Returns : JB_ERR_OK on success
2326 * JB_ERR_MEMORY on out-of-memory error.
2328 *********************************************************************/
2329 jb_err cgi_error_parse(struct client_state *csp,
2330 struct http_response *rsp,
2331 struct editable_file *file)
2333 struct map *exports;
2335 struct file_line *cur_line;
2341 if (NULL == (exports = default_exports(csp, NULL)))
2343 return JB_ERR_MEMORY;
2346 err = map(exports, "f", 1, file->identifier, 1);
2347 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2349 cur_line = file->parse_error;
2352 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2353 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2361 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2365 /*********************************************************************
2367 * Function : cgi_error_file
2369 * Description : CGI function that is called when a file cannot be
2370 * opened by the CGI editor.
2373 * 1 : csp = Current client state (buffers, headers, etc...)
2374 * 2 : rsp = http_response data structure for output
2375 * 3 : filename = The file that was modified.
2377 * CGI Parameters : none
2379 * Returns : JB_ERR_OK on success
2380 * JB_ERR_MEMORY on out-of-memory error.
2382 *********************************************************************/
2383 jb_err cgi_error_file(struct client_state *csp,
2384 struct http_response *rsp,
2385 const char *filename)
2387 struct map *exports;
2394 if (NULL == (exports = default_exports(csp, NULL)))
2396 return JB_ERR_MEMORY;
2399 err = map(exports, "f", 1, html_encode(filename), 0);
2406 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2410 /*********************************************************************
2412 * Function : cgi_error_disabled
2414 * Description : CGI function that is called if the actions editor
2415 * is called although it's disabled in config
2418 * 1 : csp = Current client state (buffers, headers, etc...)
2419 * 2 : rsp = http_response data structure for output
2421 * CGI Parameters : none
2423 * Returns : JB_ERR_OK on success
2424 * JB_ERR_MEMORY on out-of-memory error.
2426 *********************************************************************/
2427 jb_err cgi_error_disabled(struct client_state *csp,
2428 struct http_response *rsp)
2430 struct map *exports;
2435 if (NULL == (exports = default_exports(csp, NULL)))
2437 return JB_ERR_MEMORY;
2440 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2444 /*********************************************************************
2446 * Function : cgi_edit_actions
2448 * Description : CGI function that allows the user to choose which
2449 * actions file to edit.
2452 * 1 : csp = Current client state (buffers, headers, etc...)
2453 * 2 : rsp = http_response data structure for output
2454 * 3 : parameters = map of cgi parameters
2456 * CGI Parameters : None
2458 * Returns : JB_ERR_OK on success
2459 * JB_ERR_MEMORY on out-of-memory error
2461 *********************************************************************/
2462 jb_err cgi_edit_actions(struct client_state *csp,
2463 struct http_response *rsp,
2464 const struct map *parameters)
2467 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2469 return cgi_error_disabled(csp, rsp);
2472 /* FIXME: Incomplete */
2473 rsp->status = strdup("302 Local Redirect from Privoxy");
2474 if (rsp->status == NULL)
2476 return JB_ERR_MEMORY;
2478 if (enlist_unique_header(rsp->headers, "Location",
2479 CGI_PREFIX "edit-actions-list?f=default"))
2483 return JB_ERR_MEMORY;
2490 /*********************************************************************
2492 * Function : cgi_edit_actions_list
2494 * Description : CGI function that edits the actions list.
2495 * FIXME: This function shouldn't FATAL ever.
2496 * FIXME: This function doesn't check the retval of map()
2498 * 1 : csp = Current client state (buffers, headers, etc...)
2499 * 2 : rsp = http_response data structure for output
2500 * 3 : parameters = map of cgi parameters
2502 * CGI Parameters : filename
2504 * Returns : JB_ERR_OK on success
2505 * JB_ERR_MEMORY on out-of-memory
2506 * JB_ERR_FILE if the file cannot be opened or
2508 * JB_ERR_CGI_PARAMS if "filename" was not specified
2511 *********************************************************************/
2512 jb_err cgi_edit_actions_list(struct client_state *csp,
2513 struct http_response *rsp,
2514 const struct map *parameters)
2516 char * section_template;
2517 char * url_template;
2522 struct map * exports;
2523 struct map * section_exports;
2524 struct map * url_exports;
2525 struct editable_file * file;
2526 struct file_line * cur_line;
2527 unsigned line_number = 0;
2528 unsigned prev_section_line_number = ((unsigned) (-1));
2532 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2534 return cgi_error_disabled(csp, rsp);
2537 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2540 /* No filename specified, can't read file, or out of memory. */
2541 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2544 if (NULL == (exports = default_exports(csp, NULL)))
2546 edit_free_file(file);
2547 return JB_ERR_MEMORY;
2550 err = map(exports, "f", 1, file->identifier, 1);
2551 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2555 edit_free_file(file);
2560 /* Should do all global exports above this point */
2562 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2565 edit_free_file(file);
2567 if (err == JB_ERR_FILE)
2569 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2574 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2577 free(section_template);
2578 edit_free_file(file);
2580 if (err == JB_ERR_FILE)
2582 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2587 err = template_fill(§ion_template, exports);
2591 edit_free_file(file);
2597 err = template_fill(&url_template, exports);
2600 free(section_template);
2601 edit_free_file(file);
2606 /* Find start of actions in file */
2607 cur_line = file->lines;
2609 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2611 cur_line = cur_line->next;
2615 if (NULL == (sections = strdup("")))
2617 free(section_template);
2619 edit_free_file(file);
2621 return JB_ERR_MEMORY;
2624 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2626 if (NULL == (section_exports = new_map()))
2629 free(section_template);
2631 edit_free_file(file);
2633 return JB_ERR_MEMORY;
2636 snprintf(buf, 50, "%d", line_number);
2637 err = map(section_exports, "s", 1, buf, 1);
2638 if (!err) err = map(section_exports, "actions", 1,
2639 actions_to_html(cur_line->data.action), 0);
2642 && (cur_line->next != NULL)
2643 && (cur_line->next->type == FILE_LINE_URL))
2645 /* This section contains at least one URL, don't allow delete */
2646 err = map_block_killer(section_exports, "empty-section");
2650 if (!err) err = map_block_keep(section_exports, "empty-section");
2653 if (prev_section_line_number != ((unsigned)(-1)))
2655 /* Not last section */
2656 snprintf(buf, 50, "%d", prev_section_line_number);
2657 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2658 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2663 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2665 prev_section_line_number = line_number;
2670 free(section_template);
2672 edit_free_file(file);
2674 free_map(section_exports);
2678 /* Should do all section-specific exports above this point */
2680 if (NULL == (urls = strdup("")))
2683 free(section_template);
2685 edit_free_file(file);
2687 free_map(section_exports);
2688 return JB_ERR_MEMORY;
2693 cur_line = cur_line->next;
2696 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2698 if (NULL == (url_exports = new_map()))
2702 free(section_template);
2704 edit_free_file(file);
2706 free_map(section_exports);
2707 return JB_ERR_MEMORY;
2710 snprintf(buf, 50, "%d", line_number);
2711 err = map(url_exports, "p", 1, buf, 1);
2713 snprintf(buf, 50, "%d", url_1_2);
2714 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2716 if (!err) err = map(url_exports, "url-html", 1,
2717 html_encode(cur_line->unprocessed), 0);
2718 if (!err) err = map(url_exports, "url", 1,
2719 url_encode(cur_line->unprocessed), 0);
2725 free(section_template);
2727 edit_free_file(file);
2729 free_map(section_exports);
2730 free_map(url_exports);
2734 if (NULL == (s = strdup(url_template)))
2738 free(section_template);
2740 edit_free_file(file);
2742 free_map(section_exports);
2743 free_map(url_exports);
2744 return JB_ERR_MEMORY;
2747 err = template_fill(&s, section_exports);
2748 if (!err) err = template_fill(&s, url_exports);
2749 if (!err) err = string_append(&urls, s);
2751 free_map(url_exports);
2758 free(section_template);
2760 edit_free_file(file);
2762 free_map(section_exports);
2766 url_1_2 = 3 - url_1_2;
2768 cur_line = cur_line->next;
2772 err = map(section_exports, "urls", 1, urls, 0);
2774 /* Could also do section-specific exports here, but it wouldn't be as fast */
2776 if ( (cur_line != NULL)
2777 && (cur_line->type == FILE_LINE_ACTION))
2779 /* Not last section */
2780 snprintf(buf, 50, "%d", line_number);
2781 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2782 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2787 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2793 free(section_template);
2795 edit_free_file(file);
2797 free_map(section_exports);
2801 if (NULL == (s = strdup(section_template)))
2804 free(section_template);
2806 edit_free_file(file);
2808 free_map(section_exports);
2809 return JB_ERR_MEMORY;
2812 err = template_fill(&s, section_exports);
2813 if (!err) err = string_append(§ions, s);
2816 free_map(section_exports);
2821 free(section_template);
2823 edit_free_file(file);
2829 edit_free_file(file);
2830 free(section_template);
2833 err = map(exports, "sections", 1, sections, 0);
2840 /* Could also do global exports here, but it wouldn't be as fast */
2842 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2846 /*********************************************************************
2848 * Function : cgi_edit_actions
2850 * Description : CGI function that edits the Actions list.
2853 * 1 : csp = Current client state (buffers, headers, etc...)
2854 * 2 : rsp = http_response data structure for output
2855 * 3 : parameters = map of cgi parameters
2857 * CGI Parameters : None
2859 * Returns : JB_ERR_OK on success
2860 * JB_ERR_MEMORY on out-of-memory
2861 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2862 * specified or not valid.
2864 *********************************************************************/
2865 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2866 struct http_response *rsp,
2867 const struct map *parameters)
2869 struct map * exports;
2871 struct editable_file * file;
2872 struct file_line * cur_line;
2873 unsigned line_number;
2875 struct file_list *filter_file;
2876 struct re_filterfile_spec *filter_group;
2878 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2880 return cgi_error_disabled(csp, rsp);
2883 err = get_number_param(csp, parameters, "s", §ionid);
2889 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2892 /* No filename specified, can't read file, modified, or out of memory. */
2893 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2896 cur_line = file->lines;
2898 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2900 cur_line = cur_line->next;
2903 if ( (cur_line == NULL)
2904 || (line_number != sectionid)
2906 || (cur_line->type != FILE_LINE_ACTION))
2908 /* Invalid "sectionid" parameter */
2909 edit_free_file(file);
2910 return JB_ERR_CGI_PARAMS;
2913 if (NULL == (exports = default_exports(csp, NULL)))
2915 edit_free_file(file);
2916 return JB_ERR_MEMORY;
2919 err = map(exports, "f", 1, file->identifier, 1);
2920 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2921 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2923 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2925 filter_file = csp->rlist;
2926 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2928 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2932 edit_free_file(file);
2937 if (filter_group == NULL)
2939 err = map(exports, "filter-params", 1, "", 1);
2943 /* We have some entries in the filter list */
2946 char * filter_template;
2948 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2951 edit_free_file(file);
2953 if (err == JB_ERR_FILE)
2955 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2960 result = strdup("");
2962 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2964 char current_mode = 'x';
2965 struct list_entry *filter_name;
2967 struct map *line_exports;
2970 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2971 while ((filter_name != NULL)
2972 && (0 != strcmp(filter_group->name, filter_name->str)))
2974 filter_name = filter_name->next;
2977 if (filter_name != NULL)
2983 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2984 while ((filter_name != NULL)
2985 && (0 != strcmp(filter_group->name, filter_name->str)))
2987 filter_name = filter_name->next;
2989 if (filter_name != NULL)
2995 /* Generate a unique serial number */
2996 snprintf(number, sizeof(number), "%x", index++);
2997 number[sizeof(number) - 1] = '\0';
2999 line_exports = new_map();
3000 if (line_exports == NULL)
3002 err = JB_ERR_MEMORY;
3007 if (!err) err = map(line_exports, "index", 1, number, 1);
3008 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3009 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3010 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3015 this_line = strdup(filter_template);
3016 if (this_line == NULL) err = JB_ERR_MEMORY;
3018 if (!err) err = template_fill(&this_line, line_exports);
3019 string_join(&result, this_line);
3021 free_map(line_exports);
3026 err = map(exports, "filter-params", 1, result, 0);
3034 if (!err) err = map_radio(exports, "filter-all", "nx",
3035 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3037 edit_free_file(file);
3045 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3049 /*********************************************************************
3051 * Function : cgi_edit_actions_submit
3053 * Description : CGI function that actually edits the Actions list.
3056 * 1 : csp = Current client state (buffers, headers, etc...)
3057 * 2 : rsp = http_response data structure for output
3058 * 3 : parameters = map of cgi parameters
3060 * CGI Parameters : None
3062 * Returns : JB_ERR_OK on success
3063 * JB_ERR_MEMORY on out-of-memory
3064 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3065 * specified or not valid.
3067 *********************************************************************/
3068 jb_err cgi_edit_actions_submit(struct client_state *csp,
3069 struct http_response *rsp,
3070 const struct map *parameters)
3076 struct editable_file * file;
3077 struct file_line * cur_line;
3078 unsigned line_number;
3084 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3086 return cgi_error_disabled(csp, rsp);
3089 err = get_number_param(csp, parameters, "s", §ionid);
3095 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3098 /* No filename specified, can't read file, modified, or out of memory. */
3099 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3102 cur_line = file->lines;
3104 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3106 cur_line = cur_line->next;
3109 if ( (cur_line == NULL)
3110 || (line_number != sectionid)
3112 || (cur_line->type != FILE_LINE_ACTION))
3114 /* Invalid "sectionid" parameter */
3115 edit_free_file(file);
3116 return JB_ERR_CGI_PARAMS;
3119 err = actions_from_radio(parameters, cur_line->data.action);
3123 edit_free_file(file);
3127 ch = get_char_param(parameters, "filter_all");
3130 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3131 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3132 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3136 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3139 for (index = 0; !err; index++)
3146 /* Generate the keys */
3147 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3148 key_value[sizeof(key_value) - 1] = '\0';
3149 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3150 key_name[sizeof(key_name) - 1] = '\0';
3152 err = get_string_param(parameters, key_name, &name);
3162 value = get_char_param(parameters, key_value);
3165 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3166 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3167 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3169 else if (value == 'N')
3171 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3172 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3174 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3175 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3178 else if (value == 'X')
3180 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3181 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3188 edit_free_file(file);
3192 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3195 edit_free_file(file);
3196 return JB_ERR_MEMORY;
3199 len = strlen(actiontext);
3203 * Empty action - must special-case this.
3204 * Simply setting len to 1 is sufficient...
3209 if (NULL == (newtext = malloc(len + 2)))
3213 edit_free_file(file);
3214 return JB_ERR_MEMORY;
3216 strcpy(newtext, actiontext);
3220 newtext[len + 1] = '\0';
3222 freez(cur_line->raw);
3223 freez(cur_line->unprocessed);
3224 cur_line->unprocessed = newtext;
3226 err = edit_write_file(file);
3229 /* Error writing file */
3230 edit_free_file(file);
3234 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3235 string_append(&target, file->identifier);
3237 edit_free_file(file);
3242 return JB_ERR_MEMORY;
3245 rsp->status = strdup("302 Local Redirect from Privoxy");
3246 if (rsp->status == NULL)
3249 return JB_ERR_MEMORY;
3251 err = enlist_unique_header(rsp->headers, "Location", target);
3258 /*********************************************************************
3260 * Function : cgi_edit_actions_url
3262 * Description : CGI function that actually edits a URL pattern in
3266 * 1 : csp = Current client state (buffers, headers, etc...)
3267 * 2 : rsp = http_response data structure for output
3268 * 3 : parameters = map of cgi parameters
3271 * filename : Identifies the file to edit
3272 * ver : File's last-modified time
3273 * section : Line number of section to edit
3274 * pattern : Line number of pattern to edit
3275 * newval : New value for pattern
3277 * Returns : JB_ERR_OK on success
3278 * JB_ERR_MEMORY on out-of-memory
3279 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3280 * specified or not valid.
3282 *********************************************************************/
3283 jb_err cgi_edit_actions_url(struct client_state *csp,
3284 struct http_response *rsp,
3285 const struct map *parameters)
3289 struct editable_file * file;
3290 struct file_line * cur_line;
3291 unsigned line_number;
3295 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3297 return cgi_error_disabled(csp, rsp);
3300 err = get_number_param(csp, parameters, "p", &patternid);
3307 return JB_ERR_CGI_PARAMS;
3310 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3316 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3319 /* No filename specified, can't read file, modified, or out of memory. */
3321 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3325 cur_line = file->lines;
3327 while ((cur_line != NULL) && (line_number < patternid))
3329 cur_line = cur_line->next;
3333 if ( (cur_line == NULL)
3334 || (cur_line->type != FILE_LINE_URL))
3336 /* Invalid "patternid" parameter */
3338 edit_free_file(file);
3339 return JB_ERR_CGI_PARAMS;
3342 /* At this point, the line to edit is in cur_line */
3344 freez(cur_line->raw);
3345 freez(cur_line->unprocessed);
3346 cur_line->unprocessed = new_pattern;
3348 err = edit_write_file(file);
3351 /* Error writing file */
3352 edit_free_file(file);
3356 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3357 string_append(&target, file->identifier);
3359 edit_free_file(file);
3364 return JB_ERR_MEMORY;
3367 rsp->status = strdup("302 Local Redirect from Privoxy");
3368 if (rsp->status == NULL)
3371 return JB_ERR_MEMORY;
3373 err = enlist_unique_header(rsp->headers, "Location", target);
3380 /*********************************************************************
3382 * Function : cgi_edit_actions_add_url
3384 * Description : CGI function that actually adds a URL pattern to
3388 * 1 : csp = Current client state (buffers, headers, etc...)
3389 * 2 : rsp = http_response data structure for output
3390 * 3 : parameters = map of cgi parameters
3393 * filename : Identifies the file to edit
3394 * ver : File's last-modified time
3395 * section : Line number of section to edit
3396 * newval : New pattern
3398 * Returns : JB_ERR_OK on success
3399 * JB_ERR_MEMORY on out-of-memory
3400 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3401 * specified or not valid.
3403 *********************************************************************/
3404 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3405 struct http_response *rsp,
3406 const struct map *parameters)
3410 struct file_line * new_line;
3411 struct editable_file * file;
3412 struct file_line * cur_line;
3413 unsigned line_number;
3417 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3419 return cgi_error_disabled(csp, rsp);
3422 err = get_number_param(csp, parameters, "s", §ionid);
3429 return JB_ERR_CGI_PARAMS;
3432 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3438 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3441 /* No filename specified, can't read file, modified, or out of memory. */
3443 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3447 cur_line = file->lines;
3449 while ((cur_line != NULL) && (line_number < sectionid))
3451 cur_line = cur_line->next;
3455 if ( (cur_line == NULL)
3456 || (cur_line->type != FILE_LINE_ACTION))
3458 /* Invalid "sectionid" parameter */
3460 edit_free_file(file);
3461 return JB_ERR_CGI_PARAMS;
3464 /* At this point, the section header is in cur_line - add after this. */
3466 /* Allocate the new line */
3467 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3468 if (new_line == NULL)
3471 edit_free_file(file);
3472 return JB_ERR_MEMORY;
3475 /* Fill in the data members of the new line */
3476 new_line->raw = NULL;
3477 new_line->prefix = NULL;
3478 new_line->unprocessed = new_pattern;
3479 new_line->type = FILE_LINE_URL;
3481 /* Link new_line into the list, after cur_line */
3482 new_line->next = cur_line->next;
3483 cur_line->next = new_line;
3485 /* Done making changes, now commit */
3487 err = edit_write_file(file);
3490 /* Error writing file */
3491 edit_free_file(file);
3495 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3496 string_append(&target, file->identifier);
3498 edit_free_file(file);
3503 return JB_ERR_MEMORY;
3506 rsp->status = strdup("302 Local Redirect from Privoxy");
3507 if (rsp->status == NULL)
3510 return JB_ERR_MEMORY;
3512 err = enlist_unique_header(rsp->headers, "Location", target);
3519 /*********************************************************************
3521 * Function : cgi_edit_actions_remove_url
3523 * Description : CGI function that actually removes a URL pattern from
3527 * 1 : csp = Current client state (buffers, headers, etc...)
3528 * 2 : rsp = http_response data structure for output
3529 * 3 : parameters = map of cgi parameters
3532 * f : (filename) Identifies the file to edit
3533 * v : (version) File's last-modified time
3534 * p : (pattern) Line number of pattern to remove
3536 * Returns : JB_ERR_OK on success
3537 * JB_ERR_MEMORY on out-of-memory
3538 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3539 * specified or not valid.
3541 *********************************************************************/
3542 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3543 struct http_response *rsp,
3544 const struct map *parameters)
3547 struct editable_file * file;
3548 struct file_line * cur_line;
3549 struct file_line * prev_line;
3550 unsigned line_number;
3554 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3556 return cgi_error_disabled(csp, rsp);
3559 err = get_number_param(csp, parameters, "p", &patternid);
3565 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3568 /* No filename specified, can't read file, modified, or out of memory. */
3569 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3574 cur_line = file->lines;
3576 while ((cur_line != NULL) && (line_number < patternid))
3578 prev_line = cur_line;
3579 cur_line = cur_line->next;
3583 if ( (cur_line == NULL)
3584 || (prev_line == NULL)
3585 || (cur_line->type != FILE_LINE_URL))
3587 /* Invalid "patternid" parameter */
3588 edit_free_file(file);
3589 return JB_ERR_CGI_PARAMS;
3592 /* At this point, the line to remove is in cur_line, and the previous
3593 * one is in prev_line
3596 /* Unlink cur_line */
3597 prev_line->next = cur_line->next;
3598 cur_line->next = NULL;
3601 edit_free_file_lines(cur_line);
3603 err = edit_write_file(file);
3606 /* Error writing file */
3607 edit_free_file(file);
3611 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3612 string_append(&target, file->identifier);
3614 edit_free_file(file);
3619 return JB_ERR_MEMORY;
3622 rsp->status = strdup("302 Local Redirect from Privoxy");
3623 if (rsp->status == NULL)
3626 return JB_ERR_MEMORY;
3628 err = enlist_unique_header(rsp->headers, "Location", target);
3635 /*********************************************************************
3637 * Function : cgi_edit_actions_section_remove
3639 * Description : CGI function that actually removes a whole section from
3640 * the actions file. The section must be empty first
3641 * (else JB_ERR_CGI_PARAMS).
3644 * 1 : csp = Current client state (buffers, headers, etc...)
3645 * 2 : rsp = http_response data structure for output
3646 * 3 : parameters = map of cgi parameters
3649 * f : (filename) Identifies the file to edit
3650 * v : (version) File's last-modified time
3651 * s : (section) Line number of section to edit
3653 * Returns : JB_ERR_OK on success
3654 * JB_ERR_MEMORY on out-of-memory
3655 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3656 * specified or not valid.
3658 *********************************************************************/
3659 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3660 struct http_response *rsp,
3661 const struct map *parameters)
3664 struct editable_file * file;
3665 struct file_line * cur_line;
3666 struct file_line * prev_line;
3667 unsigned line_number;
3671 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3673 return cgi_error_disabled(csp, rsp);
3676 err = get_number_param(csp, parameters, "s", §ionid);
3682 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3685 /* No filename specified, can't read file, modified, or out of memory. */
3686 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3690 cur_line = file->lines;
3693 while ((cur_line != NULL) && (line_number < sectionid))
3695 prev_line = cur_line;
3696 cur_line = cur_line->next;
3700 if ( (cur_line == NULL)
3701 || (cur_line->type != FILE_LINE_ACTION) )
3703 /* Invalid "sectionid" parameter */
3704 edit_free_file(file);
3705 return JB_ERR_CGI_PARAMS;
3708 if ( (cur_line->next != NULL)
3709 && (cur_line->next->type == FILE_LINE_URL) )
3711 /* Section not empty. */
3712 edit_free_file(file);
3713 return JB_ERR_CGI_PARAMS;
3716 /* At this point, the line to remove is in cur_line, and the previous
3717 * one is in prev_line
3720 /* Unlink cur_line */
3721 if (prev_line == NULL)
3723 /* Removing the first line from the file */
3724 file->lines = cur_line->next;
3728 prev_line->next = cur_line->next;
3730 cur_line->next = NULL;
3733 edit_free_file_lines(cur_line);
3735 err = edit_write_file(file);
3738 /* Error writing file */
3739 edit_free_file(file);
3743 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3744 string_append(&target, file->identifier);
3746 edit_free_file(file);
3751 return JB_ERR_MEMORY;
3754 rsp->status = strdup("302 Local Redirect from Privoxy");
3755 if (rsp->status == NULL)
3758 return JB_ERR_MEMORY;
3760 err = enlist_unique_header(rsp->headers, "Location", target);
3767 /*********************************************************************
3769 * Function : cgi_edit_actions_section_add
3771 * Description : CGI function that adds a new empty section to
3775 * 1 : csp = Current client state (buffers, headers, etc...)
3776 * 2 : rsp = http_response data structure for output
3777 * 3 : parameters = map of cgi parameters
3780 * f : (filename) Identifies the file to edit
3781 * v : (version) File's last-modified time
3782 * s : (section) Line number of section to add after, 0 for
3785 * Returns : JB_ERR_OK on success
3786 * JB_ERR_MEMORY on out-of-memory
3787 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3788 * specified or not valid.
3790 *********************************************************************/
3791 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3792 struct http_response *rsp,
3793 const struct map *parameters)
3796 struct file_line * new_line;
3798 struct editable_file * file;
3799 struct file_line * cur_line;
3800 unsigned line_number;
3804 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3806 return cgi_error_disabled(csp, rsp);
3809 err = get_number_param(csp, parameters, "s", §ionid);
3815 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3818 /* No filename specified, can't read file, modified, or out of memory. */
3819 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3823 cur_line = file->lines;
3827 /* Add to start of file */
3828 if (cur_line != NULL)
3830 /* There's something in the file, find the line before the first
3833 while ( (cur_line->next != NULL)
3834 && (cur_line->next->type != FILE_LINE_ACTION) )
3836 cur_line = cur_line->next;
3843 /* Add after stated section. */
3844 while ((cur_line != NULL) && (line_number < sectionid))
3846 cur_line = cur_line->next;
3850 if ( (cur_line == NULL)
3851 || (cur_line->type != FILE_LINE_ACTION))
3853 /* Invalid "sectionid" parameter */
3854 edit_free_file(file);
3855 return JB_ERR_CGI_PARAMS;
3858 /* Skip through the section to find the last line in it. */
3859 while ( (cur_line->next != NULL)
3860 && (cur_line->next->type != FILE_LINE_ACTION) )
3862 cur_line = cur_line->next;
3867 /* At this point, the last line in the previous section is in cur_line
3868 * - add after this. (Or if we need to add as the first line, cur_line
3872 new_text = strdup("{}");
3873 if (NULL == new_text)
3875 edit_free_file(file);
3876 return JB_ERR_MEMORY;
3879 /* Allocate the new line */
3880 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3881 if (new_line == NULL)
3884 edit_free_file(file);
3885 return JB_ERR_MEMORY;
3888 /* Fill in the data members of the new line */
3889 new_line->raw = NULL;
3890 new_line->prefix = NULL;
3891 new_line->unprocessed = new_text;
3892 new_line->type = FILE_LINE_ACTION;
3894 if (cur_line != NULL)
3896 /* Link new_line into the list, after cur_line */
3897 new_line->next = cur_line->next;
3898 cur_line->next = new_line;
3902 /* Link new_line into the list, as first line */
3903 new_line->next = file->lines;
3904 file->lines = new_line;
3907 /* Done making changes, now commit */
3909 err = edit_write_file(file);
3912 /* Error writing file */
3913 edit_free_file(file);
3917 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3918 string_append(&target, file->identifier);
3920 edit_free_file(file);
3925 return JB_ERR_MEMORY;
3928 rsp->status = strdup("302 Local Redirect from Privoxy");
3929 if (rsp->status == NULL)
3932 return JB_ERR_MEMORY;
3934 err = enlist_unique_header(rsp->headers, "Location", target);
3941 /*********************************************************************
3943 * Function : cgi_edit_actions_section_swap
3945 * Description : CGI function that swaps the order of two sections
3946 * in the actions file. Note that this CGI can actually
3947 * swap any two arbitrary sections, but the GUI interface
3948 * currently only allows consecutive sections to be
3952 * 1 : csp = Current client state (buffers, headers, etc...)
3953 * 2 : rsp = http_response data structure for output
3954 * 3 : parameters = map of cgi parameters
3957 * f : (filename) Identifies the file to edit
3958 * v : (version) File's last-modified time
3959 * s1 : (section1) Line number of first section to swap
3960 * s2 : (section2) Line number of second section to swap
3962 * Returns : JB_ERR_OK on success
3963 * JB_ERR_MEMORY on out-of-memory
3964 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3965 * specified or not valid.
3967 *********************************************************************/
3968 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3969 struct http_response *rsp,
3970 const struct map *parameters)
3974 struct editable_file * file;
3975 struct file_line * cur_line;
3976 struct file_line * prev_line;
3977 struct file_line * line_before_section1;
3978 struct file_line * line_start_section1;
3979 struct file_line * line_end_section1;
3980 struct file_line * line_after_section1;
3981 struct file_line * line_before_section2;
3982 struct file_line * line_start_section2;
3983 struct file_line * line_end_section2;
3984 struct file_line * line_after_section2;
3985 unsigned line_number;
3989 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3991 return cgi_error_disabled(csp, rsp);
3994 err = get_number_param(csp, parameters, "s1", §ion1);
3995 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4001 if (section1 > section2)
4003 unsigned temp = section2;
4004 section2 = section1;
4008 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4011 /* No filename specified, can't read file, modified, or out of memory. */
4012 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4015 /* Start at the beginning... */
4017 cur_line = file->lines;
4020 /* ... find section1 ... */
4021 while ((cur_line != NULL) && (line_number < section1))
4023 prev_line = cur_line;
4024 cur_line = cur_line->next;
4028 if ( (cur_line == NULL)
4029 || (cur_line->type != FILE_LINE_ACTION) )
4031 /* Invalid "section1" parameter */
4032 edit_free_file(file);
4033 return JB_ERR_CGI_PARAMS;
4036 /* If no-op, we've validated params and can skip the rest. */
4037 if (section1 != section2)
4039 /* ... find the end of section1 ... */
4040 line_before_section1 = prev_line;
4041 line_start_section1 = cur_line;
4044 prev_line = cur_line;
4045 cur_line = cur_line->next;
4048 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4049 line_end_section1 = prev_line;
4050 line_after_section1 = cur_line;
4052 /* ... find section2 ... */
4053 while ((cur_line != NULL) && (line_number < section2))
4055 prev_line = cur_line;
4056 cur_line = cur_line->next;
4060 if ( (cur_line == NULL)
4061 || (cur_line->type != FILE_LINE_ACTION) )
4063 /* Invalid "section2" parameter */
4064 edit_free_file(file);
4065 return JB_ERR_CGI_PARAMS;
4068 /* ... find the end of section2 ... */
4069 line_before_section2 = prev_line;
4070 line_start_section2 = cur_line;
4073 prev_line = cur_line;
4074 cur_line = cur_line->next;
4077 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4078 line_end_section2 = prev_line;
4079 line_after_section2 = cur_line;
4081 /* Now have all the pointers we need. Do the swap. */
4083 /* Change the pointer to section1 to point to section2 instead */
4084 if (line_before_section1 == NULL)
4086 file->lines = line_start_section2;
4090 line_before_section1->next = line_start_section2;
4093 if (line_before_section2 == line_end_section1)
4095 /* Consecutive sections */
4096 line_end_section2->next = line_start_section1;
4100 line_end_section2->next = line_after_section1;
4101 line_before_section2->next = line_start_section1;
4104 /* Set the pointer from the end of section1 to the rest of the file */
4105 line_end_section1->next = line_after_section2;
4107 err = edit_write_file(file);
4110 /* Error writing file */
4111 edit_free_file(file);
4114 } /* END if (section1 != section2) */
4116 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4117 string_append(&target, file->identifier);
4119 edit_free_file(file);
4124 return JB_ERR_MEMORY;
4127 rsp->status = strdup("302 Local Redirect from Privoxy");
4128 if (rsp->status == NULL)
4131 return JB_ERR_MEMORY;
4133 err = enlist_unique_header(rsp->headers, "Location", target);
4140 /*********************************************************************
4142 * Function : cgi_toggle
4144 * Description : CGI function that adds a new empty section to
4148 * 1 : csp = Current client state (buffers, headers, etc...)
4149 * 2 : rsp = http_response data structure for output
4150 * 3 : parameters = map of cgi parameters
4153 * set : If present, how to change toggle setting:
4154 * "enable", "disable", "toggle", or none (default).
4155 * mini : If present, use mini reply template.
4157 * Returns : JB_ERR_OK on success
4158 * JB_ERR_MEMORY on out-of-memory
4160 *********************************************************************/
4161 jb_err cgi_toggle(struct client_state *csp,
4162 struct http_response *rsp,
4163 const struct map *parameters)
4165 struct map *exports;
4167 const char *template_name;
4173 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4175 return cgi_error_disabled(csp, rsp);
4178 mode = get_char_param(parameters, "set");
4185 else if (mode == 'D')
4190 else if (mode == 'T')
4193 g_bToggleIJB = !g_bToggleIJB;
4196 if (NULL == (exports = default_exports(csp, "toggle")))
4198 return JB_ERR_MEMORY;
4201 template_name = (get_char_param(parameters, "mini")
4205 return template_fill_for_cgi(csp, template_name, exports, rsp);
4209 /*********************************************************************
4211 * Function : javascriptify
4213 * Description : Converts a string into a form JavaScript will like.
4215 * Netscape 4's JavaScript sucks - it doesn't use
4216 * "id" parameters, so you have to set the "name"
4217 * used to submit a form element to something JavaScript
4218 * will like. (Or access the elements by index in an
4219 * array. That array contains >60 elements and will
4220 * be changed whenever we add a new action to the
4221 * editor, so I'm NOT going to use indexes that have
4222 * to be figured out by hand.)
4224 * Currently the only thing we have to worry about
4225 * is "-" ==> "_" conversion.
4227 * This is a length-preserving operation so it is
4228 * carried out in-place, no memory is allocated
4232 * 1 : identifier = String to make JavaScript-friendly.
4236 *********************************************************************/
4237 static void javascriptify(char * identifier)
4239 char * p = identifier;
4240 while (NULL != (p = strchr(p, '-')))
4247 /*********************************************************************
4249 * Function : actions_to_radio
4251 * Description : Converts a actionsfile entry into settings for
4252 * radio buttons and edit boxes on a HTML form.
4255 * 1 : exports = List of substitutions to add to.
4256 * 2 : action = Action to read
4258 * Returns : JB_ERR_OK on success
4259 * JB_ERR_MEMORY on out-of-memory
4261 *********************************************************************/
4262 static jb_err actions_to_radio(struct map * exports,
4263 const struct action_spec *action)
4265 unsigned mask = action->mask;
4266 unsigned add = action->add;
4274 mask = action->mask;
4277 /* sanity - prevents "-feature +feature" */
4281 #define DEFINE_ACTION_BOOL(name, bit) \
4282 if (!(mask & bit)) \
4284 current_mode = 'n'; \
4286 else if (add & bit) \
4288 current_mode = 'y'; \
4292 current_mode = 'x'; \
4294 if (map_radio(exports, name, "ynx", current_mode)) \
4296 return JB_ERR_MEMORY; \
4299 #define DEFINE_ACTION_STRING(name, bit, index) \
4300 DEFINE_ACTION_BOOL(name, bit); \
4303 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4306 checked = !strcmp(action->string[index], value); \
4310 checked = is_default; \
4312 mapped_param |= checked; \
4313 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4315 return JB_ERR_MEMORY; \
4318 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4319 if (map(exports, name "-param-custom", 1, \
4320 ((!mapped_param) ? "checked" : ""), 1)) \
4322 return JB_ERR_MEMORY; \
4324 if (map(exports, name "-param", 1, \
4325 (((add & bit) && !mapped_param) ? \
4326 action->string[index] : default_val), 1)) \
4328 return JB_ERR_MEMORY; \
4331 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4332 if (map(exports, name "-param", 1, \
4333 ((add & bit) ? action->string[index] : default_val), 1)) \
4335 return JB_ERR_MEMORY; \
4338 #define DEFINE_ACTION_MULTI(name, index) \
4339 if (action->multi_add[index]->first) \
4341 current_mode = 'y'; \
4343 else if (action->multi_remove_all[index]) \
4345 current_mode = 'n'; \
4347 else if (action->multi_remove[index]->first) \
4349 current_mode = 'y'; \
4353 current_mode = 'x'; \
4355 if (map_radio(exports, name, "ynx", current_mode)) \
4357 return JB_ERR_MEMORY; \
4360 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4362 #include "actionlist.h"
4364 #undef DEFINE_ACTION_MULTI
4365 #undef DEFINE_ACTION_STRING
4366 #undef DEFINE_ACTION_BOOL
4367 #undef DEFINE_ACTION_ALIAS
4368 #undef DEFINE_CGI_PARAM_CUSTOM
4369 #undef DEFINE_CGI_PARAM_RADIO
4370 #undef DEFINE_CGI_PARAM_NO_RADIO
4376 /*********************************************************************
4378 * Function : actions_from_radio
4380 * Description : Converts a map of parameters passed to a CGI function
4381 * into an actionsfile entry.
4384 * 1 : parameters = parameters to the CGI call
4385 * 2 : action = Action to change. Must be valid before
4386 * the call, actions not specified will be
4389 * Returns : JB_ERR_OK on success
4390 * JB_ERR_MEMORY on out-of-memory
4392 *********************************************************************/
4393 static jb_err actions_from_radio(const struct map * parameters,
4394 struct action_spec *action)
4396 static int first_time = 1;
4400 const char * js_name;
4401 jb_err err = JB_ERR_OK;
4406 /* Statics are generally a potential race condition,
4407 * but in this case we're safe and don't need semaphores.
4408 * Be careful if you modify this function.
4412 #define JAVASCRIPTIFY(dest_var, string) \
4414 static char js_name_arr[] = string; \
4417 javascriptify(js_name_arr); \
4419 dest_var = js_name_arr; \
4422 #define DEFINE_ACTION_BOOL(name, bit) \
4423 JAVASCRIPTIFY(js_name, name); \
4424 ch = get_char_param(parameters, js_name); \
4427 action->add |= bit; \
4428 action->mask |= bit; \
4430 else if (ch == 'N') \
4432 action->add &= ~bit; \
4433 action->mask &= ~bit; \
4435 else if (ch == 'X') \
4437 action->add &= ~bit; \
4438 action->mask |= bit; \
4441 #define DEFINE_ACTION_STRING(name, bit, index) \
4442 JAVASCRIPTIFY(js_name, name); \
4443 ch = get_char_param(parameters, js_name); \
4447 JAVASCRIPTIFY(js_name, name "-mode"); \
4448 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4449 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4451 JAVASCRIPTIFY(js_name, name "-param"); \
4452 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4454 if (param != NULL) \
4456 if (NULL == (param_dup = strdup(param))) \
4458 return JB_ERR_MEMORY; \
4460 freez(action->string[index]); \
4461 action->add |= bit; \
4462 action->mask |= bit; \
4463 action->string[index] = param_dup; \
4466 else if (ch == 'N') \
4468 if (action->add & bit) \
4470 freez(action->string[index]); \
4472 action->add &= ~bit; \
4473 action->mask &= ~bit; \
4475 else if (ch == 'X') \
4477 if (action->add & bit) \
4479 freez(action->string[index]); \
4481 action->add &= ~bit; \
4482 action->mask |= bit; \
4485 #define DEFINE_ACTION_MULTI(name, index) \
4486 JAVASCRIPTIFY(js_name, name); \
4487 ch = get_char_param(parameters, js_name); \
4492 else if (ch == 'N') \
4494 list_remove_all(action->multi_add[index]); \
4495 list_remove_all(action->multi_remove[index]); \
4496 action->multi_remove_all[index] = 1; \
4498 else if (ch == 'X') \
4500 list_remove_all(action->multi_add[index]); \
4501 list_remove_all(action->multi_remove[index]); \
4502 action->multi_remove_all[index] = 0; \
4505 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4507 #include "actionlist.h"
4509 #undef DEFINE_ACTION_MULTI
4510 #undef DEFINE_ACTION_STRING
4511 #undef DEFINE_ACTION_BOOL
4512 #undef DEFINE_ACTION_ALIAS
4513 #undef JAVASCRIPTIFY
4521 #endif /* def FEATURE_CGI_EDIT_ACTIONS */