1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.82 2014/06/03 10:31:27 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * NOTE: The CGIs in this file use parameter names
9 * such as "f" and "s" which are really *BAD* choices.
10 * However, I'm trying to save bytes in the
11 * edit-actions-list HTML page - the standard actions
12 * file generated a 550kbyte page, which is ridiculous.
14 * Stick to the short names in this file for consistency.
16 * Copyright : Written by and Copyright (C) 2001-2014 the
17 * Privoxy team. http://www.privoxy.org/
19 * Based on the Internet Junkbuster originally written
20 * by and Copyright (C) 1997 Anonymous Coders and
21 * Junkbusters Corporation. http://www.junkbusters.com
23 * This program is free software; you can redistribute it
24 * and/or modify it under the terms of the GNU General
25 * Public License as published by the Free Software
26 * Foundation; either version 2 of the License, or (at
27 * your option) any later version.
29 * This program is distributed in the hope that it will
30 * be useful, but WITHOUT ANY WARRANTY; without even the
31 * implied warranty of MERCHANTABILITY or FITNESS FOR A
32 * PARTICULAR PURPOSE. See the GNU General Public
33 * License for more details.
35 * The GNU General Public License should be included with
36 * this file. If not, you can view it at
37 * http://www.gnu.org/copyleft/gpl.html
38 * or write to the Free Software Foundation, Inc., 59
39 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 **********************************************************************/
47 * FIXME: Following includes copied from cgi.c - which are actually needed?
52 #include <sys/types.h>
61 #include "cgisimple.h"
69 /* loadcfg.h is for global_toggle_state only */
71 #endif /* def FEATURE_TOGGLE */
74 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
77 #ifdef FEATURE_CGI_EDIT_ACTIONS
80 * A line in an editable_file.
84 /** Next entry in the linked list */
85 struct file_line * next;
87 /** The raw data, to write out if this line is unmodified. */
90 /** Comments and/or whitespace to put before this line if it's modified
91 and then written out. */
94 /** The actual data, as a string. Line continuation and comment removal
95 are performed on the data read from file before it's stored here, so
96 it will be a single line of data. */
99 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
102 /** The actual data, processed into some sensible data type. */
106 /** An action specification. */
107 struct action_spec action[1];
109 /** A name=value pair. */
113 /** The name in the name=value pair. */
116 /** The value in the name=value pair, as a string. */
119 /** The value in the name=value pair, as an integer. */
128 /** This file_line has not been processed yet. */
129 #define FILE_LINE_UNPROCESSED 1
131 /** This file_line is blank. Can only appear at the end of a file, due to
132 the way the parser works. */
133 #define FILE_LINE_BLANK 2
135 /** This file_line says {{alias}}. */
136 #define FILE_LINE_ALIAS_HEADER 3
138 /** This file_line defines an alias. */
139 #define FILE_LINE_ALIAS_ENTRY 4
141 /** This file_line defines an {action}. */
142 #define FILE_LINE_ACTION 5
144 /** This file_line specifies a URL pattern. */
145 #define FILE_LINE_URL 6
147 /** This file_line says {{settings}}. */
148 #define FILE_LINE_SETTINGS_HEADER 7
150 /** This file_line is in a {{settings}} block. */
151 #define FILE_LINE_SETTINGS_ENTRY 8
153 /** This file_line says {{description}}. */
154 #define FILE_LINE_DESCRIPTION_HEADER 9
156 /** This file_line is in a {{description}} block. */
157 #define FILE_LINE_DESCRIPTION_ENTRY 10
160 * Number of file modification time mismatches
161 * before the CGI editor gets turned off.
163 #define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
166 * A configuration file, in a format that can be edited and written back to
171 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
172 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
173 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
174 const char * version_str; /**< Last modification time, as a string. For CGI param. */
175 /**< Can be used in URL without using url_param(). */
176 unsigned version; /**< Last modification time - prevents chaos with
177 the browser's "back" button. Note that this is a
178 time_t cast to an unsigned. When comparing, always
179 cast the time_t to an unsigned, and *NOT* vice-versa.
180 This may lose the top few bits, but they're not
181 significant anyway. */
182 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
183 Note that changing this after the file has been
184 read in will cause a mess. */
185 struct file_line * parse_error; /**< On parse error, this is the offending line. */
186 const char * parse_error_text; /**< On parse error, this is the problem.
187 (Statically allocated) */
191 * Information about the filter types.
192 * Used for macro replacement in cgi_edit_actions_for_url.
194 struct filter_type_info
196 const int multi_action_index; /**< The multi action index as defined in project.h */
197 const char *macro_name; /**< Name of the macro that has to be replaced
198 with the prepared templates.
199 For example "content-filter-params" */
200 const char *type; /**< Name of the filter type,
201 for example "server-header-filter". */
202 /* XXX: check if these two can be combined. */
203 const char *disable_all_option; /**< Name of the catch-all radio option that has
204 to be checked or unchecked for this filter type. */
205 const char *disable_all_param; /**< Name of the parameter that causes all filters of
206 this type to be disabled. */
207 const char *abbr_type; /**< Abbreviation of the filter type, usually the
208 first or second character capitalized */
209 const char *anchor; /**< Anchor for the User Manual link,
210 for example "SERVER-HEADER-FILTER" */
213 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
214 static const struct filter_type_info filter_type_info[] =
218 "content-filter-params", "filter",
219 "filter-all", "filter_all",
223 ACTION_MULTI_CLIENT_HEADER_FILTER,
224 "client-header-filter-params", "client-header-filter",
225 "client-header-filter-all", "client_header_filter_all",
226 "C", "CLIENT-HEADER-FILTER"
229 ACTION_MULTI_SERVER_HEADER_FILTER,
230 "server-header-filter-params", "server-header-filter",
231 "server-header-filter-all", "server_header_filter_all",
232 "S", "SERVER-HEADER-FILTER"
235 ACTION_MULTI_CLIENT_HEADER_TAGGER,
236 "client-header-tagger-params", "client-header-tagger",
237 "client-header-tagger-all", "client_header_tagger_all",
238 "L", "CLIENT-HEADER-TAGGER"
241 ACTION_MULTI_SERVER_HEADER_TAGGER,
242 "server-header-tagger-params", "server-header-tagger",
243 "server-header-tagger-all", "server_header_tagger_all",
244 "E", "SERVER-HEADER-TAGGER"
246 #ifdef FEATURE_EXTERNAL_FILTERS
248 ACTION_MULTI_EXTERNAL_FILTER,
249 "external-content-filter-params", "external-filter",
250 "external-content-filter-all", "external_content_filter_all",
251 "E", "EXTERNAL-CONTENT-FILTER"
256 /* FIXME: Following non-static functions should be prototyped in .h or made static */
258 /* Functions to read and write arbitrary config files */
259 jb_err edit_read_file(struct client_state *csp,
260 const struct map *parameters,
262 struct editable_file **pfile);
263 jb_err edit_write_file(struct editable_file * file);
264 void edit_free_file(struct editable_file * file);
266 /* Functions to read and write actions files */
267 jb_err edit_parse_actions_file(struct editable_file * file);
268 jb_err edit_read_actions_file(struct client_state *csp,
269 struct http_response *rsp,
270 const struct map *parameters,
272 struct editable_file **pfile);
275 jb_err cgi_error_modified(struct client_state *csp,
276 struct http_response *rsp,
277 const char *filename);
278 jb_err cgi_error_parse(struct client_state *csp,
279 struct http_response *rsp,
280 struct editable_file *file);
281 jb_err cgi_error_file(struct client_state *csp,
282 struct http_response *rsp,
283 const char *filename);
284 jb_err cgi_error_file_read_only(struct client_state *csp,
285 struct http_response *rsp,
286 const char *filename);
288 /* Internal arbitrary config file support functions */
289 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
290 static void edit_free_file_lines(struct file_line * first_line);
292 /* Internal actions file support functions */
293 static int match_actions_file_header_line(const char * line, const char * name);
294 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
296 /* Internal parameter parsing functions */
297 static jb_err get_url_spec_param(struct client_state *csp,
298 const struct map *parameters,
303 /* Internal actionsfile <==> HTML conversion functions */
304 static jb_err map_radio(struct map * exports,
305 const char * optionname,
308 static jb_err actions_to_radio(struct map * exports,
309 const struct action_spec *action);
310 static jb_err actions_from_radio(const struct map * parameters,
311 struct action_spec *action);
314 static jb_err map_copy_parameter_html(struct map *out,
315 const struct map *in,
318 static jb_err get_file_name_param(struct client_state *csp,
319 const struct map *parameters,
320 const char *param_name,
321 const char **pfilename);
323 /* Internal convenience functions */
324 static char *section_target(const unsigned sectionid);
326 /*********************************************************************
328 * Function : section_target
330 * Description : Given an unsigned (section id) n, produce a dynamically
331 * allocated string of the form #l<n>, for use in link
334 * XXX: The hash should be moved into the templates
335 * to make this function more generic and render
336 * stringify() obsolete.
339 * 1 : sectionid = start line number of section
341 * Returns : String with link target, or NULL if out of
344 *********************************************************************/
345 static char *section_target(const unsigned sectionid)
349 snprintf(buf, sizeof(buf), "#l%u", sectionid);
355 /*********************************************************************
357 * Function : stringify
359 * Description : Convert a number into a dynamically allocated string.
362 * 1 : number = The number to convert.
364 * Returns : String with link target, or NULL if out of memory
366 *********************************************************************/
367 static char *stringify(const unsigned number)
371 snprintf(buf, sizeof(buf), "%u", number);
376 /*********************************************************************
378 * Function : map_copy_parameter_html
380 * Description : Copy a CGI parameter from one map to another, HTML
384 * 1 : out = target map
385 * 2 : in = source map
386 * 3 : name = name of cgi parameter to copy
388 * Returns : JB_ERR_OK on success
389 * JB_ERR_MEMORY on out-of-memory
390 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
393 *********************************************************************/
394 static jb_err map_copy_parameter_html(struct map *out,
395 const struct map *in,
405 value = lookup(in, name);
406 err = map(out, name, 1, html_encode(value), 0);
413 else if (*value == '\0')
415 return JB_ERR_CGI_PARAMS;
424 /*********************************************************************
426 * Function : cgi_edit_actions_url_form
428 * Description : CGI function that displays a form for
432 * 1 : csp = Current client state (buffers, headers, etc...)
433 * 2 : rsp = http_response data structure for output
434 * 3 : parameters = map of cgi parameters
437 * i : (action index) Identifies the file to edit
438 * v : (version) File's last-modified time
439 * p : (pattern) Line number of pattern to edit
441 * Returns : JB_ERR_OK on success
442 * JB_ERR_MEMORY on out-of-memory
443 * JB_ERR_CGI_PARAMS if the CGI parameters are not
444 * specified or not valid.
446 *********************************************************************/
447 jb_err cgi_edit_actions_url_form(struct client_state *csp,
448 struct http_response *rsp,
449 const struct map *parameters)
451 struct map * exports;
453 struct editable_file * file;
454 struct file_line * cur_line;
455 unsigned line_number;
456 unsigned section_start_line_number = 0;
463 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
465 return cgi_error_disabled(csp, rsp);
468 err = get_number_param(csp, parameters, "p", &patternid);
474 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
477 /* No filename specified, can't read file, modified, or out of memory. */
478 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
481 cur_line = file->lines;
483 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
485 if (cur_line->type == FILE_LINE_ACTION)
487 section_start_line_number = line_number;
489 cur_line = cur_line->next;
492 if ( (cur_line == NULL)
493 || (line_number != patternid)
495 || (cur_line->type != FILE_LINE_URL))
497 /* Invalid "patternid" parameter */
498 edit_free_file(file);
499 return JB_ERR_CGI_PARAMS;
502 if (NULL == (exports = default_exports(csp, NULL)))
504 edit_free_file(file);
505 return JB_ERR_MEMORY;
508 err = map(exports, "f", 1, stringify(file->identifier), 0);
509 if (!err) err = map(exports, "v", 1, file->version_str, 1);
510 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
511 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
512 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
514 edit_free_file(file);
522 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
526 /*********************************************************************
528 * Function : cgi_edit_actions_add_url_form
530 * Description : CGI function that displays a form for
534 * 1 : csp = Current client state (buffers, headers, etc...)
535 * 2 : rsp = http_response data structure for output
536 * 3 : parameters = map of cgi parameters
539 * f : (filename) Identifies the file to edit
540 * v : (version) File's last-modified time
541 * s : (section) Line number of section to edit
543 * Returns : JB_ERR_OK on success
544 * JB_ERR_MEMORY on out-of-memory
545 * JB_ERR_CGI_PARAMS if the CGI parameters are not
546 * specified or not valid.
548 *********************************************************************/
549 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
550 struct http_response *rsp,
551 const struct map *parameters)
560 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
562 return cgi_error_disabled(csp, rsp);
565 if (NULL == (exports = default_exports(csp, NULL)))
567 return JB_ERR_MEMORY;
570 err = map_copy_parameter_html(exports, parameters, "f");
571 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
572 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
580 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
584 /*********************************************************************
586 * Function : cgi_edit_actions_remove_url_form
588 * Description : CGI function that displays a form for
592 * 1 : csp = Current client state (buffers, headers, etc...)
593 * 2 : rsp = http_response data structure for output
594 * 3 : parameters = map of cgi parameters
597 * f : (number) The action file identifier.
598 * v : (version) File's last-modified time
599 * p : (pattern) Line number of pattern to edit
601 * Returns : JB_ERR_OK on success
602 * JB_ERR_MEMORY on out-of-memory
603 * JB_ERR_CGI_PARAMS if the CGI parameters are not
604 * specified or not valid.
606 *********************************************************************/
607 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
608 struct http_response *rsp,
609 const struct map *parameters)
611 struct map * exports;
613 struct editable_file * file;
614 struct file_line * cur_line;
615 unsigned line_number;
616 unsigned section_start_line_number = 0;
623 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
625 return cgi_error_disabled(csp, rsp);
628 err = get_number_param(csp, parameters, "p", &patternid);
634 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
637 /* No filename specified, can't read file, modified, or out of memory. */
638 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
641 cur_line = file->lines;
643 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
645 if (cur_line->type == FILE_LINE_ACTION)
647 section_start_line_number = line_number;
649 cur_line = cur_line->next;
652 if ( (cur_line == NULL)
653 || (line_number != patternid)
655 || (cur_line->type != FILE_LINE_URL))
657 /* Invalid "patternid" parameter */
658 edit_free_file(file);
659 return JB_ERR_CGI_PARAMS;
662 if (NULL == (exports = default_exports(csp, NULL)))
664 edit_free_file(file);
665 return JB_ERR_MEMORY;
668 err = map(exports, "f", 1, stringify(file->identifier), 0);
669 if (!err) err = map(exports, "v", 1, file->version_str, 1);
670 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
671 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
672 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
673 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
675 edit_free_file(file);
683 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
687 /*********************************************************************
689 * Function : edit_write_file
691 * Description : Write a complete file to disk.
694 * 1 : file = File to write.
696 * Returns : JB_ERR_OK on success
697 * JB_ERR_FILE on error writing to file.
698 * JB_ERR_MEMORY on out of memory
700 *********************************************************************/
701 jb_err edit_write_file(struct editable_file * file)
704 struct file_line * cur_line;
705 struct stat statbuf[1];
706 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
707 digits in time_t, assuming this is a 64-bit
708 machine, plus null terminator, plus one
712 assert(file->filename);
714 if (NULL == (fp = fopen(file->filename, "wb")))
719 cur_line = file->lines;
720 while (cur_line != NULL)
724 if (fputs(cur_line->raw, fp) < 0)
732 if (cur_line->prefix)
734 if (fputs(cur_line->prefix, fp) < 0)
740 if (cur_line->unprocessed)
743 if (NULL != strchr(cur_line->unprocessed, '#'))
745 /* Must quote '#' characters */
752 /* Count number of # characters, so we know length of output string */
753 src = cur_line->unprocessed;
754 while (NULL != (src = strchr(src, '#')))
761 /* Allocate new memory for string */
762 len = strlen(cur_line->unprocessed) + (size_t)numhash;
763 str = malloc_or_die(len + 1);
765 /* Copy string but quote hashes */
766 src = cur_line->unprocessed;
774 assert(numhash >= 0);
780 assert(numhash == 0);
781 assert(strlen(str) == len);
782 assert(str == dest - len);
783 assert(src - len <= cur_line->unprocessed);
785 if ((strlen(str) != len) || (numhash != 0))
788 * Escaping didn't work as expected, go spread the news.
789 * Only reached in non-debugging builds.
791 log_error(LOG_LEVEL_ERROR,
792 "Looks like hash escaping failed. %s might be corrupted now.",
796 if (fputs(str, fp) < 0)
807 /* Can write without quoting '#' characters. */
808 if (fputs(cur_line->unprocessed, fp) < 0)
814 if (fputs(NEWLINE(file->newline), fp) < 0)
822 /* FIXME: Write data from file->data->whatever */
826 cur_line = cur_line->next;
832 /* Update the version stamp in the file structure, since we just
833 * wrote to the file & changed it's date.
835 if (stat(file->filename, statbuf) < 0)
837 /* Error, probably file not found. */
840 file->version = (unsigned)statbuf->st_mtime;
842 /* Correct file->version_str */
843 freez(file->version_str);
844 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
845 version_buf[sizeof(version_buf)-1] = '\0';
846 file->version_str = strdup(version_buf);
847 if (version_buf == NULL)
849 return JB_ERR_MEMORY;
856 /*********************************************************************
858 * Function : edit_free_file
860 * Description : Free a complete file in memory.
863 * 1 : file = Data structure to free.
867 *********************************************************************/
868 void edit_free_file(struct editable_file * file)
872 /* Silently ignore NULL pointer */
876 edit_free_file_lines(file->lines);
877 freez(file->version_str);
879 file->parse_error_text = NULL; /* Statically allocated */
880 file->parse_error = NULL;
886 /*********************************************************************
888 * Function : edit_free_file_lines
890 * Description : Free an entire linked list of file lines.
893 * 1 : first_line = Data structure to free.
897 *********************************************************************/
898 static void edit_free_file_lines(struct file_line * first_line)
900 struct file_line * next_line;
902 while (first_line != NULL)
904 next_line = first_line->next;
905 first_line->next = NULL;
906 freez(first_line->raw);
907 freez(first_line->prefix);
908 freez(first_line->unprocessed);
909 switch(first_line->type)
911 case 0: /* special case if memory zeroed */
912 case FILE_LINE_UNPROCESSED:
913 case FILE_LINE_BLANK:
914 case FILE_LINE_ALIAS_HEADER:
915 case FILE_LINE_SETTINGS_HEADER:
916 case FILE_LINE_DESCRIPTION_HEADER:
917 case FILE_LINE_DESCRIPTION_ENTRY:
918 case FILE_LINE_ALIAS_ENTRY:
920 /* No data is stored for these */
923 case FILE_LINE_ACTION:
924 free_action(first_line->data.action);
927 case FILE_LINE_SETTINGS_ENTRY:
928 freez(first_line->data.setting.name);
929 freez(first_line->data.setting.svalue);
932 /* Should never happen */
936 first_line->type = 0; /* paranoia */
938 first_line = next_line;
943 /*********************************************************************
945 * Function : match_actions_file_header_line
947 * Description : Match an actions file {{header}} line
950 * 1 : line = String from file
951 * 2 : name = Header to match against
953 * Returns : 0 iff they match.
955 *********************************************************************/
956 static int match_actions_file_header_line(const char * line, const char * name)
964 if ((line[0] != '{') || (line[1] != '{'))
970 /* Look for optional whitespace */
971 while ((*line == ' ') || (*line == '\t'))
976 /* Look for the specified name (case-insensitive) */
978 if (0 != strncmpic(line, name, len))
984 /* Look for optional whitespace */
985 while ((*line == ' ') || (*line == '\t'))
990 /* Look for "}}" and end of string*/
991 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1001 /*********************************************************************
1003 * Function : match_actions_file_header_line
1005 * Description : Match an actions file {{header}} line
1008 * 1 : line = String from file. Must not start with
1009 * whitespace (else infinite loop!)
1010 * 2 : pname = Destination for name
1011 * 2 : pvalue = Destination for value
1013 * Returns : JB_ERR_OK on success
1014 * JB_ERR_MEMORY on out-of-memory
1015 * JB_ERR_PARSE if there's no "=" sign, or if there's
1016 * nothing before the "=" sign (but empty
1017 * values *after* the "=" sign are legal).
1019 *********************************************************************/
1020 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1022 const char * name_end;
1023 const char * value_start;
1029 assert(*line != ' ');
1030 assert(*line != '\t');
1035 value_start = strchr(line, '=');
1036 if ((value_start == NULL) || (value_start == line))
1038 return JB_ERR_PARSE;
1041 name_end = value_start - 1;
1043 /* Eat any whitespace before the '=' */
1044 while ((*name_end == ' ') || (*name_end == '\t'))
1047 * we already know we must have at least 1 non-ws char
1048 * at start of buf - no need to check
1053 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1054 *pname = malloc_or_die(name_len + 1);
1055 strncpy(*pname, line, name_len);
1056 (*pname)[name_len] = '\0';
1058 /* Eat any the whitespace after the '=' */
1060 while ((*value_start == ' ') || (*value_start == '\t'))
1065 if (NULL == (*pvalue = strdup(value_start)))
1069 return JB_ERR_MEMORY;
1076 /*********************************************************************
1078 * Function : edit_parse_actions_file
1080 * Description : Parse an actions file in memory.
1082 * Passed linked list must have the "data" member
1083 * zeroed, and must contain valid "next" and
1084 * "unprocessed" fields. The "raw" and "prefix"
1085 * fields are ignored, and "type" is just overwritten.
1087 * Note that on error the file may have been
1091 * 1 : file = Actions file to be parsed in-place.
1093 * Returns : JB_ERR_OK on success
1094 * JB_ERR_MEMORY on out-of-memory
1095 * JB_ERR_PARSE on error
1097 *********************************************************************/
1098 jb_err edit_parse_actions_file(struct editable_file * file)
1100 struct file_line * cur_line;
1102 const char * text; /* Text from a line */
1103 char * name; /* For lines of the form name=value */
1104 char * value; /* For lines of the form name=value */
1105 struct action_alias * alias_list = NULL;
1106 jb_err err = JB_ERR_OK;
1108 /* alias_list contains the aliases defined in this file.
1109 * It might be better to use the "file_line.data" fields
1110 * in the relavent places instead.
1113 cur_line = file->lines;
1115 /* A note about blank line support: Blank lines should only
1116 * ever occur as the last line in the file. This function
1117 * is more forgiving than that - FILE_LINE_BLANK can occur
1121 /* Skip leading blanks. Should only happen if file is
1122 * empty (which is valid, but pointless).
1124 while ((cur_line != NULL)
1125 && (cur_line->unprocessed[0] == '\0'))
1128 cur_line->type = FILE_LINE_BLANK;
1129 cur_line = cur_line->next;
1132 if ((cur_line != NULL)
1133 && (cur_line->unprocessed[0] != '{'))
1135 /* File doesn't start with a header */
1136 file->parse_error = cur_line;
1137 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1138 return JB_ERR_PARSE;
1141 if ((cur_line != NULL) && (0 ==
1142 match_actions_file_header_line(cur_line->unprocessed, "settings")))
1144 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1146 cur_line = cur_line->next;
1147 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1149 if (cur_line->unprocessed[0])
1151 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1153 err = split_line_on_equals(cur_line->unprocessed,
1154 &cur_line->data.setting.name,
1155 &cur_line->data.setting.svalue);
1156 if (err == JB_ERR_MEMORY)
1160 else if (err != JB_ERR_OK)
1162 /* Line does not contain a name=value pair */
1163 file->parse_error = cur_line;
1164 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1165 return JB_ERR_PARSE;
1170 cur_line->type = FILE_LINE_BLANK;
1172 cur_line = cur_line->next;
1176 if ((cur_line != NULL) && (0 ==
1177 match_actions_file_header_line(cur_line->unprocessed, "description")))
1179 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1181 cur_line = cur_line->next;
1182 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1184 if (cur_line->unprocessed[0])
1186 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1190 cur_line->type = FILE_LINE_BLANK;
1192 cur_line = cur_line->next;
1196 if ((cur_line != NULL) && (0 ==
1197 match_actions_file_header_line(cur_line->unprocessed, "alias")))
1199 cur_line->type = FILE_LINE_ALIAS_HEADER;
1201 cur_line = cur_line->next;
1202 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1204 if (cur_line->unprocessed[0])
1206 /* define an alias */
1207 struct action_alias * new_alias;
1209 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1211 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1212 if (err == JB_ERR_MEMORY)
1214 free_alias_list(alias_list);
1217 else if (err != JB_ERR_OK)
1219 /* Line does not contain a name=value pair */
1220 file->parse_error = cur_line;
1221 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1222 free_alias_list(alias_list);
1223 return JB_ERR_PARSE;
1226 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1231 free_alias_list(alias_list);
1232 return JB_ERR_MEMORY;
1235 err = get_actions(value, alias_list, new_alias->action);
1238 /* Invalid action or out of memory */
1242 free_alias_list(alias_list);
1243 if (err == JB_ERR_MEMORY)
1249 /* Line does not contain a name=value pair */
1250 file->parse_error = cur_line;
1251 file->parse_error_text = "This alias does not specify a valid set of actions.";
1252 return JB_ERR_PARSE;
1258 new_alias->name = name;
1261 new_alias->next = alias_list;
1262 alias_list = new_alias;
1266 cur_line->type = FILE_LINE_BLANK;
1268 cur_line = cur_line->next;
1272 /* Header done, process the main part of the file */
1273 while (cur_line != NULL)
1275 /* At this point, (cur_line->unprocessed[0] == '{') */
1276 assert(cur_line->unprocessed[0] == '{');
1277 text = cur_line->unprocessed + 1;
1278 len = strlen(text) - 1;
1279 if (text[len] != '}')
1281 /* No closing } on header */
1282 free_alias_list(alias_list);
1283 file->parse_error = cur_line;
1284 file->parse_error_text = "Headers starting with '{' must have a "
1285 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1286 "must close with two brackets ('}}').";
1287 return JB_ERR_PARSE;
1292 /* An invalid {{ header. */
1293 free_alias_list(alias_list);
1294 file->parse_error = cur_line;
1295 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1296 "Please remember that the system (two-bracket) headers must "
1297 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1298 "and must appear before any actions (one-bracket) headers. "
1299 "Also note that system headers may not be repeated.";
1300 return JB_ERR_PARSE;
1303 while ((*text == ' ') || (*text == '\t'))
1308 while ((len > (size_t)0)
1309 && ((text[len - 1] == ' ')
1310 || (text[len - 1] == '\t')))
1315 cur_line->type = FILE_LINE_ACTION;
1317 /* Remove {} and make copy */
1318 value = malloc_or_die(len + 1);
1319 strncpy(value, text, len);
1323 err = get_actions(value, alias_list, cur_line->data.action);
1326 /* Invalid action or out of memory */
1328 free_alias_list(alias_list);
1329 if (err == JB_ERR_MEMORY)
1335 /* Line does not contain a name=value pair */
1336 file->parse_error = cur_line;
1337 file->parse_error_text = "This header does not specify a valid set of actions.";
1338 return JB_ERR_PARSE;
1342 /* Done with string - it was clobbered anyway */
1345 /* Process next line */
1346 cur_line = cur_line->next;
1348 /* Loop processing URL patterns */
1349 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1351 if (cur_line->unprocessed[0])
1353 /* Could parse URL here, but this isn't currently needed */
1355 cur_line->type = FILE_LINE_URL;
1359 cur_line->type = FILE_LINE_BLANK;
1361 cur_line = cur_line->next;
1363 } /* End main while(cur_line != NULL) loop */
1365 free_alias_list(alias_list);
1371 /*********************************************************************
1373 * Function : edit_read_file_lines
1375 * Description : Read all the lines of a file into memory.
1376 * Handles whitespace, comments and line continuation.
1379 * 1 : fp = File to read from. On return, this will be
1380 * at EOF but it will not have been closed.
1381 * 2 : pfile = Destination for a linked list of file_lines.
1382 * Will be set to NULL on error.
1383 * 3 : newline = How to handle newlines.
1385 * Returns : JB_ERR_OK on success
1386 * JB_ERR_MEMORY on out-of-memory
1388 *********************************************************************/
1389 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1391 struct file_line * first_line; /* Keep for return value or to free */
1392 struct file_line * cur_line; /* Current line */
1393 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1401 cur_line = first_line = zalloc(sizeof(struct file_line));
1402 if (cur_line == NULL)
1404 return JB_ERR_MEMORY;
1407 cur_line->type = FILE_LINE_UNPROCESSED;
1409 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1412 /* Out of memory or empty file. */
1413 /* Note that empty file is not an error we propagate up */
1415 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1420 prev_line = cur_line;
1421 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1422 if (cur_line == NULL)
1425 edit_free_file_lines(first_line);
1426 return JB_ERR_MEMORY;
1429 cur_line->type = FILE_LINE_UNPROCESSED;
1431 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1432 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1435 edit_free_file_lines(first_line);
1436 return JB_ERR_MEMORY;
1440 while (rval != JB_ERR_FILE);
1444 /* We allocated one too many - free it */
1445 prev_line->next = NULL;
1448 *pfile = first_line;
1453 /*********************************************************************
1455 * Function : edit_read_file
1457 * Description : Read a complete file into memory.
1458 * Handles CGI parameter parsing. If requested, also
1459 * checks the file's modification timestamp.
1462 * 1 : csp = Current client state (buffers, headers, etc...)
1463 * 2 : parameters = map of cgi parameters.
1464 * 3 : require_version = true to check "ver" parameter.
1465 * 4 : pfile = Destination for the file. Will be set
1469 * f : The action file identifier.
1470 * ver : (Only if require_version is nonzero)
1471 * Timestamp of the actions file. If wrong, this
1472 * function fails with JB_ERR_MODIFIED.
1474 * Returns : JB_ERR_OK on success
1475 * JB_ERR_MEMORY on out-of-memory
1476 * JB_ERR_CGI_PARAMS if "filename" was not specified
1478 * JB_ERR_FILE if the file cannot be opened or
1480 * JB_ERR_MODIFIED if version checking was requested and
1481 * failed - the file was modified outside
1482 * of this CGI editor instance.
1484 *********************************************************************/
1485 jb_err edit_read_file(struct client_state *csp,
1486 const struct map *parameters,
1487 int require_version,
1488 struct editable_file **pfile)
1490 struct file_line * lines;
1493 const char *filename = NULL;
1494 struct editable_file * file;
1495 unsigned version = 0;
1496 struct stat statbuf[1];
1497 char version_buf[22];
1498 int newline = NEWLINE_UNKNOWN;
1507 err = get_number_param(csp, parameters, "f", &i);
1508 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1510 filename = csp->config->actions_file[i];
1512 else if (JB_ERR_CGI_PARAMS == err)
1515 * Probably an old-school URL like
1516 * http://config.privoxy.org/edit-actions-list?f=default
1518 get_file_name_param(csp, parameters, "f", &filename);
1521 if (NULL == filename || stat(filename, statbuf) < 0)
1523 /* Error, probably file not found. */
1526 version = (unsigned) statbuf->st_mtime;
1528 if (require_version)
1530 unsigned specified_version;
1531 err = get_number_param(csp, parameters, "v", &specified_version);
1537 if (version != specified_version)
1539 return JB_ERR_MODIFIED;
1543 if (NULL == (fp = fopen(filename,"rb")))
1548 err = edit_read_file_lines(fp, &lines, &newline);
1557 file = (struct editable_file *) zalloc(sizeof(*file));
1560 edit_free_file_lines(lines);
1564 file->lines = lines;
1565 file->newline = newline;
1566 file->filename = filename;
1567 file->version = version;
1568 file->identifier = i;
1570 /* Correct file->version_str */
1571 freez(file->version_str);
1572 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1573 version_buf[sizeof(version_buf)-1] = '\0';
1574 file->version_str = strdup(version_buf);
1575 if (version_buf == NULL)
1577 edit_free_file(file);
1578 return JB_ERR_MEMORY;
1586 /*********************************************************************
1588 * Function : edit_read_actions_file
1590 * Description : Read a complete actions file into memory.
1591 * Handles CGI parameter parsing. If requested, also
1592 * checks the file's modification timestamp.
1594 * If this function detects an error in the categories
1595 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1596 * then it handles it by filling in the specified
1597 * response structure and returning JB_ERR_FILE.
1600 * 1 : csp = Current client state (buffers, headers, etc...)
1601 * 2 : rsp = HTTP response. Only filled in on error.
1602 * 2 : parameters = map of cgi parameters.
1603 * 3 : require_version = true to check "ver" parameter.
1604 * 4 : pfile = Destination for the file. Will be set
1608 * f : The actions file identifier.
1609 * ver : (Only if require_version is nonzero)
1610 * Timestamp of the actions file. If wrong, this
1611 * function fails with JB_ERR_MODIFIED.
1613 * Returns : JB_ERR_OK on success
1614 * JB_ERR_MEMORY on out-of-memory
1615 * JB_ERR_CGI_PARAMS if "filename" was not specified
1617 * JB_ERR_FILE if the file does not contain valid data,
1618 * or if file cannot be opened or
1619 * contains no data, or if version
1620 * checking was requested and failed.
1622 *********************************************************************/
1623 jb_err edit_read_actions_file(struct client_state *csp,
1624 struct http_response *rsp,
1625 const struct map *parameters,
1626 int require_version,
1627 struct editable_file **pfile)
1630 struct editable_file *file;
1631 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
1639 err = edit_read_file(csp, parameters, require_version, &file);
1642 /* Try to handle if possible */
1643 if (err == JB_ERR_FILE)
1645 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1647 else if (err == JB_ERR_MODIFIED)
1649 assert(require_version);
1650 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1651 log_error(LOG_LEVEL_ERROR,
1652 "Blocking CGI edit request due to modification time mismatch.");
1653 if (acceptable_failures > 0)
1655 log_error(LOG_LEVEL_INFO,
1656 "The CGI editor will be turned off after another %d mismatche(s).",
1657 acceptable_failures);
1658 acceptable_failures--;
1662 log_error(LOG_LEVEL_INFO,
1663 "Timestamp mismatch limit reached, turning CGI editor off. "
1664 "Reload the configuration file to re-enable it.");
1665 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
1668 if (err == JB_ERR_OK)
1671 * Signal to higher-level CGI code that there was a problem but we
1672 * handled it, they should just return JB_ERR_OK.
1679 err = edit_parse_actions_file(file);
1682 if (err == JB_ERR_PARSE)
1684 err = cgi_error_parse(csp, rsp, file);
1685 if (err == JB_ERR_OK)
1688 * Signal to higher-level CGI code that there was a problem but we
1689 * handled it, they should just return JB_ERR_OK.
1694 edit_free_file(file);
1703 /*********************************************************************
1705 * Function : get_file_name_param
1707 * Description : Get the name of the file to edit from the parameters
1708 * passed to a CGI function using the old syntax.
1709 * This function handles security checks and only
1710 * accepts files that Privoxy already knows.
1713 * 1 : csp = Current client state (buffers, headers, etc...)
1714 * 2 : parameters = map of cgi parameters
1715 * 3 : param_name = The name of the parameter to read
1716 * 4 : pfilename = pointer to the filename in
1717 * csp->config->actions_file[] if found. Set to NULL on error.
1719 * Returns : JB_ERR_OK on success
1720 * JB_ERR_MEMORY on out-of-memory
1721 * JB_ERR_CGI_PARAMS if "filename" was not specified
1724 *********************************************************************/
1725 static jb_err get_file_name_param(struct client_state *csp,
1726 const struct map *parameters,
1727 const char *param_name,
1728 const char **pfilename)
1731 const char suffix[] = ".action";
1746 param = lookup(parameters, param_name);
1749 return JB_ERR_CGI_PARAMS;
1752 len = strlen(param);
1753 if (len >= FILENAME_MAX)
1756 return JB_ERR_CGI_PARAMS;
1760 * Check every character to see if it's legal.
1761 * Totally unnecessary but we do it anyway.
1764 while ((ch = *s++) != '\0')
1766 if ( ((ch < 'A') || (ch > 'Z'))
1767 && ((ch < 'a') || (ch > 'z'))
1768 && ((ch < '0') || (ch > '9'))
1772 /* Probable hack attempt. */
1773 return JB_ERR_CGI_PARAMS;
1777 /* Append extension */
1778 name_size = len + strlen(suffix) + 1;
1779 name = malloc_or_die(name_size);
1780 strlcpy(name, param, name_size);
1781 strlcat(name, suffix, name_size);
1784 fullpath = make_path(csp->config->confdir, name);
1787 if (fullpath == NULL)
1789 return JB_ERR_MEMORY;
1792 /* Check if the file is known */
1793 for (i = 0; i < MAX_AF_FILES; i++)
1795 if (NULL != csp->config->actions_file[i] &&
1796 !strcmp(fullpath, csp->config->actions_file[i]))
1799 *pfilename = csp->config->actions_file[i];
1807 return JB_ERR_CGI_PARAMS;
1811 /*********************************************************************
1813 * Function : get_url_spec_param
1815 * Description : Get a URL pattern from the parameters
1816 * passed to a CGI function. Removes leading/trailing
1817 * spaces and validates it.
1820 * 1 : csp = Current client state (buffers, headers, etc...)
1821 * 2 : parameters = map of cgi parameters
1822 * 3 : name = Name of CGI parameter to read
1823 * 4 : pvalue = destination for value. Will be malloc()'d.
1824 * Set to NULL on error.
1826 * Returns : JB_ERR_OK on success
1827 * JB_ERR_MEMORY on out-of-memory
1828 * JB_ERR_CGI_PARAMS if the parameter was not specified
1831 *********************************************************************/
1832 static jb_err get_url_spec_param(struct client_state *csp,
1833 const struct map *parameters,
1837 const char *orig_param;
1840 struct pattern_spec compiled[1];
1850 orig_param = lookup(parameters, name);
1853 return JB_ERR_CGI_PARAMS;
1856 /* Copy and trim whitespace */
1857 param = strdup(orig_param);
1860 return JB_ERR_MEMORY;
1864 /* Must be non-empty, and can't allow 1st character to be '{' */
1865 if (param[0] == '\0' || param[0] == '{')
1868 return JB_ERR_CGI_PARAMS;
1871 /* Check for embedded newlines */
1872 for (s = param; *s != '\0'; s++)
1874 if ((*s == '\r') || (*s == '\n'))
1877 return JB_ERR_CGI_PARAMS;
1881 /* Check that regex is valid */
1886 return JB_ERR_MEMORY;
1888 err = create_pattern_spec(compiled, s);
1893 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1895 free_pattern_spec(compiled);
1897 if (param[strlen(param) - 1] == '\\')
1900 * Must protect trailing '\\' from becoming line continuation character.
1901 * Two methods: 1) If it's a domain only, add a trailing '/'.
1902 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1904 if (strchr(param, '/') == NULL)
1906 err = string_append(¶m, "/");
1910 err = string_append(¶m, "(?:)");
1917 /* Check that the modified regex is valid */
1922 return JB_ERR_MEMORY;
1924 err = create_pattern_spec(compiled, s);
1929 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1931 free_pattern_spec(compiled);
1938 /*********************************************************************
1940 * Function : map_radio
1942 * Description : Map a set of radio button values. E.g. if you have
1943 * 3 radio buttons, declare them as:
1944 * <option type="radio" name="xyz" @xyz-a@>
1945 * <option type="radio" name="xyz" @xyz-b@>
1946 * <option type="radio" name="xyz" @xyz-c@>
1947 * Then map one of the @xyz-?@ variables to "checked"
1948 * and all the others to empty by calling:
1949 * map_radio(exports, "xyz", "abc", sel)
1950 * Where 'sel' is 'a', 'b', or 'c'.
1953 * 1 : exports = Exports map to modify.
1954 * 2 : optionname = name for map
1955 * 3 : values = null-terminated list of values;
1956 * 4 : value = Selected value.
1958 * CGI Parameters : None
1960 * Returns : JB_ERR_OK on success
1961 * JB_ERR_MEMORY on out-of-memory
1963 *********************************************************************/
1964 static jb_err map_radio(struct map * exports,
1965 const char * optionname,
1966 const char * values,
1972 const size_t len = strlen(optionname);
1973 const size_t buf_size = len + 3;
1979 buf = malloc_or_die(buf_size);
1981 strlcpy(buf, optionname, buf_size);
1983 /* XXX: this looks ... interesting */
1988 while ((c = *values++) != '\0')
1993 if (map(exports, buf, 1, "", 1))
1995 return JB_ERR_MEMORY;
2001 return map(exports, buf, 0, "checked", 1);
2005 /*********************************************************************
2007 * Function : cgi_error_modified
2009 * Description : CGI function that is called when a file is modified
2010 * outside the CGI editor.
2013 * 1 : csp = Current client state (buffers, headers, etc...)
2014 * 2 : rsp = http_response data structure for output
2015 * 3 : filename = The file that was modified.
2017 * CGI Parameters : none
2019 * Returns : JB_ERR_OK on success
2020 * JB_ERR_MEMORY on out-of-memory error.
2022 *********************************************************************/
2023 jb_err cgi_error_modified(struct client_state *csp,
2024 struct http_response *rsp,
2025 const char *filename)
2027 struct map *exports;
2034 if (NULL == (exports = default_exports(csp, NULL)))
2036 return JB_ERR_MEMORY;
2039 err = map(exports, "f", 1, html_encode(filename), 0);
2046 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2050 /*********************************************************************
2052 * Function : cgi_error_parse
2054 * Description : CGI function that is called when a file cannot
2055 * be parsed by the CGI editor.
2058 * 1 : csp = Current client state (buffers, headers, etc...)
2059 * 2 : rsp = http_response data structure for output
2060 * 3 : file = The file that was modified.
2062 * CGI Parameters : none
2064 * Returns : JB_ERR_OK on success
2065 * JB_ERR_MEMORY on out-of-memory error.
2067 *********************************************************************/
2068 jb_err cgi_error_parse(struct client_state *csp,
2069 struct http_response *rsp,
2070 struct editable_file *file)
2072 struct map *exports;
2074 struct file_line *cur_line;
2080 if (NULL == (exports = default_exports(csp, NULL)))
2082 return JB_ERR_MEMORY;
2085 err = map(exports, "f", 1, stringify(file->identifier), 0);
2086 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2088 cur_line = file->parse_error;
2091 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2092 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2100 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2104 /*********************************************************************
2106 * Function : cgi_error_file
2108 * Description : CGI function that is called when a file cannot be
2109 * opened by the CGI editor.
2112 * 1 : csp = Current client state (buffers, headers, etc...)
2113 * 2 : rsp = http_response data structure for output
2114 * 3 : filename = The file that was modified.
2116 * CGI Parameters : none
2118 * Returns : JB_ERR_OK on success
2119 * JB_ERR_MEMORY on out-of-memory error.
2121 *********************************************************************/
2122 jb_err cgi_error_file(struct client_state *csp,
2123 struct http_response *rsp,
2124 const char *filename)
2126 struct map *exports;
2133 if (NULL == (exports = default_exports(csp, NULL)))
2135 return JB_ERR_MEMORY;
2138 err = map(exports, "f", 1, html_encode(filename), 0);
2145 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2149 /*********************************************************************
2151 * Function : cgi_error_file_read_only
2153 * Description : CGI function that is called when a file cannot be
2154 * opened for writing by the CGI editor.
2157 * 1 : csp = Current client state (buffers, headers, etc...)
2158 * 2 : rsp = http_response data structure for output
2159 * 3 : filename = The file that we can't write to
2161 * CGI Parameters : none
2163 * Returns : JB_ERR_OK on success
2164 * JB_ERR_MEMORY on out-of-memory error.
2166 *********************************************************************/
2167 jb_err cgi_error_file_read_only(struct client_state *csp,
2168 struct http_response *rsp,
2169 const char *filename)
2171 struct map *exports;
2178 if (NULL == (exports = default_exports(csp, NULL)))
2180 return JB_ERR_MEMORY;
2183 err = map(exports, "f", 1, html_encode(filename), 0);
2190 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2194 /*********************************************************************
2196 * Function : cgi_edit_actions
2198 * Description : CGI function that allows the user to choose which
2199 * actions file to edit.
2202 * 1 : csp = Current client state (buffers, headers, etc...)
2203 * 2 : rsp = http_response data structure for output
2204 * 3 : parameters = map of cgi parameters
2206 * CGI Parameters : None
2208 * Returns : JB_ERR_OK on success
2209 * JB_ERR_MEMORY on out-of-memory error
2211 *********************************************************************/
2212 jb_err cgi_edit_actions(struct client_state *csp,
2213 struct http_response *rsp,
2214 const struct map *parameters)
2218 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2220 return cgi_error_disabled(csp, rsp);
2223 /* FIXME: Incomplete */
2225 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2230 /*********************************************************************
2232 * Function : cgi_edit_actions_list
2234 * Description : CGI function that edits the actions list.
2235 * FIXME: This function shouldn't FATAL ever.
2236 * FIXME: This function doesn't check the retval of map()
2238 * 1 : csp = Current client state (buffers, headers, etc...)
2239 * 2 : rsp = http_response data structure for output
2240 * 3 : parameters = map of cgi parameters
2242 * CGI Parameters : filename
2244 * Returns : JB_ERR_OK on success
2245 * JB_ERR_MEMORY on out-of-memory
2246 * JB_ERR_FILE if the file cannot be opened or
2248 * JB_ERR_CGI_PARAMS if "filename" was not specified
2251 *********************************************************************/
2252 jb_err cgi_edit_actions_list(struct client_state *csp,
2253 struct http_response *rsp,
2254 const struct map *parameters)
2256 char * section_template;
2257 char * url_template;
2262 struct map * exports;
2263 struct map * section_exports;
2264 struct map * url_exports;
2265 struct editable_file * file;
2266 struct file_line * cur_line;
2267 unsigned line_number = 0;
2268 unsigned prev_section_line_number = ((unsigned) (-1));
2270 struct file_list * fl;
2271 struct url_actions * b;
2272 char * buttons = NULL;
2275 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2277 return cgi_error_disabled(csp, rsp);
2280 if (NULL == (exports = default_exports(csp, NULL)))
2282 return JB_ERR_MEMORY;
2285 /* Load actions file */
2286 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2289 /* No filename specified, can't read file, or out of memory. */
2291 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2294 /* Find start of actions in file */
2295 cur_line = file->lines;
2297 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2299 cur_line = cur_line->next;
2304 * Conventional actions files should have a match all block
2306 * cur_line = {...global actions...}
2307 * cur_line->next = /
2308 * cur_line->next->next = {...actions...} or EOF
2310 if ( (cur_line != NULL)
2311 && (cur_line->type == FILE_LINE_ACTION)
2312 && (cur_line->next != NULL)
2313 && (cur_line->next->type == FILE_LINE_URL)
2314 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2315 && ( (cur_line->next->next == NULL)
2316 || (cur_line->next->next->type != FILE_LINE_URL)
2320 * Generate string with buttons to set actions for "/" to
2321 * any predefined set of actions (named standard.*, probably
2322 * residing in standard.action).
2325 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2328 edit_free_file(file);
2330 if (err == JB_ERR_FILE)
2332 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2337 err = template_fill(§ion_template, exports);
2340 edit_free_file(file);
2345 buttons = strdup("");
2346 for (i = 0; i < MAX_AF_FILES; i++)
2348 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2350 for (b = b->next; NULL != b; b = b->next)
2352 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2354 if (err || (NULL == (section_exports = new_map())))
2357 free(section_template);
2358 edit_free_file(file);
2360 return JB_ERR_MEMORY;
2363 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2365 if (err || (NULL == (s = strdup(section_template))))
2367 free_map(section_exports);
2369 free(section_template);
2370 edit_free_file(file);
2372 return JB_ERR_MEMORY;
2375 if (!err) err = template_fill(&s, section_exports);
2376 free_map(section_exports);
2377 if (!err) err = string_join(&buttons, s);
2382 freez(section_template);
2383 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2386 * Conventional actions file, supply extra editing help.
2387 * (e.g. don't allow them to make it an unconventional one).
2389 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2391 snprintf(buf, sizeof(buf), "%u", line_number);
2392 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2393 snprintf(buf, sizeof(buf), "%u", line_number + 2);
2394 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2395 if (!err) err = map(exports, "all-urls-actions", 1,
2396 actions_to_html(csp, cur_line->data.action), 0);
2398 /* Skip the 2 lines */
2399 cur_line = cur_line->next->next;
2403 * Note that prev_section_line_number is NOT set here.
2404 * This is deliberate and not a bug. It stops a "Move up"
2405 * option appearing on the next section. Clicking "Move
2406 * up" would make the actions file unconventional, which
2407 * we don't want, so we hide this option.
2413 * Non-standard actions file - does not begin with
2414 * the "All URLs" section.
2416 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2419 /* Set up global exports */
2421 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2422 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2423 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2425 /* Discourage private additions to default.action */
2427 if (!err) err = map_conditional(exports, "default-action",
2428 (strstr("default.action", file->filename) != NULL));
2431 edit_free_file(file);
2436 /* Should do all global exports above this point */
2438 /* Load templates */
2440 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2443 edit_free_file(file);
2445 if (err == JB_ERR_FILE)
2447 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2452 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2455 free(section_template);
2456 edit_free_file(file);
2458 if (err == JB_ERR_FILE)
2460 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2465 err = template_fill(§ion_template, exports);
2469 edit_free_file(file);
2474 err = template_fill(&url_template, exports);
2477 free(section_template);
2478 edit_free_file(file);
2483 if (NULL == (sections = strdup("")))
2485 free(section_template);
2487 edit_free_file(file);
2489 return JB_ERR_MEMORY;
2492 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2494 if (NULL == (section_exports = new_map()))
2497 free(section_template);
2499 edit_free_file(file);
2501 return JB_ERR_MEMORY;
2504 snprintf(buf, sizeof(buf), "%u", line_number);
2505 err = map(section_exports, "s", 1, buf, 1);
2506 if (!err) err = map(section_exports, "actions", 1,
2507 actions_to_html(csp, cur_line->data.action), 0);
2510 && (cur_line->next != NULL)
2511 && (cur_line->next->type == FILE_LINE_URL))
2513 /* This section contains at least one URL, don't allow delete */
2514 err = map_block_killer(section_exports, "empty-section");
2518 if (!err) err = map_block_keep(section_exports, "empty-section");
2521 if (prev_section_line_number != ((unsigned)(-1)))
2523 /* Not last section */
2524 snprintf(buf, sizeof(buf), "%u", prev_section_line_number);
2525 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2526 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2531 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2533 prev_section_line_number = line_number;
2538 free(section_template);
2540 edit_free_file(file);
2542 free_map(section_exports);
2546 /* Should do all section-specific exports above this point */
2548 if (NULL == (urls = strdup("")))
2551 free(section_template);
2553 edit_free_file(file);
2555 free_map(section_exports);
2556 return JB_ERR_MEMORY;
2561 cur_line = cur_line->next;
2564 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2566 if (NULL == (url_exports = new_map()))
2570 free(section_template);
2572 edit_free_file(file);
2574 free_map(section_exports);
2575 return JB_ERR_MEMORY;
2578 snprintf(buf, sizeof(buf), "%u", line_number);
2579 err = map(url_exports, "p", 1, buf, 1);
2581 snprintf(buf, sizeof(buf), "%d", url_1_2);
2582 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2584 if (!err) err = map(url_exports, "url-html", 1,
2585 html_encode(cur_line->unprocessed), 0);
2586 if (!err) err = map(url_exports, "url", 1,
2587 url_encode(cur_line->unprocessed), 0);
2593 free(section_template);
2595 edit_free_file(file);
2597 free_map(section_exports);
2598 free_map(url_exports);
2602 if (NULL == (s = strdup(url_template)))
2606 free(section_template);
2608 edit_free_file(file);
2610 free_map(section_exports);
2611 free_map(url_exports);
2612 return JB_ERR_MEMORY;
2615 err = template_fill(&s, section_exports);
2616 if (!err) err = template_fill(&s, url_exports);
2617 if (!err) err = string_append(&urls, s);
2619 free_map(url_exports);
2626 free(section_template);
2628 edit_free_file(file);
2630 free_map(section_exports);
2634 url_1_2 = 3 - url_1_2;
2636 cur_line = cur_line->next;
2640 err = map(section_exports, "urls", 1, urls, 0);
2642 /* Could also do section-specific exports here, but it wouldn't be as fast */
2644 snprintf(buf, sizeof(buf), "%u", line_number);
2645 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2647 if ((cur_line != NULL)
2648 && (cur_line->type == FILE_LINE_ACTION))
2650 /* Not last section */
2651 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2656 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2662 free(section_template);
2664 edit_free_file(file);
2666 free_map(section_exports);
2670 if (NULL == (s = strdup(section_template)))
2673 free(section_template);
2675 edit_free_file(file);
2677 free_map(section_exports);
2678 return JB_ERR_MEMORY;
2681 err = template_fill(&s, section_exports);
2682 if (!err) err = string_append(§ions, s);
2685 free_map(section_exports);
2690 free(section_template);
2692 edit_free_file(file);
2698 edit_free_file(file);
2699 free(section_template);
2702 err = map(exports, "sections", 1, sections, 0);
2709 /* Could also do global exports here, but it wouldn't be as fast */
2711 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2715 /*********************************************************************
2717 * Function : cgi_edit_actions_for_url
2719 * Description : CGI function that edits the Actions list.
2722 * 1 : csp = Current client state (buffers, headers, etc...)
2723 * 2 : rsp = http_response data structure for output
2724 * 3 : parameters = map of cgi parameters
2726 * CGI Parameters : None
2728 * Returns : JB_ERR_OK on success
2729 * JB_ERR_MEMORY on out-of-memory
2730 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2731 * specified or not valid.
2733 *********************************************************************/
2734 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2735 struct http_response *rsp,
2736 const struct map *parameters)
2738 struct map * exports;
2740 struct editable_file * file;
2741 struct file_line * cur_line;
2742 unsigned line_number;
2744 struct re_filterfile_spec *filter_group;
2745 int i, have_filters = 0;
2747 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2749 return cgi_error_disabled(csp, rsp);
2752 err = get_number_param(csp, parameters, "s", §ionid);
2758 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2761 /* No filename specified, can't read file, modified, or out of memory. */
2762 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2765 cur_line = file->lines;
2767 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2769 cur_line = cur_line->next;
2772 if ( (cur_line == NULL)
2773 || (line_number != sectionid)
2775 || (cur_line->type != FILE_LINE_ACTION))
2777 /* Invalid "sectionid" parameter */
2778 edit_free_file(file);
2779 return JB_ERR_CGI_PARAMS;
2782 if (NULL == (exports = default_exports(csp, NULL)))
2784 edit_free_file(file);
2785 return JB_ERR_MEMORY;
2788 err = map(exports, "f", 1, stringify(file->identifier), 0);
2789 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2790 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2792 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2795 * XXX: Some browsers (at least IE6 and IE7) have an artificial URL
2796 * length limitation and ignore clicks on the Submit buttons if
2797 * the resulting GET URL would be longer than their limit.
2799 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
2800 * reached this limit and action editing stopped working in these
2801 * browsers (BR #1570678).
2803 * The config option split-large-forms works around this browser
2804 * bug (HTTP has no URL length limitation) by deviding the action
2805 * list form into multiple smaller ones. It means the URLs are shorter
2806 * and work in broken browsers as well, but the user can no longer change
2807 * all actions with one submit.
2809 * A better solution would be to switch to POST requests,
2810 * but this will do for now.
2812 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
2814 /* Generate multiple smaller form by killing the big one. */
2815 err = map_block_killer(exports, "one-form-only");
2819 /* Default: Generate one large form by killing the smaller ones. */
2820 err = map_block_killer(exports, "multiple-forms");
2823 for (i = 0; i < MAX_AF_FILES; i++)
2825 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2827 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
2833 #ifndef FEATURE_EXTERNAL_FILTERS
2834 if (!err) err = map_block_killer(exports, "external-content-filters");
2839 edit_free_file(file);
2844 if (0 == have_filters)
2846 err = map(exports, "filter-params", 1, "", 1);
2851 * List available filters and their settings.
2853 char *filter_template;
2854 int filter_identifier = 0;
2855 char *prepared_templates[MAX_FILTER_TYPES];
2857 for (i = 0; i < MAX_FILTER_TYPES; i++)
2859 prepared_templates[i] = strdup("");
2862 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2865 edit_free_file(file);
2867 if (err == JB_ERR_FILE)
2869 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2874 err = template_fill(&filter_template, exports);
2876 for (i = 0; i < MAX_AF_FILES; i++)
2878 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2880 filter_group = csp->rlist[i]->f;
2881 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2883 char current_mode = 'x';
2885 struct list_entry *filter_name;
2886 struct map *line_exports;
2887 const int type = filter_group->type;
2888 const int multi_action_index = filter_type_info[type].multi_action_index;
2890 assert(type < MAX_FILTER_TYPES);
2892 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
2893 while ((filter_name != NULL)
2894 && (0 != strcmp(filter_group->name, filter_name->str)))
2896 filter_name = filter_name->next;
2899 if (filter_name != NULL)
2905 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
2906 while ((filter_name != NULL)
2907 && (0 != strcmp(filter_group->name, filter_name->str)))
2909 filter_name = filter_name->next;
2911 if (filter_name != NULL)
2917 /* Generate a unique serial number */
2918 snprintf(number, sizeof(number), "%x", filter_identifier++);
2919 number[sizeof(number) - 1] = '\0';
2921 line_exports = new_map();
2922 if (line_exports == NULL)
2924 err = JB_ERR_MEMORY;
2930 if (!err) err = map(line_exports, "index", 1, number, 1);
2931 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2932 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2933 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2934 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
2935 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
2936 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
2940 filter_line = strdup(filter_template);
2941 if (filter_line == NULL) err = JB_ERR_MEMORY;
2943 if (!err) err = template_fill(&filter_line, line_exports);
2944 string_join(&prepared_templates[type], filter_line);
2946 free_map(line_exports);
2951 freez(filter_template);
2953 /* Replace all filter macros with the aggregated templates */
2954 for (i = 0; i < MAX_FILTER_TYPES; i++)
2957 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
2962 /* Free aggregated templates */
2963 for (i = 0; i < MAX_FILTER_TYPES; i++)
2965 freez(prepared_templates[i]);
2970 /* Check or uncheck the "disable all of this type" radio buttons. */
2971 for (i = 0; i < MAX_FILTER_TYPES; i++)
2973 const int a = filter_type_info[i].multi_action_index;
2974 const int disable_all = cur_line->data.action->multi_remove_all[a];
2976 err = map_radio(exports, filter_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
2979 edit_free_file(file);
2987 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2991 /*********************************************************************
2993 * Function : cgi_edit_actions_submit
2995 * Description : CGI function that actually edits the Actions list.
2998 * 1 : csp = Current client state (buffers, headers, etc...)
2999 * 2 : rsp = http_response data structure for output
3000 * 3 : parameters = map of cgi parameters
3002 * CGI Parameters : None
3004 * Returns : JB_ERR_OK on success
3005 * JB_ERR_MEMORY on out-of-memory
3006 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3007 * specified or not valid.
3009 *********************************************************************/
3010 jb_err cgi_edit_actions_submit(struct client_state *csp,
3011 struct http_response *rsp,
3012 const struct map *parameters)
3017 size_t newtext_size;
3019 struct editable_file * file;
3020 struct file_line * cur_line;
3021 unsigned line_number;
3024 int filter_identifier;
3026 const char * action_set_name;
3027 struct file_list * fl;
3028 struct url_actions * b;
3030 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3032 return cgi_error_disabled(csp, rsp);
3035 err = get_number_param(csp, parameters, "s", §ionid);
3041 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3044 /* No filename specified, can't read file, modified, or out of memory. */
3045 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3048 cur_line = file->lines;
3050 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3052 cur_line = cur_line->next;
3055 if ( (cur_line == NULL)
3056 || (line_number != sectionid)
3058 || (cur_line->type != FILE_LINE_ACTION))
3060 /* Invalid "sectionid" parameter */
3061 edit_free_file(file);
3062 return JB_ERR_CGI_PARAMS;
3065 get_string_param(parameters, "p", &action_set_name);
3066 if (action_set_name != NULL)
3068 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3070 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3072 for (b = b->next; NULL != b; b = b->next)
3074 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3076 copy_action(cur_line->data.action, b->action);
3082 edit_free_file(file);
3083 return JB_ERR_CGI_PARAMS;
3089 err = actions_from_radio(parameters, cur_line->data.action);
3095 edit_free_file(file);
3099 /* Check the "disable all of this type" parameters. */
3100 for (i = 0; i < MAX_FILTER_TYPES; i++)
3102 const int multi_action_index = filter_type_info[i].multi_action_index;
3103 const char ch = get_char_param(parameters, filter_type_info[i].disable_all_param);
3107 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3108 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3109 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3113 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3117 for (filter_identifier = 0; !err; filter_identifier++)
3124 * Filter state. Valid states are: 'Y' (active),
3125 * 'N' (inactive) and 'X' (no change).
3129 * Abbreviated filter type. Valid types are: 'F' (content filter),
3130 * 'S' (server-header filter) and 'C' (client-header filter).
3132 int multi_action_index = 0;
3134 /* Generate the keys */
3135 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3136 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3137 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3138 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3139 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3141 err = get_string_param(parameters, key_name, &name);
3150 type = get_char_param(parameters, key_type);
3154 multi_action_index = ACTION_MULTI_FILTER;
3157 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3160 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3163 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3166 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3169 log_error(LOG_LEVEL_ERROR,
3170 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3173 assert(multi_action_index);
3175 value = get_char_param(parameters, key_value);
3178 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3179 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3180 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3182 else if (value == 'N')
3184 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3185 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3187 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3188 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3191 else if (value == 'X')
3193 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3194 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3201 edit_free_file(file);
3205 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3208 edit_free_file(file);
3209 return JB_ERR_MEMORY;
3212 len = strlen(actiontext);
3216 * Empty action - must special-case this.
3217 * Simply setting len to 1 is sufficient...
3222 newtext_size = len + 2;
3223 newtext = malloc_or_die(newtext_size);
3224 strlcpy(newtext, actiontext, newtext_size);
3228 newtext[len + 1] = '\0';
3230 freez(cur_line->raw);
3231 freez(cur_line->unprocessed);
3232 cur_line->unprocessed = newtext;
3234 err = edit_write_file(file);
3237 /* Error writing file */
3238 if (err == JB_ERR_FILE)
3240 /* Read-only file. */
3241 err = cgi_error_file_read_only(csp, rsp, file->filename);
3243 edit_free_file(file);
3247 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%u",
3248 (long) time(NULL), file->identifier, sectionid);
3250 edit_free_file(file);
3252 return cgi_redirect(rsp, target);
3256 /*********************************************************************
3258 * Function : cgi_edit_actions_url
3260 * Description : CGI function that actually edits a URL pattern in
3264 * 1 : csp = Current client state (buffers, headers, etc...)
3265 * 2 : rsp = http_response data structure for output
3266 * 3 : parameters = map of cgi parameters
3269 * filename : Identifies the file to edit
3270 * ver : File's last-modified time
3271 * section : Line number of section to edit
3272 * pattern : Line number of pattern to edit
3273 * newval : New value for pattern
3275 * Returns : JB_ERR_OK on success
3276 * JB_ERR_MEMORY on out-of-memory
3277 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3278 * specified or not valid.
3280 *********************************************************************/
3281 jb_err cgi_edit_actions_url(struct client_state *csp,
3282 struct http_response *rsp,
3283 const struct map *parameters)
3287 struct editable_file * file;
3288 struct file_line * cur_line;
3289 unsigned line_number;
3290 unsigned section_start_line_number = 0;
3298 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3300 return cgi_error_disabled(csp, rsp);
3303 err = get_number_param(csp, parameters, "p", &patternid);
3310 return JB_ERR_CGI_PARAMS;
3313 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3319 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3322 /* No filename specified, can't read file, modified, or out of memory. */
3324 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3328 cur_line = file->lines;
3330 while ((cur_line != NULL) && (line_number < patternid))
3332 if (cur_line->type == FILE_LINE_ACTION)
3334 section_start_line_number = line_number;
3336 cur_line = cur_line->next;
3340 if ((cur_line == NULL)
3341 || (cur_line->type != FILE_LINE_URL))
3343 /* Invalid "patternid" parameter */
3345 edit_free_file(file);
3346 return JB_ERR_CGI_PARAMS;
3349 /* At this point, the line to edit is in cur_line */
3351 freez(cur_line->raw);
3352 freez(cur_line->unprocessed);
3353 cur_line->unprocessed = new_pattern;
3355 err = edit_write_file(file);
3358 /* Error writing file */
3359 if (err == JB_ERR_FILE)
3361 /* Read-only file. */
3362 err = cgi_error_file_read_only(csp, rsp, file->filename);
3364 edit_free_file(file);
3368 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%u",
3369 (long) time(NULL), file->identifier, section_start_line_number);
3371 edit_free_file(file);
3373 return cgi_redirect(rsp, target);
3377 /*********************************************************************
3379 * Function : cgi_edit_actions_add_url
3381 * Description : CGI function that actually adds a URL pattern to
3385 * 1 : csp = Current client state (buffers, headers, etc...)
3386 * 2 : rsp = http_response data structure for output
3387 * 3 : parameters = map of cgi parameters
3390 * filename : Identifies the file to edit
3391 * ver : File's last-modified time
3392 * section : Line number of section to edit
3393 * newval : New pattern
3395 * Returns : JB_ERR_OK on success
3396 * JB_ERR_MEMORY on out-of-memory
3397 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3398 * specified or not valid.
3400 *********************************************************************/
3401 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3402 struct http_response *rsp,
3403 const struct map *parameters)
3407 struct file_line * new_line;
3408 struct editable_file * file;
3409 struct file_line * cur_line;
3410 unsigned line_number;
3414 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3416 return cgi_error_disabled(csp, rsp);
3419 err = get_number_param(csp, parameters, "s", §ionid);
3426 return JB_ERR_CGI_PARAMS;
3429 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3435 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3438 /* No filename specified, can't read file, modified, or out of memory. */
3440 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3444 cur_line = file->lines;
3446 while ((cur_line != NULL) && (line_number < sectionid))
3448 cur_line = cur_line->next;
3452 if ((cur_line == NULL)
3453 || (cur_line->type != FILE_LINE_ACTION))
3455 /* Invalid "sectionid" parameter */
3457 edit_free_file(file);
3458 return JB_ERR_CGI_PARAMS;
3461 /* At this point, the section header is in cur_line - add after this. */
3463 /* Allocate the new line */
3464 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3465 if (new_line == NULL)
3468 edit_free_file(file);
3469 return JB_ERR_MEMORY;
3472 /* Fill in the data members of the new line */
3473 new_line->raw = NULL;
3474 new_line->prefix = NULL;
3475 new_line->unprocessed = new_pattern;
3476 new_line->type = FILE_LINE_URL;
3478 /* Link new_line into the list, after cur_line */
3479 new_line->next = cur_line->next;
3480 cur_line->next = new_line;
3482 /* Done making changes, now commit */
3484 err = edit_write_file(file);
3487 /* Error writing file */
3488 if (err == JB_ERR_FILE)
3490 /* Read-only file. */
3491 err = cgi_error_file_read_only(csp, rsp, file->filename);
3493 edit_free_file(file);
3497 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%u",
3498 (long) time(NULL), file->identifier, sectionid);
3500 edit_free_file(file);
3502 return cgi_redirect(rsp, target);
3506 /*********************************************************************
3508 * Function : cgi_edit_actions_remove_url
3510 * Description : CGI function that actually removes a URL pattern from
3514 * 1 : csp = Current client state (buffers, headers, etc...)
3515 * 2 : rsp = http_response data structure for output
3516 * 3 : parameters = map of cgi parameters
3519 * f : (filename) Identifies the file to edit
3520 * v : (version) File's last-modified time
3521 * p : (pattern) Line number of pattern to remove
3523 * Returns : JB_ERR_OK on success
3524 * JB_ERR_MEMORY on out-of-memory
3525 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3526 * specified or not valid.
3528 *********************************************************************/
3529 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3530 struct http_response *rsp,
3531 const struct map *parameters)
3534 struct editable_file * file;
3535 struct file_line * cur_line;
3536 struct file_line * prev_line;
3537 unsigned line_number;
3538 unsigned section_start_line_number = 0;
3542 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3544 return cgi_error_disabled(csp, rsp);
3547 err = get_number_param(csp, parameters, "p", &patternid);
3553 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3556 /* No filename specified, can't read file, modified, or out of memory. */
3557 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3562 cur_line = file->lines;
3564 while ((cur_line != NULL) && (line_number < patternid))
3566 if (cur_line->type == FILE_LINE_ACTION)
3568 section_start_line_number = line_number;
3570 prev_line = cur_line;
3571 cur_line = cur_line->next;
3575 if ( (cur_line == NULL)
3576 || (prev_line == NULL)
3577 || (cur_line->type != FILE_LINE_URL))
3579 /* Invalid "patternid" parameter */
3580 edit_free_file(file);
3581 return JB_ERR_CGI_PARAMS;
3584 /* At this point, the line to remove is in cur_line, and the previous
3585 * one is in prev_line
3588 /* Unlink cur_line */
3589 prev_line->next = cur_line->next;
3590 cur_line->next = NULL;
3593 edit_free_file_lines(cur_line);
3595 err = edit_write_file(file);
3598 /* Error writing file */
3599 if (err == JB_ERR_FILE)
3601 /* Read-only file. */
3602 err = cgi_error_file_read_only(csp, rsp, file->filename);
3604 edit_free_file(file);
3608 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
3609 (long) time(NULL), file->identifier, section_start_line_number);
3611 edit_free_file(file);
3613 return cgi_redirect(rsp, target);
3617 /*********************************************************************
3619 * Function : cgi_edit_actions_section_remove
3621 * Description : CGI function that actually removes a whole section from
3622 * the actions file. The section must be empty first
3623 * (else JB_ERR_CGI_PARAMS).
3626 * 1 : csp = Current client state (buffers, headers, etc...)
3627 * 2 : rsp = http_response data structure for output
3628 * 3 : parameters = map of cgi parameters
3631 * f : (filename) Identifies the file to edit
3632 * v : (version) File's last-modified time
3633 * s : (section) Line number of section to edit
3635 * Returns : JB_ERR_OK on success
3636 * JB_ERR_MEMORY on out-of-memory
3637 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3638 * specified or not valid.
3640 *********************************************************************/
3641 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3642 struct http_response *rsp,
3643 const struct map *parameters)
3646 struct editable_file * file;
3647 struct file_line * cur_line;
3648 struct file_line * prev_line;
3649 unsigned line_number;
3653 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3655 return cgi_error_disabled(csp, rsp);
3658 err = get_number_param(csp, parameters, "s", §ionid);
3664 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3667 /* No filename specified, can't read file, modified, or out of memory. */
3668 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3672 cur_line = file->lines;
3675 while ((cur_line != NULL) && (line_number < sectionid))
3677 prev_line = cur_line;
3678 cur_line = cur_line->next;
3682 if ((cur_line == NULL)
3683 || (cur_line->type != FILE_LINE_ACTION))
3685 /* Invalid "sectionid" parameter */
3686 edit_free_file(file);
3687 return JB_ERR_CGI_PARAMS;
3690 if ((cur_line->next != NULL)
3691 && (cur_line->next->type == FILE_LINE_URL))
3693 /* Section not empty. */
3694 edit_free_file(file);
3695 return JB_ERR_CGI_PARAMS;
3698 /* At this point, the line to remove is in cur_line, and the previous
3699 * one is in prev_line
3702 /* Unlink cur_line */
3703 if (prev_line == NULL)
3705 /* Removing the first line from the file */
3706 file->lines = cur_line->next;
3710 prev_line->next = cur_line->next;
3712 cur_line->next = NULL;
3715 edit_free_file_lines(cur_line);
3717 err = edit_write_file(file);
3720 /* Error writing file */
3721 if (err == JB_ERR_FILE)
3723 /* Read-only file. */
3724 err = cgi_error_file_read_only(csp, rsp, file->filename);
3726 edit_free_file(file);
3730 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
3731 (long) time(NULL), file->identifier);
3733 edit_free_file(file);
3735 return cgi_redirect(rsp, target);
3739 /*********************************************************************
3741 * Function : cgi_edit_actions_section_add
3743 * Description : CGI function that adds a new empty section to
3747 * 1 : csp = Current client state (buffers, headers, etc...)
3748 * 2 : rsp = http_response data structure for output
3749 * 3 : parameters = map of cgi parameters
3752 * f : (filename) Identifies the file to edit
3753 * v : (version) File's last-modified time
3754 * s : (section) Line number of section to add after, 0 for
3757 * Returns : JB_ERR_OK on success
3758 * JB_ERR_MEMORY on out-of-memory
3759 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3760 * specified or not valid.
3762 *********************************************************************/
3763 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3764 struct http_response *rsp,
3765 const struct map *parameters)
3768 struct file_line * new_line;
3770 struct editable_file * file;
3771 struct file_line * cur_line;
3772 unsigned line_number;
3776 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3778 return cgi_error_disabled(csp, rsp);
3781 err = get_number_param(csp, parameters, "s", §ionid);
3787 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3790 /* No filename specified, can't read file, modified, or out of memory. */
3791 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3795 cur_line = file->lines;
3797 if (sectionid <= 1U)
3799 /* Add to start of file */
3800 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
3802 /* There's something in the file, find the line before the first
3805 while ((cur_line->next != NULL)
3806 && (cur_line->next->type != FILE_LINE_ACTION))
3808 cur_line = cur_line->next;
3814 /* File starts with action line, so insert at top */
3820 /* Add after stated section. */
3821 while ((cur_line != NULL) && (line_number < sectionid))
3823 cur_line = cur_line->next;
3827 if ((cur_line == NULL)
3828 || (cur_line->type != FILE_LINE_ACTION))
3830 /* Invalid "sectionid" parameter */
3831 edit_free_file(file);
3832 return JB_ERR_CGI_PARAMS;
3835 /* Skip through the section to find the last line in it. */
3836 while ((cur_line->next != NULL)
3837 && (cur_line->next->type != FILE_LINE_ACTION))
3839 cur_line = cur_line->next;
3844 /* At this point, the last line in the previous section is in cur_line
3845 * - add after this. (Or if we need to add as the first line, cur_line
3849 new_text = strdup("{}");
3850 if (NULL == new_text)
3852 edit_free_file(file);
3853 return JB_ERR_MEMORY;
3856 /* Allocate the new line */
3857 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3858 if (new_line == NULL)
3861 edit_free_file(file);
3862 return JB_ERR_MEMORY;
3865 /* Fill in the data members of the new line */
3866 new_line->raw = NULL;
3867 new_line->prefix = NULL;
3868 new_line->unprocessed = new_text;
3869 new_line->type = FILE_LINE_ACTION;
3871 if (cur_line != NULL)
3873 /* Link new_line into the list, after cur_line */
3874 new_line->next = cur_line->next;
3875 cur_line->next = new_line;
3879 /* Link new_line into the list, as first line */
3880 new_line->next = file->lines;
3881 file->lines = new_line;
3884 /* Done making changes, now commit */
3886 err = edit_write_file(file);
3889 /* Error writing file */
3890 if (err == JB_ERR_FILE)
3892 /* Read-only file. */
3893 err = cgi_error_file_read_only(csp, rsp, file->filename);
3895 edit_free_file(file);
3899 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
3900 (long) time(NULL), file->identifier);
3902 edit_free_file(file);
3904 return cgi_redirect(rsp, target);
3908 /*********************************************************************
3910 * Function : cgi_edit_actions_section_swap
3912 * Description : CGI function that swaps the order of two sections
3913 * in the actions file. Note that this CGI can actually
3914 * swap any two arbitrary sections, but the GUI interface
3915 * currently only allows consecutive sections to be
3919 * 1 : csp = Current client state (buffers, headers, etc...)
3920 * 2 : rsp = http_response data structure for output
3921 * 3 : parameters = map of cgi parameters
3924 * f : (filename) Identifies the file to edit
3925 * v : (version) File's last-modified time
3926 * s1 : (section1) Line number of first section to swap
3927 * s2 : (section2) Line number of second section to swap
3929 * Returns : JB_ERR_OK on success
3930 * JB_ERR_MEMORY on out-of-memory
3931 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3932 * specified or not valid.
3934 *********************************************************************/
3935 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3936 struct http_response *rsp,
3937 const struct map *parameters)
3941 struct editable_file * file;
3942 struct file_line * cur_line;
3943 struct file_line * prev_line;
3944 struct file_line * line_before_section1;
3945 struct file_line * line_start_section1;
3946 struct file_line * line_end_section1;
3947 struct file_line * line_after_section1;
3948 struct file_line * line_before_section2;
3949 struct file_line * line_start_section2;
3950 struct file_line * line_end_section2;
3951 struct file_line * line_after_section2;
3952 unsigned line_number;
3956 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3958 return cgi_error_disabled(csp, rsp);
3961 err = get_number_param(csp, parameters, "s1", §ion1);
3962 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3968 if (section1 > section2)
3970 unsigned temp = section2;
3971 section2 = section1;
3975 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3978 /* No filename specified, can't read file, modified, or out of memory. */
3979 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3982 /* Start at the beginning... */
3984 cur_line = file->lines;
3987 /* ... find section1 ... */
3988 while ((cur_line != NULL) && (line_number < section1))
3990 prev_line = cur_line;
3991 cur_line = cur_line->next;
3995 if ((cur_line == NULL)
3996 || (cur_line->type != FILE_LINE_ACTION))
3998 /* Invalid "section1" parameter */
3999 edit_free_file(file);
4000 return JB_ERR_CGI_PARAMS;
4003 /* If no-op, we've validated params and can skip the rest. */
4004 if (section1 != section2)
4006 /* ... find the end of section1 ... */
4007 line_before_section1 = prev_line;
4008 line_start_section1 = cur_line;
4011 prev_line = cur_line;
4012 cur_line = cur_line->next;
4015 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4016 line_end_section1 = prev_line;
4017 line_after_section1 = cur_line;
4019 /* ... find section2 ... */
4020 while ((cur_line != NULL) && (line_number < section2))
4022 prev_line = cur_line;
4023 cur_line = cur_line->next;
4027 if ((cur_line == NULL)
4028 || (cur_line->type != FILE_LINE_ACTION))
4030 /* Invalid "section2" parameter */
4031 edit_free_file(file);
4032 return JB_ERR_CGI_PARAMS;
4035 /* ... find the end of section2 ... */
4036 line_before_section2 = prev_line;
4037 line_start_section2 = cur_line;
4040 prev_line = cur_line;
4041 cur_line = cur_line->next;
4044 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4045 line_end_section2 = prev_line;
4046 line_after_section2 = cur_line;
4048 /* Now have all the pointers we need. Do the swap. */
4050 /* Change the pointer to section1 to point to section2 instead */
4051 if (line_before_section1 == NULL)
4053 file->lines = line_start_section2;
4057 line_before_section1->next = line_start_section2;
4060 if (line_before_section2 == line_end_section1)
4062 /* Consecutive sections */
4063 line_end_section2->next = line_start_section1;
4067 line_end_section2->next = line_after_section1;
4068 line_before_section2->next = line_start_section1;
4071 /* Set the pointer from the end of section1 to the rest of the file */
4072 line_end_section1->next = line_after_section2;
4074 err = edit_write_file(file);
4077 /* Error writing file */
4078 if (err == JB_ERR_FILE)
4080 /* Read-only file. */
4081 err = cgi_error_file_read_only(csp, rsp, file->filename);
4083 edit_free_file(file);
4086 } /* END if (section1 != section2) */
4088 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
4089 (long) time(NULL), file->identifier);
4091 edit_free_file(file);
4093 return cgi_redirect(rsp, target);
4097 /*********************************************************************
4099 * Function : javascriptify
4101 * Description : Converts a string into a form JavaScript will like.
4103 * Netscape 4's JavaScript sucks - it doesn't use
4104 * "id" parameters, so you have to set the "name"
4105 * used to submit a form element to something JavaScript
4106 * will like. (Or access the elements by index in an
4107 * array. That array contains >60 elements and will
4108 * be changed whenever we add a new action to the
4109 * editor, so I'm NOT going to use indexes that have
4110 * to be figured out by hand.)
4112 * Currently the only thing we have to worry about
4113 * is "-" ==> "_" conversion.
4115 * This is a length-preserving operation so it is
4116 * carried out in-place, no memory is allocated
4120 * 1 : identifier = String to make JavaScript-friendly.
4124 *********************************************************************/
4125 static void javascriptify(char * identifier)
4127 char * p = identifier;
4128 while (NULL != (p = strchr(p, '-')))
4135 /*********************************************************************
4137 * Function : actions_to_radio
4139 * Description : Converts a actionsfile entry into settings for
4140 * radio buttons and edit boxes on a HTML form.
4143 * 1 : exports = List of substitutions to add to.
4144 * 2 : action = Action to read
4146 * Returns : JB_ERR_OK on success
4147 * JB_ERR_MEMORY on out-of-memory
4149 *********************************************************************/
4150 static jb_err actions_to_radio(struct map * exports,
4151 const struct action_spec *action)
4162 mask = action->mask;
4165 /* sanity - prevents "-feature +feature" */
4169 #define DEFINE_ACTION_BOOL(name, bit) \
4170 if (!(mask & bit)) \
4172 current_mode = 'n'; \
4174 else if (add & bit) \
4176 current_mode = 'y'; \
4180 current_mode = 'x'; \
4182 if (map_radio(exports, name, "ynx", current_mode)) \
4184 return JB_ERR_MEMORY; \
4187 #define DEFINE_ACTION_STRING(name, bit, index) \
4188 DEFINE_ACTION_BOOL(name, bit); \
4191 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4194 checked = !strcmp(action->string[index], value); \
4198 checked = is_default; \
4200 mapped_param |= checked; \
4201 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4203 return JB_ERR_MEMORY; \
4206 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4207 if (map(exports, name "-param-custom", 1, \
4208 ((!mapped_param) ? "checked" : ""), 1)) \
4210 return JB_ERR_MEMORY; \
4212 if (map(exports, name "-param", 1, \
4213 (((add & bit) && !mapped_param) ? \
4214 action->string[index] : default_val), 1)) \
4216 return JB_ERR_MEMORY; \
4219 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4220 if (map(exports, name "-param", 1, \
4221 ((add & bit) ? action->string[index] : default_val), 1)) \
4223 return JB_ERR_MEMORY; \
4226 #define DEFINE_ACTION_MULTI(name, index) \
4227 if (action->multi_add[index]->first) \
4229 current_mode = 'y'; \
4231 else if (action->multi_remove_all[index]) \
4233 current_mode = 'n'; \
4235 else if (action->multi_remove[index]->first) \
4237 current_mode = 'y'; \
4241 current_mode = 'x'; \
4243 if (map_radio(exports, name, "ynx", current_mode)) \
4245 return JB_ERR_MEMORY; \
4248 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4250 #include "actionlist.h"
4252 #undef DEFINE_ACTION_MULTI
4253 #undef DEFINE_ACTION_STRING
4254 #undef DEFINE_ACTION_BOOL
4255 #undef DEFINE_ACTION_ALIAS
4256 #undef DEFINE_CGI_PARAM_CUSTOM
4257 #undef DEFINE_CGI_PARAM_RADIO
4258 #undef DEFINE_CGI_PARAM_NO_RADIO
4264 /*********************************************************************
4266 * Function : actions_from_radio
4268 * Description : Converts a map of parameters passed to a CGI function
4269 * into an actionsfile entry.
4272 * 1 : parameters = parameters to the CGI call
4273 * 2 : action = Action to change. Must be valid before
4274 * the call, actions not specified will be
4277 * Returns : JB_ERR_OK on success
4278 * JB_ERR_MEMORY on out-of-memory
4280 *********************************************************************/
4281 static jb_err actions_from_radio(const struct map * parameters,
4282 struct action_spec *action)
4287 const char * js_name;
4288 jb_err err = JB_ERR_OK;
4293 /* Statics are generally a potential race condition,
4294 * but in this case we're safe and don't need semaphores.
4295 * Be careful if you modify this function.
4297 * The js_name_arr's are never free()d, but this is no
4298 * problem, since they will only be created once and
4299 * used by all threads thereafter. -oes
4302 #define JAVASCRIPTIFY(dest_var, string) \
4304 static int first_time = 1; \
4305 static char *js_name_arr; \
4308 js_name_arr = strdup(string); \
4309 javascriptify(js_name_arr); \
4311 dest_var = js_name_arr; \
4315 #define DEFINE_ACTION_BOOL(name, bit) \
4316 JAVASCRIPTIFY(js_name, name); \
4317 ch = get_char_param(parameters, js_name); \
4320 action->add |= bit; \
4321 action->mask |= bit; \
4323 else if (ch == 'N') \
4325 action->add &= ~bit; \
4326 action->mask &= ~bit; \
4328 else if (ch == 'X') \
4330 action->add &= ~bit; \
4331 action->mask |= bit; \
4334 #define DEFINE_ACTION_STRING(name, bit, index) \
4335 JAVASCRIPTIFY(js_name, name); \
4336 ch = get_char_param(parameters, js_name); \
4340 JAVASCRIPTIFY(js_name, name "-mode"); \
4341 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4342 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4344 JAVASCRIPTIFY(js_name, name "-param"); \
4345 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4347 if (param != NULL) \
4349 if (NULL == (param_dup = strdup(param))) \
4351 return JB_ERR_MEMORY; \
4353 freez(action->string[index]); \
4354 action->add |= bit; \
4355 action->mask |= bit; \
4356 action->string[index] = param_dup; \
4359 else if (ch == 'N') \
4361 if (action->add & bit) \
4363 freez(action->string[index]); \
4365 action->add &= ~bit; \
4366 action->mask &= ~bit; \
4368 else if (ch == 'X') \
4370 if (action->add & bit) \
4372 freez(action->string[index]); \
4374 action->add &= ~bit; \
4375 action->mask |= bit; \
4378 #define DEFINE_ACTION_MULTI(name, index) \
4379 JAVASCRIPTIFY(js_name, name); \
4380 ch = get_char_param(parameters, js_name); \
4385 else if (ch == 'N') \
4387 list_remove_all(action->multi_add[index]); \
4388 list_remove_all(action->multi_remove[index]); \
4389 action->multi_remove_all[index] = 1; \
4391 else if (ch == 'X') \
4393 list_remove_all(action->multi_add[index]); \
4394 list_remove_all(action->multi_remove[index]); \
4395 action->multi_remove_all[index] = 0; \
4398 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4400 #include "actionlist.h"
4402 #undef DEFINE_ACTION_MULTI
4403 #undef DEFINE_ACTION_STRING
4404 #undef DEFINE_ACTION_BOOL
4405 #undef DEFINE_ACTION_ALIAS
4406 #undef JAVASCRIPTIFY
4410 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4413 #ifdef FEATURE_TOGGLE
4414 /*********************************************************************
4416 * Function : cgi_toggle
4418 * Description : CGI function that adds a new empty section to
4422 * 1 : csp = Current client state (buffers, headers, etc...)
4423 * 2 : rsp = http_response data structure for output
4424 * 3 : parameters = map of cgi parameters
4427 * set : If present, how to change toggle setting:
4428 * "enable", "disable", "toggle", or none (default).
4429 * mini : If present, use mini reply template.
4431 * Returns : JB_ERR_OK on success
4432 * JB_ERR_MEMORY on out-of-memory
4434 *********************************************************************/
4435 jb_err cgi_toggle(struct client_state *csp,
4436 struct http_response *rsp,
4437 const struct map *parameters)
4439 struct map *exports;
4441 const char *template_name;
4447 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4449 return cgi_error_disabled(csp, rsp);
4452 mode = get_char_param(parameters, "set");
4457 global_toggle_state = 1;
4459 else if (mode == 'D')
4462 global_toggle_state = 0;
4464 else if (mode == 'T')
4467 global_toggle_state = !global_toggle_state;
4470 if (NULL == (exports = default_exports(csp, "toggle")))
4472 return JB_ERR_MEMORY;
4475 template_name = (get_char_param(parameters, "mini")
4479 return template_fill_for_cgi(csp, template_name, exports, rsp);
4481 #endif /* def FEATURE_TOGGLE */