1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.32 2002/04/19 16:55:31 jongfoster 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.32 2002/04/19 16:55:31 jongfoster
46 * Fixing newline problems. If we do our own text file newline
47 * mangling, we don't want the library to do any, so we need to
48 * open the files in *binary* mode.
50 * Revision 1.31 2002/04/18 19:21:08 jongfoster
51 * Added code to detect "conventional" action files, that start
52 * with a set of actions for all URLs (the pattern "/").
53 * These are special-cased in the "edit-actions-list" CGI, so
54 * that a special UI can be written for them.
56 * Revision 1.30 2002/04/10 13:38:35 oes
57 * load_template signature changed
59 * Revision 1.29 2002/04/08 16:59:08 oes
62 * Revision 1.28 2002/03/27 12:30:29 oes
63 * Deleted unsused variable
65 * Revision 1.27 2002/03/26 23:06:04 jongfoster
66 * Removing duplicate @ifs on the toggle page
68 * Revision 1.26 2002/03/26 22:59:17 jongfoster
69 * Fixing /toggle to display status consistently.
71 * Revision 1.25 2002/03/26 22:29:54 swa
72 * we have a new homepage!
74 * Revision 1.24 2002/03/24 15:23:33 jongfoster
77 * Revision 1.23 2002/03/24 13:32:41 swa
78 * name change related issues
80 * Revision 1.22 2002/03/24 13:25:43 swa
81 * name change related issues
83 * Revision 1.21 2002/03/22 18:02:48 jongfoster
84 * Fixing remote toggle
86 * Revision 1.20 2002/03/16 20:28:34 oes
87 * Added descriptions to the filters so users will know what they select in the cgi editor
89 * Revision 1.19 2002/03/16 18:38:14 jongfoster
90 * Stopping stupid or malicious users from breaking the actions
91 * file using the web-based editor.
93 * Revision 1.18 2002/03/16 14:57:44 jongfoster
94 * Full support for enabling/disabling modular filters.
96 * Revision 1.17 2002/03/16 14:26:42 jongfoster
97 * First version of modular filters support - READ ONLY!
98 * Fixing a double-free bug in the out-of-memory handling in map_radio().
100 * Revision 1.16 2002/03/07 03:46:17 oes
101 * Fixed compiler warnings
103 * Revision 1.15 2002/03/06 22:54:35 jongfoster
104 * Automated function-comment nitpicking.
106 * Revision 1.14 2002/03/05 00:24:51 jongfoster
107 * Patch to always edit the current actions file.
109 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
110 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
112 * Revision 1.12 2002/03/03 09:18:03 joergs
113 * Made jumbjuster work on AmigaOS again.
115 * Revision 1.11 2002/01/23 01:03:31 jongfoster
116 * Fixing gcc [CygWin] compiler warnings
118 * Revision 1.10 2002/01/23 00:22:59 jongfoster
119 * Adding new function cgi_edit_actions_section_swap(), to reorder
122 * Adding get_url_spec_param() to get a validated URL pattern.
124 * Moving edit_read_line() out of this file and into loaders.c.
126 * Adding missing html_encode() to many CGI functions.
128 * Moving the functions that #include actionlist.h to the end of the file,
129 * because the Visual C++ 97 debugger gets extremely confused if you try
130 * to debug any code that comes after them in the file.
132 * Major optimizations in cgi_edit_actions_list() to reduce the size of
133 * the generated HTML (down 40% from 550k to 304k), with major side-effects
134 * throughout the editor and templates. In particular, the length of the
135 * URLs throughout the editor has been drastically reduced, by cutting
136 * paramater names down to 1 character and CGI names down to 3-4
137 * characters, by removing all non-essential CGI paramaters even at the
138 * expense of having to re-read the actions file for the most trivial
139 * page, and by using relative rather than absolute URLs. This means
140 * that this (typical example):
142 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
143 * filename=ijb&ver=1011487572&section=12&pattern=13
144 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
148 * <a href="eau?f=ijb&v=1011487572&p=13">
150 * Revision 1.9 2002/01/17 20:56:22 jongfoster
151 * Replacing hard references to the URL of the config interface
152 * with #defines from project.h
154 * Revision 1.8 2001/11/30 23:35:51 jongfoster
155 * Renaming actionsfile to ijb.action
157 * Revision 1.7 2001/11/13 00:28:24 jongfoster
158 * - Renaming parameters from edit-actions-for-url so that they only
159 * contain legal JavaScript characters. If we wanted to write
160 * JavaScript that worked with Netscape 4, this is nessacery.
161 * (Note that at the moment the JavaScript doesn't actually work
162 * with Netscape 4, but now this is purely a template issue, not
163 * one affecting code).
164 * - Adding new CGIs for use by non-JavaScript browsers:
165 * edit-actions-url-form
166 * edit-actions-add-url-form
167 * edit-actions-remove-url-form
170 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
171 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
172 * by and __OS2__ ifdef.
174 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
175 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
176 * threads to call select() simultaneously. So, it's time to do a real, live,
177 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
178 * (native). Both versions will work, but using __OS2__ offers multi-threading.
180 * Revision 1.4 2001/10/23 21:48:19 jongfoster
181 * Cleaning up error handling in CGI functions - they now send back
182 * a HTML error page and should never cause a FATAL error. (Fixes one
183 * potential source of "denial of service" attacks).
185 * CGI actions file editor that works and is actually useful.
187 * Ability to toggle JunkBuster remotely using a CGI call.
189 * You can turn off both the above features in the main configuration
190 * file, e.g. if you are running a multi-user proxy.
192 * Revision 1.3 2001/10/14 22:12:49 jongfoster
193 * New version of CGI-based actionsfile editor.
194 * Major changes, including:
195 * - Completely new file parser and file output routines
196 * - edit-actions CGI renamed edit-actions-for-url
197 * - All CGIs now need a filename parameter, except for...
198 * - New CGI edit-actions which doesn't need a filename,
199 * to allow you to start the editor up.
200 * - edit-actions-submit now works, and now automatically
201 * redirects you back to the main edit-actions-list handler.
203 * Revision 1.2 2001/09/16 17:05:14 jongfoster
204 * Removing unused #include showarg.h
206 * Revision 1.1 2001/09/16 15:47:37 jongfoster
207 * First version of CGI-based edit interface. This is very much a
208 * work-in-progress, and you can't actually use it to edit anything
209 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
210 * to have any effect.
213 **********************************************************************/
219 * FIXME: Following includes copied from cgi.c - which are actually needed?
224 #include <sys/types.h>
228 #include <sys/stat.h>
231 #define snprintf _snprintf
232 #endif /* def _WIN32 */
237 #include "cgisimple.h"
241 #include "miscutil.h"
245 /* loadcfg.h is for g_bToggleIJB only */
246 #include "urlmatch.h"
248 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
251 #ifdef FEATURE_CGI_EDIT_ACTIONS
255 struct file_line * next;
263 struct action_spec action[1];
272 /* Add more data types here... e.g.
275 struct url_spec url[1];
279 struct action_spec action[1];
288 #define FILE_LINE_UNPROCESSED 1
289 #define FILE_LINE_BLANK 2
290 #define FILE_LINE_ALIAS_HEADER 3
291 #define FILE_LINE_ALIAS_ENTRY 4
292 #define FILE_LINE_ACTION 5
293 #define FILE_LINE_URL 6
294 #define FILE_LINE_SETTINGS_HEADER 7
295 #define FILE_LINE_SETTINGS_ENTRY 8
296 #define FILE_LINE_DESCRIPTION_HEADER 9
297 #define FILE_LINE_DESCRIPTION_ENTRY 10
302 struct file_line * lines;
303 const char * filename; /* Full pathname - e.g. "/etc/privoxy/wibble.action" */
304 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
305 /* Pre-encoded with url_encode() for ease of use. */
306 const char * version_str; /* Last modification time, as a string. For CGI param */
307 /* Can be used in URL without using url_param(). */
308 unsigned version; /* Last modification time - prevents chaos with
309 * the browser's "back" button. Note that this is a
310 * time_t cast to an unsigned. When comparing, always
311 * cast the time_t to an unsigned, and *NOT* vice-versa.
312 * This may lose the top few bits, but they're not
313 * significant anyway.
315 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
316 * Note that changing this after the file has been
317 * read in will cause a mess.
319 struct file_line * parse_error; /* On parse error, this is the offending line. */
320 const char * parse_error_text; /* On parse error, this is the problem.
321 * (Statically allocated) */
324 /* FIXME: Following non-static functions should be prototyped in .h or made static */
326 /* Functions to read and write arbitrary config files */
327 jb_err edit_read_file(struct client_state *csp,
328 const struct map *parameters,
331 struct editable_file **pfile);
332 jb_err edit_write_file(struct editable_file * file);
333 void edit_free_file(struct editable_file * file);
335 /* Functions to read and write actions files */
336 jb_err edit_parse_actions_file(struct editable_file * file);
337 jb_err edit_read_actions_file(struct client_state *csp,
338 struct http_response *rsp,
339 const struct map *parameters,
341 struct editable_file **pfile);
344 jb_err cgi_error_modified(struct client_state *csp,
345 struct http_response *rsp,
346 const char *filename);
347 jb_err cgi_error_parse(struct client_state *csp,
348 struct http_response *rsp,
349 struct editable_file *file);
350 jb_err cgi_error_file(struct client_state *csp,
351 struct http_response *rsp,
352 const char *filename);
353 jb_err cgi_error_disabled(struct client_state *csp,
354 struct http_response *rsp);
356 /* Internal arbitrary config file support functions */
357 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
358 static void edit_free_file_lines(struct file_line * first_line);
360 /* Internal actions file support functions */
361 static int match_actions_file_header_line(const char * line, const char * name);
362 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
364 /* Internal parameter parsing functions */
365 static jb_err get_file_name_param(struct client_state *csp,
366 const struct map *parameters,
367 const char *param_name,
370 const char **pparam);
372 static jb_err get_url_spec_param(struct client_state *csp,
373 const struct map *parameters,
378 /* Internal actionsfile <==> HTML conversion functions */
379 static jb_err map_radio(struct map * exports,
380 const char * optionname,
383 static jb_err actions_to_radio(struct map * exports,
384 const struct action_spec *action);
385 static jb_err actions_from_radio(const struct map * parameters,
386 struct action_spec *action);
389 static jb_err map_copy_parameter_html(struct map *out,
390 const struct map *in,
392 #if 0 /* unused function */
393 static jb_err map_copy_parameter_url(struct map *out,
394 const struct map *in,
396 #endif /* unused function */
398 /*********************************************************************
400 * Function : map_copy_parameter_html
402 * Description : Copy a CGI parameter from one map to another, HTML
406 * 1 : out = target map
407 * 2 : in = source map
408 * 3 : name = name of cgi parameter to copy
410 * Returns : JB_ERR_OK on success
411 * JB_ERR_MEMORY on out-of-memory
412 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
415 *********************************************************************/
416 static jb_err map_copy_parameter_html(struct map *out,
417 const struct map *in,
427 value = lookup(in, name);
428 err = map(out, name, 1, html_encode(value), 0);
435 else if (*value == '\0')
437 return JB_ERR_CGI_PARAMS;
446 #if 0 /* unused function */
447 /*********************************************************************
449 * Function : map_copy_parameter_url
451 * Description : Copy a CGI parameter from one map to another, URL
455 * 1 : out = target map
456 * 2 : in = source map
457 * 3 : name = name of cgi parameter to copy
459 * Returns : JB_ERR_OK on success
460 * JB_ERR_MEMORY on out-of-memory
461 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
464 *********************************************************************/
465 static jb_err map_copy_parameter_url(struct map *out,
466 const struct map *in,
476 value = lookup(in, name);
477 err = map(out, name, 1, url_encode(value), 0);
484 else if (*value == '\0')
486 return JB_ERR_CGI_PARAMS;
493 #endif /* 0 - unused function */
495 /*********************************************************************
497 * Function : cgi_edit_actions_url_form
499 * Description : CGI function that displays a form for
503 * 1 : csp = Current client state (buffers, headers, etc...)
504 * 2 : rsp = http_response data structure for output
505 * 3 : parameters = map of cgi parameters
508 * f : (filename) Identifies the file to edit
509 * v : (version) File's last-modified time
510 * p : (pattern) Line number of pattern to edit
512 * Returns : JB_ERR_OK on success
513 * JB_ERR_MEMORY on out-of-memory
514 * JB_ERR_CGI_PARAMS if the CGI parameters are not
515 * specified or not valid.
517 *********************************************************************/
518 jb_err cgi_edit_actions_url_form(struct client_state *csp,
519 struct http_response *rsp,
520 const struct map *parameters)
522 struct map * exports;
524 struct editable_file * file;
525 struct file_line * cur_line;
526 unsigned line_number;
533 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
535 return cgi_error_disabled(csp, rsp);
538 err = get_number_param(csp, parameters, "p", &patternid);
544 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
547 /* No filename specified, can't read file, modified, or out of memory. */
548 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
551 cur_line = file->lines;
553 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
555 cur_line = cur_line->next;
558 if ( (cur_line == NULL)
559 || (line_number != patternid)
561 || (cur_line->type != FILE_LINE_URL))
563 /* Invalid "patternid" parameter */
564 edit_free_file(file);
565 return JB_ERR_CGI_PARAMS;
568 if (NULL == (exports = default_exports(csp, NULL)))
570 edit_free_file(file);
571 return JB_ERR_MEMORY;
574 err = map(exports, "f", 1, file->identifier, 1);
575 if (!err) err = map(exports, "v", 1, file->version_str, 1);
576 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
577 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
579 edit_free_file(file);
587 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
591 /*********************************************************************
593 * Function : cgi_edit_actions_add_url_form
595 * Description : CGI function that displays a form for
599 * 1 : csp = Current client state (buffers, headers, etc...)
600 * 2 : rsp = http_response data structure for output
601 * 3 : parameters = map of cgi parameters
604 * f : (filename) Identifies the file to edit
605 * v : (version) File's last-modified time
606 * s : (section) Line number of section to edit
608 * Returns : JB_ERR_OK on success
609 * JB_ERR_MEMORY on out-of-memory
610 * JB_ERR_CGI_PARAMS if the CGI parameters are not
611 * specified or not valid.
613 *********************************************************************/
614 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
615 struct http_response *rsp,
616 const struct map *parameters)
625 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
627 return cgi_error_disabled(csp, rsp);
630 if (NULL == (exports = default_exports(csp, NULL)))
632 return JB_ERR_MEMORY;
635 err = map_copy_parameter_html(exports, parameters, "f");
636 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
637 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
645 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
649 /*********************************************************************
651 * Function : cgi_edit_actions_remove_url_form
653 * Description : CGI function that displays a form for
657 * 1 : csp = Current client state (buffers, headers, etc...)
658 * 2 : rsp = http_response data structure for output
659 * 3 : parameters = map of cgi parameters
662 * f : (filename) Identifies the file to edit
663 * v : (version) File's last-modified time
664 * p : (pattern) Line number of pattern to edit
666 * Returns : JB_ERR_OK on success
667 * JB_ERR_MEMORY on out-of-memory
668 * JB_ERR_CGI_PARAMS if the CGI parameters are not
669 * specified or not valid.
671 *********************************************************************/
672 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
673 struct http_response *rsp,
674 const struct map *parameters)
676 struct map * exports;
678 struct editable_file * file;
679 struct file_line * cur_line;
680 unsigned line_number;
687 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
689 return cgi_error_disabled(csp, rsp);
692 err = get_number_param(csp, parameters, "p", &patternid);
698 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
701 /* No filename specified, can't read file, modified, or out of memory. */
702 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
705 cur_line = file->lines;
707 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
709 cur_line = cur_line->next;
712 if ( (cur_line == NULL)
713 || (line_number != patternid)
715 || (cur_line->type != FILE_LINE_URL))
717 /* Invalid "patternid" parameter */
718 edit_free_file(file);
719 return JB_ERR_CGI_PARAMS;
722 if (NULL == (exports = default_exports(csp, NULL)))
724 edit_free_file(file);
725 return JB_ERR_MEMORY;
728 err = map(exports, "f", 1, file->identifier, 1);
729 if (!err) err = map(exports, "v", 1, file->version_str, 1);
730 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
731 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
733 edit_free_file(file);
741 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
745 /*********************************************************************
747 * Function : edit_write_file
749 * Description : Write a complete file to disk.
752 * 1 : filename = File to write to.
753 * 2 : file = Data structure to write.
755 * Returns : JB_ERR_OK on success
756 * JB_ERR_FILE on error writing to file.
757 * JB_ERR_MEMORY on out of memory
759 *********************************************************************/
760 jb_err edit_write_file(struct editable_file * file)
763 struct file_line * cur_line;
764 struct stat statbuf[1];
765 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
766 digits in time_t, assuming this is a 64-bit
767 machine, plus null terminator, plus one
771 assert(file->filename);
773 if (NULL == (fp = fopen(file->filename, "wb")))
778 cur_line = file->lines;
779 while (cur_line != NULL)
783 if (fputs(cur_line->raw, fp) < 0)
791 if (cur_line->prefix)
793 if (fputs(cur_line->prefix, fp) < 0)
799 if (cur_line->unprocessed)
801 /* This should be a single line - sanity check. */
802 assert(NULL == strchr(cur_line->unprocessed, '\r'));
803 assert(NULL == strchr(cur_line->unprocessed, '\n'));
805 if (NULL != strchr(cur_line->unprocessed, '#'))
807 /* Must quote '#' characters */
814 /* Count number of # characters, so we know length of output string */
815 src = cur_line->unprocessed;
816 while (NULL != (src = strchr(src, '#')))
823 /* Allocate new memory for string */
824 len = strlen(cur_line->unprocessed);
825 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
827 /* Uh oh, just trashed file! */
829 return JB_ERR_MEMORY;
832 /* Loop through string from end */
833 src = cur_line->unprocessed + len;
834 dest = str + len + numhash;
835 for ( ; len >= 0; len--)
837 if ((*dest-- = *src--) == '#')
841 assert(numhash >= 0);
844 assert(numhash == 0);
845 assert(src + 1 == cur_line->unprocessed);
846 assert(dest + 1 == str);
848 if (fputs(str, fp) < 0)
859 /* Can write without quoting '#' characters. */
860 if (fputs(cur_line->unprocessed, fp) < 0)
866 if (fputs(NEWLINE(file->newline), fp) < 0)
874 /* FIXME: Write data from file->data->whatever */
878 cur_line = cur_line->next;
884 /* Update the version stamp in the file structure, since we just
885 * wrote to the file & changed it's date.
887 if (stat(file->filename, statbuf) < 0)
889 /* Error, probably file not found. */
892 file->version = (unsigned)statbuf->st_mtime;
894 /* Correct file->version_str */
895 freez(file->version_str);
896 snprintf(version_buf, 22, "%u", file->version);
897 version_buf[21] = '\0';
898 file->version_str = strdup(version_buf);
899 if (version_buf == NULL)
901 return JB_ERR_MEMORY;
908 /*********************************************************************
910 * Function : edit_free_file
912 * Description : Free a complete file in memory.
915 * 1 : file = Data structure to free.
919 *********************************************************************/
920 void edit_free_file(struct editable_file * file)
924 /* Silently ignore NULL pointer */
928 edit_free_file_lines(file->lines);
929 freez(file->filename);
930 freez(file->identifier);
931 freez(file->version_str);
933 file->parse_error_text = NULL; /* Statically allocated */
934 file->parse_error = NULL;
940 /*********************************************************************
942 * Function : edit_free_file_lines
944 * Description : Free an entire linked list of file lines.
947 * 1 : first_line = Data structure to free.
951 *********************************************************************/
952 static void edit_free_file_lines(struct file_line * first_line)
954 struct file_line * next_line;
956 while (first_line != NULL)
958 next_line = first_line->next;
959 first_line->next = NULL;
960 freez(first_line->raw);
961 freez(first_line->prefix);
962 freez(first_line->unprocessed);
963 switch(first_line->type)
965 case 0: /* special case if memory zeroed */
966 case FILE_LINE_UNPROCESSED:
967 case FILE_LINE_BLANK:
968 case FILE_LINE_ALIAS_HEADER:
969 case FILE_LINE_SETTINGS_HEADER:
970 case FILE_LINE_DESCRIPTION_HEADER:
971 case FILE_LINE_DESCRIPTION_ENTRY:
972 case FILE_LINE_ALIAS_ENTRY:
974 /* No data is stored for these */
977 case FILE_LINE_ACTION:
978 free_action(first_line->data.action);
981 case FILE_LINE_SETTINGS_ENTRY:
982 freez(first_line->data.setting.name);
983 freez(first_line->data.setting.svalue);
986 /* Should never happen */
990 first_line->type = 0; /* paranoia */
992 first_line = next_line;
997 /*********************************************************************
999 * Function : match_actions_file_header_line
1001 * Description : Match an actions file {{header}} line
1004 * 1 : line = String from file
1005 * 2 : name = Header to match against
1007 * Returns : 0 iff they match.
1009 *********************************************************************/
1010 static int match_actions_file_header_line(const char * line, const char * name)
1018 if ((line[0] != '{') || (line[1] != '{'))
1024 /* Look for optional whitespace */
1025 while ( (*line == ' ') || (*line == '\t') )
1030 /* Look for the specified name (case-insensitive) */
1032 if (0 != strncmpic(line, name, len))
1038 /* Look for optional whitespace */
1039 while ( (*line == ' ') || (*line == '\t') )
1044 /* Look for "}}" and end of string*/
1045 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1055 /*********************************************************************
1057 * Function : match_actions_file_header_line
1059 * Description : Match an actions file {{header}} line
1062 * 1 : line = String from file. Must not start with
1063 * whitespace (else infinite loop!)
1064 * 2 : name = Destination for name
1065 * 2 : name = Destination for value
1067 * Returns : JB_ERR_OK on success
1068 * JB_ERR_MEMORY on out-of-memory
1069 * JB_ERR_PARSE if there's no "=" sign, or if there's
1070 * nothing before the "=" sign (but empty
1071 * values *after* the "=" sign are legal).
1073 *********************************************************************/
1074 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1076 const char * name_end;
1077 const char * value_start;
1083 assert(*line != ' ');
1084 assert(*line != '\t');
1089 value_start = strchr(line, '=');
1090 if ((value_start == NULL) || (value_start == line))
1092 return JB_ERR_PARSE;
1095 name_end = value_start - 1;
1097 /* Eat any whitespace before the '=' */
1098 while ((*name_end == ' ') || (*name_end == '\t'))
1101 * we already know we must have at least 1 non-ws char
1102 * at start of buf - no need to check
1107 name_len = name_end - line + 1; /* Length excluding \0 */
1108 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1110 return JB_ERR_MEMORY;
1112 strncpy(*pname, line, name_len);
1113 (*pname)[name_len] = '\0';
1115 /* Eat any the whitespace after the '=' */
1117 while ((*value_start == ' ') || (*value_start == '\t'))
1122 if (NULL == (*pvalue = strdup(value_start)))
1126 return JB_ERR_MEMORY;
1133 /*********************************************************************
1135 * Function : edit_parse_actions_file
1137 * Description : Parse an actions file in memory.
1139 * Passed linked list must have the "data" member
1140 * zeroed, and must contain valid "next" and
1141 * "unprocessed" fields. The "raw" and "prefix"
1142 * fields are ignored, and "type" is just overwritten.
1144 * Note that on error the file may have been
1148 * 1 : file = Actions file to be parsed in-place.
1150 * Returns : JB_ERR_OK on success
1151 * JB_ERR_MEMORY on out-of-memory
1152 * JB_ERR_PARSE on error
1154 *********************************************************************/
1155 jb_err edit_parse_actions_file(struct editable_file * file)
1157 struct file_line * cur_line;
1159 const char * text; /* Text from a line */
1160 char * name; /* For lines of the form name=value */
1161 char * value; /* For lines of the form name=value */
1162 struct action_alias * alias_list = NULL;
1163 jb_err err = JB_ERR_OK;
1165 /* alias_list contains the aliases defined in this file.
1166 * It might be better to use the "file_line.data" fields
1167 * in the relavent places instead.
1170 cur_line = file->lines;
1172 /* A note about blank line support: Blank lines should only
1173 * ever occur as the last line in the file. This function
1174 * is more forgiving than that - FILE_LINE_BLANK can occur
1178 /* Skip leading blanks. Should only happen if file is
1179 * empty (which is valid, but pointless).
1181 while ( (cur_line != NULL)
1182 && (cur_line->unprocessed[0] == '\0') )
1185 cur_line->type = FILE_LINE_BLANK;
1186 cur_line = cur_line->next;
1189 if ( (cur_line != NULL)
1190 && (cur_line->unprocessed[0] != '{') )
1192 /* File doesn't start with a header */
1193 file->parse_error = cur_line;
1194 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1195 return JB_ERR_PARSE;
1198 if ( (cur_line != NULL) && (0 ==
1199 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1201 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1203 cur_line = cur_line->next;
1204 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1206 if (cur_line->unprocessed[0])
1208 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1210 err = split_line_on_equals(cur_line->unprocessed,
1211 &cur_line->data.setting.name,
1212 &cur_line->data.setting.svalue);
1213 if (err == JB_ERR_MEMORY)
1217 else if (err != JB_ERR_OK)
1219 /* Line does not contain a name=value pair */
1220 file->parse_error = cur_line;
1221 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1222 return JB_ERR_PARSE;
1227 cur_line->type = FILE_LINE_BLANK;
1229 cur_line = cur_line->next;
1233 if ( (cur_line != NULL) && (0 ==
1234 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1236 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1238 cur_line = cur_line->next;
1239 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1241 if (cur_line->unprocessed[0])
1243 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1247 cur_line->type = FILE_LINE_BLANK;
1249 cur_line = cur_line->next;
1253 if ( (cur_line != NULL) && (0 ==
1254 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1256 cur_line->type = FILE_LINE_ALIAS_HEADER;
1258 cur_line = cur_line->next;
1259 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1261 if (cur_line->unprocessed[0])
1263 /* define an alias */
1264 struct action_alias * new_alias;
1266 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1268 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1269 if (err == JB_ERR_MEMORY)
1273 else if (err != JB_ERR_OK)
1275 /* Line does not contain a name=value pair */
1276 file->parse_error = cur_line;
1277 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1278 return JB_ERR_PARSE;
1281 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1286 free_alias_list(alias_list);
1287 return JB_ERR_MEMORY;
1290 err = get_actions(value, alias_list, new_alias->action);
1293 /* Invalid action or out of memory */
1297 free_alias_list(alias_list);
1298 if (err == JB_ERR_MEMORY)
1304 /* Line does not contain a name=value pair */
1305 file->parse_error = cur_line;
1306 file->parse_error_text = "This alias does not specify a valid set of actions.";
1307 return JB_ERR_PARSE;
1313 new_alias->name = name;
1316 new_alias->next = alias_list;
1317 alias_list = new_alias;
1321 cur_line->type = FILE_LINE_BLANK;
1323 cur_line = cur_line->next;
1327 /* Header done, process the main part of the file */
1328 while (cur_line != NULL)
1330 /* At this point, (cur_line->unprocessed[0] == '{') */
1331 assert(cur_line->unprocessed[0] == '{');
1332 text = cur_line->unprocessed + 1;
1333 len = strlen(text) - 1;
1334 if (text[len] != '}')
1336 /* No closing } on header */
1337 free_alias_list(alias_list);
1338 file->parse_error = cur_line;
1339 file->parse_error_text = "Headers starting with '{' must have a "
1340 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1341 "must close with two brackets ('}}').";
1342 return JB_ERR_PARSE;
1347 /* An invalid {{ header. */
1348 free_alias_list(alias_list);
1349 file->parse_error = cur_line;
1350 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1351 "Please remember that the system (two-bracket) headers must "
1352 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1353 "and must appear before any actions (one-bracket) headers. "
1354 "Also note that system headers may not be repeated.";
1355 return JB_ERR_PARSE;
1358 while ( (*text == ' ') || (*text == '\t') )
1364 && ( (text[len - 1] == ' ')
1365 || (text[len - 1] == '\t') ) )
1370 cur_line->type = FILE_LINE_ACTION;
1372 /* Remove {} and make copy */
1373 if (NULL == (value = (char *) malloc(len + 1)))
1376 free_alias_list(alias_list);
1377 return JB_ERR_MEMORY;
1379 strncpy(value, text, len);
1383 err = get_actions(value, alias_list, cur_line->data.action);
1386 /* Invalid action or out of memory */
1388 free_alias_list(alias_list);
1389 if (err == JB_ERR_MEMORY)
1395 /* Line does not contain a name=value pair */
1396 file->parse_error = cur_line;
1397 file->parse_error_text = "This header does not specify a valid set of actions.";
1398 return JB_ERR_PARSE;
1402 /* Done with string - it was clobbered anyway */
1405 /* Process next line */
1406 cur_line = cur_line->next;
1408 /* Loop processing URL patterns */
1409 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1411 if (cur_line->unprocessed[0])
1413 /* Could parse URL here, but this isn't currently needed */
1415 cur_line->type = FILE_LINE_URL;
1419 cur_line->type = FILE_LINE_BLANK;
1421 cur_line = cur_line->next;
1423 } /* End main while(cur_line != NULL) loop */
1425 free_alias_list(alias_list);
1431 /*********************************************************************
1433 * Function : edit_read_file_lines
1435 * Description : Read all the lines of a file into memory.
1436 * Handles whitespace, comments and line continuation.
1439 * 1 : fp = File to read from. On return, this will be
1440 * at EOF but it will not have been closed.
1441 * 2 : pfile = Destination for a linked list of file_lines.
1442 * Will be set to NULL on error.
1444 * Returns : JB_ERR_OK on success
1445 * JB_ERR_MEMORY on out-of-memory
1447 *********************************************************************/
1448 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1450 struct file_line * first_line; /* Keep for return value or to free */
1451 struct file_line * cur_line; /* Current line */
1452 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1460 cur_line = first_line = zalloc(sizeof(struct file_line));
1461 if (cur_line == NULL)
1463 return JB_ERR_MEMORY;
1466 cur_line->type = FILE_LINE_UNPROCESSED;
1468 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1471 /* Out of memory or empty file. */
1472 /* Note that empty file is not an error we propogate up */
1474 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1479 prev_line = cur_line;
1480 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1481 if (cur_line == NULL)
1484 edit_free_file_lines(first_line);
1485 return JB_ERR_MEMORY;
1488 cur_line->type = FILE_LINE_UNPROCESSED;
1490 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1491 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1494 edit_free_file_lines(first_line);
1495 return JB_ERR_MEMORY;
1499 while (rval != JB_ERR_FILE);
1503 /* We allocated one too many - free it */
1504 prev_line->next = NULL;
1507 *pfile = first_line;
1512 /*********************************************************************
1514 * Function : edit_read_file
1516 * Description : Read a complete file into memory.
1517 * Handles CGI parameter parsing. If requested, also
1518 * checks the file's modification timestamp.
1521 * 1 : csp = Current client state (buffers, headers, etc...)
1522 * 2 : parameters = map of cgi parameters.
1523 * 3 : require_version = true to check "ver" parameter.
1524 * 4 : suffix = File extension, e.g. ".action".
1525 * 5 : pfile = Destination for the file. Will be set
1529 * filename : The name of the file to read, without the
1530 * path or ".action" extension.
1531 * ver : (Only if require_version is nonzero)
1532 * Timestamp of the actions file. If wrong, this
1533 * function fails with JB_ERR_MODIFIED.
1535 * Returns : JB_ERR_OK on success
1536 * JB_ERR_MEMORY on out-of-memory
1537 * JB_ERR_CGI_PARAMS if "filename" was not specified
1539 * JB_ERR_FILE if the file cannot be opened or
1541 * JB_ERR_MODIFIED if version checking was requested and
1542 * failed - the file was modified outside
1543 * of this CGI editor instance.
1545 *********************************************************************/
1546 jb_err edit_read_file(struct client_state *csp,
1547 const struct map *parameters,
1548 int require_version,
1550 struct editable_file **pfile)
1552 struct file_line * lines;
1556 const char * identifier;
1557 struct editable_file * file;
1558 unsigned version = 0;
1559 struct stat statbuf[1];
1560 char version_buf[22];
1561 int newline = NEWLINE_UNKNOWN;
1569 err = get_file_name_param(csp, parameters, "f", suffix,
1570 &filename, &identifier);
1576 if (stat(filename, statbuf) < 0)
1578 /* Error, probably file not found. */
1582 version = (unsigned) statbuf->st_mtime;
1584 if (require_version)
1586 unsigned specified_version;
1587 err = get_number_param(csp, parameters, "v", &specified_version);
1594 if (version != specified_version)
1596 return JB_ERR_MODIFIED;
1600 if (NULL == (fp = fopen(filename,"rb")))
1606 err = edit_read_file_lines(fp, &lines, &newline);
1616 file = (struct editable_file *) zalloc(sizeof(*file));
1620 edit_free_file_lines(lines);
1624 file->lines = lines;
1625 file->newline = newline;
1626 file->filename = filename;
1627 file->version = version;
1628 file->identifier = url_encode(identifier);
1630 if (file->identifier == NULL)
1632 edit_free_file(file);
1633 return JB_ERR_MEMORY;
1636 /* Correct file->version_str */
1637 freez(file->version_str);
1638 snprintf(version_buf, 22, "%u", file->version);
1639 version_buf[21] = '\0';
1640 file->version_str = strdup(version_buf);
1641 if (version_buf == NULL)
1643 edit_free_file(file);
1644 return JB_ERR_MEMORY;
1652 /*********************************************************************
1654 * Function : edit_read_actions_file
1656 * Description : Read a complete actions file into memory.
1657 * Handles CGI parameter parsing. If requested, also
1658 * checks the file's modification timestamp.
1660 * If this function detects an error in the categories
1661 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1662 * then it handles it by filling in the specified
1663 * response structure and returning JB_ERR_FILE.
1666 * 1 : csp = Current client state (buffers, headers, etc...)
1667 * 2 : rsp = HTTP response. Only filled in on error.
1668 * 2 : parameters = map of cgi parameters.
1669 * 3 : require_version = true to check "ver" parameter.
1670 * 4 : pfile = Destination for the file. Will be set
1674 * filename : The name of the actions file to read, without the
1675 * path or ".action" extension.
1676 * ver : (Only if require_version is nonzero)
1677 * Timestamp of the actions file. If wrong, this
1678 * function fails with JB_ERR_MODIFIED.
1680 * Returns : JB_ERR_OK on success
1681 * JB_ERR_MEMORY on out-of-memory
1682 * JB_ERR_CGI_PARAMS if "filename" was not specified
1684 * JB_ERR_FILE if the file does not contain valid data,
1685 * or if file cannot be opened or
1686 * contains no data, or if version
1687 * checking was requested and failed.
1689 *********************************************************************/
1690 jb_err edit_read_actions_file(struct client_state *csp,
1691 struct http_response *rsp,
1692 const struct map *parameters,
1693 int require_version,
1694 struct editable_file **pfile)
1697 struct editable_file *file;
1705 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1708 /* Try to handle if possible */
1709 if (err == JB_ERR_FILE)
1711 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1713 else if (err == JB_ERR_MODIFIED)
1715 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1717 if (err == JB_ERR_OK)
1720 * Signal to higher-level CGI code that there was a problem but we
1721 * handled it, they should just return JB_ERR_OK.
1728 err = edit_parse_actions_file(file);
1731 if (err == JB_ERR_PARSE)
1733 err = cgi_error_parse(csp, rsp, file);
1734 if (err == JB_ERR_OK)
1737 * Signal to higher-level CGI code that there was a problem but we
1738 * handled it, they should just return JB_ERR_OK.
1743 edit_free_file(file);
1752 /*********************************************************************
1754 * Function : get_file_name_param
1756 * Description : Get the name of the file to edit from the parameters
1757 * passed to a CGI function. This function handles
1758 * security checks such as blocking urls containing
1759 * "/" or ".", prepending the config file directory,
1760 * and adding the specified suffix.
1762 * (This is an essential security check, otherwise
1763 * users may be able to pass "../../../etc/passwd"
1764 * and overwrite the password file [linux], "prn:"
1765 * and print random data [Windows], etc...)
1767 * This function only allows filenames contining the
1768 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1769 * That's probably too restrictive but at least it's
1773 * 1 : csp = Current client state (buffers, headers, etc...)
1774 * 2 : parameters = map of cgi parameters
1775 * 3 : param_name = The name of the parameter to read
1776 * 4 : suffix = File extension, e.g. ".actions"
1777 * 5 : pfilename = destination for full filename. Caller
1778 * free()s. Set to NULL on error.
1779 * 6 : pparam = destination for partial filename,
1780 * suitable for use in another URL. Allocated as part
1781 * of the map "parameters", so don't free it.
1782 * Set to NULL if not specified.
1784 * Returns : JB_ERR_OK on success
1785 * JB_ERR_MEMORY on out-of-memory
1786 * JB_ERR_CGI_PARAMS if "filename" was not specified
1789 *********************************************************************/
1790 static jb_err get_file_name_param(struct client_state *csp,
1791 const struct map *parameters,
1792 const char *param_name,
1795 const char **pparam)
1813 param = lookup(parameters, param_name);
1816 return JB_ERR_CGI_PARAMS;
1821 len = strlen(param);
1822 if (len >= FILENAME_MAX)
1825 return JB_ERR_CGI_PARAMS;
1828 /* Check every character to see if it's legal */
1830 while ((ch = *s++) != '\0')
1832 if ( ((ch < 'A') || (ch > 'Z'))
1833 && ((ch < 'a') || (ch > 'z'))
1834 && ((ch < '0') || (ch > '9'))
1838 /* Probable hack attempt. */
1839 return JB_ERR_CGI_PARAMS;
1843 /* Append extension */
1844 name = malloc(len + strlen(suffix) + 1);
1847 return JB_ERR_MEMORY;
1849 strcpy(name, param);
1850 strcpy(name + len, suffix);
1853 fullpath = make_path(csp->config->confdir, name);
1856 if (fullpath == NULL)
1858 return JB_ERR_MEMORY;
1862 *pfilename = fullpath;
1868 /*********************************************************************
1870 * Function : get_url_spec_param
1872 * Description : Get a URL pattern from the parameters
1873 * passed to a CGI function. Removes leading/trailing
1874 * spaces and validates it.
1877 * 1 : csp = Current client state (buffers, headers, etc...)
1878 * 2 : parameters = map of cgi parameters
1879 * 3 : name = Name of CGI parameter to read
1880 * 4 : pvalue = destination for value. Will be malloc()'d.
1881 * Set to NULL on error.
1883 * Returns : JB_ERR_OK on success
1884 * JB_ERR_MEMORY on out-of-memory
1885 * JB_ERR_CGI_PARAMS if the parameter was not specified
1888 *********************************************************************/
1889 static jb_err get_url_spec_param(struct client_state *csp,
1890 const struct map *parameters,
1894 const char *orig_param;
1897 struct url_spec compiled[1];
1907 orig_param = lookup(parameters, name);
1910 return JB_ERR_CGI_PARAMS;
1913 /* Copy and trim whitespace */
1914 param = strdup(orig_param);
1917 return JB_ERR_MEMORY;
1921 /* Must be non-empty, and can't allow 1st character to be '{' */
1922 if (param[0] == '\0' || param[0] == '{')
1925 return JB_ERR_CGI_PARAMS;
1928 /* Check for embedded newlines */
1929 for (s = param; *s != '\0'; s++)
1931 if ((*s == '\r') || (*s == '\n'))
1934 return JB_ERR_CGI_PARAMS;
1938 /* Check that regex is valid */
1943 return JB_ERR_MEMORY;
1945 err = create_url_spec(compiled, s);
1950 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1952 free_url_spec(compiled);
1954 if (param[strlen(param) - 1] == '\\')
1957 * Must protect trailing '\\' from becoming line continuation character.
1958 * Two methods: 1) If it's a domain only, add a trailing '/'.
1959 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1961 if (strchr(param, '/') == NULL)
1963 err = string_append(¶m, "/");
1967 err = string_append(¶m, "(?:)");
1974 /* Check that the modified regex is valid */
1979 return JB_ERR_MEMORY;
1981 err = create_url_spec(compiled, s);
1986 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1988 free_url_spec(compiled);
1995 /*********************************************************************
1997 * Function : map_radio
1999 * Description : Map a set of radio button values. E.g. if you have
2000 * 3 radio buttons, declare them as:
2001 * <option type="radio" name="xyz" @xyz-a@>
2002 * <option type="radio" name="xyz" @xyz-b@>
2003 * <option type="radio" name="xyz" @xyz-c@>
2004 * Then map one of the @xyz-?@ variables to "checked"
2005 * and all the others to empty by calling:
2006 * map_radio(exports, "xyz", "abc", sel)
2007 * Where 'sel' is 'a', 'b', or 'c'.
2010 * 1 : exports = Exports map to modify.
2011 * 2 : optionname = name for map
2012 * 3 : values = null-terminated list of values;
2013 * 4 : value = Selected value.
2015 * CGI Parameters : None
2017 * Returns : JB_ERR_OK on success
2018 * JB_ERR_MEMORY on out-of-memory
2020 *********************************************************************/
2021 static jb_err map_radio(struct map * exports,
2022 const char * optionname,
2023 const char * values,
2035 len = strlen(optionname);
2036 buf = malloc(len + 3);
2039 return JB_ERR_MEMORY;
2042 strcpy(buf, optionname);
2047 while ((c = *values++) != '\0')
2052 if (map(exports, buf, 1, "", 1))
2054 return JB_ERR_MEMORY;
2060 return map(exports, buf, 0, "checked", 1);
2064 /*********************************************************************
2066 * Function : cgi_error_modified
2068 * Description : CGI function that is called when a file is modified
2069 * outside the CGI editor.
2072 * 1 : csp = Current client state (buffers, headers, etc...)
2073 * 2 : rsp = http_response data structure for output
2074 * 3 : filename = The file that was modified.
2076 * CGI Parameters : none
2078 * Returns : JB_ERR_OK on success
2079 * JB_ERR_MEMORY on out-of-memory error.
2081 *********************************************************************/
2082 jb_err cgi_error_modified(struct client_state *csp,
2083 struct http_response *rsp,
2084 const char *filename)
2086 struct map *exports;
2093 if (NULL == (exports = default_exports(csp, NULL)))
2095 return JB_ERR_MEMORY;
2098 err = map(exports, "f", 1, html_encode(filename), 0);
2105 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2109 /*********************************************************************
2111 * Function : cgi_error_parse
2113 * Description : CGI function that is called when a file cannot
2114 * be parsed by the CGI editor.
2117 * 1 : csp = Current client state (buffers, headers, etc...)
2118 * 2 : rsp = http_response data structure for output
2119 * 3 : file = The file that was modified.
2121 * CGI Parameters : none
2123 * Returns : JB_ERR_OK on success
2124 * JB_ERR_MEMORY on out-of-memory error.
2126 *********************************************************************/
2127 jb_err cgi_error_parse(struct client_state *csp,
2128 struct http_response *rsp,
2129 struct editable_file *file)
2131 struct map *exports;
2133 struct file_line *cur_line;
2139 if (NULL == (exports = default_exports(csp, NULL)))
2141 return JB_ERR_MEMORY;
2144 err = map(exports, "f", 1, file->identifier, 1);
2145 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2147 cur_line = file->parse_error;
2150 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2151 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2159 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2163 /*********************************************************************
2165 * Function : cgi_error_file
2167 * Description : CGI function that is called when a file cannot be
2168 * opened by the CGI editor.
2171 * 1 : csp = Current client state (buffers, headers, etc...)
2172 * 2 : rsp = http_response data structure for output
2173 * 3 : filename = The file that was modified.
2175 * CGI Parameters : none
2177 * Returns : JB_ERR_OK on success
2178 * JB_ERR_MEMORY on out-of-memory error.
2180 *********************************************************************/
2181 jb_err cgi_error_file(struct client_state *csp,
2182 struct http_response *rsp,
2183 const char *filename)
2185 struct map *exports;
2192 if (NULL == (exports = default_exports(csp, NULL)))
2194 return JB_ERR_MEMORY;
2197 err = map(exports, "f", 1, html_encode(filename), 0);
2204 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2208 /*********************************************************************
2210 * Function : cgi_error_disabled
2212 * Description : CGI function that is called if the actions editor
2213 * is called although it's disabled in config
2216 * 1 : csp = Current client state (buffers, headers, etc...)
2217 * 2 : rsp = http_response data structure for output
2219 * CGI Parameters : none
2221 * Returns : JB_ERR_OK on success
2222 * JB_ERR_MEMORY on out-of-memory error.
2224 *********************************************************************/
2225 jb_err cgi_error_disabled(struct client_state *csp,
2226 struct http_response *rsp)
2228 struct map *exports;
2233 if (NULL == (exports = default_exports(csp, NULL)))
2235 return JB_ERR_MEMORY;
2238 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2242 /*********************************************************************
2244 * Function : cgi_edit_actions
2246 * Description : CGI function that allows the user to choose which
2247 * actions file to edit.
2250 * 1 : csp = Current client state (buffers, headers, etc...)
2251 * 2 : rsp = http_response data structure for output
2252 * 3 : parameters = map of cgi parameters
2254 * CGI Parameters : None
2256 * Returns : JB_ERR_OK on success
2257 * JB_ERR_MEMORY on out-of-memory error
2259 *********************************************************************/
2260 jb_err cgi_edit_actions(struct client_state *csp,
2261 struct http_response *rsp,
2262 const struct map *parameters)
2265 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2267 return cgi_error_disabled(csp, rsp);
2270 /* FIXME: Incomplete */
2271 rsp->status = strdup("302 Local Redirect from Privoxy");
2272 if (rsp->status == NULL)
2274 return JB_ERR_MEMORY;
2276 if (enlist_unique_header(rsp->headers, "Location",
2277 CGI_PREFIX "edit-actions-list?f=default"))
2281 return JB_ERR_MEMORY;
2288 /*********************************************************************
2290 * Function : cgi_edit_actions_list
2292 * Description : CGI function that edits the actions list.
2293 * FIXME: This function shouldn't FATAL ever.
2294 * FIXME: This function doesn't check the retval of map()
2296 * 1 : csp = Current client state (buffers, headers, etc...)
2297 * 2 : rsp = http_response data structure for output
2298 * 3 : parameters = map of cgi parameters
2300 * CGI Parameters : filename
2302 * Returns : JB_ERR_OK on success
2303 * JB_ERR_MEMORY on out-of-memory
2304 * JB_ERR_FILE if the file cannot be opened or
2306 * JB_ERR_CGI_PARAMS if "filename" was not specified
2309 *********************************************************************/
2310 jb_err cgi_edit_actions_list(struct client_state *csp,
2311 struct http_response *rsp,
2312 const struct map *parameters)
2314 char * section_template;
2315 char * url_template;
2320 struct map * exports;
2321 struct map * section_exports;
2322 struct map * url_exports;
2323 struct editable_file * file;
2324 struct file_line * cur_line;
2325 unsigned line_number = 0;
2326 unsigned prev_section_line_number = ((unsigned) (-1));
2328 struct file_list * fl;
2329 struct url_actions * b;
2330 char * buttons = NULL;
2333 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2335 return cgi_error_disabled(csp, rsp);
2338 if (NULL == (exports = default_exports(csp, NULL)))
2340 edit_free_file(file);
2341 return JB_ERR_MEMORY;
2344 /* Load actions file */
2345 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2348 /* No filename specified, can't read file, or out of memory. */
2349 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2352 /* Find start of actions in file */
2353 cur_line = file->lines;
2355 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2357 cur_line = cur_line->next;
2362 * Conventional actions files should have a match all block
2364 * cur_line = {...global actions...}
2365 * cur_line->next = /
2366 * cur_line->next->next = {...actions...} or EOF
2368 if ( (cur_line != NULL)
2369 && (cur_line->type == FILE_LINE_ACTION)
2370 && (cur_line->next != NULL)
2371 && (cur_line->next->type == FILE_LINE_URL)
2372 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2373 && ( (cur_line->next->next == NULL)
2374 || (cur_line->next->next->type != FILE_LINE_URL)
2378 * Generate string with buttons to set actions for "/" to
2379 * any predefined set of actions (named standard.*, propably
2380 * residing in standard.action).
2381 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
2384 buttons = strdup(" ");
2385 for (i = 0; i < MAX_ACTION_FILES; i++)
2387 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2389 for (b = b->next; NULL != b; b = b->next)
2391 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2393 snprintf(buf, 150, "<p><a name=\"l@s@\" href=\"eas?f=@f@&v=@v@&s=@all-urls-s@&p=%s\">Set"
2394 " to %s</a></p>", b->url->spec + 9, b->url->spec + 9);
2395 if (!err) err = string_append(&buttons, buf);
2400 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2403 * Conventional actions file, supply extra editing help.
2404 * (e.g. don't allow them to make it an unconventional one).
2406 err = map_conditional(exports, "all-urls-present", 1);
2408 snprintf(buf, 150, "%d", line_number);
2409 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2410 snprintf(buf, 150, "%d", line_number + 2);
2411 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2412 if (!err) err = map(exports, "all-urls-actions", 1,
2413 actions_to_html(cur_line->data.action), 0);
2415 /* Skip the 2 lines */
2416 cur_line = cur_line->next->next;
2420 * Note that prev_section_line_number is NOT set here.
2421 * This is deliberate and not a bug. It stops a "Move up"
2422 * option appearing on the next section. Clicking "Move
2423 * up" would make the actions file unconventional, which
2424 * we don't want, so we hide this option.
2430 * Non-standard actions file - does not begin with
2431 * the "All URLs" section.
2433 err = map_conditional(exports, "all-urls-present", 0);
2436 /* Set up global exports */
2438 err = map(exports, "f", 1, file->identifier, 1);
2439 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2441 /* Discourage private additions to default.action */
2443 if (!err) err = map_conditional(exports, "default-action",
2444 (strcmp("default", lookup(parameters, "f")) == 0));
2447 edit_free_file(file);
2452 /* Should do all global exports above this point */
2454 /* Load templates */
2456 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2459 edit_free_file(file);
2461 if (err == JB_ERR_FILE)
2463 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2468 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2471 free(section_template);
2472 edit_free_file(file);
2474 if (err == JB_ERR_FILE)
2476 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2481 err = template_fill(§ion_template, exports);
2485 edit_free_file(file);
2491 err = template_fill(&url_template, exports);
2494 free(section_template);
2495 edit_free_file(file);
2500 if (NULL == (sections = strdup("")))
2502 free(section_template);
2504 edit_free_file(file);
2506 return JB_ERR_MEMORY;
2509 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2511 if (NULL == (section_exports = new_map()))
2514 free(section_template);
2516 edit_free_file(file);
2518 return JB_ERR_MEMORY;
2521 snprintf(buf, 150, "%d", line_number);
2522 err = map(section_exports, "s", 1, buf, 1);
2523 if (!err) err = map(section_exports, "actions", 1,
2524 actions_to_html(cur_line->data.action), 0);
2527 && (cur_line->next != NULL)
2528 && (cur_line->next->type == FILE_LINE_URL))
2530 /* This section contains at least one URL, don't allow delete */
2531 err = map_block_killer(section_exports, "empty-section");
2535 if (!err) err = map_block_keep(section_exports, "empty-section");
2538 if (prev_section_line_number != ((unsigned)(-1)))
2540 /* Not last section */
2541 snprintf(buf, 150, "%d", prev_section_line_number);
2542 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2543 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2548 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2550 prev_section_line_number = line_number;
2555 free(section_template);
2557 edit_free_file(file);
2559 free_map(section_exports);
2563 /* Should do all section-specific exports above this point */
2565 if (NULL == (urls = strdup("")))
2568 free(section_template);
2570 edit_free_file(file);
2572 free_map(section_exports);
2573 return JB_ERR_MEMORY;
2578 cur_line = cur_line->next;
2581 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2583 if (NULL == (url_exports = new_map()))
2587 free(section_template);
2589 edit_free_file(file);
2591 free_map(section_exports);
2592 return JB_ERR_MEMORY;
2595 snprintf(buf, 150, "%d", line_number);
2596 err = map(url_exports, "p", 1, buf, 1);
2598 snprintf(buf, 150, "%d", url_1_2);
2599 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2601 if (!err) err = map(url_exports, "url-html", 1,
2602 html_encode(cur_line->unprocessed), 0);
2603 if (!err) err = map(url_exports, "url", 1,
2604 url_encode(cur_line->unprocessed), 0);
2610 free(section_template);
2612 edit_free_file(file);
2614 free_map(section_exports);
2615 free_map(url_exports);
2619 if (NULL == (s = strdup(url_template)))
2623 free(section_template);
2625 edit_free_file(file);
2627 free_map(section_exports);
2628 free_map(url_exports);
2629 return JB_ERR_MEMORY;
2632 err = template_fill(&s, section_exports);
2633 if (!err) err = template_fill(&s, url_exports);
2634 if (!err) err = string_append(&urls, s);
2636 free_map(url_exports);
2643 free(section_template);
2645 edit_free_file(file);
2647 free_map(section_exports);
2651 url_1_2 = 3 - url_1_2;
2653 cur_line = cur_line->next;
2657 err = map(section_exports, "urls", 1, urls, 0);
2659 /* Could also do section-specific exports here, but it wouldn't be as fast */
2661 if ( (cur_line != NULL)
2662 && (cur_line->type == FILE_LINE_ACTION))
2664 /* Not last section */
2665 snprintf(buf, 150, "%d", line_number);
2666 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2667 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2672 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2678 free(section_template);
2680 edit_free_file(file);
2682 free_map(section_exports);
2686 if (NULL == (s = strdup(section_template)))
2689 free(section_template);
2691 edit_free_file(file);
2693 free_map(section_exports);
2694 return JB_ERR_MEMORY;
2697 err = template_fill(&s, section_exports);
2698 if (!err) err = string_append(§ions, s);
2701 free_map(section_exports);
2706 free(section_template);
2708 edit_free_file(file);
2714 edit_free_file(file);
2715 free(section_template);
2718 err = map(exports, "sections", 1, sections, 0);
2725 /* Could also do global exports here, but it wouldn't be as fast */
2727 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2731 /*********************************************************************
2733 * Function : cgi_edit_actions_for_url
2735 * Description : CGI function that edits the Actions list.
2738 * 1 : csp = Current client state (buffers, headers, etc...)
2739 * 2 : rsp = http_response data structure for output
2740 * 3 : parameters = map of cgi parameters
2742 * CGI Parameters : None
2744 * Returns : JB_ERR_OK on success
2745 * JB_ERR_MEMORY on out-of-memory
2746 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2747 * specified or not valid.
2749 *********************************************************************/
2750 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2751 struct http_response *rsp,
2752 const struct map *parameters)
2754 struct map * exports;
2756 struct editable_file * file;
2757 struct file_line * cur_line;
2758 unsigned line_number;
2760 struct file_list *filter_file;
2761 struct re_filterfile_spec *filter_group;
2763 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2765 return cgi_error_disabled(csp, rsp);
2768 err = get_number_param(csp, parameters, "s", §ionid);
2774 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2777 /* No filename specified, can't read file, modified, or out of memory. */
2778 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2781 cur_line = file->lines;
2783 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2785 cur_line = cur_line->next;
2788 if ( (cur_line == NULL)
2789 || (line_number != sectionid)
2791 || (cur_line->type != FILE_LINE_ACTION))
2793 /* Invalid "sectionid" parameter */
2794 edit_free_file(file);
2795 return JB_ERR_CGI_PARAMS;
2798 if (NULL == (exports = default_exports(csp, NULL)))
2800 edit_free_file(file);
2801 return JB_ERR_MEMORY;
2804 err = map(exports, "f", 1, file->identifier, 1);
2805 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2806 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2808 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2810 filter_file = csp->rlist;
2811 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2813 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2817 edit_free_file(file);
2822 if (filter_group == NULL)
2824 err = map(exports, "filter-params", 1, "", 1);
2828 /* We have some entries in the filter list */
2831 char * filter_template;
2833 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2836 edit_free_file(file);
2838 if (err == JB_ERR_FILE)
2840 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2845 result = strdup("");
2847 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2849 char current_mode = 'x';
2850 struct list_entry *filter_name;
2852 struct map *line_exports;
2855 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2856 while ((filter_name != NULL)
2857 && (0 != strcmp(filter_group->name, filter_name->str)))
2859 filter_name = filter_name->next;
2862 if (filter_name != NULL)
2868 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2869 while ((filter_name != NULL)
2870 && (0 != strcmp(filter_group->name, filter_name->str)))
2872 filter_name = filter_name->next;
2874 if (filter_name != NULL)
2880 /* Generate a unique serial number */
2881 snprintf(number, sizeof(number), "%x", index++);
2882 number[sizeof(number) - 1] = '\0';
2884 line_exports = new_map();
2885 if (line_exports == NULL)
2887 err = JB_ERR_MEMORY;
2892 if (!err) err = map(line_exports, "index", 1, number, 1);
2893 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2894 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2895 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2900 this_line = strdup(filter_template);
2901 if (this_line == NULL) err = JB_ERR_MEMORY;
2903 if (!err) err = template_fill(&this_line, line_exports);
2904 string_join(&result, this_line);
2906 free_map(line_exports);
2911 err = map(exports, "filter-params", 1, result, 0);
2919 if (!err) err = map_radio(exports, "filter-all", "nx",
2920 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
2922 edit_free_file(file);
2930 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2934 /*********************************************************************
2936 * Function : cgi_edit_actions_submit
2938 * Description : CGI function that actually edits the Actions list.
2941 * 1 : csp = Current client state (buffers, headers, etc...)
2942 * 2 : rsp = http_response data structure for output
2943 * 3 : parameters = map of cgi parameters
2945 * CGI Parameters : None
2947 * Returns : JB_ERR_OK on success
2948 * JB_ERR_MEMORY on out-of-memory
2949 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2950 * specified or not valid.
2952 *********************************************************************/
2953 jb_err cgi_edit_actions_submit(struct client_state *csp,
2954 struct http_response *rsp,
2955 const struct map *parameters)
2961 struct editable_file * file;
2962 struct file_line * cur_line;
2963 unsigned line_number;
2967 const char * action_set_name;
2969 struct file_list * fl;
2970 struct url_actions * b;
2972 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2974 return cgi_error_disabled(csp, rsp);
2977 err = get_number_param(csp, parameters, "s", §ionid);
2983 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2986 /* No filename specified, can't read file, modified, or out of memory. */
2987 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2990 cur_line = file->lines;
2992 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2994 cur_line = cur_line->next;
2997 if ( (cur_line == NULL)
2998 || (line_number != sectionid)
3000 || (cur_line->type != FILE_LINE_ACTION))
3002 /* Invalid "sectionid" parameter */
3003 edit_free_file(file);
3004 return JB_ERR_CGI_PARAMS;
3007 get_string_param(parameters, "p", &action_set_name);
3008 if (action_set_name != NULL)
3010 for (index = 0; index < MAX_ACTION_FILES; index++)
3012 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3014 for (b = b->next; NULL != b; b = b->next)
3016 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3018 copy_action(cur_line->data.action, b->action);
3024 edit_free_file(file);
3025 return JB_ERR_CGI_PARAMS;
3031 err = actions_from_radio(parameters, cur_line->data.action);
3037 edit_free_file(file);
3041 ch = get_char_param(parameters, "filter_all");
3044 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3045 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3046 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3050 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3053 for (index = 0; !err; index++)
3060 /* Generate the keys */
3061 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3062 key_value[sizeof(key_value) - 1] = '\0';
3063 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3064 key_name[sizeof(key_name) - 1] = '\0';
3066 err = get_string_param(parameters, key_name, &name);
3075 value = get_char_param(parameters, key_value);
3078 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3079 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3080 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3082 else if (value == 'N')
3084 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3085 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3087 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3088 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3091 else if (value == 'X')
3093 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3094 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3101 edit_free_file(file);
3105 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3108 edit_free_file(file);
3109 return JB_ERR_MEMORY;
3112 len = strlen(actiontext);
3116 * Empty action - must special-case this.
3117 * Simply setting len to 1 is sufficient...
3122 if (NULL == (newtext = malloc(len + 2)))
3126 edit_free_file(file);
3127 return JB_ERR_MEMORY;
3129 strcpy(newtext, actiontext);
3133 newtext[len + 1] = '\0';
3135 freez(cur_line->raw);
3136 freez(cur_line->unprocessed);
3137 cur_line->unprocessed = newtext;
3139 err = edit_write_file(file);
3142 /* Error writing file */
3143 edit_free_file(file);
3147 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3148 string_append(&target, file->identifier);
3150 edit_free_file(file);
3155 return JB_ERR_MEMORY;
3158 rsp->status = strdup("302 Local Redirect from Privoxy");
3159 if (rsp->status == NULL)
3162 return JB_ERR_MEMORY;
3164 err = enlist_unique_header(rsp->headers, "Location", target);
3171 /*********************************************************************
3173 * Function : cgi_edit_actions_url
3175 * Description : CGI function that actually edits a URL pattern in
3179 * 1 : csp = Current client state (buffers, headers, etc...)
3180 * 2 : rsp = http_response data structure for output
3181 * 3 : parameters = map of cgi parameters
3184 * filename : Identifies the file to edit
3185 * ver : File's last-modified time
3186 * section : Line number of section to edit
3187 * pattern : Line number of pattern to edit
3188 * newval : New value for pattern
3190 * Returns : JB_ERR_OK on success
3191 * JB_ERR_MEMORY on out-of-memory
3192 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3193 * specified or not valid.
3195 *********************************************************************/
3196 jb_err cgi_edit_actions_url(struct client_state *csp,
3197 struct http_response *rsp,
3198 const struct map *parameters)
3202 struct editable_file * file;
3203 struct file_line * cur_line;
3204 unsigned line_number;
3208 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3210 return cgi_error_disabled(csp, rsp);
3213 err = get_number_param(csp, parameters, "p", &patternid);
3220 return JB_ERR_CGI_PARAMS;
3223 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3229 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3232 /* No filename specified, can't read file, modified, or out of memory. */
3234 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3238 cur_line = file->lines;
3240 while ((cur_line != NULL) && (line_number < patternid))
3242 cur_line = cur_line->next;
3246 if ( (cur_line == NULL)
3247 || (cur_line->type != FILE_LINE_URL))
3249 /* Invalid "patternid" parameter */
3251 edit_free_file(file);
3252 return JB_ERR_CGI_PARAMS;
3255 /* At this point, the line to edit is in cur_line */
3257 freez(cur_line->raw);
3258 freez(cur_line->unprocessed);
3259 cur_line->unprocessed = new_pattern;
3261 err = edit_write_file(file);
3264 /* Error writing file */
3265 edit_free_file(file);
3269 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3270 string_append(&target, file->identifier);
3272 edit_free_file(file);
3277 return JB_ERR_MEMORY;
3280 rsp->status = strdup("302 Local Redirect from Privoxy");
3281 if (rsp->status == NULL)
3284 return JB_ERR_MEMORY;
3286 err = enlist_unique_header(rsp->headers, "Location", target);
3293 /*********************************************************************
3295 * Function : cgi_edit_actions_add_url
3297 * Description : CGI function that actually adds a URL pattern to
3301 * 1 : csp = Current client state (buffers, headers, etc...)
3302 * 2 : rsp = http_response data structure for output
3303 * 3 : parameters = map of cgi parameters
3306 * filename : Identifies the file to edit
3307 * ver : File's last-modified time
3308 * section : Line number of section to edit
3309 * newval : New pattern
3311 * Returns : JB_ERR_OK on success
3312 * JB_ERR_MEMORY on out-of-memory
3313 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3314 * specified or not valid.
3316 *********************************************************************/
3317 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3318 struct http_response *rsp,
3319 const struct map *parameters)
3323 struct file_line * new_line;
3324 struct editable_file * file;
3325 struct file_line * cur_line;
3326 unsigned line_number;
3330 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3332 return cgi_error_disabled(csp, rsp);
3335 err = get_number_param(csp, parameters, "s", §ionid);
3342 return JB_ERR_CGI_PARAMS;
3345 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3351 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3354 /* No filename specified, can't read file, modified, or out of memory. */
3356 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3360 cur_line = file->lines;
3362 while ((cur_line != NULL) && (line_number < sectionid))
3364 cur_line = cur_line->next;
3368 if ( (cur_line == NULL)
3369 || (cur_line->type != FILE_LINE_ACTION))
3371 /* Invalid "sectionid" parameter */
3373 edit_free_file(file);
3374 return JB_ERR_CGI_PARAMS;
3377 /* At this point, the section header is in cur_line - add after this. */
3379 /* Allocate the new line */
3380 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3381 if (new_line == NULL)
3384 edit_free_file(file);
3385 return JB_ERR_MEMORY;
3388 /* Fill in the data members of the new line */
3389 new_line->raw = NULL;
3390 new_line->prefix = NULL;
3391 new_line->unprocessed = new_pattern;
3392 new_line->type = FILE_LINE_URL;
3394 /* Link new_line into the list, after cur_line */
3395 new_line->next = cur_line->next;
3396 cur_line->next = new_line;
3398 /* Done making changes, now commit */
3400 err = edit_write_file(file);
3403 /* Error writing file */
3404 edit_free_file(file);
3408 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3409 string_append(&target, file->identifier);
3411 edit_free_file(file);
3416 return JB_ERR_MEMORY;
3419 rsp->status = strdup("302 Local Redirect from Privoxy");
3420 if (rsp->status == NULL)
3423 return JB_ERR_MEMORY;
3425 err = enlist_unique_header(rsp->headers, "Location", target);
3432 /*********************************************************************
3434 * Function : cgi_edit_actions_remove_url
3436 * Description : CGI function that actually removes a URL pattern from
3440 * 1 : csp = Current client state (buffers, headers, etc...)
3441 * 2 : rsp = http_response data structure for output
3442 * 3 : parameters = map of cgi parameters
3445 * f : (filename) Identifies the file to edit
3446 * v : (version) File's last-modified time
3447 * p : (pattern) Line number of pattern to remove
3449 * Returns : JB_ERR_OK on success
3450 * JB_ERR_MEMORY on out-of-memory
3451 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3452 * specified or not valid.
3454 *********************************************************************/
3455 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3456 struct http_response *rsp,
3457 const struct map *parameters)
3460 struct editable_file * file;
3461 struct file_line * cur_line;
3462 struct file_line * prev_line;
3463 unsigned line_number;
3467 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3469 return cgi_error_disabled(csp, rsp);
3472 err = get_number_param(csp, parameters, "p", &patternid);
3478 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3481 /* No filename specified, can't read file, modified, or out of memory. */
3482 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3487 cur_line = file->lines;
3489 while ((cur_line != NULL) && (line_number < patternid))
3491 prev_line = cur_line;
3492 cur_line = cur_line->next;
3496 if ( (cur_line == NULL)
3497 || (prev_line == NULL)
3498 || (cur_line->type != FILE_LINE_URL))
3500 /* Invalid "patternid" parameter */
3501 edit_free_file(file);
3502 return JB_ERR_CGI_PARAMS;
3505 /* At this point, the line to remove is in cur_line, and the previous
3506 * one is in prev_line
3509 /* Unlink cur_line */
3510 prev_line->next = cur_line->next;
3511 cur_line->next = NULL;
3514 edit_free_file_lines(cur_line);
3516 err = edit_write_file(file);
3519 /* Error writing file */
3520 edit_free_file(file);
3524 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3525 string_append(&target, file->identifier);
3527 edit_free_file(file);
3532 return JB_ERR_MEMORY;
3535 rsp->status = strdup("302 Local Redirect from Privoxy");
3536 if (rsp->status == NULL)
3539 return JB_ERR_MEMORY;
3541 err = enlist_unique_header(rsp->headers, "Location", target);
3548 /*********************************************************************
3550 * Function : cgi_edit_actions_section_remove
3552 * Description : CGI function that actually removes a whole section from
3553 * the actions file. The section must be empty first
3554 * (else JB_ERR_CGI_PARAMS).
3557 * 1 : csp = Current client state (buffers, headers, etc...)
3558 * 2 : rsp = http_response data structure for output
3559 * 3 : parameters = map of cgi parameters
3562 * f : (filename) Identifies the file to edit
3563 * v : (version) File's last-modified time
3564 * s : (section) Line number of section to edit
3566 * Returns : JB_ERR_OK on success
3567 * JB_ERR_MEMORY on out-of-memory
3568 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3569 * specified or not valid.
3571 *********************************************************************/
3572 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3573 struct http_response *rsp,
3574 const struct map *parameters)
3577 struct editable_file * file;
3578 struct file_line * cur_line;
3579 struct file_line * prev_line;
3580 unsigned line_number;
3584 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3586 return cgi_error_disabled(csp, rsp);
3589 err = get_number_param(csp, parameters, "s", §ionid);
3595 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3598 /* No filename specified, can't read file, modified, or out of memory. */
3599 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3603 cur_line = file->lines;
3606 while ((cur_line != NULL) && (line_number < sectionid))
3608 prev_line = cur_line;
3609 cur_line = cur_line->next;
3613 if ( (cur_line == NULL)
3614 || (cur_line->type != FILE_LINE_ACTION) )
3616 /* Invalid "sectionid" parameter */
3617 edit_free_file(file);
3618 return JB_ERR_CGI_PARAMS;
3621 if ( (cur_line->next != NULL)
3622 && (cur_line->next->type == FILE_LINE_URL) )
3624 /* Section not empty. */
3625 edit_free_file(file);
3626 return JB_ERR_CGI_PARAMS;
3629 /* At this point, the line to remove is in cur_line, and the previous
3630 * one is in prev_line
3633 /* Unlink cur_line */
3634 if (prev_line == NULL)
3636 /* Removing the first line from the file */
3637 file->lines = cur_line->next;
3641 prev_line->next = cur_line->next;
3643 cur_line->next = NULL;
3646 edit_free_file_lines(cur_line);
3648 err = edit_write_file(file);
3651 /* Error writing file */
3652 edit_free_file(file);
3656 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3657 string_append(&target, file->identifier);
3659 edit_free_file(file);
3664 return JB_ERR_MEMORY;
3667 rsp->status = strdup("302 Local Redirect from Privoxy");
3668 if (rsp->status == NULL)
3671 return JB_ERR_MEMORY;
3673 err = enlist_unique_header(rsp->headers, "Location", target);
3680 /*********************************************************************
3682 * Function : cgi_edit_actions_section_add
3684 * Description : CGI function that adds a new empty section to
3688 * 1 : csp = Current client state (buffers, headers, etc...)
3689 * 2 : rsp = http_response data structure for output
3690 * 3 : parameters = map of cgi parameters
3693 * f : (filename) Identifies the file to edit
3694 * v : (version) File's last-modified time
3695 * s : (section) Line number of section to add after, 0 for
3698 * Returns : JB_ERR_OK on success
3699 * JB_ERR_MEMORY on out-of-memory
3700 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3701 * specified or not valid.
3703 *********************************************************************/
3704 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3705 struct http_response *rsp,
3706 const struct map *parameters)
3709 struct file_line * new_line;
3711 struct editable_file * file;
3712 struct file_line * cur_line;
3713 unsigned line_number;
3717 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3719 return cgi_error_disabled(csp, rsp);
3722 err = get_number_param(csp, parameters, "s", §ionid);
3728 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3731 /* No filename specified, can't read file, modified, or out of memory. */
3732 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3736 cur_line = file->lines;
3740 /* Add to start of file */
3741 if (cur_line != NULL)
3743 /* There's something in the file, find the line before the first
3746 while ( (cur_line->next != NULL)
3747 && (cur_line->next->type != FILE_LINE_ACTION) )
3749 cur_line = cur_line->next;
3756 /* Add after stated section. */
3757 while ((cur_line != NULL) && (line_number < sectionid))
3759 cur_line = cur_line->next;
3763 if ( (cur_line == NULL)
3764 || (cur_line->type != FILE_LINE_ACTION))
3766 /* Invalid "sectionid" parameter */
3767 edit_free_file(file);
3768 return JB_ERR_CGI_PARAMS;
3771 /* Skip through the section to find the last line in it. */
3772 while ( (cur_line->next != NULL)
3773 && (cur_line->next->type != FILE_LINE_ACTION) )
3775 cur_line = cur_line->next;
3780 /* At this point, the last line in the previous section is in cur_line
3781 * - add after this. (Or if we need to add as the first line, cur_line
3785 new_text = strdup("{}");
3786 if (NULL == new_text)
3788 edit_free_file(file);
3789 return JB_ERR_MEMORY;
3792 /* Allocate the new line */
3793 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3794 if (new_line == NULL)
3797 edit_free_file(file);
3798 return JB_ERR_MEMORY;
3801 /* Fill in the data members of the new line */
3802 new_line->raw = NULL;
3803 new_line->prefix = NULL;
3804 new_line->unprocessed = new_text;
3805 new_line->type = FILE_LINE_ACTION;
3807 if (cur_line != NULL)
3809 /* Link new_line into the list, after cur_line */
3810 new_line->next = cur_line->next;
3811 cur_line->next = new_line;
3815 /* Link new_line into the list, as first line */
3816 new_line->next = file->lines;
3817 file->lines = new_line;
3820 /* Done making changes, now commit */
3822 err = edit_write_file(file);
3825 /* Error writing file */
3826 edit_free_file(file);
3830 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3831 string_append(&target, file->identifier);
3833 edit_free_file(file);
3838 return JB_ERR_MEMORY;
3841 rsp->status = strdup("302 Local Redirect from Privoxy");
3842 if (rsp->status == NULL)
3845 return JB_ERR_MEMORY;
3847 err = enlist_unique_header(rsp->headers, "Location", target);
3854 /*********************************************************************
3856 * Function : cgi_edit_actions_section_swap
3858 * Description : CGI function that swaps the order of two sections
3859 * in the actions file. Note that this CGI can actually
3860 * swap any two arbitrary sections, but the GUI interface
3861 * currently only allows consecutive sections to be
3865 * 1 : csp = Current client state (buffers, headers, etc...)
3866 * 2 : rsp = http_response data structure for output
3867 * 3 : parameters = map of cgi parameters
3870 * f : (filename) Identifies the file to edit
3871 * v : (version) File's last-modified time
3872 * s1 : (section1) Line number of first section to swap
3873 * s2 : (section2) Line number of second section to swap
3875 * Returns : JB_ERR_OK on success
3876 * JB_ERR_MEMORY on out-of-memory
3877 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3878 * specified or not valid.
3880 *********************************************************************/
3881 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3882 struct http_response *rsp,
3883 const struct map *parameters)
3887 struct editable_file * file;
3888 struct file_line * cur_line;
3889 struct file_line * prev_line;
3890 struct file_line * line_before_section1;
3891 struct file_line * line_start_section1;
3892 struct file_line * line_end_section1;
3893 struct file_line * line_after_section1;
3894 struct file_line * line_before_section2;
3895 struct file_line * line_start_section2;
3896 struct file_line * line_end_section2;
3897 struct file_line * line_after_section2;
3898 unsigned line_number;
3902 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3904 return cgi_error_disabled(csp, rsp);
3907 err = get_number_param(csp, parameters, "s1", §ion1);
3908 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3914 if (section1 > section2)
3916 unsigned temp = section2;
3917 section2 = section1;
3921 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3924 /* No filename specified, can't read file, modified, or out of memory. */
3925 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3928 /* Start at the beginning... */
3930 cur_line = file->lines;
3933 /* ... find section1 ... */
3934 while ((cur_line != NULL) && (line_number < section1))
3936 prev_line = cur_line;
3937 cur_line = cur_line->next;
3941 if ( (cur_line == NULL)
3942 || (cur_line->type != FILE_LINE_ACTION) )
3944 /* Invalid "section1" parameter */
3945 edit_free_file(file);
3946 return JB_ERR_CGI_PARAMS;
3949 /* If no-op, we've validated params and can skip the rest. */
3950 if (section1 != section2)
3952 /* ... find the end of section1 ... */
3953 line_before_section1 = prev_line;
3954 line_start_section1 = cur_line;
3957 prev_line = cur_line;
3958 cur_line = cur_line->next;
3961 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3962 line_end_section1 = prev_line;
3963 line_after_section1 = cur_line;
3965 /* ... find section2 ... */
3966 while ((cur_line != NULL) && (line_number < section2))
3968 prev_line = cur_line;
3969 cur_line = cur_line->next;
3973 if ( (cur_line == NULL)
3974 || (cur_line->type != FILE_LINE_ACTION) )
3976 /* Invalid "section2" parameter */
3977 edit_free_file(file);
3978 return JB_ERR_CGI_PARAMS;
3981 /* ... find the end of section2 ... */
3982 line_before_section2 = prev_line;
3983 line_start_section2 = cur_line;
3986 prev_line = cur_line;
3987 cur_line = cur_line->next;
3990 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3991 line_end_section2 = prev_line;
3992 line_after_section2 = cur_line;
3994 /* Now have all the pointers we need. Do the swap. */
3996 /* Change the pointer to section1 to point to section2 instead */
3997 if (line_before_section1 == NULL)
3999 file->lines = line_start_section2;
4003 line_before_section1->next = line_start_section2;
4006 if (line_before_section2 == line_end_section1)
4008 /* Consecutive sections */
4009 line_end_section2->next = line_start_section1;
4013 line_end_section2->next = line_after_section1;
4014 line_before_section2->next = line_start_section1;
4017 /* Set the pointer from the end of section1 to the rest of the file */
4018 line_end_section1->next = line_after_section2;
4020 err = edit_write_file(file);
4023 /* Error writing file */
4024 edit_free_file(file);
4027 } /* END if (section1 != section2) */
4029 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4030 string_append(&target, file->identifier);
4032 edit_free_file(file);
4037 return JB_ERR_MEMORY;
4040 rsp->status = strdup("302 Local Redirect from Privoxy");
4041 if (rsp->status == NULL)
4044 return JB_ERR_MEMORY;
4046 err = enlist_unique_header(rsp->headers, "Location", target);
4053 /*********************************************************************
4055 * Function : cgi_toggle
4057 * Description : CGI function that adds a new empty section to
4061 * 1 : csp = Current client state (buffers, headers, etc...)
4062 * 2 : rsp = http_response data structure for output
4063 * 3 : parameters = map of cgi parameters
4066 * set : If present, how to change toggle setting:
4067 * "enable", "disable", "toggle", or none (default).
4068 * mini : If present, use mini reply template.
4070 * Returns : JB_ERR_OK on success
4071 * JB_ERR_MEMORY on out-of-memory
4073 *********************************************************************/
4074 jb_err cgi_toggle(struct client_state *csp,
4075 struct http_response *rsp,
4076 const struct map *parameters)
4078 struct map *exports;
4080 const char *template_name;
4086 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4088 return cgi_error_disabled(csp, rsp);
4091 mode = get_char_param(parameters, "set");
4098 else if (mode == 'D')
4103 else if (mode == 'T')
4106 g_bToggleIJB = !g_bToggleIJB;
4109 if (NULL == (exports = default_exports(csp, "toggle")))
4111 return JB_ERR_MEMORY;
4114 template_name = (get_char_param(parameters, "mini")
4118 return template_fill_for_cgi(csp, template_name, exports, rsp);
4122 /*********************************************************************
4124 * Function : javascriptify
4126 * Description : Converts a string into a form JavaScript will like.
4128 * Netscape 4's JavaScript sucks - it doesn't use
4129 * "id" parameters, so you have to set the "name"
4130 * used to submit a form element to something JavaScript
4131 * will like. (Or access the elements by index in an
4132 * array. That array contains >60 elements and will
4133 * be changed whenever we add a new action to the
4134 * editor, so I'm NOT going to use indexes that have
4135 * to be figured out by hand.)
4137 * Currently the only thing we have to worry about
4138 * is "-" ==> "_" conversion.
4140 * This is a length-preserving operation so it is
4141 * carried out in-place, no memory is allocated
4145 * 1 : identifier = String to make JavaScript-friendly.
4149 *********************************************************************/
4150 static void javascriptify(char * identifier)
4152 char * p = identifier;
4153 while (NULL != (p = strchr(p, '-')))
4160 /*********************************************************************
4162 * Function : actions_to_radio
4164 * Description : Converts a actionsfile entry into settings for
4165 * radio buttons and edit boxes on a HTML form.
4168 * 1 : exports = List of substitutions to add to.
4169 * 2 : action = Action to read
4171 * Returns : JB_ERR_OK on success
4172 * JB_ERR_MEMORY on out-of-memory
4174 *********************************************************************/
4175 static jb_err actions_to_radio(struct map * exports,
4176 const struct action_spec *action)
4178 unsigned mask = action->mask;
4179 unsigned add = action->add;
4187 mask = action->mask;
4190 /* sanity - prevents "-feature +feature" */
4194 #define DEFINE_ACTION_BOOL(name, bit) \
4195 if (!(mask & bit)) \
4197 current_mode = 'n'; \
4199 else if (add & bit) \
4201 current_mode = 'y'; \
4205 current_mode = 'x'; \
4207 if (map_radio(exports, name, "ynx", current_mode)) \
4209 return JB_ERR_MEMORY; \
4212 #define DEFINE_ACTION_STRING(name, bit, index) \
4213 DEFINE_ACTION_BOOL(name, bit); \
4216 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4219 checked = !strcmp(action->string[index], value); \
4223 checked = is_default; \
4225 mapped_param |= checked; \
4226 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4228 return JB_ERR_MEMORY; \
4231 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4232 if (map(exports, name "-param-custom", 1, \
4233 ((!mapped_param) ? "checked" : ""), 1)) \
4235 return JB_ERR_MEMORY; \
4237 if (map(exports, name "-param", 1, \
4238 (((add & bit) && !mapped_param) ? \
4239 action->string[index] : default_val), 1)) \
4241 return JB_ERR_MEMORY; \
4244 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4245 if (map(exports, name "-param", 1, \
4246 ((add & bit) ? action->string[index] : default_val), 1)) \
4248 return JB_ERR_MEMORY; \
4251 #define DEFINE_ACTION_MULTI(name, index) \
4252 if (action->multi_add[index]->first) \
4254 current_mode = 'y'; \
4256 else if (action->multi_remove_all[index]) \
4258 current_mode = 'n'; \
4260 else if (action->multi_remove[index]->first) \
4262 current_mode = 'y'; \
4266 current_mode = 'x'; \
4268 if (map_radio(exports, name, "ynx", current_mode)) \
4270 return JB_ERR_MEMORY; \
4273 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4275 #include "actionlist.h"
4277 #undef DEFINE_ACTION_MULTI
4278 #undef DEFINE_ACTION_STRING
4279 #undef DEFINE_ACTION_BOOL
4280 #undef DEFINE_ACTION_ALIAS
4281 #undef DEFINE_CGI_PARAM_CUSTOM
4282 #undef DEFINE_CGI_PARAM_RADIO
4283 #undef DEFINE_CGI_PARAM_NO_RADIO
4289 /*********************************************************************
4291 * Function : actions_from_radio
4293 * Description : Converts a map of parameters passed to a CGI function
4294 * into an actionsfile entry.
4297 * 1 : parameters = parameters to the CGI call
4298 * 2 : action = Action to change. Must be valid before
4299 * the call, actions not specified will be
4302 * Returns : JB_ERR_OK on success
4303 * JB_ERR_MEMORY on out-of-memory
4305 *********************************************************************/
4306 static jb_err actions_from_radio(const struct map * parameters,
4307 struct action_spec *action)
4309 static int first_time = 1;
4313 const char * js_name;
4314 jb_err err = JB_ERR_OK;
4319 /* Statics are generally a potential race condition,
4320 * but in this case we're safe and don't need semaphores.
4321 * Be careful if you modify this function.
4325 #define JAVASCRIPTIFY(dest_var, string) \
4327 static char js_name_arr[] = string; \
4330 javascriptify(js_name_arr); \
4332 dest_var = js_name_arr; \
4335 #define DEFINE_ACTION_BOOL(name, bit) \
4336 JAVASCRIPTIFY(js_name, name); \
4337 ch = get_char_param(parameters, js_name); \
4340 action->add |= bit; \
4341 action->mask |= bit; \
4343 else if (ch == 'N') \
4345 action->add &= ~bit; \
4346 action->mask &= ~bit; \
4348 else if (ch == 'X') \
4350 action->add &= ~bit; \
4351 action->mask |= bit; \
4354 #define DEFINE_ACTION_STRING(name, bit, index) \
4355 JAVASCRIPTIFY(js_name, name); \
4356 ch = get_char_param(parameters, js_name); \
4360 JAVASCRIPTIFY(js_name, name "-mode"); \
4361 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4362 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4364 JAVASCRIPTIFY(js_name, name "-param"); \
4365 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4367 if (param != NULL) \
4369 if (NULL == (param_dup = strdup(param))) \
4371 return JB_ERR_MEMORY; \
4373 freez(action->string[index]); \
4374 action->add |= bit; \
4375 action->mask |= bit; \
4376 action->string[index] = param_dup; \
4379 else if (ch == 'N') \
4381 if (action->add & bit) \
4383 freez(action->string[index]); \
4385 action->add &= ~bit; \
4386 action->mask &= ~bit; \
4388 else if (ch == 'X') \
4390 if (action->add & bit) \
4392 freez(action->string[index]); \
4394 action->add &= ~bit; \
4395 action->mask |= bit; \
4398 #define DEFINE_ACTION_MULTI(name, index) \
4399 JAVASCRIPTIFY(js_name, name); \
4400 ch = get_char_param(parameters, js_name); \
4405 else if (ch == 'N') \
4407 list_remove_all(action->multi_add[index]); \
4408 list_remove_all(action->multi_remove[index]); \
4409 action->multi_remove_all[index] = 1; \
4411 else if (ch == 'X') \
4413 list_remove_all(action->multi_add[index]); \
4414 list_remove_all(action->multi_remove[index]); \
4415 action->multi_remove_all[index] = 0; \
4418 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4420 #include "actionlist.h"
4422 #undef DEFINE_ACTION_MULTI
4423 #undef DEFINE_ACTION_STRING
4424 #undef DEFINE_ACTION_BOOL
4425 #undef DEFINE_ACTION_ALIAS
4426 #undef JAVASCRIPTIFY
4434 #endif /* def FEATURE_CGI_EDIT_ACTIONS */