1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.28 2002/03/27 12:30:29 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
10 * NOTE: The CGIs in this file use parameter names
11 * such as "f" and "s" which are really *BAD* choices.
12 * However, I'm trying to save bytes in the
13 * edit-actions-list HTML page - the standard actions
14 * file generated a 550kbyte page, which is ridiculous.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001 the SourceForge
19 * Privoxy team. http://www.privoxy.org/
21 * Based on the Internet Junkbuster originally written
22 * by and Copyright (C) 1997 Anonymous Coders and
23 * Junkbusters Corporation. http://www.junkbusters.com
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General
27 * Public License as published by the Free Software
28 * Foundation; either version 2 of the License, or (at
29 * your option) any later version.
31 * This program is distributed in the hope that it will
32 * be useful, but WITHOUT ANY WARRANTY; without even the
33 * implied warranty of MERCHANTABILITY or FITNESS FOR A
34 * PARTICULAR PURPOSE. See the GNU General Public
35 * License for more details.
37 * The GNU General Public License should be included with
38 * this file. If not, you can view it at
39 * http://www.gnu.org/copyleft/gpl.html
40 * or write to the Free Software Foundation, Inc., 59
41 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 * Revision 1.28 2002/03/27 12:30:29 oes
46 * Deleted unsused variable
48 * Revision 1.27 2002/03/26 23:06:04 jongfoster
49 * Removing duplicate @ifs on the toggle page
51 * Revision 1.26 2002/03/26 22:59:17 jongfoster
52 * Fixing /toggle to display status consistently.
54 * Revision 1.25 2002/03/26 22:29:54 swa
55 * we have a new homepage!
57 * Revision 1.24 2002/03/24 15:23:33 jongfoster
60 * Revision 1.23 2002/03/24 13:32:41 swa
61 * name change related issues
63 * Revision 1.22 2002/03/24 13:25:43 swa
64 * name change related issues
66 * Revision 1.21 2002/03/22 18:02:48 jongfoster
67 * Fixing remote toggle
69 * Revision 1.20 2002/03/16 20:28:34 oes
70 * Added descriptions to the filters so users will know what they select in the cgi editor
72 * Revision 1.19 2002/03/16 18:38:14 jongfoster
73 * Stopping stupid or malicious users from breaking the actions
74 * file using the web-based editor.
76 * Revision 1.18 2002/03/16 14:57:44 jongfoster
77 * Full support for enabling/disabling modular filters.
79 * Revision 1.17 2002/03/16 14:26:42 jongfoster
80 * First version of modular filters support - READ ONLY!
81 * Fixing a double-free bug in the out-of-memory handling in map_radio().
83 * Revision 1.16 2002/03/07 03:46:17 oes
84 * Fixed compiler warnings
86 * Revision 1.15 2002/03/06 22:54:35 jongfoster
87 * Automated function-comment nitpicking.
89 * Revision 1.14 2002/03/05 00:24:51 jongfoster
90 * Patch to always edit the current actions file.
92 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
93 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
95 * Revision 1.12 2002/03/03 09:18:03 joergs
96 * Made jumbjuster work on AmigaOS again.
98 * Revision 1.11 2002/01/23 01:03:31 jongfoster
99 * Fixing gcc [CygWin] compiler warnings
101 * Revision 1.10 2002/01/23 00:22:59 jongfoster
102 * Adding new function cgi_edit_actions_section_swap(), to reorder
105 * Adding get_url_spec_param() to get a validated URL pattern.
107 * Moving edit_read_line() out of this file and into loaders.c.
109 * Adding missing html_encode() to many CGI functions.
111 * Moving the functions that #include actionlist.h to the end of the file,
112 * because the Visual C++ 97 debugger gets extremely confused if you try
113 * to debug any code that comes after them in the file.
115 * Major optimizations in cgi_edit_actions_list() to reduce the size of
116 * the generated HTML (down 40% from 550k to 304k), with major side-effects
117 * throughout the editor and templates. In particular, the length of the
118 * URLs throughout the editor has been drastically reduced, by cutting
119 * paramater names down to 1 character and CGI names down to 3-4
120 * characters, by removing all non-essential CGI paramaters even at the
121 * expense of having to re-read the actions file for the most trivial
122 * page, and by using relative rather than absolute URLs. This means
123 * that this (typical example):
125 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
126 * filename=ijb&ver=1011487572&section=12&pattern=13
127 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
131 * <a href="eau?f=ijb&v=1011487572&p=13">
133 * Revision 1.9 2002/01/17 20:56:22 jongfoster
134 * Replacing hard references to the URL of the config interface
135 * with #defines from project.h
137 * Revision 1.8 2001/11/30 23:35:51 jongfoster
138 * Renaming actionsfile to ijb.action
140 * Revision 1.7 2001/11/13 00:28:24 jongfoster
141 * - Renaming parameters from edit-actions-for-url so that they only
142 * contain legal JavaScript characters. If we wanted to write
143 * JavaScript that worked with Netscape 4, this is nessacery.
144 * (Note that at the moment the JavaScript doesn't actually work
145 * with Netscape 4, but now this is purely a template issue, not
146 * one affecting code).
147 * - Adding new CGIs for use by non-JavaScript browsers:
148 * edit-actions-url-form
149 * edit-actions-add-url-form
150 * edit-actions-remove-url-form
153 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
154 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
155 * by and __OS2__ ifdef.
157 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
158 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
159 * threads to call select() simultaneously. So, it's time to do a real, live,
160 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
161 * (native). Both versions will work, but using __OS2__ offers multi-threading.
163 * Revision 1.4 2001/10/23 21:48:19 jongfoster
164 * Cleaning up error handling in CGI functions - they now send back
165 * a HTML error page and should never cause a FATAL error. (Fixes one
166 * potential source of "denial of service" attacks).
168 * CGI actions file editor that works and is actually useful.
170 * Ability to toggle JunkBuster remotely using a CGI call.
172 * You can turn off both the above features in the main configuration
173 * file, e.g. if you are running a multi-user proxy.
175 * Revision 1.3 2001/10/14 22:12:49 jongfoster
176 * New version of CGI-based actionsfile editor.
177 * Major changes, including:
178 * - Completely new file parser and file output routines
179 * - edit-actions CGI renamed edit-actions-for-url
180 * - All CGIs now need a filename parameter, except for...
181 * - New CGI edit-actions which doesn't need a filename,
182 * to allow you to start the editor up.
183 * - edit-actions-submit now works, and now automatically
184 * redirects you back to the main edit-actions-list handler.
186 * Revision 1.2 2001/09/16 17:05:14 jongfoster
187 * Removing unused #include showarg.h
189 * Revision 1.1 2001/09/16 15:47:37 jongfoster
190 * First version of CGI-based edit interface. This is very much a
191 * work-in-progress, and you can't actually use it to edit anything
192 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
193 * to have any effect.
196 **********************************************************************/
202 * FIXME: Following includes copied from cgi.c - which are actually needed?
207 #include <sys/types.h>
212 #include <sys/stat.h>
215 #define snprintf _snprintf
216 #endif /* def _WIN32 */
221 #include "cgisimple.h"
225 #include "miscutil.h"
229 /* loadcfg.h is for g_bToggleIJB only */
230 #include "urlmatch.h"
232 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
235 #ifdef FEATURE_CGI_EDIT_ACTIONS
239 struct file_line * next;
247 struct action_spec action[1];
256 /* Add more data types here... e.g.
259 struct url_spec url[1];
263 struct action_spec action[1];
272 #define FILE_LINE_UNPROCESSED 1
273 #define FILE_LINE_BLANK 2
274 #define FILE_LINE_ALIAS_HEADER 3
275 #define FILE_LINE_ALIAS_ENTRY 4
276 #define FILE_LINE_ACTION 5
277 #define FILE_LINE_URL 6
278 #define FILE_LINE_SETTINGS_HEADER 7
279 #define FILE_LINE_SETTINGS_ENTRY 8
280 #define FILE_LINE_DESCRIPTION_HEADER 9
281 #define FILE_LINE_DESCRIPTION_ENTRY 10
286 struct file_line * lines;
287 const char * filename; /* Full pathname - e.g. "/etc/privoxy/wibble.action" */
288 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
289 /* Pre-encoded with url_encode() for ease of use. */
290 const char * version_str; /* Last modification time, as a string. For CGI param */
291 /* Can be used in URL without using url_param(). */
292 unsigned version; /* Last modification time - prevents chaos with
293 * the browser's "back" button. Note that this is a
294 * time_t cast to an unsigned. When comparing, always
295 * cast the time_t to an unsigned, and *NOT* vice-versa.
296 * This may lose the top few bits, but they're not
297 * significant anyway.
299 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
300 * Note that changing this after the file has been
301 * read in will cause a mess.
303 struct file_line * parse_error; /* On parse error, this is the offending line. */
304 const char * parse_error_text; /* On parse error, this is the problem.
305 * (Statically allocated) */
308 #define CGI_ACTION_PARAM_LEN_MAX 500
310 /* FIXME: Following non-static functions should be prototyped in .h or made static */
312 /* Functions to read and write arbitrary config files */
313 jb_err edit_read_file(struct client_state *csp,
314 const struct map *parameters,
317 struct editable_file **pfile);
318 jb_err edit_write_file(struct editable_file * file);
319 void edit_free_file(struct editable_file * file);
321 /* Functions to read and write actions files */
322 jb_err edit_parse_actions_file(struct editable_file * file);
323 jb_err edit_read_actions_file(struct client_state *csp,
324 struct http_response *rsp,
325 const struct map *parameters,
327 struct editable_file **pfile);
330 jb_err cgi_error_modified(struct client_state *csp,
331 struct http_response *rsp,
332 const char *filename);
333 jb_err cgi_error_parse(struct client_state *csp,
334 struct http_response *rsp,
335 struct editable_file *file);
336 jb_err cgi_error_file(struct client_state *csp,
337 struct http_response *rsp,
338 const char *filename);
339 jb_err cgi_error_disabled(struct client_state *csp,
340 struct http_response *rsp);
342 /* Internal arbitrary config file support functions */
343 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
344 static void edit_free_file_lines(struct file_line * first_line);
346 /* Internal actions file support functions */
347 static int match_actions_file_header_line(const char * line, const char * name);
348 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
350 /* Internal parameter parsing functions */
351 static jb_err get_file_name_param(struct client_state *csp,
352 const struct map *parameters,
353 const char *param_name,
356 const char **pparam);
357 static jb_err get_number_param(struct client_state *csp,
358 const struct map *parameters,
361 static jb_err get_url_spec_param(struct client_state *csp,
362 const struct map *parameters,
365 static jb_err get_string_param(const struct map *parameters,
366 const char *param_name,
367 const char **pparam);
369 /* Internal actionsfile <==> HTML conversion functions */
370 static jb_err map_radio(struct map * exports,
371 const char * optionname,
374 static jb_err actions_to_radio(struct map * exports,
375 const struct action_spec *action);
376 static jb_err actions_from_radio(const struct map * parameters,
377 struct action_spec *action);
380 static jb_err map_copy_parameter_html(struct map *out,
381 const struct map *in,
383 #if 0 /* unused function */
384 static jb_err map_copy_parameter_url(struct map *out,
385 const struct map *in,
387 #endif /* unused function */
389 /*********************************************************************
391 * Function : map_copy_parameter_html
393 * Description : Copy a CGI parameter from one map to another, HTML
397 * 1 : out = target map
398 * 2 : in = source map
399 * 3 : name = name of cgi parameter to copy
401 * Returns : JB_ERR_OK on success
402 * JB_ERR_MEMORY on out-of-memory
403 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
406 *********************************************************************/
407 static jb_err map_copy_parameter_html(struct map *out,
408 const struct map *in,
418 value = lookup(in, name);
419 err = map(out, name, 1, html_encode(value), 0);
426 else if (*value == '\0')
428 return JB_ERR_CGI_PARAMS;
437 #if 0 /* unused function */
438 /*********************************************************************
440 * Function : map_copy_parameter_html
442 * Description : Copy a CGI parameter from one map to another, URL
446 * 1 : out = target map
447 * 2 : in = source map
448 * 3 : name = name of cgi parameter to copy
450 * Returns : JB_ERR_OK on success
451 * JB_ERR_MEMORY on out-of-memory
452 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
455 *********************************************************************/
456 static jb_err map_copy_parameter_url(struct map *out,
457 const struct map *in,
467 value = lookup(in, name);
468 err = map(out, name, 1, url_encode(value), 0);
475 else if (*value == '\0')
477 return JB_ERR_CGI_PARAMS;
484 #endif /* 0 - unused function */
486 /*********************************************************************
488 * Function : cgi_edit_actions_url_form
490 * Description : CGI function that displays a form for
494 * 1 : csp = Current client state (buffers, headers, etc...)
495 * 2 : rsp = http_response data structure for output
496 * 3 : parameters = map of cgi parameters
499 * f : (filename) Identifies the file to edit
500 * v : (version) File's last-modified time
501 * p : (pattern) Line number of pattern to edit
503 * Returns : JB_ERR_OK on success
504 * JB_ERR_MEMORY on out-of-memory
505 * JB_ERR_CGI_PARAMS if the CGI parameters are not
506 * specified or not valid.
508 *********************************************************************/
509 jb_err cgi_edit_actions_url_form(struct client_state *csp,
510 struct http_response *rsp,
511 const struct map *parameters)
513 struct map * exports;
515 struct editable_file * file;
516 struct file_line * cur_line;
517 unsigned line_number;
524 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
526 return cgi_error_disabled(csp, rsp);
529 err = get_number_param(csp, parameters, "p", &patternid);
535 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
538 /* No filename specified, can't read file, modified, or out of memory. */
539 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
542 cur_line = file->lines;
544 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
546 cur_line = cur_line->next;
549 if ( (cur_line == NULL)
550 || (line_number != patternid)
552 || (cur_line->type != FILE_LINE_URL))
554 /* Invalid "patternid" parameter */
555 edit_free_file(file);
556 return JB_ERR_CGI_PARAMS;
559 if (NULL == (exports = default_exports(csp, NULL)))
561 edit_free_file(file);
562 return JB_ERR_MEMORY;
565 err = map(exports, "f", 1, file->identifier, 1);
566 if (!err) err = map(exports, "v", 1, file->version_str, 1);
567 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
568 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
570 edit_free_file(file);
578 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
582 /*********************************************************************
584 * Function : cgi_edit_actions_add_url_form
586 * Description : CGI function that displays a form for
590 * 1 : csp = Current client state (buffers, headers, etc...)
591 * 2 : rsp = http_response data structure for output
592 * 3 : parameters = map of cgi parameters
595 * f : (filename) Identifies the file to edit
596 * v : (version) File's last-modified time
597 * s : (section) Line number of section to edit
599 * Returns : JB_ERR_OK on success
600 * JB_ERR_MEMORY on out-of-memory
601 * JB_ERR_CGI_PARAMS if the CGI parameters are not
602 * specified or not valid.
604 *********************************************************************/
605 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
606 struct http_response *rsp,
607 const struct map *parameters)
616 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
618 return cgi_error_disabled(csp, rsp);
621 if (NULL == (exports = default_exports(csp, NULL)))
623 return JB_ERR_MEMORY;
626 err = map_copy_parameter_html(exports, parameters, "f");
627 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
628 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
636 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
640 /*********************************************************************
642 * Function : cgi_edit_actions_remove_url_form
644 * Description : CGI function that displays a form for
648 * 1 : csp = Current client state (buffers, headers, etc...)
649 * 2 : rsp = http_response data structure for output
650 * 3 : parameters = map of cgi parameters
653 * f : (filename) Identifies the file to edit
654 * v : (version) File's last-modified time
655 * p : (pattern) Line number of pattern to edit
657 * Returns : JB_ERR_OK on success
658 * JB_ERR_MEMORY on out-of-memory
659 * JB_ERR_CGI_PARAMS if the CGI parameters are not
660 * specified or not valid.
662 *********************************************************************/
663 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
664 struct http_response *rsp,
665 const struct map *parameters)
667 struct map * exports;
669 struct editable_file * file;
670 struct file_line * cur_line;
671 unsigned line_number;
678 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
680 return cgi_error_disabled(csp, rsp);
683 err = get_number_param(csp, parameters, "p", &patternid);
689 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
692 /* No filename specified, can't read file, modified, or out of memory. */
693 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
696 cur_line = file->lines;
698 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
700 cur_line = cur_line->next;
703 if ( (cur_line == NULL)
704 || (line_number != patternid)
706 || (cur_line->type != FILE_LINE_URL))
708 /* Invalid "patternid" parameter */
709 edit_free_file(file);
710 return JB_ERR_CGI_PARAMS;
713 if (NULL == (exports = default_exports(csp, NULL)))
715 edit_free_file(file);
716 return JB_ERR_MEMORY;
719 err = map(exports, "f", 1, file->identifier, 1);
720 if (!err) err = map(exports, "v", 1, file->version_str, 1);
721 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
722 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
724 edit_free_file(file);
732 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
736 /*********************************************************************
738 * Function : edit_write_file
740 * Description : Write a complete file to disk.
743 * 1 : filename = File to write to.
744 * 2 : file = Data structure to write.
746 * Returns : JB_ERR_OK on success
747 * JB_ERR_FILE on error writing to file.
748 * JB_ERR_MEMORY on out of memory
750 *********************************************************************/
751 jb_err edit_write_file(struct editable_file * file)
754 struct file_line * cur_line;
755 struct stat statbuf[1];
756 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
757 digits in time_t, assuming this is a 64-bit
758 machine, plus null terminator, plus one
762 assert(file->filename);
764 #if defined(AMIGA) || defined(__OS2__)
765 if (NULL == (fp = fopen(file->filename, "w")))
767 if (NULL == (fp = fopen(file->filename, "wt")))
768 #endif /* def AMIGA */
773 cur_line = file->lines;
774 while (cur_line != NULL)
778 if (fputs(cur_line->raw, fp) < 0)
786 if (cur_line->prefix)
788 if (fputs(cur_line->prefix, fp) < 0)
794 if (cur_line->unprocessed)
796 /* This should be a single line - sanity check. */
797 assert(NULL == strchr(cur_line->unprocessed, '\r'));
798 assert(NULL == strchr(cur_line->unprocessed, '\n'));
800 if (NULL != strchr(cur_line->unprocessed, '#'))
802 /* Must quote '#' characters */
809 /* Count number of # characters, so we know length of output string */
810 src = cur_line->unprocessed;
811 while (NULL != (src = strchr(src, '#')))
818 /* Allocate new memory for string */
819 len = strlen(cur_line->unprocessed);
820 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
822 /* Uh oh, just trashed file! */
824 return JB_ERR_MEMORY;
827 /* Loop through string from end */
828 src = cur_line->unprocessed + len;
829 dest = str + len + numhash;
830 for ( ; len >= 0; len--)
832 if ((*dest-- = *src--) == '#')
836 assert(numhash >= 0);
839 assert(numhash == 0);
840 assert(src + 1 == cur_line->unprocessed);
841 assert(dest + 1 == str);
843 if (fputs(str, fp) < 0)
854 /* Can write without quoting '#' characters. */
855 if (fputs(cur_line->unprocessed, fp) < 0)
861 if (fputs(NEWLINE(file->newline), fp) < 0)
869 /* FIXME: Write data from file->data->whatever */
873 cur_line = cur_line->next;
879 /* Update the version stamp in the file structure, since we just
880 * wrote to the file & changed it's date.
882 if (stat(file->filename, statbuf) < 0)
884 /* Error, probably file not found. */
887 file->version = (unsigned)statbuf->st_mtime;
889 /* Correct file->version_str */
890 freez(file->version_str);
891 snprintf(version_buf, 22, "%u", file->version);
892 version_buf[21] = '\0';
893 file->version_str = strdup(version_buf);
894 if (version_buf == NULL)
896 return JB_ERR_MEMORY;
903 /*********************************************************************
905 * Function : edit_free_file
907 * Description : Free a complete file in memory.
910 * 1 : file = Data structure to free.
914 *********************************************************************/
915 void edit_free_file(struct editable_file * file)
919 /* Silently ignore NULL pointer */
923 edit_free_file_lines(file->lines);
924 freez(file->filename);
925 freez(file->identifier);
926 freez(file->version_str);
928 file->parse_error_text = NULL; /* Statically allocated */
929 file->parse_error = NULL;
935 /*********************************************************************
937 * Function : edit_free_file
939 * Description : Free an entire linked list of file lines.
942 * 1 : first_line = Data structure to free.
946 *********************************************************************/
947 static void edit_free_file_lines(struct file_line * first_line)
949 struct file_line * next_line;
951 while (first_line != NULL)
953 next_line = first_line->next;
954 first_line->next = NULL;
955 freez(first_line->raw);
956 freez(first_line->prefix);
957 freez(first_line->unprocessed);
958 switch(first_line->type)
960 case 0: /* special case if memory zeroed */
961 case FILE_LINE_UNPROCESSED:
962 case FILE_LINE_BLANK:
963 case FILE_LINE_ALIAS_HEADER:
964 case FILE_LINE_SETTINGS_HEADER:
965 case FILE_LINE_DESCRIPTION_HEADER:
966 case FILE_LINE_DESCRIPTION_ENTRY:
967 case FILE_LINE_ALIAS_ENTRY:
969 /* No data is stored for these */
972 case FILE_LINE_ACTION:
973 free_action(first_line->data.action);
976 case FILE_LINE_SETTINGS_ENTRY:
977 freez(first_line->data.setting.name);
978 freez(first_line->data.setting.svalue);
981 /* Should never happen */
985 first_line->type = 0; /* paranoia */
987 first_line = next_line;
992 /*********************************************************************
994 * Function : match_actions_file_header_line
996 * Description : Match an actions file {{header}} line
999 * 1 : line = String from file
1000 * 2 : name = Header to match against
1002 * Returns : 0 iff they match.
1004 *********************************************************************/
1005 static int match_actions_file_header_line(const char * line, const char * name)
1013 if ((line[0] != '{') || (line[1] != '{'))
1019 /* Look for optional whitespace */
1020 while ( (*line == ' ') || (*line == '\t') )
1025 /* Look for the specified name (case-insensitive) */
1027 if (0 != strncmpic(line, name, len))
1033 /* Look for optional whitespace */
1034 while ( (*line == ' ') || (*line == '\t') )
1039 /* Look for "}}" and end of string*/
1040 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1050 /*********************************************************************
1052 * Function : match_actions_file_header_line
1054 * Description : Match an actions file {{header}} line
1057 * 1 : line = String from file. Must not start with
1058 * whitespace (else infinite loop!)
1059 * 2 : name = Destination for name
1060 * 2 : name = Destination for value
1062 * Returns : JB_ERR_OK on success
1063 * JB_ERR_MEMORY on out-of-memory
1064 * JB_ERR_PARSE if there's no "=" sign, or if there's
1065 * nothing before the "=" sign (but empty
1066 * values *after* the "=" sign are legal).
1068 *********************************************************************/
1069 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1071 const char * name_end;
1072 const char * value_start;
1078 assert(*line != ' ');
1079 assert(*line != '\t');
1084 value_start = strchr(line, '=');
1085 if ((value_start == NULL) || (value_start == line))
1087 return JB_ERR_PARSE;
1090 name_end = value_start - 1;
1092 /* Eat any whitespace before the '=' */
1093 while ((*name_end == ' ') || (*name_end == '\t'))
1096 * we already know we must have at least 1 non-ws char
1097 * at start of buf - no need to check
1102 name_len = name_end - line + 1; /* Length excluding \0 */
1103 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1105 return JB_ERR_MEMORY;
1107 strncpy(*pname, line, name_len);
1108 (*pname)[name_len] = '\0';
1110 /* Eat any the whitespace after the '=' */
1112 while ((*value_start == ' ') || (*value_start == '\t'))
1117 if (NULL == (*pvalue = strdup(value_start)))
1121 return JB_ERR_MEMORY;
1128 /*********************************************************************
1130 * Function : edit_parse_actions_file
1132 * Description : Parse an actions file in memory.
1134 * Passed linked list must have the "data" member
1135 * zeroed, and must contain valid "next" and
1136 * "unprocessed" fields. The "raw" and "prefix"
1137 * fields are ignored, and "type" is just overwritten.
1139 * Note that on error the file may have been
1143 * 1 : file = Actions file to be parsed in-place.
1145 * Returns : JB_ERR_OK on success
1146 * JB_ERR_MEMORY on out-of-memory
1147 * JB_ERR_PARSE on error
1149 *********************************************************************/
1150 jb_err edit_parse_actions_file(struct editable_file * file)
1152 struct file_line * cur_line;
1154 const char * text; /* Text from a line */
1155 char * name; /* For lines of the form name=value */
1156 char * value; /* For lines of the form name=value */
1157 struct action_alias * alias_list = NULL;
1158 jb_err err = JB_ERR_OK;
1160 /* alias_list contains the aliases defined in this file.
1161 * It might be better to use the "file_line.data" fields
1162 * in the relavent places instead.
1165 cur_line = file->lines;
1167 /* A note about blank line support: Blank lines should only
1168 * ever occur as the last line in the file. This function
1169 * is more forgiving than that - FILE_LINE_BLANK can occur
1173 /* Skip leading blanks. Should only happen if file is
1174 * empty (which is valid, but pointless).
1176 while ( (cur_line != NULL)
1177 && (cur_line->unprocessed[0] == '\0') )
1180 cur_line->type = FILE_LINE_BLANK;
1181 cur_line = cur_line->next;
1184 if ( (cur_line != NULL)
1185 && (cur_line->unprocessed[0] != '{') )
1187 /* File doesn't start with a header */
1188 file->parse_error = cur_line;
1189 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1190 return JB_ERR_PARSE;
1193 if ( (cur_line != NULL) && (0 ==
1194 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1196 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1198 cur_line = cur_line->next;
1199 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1201 if (cur_line->unprocessed[0])
1203 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1205 err = split_line_on_equals(cur_line->unprocessed,
1206 &cur_line->data.setting.name,
1207 &cur_line->data.setting.svalue);
1208 if (err == JB_ERR_MEMORY)
1212 else if (err != JB_ERR_OK)
1214 /* Line does not contain a name=value pair */
1215 file->parse_error = cur_line;
1216 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1217 return JB_ERR_PARSE;
1222 cur_line->type = FILE_LINE_BLANK;
1224 cur_line = cur_line->next;
1228 if ( (cur_line != NULL) && (0 ==
1229 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1231 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1233 cur_line = cur_line->next;
1234 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1236 if (cur_line->unprocessed[0])
1238 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1242 cur_line->type = FILE_LINE_BLANK;
1244 cur_line = cur_line->next;
1248 if ( (cur_line != NULL) && (0 ==
1249 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1251 cur_line->type = FILE_LINE_ALIAS_HEADER;
1253 cur_line = cur_line->next;
1254 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1256 if (cur_line->unprocessed[0])
1258 /* define an alias */
1259 struct action_alias * new_alias;
1261 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1263 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1264 if (err == JB_ERR_MEMORY)
1268 else if (err != JB_ERR_OK)
1270 /* Line does not contain a name=value pair */
1271 file->parse_error = cur_line;
1272 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1273 return JB_ERR_PARSE;
1276 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1281 free_alias_list(alias_list);
1282 return JB_ERR_MEMORY;
1285 err = get_actions(value, alias_list, new_alias->action);
1288 /* Invalid action or out of memory */
1292 free_alias_list(alias_list);
1293 if (err == JB_ERR_MEMORY)
1299 /* Line does not contain a name=value pair */
1300 file->parse_error = cur_line;
1301 file->parse_error_text = "This alias does not specify a valid set of actions.";
1302 return JB_ERR_PARSE;
1308 new_alias->name = name;
1311 new_alias->next = alias_list;
1312 alias_list = new_alias;
1316 cur_line->type = FILE_LINE_BLANK;
1318 cur_line = cur_line->next;
1322 /* Header done, process the main part of the file */
1323 while (cur_line != NULL)
1325 /* At this point, (cur_line->unprocessed[0] == '{') */
1326 assert(cur_line->unprocessed[0] == '{');
1327 text = cur_line->unprocessed + 1;
1328 len = strlen(text) - 1;
1329 if (text[len] != '}')
1331 /* No closing } on header */
1332 free_alias_list(alias_list);
1333 file->parse_error = cur_line;
1334 file->parse_error_text = "Headers starting with '{' must have a "
1335 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1336 "must close with two brackets ('}}').";
1337 return JB_ERR_PARSE;
1342 /* An invalid {{ header. */
1343 free_alias_list(alias_list);
1344 file->parse_error = cur_line;
1345 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1346 "Please remember that the system (two-bracket) headers must "
1347 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1348 "and must appear before any actions (one-bracket) headers. "
1349 "Also note that system headers may not be repeated.";
1350 return JB_ERR_PARSE;
1353 while ( (*text == ' ') || (*text == '\t') )
1359 && ( (text[len - 1] == ' ')
1360 || (text[len - 1] == '\t') ) )
1365 cur_line->type = FILE_LINE_ACTION;
1367 /* Remove {} and make copy */
1368 if (NULL == (value = (char *) malloc(len + 1)))
1371 free_alias_list(alias_list);
1372 return JB_ERR_MEMORY;
1374 strncpy(value, text, len);
1378 err = get_actions(value, alias_list, cur_line->data.action);
1381 /* Invalid action or out of memory */
1383 free_alias_list(alias_list);
1384 if (err == JB_ERR_MEMORY)
1390 /* Line does not contain a name=value pair */
1391 file->parse_error = cur_line;
1392 file->parse_error_text = "This header does not specify a valid set of actions.";
1393 return JB_ERR_PARSE;
1397 /* Done with string - it was clobbered anyway */
1400 /* Process next line */
1401 cur_line = cur_line->next;
1403 /* Loop processing URL patterns */
1404 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1406 if (cur_line->unprocessed[0])
1408 /* Could parse URL here, but this isn't currently needed */
1410 cur_line->type = FILE_LINE_URL;
1414 cur_line->type = FILE_LINE_BLANK;
1416 cur_line = cur_line->next;
1418 } /* End main while(cur_line != NULL) loop */
1420 free_alias_list(alias_list);
1426 /*********************************************************************
1428 * Function : edit_read_file_lines
1430 * Description : Read all the lines of a file into memory.
1431 * Handles whitespace, comments and line continuation.
1434 * 1 : fp = File to read from. On return, this will be
1435 * at EOF but it will not have been closed.
1436 * 2 : pfile = Destination for a linked list of file_lines.
1437 * Will be set to NULL on error.
1439 * Returns : JB_ERR_OK on success
1440 * JB_ERR_MEMORY on out-of-memory
1442 *********************************************************************/
1443 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1445 struct file_line * first_line; /* Keep for return value or to free */
1446 struct file_line * cur_line; /* Current line */
1447 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1455 cur_line = first_line = zalloc(sizeof(struct file_line));
1456 if (cur_line == NULL)
1458 return JB_ERR_MEMORY;
1461 cur_line->type = FILE_LINE_UNPROCESSED;
1463 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1466 /* Out of memory or empty file. */
1467 /* Note that empty file is not an error we propogate up */
1469 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1474 prev_line = cur_line;
1475 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1476 if (cur_line == NULL)
1479 edit_free_file_lines(first_line);
1480 return JB_ERR_MEMORY;
1483 cur_line->type = FILE_LINE_UNPROCESSED;
1485 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1486 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1489 edit_free_file_lines(first_line);
1490 return JB_ERR_MEMORY;
1494 while (rval != JB_ERR_FILE);
1498 /* We allocated one too many - free it */
1499 prev_line->next = NULL;
1502 *pfile = first_line;
1507 /*********************************************************************
1509 * Function : edit_read_file
1511 * Description : Read a complete file into memory.
1512 * Handles CGI parameter parsing. If requested, also
1513 * checks the file's modification timestamp.
1516 * 1 : csp = Current client state (buffers, headers, etc...)
1517 * 2 : parameters = map of cgi parameters.
1518 * 3 : require_version = true to check "ver" parameter.
1519 * 4 : suffix = File extension, e.g. ".action".
1520 * 5 : pfile = Destination for the file. Will be set
1524 * filename : The name of the file to read, without the
1525 * path or ".action" extension.
1526 * ver : (Only if require_version is nonzero)
1527 * Timestamp of the actions file. If wrong, this
1528 * function fails with JB_ERR_MODIFIED.
1530 * Returns : JB_ERR_OK on success
1531 * JB_ERR_MEMORY on out-of-memory
1532 * JB_ERR_CGI_PARAMS if "filename" was not specified
1534 * JB_ERR_FILE if the file cannot be opened or
1536 * JB_ERR_MODIFIED if version checking was requested and
1537 * failed - the file was modified outside
1538 * of this CGI editor instance.
1540 *********************************************************************/
1541 jb_err edit_read_file(struct client_state *csp,
1542 const struct map *parameters,
1543 int require_version,
1545 struct editable_file **pfile)
1547 struct file_line * lines;
1551 const char * identifier;
1552 struct editable_file * file;
1553 unsigned version = 0;
1554 struct stat statbuf[1];
1555 char version_buf[22];
1556 int newline = NEWLINE_UNKNOWN;
1564 err = get_file_name_param(csp, parameters, "f", suffix,
1565 &filename, &identifier);
1571 if (stat(filename, statbuf) < 0)
1573 /* Error, probably file not found. */
1577 version = (unsigned) statbuf->st_mtime;
1579 if (require_version)
1581 unsigned specified_version;
1582 err = get_number_param(csp, parameters, "v", &specified_version);
1589 if (version != specified_version)
1591 return JB_ERR_MODIFIED;
1595 #if defined(AMIGA) || defined(__OS2__)
1596 if (NULL == (fp = fopen(filename,"r")))
1598 if (NULL == (fp = fopen(filename,"rt")))
1599 #endif /* def AMIGA */
1605 err = edit_read_file_lines(fp, &lines, &newline);
1615 file = (struct editable_file *) zalloc(sizeof(*file));
1619 edit_free_file_lines(lines);
1623 file->lines = lines;
1624 file->newline = newline;
1625 file->filename = filename;
1626 file->version = version;
1627 file->identifier = url_encode(identifier);
1629 if (file->identifier == NULL)
1631 edit_free_file(file);
1632 return JB_ERR_MEMORY;
1635 /* Correct file->version_str */
1636 freez(file->version_str);
1637 snprintf(version_buf, 22, "%u", file->version);
1638 version_buf[21] = '\0';
1639 file->version_str = strdup(version_buf);
1640 if (version_buf == NULL)
1642 edit_free_file(file);
1643 return JB_ERR_MEMORY;
1651 /*********************************************************************
1653 * Function : edit_read_actions_file
1655 * Description : Read a complete actions file into memory.
1656 * Handles CGI parameter parsing. If requested, also
1657 * checks the file's modification timestamp.
1659 * If this function detects an error in the categories
1660 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1661 * then it handles it by filling in the specified
1662 * response structure and returning JB_ERR_FILE.
1665 * 1 : csp = Current client state (buffers, headers, etc...)
1666 * 2 : rsp = HTTP response. Only filled in on error.
1667 * 2 : parameters = map of cgi parameters.
1668 * 3 : require_version = true to check "ver" parameter.
1669 * 4 : pfile = Destination for the file. Will be set
1673 * filename : The name of the actions file to read, without the
1674 * path or ".action" extension.
1675 * ver : (Only if require_version is nonzero)
1676 * Timestamp of the actions file. If wrong, this
1677 * function fails with JB_ERR_MODIFIED.
1679 * Returns : JB_ERR_OK on success
1680 * JB_ERR_MEMORY on out-of-memory
1681 * JB_ERR_CGI_PARAMS if "filename" was not specified
1683 * JB_ERR_FILE if the file does not contain valid data,
1684 * or if file cannot be opened or
1685 * contains no data, or if version
1686 * checking was requested and failed.
1688 *********************************************************************/
1689 jb_err edit_read_actions_file(struct client_state *csp,
1690 struct http_response *rsp,
1691 const struct map *parameters,
1692 int require_version,
1693 struct editable_file **pfile)
1696 struct editable_file *file;
1704 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1707 /* Try to handle if possible */
1708 if (err == JB_ERR_FILE)
1710 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1712 else if (err == JB_ERR_MODIFIED)
1714 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1716 if (err == JB_ERR_OK)
1719 * Signal to higher-level CGI code that there was a problem but we
1720 * handled it, they should just return JB_ERR_OK.
1727 err = edit_parse_actions_file(file);
1730 if (err == JB_ERR_PARSE)
1732 err = cgi_error_parse(csp, rsp, file);
1733 if (err == JB_ERR_OK)
1736 * Signal to higher-level CGI code that there was a problem but we
1737 * handled it, they should just return JB_ERR_OK.
1742 edit_free_file(file);
1751 /*********************************************************************
1753 * Function : get_file_name_param
1755 * Description : Get the name of the file to edit from the parameters
1756 * passed to a CGI function. This function handles
1757 * security checks such as blocking urls containing
1758 * "/" or ".", prepending the config file directory,
1759 * and adding the specified suffix.
1761 * (This is an essential security check, otherwise
1762 * users may be able to pass "../../../etc/passwd"
1763 * and overwrite the password file [linux], "prn:"
1764 * and print random data [Windows], etc...)
1766 * This function only allows filenames contining the
1767 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1768 * That's probably too restrictive but at least it's
1772 * 1 : csp = Current client state (buffers, headers, etc...)
1773 * 2 : parameters = map of cgi parameters
1774 * 3 : param_name = The name of the parameter to read
1775 * 4 : suffix = File extension, e.g. ".actions"
1776 * 5 : pfilename = destination for full filename. Caller
1777 * free()s. Set to NULL on error.
1778 * 6 : pparam = destination for partial filename,
1779 * suitable for use in another URL. Allocated as part
1780 * of the map "parameters", so don't free it.
1781 * Set to NULL if not specified.
1783 * Returns : JB_ERR_OK on success
1784 * JB_ERR_MEMORY on out-of-memory
1785 * JB_ERR_CGI_PARAMS if "filename" was not specified
1788 *********************************************************************/
1789 static jb_err get_file_name_param(struct client_state *csp,
1790 const struct map *parameters,
1791 const char *param_name,
1794 const char **pparam)
1798 #if 0 /* Patch to make 3.0.0 work properly. */
1800 #endif /* 0 - Patch to make 3.0.0 work properly. */
1814 param = lookup(parameters, param_name);
1817 return JB_ERR_CGI_PARAMS;
1822 len = strlen(param);
1823 if (len >= FILENAME_MAX)
1826 return JB_ERR_CGI_PARAMS;
1829 /* Check every character to see if it's legal */
1831 while ((ch = *s++) != '\0')
1833 if ( ((ch < 'A') || (ch > 'Z'))
1834 && ((ch < 'a') || (ch > 'z'))
1835 && ((ch < '0') || (ch > '9'))
1839 /* Probable hack attempt. */
1840 return JB_ERR_CGI_PARAMS;
1845 * FIXME Following is a hack to make 3.0.0 work properly.
1846 * Change "#if 0" --> "#if 1" below when we have modular action
1850 #if 0 /* Patch to make 3.0.0 work properly. */
1851 /* Append extension */
1852 name = malloc(len + strlen(suffix) + 1);
1855 return JB_ERR_MEMORY;
1857 strcpy(name, param);
1858 strcpy(name + len, suffix);
1861 fullpath = make_path(csp->config->confdir, name);
1863 #else /* 1 - Patch to make 3.0.0 work properly. */
1864 if ((csp->actions_list == NULL)
1865 || (csp->actions_list->filename == NULL))
1867 return JB_ERR_CGI_PARAMS;
1870 fullpath = ( (csp->actions_list && csp->actions_list->filename)
1871 ? strdup(csp->actions_list->filename) : NULL);
1872 #endif /* 1 - Patch to make 3.0.0 work properly. */
1873 if (fullpath == NULL)
1875 return JB_ERR_MEMORY;
1879 *pfilename = fullpath;
1885 /*********************************************************************
1887 * Function : get_char_param
1889 * Description : Get a single-character parameter passed to a CGI
1893 * 1 : parameters = map of cgi parameters
1894 * 2 : param_name = The name of the parameter to read
1896 * Returns : Uppercase character on success, '\0' on error.
1898 *********************************************************************/
1899 static char get_char_param(const struct map *parameters,
1900 const char *param_name)
1907 ch = *(lookup(parameters, param_name));
1908 if ((ch >= 'a') && (ch <= 'z'))
1910 ch = ch - 'a' + 'A';
1917 /*********************************************************************
1919 * Function : get_string_param
1921 * Description : Get a string paramater, to be used as an
1922 * ACTION_STRING or ACTION_MULTI paramater.
1923 * Validates the input to prevent stupid/malicious
1924 * users from corrupting their action file.
1927 * 1 : parameters = map of cgi parameters
1928 * 2 : param_name = The name of the parameter to read
1929 * 3 : pparam = destination for paramater. Allocated as
1930 * part of the map "parameters", so don't free it.
1931 * Set to NULL if not specified.
1933 * Returns : JB_ERR_OK on success, or if the paramater
1934 * was not specified.
1935 * JB_ERR_MEMORY on out-of-memory.
1936 * JB_ERR_CGI_PARAMS if the paramater is not valid.
1938 *********************************************************************/
1939 static jb_err get_string_param(const struct map *parameters,
1940 const char *param_name,
1941 const char **pparam)
1953 param = lookup(parameters, param_name);
1959 if (strlen(param) >= CGI_ACTION_PARAM_LEN_MAX)
1964 * Note that the length limit is arbitrary, it just seems
1965 * sensible to limit it to *something*. There's no
1966 * technical reason for any limit at all.
1968 return JB_ERR_CGI_PARAMS;
1971 /* Check every character to see if it's legal */
1973 while ((ch = *s++) != '\0')
1975 if ( ((unsigned char)ch < (unsigned char)' ')
1978 /* Probable hack attempt, or user accidentally used '}'. */
1979 return JB_ERR_CGI_PARAMS;
1990 /*********************************************************************
1992 * Function : get_number_param
1994 * Description : Get a non-negative integer from the parameters
1995 * passed to a CGI function.
1998 * 1 : csp = Current client state (buffers, headers, etc...)
1999 * 2 : parameters = map of cgi parameters
2000 * 3 : name = Name of CGI parameter to read
2001 * 4 : pvalue = destination for value.
2002 * Set to -1 on error.
2004 * Returns : JB_ERR_OK on success
2005 * JB_ERR_MEMORY on out-of-memory
2006 * JB_ERR_CGI_PARAMS if the parameter was not specified
2009 *********************************************************************/
2010 static jb_err get_number_param(struct client_state *csp,
2011 const struct map *parameters,
2026 param = lookup(parameters, name);
2029 return JB_ERR_CGI_PARAMS;
2032 /* We don't use atoi because I want to check this carefully... */
2035 while ((ch = *param++) != '\0')
2037 if ((ch < '0') || (ch > '9'))
2039 return JB_ERR_CGI_PARAMS;
2046 * <limits.h> defines UINT_MAX
2048 * (UINT_MAX - ch) / 10 is the largest number that
2049 * can be safely multiplied by 10 then have ch added.
2051 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
2053 return JB_ERR_CGI_PARAMS;
2056 value = value * 10 + ch;
2067 /*********************************************************************
2069 * Function : get_url_spec_param
2071 * Description : Get a URL pattern from the parameters
2072 * passed to a CGI function. Removes leading/trailing
2073 * spaces and validates it.
2076 * 1 : csp = Current client state (buffers, headers, etc...)
2077 * 2 : parameters = map of cgi parameters
2078 * 3 : name = Name of CGI parameter to read
2079 * 4 : pvalue = destination for value. Will be malloc()'d.
2080 * Set to NULL on error.
2082 * Returns : JB_ERR_OK on success
2083 * JB_ERR_MEMORY on out-of-memory
2084 * JB_ERR_CGI_PARAMS if the parameter was not specified
2087 *********************************************************************/
2088 static jb_err get_url_spec_param(struct client_state *csp,
2089 const struct map *parameters,
2093 const char *orig_param;
2096 struct url_spec compiled[1];
2106 orig_param = lookup(parameters, name);
2109 return JB_ERR_CGI_PARAMS;
2112 /* Copy and trim whitespace */
2113 param = strdup(orig_param);
2116 return JB_ERR_MEMORY;
2120 /* Must be non-empty, and can't allow 1st character to be '{' */
2121 if (param[0] == '\0' || param[0] == '{')
2124 return JB_ERR_CGI_PARAMS;
2127 /* Check for embedded newlines */
2128 for (s = param; *s != '\0'; s++)
2130 if ((*s == '\r') || (*s == '\n'))
2133 return JB_ERR_CGI_PARAMS;
2137 /* Check that regex is valid */
2142 return JB_ERR_MEMORY;
2144 err = create_url_spec(compiled, s);
2149 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2151 free_url_spec(compiled);
2153 if (param[strlen(param) - 1] == '\\')
2156 * Must protect trailing '\\' from becoming line continuation character.
2157 * Two methods: 1) If it's a domain only, add a trailing '/'.
2158 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2160 if (strchr(param, '/') == NULL)
2162 err = string_append(¶m, "/");
2166 err = string_append(¶m, "(?:)");
2173 /* Check that the modified regex is valid */
2178 return JB_ERR_MEMORY;
2180 err = create_url_spec(compiled, s);
2185 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2187 free_url_spec(compiled);
2194 /*********************************************************************
2196 * Function : map_radio
2198 * Description : Map a set of radio button values. E.g. if you have
2199 * 3 radio buttons, declare them as:
2200 * <option type="radio" name="xyz" @xyz-a@>
2201 * <option type="radio" name="xyz" @xyz-b@>
2202 * <option type="radio" name="xyz" @xyz-c@>
2203 * Then map one of the @xyz-?@ variables to "checked"
2204 * and all the others to empty by calling:
2205 * map_radio(exports, "xyz", "abc", sel)
2206 * Where 'sel' is 'a', 'b', or 'c'.
2209 * 1 : exports = Exports map to modify.
2210 * 2 : optionname = name for map
2211 * 3 : values = null-terminated list of values;
2212 * 4 : value = Selected value.
2214 * CGI Parameters : None
2216 * Returns : JB_ERR_OK on success
2217 * JB_ERR_MEMORY on out-of-memory
2219 *********************************************************************/
2220 static jb_err map_radio(struct map * exports,
2221 const char * optionname,
2222 const char * values,
2234 len = strlen(optionname);
2235 buf = malloc(len + 3);
2238 return JB_ERR_MEMORY;
2241 strcpy(buf, optionname);
2246 while ((c = *values++) != '\0')
2251 if (map(exports, buf, 1, "", 1))
2253 return JB_ERR_MEMORY;
2259 return map(exports, buf, 0, "checked", 1);
2263 /*********************************************************************
2265 * Function : cgi_error_modified
2267 * Description : CGI function that is called when a file is modified
2268 * outside the CGI editor.
2271 * 1 : csp = Current client state (buffers, headers, etc...)
2272 * 2 : rsp = http_response data structure for output
2273 * 3 : filename = The file that was modified.
2275 * CGI Parameters : none
2277 * Returns : JB_ERR_OK on success
2278 * JB_ERR_MEMORY on out-of-memory error.
2280 *********************************************************************/
2281 jb_err cgi_error_modified(struct client_state *csp,
2282 struct http_response *rsp,
2283 const char *filename)
2285 struct map *exports;
2292 if (NULL == (exports = default_exports(csp, NULL)))
2294 return JB_ERR_MEMORY;
2297 err = map(exports, "f", 1, html_encode(filename), 0);
2304 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2308 /*********************************************************************
2310 * Function : cgi_error_parse
2312 * Description : CGI function that is called when a file cannot
2313 * be parsed by the CGI editor.
2316 * 1 : csp = Current client state (buffers, headers, etc...)
2317 * 2 : rsp = http_response data structure for output
2318 * 3 : file = The file that was modified.
2320 * CGI Parameters : none
2322 * Returns : JB_ERR_OK on success
2323 * JB_ERR_MEMORY on out-of-memory error.
2325 *********************************************************************/
2326 jb_err cgi_error_parse(struct client_state *csp,
2327 struct http_response *rsp,
2328 struct editable_file *file)
2330 struct map *exports;
2332 struct file_line *cur_line;
2338 if (NULL == (exports = default_exports(csp, NULL)))
2340 return JB_ERR_MEMORY;
2343 err = map(exports, "f", 1, file->identifier, 1);
2344 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2346 cur_line = file->parse_error;
2349 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2350 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2358 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2362 /*********************************************************************
2364 * Function : cgi_error_file
2366 * Description : CGI function that is called when a file cannot be
2367 * opened by the CGI editor.
2370 * 1 : csp = Current client state (buffers, headers, etc...)
2371 * 2 : rsp = http_response data structure for output
2372 * 3 : filename = The file that was modified.
2374 * CGI Parameters : none
2376 * Returns : JB_ERR_OK on success
2377 * JB_ERR_MEMORY on out-of-memory error.
2379 *********************************************************************/
2380 jb_err cgi_error_file(struct client_state *csp,
2381 struct http_response *rsp,
2382 const char *filename)
2384 struct map *exports;
2391 if (NULL == (exports = default_exports(csp, NULL)))
2393 return JB_ERR_MEMORY;
2396 err = map(exports, "f", 1, html_encode(filename), 0);
2403 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2407 /*********************************************************************
2409 * Function : cgi_error_disabled
2411 * Description : CGI function that is called if the actions editor
2412 * is called although it's disabled in config
2415 * 1 : csp = Current client state (buffers, headers, etc...)
2416 * 2 : rsp = http_response data structure for output
2418 * CGI Parameters : none
2420 * Returns : JB_ERR_OK on success
2421 * JB_ERR_MEMORY on out-of-memory error.
2423 *********************************************************************/
2424 jb_err cgi_error_disabled(struct client_state *csp,
2425 struct http_response *rsp)
2427 struct map *exports;
2432 if (NULL == (exports = default_exports(csp, NULL)))
2434 return JB_ERR_MEMORY;
2437 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2441 /*********************************************************************
2443 * Function : cgi_edit_actions
2445 * Description : CGI function that allows the user to choose which
2446 * actions file to edit.
2449 * 1 : csp = Current client state (buffers, headers, etc...)
2450 * 2 : rsp = http_response data structure for output
2451 * 3 : parameters = map of cgi parameters
2453 * CGI Parameters : None
2455 * Returns : JB_ERR_OK on success
2456 * JB_ERR_MEMORY on out-of-memory error
2458 *********************************************************************/
2459 jb_err cgi_edit_actions(struct client_state *csp,
2460 struct http_response *rsp,
2461 const struct map *parameters)
2464 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2466 return cgi_error_disabled(csp, rsp);
2469 /* FIXME: Incomplete */
2470 rsp->status = strdup("302 Local Redirect from Privoxy");
2471 if (rsp->status == NULL)
2473 return JB_ERR_MEMORY;
2475 if (enlist_unique_header(rsp->headers, "Location",
2476 CGI_PREFIX "edit-actions-list?f=default"))
2480 return JB_ERR_MEMORY;
2487 /*********************************************************************
2489 * Function : cgi_edit_actions_list
2491 * Description : CGI function that edits the actions list.
2492 * FIXME: This function shouldn't FATAL ever.
2493 * FIXME: This function doesn't check the retval of map()
2495 * 1 : csp = Current client state (buffers, headers, etc...)
2496 * 2 : rsp = http_response data structure for output
2497 * 3 : parameters = map of cgi parameters
2499 * CGI Parameters : filename
2501 * Returns : JB_ERR_OK on success
2502 * JB_ERR_MEMORY on out-of-memory
2503 * JB_ERR_FILE if the file cannot be opened or
2505 * JB_ERR_CGI_PARAMS if "filename" was not specified
2508 *********************************************************************/
2509 jb_err cgi_edit_actions_list(struct client_state *csp,
2510 struct http_response *rsp,
2511 const struct map *parameters)
2513 char * section_template;
2514 char * url_template;
2519 struct map * exports;
2520 struct map * section_exports;
2521 struct map * url_exports;
2522 struct editable_file * file;
2523 struct file_line * cur_line;
2524 unsigned line_number = 0;
2525 unsigned prev_section_line_number = ((unsigned) (-1));
2529 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2531 return cgi_error_disabled(csp, rsp);
2534 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2537 /* No filename specified, can't read file, or out of memory. */
2538 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2541 if (NULL == (exports = default_exports(csp, NULL)))
2543 edit_free_file(file);
2544 return JB_ERR_MEMORY;
2547 err = map(exports, "f", 1, file->identifier, 1);
2548 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2552 edit_free_file(file);
2557 /* Should do all global exports above this point */
2559 err = template_load(csp, §ion_template, "edit-actions-list-section");
2562 edit_free_file(file);
2564 if (err == JB_ERR_FILE)
2566 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2571 err = template_load(csp, &url_template, "edit-actions-list-url");
2574 free(section_template);
2575 edit_free_file(file);
2577 if (err == JB_ERR_FILE)
2579 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2584 err = template_fill(§ion_template, exports);
2588 edit_free_file(file);
2594 err = template_fill(&url_template, exports);
2597 free(section_template);
2598 edit_free_file(file);
2603 /* Find start of actions in file */
2604 cur_line = file->lines;
2606 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2608 cur_line = cur_line->next;
2612 if (NULL == (sections = strdup("")))
2614 free(section_template);
2616 edit_free_file(file);
2618 return JB_ERR_MEMORY;
2621 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2623 if (NULL == (section_exports = new_map()))
2626 free(section_template);
2628 edit_free_file(file);
2630 return JB_ERR_MEMORY;
2633 snprintf(buf, 50, "%d", line_number);
2634 err = map(section_exports, "s", 1, buf, 1);
2635 if (!err) err = map(section_exports, "actions", 1,
2636 actions_to_html(cur_line->data.action), 0);
2639 && (cur_line->next != NULL)
2640 && (cur_line->next->type == FILE_LINE_URL))
2642 /* This section contains at least one URL, don't allow delete */
2643 err = map_block_killer(section_exports, "empty-section");
2647 if (!err) err = map_block_keep(section_exports, "empty-section");
2650 if (prev_section_line_number != ((unsigned)(-1)))
2652 /* Not last section */
2653 snprintf(buf, 50, "%d", prev_section_line_number);
2654 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2655 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2660 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2662 prev_section_line_number = line_number;
2667 free(section_template);
2669 edit_free_file(file);
2671 free_map(section_exports);
2675 /* Should do all section-specific exports above this point */
2677 if (NULL == (urls = strdup("")))
2680 free(section_template);
2682 edit_free_file(file);
2684 free_map(section_exports);
2685 return JB_ERR_MEMORY;
2690 cur_line = cur_line->next;
2693 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2695 if (NULL == (url_exports = new_map()))
2699 free(section_template);
2701 edit_free_file(file);
2703 free_map(section_exports);
2704 return JB_ERR_MEMORY;
2707 snprintf(buf, 50, "%d", line_number);
2708 err = map(url_exports, "p", 1, buf, 1);
2710 snprintf(buf, 50, "%d", url_1_2);
2711 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2713 if (!err) err = map(url_exports, "url-html", 1,
2714 html_encode(cur_line->unprocessed), 0);
2715 if (!err) err = map(url_exports, "url", 1,
2716 url_encode(cur_line->unprocessed), 0);
2722 free(section_template);
2724 edit_free_file(file);
2726 free_map(section_exports);
2727 free_map(url_exports);
2731 if (NULL == (s = strdup(url_template)))
2735 free(section_template);
2737 edit_free_file(file);
2739 free_map(section_exports);
2740 free_map(url_exports);
2741 return JB_ERR_MEMORY;
2744 err = template_fill(&s, section_exports);
2745 if (!err) err = template_fill(&s, url_exports);
2746 if (!err) err = string_append(&urls, s);
2748 free_map(url_exports);
2755 free(section_template);
2757 edit_free_file(file);
2759 free_map(section_exports);
2763 url_1_2 = 3 - url_1_2;
2765 cur_line = cur_line->next;
2769 err = map(section_exports, "urls", 1, urls, 0);
2771 /* Could also do section-specific exports here, but it wouldn't be as fast */
2773 if ( (cur_line != NULL)
2774 && (cur_line->type == FILE_LINE_ACTION))
2776 /* Not last section */
2777 snprintf(buf, 50, "%d", line_number);
2778 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2779 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2784 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2790 free(section_template);
2792 edit_free_file(file);
2794 free_map(section_exports);
2798 if (NULL == (s = strdup(section_template)))
2801 free(section_template);
2803 edit_free_file(file);
2805 free_map(section_exports);
2806 return JB_ERR_MEMORY;
2809 err = template_fill(&s, section_exports);
2810 if (!err) err = string_append(§ions, s);
2813 free_map(section_exports);
2818 free(section_template);
2820 edit_free_file(file);
2826 edit_free_file(file);
2827 free(section_template);
2830 err = map(exports, "sections", 1, sections, 0);
2837 /* Could also do global exports here, but it wouldn't be as fast */
2839 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2843 /*********************************************************************
2845 * Function : cgi_edit_actions
2847 * Description : CGI function that edits the Actions list.
2850 * 1 : csp = Current client state (buffers, headers, etc...)
2851 * 2 : rsp = http_response data structure for output
2852 * 3 : parameters = map of cgi parameters
2854 * CGI Parameters : None
2856 * Returns : JB_ERR_OK on success
2857 * JB_ERR_MEMORY on out-of-memory
2858 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2859 * specified or not valid.
2861 *********************************************************************/
2862 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2863 struct http_response *rsp,
2864 const struct map *parameters)
2866 struct map * exports;
2868 struct editable_file * file;
2869 struct file_line * cur_line;
2870 unsigned line_number;
2872 struct file_list *filter_file;
2873 struct re_filterfile_spec *filter_group;
2875 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2877 return cgi_error_disabled(csp, rsp);
2880 err = get_number_param(csp, parameters, "s", §ionid);
2886 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2889 /* No filename specified, can't read file, modified, or out of memory. */
2890 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2893 cur_line = file->lines;
2895 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2897 cur_line = cur_line->next;
2900 if ( (cur_line == NULL)
2901 || (line_number != sectionid)
2903 || (cur_line->type != FILE_LINE_ACTION))
2905 /* Invalid "sectionid" parameter */
2906 edit_free_file(file);
2907 return JB_ERR_CGI_PARAMS;
2910 if (NULL == (exports = default_exports(csp, NULL)))
2912 edit_free_file(file);
2913 return JB_ERR_MEMORY;
2916 err = map(exports, "f", 1, file->identifier, 1);
2917 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2918 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2920 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2922 filter_file = csp->rlist;
2923 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2925 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2929 edit_free_file(file);
2934 if (filter_group == NULL)
2936 err = map(exports, "filter-params", 1, "", 1);
2940 /* We have some entries in the filter list */
2943 char * filter_template;
2945 err = template_load(csp, &filter_template, "edit-actions-for-url-filter");
2948 edit_free_file(file);
2950 if (err == JB_ERR_FILE)
2952 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2957 result = strdup("");
2959 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2961 char current_mode = 'x';
2962 struct list_entry *filter_name;
2964 struct map *line_exports;
2967 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2968 while ((filter_name != NULL)
2969 && (0 != strcmp(filter_group->name, filter_name->str)))
2971 filter_name = filter_name->next;
2974 if (filter_name != NULL)
2980 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2981 while ((filter_name != NULL)
2982 && (0 != strcmp(filter_group->name, filter_name->str)))
2984 filter_name = filter_name->next;
2986 if (filter_name != NULL)
2992 /* Generate a unique serial number */
2993 snprintf(number, sizeof(number), "%x", index++);
2994 number[sizeof(number) - 1] = '\0';
2996 line_exports = new_map();
2997 if (line_exports == NULL)
2999 err = JB_ERR_MEMORY;
3004 if (!err) err = map(line_exports, "index", 1, number, 1);
3005 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3006 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3007 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3012 this_line = strdup(filter_template);
3013 if (this_line == NULL) err = JB_ERR_MEMORY;
3015 if (!err) err = template_fill(&this_line, line_exports);
3016 string_join(&result, this_line);
3018 free_map(line_exports);
3023 err = map(exports, "filter-params", 1, result, 0);
3031 if (!err) err = map_radio(exports, "filter-all", "nx",
3032 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3034 edit_free_file(file);
3042 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3046 /*********************************************************************
3048 * Function : cgi_edit_actions_submit
3050 * Description : CGI function that actually edits the Actions list.
3053 * 1 : csp = Current client state (buffers, headers, etc...)
3054 * 2 : rsp = http_response data structure for output
3055 * 3 : parameters = map of cgi parameters
3057 * CGI Parameters : None
3059 * Returns : JB_ERR_OK on success
3060 * JB_ERR_MEMORY on out-of-memory
3061 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3062 * specified or not valid.
3064 *********************************************************************/
3065 jb_err cgi_edit_actions_submit(struct client_state *csp,
3066 struct http_response *rsp,
3067 const struct map *parameters)
3073 struct editable_file * file;
3074 struct file_line * cur_line;
3075 unsigned line_number;
3081 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3083 return cgi_error_disabled(csp, rsp);
3086 err = get_number_param(csp, parameters, "s", §ionid);
3092 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3095 /* No filename specified, can't read file, modified, or out of memory. */
3096 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3099 cur_line = file->lines;
3101 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3103 cur_line = cur_line->next;
3106 if ( (cur_line == NULL)
3107 || (line_number != sectionid)
3109 || (cur_line->type != FILE_LINE_ACTION))
3111 /* Invalid "sectionid" parameter */
3112 edit_free_file(file);
3113 return JB_ERR_CGI_PARAMS;
3116 err = actions_from_radio(parameters, cur_line->data.action);
3120 edit_free_file(file);
3124 ch = get_char_param(parameters, "filter_all");
3127 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3128 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3129 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3133 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3136 for (index = 0; !err; index++)
3143 /* Generate the keys */
3144 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3145 key_value[sizeof(key_value) - 1] = '\0';
3146 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3147 key_name[sizeof(key_name) - 1] = '\0';
3149 err = get_string_param(parameters, key_name, &name);
3159 value = get_char_param(parameters, key_value);
3162 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3163 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3164 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3166 else if (value == 'N')
3168 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3169 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3171 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3172 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3175 else if (value == 'X')
3177 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3178 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3185 edit_free_file(file);
3189 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3192 edit_free_file(file);
3193 return JB_ERR_MEMORY;
3196 len = strlen(actiontext);
3200 * Empty action - must special-case this.
3201 * Simply setting len to 1 is sufficient...
3206 if (NULL == (newtext = malloc(len + 2)))
3210 edit_free_file(file);
3211 return JB_ERR_MEMORY;
3213 strcpy(newtext, actiontext);
3217 newtext[len + 1] = '\0';
3219 freez(cur_line->raw);
3220 freez(cur_line->unprocessed);
3221 cur_line->unprocessed = newtext;
3223 err = edit_write_file(file);
3226 /* Error writing file */
3227 edit_free_file(file);
3231 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3232 string_append(&target, file->identifier);
3234 edit_free_file(file);
3239 return JB_ERR_MEMORY;
3242 rsp->status = strdup("302 Local Redirect from Privoxy");
3243 if (rsp->status == NULL)
3246 return JB_ERR_MEMORY;
3248 err = enlist_unique_header(rsp->headers, "Location", target);
3255 /*********************************************************************
3257 * Function : cgi_edit_actions_url
3259 * Description : CGI function that actually edits a URL pattern in
3263 * 1 : csp = Current client state (buffers, headers, etc...)
3264 * 2 : rsp = http_response data structure for output
3265 * 3 : parameters = map of cgi parameters
3268 * filename : Identifies the file to edit
3269 * ver : File's last-modified time
3270 * section : Line number of section to edit
3271 * pattern : Line number of pattern to edit
3272 * newval : New value for pattern
3274 * Returns : JB_ERR_OK on success
3275 * JB_ERR_MEMORY on out-of-memory
3276 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3277 * specified or not valid.
3279 *********************************************************************/
3280 jb_err cgi_edit_actions_url(struct client_state *csp,
3281 struct http_response *rsp,
3282 const struct map *parameters)
3286 struct editable_file * file;
3287 struct file_line * cur_line;
3288 unsigned line_number;
3292 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3294 return cgi_error_disabled(csp, rsp);
3297 err = get_number_param(csp, parameters, "p", &patternid);
3304 return JB_ERR_CGI_PARAMS;
3307 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3313 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3316 /* No filename specified, can't read file, modified, or out of memory. */
3318 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3322 cur_line = file->lines;
3324 while ((cur_line != NULL) && (line_number < patternid))
3326 cur_line = cur_line->next;
3330 if ( (cur_line == NULL)
3331 || (cur_line->type != FILE_LINE_URL))
3333 /* Invalid "patternid" parameter */
3335 edit_free_file(file);
3336 return JB_ERR_CGI_PARAMS;
3339 /* At this point, the line to edit is in cur_line */
3341 freez(cur_line->raw);
3342 freez(cur_line->unprocessed);
3343 cur_line->unprocessed = new_pattern;
3345 err = edit_write_file(file);
3348 /* Error writing file */
3349 edit_free_file(file);
3353 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3354 string_append(&target, file->identifier);
3356 edit_free_file(file);
3361 return JB_ERR_MEMORY;
3364 rsp->status = strdup("302 Local Redirect from Privoxy");
3365 if (rsp->status == NULL)
3368 return JB_ERR_MEMORY;
3370 err = enlist_unique_header(rsp->headers, "Location", target);
3377 /*********************************************************************
3379 * Function : cgi_edit_actions_add_url
3381 * Description : CGI function that actually adds a URL pattern to
3385 * 1 : csp = Current client state (buffers, headers, etc...)
3386 * 2 : rsp = http_response data structure for output
3387 * 3 : parameters = map of cgi parameters
3390 * filename : Identifies the file to edit
3391 * ver : File's last-modified time
3392 * section : Line number of section to edit
3393 * newval : New pattern
3395 * Returns : JB_ERR_OK on success
3396 * JB_ERR_MEMORY on out-of-memory
3397 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3398 * specified or not valid.
3400 *********************************************************************/
3401 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3402 struct http_response *rsp,
3403 const struct map *parameters)
3407 struct file_line * new_line;
3408 struct editable_file * file;
3409 struct file_line * cur_line;
3410 unsigned line_number;
3414 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3416 return cgi_error_disabled(csp, rsp);
3419 err = get_number_param(csp, parameters, "s", §ionid);
3426 return JB_ERR_CGI_PARAMS;
3429 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3435 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3438 /* No filename specified, can't read file, modified, or out of memory. */
3440 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3444 cur_line = file->lines;
3446 while ((cur_line != NULL) && (line_number < sectionid))
3448 cur_line = cur_line->next;
3452 if ( (cur_line == NULL)
3453 || (cur_line->type != FILE_LINE_ACTION))
3455 /* Invalid "sectionid" parameter */
3457 edit_free_file(file);
3458 return JB_ERR_CGI_PARAMS;
3461 /* At this point, the section header is in cur_line - add after this. */
3463 /* Allocate the new line */
3464 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3465 if (new_line == NULL)
3468 edit_free_file(file);
3469 return JB_ERR_MEMORY;
3472 /* Fill in the data members of the new line */
3473 new_line->raw = NULL;
3474 new_line->prefix = NULL;
3475 new_line->unprocessed = new_pattern;
3476 new_line->type = FILE_LINE_URL;
3478 /* Link new_line into the list, after cur_line */
3479 new_line->next = cur_line->next;
3480 cur_line->next = new_line;
3482 /* Done making changes, now commit */
3484 err = edit_write_file(file);
3487 /* Error writing file */
3488 edit_free_file(file);
3492 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3493 string_append(&target, file->identifier);
3495 edit_free_file(file);
3500 return JB_ERR_MEMORY;
3503 rsp->status = strdup("302 Local Redirect from Privoxy");
3504 if (rsp->status == NULL)
3507 return JB_ERR_MEMORY;
3509 err = enlist_unique_header(rsp->headers, "Location", target);
3516 /*********************************************************************
3518 * Function : cgi_edit_actions_remove_url
3520 * Description : CGI function that actually removes a URL pattern from
3524 * 1 : csp = Current client state (buffers, headers, etc...)
3525 * 2 : rsp = http_response data structure for output
3526 * 3 : parameters = map of cgi parameters
3529 * f : (filename) Identifies the file to edit
3530 * v : (version) File's last-modified time
3531 * p : (pattern) Line number of pattern to remove
3533 * Returns : JB_ERR_OK on success
3534 * JB_ERR_MEMORY on out-of-memory
3535 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3536 * specified or not valid.
3538 *********************************************************************/
3539 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3540 struct http_response *rsp,
3541 const struct map *parameters)
3544 struct editable_file * file;
3545 struct file_line * cur_line;
3546 struct file_line * prev_line;
3547 unsigned line_number;
3551 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3553 return cgi_error_disabled(csp, rsp);
3556 err = get_number_param(csp, parameters, "p", &patternid);
3562 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3565 /* No filename specified, can't read file, modified, or out of memory. */
3566 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3571 cur_line = file->lines;
3573 while ((cur_line != NULL) && (line_number < patternid))
3575 prev_line = cur_line;
3576 cur_line = cur_line->next;
3580 if ( (cur_line == NULL)
3581 || (prev_line == NULL)
3582 || (cur_line->type != FILE_LINE_URL))
3584 /* Invalid "patternid" parameter */
3585 edit_free_file(file);
3586 return JB_ERR_CGI_PARAMS;
3589 /* At this point, the line to remove is in cur_line, and the previous
3590 * one is in prev_line
3593 /* Unlink cur_line */
3594 prev_line->next = cur_line->next;
3595 cur_line->next = NULL;
3598 edit_free_file_lines(cur_line);
3600 err = edit_write_file(file);
3603 /* Error writing file */
3604 edit_free_file(file);
3608 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3609 string_append(&target, file->identifier);
3611 edit_free_file(file);
3616 return JB_ERR_MEMORY;
3619 rsp->status = strdup("302 Local Redirect from Privoxy");
3620 if (rsp->status == NULL)
3623 return JB_ERR_MEMORY;
3625 err = enlist_unique_header(rsp->headers, "Location", target);
3632 /*********************************************************************
3634 * Function : cgi_edit_actions_section_remove
3636 * Description : CGI function that actually removes a whole section from
3637 * the actions file. The section must be empty first
3638 * (else JB_ERR_CGI_PARAMS).
3641 * 1 : csp = Current client state (buffers, headers, etc...)
3642 * 2 : rsp = http_response data structure for output
3643 * 3 : parameters = map of cgi parameters
3646 * f : (filename) Identifies the file to edit
3647 * v : (version) File's last-modified time
3648 * s : (section) Line number of section to edit
3650 * Returns : JB_ERR_OK on success
3651 * JB_ERR_MEMORY on out-of-memory
3652 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3653 * specified or not valid.
3655 *********************************************************************/
3656 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3657 struct http_response *rsp,
3658 const struct map *parameters)
3661 struct editable_file * file;
3662 struct file_line * cur_line;
3663 struct file_line * prev_line;
3664 unsigned line_number;
3668 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3670 return cgi_error_disabled(csp, rsp);
3673 err = get_number_param(csp, parameters, "s", §ionid);
3679 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3682 /* No filename specified, can't read file, modified, or out of memory. */
3683 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3687 cur_line = file->lines;
3690 while ((cur_line != NULL) && (line_number < sectionid))
3692 prev_line = cur_line;
3693 cur_line = cur_line->next;
3697 if ( (cur_line == NULL)
3698 || (cur_line->type != FILE_LINE_ACTION) )
3700 /* Invalid "sectionid" parameter */
3701 edit_free_file(file);
3702 return JB_ERR_CGI_PARAMS;
3705 if ( (cur_line->next != NULL)
3706 && (cur_line->next->type == FILE_LINE_URL) )
3708 /* Section not empty. */
3709 edit_free_file(file);
3710 return JB_ERR_CGI_PARAMS;
3713 /* At this point, the line to remove is in cur_line, and the previous
3714 * one is in prev_line
3717 /* Unlink cur_line */
3718 if (prev_line == NULL)
3720 /* Removing the first line from the file */
3721 file->lines = cur_line->next;
3725 prev_line->next = cur_line->next;
3727 cur_line->next = NULL;
3730 edit_free_file_lines(cur_line);
3732 err = edit_write_file(file);
3735 /* Error writing file */
3736 edit_free_file(file);
3740 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3741 string_append(&target, file->identifier);
3743 edit_free_file(file);
3748 return JB_ERR_MEMORY;
3751 rsp->status = strdup("302 Local Redirect from Privoxy");
3752 if (rsp->status == NULL)
3755 return JB_ERR_MEMORY;
3757 err = enlist_unique_header(rsp->headers, "Location", target);
3764 /*********************************************************************
3766 * Function : cgi_edit_actions_section_add
3768 * Description : CGI function that adds a new empty section to
3772 * 1 : csp = Current client state (buffers, headers, etc...)
3773 * 2 : rsp = http_response data structure for output
3774 * 3 : parameters = map of cgi parameters
3777 * f : (filename) Identifies the file to edit
3778 * v : (version) File's last-modified time
3779 * s : (section) Line number of section to add after, 0 for
3782 * Returns : JB_ERR_OK on success
3783 * JB_ERR_MEMORY on out-of-memory
3784 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3785 * specified or not valid.
3787 *********************************************************************/
3788 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3789 struct http_response *rsp,
3790 const struct map *parameters)
3793 struct file_line * new_line;
3795 struct editable_file * file;
3796 struct file_line * cur_line;
3797 unsigned line_number;
3801 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3803 return cgi_error_disabled(csp, rsp);
3806 err = get_number_param(csp, parameters, "s", §ionid);
3812 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3815 /* No filename specified, can't read file, modified, or out of memory. */
3816 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3820 cur_line = file->lines;
3824 /* Add to start of file */
3825 if (cur_line != NULL)
3827 /* There's something in the file, find the line before the first
3830 while ( (cur_line->next != NULL)
3831 && (cur_line->next->type != FILE_LINE_ACTION) )
3833 cur_line = cur_line->next;
3840 /* Add after stated section. */
3841 while ((cur_line != NULL) && (line_number < sectionid))
3843 cur_line = cur_line->next;
3847 if ( (cur_line == NULL)
3848 || (cur_line->type != FILE_LINE_ACTION))
3850 /* Invalid "sectionid" parameter */
3851 edit_free_file(file);
3852 return JB_ERR_CGI_PARAMS;
3855 /* Skip through the section to find the last line in it. */
3856 while ( (cur_line->next != NULL)
3857 && (cur_line->next->type != FILE_LINE_ACTION) )
3859 cur_line = cur_line->next;
3864 /* At this point, the last line in the previous section is in cur_line
3865 * - add after this. (Or if we need to add as the first line, cur_line
3869 new_text = strdup("{}");
3870 if (NULL == new_text)
3872 edit_free_file(file);
3873 return JB_ERR_MEMORY;
3876 /* Allocate the new line */
3877 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3878 if (new_line == NULL)
3881 edit_free_file(file);
3882 return JB_ERR_MEMORY;
3885 /* Fill in the data members of the new line */
3886 new_line->raw = NULL;
3887 new_line->prefix = NULL;
3888 new_line->unprocessed = new_text;
3889 new_line->type = FILE_LINE_ACTION;
3891 if (cur_line != NULL)
3893 /* Link new_line into the list, after cur_line */
3894 new_line->next = cur_line->next;
3895 cur_line->next = new_line;
3899 /* Link new_line into the list, as first line */
3900 new_line->next = file->lines;
3901 file->lines = new_line;
3904 /* Done making changes, now commit */
3906 err = edit_write_file(file);
3909 /* Error writing file */
3910 edit_free_file(file);
3914 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3915 string_append(&target, file->identifier);
3917 edit_free_file(file);
3922 return JB_ERR_MEMORY;
3925 rsp->status = strdup("302 Local Redirect from Privoxy");
3926 if (rsp->status == NULL)
3929 return JB_ERR_MEMORY;
3931 err = enlist_unique_header(rsp->headers, "Location", target);
3938 /*********************************************************************
3940 * Function : cgi_edit_actions_section_swap
3942 * Description : CGI function that swaps the order of two sections
3943 * in the actions file. Note that this CGI can actually
3944 * swap any two arbitrary sections, but the GUI interface
3945 * currently only allows consecutive sections to be
3949 * 1 : csp = Current client state (buffers, headers, etc...)
3950 * 2 : rsp = http_response data structure for output
3951 * 3 : parameters = map of cgi parameters
3954 * f : (filename) Identifies the file to edit
3955 * v : (version) File's last-modified time
3956 * s1 : (section1) Line number of first section to swap
3957 * s2 : (section2) Line number of second section to swap
3959 * Returns : JB_ERR_OK on success
3960 * JB_ERR_MEMORY on out-of-memory
3961 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3962 * specified or not valid.
3964 *********************************************************************/
3965 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3966 struct http_response *rsp,
3967 const struct map *parameters)
3971 struct editable_file * file;
3972 struct file_line * cur_line;
3973 struct file_line * prev_line;
3974 struct file_line * line_before_section1;
3975 struct file_line * line_start_section1;
3976 struct file_line * line_end_section1;
3977 struct file_line * line_after_section1;
3978 struct file_line * line_before_section2;
3979 struct file_line * line_start_section2;
3980 struct file_line * line_end_section2;
3981 struct file_line * line_after_section2;
3982 unsigned line_number;
3986 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3988 return cgi_error_disabled(csp, rsp);
3991 err = get_number_param(csp, parameters, "s1", §ion1);
3992 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3998 if (section1 > section2)
4000 unsigned temp = section2;
4001 section2 = section1;
4005 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4008 /* No filename specified, can't read file, modified, or out of memory. */
4009 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4012 /* Start at the beginning... */
4014 cur_line = file->lines;
4017 /* ... find section1 ... */
4018 while ((cur_line != NULL) && (line_number < section1))
4020 prev_line = cur_line;
4021 cur_line = cur_line->next;
4025 if ( (cur_line == NULL)
4026 || (cur_line->type != FILE_LINE_ACTION) )
4028 /* Invalid "section1" parameter */
4029 edit_free_file(file);
4030 return JB_ERR_CGI_PARAMS;
4033 /* If no-op, we've validated params and can skip the rest. */
4034 if (section1 != section2)
4036 /* ... find the end of section1 ... */
4037 line_before_section1 = prev_line;
4038 line_start_section1 = cur_line;
4041 prev_line = cur_line;
4042 cur_line = cur_line->next;
4045 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4046 line_end_section1 = prev_line;
4047 line_after_section1 = cur_line;
4049 /* ... find section2 ... */
4050 while ((cur_line != NULL) && (line_number < section2))
4052 prev_line = cur_line;
4053 cur_line = cur_line->next;
4057 if ( (cur_line == NULL)
4058 || (cur_line->type != FILE_LINE_ACTION) )
4060 /* Invalid "section2" parameter */
4061 edit_free_file(file);
4062 return JB_ERR_CGI_PARAMS;
4065 /* ... find the end of section2 ... */
4066 line_before_section2 = prev_line;
4067 line_start_section2 = cur_line;
4070 prev_line = cur_line;
4071 cur_line = cur_line->next;
4074 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4075 line_end_section2 = prev_line;
4076 line_after_section2 = cur_line;
4078 /* Now have all the pointers we need. Do the swap. */
4080 /* Change the pointer to section1 to point to section2 instead */
4081 if (line_before_section1 == NULL)
4083 file->lines = line_start_section2;
4087 line_before_section1->next = line_start_section2;
4090 if (line_before_section2 == line_end_section1)
4092 /* Consecutive sections */
4093 line_end_section2->next = line_start_section1;
4097 line_end_section2->next = line_after_section1;
4098 line_before_section2->next = line_start_section1;
4101 /* Set the pointer from the end of section1 to the rest of the file */
4102 line_end_section1->next = line_after_section2;
4104 err = edit_write_file(file);
4107 /* Error writing file */
4108 edit_free_file(file);
4111 } /* END if (section1 != section2) */
4113 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4114 string_append(&target, file->identifier);
4116 edit_free_file(file);
4121 return JB_ERR_MEMORY;
4124 rsp->status = strdup("302 Local Redirect from Privoxy");
4125 if (rsp->status == NULL)
4128 return JB_ERR_MEMORY;
4130 err = enlist_unique_header(rsp->headers, "Location", target);
4137 /*********************************************************************
4139 * Function : cgi_toggle
4141 * Description : CGI function that adds a new empty section to
4145 * 1 : csp = Current client state (buffers, headers, etc...)
4146 * 2 : rsp = http_response data structure for output
4147 * 3 : parameters = map of cgi parameters
4150 * set : If present, how to change toggle setting:
4151 * "enable", "disable", "toggle", or none (default).
4152 * mini : If present, use mini reply template.
4154 * Returns : JB_ERR_OK on success
4155 * JB_ERR_MEMORY on out-of-memory
4157 *********************************************************************/
4158 jb_err cgi_toggle(struct client_state *csp,
4159 struct http_response *rsp,
4160 const struct map *parameters)
4162 struct map *exports;
4164 const char *template_name;
4170 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4172 return cgi_error_disabled(csp, rsp);
4175 mode = get_char_param(parameters, "set");
4182 else if (mode == 'D')
4187 else if (mode == 'T')
4190 g_bToggleIJB = !g_bToggleIJB;
4193 if (NULL == (exports = default_exports(csp, "toggle")))
4195 return JB_ERR_MEMORY;
4198 template_name = (get_char_param(parameters, "mini")
4202 return template_fill_for_cgi(csp, template_name, exports, rsp);
4206 /*********************************************************************
4208 * Function : javascriptify
4210 * Description : Converts a string into a form JavaScript will like.
4212 * Netscape 4's JavaScript sucks - it doesn't use
4213 * "id" parameters, so you have to set the "name"
4214 * used to submit a form element to something JavaScript
4215 * will like. (Or access the elements by index in an
4216 * array. That array contains >60 elements and will
4217 * be changed whenever we add a new action to the
4218 * editor, so I'm NOT going to use indexes that have
4219 * to be figured out by hand.)
4221 * Currently the only thing we have to worry about
4222 * is "-" ==> "_" conversion.
4224 * This is a length-preserving operation so it is
4225 * carried out in-place, no memory is allocated
4229 * 1 : identifier = String to make JavaScript-friendly.
4233 *********************************************************************/
4234 static void javascriptify(char * identifier)
4236 char * p = identifier;
4237 while (NULL != (p = strchr(p, '-')))
4244 /*********************************************************************
4246 * Function : actions_to_radio
4248 * Description : Converts a actionsfile entry into settings for
4249 * radio buttons and edit boxes on a HTML form.
4252 * 1 : exports = List of substitutions to add to.
4253 * 2 : action = Action to read
4255 * Returns : JB_ERR_OK on success
4256 * JB_ERR_MEMORY on out-of-memory
4258 *********************************************************************/
4259 static jb_err actions_to_radio(struct map * exports,
4260 const struct action_spec *action)
4262 unsigned mask = action->mask;
4263 unsigned add = action->add;
4271 mask = action->mask;
4274 /* sanity - prevents "-feature +feature" */
4278 #define DEFINE_ACTION_BOOL(name, bit) \
4279 if (!(mask & bit)) \
4281 current_mode = 'n'; \
4283 else if (add & bit) \
4285 current_mode = 'y'; \
4289 current_mode = 'x'; \
4291 if (map_radio(exports, name, "ynx", current_mode)) \
4293 return JB_ERR_MEMORY; \
4296 #define DEFINE_ACTION_STRING(name, bit, index) \
4297 DEFINE_ACTION_BOOL(name, bit); \
4300 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4303 checked = !strcmp(action->string[index], value); \
4307 checked = is_default; \
4309 mapped_param |= checked; \
4310 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4312 return JB_ERR_MEMORY; \
4315 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4316 if (map(exports, name "-param-custom", 1, \
4317 ((!mapped_param) ? "checked" : ""), 1)) \
4319 return JB_ERR_MEMORY; \
4321 if (map(exports, name "-param", 1, \
4322 (((add & bit) && !mapped_param) ? \
4323 action->string[index] : default_val), 1)) \
4325 return JB_ERR_MEMORY; \
4328 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4329 if (map(exports, name "-param", 1, \
4330 ((add & bit) ? action->string[index] : default_val), 1)) \
4332 return JB_ERR_MEMORY; \
4335 #define DEFINE_ACTION_MULTI(name, index) \
4336 if (action->multi_add[index]->first) \
4338 current_mode = 'y'; \
4340 else if (action->multi_remove_all[index]) \
4342 current_mode = 'n'; \
4344 else if (action->multi_remove[index]->first) \
4346 current_mode = 'y'; \
4350 current_mode = 'x'; \
4352 if (map_radio(exports, name, "ynx", current_mode)) \
4354 return JB_ERR_MEMORY; \
4357 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4359 #include "actionlist.h"
4361 #undef DEFINE_ACTION_MULTI
4362 #undef DEFINE_ACTION_STRING
4363 #undef DEFINE_ACTION_BOOL
4364 #undef DEFINE_ACTION_ALIAS
4365 #undef DEFINE_CGI_PARAM_CUSTOM
4366 #undef DEFINE_CGI_PARAM_RADIO
4367 #undef DEFINE_CGI_PARAM_NO_RADIO
4373 /*********************************************************************
4375 * Function : actions_from_radio
4377 * Description : Converts a map of parameters passed to a CGI function
4378 * into an actionsfile entry.
4381 * 1 : parameters = parameters to the CGI call
4382 * 2 : action = Action to change. Must be valid before
4383 * the call, actions not specified will be
4386 * Returns : JB_ERR_OK on success
4387 * JB_ERR_MEMORY on out-of-memory
4389 *********************************************************************/
4390 static jb_err actions_from_radio(const struct map * parameters,
4391 struct action_spec *action)
4393 static int first_time = 1;
4397 const char * js_name;
4398 jb_err err = JB_ERR_OK;
4403 /* Statics are generally a potential race condition,
4404 * but in this case we're safe and don't need semaphores.
4405 * Be careful if you modify this function.
4409 #define JAVASCRIPTIFY(dest_var, string) \
4411 static char js_name_arr[] = string; \
4414 javascriptify(js_name_arr); \
4416 dest_var = js_name_arr; \
4419 #define DEFINE_ACTION_BOOL(name, bit) \
4420 JAVASCRIPTIFY(js_name, name); \
4421 ch = get_char_param(parameters, js_name); \
4424 action->add |= bit; \
4425 action->mask |= bit; \
4427 else if (ch == 'N') \
4429 action->add &= ~bit; \
4430 action->mask &= ~bit; \
4432 else if (ch == 'X') \
4434 action->add &= ~bit; \
4435 action->mask |= bit; \
4438 #define DEFINE_ACTION_STRING(name, bit, index) \
4439 JAVASCRIPTIFY(js_name, name); \
4440 ch = get_char_param(parameters, js_name); \
4444 JAVASCRIPTIFY(js_name, name "-mode"); \
4445 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4446 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4448 JAVASCRIPTIFY(js_name, name "-param"); \
4449 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4451 if (param != NULL) \
4453 if (NULL == (param_dup = strdup(param))) \
4455 return JB_ERR_MEMORY; \
4457 freez(action->string[index]); \
4458 action->add |= bit; \
4459 action->mask |= bit; \
4460 action->string[index] = param_dup; \
4463 else if (ch == 'N') \
4465 if (action->add & bit) \
4467 freez(action->string[index]); \
4469 action->add &= ~bit; \
4470 action->mask &= ~bit; \
4472 else if (ch == 'X') \
4474 if (action->add & bit) \
4476 freez(action->string[index]); \
4478 action->add &= ~bit; \
4479 action->mask |= bit; \
4482 #define DEFINE_ACTION_MULTI(name, index) \
4483 JAVASCRIPTIFY(js_name, name); \
4484 ch = get_char_param(parameters, js_name); \
4489 else if (ch == 'N') \
4491 list_remove_all(action->multi_add[index]); \
4492 list_remove_all(action->multi_remove[index]); \
4493 action->multi_remove_all[index] = 1; \
4495 else if (ch == 'X') \
4497 list_remove_all(action->multi_add[index]); \
4498 list_remove_all(action->multi_remove[index]); \
4499 action->multi_remove_all[index] = 0; \
4502 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4504 #include "actionlist.h"
4506 #undef DEFINE_ACTION_MULTI
4507 #undef DEFINE_ACTION_STRING
4508 #undef DEFINE_ACTION_BOOL
4509 #undef DEFINE_ACTION_ALIAS
4510 #undef JAVASCRIPTIFY
4518 #endif /* def FEATURE_CGI_EDIT_ACTIONS */