1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.73 2012/03/09 16:23:50 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-2008 the SourceForge
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. */
124 /* Add more data types here... e.g.
127 struct url_spec url[1];
131 struct action_spec action[1];
141 /** This file_line has not been processed yet. */
142 #define FILE_LINE_UNPROCESSED 1
144 /** This file_line is blank. Can only appear at the end of a file, due to
145 the way the parser works. */
146 #define FILE_LINE_BLANK 2
148 /** This file_line says {{alias}}. */
149 #define FILE_LINE_ALIAS_HEADER 3
151 /** This file_line defines an alias. */
152 #define FILE_LINE_ALIAS_ENTRY 4
154 /** This file_line defines an {action}. */
155 #define FILE_LINE_ACTION 5
157 /** This file_line specifies a URL pattern. */
158 #define FILE_LINE_URL 6
160 /** This file_line says {{settings}}. */
161 #define FILE_LINE_SETTINGS_HEADER 7
163 /** This file_line is in a {{settings}} block. */
164 #define FILE_LINE_SETTINGS_ENTRY 8
166 /** This file_line says {{description}}. */
167 #define FILE_LINE_DESCRIPTION_HEADER 9
169 /** This file_line is in a {{description}} block. */
170 #define FILE_LINE_DESCRIPTION_ENTRY 10
173 * Number of file modification time mismatches
174 * before the CGI editor gets turned off.
176 #define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
179 * A configuration file, in a format that can be edited and written back to
184 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
185 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
186 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
187 const char * version_str; /**< Last modification time, as a string. For CGI param. */
188 /**< Can be used in URL without using url_param(). */
189 unsigned version; /**< Last modification time - prevents chaos with
190 the browser's "back" button. Note that this is a
191 time_t cast to an unsigned. When comparing, always
192 cast the time_t to an unsigned, and *NOT* vice-versa.
193 This may lose the top few bits, but they're not
194 significant anyway. */
195 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
196 Note that changing this after the file has been
197 read in will cause a mess. */
198 struct file_line * parse_error; /**< On parse error, this is the offending line. */
199 const char * parse_error_text; /**< On parse error, this is the problem.
200 (Statically allocated) */
204 * Information about the filter types.
205 * Used for macro replacement in cgi_edit_actions_for_url.
207 struct filter_type_info
209 const int multi_action_index; /**< The multi action index as defined in project.h */
210 const char *macro_name; /**< Name of the macro that has to be replaced
211 with the prepared templates.
212 For example "content-filter-params" */
213 const char *type; /**< Name of the filter type,
214 for example "server-header-filter". */
215 /* XXX: check if these two can be combined. */
216 const char *disable_all_option; /**< Name of the catch-all radio option that has
217 to be checked or unchecked for this filter type. */
218 const char *disable_all_param; /**< Name of the parameter that causes all filters of
219 this type to be disabled. */
220 const char *abbr_type; /**< Abbreviation of the filter type, usually the
221 first or second character capitalized */
222 const char *anchor; /**< Anchor for the User Manual link,
223 for example "SERVER-HEADER-FILTER" */
226 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
227 static const struct filter_type_info filter_type_info[] =
231 "content-filter-params", "filter",
232 "filter-all", "filter_all",
236 ACTION_MULTI_CLIENT_HEADER_FILTER,
237 "client-header-filter-params", "client-header-filter",
238 "client-header-filter-all", "client_header_filter_all",
239 "C", "CLIENT-HEADER-FILTER"
242 ACTION_MULTI_SERVER_HEADER_FILTER,
243 "server-header-filter-params", "server-header-filter",
244 "server-header-filter-all", "server_header_filter_all",
245 "S", "SERVER-HEADER-FILTER"
248 ACTION_MULTI_CLIENT_HEADER_TAGGER,
249 "client-header-tagger-params", "client-header-tagger",
250 "client-header-tagger-all", "client_header_tagger_all",
251 "L", "CLIENT-HEADER-TAGGER"
254 ACTION_MULTI_SERVER_HEADER_TAGGER,
255 "server-header-tagger-params", "server-header-tagger",
256 "server-header-tagger-all", "server_header_tagger_all",
257 "E", "SERVER-HEADER-TAGGER"
261 /* FIXME: Following non-static functions should be prototyped in .h or made static */
263 /* Functions to read and write arbitrary config files */
264 jb_err edit_read_file(struct client_state *csp,
265 const struct map *parameters,
267 struct editable_file **pfile);
268 jb_err edit_write_file(struct editable_file * file);
269 void edit_free_file(struct editable_file * file);
271 /* Functions to read and write actions files */
272 jb_err edit_parse_actions_file(struct editable_file * file);
273 jb_err edit_read_actions_file(struct client_state *csp,
274 struct http_response *rsp,
275 const struct map *parameters,
277 struct editable_file **pfile);
280 jb_err cgi_error_modified(struct client_state *csp,
281 struct http_response *rsp,
282 const char *filename);
283 jb_err cgi_error_parse(struct client_state *csp,
284 struct http_response *rsp,
285 struct editable_file *file);
286 jb_err cgi_error_file(struct client_state *csp,
287 struct http_response *rsp,
288 const char *filename);
289 jb_err cgi_error_file_read_only(struct client_state *csp,
290 struct http_response *rsp,
291 const char *filename);
293 /* Internal arbitrary config file support functions */
294 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
295 static void edit_free_file_lines(struct file_line * first_line);
297 /* Internal actions file support functions */
298 static int match_actions_file_header_line(const char * line, const char * name);
299 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
301 /* Internal parameter parsing functions */
302 static jb_err get_url_spec_param(struct client_state *csp,
303 const struct map *parameters,
308 /* Internal actionsfile <==> HTML conversion functions */
309 static jb_err map_radio(struct map * exports,
310 const char * optionname,
313 static jb_err actions_to_radio(struct map * exports,
314 const struct action_spec *action);
315 static jb_err actions_from_radio(const struct map * parameters,
316 struct action_spec *action);
319 static jb_err map_copy_parameter_html(struct map *out,
320 const struct map *in,
323 static jb_err get_file_name_param(struct client_state *csp,
324 const struct map *parameters,
325 const char *param_name,
326 const char **pfilename);
328 /* Internal convenience functions */
329 static char *section_target(const unsigned sectionid);
331 /*********************************************************************
333 * Function : section_target
335 * Description : Given an unsigned (section id) n, produce a dynamically
336 * allocated string of the form #l<n>, for use in link
339 * XXX: The hash should be moved into the templates
340 * to make this function more generic and render
341 * stringify() obsolete.
344 * 1 : sectionid = start line number of section
346 * Returns : String with link target, or NULL if out of
349 *********************************************************************/
350 static char *section_target(const unsigned sectionid)
354 snprintf(buf, sizeof(buf), "#l%d", sectionid);
360 /*********************************************************************
362 * Function : stringify
364 * Description : Convert a number into a dynamically allocated string.
367 * 1 : number = The number to convert.
369 * Returns : String with link target, or NULL if out of memory
371 *********************************************************************/
372 static char *stringify(const unsigned number)
376 snprintf(buf, sizeof(buf), "%i", number);
381 /*********************************************************************
383 * Function : map_copy_parameter_html
385 * Description : Copy a CGI parameter from one map to another, HTML
389 * 1 : out = target map
390 * 2 : in = source map
391 * 3 : name = name of cgi parameter to copy
393 * Returns : JB_ERR_OK on success
394 * JB_ERR_MEMORY on out-of-memory
395 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
398 *********************************************************************/
399 static jb_err map_copy_parameter_html(struct map *out,
400 const struct map *in,
410 value = lookup(in, name);
411 err = map(out, name, 1, html_encode(value), 0);
418 else if (*value == '\0')
420 return JB_ERR_CGI_PARAMS;
429 /*********************************************************************
431 * Function : cgi_edit_actions_url_form
433 * Description : CGI function that displays a form for
437 * 1 : csp = Current client state (buffers, headers, etc...)
438 * 2 : rsp = http_response data structure for output
439 * 3 : parameters = map of cgi parameters
442 * i : (action index) Identifies the file to edit
443 * v : (version) File's last-modified time
444 * p : (pattern) Line number of pattern to edit
446 * Returns : JB_ERR_OK on success
447 * JB_ERR_MEMORY on out-of-memory
448 * JB_ERR_CGI_PARAMS if the CGI parameters are not
449 * specified or not valid.
451 *********************************************************************/
452 jb_err cgi_edit_actions_url_form(struct client_state *csp,
453 struct http_response *rsp,
454 const struct map *parameters)
456 struct map * exports;
458 struct editable_file * file;
459 struct file_line * cur_line;
460 unsigned line_number;
461 unsigned section_start_line_number = 0;
468 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
470 return cgi_error_disabled(csp, rsp);
473 err = get_number_param(csp, parameters, "p", &patternid);
479 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
482 /* No filename specified, can't read file, modified, or out of memory. */
483 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
486 cur_line = file->lines;
488 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
490 if (cur_line->type == FILE_LINE_ACTION)
492 section_start_line_number = line_number;
494 cur_line = cur_line->next;
497 if ((cur_line == NULL)
498 || (line_number != patternid)
500 || (cur_line->type != FILE_LINE_URL))
502 /* Invalid "patternid" parameter */
503 edit_free_file(file);
504 return JB_ERR_CGI_PARAMS;
507 if (NULL == (exports = default_exports(csp, NULL)))
509 edit_free_file(file);
510 return JB_ERR_MEMORY;
513 err = map(exports, "f", 1, stringify(file->identifier), 0);
514 if (!err) err = map(exports, "v", 1, file->version_str, 1);
515 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
516 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
517 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
519 edit_free_file(file);
527 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
531 /*********************************************************************
533 * Function : cgi_edit_actions_add_url_form
535 * Description : CGI function that displays a form for
539 * 1 : csp = Current client state (buffers, headers, etc...)
540 * 2 : rsp = http_response data structure for output
541 * 3 : parameters = map of cgi parameters
544 * f : (filename) Identifies the file to edit
545 * v : (version) File's last-modified time
546 * s : (section) Line number of section to edit
548 * Returns : JB_ERR_OK on success
549 * JB_ERR_MEMORY on out-of-memory
550 * JB_ERR_CGI_PARAMS if the CGI parameters are not
551 * specified or not valid.
553 *********************************************************************/
554 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
555 struct http_response *rsp,
556 const struct map *parameters)
565 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
567 return cgi_error_disabled(csp, rsp);
570 if (NULL == (exports = default_exports(csp, NULL)))
572 return JB_ERR_MEMORY;
575 err = map_copy_parameter_html(exports, parameters, "f");
576 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
577 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
585 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
589 /*********************************************************************
591 * Function : cgi_edit_actions_remove_url_form
593 * Description : CGI function that displays a form for
597 * 1 : csp = Current client state (buffers, headers, etc...)
598 * 2 : rsp = http_response data structure for output
599 * 3 : parameters = map of cgi parameters
602 * f : (number) The action file identifier.
603 * v : (version) File's last-modified time
604 * p : (pattern) Line number of pattern to edit
606 * Returns : JB_ERR_OK on success
607 * JB_ERR_MEMORY on out-of-memory
608 * JB_ERR_CGI_PARAMS if the CGI parameters are not
609 * specified or not valid.
611 *********************************************************************/
612 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
613 struct http_response *rsp,
614 const struct map *parameters)
616 struct map * exports;
618 struct editable_file * file;
619 struct file_line * cur_line;
620 unsigned line_number;
621 unsigned section_start_line_number = 0;
628 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
630 return cgi_error_disabled(csp, rsp);
633 err = get_number_param(csp, parameters, "p", &patternid);
639 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
642 /* No filename specified, can't read file, modified, or out of memory. */
643 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
646 cur_line = file->lines;
648 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
650 if (cur_line->type == FILE_LINE_ACTION)
652 section_start_line_number = line_number;
654 cur_line = cur_line->next;
657 if ((cur_line == NULL)
658 || (line_number != patternid)
660 || (cur_line->type != FILE_LINE_URL))
662 /* Invalid "patternid" parameter */
663 edit_free_file(file);
664 return JB_ERR_CGI_PARAMS;
667 if (NULL == (exports = default_exports(csp, NULL)))
669 edit_free_file(file);
670 return JB_ERR_MEMORY;
673 err = map(exports, "f", 1, stringify(file->identifier), 0);
674 if (!err) err = map(exports, "v", 1, file->version_str, 1);
675 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
676 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
677 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
678 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
680 edit_free_file(file);
688 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
692 /*********************************************************************
694 * Function : edit_write_file
696 * Description : Write a complete file to disk.
699 * 1 : file = File to write.
701 * Returns : JB_ERR_OK on success
702 * JB_ERR_FILE on error writing to file.
703 * JB_ERR_MEMORY on out of memory
705 *********************************************************************/
706 jb_err edit_write_file(struct editable_file * file)
709 struct file_line * cur_line;
710 struct stat statbuf[1];
711 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
712 digits in time_t, assuming this is a 64-bit
713 machine, plus null terminator, plus one
717 assert(file->filename);
719 if (NULL == (fp = fopen(file->filename, "wb")))
724 cur_line = file->lines;
725 while (cur_line != NULL)
729 if (fputs(cur_line->raw, fp) < 0)
737 if (cur_line->prefix)
739 if (fputs(cur_line->prefix, fp) < 0)
745 if (cur_line->unprocessed)
748 if (NULL != strchr(cur_line->unprocessed, '#'))
750 /* Must quote '#' characters */
757 /* Count number of # characters, so we know length of output string */
758 src = cur_line->unprocessed;
759 while (NULL != (src = strchr(src, '#')))
766 /* Allocate new memory for string */
767 len = strlen(cur_line->unprocessed) + (size_t)numhash;
768 if (NULL == (str = malloc(len + 1)))
770 /* Uh oh, just trashed file! */
772 return JB_ERR_MEMORY;
775 /* Copy string but quote hashes */
776 src = cur_line->unprocessed;
784 assert(numhash >= 0);
790 assert(numhash == 0);
791 assert(strlen(str) == len);
792 assert(str == dest - len);
793 assert(src - len <= cur_line->unprocessed);
795 if ((strlen(str) != len) || (numhash != 0))
798 * Escaping didn't work as expected, go spread the news.
799 * Only reached in non-debugging builds.
801 log_error(LOG_LEVEL_ERROR,
802 "Looks like hash escaping failed. %s might be corrupted now.",
806 if (fputs(str, fp) < 0)
817 /* Can write without quoting '#' characters. */
818 if (fputs(cur_line->unprocessed, fp) < 0)
824 if (fputs(NEWLINE(file->newline), fp) < 0)
832 /* FIXME: Write data from file->data->whatever */
836 cur_line = cur_line->next;
842 /* Update the version stamp in the file structure, since we just
843 * wrote to the file & changed it's date.
845 if (stat(file->filename, statbuf) < 0)
847 /* Error, probably file not found. */
850 file->version = (unsigned)statbuf->st_mtime;
852 /* Correct file->version_str */
853 freez(file->version_str);
854 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
855 version_buf[sizeof(version_buf)-1] = '\0';
856 file->version_str = strdup(version_buf);
857 if (version_buf == NULL)
859 return JB_ERR_MEMORY;
866 /*********************************************************************
868 * Function : edit_free_file
870 * Description : Free a complete file in memory.
873 * 1 : file = Data structure to free.
877 *********************************************************************/
878 void edit_free_file(struct editable_file * file)
882 /* Silently ignore NULL pointer */
886 edit_free_file_lines(file->lines);
887 freez(file->version_str);
889 file->parse_error_text = NULL; /* Statically allocated */
890 file->parse_error = NULL;
896 /*********************************************************************
898 * Function : edit_free_file_lines
900 * Description : Free an entire linked list of file lines.
903 * 1 : first_line = Data structure to free.
907 *********************************************************************/
908 static void edit_free_file_lines(struct file_line * first_line)
910 struct file_line * next_line;
912 while (first_line != NULL)
914 next_line = first_line->next;
915 first_line->next = NULL;
916 freez(first_line->raw);
917 freez(first_line->prefix);
918 freez(first_line->unprocessed);
919 switch(first_line->type)
921 case 0: /* special case if memory zeroed */
922 case FILE_LINE_UNPROCESSED:
923 case FILE_LINE_BLANK:
924 case FILE_LINE_ALIAS_HEADER:
925 case FILE_LINE_SETTINGS_HEADER:
926 case FILE_LINE_DESCRIPTION_HEADER:
927 case FILE_LINE_DESCRIPTION_ENTRY:
928 case FILE_LINE_ALIAS_ENTRY:
930 /* No data is stored for these */
933 case FILE_LINE_ACTION:
934 free_action(first_line->data.action);
937 case FILE_LINE_SETTINGS_ENTRY:
938 freez(first_line->data.setting.name);
939 freez(first_line->data.setting.svalue);
942 /* Should never happen */
946 first_line->type = 0; /* paranoia */
948 first_line = next_line;
953 /*********************************************************************
955 * Function : match_actions_file_header_line
957 * Description : Match an actions file {{header}} line
960 * 1 : line = String from file
961 * 2 : name = Header to match against
963 * Returns : 0 iff they match.
965 *********************************************************************/
966 static int match_actions_file_header_line(const char * line, const char * name)
974 if ((line[0] != '{') || (line[1] != '{'))
980 /* Look for optional whitespace */
981 while ((*line == ' ') || (*line == '\t'))
986 /* Look for the specified name (case-insensitive) */
988 if (0 != strncmpic(line, name, len))
994 /* Look for optional whitespace */
995 while ((*line == ' ') || (*line == '\t'))
1000 /* Look for "}}" and end of string*/
1001 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1011 /*********************************************************************
1013 * Function : match_actions_file_header_line
1015 * Description : Match an actions file {{header}} line
1018 * 1 : line = String from file. Must not start with
1019 * whitespace (else infinite loop!)
1020 * 2 : pname = Destination for name
1021 * 2 : pvalue = Destination for value
1023 * Returns : JB_ERR_OK on success
1024 * JB_ERR_MEMORY on out-of-memory
1025 * JB_ERR_PARSE if there's no "=" sign, or if there's
1026 * nothing before the "=" sign (but empty
1027 * values *after* the "=" sign are legal).
1029 *********************************************************************/
1030 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1032 const char * name_end;
1033 const char * value_start;
1039 assert(*line != ' ');
1040 assert(*line != '\t');
1045 value_start = strchr(line, '=');
1046 if ((value_start == NULL) || (value_start == line))
1048 return JB_ERR_PARSE;
1051 name_end = value_start - 1;
1053 /* Eat any whitespace before the '=' */
1054 while ((*name_end == ' ') || (*name_end == '\t'))
1057 * we already know we must have at least 1 non-ws char
1058 * at start of buf - no need to check
1063 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1064 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1066 return JB_ERR_MEMORY;
1068 strncpy(*pname, line, name_len);
1069 (*pname)[name_len] = '\0';
1071 /* Eat any the whitespace after the '=' */
1073 while ((*value_start == ' ') || (*value_start == '\t'))
1078 if (NULL == (*pvalue = strdup(value_start)))
1082 return JB_ERR_MEMORY;
1089 /*********************************************************************
1091 * Function : edit_parse_actions_file
1093 * Description : Parse an actions file in memory.
1095 * Passed linked list must have the "data" member
1096 * zeroed, and must contain valid "next" and
1097 * "unprocessed" fields. The "raw" and "prefix"
1098 * fields are ignored, and "type" is just overwritten.
1100 * Note that on error the file may have been
1104 * 1 : file = Actions file to be parsed in-place.
1106 * Returns : JB_ERR_OK on success
1107 * JB_ERR_MEMORY on out-of-memory
1108 * JB_ERR_PARSE on error
1110 *********************************************************************/
1111 jb_err edit_parse_actions_file(struct editable_file * file)
1113 struct file_line * cur_line;
1115 const char * text; /* Text from a line */
1116 char * name; /* For lines of the form name=value */
1117 char * value; /* For lines of the form name=value */
1118 struct action_alias * alias_list = NULL;
1119 jb_err err = JB_ERR_OK;
1121 /* alias_list contains the aliases defined in this file.
1122 * It might be better to use the "file_line.data" fields
1123 * in the relavent places instead.
1126 cur_line = file->lines;
1128 /* A note about blank line support: Blank lines should only
1129 * ever occur as the last line in the file. This function
1130 * is more forgiving than that - FILE_LINE_BLANK can occur
1134 /* Skip leading blanks. Should only happen if file is
1135 * empty (which is valid, but pointless).
1137 while ((cur_line != NULL)
1138 && (cur_line->unprocessed[0] == '\0'))
1141 cur_line->type = FILE_LINE_BLANK;
1142 cur_line = cur_line->next;
1145 if ((cur_line != NULL)
1146 && (cur_line->unprocessed[0] != '{'))
1148 /* File doesn't start with a header */
1149 file->parse_error = cur_line;
1150 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1151 return JB_ERR_PARSE;
1154 if ((cur_line != NULL) && (0 ==
1155 match_actions_file_header_line(cur_line->unprocessed, "settings")) )
1157 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1159 cur_line = cur_line->next;
1160 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1162 if (cur_line->unprocessed[0])
1164 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1166 err = split_line_on_equals(cur_line->unprocessed,
1167 &cur_line->data.setting.name,
1168 &cur_line->data.setting.svalue);
1169 if (err == JB_ERR_MEMORY)
1173 else if (err != JB_ERR_OK)
1175 /* Line does not contain a name=value pair */
1176 file->parse_error = cur_line;
1177 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1178 return JB_ERR_PARSE;
1183 cur_line->type = FILE_LINE_BLANK;
1185 cur_line = cur_line->next;
1189 if ((cur_line != NULL) && (0 ==
1190 match_actions_file_header_line(cur_line->unprocessed, "description")) )
1192 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1194 cur_line = cur_line->next;
1195 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1197 if (cur_line->unprocessed[0])
1199 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1203 cur_line->type = FILE_LINE_BLANK;
1205 cur_line = cur_line->next;
1209 if ((cur_line != NULL) && (0 ==
1210 match_actions_file_header_line(cur_line->unprocessed, "alias")) )
1212 cur_line->type = FILE_LINE_ALIAS_HEADER;
1214 cur_line = cur_line->next;
1215 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1217 if (cur_line->unprocessed[0])
1219 /* define an alias */
1220 struct action_alias * new_alias;
1222 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1224 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1225 if (err == JB_ERR_MEMORY)
1229 else if (err != JB_ERR_OK)
1231 /* Line does not contain a name=value pair */
1232 file->parse_error = cur_line;
1233 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1234 return JB_ERR_PARSE;
1237 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1242 free_alias_list(alias_list);
1243 return JB_ERR_MEMORY;
1246 err = get_actions(value, alias_list, new_alias->action);
1249 /* Invalid action or out of memory */
1253 free_alias_list(alias_list);
1254 if (err == JB_ERR_MEMORY)
1260 /* Line does not contain a name=value pair */
1261 file->parse_error = cur_line;
1262 file->parse_error_text = "This alias does not specify a valid set of actions.";
1263 return JB_ERR_PARSE;
1269 new_alias->name = name;
1272 new_alias->next = alias_list;
1273 alias_list = new_alias;
1277 cur_line->type = FILE_LINE_BLANK;
1279 cur_line = cur_line->next;
1283 /* Header done, process the main part of the file */
1284 while (cur_line != NULL)
1286 /* At this point, (cur_line->unprocessed[0] == '{') */
1287 assert(cur_line->unprocessed[0] == '{');
1288 text = cur_line->unprocessed + 1;
1289 len = strlen(text) - 1;
1290 if (text[len] != '}')
1292 /* No closing } on header */
1293 free_alias_list(alias_list);
1294 file->parse_error = cur_line;
1295 file->parse_error_text = "Headers starting with '{' must have a "
1296 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1297 "must close with two brackets ('}}').";
1298 return JB_ERR_PARSE;
1303 /* An invalid {{ header. */
1304 free_alias_list(alias_list);
1305 file->parse_error = cur_line;
1306 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1307 "Please remember that the system (two-bracket) headers must "
1308 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1309 "and must appear before any actions (one-bracket) headers. "
1310 "Also note that system headers may not be repeated.";
1311 return JB_ERR_PARSE;
1314 while ((*text == ' ') || (*text == '\t'))
1319 while ((len > (size_t)0)
1320 && ((text[len - 1] == ' ')
1321 || (text[len - 1] == '\t')) )
1326 cur_line->type = FILE_LINE_ACTION;
1328 /* Remove {} and make copy */
1329 if (NULL == (value = (char *) malloc(len + 1)))
1332 free_alias_list(alias_list);
1333 return JB_ERR_MEMORY;
1335 strncpy(value, text, len);
1339 err = get_actions(value, alias_list, cur_line->data.action);
1342 /* Invalid action or out of memory */
1344 free_alias_list(alias_list);
1345 if (err == JB_ERR_MEMORY)
1351 /* Line does not contain a name=value pair */
1352 file->parse_error = cur_line;
1353 file->parse_error_text = "This header does not specify a valid set of actions.";
1354 return JB_ERR_PARSE;
1358 /* Done with string - it was clobbered anyway */
1361 /* Process next line */
1362 cur_line = cur_line->next;
1364 /* Loop processing URL patterns */
1365 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1367 if (cur_line->unprocessed[0])
1369 /* Could parse URL here, but this isn't currently needed */
1371 cur_line->type = FILE_LINE_URL;
1375 cur_line->type = FILE_LINE_BLANK;
1377 cur_line = cur_line->next;
1379 } /* End main while(cur_line != NULL) loop */
1381 free_alias_list(alias_list);
1387 /*********************************************************************
1389 * Function : edit_read_file_lines
1391 * Description : Read all the lines of a file into memory.
1392 * Handles whitespace, comments and line continuation.
1395 * 1 : fp = File to read from. On return, this will be
1396 * at EOF but it will not have been closed.
1397 * 2 : pfile = Destination for a linked list of file_lines.
1398 * Will be set to NULL on error.
1399 * 3 : newline = How to handle newlines.
1401 * Returns : JB_ERR_OK on success
1402 * JB_ERR_MEMORY on out-of-memory
1404 *********************************************************************/
1405 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1407 struct file_line * first_line; /* Keep for return value or to free */
1408 struct file_line * cur_line; /* Current line */
1409 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1417 cur_line = first_line = zalloc(sizeof(struct file_line));
1418 if (cur_line == NULL)
1420 return JB_ERR_MEMORY;
1423 cur_line->type = FILE_LINE_UNPROCESSED;
1425 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1428 /* Out of memory or empty file. */
1429 /* Note that empty file is not an error we propagate up */
1431 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1436 prev_line = cur_line;
1437 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1438 if (cur_line == NULL)
1441 edit_free_file_lines(first_line);
1442 return JB_ERR_MEMORY;
1445 cur_line->type = FILE_LINE_UNPROCESSED;
1447 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1448 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1451 edit_free_file_lines(first_line);
1452 return JB_ERR_MEMORY;
1456 while (rval != JB_ERR_FILE);
1460 /* We allocated one too many - free it */
1461 prev_line->next = NULL;
1464 *pfile = first_line;
1469 /*********************************************************************
1471 * Function : edit_read_file
1473 * Description : Read a complete file into memory.
1474 * Handles CGI parameter parsing. If requested, also
1475 * checks the file's modification timestamp.
1478 * 1 : csp = Current client state (buffers, headers, etc...)
1479 * 2 : parameters = map of cgi parameters.
1480 * 3 : require_version = true to check "ver" parameter.
1481 * 4 : pfile = Destination for the file. Will be set
1485 * f : The action file identifier.
1486 * ver : (Only if require_version is nonzero)
1487 * Timestamp of the actions file. If wrong, this
1488 * function fails with JB_ERR_MODIFIED.
1490 * Returns : JB_ERR_OK on success
1491 * JB_ERR_MEMORY on out-of-memory
1492 * JB_ERR_CGI_PARAMS if "filename" was not specified
1494 * JB_ERR_FILE if the file cannot be opened or
1496 * JB_ERR_MODIFIED if version checking was requested and
1497 * failed - the file was modified outside
1498 * of this CGI editor instance.
1500 *********************************************************************/
1501 jb_err edit_read_file(struct client_state *csp,
1502 const struct map *parameters,
1503 int require_version,
1504 struct editable_file **pfile)
1506 struct file_line * lines;
1509 const char *filename = NULL;
1510 struct editable_file * file;
1511 unsigned version = 0;
1512 struct stat statbuf[1];
1513 char version_buf[22];
1514 int newline = NEWLINE_UNKNOWN;
1523 err = get_number_param(csp, parameters, "f", &i);
1524 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1526 filename = csp->config->actions_file[i];
1528 else if (JB_ERR_CGI_PARAMS == err)
1531 * Probably an old-school URL like
1532 * http://config.privoxy.org/edit-actions-list?f=default
1534 get_file_name_param(csp, parameters, "f", &filename);
1537 if (NULL == filename || stat(filename, statbuf) < 0)
1539 /* Error, probably file not found. */
1542 version = (unsigned) statbuf->st_mtime;
1544 if (require_version)
1546 unsigned specified_version;
1547 err = get_number_param(csp, parameters, "v", &specified_version);
1553 if (version != specified_version)
1555 return JB_ERR_MODIFIED;
1559 if (NULL == (fp = fopen(filename,"rb")))
1564 err = edit_read_file_lines(fp, &lines, &newline);
1573 file = (struct editable_file *) zalloc(sizeof(*file));
1576 edit_free_file_lines(lines);
1580 file->lines = lines;
1581 file->newline = newline;
1582 file->filename = filename;
1583 file->version = version;
1584 file->identifier = i;
1586 /* Correct file->version_str */
1587 freez(file->version_str);
1588 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1589 version_buf[sizeof(version_buf)-1] = '\0';
1590 file->version_str = strdup(version_buf);
1591 if (version_buf == NULL)
1593 edit_free_file(file);
1594 return JB_ERR_MEMORY;
1602 /*********************************************************************
1604 * Function : edit_read_actions_file
1606 * Description : Read a complete actions file into memory.
1607 * Handles CGI parameter parsing. If requested, also
1608 * checks the file's modification timestamp.
1610 * If this function detects an error in the categories
1611 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1612 * then it handles it by filling in the specified
1613 * response structure and returning JB_ERR_FILE.
1616 * 1 : csp = Current client state (buffers, headers, etc...)
1617 * 2 : rsp = HTTP response. Only filled in on error.
1618 * 2 : parameters = map of cgi parameters.
1619 * 3 : require_version = true to check "ver" parameter.
1620 * 4 : pfile = Destination for the file. Will be set
1624 * f : The actions file identifier.
1625 * ver : (Only if require_version is nonzero)
1626 * Timestamp of the actions file. If wrong, this
1627 * function fails with JB_ERR_MODIFIED.
1629 * Returns : JB_ERR_OK on success
1630 * JB_ERR_MEMORY on out-of-memory
1631 * JB_ERR_CGI_PARAMS if "filename" was not specified
1633 * JB_ERR_FILE if the file does not contain valid data,
1634 * or if file cannot be opened or
1635 * contains no data, or if version
1636 * checking was requested and failed.
1638 *********************************************************************/
1639 jb_err edit_read_actions_file(struct client_state *csp,
1640 struct http_response *rsp,
1641 const struct map *parameters,
1642 int require_version,
1643 struct editable_file **pfile)
1646 struct editable_file *file;
1647 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
1655 err = edit_read_file(csp, parameters, require_version, &file);
1658 /* Try to handle if possible */
1659 if (err == JB_ERR_FILE)
1661 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1663 else if (err == JB_ERR_MODIFIED)
1665 assert(require_version);
1666 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1667 log_error(LOG_LEVEL_ERROR,
1668 "Blocking CGI edit request due to modification time mismatch.");
1669 if (acceptable_failures > 0)
1671 log_error(LOG_LEVEL_INFO,
1672 "The CGI editor will be turned off after another %d mismatche(s).",
1673 acceptable_failures);
1674 acceptable_failures--;
1678 log_error(LOG_LEVEL_INFO,
1679 "Timestamp mismatch limit reached, turning CGI editor off. "
1680 "Reload the configuration file to re-enable it.");
1681 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
1684 if (err == JB_ERR_OK)
1687 * Signal to higher-level CGI code that there was a problem but we
1688 * handled it, they should just return JB_ERR_OK.
1695 err = edit_parse_actions_file(file);
1698 if (err == JB_ERR_PARSE)
1700 err = cgi_error_parse(csp, rsp, file);
1701 if (err == JB_ERR_OK)
1704 * Signal to higher-level CGI code that there was a problem but we
1705 * handled it, they should just return JB_ERR_OK.
1710 edit_free_file(file);
1719 /*********************************************************************
1721 * Function : get_file_name_param
1723 * Description : Get the name of the file to edit from the parameters
1724 * passed to a CGI function using the old syntax.
1725 * This function handles security checks and only
1726 * accepts files that Privoxy already knows.
1729 * 1 : csp = Current client state (buffers, headers, etc...)
1730 * 2 : parameters = map of cgi parameters
1731 * 3 : param_name = The name of the parameter to read
1732 * 4 : pfilename = pointer to the filename in
1733 * csp->config->actions_file[] if found. Set to NULL on error.
1735 * Returns : JB_ERR_OK on success
1736 * JB_ERR_MEMORY on out-of-memory
1737 * JB_ERR_CGI_PARAMS if "filename" was not specified
1740 *********************************************************************/
1741 static jb_err get_file_name_param(struct client_state *csp,
1742 const struct map *parameters,
1743 const char *param_name,
1744 const char **pfilename)
1747 const char suffix[] = ".action";
1762 param = lookup(parameters, param_name);
1765 return JB_ERR_CGI_PARAMS;
1768 len = strlen(param);
1769 if (len >= FILENAME_MAX)
1772 return JB_ERR_CGI_PARAMS;
1776 * Check every character to see if it's legal.
1777 * Totally unnecessary but we do it anyway.
1780 while ((ch = *s++) != '\0')
1782 if (((ch < 'A') || (ch > 'Z'))
1783 && ((ch < 'a') || (ch > 'z'))
1784 && ((ch < '0') || (ch > '9'))
1788 /* Probable hack attempt. */
1789 return JB_ERR_CGI_PARAMS;
1793 /* Append extension */
1794 name_size = len + strlen(suffix) + 1;
1795 name = malloc(name_size);
1798 return JB_ERR_MEMORY;
1800 strlcpy(name, param, name_size);
1801 strlcat(name, suffix, name_size);
1804 fullpath = make_path(csp->config->confdir, name);
1807 if (fullpath == NULL)
1809 return JB_ERR_MEMORY;
1812 /* Check if the file is known */
1813 for (i = 0; i < MAX_AF_FILES; i++)
1815 if (NULL != csp->config->actions_file[i] &&
1816 !strcmp(fullpath, csp->config->actions_file[i]))
1819 *pfilename = csp->config->actions_file[i];
1827 return JB_ERR_CGI_PARAMS;
1831 /*********************************************************************
1833 * Function : get_url_spec_param
1835 * Description : Get a URL pattern from the parameters
1836 * passed to a CGI function. Removes leading/trailing
1837 * spaces and validates it.
1840 * 1 : csp = Current client state (buffers, headers, etc...)
1841 * 2 : parameters = map of cgi parameters
1842 * 3 : name = Name of CGI parameter to read
1843 * 4 : pvalue = destination for value. Will be malloc()'d.
1844 * Set to NULL on error.
1846 * Returns : JB_ERR_OK on success
1847 * JB_ERR_MEMORY on out-of-memory
1848 * JB_ERR_CGI_PARAMS if the parameter was not specified
1851 *********************************************************************/
1852 static jb_err get_url_spec_param(struct client_state *csp,
1853 const struct map *parameters,
1857 const char *orig_param;
1860 struct url_spec compiled[1];
1870 orig_param = lookup(parameters, name);
1873 return JB_ERR_CGI_PARAMS;
1876 /* Copy and trim whitespace */
1877 param = strdup(orig_param);
1880 return JB_ERR_MEMORY;
1884 /* Must be non-empty, and can't allow 1st character to be '{' */
1885 if (param[0] == '\0' || param[0] == '{')
1888 return JB_ERR_CGI_PARAMS;
1891 /* Check for embedded newlines */
1892 for (s = param; *s != '\0'; s++)
1894 if ((*s == '\r') || (*s == '\n'))
1897 return JB_ERR_CGI_PARAMS;
1901 /* Check that regex is valid */
1906 return JB_ERR_MEMORY;
1908 err = create_url_spec(compiled, s);
1913 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1915 free_url_spec(compiled);
1917 if (param[strlen(param) - 1] == '\\')
1920 * Must protect trailing '\\' from becoming line continuation character.
1921 * Two methods: 1) If it's a domain only, add a trailing '/'.
1922 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1924 if (strchr(param, '/') == NULL)
1926 err = string_append(¶m, "/");
1930 err = string_append(¶m, "(?:)");
1937 /* Check that the modified regex is valid */
1942 return JB_ERR_MEMORY;
1944 err = create_url_spec(compiled, s);
1949 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1951 free_url_spec(compiled);
1958 /*********************************************************************
1960 * Function : map_radio
1962 * Description : Map a set of radio button values. E.g. if you have
1963 * 3 radio buttons, declare them as:
1964 * <option type="radio" name="xyz" @xyz-a@>
1965 * <option type="radio" name="xyz" @xyz-b@>
1966 * <option type="radio" name="xyz" @xyz-c@>
1967 * Then map one of the @xyz-?@ variables to "checked"
1968 * and all the others to empty by calling:
1969 * map_radio(exports, "xyz", "abc", sel)
1970 * Where 'sel' is 'a', 'b', or 'c'.
1973 * 1 : exports = Exports map to modify.
1974 * 2 : optionname = name for map
1975 * 3 : values = null-terminated list of values;
1976 * 4 : value = Selected value.
1978 * CGI Parameters : None
1980 * Returns : JB_ERR_OK on success
1981 * JB_ERR_MEMORY on out-of-memory
1983 *********************************************************************/
1984 static jb_err map_radio(struct map * exports,
1985 const char * optionname,
1986 const char * values,
1992 const size_t len = strlen(optionname);
1993 const size_t buf_size = len + 3;
1999 buf = malloc(buf_size);
2002 return JB_ERR_MEMORY;
2005 strlcpy(buf, optionname, buf_size);
2007 /* XXX: this looks ... interesting */
2012 while ((c = *values++) != '\0')
2017 if (map(exports, buf, 1, "", 1))
2019 return JB_ERR_MEMORY;
2025 return map(exports, buf, 0, "checked", 1);
2029 /*********************************************************************
2031 * Function : cgi_error_modified
2033 * Description : CGI function that is called when a file is modified
2034 * outside the CGI editor.
2037 * 1 : csp = Current client state (buffers, headers, etc...)
2038 * 2 : rsp = http_response data structure for output
2039 * 3 : filename = The file that was modified.
2041 * CGI Parameters : none
2043 * Returns : JB_ERR_OK on success
2044 * JB_ERR_MEMORY on out-of-memory error.
2046 *********************************************************************/
2047 jb_err cgi_error_modified(struct client_state *csp,
2048 struct http_response *rsp,
2049 const char *filename)
2051 struct map *exports;
2058 if (NULL == (exports = default_exports(csp, NULL)))
2060 return JB_ERR_MEMORY;
2063 err = map(exports, "f", 1, html_encode(filename), 0);
2070 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2074 /*********************************************************************
2076 * Function : cgi_error_parse
2078 * Description : CGI function that is called when a file cannot
2079 * be parsed by the CGI editor.
2082 * 1 : csp = Current client state (buffers, headers, etc...)
2083 * 2 : rsp = http_response data structure for output
2084 * 3 : file = The file that was modified.
2086 * CGI Parameters : none
2088 * Returns : JB_ERR_OK on success
2089 * JB_ERR_MEMORY on out-of-memory error.
2091 *********************************************************************/
2092 jb_err cgi_error_parse(struct client_state *csp,
2093 struct http_response *rsp,
2094 struct editable_file *file)
2096 struct map *exports;
2098 struct file_line *cur_line;
2104 if (NULL == (exports = default_exports(csp, NULL)))
2106 return JB_ERR_MEMORY;
2109 err = map(exports, "f", 1, stringify(file->identifier), 0);
2110 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2112 cur_line = file->parse_error;
2115 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2116 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2124 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2128 /*********************************************************************
2130 * Function : cgi_error_file
2132 * Description : CGI function that is called when a file cannot be
2133 * opened by the CGI editor.
2136 * 1 : csp = Current client state (buffers, headers, etc...)
2137 * 2 : rsp = http_response data structure for output
2138 * 3 : filename = The file that was modified.
2140 * CGI Parameters : none
2142 * Returns : JB_ERR_OK on success
2143 * JB_ERR_MEMORY on out-of-memory error.
2145 *********************************************************************/
2146 jb_err cgi_error_file(struct client_state *csp,
2147 struct http_response *rsp,
2148 const char *filename)
2150 struct map *exports;
2157 if (NULL == (exports = default_exports(csp, NULL)))
2159 return JB_ERR_MEMORY;
2162 err = map(exports, "f", 1, html_encode(filename), 0);
2169 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2173 /*********************************************************************
2175 * Function : cgi_error_file_read_only
2177 * Description : CGI function that is called when a file cannot be
2178 * opened for writing by the CGI editor.
2181 * 1 : csp = Current client state (buffers, headers, etc...)
2182 * 2 : rsp = http_response data structure for output
2183 * 3 : filename = The file that we can't write to
2185 * CGI Parameters : none
2187 * Returns : JB_ERR_OK on success
2188 * JB_ERR_MEMORY on out-of-memory error.
2190 *********************************************************************/
2191 jb_err cgi_error_file_read_only(struct client_state *csp,
2192 struct http_response *rsp,
2193 const char *filename)
2195 struct map *exports;
2202 if (NULL == (exports = default_exports(csp, NULL)))
2204 return JB_ERR_MEMORY;
2207 err = map(exports, "f", 1, html_encode(filename), 0);
2214 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2218 /*********************************************************************
2220 * Function : cgi_edit_actions
2222 * Description : CGI function that allows the user to choose which
2223 * actions file to edit.
2226 * 1 : csp = Current client state (buffers, headers, etc...)
2227 * 2 : rsp = http_response data structure for output
2228 * 3 : parameters = map of cgi parameters
2230 * CGI Parameters : None
2232 * Returns : JB_ERR_OK on success
2233 * JB_ERR_MEMORY on out-of-memory error
2235 *********************************************************************/
2236 jb_err cgi_edit_actions(struct client_state *csp,
2237 struct http_response *rsp,
2238 const struct map *parameters)
2242 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2244 return cgi_error_disabled(csp, rsp);
2247 /* FIXME: Incomplete */
2249 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2254 /*********************************************************************
2256 * Function : cgi_edit_actions_list
2258 * Description : CGI function that edits the actions list.
2259 * FIXME: This function shouldn't FATAL ever.
2260 * FIXME: This function doesn't check the retval of map()
2262 * 1 : csp = Current client state (buffers, headers, etc...)
2263 * 2 : rsp = http_response data structure for output
2264 * 3 : parameters = map of cgi parameters
2266 * CGI Parameters : filename
2268 * Returns : JB_ERR_OK on success
2269 * JB_ERR_MEMORY on out-of-memory
2270 * JB_ERR_FILE if the file cannot be opened or
2272 * JB_ERR_CGI_PARAMS if "filename" was not specified
2275 *********************************************************************/
2276 jb_err cgi_edit_actions_list(struct client_state *csp,
2277 struct http_response *rsp,
2278 const struct map *parameters)
2280 char * section_template;
2281 char * url_template;
2286 struct map * exports;
2287 struct map * section_exports;
2288 struct map * url_exports;
2289 struct editable_file * file;
2290 struct file_line * cur_line;
2291 unsigned line_number = 0;
2292 unsigned prev_section_line_number = ((unsigned) (-1));
2294 struct file_list * fl;
2295 struct url_actions * b;
2296 char * buttons = NULL;
2299 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2301 return cgi_error_disabled(csp, rsp);
2304 if (NULL == (exports = default_exports(csp, NULL)))
2306 return JB_ERR_MEMORY;
2309 /* Load actions file */
2310 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2313 /* No filename specified, can't read file, or out of memory. */
2315 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2318 /* Find start of actions in file */
2319 cur_line = file->lines;
2321 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2323 cur_line = cur_line->next;
2328 * Conventional actions files should have a match all block
2330 * cur_line = {...global actions...}
2331 * cur_line->next = /
2332 * cur_line->next->next = {...actions...} or EOF
2334 if ((cur_line != NULL)
2335 && (cur_line->type == FILE_LINE_ACTION)
2336 && (cur_line->next != NULL)
2337 && (cur_line->next->type == FILE_LINE_URL)
2338 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2339 && ((cur_line->next->next == NULL)
2340 || (cur_line->next->next->type != FILE_LINE_URL)
2344 * Generate string with buttons to set actions for "/" to
2345 * any predefined set of actions (named standard.*, probably
2346 * residing in standard.action).
2349 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2352 edit_free_file(file);
2354 if (err == JB_ERR_FILE)
2356 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2361 err = template_fill(§ion_template, exports);
2364 edit_free_file(file);
2369 buttons = strdup("");
2370 for (i = 0; i < MAX_AF_FILES; i++)
2372 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2374 for (b = b->next; NULL != b; b = b->next)
2376 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2378 if (err || (NULL == (section_exports = new_map())))
2381 free(section_template);
2382 edit_free_file(file);
2384 return JB_ERR_MEMORY;
2387 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2389 if (err || (NULL == (s = strdup(section_template))))
2391 free_map(section_exports);
2393 free(section_template);
2394 edit_free_file(file);
2396 return JB_ERR_MEMORY;
2399 if (!err) err = template_fill(&s, section_exports);
2400 free_map(section_exports);
2401 if (!err) err = string_join(&buttons, s);
2406 freez(section_template);
2407 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2410 * Conventional actions file, supply extra editing help.
2411 * (e.g. don't allow them to make it an unconventional one).
2413 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2415 snprintf(buf, sizeof(buf), "%d", line_number);
2416 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2417 snprintf(buf, sizeof(buf), "%d", line_number + 2);
2418 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2419 if (!err) err = map(exports, "all-urls-actions", 1,
2420 actions_to_html(csp, cur_line->data.action), 0);
2422 /* Skip the 2 lines */
2423 cur_line = cur_line->next->next;
2427 * Note that prev_section_line_number is NOT set here.
2428 * This is deliberate and not a bug. It stops a "Move up"
2429 * option appearing on the next section. Clicking "Move
2430 * up" would make the actions file unconventional, which
2431 * we don't want, so we hide this option.
2437 * Non-standard actions file - does not begin with
2438 * the "All URLs" section.
2440 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2443 /* Set up global exports */
2445 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2446 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2447 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2449 /* Discourage private additions to default.action */
2451 if (!err) err = map_conditional(exports, "default-action",
2452 (strstr("default.action", file->filename) != NULL));
2455 edit_free_file(file);
2460 /* Should do all global exports above this point */
2462 /* Load templates */
2464 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2467 edit_free_file(file);
2469 if (err == JB_ERR_FILE)
2471 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2476 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2479 free(section_template);
2480 edit_free_file(file);
2482 if (err == JB_ERR_FILE)
2484 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2489 err = template_fill(§ion_template, exports);
2493 edit_free_file(file);
2498 err = template_fill(&url_template, exports);
2501 free(section_template);
2502 edit_free_file(file);
2507 if (NULL == (sections = strdup("")))
2509 free(section_template);
2511 edit_free_file(file);
2513 return JB_ERR_MEMORY;
2516 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2518 if (NULL == (section_exports = new_map()))
2521 free(section_template);
2523 edit_free_file(file);
2525 return JB_ERR_MEMORY;
2528 snprintf(buf, sizeof(buf), "%d", line_number);
2529 err = map(section_exports, "s", 1, buf, 1);
2530 if (!err) err = map(section_exports, "actions", 1,
2531 actions_to_html(csp, cur_line->data.action), 0);
2534 && (cur_line->next != NULL)
2535 && (cur_line->next->type == FILE_LINE_URL))
2537 /* This section contains at least one URL, don't allow delete */
2538 err = map_block_killer(section_exports, "empty-section");
2542 if (!err) err = map_block_keep(section_exports, "empty-section");
2545 if (prev_section_line_number != ((unsigned)(-1)))
2547 /* Not last section */
2548 snprintf(buf, sizeof(buf), "%d", prev_section_line_number);
2549 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2550 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2555 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2557 prev_section_line_number = line_number;
2562 free(section_template);
2564 edit_free_file(file);
2566 free_map(section_exports);
2570 /* Should do all section-specific exports above this point */
2572 if (NULL == (urls = strdup("")))
2575 free(section_template);
2577 edit_free_file(file);
2579 free_map(section_exports);
2580 return JB_ERR_MEMORY;
2585 cur_line = cur_line->next;
2588 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2590 if (NULL == (url_exports = new_map()))
2594 free(section_template);
2596 edit_free_file(file);
2598 free_map(section_exports);
2599 return JB_ERR_MEMORY;
2602 snprintf(buf, sizeof(buf), "%d", line_number);
2603 err = map(url_exports, "p", 1, buf, 1);
2605 snprintf(buf, sizeof(buf), "%d", url_1_2);
2606 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2608 if (!err) err = map(url_exports, "url-html", 1,
2609 html_encode(cur_line->unprocessed), 0);
2610 if (!err) err = map(url_exports, "url", 1,
2611 url_encode(cur_line->unprocessed), 0);
2617 free(section_template);
2619 edit_free_file(file);
2621 free_map(section_exports);
2622 free_map(url_exports);
2626 if (NULL == (s = strdup(url_template)))
2630 free(section_template);
2632 edit_free_file(file);
2634 free_map(section_exports);
2635 free_map(url_exports);
2636 return JB_ERR_MEMORY;
2639 err = template_fill(&s, section_exports);
2640 if (!err) err = template_fill(&s, url_exports);
2641 if (!err) err = string_append(&urls, s);
2643 free_map(url_exports);
2650 free(section_template);
2652 edit_free_file(file);
2654 free_map(section_exports);
2658 url_1_2 = 3 - url_1_2;
2660 cur_line = cur_line->next;
2664 err = map(section_exports, "urls", 1, urls, 0);
2666 /* Could also do section-specific exports here, but it wouldn't be as fast */
2668 snprintf(buf, sizeof(buf), "%d", line_number);
2669 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2671 if ((cur_line != NULL)
2672 && (cur_line->type == FILE_LINE_ACTION))
2674 /* Not last section */
2675 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2680 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2686 free(section_template);
2688 edit_free_file(file);
2690 free_map(section_exports);
2694 if (NULL == (s = strdup(section_template)))
2697 free(section_template);
2699 edit_free_file(file);
2701 free_map(section_exports);
2702 return JB_ERR_MEMORY;
2705 err = template_fill(&s, section_exports);
2706 if (!err) err = string_append(§ions, s);
2709 free_map(section_exports);
2714 free(section_template);
2716 edit_free_file(file);
2722 edit_free_file(file);
2723 free(section_template);
2726 err = map(exports, "sections", 1, sections, 0);
2733 /* Could also do global exports here, but it wouldn't be as fast */
2735 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2739 /*********************************************************************
2741 * Function : cgi_edit_actions_for_url
2743 * Description : CGI function that edits the Actions list.
2746 * 1 : csp = Current client state (buffers, headers, etc...)
2747 * 2 : rsp = http_response data structure for output
2748 * 3 : parameters = map of cgi parameters
2750 * CGI Parameters : None
2752 * Returns : JB_ERR_OK on success
2753 * JB_ERR_MEMORY on out-of-memory
2754 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2755 * specified or not valid.
2757 *********************************************************************/
2758 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2759 struct http_response *rsp,
2760 const struct map *parameters)
2762 struct map * exports;
2764 struct editable_file * file;
2765 struct file_line * cur_line;
2766 unsigned line_number;
2768 struct re_filterfile_spec *filter_group;
2769 int i, have_filters = 0;
2771 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2773 return cgi_error_disabled(csp, rsp);
2776 err = get_number_param(csp, parameters, "s", §ionid);
2782 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2785 /* No filename specified, can't read file, modified, or out of memory. */
2786 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2789 cur_line = file->lines;
2791 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2793 cur_line = cur_line->next;
2796 if ((cur_line == NULL)
2797 || (line_number != sectionid)
2799 || (cur_line->type != FILE_LINE_ACTION))
2801 /* Invalid "sectionid" parameter */
2802 edit_free_file(file);
2803 return JB_ERR_CGI_PARAMS;
2806 if (NULL == (exports = default_exports(csp, NULL)))
2808 edit_free_file(file);
2809 return JB_ERR_MEMORY;
2812 err = map(exports, "f", 1, stringify(file->identifier), 0);
2813 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2814 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2816 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2819 * XXX: Some browsers (at least IE6 and IE7) have an artificial URL
2820 * length limitation and ignore clicks on the Submit buttons if
2821 * the resulting GET URL would be longer than their limit.
2823 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
2824 * reached this limit and action editing stopped working in these
2825 * browsers (BR #1570678).
2827 * The config option split-large-forms works around this browser
2828 * bug (HTTP has no URL length limitation) by deviding the action
2829 * list form into multiple smaller ones. It means the URLs are shorter
2830 * and work in broken browsers as well, but the user can no longer change
2831 * all actions with one submit.
2833 * A better solution would be to switch to POST requests,
2834 * but this will do for now.
2836 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
2838 /* Generate multiple smaller form by killing the big one. */
2839 err = map_block_killer(exports, "one-form-only");
2843 /* Default: Generate one large form by killing the smaller ones. */
2844 err = map_block_killer(exports, "multiple-forms");
2847 for (i = 0; i < MAX_AF_FILES; i++)
2849 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2851 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
2859 edit_free_file(file);
2864 if (0 == have_filters)
2866 err = map(exports, "filter-params", 1, "", 1);
2871 * List available filters and their settings.
2873 char *filter_template;
2874 int filter_identifier = 0;
2875 char *prepared_templates[MAX_FILTER_TYPES];
2877 for (i = 0; i < MAX_FILTER_TYPES; i++)
2879 prepared_templates[i] = strdup("");
2882 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2885 edit_free_file(file);
2887 if (err == JB_ERR_FILE)
2889 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2894 err = template_fill(&filter_template, exports);
2896 for (i = 0; i < MAX_AF_FILES; i++)
2898 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2900 filter_group = csp->rlist[i]->f;
2901 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2903 char current_mode = 'x';
2905 struct list_entry *filter_name;
2906 struct map *line_exports;
2907 const int type = filter_group->type;
2908 const int multi_action_index = filter_type_info[type].multi_action_index;
2910 assert(type < MAX_FILTER_TYPES);
2912 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
2913 while ((filter_name != NULL)
2914 && (0 != strcmp(filter_group->name, filter_name->str)))
2916 filter_name = filter_name->next;
2919 if (filter_name != NULL)
2925 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
2926 while ((filter_name != NULL)
2927 && (0 != strcmp(filter_group->name, filter_name->str)))
2929 filter_name = filter_name->next;
2931 if (filter_name != NULL)
2937 /* Generate a unique serial number */
2938 snprintf(number, sizeof(number), "%x", filter_identifier++);
2939 number[sizeof(number) - 1] = '\0';
2941 line_exports = new_map();
2942 if (line_exports == NULL)
2944 err = JB_ERR_MEMORY;
2950 if (!err) err = map(line_exports, "index", 1, number, 1);
2951 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2952 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2953 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2954 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
2955 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
2956 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
2960 filter_line = strdup(filter_template);
2961 if (filter_line == NULL) err = JB_ERR_MEMORY;
2963 if (!err) err = template_fill(&filter_line, line_exports);
2964 string_join(&prepared_templates[type], filter_line);
2966 free_map(line_exports);
2971 freez(filter_template);
2973 /* Replace all filter macros with the aggregated templates */
2974 for (i = 0; i < MAX_FILTER_TYPES; i++)
2977 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
2982 /* Free aggregated templates */
2983 for (i = 0; i < MAX_FILTER_TYPES; i++)
2985 freez(prepared_templates[i]);
2990 /* Check or uncheck the "disable all of this type" radio buttons. */
2991 for (i = 0; i < MAX_FILTER_TYPES; i++)
2993 const int a = filter_type_info[i].multi_action_index;
2994 const int disable_all = cur_line->data.action->multi_remove_all[a];
2996 err = map_radio(exports, filter_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
2999 edit_free_file(file);
3007 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3011 /*********************************************************************
3013 * Function : cgi_edit_actions_submit
3015 * Description : CGI function that actually edits the Actions list.
3018 * 1 : csp = Current client state (buffers, headers, etc...)
3019 * 2 : rsp = http_response data structure for output
3020 * 3 : parameters = map of cgi parameters
3022 * CGI Parameters : None
3024 * Returns : JB_ERR_OK on success
3025 * JB_ERR_MEMORY on out-of-memory
3026 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3027 * specified or not valid.
3029 *********************************************************************/
3030 jb_err cgi_edit_actions_submit(struct client_state *csp,
3031 struct http_response *rsp,
3032 const struct map *parameters)
3037 size_t newtext_size;
3039 struct editable_file * file;
3040 struct file_line * cur_line;
3041 unsigned line_number;
3044 int filter_identifier;
3046 const char * action_set_name;
3047 struct file_list * fl;
3048 struct url_actions * b;
3050 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3052 return cgi_error_disabled(csp, rsp);
3055 err = get_number_param(csp, parameters, "s", §ionid);
3061 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3064 /* No filename specified, can't read file, modified, or out of memory. */
3065 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3068 cur_line = file->lines;
3070 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3072 cur_line = cur_line->next;
3075 if ((cur_line == NULL)
3076 || (line_number != sectionid)
3078 || (cur_line->type != FILE_LINE_ACTION))
3080 /* Invalid "sectionid" parameter */
3081 edit_free_file(file);
3082 return JB_ERR_CGI_PARAMS;
3085 get_string_param(parameters, "p", &action_set_name);
3086 if (action_set_name != NULL)
3088 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3090 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3092 for (b = b->next; NULL != b; b = b->next)
3094 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3096 copy_action(cur_line->data.action, b->action);
3102 edit_free_file(file);
3103 return JB_ERR_CGI_PARAMS;
3109 err = actions_from_radio(parameters, cur_line->data.action);
3115 edit_free_file(file);
3119 /* Check the "disable all of this type" parameters. */
3120 for (i = 0; i < MAX_FILTER_TYPES; i++)
3122 const int multi_action_index = filter_type_info[i].multi_action_index;
3123 const char ch = get_char_param(parameters, filter_type_info[i].disable_all_param);
3127 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3128 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3129 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3133 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3137 for (filter_identifier = 0; !err; filter_identifier++)
3144 * Filter state. Valid states are: 'Y' (active),
3145 * 'N' (inactive) and 'X' (no change).
3149 * Abbreviated filter type. Valid types are: 'F' (content filter),
3150 * 'S' (server-header filter) and 'C' (client-header filter).
3152 int multi_action_index = 0;
3154 /* Generate the keys */
3155 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3156 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3157 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3158 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3159 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3161 err = get_string_param(parameters, key_name, &name);
3170 type = get_char_param(parameters, key_type);
3174 multi_action_index = ACTION_MULTI_FILTER;
3177 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3180 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3183 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3186 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3189 log_error(LOG_LEVEL_ERROR,
3190 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3193 assert(multi_action_index);
3195 value = get_char_param(parameters, key_value);
3198 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3199 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3200 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3202 else if (value == 'N')
3204 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3205 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3207 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3208 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3211 else if (value == 'X')
3213 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3214 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3221 edit_free_file(file);
3225 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3228 edit_free_file(file);
3229 return JB_ERR_MEMORY;
3232 len = strlen(actiontext);
3236 * Empty action - must special-case this.
3237 * Simply setting len to 1 is sufficient...
3242 newtext_size = len + 2;
3243 if (NULL == (newtext = malloc(newtext_size)))
3247 edit_free_file(file);
3248 return JB_ERR_MEMORY;
3250 strlcpy(newtext, actiontext, newtext_size);
3254 newtext[len + 1] = '\0';
3256 freez(cur_line->raw);
3257 freez(cur_line->unprocessed);
3258 cur_line->unprocessed = newtext;
3260 err = edit_write_file(file);
3263 /* Error writing file */
3264 if (err == JB_ERR_FILE)
3266 /* Read-only file. */
3267 err = cgi_error_file_read_only(csp, rsp, file->filename);
3269 edit_free_file(file);
3273 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3274 (long) time(NULL), file->identifier, sectionid);
3276 edit_free_file(file);
3278 return cgi_redirect(rsp, target);
3282 /*********************************************************************
3284 * Function : cgi_edit_actions_url
3286 * Description : CGI function that actually edits a URL pattern in
3290 * 1 : csp = Current client state (buffers, headers, etc...)
3291 * 2 : rsp = http_response data structure for output
3292 * 3 : parameters = map of cgi parameters
3295 * filename : Identifies the file to edit
3296 * ver : File's last-modified time
3297 * section : Line number of section to edit
3298 * pattern : Line number of pattern to edit
3299 * newval : New value for pattern
3301 * Returns : JB_ERR_OK on success
3302 * JB_ERR_MEMORY on out-of-memory
3303 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3304 * specified or not valid.
3306 *********************************************************************/
3307 jb_err cgi_edit_actions_url(struct client_state *csp,
3308 struct http_response *rsp,
3309 const struct map *parameters)
3313 struct editable_file * file;
3314 struct file_line * cur_line;
3315 unsigned line_number;
3316 unsigned section_start_line_number = 0;
3324 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3326 return cgi_error_disabled(csp, rsp);
3329 err = get_number_param(csp, parameters, "p", &patternid);
3336 return JB_ERR_CGI_PARAMS;
3339 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3345 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3348 /* No filename specified, can't read file, modified, or out of memory. */
3350 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3354 cur_line = file->lines;
3356 while ((cur_line != NULL) && (line_number < patternid))
3358 if (cur_line->type == FILE_LINE_ACTION)
3360 section_start_line_number = line_number;
3362 cur_line = cur_line->next;
3366 if ((cur_line == NULL)
3367 || (cur_line->type != FILE_LINE_URL))
3369 /* Invalid "patternid" parameter */
3371 edit_free_file(file);
3372 return JB_ERR_CGI_PARAMS;
3375 /* At this point, the line to edit is in cur_line */
3377 freez(cur_line->raw);
3378 freez(cur_line->unprocessed);
3379 cur_line->unprocessed = new_pattern;
3381 err = edit_write_file(file);
3384 /* Error writing file */
3385 if (err == JB_ERR_FILE)
3387 /* Read-only file. */
3388 err = cgi_error_file_read_only(csp, rsp, file->filename);
3390 edit_free_file(file);
3394 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3395 (long) time(NULL), file->identifier, section_start_line_number);
3397 edit_free_file(file);
3399 return cgi_redirect(rsp, target);
3403 /*********************************************************************
3405 * Function : cgi_edit_actions_add_url
3407 * Description : CGI function that actually adds a URL pattern to
3411 * 1 : csp = Current client state (buffers, headers, etc...)
3412 * 2 : rsp = http_response data structure for output
3413 * 3 : parameters = map of cgi parameters
3416 * filename : Identifies the file to edit
3417 * ver : File's last-modified time
3418 * section : Line number of section to edit
3419 * newval : New pattern
3421 * Returns : JB_ERR_OK on success
3422 * JB_ERR_MEMORY on out-of-memory
3423 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3424 * specified or not valid.
3426 *********************************************************************/
3427 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3428 struct http_response *rsp,
3429 const struct map *parameters)
3433 struct file_line * new_line;
3434 struct editable_file * file;
3435 struct file_line * cur_line;
3436 unsigned line_number;
3440 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3442 return cgi_error_disabled(csp, rsp);
3445 err = get_number_param(csp, parameters, "s", §ionid);
3452 return JB_ERR_CGI_PARAMS;
3455 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3461 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3464 /* No filename specified, can't read file, modified, or out of memory. */
3466 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3470 cur_line = file->lines;
3472 while ((cur_line != NULL) && (line_number < sectionid))
3474 cur_line = cur_line->next;
3478 if ((cur_line == NULL)
3479 || (cur_line->type != FILE_LINE_ACTION))
3481 /* Invalid "sectionid" parameter */
3483 edit_free_file(file);
3484 return JB_ERR_CGI_PARAMS;
3487 /* At this point, the section header is in cur_line - add after this. */
3489 /* Allocate the new line */
3490 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3491 if (new_line == NULL)
3494 edit_free_file(file);
3495 return JB_ERR_MEMORY;
3498 /* Fill in the data members of the new line */
3499 new_line->raw = NULL;
3500 new_line->prefix = NULL;
3501 new_line->unprocessed = new_pattern;
3502 new_line->type = FILE_LINE_URL;
3504 /* Link new_line into the list, after cur_line */
3505 new_line->next = cur_line->next;
3506 cur_line->next = new_line;
3508 /* Done making changes, now commit */
3510 err = edit_write_file(file);
3513 /* Error writing file */
3514 if (err == JB_ERR_FILE)
3516 /* Read-only file. */
3517 err = cgi_error_file_read_only(csp, rsp, file->filename);
3519 edit_free_file(file);
3523 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3524 (long) time(NULL), file->identifier, sectionid);
3526 edit_free_file(file);
3528 return cgi_redirect(rsp, target);
3532 /*********************************************************************
3534 * Function : cgi_edit_actions_remove_url
3536 * Description : CGI function that actually removes a URL pattern from
3540 * 1 : csp = Current client state (buffers, headers, etc...)
3541 * 2 : rsp = http_response data structure for output
3542 * 3 : parameters = map of cgi parameters
3545 * f : (filename) Identifies the file to edit
3546 * v : (version) File's last-modified time
3547 * p : (pattern) Line number of pattern to remove
3549 * Returns : JB_ERR_OK on success
3550 * JB_ERR_MEMORY on out-of-memory
3551 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3552 * specified or not valid.
3554 *********************************************************************/
3555 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3556 struct http_response *rsp,
3557 const struct map *parameters)
3560 struct editable_file * file;
3561 struct file_line * cur_line;
3562 struct file_line * prev_line;
3563 unsigned line_number;
3564 unsigned section_start_line_number = 0;
3568 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3570 return cgi_error_disabled(csp, rsp);
3573 err = get_number_param(csp, parameters, "p", &patternid);
3579 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3582 /* No filename specified, can't read file, modified, or out of memory. */
3583 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3588 cur_line = file->lines;
3590 while ((cur_line != NULL) && (line_number < patternid))
3592 if (cur_line->type == FILE_LINE_ACTION)
3594 section_start_line_number = line_number;
3596 prev_line = cur_line;
3597 cur_line = cur_line->next;
3601 if ((cur_line == NULL)
3602 || (prev_line == NULL)
3603 || (cur_line->type != FILE_LINE_URL))
3605 /* Invalid "patternid" parameter */
3606 edit_free_file(file);
3607 return JB_ERR_CGI_PARAMS;
3610 /* At this point, the line to remove is in cur_line, and the previous
3611 * one is in prev_line
3614 /* Unlink cur_line */
3615 prev_line->next = cur_line->next;
3616 cur_line->next = NULL;
3619 edit_free_file_lines(cur_line);
3621 err = edit_write_file(file);
3624 /* Error writing file */
3625 if (err == JB_ERR_FILE)
3627 /* Read-only file. */
3628 err = cgi_error_file_read_only(csp, rsp, file->filename);
3630 edit_free_file(file);
3634 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3635 (long) time(NULL), file->identifier, section_start_line_number);
3637 edit_free_file(file);
3639 return cgi_redirect(rsp, target);
3643 /*********************************************************************
3645 * Function : cgi_edit_actions_section_remove
3647 * Description : CGI function that actually removes a whole section from
3648 * the actions file. The section must be empty first
3649 * (else JB_ERR_CGI_PARAMS).
3652 * 1 : csp = Current client state (buffers, headers, etc...)
3653 * 2 : rsp = http_response data structure for output
3654 * 3 : parameters = map of cgi parameters
3657 * f : (filename) Identifies the file to edit
3658 * v : (version) File's last-modified time
3659 * s : (section) Line number of section to edit
3661 * Returns : JB_ERR_OK on success
3662 * JB_ERR_MEMORY on out-of-memory
3663 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3664 * specified or not valid.
3666 *********************************************************************/
3667 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3668 struct http_response *rsp,
3669 const struct map *parameters)
3672 struct editable_file * file;
3673 struct file_line * cur_line;
3674 struct file_line * prev_line;
3675 unsigned line_number;
3679 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3681 return cgi_error_disabled(csp, rsp);
3684 err = get_number_param(csp, parameters, "s", §ionid);
3690 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3693 /* No filename specified, can't read file, modified, or out of memory. */
3694 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3698 cur_line = file->lines;
3701 while ((cur_line != NULL) && (line_number < sectionid))
3703 prev_line = cur_line;
3704 cur_line = cur_line->next;
3708 if ((cur_line == NULL)
3709 || (cur_line->type != FILE_LINE_ACTION))
3711 /* Invalid "sectionid" parameter */
3712 edit_free_file(file);
3713 return JB_ERR_CGI_PARAMS;
3716 if ((cur_line->next != NULL)
3717 && (cur_line->next->type == FILE_LINE_URL))
3719 /* Section not empty. */
3720 edit_free_file(file);
3721 return JB_ERR_CGI_PARAMS;
3724 /* At this point, the line to remove is in cur_line, and the previous
3725 * one is in prev_line
3728 /* Unlink cur_line */
3729 if (prev_line == NULL)
3731 /* Removing the first line from the file */
3732 file->lines = cur_line->next;
3736 prev_line->next = cur_line->next;
3738 cur_line->next = NULL;
3741 edit_free_file_lines(cur_line);
3743 err = edit_write_file(file);
3746 /* Error writing file */
3747 if (err == JB_ERR_FILE)
3749 /* Read-only file. */
3750 err = cgi_error_file_read_only(csp, rsp, file->filename);
3752 edit_free_file(file);
3756 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
3757 (long) time(NULL), file->identifier);
3759 edit_free_file(file);
3761 return cgi_redirect(rsp, target);
3765 /*********************************************************************
3767 * Function : cgi_edit_actions_section_add
3769 * Description : CGI function that adds a new empty section to
3773 * 1 : csp = Current client state (buffers, headers, etc...)
3774 * 2 : rsp = http_response data structure for output
3775 * 3 : parameters = map of cgi parameters
3778 * f : (filename) Identifies the file to edit
3779 * v : (version) File's last-modified time
3780 * s : (section) Line number of section to add after, 0 for
3783 * Returns : JB_ERR_OK on success
3784 * JB_ERR_MEMORY on out-of-memory
3785 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3786 * specified or not valid.
3788 *********************************************************************/
3789 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3790 struct http_response *rsp,
3791 const struct map *parameters)
3794 struct file_line * new_line;
3796 struct editable_file * file;
3797 struct file_line * cur_line;
3798 unsigned line_number;
3802 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3804 return cgi_error_disabled(csp, rsp);
3807 err = get_number_param(csp, parameters, "s", §ionid);
3813 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3816 /* No filename specified, can't read file, modified, or out of memory. */
3817 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3821 cur_line = file->lines;
3823 if (sectionid <= 1U)
3825 /* Add to start of file */
3826 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
3828 /* There's something in the file, find the line before the first
3831 while ((cur_line->next != NULL)
3832 && (cur_line->next->type != FILE_LINE_ACTION))
3834 cur_line = cur_line->next;
3840 /* File starts with action line, so insert at top */
3846 /* Add after stated section. */
3847 while ((cur_line != NULL) && (line_number < sectionid))
3849 cur_line = cur_line->next;
3853 if ((cur_line == NULL)
3854 || (cur_line->type != FILE_LINE_ACTION))
3856 /* Invalid "sectionid" parameter */
3857 edit_free_file(file);
3858 return JB_ERR_CGI_PARAMS;
3861 /* Skip through the section to find the last line in it. */
3862 while ((cur_line->next != NULL)
3863 && (cur_line->next->type != FILE_LINE_ACTION))
3865 cur_line = cur_line->next;
3870 /* At this point, the last line in the previous section is in cur_line
3871 * - add after this. (Or if we need to add as the first line, cur_line
3875 new_text = strdup("{}");
3876 if (NULL == new_text)
3878 edit_free_file(file);
3879 return JB_ERR_MEMORY;
3882 /* Allocate the new line */
3883 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3884 if (new_line == NULL)
3887 edit_free_file(file);
3888 return JB_ERR_MEMORY;
3891 /* Fill in the data members of the new line */
3892 new_line->raw = NULL;
3893 new_line->prefix = NULL;
3894 new_line->unprocessed = new_text;
3895 new_line->type = FILE_LINE_ACTION;
3897 if (cur_line != NULL)
3899 /* Link new_line into the list, after cur_line */
3900 new_line->next = cur_line->next;
3901 cur_line->next = new_line;
3905 /* Link new_line into the list, as first line */
3906 new_line->next = file->lines;
3907 file->lines = new_line;
3910 /* Done making changes, now commit */
3912 err = edit_write_file(file);
3915 /* Error writing file */
3916 if (err == JB_ERR_FILE)
3918 /* Read-only file. */
3919 err = cgi_error_file_read_only(csp, rsp, file->filename);
3921 edit_free_file(file);
3925 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
3926 (long) time(NULL), file->identifier);
3928 edit_free_file(file);
3930 return cgi_redirect(rsp, target);
3934 /*********************************************************************
3936 * Function : cgi_edit_actions_section_swap
3938 * Description : CGI function that swaps the order of two sections
3939 * in the actions file. Note that this CGI can actually
3940 * swap any two arbitrary sections, but the GUI interface
3941 * currently only allows consecutive sections to be
3945 * 1 : csp = Current client state (buffers, headers, etc...)
3946 * 2 : rsp = http_response data structure for output
3947 * 3 : parameters = map of cgi parameters
3950 * f : (filename) Identifies the file to edit
3951 * v : (version) File's last-modified time
3952 * s1 : (section1) Line number of first section to swap
3953 * s2 : (section2) Line number of second section to swap
3955 * Returns : JB_ERR_OK on success
3956 * JB_ERR_MEMORY on out-of-memory
3957 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3958 * specified or not valid.
3960 *********************************************************************/
3961 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3962 struct http_response *rsp,
3963 const struct map *parameters)
3967 struct editable_file * file;
3968 struct file_line * cur_line;
3969 struct file_line * prev_line;
3970 struct file_line * line_before_section1;
3971 struct file_line * line_start_section1;
3972 struct file_line * line_end_section1;
3973 struct file_line * line_after_section1;
3974 struct file_line * line_before_section2;
3975 struct file_line * line_start_section2;
3976 struct file_line * line_end_section2;
3977 struct file_line * line_after_section2;
3978 unsigned line_number;
3982 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3984 return cgi_error_disabled(csp, rsp);
3987 err = get_number_param(csp, parameters, "s1", §ion1);
3988 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3994 if (section1 > section2)
3996 unsigned temp = section2;
3997 section2 = section1;
4001 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4004 /* No filename specified, can't read file, modified, or out of memory. */
4005 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4008 /* Start at the beginning... */
4010 cur_line = file->lines;
4013 /* ... find section1 ... */
4014 while ((cur_line != NULL) && (line_number < section1))
4016 prev_line = cur_line;
4017 cur_line = cur_line->next;
4021 if ((cur_line == NULL)
4022 || (cur_line->type != FILE_LINE_ACTION))
4024 /* Invalid "section1" parameter */
4025 edit_free_file(file);
4026 return JB_ERR_CGI_PARAMS;
4029 /* If no-op, we've validated params and can skip the rest. */
4030 if (section1 != section2)
4032 /* ... find the end of section1 ... */
4033 line_before_section1 = prev_line;
4034 line_start_section1 = cur_line;
4037 prev_line = cur_line;
4038 cur_line = cur_line->next;
4041 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4042 line_end_section1 = prev_line;
4043 line_after_section1 = cur_line;
4045 /* ... find section2 ... */
4046 while ((cur_line != NULL) && (line_number < section2))
4048 prev_line = cur_line;
4049 cur_line = cur_line->next;
4053 if ((cur_line == NULL)
4054 || (cur_line->type != FILE_LINE_ACTION))
4056 /* Invalid "section2" parameter */
4057 edit_free_file(file);
4058 return JB_ERR_CGI_PARAMS;
4061 /* ... find the end of section2 ... */
4062 line_before_section2 = prev_line;
4063 line_start_section2 = cur_line;
4066 prev_line = cur_line;
4067 cur_line = cur_line->next;
4070 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4071 line_end_section2 = prev_line;
4072 line_after_section2 = cur_line;
4074 /* Now have all the pointers we need. Do the swap. */
4076 /* Change the pointer to section1 to point to section2 instead */
4077 if (line_before_section1 == NULL)
4079 file->lines = line_start_section2;
4083 line_before_section1->next = line_start_section2;
4086 if (line_before_section2 == line_end_section1)
4088 /* Consecutive sections */
4089 line_end_section2->next = line_start_section1;
4093 line_end_section2->next = line_after_section1;
4094 line_before_section2->next = line_start_section1;
4097 /* Set the pointer from the end of section1 to the rest of the file */
4098 line_end_section1->next = line_after_section2;
4100 err = edit_write_file(file);
4103 /* Error writing file */
4104 if (err == JB_ERR_FILE)
4106 /* Read-only file. */
4107 err = cgi_error_file_read_only(csp, rsp, file->filename);
4109 edit_free_file(file);
4112 } /* END if (section1 != section2) */
4114 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4115 (long) time(NULL), file->identifier);
4117 edit_free_file(file);
4119 return cgi_redirect(rsp, target);
4123 /*********************************************************************
4125 * Function : javascriptify
4127 * Description : Converts a string into a form JavaScript will like.
4129 * Netscape 4's JavaScript sucks - it doesn't use
4130 * "id" parameters, so you have to set the "name"
4131 * used to submit a form element to something JavaScript
4132 * will like. (Or access the elements by index in an
4133 * array. That array contains >60 elements and will
4134 * be changed whenever we add a new action to the
4135 * editor, so I'm NOT going to use indexes that have
4136 * to be figured out by hand.)
4138 * Currently the only thing we have to worry about
4139 * is "-" ==> "_" conversion.
4141 * This is a length-preserving operation so it is
4142 * carried out in-place, no memory is allocated
4146 * 1 : identifier = String to make JavaScript-friendly.
4150 *********************************************************************/
4151 static void javascriptify(char * identifier)
4153 char * p = identifier;
4154 while (NULL != (p = strchr(p, '-')))
4161 /*********************************************************************
4163 * Function : actions_to_radio
4165 * Description : Converts a actionsfile entry into settings for
4166 * radio buttons and edit boxes on a HTML form.
4169 * 1 : exports = List of substitutions to add to.
4170 * 2 : action = Action to read
4172 * Returns : JB_ERR_OK on success
4173 * JB_ERR_MEMORY on out-of-memory
4175 *********************************************************************/
4176 static jb_err actions_to_radio(struct map * exports,
4177 const struct action_spec *action)
4188 mask = action->mask;
4191 /* sanity - prevents "-feature +feature" */
4195 #define DEFINE_ACTION_BOOL(name, bit) \
4196 if (!(mask & bit)) \
4198 current_mode = 'n'; \
4200 else if (add & bit) \
4202 current_mode = 'y'; \
4206 current_mode = 'x'; \
4208 if (map_radio(exports, name, "ynx", current_mode)) \
4210 return JB_ERR_MEMORY; \
4213 #define DEFINE_ACTION_STRING(name, bit, index) \
4214 DEFINE_ACTION_BOOL(name, bit); \
4217 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4220 checked = !strcmp(action->string[index], value); \
4224 checked = is_default; \
4226 mapped_param |= checked; \
4227 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4229 return JB_ERR_MEMORY; \
4232 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4233 if (map(exports, name "-param-custom", 1, \
4234 ((!mapped_param) ? "checked" : ""), 1)) \
4236 return JB_ERR_MEMORY; \
4238 if (map(exports, name "-param", 1, \
4239 (((add & bit) && !mapped_param) ? \
4240 action->string[index] : default_val), 1)) \
4242 return JB_ERR_MEMORY; \
4245 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4246 if (map(exports, name "-param", 1, \
4247 ((add & bit) ? action->string[index] : default_val), 1)) \
4249 return JB_ERR_MEMORY; \
4252 #define DEFINE_ACTION_MULTI(name, index) \
4253 if (action->multi_add[index]->first) \
4255 current_mode = 'y'; \
4257 else if (action->multi_remove_all[index]) \
4259 current_mode = 'n'; \
4261 else if (action->multi_remove[index]->first) \
4263 current_mode = 'y'; \
4267 current_mode = 'x'; \
4269 if (map_radio(exports, name, "ynx", current_mode)) \
4271 return JB_ERR_MEMORY; \
4274 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4276 #include "actionlist.h"
4278 #undef DEFINE_ACTION_MULTI
4279 #undef DEFINE_ACTION_STRING
4280 #undef DEFINE_ACTION_BOOL
4281 #undef DEFINE_ACTION_ALIAS
4282 #undef DEFINE_CGI_PARAM_CUSTOM
4283 #undef DEFINE_CGI_PARAM_RADIO
4284 #undef DEFINE_CGI_PARAM_NO_RADIO
4290 /*********************************************************************
4292 * Function : actions_from_radio
4294 * Description : Converts a map of parameters passed to a CGI function
4295 * into an actionsfile entry.
4298 * 1 : parameters = parameters to the CGI call
4299 * 2 : action = Action to change. Must be valid before
4300 * the call, actions not specified will be
4303 * Returns : JB_ERR_OK on success
4304 * JB_ERR_MEMORY on out-of-memory
4306 *********************************************************************/
4307 static jb_err actions_from_radio(const struct map * parameters,
4308 struct action_spec *action)
4313 const char * js_name;
4314 jb_err err = JB_ERR_OK;
4319 /* Statics are generally a potential race condition,
4320 * but in this case we're safe and don't need semaphores.
4321 * Be careful if you modify this function.
4323 * The js_name_arr's are never free()d, but this is no
4324 * problem, since they will only be created once and
4325 * used by all threads thereafter. -oes
4328 #define JAVASCRIPTIFY(dest_var, string) \
4330 static int first_time = 1; \
4331 static char *js_name_arr; \
4334 js_name_arr = strdup(string); \
4335 javascriptify(js_name_arr); \
4337 dest_var = js_name_arr; \
4341 #define DEFINE_ACTION_BOOL(name, bit) \
4342 JAVASCRIPTIFY(js_name, name); \
4343 ch = get_char_param(parameters, js_name); \
4346 action->add |= bit; \
4347 action->mask |= bit; \
4349 else if (ch == 'N') \
4351 action->add &= ~bit; \
4352 action->mask &= ~bit; \
4354 else if (ch == 'X') \
4356 action->add &= ~bit; \
4357 action->mask |= bit; \
4360 #define DEFINE_ACTION_STRING(name, bit, index) \
4361 JAVASCRIPTIFY(js_name, name); \
4362 ch = get_char_param(parameters, js_name); \
4366 JAVASCRIPTIFY(js_name, name "-mode"); \
4367 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4368 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4370 JAVASCRIPTIFY(js_name, name "-param"); \
4371 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4373 if (param != NULL) \
4375 if (NULL == (param_dup = strdup(param))) \
4377 return JB_ERR_MEMORY; \
4379 freez(action->string[index]); \
4380 action->add |= bit; \
4381 action->mask |= bit; \
4382 action->string[index] = param_dup; \
4385 else if (ch == 'N') \
4387 if (action->add & bit) \
4389 freez(action->string[index]); \
4391 action->add &= ~bit; \
4392 action->mask &= ~bit; \
4394 else if (ch == 'X') \
4396 if (action->add & bit) \
4398 freez(action->string[index]); \
4400 action->add &= ~bit; \
4401 action->mask |= bit; \
4404 #define DEFINE_ACTION_MULTI(name, index) \
4405 JAVASCRIPTIFY(js_name, name); \
4406 ch = get_char_param(parameters, js_name); \
4411 else if (ch == 'N') \
4413 list_remove_all(action->multi_add[index]); \
4414 list_remove_all(action->multi_remove[index]); \
4415 action->multi_remove_all[index] = 1; \
4417 else if (ch == 'X') \
4419 list_remove_all(action->multi_add[index]); \
4420 list_remove_all(action->multi_remove[index]); \
4421 action->multi_remove_all[index] = 0; \
4424 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4426 #include "actionlist.h"
4428 #undef DEFINE_ACTION_MULTI
4429 #undef DEFINE_ACTION_STRING
4430 #undef DEFINE_ACTION_BOOL
4431 #undef DEFINE_ACTION_ALIAS
4432 #undef JAVASCRIPTIFY
4436 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4439 #ifdef FEATURE_TOGGLE
4440 /*********************************************************************
4442 * Function : cgi_toggle
4444 * Description : CGI function that adds a new empty section to
4448 * 1 : csp = Current client state (buffers, headers, etc...)
4449 * 2 : rsp = http_response data structure for output
4450 * 3 : parameters = map of cgi parameters
4453 * set : If present, how to change toggle setting:
4454 * "enable", "disable", "toggle", or none (default).
4455 * mini : If present, use mini reply template.
4457 * Returns : JB_ERR_OK on success
4458 * JB_ERR_MEMORY on out-of-memory
4460 *********************************************************************/
4461 jb_err cgi_toggle(struct client_state *csp,
4462 struct http_response *rsp,
4463 const struct map *parameters)
4465 struct map *exports;
4467 const char *template_name;
4473 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4475 return cgi_error_disabled(csp, rsp);
4478 mode = get_char_param(parameters, "set");
4483 global_toggle_state = 1;
4485 else if (mode == 'D')
4488 global_toggle_state = 0;
4490 else if (mode == 'T')
4493 global_toggle_state = !global_toggle_state;
4496 if (NULL == (exports = default_exports(csp, "toggle")))
4498 return JB_ERR_MEMORY;
4501 template_name = (get_char_param(parameters, "mini")
4505 return template_fill_for_cgi(csp, template_name, exports, rsp);
4507 #endif /* def FEATURE_TOGGLE */