1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.80 2014/06/02 06:19:04 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%d", 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), "%i", 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)
1216 else if (err != JB_ERR_OK)
1218 /* Line does not contain a name=value pair */
1219 file->parse_error = cur_line;
1220 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1221 return JB_ERR_PARSE;
1224 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1229 free_alias_list(alias_list);
1230 return JB_ERR_MEMORY;
1233 err = get_actions(value, alias_list, new_alias->action);
1236 /* Invalid action or out of memory */
1240 free_alias_list(alias_list);
1241 if (err == JB_ERR_MEMORY)
1247 /* Line does not contain a name=value pair */
1248 file->parse_error = cur_line;
1249 file->parse_error_text = "This alias does not specify a valid set of actions.";
1250 return JB_ERR_PARSE;
1256 new_alias->name = name;
1259 new_alias->next = alias_list;
1260 alias_list = new_alias;
1264 cur_line->type = FILE_LINE_BLANK;
1266 cur_line = cur_line->next;
1270 /* Header done, process the main part of the file */
1271 while (cur_line != NULL)
1273 /* At this point, (cur_line->unprocessed[0] == '{') */
1274 assert(cur_line->unprocessed[0] == '{');
1275 text = cur_line->unprocessed + 1;
1276 len = strlen(text) - 1;
1277 if (text[len] != '}')
1279 /* No closing } on header */
1280 free_alias_list(alias_list);
1281 file->parse_error = cur_line;
1282 file->parse_error_text = "Headers starting with '{' must have a "
1283 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1284 "must close with two brackets ('}}').";
1285 return JB_ERR_PARSE;
1290 /* An invalid {{ header. */
1291 free_alias_list(alias_list);
1292 file->parse_error = cur_line;
1293 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1294 "Please remember that the system (two-bracket) headers must "
1295 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1296 "and must appear before any actions (one-bracket) headers. "
1297 "Also note that system headers may not be repeated.";
1298 return JB_ERR_PARSE;
1301 while ((*text == ' ') || (*text == '\t'))
1306 while ((len > (size_t)0)
1307 && ((text[len - 1] == ' ')
1308 || (text[len - 1] == '\t')))
1313 cur_line->type = FILE_LINE_ACTION;
1315 /* Remove {} and make copy */
1316 value = malloc_or_die(len + 1);
1317 strncpy(value, text, len);
1321 err = get_actions(value, alias_list, cur_line->data.action);
1324 /* Invalid action or out of memory */
1326 free_alias_list(alias_list);
1327 if (err == JB_ERR_MEMORY)
1333 /* Line does not contain a name=value pair */
1334 file->parse_error = cur_line;
1335 file->parse_error_text = "This header does not specify a valid set of actions.";
1336 return JB_ERR_PARSE;
1340 /* Done with string - it was clobbered anyway */
1343 /* Process next line */
1344 cur_line = cur_line->next;
1346 /* Loop processing URL patterns */
1347 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1349 if (cur_line->unprocessed[0])
1351 /* Could parse URL here, but this isn't currently needed */
1353 cur_line->type = FILE_LINE_URL;
1357 cur_line->type = FILE_LINE_BLANK;
1359 cur_line = cur_line->next;
1361 } /* End main while(cur_line != NULL) loop */
1363 free_alias_list(alias_list);
1369 /*********************************************************************
1371 * Function : edit_read_file_lines
1373 * Description : Read all the lines of a file into memory.
1374 * Handles whitespace, comments and line continuation.
1377 * 1 : fp = File to read from. On return, this will be
1378 * at EOF but it will not have been closed.
1379 * 2 : pfile = Destination for a linked list of file_lines.
1380 * Will be set to NULL on error.
1381 * 3 : newline = How to handle newlines.
1383 * Returns : JB_ERR_OK on success
1384 * JB_ERR_MEMORY on out-of-memory
1386 *********************************************************************/
1387 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1389 struct file_line * first_line; /* Keep for return value or to free */
1390 struct file_line * cur_line; /* Current line */
1391 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1399 cur_line = first_line = zalloc(sizeof(struct file_line));
1400 if (cur_line == NULL)
1402 return JB_ERR_MEMORY;
1405 cur_line->type = FILE_LINE_UNPROCESSED;
1407 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1410 /* Out of memory or empty file. */
1411 /* Note that empty file is not an error we propagate up */
1413 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1418 prev_line = cur_line;
1419 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1420 if (cur_line == NULL)
1423 edit_free_file_lines(first_line);
1424 return JB_ERR_MEMORY;
1427 cur_line->type = FILE_LINE_UNPROCESSED;
1429 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1430 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1433 edit_free_file_lines(first_line);
1434 return JB_ERR_MEMORY;
1438 while (rval != JB_ERR_FILE);
1442 /* We allocated one too many - free it */
1443 prev_line->next = NULL;
1446 *pfile = first_line;
1451 /*********************************************************************
1453 * Function : edit_read_file
1455 * Description : Read a complete file into memory.
1456 * Handles CGI parameter parsing. If requested, also
1457 * checks the file's modification timestamp.
1460 * 1 : csp = Current client state (buffers, headers, etc...)
1461 * 2 : parameters = map of cgi parameters.
1462 * 3 : require_version = true to check "ver" parameter.
1463 * 4 : pfile = Destination for the file. Will be set
1467 * f : The action file identifier.
1468 * ver : (Only if require_version is nonzero)
1469 * Timestamp of the actions file. If wrong, this
1470 * function fails with JB_ERR_MODIFIED.
1472 * Returns : JB_ERR_OK on success
1473 * JB_ERR_MEMORY on out-of-memory
1474 * JB_ERR_CGI_PARAMS if "filename" was not specified
1476 * JB_ERR_FILE if the file cannot be opened or
1478 * JB_ERR_MODIFIED if version checking was requested and
1479 * failed - the file was modified outside
1480 * of this CGI editor instance.
1482 *********************************************************************/
1483 jb_err edit_read_file(struct client_state *csp,
1484 const struct map *parameters,
1485 int require_version,
1486 struct editable_file **pfile)
1488 struct file_line * lines;
1491 const char *filename = NULL;
1492 struct editable_file * file;
1493 unsigned version = 0;
1494 struct stat statbuf[1];
1495 char version_buf[22];
1496 int newline = NEWLINE_UNKNOWN;
1505 err = get_number_param(csp, parameters, "f", &i);
1506 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1508 filename = csp->config->actions_file[i];
1510 else if (JB_ERR_CGI_PARAMS == err)
1513 * Probably an old-school URL like
1514 * http://config.privoxy.org/edit-actions-list?f=default
1516 get_file_name_param(csp, parameters, "f", &filename);
1519 if (NULL == filename || stat(filename, statbuf) < 0)
1521 /* Error, probably file not found. */
1524 version = (unsigned) statbuf->st_mtime;
1526 if (require_version)
1528 unsigned specified_version;
1529 err = get_number_param(csp, parameters, "v", &specified_version);
1535 if (version != specified_version)
1537 return JB_ERR_MODIFIED;
1541 if (NULL == (fp = fopen(filename,"rb")))
1546 err = edit_read_file_lines(fp, &lines, &newline);
1555 file = (struct editable_file *) zalloc(sizeof(*file));
1558 edit_free_file_lines(lines);
1562 file->lines = lines;
1563 file->newline = newline;
1564 file->filename = filename;
1565 file->version = version;
1566 file->identifier = i;
1568 /* Correct file->version_str */
1569 freez(file->version_str);
1570 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1571 version_buf[sizeof(version_buf)-1] = '\0';
1572 file->version_str = strdup(version_buf);
1573 if (version_buf == NULL)
1575 edit_free_file(file);
1576 return JB_ERR_MEMORY;
1584 /*********************************************************************
1586 * Function : edit_read_actions_file
1588 * Description : Read a complete actions file into memory.
1589 * Handles CGI parameter parsing. If requested, also
1590 * checks the file's modification timestamp.
1592 * If this function detects an error in the categories
1593 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1594 * then it handles it by filling in the specified
1595 * response structure and returning JB_ERR_FILE.
1598 * 1 : csp = Current client state (buffers, headers, etc...)
1599 * 2 : rsp = HTTP response. Only filled in on error.
1600 * 2 : parameters = map of cgi parameters.
1601 * 3 : require_version = true to check "ver" parameter.
1602 * 4 : pfile = Destination for the file. Will be set
1606 * f : The actions file identifier.
1607 * ver : (Only if require_version is nonzero)
1608 * Timestamp of the actions file. If wrong, this
1609 * function fails with JB_ERR_MODIFIED.
1611 * Returns : JB_ERR_OK on success
1612 * JB_ERR_MEMORY on out-of-memory
1613 * JB_ERR_CGI_PARAMS if "filename" was not specified
1615 * JB_ERR_FILE if the file does not contain valid data,
1616 * or if file cannot be opened or
1617 * contains no data, or if version
1618 * checking was requested and failed.
1620 *********************************************************************/
1621 jb_err edit_read_actions_file(struct client_state *csp,
1622 struct http_response *rsp,
1623 const struct map *parameters,
1624 int require_version,
1625 struct editable_file **pfile)
1628 struct editable_file *file;
1629 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
1637 err = edit_read_file(csp, parameters, require_version, &file);
1640 /* Try to handle if possible */
1641 if (err == JB_ERR_FILE)
1643 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1645 else if (err == JB_ERR_MODIFIED)
1647 assert(require_version);
1648 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1649 log_error(LOG_LEVEL_ERROR,
1650 "Blocking CGI edit request due to modification time mismatch.");
1651 if (acceptable_failures > 0)
1653 log_error(LOG_LEVEL_INFO,
1654 "The CGI editor will be turned off after another %d mismatche(s).",
1655 acceptable_failures);
1656 acceptable_failures--;
1660 log_error(LOG_LEVEL_INFO,
1661 "Timestamp mismatch limit reached, turning CGI editor off. "
1662 "Reload the configuration file to re-enable it.");
1663 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
1666 if (err == JB_ERR_OK)
1669 * Signal to higher-level CGI code that there was a problem but we
1670 * handled it, they should just return JB_ERR_OK.
1677 err = edit_parse_actions_file(file);
1680 if (err == JB_ERR_PARSE)
1682 err = cgi_error_parse(csp, rsp, file);
1683 if (err == JB_ERR_OK)
1686 * Signal to higher-level CGI code that there was a problem but we
1687 * handled it, they should just return JB_ERR_OK.
1692 edit_free_file(file);
1701 /*********************************************************************
1703 * Function : get_file_name_param
1705 * Description : Get the name of the file to edit from the parameters
1706 * passed to a CGI function using the old syntax.
1707 * This function handles security checks and only
1708 * accepts files that Privoxy already knows.
1711 * 1 : csp = Current client state (buffers, headers, etc...)
1712 * 2 : parameters = map of cgi parameters
1713 * 3 : param_name = The name of the parameter to read
1714 * 4 : pfilename = pointer to the filename in
1715 * csp->config->actions_file[] if found. Set to NULL on error.
1717 * Returns : JB_ERR_OK on success
1718 * JB_ERR_MEMORY on out-of-memory
1719 * JB_ERR_CGI_PARAMS if "filename" was not specified
1722 *********************************************************************/
1723 static jb_err get_file_name_param(struct client_state *csp,
1724 const struct map *parameters,
1725 const char *param_name,
1726 const char **pfilename)
1729 const char suffix[] = ".action";
1744 param = lookup(parameters, param_name);
1747 return JB_ERR_CGI_PARAMS;
1750 len = strlen(param);
1751 if (len >= FILENAME_MAX)
1754 return JB_ERR_CGI_PARAMS;
1758 * Check every character to see if it's legal.
1759 * Totally unnecessary but we do it anyway.
1762 while ((ch = *s++) != '\0')
1764 if ( ((ch < 'A') || (ch > 'Z'))
1765 && ((ch < 'a') || (ch > 'z'))
1766 && ((ch < '0') || (ch > '9'))
1770 /* Probable hack attempt. */
1771 return JB_ERR_CGI_PARAMS;
1775 /* Append extension */
1776 name_size = len + strlen(suffix) + 1;
1777 name = malloc_or_die(name_size);
1778 strlcpy(name, param, name_size);
1779 strlcat(name, suffix, name_size);
1782 fullpath = make_path(csp->config->confdir, name);
1785 if (fullpath == NULL)
1787 return JB_ERR_MEMORY;
1790 /* Check if the file is known */
1791 for (i = 0; i < MAX_AF_FILES; i++)
1793 if (NULL != csp->config->actions_file[i] &&
1794 !strcmp(fullpath, csp->config->actions_file[i]))
1797 *pfilename = csp->config->actions_file[i];
1805 return JB_ERR_CGI_PARAMS;
1809 /*********************************************************************
1811 * Function : get_url_spec_param
1813 * Description : Get a URL pattern from the parameters
1814 * passed to a CGI function. Removes leading/trailing
1815 * spaces and validates it.
1818 * 1 : csp = Current client state (buffers, headers, etc...)
1819 * 2 : parameters = map of cgi parameters
1820 * 3 : name = Name of CGI parameter to read
1821 * 4 : pvalue = destination for value. Will be malloc()'d.
1822 * Set to NULL on error.
1824 * Returns : JB_ERR_OK on success
1825 * JB_ERR_MEMORY on out-of-memory
1826 * JB_ERR_CGI_PARAMS if the parameter was not specified
1829 *********************************************************************/
1830 static jb_err get_url_spec_param(struct client_state *csp,
1831 const struct map *parameters,
1835 const char *orig_param;
1838 struct pattern_spec compiled[1];
1848 orig_param = lookup(parameters, name);
1851 return JB_ERR_CGI_PARAMS;
1854 /* Copy and trim whitespace */
1855 param = strdup(orig_param);
1858 return JB_ERR_MEMORY;
1862 /* Must be non-empty, and can't allow 1st character to be '{' */
1863 if (param[0] == '\0' || param[0] == '{')
1866 return JB_ERR_CGI_PARAMS;
1869 /* Check for embedded newlines */
1870 for (s = param; *s != '\0'; s++)
1872 if ((*s == '\r') || (*s == '\n'))
1875 return JB_ERR_CGI_PARAMS;
1879 /* Check that regex is valid */
1884 return JB_ERR_MEMORY;
1886 err = create_pattern_spec(compiled, s);
1891 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1893 free_pattern_spec(compiled);
1895 if (param[strlen(param) - 1] == '\\')
1898 * Must protect trailing '\\' from becoming line continuation character.
1899 * Two methods: 1) If it's a domain only, add a trailing '/'.
1900 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1902 if (strchr(param, '/') == NULL)
1904 err = string_append(¶m, "/");
1908 err = string_append(¶m, "(?:)");
1915 /* Check that the modified regex is valid */
1920 return JB_ERR_MEMORY;
1922 err = create_pattern_spec(compiled, s);
1927 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1929 free_pattern_spec(compiled);
1936 /*********************************************************************
1938 * Function : map_radio
1940 * Description : Map a set of radio button values. E.g. if you have
1941 * 3 radio buttons, declare them as:
1942 * <option type="radio" name="xyz" @xyz-a@>
1943 * <option type="radio" name="xyz" @xyz-b@>
1944 * <option type="radio" name="xyz" @xyz-c@>
1945 * Then map one of the @xyz-?@ variables to "checked"
1946 * and all the others to empty by calling:
1947 * map_radio(exports, "xyz", "abc", sel)
1948 * Where 'sel' is 'a', 'b', or 'c'.
1951 * 1 : exports = Exports map to modify.
1952 * 2 : optionname = name for map
1953 * 3 : values = null-terminated list of values;
1954 * 4 : value = Selected value.
1956 * CGI Parameters : None
1958 * Returns : JB_ERR_OK on success
1959 * JB_ERR_MEMORY on out-of-memory
1961 *********************************************************************/
1962 static jb_err map_radio(struct map * exports,
1963 const char * optionname,
1964 const char * values,
1970 const size_t len = strlen(optionname);
1971 const size_t buf_size = len + 3;
1977 buf = malloc_or_die(buf_size);
1979 strlcpy(buf, optionname, buf_size);
1981 /* XXX: this looks ... interesting */
1986 while ((c = *values++) != '\0')
1991 if (map(exports, buf, 1, "", 1))
1993 return JB_ERR_MEMORY;
1999 return map(exports, buf, 0, "checked", 1);
2003 /*********************************************************************
2005 * Function : cgi_error_modified
2007 * Description : CGI function that is called when a file is modified
2008 * outside the CGI editor.
2011 * 1 : csp = Current client state (buffers, headers, etc...)
2012 * 2 : rsp = http_response data structure for output
2013 * 3 : filename = The file that was modified.
2015 * CGI Parameters : none
2017 * Returns : JB_ERR_OK on success
2018 * JB_ERR_MEMORY on out-of-memory error.
2020 *********************************************************************/
2021 jb_err cgi_error_modified(struct client_state *csp,
2022 struct http_response *rsp,
2023 const char *filename)
2025 struct map *exports;
2032 if (NULL == (exports = default_exports(csp, NULL)))
2034 return JB_ERR_MEMORY;
2037 err = map(exports, "f", 1, html_encode(filename), 0);
2044 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2048 /*********************************************************************
2050 * Function : cgi_error_parse
2052 * Description : CGI function that is called when a file cannot
2053 * be parsed by the CGI editor.
2056 * 1 : csp = Current client state (buffers, headers, etc...)
2057 * 2 : rsp = http_response data structure for output
2058 * 3 : file = The file that was modified.
2060 * CGI Parameters : none
2062 * Returns : JB_ERR_OK on success
2063 * JB_ERR_MEMORY on out-of-memory error.
2065 *********************************************************************/
2066 jb_err cgi_error_parse(struct client_state *csp,
2067 struct http_response *rsp,
2068 struct editable_file *file)
2070 struct map *exports;
2072 struct file_line *cur_line;
2078 if (NULL == (exports = default_exports(csp, NULL)))
2080 return JB_ERR_MEMORY;
2083 err = map(exports, "f", 1, stringify(file->identifier), 0);
2084 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2086 cur_line = file->parse_error;
2089 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2090 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2098 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2102 /*********************************************************************
2104 * Function : cgi_error_file
2106 * Description : CGI function that is called when a file cannot be
2107 * opened by the CGI editor.
2110 * 1 : csp = Current client state (buffers, headers, etc...)
2111 * 2 : rsp = http_response data structure for output
2112 * 3 : filename = The file that was modified.
2114 * CGI Parameters : none
2116 * Returns : JB_ERR_OK on success
2117 * JB_ERR_MEMORY on out-of-memory error.
2119 *********************************************************************/
2120 jb_err cgi_error_file(struct client_state *csp,
2121 struct http_response *rsp,
2122 const char *filename)
2124 struct map *exports;
2131 if (NULL == (exports = default_exports(csp, NULL)))
2133 return JB_ERR_MEMORY;
2136 err = map(exports, "f", 1, html_encode(filename), 0);
2143 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2147 /*********************************************************************
2149 * Function : cgi_error_file_read_only
2151 * Description : CGI function that is called when a file cannot be
2152 * opened for writing by the CGI editor.
2155 * 1 : csp = Current client state (buffers, headers, etc...)
2156 * 2 : rsp = http_response data structure for output
2157 * 3 : filename = The file that we can't write to
2159 * CGI Parameters : none
2161 * Returns : JB_ERR_OK on success
2162 * JB_ERR_MEMORY on out-of-memory error.
2164 *********************************************************************/
2165 jb_err cgi_error_file_read_only(struct client_state *csp,
2166 struct http_response *rsp,
2167 const char *filename)
2169 struct map *exports;
2176 if (NULL == (exports = default_exports(csp, NULL)))
2178 return JB_ERR_MEMORY;
2181 err = map(exports, "f", 1, html_encode(filename), 0);
2188 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2192 /*********************************************************************
2194 * Function : cgi_edit_actions
2196 * Description : CGI function that allows the user to choose which
2197 * actions file to edit.
2200 * 1 : csp = Current client state (buffers, headers, etc...)
2201 * 2 : rsp = http_response data structure for output
2202 * 3 : parameters = map of cgi parameters
2204 * CGI Parameters : None
2206 * Returns : JB_ERR_OK on success
2207 * JB_ERR_MEMORY on out-of-memory error
2209 *********************************************************************/
2210 jb_err cgi_edit_actions(struct client_state *csp,
2211 struct http_response *rsp,
2212 const struct map *parameters)
2216 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2218 return cgi_error_disabled(csp, rsp);
2221 /* FIXME: Incomplete */
2223 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2228 /*********************************************************************
2230 * Function : cgi_edit_actions_list
2232 * Description : CGI function that edits the actions list.
2233 * FIXME: This function shouldn't FATAL ever.
2234 * FIXME: This function doesn't check the retval of map()
2236 * 1 : csp = Current client state (buffers, headers, etc...)
2237 * 2 : rsp = http_response data structure for output
2238 * 3 : parameters = map of cgi parameters
2240 * CGI Parameters : filename
2242 * Returns : JB_ERR_OK on success
2243 * JB_ERR_MEMORY on out-of-memory
2244 * JB_ERR_FILE if the file cannot be opened or
2246 * JB_ERR_CGI_PARAMS if "filename" was not specified
2249 *********************************************************************/
2250 jb_err cgi_edit_actions_list(struct client_state *csp,
2251 struct http_response *rsp,
2252 const struct map *parameters)
2254 char * section_template;
2255 char * url_template;
2260 struct map * exports;
2261 struct map * section_exports;
2262 struct map * url_exports;
2263 struct editable_file * file;
2264 struct file_line * cur_line;
2265 unsigned line_number = 0;
2266 unsigned prev_section_line_number = ((unsigned) (-1));
2268 struct file_list * fl;
2269 struct url_actions * b;
2270 char * buttons = NULL;
2273 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2275 return cgi_error_disabled(csp, rsp);
2278 if (NULL == (exports = default_exports(csp, NULL)))
2280 return JB_ERR_MEMORY;
2283 /* Load actions file */
2284 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2287 /* No filename specified, can't read file, or out of memory. */
2289 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2292 /* Find start of actions in file */
2293 cur_line = file->lines;
2295 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2297 cur_line = cur_line->next;
2302 * Conventional actions files should have a match all block
2304 * cur_line = {...global actions...}
2305 * cur_line->next = /
2306 * cur_line->next->next = {...actions...} or EOF
2308 if ( (cur_line != NULL)
2309 && (cur_line->type == FILE_LINE_ACTION)
2310 && (cur_line->next != NULL)
2311 && (cur_line->next->type == FILE_LINE_URL)
2312 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2313 && ( (cur_line->next->next == NULL)
2314 || (cur_line->next->next->type != FILE_LINE_URL)
2318 * Generate string with buttons to set actions for "/" to
2319 * any predefined set of actions (named standard.*, probably
2320 * residing in standard.action).
2323 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2326 edit_free_file(file);
2328 if (err == JB_ERR_FILE)
2330 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2335 err = template_fill(§ion_template, exports);
2338 edit_free_file(file);
2343 buttons = strdup("");
2344 for (i = 0; i < MAX_AF_FILES; i++)
2346 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2348 for (b = b->next; NULL != b; b = b->next)
2350 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2352 if (err || (NULL == (section_exports = new_map())))
2355 free(section_template);
2356 edit_free_file(file);
2358 return JB_ERR_MEMORY;
2361 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2363 if (err || (NULL == (s = strdup(section_template))))
2365 free_map(section_exports);
2367 free(section_template);
2368 edit_free_file(file);
2370 return JB_ERR_MEMORY;
2373 if (!err) err = template_fill(&s, section_exports);
2374 free_map(section_exports);
2375 if (!err) err = string_join(&buttons, s);
2380 freez(section_template);
2381 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2384 * Conventional actions file, supply extra editing help.
2385 * (e.g. don't allow them to make it an unconventional one).
2387 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2389 snprintf(buf, sizeof(buf), "%d", line_number);
2390 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2391 snprintf(buf, sizeof(buf), "%d", line_number + 2);
2392 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2393 if (!err) err = map(exports, "all-urls-actions", 1,
2394 actions_to_html(csp, cur_line->data.action), 0);
2396 /* Skip the 2 lines */
2397 cur_line = cur_line->next->next;
2401 * Note that prev_section_line_number is NOT set here.
2402 * This is deliberate and not a bug. It stops a "Move up"
2403 * option appearing on the next section. Clicking "Move
2404 * up" would make the actions file unconventional, which
2405 * we don't want, so we hide this option.
2411 * Non-standard actions file - does not begin with
2412 * the "All URLs" section.
2414 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2417 /* Set up global exports */
2419 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2420 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2421 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2423 /* Discourage private additions to default.action */
2425 if (!err) err = map_conditional(exports, "default-action",
2426 (strstr("default.action", file->filename) != NULL));
2429 edit_free_file(file);
2434 /* Should do all global exports above this point */
2436 /* Load templates */
2438 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2441 edit_free_file(file);
2443 if (err == JB_ERR_FILE)
2445 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2450 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2453 free(section_template);
2454 edit_free_file(file);
2456 if (err == JB_ERR_FILE)
2458 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2463 err = template_fill(§ion_template, exports);
2467 edit_free_file(file);
2472 err = template_fill(&url_template, exports);
2475 free(section_template);
2476 edit_free_file(file);
2481 if (NULL == (sections = strdup("")))
2483 free(section_template);
2485 edit_free_file(file);
2487 return JB_ERR_MEMORY;
2490 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2492 if (NULL == (section_exports = new_map()))
2495 free(section_template);
2497 edit_free_file(file);
2499 return JB_ERR_MEMORY;
2502 snprintf(buf, sizeof(buf), "%d", line_number);
2503 err = map(section_exports, "s", 1, buf, 1);
2504 if (!err) err = map(section_exports, "actions", 1,
2505 actions_to_html(csp, cur_line->data.action), 0);
2508 && (cur_line->next != NULL)
2509 && (cur_line->next->type == FILE_LINE_URL))
2511 /* This section contains at least one URL, don't allow delete */
2512 err = map_block_killer(section_exports, "empty-section");
2516 if (!err) err = map_block_keep(section_exports, "empty-section");
2519 if (prev_section_line_number != ((unsigned)(-1)))
2521 /* Not last section */
2522 snprintf(buf, sizeof(buf), "%d", prev_section_line_number);
2523 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2524 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2529 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2531 prev_section_line_number = line_number;
2536 free(section_template);
2538 edit_free_file(file);
2540 free_map(section_exports);
2544 /* Should do all section-specific exports above this point */
2546 if (NULL == (urls = strdup("")))
2549 free(section_template);
2551 edit_free_file(file);
2553 free_map(section_exports);
2554 return JB_ERR_MEMORY;
2559 cur_line = cur_line->next;
2562 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2564 if (NULL == (url_exports = new_map()))
2568 free(section_template);
2570 edit_free_file(file);
2572 free_map(section_exports);
2573 return JB_ERR_MEMORY;
2576 snprintf(buf, sizeof(buf), "%d", line_number);
2577 err = map(url_exports, "p", 1, buf, 1);
2579 snprintf(buf, sizeof(buf), "%d", url_1_2);
2580 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2582 if (!err) err = map(url_exports, "url-html", 1,
2583 html_encode(cur_line->unprocessed), 0);
2584 if (!err) err = map(url_exports, "url", 1,
2585 url_encode(cur_line->unprocessed), 0);
2591 free(section_template);
2593 edit_free_file(file);
2595 free_map(section_exports);
2596 free_map(url_exports);
2600 if (NULL == (s = strdup(url_template)))
2604 free(section_template);
2606 edit_free_file(file);
2608 free_map(section_exports);
2609 free_map(url_exports);
2610 return JB_ERR_MEMORY;
2613 err = template_fill(&s, section_exports);
2614 if (!err) err = template_fill(&s, url_exports);
2615 if (!err) err = string_append(&urls, s);
2617 free_map(url_exports);
2624 free(section_template);
2626 edit_free_file(file);
2628 free_map(section_exports);
2632 url_1_2 = 3 - url_1_2;
2634 cur_line = cur_line->next;
2638 err = map(section_exports, "urls", 1, urls, 0);
2640 /* Could also do section-specific exports here, but it wouldn't be as fast */
2642 snprintf(buf, sizeof(buf), "%d", line_number);
2643 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2645 if ((cur_line != NULL)
2646 && (cur_line->type == FILE_LINE_ACTION))
2648 /* Not last section */
2649 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2654 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2660 free(section_template);
2662 edit_free_file(file);
2664 free_map(section_exports);
2668 if (NULL == (s = strdup(section_template)))
2671 free(section_template);
2673 edit_free_file(file);
2675 free_map(section_exports);
2676 return JB_ERR_MEMORY;
2679 err = template_fill(&s, section_exports);
2680 if (!err) err = string_append(§ions, s);
2683 free_map(section_exports);
2688 free(section_template);
2690 edit_free_file(file);
2696 edit_free_file(file);
2697 free(section_template);
2700 err = map(exports, "sections", 1, sections, 0);
2707 /* Could also do global exports here, but it wouldn't be as fast */
2709 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2713 /*********************************************************************
2715 * Function : cgi_edit_actions_for_url
2717 * Description : CGI function that edits the Actions list.
2720 * 1 : csp = Current client state (buffers, headers, etc...)
2721 * 2 : rsp = http_response data structure for output
2722 * 3 : parameters = map of cgi parameters
2724 * CGI Parameters : None
2726 * Returns : JB_ERR_OK on success
2727 * JB_ERR_MEMORY on out-of-memory
2728 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2729 * specified or not valid.
2731 *********************************************************************/
2732 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2733 struct http_response *rsp,
2734 const struct map *parameters)
2736 struct map * exports;
2738 struct editable_file * file;
2739 struct file_line * cur_line;
2740 unsigned line_number;
2742 struct re_filterfile_spec *filter_group;
2743 int i, have_filters = 0;
2745 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2747 return cgi_error_disabled(csp, rsp);
2750 err = get_number_param(csp, parameters, "s", §ionid);
2756 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2759 /* No filename specified, can't read file, modified, or out of memory. */
2760 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2763 cur_line = file->lines;
2765 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2767 cur_line = cur_line->next;
2770 if ( (cur_line == NULL)
2771 || (line_number != sectionid)
2773 || (cur_line->type != FILE_LINE_ACTION))
2775 /* Invalid "sectionid" parameter */
2776 edit_free_file(file);
2777 return JB_ERR_CGI_PARAMS;
2780 if (NULL == (exports = default_exports(csp, NULL)))
2782 edit_free_file(file);
2783 return JB_ERR_MEMORY;
2786 err = map(exports, "f", 1, stringify(file->identifier), 0);
2787 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2788 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2790 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2793 * XXX: Some browsers (at least IE6 and IE7) have an artificial URL
2794 * length limitation and ignore clicks on the Submit buttons if
2795 * the resulting GET URL would be longer than their limit.
2797 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
2798 * reached this limit and action editing stopped working in these
2799 * browsers (BR #1570678).
2801 * The config option split-large-forms works around this browser
2802 * bug (HTTP has no URL length limitation) by deviding the action
2803 * list form into multiple smaller ones. It means the URLs are shorter
2804 * and work in broken browsers as well, but the user can no longer change
2805 * all actions with one submit.
2807 * A better solution would be to switch to POST requests,
2808 * but this will do for now.
2810 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
2812 /* Generate multiple smaller form by killing the big one. */
2813 err = map_block_killer(exports, "one-form-only");
2817 /* Default: Generate one large form by killing the smaller ones. */
2818 err = map_block_killer(exports, "multiple-forms");
2821 for (i = 0; i < MAX_AF_FILES; i++)
2823 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2825 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
2831 #ifndef FEATURE_EXTERNAL_FILTERS
2832 if (!err) err = map_block_killer(exports, "external-content-filters");
2837 edit_free_file(file);
2842 if (0 == have_filters)
2844 err = map(exports, "filter-params", 1, "", 1);
2849 * List available filters and their settings.
2851 char *filter_template;
2852 int filter_identifier = 0;
2853 char *prepared_templates[MAX_FILTER_TYPES];
2855 for (i = 0; i < MAX_FILTER_TYPES; i++)
2857 prepared_templates[i] = strdup("");
2860 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2863 edit_free_file(file);
2865 if (err == JB_ERR_FILE)
2867 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2872 err = template_fill(&filter_template, exports);
2874 for (i = 0; i < MAX_AF_FILES; i++)
2876 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2878 filter_group = csp->rlist[i]->f;
2879 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2881 char current_mode = 'x';
2883 struct list_entry *filter_name;
2884 struct map *line_exports;
2885 const int type = filter_group->type;
2886 const int multi_action_index = filter_type_info[type].multi_action_index;
2888 assert(type < MAX_FILTER_TYPES);
2890 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
2891 while ((filter_name != NULL)
2892 && (0 != strcmp(filter_group->name, filter_name->str)))
2894 filter_name = filter_name->next;
2897 if (filter_name != NULL)
2903 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
2904 while ((filter_name != NULL)
2905 && (0 != strcmp(filter_group->name, filter_name->str)))
2907 filter_name = filter_name->next;
2909 if (filter_name != NULL)
2915 /* Generate a unique serial number */
2916 snprintf(number, sizeof(number), "%x", filter_identifier++);
2917 number[sizeof(number) - 1] = '\0';
2919 line_exports = new_map();
2920 if (line_exports == NULL)
2922 err = JB_ERR_MEMORY;
2928 if (!err) err = map(line_exports, "index", 1, number, 1);
2929 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2930 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2931 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2932 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
2933 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
2934 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
2938 filter_line = strdup(filter_template);
2939 if (filter_line == NULL) err = JB_ERR_MEMORY;
2941 if (!err) err = template_fill(&filter_line, line_exports);
2942 string_join(&prepared_templates[type], filter_line);
2944 free_map(line_exports);
2949 freez(filter_template);
2951 /* Replace all filter macros with the aggregated templates */
2952 for (i = 0; i < MAX_FILTER_TYPES; i++)
2955 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
2960 /* Free aggregated templates */
2961 for (i = 0; i < MAX_FILTER_TYPES; i++)
2963 freez(prepared_templates[i]);
2968 /* Check or uncheck the "disable all of this type" radio buttons. */
2969 for (i = 0; i < MAX_FILTER_TYPES; i++)
2971 const int a = filter_type_info[i].multi_action_index;
2972 const int disable_all = cur_line->data.action->multi_remove_all[a];
2974 err = map_radio(exports, filter_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
2977 edit_free_file(file);
2985 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2989 /*********************************************************************
2991 * Function : cgi_edit_actions_submit
2993 * Description : CGI function that actually edits the Actions list.
2996 * 1 : csp = Current client state (buffers, headers, etc...)
2997 * 2 : rsp = http_response data structure for output
2998 * 3 : parameters = map of cgi parameters
3000 * CGI Parameters : None
3002 * Returns : JB_ERR_OK on success
3003 * JB_ERR_MEMORY on out-of-memory
3004 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3005 * specified or not valid.
3007 *********************************************************************/
3008 jb_err cgi_edit_actions_submit(struct client_state *csp,
3009 struct http_response *rsp,
3010 const struct map *parameters)
3015 size_t newtext_size;
3017 struct editable_file * file;
3018 struct file_line * cur_line;
3019 unsigned line_number;
3022 int filter_identifier;
3024 const char * action_set_name;
3025 struct file_list * fl;
3026 struct url_actions * b;
3028 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3030 return cgi_error_disabled(csp, rsp);
3033 err = get_number_param(csp, parameters, "s", §ionid);
3039 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3042 /* No filename specified, can't read file, modified, or out of memory. */
3043 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3046 cur_line = file->lines;
3048 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3050 cur_line = cur_line->next;
3053 if ( (cur_line == NULL)
3054 || (line_number != sectionid)
3056 || (cur_line->type != FILE_LINE_ACTION))
3058 /* Invalid "sectionid" parameter */
3059 edit_free_file(file);
3060 return JB_ERR_CGI_PARAMS;
3063 get_string_param(parameters, "p", &action_set_name);
3064 if (action_set_name != NULL)
3066 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3068 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3070 for (b = b->next; NULL != b; b = b->next)
3072 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3074 copy_action(cur_line->data.action, b->action);
3080 edit_free_file(file);
3081 return JB_ERR_CGI_PARAMS;
3087 err = actions_from_radio(parameters, cur_line->data.action);
3093 edit_free_file(file);
3097 /* Check the "disable all of this type" parameters. */
3098 for (i = 0; i < MAX_FILTER_TYPES; i++)
3100 const int multi_action_index = filter_type_info[i].multi_action_index;
3101 const char ch = get_char_param(parameters, filter_type_info[i].disable_all_param);
3105 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3106 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3107 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3111 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3115 for (filter_identifier = 0; !err; filter_identifier++)
3122 * Filter state. Valid states are: 'Y' (active),
3123 * 'N' (inactive) and 'X' (no change).
3127 * Abbreviated filter type. Valid types are: 'F' (content filter),
3128 * 'S' (server-header filter) and 'C' (client-header filter).
3130 int multi_action_index = 0;
3132 /* Generate the keys */
3133 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3134 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3135 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3136 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3137 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3139 err = get_string_param(parameters, key_name, &name);
3148 type = get_char_param(parameters, key_type);
3152 multi_action_index = ACTION_MULTI_FILTER;
3155 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3158 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3161 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3164 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3167 log_error(LOG_LEVEL_ERROR,
3168 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3171 assert(multi_action_index);
3173 value = get_char_param(parameters, key_value);
3176 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3177 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3178 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3180 else if (value == 'N')
3182 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3183 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3185 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3186 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3189 else if (value == 'X')
3191 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3192 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3199 edit_free_file(file);
3203 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3206 edit_free_file(file);
3207 return JB_ERR_MEMORY;
3210 len = strlen(actiontext);
3214 * Empty action - must special-case this.
3215 * Simply setting len to 1 is sufficient...
3220 newtext_size = len + 2;
3221 newtext = malloc_or_die(newtext_size);
3222 strlcpy(newtext, actiontext, newtext_size);
3226 newtext[len + 1] = '\0';
3228 freez(cur_line->raw);
3229 freez(cur_line->unprocessed);
3230 cur_line->unprocessed = newtext;
3232 err = edit_write_file(file);
3235 /* Error writing file */
3236 if (err == JB_ERR_FILE)
3238 /* Read-only file. */
3239 err = cgi_error_file_read_only(csp, rsp, file->filename);
3241 edit_free_file(file);
3245 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3246 (long) time(NULL), file->identifier, sectionid);
3248 edit_free_file(file);
3250 return cgi_redirect(rsp, target);
3254 /*********************************************************************
3256 * Function : cgi_edit_actions_url
3258 * Description : CGI function that actually edits a URL pattern in
3262 * 1 : csp = Current client state (buffers, headers, etc...)
3263 * 2 : rsp = http_response data structure for output
3264 * 3 : parameters = map of cgi parameters
3267 * filename : Identifies the file to edit
3268 * ver : File's last-modified time
3269 * section : Line number of section to edit
3270 * pattern : Line number of pattern to edit
3271 * newval : New value for pattern
3273 * Returns : JB_ERR_OK on success
3274 * JB_ERR_MEMORY on out-of-memory
3275 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3276 * specified or not valid.
3278 *********************************************************************/
3279 jb_err cgi_edit_actions_url(struct client_state *csp,
3280 struct http_response *rsp,
3281 const struct map *parameters)
3285 struct editable_file * file;
3286 struct file_line * cur_line;
3287 unsigned line_number;
3288 unsigned section_start_line_number = 0;
3296 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3298 return cgi_error_disabled(csp, rsp);
3301 err = get_number_param(csp, parameters, "p", &patternid);
3308 return JB_ERR_CGI_PARAMS;
3311 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3317 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3320 /* No filename specified, can't read file, modified, or out of memory. */
3322 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3326 cur_line = file->lines;
3328 while ((cur_line != NULL) && (line_number < patternid))
3330 if (cur_line->type == FILE_LINE_ACTION)
3332 section_start_line_number = line_number;
3334 cur_line = cur_line->next;
3338 if ((cur_line == NULL)
3339 || (cur_line->type != FILE_LINE_URL))
3341 /* Invalid "patternid" parameter */
3343 edit_free_file(file);
3344 return JB_ERR_CGI_PARAMS;
3347 /* At this point, the line to edit is in cur_line */
3349 freez(cur_line->raw);
3350 freez(cur_line->unprocessed);
3351 cur_line->unprocessed = new_pattern;
3353 err = edit_write_file(file);
3356 /* Error writing file */
3357 if (err == JB_ERR_FILE)
3359 /* Read-only file. */
3360 err = cgi_error_file_read_only(csp, rsp, file->filename);
3362 edit_free_file(file);
3366 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3367 (long) time(NULL), file->identifier, section_start_line_number);
3369 edit_free_file(file);
3371 return cgi_redirect(rsp, target);
3375 /*********************************************************************
3377 * Function : cgi_edit_actions_add_url
3379 * Description : CGI function that actually adds a URL pattern to
3383 * 1 : csp = Current client state (buffers, headers, etc...)
3384 * 2 : rsp = http_response data structure for output
3385 * 3 : parameters = map of cgi parameters
3388 * filename : Identifies the file to edit
3389 * ver : File's last-modified time
3390 * section : Line number of section to edit
3391 * newval : New pattern
3393 * Returns : JB_ERR_OK on success
3394 * JB_ERR_MEMORY on out-of-memory
3395 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3396 * specified or not valid.
3398 *********************************************************************/
3399 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3400 struct http_response *rsp,
3401 const struct map *parameters)
3405 struct file_line * new_line;
3406 struct editable_file * file;
3407 struct file_line * cur_line;
3408 unsigned line_number;
3412 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3414 return cgi_error_disabled(csp, rsp);
3417 err = get_number_param(csp, parameters, "s", §ionid);
3424 return JB_ERR_CGI_PARAMS;
3427 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3433 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3436 /* No filename specified, can't read file, modified, or out of memory. */
3438 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3442 cur_line = file->lines;
3444 while ((cur_line != NULL) && (line_number < sectionid))
3446 cur_line = cur_line->next;
3450 if ((cur_line == NULL)
3451 || (cur_line->type != FILE_LINE_ACTION))
3453 /* Invalid "sectionid" parameter */
3455 edit_free_file(file);
3456 return JB_ERR_CGI_PARAMS;
3459 /* At this point, the section header is in cur_line - add after this. */
3461 /* Allocate the new line */
3462 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3463 if (new_line == NULL)
3466 edit_free_file(file);
3467 return JB_ERR_MEMORY;
3470 /* Fill in the data members of the new line */
3471 new_line->raw = NULL;
3472 new_line->prefix = NULL;
3473 new_line->unprocessed = new_pattern;
3474 new_line->type = FILE_LINE_URL;
3476 /* Link new_line into the list, after cur_line */
3477 new_line->next = cur_line->next;
3478 cur_line->next = new_line;
3480 /* Done making changes, now commit */
3482 err = edit_write_file(file);
3485 /* Error writing file */
3486 if (err == JB_ERR_FILE)
3488 /* Read-only file. */
3489 err = cgi_error_file_read_only(csp, rsp, file->filename);
3491 edit_free_file(file);
3495 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3496 (long) time(NULL), file->identifier, sectionid);
3498 edit_free_file(file);
3500 return cgi_redirect(rsp, target);
3504 /*********************************************************************
3506 * Function : cgi_edit_actions_remove_url
3508 * Description : CGI function that actually removes a URL pattern from
3512 * 1 : csp = Current client state (buffers, headers, etc...)
3513 * 2 : rsp = http_response data structure for output
3514 * 3 : parameters = map of cgi parameters
3517 * f : (filename) Identifies the file to edit
3518 * v : (version) File's last-modified time
3519 * p : (pattern) Line number of pattern to remove
3521 * Returns : JB_ERR_OK on success
3522 * JB_ERR_MEMORY on out-of-memory
3523 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3524 * specified or not valid.
3526 *********************************************************************/
3527 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3528 struct http_response *rsp,
3529 const struct map *parameters)
3532 struct editable_file * file;
3533 struct file_line * cur_line;
3534 struct file_line * prev_line;
3535 unsigned line_number;
3536 unsigned section_start_line_number = 0;
3540 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3542 return cgi_error_disabled(csp, rsp);
3545 err = get_number_param(csp, parameters, "p", &patternid);
3551 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3554 /* No filename specified, can't read file, modified, or out of memory. */
3555 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3560 cur_line = file->lines;
3562 while ((cur_line != NULL) && (line_number < patternid))
3564 if (cur_line->type == FILE_LINE_ACTION)
3566 section_start_line_number = line_number;
3568 prev_line = cur_line;
3569 cur_line = cur_line->next;
3573 if ( (cur_line == NULL)
3574 || (prev_line == NULL)
3575 || (cur_line->type != FILE_LINE_URL))
3577 /* Invalid "patternid" parameter */
3578 edit_free_file(file);
3579 return JB_ERR_CGI_PARAMS;
3582 /* At this point, the line to remove is in cur_line, and the previous
3583 * one is in prev_line
3586 /* Unlink cur_line */
3587 prev_line->next = cur_line->next;
3588 cur_line->next = NULL;
3591 edit_free_file_lines(cur_line);
3593 err = edit_write_file(file);
3596 /* Error writing file */
3597 if (err == JB_ERR_FILE)
3599 /* Read-only file. */
3600 err = cgi_error_file_read_only(csp, rsp, file->filename);
3602 edit_free_file(file);
3606 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3607 (long) time(NULL), file->identifier, section_start_line_number);
3609 edit_free_file(file);
3611 return cgi_redirect(rsp, target);
3615 /*********************************************************************
3617 * Function : cgi_edit_actions_section_remove
3619 * Description : CGI function that actually removes a whole section from
3620 * the actions file. The section must be empty first
3621 * (else JB_ERR_CGI_PARAMS).
3624 * 1 : csp = Current client state (buffers, headers, etc...)
3625 * 2 : rsp = http_response data structure for output
3626 * 3 : parameters = map of cgi parameters
3629 * f : (filename) Identifies the file to edit
3630 * v : (version) File's last-modified time
3631 * s : (section) Line number of section to edit
3633 * Returns : JB_ERR_OK on success
3634 * JB_ERR_MEMORY on out-of-memory
3635 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3636 * specified or not valid.
3638 *********************************************************************/
3639 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3640 struct http_response *rsp,
3641 const struct map *parameters)
3644 struct editable_file * file;
3645 struct file_line * cur_line;
3646 struct file_line * prev_line;
3647 unsigned line_number;
3651 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3653 return cgi_error_disabled(csp, rsp);
3656 err = get_number_param(csp, parameters, "s", §ionid);
3662 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3665 /* No filename specified, can't read file, modified, or out of memory. */
3666 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3670 cur_line = file->lines;
3673 while ((cur_line != NULL) && (line_number < sectionid))
3675 prev_line = cur_line;
3676 cur_line = cur_line->next;
3680 if ((cur_line == NULL)
3681 || (cur_line->type != FILE_LINE_ACTION))
3683 /* Invalid "sectionid" parameter */
3684 edit_free_file(file);
3685 return JB_ERR_CGI_PARAMS;
3688 if ((cur_line->next != NULL)
3689 && (cur_line->next->type == FILE_LINE_URL))
3691 /* Section not empty. */
3692 edit_free_file(file);
3693 return JB_ERR_CGI_PARAMS;
3696 /* At this point, the line to remove is in cur_line, and the previous
3697 * one is in prev_line
3700 /* Unlink cur_line */
3701 if (prev_line == NULL)
3703 /* Removing the first line from the file */
3704 file->lines = cur_line->next;
3708 prev_line->next = cur_line->next;
3710 cur_line->next = NULL;
3713 edit_free_file_lines(cur_line);
3715 err = edit_write_file(file);
3718 /* Error writing file */
3719 if (err == JB_ERR_FILE)
3721 /* Read-only file. */
3722 err = cgi_error_file_read_only(csp, rsp, file->filename);
3724 edit_free_file(file);
3728 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
3729 (long) time(NULL), file->identifier);
3731 edit_free_file(file);
3733 return cgi_redirect(rsp, target);
3737 /*********************************************************************
3739 * Function : cgi_edit_actions_section_add
3741 * Description : CGI function that adds a new empty section to
3745 * 1 : csp = Current client state (buffers, headers, etc...)
3746 * 2 : rsp = http_response data structure for output
3747 * 3 : parameters = map of cgi parameters
3750 * f : (filename) Identifies the file to edit
3751 * v : (version) File's last-modified time
3752 * s : (section) Line number of section to add after, 0 for
3755 * Returns : JB_ERR_OK on success
3756 * JB_ERR_MEMORY on out-of-memory
3757 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3758 * specified or not valid.
3760 *********************************************************************/
3761 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3762 struct http_response *rsp,
3763 const struct map *parameters)
3766 struct file_line * new_line;
3768 struct editable_file * file;
3769 struct file_line * cur_line;
3770 unsigned line_number;
3774 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3776 return cgi_error_disabled(csp, rsp);
3779 err = get_number_param(csp, parameters, "s", §ionid);
3785 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3788 /* No filename specified, can't read file, modified, or out of memory. */
3789 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3793 cur_line = file->lines;
3795 if (sectionid <= 1U)
3797 /* Add to start of file */
3798 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
3800 /* There's something in the file, find the line before the first
3803 while ((cur_line->next != NULL)
3804 && (cur_line->next->type != FILE_LINE_ACTION))
3806 cur_line = cur_line->next;
3812 /* File starts with action line, so insert at top */
3818 /* Add after stated section. */
3819 while ((cur_line != NULL) && (line_number < sectionid))
3821 cur_line = cur_line->next;
3825 if ((cur_line == NULL)
3826 || (cur_line->type != FILE_LINE_ACTION))
3828 /* Invalid "sectionid" parameter */
3829 edit_free_file(file);
3830 return JB_ERR_CGI_PARAMS;
3833 /* Skip through the section to find the last line in it. */
3834 while ((cur_line->next != NULL)
3835 && (cur_line->next->type != FILE_LINE_ACTION))
3837 cur_line = cur_line->next;
3842 /* At this point, the last line in the previous section is in cur_line
3843 * - add after this. (Or if we need to add as the first line, cur_line
3847 new_text = strdup("{}");
3848 if (NULL == new_text)
3850 edit_free_file(file);
3851 return JB_ERR_MEMORY;
3854 /* Allocate the new line */
3855 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3856 if (new_line == NULL)
3859 edit_free_file(file);
3860 return JB_ERR_MEMORY;
3863 /* Fill in the data members of the new line */
3864 new_line->raw = NULL;
3865 new_line->prefix = NULL;
3866 new_line->unprocessed = new_text;
3867 new_line->type = FILE_LINE_ACTION;
3869 if (cur_line != NULL)
3871 /* Link new_line into the list, after cur_line */
3872 new_line->next = cur_line->next;
3873 cur_line->next = new_line;
3877 /* Link new_line into the list, as first line */
3878 new_line->next = file->lines;
3879 file->lines = new_line;
3882 /* Done making changes, now commit */
3884 err = edit_write_file(file);
3887 /* Error writing file */
3888 if (err == JB_ERR_FILE)
3890 /* Read-only file. */
3891 err = cgi_error_file_read_only(csp, rsp, file->filename);
3893 edit_free_file(file);
3897 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
3898 (long) time(NULL), file->identifier);
3900 edit_free_file(file);
3902 return cgi_redirect(rsp, target);
3906 /*********************************************************************
3908 * Function : cgi_edit_actions_section_swap
3910 * Description : CGI function that swaps the order of two sections
3911 * in the actions file. Note that this CGI can actually
3912 * swap any two arbitrary sections, but the GUI interface
3913 * currently only allows consecutive sections to be
3917 * 1 : csp = Current client state (buffers, headers, etc...)
3918 * 2 : rsp = http_response data structure for output
3919 * 3 : parameters = map of cgi parameters
3922 * f : (filename) Identifies the file to edit
3923 * v : (version) File's last-modified time
3924 * s1 : (section1) Line number of first section to swap
3925 * s2 : (section2) Line number of second section to swap
3927 * Returns : JB_ERR_OK on success
3928 * JB_ERR_MEMORY on out-of-memory
3929 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3930 * specified or not valid.
3932 *********************************************************************/
3933 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3934 struct http_response *rsp,
3935 const struct map *parameters)
3939 struct editable_file * file;
3940 struct file_line * cur_line;
3941 struct file_line * prev_line;
3942 struct file_line * line_before_section1;
3943 struct file_line * line_start_section1;
3944 struct file_line * line_end_section1;
3945 struct file_line * line_after_section1;
3946 struct file_line * line_before_section2;
3947 struct file_line * line_start_section2;
3948 struct file_line * line_end_section2;
3949 struct file_line * line_after_section2;
3950 unsigned line_number;
3954 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3956 return cgi_error_disabled(csp, rsp);
3959 err = get_number_param(csp, parameters, "s1", §ion1);
3960 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3966 if (section1 > section2)
3968 unsigned temp = section2;
3969 section2 = section1;
3973 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3976 /* No filename specified, can't read file, modified, or out of memory. */
3977 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3980 /* Start at the beginning... */
3982 cur_line = file->lines;
3985 /* ... find section1 ... */
3986 while ((cur_line != NULL) && (line_number < section1))
3988 prev_line = cur_line;
3989 cur_line = cur_line->next;
3993 if ((cur_line == NULL)
3994 || (cur_line->type != FILE_LINE_ACTION))
3996 /* Invalid "section1" parameter */
3997 edit_free_file(file);
3998 return JB_ERR_CGI_PARAMS;
4001 /* If no-op, we've validated params and can skip the rest. */
4002 if (section1 != section2)
4004 /* ... find the end of section1 ... */
4005 line_before_section1 = prev_line;
4006 line_start_section1 = cur_line;
4009 prev_line = cur_line;
4010 cur_line = cur_line->next;
4013 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4014 line_end_section1 = prev_line;
4015 line_after_section1 = cur_line;
4017 /* ... find section2 ... */
4018 while ((cur_line != NULL) && (line_number < section2))
4020 prev_line = cur_line;
4021 cur_line = cur_line->next;
4025 if ((cur_line == NULL)
4026 || (cur_line->type != FILE_LINE_ACTION))
4028 /* Invalid "section2" parameter */
4029 edit_free_file(file);
4030 return JB_ERR_CGI_PARAMS;
4033 /* ... find the end of section2 ... */
4034 line_before_section2 = prev_line;
4035 line_start_section2 = cur_line;
4038 prev_line = cur_line;
4039 cur_line = cur_line->next;
4042 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4043 line_end_section2 = prev_line;
4044 line_after_section2 = cur_line;
4046 /* Now have all the pointers we need. Do the swap. */
4048 /* Change the pointer to section1 to point to section2 instead */
4049 if (line_before_section1 == NULL)
4051 file->lines = line_start_section2;
4055 line_before_section1->next = line_start_section2;
4058 if (line_before_section2 == line_end_section1)
4060 /* Consecutive sections */
4061 line_end_section2->next = line_start_section1;
4065 line_end_section2->next = line_after_section1;
4066 line_before_section2->next = line_start_section1;
4069 /* Set the pointer from the end of section1 to the rest of the file */
4070 line_end_section1->next = line_after_section2;
4072 err = edit_write_file(file);
4075 /* Error writing file */
4076 if (err == JB_ERR_FILE)
4078 /* Read-only file. */
4079 err = cgi_error_file_read_only(csp, rsp, file->filename);
4081 edit_free_file(file);
4084 } /* END if (section1 != section2) */
4086 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4087 (long) time(NULL), file->identifier);
4089 edit_free_file(file);
4091 return cgi_redirect(rsp, target);
4095 /*********************************************************************
4097 * Function : javascriptify
4099 * Description : Converts a string into a form JavaScript will like.
4101 * Netscape 4's JavaScript sucks - it doesn't use
4102 * "id" parameters, so you have to set the "name"
4103 * used to submit a form element to something JavaScript
4104 * will like. (Or access the elements by index in an
4105 * array. That array contains >60 elements and will
4106 * be changed whenever we add a new action to the
4107 * editor, so I'm NOT going to use indexes that have
4108 * to be figured out by hand.)
4110 * Currently the only thing we have to worry about
4111 * is "-" ==> "_" conversion.
4113 * This is a length-preserving operation so it is
4114 * carried out in-place, no memory is allocated
4118 * 1 : identifier = String to make JavaScript-friendly.
4122 *********************************************************************/
4123 static void javascriptify(char * identifier)
4125 char * p = identifier;
4126 while (NULL != (p = strchr(p, '-')))
4133 /*********************************************************************
4135 * Function : actions_to_radio
4137 * Description : Converts a actionsfile entry into settings for
4138 * radio buttons and edit boxes on a HTML form.
4141 * 1 : exports = List of substitutions to add to.
4142 * 2 : action = Action to read
4144 * Returns : JB_ERR_OK on success
4145 * JB_ERR_MEMORY on out-of-memory
4147 *********************************************************************/
4148 static jb_err actions_to_radio(struct map * exports,
4149 const struct action_spec *action)
4160 mask = action->mask;
4163 /* sanity - prevents "-feature +feature" */
4167 #define DEFINE_ACTION_BOOL(name, bit) \
4168 if (!(mask & bit)) \
4170 current_mode = 'n'; \
4172 else if (add & bit) \
4174 current_mode = 'y'; \
4178 current_mode = 'x'; \
4180 if (map_radio(exports, name, "ynx", current_mode)) \
4182 return JB_ERR_MEMORY; \
4185 #define DEFINE_ACTION_STRING(name, bit, index) \
4186 DEFINE_ACTION_BOOL(name, bit); \
4189 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4192 checked = !strcmp(action->string[index], value); \
4196 checked = is_default; \
4198 mapped_param |= checked; \
4199 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4201 return JB_ERR_MEMORY; \
4204 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4205 if (map(exports, name "-param-custom", 1, \
4206 ((!mapped_param) ? "checked" : ""), 1)) \
4208 return JB_ERR_MEMORY; \
4210 if (map(exports, name "-param", 1, \
4211 (((add & bit) && !mapped_param) ? \
4212 action->string[index] : default_val), 1)) \
4214 return JB_ERR_MEMORY; \
4217 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4218 if (map(exports, name "-param", 1, \
4219 ((add & bit) ? action->string[index] : default_val), 1)) \
4221 return JB_ERR_MEMORY; \
4224 #define DEFINE_ACTION_MULTI(name, index) \
4225 if (action->multi_add[index]->first) \
4227 current_mode = 'y'; \
4229 else if (action->multi_remove_all[index]) \
4231 current_mode = 'n'; \
4233 else if (action->multi_remove[index]->first) \
4235 current_mode = 'y'; \
4239 current_mode = 'x'; \
4241 if (map_radio(exports, name, "ynx", current_mode)) \
4243 return JB_ERR_MEMORY; \
4246 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4248 #include "actionlist.h"
4250 #undef DEFINE_ACTION_MULTI
4251 #undef DEFINE_ACTION_STRING
4252 #undef DEFINE_ACTION_BOOL
4253 #undef DEFINE_ACTION_ALIAS
4254 #undef DEFINE_CGI_PARAM_CUSTOM
4255 #undef DEFINE_CGI_PARAM_RADIO
4256 #undef DEFINE_CGI_PARAM_NO_RADIO
4262 /*********************************************************************
4264 * Function : actions_from_radio
4266 * Description : Converts a map of parameters passed to a CGI function
4267 * into an actionsfile entry.
4270 * 1 : parameters = parameters to the CGI call
4271 * 2 : action = Action to change. Must be valid before
4272 * the call, actions not specified will be
4275 * Returns : JB_ERR_OK on success
4276 * JB_ERR_MEMORY on out-of-memory
4278 *********************************************************************/
4279 static jb_err actions_from_radio(const struct map * parameters,
4280 struct action_spec *action)
4285 const char * js_name;
4286 jb_err err = JB_ERR_OK;
4291 /* Statics are generally a potential race condition,
4292 * but in this case we're safe and don't need semaphores.
4293 * Be careful if you modify this function.
4295 * The js_name_arr's are never free()d, but this is no
4296 * problem, since they will only be created once and
4297 * used by all threads thereafter. -oes
4300 #define JAVASCRIPTIFY(dest_var, string) \
4302 static int first_time = 1; \
4303 static char *js_name_arr; \
4306 js_name_arr = strdup(string); \
4307 javascriptify(js_name_arr); \
4309 dest_var = js_name_arr; \
4313 #define DEFINE_ACTION_BOOL(name, bit) \
4314 JAVASCRIPTIFY(js_name, name); \
4315 ch = get_char_param(parameters, js_name); \
4318 action->add |= bit; \
4319 action->mask |= bit; \
4321 else if (ch == 'N') \
4323 action->add &= ~bit; \
4324 action->mask &= ~bit; \
4326 else if (ch == 'X') \
4328 action->add &= ~bit; \
4329 action->mask |= bit; \
4332 #define DEFINE_ACTION_STRING(name, bit, index) \
4333 JAVASCRIPTIFY(js_name, name); \
4334 ch = get_char_param(parameters, js_name); \
4338 JAVASCRIPTIFY(js_name, name "-mode"); \
4339 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4340 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4342 JAVASCRIPTIFY(js_name, name "-param"); \
4343 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4345 if (param != NULL) \
4347 if (NULL == (param_dup = strdup(param))) \
4349 return JB_ERR_MEMORY; \
4351 freez(action->string[index]); \
4352 action->add |= bit; \
4353 action->mask |= bit; \
4354 action->string[index] = param_dup; \
4357 else if (ch == 'N') \
4359 if (action->add & bit) \
4361 freez(action->string[index]); \
4363 action->add &= ~bit; \
4364 action->mask &= ~bit; \
4366 else if (ch == 'X') \
4368 if (action->add & bit) \
4370 freez(action->string[index]); \
4372 action->add &= ~bit; \
4373 action->mask |= bit; \
4376 #define DEFINE_ACTION_MULTI(name, index) \
4377 JAVASCRIPTIFY(js_name, name); \
4378 ch = get_char_param(parameters, js_name); \
4383 else if (ch == 'N') \
4385 list_remove_all(action->multi_add[index]); \
4386 list_remove_all(action->multi_remove[index]); \
4387 action->multi_remove_all[index] = 1; \
4389 else if (ch == 'X') \
4391 list_remove_all(action->multi_add[index]); \
4392 list_remove_all(action->multi_remove[index]); \
4393 action->multi_remove_all[index] = 0; \
4396 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4398 #include "actionlist.h"
4400 #undef DEFINE_ACTION_MULTI
4401 #undef DEFINE_ACTION_STRING
4402 #undef DEFINE_ACTION_BOOL
4403 #undef DEFINE_ACTION_ALIAS
4404 #undef JAVASCRIPTIFY
4408 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4411 #ifdef FEATURE_TOGGLE
4412 /*********************************************************************
4414 * Function : cgi_toggle
4416 * Description : CGI function that adds a new empty section to
4420 * 1 : csp = Current client state (buffers, headers, etc...)
4421 * 2 : rsp = http_response data structure for output
4422 * 3 : parameters = map of cgi parameters
4425 * set : If present, how to change toggle setting:
4426 * "enable", "disable", "toggle", or none (default).
4427 * mini : If present, use mini reply template.
4429 * Returns : JB_ERR_OK on success
4430 * JB_ERR_MEMORY on out-of-memory
4432 *********************************************************************/
4433 jb_err cgi_toggle(struct client_state *csp,
4434 struct http_response *rsp,
4435 const struct map *parameters)
4437 struct map *exports;
4439 const char *template_name;
4445 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4447 return cgi_error_disabled(csp, rsp);
4450 mode = get_char_param(parameters, "set");
4455 global_toggle_state = 1;
4457 else if (mode == 'D')
4460 global_toggle_state = 0;
4462 else if (mode == 'T')
4465 global_toggle_state = !global_toggle_state;
4468 if (NULL == (exports = default_exports(csp, "toggle")))
4470 return JB_ERR_MEMORY;
4473 template_name = (get_char_param(parameters, "mini")
4477 return template_fill_for_cgi(csp, template_name, exports, rsp);
4479 #endif /* def FEATURE_TOGGLE */