1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.78 2013/11/24 14:22:51 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. */
128 /** This file_line has not been processed yet. */
129 #define FILE_LINE_UNPROCESSED 1
131 /** This file_line is blank. Can only appear at the end of a file, due to
132 the way the parser works. */
133 #define FILE_LINE_BLANK 2
135 /** This file_line says {{alias}}. */
136 #define FILE_LINE_ALIAS_HEADER 3
138 /** This file_line defines an alias. */
139 #define FILE_LINE_ALIAS_ENTRY 4
141 /** This file_line defines an {action}. */
142 #define FILE_LINE_ACTION 5
144 /** This file_line specifies a URL pattern. */
145 #define FILE_LINE_URL 6
147 /** This file_line says {{settings}}. */
148 #define FILE_LINE_SETTINGS_HEADER 7
150 /** This file_line is in a {{settings}} block. */
151 #define FILE_LINE_SETTINGS_ENTRY 8
153 /** This file_line says {{description}}. */
154 #define FILE_LINE_DESCRIPTION_HEADER 9
156 /** This file_line is in a {{description}} block. */
157 #define FILE_LINE_DESCRIPTION_ENTRY 10
160 * Number of file modification time mismatches
161 * before the CGI editor gets turned off.
163 #define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
166 * A configuration file, in a format that can be edited and written back to
171 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
172 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
173 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
174 const char * version_str; /**< Last modification time, as a string. For CGI param. */
175 /**< Can be used in URL without using url_param(). */
176 unsigned version; /**< Last modification time - prevents chaos with
177 the browser's "back" button. Note that this is a
178 time_t cast to an unsigned. When comparing, always
179 cast the time_t to an unsigned, and *NOT* vice-versa.
180 This may lose the top few bits, but they're not
181 significant anyway. */
182 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
183 Note that changing this after the file has been
184 read in will cause a mess. */
185 struct file_line * parse_error; /**< On parse error, this is the offending line. */
186 const char * parse_error_text; /**< On parse error, this is the problem.
187 (Statically allocated) */
191 * Information about the filter types.
192 * Used for macro replacement in cgi_edit_actions_for_url.
194 struct filter_type_info
196 const int multi_action_index; /**< The multi action index as defined in project.h */
197 const char *macro_name; /**< Name of the macro that has to be replaced
198 with the prepared templates.
199 For example "content-filter-params" */
200 const char *type; /**< Name of the filter type,
201 for example "server-header-filter". */
202 /* XXX: check if these two can be combined. */
203 const char *disable_all_option; /**< Name of the catch-all radio option that has
204 to be checked or unchecked for this filter type. */
205 const char *disable_all_param; /**< Name of the parameter that causes all filters of
206 this type to be disabled. */
207 const char *abbr_type; /**< Abbreviation of the filter type, usually the
208 first or second character capitalized */
209 const char *anchor; /**< Anchor for the User Manual link,
210 for example "SERVER-HEADER-FILTER" */
213 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
214 static const struct filter_type_info filter_type_info[] =
218 "content-filter-params", "filter",
219 "filter-all", "filter_all",
223 ACTION_MULTI_CLIENT_HEADER_FILTER,
224 "client-header-filter-params", "client-header-filter",
225 "client-header-filter-all", "client_header_filter_all",
226 "C", "CLIENT-HEADER-FILTER"
229 ACTION_MULTI_SERVER_HEADER_FILTER,
230 "server-header-filter-params", "server-header-filter",
231 "server-header-filter-all", "server_header_filter_all",
232 "S", "SERVER-HEADER-FILTER"
235 ACTION_MULTI_CLIENT_HEADER_TAGGER,
236 "client-header-tagger-params", "client-header-tagger",
237 "client-header-tagger-all", "client_header_tagger_all",
238 "L", "CLIENT-HEADER-TAGGER"
241 ACTION_MULTI_SERVER_HEADER_TAGGER,
242 "server-header-tagger-params", "server-header-tagger",
243 "server-header-tagger-all", "server_header_tagger_all",
244 "E", "SERVER-HEADER-TAGGER"
248 /* FIXME: Following non-static functions should be prototyped in .h or made static */
250 /* Functions to read and write arbitrary config files */
251 jb_err edit_read_file(struct client_state *csp,
252 const struct map *parameters,
254 struct editable_file **pfile);
255 jb_err edit_write_file(struct editable_file * file);
256 void edit_free_file(struct editable_file * file);
258 /* Functions to read and write actions files */
259 jb_err edit_parse_actions_file(struct editable_file * file);
260 jb_err edit_read_actions_file(struct client_state *csp,
261 struct http_response *rsp,
262 const struct map *parameters,
264 struct editable_file **pfile);
267 jb_err cgi_error_modified(struct client_state *csp,
268 struct http_response *rsp,
269 const char *filename);
270 jb_err cgi_error_parse(struct client_state *csp,
271 struct http_response *rsp,
272 struct editable_file *file);
273 jb_err cgi_error_file(struct client_state *csp,
274 struct http_response *rsp,
275 const char *filename);
276 jb_err cgi_error_file_read_only(struct client_state *csp,
277 struct http_response *rsp,
278 const char *filename);
280 /* Internal arbitrary config file support functions */
281 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
282 static void edit_free_file_lines(struct file_line * first_line);
284 /* Internal actions file support functions */
285 static int match_actions_file_header_line(const char * line, const char * name);
286 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
288 /* Internal parameter parsing functions */
289 static jb_err get_url_spec_param(struct client_state *csp,
290 const struct map *parameters,
295 /* Internal actionsfile <==> HTML conversion functions */
296 static jb_err map_radio(struct map * exports,
297 const char * optionname,
300 static jb_err actions_to_radio(struct map * exports,
301 const struct action_spec *action);
302 static jb_err actions_from_radio(const struct map * parameters,
303 struct action_spec *action);
306 static jb_err map_copy_parameter_html(struct map *out,
307 const struct map *in,
310 static jb_err get_file_name_param(struct client_state *csp,
311 const struct map *parameters,
312 const char *param_name,
313 const char **pfilename);
315 /* Internal convenience functions */
316 static char *section_target(const unsigned sectionid);
318 /*********************************************************************
320 * Function : section_target
322 * Description : Given an unsigned (section id) n, produce a dynamically
323 * allocated string of the form #l<n>, for use in link
326 * XXX: The hash should be moved into the templates
327 * to make this function more generic and render
328 * stringify() obsolete.
331 * 1 : sectionid = start line number of section
333 * Returns : String with link target, or NULL if out of
336 *********************************************************************/
337 static char *section_target(const unsigned sectionid)
341 snprintf(buf, sizeof(buf), "#l%d", sectionid);
347 /*********************************************************************
349 * Function : stringify
351 * Description : Convert a number into a dynamically allocated string.
354 * 1 : number = The number to convert.
356 * Returns : String with link target, or NULL if out of memory
358 *********************************************************************/
359 static char *stringify(const unsigned number)
363 snprintf(buf, sizeof(buf), "%i", number);
368 /*********************************************************************
370 * Function : map_copy_parameter_html
372 * Description : Copy a CGI parameter from one map to another, HTML
376 * 1 : out = target map
377 * 2 : in = source map
378 * 3 : name = name of cgi parameter to copy
380 * Returns : JB_ERR_OK on success
381 * JB_ERR_MEMORY on out-of-memory
382 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
385 *********************************************************************/
386 static jb_err map_copy_parameter_html(struct map *out,
387 const struct map *in,
397 value = lookup(in, name);
398 err = map(out, name, 1, html_encode(value), 0);
405 else if (*value == '\0')
407 return JB_ERR_CGI_PARAMS;
416 /*********************************************************************
418 * Function : cgi_edit_actions_url_form
420 * Description : CGI function that displays a form for
424 * 1 : csp = Current client state (buffers, headers, etc...)
425 * 2 : rsp = http_response data structure for output
426 * 3 : parameters = map of cgi parameters
429 * i : (action index) Identifies the file to edit
430 * v : (version) File's last-modified time
431 * p : (pattern) Line number of pattern to edit
433 * Returns : JB_ERR_OK on success
434 * JB_ERR_MEMORY on out-of-memory
435 * JB_ERR_CGI_PARAMS if the CGI parameters are not
436 * specified or not valid.
438 *********************************************************************/
439 jb_err cgi_edit_actions_url_form(struct client_state *csp,
440 struct http_response *rsp,
441 const struct map *parameters)
443 struct map * exports;
445 struct editable_file * file;
446 struct file_line * cur_line;
447 unsigned line_number;
448 unsigned section_start_line_number = 0;
455 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
457 return cgi_error_disabled(csp, rsp);
460 err = get_number_param(csp, parameters, "p", &patternid);
466 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
469 /* No filename specified, can't read file, modified, or out of memory. */
470 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
473 cur_line = file->lines;
475 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
477 if (cur_line->type == FILE_LINE_ACTION)
479 section_start_line_number = line_number;
481 cur_line = cur_line->next;
484 if ( (cur_line == NULL)
485 || (line_number != patternid)
487 || (cur_line->type != FILE_LINE_URL))
489 /* Invalid "patternid" parameter */
490 edit_free_file(file);
491 return JB_ERR_CGI_PARAMS;
494 if (NULL == (exports = default_exports(csp, NULL)))
496 edit_free_file(file);
497 return JB_ERR_MEMORY;
500 err = map(exports, "f", 1, stringify(file->identifier), 0);
501 if (!err) err = map(exports, "v", 1, file->version_str, 1);
502 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
503 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
504 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
506 edit_free_file(file);
514 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
518 /*********************************************************************
520 * Function : cgi_edit_actions_add_url_form
522 * Description : CGI function that displays a form for
526 * 1 : csp = Current client state (buffers, headers, etc...)
527 * 2 : rsp = http_response data structure for output
528 * 3 : parameters = map of cgi parameters
531 * f : (filename) Identifies the file to edit
532 * v : (version) File's last-modified time
533 * s : (section) Line number of section to edit
535 * Returns : JB_ERR_OK on success
536 * JB_ERR_MEMORY on out-of-memory
537 * JB_ERR_CGI_PARAMS if the CGI parameters are not
538 * specified or not valid.
540 *********************************************************************/
541 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
542 struct http_response *rsp,
543 const struct map *parameters)
552 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
554 return cgi_error_disabled(csp, rsp);
557 if (NULL == (exports = default_exports(csp, NULL)))
559 return JB_ERR_MEMORY;
562 err = map_copy_parameter_html(exports, parameters, "f");
563 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
564 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
572 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
576 /*********************************************************************
578 * Function : cgi_edit_actions_remove_url_form
580 * Description : CGI function that displays a form for
584 * 1 : csp = Current client state (buffers, headers, etc...)
585 * 2 : rsp = http_response data structure for output
586 * 3 : parameters = map of cgi parameters
589 * f : (number) The action file identifier.
590 * v : (version) File's last-modified time
591 * p : (pattern) Line number of pattern to edit
593 * Returns : JB_ERR_OK on success
594 * JB_ERR_MEMORY on out-of-memory
595 * JB_ERR_CGI_PARAMS if the CGI parameters are not
596 * specified or not valid.
598 *********************************************************************/
599 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
600 struct http_response *rsp,
601 const struct map *parameters)
603 struct map * exports;
605 struct editable_file * file;
606 struct file_line * cur_line;
607 unsigned line_number;
608 unsigned section_start_line_number = 0;
615 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
617 return cgi_error_disabled(csp, rsp);
620 err = get_number_param(csp, parameters, "p", &patternid);
626 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
629 /* No filename specified, can't read file, modified, or out of memory. */
630 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
633 cur_line = file->lines;
635 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
637 if (cur_line->type == FILE_LINE_ACTION)
639 section_start_line_number = line_number;
641 cur_line = cur_line->next;
644 if ( (cur_line == NULL)
645 || (line_number != patternid)
647 || (cur_line->type != FILE_LINE_URL))
649 /* Invalid "patternid" parameter */
650 edit_free_file(file);
651 return JB_ERR_CGI_PARAMS;
654 if (NULL == (exports = default_exports(csp, NULL)))
656 edit_free_file(file);
657 return JB_ERR_MEMORY;
660 err = map(exports, "f", 1, stringify(file->identifier), 0);
661 if (!err) err = map(exports, "v", 1, file->version_str, 1);
662 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
663 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
664 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
665 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
667 edit_free_file(file);
675 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
679 /*********************************************************************
681 * Function : edit_write_file
683 * Description : Write a complete file to disk.
686 * 1 : file = File to write.
688 * Returns : JB_ERR_OK on success
689 * JB_ERR_FILE on error writing to file.
690 * JB_ERR_MEMORY on out of memory
692 *********************************************************************/
693 jb_err edit_write_file(struct editable_file * file)
696 struct file_line * cur_line;
697 struct stat statbuf[1];
698 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
699 digits in time_t, assuming this is a 64-bit
700 machine, plus null terminator, plus one
704 assert(file->filename);
706 if (NULL == (fp = fopen(file->filename, "wb")))
711 cur_line = file->lines;
712 while (cur_line != NULL)
716 if (fputs(cur_line->raw, fp) < 0)
724 if (cur_line->prefix)
726 if (fputs(cur_line->prefix, fp) < 0)
732 if (cur_line->unprocessed)
735 if (NULL != strchr(cur_line->unprocessed, '#'))
737 /* Must quote '#' characters */
744 /* Count number of # characters, so we know length of output string */
745 src = cur_line->unprocessed;
746 while (NULL != (src = strchr(src, '#')))
753 /* Allocate new memory for string */
754 len = strlen(cur_line->unprocessed) + (size_t)numhash;
755 str = malloc_or_die(len + 1);
757 /* Copy string but quote hashes */
758 src = cur_line->unprocessed;
766 assert(numhash >= 0);
772 assert(numhash == 0);
773 assert(strlen(str) == len);
774 assert(str == dest - len);
775 assert(src - len <= cur_line->unprocessed);
777 if ((strlen(str) != len) || (numhash != 0))
780 * Escaping didn't work as expected, go spread the news.
781 * Only reached in non-debugging builds.
783 log_error(LOG_LEVEL_ERROR,
784 "Looks like hash escaping failed. %s might be corrupted now.",
788 if (fputs(str, fp) < 0)
799 /* Can write without quoting '#' characters. */
800 if (fputs(cur_line->unprocessed, fp) < 0)
806 if (fputs(NEWLINE(file->newline), fp) < 0)
814 /* FIXME: Write data from file->data->whatever */
818 cur_line = cur_line->next;
824 /* Update the version stamp in the file structure, since we just
825 * wrote to the file & changed it's date.
827 if (stat(file->filename, statbuf) < 0)
829 /* Error, probably file not found. */
832 file->version = (unsigned)statbuf->st_mtime;
834 /* Correct file->version_str */
835 freez(file->version_str);
836 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
837 version_buf[sizeof(version_buf)-1] = '\0';
838 file->version_str = strdup(version_buf);
839 if (version_buf == NULL)
841 return JB_ERR_MEMORY;
848 /*********************************************************************
850 * Function : edit_free_file
852 * Description : Free a complete file in memory.
855 * 1 : file = Data structure to free.
859 *********************************************************************/
860 void edit_free_file(struct editable_file * file)
864 /* Silently ignore NULL pointer */
868 edit_free_file_lines(file->lines);
869 freez(file->version_str);
871 file->parse_error_text = NULL; /* Statically allocated */
872 file->parse_error = NULL;
878 /*********************************************************************
880 * Function : edit_free_file_lines
882 * Description : Free an entire linked list of file lines.
885 * 1 : first_line = Data structure to free.
889 *********************************************************************/
890 static void edit_free_file_lines(struct file_line * first_line)
892 struct file_line * next_line;
894 while (first_line != NULL)
896 next_line = first_line->next;
897 first_line->next = NULL;
898 freez(first_line->raw);
899 freez(first_line->prefix);
900 freez(first_line->unprocessed);
901 switch(first_line->type)
903 case 0: /* special case if memory zeroed */
904 case FILE_LINE_UNPROCESSED:
905 case FILE_LINE_BLANK:
906 case FILE_LINE_ALIAS_HEADER:
907 case FILE_LINE_SETTINGS_HEADER:
908 case FILE_LINE_DESCRIPTION_HEADER:
909 case FILE_LINE_DESCRIPTION_ENTRY:
910 case FILE_LINE_ALIAS_ENTRY:
912 /* No data is stored for these */
915 case FILE_LINE_ACTION:
916 free_action(first_line->data.action);
919 case FILE_LINE_SETTINGS_ENTRY:
920 freez(first_line->data.setting.name);
921 freez(first_line->data.setting.svalue);
924 /* Should never happen */
928 first_line->type = 0; /* paranoia */
930 first_line = next_line;
935 /*********************************************************************
937 * Function : match_actions_file_header_line
939 * Description : Match an actions file {{header}} line
942 * 1 : line = String from file
943 * 2 : name = Header to match against
945 * Returns : 0 iff they match.
947 *********************************************************************/
948 static int match_actions_file_header_line(const char * line, const char * name)
956 if ((line[0] != '{') || (line[1] != '{'))
962 /* Look for optional whitespace */
963 while ((*line == ' ') || (*line == '\t'))
968 /* Look for the specified name (case-insensitive) */
970 if (0 != strncmpic(line, name, len))
976 /* Look for optional whitespace */
977 while ((*line == ' ') || (*line == '\t'))
982 /* Look for "}}" and end of string*/
983 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
993 /*********************************************************************
995 * Function : match_actions_file_header_line
997 * Description : Match an actions file {{header}} line
1000 * 1 : line = String from file. Must not start with
1001 * whitespace (else infinite loop!)
1002 * 2 : pname = Destination for name
1003 * 2 : pvalue = Destination for value
1005 * Returns : JB_ERR_OK on success
1006 * JB_ERR_MEMORY on out-of-memory
1007 * JB_ERR_PARSE if there's no "=" sign, or if there's
1008 * nothing before the "=" sign (but empty
1009 * values *after* the "=" sign are legal).
1011 *********************************************************************/
1012 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1014 const char * name_end;
1015 const char * value_start;
1021 assert(*line != ' ');
1022 assert(*line != '\t');
1027 value_start = strchr(line, '=');
1028 if ((value_start == NULL) || (value_start == line))
1030 return JB_ERR_PARSE;
1033 name_end = value_start - 1;
1035 /* Eat any whitespace before the '=' */
1036 while ((*name_end == ' ') || (*name_end == '\t'))
1039 * we already know we must have at least 1 non-ws char
1040 * at start of buf - no need to check
1045 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1046 *pname = malloc_or_die(name_len + 1);
1047 strncpy(*pname, line, name_len);
1048 (*pname)[name_len] = '\0';
1050 /* Eat any the whitespace after the '=' */
1052 while ((*value_start == ' ') || (*value_start == '\t'))
1057 if (NULL == (*pvalue = strdup(value_start)))
1061 return JB_ERR_MEMORY;
1068 /*********************************************************************
1070 * Function : edit_parse_actions_file
1072 * Description : Parse an actions file in memory.
1074 * Passed linked list must have the "data" member
1075 * zeroed, and must contain valid "next" and
1076 * "unprocessed" fields. The "raw" and "prefix"
1077 * fields are ignored, and "type" is just overwritten.
1079 * Note that on error the file may have been
1083 * 1 : file = Actions file to be parsed in-place.
1085 * Returns : JB_ERR_OK on success
1086 * JB_ERR_MEMORY on out-of-memory
1087 * JB_ERR_PARSE on error
1089 *********************************************************************/
1090 jb_err edit_parse_actions_file(struct editable_file * file)
1092 struct file_line * cur_line;
1094 const char * text; /* Text from a line */
1095 char * name; /* For lines of the form name=value */
1096 char * value; /* For lines of the form name=value */
1097 struct action_alias * alias_list = NULL;
1098 jb_err err = JB_ERR_OK;
1100 /* alias_list contains the aliases defined in this file.
1101 * It might be better to use the "file_line.data" fields
1102 * in the relavent places instead.
1105 cur_line = file->lines;
1107 /* A note about blank line support: Blank lines should only
1108 * ever occur as the last line in the file. This function
1109 * is more forgiving than that - FILE_LINE_BLANK can occur
1113 /* Skip leading blanks. Should only happen if file is
1114 * empty (which is valid, but pointless).
1116 while ((cur_line != NULL)
1117 && (cur_line->unprocessed[0] == '\0'))
1120 cur_line->type = FILE_LINE_BLANK;
1121 cur_line = cur_line->next;
1124 if ((cur_line != NULL)
1125 && (cur_line->unprocessed[0] != '{'))
1127 /* File doesn't start with a header */
1128 file->parse_error = cur_line;
1129 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1130 return JB_ERR_PARSE;
1133 if ((cur_line != NULL) && (0 ==
1134 match_actions_file_header_line(cur_line->unprocessed, "settings")))
1136 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1138 cur_line = cur_line->next;
1139 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1141 if (cur_line->unprocessed[0])
1143 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1145 err = split_line_on_equals(cur_line->unprocessed,
1146 &cur_line->data.setting.name,
1147 &cur_line->data.setting.svalue);
1148 if (err == JB_ERR_MEMORY)
1152 else if (err != JB_ERR_OK)
1154 /* Line does not contain a name=value pair */
1155 file->parse_error = cur_line;
1156 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1157 return JB_ERR_PARSE;
1162 cur_line->type = FILE_LINE_BLANK;
1164 cur_line = cur_line->next;
1168 if ((cur_line != NULL) && (0 ==
1169 match_actions_file_header_line(cur_line->unprocessed, "description")))
1171 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1173 cur_line = cur_line->next;
1174 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1176 if (cur_line->unprocessed[0])
1178 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1182 cur_line->type = FILE_LINE_BLANK;
1184 cur_line = cur_line->next;
1188 if ((cur_line != NULL) && (0 ==
1189 match_actions_file_header_line(cur_line->unprocessed, "alias")))
1191 cur_line->type = FILE_LINE_ALIAS_HEADER;
1193 cur_line = cur_line->next;
1194 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1196 if (cur_line->unprocessed[0])
1198 /* define an alias */
1199 struct action_alias * new_alias;
1201 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1203 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1204 if (err == JB_ERR_MEMORY)
1208 else if (err != JB_ERR_OK)
1210 /* Line does not contain a name=value pair */
1211 file->parse_error = cur_line;
1212 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1213 return JB_ERR_PARSE;
1216 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1221 free_alias_list(alias_list);
1222 return JB_ERR_MEMORY;
1225 err = get_actions(value, alias_list, new_alias->action);
1228 /* Invalid action or out of memory */
1232 free_alias_list(alias_list);
1233 if (err == JB_ERR_MEMORY)
1239 /* Line does not contain a name=value pair */
1240 file->parse_error = cur_line;
1241 file->parse_error_text = "This alias does not specify a valid set of actions.";
1242 return JB_ERR_PARSE;
1248 new_alias->name = name;
1251 new_alias->next = alias_list;
1252 alias_list = new_alias;
1256 cur_line->type = FILE_LINE_BLANK;
1258 cur_line = cur_line->next;
1262 /* Header done, process the main part of the file */
1263 while (cur_line != NULL)
1265 /* At this point, (cur_line->unprocessed[0] == '{') */
1266 assert(cur_line->unprocessed[0] == '{');
1267 text = cur_line->unprocessed + 1;
1268 len = strlen(text) - 1;
1269 if (text[len] != '}')
1271 /* No closing } on header */
1272 free_alias_list(alias_list);
1273 file->parse_error = cur_line;
1274 file->parse_error_text = "Headers starting with '{' must have a "
1275 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1276 "must close with two brackets ('}}').";
1277 return JB_ERR_PARSE;
1282 /* An invalid {{ header. */
1283 free_alias_list(alias_list);
1284 file->parse_error = cur_line;
1285 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1286 "Please remember that the system (two-bracket) headers must "
1287 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1288 "and must appear before any actions (one-bracket) headers. "
1289 "Also note that system headers may not be repeated.";
1290 return JB_ERR_PARSE;
1293 while ((*text == ' ') || (*text == '\t'))
1298 while ((len > (size_t)0)
1299 && ((text[len - 1] == ' ')
1300 || (text[len - 1] == '\t')))
1305 cur_line->type = FILE_LINE_ACTION;
1307 /* Remove {} and make copy */
1308 value = malloc_or_die(len + 1);
1309 strncpy(value, text, len);
1313 err = get_actions(value, alias_list, cur_line->data.action);
1316 /* Invalid action or out of memory */
1318 free_alias_list(alias_list);
1319 if (err == JB_ERR_MEMORY)
1325 /* Line does not contain a name=value pair */
1326 file->parse_error = cur_line;
1327 file->parse_error_text = "This header does not specify a valid set of actions.";
1328 return JB_ERR_PARSE;
1332 /* Done with string - it was clobbered anyway */
1335 /* Process next line */
1336 cur_line = cur_line->next;
1338 /* Loop processing URL patterns */
1339 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1341 if (cur_line->unprocessed[0])
1343 /* Could parse URL here, but this isn't currently needed */
1345 cur_line->type = FILE_LINE_URL;
1349 cur_line->type = FILE_LINE_BLANK;
1351 cur_line = cur_line->next;
1353 } /* End main while(cur_line != NULL) loop */
1355 free_alias_list(alias_list);
1361 /*********************************************************************
1363 * Function : edit_read_file_lines
1365 * Description : Read all the lines of a file into memory.
1366 * Handles whitespace, comments and line continuation.
1369 * 1 : fp = File to read from. On return, this will be
1370 * at EOF but it will not have been closed.
1371 * 2 : pfile = Destination for a linked list of file_lines.
1372 * Will be set to NULL on error.
1373 * 3 : newline = How to handle newlines.
1375 * Returns : JB_ERR_OK on success
1376 * JB_ERR_MEMORY on out-of-memory
1378 *********************************************************************/
1379 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1381 struct file_line * first_line; /* Keep for return value or to free */
1382 struct file_line * cur_line; /* Current line */
1383 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1391 cur_line = first_line = zalloc(sizeof(struct file_line));
1392 if (cur_line == NULL)
1394 return JB_ERR_MEMORY;
1397 cur_line->type = FILE_LINE_UNPROCESSED;
1399 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1402 /* Out of memory or empty file. */
1403 /* Note that empty file is not an error we propagate up */
1405 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1410 prev_line = cur_line;
1411 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1412 if (cur_line == NULL)
1415 edit_free_file_lines(first_line);
1416 return JB_ERR_MEMORY;
1419 cur_line->type = FILE_LINE_UNPROCESSED;
1421 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1422 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1425 edit_free_file_lines(first_line);
1426 return JB_ERR_MEMORY;
1430 while (rval != JB_ERR_FILE);
1434 /* We allocated one too many - free it */
1435 prev_line->next = NULL;
1438 *pfile = first_line;
1443 /*********************************************************************
1445 * Function : edit_read_file
1447 * Description : Read a complete file into memory.
1448 * Handles CGI parameter parsing. If requested, also
1449 * checks the file's modification timestamp.
1452 * 1 : csp = Current client state (buffers, headers, etc...)
1453 * 2 : parameters = map of cgi parameters.
1454 * 3 : require_version = true to check "ver" parameter.
1455 * 4 : pfile = Destination for the file. Will be set
1459 * f : The action file identifier.
1460 * ver : (Only if require_version is nonzero)
1461 * Timestamp of the actions file. If wrong, this
1462 * function fails with JB_ERR_MODIFIED.
1464 * Returns : JB_ERR_OK on success
1465 * JB_ERR_MEMORY on out-of-memory
1466 * JB_ERR_CGI_PARAMS if "filename" was not specified
1468 * JB_ERR_FILE if the file cannot be opened or
1470 * JB_ERR_MODIFIED if version checking was requested and
1471 * failed - the file was modified outside
1472 * of this CGI editor instance.
1474 *********************************************************************/
1475 jb_err edit_read_file(struct client_state *csp,
1476 const struct map *parameters,
1477 int require_version,
1478 struct editable_file **pfile)
1480 struct file_line * lines;
1483 const char *filename = NULL;
1484 struct editable_file * file;
1485 unsigned version = 0;
1486 struct stat statbuf[1];
1487 char version_buf[22];
1488 int newline = NEWLINE_UNKNOWN;
1497 err = get_number_param(csp, parameters, "f", &i);
1498 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1500 filename = csp->config->actions_file[i];
1502 else if (JB_ERR_CGI_PARAMS == err)
1505 * Probably an old-school URL like
1506 * http://config.privoxy.org/edit-actions-list?f=default
1508 get_file_name_param(csp, parameters, "f", &filename);
1511 if (NULL == filename || stat(filename, statbuf) < 0)
1513 /* Error, probably file not found. */
1516 version = (unsigned) statbuf->st_mtime;
1518 if (require_version)
1520 unsigned specified_version;
1521 err = get_number_param(csp, parameters, "v", &specified_version);
1527 if (version != specified_version)
1529 return JB_ERR_MODIFIED;
1533 if (NULL == (fp = fopen(filename,"rb")))
1538 err = edit_read_file_lines(fp, &lines, &newline);
1547 file = (struct editable_file *) zalloc(sizeof(*file));
1550 edit_free_file_lines(lines);
1554 file->lines = lines;
1555 file->newline = newline;
1556 file->filename = filename;
1557 file->version = version;
1558 file->identifier = i;
1560 /* Correct file->version_str */
1561 freez(file->version_str);
1562 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1563 version_buf[sizeof(version_buf)-1] = '\0';
1564 file->version_str = strdup(version_buf);
1565 if (version_buf == NULL)
1567 edit_free_file(file);
1568 return JB_ERR_MEMORY;
1576 /*********************************************************************
1578 * Function : edit_read_actions_file
1580 * Description : Read a complete actions file into memory.
1581 * Handles CGI parameter parsing. If requested, also
1582 * checks the file's modification timestamp.
1584 * If this function detects an error in the categories
1585 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1586 * then it handles it by filling in the specified
1587 * response structure and returning JB_ERR_FILE.
1590 * 1 : csp = Current client state (buffers, headers, etc...)
1591 * 2 : rsp = HTTP response. Only filled in on error.
1592 * 2 : parameters = map of cgi parameters.
1593 * 3 : require_version = true to check "ver" parameter.
1594 * 4 : pfile = Destination for the file. Will be set
1598 * f : The actions file identifier.
1599 * ver : (Only if require_version is nonzero)
1600 * Timestamp of the actions file. If wrong, this
1601 * function fails with JB_ERR_MODIFIED.
1603 * Returns : JB_ERR_OK on success
1604 * JB_ERR_MEMORY on out-of-memory
1605 * JB_ERR_CGI_PARAMS if "filename" was not specified
1607 * JB_ERR_FILE if the file does not contain valid data,
1608 * or if file cannot be opened or
1609 * contains no data, or if version
1610 * checking was requested and failed.
1612 *********************************************************************/
1613 jb_err edit_read_actions_file(struct client_state *csp,
1614 struct http_response *rsp,
1615 const struct map *parameters,
1616 int require_version,
1617 struct editable_file **pfile)
1620 struct editable_file *file;
1621 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
1629 err = edit_read_file(csp, parameters, require_version, &file);
1632 /* Try to handle if possible */
1633 if (err == JB_ERR_FILE)
1635 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1637 else if (err == JB_ERR_MODIFIED)
1639 assert(require_version);
1640 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1641 log_error(LOG_LEVEL_ERROR,
1642 "Blocking CGI edit request due to modification time mismatch.");
1643 if (acceptable_failures > 0)
1645 log_error(LOG_LEVEL_INFO,
1646 "The CGI editor will be turned off after another %d mismatche(s).",
1647 acceptable_failures);
1648 acceptable_failures--;
1652 log_error(LOG_LEVEL_INFO,
1653 "Timestamp mismatch limit reached, turning CGI editor off. "
1654 "Reload the configuration file to re-enable it.");
1655 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
1658 if (err == JB_ERR_OK)
1661 * Signal to higher-level CGI code that there was a problem but we
1662 * handled it, they should just return JB_ERR_OK.
1669 err = edit_parse_actions_file(file);
1672 if (err == JB_ERR_PARSE)
1674 err = cgi_error_parse(csp, rsp, file);
1675 if (err == JB_ERR_OK)
1678 * Signal to higher-level CGI code that there was a problem but we
1679 * handled it, they should just return JB_ERR_OK.
1684 edit_free_file(file);
1693 /*********************************************************************
1695 * Function : get_file_name_param
1697 * Description : Get the name of the file to edit from the parameters
1698 * passed to a CGI function using the old syntax.
1699 * This function handles security checks and only
1700 * accepts files that Privoxy already knows.
1703 * 1 : csp = Current client state (buffers, headers, etc...)
1704 * 2 : parameters = map of cgi parameters
1705 * 3 : param_name = The name of the parameter to read
1706 * 4 : pfilename = pointer to the filename in
1707 * csp->config->actions_file[] if found. Set to NULL on error.
1709 * Returns : JB_ERR_OK on success
1710 * JB_ERR_MEMORY on out-of-memory
1711 * JB_ERR_CGI_PARAMS if "filename" was not specified
1714 *********************************************************************/
1715 static jb_err get_file_name_param(struct client_state *csp,
1716 const struct map *parameters,
1717 const char *param_name,
1718 const char **pfilename)
1721 const char suffix[] = ".action";
1736 param = lookup(parameters, param_name);
1739 return JB_ERR_CGI_PARAMS;
1742 len = strlen(param);
1743 if (len >= FILENAME_MAX)
1746 return JB_ERR_CGI_PARAMS;
1750 * Check every character to see if it's legal.
1751 * Totally unnecessary but we do it anyway.
1754 while ((ch = *s++) != '\0')
1756 if ( ((ch < 'A') || (ch > 'Z'))
1757 && ((ch < 'a') || (ch > 'z'))
1758 && ((ch < '0') || (ch > '9'))
1762 /* Probable hack attempt. */
1763 return JB_ERR_CGI_PARAMS;
1767 /* Append extension */
1768 name_size = len + strlen(suffix) + 1;
1769 name = malloc_or_die(name_size);
1770 strlcpy(name, param, name_size);
1771 strlcat(name, suffix, name_size);
1774 fullpath = make_path(csp->config->confdir, name);
1777 if (fullpath == NULL)
1779 return JB_ERR_MEMORY;
1782 /* Check if the file is known */
1783 for (i = 0; i < MAX_AF_FILES; i++)
1785 if (NULL != csp->config->actions_file[i] &&
1786 !strcmp(fullpath, csp->config->actions_file[i]))
1789 *pfilename = csp->config->actions_file[i];
1797 return JB_ERR_CGI_PARAMS;
1801 /*********************************************************************
1803 * Function : get_url_spec_param
1805 * Description : Get a URL pattern from the parameters
1806 * passed to a CGI function. Removes leading/trailing
1807 * spaces and validates it.
1810 * 1 : csp = Current client state (buffers, headers, etc...)
1811 * 2 : parameters = map of cgi parameters
1812 * 3 : name = Name of CGI parameter to read
1813 * 4 : pvalue = destination for value. Will be malloc()'d.
1814 * Set to NULL on error.
1816 * Returns : JB_ERR_OK on success
1817 * JB_ERR_MEMORY on out-of-memory
1818 * JB_ERR_CGI_PARAMS if the parameter was not specified
1821 *********************************************************************/
1822 static jb_err get_url_spec_param(struct client_state *csp,
1823 const struct map *parameters,
1827 const char *orig_param;
1830 struct pattern_spec compiled[1];
1840 orig_param = lookup(parameters, name);
1843 return JB_ERR_CGI_PARAMS;
1846 /* Copy and trim whitespace */
1847 param = strdup(orig_param);
1850 return JB_ERR_MEMORY;
1854 /* Must be non-empty, and can't allow 1st character to be '{' */
1855 if (param[0] == '\0' || param[0] == '{')
1858 return JB_ERR_CGI_PARAMS;
1861 /* Check for embedded newlines */
1862 for (s = param; *s != '\0'; s++)
1864 if ((*s == '\r') || (*s == '\n'))
1867 return JB_ERR_CGI_PARAMS;
1871 /* Check that regex is valid */
1876 return JB_ERR_MEMORY;
1878 err = create_pattern_spec(compiled, s);
1883 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1885 free_pattern_spec(compiled);
1887 if (param[strlen(param) - 1] == '\\')
1890 * Must protect trailing '\\' from becoming line continuation character.
1891 * Two methods: 1) If it's a domain only, add a trailing '/'.
1892 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1894 if (strchr(param, '/') == NULL)
1896 err = string_append(¶m, "/");
1900 err = string_append(¶m, "(?:)");
1907 /* Check that the modified regex is valid */
1912 return JB_ERR_MEMORY;
1914 err = create_pattern_spec(compiled, s);
1919 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1921 free_pattern_spec(compiled);
1928 /*********************************************************************
1930 * Function : map_radio
1932 * Description : Map a set of radio button values. E.g. if you have
1933 * 3 radio buttons, declare them as:
1934 * <option type="radio" name="xyz" @xyz-a@>
1935 * <option type="radio" name="xyz" @xyz-b@>
1936 * <option type="radio" name="xyz" @xyz-c@>
1937 * Then map one of the @xyz-?@ variables to "checked"
1938 * and all the others to empty by calling:
1939 * map_radio(exports, "xyz", "abc", sel)
1940 * Where 'sel' is 'a', 'b', or 'c'.
1943 * 1 : exports = Exports map to modify.
1944 * 2 : optionname = name for map
1945 * 3 : values = null-terminated list of values;
1946 * 4 : value = Selected value.
1948 * CGI Parameters : None
1950 * Returns : JB_ERR_OK on success
1951 * JB_ERR_MEMORY on out-of-memory
1953 *********************************************************************/
1954 static jb_err map_radio(struct map * exports,
1955 const char * optionname,
1956 const char * values,
1962 const size_t len = strlen(optionname);
1963 const size_t buf_size = len + 3;
1969 buf = malloc_or_die(buf_size);
1971 strlcpy(buf, optionname, buf_size);
1973 /* XXX: this looks ... interesting */
1978 while ((c = *values++) != '\0')
1983 if (map(exports, buf, 1, "", 1))
1985 return JB_ERR_MEMORY;
1991 return map(exports, buf, 0, "checked", 1);
1995 /*********************************************************************
1997 * Function : cgi_error_modified
1999 * Description : CGI function that is called when a file is modified
2000 * outside the CGI editor.
2003 * 1 : csp = Current client state (buffers, headers, etc...)
2004 * 2 : rsp = http_response data structure for output
2005 * 3 : filename = The file that was modified.
2007 * CGI Parameters : none
2009 * Returns : JB_ERR_OK on success
2010 * JB_ERR_MEMORY on out-of-memory error.
2012 *********************************************************************/
2013 jb_err cgi_error_modified(struct client_state *csp,
2014 struct http_response *rsp,
2015 const char *filename)
2017 struct map *exports;
2024 if (NULL == (exports = default_exports(csp, NULL)))
2026 return JB_ERR_MEMORY;
2029 err = map(exports, "f", 1, html_encode(filename), 0);
2036 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2040 /*********************************************************************
2042 * Function : cgi_error_parse
2044 * Description : CGI function that is called when a file cannot
2045 * be parsed by the CGI editor.
2048 * 1 : csp = Current client state (buffers, headers, etc...)
2049 * 2 : rsp = http_response data structure for output
2050 * 3 : file = The file that was modified.
2052 * CGI Parameters : none
2054 * Returns : JB_ERR_OK on success
2055 * JB_ERR_MEMORY on out-of-memory error.
2057 *********************************************************************/
2058 jb_err cgi_error_parse(struct client_state *csp,
2059 struct http_response *rsp,
2060 struct editable_file *file)
2062 struct map *exports;
2064 struct file_line *cur_line;
2070 if (NULL == (exports = default_exports(csp, NULL)))
2072 return JB_ERR_MEMORY;
2075 err = map(exports, "f", 1, stringify(file->identifier), 0);
2076 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2078 cur_line = file->parse_error;
2081 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2082 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2090 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2094 /*********************************************************************
2096 * Function : cgi_error_file
2098 * Description : CGI function that is called when a file cannot be
2099 * opened by the CGI editor.
2102 * 1 : csp = Current client state (buffers, headers, etc...)
2103 * 2 : rsp = http_response data structure for output
2104 * 3 : filename = The file that was modified.
2106 * CGI Parameters : none
2108 * Returns : JB_ERR_OK on success
2109 * JB_ERR_MEMORY on out-of-memory error.
2111 *********************************************************************/
2112 jb_err cgi_error_file(struct client_state *csp,
2113 struct http_response *rsp,
2114 const char *filename)
2116 struct map *exports;
2123 if (NULL == (exports = default_exports(csp, NULL)))
2125 return JB_ERR_MEMORY;
2128 err = map(exports, "f", 1, html_encode(filename), 0);
2135 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2139 /*********************************************************************
2141 * Function : cgi_error_file_read_only
2143 * Description : CGI function that is called when a file cannot be
2144 * opened for writing by the CGI editor.
2147 * 1 : csp = Current client state (buffers, headers, etc...)
2148 * 2 : rsp = http_response data structure for output
2149 * 3 : filename = The file that we can't write to
2151 * CGI Parameters : none
2153 * Returns : JB_ERR_OK on success
2154 * JB_ERR_MEMORY on out-of-memory error.
2156 *********************************************************************/
2157 jb_err cgi_error_file_read_only(struct client_state *csp,
2158 struct http_response *rsp,
2159 const char *filename)
2161 struct map *exports;
2168 if (NULL == (exports = default_exports(csp, NULL)))
2170 return JB_ERR_MEMORY;
2173 err = map(exports, "f", 1, html_encode(filename), 0);
2180 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2184 /*********************************************************************
2186 * Function : cgi_edit_actions
2188 * Description : CGI function that allows the user to choose which
2189 * actions file to edit.
2192 * 1 : csp = Current client state (buffers, headers, etc...)
2193 * 2 : rsp = http_response data structure for output
2194 * 3 : parameters = map of cgi parameters
2196 * CGI Parameters : None
2198 * Returns : JB_ERR_OK on success
2199 * JB_ERR_MEMORY on out-of-memory error
2201 *********************************************************************/
2202 jb_err cgi_edit_actions(struct client_state *csp,
2203 struct http_response *rsp,
2204 const struct map *parameters)
2208 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2210 return cgi_error_disabled(csp, rsp);
2213 /* FIXME: Incomplete */
2215 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2220 /*********************************************************************
2222 * Function : cgi_edit_actions_list
2224 * Description : CGI function that edits the actions list.
2225 * FIXME: This function shouldn't FATAL ever.
2226 * FIXME: This function doesn't check the retval of map()
2228 * 1 : csp = Current client state (buffers, headers, etc...)
2229 * 2 : rsp = http_response data structure for output
2230 * 3 : parameters = map of cgi parameters
2232 * CGI Parameters : filename
2234 * Returns : JB_ERR_OK on success
2235 * JB_ERR_MEMORY on out-of-memory
2236 * JB_ERR_FILE if the file cannot be opened or
2238 * JB_ERR_CGI_PARAMS if "filename" was not specified
2241 *********************************************************************/
2242 jb_err cgi_edit_actions_list(struct client_state *csp,
2243 struct http_response *rsp,
2244 const struct map *parameters)
2246 char * section_template;
2247 char * url_template;
2252 struct map * exports;
2253 struct map * section_exports;
2254 struct map * url_exports;
2255 struct editable_file * file;
2256 struct file_line * cur_line;
2257 unsigned line_number = 0;
2258 unsigned prev_section_line_number = ((unsigned) (-1));
2260 struct file_list * fl;
2261 struct url_actions * b;
2262 char * buttons = NULL;
2265 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2267 return cgi_error_disabled(csp, rsp);
2270 if (NULL == (exports = default_exports(csp, NULL)))
2272 return JB_ERR_MEMORY;
2275 /* Load actions file */
2276 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2279 /* No filename specified, can't read file, or out of memory. */
2281 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2284 /* Find start of actions in file */
2285 cur_line = file->lines;
2287 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2289 cur_line = cur_line->next;
2294 * Conventional actions files should have a match all block
2296 * cur_line = {...global actions...}
2297 * cur_line->next = /
2298 * cur_line->next->next = {...actions...} or EOF
2300 if ( (cur_line != NULL)
2301 && (cur_line->type == FILE_LINE_ACTION)
2302 && (cur_line->next != NULL)
2303 && (cur_line->next->type == FILE_LINE_URL)
2304 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2305 && ( (cur_line->next->next == NULL)
2306 || (cur_line->next->next->type != FILE_LINE_URL)
2310 * Generate string with buttons to set actions for "/" to
2311 * any predefined set of actions (named standard.*, probably
2312 * residing in standard.action).
2315 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2318 edit_free_file(file);
2320 if (err == JB_ERR_FILE)
2322 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2327 err = template_fill(§ion_template, exports);
2330 edit_free_file(file);
2335 buttons = strdup("");
2336 for (i = 0; i < MAX_AF_FILES; i++)
2338 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2340 for (b = b->next; NULL != b; b = b->next)
2342 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2344 if (err || (NULL == (section_exports = new_map())))
2347 free(section_template);
2348 edit_free_file(file);
2350 return JB_ERR_MEMORY;
2353 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2355 if (err || (NULL == (s = strdup(section_template))))
2357 free_map(section_exports);
2359 free(section_template);
2360 edit_free_file(file);
2362 return JB_ERR_MEMORY;
2365 if (!err) err = template_fill(&s, section_exports);
2366 free_map(section_exports);
2367 if (!err) err = string_join(&buttons, s);
2372 freez(section_template);
2373 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2376 * Conventional actions file, supply extra editing help.
2377 * (e.g. don't allow them to make it an unconventional one).
2379 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2381 snprintf(buf, sizeof(buf), "%d", line_number);
2382 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2383 snprintf(buf, sizeof(buf), "%d", line_number + 2);
2384 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2385 if (!err) err = map(exports, "all-urls-actions", 1,
2386 actions_to_html(csp, cur_line->data.action), 0);
2388 /* Skip the 2 lines */
2389 cur_line = cur_line->next->next;
2393 * Note that prev_section_line_number is NOT set here.
2394 * This is deliberate and not a bug. It stops a "Move up"
2395 * option appearing on the next section. Clicking "Move
2396 * up" would make the actions file unconventional, which
2397 * we don't want, so we hide this option.
2403 * Non-standard actions file - does not begin with
2404 * the "All URLs" section.
2406 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2409 /* Set up global exports */
2411 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2412 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2413 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2415 /* Discourage private additions to default.action */
2417 if (!err) err = map_conditional(exports, "default-action",
2418 (strstr("default.action", file->filename) != NULL));
2421 edit_free_file(file);
2426 /* Should do all global exports above this point */
2428 /* Load templates */
2430 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2433 edit_free_file(file);
2435 if (err == JB_ERR_FILE)
2437 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2442 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2445 free(section_template);
2446 edit_free_file(file);
2448 if (err == JB_ERR_FILE)
2450 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2455 err = template_fill(§ion_template, exports);
2459 edit_free_file(file);
2464 err = template_fill(&url_template, exports);
2467 free(section_template);
2468 edit_free_file(file);
2473 if (NULL == (sections = strdup("")))
2475 free(section_template);
2477 edit_free_file(file);
2479 return JB_ERR_MEMORY;
2482 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2484 if (NULL == (section_exports = new_map()))
2487 free(section_template);
2489 edit_free_file(file);
2491 return JB_ERR_MEMORY;
2494 snprintf(buf, sizeof(buf), "%d", line_number);
2495 err = map(section_exports, "s", 1, buf, 1);
2496 if (!err) err = map(section_exports, "actions", 1,
2497 actions_to_html(csp, cur_line->data.action), 0);
2500 && (cur_line->next != NULL)
2501 && (cur_line->next->type == FILE_LINE_URL))
2503 /* This section contains at least one URL, don't allow delete */
2504 err = map_block_killer(section_exports, "empty-section");
2508 if (!err) err = map_block_keep(section_exports, "empty-section");
2511 if (prev_section_line_number != ((unsigned)(-1)))
2513 /* Not last section */
2514 snprintf(buf, sizeof(buf), "%d", prev_section_line_number);
2515 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2516 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2521 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2523 prev_section_line_number = line_number;
2528 free(section_template);
2530 edit_free_file(file);
2532 free_map(section_exports);
2536 /* Should do all section-specific exports above this point */
2538 if (NULL == (urls = strdup("")))
2541 free(section_template);
2543 edit_free_file(file);
2545 free_map(section_exports);
2546 return JB_ERR_MEMORY;
2551 cur_line = cur_line->next;
2554 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2556 if (NULL == (url_exports = new_map()))
2560 free(section_template);
2562 edit_free_file(file);
2564 free_map(section_exports);
2565 return JB_ERR_MEMORY;
2568 snprintf(buf, sizeof(buf), "%d", line_number);
2569 err = map(url_exports, "p", 1, buf, 1);
2571 snprintf(buf, sizeof(buf), "%d", url_1_2);
2572 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2574 if (!err) err = map(url_exports, "url-html", 1,
2575 html_encode(cur_line->unprocessed), 0);
2576 if (!err) err = map(url_exports, "url", 1,
2577 url_encode(cur_line->unprocessed), 0);
2583 free(section_template);
2585 edit_free_file(file);
2587 free_map(section_exports);
2588 free_map(url_exports);
2592 if (NULL == (s = strdup(url_template)))
2596 free(section_template);
2598 edit_free_file(file);
2600 free_map(section_exports);
2601 free_map(url_exports);
2602 return JB_ERR_MEMORY;
2605 err = template_fill(&s, section_exports);
2606 if (!err) err = template_fill(&s, url_exports);
2607 if (!err) err = string_append(&urls, s);
2609 free_map(url_exports);
2616 free(section_template);
2618 edit_free_file(file);
2620 free_map(section_exports);
2624 url_1_2 = 3 - url_1_2;
2626 cur_line = cur_line->next;
2630 err = map(section_exports, "urls", 1, urls, 0);
2632 /* Could also do section-specific exports here, but it wouldn't be as fast */
2634 snprintf(buf, sizeof(buf), "%d", line_number);
2635 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2637 if ((cur_line != NULL)
2638 && (cur_line->type == FILE_LINE_ACTION))
2640 /* Not last section */
2641 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2646 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2652 free(section_template);
2654 edit_free_file(file);
2656 free_map(section_exports);
2660 if (NULL == (s = strdup(section_template)))
2663 free(section_template);
2665 edit_free_file(file);
2667 free_map(section_exports);
2668 return JB_ERR_MEMORY;
2671 err = template_fill(&s, section_exports);
2672 if (!err) err = string_append(§ions, s);
2675 free_map(section_exports);
2680 free(section_template);
2682 edit_free_file(file);
2688 edit_free_file(file);
2689 free(section_template);
2692 err = map(exports, "sections", 1, sections, 0);
2699 /* Could also do global exports here, but it wouldn't be as fast */
2701 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2705 /*********************************************************************
2707 * Function : cgi_edit_actions_for_url
2709 * Description : CGI function that edits the Actions list.
2712 * 1 : csp = Current client state (buffers, headers, etc...)
2713 * 2 : rsp = http_response data structure for output
2714 * 3 : parameters = map of cgi parameters
2716 * CGI Parameters : None
2718 * Returns : JB_ERR_OK on success
2719 * JB_ERR_MEMORY on out-of-memory
2720 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2721 * specified or not valid.
2723 *********************************************************************/
2724 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2725 struct http_response *rsp,
2726 const struct map *parameters)
2728 struct map * exports;
2730 struct editable_file * file;
2731 struct file_line * cur_line;
2732 unsigned line_number;
2734 struct re_filterfile_spec *filter_group;
2735 int i, have_filters = 0;
2737 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2739 return cgi_error_disabled(csp, rsp);
2742 err = get_number_param(csp, parameters, "s", §ionid);
2748 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2751 /* No filename specified, can't read file, modified, or out of memory. */
2752 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2755 cur_line = file->lines;
2757 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2759 cur_line = cur_line->next;
2762 if ( (cur_line == NULL)
2763 || (line_number != sectionid)
2765 || (cur_line->type != FILE_LINE_ACTION))
2767 /* Invalid "sectionid" parameter */
2768 edit_free_file(file);
2769 return JB_ERR_CGI_PARAMS;
2772 if (NULL == (exports = default_exports(csp, NULL)))
2774 edit_free_file(file);
2775 return JB_ERR_MEMORY;
2778 err = map(exports, "f", 1, stringify(file->identifier), 0);
2779 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2780 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2782 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2785 * XXX: Some browsers (at least IE6 and IE7) have an artificial URL
2786 * length limitation and ignore clicks on the Submit buttons if
2787 * the resulting GET URL would be longer than their limit.
2789 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
2790 * reached this limit and action editing stopped working in these
2791 * browsers (BR #1570678).
2793 * The config option split-large-forms works around this browser
2794 * bug (HTTP has no URL length limitation) by deviding the action
2795 * list form into multiple smaller ones. It means the URLs are shorter
2796 * and work in broken browsers as well, but the user can no longer change
2797 * all actions with one submit.
2799 * A better solution would be to switch to POST requests,
2800 * but this will do for now.
2802 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
2804 /* Generate multiple smaller form by killing the big one. */
2805 err = map_block_killer(exports, "one-form-only");
2809 /* Default: Generate one large form by killing the smaller ones. */
2810 err = map_block_killer(exports, "multiple-forms");
2813 for (i = 0; i < MAX_AF_FILES; i++)
2815 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2817 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
2825 edit_free_file(file);
2830 if (0 == have_filters)
2832 err = map(exports, "filter-params", 1, "", 1);
2837 * List available filters and their settings.
2839 char *filter_template;
2840 int filter_identifier = 0;
2841 char *prepared_templates[MAX_FILTER_TYPES];
2843 for (i = 0; i < MAX_FILTER_TYPES; i++)
2845 prepared_templates[i] = strdup("");
2848 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2851 edit_free_file(file);
2853 if (err == JB_ERR_FILE)
2855 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2860 err = template_fill(&filter_template, exports);
2862 for (i = 0; i < MAX_AF_FILES; i++)
2864 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2866 filter_group = csp->rlist[i]->f;
2867 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2869 char current_mode = 'x';
2871 struct list_entry *filter_name;
2872 struct map *line_exports;
2873 const int type = filter_group->type;
2874 const int multi_action_index = filter_type_info[type].multi_action_index;
2876 assert(type < MAX_FILTER_TYPES);
2878 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
2879 while ((filter_name != NULL)
2880 && (0 != strcmp(filter_group->name, filter_name->str)))
2882 filter_name = filter_name->next;
2885 if (filter_name != NULL)
2891 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
2892 while ((filter_name != NULL)
2893 && (0 != strcmp(filter_group->name, filter_name->str)))
2895 filter_name = filter_name->next;
2897 if (filter_name != NULL)
2903 /* Generate a unique serial number */
2904 snprintf(number, sizeof(number), "%x", filter_identifier++);
2905 number[sizeof(number) - 1] = '\0';
2907 line_exports = new_map();
2908 if (line_exports == NULL)
2910 err = JB_ERR_MEMORY;
2916 if (!err) err = map(line_exports, "index", 1, number, 1);
2917 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2918 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2919 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2920 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
2921 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
2922 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
2926 filter_line = strdup(filter_template);
2927 if (filter_line == NULL) err = JB_ERR_MEMORY;
2929 if (!err) err = template_fill(&filter_line, line_exports);
2930 string_join(&prepared_templates[type], filter_line);
2932 free_map(line_exports);
2937 freez(filter_template);
2939 /* Replace all filter macros with the aggregated templates */
2940 for (i = 0; i < MAX_FILTER_TYPES; i++)
2943 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
2948 /* Free aggregated templates */
2949 for (i = 0; i < MAX_FILTER_TYPES; i++)
2951 freez(prepared_templates[i]);
2956 /* Check or uncheck the "disable all of this type" radio buttons. */
2957 for (i = 0; i < MAX_FILTER_TYPES; i++)
2959 const int a = filter_type_info[i].multi_action_index;
2960 const int disable_all = cur_line->data.action->multi_remove_all[a];
2962 err = map_radio(exports, filter_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
2965 edit_free_file(file);
2973 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2977 /*********************************************************************
2979 * Function : cgi_edit_actions_submit
2981 * Description : CGI function that actually edits the Actions list.
2984 * 1 : csp = Current client state (buffers, headers, etc...)
2985 * 2 : rsp = http_response data structure for output
2986 * 3 : parameters = map of cgi parameters
2988 * CGI Parameters : None
2990 * Returns : JB_ERR_OK on success
2991 * JB_ERR_MEMORY on out-of-memory
2992 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2993 * specified or not valid.
2995 *********************************************************************/
2996 jb_err cgi_edit_actions_submit(struct client_state *csp,
2997 struct http_response *rsp,
2998 const struct map *parameters)
3003 size_t newtext_size;
3005 struct editable_file * file;
3006 struct file_line * cur_line;
3007 unsigned line_number;
3010 int filter_identifier;
3012 const char * action_set_name;
3013 struct file_list * fl;
3014 struct url_actions * b;
3016 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3018 return cgi_error_disabled(csp, rsp);
3021 err = get_number_param(csp, parameters, "s", §ionid);
3027 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3030 /* No filename specified, can't read file, modified, or out of memory. */
3031 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3034 cur_line = file->lines;
3036 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3038 cur_line = cur_line->next;
3041 if ( (cur_line == NULL)
3042 || (line_number != sectionid)
3044 || (cur_line->type != FILE_LINE_ACTION))
3046 /* Invalid "sectionid" parameter */
3047 edit_free_file(file);
3048 return JB_ERR_CGI_PARAMS;
3051 get_string_param(parameters, "p", &action_set_name);
3052 if (action_set_name != NULL)
3054 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3056 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3058 for (b = b->next; NULL != b; b = b->next)
3060 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3062 copy_action(cur_line->data.action, b->action);
3068 edit_free_file(file);
3069 return JB_ERR_CGI_PARAMS;
3075 err = actions_from_radio(parameters, cur_line->data.action);
3081 edit_free_file(file);
3085 /* Check the "disable all of this type" parameters. */
3086 for (i = 0; i < MAX_FILTER_TYPES; i++)
3088 const int multi_action_index = filter_type_info[i].multi_action_index;
3089 const char ch = get_char_param(parameters, filter_type_info[i].disable_all_param);
3093 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3094 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3095 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3099 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3103 for (filter_identifier = 0; !err; filter_identifier++)
3110 * Filter state. Valid states are: 'Y' (active),
3111 * 'N' (inactive) and 'X' (no change).
3115 * Abbreviated filter type. Valid types are: 'F' (content filter),
3116 * 'S' (server-header filter) and 'C' (client-header filter).
3118 int multi_action_index = 0;
3120 /* Generate the keys */
3121 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3122 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3123 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3124 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3125 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3127 err = get_string_param(parameters, key_name, &name);
3136 type = get_char_param(parameters, key_type);
3140 multi_action_index = ACTION_MULTI_FILTER;
3143 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3146 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3149 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3152 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3155 log_error(LOG_LEVEL_ERROR,
3156 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3159 assert(multi_action_index);
3161 value = get_char_param(parameters, key_value);
3164 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3165 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3166 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3168 else if (value == 'N')
3170 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3171 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3173 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3174 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3177 else if (value == 'X')
3179 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3180 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3187 edit_free_file(file);
3191 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3194 edit_free_file(file);
3195 return JB_ERR_MEMORY;
3198 len = strlen(actiontext);
3202 * Empty action - must special-case this.
3203 * Simply setting len to 1 is sufficient...
3208 newtext_size = len + 2;
3209 newtext = malloc_or_die(newtext_size);
3210 strlcpy(newtext, actiontext, newtext_size);
3214 newtext[len + 1] = '\0';
3216 freez(cur_line->raw);
3217 freez(cur_line->unprocessed);
3218 cur_line->unprocessed = newtext;
3220 err = edit_write_file(file);
3223 /* Error writing file */
3224 if (err == JB_ERR_FILE)
3226 /* Read-only file. */
3227 err = cgi_error_file_read_only(csp, rsp, file->filename);
3229 edit_free_file(file);
3233 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3234 (long) time(NULL), file->identifier, sectionid);
3236 edit_free_file(file);
3238 return cgi_redirect(rsp, target);
3242 /*********************************************************************
3244 * Function : cgi_edit_actions_url
3246 * Description : CGI function that actually edits a URL pattern in
3250 * 1 : csp = Current client state (buffers, headers, etc...)
3251 * 2 : rsp = http_response data structure for output
3252 * 3 : parameters = map of cgi parameters
3255 * filename : Identifies the file to edit
3256 * ver : File's last-modified time
3257 * section : Line number of section to edit
3258 * pattern : Line number of pattern to edit
3259 * newval : New value for pattern
3261 * Returns : JB_ERR_OK on success
3262 * JB_ERR_MEMORY on out-of-memory
3263 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3264 * specified or not valid.
3266 *********************************************************************/
3267 jb_err cgi_edit_actions_url(struct client_state *csp,
3268 struct http_response *rsp,
3269 const struct map *parameters)
3273 struct editable_file * file;
3274 struct file_line * cur_line;
3275 unsigned line_number;
3276 unsigned section_start_line_number = 0;
3284 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3286 return cgi_error_disabled(csp, rsp);
3289 err = get_number_param(csp, parameters, "p", &patternid);
3296 return JB_ERR_CGI_PARAMS;
3299 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3305 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3308 /* No filename specified, can't read file, modified, or out of memory. */
3310 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3314 cur_line = file->lines;
3316 while ((cur_line != NULL) && (line_number < patternid))
3318 if (cur_line->type == FILE_LINE_ACTION)
3320 section_start_line_number = line_number;
3322 cur_line = cur_line->next;
3326 if ((cur_line == NULL)
3327 || (cur_line->type != FILE_LINE_URL))
3329 /* Invalid "patternid" parameter */
3331 edit_free_file(file);
3332 return JB_ERR_CGI_PARAMS;
3335 /* At this point, the line to edit is in cur_line */
3337 freez(cur_line->raw);
3338 freez(cur_line->unprocessed);
3339 cur_line->unprocessed = new_pattern;
3341 err = edit_write_file(file);
3344 /* Error writing file */
3345 if (err == JB_ERR_FILE)
3347 /* Read-only file. */
3348 err = cgi_error_file_read_only(csp, rsp, file->filename);
3350 edit_free_file(file);
3354 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3355 (long) time(NULL), file->identifier, section_start_line_number);
3357 edit_free_file(file);
3359 return cgi_redirect(rsp, target);
3363 /*********************************************************************
3365 * Function : cgi_edit_actions_add_url
3367 * Description : CGI function that actually adds a URL pattern to
3371 * 1 : csp = Current client state (buffers, headers, etc...)
3372 * 2 : rsp = http_response data structure for output
3373 * 3 : parameters = map of cgi parameters
3376 * filename : Identifies the file to edit
3377 * ver : File's last-modified time
3378 * section : Line number of section to edit
3379 * newval : New pattern
3381 * Returns : JB_ERR_OK on success
3382 * JB_ERR_MEMORY on out-of-memory
3383 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3384 * specified or not valid.
3386 *********************************************************************/
3387 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3388 struct http_response *rsp,
3389 const struct map *parameters)
3393 struct file_line * new_line;
3394 struct editable_file * file;
3395 struct file_line * cur_line;
3396 unsigned line_number;
3400 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3402 return cgi_error_disabled(csp, rsp);
3405 err = get_number_param(csp, parameters, "s", §ionid);
3412 return JB_ERR_CGI_PARAMS;
3415 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3421 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3424 /* No filename specified, can't read file, modified, or out of memory. */
3426 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3430 cur_line = file->lines;
3432 while ((cur_line != NULL) && (line_number < sectionid))
3434 cur_line = cur_line->next;
3438 if ((cur_line == NULL)
3439 || (cur_line->type != FILE_LINE_ACTION))
3441 /* Invalid "sectionid" parameter */
3443 edit_free_file(file);
3444 return JB_ERR_CGI_PARAMS;
3447 /* At this point, the section header is in cur_line - add after this. */
3449 /* Allocate the new line */
3450 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3451 if (new_line == NULL)
3454 edit_free_file(file);
3455 return JB_ERR_MEMORY;
3458 /* Fill in the data members of the new line */
3459 new_line->raw = NULL;
3460 new_line->prefix = NULL;
3461 new_line->unprocessed = new_pattern;
3462 new_line->type = FILE_LINE_URL;
3464 /* Link new_line into the list, after cur_line */
3465 new_line->next = cur_line->next;
3466 cur_line->next = new_line;
3468 /* Done making changes, now commit */
3470 err = edit_write_file(file);
3473 /* Error writing file */
3474 if (err == JB_ERR_FILE)
3476 /* Read-only file. */
3477 err = cgi_error_file_read_only(csp, rsp, file->filename);
3479 edit_free_file(file);
3483 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3484 (long) time(NULL), file->identifier, sectionid);
3486 edit_free_file(file);
3488 return cgi_redirect(rsp, target);
3492 /*********************************************************************
3494 * Function : cgi_edit_actions_remove_url
3496 * Description : CGI function that actually removes a URL pattern from
3500 * 1 : csp = Current client state (buffers, headers, etc...)
3501 * 2 : rsp = http_response data structure for output
3502 * 3 : parameters = map of cgi parameters
3505 * f : (filename) Identifies the file to edit
3506 * v : (version) File's last-modified time
3507 * p : (pattern) Line number of pattern to remove
3509 * Returns : JB_ERR_OK on success
3510 * JB_ERR_MEMORY on out-of-memory
3511 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3512 * specified or not valid.
3514 *********************************************************************/
3515 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3516 struct http_response *rsp,
3517 const struct map *parameters)
3520 struct editable_file * file;
3521 struct file_line * cur_line;
3522 struct file_line * prev_line;
3523 unsigned line_number;
3524 unsigned section_start_line_number = 0;
3528 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3530 return cgi_error_disabled(csp, rsp);
3533 err = get_number_param(csp, parameters, "p", &patternid);
3539 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3542 /* No filename specified, can't read file, modified, or out of memory. */
3543 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3548 cur_line = file->lines;
3550 while ((cur_line != NULL) && (line_number < patternid))
3552 if (cur_line->type == FILE_LINE_ACTION)
3554 section_start_line_number = line_number;
3556 prev_line = cur_line;
3557 cur_line = cur_line->next;
3561 if ( (cur_line == NULL)
3562 || (prev_line == NULL)
3563 || (cur_line->type != FILE_LINE_URL))
3565 /* Invalid "patternid" parameter */
3566 edit_free_file(file);
3567 return JB_ERR_CGI_PARAMS;
3570 /* At this point, the line to remove is in cur_line, and the previous
3571 * one is in prev_line
3574 /* Unlink cur_line */
3575 prev_line->next = cur_line->next;
3576 cur_line->next = NULL;
3579 edit_free_file_lines(cur_line);
3581 err = edit_write_file(file);
3584 /* Error writing file */
3585 if (err == JB_ERR_FILE)
3587 /* Read-only file. */
3588 err = cgi_error_file_read_only(csp, rsp, file->filename);
3590 edit_free_file(file);
3594 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3595 (long) time(NULL), file->identifier, section_start_line_number);
3597 edit_free_file(file);
3599 return cgi_redirect(rsp, target);
3603 /*********************************************************************
3605 * Function : cgi_edit_actions_section_remove
3607 * Description : CGI function that actually removes a whole section from
3608 * the actions file. The section must be empty first
3609 * (else JB_ERR_CGI_PARAMS).
3612 * 1 : csp = Current client state (buffers, headers, etc...)
3613 * 2 : rsp = http_response data structure for output
3614 * 3 : parameters = map of cgi parameters
3617 * f : (filename) Identifies the file to edit
3618 * v : (version) File's last-modified time
3619 * s : (section) Line number of section to edit
3621 * Returns : JB_ERR_OK on success
3622 * JB_ERR_MEMORY on out-of-memory
3623 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3624 * specified or not valid.
3626 *********************************************************************/
3627 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3628 struct http_response *rsp,
3629 const struct map *parameters)
3632 struct editable_file * file;
3633 struct file_line * cur_line;
3634 struct file_line * prev_line;
3635 unsigned line_number;
3639 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3641 return cgi_error_disabled(csp, rsp);
3644 err = get_number_param(csp, parameters, "s", §ionid);
3650 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3653 /* No filename specified, can't read file, modified, or out of memory. */
3654 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3658 cur_line = file->lines;
3661 while ((cur_line != NULL) && (line_number < sectionid))
3663 prev_line = cur_line;
3664 cur_line = cur_line->next;
3668 if ((cur_line == NULL)
3669 || (cur_line->type != FILE_LINE_ACTION))
3671 /* Invalid "sectionid" parameter */
3672 edit_free_file(file);
3673 return JB_ERR_CGI_PARAMS;
3676 if ((cur_line->next != NULL)
3677 && (cur_line->next->type == FILE_LINE_URL))
3679 /* Section not empty. */
3680 edit_free_file(file);
3681 return JB_ERR_CGI_PARAMS;
3684 /* At this point, the line to remove is in cur_line, and the previous
3685 * one is in prev_line
3688 /* Unlink cur_line */
3689 if (prev_line == NULL)
3691 /* Removing the first line from the file */
3692 file->lines = cur_line->next;
3696 prev_line->next = cur_line->next;
3698 cur_line->next = NULL;
3701 edit_free_file_lines(cur_line);
3703 err = edit_write_file(file);
3706 /* Error writing file */
3707 if (err == JB_ERR_FILE)
3709 /* Read-only file. */
3710 err = cgi_error_file_read_only(csp, rsp, file->filename);
3712 edit_free_file(file);
3716 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
3717 (long) time(NULL), file->identifier);
3719 edit_free_file(file);
3721 return cgi_redirect(rsp, target);
3725 /*********************************************************************
3727 * Function : cgi_edit_actions_section_add
3729 * Description : CGI function that adds a new empty section to
3733 * 1 : csp = Current client state (buffers, headers, etc...)
3734 * 2 : rsp = http_response data structure for output
3735 * 3 : parameters = map of cgi parameters
3738 * f : (filename) Identifies the file to edit
3739 * v : (version) File's last-modified time
3740 * s : (section) Line number of section to add after, 0 for
3743 * Returns : JB_ERR_OK on success
3744 * JB_ERR_MEMORY on out-of-memory
3745 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3746 * specified or not valid.
3748 *********************************************************************/
3749 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3750 struct http_response *rsp,
3751 const struct map *parameters)
3754 struct file_line * new_line;
3756 struct editable_file * file;
3757 struct file_line * cur_line;
3758 unsigned line_number;
3762 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3764 return cgi_error_disabled(csp, rsp);
3767 err = get_number_param(csp, parameters, "s", §ionid);
3773 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3776 /* No filename specified, can't read file, modified, or out of memory. */
3777 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3781 cur_line = file->lines;
3783 if (sectionid <= 1U)
3785 /* Add to start of file */
3786 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
3788 /* There's something in the file, find the line before the first
3791 while ((cur_line->next != NULL)
3792 && (cur_line->next->type != FILE_LINE_ACTION))
3794 cur_line = cur_line->next;
3800 /* File starts with action line, so insert at top */
3806 /* Add after stated section. */
3807 while ((cur_line != NULL) && (line_number < sectionid))
3809 cur_line = cur_line->next;
3813 if ((cur_line == NULL)
3814 || (cur_line->type != FILE_LINE_ACTION))
3816 /* Invalid "sectionid" parameter */
3817 edit_free_file(file);
3818 return JB_ERR_CGI_PARAMS;
3821 /* Skip through the section to find the last line in it. */
3822 while ((cur_line->next != NULL)
3823 && (cur_line->next->type != FILE_LINE_ACTION))
3825 cur_line = cur_line->next;
3830 /* At this point, the last line in the previous section is in cur_line
3831 * - add after this. (Or if we need to add as the first line, cur_line
3835 new_text = strdup("{}");
3836 if (NULL == new_text)
3838 edit_free_file(file);
3839 return JB_ERR_MEMORY;
3842 /* Allocate the new line */
3843 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3844 if (new_line == NULL)
3847 edit_free_file(file);
3848 return JB_ERR_MEMORY;
3851 /* Fill in the data members of the new line */
3852 new_line->raw = NULL;
3853 new_line->prefix = NULL;
3854 new_line->unprocessed = new_text;
3855 new_line->type = FILE_LINE_ACTION;
3857 if (cur_line != NULL)
3859 /* Link new_line into the list, after cur_line */
3860 new_line->next = cur_line->next;
3861 cur_line->next = new_line;
3865 /* Link new_line into the list, as first line */
3866 new_line->next = file->lines;
3867 file->lines = new_line;
3870 /* Done making changes, now commit */
3872 err = edit_write_file(file);
3875 /* Error writing file */
3876 if (err == JB_ERR_FILE)
3878 /* Read-only file. */
3879 err = cgi_error_file_read_only(csp, rsp, file->filename);
3881 edit_free_file(file);
3885 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
3886 (long) time(NULL), file->identifier);
3888 edit_free_file(file);
3890 return cgi_redirect(rsp, target);
3894 /*********************************************************************
3896 * Function : cgi_edit_actions_section_swap
3898 * Description : CGI function that swaps the order of two sections
3899 * in the actions file. Note that this CGI can actually
3900 * swap any two arbitrary sections, but the GUI interface
3901 * currently only allows consecutive sections to be
3905 * 1 : csp = Current client state (buffers, headers, etc...)
3906 * 2 : rsp = http_response data structure for output
3907 * 3 : parameters = map of cgi parameters
3910 * f : (filename) Identifies the file to edit
3911 * v : (version) File's last-modified time
3912 * s1 : (section1) Line number of first section to swap
3913 * s2 : (section2) Line number of second section to swap
3915 * Returns : JB_ERR_OK on success
3916 * JB_ERR_MEMORY on out-of-memory
3917 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3918 * specified or not valid.
3920 *********************************************************************/
3921 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3922 struct http_response *rsp,
3923 const struct map *parameters)
3927 struct editable_file * file;
3928 struct file_line * cur_line;
3929 struct file_line * prev_line;
3930 struct file_line * line_before_section1;
3931 struct file_line * line_start_section1;
3932 struct file_line * line_end_section1;
3933 struct file_line * line_after_section1;
3934 struct file_line * line_before_section2;
3935 struct file_line * line_start_section2;
3936 struct file_line * line_end_section2;
3937 struct file_line * line_after_section2;
3938 unsigned line_number;
3942 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3944 return cgi_error_disabled(csp, rsp);
3947 err = get_number_param(csp, parameters, "s1", §ion1);
3948 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3954 if (section1 > section2)
3956 unsigned temp = section2;
3957 section2 = section1;
3961 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3964 /* No filename specified, can't read file, modified, or out of memory. */
3965 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3968 /* Start at the beginning... */
3970 cur_line = file->lines;
3973 /* ... find section1 ... */
3974 while ((cur_line != NULL) && (line_number < section1))
3976 prev_line = cur_line;
3977 cur_line = cur_line->next;
3981 if ((cur_line == NULL)
3982 || (cur_line->type != FILE_LINE_ACTION))
3984 /* Invalid "section1" parameter */
3985 edit_free_file(file);
3986 return JB_ERR_CGI_PARAMS;
3989 /* If no-op, we've validated params and can skip the rest. */
3990 if (section1 != section2)
3992 /* ... find the end of section1 ... */
3993 line_before_section1 = prev_line;
3994 line_start_section1 = cur_line;
3997 prev_line = cur_line;
3998 cur_line = cur_line->next;
4001 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4002 line_end_section1 = prev_line;
4003 line_after_section1 = cur_line;
4005 /* ... find section2 ... */
4006 while ((cur_line != NULL) && (line_number < section2))
4008 prev_line = cur_line;
4009 cur_line = cur_line->next;
4013 if ((cur_line == NULL)
4014 || (cur_line->type != FILE_LINE_ACTION))
4016 /* Invalid "section2" parameter */
4017 edit_free_file(file);
4018 return JB_ERR_CGI_PARAMS;
4021 /* ... find the end of section2 ... */
4022 line_before_section2 = prev_line;
4023 line_start_section2 = cur_line;
4026 prev_line = cur_line;
4027 cur_line = cur_line->next;
4030 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4031 line_end_section2 = prev_line;
4032 line_after_section2 = cur_line;
4034 /* Now have all the pointers we need. Do the swap. */
4036 /* Change the pointer to section1 to point to section2 instead */
4037 if (line_before_section1 == NULL)
4039 file->lines = line_start_section2;
4043 line_before_section1->next = line_start_section2;
4046 if (line_before_section2 == line_end_section1)
4048 /* Consecutive sections */
4049 line_end_section2->next = line_start_section1;
4053 line_end_section2->next = line_after_section1;
4054 line_before_section2->next = line_start_section1;
4057 /* Set the pointer from the end of section1 to the rest of the file */
4058 line_end_section1->next = line_after_section2;
4060 err = edit_write_file(file);
4063 /* Error writing file */
4064 if (err == JB_ERR_FILE)
4066 /* Read-only file. */
4067 err = cgi_error_file_read_only(csp, rsp, file->filename);
4069 edit_free_file(file);
4072 } /* END if (section1 != section2) */
4074 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4075 (long) time(NULL), file->identifier);
4077 edit_free_file(file);
4079 return cgi_redirect(rsp, target);
4083 /*********************************************************************
4085 * Function : javascriptify
4087 * Description : Converts a string into a form JavaScript will like.
4089 * Netscape 4's JavaScript sucks - it doesn't use
4090 * "id" parameters, so you have to set the "name"
4091 * used to submit a form element to something JavaScript
4092 * will like. (Or access the elements by index in an
4093 * array. That array contains >60 elements and will
4094 * be changed whenever we add a new action to the
4095 * editor, so I'm NOT going to use indexes that have
4096 * to be figured out by hand.)
4098 * Currently the only thing we have to worry about
4099 * is "-" ==> "_" conversion.
4101 * This is a length-preserving operation so it is
4102 * carried out in-place, no memory is allocated
4106 * 1 : identifier = String to make JavaScript-friendly.
4110 *********************************************************************/
4111 static void javascriptify(char * identifier)
4113 char * p = identifier;
4114 while (NULL != (p = strchr(p, '-')))
4121 /*********************************************************************
4123 * Function : actions_to_radio
4125 * Description : Converts a actionsfile entry into settings for
4126 * radio buttons and edit boxes on a HTML form.
4129 * 1 : exports = List of substitutions to add to.
4130 * 2 : action = Action to read
4132 * Returns : JB_ERR_OK on success
4133 * JB_ERR_MEMORY on out-of-memory
4135 *********************************************************************/
4136 static jb_err actions_to_radio(struct map * exports,
4137 const struct action_spec *action)
4148 mask = action->mask;
4151 /* sanity - prevents "-feature +feature" */
4155 #define DEFINE_ACTION_BOOL(name, bit) \
4156 if (!(mask & bit)) \
4158 current_mode = 'n'; \
4160 else if (add & bit) \
4162 current_mode = 'y'; \
4166 current_mode = 'x'; \
4168 if (map_radio(exports, name, "ynx", current_mode)) \
4170 return JB_ERR_MEMORY; \
4173 #define DEFINE_ACTION_STRING(name, bit, index) \
4174 DEFINE_ACTION_BOOL(name, bit); \
4177 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4180 checked = !strcmp(action->string[index], value); \
4184 checked = is_default; \
4186 mapped_param |= checked; \
4187 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4189 return JB_ERR_MEMORY; \
4192 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4193 if (map(exports, name "-param-custom", 1, \
4194 ((!mapped_param) ? "checked" : ""), 1)) \
4196 return JB_ERR_MEMORY; \
4198 if (map(exports, name "-param", 1, \
4199 (((add & bit) && !mapped_param) ? \
4200 action->string[index] : default_val), 1)) \
4202 return JB_ERR_MEMORY; \
4205 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4206 if (map(exports, name "-param", 1, \
4207 ((add & bit) ? action->string[index] : default_val), 1)) \
4209 return JB_ERR_MEMORY; \
4212 #define DEFINE_ACTION_MULTI(name, index) \
4213 if (action->multi_add[index]->first) \
4215 current_mode = 'y'; \
4217 else if (action->multi_remove_all[index]) \
4219 current_mode = 'n'; \
4221 else if (action->multi_remove[index]->first) \
4223 current_mode = 'y'; \
4227 current_mode = 'x'; \
4229 if (map_radio(exports, name, "ynx", current_mode)) \
4231 return JB_ERR_MEMORY; \
4234 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4236 #include "actionlist.h"
4238 #undef DEFINE_ACTION_MULTI
4239 #undef DEFINE_ACTION_STRING
4240 #undef DEFINE_ACTION_BOOL
4241 #undef DEFINE_ACTION_ALIAS
4242 #undef DEFINE_CGI_PARAM_CUSTOM
4243 #undef DEFINE_CGI_PARAM_RADIO
4244 #undef DEFINE_CGI_PARAM_NO_RADIO
4250 /*********************************************************************
4252 * Function : actions_from_radio
4254 * Description : Converts a map of parameters passed to a CGI function
4255 * into an actionsfile entry.
4258 * 1 : parameters = parameters to the CGI call
4259 * 2 : action = Action to change. Must be valid before
4260 * the call, actions not specified will be
4263 * Returns : JB_ERR_OK on success
4264 * JB_ERR_MEMORY on out-of-memory
4266 *********************************************************************/
4267 static jb_err actions_from_radio(const struct map * parameters,
4268 struct action_spec *action)
4273 const char * js_name;
4274 jb_err err = JB_ERR_OK;
4279 /* Statics are generally a potential race condition,
4280 * but in this case we're safe and don't need semaphores.
4281 * Be careful if you modify this function.
4283 * The js_name_arr's are never free()d, but this is no
4284 * problem, since they will only be created once and
4285 * used by all threads thereafter. -oes
4288 #define JAVASCRIPTIFY(dest_var, string) \
4290 static int first_time = 1; \
4291 static char *js_name_arr; \
4294 js_name_arr = strdup(string); \
4295 javascriptify(js_name_arr); \
4297 dest_var = js_name_arr; \
4301 #define DEFINE_ACTION_BOOL(name, bit) \
4302 JAVASCRIPTIFY(js_name, name); \
4303 ch = get_char_param(parameters, js_name); \
4306 action->add |= bit; \
4307 action->mask |= bit; \
4309 else if (ch == 'N') \
4311 action->add &= ~bit; \
4312 action->mask &= ~bit; \
4314 else if (ch == 'X') \
4316 action->add &= ~bit; \
4317 action->mask |= bit; \
4320 #define DEFINE_ACTION_STRING(name, bit, index) \
4321 JAVASCRIPTIFY(js_name, name); \
4322 ch = get_char_param(parameters, js_name); \
4326 JAVASCRIPTIFY(js_name, name "-mode"); \
4327 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4328 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4330 JAVASCRIPTIFY(js_name, name "-param"); \
4331 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4333 if (param != NULL) \
4335 if (NULL == (param_dup = strdup(param))) \
4337 return JB_ERR_MEMORY; \
4339 freez(action->string[index]); \
4340 action->add |= bit; \
4341 action->mask |= bit; \
4342 action->string[index] = param_dup; \
4345 else if (ch == 'N') \
4347 if (action->add & bit) \
4349 freez(action->string[index]); \
4351 action->add &= ~bit; \
4352 action->mask &= ~bit; \
4354 else if (ch == 'X') \
4356 if (action->add & bit) \
4358 freez(action->string[index]); \
4360 action->add &= ~bit; \
4361 action->mask |= bit; \
4364 #define DEFINE_ACTION_MULTI(name, index) \
4365 JAVASCRIPTIFY(js_name, name); \
4366 ch = get_char_param(parameters, js_name); \
4371 else if (ch == 'N') \
4373 list_remove_all(action->multi_add[index]); \
4374 list_remove_all(action->multi_remove[index]); \
4375 action->multi_remove_all[index] = 1; \
4377 else if (ch == 'X') \
4379 list_remove_all(action->multi_add[index]); \
4380 list_remove_all(action->multi_remove[index]); \
4381 action->multi_remove_all[index] = 0; \
4384 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4386 #include "actionlist.h"
4388 #undef DEFINE_ACTION_MULTI
4389 #undef DEFINE_ACTION_STRING
4390 #undef DEFINE_ACTION_BOOL
4391 #undef DEFINE_ACTION_ALIAS
4392 #undef JAVASCRIPTIFY
4396 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4399 #ifdef FEATURE_TOGGLE
4400 /*********************************************************************
4402 * Function : cgi_toggle
4404 * Description : CGI function that adds a new empty section to
4408 * 1 : csp = Current client state (buffers, headers, etc...)
4409 * 2 : rsp = http_response data structure for output
4410 * 3 : parameters = map of cgi parameters
4413 * set : If present, how to change toggle setting:
4414 * "enable", "disable", "toggle", or none (default).
4415 * mini : If present, use mini reply template.
4417 * Returns : JB_ERR_OK on success
4418 * JB_ERR_MEMORY on out-of-memory
4420 *********************************************************************/
4421 jb_err cgi_toggle(struct client_state *csp,
4422 struct http_response *rsp,
4423 const struct map *parameters)
4425 struct map *exports;
4427 const char *template_name;
4433 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4435 return cgi_error_disabled(csp, rsp);
4438 mode = get_char_param(parameters, "set");
4443 global_toggle_state = 1;
4445 else if (mode == 'D')
4448 global_toggle_state = 0;
4450 else if (mode == 'T')
4453 global_toggle_state = !global_toggle_state;
4456 if (NULL == (exports = default_exports(csp, "toggle")))
4458 return JB_ERR_MEMORY;
4461 template_name = (get_char_param(parameters, "mini")
4465 return template_fill_for_cgi(csp, template_name, exports, rsp);
4467 #endif /* def FEATURE_TOGGLE */