1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.25 2002/03/26 22:29:54 swa 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.25 2002/03/26 22:29:54 swa
46 * we have a new homepage!
48 * Revision 1.24 2002/03/24 15:23:33 jongfoster
51 * Revision 1.23 2002/03/24 13:32:41 swa
52 * name change related issues
54 * Revision 1.22 2002/03/24 13:25:43 swa
55 * name change related issues
57 * Revision 1.21 2002/03/22 18:02:48 jongfoster
58 * Fixing remote toggle
60 * Revision 1.20 2002/03/16 20:28:34 oes
61 * Added descriptions to the filters so users will know what they select in the cgi editor
63 * Revision 1.19 2002/03/16 18:38:14 jongfoster
64 * Stopping stupid or malicious users from breaking the actions
65 * file using the web-based editor.
67 * Revision 1.18 2002/03/16 14:57:44 jongfoster
68 * Full support for enabling/disabling modular filters.
70 * Revision 1.17 2002/03/16 14:26:42 jongfoster
71 * First version of modular filters support - READ ONLY!
72 * Fixing a double-free bug in the out-of-memory handling in map_radio().
74 * Revision 1.16 2002/03/07 03:46:17 oes
75 * Fixed compiler warnings
77 * Revision 1.15 2002/03/06 22:54:35 jongfoster
78 * Automated function-comment nitpicking.
80 * Revision 1.14 2002/03/05 00:24:51 jongfoster
81 * Patch to always edit the current actions file.
83 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
84 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
86 * Revision 1.12 2002/03/03 09:18:03 joergs
87 * Made jumbjuster work on AmigaOS again.
89 * Revision 1.11 2002/01/23 01:03:31 jongfoster
90 * Fixing gcc [CygWin] compiler warnings
92 * Revision 1.10 2002/01/23 00:22:59 jongfoster
93 * Adding new function cgi_edit_actions_section_swap(), to reorder
96 * Adding get_url_spec_param() to get a validated URL pattern.
98 * Moving edit_read_line() out of this file and into loaders.c.
100 * Adding missing html_encode() to many CGI functions.
102 * Moving the functions that #include actionlist.h to the end of the file,
103 * because the Visual C++ 97 debugger gets extremely confused if you try
104 * to debug any code that comes after them in the file.
106 * Major optimizations in cgi_edit_actions_list() to reduce the size of
107 * the generated HTML (down 40% from 550k to 304k), with major side-effects
108 * throughout the editor and templates. In particular, the length of the
109 * URLs throughout the editor has been drastically reduced, by cutting
110 * paramater names down to 1 character and CGI names down to 3-4
111 * characters, by removing all non-essential CGI paramaters even at the
112 * expense of having to re-read the actions file for the most trivial
113 * page, and by using relative rather than absolute URLs. This means
114 * that this (typical example):
116 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
117 * filename=ijb&ver=1011487572&section=12&pattern=13
118 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
122 * <a href="eau?f=ijb&v=1011487572&p=13">
124 * Revision 1.9 2002/01/17 20:56:22 jongfoster
125 * Replacing hard references to the URL of the config interface
126 * with #defines from project.h
128 * Revision 1.8 2001/11/30 23:35:51 jongfoster
129 * Renaming actionsfile to ijb.action
131 * Revision 1.7 2001/11/13 00:28:24 jongfoster
132 * - Renaming parameters from edit-actions-for-url so that they only
133 * contain legal JavaScript characters. If we wanted to write
134 * JavaScript that worked with Netscape 4, this is nessacery.
135 * (Note that at the moment the JavaScript doesn't actually work
136 * with Netscape 4, but now this is purely a template issue, not
137 * one affecting code).
138 * - Adding new CGIs for use by non-JavaScript browsers:
139 * edit-actions-url-form
140 * edit-actions-add-url-form
141 * edit-actions-remove-url-form
144 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
145 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
146 * by and __OS2__ ifdef.
148 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
149 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
150 * threads to call select() simultaneously. So, it's time to do a real, live,
151 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
152 * (native). Both versions will work, but using __OS2__ offers multi-threading.
154 * Revision 1.4 2001/10/23 21:48:19 jongfoster
155 * Cleaning up error handling in CGI functions - they now send back
156 * a HTML error page and should never cause a FATAL error. (Fixes one
157 * potential source of "denial of service" attacks).
159 * CGI actions file editor that works and is actually useful.
161 * Ability to toggle JunkBuster remotely using a CGI call.
163 * You can turn off both the above features in the main configuration
164 * file, e.g. if you are running a multi-user proxy.
166 * Revision 1.3 2001/10/14 22:12:49 jongfoster
167 * New version of CGI-based actionsfile editor.
168 * Major changes, including:
169 * - Completely new file parser and file output routines
170 * - edit-actions CGI renamed edit-actions-for-url
171 * - All CGIs now need a filename parameter, except for...
172 * - New CGI edit-actions which doesn't need a filename,
173 * to allow you to start the editor up.
174 * - edit-actions-submit now works, and now automatically
175 * redirects you back to the main edit-actions-list handler.
177 * Revision 1.2 2001/09/16 17:05:14 jongfoster
178 * Removing unused #include showarg.h
180 * Revision 1.1 2001/09/16 15:47:37 jongfoster
181 * First version of CGI-based edit interface. This is very much a
182 * work-in-progress, and you can't actually use it to edit anything
183 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
184 * to have any effect.
187 **********************************************************************/
193 * FIXME: Following includes copied from cgi.c - which are actually needed?
198 #include <sys/types.h>
203 #include <sys/stat.h>
206 #define snprintf _snprintf
207 #endif /* def _WIN32 */
212 #include "cgisimple.h"
216 #include "miscutil.h"
220 /* loadcfg.h is for g_bToggleIJB only */
221 #include "urlmatch.h"
223 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
226 #ifdef FEATURE_CGI_EDIT_ACTIONS
230 struct file_line * next;
238 struct action_spec action[1];
247 /* Add more data types here... e.g.
250 struct url_spec url[1];
254 struct action_spec action[1];
263 #define FILE_LINE_UNPROCESSED 1
264 #define FILE_LINE_BLANK 2
265 #define FILE_LINE_ALIAS_HEADER 3
266 #define FILE_LINE_ALIAS_ENTRY 4
267 #define FILE_LINE_ACTION 5
268 #define FILE_LINE_URL 6
269 #define FILE_LINE_SETTINGS_HEADER 7
270 #define FILE_LINE_SETTINGS_ENTRY 8
271 #define FILE_LINE_DESCRIPTION_HEADER 9
272 #define FILE_LINE_DESCRIPTION_ENTRY 10
277 struct file_line * lines;
278 const char * filename; /* Full pathname - e.g. "/etc/privoxy/wibble.action" */
279 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
280 /* Pre-encoded with url_encode() for ease of use. */
281 const char * version_str; /* Last modification time, as a string. For CGI param */
282 /* Can be used in URL without using url_param(). */
283 unsigned version; /* Last modification time - prevents chaos with
284 * the browser's "back" button. Note that this is a
285 * time_t cast to an unsigned. When comparing, always
286 * cast the time_t to an unsigned, and *NOT* vice-versa.
287 * This may lose the top few bits, but they're not
288 * significant anyway.
290 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
291 * Note that changing this after the file has been
292 * read in will cause a mess.
294 struct file_line * parse_error; /* On parse error, this is the offending line. */
295 const char * parse_error_text; /* On parse error, this is the problem.
296 * (Statically allocated) */
299 #define CGI_ACTION_PARAM_LEN_MAX 500
301 /* FIXME: Following non-static functions should be prototyped in .h or made static */
303 /* Functions to read and write arbitrary config files */
304 jb_err edit_read_file(struct client_state *csp,
305 const struct map *parameters,
308 struct editable_file **pfile);
309 jb_err edit_write_file(struct editable_file * file);
310 void edit_free_file(struct editable_file * file);
312 /* Functions to read and write actions files */
313 jb_err edit_parse_actions_file(struct editable_file * file);
314 jb_err edit_read_actions_file(struct client_state *csp,
315 struct http_response *rsp,
316 const struct map *parameters,
318 struct editable_file **pfile);
321 jb_err cgi_error_modified(struct client_state *csp,
322 struct http_response *rsp,
323 const char *filename);
324 jb_err cgi_error_parse(struct client_state *csp,
325 struct http_response *rsp,
326 struct editable_file *file);
327 jb_err cgi_error_file(struct client_state *csp,
328 struct http_response *rsp,
329 const char *filename);
330 jb_err cgi_error_disabled(struct client_state *csp,
331 struct http_response *rsp);
333 /* Internal arbitrary config file support functions */
334 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
335 static void edit_free_file_lines(struct file_line * first_line);
337 /* Internal actions file support functions */
338 static int match_actions_file_header_line(const char * line, const char * name);
339 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
341 /* Internal parameter parsing functions */
342 static jb_err get_file_name_param(struct client_state *csp,
343 const struct map *parameters,
344 const char *param_name,
347 const char **pparam);
348 static jb_err get_number_param(struct client_state *csp,
349 const struct map *parameters,
352 static jb_err get_url_spec_param(struct client_state *csp,
353 const struct map *parameters,
356 static jb_err get_string_param(const struct map *parameters,
357 const char *param_name,
358 const char **pparam);
360 /* Internal actionsfile <==> HTML conversion functions */
361 static jb_err map_radio(struct map * exports,
362 const char * optionname,
365 static jb_err actions_to_radio(struct map * exports,
366 const struct action_spec *action);
367 static jb_err actions_from_radio(const struct map * parameters,
368 struct action_spec *action);
371 static jb_err map_copy_parameter_html(struct map *out,
372 const struct map *in,
374 #if 0 /* unused function */
375 static jb_err map_copy_parameter_url(struct map *out,
376 const struct map *in,
378 #endif /* unused function */
380 /*********************************************************************
382 * Function : map_copy_parameter_html
384 * Description : Copy a CGI parameter from one map to another, HTML
388 * 1 : out = target map
389 * 2 : in = source map
390 * 3 : name = name of cgi parameter to copy
392 * Returns : JB_ERR_OK on success
393 * JB_ERR_MEMORY on out-of-memory
394 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
397 *********************************************************************/
398 static jb_err map_copy_parameter_html(struct map *out,
399 const struct map *in,
409 value = lookup(in, name);
410 err = map(out, name, 1, html_encode(value), 0);
417 else if (*value == '\0')
419 return JB_ERR_CGI_PARAMS;
428 #if 0 /* unused function */
429 /*********************************************************************
431 * Function : map_copy_parameter_html
433 * Description : Copy a CGI parameter from one map to another, URL
437 * 1 : out = target map
438 * 2 : in = source map
439 * 3 : name = name of cgi parameter to copy
441 * Returns : JB_ERR_OK on success
442 * JB_ERR_MEMORY on out-of-memory
443 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
446 *********************************************************************/
447 static jb_err map_copy_parameter_url(struct map *out,
448 const struct map *in,
458 value = lookup(in, name);
459 err = map(out, name, 1, url_encode(value), 0);
466 else if (*value == '\0')
468 return JB_ERR_CGI_PARAMS;
475 #endif /* 0 - unused function */
477 /*********************************************************************
479 * Function : cgi_edit_actions_url_form
481 * Description : CGI function that displays a form for
485 * 1 : csp = Current client state (buffers, headers, etc...)
486 * 2 : rsp = http_response data structure for output
487 * 3 : parameters = map of cgi parameters
490 * f : (filename) Identifies the file to edit
491 * v : (version) File's last-modified time
492 * p : (pattern) Line number of pattern to edit
494 * Returns : JB_ERR_OK on success
495 * JB_ERR_MEMORY on out-of-memory
496 * JB_ERR_CGI_PARAMS if the CGI parameters are not
497 * specified or not valid.
499 *********************************************************************/
500 jb_err cgi_edit_actions_url_form(struct client_state *csp,
501 struct http_response *rsp,
502 const struct map *parameters)
504 struct map * exports;
506 struct editable_file * file;
507 struct file_line * cur_line;
508 unsigned line_number;
515 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
517 return cgi_error_disabled(csp, rsp);
520 err = get_number_param(csp, parameters, "p", &patternid);
526 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
529 /* No filename specified, can't read file, modified, or out of memory. */
530 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
533 cur_line = file->lines;
535 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
537 cur_line = cur_line->next;
540 if ( (cur_line == NULL)
541 || (line_number != patternid)
543 || (cur_line->type != FILE_LINE_URL))
545 /* Invalid "patternid" parameter */
546 edit_free_file(file);
547 return JB_ERR_CGI_PARAMS;
550 if (NULL == (exports = default_exports(csp, NULL)))
552 edit_free_file(file);
553 return JB_ERR_MEMORY;
556 err = map(exports, "f", 1, file->identifier, 1);
557 if (!err) err = map(exports, "v", 1, file->version_str, 1);
558 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
559 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
561 edit_free_file(file);
569 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
573 /*********************************************************************
575 * Function : cgi_edit_actions_add_url_form
577 * Description : CGI function that displays a form for
581 * 1 : csp = Current client state (buffers, headers, etc...)
582 * 2 : rsp = http_response data structure for output
583 * 3 : parameters = map of cgi parameters
586 * f : (filename) Identifies the file to edit
587 * v : (version) File's last-modified time
588 * s : (section) Line number of section to edit
590 * Returns : JB_ERR_OK on success
591 * JB_ERR_MEMORY on out-of-memory
592 * JB_ERR_CGI_PARAMS if the CGI parameters are not
593 * specified or not valid.
595 *********************************************************************/
596 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
597 struct http_response *rsp,
598 const struct map *parameters)
607 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
609 return cgi_error_disabled(csp, rsp);
612 if (NULL == (exports = default_exports(csp, NULL)))
614 return JB_ERR_MEMORY;
617 err = map_copy_parameter_html(exports, parameters, "f");
618 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
619 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
627 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
631 /*********************************************************************
633 * Function : cgi_edit_actions_remove_url_form
635 * Description : CGI function that displays a form for
639 * 1 : csp = Current client state (buffers, headers, etc...)
640 * 2 : rsp = http_response data structure for output
641 * 3 : parameters = map of cgi parameters
644 * f : (filename) Identifies the file to edit
645 * v : (version) File's last-modified time
646 * p : (pattern) Line number of pattern to edit
648 * Returns : JB_ERR_OK on success
649 * JB_ERR_MEMORY on out-of-memory
650 * JB_ERR_CGI_PARAMS if the CGI parameters are not
651 * specified or not valid.
653 *********************************************************************/
654 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
655 struct http_response *rsp,
656 const struct map *parameters)
658 struct map * exports;
660 struct editable_file * file;
661 struct file_line * cur_line;
662 unsigned line_number;
669 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
671 return cgi_error_disabled(csp, rsp);
674 err = get_number_param(csp, parameters, "p", &patternid);
680 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
683 /* No filename specified, can't read file, modified, or out of memory. */
684 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
687 cur_line = file->lines;
689 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
691 cur_line = cur_line->next;
694 if ( (cur_line == NULL)
695 || (line_number != patternid)
697 || (cur_line->type != FILE_LINE_URL))
699 /* Invalid "patternid" parameter */
700 edit_free_file(file);
701 return JB_ERR_CGI_PARAMS;
704 if (NULL == (exports = default_exports(csp, NULL)))
706 edit_free_file(file);
707 return JB_ERR_MEMORY;
710 err = map(exports, "f", 1, file->identifier, 1);
711 if (!err) err = map(exports, "v", 1, file->version_str, 1);
712 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
713 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
715 edit_free_file(file);
723 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
727 /*********************************************************************
729 * Function : edit_write_file
731 * Description : Write a complete file to disk.
734 * 1 : filename = File to write to.
735 * 2 : file = Data structure to write.
737 * Returns : JB_ERR_OK on success
738 * JB_ERR_FILE on error writing to file.
739 * JB_ERR_MEMORY on out of memory
741 *********************************************************************/
742 jb_err edit_write_file(struct editable_file * file)
745 struct file_line * cur_line;
746 struct stat statbuf[1];
747 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
748 digits in time_t, assuming this is a 64-bit
749 machine, plus null terminator, plus one
753 assert(file->filename);
755 #if defined(AMIGA) || defined(__OS2__)
756 if (NULL == (fp = fopen(file->filename, "w")))
758 if (NULL == (fp = fopen(file->filename, "wt")))
759 #endif /* def AMIGA */
764 cur_line = file->lines;
765 while (cur_line != NULL)
769 if (fputs(cur_line->raw, fp) < 0)
777 if (cur_line->prefix)
779 if (fputs(cur_line->prefix, fp) < 0)
785 if (cur_line->unprocessed)
787 /* This should be a single line - sanity check. */
788 assert(NULL == strchr(cur_line->unprocessed, '\r'));
789 assert(NULL == strchr(cur_line->unprocessed, '\n'));
791 if (NULL != strchr(cur_line->unprocessed, '#'))
793 /* Must quote '#' characters */
800 /* Count number of # characters, so we know length of output string */
801 src = cur_line->unprocessed;
802 while (NULL != (src = strchr(src, '#')))
809 /* Allocate new memory for string */
810 len = strlen(cur_line->unprocessed);
811 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
813 /* Uh oh, just trashed file! */
815 return JB_ERR_MEMORY;
818 /* Loop through string from end */
819 src = cur_line->unprocessed + len;
820 dest = str + len + numhash;
821 for ( ; len >= 0; len--)
823 if ((*dest-- = *src--) == '#')
827 assert(numhash >= 0);
830 assert(numhash == 0);
831 assert(src + 1 == cur_line->unprocessed);
832 assert(dest + 1 == str);
834 if (fputs(str, fp) < 0)
845 /* Can write without quoting '#' characters. */
846 if (fputs(cur_line->unprocessed, fp) < 0)
852 if (fputs(NEWLINE(file->newline), fp) < 0)
860 /* FIXME: Write data from file->data->whatever */
864 cur_line = cur_line->next;
870 /* Update the version stamp in the file structure, since we just
871 * wrote to the file & changed it's date.
873 if (stat(file->filename, statbuf) < 0)
875 /* Error, probably file not found. */
878 file->version = (unsigned)statbuf->st_mtime;
880 /* Correct file->version_str */
881 freez(file->version_str);
882 snprintf(version_buf, 22, "%u", file->version);
883 version_buf[21] = '\0';
884 file->version_str = strdup(version_buf);
885 if (version_buf == NULL)
887 return JB_ERR_MEMORY;
894 /*********************************************************************
896 * Function : edit_free_file
898 * Description : Free a complete file in memory.
901 * 1 : file = Data structure to free.
905 *********************************************************************/
906 void edit_free_file(struct editable_file * file)
910 /* Silently ignore NULL pointer */
914 edit_free_file_lines(file->lines);
915 freez(file->filename);
916 freez(file->identifier);
917 freez(file->version_str);
919 file->parse_error_text = NULL; /* Statically allocated */
920 file->parse_error = NULL;
926 /*********************************************************************
928 * Function : edit_free_file
930 * Description : Free an entire linked list of file lines.
933 * 1 : first_line = Data structure to free.
937 *********************************************************************/
938 static void edit_free_file_lines(struct file_line * first_line)
940 struct file_line * next_line;
942 while (first_line != NULL)
944 next_line = first_line->next;
945 first_line->next = NULL;
946 freez(first_line->raw);
947 freez(first_line->prefix);
948 freez(first_line->unprocessed);
949 switch(first_line->type)
951 case 0: /* special case if memory zeroed */
952 case FILE_LINE_UNPROCESSED:
953 case FILE_LINE_BLANK:
954 case FILE_LINE_ALIAS_HEADER:
955 case FILE_LINE_SETTINGS_HEADER:
956 case FILE_LINE_DESCRIPTION_HEADER:
957 case FILE_LINE_DESCRIPTION_ENTRY:
958 case FILE_LINE_ALIAS_ENTRY:
960 /* No data is stored for these */
963 case FILE_LINE_ACTION:
964 free_action(first_line->data.action);
967 case FILE_LINE_SETTINGS_ENTRY:
968 freez(first_line->data.setting.name);
969 freez(first_line->data.setting.svalue);
972 /* Should never happen */
976 first_line->type = 0; /* paranoia */
978 first_line = next_line;
983 /*********************************************************************
985 * Function : match_actions_file_header_line
987 * Description : Match an actions file {{header}} line
990 * 1 : line = String from file
991 * 2 : name = Header to match against
993 * Returns : 0 iff they match.
995 *********************************************************************/
996 static int match_actions_file_header_line(const char * line, const char * name)
1004 if ((line[0] != '{') || (line[1] != '{'))
1010 /* Look for optional whitespace */
1011 while ( (*line == ' ') || (*line == '\t') )
1016 /* Look for the specified name (case-insensitive) */
1018 if (0 != strncmpic(line, name, len))
1024 /* Look for optional whitespace */
1025 while ( (*line == ' ') || (*line == '\t') )
1030 /* Look for "}}" and end of string*/
1031 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1041 /*********************************************************************
1043 * Function : match_actions_file_header_line
1045 * Description : Match an actions file {{header}} line
1048 * 1 : line = String from file. Must not start with
1049 * whitespace (else infinite loop!)
1050 * 2 : name = Destination for name
1051 * 2 : name = Destination for value
1053 * Returns : JB_ERR_OK on success
1054 * JB_ERR_MEMORY on out-of-memory
1055 * JB_ERR_PARSE if there's no "=" sign, or if there's
1056 * nothing before the "=" sign (but empty
1057 * values *after* the "=" sign are legal).
1059 *********************************************************************/
1060 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1062 const char * name_end;
1063 const char * value_start;
1069 assert(*line != ' ');
1070 assert(*line != '\t');
1075 value_start = strchr(line, '=');
1076 if ((value_start == NULL) || (value_start == line))
1078 return JB_ERR_PARSE;
1081 name_end = value_start - 1;
1083 /* Eat any whitespace before the '=' */
1084 while ((*name_end == ' ') || (*name_end == '\t'))
1087 * we already know we must have at least 1 non-ws char
1088 * at start of buf - no need to check
1093 name_len = name_end - line + 1; /* Length excluding \0 */
1094 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1096 return JB_ERR_MEMORY;
1098 strncpy(*pname, line, name_len);
1099 (*pname)[name_len] = '\0';
1101 /* Eat any the whitespace after the '=' */
1103 while ((*value_start == ' ') || (*value_start == '\t'))
1108 if (NULL == (*pvalue = strdup(value_start)))
1112 return JB_ERR_MEMORY;
1119 /*********************************************************************
1121 * Function : edit_parse_actions_file
1123 * Description : Parse an actions file in memory.
1125 * Passed linked list must have the "data" member
1126 * zeroed, and must contain valid "next" and
1127 * "unprocessed" fields. The "raw" and "prefix"
1128 * fields are ignored, and "type" is just overwritten.
1130 * Note that on error the file may have been
1134 * 1 : file = Actions file to be parsed in-place.
1136 * Returns : JB_ERR_OK on success
1137 * JB_ERR_MEMORY on out-of-memory
1138 * JB_ERR_PARSE on error
1140 *********************************************************************/
1141 jb_err edit_parse_actions_file(struct editable_file * file)
1143 struct file_line * cur_line;
1145 const char * text; /* Text from a line */
1146 char * name; /* For lines of the form name=value */
1147 char * value; /* For lines of the form name=value */
1148 struct action_alias * alias_list = NULL;
1149 jb_err err = JB_ERR_OK;
1151 /* alias_list contains the aliases defined in this file.
1152 * It might be better to use the "file_line.data" fields
1153 * in the relavent places instead.
1156 cur_line = file->lines;
1158 /* A note about blank line support: Blank lines should only
1159 * ever occur as the last line in the file. This function
1160 * is more forgiving than that - FILE_LINE_BLANK can occur
1164 /* Skip leading blanks. Should only happen if file is
1165 * empty (which is valid, but pointless).
1167 while ( (cur_line != NULL)
1168 && (cur_line->unprocessed[0] == '\0') )
1171 cur_line->type = FILE_LINE_BLANK;
1172 cur_line = cur_line->next;
1175 if ( (cur_line != NULL)
1176 && (cur_line->unprocessed[0] != '{') )
1178 /* File doesn't start with a header */
1179 file->parse_error = cur_line;
1180 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1181 return JB_ERR_PARSE;
1184 if ( (cur_line != NULL) && (0 ==
1185 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1187 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1189 cur_line = cur_line->next;
1190 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1192 if (cur_line->unprocessed[0])
1194 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1196 err = split_line_on_equals(cur_line->unprocessed,
1197 &cur_line->data.setting.name,
1198 &cur_line->data.setting.svalue);
1199 if (err == JB_ERR_MEMORY)
1203 else if (err != JB_ERR_OK)
1205 /* Line does not contain a name=value pair */
1206 file->parse_error = cur_line;
1207 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1208 return JB_ERR_PARSE;
1213 cur_line->type = FILE_LINE_BLANK;
1215 cur_line = cur_line->next;
1219 if ( (cur_line != NULL) && (0 ==
1220 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1222 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1224 cur_line = cur_line->next;
1225 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1227 if (cur_line->unprocessed[0])
1229 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1233 cur_line->type = FILE_LINE_BLANK;
1235 cur_line = cur_line->next;
1239 if ( (cur_line != NULL) && (0 ==
1240 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1242 cur_line->type = FILE_LINE_ALIAS_HEADER;
1244 cur_line = cur_line->next;
1245 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1247 if (cur_line->unprocessed[0])
1249 /* define an alias */
1250 struct action_alias * new_alias;
1252 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1254 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1255 if (err == JB_ERR_MEMORY)
1259 else if (err != JB_ERR_OK)
1261 /* Line does not contain a name=value pair */
1262 file->parse_error = cur_line;
1263 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1264 return JB_ERR_PARSE;
1267 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1272 free_alias_list(alias_list);
1273 return JB_ERR_MEMORY;
1276 err = get_actions(value, alias_list, new_alias->action);
1279 /* Invalid action or out of memory */
1283 free_alias_list(alias_list);
1284 if (err == JB_ERR_MEMORY)
1290 /* Line does not contain a name=value pair */
1291 file->parse_error = cur_line;
1292 file->parse_error_text = "This alias does not specify a valid set of actions.";
1293 return JB_ERR_PARSE;
1299 new_alias->name = name;
1302 new_alias->next = alias_list;
1303 alias_list = new_alias;
1307 cur_line->type = FILE_LINE_BLANK;
1309 cur_line = cur_line->next;
1313 /* Header done, process the main part of the file */
1314 while (cur_line != NULL)
1316 /* At this point, (cur_line->unprocessed[0] == '{') */
1317 assert(cur_line->unprocessed[0] == '{');
1318 text = cur_line->unprocessed + 1;
1319 len = strlen(text) - 1;
1320 if (text[len] != '}')
1322 /* No closing } on header */
1323 free_alias_list(alias_list);
1324 file->parse_error = cur_line;
1325 file->parse_error_text = "Headers starting with '{' must have a "
1326 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1327 "must close with two brackets ('}}').";
1328 return JB_ERR_PARSE;
1333 /* An invalid {{ header. */
1334 free_alias_list(alias_list);
1335 file->parse_error = cur_line;
1336 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1337 "Please remember that the system (two-bracket) headers must "
1338 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1339 "and must appear before any actions (one-bracket) headers. "
1340 "Also note that system headers may not be repeated.";
1341 return JB_ERR_PARSE;
1344 while ( (*text == ' ') || (*text == '\t') )
1350 && ( (text[len - 1] == ' ')
1351 || (text[len - 1] == '\t') ) )
1356 cur_line->type = FILE_LINE_ACTION;
1358 /* Remove {} and make copy */
1359 if (NULL == (value = (char *) malloc(len + 1)))
1362 free_alias_list(alias_list);
1363 return JB_ERR_MEMORY;
1365 strncpy(value, text, len);
1369 err = get_actions(value, alias_list, cur_line->data.action);
1372 /* Invalid action or out of memory */
1374 free_alias_list(alias_list);
1375 if (err == JB_ERR_MEMORY)
1381 /* Line does not contain a name=value pair */
1382 file->parse_error = cur_line;
1383 file->parse_error_text = "This header does not specify a valid set of actions.";
1384 return JB_ERR_PARSE;
1388 /* Done with string - it was clobbered anyway */
1391 /* Process next line */
1392 cur_line = cur_line->next;
1394 /* Loop processing URL patterns */
1395 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1397 if (cur_line->unprocessed[0])
1399 /* Could parse URL here, but this isn't currently needed */
1401 cur_line->type = FILE_LINE_URL;
1405 cur_line->type = FILE_LINE_BLANK;
1407 cur_line = cur_line->next;
1409 } /* End main while(cur_line != NULL) loop */
1411 free_alias_list(alias_list);
1417 /*********************************************************************
1419 * Function : edit_read_file_lines
1421 * Description : Read all the lines of a file into memory.
1422 * Handles whitespace, comments and line continuation.
1425 * 1 : fp = File to read from. On return, this will be
1426 * at EOF but it will not have been closed.
1427 * 2 : pfile = Destination for a linked list of file_lines.
1428 * Will be set to NULL on error.
1430 * Returns : JB_ERR_OK on success
1431 * JB_ERR_MEMORY on out-of-memory
1433 *********************************************************************/
1434 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1436 struct file_line * first_line; /* Keep for return value or to free */
1437 struct file_line * cur_line; /* Current line */
1438 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1446 cur_line = first_line = zalloc(sizeof(struct file_line));
1447 if (cur_line == NULL)
1449 return JB_ERR_MEMORY;
1452 cur_line->type = FILE_LINE_UNPROCESSED;
1454 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1457 /* Out of memory or empty file. */
1458 /* Note that empty file is not an error we propogate up */
1460 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1465 prev_line = cur_line;
1466 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1467 if (cur_line == NULL)
1470 edit_free_file_lines(first_line);
1471 return JB_ERR_MEMORY;
1474 cur_line->type = FILE_LINE_UNPROCESSED;
1476 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1477 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1480 edit_free_file_lines(first_line);
1481 return JB_ERR_MEMORY;
1485 while (rval != JB_ERR_FILE);
1489 /* We allocated one too many - free it */
1490 prev_line->next = NULL;
1493 *pfile = first_line;
1498 /*********************************************************************
1500 * Function : edit_read_file
1502 * Description : Read a complete file into memory.
1503 * Handles CGI parameter parsing. If requested, also
1504 * checks the file's modification timestamp.
1507 * 1 : csp = Current client state (buffers, headers, etc...)
1508 * 2 : parameters = map of cgi parameters.
1509 * 3 : require_version = true to check "ver" parameter.
1510 * 4 : suffix = File extension, e.g. ".action".
1511 * 5 : pfile = Destination for the file. Will be set
1515 * filename : The name of the file to read, without the
1516 * path or ".action" extension.
1517 * ver : (Only if require_version is nonzero)
1518 * Timestamp of the actions file. If wrong, this
1519 * function fails with JB_ERR_MODIFIED.
1521 * Returns : JB_ERR_OK on success
1522 * JB_ERR_MEMORY on out-of-memory
1523 * JB_ERR_CGI_PARAMS if "filename" was not specified
1525 * JB_ERR_FILE if the file cannot be opened or
1527 * JB_ERR_MODIFIED if version checking was requested and
1528 * failed - the file was modified outside
1529 * of this CGI editor instance.
1531 *********************************************************************/
1532 jb_err edit_read_file(struct client_state *csp,
1533 const struct map *parameters,
1534 int require_version,
1536 struct editable_file **pfile)
1538 struct file_line * lines;
1542 const char * identifier;
1543 struct editable_file * file;
1544 unsigned version = 0;
1545 struct stat statbuf[1];
1546 char version_buf[22];
1547 int newline = NEWLINE_UNKNOWN;
1555 err = get_file_name_param(csp, parameters, "f", suffix,
1556 &filename, &identifier);
1562 if (stat(filename, statbuf) < 0)
1564 /* Error, probably file not found. */
1568 version = (unsigned) statbuf->st_mtime;
1570 if (require_version)
1572 unsigned specified_version;
1573 err = get_number_param(csp, parameters, "v", &specified_version);
1580 if (version != specified_version)
1582 return JB_ERR_MODIFIED;
1586 #if defined(AMIGA) || defined(__OS2__)
1587 if (NULL == (fp = fopen(filename,"r")))
1589 if (NULL == (fp = fopen(filename,"rt")))
1590 #endif /* def AMIGA */
1596 err = edit_read_file_lines(fp, &lines, &newline);
1606 file = (struct editable_file *) zalloc(sizeof(*file));
1610 edit_free_file_lines(lines);
1614 file->lines = lines;
1615 file->newline = newline;
1616 file->filename = filename;
1617 file->version = version;
1618 file->identifier = url_encode(identifier);
1620 if (file->identifier == NULL)
1622 edit_free_file(file);
1623 return JB_ERR_MEMORY;
1626 /* Correct file->version_str */
1627 freez(file->version_str);
1628 snprintf(version_buf, 22, "%u", file->version);
1629 version_buf[21] = '\0';
1630 file->version_str = strdup(version_buf);
1631 if (version_buf == NULL)
1633 edit_free_file(file);
1634 return JB_ERR_MEMORY;
1642 /*********************************************************************
1644 * Function : edit_read_actions_file
1646 * Description : Read a complete actions file into memory.
1647 * Handles CGI parameter parsing. If requested, also
1648 * checks the file's modification timestamp.
1650 * If this function detects an error in the categories
1651 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1652 * then it handles it by filling in the specified
1653 * response structure and returning JB_ERR_FILE.
1656 * 1 : csp = Current client state (buffers, headers, etc...)
1657 * 2 : rsp = HTTP response. Only filled in on error.
1658 * 2 : parameters = map of cgi parameters.
1659 * 3 : require_version = true to check "ver" parameter.
1660 * 4 : pfile = Destination for the file. Will be set
1664 * filename : The name of the actions file to read, without the
1665 * path or ".action" extension.
1666 * ver : (Only if require_version is nonzero)
1667 * Timestamp of the actions file. If wrong, this
1668 * function fails with JB_ERR_MODIFIED.
1670 * Returns : JB_ERR_OK on success
1671 * JB_ERR_MEMORY on out-of-memory
1672 * JB_ERR_CGI_PARAMS if "filename" was not specified
1674 * JB_ERR_FILE if the file does not contain valid data,
1675 * or if file cannot be opened or
1676 * contains no data, or if version
1677 * checking was requested and failed.
1679 *********************************************************************/
1680 jb_err edit_read_actions_file(struct client_state *csp,
1681 struct http_response *rsp,
1682 const struct map *parameters,
1683 int require_version,
1684 struct editable_file **pfile)
1687 struct editable_file *file;
1695 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1698 /* Try to handle if possible */
1699 if (err == JB_ERR_FILE)
1701 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1703 else if (err == JB_ERR_MODIFIED)
1705 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1707 if (err == JB_ERR_OK)
1710 * Signal to higher-level CGI code that there was a problem but we
1711 * handled it, they should just return JB_ERR_OK.
1718 err = edit_parse_actions_file(file);
1721 if (err == JB_ERR_PARSE)
1723 err = cgi_error_parse(csp, rsp, file);
1724 if (err == JB_ERR_OK)
1727 * Signal to higher-level CGI code that there was a problem but we
1728 * handled it, they should just return JB_ERR_OK.
1733 edit_free_file(file);
1742 /*********************************************************************
1744 * Function : get_file_name_param
1746 * Description : Get the name of the file to edit from the parameters
1747 * passed to a CGI function. This function handles
1748 * security checks such as blocking urls containing
1749 * "/" or ".", prepending the config file directory,
1750 * and adding the specified suffix.
1752 * (This is an essential security check, otherwise
1753 * users may be able to pass "../../../etc/passwd"
1754 * and overwrite the password file [linux], "prn:"
1755 * and print random data [Windows], etc...)
1757 * This function only allows filenames contining the
1758 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1759 * That's probably too restrictive but at least it's
1763 * 1 : csp = Current client state (buffers, headers, etc...)
1764 * 2 : parameters = map of cgi parameters
1765 * 3 : param_name = The name of the parameter to read
1766 * 4 : suffix = File extension, e.g. ".actions"
1767 * 5 : pfilename = destination for full filename. Caller
1768 * free()s. Set to NULL on error.
1769 * 6 : pparam = destination for partial filename,
1770 * suitable for use in another URL. Allocated as part
1771 * of the map "parameters", so don't free it.
1772 * Set to NULL if not specified.
1774 * Returns : JB_ERR_OK on success
1775 * JB_ERR_MEMORY on out-of-memory
1776 * JB_ERR_CGI_PARAMS if "filename" was not specified
1779 *********************************************************************/
1780 static jb_err get_file_name_param(struct client_state *csp,
1781 const struct map *parameters,
1782 const char *param_name,
1785 const char **pparam)
1789 #if 0 /* Patch to make 3.0.0 work properly. */
1791 #endif /* 0 - Patch to make 3.0.0 work properly. */
1805 param = lookup(parameters, param_name);
1808 return JB_ERR_CGI_PARAMS;
1813 len = strlen(param);
1814 if (len >= FILENAME_MAX)
1817 return JB_ERR_CGI_PARAMS;
1820 /* Check every character to see if it's legal */
1822 while ((ch = *s++) != '\0')
1824 if ( ((ch < 'A') || (ch > 'Z'))
1825 && ((ch < 'a') || (ch > 'z'))
1826 && ((ch < '0') || (ch > '9'))
1830 /* Probable hack attempt. */
1831 return JB_ERR_CGI_PARAMS;
1836 * FIXME Following is a hack to make 3.0.0 work properly.
1837 * Change "#if 0" --> "#if 1" below when we have modular action
1841 #if 0 /* Patch to make 3.0.0 work properly. */
1842 /* Append extension */
1843 name = malloc(len + strlen(suffix) + 1);
1846 return JB_ERR_MEMORY;
1848 strcpy(name, param);
1849 strcpy(name + len, suffix);
1852 fullpath = make_path(csp->config->confdir, name);
1854 #else /* 1 - Patch to make 3.0.0 work properly. */
1855 if ((csp->actions_list == NULL)
1856 || (csp->actions_list->filename == NULL))
1858 return JB_ERR_CGI_PARAMS;
1861 fullpath = ( (csp->actions_list && csp->actions_list->filename)
1862 ? strdup(csp->actions_list->filename) : NULL);
1863 #endif /* 1 - Patch to make 3.0.0 work properly. */
1864 if (fullpath == NULL)
1866 return JB_ERR_MEMORY;
1870 *pfilename = fullpath;
1876 /*********************************************************************
1878 * Function : get_char_param
1880 * Description : Get a single-character parameter passed to a CGI
1884 * 1 : parameters = map of cgi parameters
1885 * 2 : param_name = The name of the parameter to read
1887 * Returns : Uppercase character on success, '\0' on error.
1889 *********************************************************************/
1890 static char get_char_param(const struct map *parameters,
1891 const char *param_name)
1898 ch = *(lookup(parameters, param_name));
1899 if ((ch >= 'a') && (ch <= 'z'))
1901 ch = ch - 'a' + 'A';
1908 /*********************************************************************
1910 * Function : get_string_param
1912 * Description : Get a string paramater, to be used as an
1913 * ACTION_STRING or ACTION_MULTI paramater.
1914 * Validates the input to prevent stupid/malicious
1915 * users from corrupting their action file.
1918 * 1 : parameters = map of cgi parameters
1919 * 2 : param_name = The name of the parameter to read
1920 * 3 : pparam = destination for paramater. Allocated as
1921 * part of the map "parameters", so don't free it.
1922 * Set to NULL if not specified.
1924 * Returns : JB_ERR_OK on success, or if the paramater
1925 * was not specified.
1926 * JB_ERR_MEMORY on out-of-memory.
1927 * JB_ERR_CGI_PARAMS if the paramater is not valid.
1929 *********************************************************************/
1930 static jb_err get_string_param(const struct map *parameters,
1931 const char *param_name,
1932 const char **pparam)
1944 param = lookup(parameters, param_name);
1950 if (strlen(param) >= CGI_ACTION_PARAM_LEN_MAX)
1955 * Note that the length limit is arbitrary, it just seems
1956 * sensible to limit it to *something*. There's no
1957 * technical reason for any limit at all.
1959 return JB_ERR_CGI_PARAMS;
1962 /* Check every character to see if it's legal */
1964 while ((ch = *s++) != '\0')
1966 if ( ((unsigned char)ch < (unsigned char)' ')
1969 /* Probable hack attempt, or user accidentally used '}'. */
1970 return JB_ERR_CGI_PARAMS;
1981 /*********************************************************************
1983 * Function : get_number_param
1985 * Description : Get a non-negative integer from the parameters
1986 * passed to a CGI function.
1989 * 1 : csp = Current client state (buffers, headers, etc...)
1990 * 2 : parameters = map of cgi parameters
1991 * 3 : name = Name of CGI parameter to read
1992 * 4 : pvalue = destination for value.
1993 * Set to -1 on error.
1995 * Returns : JB_ERR_OK on success
1996 * JB_ERR_MEMORY on out-of-memory
1997 * JB_ERR_CGI_PARAMS if the parameter was not specified
2000 *********************************************************************/
2001 static jb_err get_number_param(struct client_state *csp,
2002 const struct map *parameters,
2017 param = lookup(parameters, name);
2020 return JB_ERR_CGI_PARAMS;
2023 /* We don't use atoi because I want to check this carefully... */
2026 while ((ch = *param++) != '\0')
2028 if ((ch < '0') || (ch > '9'))
2030 return JB_ERR_CGI_PARAMS;
2037 * <limits.h> defines UINT_MAX
2039 * (UINT_MAX - ch) / 10 is the largest number that
2040 * can be safely multiplied by 10 then have ch added.
2042 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
2044 return JB_ERR_CGI_PARAMS;
2047 value = value * 10 + ch;
2058 /*********************************************************************
2060 * Function : get_url_spec_param
2062 * Description : Get a URL pattern from the parameters
2063 * passed to a CGI function. Removes leading/trailing
2064 * spaces and validates it.
2067 * 1 : csp = Current client state (buffers, headers, etc...)
2068 * 2 : parameters = map of cgi parameters
2069 * 3 : name = Name of CGI parameter to read
2070 * 4 : pvalue = destination for value. Will be malloc()'d.
2071 * Set to NULL on error.
2073 * Returns : JB_ERR_OK on success
2074 * JB_ERR_MEMORY on out-of-memory
2075 * JB_ERR_CGI_PARAMS if the parameter was not specified
2078 *********************************************************************/
2079 static jb_err get_url_spec_param(struct client_state *csp,
2080 const struct map *parameters,
2084 const char *orig_param;
2087 struct url_spec compiled[1];
2097 orig_param = lookup(parameters, name);
2100 return JB_ERR_CGI_PARAMS;
2103 /* Copy and trim whitespace */
2104 param = strdup(orig_param);
2107 return JB_ERR_MEMORY;
2111 /* Must be non-empty, and can't allow 1st character to be '{' */
2112 if (param[0] == '\0' || param[0] == '{')
2115 return JB_ERR_CGI_PARAMS;
2118 /* Check for embedded newlines */
2119 for (s = param; *s != '\0'; s++)
2121 if ((*s == '\r') || (*s == '\n'))
2124 return JB_ERR_CGI_PARAMS;
2128 /* Check that regex is valid */
2133 return JB_ERR_MEMORY;
2135 err = create_url_spec(compiled, s);
2140 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2142 free_url_spec(compiled);
2144 if (param[strlen(param) - 1] == '\\')
2147 * Must protect trailing '\\' from becoming line continuation character.
2148 * Two methods: 1) If it's a domain only, add a trailing '/'.
2149 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2151 if (strchr(param, '/') == NULL)
2153 err = string_append(¶m, "/");
2157 err = string_append(¶m, "(?:)");
2164 /* Check that the modified regex is valid */
2169 return JB_ERR_MEMORY;
2171 err = create_url_spec(compiled, s);
2176 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2178 free_url_spec(compiled);
2185 /*********************************************************************
2187 * Function : map_radio
2189 * Description : Map a set of radio button values. E.g. if you have
2190 * 3 radio buttons, declare them as:
2191 * <option type="radio" name="xyz" @xyz-a@>
2192 * <option type="radio" name="xyz" @xyz-b@>
2193 * <option type="radio" name="xyz" @xyz-c@>
2194 * Then map one of the @xyz-?@ variables to "checked"
2195 * and all the others to empty by calling:
2196 * map_radio(exports, "xyz", "abc", sel)
2197 * Where 'sel' is 'a', 'b', or 'c'.
2200 * 1 : exports = Exports map to modify.
2201 * 2 : optionname = name for map
2202 * 3 : values = null-terminated list of values;
2203 * 4 : value = Selected value.
2205 * CGI Parameters : None
2207 * Returns : JB_ERR_OK on success
2208 * JB_ERR_MEMORY on out-of-memory
2210 *********************************************************************/
2211 static jb_err map_radio(struct map * exports,
2212 const char * optionname,
2213 const char * values,
2225 len = strlen(optionname);
2226 buf = malloc(len + 3);
2229 return JB_ERR_MEMORY;
2232 strcpy(buf, optionname);
2237 while ((c = *values++) != '\0')
2242 if (map(exports, buf, 1, "", 1))
2244 return JB_ERR_MEMORY;
2250 return map(exports, buf, 0, "checked", 1);
2254 /*********************************************************************
2256 * Function : cgi_error_modified
2258 * Description : CGI function that is called when a file is modified
2259 * outside the CGI editor.
2262 * 1 : csp = Current client state (buffers, headers, etc...)
2263 * 2 : rsp = http_response data structure for output
2264 * 3 : filename = The file that was modified.
2266 * CGI Parameters : none
2268 * Returns : JB_ERR_OK on success
2269 * JB_ERR_MEMORY on out-of-memory error.
2271 *********************************************************************/
2272 jb_err cgi_error_modified(struct client_state *csp,
2273 struct http_response *rsp,
2274 const char *filename)
2276 struct map *exports;
2283 if (NULL == (exports = default_exports(csp, NULL)))
2285 return JB_ERR_MEMORY;
2288 err = map(exports, "f", 1, html_encode(filename), 0);
2295 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2299 /*********************************************************************
2301 * Function : cgi_error_parse
2303 * Description : CGI function that is called when a file cannot
2304 * be parsed by the CGI editor.
2307 * 1 : csp = Current client state (buffers, headers, etc...)
2308 * 2 : rsp = http_response data structure for output
2309 * 3 : file = The file that was modified.
2311 * CGI Parameters : none
2313 * Returns : JB_ERR_OK on success
2314 * JB_ERR_MEMORY on out-of-memory error.
2316 *********************************************************************/
2317 jb_err cgi_error_parse(struct client_state *csp,
2318 struct http_response *rsp,
2319 struct editable_file *file)
2321 struct map *exports;
2323 struct file_line *cur_line;
2329 if (NULL == (exports = default_exports(csp, NULL)))
2331 return JB_ERR_MEMORY;
2334 err = map(exports, "f", 1, file->identifier, 1);
2335 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2337 cur_line = file->parse_error;
2340 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2341 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2349 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2353 /*********************************************************************
2355 * Function : cgi_error_file
2357 * Description : CGI function that is called when a file cannot be
2358 * opened by the CGI editor.
2361 * 1 : csp = Current client state (buffers, headers, etc...)
2362 * 2 : rsp = http_response data structure for output
2363 * 3 : filename = The file that was modified.
2365 * CGI Parameters : none
2367 * Returns : JB_ERR_OK on success
2368 * JB_ERR_MEMORY on out-of-memory error.
2370 *********************************************************************/
2371 jb_err cgi_error_file(struct client_state *csp,
2372 struct http_response *rsp,
2373 const char *filename)
2375 struct map *exports;
2382 if (NULL == (exports = default_exports(csp, NULL)))
2384 return JB_ERR_MEMORY;
2387 err = map(exports, "f", 1, html_encode(filename), 0);
2394 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2398 /*********************************************************************
2400 * Function : cgi_error_bad_param
2402 * Description : CGI function that is called if the parameters
2403 * (query string) for a CGI were wrong.
2406 * 1 : csp = Current client state (buffers, headers, etc...)
2407 * 2 : rsp = http_response data structure for output
2409 * CGI Parameters : none
2411 * Returns : JB_ERR_OK on success
2412 * JB_ERR_MEMORY on out-of-memory error.
2414 *********************************************************************/
2415 jb_err cgi_error_disabled(struct client_state *csp,
2416 struct http_response *rsp)
2418 struct map *exports;
2423 if (NULL == (exports = default_exports(csp, NULL)))
2425 return JB_ERR_MEMORY;
2428 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2432 /*********************************************************************
2434 * Function : cgi_edit_actions
2436 * Description : CGI function that allows the user to choose which
2437 * actions file to edit.
2440 * 1 : csp = Current client state (buffers, headers, etc...)
2441 * 2 : rsp = http_response data structure for output
2442 * 3 : parameters = map of cgi parameters
2444 * CGI Parameters : None
2446 * Returns : JB_ERR_OK on success
2447 * JB_ERR_MEMORY on out-of-memory error
2449 *********************************************************************/
2450 jb_err cgi_edit_actions(struct client_state *csp,
2451 struct http_response *rsp,
2452 const struct map *parameters)
2455 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2457 return cgi_error_disabled(csp, rsp);
2460 /* FIXME: Incomplete */
2461 rsp->status = strdup("302 Local Redirect from Privoxy");
2462 if (rsp->status == NULL)
2464 return JB_ERR_MEMORY;
2466 if (enlist_unique_header(rsp->headers, "Location",
2467 CGI_PREFIX "edit-actions-list?f=default"))
2471 return JB_ERR_MEMORY;
2478 /*********************************************************************
2480 * Function : cgi_edit_actions_list
2482 * Description : CGI function that edits the actions list.
2483 * FIXME: This function shouldn't FATAL ever.
2484 * FIXME: This function doesn't check the retval of map()
2486 * 1 : csp = Current client state (buffers, headers, etc...)
2487 * 2 : rsp = http_response data structure for output
2488 * 3 : parameters = map of cgi parameters
2490 * CGI Parameters : filename
2492 * Returns : JB_ERR_OK on success
2493 * JB_ERR_MEMORY on out-of-memory
2494 * JB_ERR_FILE if the file cannot be opened or
2496 * JB_ERR_CGI_PARAMS if "filename" was not specified
2499 *********************************************************************/
2500 jb_err cgi_edit_actions_list(struct client_state *csp,
2501 struct http_response *rsp,
2502 const struct map *parameters)
2504 char * section_template;
2505 char * url_template;
2510 struct map * exports;
2511 struct map * section_exports;
2512 struct map * url_exports;
2513 struct editable_file * file;
2514 struct file_line * cur_line;
2515 unsigned line_number = 0;
2516 unsigned prev_section_line_number = ((unsigned) (-1));
2520 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2522 return cgi_error_disabled(csp, rsp);
2525 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2528 /* No filename specified, can't read file, or out of memory. */
2529 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2532 if (NULL == (exports = default_exports(csp, NULL)))
2534 edit_free_file(file);
2535 return JB_ERR_MEMORY;
2538 err = map(exports, "f", 1, file->identifier, 1);
2539 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2543 edit_free_file(file);
2548 /* Should do all global exports above this point */
2550 err = template_load(csp, §ion_template, "edit-actions-list-section");
2553 edit_free_file(file);
2555 if (err == JB_ERR_FILE)
2557 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2562 err = template_load(csp, &url_template, "edit-actions-list-url");
2565 free(section_template);
2566 edit_free_file(file);
2568 if (err == JB_ERR_FILE)
2570 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2575 err = template_fill(§ion_template, exports);
2579 edit_free_file(file);
2585 err = template_fill(&url_template, exports);
2588 free(section_template);
2589 edit_free_file(file);
2594 /* Find start of actions in file */
2595 cur_line = file->lines;
2597 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2599 cur_line = cur_line->next;
2603 if (NULL == (sections = strdup("")))
2605 free(section_template);
2607 edit_free_file(file);
2609 return JB_ERR_MEMORY;
2612 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2614 if (NULL == (section_exports = new_map()))
2617 free(section_template);
2619 edit_free_file(file);
2621 return JB_ERR_MEMORY;
2624 snprintf(buf, 50, "%d", line_number);
2625 err = map(section_exports, "s", 1, buf, 1);
2626 if (!err) err = map(section_exports, "actions", 1,
2627 actions_to_html(cur_line->data.action), 0);
2630 && (cur_line->next != NULL)
2631 && (cur_line->next->type == FILE_LINE_URL))
2633 /* This section contains at least one URL, don't allow delete */
2634 err = map_block_killer(section_exports, "empty-section");
2638 if (!err) err = map_block_keep(section_exports, "empty-section");
2641 if (prev_section_line_number != ((unsigned)(-1)))
2643 /* Not last section */
2644 snprintf(buf, 50, "%d", prev_section_line_number);
2645 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2646 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2651 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2653 prev_section_line_number = line_number;
2658 free(section_template);
2660 edit_free_file(file);
2662 free_map(section_exports);
2666 /* Should do all section-specific exports above this point */
2668 if (NULL == (urls = strdup("")))
2671 free(section_template);
2673 edit_free_file(file);
2675 free_map(section_exports);
2676 return JB_ERR_MEMORY;
2681 cur_line = cur_line->next;
2684 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2686 if (NULL == (url_exports = new_map()))
2690 free(section_template);
2692 edit_free_file(file);
2694 free_map(section_exports);
2695 return JB_ERR_MEMORY;
2698 snprintf(buf, 50, "%d", line_number);
2699 err = map(url_exports, "p", 1, buf, 1);
2701 snprintf(buf, 50, "%d", url_1_2);
2702 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2704 if (!err) err = map(url_exports, "url-html", 1,
2705 html_encode(cur_line->unprocessed), 0);
2706 if (!err) err = map(url_exports, "url", 1,
2707 url_encode(cur_line->unprocessed), 0);
2713 free(section_template);
2715 edit_free_file(file);
2717 free_map(section_exports);
2718 free_map(url_exports);
2722 if (NULL == (s = strdup(url_template)))
2726 free(section_template);
2728 edit_free_file(file);
2730 free_map(section_exports);
2731 free_map(url_exports);
2732 return JB_ERR_MEMORY;
2735 err = template_fill(&s, section_exports);
2736 if (!err) err = template_fill(&s, url_exports);
2737 if (!err) err = string_append(&urls, s);
2739 free_map(url_exports);
2746 free(section_template);
2748 edit_free_file(file);
2750 free_map(section_exports);
2754 url_1_2 = 3 - url_1_2;
2756 cur_line = cur_line->next;
2760 err = map(section_exports, "urls", 1, urls, 0);
2762 /* Could also do section-specific exports here, but it wouldn't be as fast */
2764 if ( (cur_line != NULL)
2765 && (cur_line->type == FILE_LINE_ACTION))
2767 /* Not last section */
2768 snprintf(buf, 50, "%d", line_number);
2769 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2770 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2775 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2781 free(section_template);
2783 edit_free_file(file);
2785 free_map(section_exports);
2789 if (NULL == (s = strdup(section_template)))
2792 free(section_template);
2794 edit_free_file(file);
2796 free_map(section_exports);
2797 return JB_ERR_MEMORY;
2800 err = template_fill(&s, section_exports);
2801 if (!err) err = string_append(§ions, s);
2804 free_map(section_exports);
2809 free(section_template);
2811 edit_free_file(file);
2817 edit_free_file(file);
2818 free(section_template);
2821 err = map(exports, "sections", 1, sections, 0);
2828 /* Could also do global exports here, but it wouldn't be as fast */
2830 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2834 /*********************************************************************
2836 * Function : cgi_edit_actions
2838 * Description : CGI function that edits the Actions list.
2841 * 1 : csp = Current client state (buffers, headers, etc...)
2842 * 2 : rsp = http_response data structure for output
2843 * 3 : parameters = map of cgi parameters
2845 * CGI Parameters : None
2847 * Returns : JB_ERR_OK on success
2848 * JB_ERR_MEMORY on out-of-memory
2849 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2850 * specified or not valid.
2852 *********************************************************************/
2853 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2854 struct http_response *rsp,
2855 const struct map *parameters)
2857 struct map * exports;
2859 struct editable_file * file;
2860 struct file_line * cur_line;
2861 unsigned line_number;
2863 struct file_list *filter_file;
2864 struct re_filterfile_spec *filter_group;
2866 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2868 return cgi_error_disabled(csp, rsp);
2871 err = get_number_param(csp, parameters, "s", §ionid);
2877 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2880 /* No filename specified, can't read file, modified, or out of memory. */
2881 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2884 cur_line = file->lines;
2886 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2888 cur_line = cur_line->next;
2891 if ( (cur_line == NULL)
2892 || (line_number != sectionid)
2894 || (cur_line->type != FILE_LINE_ACTION))
2896 /* Invalid "sectionid" parameter */
2897 edit_free_file(file);
2898 return JB_ERR_CGI_PARAMS;
2901 if (NULL == (exports = default_exports(csp, NULL)))
2903 edit_free_file(file);
2904 return JB_ERR_MEMORY;
2907 err = map(exports, "f", 1, file->identifier, 1);
2908 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2909 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2911 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2913 filter_file = csp->rlist;
2914 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2916 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2920 edit_free_file(file);
2925 if (filter_group == NULL)
2927 err = map(exports, "filter-params", 1, "", 1);
2931 /* We have some entries in the filter list */
2934 char * filter_template;
2936 err = template_load(csp, &filter_template, "edit-actions-for-url-filter");
2939 edit_free_file(file);
2941 if (err == JB_ERR_FILE)
2943 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2948 result = strdup("");
2950 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2952 char current_mode = 'x';
2953 struct list_entry *filter_name;
2955 struct map *line_exports;
2958 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2959 while ((filter_name != NULL)
2960 && (0 != strcmp(filter_group->name, filter_name->str)))
2962 filter_name = filter_name->next;
2965 if (filter_name != NULL)
2971 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2972 while ((filter_name != NULL)
2973 && (0 != strcmp(filter_group->name, filter_name->str)))
2975 filter_name = filter_name->next;
2977 if (filter_name != NULL)
2983 /* Generate a unique serial number */
2984 snprintf(number, sizeof(number), "%x", index++);
2985 number[sizeof(number) - 1] = '\0';
2987 line_exports = new_map();
2988 if (line_exports == NULL)
2990 err = JB_ERR_MEMORY;
2995 if (!err) err = map(line_exports, "index", 1, number, 1);
2996 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2997 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2998 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3003 this_line = strdup(filter_template);
3004 if (this_line == NULL) err = JB_ERR_MEMORY;
3006 if (!err) err = template_fill(&this_line, line_exports);
3007 string_join(&result, this_line);
3009 free_map(line_exports);
3014 err = map(exports, "filter-params", 1, result, 0);
3022 if (!err) err = map_radio(exports, "filter-all", "nx",
3023 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3025 edit_free_file(file);
3033 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3037 /*********************************************************************
3039 * Function : cgi_edit_actions_submit
3041 * Description : CGI function that actually edits the Actions list.
3044 * 1 : csp = Current client state (buffers, headers, etc...)
3045 * 2 : rsp = http_response data structure for output
3046 * 3 : parameters = map of cgi parameters
3048 * CGI Parameters : None
3050 * Returns : JB_ERR_OK on success
3051 * JB_ERR_MEMORY on out-of-memory
3052 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3053 * specified or not valid.
3055 *********************************************************************/
3056 jb_err cgi_edit_actions_submit(struct client_state *csp,
3057 struct http_response *rsp,
3058 const struct map *parameters)
3064 struct editable_file * file;
3065 struct file_line * cur_line;
3066 unsigned line_number;
3072 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3074 return cgi_error_disabled(csp, rsp);
3077 err = get_number_param(csp, parameters, "s", §ionid);
3083 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3086 /* No filename specified, can't read file, modified, or out of memory. */
3087 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3090 cur_line = file->lines;
3092 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3094 cur_line = cur_line->next;
3097 if ( (cur_line == NULL)
3098 || (line_number != sectionid)
3100 || (cur_line->type != FILE_LINE_ACTION))
3102 /* Invalid "sectionid" parameter */
3103 edit_free_file(file);
3104 return JB_ERR_CGI_PARAMS;
3107 err = actions_from_radio(parameters, cur_line->data.action);
3111 edit_free_file(file);
3115 ch = get_char_param(parameters, "filter_all");
3118 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3119 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3120 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3124 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3127 for (index = 0; !err; index++)
3134 /* Generate the keys */
3135 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3136 key_value[sizeof(key_value) - 1] = '\0';
3137 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3138 key_name[sizeof(key_name) - 1] = '\0';
3140 err = get_string_param(parameters, key_name, &name);
3150 value = get_char_param(parameters, key_value);
3153 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3154 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3155 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3157 else if (value == 'N')
3159 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3160 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3162 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3163 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3166 else if (value == 'X')
3168 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3169 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3176 edit_free_file(file);
3180 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3183 edit_free_file(file);
3184 return JB_ERR_MEMORY;
3187 len = strlen(actiontext);
3191 * Empty action - must special-case this.
3192 * Simply setting len to 1 is sufficient...
3197 if (NULL == (newtext = malloc(len + 2)))
3201 edit_free_file(file);
3202 return JB_ERR_MEMORY;
3204 strcpy(newtext, actiontext);
3208 newtext[len + 1] = '\0';
3210 freez(cur_line->raw);
3211 freez(cur_line->unprocessed);
3212 cur_line->unprocessed = newtext;
3214 err = edit_write_file(file);
3217 /* Error writing file */
3218 edit_free_file(file);
3222 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3223 string_append(&target, file->identifier);
3225 edit_free_file(file);
3230 return JB_ERR_MEMORY;
3233 rsp->status = strdup("302 Local Redirect from Privoxy");
3234 if (rsp->status == NULL)
3237 return JB_ERR_MEMORY;
3239 err = enlist_unique_header(rsp->headers, "Location", target);
3246 /*********************************************************************
3248 * Function : cgi_edit_actions_url
3250 * Description : CGI function that actually edits a URL pattern in
3254 * 1 : csp = Current client state (buffers, headers, etc...)
3255 * 2 : rsp = http_response data structure for output
3256 * 3 : parameters = map of cgi parameters
3259 * filename : Identifies the file to edit
3260 * ver : File's last-modified time
3261 * section : Line number of section to edit
3262 * pattern : Line number of pattern to edit
3263 * newval : New value for pattern
3265 * Returns : JB_ERR_OK on success
3266 * JB_ERR_MEMORY on out-of-memory
3267 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3268 * specified or not valid.
3270 *********************************************************************/
3271 jb_err cgi_edit_actions_url(struct client_state *csp,
3272 struct http_response *rsp,
3273 const struct map *parameters)
3277 struct editable_file * file;
3278 struct file_line * cur_line;
3279 unsigned line_number;
3283 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3285 return cgi_error_disabled(csp, rsp);
3288 err = get_number_param(csp, parameters, "p", &patternid);
3295 return JB_ERR_CGI_PARAMS;
3298 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3304 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3307 /* No filename specified, can't read file, modified, or out of memory. */
3309 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3313 cur_line = file->lines;
3315 while ((cur_line != NULL) && (line_number < patternid))
3317 cur_line = cur_line->next;
3321 if ( (cur_line == NULL)
3322 || (cur_line->type != FILE_LINE_URL))
3324 /* Invalid "patternid" parameter */
3326 edit_free_file(file);
3327 return JB_ERR_CGI_PARAMS;
3330 /* At this point, the line to edit is in cur_line */
3332 freez(cur_line->raw);
3333 freez(cur_line->unprocessed);
3334 cur_line->unprocessed = new_pattern;
3336 err = edit_write_file(file);
3339 /* Error writing file */
3340 edit_free_file(file);
3344 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3345 string_append(&target, file->identifier);
3347 edit_free_file(file);
3352 return JB_ERR_MEMORY;
3355 rsp->status = strdup("302 Local Redirect from Privoxy");
3356 if (rsp->status == NULL)
3359 return JB_ERR_MEMORY;
3361 err = enlist_unique_header(rsp->headers, "Location", target);
3368 /*********************************************************************
3370 * Function : cgi_edit_actions_add_url
3372 * Description : CGI function that actually adds a URL pattern to
3376 * 1 : csp = Current client state (buffers, headers, etc...)
3377 * 2 : rsp = http_response data structure for output
3378 * 3 : parameters = map of cgi parameters
3381 * filename : Identifies the file to edit
3382 * ver : File's last-modified time
3383 * section : Line number of section to edit
3384 * newval : New pattern
3386 * Returns : JB_ERR_OK on success
3387 * JB_ERR_MEMORY on out-of-memory
3388 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3389 * specified or not valid.
3391 *********************************************************************/
3392 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3393 struct http_response *rsp,
3394 const struct map *parameters)
3398 struct file_line * new_line;
3399 struct editable_file * file;
3400 struct file_line * cur_line;
3401 unsigned line_number;
3405 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3407 return cgi_error_disabled(csp, rsp);
3410 err = get_number_param(csp, parameters, "s", §ionid);
3417 return JB_ERR_CGI_PARAMS;
3420 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3426 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3429 /* No filename specified, can't read file, modified, or out of memory. */
3431 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3435 cur_line = file->lines;
3437 while ((cur_line != NULL) && (line_number < sectionid))
3439 cur_line = cur_line->next;
3443 if ( (cur_line == NULL)
3444 || (cur_line->type != FILE_LINE_ACTION))
3446 /* Invalid "sectionid" parameter */
3448 edit_free_file(file);
3449 return JB_ERR_CGI_PARAMS;
3452 /* At this point, the section header is in cur_line - add after this. */
3454 /* Allocate the new line */
3455 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3456 if (new_line == NULL)
3459 edit_free_file(file);
3460 return JB_ERR_MEMORY;
3463 /* Fill in the data members of the new line */
3464 new_line->raw = NULL;
3465 new_line->prefix = NULL;
3466 new_line->unprocessed = new_pattern;
3467 new_line->type = FILE_LINE_URL;
3469 /* Link new_line into the list, after cur_line */
3470 new_line->next = cur_line->next;
3471 cur_line->next = new_line;
3473 /* Done making changes, now commit */
3475 err = edit_write_file(file);
3478 /* Error writing file */
3479 edit_free_file(file);
3483 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3484 string_append(&target, file->identifier);
3486 edit_free_file(file);
3491 return JB_ERR_MEMORY;
3494 rsp->status = strdup("302 Local Redirect from Privoxy");
3495 if (rsp->status == NULL)
3498 return JB_ERR_MEMORY;
3500 err = enlist_unique_header(rsp->headers, "Location", target);
3507 /*********************************************************************
3509 * Function : cgi_edit_actions_remove_url
3511 * Description : CGI function that actually removes a URL pattern from
3515 * 1 : csp = Current client state (buffers, headers, etc...)
3516 * 2 : rsp = http_response data structure for output
3517 * 3 : parameters = map of cgi parameters
3520 * f : (filename) Identifies the file to edit
3521 * v : (version) File's last-modified time
3522 * p : (pattern) Line number of pattern to remove
3524 * Returns : JB_ERR_OK on success
3525 * JB_ERR_MEMORY on out-of-memory
3526 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3527 * specified or not valid.
3529 *********************************************************************/
3530 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3531 struct http_response *rsp,
3532 const struct map *parameters)
3535 struct editable_file * file;
3536 struct file_line * cur_line;
3537 struct file_line * prev_line;
3538 unsigned line_number;
3542 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3544 return cgi_error_disabled(csp, rsp);
3547 err = get_number_param(csp, parameters, "p", &patternid);
3553 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3556 /* No filename specified, can't read file, modified, or out of memory. */
3557 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3562 cur_line = file->lines;
3564 while ((cur_line != NULL) && (line_number < patternid))
3566 prev_line = cur_line;
3567 cur_line = cur_line->next;
3571 if ( (cur_line == NULL)
3572 || (prev_line == NULL)
3573 || (cur_line->type != FILE_LINE_URL))
3575 /* Invalid "patternid" parameter */
3576 edit_free_file(file);
3577 return JB_ERR_CGI_PARAMS;
3580 /* At this point, the line to remove is in cur_line, and the previous
3581 * one is in prev_line
3584 /* Unlink cur_line */
3585 prev_line->next = cur_line->next;
3586 cur_line->next = NULL;
3589 edit_free_file_lines(cur_line);
3591 err = edit_write_file(file);
3594 /* Error writing file */
3595 edit_free_file(file);
3599 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3600 string_append(&target, file->identifier);
3602 edit_free_file(file);
3607 return JB_ERR_MEMORY;
3610 rsp->status = strdup("302 Local Redirect from Privoxy");
3611 if (rsp->status == NULL)
3614 return JB_ERR_MEMORY;
3616 err = enlist_unique_header(rsp->headers, "Location", target);
3623 /*********************************************************************
3625 * Function : cgi_edit_actions_section_remove
3627 * Description : CGI function that actually removes a whole section from
3628 * the actions file. The section must be empty first
3629 * (else JB_ERR_CGI_PARAMS).
3632 * 1 : csp = Current client state (buffers, headers, etc...)
3633 * 2 : rsp = http_response data structure for output
3634 * 3 : parameters = map of cgi parameters
3637 * f : (filename) Identifies the file to edit
3638 * v : (version) File's last-modified time
3639 * s : (section) Line number of section to edit
3641 * Returns : JB_ERR_OK on success
3642 * JB_ERR_MEMORY on out-of-memory
3643 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3644 * specified or not valid.
3646 *********************************************************************/
3647 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3648 struct http_response *rsp,
3649 const struct map *parameters)
3652 struct editable_file * file;
3653 struct file_line * cur_line;
3654 struct file_line * prev_line;
3655 unsigned line_number;
3659 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3661 return cgi_error_disabled(csp, rsp);
3664 err = get_number_param(csp, parameters, "s", §ionid);
3670 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3673 /* No filename specified, can't read file, modified, or out of memory. */
3674 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3678 cur_line = file->lines;
3681 while ((cur_line != NULL) && (line_number < sectionid))
3683 prev_line = cur_line;
3684 cur_line = cur_line->next;
3688 if ( (cur_line == NULL)
3689 || (cur_line->type != FILE_LINE_ACTION) )
3691 /* Invalid "sectionid" parameter */
3692 edit_free_file(file);
3693 return JB_ERR_CGI_PARAMS;
3696 if ( (cur_line->next != NULL)
3697 && (cur_line->next->type == FILE_LINE_URL) )
3699 /* Section not empty. */
3700 edit_free_file(file);
3701 return JB_ERR_CGI_PARAMS;
3704 /* At this point, the line to remove is in cur_line, and the previous
3705 * one is in prev_line
3708 /* Unlink cur_line */
3709 if (prev_line == NULL)
3711 /* Removing the first line from the file */
3712 file->lines = cur_line->next;
3716 prev_line->next = cur_line->next;
3718 cur_line->next = NULL;
3721 edit_free_file_lines(cur_line);
3723 err = edit_write_file(file);
3726 /* Error writing file */
3727 edit_free_file(file);
3731 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3732 string_append(&target, file->identifier);
3734 edit_free_file(file);
3739 return JB_ERR_MEMORY;
3742 rsp->status = strdup("302 Local Redirect from Privoxy");
3743 if (rsp->status == NULL)
3746 return JB_ERR_MEMORY;
3748 err = enlist_unique_header(rsp->headers, "Location", target);
3755 /*********************************************************************
3757 * Function : cgi_edit_actions_section_add
3759 * Description : CGI function that adds a new empty section to
3763 * 1 : csp = Current client state (buffers, headers, etc...)
3764 * 2 : rsp = http_response data structure for output
3765 * 3 : parameters = map of cgi parameters
3768 * f : (filename) Identifies the file to edit
3769 * v : (version) File's last-modified time
3770 * s : (section) Line number of section to add after, 0 for
3773 * Returns : JB_ERR_OK on success
3774 * JB_ERR_MEMORY on out-of-memory
3775 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3776 * specified or not valid.
3778 *********************************************************************/
3779 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3780 struct http_response *rsp,
3781 const struct map *parameters)
3784 struct file_line * new_line;
3786 struct editable_file * file;
3787 struct file_line * cur_line;
3788 unsigned line_number;
3792 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3794 return cgi_error_disabled(csp, rsp);
3797 err = get_number_param(csp, parameters, "s", §ionid);
3803 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3806 /* No filename specified, can't read file, modified, or out of memory. */
3807 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3811 cur_line = file->lines;
3815 /* Add to start of file */
3816 if (cur_line != NULL)
3818 /* There's something in the file, find the line before the first
3821 while ( (cur_line->next != NULL)
3822 && (cur_line->next->type != FILE_LINE_ACTION) )
3824 cur_line = cur_line->next;
3831 /* Add after stated section. */
3832 while ((cur_line != NULL) && (line_number < sectionid))
3834 cur_line = cur_line->next;
3838 if ( (cur_line == NULL)
3839 || (cur_line->type != FILE_LINE_ACTION))
3841 /* Invalid "sectionid" parameter */
3842 edit_free_file(file);
3843 return JB_ERR_CGI_PARAMS;
3846 /* Skip through the section to find the last line in it. */
3847 while ( (cur_line->next != NULL)
3848 && (cur_line->next->type != FILE_LINE_ACTION) )
3850 cur_line = cur_line->next;
3855 /* At this point, the last line in the previous section is in cur_line
3856 * - add after this. (Or if we need to add as the first line, cur_line
3860 new_text = strdup("{}");
3861 if (NULL == new_text)
3863 edit_free_file(file);
3864 return JB_ERR_MEMORY;
3867 /* Allocate the new line */
3868 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3869 if (new_line == NULL)
3872 edit_free_file(file);
3873 return JB_ERR_MEMORY;
3876 /* Fill in the data members of the new line */
3877 new_line->raw = NULL;
3878 new_line->prefix = NULL;
3879 new_line->unprocessed = new_text;
3880 new_line->type = FILE_LINE_ACTION;
3882 if (cur_line != NULL)
3884 /* Link new_line into the list, after cur_line */
3885 new_line->next = cur_line->next;
3886 cur_line->next = new_line;
3890 /* Link new_line into the list, as first line */
3891 new_line->next = file->lines;
3892 file->lines = new_line;
3895 /* Done making changes, now commit */
3897 err = edit_write_file(file);
3900 /* Error writing file */
3901 edit_free_file(file);
3905 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3906 string_append(&target, file->identifier);
3908 edit_free_file(file);
3913 return JB_ERR_MEMORY;
3916 rsp->status = strdup("302 Local Redirect from Privoxy");
3917 if (rsp->status == NULL)
3920 return JB_ERR_MEMORY;
3922 err = enlist_unique_header(rsp->headers, "Location", target);
3929 /*********************************************************************
3931 * Function : cgi_edit_actions_section_swap
3933 * Description : CGI function that swaps the order of two sections
3934 * in the actions file. Note that this CGI can actually
3935 * swap any two arbitrary sections, but the GUI interface
3936 * currently only allows consecutive sections to be
3940 * 1 : csp = Current client state (buffers, headers, etc...)
3941 * 2 : rsp = http_response data structure for output
3942 * 3 : parameters = map of cgi parameters
3945 * f : (filename) Identifies the file to edit
3946 * v : (version) File's last-modified time
3947 * s1 : (section1) Line number of first section to swap
3948 * s2 : (section2) Line number of second section to swap
3950 * Returns : JB_ERR_OK on success
3951 * JB_ERR_MEMORY on out-of-memory
3952 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3953 * specified or not valid.
3955 *********************************************************************/
3956 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3957 struct http_response *rsp,
3958 const struct map *parameters)
3962 struct editable_file * file;
3963 struct file_line * cur_line;
3964 struct file_line * prev_line;
3965 struct file_line * line_before_section1;
3966 struct file_line * line_start_section1;
3967 struct file_line * line_end_section1;
3968 struct file_line * line_after_section1;
3969 struct file_line * line_before_section2;
3970 struct file_line * line_start_section2;
3971 struct file_line * line_end_section2;
3972 struct file_line * line_after_section2;
3973 unsigned line_number;
3977 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3979 return cgi_error_disabled(csp, rsp);
3982 err = get_number_param(csp, parameters, "s1", §ion1);
3983 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3989 if (section1 > section2)
3991 unsigned temp = section2;
3992 section2 = section1;
3996 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3999 /* No filename specified, can't read file, modified, or out of memory. */
4000 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4003 /* Start at the beginning... */
4005 cur_line = file->lines;
4008 /* ... find section1 ... */
4009 while ((cur_line != NULL) && (line_number < section1))
4011 prev_line = cur_line;
4012 cur_line = cur_line->next;
4016 if ( (cur_line == NULL)
4017 || (cur_line->type != FILE_LINE_ACTION) )
4019 /* Invalid "section1" parameter */
4020 edit_free_file(file);
4021 return JB_ERR_CGI_PARAMS;
4024 /* If no-op, we've validated params and can skip the rest. */
4025 if (section1 != section2)
4027 /* ... find the end of section1 ... */
4028 line_before_section1 = prev_line;
4029 line_start_section1 = cur_line;
4032 prev_line = cur_line;
4033 cur_line = cur_line->next;
4036 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4037 line_end_section1 = prev_line;
4038 line_after_section1 = cur_line;
4040 /* ... find section2 ... */
4041 while ((cur_line != NULL) && (line_number < section2))
4043 prev_line = cur_line;
4044 cur_line = cur_line->next;
4048 if ( (cur_line == NULL)
4049 || (cur_line->type != FILE_LINE_ACTION) )
4051 /* Invalid "section2" parameter */
4052 edit_free_file(file);
4053 return JB_ERR_CGI_PARAMS;
4056 /* ... find the end of section2 ... */
4057 line_before_section2 = prev_line;
4058 line_start_section2 = cur_line;
4061 prev_line = cur_line;
4062 cur_line = cur_line->next;
4065 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4066 line_end_section2 = prev_line;
4067 line_after_section2 = cur_line;
4069 /* Now have all the pointers we need. Do the swap. */
4071 /* Change the pointer to section1 to point to section2 instead */
4072 if (line_before_section1 == NULL)
4074 file->lines = line_start_section2;
4078 line_before_section1->next = line_start_section2;
4081 if (line_before_section2 == line_end_section1)
4083 /* Consecutive sections */
4084 line_end_section2->next = line_start_section1;
4088 line_end_section2->next = line_after_section1;
4089 line_before_section2->next = line_start_section1;
4092 /* Set the pointer from the end of section1 to the rest of the file */
4093 line_end_section1->next = line_after_section2;
4095 err = edit_write_file(file);
4098 /* Error writing file */
4099 edit_free_file(file);
4102 } /* END if (section1 != section2) */
4104 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4105 string_append(&target, file->identifier);
4107 edit_free_file(file);
4112 return JB_ERR_MEMORY;
4115 rsp->status = strdup("302 Local Redirect from Privoxy");
4116 if (rsp->status == NULL)
4119 return JB_ERR_MEMORY;
4121 err = enlist_unique_header(rsp->headers, "Location", target);
4128 /*********************************************************************
4130 * Function : cgi_toggle
4132 * Description : CGI function that adds a new empty section to
4136 * 1 : csp = Current client state (buffers, headers, etc...)
4137 * 2 : rsp = http_response data structure for output
4138 * 3 : parameters = map of cgi parameters
4141 * set : If present, how to change toggle setting:
4142 * "enable", "disable", "toggle", or none (default).
4143 * mini : If present, use mini reply template.
4145 * Returns : JB_ERR_OK on success
4146 * JB_ERR_MEMORY on out-of-memory
4148 *********************************************************************/
4149 jb_err cgi_toggle(struct client_state *csp,
4150 struct http_response *rsp,
4151 const struct map *parameters)
4153 struct map *exports;
4155 const char *template_name;
4162 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4164 return cgi_error_disabled(csp, rsp);
4167 mode = get_char_param(parameters, "set");
4174 else if (mode == 'D')
4179 else if (mode == 'T')
4182 g_bToggleIJB = !g_bToggleIJB;
4185 if (NULL == (exports = default_exports(csp, "toggle")))
4187 return JB_ERR_MEMORY;
4190 err = map_conditional(exports, "enabled", g_bToggleIJB);
4197 template_name = (get_char_param(parameters, "mini")
4201 return template_fill_for_cgi(csp, template_name, exports, rsp);
4205 /*********************************************************************
4207 * Function : javascriptify
4209 * Description : Converts a string into a form JavaScript will like.
4211 * Netscape 4's JavaScript sucks - it doesn't use
4212 * "id" parameters, so you have to set the "name"
4213 * used to submit a form element to something JavaScript
4214 * will like. (Or access the elements by index in an
4215 * array. That array contains >60 elements and will
4216 * be changed whenever we add a new action to the
4217 * editor, so I'm NOT going to use indexes that have
4218 * to be figured out by hand.)
4220 * Currently the only thing we have to worry about
4221 * is "-" ==> "_" conversion.
4223 * This is a length-preserving operation so it is
4224 * carried out in-place, no memory is allocated
4228 * 1 : identifier = String to make JavaScript-friendly.
4232 *********************************************************************/
4233 static void javascriptify(char * identifier)
4235 char * p = identifier;
4236 while (NULL != (p = strchr(p, '-')))
4243 /*********************************************************************
4245 * Function : actions_to_radio
4247 * Description : Converts a actionsfile entry into settings for
4248 * radio buttons and edit boxes on a HTML form.
4251 * 1 : exports = List of substitutions to add to.
4252 * 2 : action = Action to read
4254 * Returns : JB_ERR_OK on success
4255 * JB_ERR_MEMORY on out-of-memory
4257 *********************************************************************/
4258 static jb_err actions_to_radio(struct map * exports,
4259 const struct action_spec *action)
4261 unsigned mask = action->mask;
4262 unsigned add = action->add;
4270 mask = action->mask;
4273 /* sanity - prevents "-feature +feature" */
4277 #define DEFINE_ACTION_BOOL(name, bit) \
4278 if (!(mask & bit)) \
4280 current_mode = 'n'; \
4282 else if (add & bit) \
4284 current_mode = 'y'; \
4288 current_mode = 'x'; \
4290 if (map_radio(exports, name, "ynx", current_mode)) \
4292 return JB_ERR_MEMORY; \
4295 #define DEFINE_ACTION_STRING(name, bit, index) \
4296 DEFINE_ACTION_BOOL(name, bit); \
4299 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4302 checked = !strcmp(action->string[index], value); \
4306 checked = is_default; \
4308 mapped_param |= checked; \
4309 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4311 return JB_ERR_MEMORY; \
4314 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4315 if (map(exports, name "-param-custom", 1, \
4316 ((!mapped_param) ? "checked" : ""), 1)) \
4318 return JB_ERR_MEMORY; \
4320 if (map(exports, name "-param", 1, \
4321 (((add & bit) && !mapped_param) ? \
4322 action->string[index] : default_val), 1)) \
4324 return JB_ERR_MEMORY; \
4327 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4328 if (map(exports, name "-param", 1, \
4329 ((add & bit) ? action->string[index] : default_val), 1)) \
4331 return JB_ERR_MEMORY; \
4334 #define DEFINE_ACTION_MULTI(name, index) \
4335 if (action->multi_add[index]->first) \
4337 current_mode = 'y'; \
4339 else if (action->multi_remove_all[index]) \
4341 current_mode = 'n'; \
4343 else if (action->multi_remove[index]->first) \
4345 current_mode = 'y'; \
4349 current_mode = 'x'; \
4351 if (map_radio(exports, name, "ynx", current_mode)) \
4353 return JB_ERR_MEMORY; \
4356 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4358 #include "actionlist.h"
4360 #undef DEFINE_ACTION_MULTI
4361 #undef DEFINE_ACTION_STRING
4362 #undef DEFINE_ACTION_BOOL
4363 #undef DEFINE_ACTION_ALIAS
4364 #undef DEFINE_CGI_PARAM_CUSTOM
4365 #undef DEFINE_CGI_PARAM_RADIO
4366 #undef DEFINE_CGI_PARAM_NO_RADIO
4372 /*********************************************************************
4374 * Function : actions_from_radio
4376 * Description : Converts a map of parameters passed to a CGI function
4377 * into an actionsfile entry.
4380 * 1 : parameters = parameters to the CGI call
4381 * 2 : action = Action to change. Must be valid before
4382 * the call, actions not specified will be
4385 * Returns : JB_ERR_OK on success
4386 * JB_ERR_MEMORY on out-of-memory
4388 *********************************************************************/
4389 static jb_err actions_from_radio(const struct map * parameters,
4390 struct action_spec *action)
4392 static int first_time = 1;
4396 const char * js_name;
4397 jb_err err = JB_ERR_OK;
4402 /* Statics are generally a potential race condition,
4403 * but in this case we're safe and don't need semaphores.
4404 * Be careful if you modify this function.
4408 #define JAVASCRIPTIFY(dest_var, string) \
4410 static char js_name_arr[] = string; \
4413 javascriptify(js_name_arr); \
4415 dest_var = js_name_arr; \
4418 #define DEFINE_ACTION_BOOL(name, bit) \
4419 JAVASCRIPTIFY(js_name, name); \
4420 ch = get_char_param(parameters, js_name); \
4423 action->add |= bit; \
4424 action->mask |= bit; \
4426 else if (ch == 'N') \
4428 action->add &= ~bit; \
4429 action->mask &= ~bit; \
4431 else if (ch == 'X') \
4433 action->add &= ~bit; \
4434 action->mask |= bit; \
4437 #define DEFINE_ACTION_STRING(name, bit, index) \
4438 JAVASCRIPTIFY(js_name, name); \
4439 ch = get_char_param(parameters, js_name); \
4443 JAVASCRIPTIFY(js_name, name "-mode"); \
4444 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4445 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4447 JAVASCRIPTIFY(js_name, name "-param"); \
4448 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4450 if (param != NULL) \
4452 if (NULL == (param_dup = strdup(param))) \
4454 return JB_ERR_MEMORY; \
4456 freez(action->string[index]); \
4457 action->add |= bit; \
4458 action->mask |= bit; \
4459 action->string[index] = param_dup; \
4462 else if (ch == 'N') \
4464 if (action->add & bit) \
4466 freez(action->string[index]); \
4468 action->add &= ~bit; \
4469 action->mask &= ~bit; \
4471 else if (ch == 'X') \
4473 if (action->add & bit) \
4475 freez(action->string[index]); \
4477 action->add &= ~bit; \
4478 action->mask |= bit; \
4481 #define DEFINE_ACTION_MULTI(name, index) \
4482 JAVASCRIPTIFY(js_name, name); \
4483 ch = get_char_param(parameters, js_name); \
4488 else if (ch == 'N') \
4490 list_remove_all(action->multi_add[index]); \
4491 list_remove_all(action->multi_remove[index]); \
4492 action->multi_remove_all[index] = 1; \
4494 else if (ch == 'X') \
4496 list_remove_all(action->multi_add[index]); \
4497 list_remove_all(action->multi_remove[index]); \
4498 action->multi_remove_all[index] = 0; \
4501 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4503 #include "actionlist.h"
4505 #undef DEFINE_ACTION_MULTI
4506 #undef DEFINE_ACTION_STRING
4507 #undef DEFINE_ACTION_BOOL
4508 #undef DEFINE_ACTION_ALIAS
4509 #undef JAVASCRIPTIFY
4517 #endif /* def FEATURE_CGI_EDIT_ACTIONS */