1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.83 2014/10/18 11:25:42 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * NOTE: The CGIs in this file use parameter names
9 * such as "f" and "s" which are really *BAD* choices.
10 * However, I'm trying to save bytes in the
11 * edit-actions-list HTML page - the standard actions
12 * file generated a 550kbyte page, which is ridiculous.
14 * Stick to the short names in this file for consistency.
16 * Copyright : Written by and Copyright (C) 2001-2014 the
17 * Privoxy team. http://www.privoxy.org/
19 * Based on the Internet Junkbuster originally written
20 * by and Copyright (C) 1997 Anonymous Coders and
21 * Junkbusters Corporation. http://www.junkbusters.com
23 * This program is free software; you can redistribute it
24 * and/or modify it under the terms of the GNU General
25 * Public License as published by the Free Software
26 * Foundation; either version 2 of the License, or (at
27 * your option) any later version.
29 * This program is distributed in the hope that it will
30 * be useful, but WITHOUT ANY WARRANTY; without even the
31 * implied warranty of MERCHANTABILITY or FITNESS FOR A
32 * PARTICULAR PURPOSE. See the GNU General Public
33 * License for more details.
35 * The GNU General Public License should be included with
36 * this file. If not, you can view it at
37 * http://www.gnu.org/copyleft/gpl.html
38 * or write to the Free Software Foundation, Inc., 59
39 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 **********************************************************************/
47 * FIXME: Following includes copied from cgi.c - which are actually needed?
52 #include <sys/types.h>
61 #include "cgisimple.h"
69 /* loadcfg.h is for global_toggle_state only */
71 #endif /* def FEATURE_TOGGLE */
74 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
77 #ifdef FEATURE_CGI_EDIT_ACTIONS
80 * A line in an editable_file.
84 /** Next entry in the linked list */
85 struct file_line * next;
87 /** The raw data, to write out if this line is unmodified. */
90 /** Comments and/or whitespace to put before this line if it's modified
91 and then written out. */
94 /** The actual data, as a string. Line continuation and comment removal
95 are performed on the data read from file before it's stored here, so
96 it will be a single line of data. */
99 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
102 /** The actual data, processed into some sensible data type. */
106 /** An action specification. */
107 struct action_spec action[1];
109 /** A name=value pair. */
113 /** The name in the name=value pair. */
116 /** The value in the name=value pair, as a string. */
119 /** The value in the name=value pair, as an integer. */
128 /** This file_line has not been processed yet. */
129 #define FILE_LINE_UNPROCESSED 1
131 /** This file_line is blank. Can only appear at the end of a file, due to
132 the way the parser works. */
133 #define FILE_LINE_BLANK 2
135 /** This file_line says {{alias}}. */
136 #define FILE_LINE_ALIAS_HEADER 3
138 /** This file_line defines an alias. */
139 #define FILE_LINE_ALIAS_ENTRY 4
141 /** This file_line defines an {action}. */
142 #define FILE_LINE_ACTION 5
144 /** This file_line specifies a URL pattern. */
145 #define FILE_LINE_URL 6
147 /** This file_line says {{settings}}. */
148 #define FILE_LINE_SETTINGS_HEADER 7
150 /** This file_line is in a {{settings}} block. */
151 #define FILE_LINE_SETTINGS_ENTRY 8
153 /** This file_line says {{description}}. */
154 #define FILE_LINE_DESCRIPTION_HEADER 9
156 /** This file_line is in a {{description}} block. */
157 #define FILE_LINE_DESCRIPTION_ENTRY 10
160 * Number of file modification time mismatches
161 * before the CGI editor gets turned off.
163 #define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
166 * A configuration file, in a format that can be edited and written back to
171 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
172 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
173 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
174 const char * version_str; /**< Last modification time, as a string. For CGI param. */
175 /**< Can be used in URL without using url_param(). */
176 unsigned version; /**< Last modification time - prevents chaos with
177 the browser's "back" button. Note that this is a
178 time_t cast to an unsigned. When comparing, always
179 cast the time_t to an unsigned, and *NOT* vice-versa.
180 This may lose the top few bits, but they're not
181 significant anyway. */
182 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
183 Note that changing this after the file has been
184 read in will cause a mess. */
185 struct file_line * parse_error; /**< On parse error, this is the offending line. */
186 const char * parse_error_text; /**< On parse error, this is the problem.
187 (Statically allocated) */
191 * Information about the filter types.
192 * Used for macro replacement in cgi_edit_actions_for_url.
194 struct filter_type_info
196 const int multi_action_index; /**< The multi action index as defined in project.h */
197 const char *macro_name; /**< Name of the macro that has to be replaced
198 with the prepared templates.
199 For example "content-filter-params" */
200 const char *type; /**< Name of the filter type,
201 for example "server-header-filter". */
202 /* XXX: check if these two can be combined. */
203 const char *disable_all_option; /**< Name of the catch-all radio option that has
204 to be checked or unchecked for this filter type. */
205 const char *disable_all_param; /**< Name of the parameter that causes all filters of
206 this type to be disabled. */
207 const char *abbr_type; /**< Abbreviation of the filter type, usually the
208 first or second character capitalized */
209 const char *anchor; /**< Anchor for the User Manual link,
210 for example "SERVER-HEADER-FILTER" */
213 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
214 static const struct filter_type_info filter_type_info[] =
218 "content-filter-params", "filter",
219 "filter-all", "filter_all",
223 ACTION_MULTI_CLIENT_HEADER_FILTER,
224 "client-header-filter-params", "client-header-filter",
225 "client-header-filter-all", "client_header_filter_all",
226 "C", "CLIENT-HEADER-FILTER"
229 ACTION_MULTI_SERVER_HEADER_FILTER,
230 "server-header-filter-params", "server-header-filter",
231 "server-header-filter-all", "server_header_filter_all",
232 "S", "SERVER-HEADER-FILTER"
235 ACTION_MULTI_CLIENT_HEADER_TAGGER,
236 "client-header-tagger-params", "client-header-tagger",
237 "client-header-tagger-all", "client_header_tagger_all",
238 "L", "CLIENT-HEADER-TAGGER"
241 ACTION_MULTI_SERVER_HEADER_TAGGER,
242 "server-header-tagger-params", "server-header-tagger",
243 "server-header-tagger-all", "server_header_tagger_all",
244 "E", "SERVER-HEADER-TAGGER"
246 #ifdef FEATURE_EXTERNAL_FILTERS
248 ACTION_MULTI_EXTERNAL_FILTER,
249 "external-content-filter-params", "external-filter",
250 "external-content-filter-all", "external_content_filter_all",
251 "E", "EXTERNAL-CONTENT-FILTER"
256 /* FIXME: Following non-static functions should be prototyped in .h or made static */
258 /* Functions to read and write arbitrary config files */
259 jb_err edit_read_file(struct client_state *csp,
260 const struct map *parameters,
262 struct editable_file **pfile);
263 jb_err edit_write_file(struct editable_file * file);
264 void edit_free_file(struct editable_file * file);
266 /* Functions to read and write actions files */
267 jb_err edit_parse_actions_file(struct editable_file * file);
268 jb_err edit_read_actions_file(struct client_state *csp,
269 struct http_response *rsp,
270 const struct map *parameters,
272 struct editable_file **pfile);
275 jb_err cgi_error_modified(struct client_state *csp,
276 struct http_response *rsp,
277 const char *filename);
278 jb_err cgi_error_parse(struct client_state *csp,
279 struct http_response *rsp,
280 struct editable_file *file);
281 jb_err cgi_error_file(struct client_state *csp,
282 struct http_response *rsp,
283 const char *filename);
284 jb_err cgi_error_file_read_only(struct client_state *csp,
285 struct http_response *rsp,
286 const char *filename);
288 /* Internal arbitrary config file support functions */
289 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
290 static void edit_free_file_lines(struct file_line * first_line);
292 /* Internal actions file support functions */
293 static int match_actions_file_header_line(const char * line, const char * name);
294 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
296 /* Internal parameter parsing functions */
297 static jb_err get_url_spec_param(struct client_state *csp,
298 const struct map *parameters,
303 /* Internal actionsfile <==> HTML conversion functions */
304 static jb_err map_radio(struct map * exports,
305 const char * optionname,
308 static jb_err actions_to_radio(struct map * exports,
309 const struct action_spec *action);
310 static jb_err actions_from_radio(const struct map * parameters,
311 struct action_spec *action);
314 static jb_err map_copy_parameter_html(struct map *out,
315 const struct map *in,
318 static jb_err get_file_name_param(struct client_state *csp,
319 const struct map *parameters,
320 const char *param_name,
321 const char **pfilename);
323 /* Internal convenience functions */
324 static char *section_target(const unsigned sectionid);
326 /*********************************************************************
328 * Function : section_target
330 * Description : Given an unsigned (section id) n, produce a dynamically
331 * allocated string of the form #l<n>, for use in link
334 * XXX: The hash should be moved into the templates
335 * to make this function more generic and render
336 * stringify() obsolete.
339 * 1 : sectionid = start line number of section
341 * Returns : String with link target, or NULL if out of
344 *********************************************************************/
345 static char *section_target(const unsigned sectionid)
349 snprintf(buf, sizeof(buf), "#l%u", sectionid);
355 /*********************************************************************
357 * Function : stringify
359 * Description : Convert a number into a dynamically allocated string.
362 * 1 : number = The number to convert.
364 * Returns : String with link target, or NULL if out of memory
366 *********************************************************************/
367 static char *stringify(const unsigned number)
371 snprintf(buf, sizeof(buf), "%u", number);
376 /*********************************************************************
378 * Function : map_copy_parameter_html
380 * Description : Copy a CGI parameter from one map to another, HTML
384 * 1 : out = target map
385 * 2 : in = source map
386 * 3 : name = name of cgi parameter to copy
388 * Returns : JB_ERR_OK on success
389 * JB_ERR_MEMORY on out-of-memory
390 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
393 *********************************************************************/
394 static jb_err map_copy_parameter_html(struct map *out,
395 const struct map *in,
405 value = lookup(in, name);
406 err = map(out, name, 1, html_encode(value), 0);
413 else if (*value == '\0')
415 return JB_ERR_CGI_PARAMS;
424 /*********************************************************************
426 * Function : cgi_edit_actions_url_form
428 * Description : CGI function that displays a form for
432 * 1 : csp = Current client state (buffers, headers, etc...)
433 * 2 : rsp = http_response data structure for output
434 * 3 : parameters = map of cgi parameters
437 * i : (action index) Identifies the file to edit
438 * v : (version) File's last-modified time
439 * p : (pattern) Line number of pattern to edit
441 * Returns : JB_ERR_OK on success
442 * JB_ERR_MEMORY on out-of-memory
443 * JB_ERR_CGI_PARAMS if the CGI parameters are not
444 * specified or not valid.
446 *********************************************************************/
447 jb_err cgi_edit_actions_url_form(struct client_state *csp,
448 struct http_response *rsp,
449 const struct map *parameters)
451 struct map * exports;
453 struct editable_file * file;
454 struct file_line * cur_line;
455 unsigned line_number;
456 unsigned section_start_line_number = 0;
463 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
465 return cgi_error_disabled(csp, rsp);
468 err = get_number_param(csp, parameters, "p", &patternid);
474 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
477 /* No filename specified, can't read file, modified, or out of memory. */
478 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
481 cur_line = file->lines;
483 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
485 if (cur_line->type == FILE_LINE_ACTION)
487 section_start_line_number = line_number;
489 cur_line = cur_line->next;
492 if ( (cur_line == NULL)
493 || (line_number != patternid)
495 || (cur_line->type != FILE_LINE_URL))
497 /* Invalid "patternid" parameter */
498 edit_free_file(file);
499 return JB_ERR_CGI_PARAMS;
502 if (NULL == (exports = default_exports(csp, NULL)))
504 edit_free_file(file);
505 return JB_ERR_MEMORY;
508 err = map(exports, "f", 1, stringify(file->identifier), 0);
509 if (!err) err = map(exports, "v", 1, file->version_str, 1);
510 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
511 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
512 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
514 edit_free_file(file);
522 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
526 /*********************************************************************
528 * Function : cgi_edit_actions_add_url_form
530 * Description : CGI function that displays a form for
534 * 1 : csp = Current client state (buffers, headers, etc...)
535 * 2 : rsp = http_response data structure for output
536 * 3 : parameters = map of cgi parameters
539 * f : (filename) Identifies the file to edit
540 * v : (version) File's last-modified time
541 * s : (section) Line number of section to edit
543 * Returns : JB_ERR_OK on success
544 * JB_ERR_MEMORY on out-of-memory
545 * JB_ERR_CGI_PARAMS if the CGI parameters are not
546 * specified or not valid.
548 *********************************************************************/
549 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
550 struct http_response *rsp,
551 const struct map *parameters)
560 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
562 return cgi_error_disabled(csp, rsp);
565 if (NULL == (exports = default_exports(csp, NULL)))
567 return JB_ERR_MEMORY;
570 err = map_copy_parameter_html(exports, parameters, "f");
571 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
572 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
580 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
584 /*********************************************************************
586 * Function : cgi_edit_actions_remove_url_form
588 * Description : CGI function that displays a form for
592 * 1 : csp = Current client state (buffers, headers, etc...)
593 * 2 : rsp = http_response data structure for output
594 * 3 : parameters = map of cgi parameters
597 * f : (number) The action file identifier.
598 * v : (version) File's last-modified time
599 * p : (pattern) Line number of pattern to edit
601 * Returns : JB_ERR_OK on success
602 * JB_ERR_MEMORY on out-of-memory
603 * JB_ERR_CGI_PARAMS if the CGI parameters are not
604 * specified or not valid.
606 *********************************************************************/
607 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
608 struct http_response *rsp,
609 const struct map *parameters)
611 struct map * exports;
613 struct editable_file * file;
614 struct file_line * cur_line;
615 unsigned line_number;
616 unsigned section_start_line_number = 0;
623 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
625 return cgi_error_disabled(csp, rsp);
628 err = get_number_param(csp, parameters, "p", &patternid);
634 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
637 /* No filename specified, can't read file, modified, or out of memory. */
638 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
641 cur_line = file->lines;
643 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
645 if (cur_line->type == FILE_LINE_ACTION)
647 section_start_line_number = line_number;
649 cur_line = cur_line->next;
652 if ( (cur_line == NULL)
653 || (line_number != patternid)
655 || (cur_line->type != FILE_LINE_URL))
657 /* Invalid "patternid" parameter */
658 edit_free_file(file);
659 return JB_ERR_CGI_PARAMS;
662 if (NULL == (exports = default_exports(csp, NULL)))
664 edit_free_file(file);
665 return JB_ERR_MEMORY;
668 err = map(exports, "f", 1, stringify(file->identifier), 0);
669 if (!err) err = map(exports, "v", 1, file->version_str, 1);
670 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
671 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
672 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
673 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
675 edit_free_file(file);
683 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
687 /*********************************************************************
689 * Function : edit_write_file
691 * Description : Write a complete file to disk.
694 * 1 : file = File to write.
696 * Returns : JB_ERR_OK on success
697 * JB_ERR_FILE on error writing to file.
698 * JB_ERR_MEMORY on out of memory
700 *********************************************************************/
701 jb_err edit_write_file(struct editable_file * file)
704 struct file_line * cur_line;
705 struct stat statbuf[1];
706 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
707 digits in time_t, assuming this is a 64-bit
708 machine, plus null terminator, plus one
712 assert(file->filename);
714 if (NULL == (fp = fopen(file->filename, "wb")))
719 cur_line = file->lines;
720 while (cur_line != NULL)
724 if (fputs(cur_line->raw, fp) < 0)
732 if (cur_line->prefix)
734 if (fputs(cur_line->prefix, fp) < 0)
740 if (cur_line->unprocessed)
743 if (NULL != strchr(cur_line->unprocessed, '#'))
745 /* Must quote '#' characters */
752 /* Count number of # characters, so we know length of output string */
753 src = cur_line->unprocessed;
754 while (NULL != (src = strchr(src, '#')))
761 /* Allocate new memory for string */
762 len = strlen(cur_line->unprocessed) + (size_t)numhash;
763 str = malloc_or_die(len + 1);
765 /* Copy string but quote hashes */
766 src = cur_line->unprocessed;
774 assert(numhash >= 0);
780 assert(numhash == 0);
781 assert(strlen(str) == len);
782 assert(str == dest - len);
783 assert(src - len <= cur_line->unprocessed);
785 if ((strlen(str) != len) || (numhash != 0))
788 * Escaping didn't work as expected, go spread the news.
789 * Only reached in non-debugging builds.
791 log_error(LOG_LEVEL_ERROR,
792 "Looks like hash escaping failed. %s might be corrupted now.",
796 if (fputs(str, fp) < 0)
807 /* Can write without quoting '#' characters. */
808 if (fputs(cur_line->unprocessed, fp) < 0)
814 if (fputs(NEWLINE(file->newline), fp) < 0)
822 /* FIXME: Write data from file->data->whatever */
826 cur_line = cur_line->next;
832 /* Update the version stamp in the file structure, since we just
833 * wrote to the file & changed it's date.
835 if (stat(file->filename, statbuf) < 0)
837 /* Error, probably file not found. */
840 file->version = (unsigned)statbuf->st_mtime;
842 /* Correct file->version_str */
843 freez(file->version_str);
844 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
845 version_buf[sizeof(version_buf)-1] = '\0';
846 file->version_str = strdup(version_buf);
847 if (version_buf == NULL)
849 return JB_ERR_MEMORY;
856 /*********************************************************************
858 * Function : edit_free_file
860 * Description : Free a complete file in memory.
863 * 1 : file = Data structure to free.
867 *********************************************************************/
868 void edit_free_file(struct editable_file * file)
872 /* Silently ignore NULL pointer */
876 edit_free_file_lines(file->lines);
877 freez(file->version_str);
879 file->parse_error_text = NULL; /* Statically allocated */
880 file->parse_error = NULL;
886 /*********************************************************************
888 * Function : edit_free_file_lines
890 * Description : Free an entire linked list of file lines.
893 * 1 : first_line = Data structure to free.
897 *********************************************************************/
898 static void edit_free_file_lines(struct file_line * first_line)
900 struct file_line * next_line;
902 while (first_line != NULL)
904 next_line = first_line->next;
905 first_line->next = NULL;
906 freez(first_line->raw);
907 freez(first_line->prefix);
908 freez(first_line->unprocessed);
909 switch(first_line->type)
911 case 0: /* special case if memory zeroed */
912 case FILE_LINE_UNPROCESSED:
913 case FILE_LINE_BLANK:
914 case FILE_LINE_ALIAS_HEADER:
915 case FILE_LINE_SETTINGS_HEADER:
916 case FILE_LINE_DESCRIPTION_HEADER:
917 case FILE_LINE_DESCRIPTION_ENTRY:
918 case FILE_LINE_ALIAS_ENTRY:
920 /* No data is stored for these */
923 case FILE_LINE_ACTION:
924 free_action(first_line->data.action);
927 case FILE_LINE_SETTINGS_ENTRY:
928 freez(first_line->data.setting.name);
929 freez(first_line->data.setting.svalue);
932 /* Should never happen */
936 first_line->type = 0; /* paranoia */
938 first_line = next_line;
943 /*********************************************************************
945 * Function : match_actions_file_header_line
947 * Description : Match an actions file {{header}} line
950 * 1 : line = String from file
951 * 2 : name = Header to match against
953 * Returns : 0 iff they match.
955 *********************************************************************/
956 static int match_actions_file_header_line(const char * line, const char * name)
964 if ((line[0] != '{') || (line[1] != '{'))
970 /* Look for optional whitespace */
971 while ((*line == ' ') || (*line == '\t'))
976 /* Look for the specified name (case-insensitive) */
978 if (0 != strncmpic(line, name, len))
984 /* Look for optional whitespace */
985 while ((*line == ' ') || (*line == '\t'))
990 /* Look for "}}" and end of string*/
991 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1001 /*********************************************************************
1003 * Function : match_actions_file_header_line
1005 * Description : Match an actions file {{header}} line
1008 * 1 : line = String from file. Must not start with
1009 * whitespace (else infinite loop!)
1010 * 2 : pname = Destination for name
1011 * 2 : pvalue = Destination for value
1013 * Returns : JB_ERR_OK on success
1014 * JB_ERR_MEMORY on out-of-memory
1015 * JB_ERR_PARSE if there's no "=" sign, or if there's
1016 * nothing before the "=" sign (but empty
1017 * values *after* the "=" sign are legal).
1019 *********************************************************************/
1020 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1022 const char * name_end;
1023 const char * value_start;
1029 assert(*line != ' ');
1030 assert(*line != '\t');
1035 value_start = strchr(line, '=');
1036 if ((value_start == NULL) || (value_start == line))
1038 return JB_ERR_PARSE;
1041 name_end = value_start - 1;
1043 /* Eat any whitespace before the '=' */
1044 while ((*name_end == ' ') || (*name_end == '\t'))
1047 * we already know we must have at least 1 non-ws char
1048 * at start of buf - no need to check
1053 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1054 *pname = malloc_or_die(name_len + 1);
1055 strncpy(*pname, line, name_len);
1056 (*pname)[name_len] = '\0';
1058 /* Eat any the whitespace after the '=' */
1060 while ((*value_start == ' ') || (*value_start == '\t'))
1065 if (NULL == (*pvalue = strdup(value_start)))
1069 return JB_ERR_MEMORY;
1076 /*********************************************************************
1078 * Function : edit_parse_actions_file
1080 * Description : Parse an actions file in memory.
1082 * Passed linked list must have the "data" member
1083 * zeroed, and must contain valid "next" and
1084 * "unprocessed" fields. The "raw" and "prefix"
1085 * fields are ignored, and "type" is just overwritten.
1087 * Note that on error the file may have been
1091 * 1 : file = Actions file to be parsed in-place.
1093 * Returns : JB_ERR_OK on success
1094 * JB_ERR_MEMORY on out-of-memory
1095 * JB_ERR_PARSE on error
1097 *********************************************************************/
1098 jb_err edit_parse_actions_file(struct editable_file * file)
1100 struct file_line * cur_line;
1102 const char * text; /* Text from a line */
1103 char * name; /* For lines of the form name=value */
1104 char * value; /* For lines of the form name=value */
1105 struct action_alias * alias_list = NULL;
1106 jb_err err = JB_ERR_OK;
1108 /* alias_list contains the aliases defined in this file.
1109 * It might be better to use the "file_line.data" fields
1110 * in the relavent places instead.
1113 cur_line = file->lines;
1115 /* A note about blank line support: Blank lines should only
1116 * ever occur as the last line in the file. This function
1117 * is more forgiving than that - FILE_LINE_BLANK can occur
1121 /* Skip leading blanks. Should only happen if file is
1122 * empty (which is valid, but pointless).
1124 while ((cur_line != NULL)
1125 && (cur_line->unprocessed[0] == '\0'))
1128 cur_line->type = FILE_LINE_BLANK;
1129 cur_line = cur_line->next;
1132 if ((cur_line != NULL)
1133 && (cur_line->unprocessed[0] != '{'))
1135 /* File doesn't start with a header */
1136 file->parse_error = cur_line;
1137 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1138 return JB_ERR_PARSE;
1141 if ((cur_line != NULL) && (0 ==
1142 match_actions_file_header_line(cur_line->unprocessed, "settings")))
1144 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1146 cur_line = cur_line->next;
1147 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1149 if (cur_line->unprocessed[0])
1151 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1153 err = split_line_on_equals(cur_line->unprocessed,
1154 &cur_line->data.setting.name,
1155 &cur_line->data.setting.svalue);
1156 if (err == JB_ERR_MEMORY)
1160 else if (err != JB_ERR_OK)
1162 /* Line does not contain a name=value pair */
1163 file->parse_error = cur_line;
1164 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1165 return JB_ERR_PARSE;
1170 cur_line->type = FILE_LINE_BLANK;
1172 cur_line = cur_line->next;
1176 if ((cur_line != NULL) && (0 ==
1177 match_actions_file_header_line(cur_line->unprocessed, "description")))
1179 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1181 cur_line = cur_line->next;
1182 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1184 if (cur_line->unprocessed[0])
1186 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1190 cur_line->type = FILE_LINE_BLANK;
1192 cur_line = cur_line->next;
1196 if ((cur_line != NULL) && (0 ==
1197 match_actions_file_header_line(cur_line->unprocessed, "alias")))
1199 cur_line->type = FILE_LINE_ALIAS_HEADER;
1201 cur_line = cur_line->next;
1202 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1204 if (cur_line->unprocessed[0])
1206 /* define an alias */
1207 struct action_alias * new_alias;
1209 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1211 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1212 if (err == JB_ERR_MEMORY)
1214 free_alias_list(alias_list);
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 {{alias}} line, but couldn't find one.";
1222 free_alias_list(alias_list);
1223 return JB_ERR_PARSE;
1226 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1231 free_alias_list(alias_list);
1232 return JB_ERR_MEMORY;
1235 err = get_actions(value, alias_list, new_alias->action);
1238 /* Invalid action or out of memory */
1242 free_alias_list(alias_list);
1243 if (err == JB_ERR_MEMORY)
1249 /* Line does not contain a name=value pair */
1250 file->parse_error = cur_line;
1251 file->parse_error_text = "This alias does not specify a valid set of actions.";
1252 return JB_ERR_PARSE;
1258 new_alias->name = name;
1261 new_alias->next = alias_list;
1262 alias_list = new_alias;
1266 cur_line->type = FILE_LINE_BLANK;
1268 cur_line = cur_line->next;
1272 /* Header done, process the main part of the file */
1273 while (cur_line != NULL)
1275 /* At this point, (cur_line->unprocessed[0] == '{') */
1276 assert(cur_line->unprocessed[0] == '{');
1277 text = cur_line->unprocessed + 1;
1278 len = strlen(text) - 1;
1279 if (text[len] != '}')
1281 /* No closing } on header */
1282 free_alias_list(alias_list);
1283 file->parse_error = cur_line;
1284 file->parse_error_text = "Headers starting with '{' must have a "
1285 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1286 "must close with two brackets ('}}').";
1287 return JB_ERR_PARSE;
1292 /* An invalid {{ header. */
1293 free_alias_list(alias_list);
1294 file->parse_error = cur_line;
1295 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1296 "Please remember that the system (two-bracket) headers must "
1297 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1298 "and must appear before any actions (one-bracket) headers. "
1299 "Also note that system headers may not be repeated.";
1300 return JB_ERR_PARSE;
1303 while ((*text == ' ') || (*text == '\t'))
1308 while ((len > (size_t)0)
1309 && ((text[len - 1] == ' ')
1310 || (text[len - 1] == '\t')))
1315 cur_line->type = FILE_LINE_ACTION;
1317 /* Remove {} and make copy */
1318 value = malloc_or_die(len + 1);
1319 strncpy(value, text, len);
1323 err = get_actions(value, alias_list, cur_line->data.action);
1326 /* Invalid action or out of memory */
1328 free_alias_list(alias_list);
1329 if (err == JB_ERR_MEMORY)
1335 /* Line does not contain a name=value pair */
1336 file->parse_error = cur_line;
1337 file->parse_error_text = "This header does not specify a valid set of actions.";
1338 return JB_ERR_PARSE;
1342 /* Done with string - it was clobbered anyway */
1345 /* Process next line */
1346 cur_line = cur_line->next;
1348 /* Loop processing URL patterns */
1349 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1351 if (cur_line->unprocessed[0])
1353 /* Could parse URL here, but this isn't currently needed */
1355 cur_line->type = FILE_LINE_URL;
1359 cur_line->type = FILE_LINE_BLANK;
1361 cur_line = cur_line->next;
1363 } /* End main while(cur_line != NULL) loop */
1365 free_alias_list(alias_list);
1371 /*********************************************************************
1373 * Function : edit_read_file_lines
1375 * Description : Read all the lines of a file into memory.
1376 * Handles whitespace, comments and line continuation.
1379 * 1 : fp = File to read from. On return, this will be
1380 * at EOF but it will not have been closed.
1381 * 2 : pfile = Destination for a linked list of file_lines.
1382 * Will be set to NULL on error.
1383 * 3 : newline = How to handle newlines.
1385 * Returns : JB_ERR_OK on success
1386 * JB_ERR_MEMORY on out-of-memory
1388 *********************************************************************/
1389 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1391 struct file_line * first_line; /* Keep for return value or to free */
1392 struct file_line * cur_line; /* Current line */
1393 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1401 cur_line = first_line = zalloc(sizeof(struct file_line));
1402 if (cur_line == NULL)
1404 return JB_ERR_MEMORY;
1407 cur_line->type = FILE_LINE_UNPROCESSED;
1409 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1412 /* Out of memory or empty file. */
1413 /* Note that empty file is not an error we propagate up */
1415 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1420 prev_line = cur_line;
1421 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1422 if (cur_line == NULL)
1425 edit_free_file_lines(first_line);
1426 return JB_ERR_MEMORY;
1429 cur_line->type = FILE_LINE_UNPROCESSED;
1431 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1432 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1435 edit_free_file_lines(first_line);
1436 return JB_ERR_MEMORY;
1440 while (rval != JB_ERR_FILE);
1444 /* We allocated one too many - free it */
1445 prev_line->next = NULL;
1448 *pfile = first_line;
1453 /*********************************************************************
1455 * Function : edit_read_file
1457 * Description : Read a complete file into memory.
1458 * Handles CGI parameter parsing. If requested, also
1459 * checks the file's modification timestamp.
1462 * 1 : csp = Current client state (buffers, headers, etc...)
1463 * 2 : parameters = map of cgi parameters.
1464 * 3 : require_version = true to check "ver" parameter.
1465 * 4 : pfile = Destination for the file. Will be set
1469 * f : The action file identifier.
1470 * ver : (Only if require_version is nonzero)
1471 * Timestamp of the actions file. If wrong, this
1472 * function fails with JB_ERR_MODIFIED.
1474 * Returns : JB_ERR_OK on success
1475 * JB_ERR_MEMORY on out-of-memory
1476 * JB_ERR_CGI_PARAMS if "filename" was not specified
1478 * JB_ERR_FILE if the file cannot be opened or
1480 * JB_ERR_MODIFIED if version checking was requested and
1481 * failed - the file was modified outside
1482 * of this CGI editor instance.
1484 *********************************************************************/
1485 jb_err edit_read_file(struct client_state *csp,
1486 const struct map *parameters,
1487 int require_version,
1488 struct editable_file **pfile)
1490 struct file_line * lines;
1493 const char *filename = NULL;
1494 struct editable_file * file;
1495 unsigned version = 0;
1496 struct stat statbuf[1];
1497 char version_buf[22];
1498 int newline = NEWLINE_UNKNOWN;
1507 err = get_number_param(csp, parameters, "f", &i);
1508 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1510 filename = csp->config->actions_file[i];
1512 else if (JB_ERR_CGI_PARAMS == err)
1515 * Probably an old-school URL like
1516 * http://config.privoxy.org/edit-actions-list?f=default
1518 get_file_name_param(csp, parameters, "f", &filename);
1521 if (NULL == filename || stat(filename, statbuf) < 0)
1523 /* Error, probably file not found. */
1526 version = (unsigned) statbuf->st_mtime;
1528 if (require_version)
1530 unsigned specified_version;
1531 err = get_number_param(csp, parameters, "v", &specified_version);
1537 if (version != specified_version)
1539 return JB_ERR_MODIFIED;
1543 if (NULL == (fp = fopen(filename,"rb")))
1548 err = edit_read_file_lines(fp, &lines, &newline);
1557 file = (struct editable_file *) zalloc(sizeof(*file));
1560 edit_free_file_lines(lines);
1564 file->lines = lines;
1565 file->newline = newline;
1566 file->filename = filename;
1567 file->version = version;
1568 file->identifier = i;
1570 /* Correct file->version_str */
1571 freez(file->version_str);
1572 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1573 version_buf[sizeof(version_buf)-1] = '\0';
1574 file->version_str = strdup_or_die(version_buf);
1581 /*********************************************************************
1583 * Function : edit_read_actions_file
1585 * Description : Read a complete actions file into memory.
1586 * Handles CGI parameter parsing. If requested, also
1587 * checks the file's modification timestamp.
1589 * If this function detects an error in the categories
1590 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1591 * then it handles it by filling in the specified
1592 * response structure and returning JB_ERR_FILE.
1595 * 1 : csp = Current client state (buffers, headers, etc...)
1596 * 2 : rsp = HTTP response. Only filled in on error.
1597 * 2 : parameters = map of cgi parameters.
1598 * 3 : require_version = true to check "ver" parameter.
1599 * 4 : pfile = Destination for the file. Will be set
1603 * f : The actions file identifier.
1604 * ver : (Only if require_version is nonzero)
1605 * Timestamp of the actions file. If wrong, this
1606 * function fails with JB_ERR_MODIFIED.
1608 * Returns : JB_ERR_OK on success
1609 * JB_ERR_MEMORY on out-of-memory
1610 * JB_ERR_CGI_PARAMS if "filename" was not specified
1612 * JB_ERR_FILE if the file does not contain valid data,
1613 * or if file cannot be opened or
1614 * contains no data, or if version
1615 * checking was requested and failed.
1617 *********************************************************************/
1618 jb_err edit_read_actions_file(struct client_state *csp,
1619 struct http_response *rsp,
1620 const struct map *parameters,
1621 int require_version,
1622 struct editable_file **pfile)
1625 struct editable_file *file;
1626 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
1634 err = edit_read_file(csp, parameters, require_version, &file);
1637 /* Try to handle if possible */
1638 if (err == JB_ERR_FILE)
1640 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1642 else if (err == JB_ERR_MODIFIED)
1644 assert(require_version);
1645 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1646 log_error(LOG_LEVEL_ERROR,
1647 "Blocking CGI edit request due to modification time mismatch.");
1648 if (acceptable_failures > 0)
1650 log_error(LOG_LEVEL_INFO,
1651 "The CGI editor will be turned off after another %d mismatche(s).",
1652 acceptable_failures);
1653 acceptable_failures--;
1657 log_error(LOG_LEVEL_INFO,
1658 "Timestamp mismatch limit reached, turning CGI editor off. "
1659 "Reload the configuration file to re-enable it.");
1660 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
1663 if (err == JB_ERR_OK)
1666 * Signal to higher-level CGI code that there was a problem but we
1667 * handled it, they should just return JB_ERR_OK.
1674 err = edit_parse_actions_file(file);
1677 if (err == JB_ERR_PARSE)
1679 err = cgi_error_parse(csp, rsp, file);
1680 if (err == JB_ERR_OK)
1683 * Signal to higher-level CGI code that there was a problem but we
1684 * handled it, they should just return JB_ERR_OK.
1689 edit_free_file(file);
1698 /*********************************************************************
1700 * Function : get_file_name_param
1702 * Description : Get the name of the file to edit from the parameters
1703 * passed to a CGI function using the old syntax.
1704 * This function handles security checks and only
1705 * accepts files that Privoxy already knows.
1708 * 1 : csp = Current client state (buffers, headers, etc...)
1709 * 2 : parameters = map of cgi parameters
1710 * 3 : param_name = The name of the parameter to read
1711 * 4 : pfilename = pointer to the filename in
1712 * csp->config->actions_file[] if found. Set to NULL on error.
1714 * Returns : JB_ERR_OK on success
1715 * JB_ERR_MEMORY on out-of-memory
1716 * JB_ERR_CGI_PARAMS if "filename" was not specified
1719 *********************************************************************/
1720 static jb_err get_file_name_param(struct client_state *csp,
1721 const struct map *parameters,
1722 const char *param_name,
1723 const char **pfilename)
1726 const char suffix[] = ".action";
1741 param = lookup(parameters, param_name);
1744 return JB_ERR_CGI_PARAMS;
1747 len = strlen(param);
1748 if (len >= FILENAME_MAX)
1751 return JB_ERR_CGI_PARAMS;
1755 * Check every character to see if it's legal.
1756 * Totally unnecessary but we do it anyway.
1759 while ((ch = *s++) != '\0')
1761 if ( ((ch < 'A') || (ch > 'Z'))
1762 && ((ch < 'a') || (ch > 'z'))
1763 && ((ch < '0') || (ch > '9'))
1767 /* Probable hack attempt. */
1768 return JB_ERR_CGI_PARAMS;
1772 /* Append extension */
1773 name_size = len + strlen(suffix) + 1;
1774 name = malloc_or_die(name_size);
1775 strlcpy(name, param, name_size);
1776 strlcat(name, suffix, name_size);
1779 fullpath = make_path(csp->config->confdir, name);
1782 if (fullpath == NULL)
1784 return JB_ERR_MEMORY;
1787 /* Check if the file is known */
1788 for (i = 0; i < MAX_AF_FILES; i++)
1790 if (NULL != csp->config->actions_file[i] &&
1791 !strcmp(fullpath, csp->config->actions_file[i]))
1794 *pfilename = csp->config->actions_file[i];
1802 return JB_ERR_CGI_PARAMS;
1806 /*********************************************************************
1808 * Function : get_url_spec_param
1810 * Description : Get a URL pattern from the parameters
1811 * passed to a CGI function. Removes leading/trailing
1812 * spaces and validates it.
1815 * 1 : csp = Current client state (buffers, headers, etc...)
1816 * 2 : parameters = map of cgi parameters
1817 * 3 : name = Name of CGI parameter to read
1818 * 4 : pvalue = destination for value. Will be malloc()'d.
1819 * Set to NULL on error.
1821 * Returns : JB_ERR_OK on success
1822 * JB_ERR_MEMORY on out-of-memory
1823 * JB_ERR_CGI_PARAMS if the parameter was not specified
1826 *********************************************************************/
1827 static jb_err get_url_spec_param(struct client_state *csp,
1828 const struct map *parameters,
1832 const char *orig_param;
1835 struct pattern_spec compiled[1];
1845 orig_param = lookup(parameters, name);
1848 return JB_ERR_CGI_PARAMS;
1851 /* Copy and trim whitespace */
1852 param = strdup(orig_param);
1855 return JB_ERR_MEMORY;
1859 /* Must be non-empty, and can't allow 1st character to be '{' */
1860 if (param[0] == '\0' || param[0] == '{')
1863 return JB_ERR_CGI_PARAMS;
1866 /* Check for embedded newlines */
1867 for (s = param; *s != '\0'; s++)
1869 if ((*s == '\r') || (*s == '\n'))
1872 return JB_ERR_CGI_PARAMS;
1876 /* Check that regex is valid */
1881 return JB_ERR_MEMORY;
1883 err = create_pattern_spec(compiled, s);
1888 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1890 free_pattern_spec(compiled);
1892 if (param[strlen(param) - 1] == '\\')
1895 * Must protect trailing '\\' from becoming line continuation character.
1896 * Two methods: 1) If it's a domain only, add a trailing '/'.
1897 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1899 if (strchr(param, '/') == NULL)
1901 err = string_append(¶m, "/");
1905 err = string_append(¶m, "(?:)");
1912 /* Check that the modified regex is valid */
1917 return JB_ERR_MEMORY;
1919 err = create_pattern_spec(compiled, s);
1924 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1926 free_pattern_spec(compiled);
1933 /*********************************************************************
1935 * Function : map_radio
1937 * Description : Map a set of radio button values. E.g. if you have
1938 * 3 radio buttons, declare them as:
1939 * <option type="radio" name="xyz" @xyz-a@>
1940 * <option type="radio" name="xyz" @xyz-b@>
1941 * <option type="radio" name="xyz" @xyz-c@>
1942 * Then map one of the @xyz-?@ variables to "checked"
1943 * and all the others to empty by calling:
1944 * map_radio(exports, "xyz", "abc", sel)
1945 * Where 'sel' is 'a', 'b', or 'c'.
1948 * 1 : exports = Exports map to modify.
1949 * 2 : optionname = name for map
1950 * 3 : values = null-terminated list of values;
1951 * 4 : value = Selected value.
1953 * CGI Parameters : None
1955 * Returns : JB_ERR_OK on success
1956 * JB_ERR_MEMORY on out-of-memory
1958 *********************************************************************/
1959 static jb_err map_radio(struct map * exports,
1960 const char * optionname,
1961 const char * values,
1967 const size_t len = strlen(optionname);
1968 const size_t buf_size = len + 3;
1974 buf = malloc_or_die(buf_size);
1976 strlcpy(buf, optionname, buf_size);
1978 /* XXX: this looks ... interesting */
1983 while ((c = *values++) != '\0')
1988 if (map(exports, buf, 1, "", 1))
1990 return JB_ERR_MEMORY;
1996 return map(exports, buf, 0, "checked", 1);
2000 /*********************************************************************
2002 * Function : cgi_error_modified
2004 * Description : CGI function that is called when a file is modified
2005 * outside the CGI editor.
2008 * 1 : csp = Current client state (buffers, headers, etc...)
2009 * 2 : rsp = http_response data structure for output
2010 * 3 : filename = The file that was modified.
2012 * CGI Parameters : none
2014 * Returns : JB_ERR_OK on success
2015 * JB_ERR_MEMORY on out-of-memory error.
2017 *********************************************************************/
2018 jb_err cgi_error_modified(struct client_state *csp,
2019 struct http_response *rsp,
2020 const char *filename)
2022 struct map *exports;
2029 if (NULL == (exports = default_exports(csp, NULL)))
2031 return JB_ERR_MEMORY;
2034 err = map(exports, "f", 1, html_encode(filename), 0);
2041 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2045 /*********************************************************************
2047 * Function : cgi_error_parse
2049 * Description : CGI function that is called when a file cannot
2050 * be parsed by the CGI editor.
2053 * 1 : csp = Current client state (buffers, headers, etc...)
2054 * 2 : rsp = http_response data structure for output
2055 * 3 : file = The file that was modified.
2057 * CGI Parameters : none
2059 * Returns : JB_ERR_OK on success
2060 * JB_ERR_MEMORY on out-of-memory error.
2062 *********************************************************************/
2063 jb_err cgi_error_parse(struct client_state *csp,
2064 struct http_response *rsp,
2065 struct editable_file *file)
2067 struct map *exports;
2069 struct file_line *cur_line;
2075 if (NULL == (exports = default_exports(csp, NULL)))
2077 return JB_ERR_MEMORY;
2080 err = map(exports, "f", 1, stringify(file->identifier), 0);
2081 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2083 cur_line = file->parse_error;
2086 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2087 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2095 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2099 /*********************************************************************
2101 * Function : cgi_error_file
2103 * Description : CGI function that is called when a file cannot be
2104 * opened by the CGI editor.
2107 * 1 : csp = Current client state (buffers, headers, etc...)
2108 * 2 : rsp = http_response data structure for output
2109 * 3 : filename = The file that was modified.
2111 * CGI Parameters : none
2113 * Returns : JB_ERR_OK on success
2114 * JB_ERR_MEMORY on out-of-memory error.
2116 *********************************************************************/
2117 jb_err cgi_error_file(struct client_state *csp,
2118 struct http_response *rsp,
2119 const char *filename)
2121 struct map *exports;
2128 if (NULL == (exports = default_exports(csp, NULL)))
2130 return JB_ERR_MEMORY;
2133 err = map(exports, "f", 1, html_encode(filename), 0);
2140 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2144 /*********************************************************************
2146 * Function : cgi_error_file_read_only
2148 * Description : CGI function that is called when a file cannot be
2149 * opened for writing by the CGI editor.
2152 * 1 : csp = Current client state (buffers, headers, etc...)
2153 * 2 : rsp = http_response data structure for output
2154 * 3 : filename = The file that we can't write to
2156 * CGI Parameters : none
2158 * Returns : JB_ERR_OK on success
2159 * JB_ERR_MEMORY on out-of-memory error.
2161 *********************************************************************/
2162 jb_err cgi_error_file_read_only(struct client_state *csp,
2163 struct http_response *rsp,
2164 const char *filename)
2166 struct map *exports;
2173 if (NULL == (exports = default_exports(csp, NULL)))
2175 return JB_ERR_MEMORY;
2178 err = map(exports, "f", 1, html_encode(filename), 0);
2185 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2189 /*********************************************************************
2191 * Function : cgi_edit_actions
2193 * Description : CGI function that allows the user to choose which
2194 * actions file to edit.
2197 * 1 : csp = Current client state (buffers, headers, etc...)
2198 * 2 : rsp = http_response data structure for output
2199 * 3 : parameters = map of cgi parameters
2201 * CGI Parameters : None
2203 * Returns : JB_ERR_OK on success
2204 * JB_ERR_MEMORY on out-of-memory error
2206 *********************************************************************/
2207 jb_err cgi_edit_actions(struct client_state *csp,
2208 struct http_response *rsp,
2209 const struct map *parameters)
2213 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2215 return cgi_error_disabled(csp, rsp);
2218 /* FIXME: Incomplete */
2220 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2225 /*********************************************************************
2227 * Function : cgi_edit_actions_list
2229 * Description : CGI function that edits the actions list.
2230 * FIXME: This function shouldn't FATAL ever.
2231 * FIXME: This function doesn't check the retval of map()
2233 * 1 : csp = Current client state (buffers, headers, etc...)
2234 * 2 : rsp = http_response data structure for output
2235 * 3 : parameters = map of cgi parameters
2237 * CGI Parameters : filename
2239 * Returns : JB_ERR_OK on success
2240 * JB_ERR_MEMORY on out-of-memory
2241 * JB_ERR_FILE if the file cannot be opened or
2243 * JB_ERR_CGI_PARAMS if "filename" was not specified
2246 *********************************************************************/
2247 jb_err cgi_edit_actions_list(struct client_state *csp,
2248 struct http_response *rsp,
2249 const struct map *parameters)
2251 char * section_template;
2252 char * url_template;
2257 struct map * exports;
2258 struct map * section_exports;
2259 struct map * url_exports;
2260 struct editable_file * file;
2261 struct file_line * cur_line;
2262 unsigned line_number = 0;
2263 unsigned prev_section_line_number = ((unsigned) (-1));
2265 struct file_list * fl;
2266 struct url_actions * b;
2267 char * buttons = NULL;
2270 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2272 return cgi_error_disabled(csp, rsp);
2275 if (NULL == (exports = default_exports(csp, NULL)))
2277 return JB_ERR_MEMORY;
2280 /* Load actions file */
2281 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2284 /* No filename specified, can't read file, or out of memory. */
2286 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2289 /* Find start of actions in file */
2290 cur_line = file->lines;
2292 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2294 cur_line = cur_line->next;
2299 * Conventional actions files should have a match all block
2301 * cur_line = {...global actions...}
2302 * cur_line->next = /
2303 * cur_line->next->next = {...actions...} or EOF
2305 if ( (cur_line != NULL)
2306 && (cur_line->type == FILE_LINE_ACTION)
2307 && (cur_line->next != NULL)
2308 && (cur_line->next->type == FILE_LINE_URL)
2309 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2310 && ( (cur_line->next->next == NULL)
2311 || (cur_line->next->next->type != FILE_LINE_URL)
2315 * Generate string with buttons to set actions for "/" to
2316 * any predefined set of actions (named standard.*, probably
2317 * residing in standard.action).
2320 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2323 edit_free_file(file);
2325 if (err == JB_ERR_FILE)
2327 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2332 err = template_fill(§ion_template, exports);
2335 edit_free_file(file);
2340 buttons = strdup("");
2341 for (i = 0; i < MAX_AF_FILES; i++)
2343 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2345 for (b = b->next; NULL != b; b = b->next)
2347 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2349 if (err || (NULL == (section_exports = new_map())))
2352 free(section_template);
2353 edit_free_file(file);
2355 return JB_ERR_MEMORY;
2358 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2360 if (err || (NULL == (s = strdup(section_template))))
2362 free_map(section_exports);
2364 free(section_template);
2365 edit_free_file(file);
2367 return JB_ERR_MEMORY;
2370 if (!err) err = template_fill(&s, section_exports);
2371 free_map(section_exports);
2372 if (!err) err = string_join(&buttons, s);
2377 freez(section_template);
2378 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2381 * Conventional actions file, supply extra editing help.
2382 * (e.g. don't allow them to make it an unconventional one).
2384 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2386 snprintf(buf, sizeof(buf), "%u", line_number);
2387 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2388 snprintf(buf, sizeof(buf), "%u", line_number + 2);
2389 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2390 if (!err) err = map(exports, "all-urls-actions", 1,
2391 actions_to_html(csp, cur_line->data.action), 0);
2393 /* Skip the 2 lines */
2394 cur_line = cur_line->next->next;
2398 * Note that prev_section_line_number is NOT set here.
2399 * This is deliberate and not a bug. It stops a "Move up"
2400 * option appearing on the next section. Clicking "Move
2401 * up" would make the actions file unconventional, which
2402 * we don't want, so we hide this option.
2408 * Non-standard actions file - does not begin with
2409 * the "All URLs" section.
2411 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2414 /* Set up global exports */
2416 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2417 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2418 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2420 /* Discourage private additions to default.action */
2422 if (!err) err = map_conditional(exports, "default-action",
2423 (strstr("default.action", file->filename) != NULL));
2426 edit_free_file(file);
2431 /* Should do all global exports above this point */
2433 /* Load templates */
2435 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2438 edit_free_file(file);
2440 if (err == JB_ERR_FILE)
2442 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2447 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2450 free(section_template);
2451 edit_free_file(file);
2453 if (err == JB_ERR_FILE)
2455 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2460 err = template_fill(§ion_template, exports);
2464 edit_free_file(file);
2469 err = template_fill(&url_template, exports);
2472 free(section_template);
2473 edit_free_file(file);
2478 if (NULL == (sections = strdup("")))
2480 free(section_template);
2482 edit_free_file(file);
2484 return JB_ERR_MEMORY;
2487 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2489 if (NULL == (section_exports = new_map()))
2492 free(section_template);
2494 edit_free_file(file);
2496 return JB_ERR_MEMORY;
2499 snprintf(buf, sizeof(buf), "%u", line_number);
2500 err = map(section_exports, "s", 1, buf, 1);
2501 if (!err) err = map(section_exports, "actions", 1,
2502 actions_to_html(csp, cur_line->data.action), 0);
2505 && (cur_line->next != NULL)
2506 && (cur_line->next->type == FILE_LINE_URL))
2508 /* This section contains at least one URL, don't allow delete */
2509 err = map_block_killer(section_exports, "empty-section");
2513 if (!err) err = map_block_keep(section_exports, "empty-section");
2516 if (prev_section_line_number != ((unsigned)(-1)))
2518 /* Not last section */
2519 snprintf(buf, sizeof(buf), "%u", prev_section_line_number);
2520 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2521 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2526 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2528 prev_section_line_number = line_number;
2533 free(section_template);
2535 edit_free_file(file);
2537 free_map(section_exports);
2541 /* Should do all section-specific exports above this point */
2543 if (NULL == (urls = strdup("")))
2546 free(section_template);
2548 edit_free_file(file);
2550 free_map(section_exports);
2551 return JB_ERR_MEMORY;
2556 cur_line = cur_line->next;
2559 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2561 if (NULL == (url_exports = new_map()))
2565 free(section_template);
2567 edit_free_file(file);
2569 free_map(section_exports);
2570 return JB_ERR_MEMORY;
2573 snprintf(buf, sizeof(buf), "%u", line_number);
2574 err = map(url_exports, "p", 1, buf, 1);
2576 snprintf(buf, sizeof(buf), "%d", url_1_2);
2577 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2579 if (!err) err = map(url_exports, "url-html", 1,
2580 html_encode(cur_line->unprocessed), 0);
2581 if (!err) err = map(url_exports, "url", 1,
2582 url_encode(cur_line->unprocessed), 0);
2588 free(section_template);
2590 edit_free_file(file);
2592 free_map(section_exports);
2593 free_map(url_exports);
2597 if (NULL == (s = strdup(url_template)))
2601 free(section_template);
2603 edit_free_file(file);
2605 free_map(section_exports);
2606 free_map(url_exports);
2607 return JB_ERR_MEMORY;
2610 err = template_fill(&s, section_exports);
2611 if (!err) err = template_fill(&s, url_exports);
2612 if (!err) err = string_append(&urls, s);
2614 free_map(url_exports);
2621 free(section_template);
2623 edit_free_file(file);
2625 free_map(section_exports);
2629 url_1_2 = 3 - url_1_2;
2631 cur_line = cur_line->next;
2635 err = map(section_exports, "urls", 1, urls, 0);
2637 /* Could also do section-specific exports here, but it wouldn't be as fast */
2639 snprintf(buf, sizeof(buf), "%u", line_number);
2640 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2642 if ((cur_line != NULL)
2643 && (cur_line->type == FILE_LINE_ACTION))
2645 /* Not last section */
2646 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2651 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2657 free(section_template);
2659 edit_free_file(file);
2661 free_map(section_exports);
2665 if (NULL == (s = strdup(section_template)))
2668 free(section_template);
2670 edit_free_file(file);
2672 free_map(section_exports);
2673 return JB_ERR_MEMORY;
2676 err = template_fill(&s, section_exports);
2677 if (!err) err = string_append(§ions, s);
2680 free_map(section_exports);
2685 free(section_template);
2687 edit_free_file(file);
2693 edit_free_file(file);
2694 free(section_template);
2697 err = map(exports, "sections", 1, sections, 0);
2704 /* Could also do global exports here, but it wouldn't be as fast */
2706 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2710 /*********************************************************************
2712 * Function : cgi_edit_actions_for_url
2714 * Description : CGI function that edits the Actions list.
2717 * 1 : csp = Current client state (buffers, headers, etc...)
2718 * 2 : rsp = http_response data structure for output
2719 * 3 : parameters = map of cgi parameters
2721 * CGI Parameters : None
2723 * Returns : JB_ERR_OK on success
2724 * JB_ERR_MEMORY on out-of-memory
2725 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2726 * specified or not valid.
2728 *********************************************************************/
2729 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2730 struct http_response *rsp,
2731 const struct map *parameters)
2733 struct map * exports;
2735 struct editable_file * file;
2736 struct file_line * cur_line;
2737 unsigned line_number;
2739 struct re_filterfile_spec *filter_group;
2740 int i, have_filters = 0;
2742 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2744 return cgi_error_disabled(csp, rsp);
2747 err = get_number_param(csp, parameters, "s", §ionid);
2753 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2756 /* No filename specified, can't read file, modified, or out of memory. */
2757 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2760 cur_line = file->lines;
2762 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2764 cur_line = cur_line->next;
2767 if ( (cur_line == NULL)
2768 || (line_number != sectionid)
2770 || (cur_line->type != FILE_LINE_ACTION))
2772 /* Invalid "sectionid" parameter */
2773 edit_free_file(file);
2774 return JB_ERR_CGI_PARAMS;
2777 if (NULL == (exports = default_exports(csp, NULL)))
2779 edit_free_file(file);
2780 return JB_ERR_MEMORY;
2783 err = map(exports, "f", 1, stringify(file->identifier), 0);
2784 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2785 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2787 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2790 * XXX: Some browsers (at least IE6 and IE7) have an artificial URL
2791 * length limitation and ignore clicks on the Submit buttons if
2792 * the resulting GET URL would be longer than their limit.
2794 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
2795 * reached this limit and action editing stopped working in these
2796 * browsers (BR #1570678).
2798 * The config option split-large-forms works around this browser
2799 * bug (HTTP has no URL length limitation) by deviding the action
2800 * list form into multiple smaller ones. It means the URLs are shorter
2801 * and work in broken browsers as well, but the user can no longer change
2802 * all actions with one submit.
2804 * A better solution would be to switch to POST requests,
2805 * but this will do for now.
2807 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
2809 /* Generate multiple smaller form by killing the big one. */
2810 err = map_block_killer(exports, "one-form-only");
2814 /* Default: Generate one large form by killing the smaller ones. */
2815 err = map_block_killer(exports, "multiple-forms");
2818 for (i = 0; i < MAX_AF_FILES; i++)
2820 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2822 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
2828 #ifndef FEATURE_EXTERNAL_FILTERS
2829 if (!err) err = map_block_killer(exports, "external-content-filters");
2834 edit_free_file(file);
2839 if (0 == have_filters)
2841 err = map(exports, "filter-params", 1, "", 1);
2846 * List available filters and their settings.
2848 char *filter_template;
2849 int filter_identifier = 0;
2850 char *prepared_templates[MAX_FILTER_TYPES];
2852 for (i = 0; i < MAX_FILTER_TYPES; i++)
2854 prepared_templates[i] = strdup("");
2857 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2860 edit_free_file(file);
2862 if (err == JB_ERR_FILE)
2864 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2869 err = template_fill(&filter_template, exports);
2871 for (i = 0; i < MAX_AF_FILES; i++)
2873 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2875 filter_group = csp->rlist[i]->f;
2876 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2878 char current_mode = 'x';
2880 struct list_entry *filter_name;
2881 struct map *line_exports;
2882 const int type = filter_group->type;
2883 const int multi_action_index = filter_type_info[type].multi_action_index;
2885 assert(type < MAX_FILTER_TYPES);
2887 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
2888 while ((filter_name != NULL)
2889 && (0 != strcmp(filter_group->name, filter_name->str)))
2891 filter_name = filter_name->next;
2894 if (filter_name != NULL)
2900 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
2901 while ((filter_name != NULL)
2902 && (0 != strcmp(filter_group->name, filter_name->str)))
2904 filter_name = filter_name->next;
2906 if (filter_name != NULL)
2912 /* Generate a unique serial number */
2913 snprintf(number, sizeof(number), "%x", filter_identifier++);
2914 number[sizeof(number) - 1] = '\0';
2916 line_exports = new_map();
2917 if (line_exports == NULL)
2919 err = JB_ERR_MEMORY;
2925 if (!err) err = map(line_exports, "index", 1, number, 1);
2926 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2927 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2928 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2929 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
2930 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
2931 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
2935 filter_line = strdup(filter_template);
2936 if (filter_line == NULL) err = JB_ERR_MEMORY;
2938 if (!err) err = template_fill(&filter_line, line_exports);
2939 string_join(&prepared_templates[type], filter_line);
2941 free_map(line_exports);
2946 freez(filter_template);
2948 /* Replace all filter macros with the aggregated templates */
2949 for (i = 0; i < MAX_FILTER_TYPES; i++)
2952 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
2957 /* Free aggregated templates */
2958 for (i = 0; i < MAX_FILTER_TYPES; i++)
2960 freez(prepared_templates[i]);
2965 /* Check or uncheck the "disable all of this type" radio buttons. */
2966 for (i = 0; i < MAX_FILTER_TYPES; i++)
2968 const int a = filter_type_info[i].multi_action_index;
2969 const int disable_all = cur_line->data.action->multi_remove_all[a];
2971 err = map_radio(exports, filter_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
2974 edit_free_file(file);
2982 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2986 /*********************************************************************
2988 * Function : cgi_edit_actions_submit
2990 * Description : CGI function that actually edits the Actions list.
2993 * 1 : csp = Current client state (buffers, headers, etc...)
2994 * 2 : rsp = http_response data structure for output
2995 * 3 : parameters = map of cgi parameters
2997 * CGI Parameters : None
2999 * Returns : JB_ERR_OK on success
3000 * JB_ERR_MEMORY on out-of-memory
3001 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3002 * specified or not valid.
3004 *********************************************************************/
3005 jb_err cgi_edit_actions_submit(struct client_state *csp,
3006 struct http_response *rsp,
3007 const struct map *parameters)
3012 size_t newtext_size;
3014 struct editable_file * file;
3015 struct file_line * cur_line;
3016 unsigned line_number;
3019 int filter_identifier;
3021 const char * action_set_name;
3022 struct file_list * fl;
3023 struct url_actions * b;
3025 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3027 return cgi_error_disabled(csp, rsp);
3030 err = get_number_param(csp, parameters, "s", §ionid);
3036 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3039 /* No filename specified, can't read file, modified, or out of memory. */
3040 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3043 cur_line = file->lines;
3045 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3047 cur_line = cur_line->next;
3050 if ( (cur_line == NULL)
3051 || (line_number != sectionid)
3053 || (cur_line->type != FILE_LINE_ACTION))
3055 /* Invalid "sectionid" parameter */
3056 edit_free_file(file);
3057 return JB_ERR_CGI_PARAMS;
3060 get_string_param(parameters, "p", &action_set_name);
3061 if (action_set_name != NULL)
3063 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3065 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3067 for (b = b->next; NULL != b; b = b->next)
3069 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3071 copy_action(cur_line->data.action, b->action);
3077 edit_free_file(file);
3078 return JB_ERR_CGI_PARAMS;
3084 err = actions_from_radio(parameters, cur_line->data.action);
3090 edit_free_file(file);
3094 /* Check the "disable all of this type" parameters. */
3095 for (i = 0; i < MAX_FILTER_TYPES; i++)
3097 const int multi_action_index = filter_type_info[i].multi_action_index;
3098 const char ch = get_char_param(parameters, filter_type_info[i].disable_all_param);
3102 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3103 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3104 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3108 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3112 for (filter_identifier = 0; !err; filter_identifier++)
3119 * Filter state. Valid states are: 'Y' (active),
3120 * 'N' (inactive) and 'X' (no change).
3124 * Abbreviated filter type. Valid types are: 'F' (content filter),
3125 * 'S' (server-header filter) and 'C' (client-header filter).
3127 int multi_action_index = 0;
3129 /* Generate the keys */
3130 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3131 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3132 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3133 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3134 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3136 err = get_string_param(parameters, key_name, &name);
3145 type = get_char_param(parameters, key_type);
3149 multi_action_index = ACTION_MULTI_FILTER;
3152 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3155 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3158 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3161 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3164 log_error(LOG_LEVEL_ERROR,
3165 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3168 assert(multi_action_index);
3170 value = get_char_param(parameters, key_value);
3173 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3174 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3175 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3177 else if (value == 'N')
3179 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3180 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3182 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3183 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3186 else if (value == 'X')
3188 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3189 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3196 edit_free_file(file);
3200 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3203 edit_free_file(file);
3204 return JB_ERR_MEMORY;
3207 len = strlen(actiontext);
3211 * Empty action - must special-case this.
3212 * Simply setting len to 1 is sufficient...
3217 newtext_size = len + 2;
3218 newtext = malloc_or_die(newtext_size);
3219 strlcpy(newtext, actiontext, newtext_size);
3223 newtext[len + 1] = '\0';
3225 freez(cur_line->raw);
3226 freez(cur_line->unprocessed);
3227 cur_line->unprocessed = newtext;
3229 err = edit_write_file(file);
3232 /* Error writing file */
3233 if (err == JB_ERR_FILE)
3235 /* Read-only file. */
3236 err = cgi_error_file_read_only(csp, rsp, file->filename);
3238 edit_free_file(file);
3242 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%u",
3243 (long) time(NULL), file->identifier, sectionid);
3245 edit_free_file(file);
3247 return cgi_redirect(rsp, target);
3251 /*********************************************************************
3253 * Function : cgi_edit_actions_url
3255 * Description : CGI function that actually edits a URL pattern in
3259 * 1 : csp = Current client state (buffers, headers, etc...)
3260 * 2 : rsp = http_response data structure for output
3261 * 3 : parameters = map of cgi parameters
3264 * filename : Identifies the file to edit
3265 * ver : File's last-modified time
3266 * section : Line number of section to edit
3267 * pattern : Line number of pattern to edit
3268 * newval : New value for pattern
3270 * Returns : JB_ERR_OK on success
3271 * JB_ERR_MEMORY on out-of-memory
3272 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3273 * specified or not valid.
3275 *********************************************************************/
3276 jb_err cgi_edit_actions_url(struct client_state *csp,
3277 struct http_response *rsp,
3278 const struct map *parameters)
3282 struct editable_file * file;
3283 struct file_line * cur_line;
3284 unsigned line_number;
3285 unsigned section_start_line_number = 0;
3293 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3295 return cgi_error_disabled(csp, rsp);
3298 err = get_number_param(csp, parameters, "p", &patternid);
3305 return JB_ERR_CGI_PARAMS;
3308 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3314 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3317 /* No filename specified, can't read file, modified, or out of memory. */
3319 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3323 cur_line = file->lines;
3325 while ((cur_line != NULL) && (line_number < patternid))
3327 if (cur_line->type == FILE_LINE_ACTION)
3329 section_start_line_number = line_number;
3331 cur_line = cur_line->next;
3335 if ((cur_line == NULL)
3336 || (cur_line->type != FILE_LINE_URL))
3338 /* Invalid "patternid" parameter */
3340 edit_free_file(file);
3341 return JB_ERR_CGI_PARAMS;
3344 /* At this point, the line to edit is in cur_line */
3346 freez(cur_line->raw);
3347 freez(cur_line->unprocessed);
3348 cur_line->unprocessed = new_pattern;
3350 err = edit_write_file(file);
3353 /* Error writing file */
3354 if (err == JB_ERR_FILE)
3356 /* Read-only file. */
3357 err = cgi_error_file_read_only(csp, rsp, file->filename);
3359 edit_free_file(file);
3363 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%u",
3364 (long) time(NULL), file->identifier, section_start_line_number);
3366 edit_free_file(file);
3368 return cgi_redirect(rsp, target);
3372 /*********************************************************************
3374 * Function : cgi_edit_actions_add_url
3376 * Description : CGI function that actually adds a URL pattern to
3380 * 1 : csp = Current client state (buffers, headers, etc...)
3381 * 2 : rsp = http_response data structure for output
3382 * 3 : parameters = map of cgi parameters
3385 * filename : Identifies the file to edit
3386 * ver : File's last-modified time
3387 * section : Line number of section to edit
3388 * newval : New pattern
3390 * Returns : JB_ERR_OK on success
3391 * JB_ERR_MEMORY on out-of-memory
3392 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3393 * specified or not valid.
3395 *********************************************************************/
3396 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3397 struct http_response *rsp,
3398 const struct map *parameters)
3402 struct file_line * new_line;
3403 struct editable_file * file;
3404 struct file_line * cur_line;
3405 unsigned line_number;
3409 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3411 return cgi_error_disabled(csp, rsp);
3414 err = get_number_param(csp, parameters, "s", §ionid);
3421 return JB_ERR_CGI_PARAMS;
3424 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3430 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3433 /* No filename specified, can't read file, modified, or out of memory. */
3435 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3439 cur_line = file->lines;
3441 while ((cur_line != NULL) && (line_number < sectionid))
3443 cur_line = cur_line->next;
3447 if ((cur_line == NULL)
3448 || (cur_line->type != FILE_LINE_ACTION))
3450 /* Invalid "sectionid" parameter */
3452 edit_free_file(file);
3453 return JB_ERR_CGI_PARAMS;
3456 /* At this point, the section header is in cur_line - add after this. */
3458 /* Allocate the new line */
3459 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3460 if (new_line == NULL)
3463 edit_free_file(file);
3464 return JB_ERR_MEMORY;
3467 /* Fill in the data members of the new line */
3468 new_line->raw = NULL;
3469 new_line->prefix = NULL;
3470 new_line->unprocessed = new_pattern;
3471 new_line->type = FILE_LINE_URL;
3473 /* Link new_line into the list, after cur_line */
3474 new_line->next = cur_line->next;
3475 cur_line->next = new_line;
3477 /* Done making changes, now commit */
3479 err = edit_write_file(file);
3482 /* Error writing file */
3483 if (err == JB_ERR_FILE)
3485 /* Read-only file. */
3486 err = cgi_error_file_read_only(csp, rsp, file->filename);
3488 edit_free_file(file);
3492 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%u",
3493 (long) time(NULL), file->identifier, sectionid);
3495 edit_free_file(file);
3497 return cgi_redirect(rsp, target);
3501 /*********************************************************************
3503 * Function : cgi_edit_actions_remove_url
3505 * Description : CGI function that actually removes a URL pattern from
3509 * 1 : csp = Current client state (buffers, headers, etc...)
3510 * 2 : rsp = http_response data structure for output
3511 * 3 : parameters = map of cgi parameters
3514 * f : (filename) Identifies the file to edit
3515 * v : (version) File's last-modified time
3516 * p : (pattern) Line number of pattern to remove
3518 * Returns : JB_ERR_OK on success
3519 * JB_ERR_MEMORY on out-of-memory
3520 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3521 * specified or not valid.
3523 *********************************************************************/
3524 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3525 struct http_response *rsp,
3526 const struct map *parameters)
3529 struct editable_file * file;
3530 struct file_line * cur_line;
3531 struct file_line * prev_line;
3532 unsigned line_number;
3533 unsigned section_start_line_number = 0;
3537 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3539 return cgi_error_disabled(csp, rsp);
3542 err = get_number_param(csp, parameters, "p", &patternid);
3548 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3551 /* No filename specified, can't read file, modified, or out of memory. */
3552 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3557 cur_line = file->lines;
3559 while ((cur_line != NULL) && (line_number < patternid))
3561 if (cur_line->type == FILE_LINE_ACTION)
3563 section_start_line_number = line_number;
3565 prev_line = cur_line;
3566 cur_line = cur_line->next;
3570 if ( (cur_line == NULL)
3571 || (prev_line == NULL)
3572 || (cur_line->type != FILE_LINE_URL))
3574 /* Invalid "patternid" parameter */
3575 edit_free_file(file);
3576 return JB_ERR_CGI_PARAMS;
3579 /* At this point, the line to remove is in cur_line, and the previous
3580 * one is in prev_line
3583 /* Unlink cur_line */
3584 prev_line->next = cur_line->next;
3585 cur_line->next = NULL;
3588 edit_free_file_lines(cur_line);
3590 err = edit_write_file(file);
3593 /* Error writing file */
3594 if (err == JB_ERR_FILE)
3596 /* Read-only file. */
3597 err = cgi_error_file_read_only(csp, rsp, file->filename);
3599 edit_free_file(file);
3603 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
3604 (long) time(NULL), file->identifier, section_start_line_number);
3606 edit_free_file(file);
3608 return cgi_redirect(rsp, target);
3612 /*********************************************************************
3614 * Function : cgi_edit_actions_section_remove
3616 * Description : CGI function that actually removes a whole section from
3617 * the actions file. The section must be empty first
3618 * (else JB_ERR_CGI_PARAMS).
3621 * 1 : csp = Current client state (buffers, headers, etc...)
3622 * 2 : rsp = http_response data structure for output
3623 * 3 : parameters = map of cgi parameters
3626 * f : (filename) Identifies the file to edit
3627 * v : (version) File's last-modified time
3628 * s : (section) Line number of section to edit
3630 * Returns : JB_ERR_OK on success
3631 * JB_ERR_MEMORY on out-of-memory
3632 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3633 * specified or not valid.
3635 *********************************************************************/
3636 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3637 struct http_response *rsp,
3638 const struct map *parameters)
3641 struct editable_file * file;
3642 struct file_line * cur_line;
3643 struct file_line * prev_line;
3644 unsigned line_number;
3648 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3650 return cgi_error_disabled(csp, rsp);
3653 err = get_number_param(csp, parameters, "s", §ionid);
3659 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3662 /* No filename specified, can't read file, modified, or out of memory. */
3663 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3667 cur_line = file->lines;
3670 while ((cur_line != NULL) && (line_number < sectionid))
3672 prev_line = cur_line;
3673 cur_line = cur_line->next;
3677 if ((cur_line == NULL)
3678 || (cur_line->type != FILE_LINE_ACTION))
3680 /* Invalid "sectionid" parameter */
3681 edit_free_file(file);
3682 return JB_ERR_CGI_PARAMS;
3685 if ((cur_line->next != NULL)
3686 && (cur_line->next->type == FILE_LINE_URL))
3688 /* Section not empty. */
3689 edit_free_file(file);
3690 return JB_ERR_CGI_PARAMS;
3693 /* At this point, the line to remove is in cur_line, and the previous
3694 * one is in prev_line
3697 /* Unlink cur_line */
3698 if (prev_line == NULL)
3700 /* Removing the first line from the file */
3701 file->lines = cur_line->next;
3705 prev_line->next = cur_line->next;
3707 cur_line->next = NULL;
3710 edit_free_file_lines(cur_line);
3712 err = edit_write_file(file);
3715 /* Error writing file */
3716 if (err == JB_ERR_FILE)
3718 /* Read-only file. */
3719 err = cgi_error_file_read_only(csp, rsp, file->filename);
3721 edit_free_file(file);
3725 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
3726 (long) time(NULL), file->identifier);
3728 edit_free_file(file);
3730 return cgi_redirect(rsp, target);
3734 /*********************************************************************
3736 * Function : cgi_edit_actions_section_add
3738 * Description : CGI function that adds a new empty section to
3742 * 1 : csp = Current client state (buffers, headers, etc...)
3743 * 2 : rsp = http_response data structure for output
3744 * 3 : parameters = map of cgi parameters
3747 * f : (filename) Identifies the file to edit
3748 * v : (version) File's last-modified time
3749 * s : (section) Line number of section to add after, 0 for
3752 * Returns : JB_ERR_OK on success
3753 * JB_ERR_MEMORY on out-of-memory
3754 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3755 * specified or not valid.
3757 *********************************************************************/
3758 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3759 struct http_response *rsp,
3760 const struct map *parameters)
3763 struct file_line * new_line;
3765 struct editable_file * file;
3766 struct file_line * cur_line;
3767 unsigned line_number;
3771 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3773 return cgi_error_disabled(csp, rsp);
3776 err = get_number_param(csp, parameters, "s", §ionid);
3782 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3785 /* No filename specified, can't read file, modified, or out of memory. */
3786 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3790 cur_line = file->lines;
3792 if (sectionid <= 1U)
3794 /* Add to start of file */
3795 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
3797 /* There's something in the file, find the line before the first
3800 while ((cur_line->next != NULL)
3801 && (cur_line->next->type != FILE_LINE_ACTION))
3803 cur_line = cur_line->next;
3809 /* File starts with action line, so insert at top */
3815 /* Add after stated section. */
3816 while ((cur_line != NULL) && (line_number < sectionid))
3818 cur_line = cur_line->next;
3822 if ((cur_line == NULL)
3823 || (cur_line->type != FILE_LINE_ACTION))
3825 /* Invalid "sectionid" parameter */
3826 edit_free_file(file);
3827 return JB_ERR_CGI_PARAMS;
3830 /* Skip through the section to find the last line in it. */
3831 while ((cur_line->next != NULL)
3832 && (cur_line->next->type != FILE_LINE_ACTION))
3834 cur_line = cur_line->next;
3839 /* At this point, the last line in the previous section is in cur_line
3840 * - add after this. (Or if we need to add as the first line, cur_line
3844 new_text = strdup("{}");
3845 if (NULL == new_text)
3847 edit_free_file(file);
3848 return JB_ERR_MEMORY;
3851 /* Allocate the new line */
3852 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3853 if (new_line == NULL)
3856 edit_free_file(file);
3857 return JB_ERR_MEMORY;
3860 /* Fill in the data members of the new line */
3861 new_line->raw = NULL;
3862 new_line->prefix = NULL;
3863 new_line->unprocessed = new_text;
3864 new_line->type = FILE_LINE_ACTION;
3866 if (cur_line != NULL)
3868 /* Link new_line into the list, after cur_line */
3869 new_line->next = cur_line->next;
3870 cur_line->next = new_line;
3874 /* Link new_line into the list, as first line */
3875 new_line->next = file->lines;
3876 file->lines = new_line;
3879 /* Done making changes, now commit */
3881 err = edit_write_file(file);
3884 /* Error writing file */
3885 if (err == JB_ERR_FILE)
3887 /* Read-only file. */
3888 err = cgi_error_file_read_only(csp, rsp, file->filename);
3890 edit_free_file(file);
3894 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
3895 (long) time(NULL), file->identifier);
3897 edit_free_file(file);
3899 return cgi_redirect(rsp, target);
3903 /*********************************************************************
3905 * Function : cgi_edit_actions_section_swap
3907 * Description : CGI function that swaps the order of two sections
3908 * in the actions file. Note that this CGI can actually
3909 * swap any two arbitrary sections, but the GUI interface
3910 * currently only allows consecutive sections to be
3914 * 1 : csp = Current client state (buffers, headers, etc...)
3915 * 2 : rsp = http_response data structure for output
3916 * 3 : parameters = map of cgi parameters
3919 * f : (filename) Identifies the file to edit
3920 * v : (version) File's last-modified time
3921 * s1 : (section1) Line number of first section to swap
3922 * s2 : (section2) Line number of second section to swap
3924 * Returns : JB_ERR_OK on success
3925 * JB_ERR_MEMORY on out-of-memory
3926 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3927 * specified or not valid.
3929 *********************************************************************/
3930 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3931 struct http_response *rsp,
3932 const struct map *parameters)
3936 struct editable_file * file;
3937 struct file_line * cur_line;
3938 struct file_line * prev_line;
3939 struct file_line * line_before_section1;
3940 struct file_line * line_start_section1;
3941 struct file_line * line_end_section1;
3942 struct file_line * line_after_section1;
3943 struct file_line * line_before_section2;
3944 struct file_line * line_start_section2;
3945 struct file_line * line_end_section2;
3946 struct file_line * line_after_section2;
3947 unsigned line_number;
3951 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3953 return cgi_error_disabled(csp, rsp);
3956 err = get_number_param(csp, parameters, "s1", §ion1);
3957 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3963 if (section1 > section2)
3965 unsigned temp = section2;
3966 section2 = section1;
3970 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3973 /* No filename specified, can't read file, modified, or out of memory. */
3974 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3977 /* Start at the beginning... */
3979 cur_line = file->lines;
3982 /* ... find section1 ... */
3983 while ((cur_line != NULL) && (line_number < section1))
3985 prev_line = cur_line;
3986 cur_line = cur_line->next;
3990 if ((cur_line == NULL)
3991 || (cur_line->type != FILE_LINE_ACTION))
3993 /* Invalid "section1" parameter */
3994 edit_free_file(file);
3995 return JB_ERR_CGI_PARAMS;
3998 /* If no-op, we've validated params and can skip the rest. */
3999 if (section1 != section2)
4001 /* ... find the end of section1 ... */
4002 line_before_section1 = prev_line;
4003 line_start_section1 = cur_line;
4006 prev_line = cur_line;
4007 cur_line = cur_line->next;
4010 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4011 line_end_section1 = prev_line;
4012 line_after_section1 = cur_line;
4014 /* ... find section2 ... */
4015 while ((cur_line != NULL) && (line_number < section2))
4017 prev_line = cur_line;
4018 cur_line = cur_line->next;
4022 if ((cur_line == NULL)
4023 || (cur_line->type != FILE_LINE_ACTION))
4025 /* Invalid "section2" parameter */
4026 edit_free_file(file);
4027 return JB_ERR_CGI_PARAMS;
4030 /* ... find the end of section2 ... */
4031 line_before_section2 = prev_line;
4032 line_start_section2 = cur_line;
4035 prev_line = cur_line;
4036 cur_line = cur_line->next;
4039 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4040 line_end_section2 = prev_line;
4041 line_after_section2 = cur_line;
4043 /* Now have all the pointers we need. Do the swap. */
4045 /* Change the pointer to section1 to point to section2 instead */
4046 if (line_before_section1 == NULL)
4048 file->lines = line_start_section2;
4052 line_before_section1->next = line_start_section2;
4055 if (line_before_section2 == line_end_section1)
4057 /* Consecutive sections */
4058 line_end_section2->next = line_start_section1;
4062 line_end_section2->next = line_after_section1;
4063 line_before_section2->next = line_start_section1;
4066 /* Set the pointer from the end of section1 to the rest of the file */
4067 line_end_section1->next = line_after_section2;
4069 err = edit_write_file(file);
4072 /* Error writing file */
4073 if (err == JB_ERR_FILE)
4075 /* Read-only file. */
4076 err = cgi_error_file_read_only(csp, rsp, file->filename);
4078 edit_free_file(file);
4081 } /* END if (section1 != section2) */
4083 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
4084 (long) time(NULL), file->identifier);
4086 edit_free_file(file);
4088 return cgi_redirect(rsp, target);
4092 /*********************************************************************
4094 * Function : javascriptify
4096 * Description : Converts a string into a form JavaScript will like.
4098 * Netscape 4's JavaScript sucks - it doesn't use
4099 * "id" parameters, so you have to set the "name"
4100 * used to submit a form element to something JavaScript
4101 * will like. (Or access the elements by index in an
4102 * array. That array contains >60 elements and will
4103 * be changed whenever we add a new action to the
4104 * editor, so I'm NOT going to use indexes that have
4105 * to be figured out by hand.)
4107 * Currently the only thing we have to worry about
4108 * is "-" ==> "_" conversion.
4110 * This is a length-preserving operation so it is
4111 * carried out in-place, no memory is allocated
4115 * 1 : identifier = String to make JavaScript-friendly.
4119 *********************************************************************/
4120 static void javascriptify(char * identifier)
4122 char * p = identifier;
4123 while (NULL != (p = strchr(p, '-')))
4130 /*********************************************************************
4132 * Function : actions_to_radio
4134 * Description : Converts a actionsfile entry into settings for
4135 * radio buttons and edit boxes on a HTML form.
4138 * 1 : exports = List of substitutions to add to.
4139 * 2 : action = Action to read
4141 * Returns : JB_ERR_OK on success
4142 * JB_ERR_MEMORY on out-of-memory
4144 *********************************************************************/
4145 static jb_err actions_to_radio(struct map * exports,
4146 const struct action_spec *action)
4157 mask = action->mask;
4160 /* sanity - prevents "-feature +feature" */
4164 #define DEFINE_ACTION_BOOL(name, bit) \
4165 if (!(mask & bit)) \
4167 current_mode = 'n'; \
4169 else if (add & bit) \
4171 current_mode = 'y'; \
4175 current_mode = 'x'; \
4177 if (map_radio(exports, name, "ynx", current_mode)) \
4179 return JB_ERR_MEMORY; \
4182 #define DEFINE_ACTION_STRING(name, bit, index) \
4183 DEFINE_ACTION_BOOL(name, bit); \
4186 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4189 checked = !strcmp(action->string[index], value); \
4193 checked = is_default; \
4195 mapped_param |= checked; \
4196 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4198 return JB_ERR_MEMORY; \
4201 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4202 if (map(exports, name "-param-custom", 1, \
4203 ((!mapped_param) ? "checked" : ""), 1)) \
4205 return JB_ERR_MEMORY; \
4207 if (map(exports, name "-param", 1, \
4208 (((add & bit) && !mapped_param) ? \
4209 action->string[index] : default_val), 1)) \
4211 return JB_ERR_MEMORY; \
4214 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4215 if (map(exports, name "-param", 1, \
4216 ((add & bit) ? action->string[index] : default_val), 1)) \
4218 return JB_ERR_MEMORY; \
4221 #define DEFINE_ACTION_MULTI(name, index) \
4222 if (action->multi_add[index]->first) \
4224 current_mode = 'y'; \
4226 else if (action->multi_remove_all[index]) \
4228 current_mode = 'n'; \
4230 else if (action->multi_remove[index]->first) \
4232 current_mode = 'y'; \
4236 current_mode = 'x'; \
4238 if (map_radio(exports, name, "ynx", current_mode)) \
4240 return JB_ERR_MEMORY; \
4243 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4245 #include "actionlist.h"
4247 #undef DEFINE_ACTION_MULTI
4248 #undef DEFINE_ACTION_STRING
4249 #undef DEFINE_ACTION_BOOL
4250 #undef DEFINE_ACTION_ALIAS
4251 #undef DEFINE_CGI_PARAM_CUSTOM
4252 #undef DEFINE_CGI_PARAM_RADIO
4253 #undef DEFINE_CGI_PARAM_NO_RADIO
4259 /*********************************************************************
4261 * Function : actions_from_radio
4263 * Description : Converts a map of parameters passed to a CGI function
4264 * into an actionsfile entry.
4267 * 1 : parameters = parameters to the CGI call
4268 * 2 : action = Action to change. Must be valid before
4269 * the call, actions not specified will be
4272 * Returns : JB_ERR_OK on success
4273 * JB_ERR_MEMORY on out-of-memory
4275 *********************************************************************/
4276 static jb_err actions_from_radio(const struct map * parameters,
4277 struct action_spec *action)
4282 const char * js_name;
4283 jb_err err = JB_ERR_OK;
4288 /* Statics are generally a potential race condition,
4289 * but in this case we're safe and don't need semaphores.
4290 * Be careful if you modify this function.
4292 * The js_name_arr's are never free()d, but this is no
4293 * problem, since they will only be created once and
4294 * used by all threads thereafter. -oes
4297 #define JAVASCRIPTIFY(dest_var, string) \
4299 static int first_time = 1; \
4300 static char *js_name_arr; \
4303 js_name_arr = strdup(string); \
4304 javascriptify(js_name_arr); \
4306 dest_var = js_name_arr; \
4310 #define DEFINE_ACTION_BOOL(name, bit) \
4311 JAVASCRIPTIFY(js_name, name); \
4312 ch = get_char_param(parameters, js_name); \
4315 action->add |= bit; \
4316 action->mask |= bit; \
4318 else if (ch == 'N') \
4320 action->add &= ~bit; \
4321 action->mask &= ~bit; \
4323 else if (ch == 'X') \
4325 action->add &= ~bit; \
4326 action->mask |= bit; \
4329 #define DEFINE_ACTION_STRING(name, bit, index) \
4330 JAVASCRIPTIFY(js_name, name); \
4331 ch = get_char_param(parameters, js_name); \
4335 JAVASCRIPTIFY(js_name, name "-mode"); \
4336 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4337 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4339 JAVASCRIPTIFY(js_name, name "-param"); \
4340 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4342 if (param != NULL) \
4344 if (NULL == (param_dup = strdup(param))) \
4346 return JB_ERR_MEMORY; \
4348 freez(action->string[index]); \
4349 action->add |= bit; \
4350 action->mask |= bit; \
4351 action->string[index] = param_dup; \
4354 else if (ch == 'N') \
4356 if (action->add & bit) \
4358 freez(action->string[index]); \
4360 action->add &= ~bit; \
4361 action->mask &= ~bit; \
4363 else if (ch == 'X') \
4365 if (action->add & bit) \
4367 freez(action->string[index]); \
4369 action->add &= ~bit; \
4370 action->mask |= bit; \
4373 #define DEFINE_ACTION_MULTI(name, index) \
4374 JAVASCRIPTIFY(js_name, name); \
4375 ch = get_char_param(parameters, js_name); \
4380 else if (ch == 'N') \
4382 list_remove_all(action->multi_add[index]); \
4383 list_remove_all(action->multi_remove[index]); \
4384 action->multi_remove_all[index] = 1; \
4386 else if (ch == 'X') \
4388 list_remove_all(action->multi_add[index]); \
4389 list_remove_all(action->multi_remove[index]); \
4390 action->multi_remove_all[index] = 0; \
4393 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4395 #include "actionlist.h"
4397 #undef DEFINE_ACTION_MULTI
4398 #undef DEFINE_ACTION_STRING
4399 #undef DEFINE_ACTION_BOOL
4400 #undef DEFINE_ACTION_ALIAS
4401 #undef JAVASCRIPTIFY
4405 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4408 #ifdef FEATURE_TOGGLE
4409 /*********************************************************************
4411 * Function : cgi_toggle
4413 * Description : CGI function that adds a new empty section to
4417 * 1 : csp = Current client state (buffers, headers, etc...)
4418 * 2 : rsp = http_response data structure for output
4419 * 3 : parameters = map of cgi parameters
4422 * set : If present, how to change toggle setting:
4423 * "enable", "disable", "toggle", or none (default).
4424 * mini : If present, use mini reply template.
4426 * Returns : JB_ERR_OK on success
4427 * JB_ERR_MEMORY on out-of-memory
4429 *********************************************************************/
4430 jb_err cgi_toggle(struct client_state *csp,
4431 struct http_response *rsp,
4432 const struct map *parameters)
4434 struct map *exports;
4436 const char *template_name;
4442 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4444 return cgi_error_disabled(csp, rsp);
4447 mode = get_char_param(parameters, "set");
4452 global_toggle_state = 1;
4454 else if (mode == 'D')
4457 global_toggle_state = 0;
4459 else if (mode == 'T')
4462 global_toggle_state = !global_toggle_state;
4465 if (NULL == (exports = default_exports(csp, "toggle")))
4467 return JB_ERR_MEMORY;
4470 template_name = (get_char_param(parameters, "mini")
4474 return template_fill_for_cgi(csp, template_name, exports, rsp);
4476 #endif /* def FEATURE_TOGGLE */