1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.30 2002/04/10 13:38:35 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.30 2002/04/10 13:38:35 oes
46 * load_template signature changed
48 * Revision 1.29 2002/04/08 16:59:08 oes
51 * Revision 1.28 2002/03/27 12:30:29 oes
52 * Deleted unsused variable
54 * Revision 1.27 2002/03/26 23:06:04 jongfoster
55 * Removing duplicate @ifs on the toggle page
57 * Revision 1.26 2002/03/26 22:59:17 jongfoster
58 * Fixing /toggle to display status consistently.
60 * Revision 1.25 2002/03/26 22:29:54 swa
61 * we have a new homepage!
63 * Revision 1.24 2002/03/24 15:23:33 jongfoster
66 * Revision 1.23 2002/03/24 13:32:41 swa
67 * name change related issues
69 * Revision 1.22 2002/03/24 13:25:43 swa
70 * name change related issues
72 * Revision 1.21 2002/03/22 18:02:48 jongfoster
73 * Fixing remote toggle
75 * Revision 1.20 2002/03/16 20:28:34 oes
76 * Added descriptions to the filters so users will know what they select in the cgi editor
78 * Revision 1.19 2002/03/16 18:38:14 jongfoster
79 * Stopping stupid or malicious users from breaking the actions
80 * file using the web-based editor.
82 * Revision 1.18 2002/03/16 14:57:44 jongfoster
83 * Full support for enabling/disabling modular filters.
85 * Revision 1.17 2002/03/16 14:26:42 jongfoster
86 * First version of modular filters support - READ ONLY!
87 * Fixing a double-free bug in the out-of-memory handling in map_radio().
89 * Revision 1.16 2002/03/07 03:46:17 oes
90 * Fixed compiler warnings
92 * Revision 1.15 2002/03/06 22:54:35 jongfoster
93 * Automated function-comment nitpicking.
95 * Revision 1.14 2002/03/05 00:24:51 jongfoster
96 * Patch to always edit the current actions file.
98 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
99 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
101 * Revision 1.12 2002/03/03 09:18:03 joergs
102 * Made jumbjuster work on AmigaOS again.
104 * Revision 1.11 2002/01/23 01:03:31 jongfoster
105 * Fixing gcc [CygWin] compiler warnings
107 * Revision 1.10 2002/01/23 00:22:59 jongfoster
108 * Adding new function cgi_edit_actions_section_swap(), to reorder
111 * Adding get_url_spec_param() to get a validated URL pattern.
113 * Moving edit_read_line() out of this file and into loaders.c.
115 * Adding missing html_encode() to many CGI functions.
117 * Moving the functions that #include actionlist.h to the end of the file,
118 * because the Visual C++ 97 debugger gets extremely confused if you try
119 * to debug any code that comes after them in the file.
121 * Major optimizations in cgi_edit_actions_list() to reduce the size of
122 * the generated HTML (down 40% from 550k to 304k), with major side-effects
123 * throughout the editor and templates. In particular, the length of the
124 * URLs throughout the editor has been drastically reduced, by cutting
125 * paramater names down to 1 character and CGI names down to 3-4
126 * characters, by removing all non-essential CGI paramaters even at the
127 * expense of having to re-read the actions file for the most trivial
128 * page, and by using relative rather than absolute URLs. This means
129 * that this (typical example):
131 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
132 * filename=ijb&ver=1011487572&section=12&pattern=13
133 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
137 * <a href="eau?f=ijb&v=1011487572&p=13">
139 * Revision 1.9 2002/01/17 20:56:22 jongfoster
140 * Replacing hard references to the URL of the config interface
141 * with #defines from project.h
143 * Revision 1.8 2001/11/30 23:35:51 jongfoster
144 * Renaming actionsfile to ijb.action
146 * Revision 1.7 2001/11/13 00:28:24 jongfoster
147 * - Renaming parameters from edit-actions-for-url so that they only
148 * contain legal JavaScript characters. If we wanted to write
149 * JavaScript that worked with Netscape 4, this is nessacery.
150 * (Note that at the moment the JavaScript doesn't actually work
151 * with Netscape 4, but now this is purely a template issue, not
152 * one affecting code).
153 * - Adding new CGIs for use by non-JavaScript browsers:
154 * edit-actions-url-form
155 * edit-actions-add-url-form
156 * edit-actions-remove-url-form
159 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
160 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
161 * by and __OS2__ ifdef.
163 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
164 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
165 * threads to call select() simultaneously. So, it's time to do a real, live,
166 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
167 * (native). Both versions will work, but using __OS2__ offers multi-threading.
169 * Revision 1.4 2001/10/23 21:48:19 jongfoster
170 * Cleaning up error handling in CGI functions - they now send back
171 * a HTML error page and should never cause a FATAL error. (Fixes one
172 * potential source of "denial of service" attacks).
174 * CGI actions file editor that works and is actually useful.
176 * Ability to toggle JunkBuster remotely using a CGI call.
178 * You can turn off both the above features in the main configuration
179 * file, e.g. if you are running a multi-user proxy.
181 * Revision 1.3 2001/10/14 22:12:49 jongfoster
182 * New version of CGI-based actionsfile editor.
183 * Major changes, including:
184 * - Completely new file parser and file output routines
185 * - edit-actions CGI renamed edit-actions-for-url
186 * - All CGIs now need a filename parameter, except for...
187 * - New CGI edit-actions which doesn't need a filename,
188 * to allow you to start the editor up.
189 * - edit-actions-submit now works, and now automatically
190 * redirects you back to the main edit-actions-list handler.
192 * Revision 1.2 2001/09/16 17:05:14 jongfoster
193 * Removing unused #include showarg.h
195 * Revision 1.1 2001/09/16 15:47:37 jongfoster
196 * First version of CGI-based edit interface. This is very much a
197 * work-in-progress, and you can't actually use it to edit anything
198 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
199 * to have any effect.
202 **********************************************************************/
208 * FIXME: Following includes copied from cgi.c - which are actually needed?
213 #include <sys/types.h>
218 #include <sys/stat.h>
221 #define snprintf _snprintf
222 #endif /* def _WIN32 */
227 #include "cgisimple.h"
231 #include "miscutil.h"
235 /* loadcfg.h is for g_bToggleIJB only */
236 #include "urlmatch.h"
238 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
241 #ifdef FEATURE_CGI_EDIT_ACTIONS
245 struct file_line * next;
253 struct action_spec action[1];
262 /* Add more data types here... e.g.
265 struct url_spec url[1];
269 struct action_spec action[1];
278 #define FILE_LINE_UNPROCESSED 1
279 #define FILE_LINE_BLANK 2
280 #define FILE_LINE_ALIAS_HEADER 3
281 #define FILE_LINE_ALIAS_ENTRY 4
282 #define FILE_LINE_ACTION 5
283 #define FILE_LINE_URL 6
284 #define FILE_LINE_SETTINGS_HEADER 7
285 #define FILE_LINE_SETTINGS_ENTRY 8
286 #define FILE_LINE_DESCRIPTION_HEADER 9
287 #define FILE_LINE_DESCRIPTION_ENTRY 10
292 struct file_line * lines;
293 const char * filename; /* Full pathname - e.g. "/etc/privoxy/wibble.action" */
294 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
295 /* Pre-encoded with url_encode() for ease of use. */
296 const char * version_str; /* Last modification time, as a string. For CGI param */
297 /* Can be used in URL without using url_param(). */
298 unsigned version; /* Last modification time - prevents chaos with
299 * the browser's "back" button. Note that this is a
300 * time_t cast to an unsigned. When comparing, always
301 * cast the time_t to an unsigned, and *NOT* vice-versa.
302 * This may lose the top few bits, but they're not
303 * significant anyway.
305 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
306 * Note that changing this after the file has been
307 * read in will cause a mess.
309 struct file_line * parse_error; /* On parse error, this is the offending line. */
310 const char * parse_error_text; /* On parse error, this is the problem.
311 * (Statically allocated) */
314 #define CGI_ACTION_PARAM_LEN_MAX 500
316 /* FIXME: Following non-static functions should be prototyped in .h or made static */
318 /* Functions to read and write arbitrary config files */
319 jb_err edit_read_file(struct client_state *csp,
320 const struct map *parameters,
323 struct editable_file **pfile);
324 jb_err edit_write_file(struct editable_file * file);
325 void edit_free_file(struct editable_file * file);
327 /* Functions to read and write actions files */
328 jb_err edit_parse_actions_file(struct editable_file * file);
329 jb_err edit_read_actions_file(struct client_state *csp,
330 struct http_response *rsp,
331 const struct map *parameters,
333 struct editable_file **pfile);
336 jb_err cgi_error_modified(struct client_state *csp,
337 struct http_response *rsp,
338 const char *filename);
339 jb_err cgi_error_parse(struct client_state *csp,
340 struct http_response *rsp,
341 struct editable_file *file);
342 jb_err cgi_error_file(struct client_state *csp,
343 struct http_response *rsp,
344 const char *filename);
345 jb_err cgi_error_disabled(struct client_state *csp,
346 struct http_response *rsp);
348 /* Internal arbitrary config file support functions */
349 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
350 static void edit_free_file_lines(struct file_line * first_line);
352 /* Internal actions file support functions */
353 static int match_actions_file_header_line(const char * line, const char * name);
354 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
356 /* Internal parameter parsing functions */
357 static jb_err get_file_name_param(struct client_state *csp,
358 const struct map *parameters,
359 const char *param_name,
362 const char **pparam);
363 static jb_err get_number_param(struct client_state *csp,
364 const struct map *parameters,
367 static jb_err get_url_spec_param(struct client_state *csp,
368 const struct map *parameters,
371 static jb_err get_string_param(const struct map *parameters,
372 const char *param_name,
373 const char **pparam);
375 /* Internal actionsfile <==> HTML conversion functions */
376 static jb_err map_radio(struct map * exports,
377 const char * optionname,
380 static jb_err actions_to_radio(struct map * exports,
381 const struct action_spec *action);
382 static jb_err actions_from_radio(const struct map * parameters,
383 struct action_spec *action);
386 static jb_err map_copy_parameter_html(struct map *out,
387 const struct map *in,
389 #if 0 /* unused function */
390 static jb_err map_copy_parameter_url(struct map *out,
391 const struct map *in,
393 #endif /* unused function */
395 /*********************************************************************
397 * Function : map_copy_parameter_html
399 * Description : Copy a CGI parameter from one map to another, HTML
403 * 1 : out = target map
404 * 2 : in = source map
405 * 3 : name = name of cgi parameter to copy
407 * Returns : JB_ERR_OK on success
408 * JB_ERR_MEMORY on out-of-memory
409 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
412 *********************************************************************/
413 static jb_err map_copy_parameter_html(struct map *out,
414 const struct map *in,
424 value = lookup(in, name);
425 err = map(out, name, 1, html_encode(value), 0);
432 else if (*value == '\0')
434 return JB_ERR_CGI_PARAMS;
443 #if 0 /* unused function */
444 /*********************************************************************
446 * Function : map_copy_parameter_html
448 * Description : Copy a CGI parameter from one map to another, URL
452 * 1 : out = target map
453 * 2 : in = source map
454 * 3 : name = name of cgi parameter to copy
456 * Returns : JB_ERR_OK on success
457 * JB_ERR_MEMORY on out-of-memory
458 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
461 *********************************************************************/
462 static jb_err map_copy_parameter_url(struct map *out,
463 const struct map *in,
473 value = lookup(in, name);
474 err = map(out, name, 1, url_encode(value), 0);
481 else if (*value == '\0')
483 return JB_ERR_CGI_PARAMS;
490 #endif /* 0 - unused function */
492 /*********************************************************************
494 * Function : cgi_edit_actions_url_form
496 * Description : CGI function that displays a form for
500 * 1 : csp = Current client state (buffers, headers, etc...)
501 * 2 : rsp = http_response data structure for output
502 * 3 : parameters = map of cgi parameters
505 * f : (filename) Identifies the file to edit
506 * v : (version) File's last-modified time
507 * p : (pattern) Line number of pattern to edit
509 * Returns : JB_ERR_OK on success
510 * JB_ERR_MEMORY on out-of-memory
511 * JB_ERR_CGI_PARAMS if the CGI parameters are not
512 * specified or not valid.
514 *********************************************************************/
515 jb_err cgi_edit_actions_url_form(struct client_state *csp,
516 struct http_response *rsp,
517 const struct map *parameters)
519 struct map * exports;
521 struct editable_file * file;
522 struct file_line * cur_line;
523 unsigned line_number;
530 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
532 return cgi_error_disabled(csp, rsp);
535 err = get_number_param(csp, parameters, "p", &patternid);
541 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
544 /* No filename specified, can't read file, modified, or out of memory. */
545 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
548 cur_line = file->lines;
550 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
552 cur_line = cur_line->next;
555 if ( (cur_line == NULL)
556 || (line_number != patternid)
558 || (cur_line->type != FILE_LINE_URL))
560 /* Invalid "patternid" parameter */
561 edit_free_file(file);
562 return JB_ERR_CGI_PARAMS;
565 if (NULL == (exports = default_exports(csp, NULL)))
567 edit_free_file(file);
568 return JB_ERR_MEMORY;
571 err = map(exports, "f", 1, file->identifier, 1);
572 if (!err) err = map(exports, "v", 1, file->version_str, 1);
573 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
574 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
576 edit_free_file(file);
584 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
588 /*********************************************************************
590 * Function : cgi_edit_actions_add_url_form
592 * Description : CGI function that displays a form for
596 * 1 : csp = Current client state (buffers, headers, etc...)
597 * 2 : rsp = http_response data structure for output
598 * 3 : parameters = map of cgi parameters
601 * f : (filename) Identifies the file to edit
602 * v : (version) File's last-modified time
603 * s : (section) Line number of section to edit
605 * Returns : JB_ERR_OK on success
606 * JB_ERR_MEMORY on out-of-memory
607 * JB_ERR_CGI_PARAMS if the CGI parameters are not
608 * specified or not valid.
610 *********************************************************************/
611 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
612 struct http_response *rsp,
613 const struct map *parameters)
622 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
624 return cgi_error_disabled(csp, rsp);
627 if (NULL == (exports = default_exports(csp, NULL)))
629 return JB_ERR_MEMORY;
632 err = map_copy_parameter_html(exports, parameters, "f");
633 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
634 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
642 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
646 /*********************************************************************
648 * Function : cgi_edit_actions_remove_url_form
650 * Description : CGI function that displays a form for
654 * 1 : csp = Current client state (buffers, headers, etc...)
655 * 2 : rsp = http_response data structure for output
656 * 3 : parameters = map of cgi parameters
659 * f : (filename) Identifies the file to edit
660 * v : (version) File's last-modified time
661 * p : (pattern) Line number of pattern to edit
663 * Returns : JB_ERR_OK on success
664 * JB_ERR_MEMORY on out-of-memory
665 * JB_ERR_CGI_PARAMS if the CGI parameters are not
666 * specified or not valid.
668 *********************************************************************/
669 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
670 struct http_response *rsp,
671 const struct map *parameters)
673 struct map * exports;
675 struct editable_file * file;
676 struct file_line * cur_line;
677 unsigned line_number;
684 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
686 return cgi_error_disabled(csp, rsp);
689 err = get_number_param(csp, parameters, "p", &patternid);
695 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
698 /* No filename specified, can't read file, modified, or out of memory. */
699 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
702 cur_line = file->lines;
704 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
706 cur_line = cur_line->next;
709 if ( (cur_line == NULL)
710 || (line_number != patternid)
712 || (cur_line->type != FILE_LINE_URL))
714 /* Invalid "patternid" parameter */
715 edit_free_file(file);
716 return JB_ERR_CGI_PARAMS;
719 if (NULL == (exports = default_exports(csp, NULL)))
721 edit_free_file(file);
722 return JB_ERR_MEMORY;
725 err = map(exports, "f", 1, file->identifier, 1);
726 if (!err) err = map(exports, "v", 1, file->version_str, 1);
727 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
728 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
730 edit_free_file(file);
738 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
742 /*********************************************************************
744 * Function : edit_write_file
746 * Description : Write a complete file to disk.
749 * 1 : filename = File to write to.
750 * 2 : file = Data structure to write.
752 * Returns : JB_ERR_OK on success
753 * JB_ERR_FILE on error writing to file.
754 * JB_ERR_MEMORY on out of memory
756 *********************************************************************/
757 jb_err edit_write_file(struct editable_file * file)
760 struct file_line * cur_line;
761 struct stat statbuf[1];
762 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
763 digits in time_t, assuming this is a 64-bit
764 machine, plus null terminator, plus one
768 assert(file->filename);
770 #if defined(AMIGA) || defined(__OS2__)
771 if (NULL == (fp = fopen(file->filename, "w")))
773 if (NULL == (fp = fopen(file->filename, "wt")))
774 #endif /* def AMIGA */
779 cur_line = file->lines;
780 while (cur_line != NULL)
784 if (fputs(cur_line->raw, fp) < 0)
792 if (cur_line->prefix)
794 if (fputs(cur_line->prefix, fp) < 0)
800 if (cur_line->unprocessed)
802 /* This should be a single line - sanity check. */
803 assert(NULL == strchr(cur_line->unprocessed, '\r'));
804 assert(NULL == strchr(cur_line->unprocessed, '\n'));
806 if (NULL != strchr(cur_line->unprocessed, '#'))
808 /* Must quote '#' characters */
815 /* Count number of # characters, so we know length of output string */
816 src = cur_line->unprocessed;
817 while (NULL != (src = strchr(src, '#')))
824 /* Allocate new memory for string */
825 len = strlen(cur_line->unprocessed);
826 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
828 /* Uh oh, just trashed file! */
830 return JB_ERR_MEMORY;
833 /* Loop through string from end */
834 src = cur_line->unprocessed + len;
835 dest = str + len + numhash;
836 for ( ; len >= 0; len--)
838 if ((*dest-- = *src--) == '#')
842 assert(numhash >= 0);
845 assert(numhash == 0);
846 assert(src + 1 == cur_line->unprocessed);
847 assert(dest + 1 == str);
849 if (fputs(str, fp) < 0)
860 /* Can write without quoting '#' characters. */
861 if (fputs(cur_line->unprocessed, fp) < 0)
867 if (fputs(NEWLINE(file->newline), fp) < 0)
875 /* FIXME: Write data from file->data->whatever */
879 cur_line = cur_line->next;
885 /* Update the version stamp in the file structure, since we just
886 * wrote to the file & changed it's date.
888 if (stat(file->filename, statbuf) < 0)
890 /* Error, probably file not found. */
893 file->version = (unsigned)statbuf->st_mtime;
895 /* Correct file->version_str */
896 freez(file->version_str);
897 snprintf(version_buf, 22, "%u", file->version);
898 version_buf[21] = '\0';
899 file->version_str = strdup(version_buf);
900 if (version_buf == NULL)
902 return JB_ERR_MEMORY;
909 /*********************************************************************
911 * Function : edit_free_file
913 * Description : Free a complete file in memory.
916 * 1 : file = Data structure to free.
920 *********************************************************************/
921 void edit_free_file(struct editable_file * file)
925 /* Silently ignore NULL pointer */
929 edit_free_file_lines(file->lines);
930 freez(file->filename);
931 freez(file->identifier);
932 freez(file->version_str);
934 file->parse_error_text = NULL; /* Statically allocated */
935 file->parse_error = NULL;
941 /*********************************************************************
943 * Function : edit_free_file
945 * Description : Free an entire linked list of file lines.
948 * 1 : first_line = Data structure to free.
952 *********************************************************************/
953 static void edit_free_file_lines(struct file_line * first_line)
955 struct file_line * next_line;
957 while (first_line != NULL)
959 next_line = first_line->next;
960 first_line->next = NULL;
961 freez(first_line->raw);
962 freez(first_line->prefix);
963 freez(first_line->unprocessed);
964 switch(first_line->type)
966 case 0: /* special case if memory zeroed */
967 case FILE_LINE_UNPROCESSED:
968 case FILE_LINE_BLANK:
969 case FILE_LINE_ALIAS_HEADER:
970 case FILE_LINE_SETTINGS_HEADER:
971 case FILE_LINE_DESCRIPTION_HEADER:
972 case FILE_LINE_DESCRIPTION_ENTRY:
973 case FILE_LINE_ALIAS_ENTRY:
975 /* No data is stored for these */
978 case FILE_LINE_ACTION:
979 free_action(first_line->data.action);
982 case FILE_LINE_SETTINGS_ENTRY:
983 freez(first_line->data.setting.name);
984 freez(first_line->data.setting.svalue);
987 /* Should never happen */
991 first_line->type = 0; /* paranoia */
993 first_line = next_line;
998 /*********************************************************************
1000 * Function : match_actions_file_header_line
1002 * Description : Match an actions file {{header}} line
1005 * 1 : line = String from file
1006 * 2 : name = Header to match against
1008 * Returns : 0 iff they match.
1010 *********************************************************************/
1011 static int match_actions_file_header_line(const char * line, const char * name)
1019 if ((line[0] != '{') || (line[1] != '{'))
1025 /* Look for optional whitespace */
1026 while ( (*line == ' ') || (*line == '\t') )
1031 /* Look for the specified name (case-insensitive) */
1033 if (0 != strncmpic(line, name, len))
1039 /* Look for optional whitespace */
1040 while ( (*line == ' ') || (*line == '\t') )
1045 /* Look for "}}" and end of string*/
1046 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1056 /*********************************************************************
1058 * Function : match_actions_file_header_line
1060 * Description : Match an actions file {{header}} line
1063 * 1 : line = String from file. Must not start with
1064 * whitespace (else infinite loop!)
1065 * 2 : name = Destination for name
1066 * 2 : name = Destination for value
1068 * Returns : JB_ERR_OK on success
1069 * JB_ERR_MEMORY on out-of-memory
1070 * JB_ERR_PARSE if there's no "=" sign, or if there's
1071 * nothing before the "=" sign (but empty
1072 * values *after* the "=" sign are legal).
1074 *********************************************************************/
1075 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1077 const char * name_end;
1078 const char * value_start;
1084 assert(*line != ' ');
1085 assert(*line != '\t');
1090 value_start = strchr(line, '=');
1091 if ((value_start == NULL) || (value_start == line))
1093 return JB_ERR_PARSE;
1096 name_end = value_start - 1;
1098 /* Eat any whitespace before the '=' */
1099 while ((*name_end == ' ') || (*name_end == '\t'))
1102 * we already know we must have at least 1 non-ws char
1103 * at start of buf - no need to check
1108 name_len = name_end - line + 1; /* Length excluding \0 */
1109 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1111 return JB_ERR_MEMORY;
1113 strncpy(*pname, line, name_len);
1114 (*pname)[name_len] = '\0';
1116 /* Eat any the whitespace after the '=' */
1118 while ((*value_start == ' ') || (*value_start == '\t'))
1123 if (NULL == (*pvalue = strdup(value_start)))
1127 return JB_ERR_MEMORY;
1134 /*********************************************************************
1136 * Function : edit_parse_actions_file
1138 * Description : Parse an actions file in memory.
1140 * Passed linked list must have the "data" member
1141 * zeroed, and must contain valid "next" and
1142 * "unprocessed" fields. The "raw" and "prefix"
1143 * fields are ignored, and "type" is just overwritten.
1145 * Note that on error the file may have been
1149 * 1 : file = Actions file to be parsed in-place.
1151 * Returns : JB_ERR_OK on success
1152 * JB_ERR_MEMORY on out-of-memory
1153 * JB_ERR_PARSE on error
1155 *********************************************************************/
1156 jb_err edit_parse_actions_file(struct editable_file * file)
1158 struct file_line * cur_line;
1160 const char * text; /* Text from a line */
1161 char * name; /* For lines of the form name=value */
1162 char * value; /* For lines of the form name=value */
1163 struct action_alias * alias_list = NULL;
1164 jb_err err = JB_ERR_OK;
1166 /* alias_list contains the aliases defined in this file.
1167 * It might be better to use the "file_line.data" fields
1168 * in the relavent places instead.
1171 cur_line = file->lines;
1173 /* A note about blank line support: Blank lines should only
1174 * ever occur as the last line in the file. This function
1175 * is more forgiving than that - FILE_LINE_BLANK can occur
1179 /* Skip leading blanks. Should only happen if file is
1180 * empty (which is valid, but pointless).
1182 while ( (cur_line != NULL)
1183 && (cur_line->unprocessed[0] == '\0') )
1186 cur_line->type = FILE_LINE_BLANK;
1187 cur_line = cur_line->next;
1190 if ( (cur_line != NULL)
1191 && (cur_line->unprocessed[0] != '{') )
1193 /* File doesn't start with a header */
1194 file->parse_error = cur_line;
1195 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1196 return JB_ERR_PARSE;
1199 if ( (cur_line != NULL) && (0 ==
1200 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1202 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1204 cur_line = cur_line->next;
1205 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1207 if (cur_line->unprocessed[0])
1209 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1211 err = split_line_on_equals(cur_line->unprocessed,
1212 &cur_line->data.setting.name,
1213 &cur_line->data.setting.svalue);
1214 if (err == JB_ERR_MEMORY)
1218 else if (err != JB_ERR_OK)
1220 /* Line does not contain a name=value pair */
1221 file->parse_error = cur_line;
1222 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1223 return JB_ERR_PARSE;
1228 cur_line->type = FILE_LINE_BLANK;
1230 cur_line = cur_line->next;
1234 if ( (cur_line != NULL) && (0 ==
1235 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1237 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1239 cur_line = cur_line->next;
1240 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1242 if (cur_line->unprocessed[0])
1244 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1248 cur_line->type = FILE_LINE_BLANK;
1250 cur_line = cur_line->next;
1254 if ( (cur_line != NULL) && (0 ==
1255 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1257 cur_line->type = FILE_LINE_ALIAS_HEADER;
1259 cur_line = cur_line->next;
1260 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1262 if (cur_line->unprocessed[0])
1264 /* define an alias */
1265 struct action_alias * new_alias;
1267 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1269 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1270 if (err == JB_ERR_MEMORY)
1274 else if (err != JB_ERR_OK)
1276 /* Line does not contain a name=value pair */
1277 file->parse_error = cur_line;
1278 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1279 return JB_ERR_PARSE;
1282 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1287 free_alias_list(alias_list);
1288 return JB_ERR_MEMORY;
1291 err = get_actions(value, alias_list, new_alias->action);
1294 /* Invalid action or out of memory */
1298 free_alias_list(alias_list);
1299 if (err == JB_ERR_MEMORY)
1305 /* Line does not contain a name=value pair */
1306 file->parse_error = cur_line;
1307 file->parse_error_text = "This alias does not specify a valid set of actions.";
1308 return JB_ERR_PARSE;
1314 new_alias->name = name;
1317 new_alias->next = alias_list;
1318 alias_list = new_alias;
1322 cur_line->type = FILE_LINE_BLANK;
1324 cur_line = cur_line->next;
1328 /* Header done, process the main part of the file */
1329 while (cur_line != NULL)
1331 /* At this point, (cur_line->unprocessed[0] == '{') */
1332 assert(cur_line->unprocessed[0] == '{');
1333 text = cur_line->unprocessed + 1;
1334 len = strlen(text) - 1;
1335 if (text[len] != '}')
1337 /* No closing } on header */
1338 free_alias_list(alias_list);
1339 file->parse_error = cur_line;
1340 file->parse_error_text = "Headers starting with '{' must have a "
1341 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1342 "must close with two brackets ('}}').";
1343 return JB_ERR_PARSE;
1348 /* An invalid {{ header. */
1349 free_alias_list(alias_list);
1350 file->parse_error = cur_line;
1351 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1352 "Please remember that the system (two-bracket) headers must "
1353 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1354 "and must appear before any actions (one-bracket) headers. "
1355 "Also note that system headers may not be repeated.";
1356 return JB_ERR_PARSE;
1359 while ( (*text == ' ') || (*text == '\t') )
1365 && ( (text[len - 1] == ' ')
1366 || (text[len - 1] == '\t') ) )
1371 cur_line->type = FILE_LINE_ACTION;
1373 /* Remove {} and make copy */
1374 if (NULL == (value = (char *) malloc(len + 1)))
1377 free_alias_list(alias_list);
1378 return JB_ERR_MEMORY;
1380 strncpy(value, text, len);
1384 err = get_actions(value, alias_list, cur_line->data.action);
1387 /* Invalid action or out of memory */
1389 free_alias_list(alias_list);
1390 if (err == JB_ERR_MEMORY)
1396 /* Line does not contain a name=value pair */
1397 file->parse_error = cur_line;
1398 file->parse_error_text = "This header does not specify a valid set of actions.";
1399 return JB_ERR_PARSE;
1403 /* Done with string - it was clobbered anyway */
1406 /* Process next line */
1407 cur_line = cur_line->next;
1409 /* Loop processing URL patterns */
1410 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1412 if (cur_line->unprocessed[0])
1414 /* Could parse URL here, but this isn't currently needed */
1416 cur_line->type = FILE_LINE_URL;
1420 cur_line->type = FILE_LINE_BLANK;
1422 cur_line = cur_line->next;
1424 } /* End main while(cur_line != NULL) loop */
1426 free_alias_list(alias_list);
1432 /*********************************************************************
1434 * Function : edit_read_file_lines
1436 * Description : Read all the lines of a file into memory.
1437 * Handles whitespace, comments and line continuation.
1440 * 1 : fp = File to read from. On return, this will be
1441 * at EOF but it will not have been closed.
1442 * 2 : pfile = Destination for a linked list of file_lines.
1443 * Will be set to NULL on error.
1445 * Returns : JB_ERR_OK on success
1446 * JB_ERR_MEMORY on out-of-memory
1448 *********************************************************************/
1449 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1451 struct file_line * first_line; /* Keep for return value or to free */
1452 struct file_line * cur_line; /* Current line */
1453 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1461 cur_line = first_line = zalloc(sizeof(struct file_line));
1462 if (cur_line == NULL)
1464 return JB_ERR_MEMORY;
1467 cur_line->type = FILE_LINE_UNPROCESSED;
1469 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1472 /* Out of memory or empty file. */
1473 /* Note that empty file is not an error we propogate up */
1475 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1480 prev_line = cur_line;
1481 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1482 if (cur_line == NULL)
1485 edit_free_file_lines(first_line);
1486 return JB_ERR_MEMORY;
1489 cur_line->type = FILE_LINE_UNPROCESSED;
1491 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1492 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1495 edit_free_file_lines(first_line);
1496 return JB_ERR_MEMORY;
1500 while (rval != JB_ERR_FILE);
1504 /* We allocated one too many - free it */
1505 prev_line->next = NULL;
1508 *pfile = first_line;
1513 /*********************************************************************
1515 * Function : edit_read_file
1517 * Description : Read a complete file into memory.
1518 * Handles CGI parameter parsing. If requested, also
1519 * checks the file's modification timestamp.
1522 * 1 : csp = Current client state (buffers, headers, etc...)
1523 * 2 : parameters = map of cgi parameters.
1524 * 3 : require_version = true to check "ver" parameter.
1525 * 4 : suffix = File extension, e.g. ".action".
1526 * 5 : pfile = Destination for the file. Will be set
1530 * filename : The name of the file to read, without the
1531 * path or ".action" extension.
1532 * ver : (Only if require_version is nonzero)
1533 * Timestamp of the actions file. If wrong, this
1534 * function fails with JB_ERR_MODIFIED.
1536 * Returns : JB_ERR_OK on success
1537 * JB_ERR_MEMORY on out-of-memory
1538 * JB_ERR_CGI_PARAMS if "filename" was not specified
1540 * JB_ERR_FILE if the file cannot be opened or
1542 * JB_ERR_MODIFIED if version checking was requested and
1543 * failed - the file was modified outside
1544 * of this CGI editor instance.
1546 *********************************************************************/
1547 jb_err edit_read_file(struct client_state *csp,
1548 const struct map *parameters,
1549 int require_version,
1551 struct editable_file **pfile)
1553 struct file_line * lines;
1557 const char * identifier;
1558 struct editable_file * file;
1559 unsigned version = 0;
1560 struct stat statbuf[1];
1561 char version_buf[22];
1562 int newline = NEWLINE_UNKNOWN;
1570 err = get_file_name_param(csp, parameters, "f", suffix,
1571 &filename, &identifier);
1577 if (stat(filename, statbuf) < 0)
1579 /* Error, probably file not found. */
1583 version = (unsigned) statbuf->st_mtime;
1585 if (require_version)
1587 unsigned specified_version;
1588 err = get_number_param(csp, parameters, "v", &specified_version);
1595 if (version != specified_version)
1597 return JB_ERR_MODIFIED;
1601 #if defined(AMIGA) || defined(__OS2__)
1602 if (NULL == (fp = fopen(filename,"r")))
1604 if (NULL == (fp = fopen(filename,"rt")))
1605 #endif /* def AMIGA */
1611 err = edit_read_file_lines(fp, &lines, &newline);
1621 file = (struct editable_file *) zalloc(sizeof(*file));
1625 edit_free_file_lines(lines);
1629 file->lines = lines;
1630 file->newline = newline;
1631 file->filename = filename;
1632 file->version = version;
1633 file->identifier = url_encode(identifier);
1635 if (file->identifier == NULL)
1637 edit_free_file(file);
1638 return JB_ERR_MEMORY;
1641 /* Correct file->version_str */
1642 freez(file->version_str);
1643 snprintf(version_buf, 22, "%u", file->version);
1644 version_buf[21] = '\0';
1645 file->version_str = strdup(version_buf);
1646 if (version_buf == NULL)
1648 edit_free_file(file);
1649 return JB_ERR_MEMORY;
1657 /*********************************************************************
1659 * Function : edit_read_actions_file
1661 * Description : Read a complete actions file into memory.
1662 * Handles CGI parameter parsing. If requested, also
1663 * checks the file's modification timestamp.
1665 * If this function detects an error in the categories
1666 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1667 * then it handles it by filling in the specified
1668 * response structure and returning JB_ERR_FILE.
1671 * 1 : csp = Current client state (buffers, headers, etc...)
1672 * 2 : rsp = HTTP response. Only filled in on error.
1673 * 2 : parameters = map of cgi parameters.
1674 * 3 : require_version = true to check "ver" parameter.
1675 * 4 : pfile = Destination for the file. Will be set
1679 * filename : The name of the actions file to read, without the
1680 * path or ".action" extension.
1681 * ver : (Only if require_version is nonzero)
1682 * Timestamp of the actions file. If wrong, this
1683 * function fails with JB_ERR_MODIFIED.
1685 * Returns : JB_ERR_OK on success
1686 * JB_ERR_MEMORY on out-of-memory
1687 * JB_ERR_CGI_PARAMS if "filename" was not specified
1689 * JB_ERR_FILE if the file does not contain valid data,
1690 * or if file cannot be opened or
1691 * contains no data, or if version
1692 * checking was requested and failed.
1694 *********************************************************************/
1695 jb_err edit_read_actions_file(struct client_state *csp,
1696 struct http_response *rsp,
1697 const struct map *parameters,
1698 int require_version,
1699 struct editable_file **pfile)
1702 struct editable_file *file;
1710 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1713 /* Try to handle if possible */
1714 if (err == JB_ERR_FILE)
1716 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1718 else if (err == JB_ERR_MODIFIED)
1720 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1722 if (err == JB_ERR_OK)
1725 * Signal to higher-level CGI code that there was a problem but we
1726 * handled it, they should just return JB_ERR_OK.
1733 err = edit_parse_actions_file(file);
1736 if (err == JB_ERR_PARSE)
1738 err = cgi_error_parse(csp, rsp, file);
1739 if (err == JB_ERR_OK)
1742 * Signal to higher-level CGI code that there was a problem but we
1743 * handled it, they should just return JB_ERR_OK.
1748 edit_free_file(file);
1757 /*********************************************************************
1759 * Function : get_file_name_param
1761 * Description : Get the name of the file to edit from the parameters
1762 * passed to a CGI function. This function handles
1763 * security checks such as blocking urls containing
1764 * "/" or ".", prepending the config file directory,
1765 * and adding the specified suffix.
1767 * (This is an essential security check, otherwise
1768 * users may be able to pass "../../../etc/passwd"
1769 * and overwrite the password file [linux], "prn:"
1770 * and print random data [Windows], etc...)
1772 * This function only allows filenames contining the
1773 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1774 * That's probably too restrictive but at least it's
1778 * 1 : csp = Current client state (buffers, headers, etc...)
1779 * 2 : parameters = map of cgi parameters
1780 * 3 : param_name = The name of the parameter to read
1781 * 4 : suffix = File extension, e.g. ".actions"
1782 * 5 : pfilename = destination for full filename. Caller
1783 * free()s. Set to NULL on error.
1784 * 6 : pparam = destination for partial filename,
1785 * suitable for use in another URL. Allocated as part
1786 * of the map "parameters", so don't free it.
1787 * Set to NULL if not specified.
1789 * Returns : JB_ERR_OK on success
1790 * JB_ERR_MEMORY on out-of-memory
1791 * JB_ERR_CGI_PARAMS if "filename" was not specified
1794 *********************************************************************/
1795 static jb_err get_file_name_param(struct client_state *csp,
1796 const struct map *parameters,
1797 const char *param_name,
1800 const char **pparam)
1804 #if 0 /* Patch to make 3.0.0 work properly. */
1806 #endif /* 0 - Patch to make 3.0.0 work properly. */
1820 param = lookup(parameters, param_name);
1823 return JB_ERR_CGI_PARAMS;
1828 len = strlen(param);
1829 if (len >= FILENAME_MAX)
1832 return JB_ERR_CGI_PARAMS;
1835 /* Check every character to see if it's legal */
1837 while ((ch = *s++) != '\0')
1839 if ( ((ch < 'A') || (ch > 'Z'))
1840 && ((ch < 'a') || (ch > 'z'))
1841 && ((ch < '0') || (ch > '9'))
1845 /* Probable hack attempt. */
1846 return JB_ERR_CGI_PARAMS;
1851 * FIXME Following is a hack to make 3.0.0 work properly.
1852 * Change "#if 0" --> "#if 1" below when we have modular action
1856 #if 0 /* Patch to make 3.0.0 work properly. */
1857 /* Append extension */
1858 name = malloc(len + strlen(suffix) + 1);
1861 return JB_ERR_MEMORY;
1863 strcpy(name, param);
1864 strcpy(name + len, suffix);
1867 fullpath = make_path(csp->config->confdir, name);
1869 #else /* 1 - Patch to make 3.0.0 work properly. */
1870 if ((csp->actions_list == NULL)
1871 || (csp->actions_list->filename == NULL))
1873 return JB_ERR_CGI_PARAMS;
1876 fullpath = ( (csp->actions_list && csp->actions_list->filename)
1877 ? strdup(csp->actions_list->filename) : NULL);
1878 #endif /* 1 - Patch to make 3.0.0 work properly. */
1879 if (fullpath == NULL)
1881 return JB_ERR_MEMORY;
1885 *pfilename = fullpath;
1891 /*********************************************************************
1893 * Function : get_char_param
1895 * Description : Get a single-character parameter passed to a CGI
1899 * 1 : parameters = map of cgi parameters
1900 * 2 : param_name = The name of the parameter to read
1902 * Returns : Uppercase character on success, '\0' on error.
1904 *********************************************************************/
1905 static char get_char_param(const struct map *parameters,
1906 const char *param_name)
1913 ch = *(lookup(parameters, param_name));
1914 if ((ch >= 'a') && (ch <= 'z'))
1916 ch = ch - 'a' + 'A';
1923 /*********************************************************************
1925 * Function : get_string_param
1927 * Description : Get a string paramater, to be used as an
1928 * ACTION_STRING or ACTION_MULTI paramater.
1929 * Validates the input to prevent stupid/malicious
1930 * users from corrupting their action file.
1933 * 1 : parameters = map of cgi parameters
1934 * 2 : param_name = The name of the parameter to read
1935 * 3 : pparam = destination for paramater. Allocated as
1936 * part of the map "parameters", so don't free it.
1937 * Set to NULL if not specified.
1939 * Returns : JB_ERR_OK on success, or if the paramater
1940 * was not specified.
1941 * JB_ERR_MEMORY on out-of-memory.
1942 * JB_ERR_CGI_PARAMS if the paramater is not valid.
1944 *********************************************************************/
1945 static jb_err get_string_param(const struct map *parameters,
1946 const char *param_name,
1947 const char **pparam)
1959 param = lookup(parameters, param_name);
1965 if (strlen(param) >= CGI_ACTION_PARAM_LEN_MAX)
1970 * Note that the length limit is arbitrary, it just seems
1971 * sensible to limit it to *something*. There's no
1972 * technical reason for any limit at all.
1974 return JB_ERR_CGI_PARAMS;
1977 /* Check every character to see if it's legal */
1979 while ((ch = *s++) != '\0')
1981 if ( ((unsigned char)ch < (unsigned char)' ')
1984 /* Probable hack attempt, or user accidentally used '}'. */
1985 return JB_ERR_CGI_PARAMS;
1996 /*********************************************************************
1998 * Function : get_number_param
2000 * Description : Get a non-negative integer from the parameters
2001 * passed to a CGI function.
2004 * 1 : csp = Current client state (buffers, headers, etc...)
2005 * 2 : parameters = map of cgi parameters
2006 * 3 : name = Name of CGI parameter to read
2007 * 4 : pvalue = destination for value.
2008 * Set to -1 on error.
2010 * Returns : JB_ERR_OK on success
2011 * JB_ERR_MEMORY on out-of-memory
2012 * JB_ERR_CGI_PARAMS if the parameter was not specified
2015 *********************************************************************/
2016 static jb_err get_number_param(struct client_state *csp,
2017 const struct map *parameters,
2032 param = lookup(parameters, name);
2035 return JB_ERR_CGI_PARAMS;
2038 /* We don't use atoi because I want to check this carefully... */
2041 while ((ch = *param++) != '\0')
2043 if ((ch < '0') || (ch > '9'))
2045 return JB_ERR_CGI_PARAMS;
2052 * <limits.h> defines UINT_MAX
2054 * (UINT_MAX - ch) / 10 is the largest number that
2055 * can be safely multiplied by 10 then have ch added.
2057 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
2059 return JB_ERR_CGI_PARAMS;
2062 value = value * 10 + ch;
2073 /*********************************************************************
2075 * Function : get_url_spec_param
2077 * Description : Get a URL pattern from the parameters
2078 * passed to a CGI function. Removes leading/trailing
2079 * spaces and validates it.
2082 * 1 : csp = Current client state (buffers, headers, etc...)
2083 * 2 : parameters = map of cgi parameters
2084 * 3 : name = Name of CGI parameter to read
2085 * 4 : pvalue = destination for value. Will be malloc()'d.
2086 * Set to NULL on error.
2088 * Returns : JB_ERR_OK on success
2089 * JB_ERR_MEMORY on out-of-memory
2090 * JB_ERR_CGI_PARAMS if the parameter was not specified
2093 *********************************************************************/
2094 static jb_err get_url_spec_param(struct client_state *csp,
2095 const struct map *parameters,
2099 const char *orig_param;
2102 struct url_spec compiled[1];
2112 orig_param = lookup(parameters, name);
2115 return JB_ERR_CGI_PARAMS;
2118 /* Copy and trim whitespace */
2119 param = strdup(orig_param);
2122 return JB_ERR_MEMORY;
2126 /* Must be non-empty, and can't allow 1st character to be '{' */
2127 if (param[0] == '\0' || param[0] == '{')
2130 return JB_ERR_CGI_PARAMS;
2133 /* Check for embedded newlines */
2134 for (s = param; *s != '\0'; s++)
2136 if ((*s == '\r') || (*s == '\n'))
2139 return JB_ERR_CGI_PARAMS;
2143 /* Check that regex is valid */
2148 return JB_ERR_MEMORY;
2150 err = create_url_spec(compiled, s);
2155 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2157 free_url_spec(compiled);
2159 if (param[strlen(param) - 1] == '\\')
2162 * Must protect trailing '\\' from becoming line continuation character.
2163 * Two methods: 1) If it's a domain only, add a trailing '/'.
2164 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2166 if (strchr(param, '/') == NULL)
2168 err = string_append(¶m, "/");
2172 err = string_append(¶m, "(?:)");
2179 /* Check that the modified regex is valid */
2184 return JB_ERR_MEMORY;
2186 err = create_url_spec(compiled, s);
2191 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2193 free_url_spec(compiled);
2200 /*********************************************************************
2202 * Function : map_radio
2204 * Description : Map a set of radio button values. E.g. if you have
2205 * 3 radio buttons, declare them as:
2206 * <option type="radio" name="xyz" @xyz-a@>
2207 * <option type="radio" name="xyz" @xyz-b@>
2208 * <option type="radio" name="xyz" @xyz-c@>
2209 * Then map one of the @xyz-?@ variables to "checked"
2210 * and all the others to empty by calling:
2211 * map_radio(exports, "xyz", "abc", sel)
2212 * Where 'sel' is 'a', 'b', or 'c'.
2215 * 1 : exports = Exports map to modify.
2216 * 2 : optionname = name for map
2217 * 3 : values = null-terminated list of values;
2218 * 4 : value = Selected value.
2220 * CGI Parameters : None
2222 * Returns : JB_ERR_OK on success
2223 * JB_ERR_MEMORY on out-of-memory
2225 *********************************************************************/
2226 static jb_err map_radio(struct map * exports,
2227 const char * optionname,
2228 const char * values,
2240 len = strlen(optionname);
2241 buf = malloc(len + 3);
2244 return JB_ERR_MEMORY;
2247 strcpy(buf, optionname);
2252 while ((c = *values++) != '\0')
2257 if (map(exports, buf, 1, "", 1))
2259 return JB_ERR_MEMORY;
2265 return map(exports, buf, 0, "checked", 1);
2269 /*********************************************************************
2271 * Function : cgi_error_modified
2273 * Description : CGI function that is called when a file is modified
2274 * outside the CGI editor.
2277 * 1 : csp = Current client state (buffers, headers, etc...)
2278 * 2 : rsp = http_response data structure for output
2279 * 3 : filename = The file that was modified.
2281 * CGI Parameters : none
2283 * Returns : JB_ERR_OK on success
2284 * JB_ERR_MEMORY on out-of-memory error.
2286 *********************************************************************/
2287 jb_err cgi_error_modified(struct client_state *csp,
2288 struct http_response *rsp,
2289 const char *filename)
2291 struct map *exports;
2298 if (NULL == (exports = default_exports(csp, NULL)))
2300 return JB_ERR_MEMORY;
2303 err = map(exports, "f", 1, html_encode(filename), 0);
2310 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2314 /*********************************************************************
2316 * Function : cgi_error_parse
2318 * Description : CGI function that is called when a file cannot
2319 * be parsed by the CGI editor.
2322 * 1 : csp = Current client state (buffers, headers, etc...)
2323 * 2 : rsp = http_response data structure for output
2324 * 3 : file = The file that was modified.
2326 * CGI Parameters : none
2328 * Returns : JB_ERR_OK on success
2329 * JB_ERR_MEMORY on out-of-memory error.
2331 *********************************************************************/
2332 jb_err cgi_error_parse(struct client_state *csp,
2333 struct http_response *rsp,
2334 struct editable_file *file)
2336 struct map *exports;
2338 struct file_line *cur_line;
2344 if (NULL == (exports = default_exports(csp, NULL)))
2346 return JB_ERR_MEMORY;
2349 err = map(exports, "f", 1, file->identifier, 1);
2350 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2352 cur_line = file->parse_error;
2355 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2356 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2364 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2368 /*********************************************************************
2370 * Function : cgi_error_file
2372 * Description : CGI function that is called when a file cannot be
2373 * opened by the CGI editor.
2376 * 1 : csp = Current client state (buffers, headers, etc...)
2377 * 2 : rsp = http_response data structure for output
2378 * 3 : filename = The file that was modified.
2380 * CGI Parameters : none
2382 * Returns : JB_ERR_OK on success
2383 * JB_ERR_MEMORY on out-of-memory error.
2385 *********************************************************************/
2386 jb_err cgi_error_file(struct client_state *csp,
2387 struct http_response *rsp,
2388 const char *filename)
2390 struct map *exports;
2397 if (NULL == (exports = default_exports(csp, NULL)))
2399 return JB_ERR_MEMORY;
2402 err = map(exports, "f", 1, html_encode(filename), 0);
2409 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2413 /*********************************************************************
2415 * Function : cgi_error_disabled
2417 * Description : CGI function that is called if the actions editor
2418 * is called although it's disabled in config
2421 * 1 : csp = Current client state (buffers, headers, etc...)
2422 * 2 : rsp = http_response data structure for output
2424 * CGI Parameters : none
2426 * Returns : JB_ERR_OK on success
2427 * JB_ERR_MEMORY on out-of-memory error.
2429 *********************************************************************/
2430 jb_err cgi_error_disabled(struct client_state *csp,
2431 struct http_response *rsp)
2433 struct map *exports;
2438 if (NULL == (exports = default_exports(csp, NULL)))
2440 return JB_ERR_MEMORY;
2443 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2447 /*********************************************************************
2449 * Function : cgi_edit_actions
2451 * Description : CGI function that allows the user to choose which
2452 * actions file to edit.
2455 * 1 : csp = Current client state (buffers, headers, etc...)
2456 * 2 : rsp = http_response data structure for output
2457 * 3 : parameters = map of cgi parameters
2459 * CGI Parameters : None
2461 * Returns : JB_ERR_OK on success
2462 * JB_ERR_MEMORY on out-of-memory error
2464 *********************************************************************/
2465 jb_err cgi_edit_actions(struct client_state *csp,
2466 struct http_response *rsp,
2467 const struct map *parameters)
2470 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2472 return cgi_error_disabled(csp, rsp);
2475 /* FIXME: Incomplete */
2476 rsp->status = strdup("302 Local Redirect from Privoxy");
2477 if (rsp->status == NULL)
2479 return JB_ERR_MEMORY;
2481 if (enlist_unique_header(rsp->headers, "Location",
2482 CGI_PREFIX "edit-actions-list?f=default"))
2486 return JB_ERR_MEMORY;
2493 /*********************************************************************
2495 * Function : cgi_edit_actions_list
2497 * Description : CGI function that edits the actions list.
2498 * FIXME: This function shouldn't FATAL ever.
2499 * FIXME: This function doesn't check the retval of map()
2501 * 1 : csp = Current client state (buffers, headers, etc...)
2502 * 2 : rsp = http_response data structure for output
2503 * 3 : parameters = map of cgi parameters
2505 * CGI Parameters : filename
2507 * Returns : JB_ERR_OK on success
2508 * JB_ERR_MEMORY on out-of-memory
2509 * JB_ERR_FILE if the file cannot be opened or
2511 * JB_ERR_CGI_PARAMS if "filename" was not specified
2514 *********************************************************************/
2515 jb_err cgi_edit_actions_list(struct client_state *csp,
2516 struct http_response *rsp,
2517 const struct map *parameters)
2519 char * section_template;
2520 char * url_template;
2525 struct map * exports;
2526 struct map * section_exports;
2527 struct map * url_exports;
2528 struct editable_file * file;
2529 struct file_line * cur_line;
2530 unsigned line_number = 0;
2531 unsigned prev_section_line_number = ((unsigned) (-1));
2535 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2537 return cgi_error_disabled(csp, rsp);
2540 /* Load actions file */
2541 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2544 /* No filename specified, can't read file, or out of memory. */
2545 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2548 /* Set up global exports */
2550 if (NULL == (exports = default_exports(csp, NULL)))
2552 edit_free_file(file);
2553 return JB_ERR_MEMORY;
2556 err = map(exports, "f", 1, file->identifier, 1);
2557 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2561 edit_free_file(file);
2566 /* Find start of actions in file */
2567 cur_line = file->lines;
2569 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2571 cur_line = cur_line->next;
2576 * Conventional actions files should have a match all block
2578 * cur_line = {...global actions...}
2579 * cur_line->next = /
2580 * cur_line->next->next = {...actions...} or EOF
2582 if ( (cur_line != NULL)
2583 && (cur_line->type == FILE_LINE_ACTION)
2584 && (cur_line->next != NULL)
2585 && (cur_line->next->type == FILE_LINE_URL)
2586 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2587 && ( (cur_line->next->next == NULL)
2588 || (cur_line->next->next->type != FILE_LINE_URL)
2592 * Conventional actions file, supply extra editing help.
2593 * (e.g. don't allow them to make it an unconventional one).
2595 err = map_conditional(exports, "all-urls-present", 1);
2597 snprintf(buf, 50, "%d", line_number);
2598 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2599 snprintf(buf, 50, "%d", line_number + 2);
2600 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2601 if (!err) err = map(exports, "all-urls-actions", 1,
2602 actions_to_html(cur_line->data.action), 0);
2604 /* Skip the 2 lines */
2605 cur_line = cur_line->next->next;
2609 * Note that prev_section_line_number is NOT set here.
2610 * This is deliberate and not a bug. It stops a "Move up"
2611 * option appearing on the next section. Clicking "Move
2612 * up" would make the actions file unconventional, which
2613 * we don't want, so we hide this option.
2619 * Non-standard actions file - does not begin with
2620 * the "All URLs" section.
2622 err = map_conditional(exports, "all-urls-present", 0);
2627 edit_free_file(file);
2632 /* Should do all global exports above this point */
2634 /* Load templates */
2636 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2639 edit_free_file(file);
2641 if (err == JB_ERR_FILE)
2643 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2648 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2651 free(section_template);
2652 edit_free_file(file);
2654 if (err == JB_ERR_FILE)
2656 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2661 err = template_fill(§ion_template, exports);
2665 edit_free_file(file);
2671 err = template_fill(&url_template, exports);
2674 free(section_template);
2675 edit_free_file(file);
2680 if (NULL == (sections = strdup("")))
2682 free(section_template);
2684 edit_free_file(file);
2686 return JB_ERR_MEMORY;
2689 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2691 if (NULL == (section_exports = new_map()))
2694 free(section_template);
2696 edit_free_file(file);
2698 return JB_ERR_MEMORY;
2701 snprintf(buf, 50, "%d", line_number);
2702 err = map(section_exports, "s", 1, buf, 1);
2703 if (!err) err = map(section_exports, "actions", 1,
2704 actions_to_html(cur_line->data.action), 0);
2707 && (cur_line->next != NULL)
2708 && (cur_line->next->type == FILE_LINE_URL))
2710 /* This section contains at least one URL, don't allow delete */
2711 err = map_block_killer(section_exports, "empty-section");
2715 if (!err) err = map_block_keep(section_exports, "empty-section");
2718 if (prev_section_line_number != ((unsigned)(-1)))
2720 /* Not last section */
2721 snprintf(buf, 50, "%d", prev_section_line_number);
2722 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2723 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2728 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2730 prev_section_line_number = line_number;
2735 free(section_template);
2737 edit_free_file(file);
2739 free_map(section_exports);
2743 /* Should do all section-specific exports above this point */
2745 if (NULL == (urls = strdup("")))
2748 free(section_template);
2750 edit_free_file(file);
2752 free_map(section_exports);
2753 return JB_ERR_MEMORY;
2758 cur_line = cur_line->next;
2761 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2763 if (NULL == (url_exports = new_map()))
2767 free(section_template);
2769 edit_free_file(file);
2771 free_map(section_exports);
2772 return JB_ERR_MEMORY;
2775 snprintf(buf, 50, "%d", line_number);
2776 err = map(url_exports, "p", 1, buf, 1);
2778 snprintf(buf, 50, "%d", url_1_2);
2779 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2781 if (!err) err = map(url_exports, "url-html", 1,
2782 html_encode(cur_line->unprocessed), 0);
2783 if (!err) err = map(url_exports, "url", 1,
2784 url_encode(cur_line->unprocessed), 0);
2790 free(section_template);
2792 edit_free_file(file);
2794 free_map(section_exports);
2795 free_map(url_exports);
2799 if (NULL == (s = strdup(url_template)))
2803 free(section_template);
2805 edit_free_file(file);
2807 free_map(section_exports);
2808 free_map(url_exports);
2809 return JB_ERR_MEMORY;
2812 err = template_fill(&s, section_exports);
2813 if (!err) err = template_fill(&s, url_exports);
2814 if (!err) err = string_append(&urls, s);
2816 free_map(url_exports);
2823 free(section_template);
2825 edit_free_file(file);
2827 free_map(section_exports);
2831 url_1_2 = 3 - url_1_2;
2833 cur_line = cur_line->next;
2837 err = map(section_exports, "urls", 1, urls, 0);
2839 /* Could also do section-specific exports here, but it wouldn't be as fast */
2841 if ( (cur_line != NULL)
2842 && (cur_line->type == FILE_LINE_ACTION))
2844 /* Not last section */
2845 snprintf(buf, 50, "%d", line_number);
2846 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2847 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2852 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2858 free(section_template);
2860 edit_free_file(file);
2862 free_map(section_exports);
2866 if (NULL == (s = strdup(section_template)))
2869 free(section_template);
2871 edit_free_file(file);
2873 free_map(section_exports);
2874 return JB_ERR_MEMORY;
2877 err = template_fill(&s, section_exports);
2878 if (!err) err = string_append(§ions, s);
2881 free_map(section_exports);
2886 free(section_template);
2888 edit_free_file(file);
2894 edit_free_file(file);
2895 free(section_template);
2898 err = map(exports, "sections", 1, sections, 0);
2905 /* Could also do global exports here, but it wouldn't be as fast */
2907 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2911 /*********************************************************************
2913 * Function : cgi_edit_actions
2915 * Description : CGI function that edits the Actions list.
2918 * 1 : csp = Current client state (buffers, headers, etc...)
2919 * 2 : rsp = http_response data structure for output
2920 * 3 : parameters = map of cgi parameters
2922 * CGI Parameters : None
2924 * Returns : JB_ERR_OK on success
2925 * JB_ERR_MEMORY on out-of-memory
2926 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2927 * specified or not valid.
2929 *********************************************************************/
2930 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2931 struct http_response *rsp,
2932 const struct map *parameters)
2934 struct map * exports;
2936 struct editable_file * file;
2937 struct file_line * cur_line;
2938 unsigned line_number;
2940 struct file_list *filter_file;
2941 struct re_filterfile_spec *filter_group;
2943 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2945 return cgi_error_disabled(csp, rsp);
2948 err = get_number_param(csp, parameters, "s", §ionid);
2954 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2957 /* No filename specified, can't read file, modified, or out of memory. */
2958 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2961 cur_line = file->lines;
2963 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2965 cur_line = cur_line->next;
2968 if ( (cur_line == NULL)
2969 || (line_number != sectionid)
2971 || (cur_line->type != FILE_LINE_ACTION))
2973 /* Invalid "sectionid" parameter */
2974 edit_free_file(file);
2975 return JB_ERR_CGI_PARAMS;
2978 if (NULL == (exports = default_exports(csp, NULL)))
2980 edit_free_file(file);
2981 return JB_ERR_MEMORY;
2984 err = map(exports, "f", 1, file->identifier, 1);
2985 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2986 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2988 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2990 filter_file = csp->rlist;
2991 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2993 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2997 edit_free_file(file);
3002 if (filter_group == NULL)
3004 err = map(exports, "filter-params", 1, "", 1);
3008 /* We have some entries in the filter list */
3011 char * filter_template;
3013 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3016 edit_free_file(file);
3018 if (err == JB_ERR_FILE)
3020 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3025 result = strdup("");
3027 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3029 char current_mode = 'x';
3030 struct list_entry *filter_name;
3032 struct map *line_exports;
3035 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
3036 while ((filter_name != NULL)
3037 && (0 != strcmp(filter_group->name, filter_name->str)))
3039 filter_name = filter_name->next;
3042 if (filter_name != NULL)
3048 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
3049 while ((filter_name != NULL)
3050 && (0 != strcmp(filter_group->name, filter_name->str)))
3052 filter_name = filter_name->next;
3054 if (filter_name != NULL)
3060 /* Generate a unique serial number */
3061 snprintf(number, sizeof(number), "%x", index++);
3062 number[sizeof(number) - 1] = '\0';
3064 line_exports = new_map();
3065 if (line_exports == NULL)
3067 err = JB_ERR_MEMORY;
3072 if (!err) err = map(line_exports, "index", 1, number, 1);
3073 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3074 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3075 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3080 this_line = strdup(filter_template);
3081 if (this_line == NULL) err = JB_ERR_MEMORY;
3083 if (!err) err = template_fill(&this_line, line_exports);
3084 string_join(&result, this_line);
3086 free_map(line_exports);
3091 err = map(exports, "filter-params", 1, result, 0);
3099 if (!err) err = map_radio(exports, "filter-all", "nx",
3100 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3102 edit_free_file(file);
3110 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3114 /*********************************************************************
3116 * Function : cgi_edit_actions_submit
3118 * Description : CGI function that actually edits the Actions list.
3121 * 1 : csp = Current client state (buffers, headers, etc...)
3122 * 2 : rsp = http_response data structure for output
3123 * 3 : parameters = map of cgi parameters
3125 * CGI Parameters : None
3127 * Returns : JB_ERR_OK on success
3128 * JB_ERR_MEMORY on out-of-memory
3129 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3130 * specified or not valid.
3132 *********************************************************************/
3133 jb_err cgi_edit_actions_submit(struct client_state *csp,
3134 struct http_response *rsp,
3135 const struct map *parameters)
3141 struct editable_file * file;
3142 struct file_line * cur_line;
3143 unsigned line_number;
3149 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3151 return cgi_error_disabled(csp, rsp);
3154 err = get_number_param(csp, parameters, "s", §ionid);
3160 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3163 /* No filename specified, can't read file, modified, or out of memory. */
3164 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3167 cur_line = file->lines;
3169 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3171 cur_line = cur_line->next;
3174 if ( (cur_line == NULL)
3175 || (line_number != sectionid)
3177 || (cur_line->type != FILE_LINE_ACTION))
3179 /* Invalid "sectionid" parameter */
3180 edit_free_file(file);
3181 return JB_ERR_CGI_PARAMS;
3184 err = actions_from_radio(parameters, cur_line->data.action);
3188 edit_free_file(file);
3192 ch = get_char_param(parameters, "filter_all");
3195 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3196 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3197 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3201 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3204 for (index = 0; !err; index++)
3211 /* Generate the keys */
3212 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3213 key_value[sizeof(key_value) - 1] = '\0';
3214 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3215 key_name[sizeof(key_name) - 1] = '\0';
3217 err = get_string_param(parameters, key_name, &name);
3227 value = get_char_param(parameters, key_value);
3230 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3231 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3232 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3234 else if (value == 'N')
3236 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3237 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3239 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3240 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3243 else if (value == 'X')
3245 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3246 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3253 edit_free_file(file);
3257 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3260 edit_free_file(file);
3261 return JB_ERR_MEMORY;
3264 len = strlen(actiontext);
3268 * Empty action - must special-case this.
3269 * Simply setting len to 1 is sufficient...
3274 if (NULL == (newtext = malloc(len + 2)))
3278 edit_free_file(file);
3279 return JB_ERR_MEMORY;
3281 strcpy(newtext, actiontext);
3285 newtext[len + 1] = '\0';
3287 freez(cur_line->raw);
3288 freez(cur_line->unprocessed);
3289 cur_line->unprocessed = newtext;
3291 err = edit_write_file(file);
3294 /* Error writing file */
3295 edit_free_file(file);
3299 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3300 string_append(&target, file->identifier);
3302 edit_free_file(file);
3307 return JB_ERR_MEMORY;
3310 rsp->status = strdup("302 Local Redirect from Privoxy");
3311 if (rsp->status == NULL)
3314 return JB_ERR_MEMORY;
3316 err = enlist_unique_header(rsp->headers, "Location", target);
3323 /*********************************************************************
3325 * Function : cgi_edit_actions_url
3327 * Description : CGI function that actually edits a URL pattern in
3331 * 1 : csp = Current client state (buffers, headers, etc...)
3332 * 2 : rsp = http_response data structure for output
3333 * 3 : parameters = map of cgi parameters
3336 * filename : Identifies the file to edit
3337 * ver : File's last-modified time
3338 * section : Line number of section to edit
3339 * pattern : Line number of pattern to edit
3340 * newval : New value for pattern
3342 * Returns : JB_ERR_OK on success
3343 * JB_ERR_MEMORY on out-of-memory
3344 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3345 * specified or not valid.
3347 *********************************************************************/
3348 jb_err cgi_edit_actions_url(struct client_state *csp,
3349 struct http_response *rsp,
3350 const struct map *parameters)
3354 struct editable_file * file;
3355 struct file_line * cur_line;
3356 unsigned line_number;
3360 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3362 return cgi_error_disabled(csp, rsp);
3365 err = get_number_param(csp, parameters, "p", &patternid);
3372 return JB_ERR_CGI_PARAMS;
3375 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3381 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3384 /* No filename specified, can't read file, modified, or out of memory. */
3386 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3390 cur_line = file->lines;
3392 while ((cur_line != NULL) && (line_number < patternid))
3394 cur_line = cur_line->next;
3398 if ( (cur_line == NULL)
3399 || (cur_line->type != FILE_LINE_URL))
3401 /* Invalid "patternid" parameter */
3403 edit_free_file(file);
3404 return JB_ERR_CGI_PARAMS;
3407 /* At this point, the line to edit is in cur_line */
3409 freez(cur_line->raw);
3410 freez(cur_line->unprocessed);
3411 cur_line->unprocessed = new_pattern;
3413 err = edit_write_file(file);
3416 /* Error writing file */
3417 edit_free_file(file);
3421 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3422 string_append(&target, file->identifier);
3424 edit_free_file(file);
3429 return JB_ERR_MEMORY;
3432 rsp->status = strdup("302 Local Redirect from Privoxy");
3433 if (rsp->status == NULL)
3436 return JB_ERR_MEMORY;
3438 err = enlist_unique_header(rsp->headers, "Location", target);
3445 /*********************************************************************
3447 * Function : cgi_edit_actions_add_url
3449 * Description : CGI function that actually adds a URL pattern to
3453 * 1 : csp = Current client state (buffers, headers, etc...)
3454 * 2 : rsp = http_response data structure for output
3455 * 3 : parameters = map of cgi parameters
3458 * filename : Identifies the file to edit
3459 * ver : File's last-modified time
3460 * section : Line number of section to edit
3461 * newval : New pattern
3463 * Returns : JB_ERR_OK on success
3464 * JB_ERR_MEMORY on out-of-memory
3465 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3466 * specified or not valid.
3468 *********************************************************************/
3469 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3470 struct http_response *rsp,
3471 const struct map *parameters)
3475 struct file_line * new_line;
3476 struct editable_file * file;
3477 struct file_line * cur_line;
3478 unsigned line_number;
3482 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3484 return cgi_error_disabled(csp, rsp);
3487 err = get_number_param(csp, parameters, "s", §ionid);
3494 return JB_ERR_CGI_PARAMS;
3497 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3503 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3506 /* No filename specified, can't read file, modified, or out of memory. */
3508 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3512 cur_line = file->lines;
3514 while ((cur_line != NULL) && (line_number < sectionid))
3516 cur_line = cur_line->next;
3520 if ( (cur_line == NULL)
3521 || (cur_line->type != FILE_LINE_ACTION))
3523 /* Invalid "sectionid" parameter */
3525 edit_free_file(file);
3526 return JB_ERR_CGI_PARAMS;
3529 /* At this point, the section header is in cur_line - add after this. */
3531 /* Allocate the new line */
3532 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3533 if (new_line == NULL)
3536 edit_free_file(file);
3537 return JB_ERR_MEMORY;
3540 /* Fill in the data members of the new line */
3541 new_line->raw = NULL;
3542 new_line->prefix = NULL;
3543 new_line->unprocessed = new_pattern;
3544 new_line->type = FILE_LINE_URL;
3546 /* Link new_line into the list, after cur_line */
3547 new_line->next = cur_line->next;
3548 cur_line->next = new_line;
3550 /* Done making changes, now commit */
3552 err = edit_write_file(file);
3555 /* Error writing file */
3556 edit_free_file(file);
3560 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3561 string_append(&target, file->identifier);
3563 edit_free_file(file);
3568 return JB_ERR_MEMORY;
3571 rsp->status = strdup("302 Local Redirect from Privoxy");
3572 if (rsp->status == NULL)
3575 return JB_ERR_MEMORY;
3577 err = enlist_unique_header(rsp->headers, "Location", target);
3584 /*********************************************************************
3586 * Function : cgi_edit_actions_remove_url
3588 * Description : CGI function that actually removes a URL pattern from
3592 * 1 : csp = Current client state (buffers, headers, etc...)
3593 * 2 : rsp = http_response data structure for output
3594 * 3 : parameters = map of cgi parameters
3597 * f : (filename) Identifies the file to edit
3598 * v : (version) File's last-modified time
3599 * p : (pattern) Line number of pattern to remove
3601 * Returns : JB_ERR_OK on success
3602 * JB_ERR_MEMORY on out-of-memory
3603 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3604 * specified or not valid.
3606 *********************************************************************/
3607 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3608 struct http_response *rsp,
3609 const struct map *parameters)
3612 struct editable_file * file;
3613 struct file_line * cur_line;
3614 struct file_line * prev_line;
3615 unsigned line_number;
3619 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3621 return cgi_error_disabled(csp, rsp);
3624 err = get_number_param(csp, parameters, "p", &patternid);
3630 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3633 /* No filename specified, can't read file, modified, or out of memory. */
3634 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3639 cur_line = file->lines;
3641 while ((cur_line != NULL) && (line_number < patternid))
3643 prev_line = cur_line;
3644 cur_line = cur_line->next;
3648 if ( (cur_line == NULL)
3649 || (prev_line == NULL)
3650 || (cur_line->type != FILE_LINE_URL))
3652 /* Invalid "patternid" parameter */
3653 edit_free_file(file);
3654 return JB_ERR_CGI_PARAMS;
3657 /* At this point, the line to remove is in cur_line, and the previous
3658 * one is in prev_line
3661 /* Unlink cur_line */
3662 prev_line->next = cur_line->next;
3663 cur_line->next = NULL;
3666 edit_free_file_lines(cur_line);
3668 err = edit_write_file(file);
3671 /* Error writing file */
3672 edit_free_file(file);
3676 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3677 string_append(&target, file->identifier);
3679 edit_free_file(file);
3684 return JB_ERR_MEMORY;
3687 rsp->status = strdup("302 Local Redirect from Privoxy");
3688 if (rsp->status == NULL)
3691 return JB_ERR_MEMORY;
3693 err = enlist_unique_header(rsp->headers, "Location", target);
3700 /*********************************************************************
3702 * Function : cgi_edit_actions_section_remove
3704 * Description : CGI function that actually removes a whole section from
3705 * the actions file. The section must be empty first
3706 * (else JB_ERR_CGI_PARAMS).
3709 * 1 : csp = Current client state (buffers, headers, etc...)
3710 * 2 : rsp = http_response data structure for output
3711 * 3 : parameters = map of cgi parameters
3714 * f : (filename) Identifies the file to edit
3715 * v : (version) File's last-modified time
3716 * s : (section) Line number of section to edit
3718 * Returns : JB_ERR_OK on success
3719 * JB_ERR_MEMORY on out-of-memory
3720 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3721 * specified or not valid.
3723 *********************************************************************/
3724 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3725 struct http_response *rsp,
3726 const struct map *parameters)
3729 struct editable_file * file;
3730 struct file_line * cur_line;
3731 struct file_line * prev_line;
3732 unsigned line_number;
3736 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3738 return cgi_error_disabled(csp, rsp);
3741 err = get_number_param(csp, parameters, "s", §ionid);
3747 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3750 /* No filename specified, can't read file, modified, or out of memory. */
3751 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3755 cur_line = file->lines;
3758 while ((cur_line != NULL) && (line_number < sectionid))
3760 prev_line = cur_line;
3761 cur_line = cur_line->next;
3765 if ( (cur_line == NULL)
3766 || (cur_line->type != FILE_LINE_ACTION) )
3768 /* Invalid "sectionid" parameter */
3769 edit_free_file(file);
3770 return JB_ERR_CGI_PARAMS;
3773 if ( (cur_line->next != NULL)
3774 && (cur_line->next->type == FILE_LINE_URL) )
3776 /* Section not empty. */
3777 edit_free_file(file);
3778 return JB_ERR_CGI_PARAMS;
3781 /* At this point, the line to remove is in cur_line, and the previous
3782 * one is in prev_line
3785 /* Unlink cur_line */
3786 if (prev_line == NULL)
3788 /* Removing the first line from the file */
3789 file->lines = cur_line->next;
3793 prev_line->next = cur_line->next;
3795 cur_line->next = NULL;
3798 edit_free_file_lines(cur_line);
3800 err = edit_write_file(file);
3803 /* Error writing file */
3804 edit_free_file(file);
3808 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3809 string_append(&target, file->identifier);
3811 edit_free_file(file);
3816 return JB_ERR_MEMORY;
3819 rsp->status = strdup("302 Local Redirect from Privoxy");
3820 if (rsp->status == NULL)
3823 return JB_ERR_MEMORY;
3825 err = enlist_unique_header(rsp->headers, "Location", target);
3832 /*********************************************************************
3834 * Function : cgi_edit_actions_section_add
3836 * Description : CGI function that adds a new empty section to
3840 * 1 : csp = Current client state (buffers, headers, etc...)
3841 * 2 : rsp = http_response data structure for output
3842 * 3 : parameters = map of cgi parameters
3845 * f : (filename) Identifies the file to edit
3846 * v : (version) File's last-modified time
3847 * s : (section) Line number of section to add after, 0 for
3850 * Returns : JB_ERR_OK on success
3851 * JB_ERR_MEMORY on out-of-memory
3852 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3853 * specified or not valid.
3855 *********************************************************************/
3856 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3857 struct http_response *rsp,
3858 const struct map *parameters)
3861 struct file_line * new_line;
3863 struct editable_file * file;
3864 struct file_line * cur_line;
3865 unsigned line_number;
3869 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3871 return cgi_error_disabled(csp, rsp);
3874 err = get_number_param(csp, parameters, "s", §ionid);
3880 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3883 /* No filename specified, can't read file, modified, or out of memory. */
3884 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3888 cur_line = file->lines;
3892 /* Add to start of file */
3893 if (cur_line != NULL)
3895 /* There's something in the file, find the line before the first
3898 while ( (cur_line->next != NULL)
3899 && (cur_line->next->type != FILE_LINE_ACTION) )
3901 cur_line = cur_line->next;
3908 /* Add after stated section. */
3909 while ((cur_line != NULL) && (line_number < sectionid))
3911 cur_line = cur_line->next;
3915 if ( (cur_line == NULL)
3916 || (cur_line->type != FILE_LINE_ACTION))
3918 /* Invalid "sectionid" parameter */
3919 edit_free_file(file);
3920 return JB_ERR_CGI_PARAMS;
3923 /* Skip through the section to find the last line in it. */
3924 while ( (cur_line->next != NULL)
3925 && (cur_line->next->type != FILE_LINE_ACTION) )
3927 cur_line = cur_line->next;
3932 /* At this point, the last line in the previous section is in cur_line
3933 * - add after this. (Or if we need to add as the first line, cur_line
3937 new_text = strdup("{}");
3938 if (NULL == new_text)
3940 edit_free_file(file);
3941 return JB_ERR_MEMORY;
3944 /* Allocate the new line */
3945 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3946 if (new_line == NULL)
3949 edit_free_file(file);
3950 return JB_ERR_MEMORY;
3953 /* Fill in the data members of the new line */
3954 new_line->raw = NULL;
3955 new_line->prefix = NULL;
3956 new_line->unprocessed = new_text;
3957 new_line->type = FILE_LINE_ACTION;
3959 if (cur_line != NULL)
3961 /* Link new_line into the list, after cur_line */
3962 new_line->next = cur_line->next;
3963 cur_line->next = new_line;
3967 /* Link new_line into the list, as first line */
3968 new_line->next = file->lines;
3969 file->lines = new_line;
3972 /* Done making changes, now commit */
3974 err = edit_write_file(file);
3977 /* Error writing file */
3978 edit_free_file(file);
3982 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3983 string_append(&target, file->identifier);
3985 edit_free_file(file);
3990 return JB_ERR_MEMORY;
3993 rsp->status = strdup("302 Local Redirect from Privoxy");
3994 if (rsp->status == NULL)
3997 return JB_ERR_MEMORY;
3999 err = enlist_unique_header(rsp->headers, "Location", target);
4006 /*********************************************************************
4008 * Function : cgi_edit_actions_section_swap
4010 * Description : CGI function that swaps the order of two sections
4011 * in the actions file. Note that this CGI can actually
4012 * swap any two arbitrary sections, but the GUI interface
4013 * currently only allows consecutive sections to be
4017 * 1 : csp = Current client state (buffers, headers, etc...)
4018 * 2 : rsp = http_response data structure for output
4019 * 3 : parameters = map of cgi parameters
4022 * f : (filename) Identifies the file to edit
4023 * v : (version) File's last-modified time
4024 * s1 : (section1) Line number of first section to swap
4025 * s2 : (section2) Line number of second section to swap
4027 * Returns : JB_ERR_OK on success
4028 * JB_ERR_MEMORY on out-of-memory
4029 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4030 * specified or not valid.
4032 *********************************************************************/
4033 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4034 struct http_response *rsp,
4035 const struct map *parameters)
4039 struct editable_file * file;
4040 struct file_line * cur_line;
4041 struct file_line * prev_line;
4042 struct file_line * line_before_section1;
4043 struct file_line * line_start_section1;
4044 struct file_line * line_end_section1;
4045 struct file_line * line_after_section1;
4046 struct file_line * line_before_section2;
4047 struct file_line * line_start_section2;
4048 struct file_line * line_end_section2;
4049 struct file_line * line_after_section2;
4050 unsigned line_number;
4054 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4056 return cgi_error_disabled(csp, rsp);
4059 err = get_number_param(csp, parameters, "s1", §ion1);
4060 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4066 if (section1 > section2)
4068 unsigned temp = section2;
4069 section2 = section1;
4073 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4076 /* No filename specified, can't read file, modified, or out of memory. */
4077 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4080 /* Start at the beginning... */
4082 cur_line = file->lines;
4085 /* ... find section1 ... */
4086 while ((cur_line != NULL) && (line_number < section1))
4088 prev_line = cur_line;
4089 cur_line = cur_line->next;
4093 if ( (cur_line == NULL)
4094 || (cur_line->type != FILE_LINE_ACTION) )
4096 /* Invalid "section1" parameter */
4097 edit_free_file(file);
4098 return JB_ERR_CGI_PARAMS;
4101 /* If no-op, we've validated params and can skip the rest. */
4102 if (section1 != section2)
4104 /* ... find the end of section1 ... */
4105 line_before_section1 = prev_line;
4106 line_start_section1 = cur_line;
4109 prev_line = cur_line;
4110 cur_line = cur_line->next;
4113 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4114 line_end_section1 = prev_line;
4115 line_after_section1 = cur_line;
4117 /* ... find section2 ... */
4118 while ((cur_line != NULL) && (line_number < section2))
4120 prev_line = cur_line;
4121 cur_line = cur_line->next;
4125 if ( (cur_line == NULL)
4126 || (cur_line->type != FILE_LINE_ACTION) )
4128 /* Invalid "section2" parameter */
4129 edit_free_file(file);
4130 return JB_ERR_CGI_PARAMS;
4133 /* ... find the end of section2 ... */
4134 line_before_section2 = prev_line;
4135 line_start_section2 = cur_line;
4138 prev_line = cur_line;
4139 cur_line = cur_line->next;
4142 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4143 line_end_section2 = prev_line;
4144 line_after_section2 = cur_line;
4146 /* Now have all the pointers we need. Do the swap. */
4148 /* Change the pointer to section1 to point to section2 instead */
4149 if (line_before_section1 == NULL)
4151 file->lines = line_start_section2;
4155 line_before_section1->next = line_start_section2;
4158 if (line_before_section2 == line_end_section1)
4160 /* Consecutive sections */
4161 line_end_section2->next = line_start_section1;
4165 line_end_section2->next = line_after_section1;
4166 line_before_section2->next = line_start_section1;
4169 /* Set the pointer from the end of section1 to the rest of the file */
4170 line_end_section1->next = line_after_section2;
4172 err = edit_write_file(file);
4175 /* Error writing file */
4176 edit_free_file(file);
4179 } /* END if (section1 != section2) */
4181 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4182 string_append(&target, file->identifier);
4184 edit_free_file(file);
4189 return JB_ERR_MEMORY;
4192 rsp->status = strdup("302 Local Redirect from Privoxy");
4193 if (rsp->status == NULL)
4196 return JB_ERR_MEMORY;
4198 err = enlist_unique_header(rsp->headers, "Location", target);
4205 /*********************************************************************
4207 * Function : cgi_toggle
4209 * Description : CGI function that adds a new empty section to
4213 * 1 : csp = Current client state (buffers, headers, etc...)
4214 * 2 : rsp = http_response data structure for output
4215 * 3 : parameters = map of cgi parameters
4218 * set : If present, how to change toggle setting:
4219 * "enable", "disable", "toggle", or none (default).
4220 * mini : If present, use mini reply template.
4222 * Returns : JB_ERR_OK on success
4223 * JB_ERR_MEMORY on out-of-memory
4225 *********************************************************************/
4226 jb_err cgi_toggle(struct client_state *csp,
4227 struct http_response *rsp,
4228 const struct map *parameters)
4230 struct map *exports;
4232 const char *template_name;
4238 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4240 return cgi_error_disabled(csp, rsp);
4243 mode = get_char_param(parameters, "set");
4250 else if (mode == 'D')
4255 else if (mode == 'T')
4258 g_bToggleIJB = !g_bToggleIJB;
4261 if (NULL == (exports = default_exports(csp, "toggle")))
4263 return JB_ERR_MEMORY;
4266 template_name = (get_char_param(parameters, "mini")
4270 return template_fill_for_cgi(csp, template_name, exports, rsp);
4274 /*********************************************************************
4276 * Function : javascriptify
4278 * Description : Converts a string into a form JavaScript will like.
4280 * Netscape 4's JavaScript sucks - it doesn't use
4281 * "id" parameters, so you have to set the "name"
4282 * used to submit a form element to something JavaScript
4283 * will like. (Or access the elements by index in an
4284 * array. That array contains >60 elements and will
4285 * be changed whenever we add a new action to the
4286 * editor, so I'm NOT going to use indexes that have
4287 * to be figured out by hand.)
4289 * Currently the only thing we have to worry about
4290 * is "-" ==> "_" conversion.
4292 * This is a length-preserving operation so it is
4293 * carried out in-place, no memory is allocated
4297 * 1 : identifier = String to make JavaScript-friendly.
4301 *********************************************************************/
4302 static void javascriptify(char * identifier)
4304 char * p = identifier;
4305 while (NULL != (p = strchr(p, '-')))
4312 /*********************************************************************
4314 * Function : actions_to_radio
4316 * Description : Converts a actionsfile entry into settings for
4317 * radio buttons and edit boxes on a HTML form.
4320 * 1 : exports = List of substitutions to add to.
4321 * 2 : action = Action to read
4323 * Returns : JB_ERR_OK on success
4324 * JB_ERR_MEMORY on out-of-memory
4326 *********************************************************************/
4327 static jb_err actions_to_radio(struct map * exports,
4328 const struct action_spec *action)
4330 unsigned mask = action->mask;
4331 unsigned add = action->add;
4339 mask = action->mask;
4342 /* sanity - prevents "-feature +feature" */
4346 #define DEFINE_ACTION_BOOL(name, bit) \
4347 if (!(mask & bit)) \
4349 current_mode = 'n'; \
4351 else if (add & bit) \
4353 current_mode = 'y'; \
4357 current_mode = 'x'; \
4359 if (map_radio(exports, name, "ynx", current_mode)) \
4361 return JB_ERR_MEMORY; \
4364 #define DEFINE_ACTION_STRING(name, bit, index) \
4365 DEFINE_ACTION_BOOL(name, bit); \
4368 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4371 checked = !strcmp(action->string[index], value); \
4375 checked = is_default; \
4377 mapped_param |= checked; \
4378 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4380 return JB_ERR_MEMORY; \
4383 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4384 if (map(exports, name "-param-custom", 1, \
4385 ((!mapped_param) ? "checked" : ""), 1)) \
4387 return JB_ERR_MEMORY; \
4389 if (map(exports, name "-param", 1, \
4390 (((add & bit) && !mapped_param) ? \
4391 action->string[index] : default_val), 1)) \
4393 return JB_ERR_MEMORY; \
4396 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4397 if (map(exports, name "-param", 1, \
4398 ((add & bit) ? action->string[index] : default_val), 1)) \
4400 return JB_ERR_MEMORY; \
4403 #define DEFINE_ACTION_MULTI(name, index) \
4404 if (action->multi_add[index]->first) \
4406 current_mode = 'y'; \
4408 else if (action->multi_remove_all[index]) \
4410 current_mode = 'n'; \
4412 else if (action->multi_remove[index]->first) \
4414 current_mode = 'y'; \
4418 current_mode = 'x'; \
4420 if (map_radio(exports, name, "ynx", current_mode)) \
4422 return JB_ERR_MEMORY; \
4425 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4427 #include "actionlist.h"
4429 #undef DEFINE_ACTION_MULTI
4430 #undef DEFINE_ACTION_STRING
4431 #undef DEFINE_ACTION_BOOL
4432 #undef DEFINE_ACTION_ALIAS
4433 #undef DEFINE_CGI_PARAM_CUSTOM
4434 #undef DEFINE_CGI_PARAM_RADIO
4435 #undef DEFINE_CGI_PARAM_NO_RADIO
4441 /*********************************************************************
4443 * Function : actions_from_radio
4445 * Description : Converts a map of parameters passed to a CGI function
4446 * into an actionsfile entry.
4449 * 1 : parameters = parameters to the CGI call
4450 * 2 : action = Action to change. Must be valid before
4451 * the call, actions not specified will be
4454 * Returns : JB_ERR_OK on success
4455 * JB_ERR_MEMORY on out-of-memory
4457 *********************************************************************/
4458 static jb_err actions_from_radio(const struct map * parameters,
4459 struct action_spec *action)
4461 static int first_time = 1;
4465 const char * js_name;
4466 jb_err err = JB_ERR_OK;
4471 /* Statics are generally a potential race condition,
4472 * but in this case we're safe and don't need semaphores.
4473 * Be careful if you modify this function.
4477 #define JAVASCRIPTIFY(dest_var, string) \
4479 static char js_name_arr[] = string; \
4482 javascriptify(js_name_arr); \
4484 dest_var = js_name_arr; \
4487 #define DEFINE_ACTION_BOOL(name, bit) \
4488 JAVASCRIPTIFY(js_name, name); \
4489 ch = get_char_param(parameters, js_name); \
4492 action->add |= bit; \
4493 action->mask |= bit; \
4495 else if (ch == 'N') \
4497 action->add &= ~bit; \
4498 action->mask &= ~bit; \
4500 else if (ch == 'X') \
4502 action->add &= ~bit; \
4503 action->mask |= bit; \
4506 #define DEFINE_ACTION_STRING(name, bit, index) \
4507 JAVASCRIPTIFY(js_name, name); \
4508 ch = get_char_param(parameters, js_name); \
4512 JAVASCRIPTIFY(js_name, name "-mode"); \
4513 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4514 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4516 JAVASCRIPTIFY(js_name, name "-param"); \
4517 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4519 if (param != NULL) \
4521 if (NULL == (param_dup = strdup(param))) \
4523 return JB_ERR_MEMORY; \
4525 freez(action->string[index]); \
4526 action->add |= bit; \
4527 action->mask |= bit; \
4528 action->string[index] = param_dup; \
4531 else if (ch == 'N') \
4533 if (action->add & bit) \
4535 freez(action->string[index]); \
4537 action->add &= ~bit; \
4538 action->mask &= ~bit; \
4540 else if (ch == 'X') \
4542 if (action->add & bit) \
4544 freez(action->string[index]); \
4546 action->add &= ~bit; \
4547 action->mask |= bit; \
4550 #define DEFINE_ACTION_MULTI(name, index) \
4551 JAVASCRIPTIFY(js_name, name); \
4552 ch = get_char_param(parameters, js_name); \
4557 else if (ch == 'N') \
4559 list_remove_all(action->multi_add[index]); \
4560 list_remove_all(action->multi_remove[index]); \
4561 action->multi_remove_all[index] = 1; \
4563 else if (ch == 'X') \
4565 list_remove_all(action->multi_add[index]); \
4566 list_remove_all(action->multi_remove[index]); \
4567 action->multi_remove_all[index] = 0; \
4570 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4572 #include "actionlist.h"
4574 #undef DEFINE_ACTION_MULTI
4575 #undef DEFINE_ACTION_STRING
4576 #undef DEFINE_ACTION_BOOL
4577 #undef DEFINE_ACTION_ALIAS
4578 #undef JAVASCRIPTIFY
4586 #endif /* def FEATURE_CGI_EDIT_ACTIONS */