1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.22 2002/03/24 13:25:43 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://ijbswa.sourceforge.net
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.22 2002/03/24 13:25:43 swa
46 * name change related issues
48 * Revision 1.21 2002/03/22 18:02:48 jongfoster
49 * Fixing remote toggle
51 * Revision 1.20 2002/03/16 20:28:34 oes
52 * Added descriptions to the filters so users will know what they select in the cgi editor
54 * Revision 1.19 2002/03/16 18:38:14 jongfoster
55 * Stopping stupid or malicious users from breaking the actions
56 * file using the web-based editor.
58 * Revision 1.18 2002/03/16 14:57:44 jongfoster
59 * Full support for enabling/disabling modular filters.
61 * Revision 1.17 2002/03/16 14:26:42 jongfoster
62 * First version of modular filters support - READ ONLY!
63 * Fixing a double-free bug in the out-of-memory handling in map_radio().
65 * Revision 1.16 2002/03/07 03:46:17 oes
66 * Fixed compiler warnings
68 * Revision 1.15 2002/03/06 22:54:35 jongfoster
69 * Automated function-comment nitpicking.
71 * Revision 1.14 2002/03/05 00:24:51 jongfoster
72 * Patch to always edit the current actions file.
74 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
75 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
77 * Revision 1.12 2002/03/03 09:18:03 joergs
78 * Made jumbjuster work on AmigaOS again.
80 * Revision 1.11 2002/01/23 01:03:31 jongfoster
81 * Fixing gcc [CygWin] compiler warnings
83 * Revision 1.10 2002/01/23 00:22:59 jongfoster
84 * Adding new function cgi_edit_actions_section_swap(), to reorder
87 * Adding get_url_spec_param() to get a validated URL pattern.
89 * Moving edit_read_line() out of this file and into loaders.c.
91 * Adding missing html_encode() to many CGI functions.
93 * Moving the functions that #include actionlist.h to the end of the file,
94 * because the Visual C++ 97 debugger gets extremely confused if you try
95 * to debug any code that comes after them in the file.
97 * Major optimizations in cgi_edit_actions_list() to reduce the size of
98 * the generated HTML (down 40% from 550k to 304k), with major side-effects
99 * throughout the editor and templates. In particular, the length of the
100 * URLs throughout the editor has been drastically reduced, by cutting
101 * paramater names down to 1 character and CGI names down to 3-4
102 * characters, by removing all non-essential CGI paramaters even at the
103 * expense of having to re-read the actions file for the most trivial
104 * page, and by using relative rather than absolute URLs. This means
105 * that this (typical example):
107 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
108 * filename=ijb&ver=1011487572&section=12&pattern=13
109 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
113 * <a href="eau?f=ijb&v=1011487572&p=13">
115 * Revision 1.9 2002/01/17 20:56:22 jongfoster
116 * Replacing hard references to the URL of the config interface
117 * with #defines from project.h
119 * Revision 1.8 2001/11/30 23:35:51 jongfoster
120 * Renaming actionsfile to ijb.action
122 * Revision 1.7 2001/11/13 00:28:24 jongfoster
123 * - Renaming parameters from edit-actions-for-url so that they only
124 * contain legal JavaScript characters. If we wanted to write
125 * JavaScript that worked with Netscape 4, this is nessacery.
126 * (Note that at the moment the JavaScript doesn't actually work
127 * with Netscape 4, but now this is purely a template issue, not
128 * one affecting code).
129 * - Adding new CGIs for use by non-JavaScript browsers:
130 * edit-actions-url-form
131 * edit-actions-add-url-form
132 * edit-actions-remove-url-form
135 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
136 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
137 * by and __OS2__ ifdef.
139 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
140 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
141 * threads to call select() simultaneously. So, it's time to do a real, live,
142 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
143 * (native). Both versions will work, but using __OS2__ offers multi-threading.
145 * Revision 1.4 2001/10/23 21:48:19 jongfoster
146 * Cleaning up error handling in CGI functions - they now send back
147 * a HTML error page and should never cause a FATAL error. (Fixes one
148 * potential source of "denial of service" attacks).
150 * CGI actions file editor that works and is actually useful.
152 * Ability to toggle JunkBuster remotely using a CGI call.
154 * You can turn off both the above features in the main configuration
155 * file, e.g. if you are running a multi-user proxy.
157 * Revision 1.3 2001/10/14 22:12:49 jongfoster
158 * New version of CGI-based actionsfile editor.
159 * Major changes, including:
160 * - Completely new file parser and file output routines
161 * - edit-actions CGI renamed edit-actions-for-url
162 * - All CGIs now need a filename parameter, except for...
163 * - New CGI edit-actions which doesn't need a filename,
164 * to allow you to start the editor up.
165 * - edit-actions-submit now works, and now automatically
166 * redirects you back to the main edit-actions-list handler.
168 * Revision 1.2 2001/09/16 17:05:14 jongfoster
169 * Removing unused #include showarg.h
171 * Revision 1.1 2001/09/16 15:47:37 jongfoster
172 * First version of CGI-based edit interface. This is very much a
173 * work-in-progress, and you can't actually use it to edit anything
174 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
175 * to have any effect.
178 **********************************************************************/
184 * FIXME: Following includes copied from cgi.c - which are actually needed?
189 #include <sys/types.h>
194 #include <sys/stat.h>
197 #define snprintf _snprintf
198 #endif /* def _WIN32 */
203 #include "cgisimple.h"
207 #include "miscutil.h"
211 /* loadcfg.h is for g_bToggleIJB only */
212 #include "urlmatch.h"
214 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
217 #ifdef FEATURE_CGI_EDIT_ACTIONS
221 struct file_line * next;
229 struct action_spec action[1];
238 /* Add more data types here... e.g.
241 struct url_spec url[1];
245 struct action_spec action[1];
254 #define FILE_LINE_UNPROCESSED 1
255 #define FILE_LINE_BLANK 2
256 #define FILE_LINE_ALIAS_HEADER 3
257 #define FILE_LINE_ALIAS_ENTRY 4
258 #define FILE_LINE_ACTION 5
259 #define FILE_LINE_URL 6
260 #define FILE_LINE_SETTINGS_HEADER 7
261 #define FILE_LINE_SETTINGS_ENTRY 8
262 #define FILE_LINE_DESCRIPTION_HEADER 9
263 #define FILE_LINE_DESCRIPTION_ENTRY 10
268 struct file_line * lines;
269 const char * filename; /* Full pathname - e.g. "/etc/junkbuster/wibble.action" */
270 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
271 /* Pre-encoded with url_encode() for ease of use. */
272 const char * version_str; /* Last modification time, as a string. For CGI param */
273 /* Can be used in URL without using url_param(). */
274 unsigned version; /* Last modification time - prevents chaos with
275 * the browser's "back" button. Note that this is a
276 * time_t cast to an unsigned. When comparing, always
277 * cast the time_t to an unsigned, and *NOT* vice-versa.
278 * This may lose the top few bits, but they're not
279 * significant anyway.
281 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
282 * Note that changing this after the file has been
283 * read in will cause a mess.
285 struct file_line * parse_error; /* On parse error, this is the offending line. */
286 const char * parse_error_text; /* On parse error, this is the problem.
287 * (Statically allocated) */
290 #define CGI_ACTION_PARAM_LEN_MAX 500
292 /* FIXME: Following non-static functions should be prototyped in .h or made static */
294 /* Functions to read and write arbitrary config files */
295 jb_err edit_read_file(struct client_state *csp,
296 const struct map *parameters,
299 struct editable_file **pfile);
300 jb_err edit_write_file(struct editable_file * file);
301 void edit_free_file(struct editable_file * file);
303 /* Functions to read and write actions files */
304 jb_err edit_parse_actions_file(struct editable_file * file);
305 jb_err edit_read_actions_file(struct client_state *csp,
306 struct http_response *rsp,
307 const struct map *parameters,
309 struct editable_file **pfile);
312 jb_err cgi_error_modified(struct client_state *csp,
313 struct http_response *rsp,
314 const char *filename);
315 jb_err cgi_error_parse(struct client_state *csp,
316 struct http_response *rsp,
317 struct editable_file *file);
318 jb_err cgi_error_file(struct client_state *csp,
319 struct http_response *rsp,
320 const char *filename);
321 jb_err cgi_error_disabled(struct client_state *csp,
322 struct http_response *rsp);
324 /* Internal arbitrary config file support functions */
325 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
326 static void edit_free_file_lines(struct file_line * first_line);
328 /* Internal actions file support functions */
329 static int match_actions_file_header_line(const char * line, const char * name);
330 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
332 /* Internal parameter parsing functions */
333 static jb_err get_file_name_param(struct client_state *csp,
334 const struct map *parameters,
335 const char *param_name,
338 const char **pparam);
339 static jb_err get_number_param(struct client_state *csp,
340 const struct map *parameters,
343 static jb_err get_url_spec_param(struct client_state *csp,
344 const struct map *parameters,
347 static jb_err get_string_param(const struct map *parameters,
348 const char *param_name,
349 const char **pparam);
351 /* Internal actionsfile <==> HTML conversion functions */
352 static jb_err map_radio(struct map * exports,
353 const char * optionname,
356 static jb_err actions_to_radio(struct map * exports,
357 const struct action_spec *action);
358 static jb_err actions_from_radio(const struct map * parameters,
359 struct action_spec *action);
362 static jb_err map_copy_parameter_html(struct map *out,
363 const struct map *in,
365 #if 0 /* unused function */
366 static jb_err map_copy_parameter_url(struct map *out,
367 const struct map *in,
369 #endif /* unused function */
371 /*********************************************************************
373 * Function : map_copy_parameter_html
375 * Description : Copy a CGI parameter from one map to another, HTML
379 * 1 : out = target map
380 * 2 : in = source map
381 * 3 : name = name of cgi parameter to copy
383 * Returns : JB_ERR_OK on success
384 * JB_ERR_MEMORY on out-of-memory
385 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
388 *********************************************************************/
389 static jb_err map_copy_parameter_html(struct map *out,
390 const struct map *in,
400 value = lookup(in, name);
401 err = map(out, name, 1, html_encode(value), 0);
408 else if (*value == '\0')
410 return JB_ERR_CGI_PARAMS;
419 #if 0 /* unused function */
420 /*********************************************************************
422 * Function : map_copy_parameter_html
424 * Description : Copy a CGI parameter from one map to another, URL
428 * 1 : out = target map
429 * 2 : in = source map
430 * 3 : name = name of cgi parameter to copy
432 * Returns : JB_ERR_OK on success
433 * JB_ERR_MEMORY on out-of-memory
434 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
437 *********************************************************************/
438 static jb_err map_copy_parameter_url(struct map *out,
439 const struct map *in,
449 value = lookup(in, name);
450 err = map(out, name, 1, url_encode(value), 0);
457 else if (*value == '\0')
459 return JB_ERR_CGI_PARAMS;
466 #endif /* 0 - unused function */
468 /*********************************************************************
470 * Function : cgi_edit_actions_url_form
472 * Description : CGI function that displays a form for
476 * 1 : csp = Current client state (buffers, headers, etc...)
477 * 2 : rsp = http_response data structure for output
478 * 3 : parameters = map of cgi parameters
481 * f : (filename) Identifies the file to edit
482 * v : (version) File's last-modified time
483 * p : (pattern) Line number of pattern to edit
485 * Returns : JB_ERR_OK on success
486 * JB_ERR_MEMORY on out-of-memory
487 * JB_ERR_CGI_PARAMS if the CGI parameters are not
488 * specified or not valid.
490 *********************************************************************/
491 jb_err cgi_edit_actions_url_form(struct client_state *csp,
492 struct http_response *rsp,
493 const struct map *parameters)
495 struct map * exports;
497 struct editable_file * file;
498 struct file_line * cur_line;
499 unsigned line_number;
506 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
508 return cgi_error_disabled(csp, rsp);
511 err = get_number_param(csp, parameters, "p", &patternid);
517 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
520 /* No filename specified, can't read file, modified, or out of memory. */
521 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
524 cur_line = file->lines;
526 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
528 cur_line = cur_line->next;
531 if ( (cur_line == NULL)
532 || (line_number != patternid)
534 || (cur_line->type != FILE_LINE_URL))
536 /* Invalid "patternid" parameter */
537 edit_free_file(file);
538 return JB_ERR_CGI_PARAMS;
541 if (NULL == (exports = default_exports(csp, NULL)))
543 edit_free_file(file);
544 return JB_ERR_MEMORY;
547 err = map(exports, "f", 1, file->identifier, 1);
548 if (!err) err = map(exports, "v", 1, file->version_str, 1);
549 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
550 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
552 edit_free_file(file);
560 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
564 /*********************************************************************
566 * Function : cgi_edit_actions_add_url_form
568 * Description : CGI function that displays a form for
572 * 1 : csp = Current client state (buffers, headers, etc...)
573 * 2 : rsp = http_response data structure for output
574 * 3 : parameters = map of cgi parameters
577 * f : (filename) Identifies the file to edit
578 * v : (version) File's last-modified time
579 * s : (section) Line number of section to edit
581 * Returns : JB_ERR_OK on success
582 * JB_ERR_MEMORY on out-of-memory
583 * JB_ERR_CGI_PARAMS if the CGI parameters are not
584 * specified or not valid.
586 *********************************************************************/
587 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
588 struct http_response *rsp,
589 const struct map *parameters)
598 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
600 return cgi_error_disabled(csp, rsp);
603 if (NULL == (exports = default_exports(csp, NULL)))
605 return JB_ERR_MEMORY;
608 err = map_copy_parameter_html(exports, parameters, "f");
609 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
610 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
618 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
622 /*********************************************************************
624 * Function : cgi_edit_actions_remove_url_form
626 * Description : CGI function that displays a form for
630 * 1 : csp = Current client state (buffers, headers, etc...)
631 * 2 : rsp = http_response data structure for output
632 * 3 : parameters = map of cgi parameters
635 * f : (filename) Identifies the file to edit
636 * v : (version) File's last-modified time
637 * p : (pattern) Line number of pattern to edit
639 * Returns : JB_ERR_OK on success
640 * JB_ERR_MEMORY on out-of-memory
641 * JB_ERR_CGI_PARAMS if the CGI parameters are not
642 * specified or not valid.
644 *********************************************************************/
645 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
646 struct http_response *rsp,
647 const struct map *parameters)
649 struct map * exports;
651 struct editable_file * file;
652 struct file_line * cur_line;
653 unsigned line_number;
660 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
662 return cgi_error_disabled(csp, rsp);
665 err = get_number_param(csp, parameters, "p", &patternid);
671 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
674 /* No filename specified, can't read file, modified, or out of memory. */
675 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
678 cur_line = file->lines;
680 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
682 cur_line = cur_line->next;
685 if ( (cur_line == NULL)
686 || (line_number != patternid)
688 || (cur_line->type != FILE_LINE_URL))
690 /* Invalid "patternid" parameter */
691 edit_free_file(file);
692 return JB_ERR_CGI_PARAMS;
695 if (NULL == (exports = default_exports(csp, NULL)))
697 edit_free_file(file);
698 return JB_ERR_MEMORY;
701 err = map(exports, "f", 1, file->identifier, 1);
702 if (!err) err = map(exports, "v", 1, file->version_str, 1);
703 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
704 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
706 edit_free_file(file);
714 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
718 /*********************************************************************
720 * Function : edit_write_file
722 * Description : Write a complete file to disk.
725 * 1 : filename = File to write to.
726 * 2 : file = Data structure to write.
728 * Returns : JB_ERR_OK on success
729 * JB_ERR_FILE on error writing to file.
730 * JB_ERR_MEMORY on out of memory
732 *********************************************************************/
733 jb_err edit_write_file(struct editable_file * file)
736 struct file_line * cur_line;
737 struct stat statbuf[1];
738 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
739 digits in time_t, assuming this is a 64-bit
740 machine, plus null terminator, plus one
744 assert(file->filename);
746 #if defined(AMIGA) || defined(__OS2__)
747 if (NULL == (fp = fopen(file->filename, "w")))
749 if (NULL == (fp = fopen(file->filename, "wt")))
750 #endif /* def AMIGA */
755 cur_line = file->lines;
756 while (cur_line != NULL)
760 if (fputs(cur_line->raw, fp) < 0)
768 if (cur_line->prefix)
770 if (fputs(cur_line->prefix, fp) < 0)
776 if (cur_line->unprocessed)
778 /* This should be a single line - sanity check. */
779 assert(NULL == strchr(cur_line->unprocessed, '\r'));
780 assert(NULL == strchr(cur_line->unprocessed, '\n'));
782 if (NULL != strchr(cur_line->unprocessed, '#'))
784 /* Must quote '#' characters */
791 /* Count number of # characters, so we know length of output string */
792 src = cur_line->unprocessed;
793 while (NULL != (src = strchr(src, '#')))
800 /* Allocate new memory for string */
801 len = strlen(cur_line->unprocessed);
802 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
804 /* Uh oh, just trashed file! */
806 return JB_ERR_MEMORY;
809 /* Loop through string from end */
810 src = cur_line->unprocessed + len;
811 dest = str + len + numhash;
812 for ( ; len >= 0; len--)
814 if ((*dest-- = *src--) == '#')
818 assert(numhash >= 0);
821 assert(numhash == 0);
822 assert(src + 1 == cur_line->unprocessed);
823 assert(dest + 1 == str);
825 if (fputs(str, fp) < 0)
836 /* Can write without quoting '#' characters. */
837 if (fputs(cur_line->unprocessed, fp) < 0)
843 if (fputs(NEWLINE(file->newline), fp) < 0)
851 /* FIXME: Write data from file->data->whatever */
855 cur_line = cur_line->next;
861 /* Update the version stamp in the file structure, since we just
862 * wrote to the file & changed it's date.
864 if (stat(file->filename, statbuf) < 0)
866 /* Error, probably file not found. */
869 file->version = (unsigned)statbuf->st_mtime;
871 /* Correct file->version_str */
872 freez(file->version_str);
873 snprintf(version_buf, 22, "%u", file->version);
874 version_buf[21] = '\0';
875 file->version_str = strdup(version_buf);
876 if (version_buf == NULL)
878 return JB_ERR_MEMORY;
885 /*********************************************************************
887 * Function : edit_free_file
889 * Description : Free a complete file in memory.
892 * 1 : file = Data structure to free.
896 *********************************************************************/
897 void edit_free_file(struct editable_file * file)
901 /* Silently ignore NULL pointer */
905 edit_free_file_lines(file->lines);
906 freez(file->filename);
907 freez(file->identifier);
908 freez(file->version_str);
910 file->parse_error_text = NULL; /* Statically allocated */
911 file->parse_error = NULL;
917 /*********************************************************************
919 * Function : edit_free_file
921 * Description : Free an entire linked list of file lines.
924 * 1 : first_line = Data structure to free.
928 *********************************************************************/
929 static void edit_free_file_lines(struct file_line * first_line)
931 struct file_line * next_line;
933 while (first_line != NULL)
935 next_line = first_line->next;
936 first_line->next = NULL;
937 freez(first_line->raw);
938 freez(first_line->prefix);
939 freez(first_line->unprocessed);
940 switch(first_line->type)
942 case 0: /* special case if memory zeroed */
943 case FILE_LINE_UNPROCESSED:
944 case FILE_LINE_BLANK:
945 case FILE_LINE_ALIAS_HEADER:
946 case FILE_LINE_SETTINGS_HEADER:
947 case FILE_LINE_DESCRIPTION_HEADER:
948 case FILE_LINE_DESCRIPTION_ENTRY:
949 case FILE_LINE_ALIAS_ENTRY:
951 /* No data is stored for these */
954 case FILE_LINE_ACTION:
955 free_action(first_line->data.action);
958 case FILE_LINE_SETTINGS_ENTRY:
959 freez(first_line->data.setting.name);
960 freez(first_line->data.setting.svalue);
963 /* Should never happen */
967 first_line->type = 0; /* paranoia */
969 first_line = next_line;
974 /*********************************************************************
976 * Function : match_actions_file_header_line
978 * Description : Match an actions file {{header}} line
981 * 1 : line = String from file
982 * 2 : name = Header to match against
984 * Returns : 0 iff they match.
986 *********************************************************************/
987 static int match_actions_file_header_line(const char * line, const char * name)
995 if ((line[0] != '{') || (line[1] != '{'))
1001 /* Look for optional whitespace */
1002 while ( (*line == ' ') || (*line == '\t') )
1007 /* Look for the specified name (case-insensitive) */
1009 if (0 != strncmpic(line, name, len))
1015 /* Look for optional whitespace */
1016 while ( (*line == ' ') || (*line == '\t') )
1021 /* Look for "}}" and end of string*/
1022 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1032 /*********************************************************************
1034 * Function : match_actions_file_header_line
1036 * Description : Match an actions file {{header}} line
1039 * 1 : line = String from file. Must not start with
1040 * whitespace (else infinite loop!)
1041 * 2 : name = Destination for name
1042 * 2 : name = Destination for value
1044 * Returns : JB_ERR_OK on success
1045 * JB_ERR_MEMORY on out-of-memory
1046 * JB_ERR_PARSE if there's no "=" sign, or if there's
1047 * nothing before the "=" sign (but empty
1048 * values *after* the "=" sign are legal).
1050 *********************************************************************/
1051 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1053 const char * name_end;
1054 const char * value_start;
1060 assert(*line != ' ');
1061 assert(*line != '\t');
1066 value_start = strchr(line, '=');
1067 if ((value_start == NULL) || (value_start == line))
1069 return JB_ERR_PARSE;
1072 name_end = value_start - 1;
1074 /* Eat any whitespace before the '=' */
1075 while ((*name_end == ' ') || (*name_end == '\t'))
1078 * we already know we must have at least 1 non-ws char
1079 * at start of buf - no need to check
1084 name_len = name_end - line + 1; /* Length excluding \0 */
1085 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1087 return JB_ERR_MEMORY;
1089 strncpy(*pname, line, name_len);
1090 (*pname)[name_len] = '\0';
1092 /* Eat any the whitespace after the '=' */
1094 while ((*value_start == ' ') || (*value_start == '\t'))
1099 if (NULL == (*pvalue = strdup(value_start)))
1103 return JB_ERR_MEMORY;
1110 /*********************************************************************
1112 * Function : edit_parse_actions_file
1114 * Description : Parse an actions file in memory.
1116 * Passed linked list must have the "data" member
1117 * zeroed, and must contain valid "next" and
1118 * "unprocessed" fields. The "raw" and "prefix"
1119 * fields are ignored, and "type" is just overwritten.
1121 * Note that on error the file may have been
1125 * 1 : file = Actions file to be parsed in-place.
1127 * Returns : JB_ERR_OK on success
1128 * JB_ERR_MEMORY on out-of-memory
1129 * JB_ERR_PARSE on error
1131 *********************************************************************/
1132 jb_err edit_parse_actions_file(struct editable_file * file)
1134 struct file_line * cur_line;
1136 const char * text; /* Text from a line */
1137 char * name; /* For lines of the form name=value */
1138 char * value; /* For lines of the form name=value */
1139 struct action_alias * alias_list = NULL;
1140 jb_err err = JB_ERR_OK;
1142 /* alias_list contains the aliases defined in this file.
1143 * It might be better to use the "file_line.data" fields
1144 * in the relavent places instead.
1147 cur_line = file->lines;
1149 /* A note about blank line support: Blank lines should only
1150 * ever occur as the last line in the file. This function
1151 * is more forgiving than that - FILE_LINE_BLANK can occur
1155 /* Skip leading blanks. Should only happen if file is
1156 * empty (which is valid, but pointless).
1158 while ( (cur_line != NULL)
1159 && (cur_line->unprocessed[0] == '\0') )
1162 cur_line->type = FILE_LINE_BLANK;
1163 cur_line = cur_line->next;
1166 if ( (cur_line != NULL)
1167 && (cur_line->unprocessed[0] != '{') )
1169 /* File doesn't start with a header */
1170 file->parse_error = cur_line;
1171 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1172 return JB_ERR_PARSE;
1175 if ( (cur_line != NULL) && (0 ==
1176 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1178 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1180 cur_line = cur_line->next;
1181 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1183 if (cur_line->unprocessed[0])
1185 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1187 err = split_line_on_equals(cur_line->unprocessed,
1188 &cur_line->data.setting.name,
1189 &cur_line->data.setting.svalue);
1190 if (err == JB_ERR_MEMORY)
1194 else if (err != JB_ERR_OK)
1196 /* Line does not contain a name=value pair */
1197 file->parse_error = cur_line;
1198 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1199 return JB_ERR_PARSE;
1204 cur_line->type = FILE_LINE_BLANK;
1206 cur_line = cur_line->next;
1210 if ( (cur_line != NULL) && (0 ==
1211 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1213 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1215 cur_line = cur_line->next;
1216 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1218 if (cur_line->unprocessed[0])
1220 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1224 cur_line->type = FILE_LINE_BLANK;
1226 cur_line = cur_line->next;
1230 if ( (cur_line != NULL) && (0 ==
1231 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1233 cur_line->type = FILE_LINE_ALIAS_HEADER;
1235 cur_line = cur_line->next;
1236 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1238 if (cur_line->unprocessed[0])
1240 /* define an alias */
1241 struct action_alias * new_alias;
1243 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1245 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1246 if (err == JB_ERR_MEMORY)
1250 else if (err != JB_ERR_OK)
1252 /* Line does not contain a name=value pair */
1253 file->parse_error = cur_line;
1254 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1255 return JB_ERR_PARSE;
1258 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1263 free_alias_list(alias_list);
1264 return JB_ERR_MEMORY;
1267 err = get_actions(value, alias_list, new_alias->action);
1270 /* Invalid action or out of memory */
1274 free_alias_list(alias_list);
1275 if (err == JB_ERR_MEMORY)
1281 /* Line does not contain a name=value pair */
1282 file->parse_error = cur_line;
1283 file->parse_error_text = "This alias does not specify a valid set of actions.";
1284 return JB_ERR_PARSE;
1290 new_alias->name = name;
1293 new_alias->next = alias_list;
1294 alias_list = new_alias;
1298 cur_line->type = FILE_LINE_BLANK;
1300 cur_line = cur_line->next;
1304 /* Header done, process the main part of the file */
1305 while (cur_line != NULL)
1307 /* At this point, (cur_line->unprocessed[0] == '{') */
1308 assert(cur_line->unprocessed[0] == '{');
1309 text = cur_line->unprocessed + 1;
1310 len = strlen(text) - 1;
1311 if (text[len] != '}')
1313 /* No closing } on header */
1314 free_alias_list(alias_list);
1315 file->parse_error = cur_line;
1316 file->parse_error_text = "Headers starting with '{' must have a "
1317 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1318 "must close with two brackets ('}}').";
1319 return JB_ERR_PARSE;
1324 /* An invalid {{ header. */
1325 free_alias_list(alias_list);
1326 file->parse_error = cur_line;
1327 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1328 "Please remember that the system (two-bracket) headers must "
1329 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1330 "and must appear before any actions (one-bracket) headers. "
1331 "Also note that system headers may not be repeated.";
1332 return JB_ERR_PARSE;
1335 while ( (*text == ' ') || (*text == '\t') )
1341 && ( (text[len - 1] == ' ')
1342 || (text[len - 1] == '\t') ) )
1347 cur_line->type = FILE_LINE_ACTION;
1349 /* Remove {} and make copy */
1350 if (NULL == (value = (char *) malloc(len + 1)))
1353 free_alias_list(alias_list);
1354 return JB_ERR_MEMORY;
1356 strncpy(value, text, len);
1360 err = get_actions(value, alias_list, cur_line->data.action);
1363 /* Invalid action or out of memory */
1365 free_alias_list(alias_list);
1366 if (err == JB_ERR_MEMORY)
1372 /* Line does not contain a name=value pair */
1373 file->parse_error = cur_line;
1374 file->parse_error_text = "This header does not specify a valid set of actions.";
1375 return JB_ERR_PARSE;
1379 /* Done with string - it was clobbered anyway */
1382 /* Process next line */
1383 cur_line = cur_line->next;
1385 /* Loop processing URL patterns */
1386 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1388 if (cur_line->unprocessed[0])
1390 /* Could parse URL here, but this isn't currently needed */
1392 cur_line->type = FILE_LINE_URL;
1396 cur_line->type = FILE_LINE_BLANK;
1398 cur_line = cur_line->next;
1400 } /* End main while(cur_line != NULL) loop */
1402 free_alias_list(alias_list);
1408 /*********************************************************************
1410 * Function : edit_read_file_lines
1412 * Description : Read all the lines of a file into memory.
1413 * Handles whitespace, comments and line continuation.
1416 * 1 : fp = File to read from. On return, this will be
1417 * at EOF but it will not have been closed.
1418 * 2 : pfile = Destination for a linked list of file_lines.
1419 * Will be set to NULL on error.
1421 * Returns : JB_ERR_OK on success
1422 * JB_ERR_MEMORY on out-of-memory
1424 *********************************************************************/
1425 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1427 struct file_line * first_line; /* Keep for return value or to free */
1428 struct file_line * cur_line; /* Current line */
1429 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1437 cur_line = first_line = zalloc(sizeof(struct file_line));
1438 if (cur_line == NULL)
1440 return JB_ERR_MEMORY;
1443 cur_line->type = FILE_LINE_UNPROCESSED;
1445 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1448 /* Out of memory or empty file. */
1449 /* Note that empty file is not an error we propogate up */
1451 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1456 prev_line = cur_line;
1457 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1458 if (cur_line == NULL)
1461 edit_free_file_lines(first_line);
1462 return JB_ERR_MEMORY;
1465 cur_line->type = FILE_LINE_UNPROCESSED;
1467 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1468 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1471 edit_free_file_lines(first_line);
1472 return JB_ERR_MEMORY;
1476 while (rval != JB_ERR_FILE);
1480 /* We allocated one too many - free it */
1481 prev_line->next = NULL;
1484 *pfile = first_line;
1489 /*********************************************************************
1491 * Function : edit_read_file
1493 * Description : Read a complete file into memory.
1494 * Handles CGI parameter parsing. If requested, also
1495 * checks the file's modification timestamp.
1498 * 1 : csp = Current client state (buffers, headers, etc...)
1499 * 2 : parameters = map of cgi parameters.
1500 * 3 : require_version = true to check "ver" parameter.
1501 * 4 : suffix = File extension, e.g. ".action".
1502 * 5 : pfile = Destination for the file. Will be set
1506 * filename : The name of the file to read, without the
1507 * path or ".action" extension.
1508 * ver : (Only if require_version is nonzero)
1509 * Timestamp of the actions file. If wrong, this
1510 * function fails with JB_ERR_MODIFIED.
1512 * Returns : JB_ERR_OK on success
1513 * JB_ERR_MEMORY on out-of-memory
1514 * JB_ERR_CGI_PARAMS if "filename" was not specified
1516 * JB_ERR_FILE if the file cannot be opened or
1518 * JB_ERR_MODIFIED if version checking was requested and
1519 * failed - the file was modified outside
1520 * of this CGI editor instance.
1522 *********************************************************************/
1523 jb_err edit_read_file(struct client_state *csp,
1524 const struct map *parameters,
1525 int require_version,
1527 struct editable_file **pfile)
1529 struct file_line * lines;
1533 const char * identifier;
1534 struct editable_file * file;
1535 unsigned version = 0;
1536 struct stat statbuf[1];
1537 char version_buf[22];
1538 int newline = NEWLINE_UNKNOWN;
1546 err = get_file_name_param(csp, parameters, "f", suffix,
1547 &filename, &identifier);
1553 if (stat(filename, statbuf) < 0)
1555 /* Error, probably file not found. */
1559 version = (unsigned) statbuf->st_mtime;
1561 if (require_version)
1563 unsigned specified_version;
1564 err = get_number_param(csp, parameters, "v", &specified_version);
1571 if (version != specified_version)
1573 return JB_ERR_MODIFIED;
1577 #if defined(AMIGA) || defined(__OS2__)
1578 if (NULL == (fp = fopen(filename,"r")))
1580 if (NULL == (fp = fopen(filename,"rt")))
1581 #endif /* def AMIGA */
1587 err = edit_read_file_lines(fp, &lines, &newline);
1597 file = (struct editable_file *) zalloc(sizeof(*file));
1601 edit_free_file_lines(lines);
1605 file->lines = lines;
1606 file->newline = newline;
1607 file->filename = filename;
1608 file->version = version;
1609 file->identifier = url_encode(identifier);
1611 if (file->identifier == NULL)
1613 edit_free_file(file);
1614 return JB_ERR_MEMORY;
1617 /* Correct file->version_str */
1618 freez(file->version_str);
1619 snprintf(version_buf, 22, "%u", file->version);
1620 version_buf[21] = '\0';
1621 file->version_str = strdup(version_buf);
1622 if (version_buf == NULL)
1624 edit_free_file(file);
1625 return JB_ERR_MEMORY;
1633 /*********************************************************************
1635 * Function : edit_read_actions_file
1637 * Description : Read a complete actions file into memory.
1638 * Handles CGI parameter parsing. If requested, also
1639 * checks the file's modification timestamp.
1641 * If this function detects an error in the categories
1642 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1643 * then it handles it by filling in the specified
1644 * response structure and returning JB_ERR_FILE.
1647 * 1 : csp = Current client state (buffers, headers, etc...)
1648 * 2 : rsp = HTTP response. Only filled in on error.
1649 * 2 : parameters = map of cgi parameters.
1650 * 3 : require_version = true to check "ver" parameter.
1651 * 4 : pfile = Destination for the file. Will be set
1655 * filename : The name of the actions file to read, without the
1656 * path or ".action" extension.
1657 * ver : (Only if require_version is nonzero)
1658 * Timestamp of the actions file. If wrong, this
1659 * function fails with JB_ERR_MODIFIED.
1661 * Returns : JB_ERR_OK on success
1662 * JB_ERR_MEMORY on out-of-memory
1663 * JB_ERR_CGI_PARAMS if "filename" was not specified
1665 * JB_ERR_FILE if the file does not contain valid data,
1666 * or if file cannot be opened or
1667 * contains no data, or if version
1668 * checking was requested and failed.
1670 *********************************************************************/
1671 jb_err edit_read_actions_file(struct client_state *csp,
1672 struct http_response *rsp,
1673 const struct map *parameters,
1674 int require_version,
1675 struct editable_file **pfile)
1678 struct editable_file *file;
1686 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1689 /* Try to handle if possible */
1690 if (err == JB_ERR_FILE)
1692 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1694 else if (err == JB_ERR_MODIFIED)
1696 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1698 if (err == JB_ERR_OK)
1701 * Signal to higher-level CGI code that there was a problem but we
1702 * handled it, they should just return JB_ERR_OK.
1709 err = edit_parse_actions_file(file);
1712 if (err == JB_ERR_PARSE)
1714 err = cgi_error_parse(csp, rsp, file);
1715 if (err == JB_ERR_OK)
1718 * Signal to higher-level CGI code that there was a problem but we
1719 * handled it, they should just return JB_ERR_OK.
1724 edit_free_file(file);
1733 /*********************************************************************
1735 * Function : get_file_name_param
1737 * Description : Get the name of the file to edit from the parameters
1738 * passed to a CGI function. This function handles
1739 * security checks such as blocking urls containing
1740 * "/" or ".", prepending the config file directory,
1741 * and adding the specified suffix.
1743 * (This is an essential security check, otherwise
1744 * users may be able to pass "../../../etc/passwd"
1745 * and overwrite the password file [linux], "prn:"
1746 * and print random data [Windows], etc...)
1748 * This function only allows filenames contining the
1749 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1750 * That's probably too restrictive but at least it's
1754 * 1 : csp = Current client state (buffers, headers, etc...)
1755 * 2 : parameters = map of cgi parameters
1756 * 3 : param_name = The name of the parameter to read
1757 * 4 : suffix = File extension, e.g. ".actions"
1758 * 5 : pfilename = destination for full filename. Caller
1759 * free()s. Set to NULL on error.
1760 * 6 : pparam = destination for partial filename,
1761 * suitable for use in another URL. Allocated as part
1762 * of the map "parameters", so don't free it.
1763 * Set to NULL if not specified.
1765 * Returns : JB_ERR_OK on success
1766 * JB_ERR_MEMORY on out-of-memory
1767 * JB_ERR_CGI_PARAMS if "filename" was not specified
1770 *********************************************************************/
1771 static jb_err get_file_name_param(struct client_state *csp,
1772 const struct map *parameters,
1773 const char *param_name,
1776 const char **pparam)
1780 #if 0 /* Patch to make 3.0.0 work properly. */
1782 #endif /* 0 - Patch to make 3.0.0 work properly. */
1796 param = lookup(parameters, param_name);
1799 return JB_ERR_CGI_PARAMS;
1804 len = strlen(param);
1805 if (len >= FILENAME_MAX)
1808 return JB_ERR_CGI_PARAMS;
1811 /* Check every character to see if it's legal */
1813 while ((ch = *s++) != '\0')
1815 if ( ((ch < 'A') || (ch > 'Z'))
1816 && ((ch < 'a') || (ch > 'z'))
1817 && ((ch < '0') || (ch > '9'))
1821 /* Probable hack attempt. */
1822 return JB_ERR_CGI_PARAMS;
1827 * FIXME Following is a hack to make 3.0.0 work properly.
1828 * Change "#if 0" --> "#if 1" below when we have modular action
1832 #if 0 /* Patch to make 3.0.0 work properly. */
1833 /* Append extension */
1834 name = malloc(len + strlen(suffix) + 1);
1837 return JB_ERR_MEMORY;
1839 strcpy(name, param);
1840 strcpy(name + len, suffix);
1843 fullpath = make_path(csp->config->confdir, name);
1845 #else /* 1 - Patch to make 3.0.0 work properly. */
1846 if ((csp->actions_list == NULL)
1847 || (csp->actions_list->filename == NULL))
1849 return JB_ERR_CGI_PARAMS;
1852 fullpath = ( (csp->actions_list && csp->actions_list->filename)
1853 ? strdup(csp->actions_list->filename) : NULL);
1854 #endif /* 1 - Patch to make 3.0.0 work properly. */
1855 if (fullpath == NULL)
1857 return JB_ERR_MEMORY;
1861 *pfilename = fullpath;
1867 /*********************************************************************
1869 * Function : get_char_param
1871 * Description : Get a single-character parameter passed to a CGI
1875 * 1 : parameters = map of cgi parameters
1876 * 2 : param_name = The name of the parameter to read
1878 * Returns : Uppercase character on success, '\0' on error.
1880 *********************************************************************/
1881 static char get_char_param(const struct map *parameters,
1882 const char *param_name)
1889 ch = *(lookup(parameters, param_name));
1890 if ((ch >= 'a') && (ch <= 'z'))
1892 ch = ch - 'a' + 'A';
1899 /*********************************************************************
1901 * Function : get_string_param
1903 * Description : Get a string paramater, to be used as an
1904 * ACTION_STRING or ACTION_MULTI paramater.
1905 * Validates the input to prevent stupid/malicious
1906 * users from corrupting their action file.
1909 * 1 : parameters = map of cgi parameters
1910 * 2 : param_name = The name of the parameter to read
1911 * 3 : pparam = destination for paramater. Allocated as
1912 * part of the map "parameters", so don't free it.
1913 * Set to NULL if not specified.
1915 * Returns : JB_ERR_OK on success, or if the paramater
1916 * was not specified.
1917 * JB_ERR_MEMORY on out-of-memory.
1918 * JB_ERR_CGI_PARAMS if the paramater is not valid.
1920 *********************************************************************/
1921 static jb_err get_string_param(const struct map *parameters,
1922 const char *param_name,
1923 const char **pparam)
1935 param = lookup(parameters, param_name);
1941 if (strlen(param) >= CGI_ACTION_PARAM_LEN_MAX)
1946 * Note that the length limit is arbitrary, it just seems
1947 * sensible to limit it to *something*. There's no
1948 * technical reason for any limit at all.
1950 return JB_ERR_CGI_PARAMS;
1953 /* Check every character to see if it's legal */
1955 while ((ch = *s++) != '\0')
1957 if ( ((unsigned char)ch < (unsigned char)' ')
1960 /* Probable hack attempt, or user accidentally used '}'. */
1961 return JB_ERR_CGI_PARAMS;
1972 /*********************************************************************
1974 * Function : get_number_param
1976 * Description : Get a non-negative integer from the parameters
1977 * passed to a CGI function.
1980 * 1 : csp = Current client state (buffers, headers, etc...)
1981 * 2 : parameters = map of cgi parameters
1982 * 3 : name = Name of CGI parameter to read
1983 * 4 : pvalue = destination for value.
1984 * Set to -1 on error.
1986 * Returns : JB_ERR_OK on success
1987 * JB_ERR_MEMORY on out-of-memory
1988 * JB_ERR_CGI_PARAMS if the parameter was not specified
1991 *********************************************************************/
1992 static jb_err get_number_param(struct client_state *csp,
1993 const struct map *parameters,
2008 param = lookup(parameters, name);
2011 return JB_ERR_CGI_PARAMS;
2014 /* We don't use atoi because I want to check this carefully... */
2017 while ((ch = *param++) != '\0')
2019 if ((ch < '0') || (ch > '9'))
2021 return JB_ERR_CGI_PARAMS;
2028 * <limits.h> defines UINT_MAX
2030 * (UINT_MAX - ch) / 10 is the largest number that
2031 * can be safely multiplied by 10 then have ch added.
2033 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
2035 return JB_ERR_CGI_PARAMS;
2038 value = value * 10 + ch;
2049 /*********************************************************************
2051 * Function : get_url_spec_param
2053 * Description : Get a URL pattern from the parameters
2054 * passed to a CGI function. Removes leading/trailing
2055 * spaces and validates it.
2058 * 1 : csp = Current client state (buffers, headers, etc...)
2059 * 2 : parameters = map of cgi parameters
2060 * 3 : name = Name of CGI parameter to read
2061 * 4 : pvalue = destination for value. Will be malloc()'d.
2062 * Set to NULL on error.
2064 * Returns : JB_ERR_OK on success
2065 * JB_ERR_MEMORY on out-of-memory
2066 * JB_ERR_CGI_PARAMS if the parameter was not specified
2069 *********************************************************************/
2070 static jb_err get_url_spec_param(struct client_state *csp,
2071 const struct map *parameters,
2075 const char *orig_param;
2078 struct url_spec compiled[1];
2088 orig_param = lookup(parameters, name);
2091 return JB_ERR_CGI_PARAMS;
2094 /* Copy and trim whitespace */
2095 param = strdup(orig_param);
2098 return JB_ERR_MEMORY;
2102 /* Must be non-empty, and can't allow 1st character to be '{' */
2103 if (param[0] == '\0' || param[0] == '{')
2106 return JB_ERR_CGI_PARAMS;
2109 /* Check for embedded newlines */
2110 for (s = param; *s != '\0'; s++)
2112 if ((*s == '\r') || (*s == '\n'))
2115 return JB_ERR_CGI_PARAMS;
2119 /* Check that regex is valid */
2124 return JB_ERR_MEMORY;
2126 err = create_url_spec(compiled, s);
2131 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2133 free_url_spec(compiled);
2135 if (param[strlen(param) - 1] == '\\')
2138 * Must protect trailing '\\' from becoming line continuation character.
2139 * Two methods: 1) If it's a domain only, add a trailing '/'.
2140 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2142 if (strchr(param, '/') == NULL)
2144 err = string_append(¶m, "/");
2148 err = string_append(¶m, "(?:)");
2155 /* Check that the modified regex is valid */
2160 return JB_ERR_MEMORY;
2162 err = create_url_spec(compiled, s);
2167 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2169 free_url_spec(compiled);
2176 /*********************************************************************
2178 * Function : map_radio
2180 * Description : Map a set of radio button values. E.g. if you have
2181 * 3 radio buttons, declare them as:
2182 * <option type="radio" name="xyz" @xyz-a@>
2183 * <option type="radio" name="xyz" @xyz-b@>
2184 * <option type="radio" name="xyz" @xyz-c@>
2185 * Then map one of the @xyz-?@ variables to "checked"
2186 * and all the others to empty by calling:
2187 * map_radio(exports, "xyz", "abc", sel)
2188 * Where 'sel' is 'a', 'b', or 'c'.
2191 * 1 : exports = Exports map to modify.
2192 * 2 : optionname = name for map
2193 * 3 : values = null-terminated list of values;
2194 * 4 : value = Selected value.
2196 * CGI Parameters : None
2198 * Returns : JB_ERR_OK on success
2199 * JB_ERR_MEMORY on out-of-memory
2201 *********************************************************************/
2202 static jb_err map_radio(struct map * exports,
2203 const char * optionname,
2204 const char * values,
2216 len = strlen(optionname);
2217 buf = malloc(len + 3);
2220 return JB_ERR_MEMORY;
2223 strcpy(buf, optionname);
2228 while ((c = *values++) != '\0')
2233 if (map(exports, buf, 1, "", 1))
2235 return JB_ERR_MEMORY;
2241 return map(exports, buf, 0, "checked", 1);
2245 /*********************************************************************
2247 * Function : cgi_error_modified
2249 * Description : CGI function that is called when a file is modified
2250 * outside the CGI editor.
2253 * 1 : csp = Current client state (buffers, headers, etc...)
2254 * 2 : rsp = http_response data structure for output
2255 * 3 : filename = The file that was modified.
2257 * CGI Parameters : none
2259 * Returns : JB_ERR_OK on success
2260 * JB_ERR_MEMORY on out-of-memory error.
2262 *********************************************************************/
2263 jb_err cgi_error_modified(struct client_state *csp,
2264 struct http_response *rsp,
2265 const char *filename)
2267 struct map *exports;
2274 if (NULL == (exports = default_exports(csp, NULL)))
2276 return JB_ERR_MEMORY;
2279 err = map(exports, "f", 1, html_encode(filename), 0);
2286 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2290 /*********************************************************************
2292 * Function : cgi_error_parse
2294 * Description : CGI function that is called when a file cannot
2295 * be parsed by the CGI editor.
2298 * 1 : csp = Current client state (buffers, headers, etc...)
2299 * 2 : rsp = http_response data structure for output
2300 * 3 : file = The file that was modified.
2302 * CGI Parameters : none
2304 * Returns : JB_ERR_OK on success
2305 * JB_ERR_MEMORY on out-of-memory error.
2307 *********************************************************************/
2308 jb_err cgi_error_parse(struct client_state *csp,
2309 struct http_response *rsp,
2310 struct editable_file *file)
2312 struct map *exports;
2314 struct file_line *cur_line;
2320 if (NULL == (exports = default_exports(csp, NULL)))
2322 return JB_ERR_MEMORY;
2325 err = map(exports, "f", 1, file->identifier, 1);
2326 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2328 cur_line = file->parse_error;
2331 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2332 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2340 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2344 /*********************************************************************
2346 * Function : cgi_error_file
2348 * Description : CGI function that is called when a file cannot be
2349 * opened by the CGI editor.
2352 * 1 : csp = Current client state (buffers, headers, etc...)
2353 * 2 : rsp = http_response data structure for output
2354 * 3 : filename = The file that was modified.
2356 * CGI Parameters : none
2358 * Returns : JB_ERR_OK on success
2359 * JB_ERR_MEMORY on out-of-memory error.
2361 *********************************************************************/
2362 jb_err cgi_error_file(struct client_state *csp,
2363 struct http_response *rsp,
2364 const char *filename)
2366 struct map *exports;
2373 if (NULL == (exports = default_exports(csp, NULL)))
2375 return JB_ERR_MEMORY;
2378 err = map(exports, "f", 1, html_encode(filename), 0);
2385 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2389 /*********************************************************************
2391 * Function : cgi_error_bad_param
2393 * Description : CGI function that is called if the parameters
2394 * (query string) for a CGI were wrong.
2397 * 1 : csp = Current client state (buffers, headers, etc...)
2398 * 2 : rsp = http_response data structure for output
2400 * CGI Parameters : none
2402 * Returns : JB_ERR_OK on success
2403 * JB_ERR_MEMORY on out-of-memory error.
2405 *********************************************************************/
2406 jb_err cgi_error_disabled(struct client_state *csp,
2407 struct http_response *rsp)
2409 struct map *exports;
2414 if (NULL == (exports = default_exports(csp, NULL)))
2416 return JB_ERR_MEMORY;
2419 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2423 /*********************************************************************
2425 * Function : cgi_edit_actions
2427 * Description : CGI function that allows the user to choose which
2428 * actions file to edit.
2431 * 1 : csp = Current client state (buffers, headers, etc...)
2432 * 2 : rsp = http_response data structure for output
2433 * 3 : parameters = map of cgi parameters
2435 * CGI Parameters : None
2437 * Returns : JB_ERR_OK on success
2438 * JB_ERR_MEMORY on out-of-memory error
2440 *********************************************************************/
2441 jb_err cgi_edit_actions(struct client_state *csp,
2442 struct http_response *rsp,
2443 const struct map *parameters)
2446 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2448 return cgi_error_disabled(csp, rsp);
2451 /* FIXME: Incomplete */
2452 rsp->status = strdup("302 Local Redirect from Privoxy");
2453 if (rsp->status == NULL)
2455 return JB_ERR_MEMORY;
2457 if (enlist_unique_header(rsp->headers, "Location",
2458 CGI_PREFIX "edit-actions-list?f=ijb"))
2462 return JB_ERR_MEMORY;
2469 /*********************************************************************
2471 * Function : cgi_edit_actions_list
2473 * Description : CGI function that edits the actions list.
2474 * FIXME: This function shouldn't FATAL ever.
2475 * FIXME: This function doesn't check the retval of map()
2477 * 1 : csp = Current client state (buffers, headers, etc...)
2478 * 2 : rsp = http_response data structure for output
2479 * 3 : parameters = map of cgi parameters
2481 * CGI Parameters : filename
2483 * Returns : JB_ERR_OK on success
2484 * JB_ERR_MEMORY on out-of-memory
2485 * JB_ERR_FILE if the file cannot be opened or
2487 * JB_ERR_CGI_PARAMS if "filename" was not specified
2490 *********************************************************************/
2491 jb_err cgi_edit_actions_list(struct client_state *csp,
2492 struct http_response *rsp,
2493 const struct map *parameters)
2495 char * section_template;
2496 char * url_template;
2501 struct map * exports;
2502 struct map * section_exports;
2503 struct map * url_exports;
2504 struct editable_file * file;
2505 struct file_line * cur_line;
2506 unsigned line_number = 0;
2507 unsigned prev_section_line_number = ((unsigned) (-1));
2511 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2513 return cgi_error_disabled(csp, rsp);
2516 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2519 /* No filename specified, can't read file, or out of memory. */
2520 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2523 if (NULL == (exports = default_exports(csp, NULL)))
2525 edit_free_file(file);
2526 return JB_ERR_MEMORY;
2529 err = map(exports, "f", 1, file->identifier, 1);
2530 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2534 edit_free_file(file);
2539 /* Should do all global exports above this point */
2541 err = template_load(csp, §ion_template, "edit-actions-list-section");
2544 edit_free_file(file);
2546 if (err == JB_ERR_FILE)
2548 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2553 err = template_load(csp, &url_template, "edit-actions-list-url");
2556 free(section_template);
2557 edit_free_file(file);
2559 if (err == JB_ERR_FILE)
2561 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2566 err = template_fill(§ion_template, exports);
2570 edit_free_file(file);
2576 err = template_fill(&url_template, exports);
2579 free(section_template);
2580 edit_free_file(file);
2585 /* Find start of actions in file */
2586 cur_line = file->lines;
2588 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2590 cur_line = cur_line->next;
2594 if (NULL == (sections = strdup("")))
2596 free(section_template);
2598 edit_free_file(file);
2600 return JB_ERR_MEMORY;
2603 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2605 if (NULL == (section_exports = new_map()))
2608 free(section_template);
2610 edit_free_file(file);
2612 return JB_ERR_MEMORY;
2615 snprintf(buf, 50, "%d", line_number);
2616 err = map(section_exports, "s", 1, buf, 1);
2617 if (!err) err = map(section_exports, "actions", 1,
2618 actions_to_html(cur_line->data.action), 0);
2621 && (cur_line->next != NULL)
2622 && (cur_line->next->type == FILE_LINE_URL))
2624 /* This section contains at least one URL, don't allow delete */
2625 err = map_block_killer(section_exports, "empty-section");
2629 if (!err) err = map_block_keep(section_exports, "empty-section");
2632 if (prev_section_line_number != ((unsigned)(-1)))
2634 /* Not last section */
2635 snprintf(buf, 50, "%d", prev_section_line_number);
2636 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2637 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2642 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2644 prev_section_line_number = line_number;
2649 free(section_template);
2651 edit_free_file(file);
2653 free_map(section_exports);
2657 /* Should do all section-specific exports above this point */
2659 if (NULL == (urls = strdup("")))
2662 free(section_template);
2664 edit_free_file(file);
2666 free_map(section_exports);
2667 return JB_ERR_MEMORY;
2672 cur_line = cur_line->next;
2675 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2677 if (NULL == (url_exports = new_map()))
2681 free(section_template);
2683 edit_free_file(file);
2685 free_map(section_exports);
2686 return JB_ERR_MEMORY;
2689 snprintf(buf, 50, "%d", line_number);
2690 err = map(url_exports, "p", 1, buf, 1);
2692 snprintf(buf, 50, "%d", url_1_2);
2693 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2695 if (!err) err = map(url_exports, "url-html", 1,
2696 html_encode(cur_line->unprocessed), 0);
2697 if (!err) err = map(url_exports, "url", 1,
2698 url_encode(cur_line->unprocessed), 0);
2704 free(section_template);
2706 edit_free_file(file);
2708 free_map(section_exports);
2709 free_map(url_exports);
2713 if (NULL == (s = strdup(url_template)))
2717 free(section_template);
2719 edit_free_file(file);
2721 free_map(section_exports);
2722 free_map(url_exports);
2723 return JB_ERR_MEMORY;
2726 err = template_fill(&s, section_exports);
2727 if (!err) err = template_fill(&s, url_exports);
2728 if (!err) err = string_append(&urls, s);
2730 free_map(url_exports);
2737 free(section_template);
2739 edit_free_file(file);
2741 free_map(section_exports);
2745 url_1_2 = 3 - url_1_2;
2747 cur_line = cur_line->next;
2751 err = map(section_exports, "urls", 1, urls, 0);
2753 /* Could also do section-specific exports here, but it wouldn't be as fast */
2755 if ( (cur_line != NULL)
2756 && (cur_line->type == FILE_LINE_ACTION))
2758 /* Not last section */
2759 snprintf(buf, 50, "%d", line_number);
2760 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2761 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2766 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2772 free(section_template);
2774 edit_free_file(file);
2776 free_map(section_exports);
2780 if (NULL == (s = strdup(section_template)))
2783 free(section_template);
2785 edit_free_file(file);
2787 free_map(section_exports);
2788 return JB_ERR_MEMORY;
2791 err = template_fill(&s, section_exports);
2792 if (!err) err = string_append(§ions, s);
2795 free_map(section_exports);
2800 free(section_template);
2802 edit_free_file(file);
2808 edit_free_file(file);
2809 free(section_template);
2812 err = map(exports, "sections", 1, sections, 0);
2819 /* Could also do global exports here, but it wouldn't be as fast */
2821 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2825 /*********************************************************************
2827 * Function : cgi_edit_actions
2829 * Description : CGI function that edits the Actions list.
2832 * 1 : csp = Current client state (buffers, headers, etc...)
2833 * 2 : rsp = http_response data structure for output
2834 * 3 : parameters = map of cgi parameters
2836 * CGI Parameters : None
2838 * Returns : JB_ERR_OK on success
2839 * JB_ERR_MEMORY on out-of-memory
2840 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2841 * specified or not valid.
2843 *********************************************************************/
2844 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2845 struct http_response *rsp,
2846 const struct map *parameters)
2848 struct map * exports;
2850 struct editable_file * file;
2851 struct file_line * cur_line;
2852 unsigned line_number;
2854 struct file_list *filter_file;
2855 struct re_filterfile_spec *filter_group;
2857 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2859 return cgi_error_disabled(csp, rsp);
2862 err = get_number_param(csp, parameters, "s", §ionid);
2868 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2871 /* No filename specified, can't read file, modified, or out of memory. */
2872 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2875 cur_line = file->lines;
2877 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2879 cur_line = cur_line->next;
2882 if ( (cur_line == NULL)
2883 || (line_number != sectionid)
2885 || (cur_line->type != FILE_LINE_ACTION))
2887 /* Invalid "sectionid" parameter */
2888 edit_free_file(file);
2889 return JB_ERR_CGI_PARAMS;
2892 if (NULL == (exports = default_exports(csp, NULL)))
2894 edit_free_file(file);
2895 return JB_ERR_MEMORY;
2898 err = map(exports, "f", 1, file->identifier, 1);
2899 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2900 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2902 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2904 filter_file = csp->rlist;
2905 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2907 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2911 edit_free_file(file);
2916 if (filter_group == NULL)
2918 err = map(exports, "filter-params", 1, "", 1);
2922 /* We have some entries in the filter list */
2925 char * filter_template;
2927 err = template_load(csp, &filter_template, "edit-actions-for-url-filter");
2930 edit_free_file(file);
2932 if (err == JB_ERR_FILE)
2934 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2939 result = strdup("");
2941 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2943 char current_mode = 'x';
2944 struct list_entry *filter_name;
2946 struct map *line_exports;
2949 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2950 while ((filter_name != NULL)
2951 && (0 != strcmp(filter_group->name, filter_name->str)))
2953 filter_name = filter_name->next;
2956 if (filter_name != NULL)
2962 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2963 while ((filter_name != NULL)
2964 && (0 != strcmp(filter_group->name, filter_name->str)))
2966 filter_name = filter_name->next;
2968 if (filter_name != NULL)
2974 /* Generate a unique serial number */
2975 snprintf(number, sizeof(number), "%x", index++);
2976 number[sizeof(number) - 1] = '\0';
2978 line_exports = new_map();
2979 if (line_exports == NULL)
2981 err = JB_ERR_MEMORY;
2986 if (!err) err = map(line_exports, "index", 1, number, 1);
2987 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2988 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2989 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2994 this_line = strdup(filter_template);
2995 if (this_line == NULL) err = JB_ERR_MEMORY;
2997 if (!err) err = template_fill(&this_line, line_exports);
2998 string_join(&result, this_line);
3000 free_map(line_exports);
3005 err = map(exports, "filter-params", 1, result, 0);
3013 if (!err) err = map_radio(exports, "filter-all", "nx",
3014 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3016 edit_free_file(file);
3024 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3028 /*********************************************************************
3030 * Function : cgi_edit_actions_submit
3032 * Description : CGI function that actually edits the Actions list.
3035 * 1 : csp = Current client state (buffers, headers, etc...)
3036 * 2 : rsp = http_response data structure for output
3037 * 3 : parameters = map of cgi parameters
3039 * CGI Parameters : None
3041 * Returns : JB_ERR_OK on success
3042 * JB_ERR_MEMORY on out-of-memory
3043 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3044 * specified or not valid.
3046 *********************************************************************/
3047 jb_err cgi_edit_actions_submit(struct client_state *csp,
3048 struct http_response *rsp,
3049 const struct map *parameters)
3055 struct editable_file * file;
3056 struct file_line * cur_line;
3057 unsigned line_number;
3063 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3065 return cgi_error_disabled(csp, rsp);
3068 err = get_number_param(csp, parameters, "s", §ionid);
3074 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3077 /* No filename specified, can't read file, modified, or out of memory. */
3078 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3081 cur_line = file->lines;
3083 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3085 cur_line = cur_line->next;
3088 if ( (cur_line == NULL)
3089 || (line_number != sectionid)
3091 || (cur_line->type != FILE_LINE_ACTION))
3093 /* Invalid "sectionid" parameter */
3094 edit_free_file(file);
3095 return JB_ERR_CGI_PARAMS;
3098 err = actions_from_radio(parameters, cur_line->data.action);
3102 edit_free_file(file);
3106 ch = get_char_param(parameters, "filter_all");
3109 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3110 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3111 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3115 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3118 for (index = 0; !err; index++)
3125 /* Generate the keys */
3126 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3127 key_value[sizeof(key_value) - 1] = '\0';
3128 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3129 key_name[sizeof(key_name) - 1] = '\0';
3131 err = get_string_param(parameters, key_name, &name);
3141 value = get_char_param(parameters, key_value);
3144 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3145 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3146 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3148 else if (value == 'N')
3150 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3151 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3153 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3154 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3157 else if (value == 'X')
3159 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3160 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3167 edit_free_file(file);
3171 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3174 edit_free_file(file);
3175 return JB_ERR_MEMORY;
3178 len = strlen(actiontext);
3182 * Empty action - must special-case this.
3183 * Simply setting len to 1 is sufficient...
3188 if (NULL == (newtext = malloc(len + 2)))
3192 edit_free_file(file);
3193 return JB_ERR_MEMORY;
3195 strcpy(newtext, actiontext);
3199 newtext[len + 1] = '\0';
3201 freez(cur_line->raw);
3202 freez(cur_line->unprocessed);
3203 cur_line->unprocessed = newtext;
3205 err = edit_write_file(file);
3208 /* Error writing file */
3209 edit_free_file(file);
3213 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3214 string_append(&target, file->identifier);
3216 edit_free_file(file);
3221 return JB_ERR_MEMORY;
3224 rsp->status = strdup("302 Local Redirect from Privoxy");
3225 if (rsp->status == NULL)
3228 return JB_ERR_MEMORY;
3230 err = enlist_unique_header(rsp->headers, "Location", target);
3237 /*********************************************************************
3239 * Function : cgi_edit_actions_url
3241 * Description : CGI function that actually edits a URL pattern in
3245 * 1 : csp = Current client state (buffers, headers, etc...)
3246 * 2 : rsp = http_response data structure for output
3247 * 3 : parameters = map of cgi parameters
3250 * filename : Identifies the file to edit
3251 * ver : File's last-modified time
3252 * section : Line number of section to edit
3253 * pattern : Line number of pattern to edit
3254 * newval : New value for pattern
3256 * Returns : JB_ERR_OK on success
3257 * JB_ERR_MEMORY on out-of-memory
3258 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3259 * specified or not valid.
3261 *********************************************************************/
3262 jb_err cgi_edit_actions_url(struct client_state *csp,
3263 struct http_response *rsp,
3264 const struct map *parameters)
3268 struct editable_file * file;
3269 struct file_line * cur_line;
3270 unsigned line_number;
3274 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3276 return cgi_error_disabled(csp, rsp);
3279 err = get_number_param(csp, parameters, "p", &patternid);
3286 return JB_ERR_CGI_PARAMS;
3289 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3295 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3298 /* No filename specified, can't read file, modified, or out of memory. */
3300 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3304 cur_line = file->lines;
3306 while ((cur_line != NULL) && (line_number < patternid))
3308 cur_line = cur_line->next;
3312 if ( (cur_line == NULL)
3313 || (cur_line->type != FILE_LINE_URL))
3315 /* Invalid "patternid" parameter */
3317 edit_free_file(file);
3318 return JB_ERR_CGI_PARAMS;
3321 /* At this point, the line to edit is in cur_line */
3323 freez(cur_line->raw);
3324 freez(cur_line->unprocessed);
3325 cur_line->unprocessed = new_pattern;
3327 err = edit_write_file(file);
3330 /* Error writing file */
3331 edit_free_file(file);
3335 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3336 string_append(&target, file->identifier);
3338 edit_free_file(file);
3343 return JB_ERR_MEMORY;
3346 rsp->status = strdup("302 Local Redirect from Privoxy");
3347 if (rsp->status == NULL)
3350 return JB_ERR_MEMORY;
3352 err = enlist_unique_header(rsp->headers, "Location", target);
3359 /*********************************************************************
3361 * Function : cgi_edit_actions_add_url
3363 * Description : CGI function that actually adds a URL pattern to
3367 * 1 : csp = Current client state (buffers, headers, etc...)
3368 * 2 : rsp = http_response data structure for output
3369 * 3 : parameters = map of cgi parameters
3372 * filename : Identifies the file to edit
3373 * ver : File's last-modified time
3374 * section : Line number of section to edit
3375 * newval : New pattern
3377 * Returns : JB_ERR_OK on success
3378 * JB_ERR_MEMORY on out-of-memory
3379 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3380 * specified or not valid.
3382 *********************************************************************/
3383 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3384 struct http_response *rsp,
3385 const struct map *parameters)
3389 struct file_line * new_line;
3390 struct editable_file * file;
3391 struct file_line * cur_line;
3392 unsigned line_number;
3396 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3398 return cgi_error_disabled(csp, rsp);
3401 err = get_number_param(csp, parameters, "s", §ionid);
3408 return JB_ERR_CGI_PARAMS;
3411 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3417 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3420 /* No filename specified, can't read file, modified, or out of memory. */
3422 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3426 cur_line = file->lines;
3428 while ((cur_line != NULL) && (line_number < sectionid))
3430 cur_line = cur_line->next;
3434 if ( (cur_line == NULL)
3435 || (cur_line->type != FILE_LINE_ACTION))
3437 /* Invalid "sectionid" parameter */
3439 edit_free_file(file);
3440 return JB_ERR_CGI_PARAMS;
3443 /* At this point, the section header is in cur_line - add after this. */
3445 /* Allocate the new line */
3446 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3447 if (new_line == NULL)
3450 edit_free_file(file);
3451 return JB_ERR_MEMORY;
3454 /* Fill in the data members of the new line */
3455 new_line->raw = NULL;
3456 new_line->prefix = NULL;
3457 new_line->unprocessed = new_pattern;
3458 new_line->type = FILE_LINE_URL;
3460 /* Link new_line into the list, after cur_line */
3461 new_line->next = cur_line->next;
3462 cur_line->next = new_line;
3464 /* Done making changes, now commit */
3466 err = edit_write_file(file);
3469 /* Error writing file */
3470 edit_free_file(file);
3474 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3475 string_append(&target, file->identifier);
3477 edit_free_file(file);
3482 return JB_ERR_MEMORY;
3485 rsp->status = strdup("302 Local Redirect from Privoxy");
3486 if (rsp->status == NULL)
3489 return JB_ERR_MEMORY;
3491 err = enlist_unique_header(rsp->headers, "Location", target);
3498 /*********************************************************************
3500 * Function : cgi_edit_actions_remove_url
3502 * Description : CGI function that actually removes a URL pattern from
3506 * 1 : csp = Current client state (buffers, headers, etc...)
3507 * 2 : rsp = http_response data structure for output
3508 * 3 : parameters = map of cgi parameters
3511 * f : (filename) Identifies the file to edit
3512 * v : (version) File's last-modified time
3513 * p : (pattern) Line number of pattern to remove
3515 * Returns : JB_ERR_OK on success
3516 * JB_ERR_MEMORY on out-of-memory
3517 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3518 * specified or not valid.
3520 *********************************************************************/
3521 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3522 struct http_response *rsp,
3523 const struct map *parameters)
3526 struct editable_file * file;
3527 struct file_line * cur_line;
3528 struct file_line * prev_line;
3529 unsigned line_number;
3533 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3535 return cgi_error_disabled(csp, rsp);
3538 err = get_number_param(csp, parameters, "p", &patternid);
3544 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3547 /* No filename specified, can't read file, modified, or out of memory. */
3548 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3553 cur_line = file->lines;
3555 while ((cur_line != NULL) && (line_number < patternid))
3557 prev_line = cur_line;
3558 cur_line = cur_line->next;
3562 if ( (cur_line == NULL)
3563 || (prev_line == NULL)
3564 || (cur_line->type != FILE_LINE_URL))
3566 /* Invalid "patternid" parameter */
3567 edit_free_file(file);
3568 return JB_ERR_CGI_PARAMS;
3571 /* At this point, the line to remove is in cur_line, and the previous
3572 * one is in prev_line
3575 /* Unlink cur_line */
3576 prev_line->next = cur_line->next;
3577 cur_line->next = NULL;
3580 edit_free_file_lines(cur_line);
3582 err = edit_write_file(file);
3585 /* Error writing file */
3586 edit_free_file(file);
3590 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3591 string_append(&target, file->identifier);
3593 edit_free_file(file);
3598 return JB_ERR_MEMORY;
3601 rsp->status = strdup("302 Local Redirect from Privoxy");
3602 if (rsp->status == NULL)
3605 return JB_ERR_MEMORY;
3607 err = enlist_unique_header(rsp->headers, "Location", target);
3614 /*********************************************************************
3616 * Function : cgi_edit_actions_section_remove
3618 * Description : CGI function that actually removes a whole section from
3619 * the actions file. The section must be empty first
3620 * (else JB_ERR_CGI_PARAMS).
3623 * 1 : csp = Current client state (buffers, headers, etc...)
3624 * 2 : rsp = http_response data structure for output
3625 * 3 : parameters = map of cgi parameters
3628 * f : (filename) Identifies the file to edit
3629 * v : (version) File's last-modified time
3630 * s : (section) Line number of section to edit
3632 * Returns : JB_ERR_OK on success
3633 * JB_ERR_MEMORY on out-of-memory
3634 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3635 * specified or not valid.
3637 *********************************************************************/
3638 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3639 struct http_response *rsp,
3640 const struct map *parameters)
3643 struct editable_file * file;
3644 struct file_line * cur_line;
3645 struct file_line * prev_line;
3646 unsigned line_number;
3650 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3652 return cgi_error_disabled(csp, rsp);
3655 err = get_number_param(csp, parameters, "s", §ionid);
3661 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3664 /* No filename specified, can't read file, modified, or out of memory. */
3665 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3669 cur_line = file->lines;
3672 while ((cur_line != NULL) && (line_number < sectionid))
3674 prev_line = cur_line;
3675 cur_line = cur_line->next;
3679 if ( (cur_line == NULL)
3680 || (cur_line->type != FILE_LINE_ACTION) )
3682 /* Invalid "sectionid" parameter */
3683 edit_free_file(file);
3684 return JB_ERR_CGI_PARAMS;
3687 if ( (cur_line->next != NULL)
3688 && (cur_line->next->type == FILE_LINE_URL) )
3690 /* Section not empty. */
3691 edit_free_file(file);
3692 return JB_ERR_CGI_PARAMS;
3695 /* At this point, the line to remove is in cur_line, and the previous
3696 * one is in prev_line
3699 /* Unlink cur_line */
3700 if (prev_line == NULL)
3702 /* Removing the first line from the file */
3703 file->lines = cur_line->next;
3707 prev_line->next = cur_line->next;
3709 cur_line->next = NULL;
3712 edit_free_file_lines(cur_line);
3714 err = edit_write_file(file);
3717 /* Error writing file */
3718 edit_free_file(file);
3722 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3723 string_append(&target, file->identifier);
3725 edit_free_file(file);
3730 return JB_ERR_MEMORY;
3733 rsp->status = strdup("302 Local Redirect from Privoxy");
3734 if (rsp->status == NULL)
3737 return JB_ERR_MEMORY;
3739 err = enlist_unique_header(rsp->headers, "Location", target);
3746 /*********************************************************************
3748 * Function : cgi_edit_actions_section_add
3750 * Description : CGI function that adds a new empty section to
3754 * 1 : csp = Current client state (buffers, headers, etc...)
3755 * 2 : rsp = http_response data structure for output
3756 * 3 : parameters = map of cgi parameters
3759 * f : (filename) Identifies the file to edit
3760 * v : (version) File's last-modified time
3761 * s : (section) Line number of section to add after, 0 for
3764 * Returns : JB_ERR_OK on success
3765 * JB_ERR_MEMORY on out-of-memory
3766 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3767 * specified or not valid.
3769 *********************************************************************/
3770 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3771 struct http_response *rsp,
3772 const struct map *parameters)
3775 struct file_line * new_line;
3777 struct editable_file * file;
3778 struct file_line * cur_line;
3779 unsigned line_number;
3783 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3785 return cgi_error_disabled(csp, rsp);
3788 err = get_number_param(csp, parameters, "s", §ionid);
3794 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3797 /* No filename specified, can't read file, modified, or out of memory. */
3798 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3802 cur_line = file->lines;
3806 /* Add to start of file */
3807 if (cur_line != NULL)
3809 /* There's something in the file, find the line before the first
3812 while ( (cur_line->next != NULL)
3813 && (cur_line->next->type != FILE_LINE_ACTION) )
3815 cur_line = cur_line->next;
3822 /* Add after stated section. */
3823 while ((cur_line != NULL) && (line_number < sectionid))
3825 cur_line = cur_line->next;
3829 if ( (cur_line == NULL)
3830 || (cur_line->type != FILE_LINE_ACTION))
3832 /* Invalid "sectionid" parameter */
3833 edit_free_file(file);
3834 return JB_ERR_CGI_PARAMS;
3837 /* Skip through the section to find the last line in it. */
3838 while ( (cur_line->next != NULL)
3839 && (cur_line->next->type != FILE_LINE_ACTION) )
3841 cur_line = cur_line->next;
3846 /* At this point, the last line in the previous section is in cur_line
3847 * - add after this. (Or if we need to add as the first line, cur_line
3851 new_text = strdup("{}");
3852 if (NULL == new_text)
3854 edit_free_file(file);
3855 return JB_ERR_MEMORY;
3858 /* Allocate the new line */
3859 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3860 if (new_line == NULL)
3863 edit_free_file(file);
3864 return JB_ERR_MEMORY;
3867 /* Fill in the data members of the new line */
3868 new_line->raw = NULL;
3869 new_line->prefix = NULL;
3870 new_line->unprocessed = new_text;
3871 new_line->type = FILE_LINE_ACTION;
3873 if (cur_line != NULL)
3875 /* Link new_line into the list, after cur_line */
3876 new_line->next = cur_line->next;
3877 cur_line->next = new_line;
3881 /* Link new_line into the list, as first line */
3882 new_line->next = file->lines;
3883 file->lines = new_line;
3886 /* Done making changes, now commit */
3888 err = edit_write_file(file);
3891 /* Error writing file */
3892 edit_free_file(file);
3896 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3897 string_append(&target, file->identifier);
3899 edit_free_file(file);
3904 return JB_ERR_MEMORY;
3907 rsp->status = strdup("302 Local Redirect from Privoxy");
3908 if (rsp->status == NULL)
3911 return JB_ERR_MEMORY;
3913 err = enlist_unique_header(rsp->headers, "Location", target);
3920 /*********************************************************************
3922 * Function : cgi_edit_actions_section_swap
3924 * Description : CGI function that swaps the order of two sections
3925 * in the actions file. Note that this CGI can actually
3926 * swap any two arbitrary sections, but the GUI interface
3927 * currently only allows consecutive sections to be
3931 * 1 : csp = Current client state (buffers, headers, etc...)
3932 * 2 : rsp = http_response data structure for output
3933 * 3 : parameters = map of cgi parameters
3936 * f : (filename) Identifies the file to edit
3937 * v : (version) File's last-modified time
3938 * s1 : (section1) Line number of first section to swap
3939 * s2 : (section2) Line number of second section to swap
3941 * Returns : JB_ERR_OK on success
3942 * JB_ERR_MEMORY on out-of-memory
3943 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3944 * specified or not valid.
3946 *********************************************************************/
3947 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3948 struct http_response *rsp,
3949 const struct map *parameters)
3953 struct editable_file * file;
3954 struct file_line * cur_line;
3955 struct file_line * prev_line;
3956 struct file_line * line_before_section1;
3957 struct file_line * line_start_section1;
3958 struct file_line * line_end_section1;
3959 struct file_line * line_after_section1;
3960 struct file_line * line_before_section2;
3961 struct file_line * line_start_section2;
3962 struct file_line * line_end_section2;
3963 struct file_line * line_after_section2;
3964 unsigned line_number;
3968 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3970 return cgi_error_disabled(csp, rsp);
3973 err = get_number_param(csp, parameters, "s1", §ion1);
3974 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3980 if (section1 > section2)
3982 unsigned temp = section2;
3983 section2 = section1;
3987 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3990 /* No filename specified, can't read file, modified, or out of memory. */
3991 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3994 /* Start at the beginning... */
3996 cur_line = file->lines;
3999 /* ... find section1 ... */
4000 while ((cur_line != NULL) && (line_number < section1))
4002 prev_line = cur_line;
4003 cur_line = cur_line->next;
4007 if ( (cur_line == NULL)
4008 || (cur_line->type != FILE_LINE_ACTION) )
4010 /* Invalid "section1" parameter */
4011 edit_free_file(file);
4012 return JB_ERR_CGI_PARAMS;
4015 /* If no-op, we've validated params and can skip the rest. */
4016 if (section1 != section2)
4018 /* ... find the end of section1 ... */
4019 line_before_section1 = prev_line;
4020 line_start_section1 = cur_line;
4023 prev_line = cur_line;
4024 cur_line = cur_line->next;
4027 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4028 line_end_section1 = prev_line;
4029 line_after_section1 = cur_line;
4031 /* ... find section2 ... */
4032 while ((cur_line != NULL) && (line_number < section2))
4034 prev_line = cur_line;
4035 cur_line = cur_line->next;
4039 if ( (cur_line == NULL)
4040 || (cur_line->type != FILE_LINE_ACTION) )
4042 /* Invalid "section2" parameter */
4043 edit_free_file(file);
4044 return JB_ERR_CGI_PARAMS;
4047 /* ... find the end of section2 ... */
4048 line_before_section2 = prev_line;
4049 line_start_section2 = cur_line;
4052 prev_line = cur_line;
4053 cur_line = cur_line->next;
4056 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4057 line_end_section2 = prev_line;
4058 line_after_section2 = cur_line;
4060 /* Now have all the pointers we need. Do the swap. */
4062 /* Change the pointer to section1 to point to section2 instead */
4063 if (line_before_section1 == NULL)
4065 file->lines = line_start_section2;
4069 line_before_section1->next = line_start_section2;
4072 if (line_before_section2 == line_end_section1)
4074 /* Consecutive sections */
4075 line_end_section2->next = line_start_section1;
4079 line_end_section2->next = line_after_section1;
4080 line_before_section2->next = line_start_section1;
4083 /* Set the pointer from the end of section1 to the rest of the file */
4084 line_end_section1->next = line_after_section2;
4086 err = edit_write_file(file);
4089 /* Error writing file */
4090 edit_free_file(file);
4093 } /* END if (section1 != section2) */
4095 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4096 string_append(&target, file->identifier);
4098 edit_free_file(file);
4103 return JB_ERR_MEMORY;
4106 rsp->status = strdup("302 Local Redirect from Privoxy");
4107 if (rsp->status == NULL)
4110 return JB_ERR_MEMORY;
4112 err = enlist_unique_header(rsp->headers, "Location", target);
4119 /*********************************************************************
4121 * Function : cgi_toggle
4123 * Description : CGI function that adds a new empty section to
4127 * 1 : csp = Current client state (buffers, headers, etc...)
4128 * 2 : rsp = http_response data structure for output
4129 * 3 : parameters = map of cgi parameters
4132 * set : If present, how to change toggle setting:
4133 * "enable", "disable", "toggle", or none (default).
4134 * mini : If present, use mini reply template.
4136 * Returns : JB_ERR_OK on success
4137 * JB_ERR_MEMORY on out-of-memory
4139 *********************************************************************/
4140 jb_err cgi_toggle(struct client_state *csp,
4141 struct http_response *rsp,
4142 const struct map *parameters)
4144 struct map *exports;
4146 const char *template_name;
4153 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4155 return cgi_error_disabled(csp, rsp);
4158 if (NULL == (exports = default_exports(csp, "toggle")))
4160 return JB_ERR_MEMORY;
4163 mode = get_char_param(parameters, "set");
4170 else if (mode == 'D')
4175 else if (mode == 'T')
4178 g_bToggleIJB = !g_bToggleIJB;
4181 err = map_conditional(exports, "enabled", g_bToggleIJB);
4188 template_name = (get_char_param(parameters, "mini")
4192 return template_fill_for_cgi(csp, template_name, exports, rsp);
4196 /*********************************************************************
4198 * Function : javascriptify
4200 * Description : Converts a string into a form JavaScript will like.
4202 * Netscape 4's JavaScript sucks - it doesn't use
4203 * "id" parameters, so you have to set the "name"
4204 * used to submit a form element to something JavaScript
4205 * will like. (Or access the elements by index in an
4206 * array. That array contains >60 elements and will
4207 * be changed whenever we add a new action to the
4208 * editor, so I'm NOT going to use indexes that have
4209 * to be figured out by hand.)
4211 * Currently the only thing we have to worry about
4212 * is "-" ==> "_" conversion.
4214 * This is a length-preserving operation so it is
4215 * carried out in-place, no memory is allocated
4219 * 1 : identifier = String to make JavaScript-friendly.
4223 *********************************************************************/
4224 static void javascriptify(char * identifier)
4226 char * p = identifier;
4227 while (NULL != (p = strchr(p, '-')))
4234 /*********************************************************************
4236 * Function : actions_to_radio
4238 * Description : Converts a actionsfile entry into settings for
4239 * radio buttons and edit boxes on a HTML form.
4242 * 1 : exports = List of substitutions to add to.
4243 * 2 : action = Action to read
4245 * Returns : JB_ERR_OK on success
4246 * JB_ERR_MEMORY on out-of-memory
4248 *********************************************************************/
4249 static jb_err actions_to_radio(struct map * exports,
4250 const struct action_spec *action)
4252 unsigned mask = action->mask;
4253 unsigned add = action->add;
4261 mask = action->mask;
4264 /* sanity - prevents "-feature +feature" */
4268 #define DEFINE_ACTION_BOOL(name, bit) \
4269 if (!(mask & bit)) \
4271 current_mode = 'n'; \
4273 else if (add & bit) \
4275 current_mode = 'y'; \
4279 current_mode = 'x'; \
4281 if (map_radio(exports, name, "ynx", current_mode)) \
4283 return JB_ERR_MEMORY; \
4286 #define DEFINE_ACTION_STRING(name, bit, index) \
4287 DEFINE_ACTION_BOOL(name, bit); \
4290 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4293 checked = !strcmp(action->string[index], value); \
4297 checked = is_default; \
4299 mapped_param |= checked; \
4300 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4302 return JB_ERR_MEMORY; \
4305 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4306 if (map(exports, name "-param-custom", 1, \
4307 ((!mapped_param) ? "checked" : ""), 1)) \
4309 return JB_ERR_MEMORY; \
4311 if (map(exports, name "-param", 1, \
4312 (((add & bit) && !mapped_param) ? \
4313 action->string[index] : default_val), 1)) \
4315 return JB_ERR_MEMORY; \
4318 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4319 if (map(exports, name "-param", 1, \
4320 ((add & bit) ? action->string[index] : default_val), 1)) \
4322 return JB_ERR_MEMORY; \
4325 #define DEFINE_ACTION_MULTI(name, index) \
4326 if (action->multi_add[index]->first) \
4328 current_mode = 'y'; \
4330 else if (action->multi_remove_all[index]) \
4332 current_mode = 'n'; \
4334 else if (action->multi_remove[index]->first) \
4336 current_mode = 'y'; \
4340 current_mode = 'x'; \
4342 if (map_radio(exports, name, "ynx", current_mode)) \
4344 return JB_ERR_MEMORY; \
4347 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4349 #include "actionlist.h"
4351 #undef DEFINE_ACTION_MULTI
4352 #undef DEFINE_ACTION_STRING
4353 #undef DEFINE_ACTION_BOOL
4354 #undef DEFINE_ACTION_ALIAS
4355 #undef DEFINE_CGI_PARAM_CUSTOM
4356 #undef DEFINE_CGI_PARAM_RADIO
4357 #undef DEFINE_CGI_PARAM_NO_RADIO
4363 /*********************************************************************
4365 * Function : actions_from_radio
4367 * Description : Converts a map of parameters passed to a CGI function
4368 * into an actionsfile entry.
4371 * 1 : parameters = parameters to the CGI call
4372 * 2 : action = Action to change. Must be valid before
4373 * the call, actions not specified will be
4376 * Returns : JB_ERR_OK on success
4377 * JB_ERR_MEMORY on out-of-memory
4379 *********************************************************************/
4380 static jb_err actions_from_radio(const struct map * parameters,
4381 struct action_spec *action)
4383 static int first_time = 1;
4387 const char * js_name;
4388 jb_err err = JB_ERR_OK;
4393 /* Statics are generally a potential race condition,
4394 * but in this case we're safe and don't need semaphores.
4395 * Be careful if you modify this function.
4399 #define JAVASCRIPTIFY(dest_var, string) \
4401 static char js_name_arr[] = string; \
4404 javascriptify(js_name_arr); \
4406 dest_var = js_name_arr; \
4409 #define DEFINE_ACTION_BOOL(name, bit) \
4410 JAVASCRIPTIFY(js_name, name); \
4411 ch = get_char_param(parameters, js_name); \
4414 action->add |= bit; \
4415 action->mask |= bit; \
4417 else if (ch == 'N') \
4419 action->add &= ~bit; \
4420 action->mask &= ~bit; \
4422 else if (ch == 'X') \
4424 action->add &= ~bit; \
4425 action->mask |= bit; \
4428 #define DEFINE_ACTION_STRING(name, bit, index) \
4429 JAVASCRIPTIFY(js_name, name); \
4430 ch = get_char_param(parameters, js_name); \
4434 JAVASCRIPTIFY(js_name, name "-mode"); \
4435 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4436 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4438 JAVASCRIPTIFY(js_name, name "-param"); \
4439 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4441 if (param != NULL) \
4443 if (NULL == (param_dup = strdup(param))) \
4445 return JB_ERR_MEMORY; \
4447 freez(action->string[index]); \
4448 action->add |= bit; \
4449 action->mask |= bit; \
4450 action->string[index] = param_dup; \
4453 else if (ch == 'N') \
4455 if (action->add & bit) \
4457 freez(action->string[index]); \
4459 action->add &= ~bit; \
4460 action->mask &= ~bit; \
4462 else if (ch == 'X') \
4464 if (action->add & bit) \
4466 freez(action->string[index]); \
4468 action->add &= ~bit; \
4469 action->mask |= bit; \
4472 #define DEFINE_ACTION_MULTI(name, index) \
4473 JAVASCRIPTIFY(js_name, name); \
4474 ch = get_char_param(parameters, js_name); \
4479 else if (ch == 'N') \
4481 list_remove_all(action->multi_add[index]); \
4482 list_remove_all(action->multi_remove[index]); \
4483 action->multi_remove_all[index] = 1; \
4485 else if (ch == 'X') \
4487 list_remove_all(action->multi_add[index]); \
4488 list_remove_all(action->multi_remove[index]); \
4489 action->multi_remove_all[index] = 0; \
4492 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4494 #include "actionlist.h"
4496 #undef DEFINE_ACTION_MULTI
4497 #undef DEFINE_ACTION_STRING
4498 #undef DEFINE_ACTION_BOOL
4499 #undef DEFINE_ACTION_ALIAS
4500 #undef JAVASCRIPTIFY
4508 #endif /* def FEATURE_CGI_EDIT_ACTIONS */