1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
5 * Purpose : CGI-based actionsfile editor.
7 * NOTE: The CGIs in this file use parameter names
8 * such as "f" and "s" which are really *BAD* choices.
9 * However, I'm trying to save bytes in the
10 * edit-actions-list HTML page - the standard actions
11 * file generated a 550kbyte page, which is ridiculous.
13 * Stick to the short names in this file for consistency.
15 * Copyright : Written by and Copyright (C) 2001-2014 the
16 * Privoxy team. https://www.privoxy.org/
18 * Based on the Internet Junkbuster originally written
19 * by and Copyright (C) 1997 Anonymous Coders and
20 * Junkbusters Corporation. http://www.junkbusters.com
22 * This program is free software; you can redistribute it
23 * and/or modify it under the terms of the GNU General
24 * Public License as published by the Free Software
25 * Foundation; either version 2 of the License, or (at
26 * your option) any later version.
28 * This program is distributed in the hope that it will
29 * be useful, but WITHOUT ANY WARRANTY; without even the
30 * implied warranty of MERCHANTABILITY or FITNESS FOR A
31 * PARTICULAR PURPOSE. See the GNU General Public
32 * License for more details.
34 * The GNU General Public License should be included with
35 * this file. If not, you can view it at
36 * http://www.gnu.org/copyleft/gpl.html
37 * or write to the Free Software Foundation, Inc., 59
38 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 **********************************************************************/
46 * FIXME: Following includes copied from cgi.c - which are actually needed?
51 #include <sys/types.h>
60 #include "cgisimple.h"
68 /* loadcfg.h is for global_toggle_state only */
70 #endif /* def FEATURE_TOGGLE */
74 #ifdef FEATURE_CGI_EDIT_ACTIONS
77 * A line in an editable_file.
81 /** Next entry in the linked list */
82 struct file_line * next;
84 /** The raw data, to write out if this line is unmodified. */
87 /** Comments and/or whitespace to put before this line if it's modified
88 and then written out. */
91 /** The actual data, as a string. Line continuation and comment removal
92 are performed on the data read from file before it's stored here, so
93 it will be a single line of data. */
96 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
99 /** The actual data, processed into some sensible data type. */
103 /** An action specification. */
104 struct action_spec action[1];
106 /** A name=value pair. */
110 /** The name in the name=value pair. */
113 /** The value in the name=value pair, as a string. */
116 /** The value in the name=value pair, as an integer. */
125 /** This file_line has not been processed yet. */
126 #define FILE_LINE_UNPROCESSED 1
128 /** This file_line is blank. Can only appear at the end of a file, due to
129 the way the parser works. */
130 #define FILE_LINE_BLANK 2
132 /** This file_line says {{alias}}. */
133 #define FILE_LINE_ALIAS_HEADER 3
135 /** This file_line defines an alias. */
136 #define FILE_LINE_ALIAS_ENTRY 4
138 /** This file_line defines an {action}. */
139 #define FILE_LINE_ACTION 5
141 /** This file_line specifies a URL pattern. */
142 #define FILE_LINE_URL 6
144 /** This file_line says {{settings}}. */
145 #define FILE_LINE_SETTINGS_HEADER 7
147 /** This file_line is in a {{settings}} block. */
148 #define FILE_LINE_SETTINGS_ENTRY 8
150 /** This file_line says {{description}}. */
151 #define FILE_LINE_DESCRIPTION_HEADER 9
153 /** This file_line is in a {{description}} block. */
154 #define FILE_LINE_DESCRIPTION_ENTRY 10
157 * Number of file modification time mismatches
158 * before the CGI editor gets turned off.
160 #define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
163 * A configuration file, in a format that can be edited and written back to
168 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
169 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
170 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
171 const char * version_str; /**< Last modification time, as a string. For CGI param. */
172 /**< Can be used in URL without using url_param(). */
173 unsigned version; /**< Last modification time - prevents chaos with
174 the browser's "back" button. Note that this is a
175 time_t cast to an unsigned. When comparing, always
176 cast the time_t to an unsigned, and *NOT* vice-versa.
177 This may lose the top few bits, but they're not
178 significant anyway. */
179 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
180 Note that changing this after the file has been
181 read in will cause a mess. */
182 struct file_line * parse_error; /**< On parse error, this is the offending line. */
183 const char * parse_error_text; /**< On parse error, this is the problem.
184 (Statically allocated) */
188 * Information about the filter types.
189 * Used for macro replacement in cgi_edit_actions_for_url.
191 struct action_type_info
193 const int multi_action_index; /**< The multi action index as defined in project.h */
194 const char *macro_name; /**< Name of the macro that has to be replaced
195 with the prepared templates.
196 For example "content-filter-params" */
197 const char *type; /**< Name of the filter type,
198 for example "server-header-filter". */
199 /* XXX: check if these two can be combined. */
200 const char *disable_all_option; /**< Name of the catch-all radio option that has
201 to be checked or unchecked for this filter type. */
202 const char *disable_all_param; /**< Name of the parameter that causes all filters of
203 this type to be disabled. */
204 const char *abbr_type; /**< Abbreviation of the filter type, usually the
205 first or second character capitalized */
206 const char *anchor; /**< Anchor for the User Manual link,
207 for example "SERVER-HEADER-FILTER" */
210 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
211 static const struct action_type_info action_type_info[] =
215 "content-filter-params", "filter",
216 "filter-all", "filter_all",
220 ACTION_MULTI_CLIENT_HEADER_FILTER,
221 "client-header-filter-params", "client-header-filter",
222 "client-header-filter-all", "client_header_filter_all",
223 "C", "CLIENT-HEADER-FILTER"
226 ACTION_MULTI_SERVER_HEADER_FILTER,
227 "server-header-filter-params", "server-header-filter",
228 "server-header-filter-all", "server_header_filter_all",
229 "S", "SERVER-HEADER-FILTER"
232 ACTION_MULTI_CLIENT_HEADER_TAGGER,
233 "client-header-tagger-params", "client-header-tagger",
234 "client-header-tagger-all", "client_header_tagger_all",
235 "L", "CLIENT-HEADER-TAGGER"
238 ACTION_MULTI_SERVER_HEADER_TAGGER,
239 "server-header-tagger-params", "server-header-tagger",
240 "server-header-tagger-all", "server_header_tagger_all",
241 "E", "SERVER-HEADER-TAGGER"
244 ACTION_MULTI_SUPPRESS_TAG,
245 "suppress-tag-params", "suppress-tag",
246 "suppress-tag-all", "suppress_tag_all",
250 ACTION_MULTI_CLIENT_BODY_FILTER,
251 "client-body-filter-params", "client-body-filter",
252 "client-body-filter-all", "client_body_filter_all",
253 "P", "CLIENT-BODY-FILTER"
256 ACTION_MULTI_ADD_HEADER,
257 "add-header-params", "add-header",
258 "add-header-all", "add_header_all",
261 #ifdef FEATURE_EXTERNAL_FILTERS
263 ACTION_MULTI_EXTERNAL_FILTER,
264 "external-content-filter-params", "external-filter",
265 "external-content-filter-all", "external_content_filter_all",
266 "E", "EXTERNAL-CONTENT-FILTER"
272 * Information about the string filter actions.
273 * Used for macro replacement in action_render_string_actions_template
275 struct string_action_type_info
277 const enum filter_type action_type; /**< Action type */
278 const char *description; /**< Action description */
279 const char *input_description; /**< Input field description */
282 /* String action types: special CGI handling */
283 static const struct string_action_type_info string_action_type_info[] =
287 "Suppress tag", "Tag to suppress"
291 "Add HTTP client header", "HTTP client header to add"
295 /* FIXME: Following non-static functions should be prototyped in .h or made static */
297 /* Functions to read and write arbitrary config files */
298 jb_err edit_read_file(struct client_state *csp,
299 const struct map *parameters,
301 struct editable_file **pfile);
302 jb_err edit_write_file(struct editable_file * file);
303 void edit_free_file(struct editable_file * file);
305 /* Functions to read and write actions files */
306 jb_err edit_parse_actions_file(struct editable_file * file);
307 jb_err edit_read_actions_file(struct client_state *csp,
308 struct http_response *rsp,
309 const struct map *parameters,
311 struct editable_file **pfile);
314 jb_err cgi_error_modified(struct client_state *csp,
315 struct http_response *rsp,
316 const char *filename);
317 jb_err cgi_error_parse(struct client_state *csp,
318 struct http_response *rsp,
319 struct editable_file *file);
320 jb_err cgi_error_file(struct client_state *csp,
321 struct http_response *rsp,
322 const char *filename);
323 jb_err cgi_error_file_read_only(struct client_state *csp,
324 struct http_response *rsp,
325 const char *filename);
327 /* Internal arbitrary config file support functions */
328 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
329 static void edit_free_file_lines(struct file_line * first_line);
331 /* Internal actions file support functions */
332 static int match_actions_file_header_line(const char * line, const char * name);
333 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
335 /* Internal parameter parsing functions */
336 static jb_err get_url_spec_param(struct client_state *csp,
337 const struct map *parameters,
342 /* Internal actionsfile <==> HTML conversion functions */
343 static jb_err map_radio(struct map * exports,
344 const char * optionname,
347 static jb_err actions_to_radio(struct map * exports,
348 const struct action_spec *action);
349 static jb_err actions_from_radio(const struct map * parameters,
350 struct action_spec *action);
351 static jb_err action_render_string_actions_template(struct map * exports,
352 const struct action_spec *action,
353 const char* action_template,
354 const struct string_action_type_info *string_action_type);
357 static jb_err map_copy_parameter_html(struct map *out,
358 const struct map *in,
361 static jb_err get_file_name_param(struct client_state *csp,
362 const struct map *parameters,
363 const char *param_name,
364 const char **pfilename);
366 /* Internal convenience functions */
367 static char *section_target(const unsigned sectionid);
369 /*********************************************************************
371 * Function : section_target
373 * Description : Given an unsigned (section id) n, produce a dynamically
374 * allocated string of the form #l<n>, for use in link
377 * XXX: The hash should be moved into the templates
378 * to make this function more generic and render
379 * stringify() obsolete.
382 * 1 : sectionid = start line number of section
384 * Returns : String with link target, or NULL if out of
387 *********************************************************************/
388 static char *section_target(const unsigned sectionid)
392 snprintf(buf, sizeof(buf), "#l%u", sectionid);
398 /*********************************************************************
400 * Function : stringify
402 * Description : Convert a number into a dynamically allocated string.
405 * 1 : number = The number to convert.
407 * Returns : String with link target, or NULL if out of memory
409 *********************************************************************/
410 static char *stringify(const unsigned number)
414 snprintf(buf, sizeof(buf), "%u", number);
419 /*********************************************************************
421 * Function : map_copy_parameter_html
423 * Description : Copy a CGI parameter from one map to another, HTML
427 * 1 : out = target map
428 * 2 : in = source map
429 * 3 : name = name of cgi parameter to copy
431 * Returns : JB_ERR_OK on success
432 * JB_ERR_MEMORY on out-of-memory
433 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
436 *********************************************************************/
437 static jb_err map_copy_parameter_html(struct map *out,
438 const struct map *in,
448 value = lookup(in, name);
449 err = map(out, name, 1, html_encode(value), 0);
456 else if (*value == '\0')
458 return JB_ERR_CGI_PARAMS;
467 /*********************************************************************
469 * Function : cgi_edit_actions_url_form
471 * Description : CGI function that displays a form for
475 * 1 : csp = Current client state (buffers, headers, etc...)
476 * 2 : rsp = http_response data structure for output
477 * 3 : parameters = map of cgi parameters
480 * i : (action index) Identifies the file to edit
481 * v : (version) File's last-modified time
482 * p : (pattern) Line number of pattern to edit
484 * Returns : JB_ERR_OK on success
485 * JB_ERR_MEMORY on out-of-memory
486 * JB_ERR_CGI_PARAMS if the CGI parameters are not
487 * specified or not valid.
489 *********************************************************************/
490 jb_err cgi_edit_actions_url_form(struct client_state *csp,
491 struct http_response *rsp,
492 const struct map *parameters)
494 struct map * exports;
496 struct editable_file * file;
497 struct file_line * cur_line;
498 unsigned line_number;
499 unsigned section_start_line_number = 0;
506 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
508 return cgi_error_disabled(csp, rsp);
511 err = get_number_param(csp, parameters, "p", &patternid);
517 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
520 /* No filename specified, can't read file, modified, or out of memory. */
521 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
524 cur_line = file->lines;
526 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
528 if (cur_line->type == FILE_LINE_ACTION)
530 section_start_line_number = line_number;
532 cur_line = cur_line->next;
535 if ( (cur_line == NULL)
536 || (line_number != patternid)
538 || (cur_line->type != FILE_LINE_URL))
540 /* Invalid "patternid" parameter */
541 edit_free_file(file);
542 return JB_ERR_CGI_PARAMS;
545 if (NULL == (exports = default_exports(csp, NULL)))
547 edit_free_file(file);
548 return JB_ERR_MEMORY;
551 err = map(exports, "f", 1, stringify(file->identifier), 0);
552 if (!err) err = map(exports, "v", 1, file->version_str, 1);
553 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
554 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
555 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
557 edit_free_file(file);
565 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
569 /*********************************************************************
571 * Function : cgi_edit_actions_add_url_form
573 * Description : CGI function that displays a form for
577 * 1 : csp = Current client state (buffers, headers, etc...)
578 * 2 : rsp = http_response data structure for output
579 * 3 : parameters = map of cgi parameters
582 * f : (filename) Identifies the file to edit
583 * v : (version) File's last-modified time
584 * s : (section) Line number of section to edit
586 * Returns : JB_ERR_OK on success
587 * JB_ERR_MEMORY on out-of-memory
588 * JB_ERR_CGI_PARAMS if the CGI parameters are not
589 * specified or not valid.
591 *********************************************************************/
592 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
593 struct http_response *rsp,
594 const struct map *parameters)
603 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
605 return cgi_error_disabled(csp, rsp);
608 if (NULL == (exports = default_exports(csp, NULL)))
610 return JB_ERR_MEMORY;
613 err = map_copy_parameter_html(exports, parameters, "f");
614 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
615 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
623 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
627 /*********************************************************************
629 * Function : cgi_edit_actions_remove_url_form
631 * Description : CGI function that displays a form for
635 * 1 : csp = Current client state (buffers, headers, etc...)
636 * 2 : rsp = http_response data structure for output
637 * 3 : parameters = map of cgi parameters
640 * f : (number) The action file identifier.
641 * v : (version) File's last-modified time
642 * p : (pattern) Line number of pattern to edit
644 * Returns : JB_ERR_OK on success
645 * JB_ERR_MEMORY on out-of-memory
646 * JB_ERR_CGI_PARAMS if the CGI parameters are not
647 * specified or not valid.
649 *********************************************************************/
650 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
651 struct http_response *rsp,
652 const struct map *parameters)
654 struct map * exports;
656 struct editable_file * file;
657 struct file_line * cur_line;
658 unsigned line_number;
659 unsigned section_start_line_number = 0;
666 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
668 return cgi_error_disabled(csp, rsp);
671 err = get_number_param(csp, parameters, "p", &patternid);
677 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
680 /* No filename specified, can't read file, modified, or out of memory. */
681 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
684 cur_line = file->lines;
686 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
688 if (cur_line->type == FILE_LINE_ACTION)
690 section_start_line_number = line_number;
692 cur_line = cur_line->next;
695 if ( (cur_line == NULL)
696 || (line_number != patternid)
698 || (cur_line->type != FILE_LINE_URL))
700 /* Invalid "patternid" parameter */
701 edit_free_file(file);
702 return JB_ERR_CGI_PARAMS;
705 if (NULL == (exports = default_exports(csp, NULL)))
707 edit_free_file(file);
708 return JB_ERR_MEMORY;
711 err = map(exports, "f", 1, stringify(file->identifier), 0);
712 if (!err) err = map(exports, "v", 1, file->version_str, 1);
713 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
714 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
715 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
716 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
718 edit_free_file(file);
726 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
730 /*********************************************************************
732 * Function : edit_write_file
734 * Description : Write a complete file to disk.
737 * 1 : file = File to write.
739 * Returns : JB_ERR_OK on success
740 * JB_ERR_FILE on error writing to file.
741 * JB_ERR_MEMORY on out of memory
743 *********************************************************************/
744 jb_err edit_write_file(struct editable_file * file)
747 struct file_line * cur_line;
748 struct stat statbuf[1];
749 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
750 digits in time_t, assuming this is a 64-bit
751 machine, plus null terminator, plus one
755 assert(file->filename);
757 if (NULL == (fp = fopen(file->filename, "wb")))
762 cur_line = file->lines;
763 while (cur_line != NULL)
767 if (fputs(cur_line->raw, fp) < 0)
775 if (cur_line->prefix)
777 if (fputs(cur_line->prefix, fp) < 0)
783 if (cur_line->unprocessed)
786 if (NULL != strchr(cur_line->unprocessed, '#'))
788 /* Must quote '#' characters */
795 /* Count number of # characters, so we know length of output string */
796 src = cur_line->unprocessed;
797 while (NULL != (src = strchr(src, '#')))
804 /* Allocate new memory for string */
805 len = strlen(cur_line->unprocessed) + (size_t)numhash;
806 str = malloc_or_die(len + 1);
808 /* Copy string but quote hashes */
809 src = cur_line->unprocessed;
817 assert(numhash >= 0);
823 assert(numhash == 0);
824 assert(strlen(str) == len);
825 assert(str == dest - len);
826 assert(src - len <= cur_line->unprocessed);
828 if ((strlen(str) != len) || (numhash != 0))
831 * Escaping didn't work as expected, go spread the news.
832 * Only reached in non-debugging builds.
834 log_error(LOG_LEVEL_ERROR,
835 "Looks like hash escaping failed. %s might be corrupted now.",
839 if (fputs(str, fp) < 0)
850 /* Can write without quoting '#' characters. */
851 if (fputs(cur_line->unprocessed, fp) < 0)
857 if (fputs(NEWLINE(file->newline), fp) < 0)
865 /* FIXME: Write data from file->data->whatever */
869 cur_line = cur_line->next;
875 /* Update the version stamp in the file structure, since we just
876 * wrote to the file & changed it's date.
878 if (stat(file->filename, statbuf) < 0)
880 /* Error, probably file not found. */
883 file->version = (unsigned)statbuf->st_mtime;
885 /* Correct file->version_str */
886 freez(file->version_str);
887 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
888 version_buf[sizeof(version_buf)-1] = '\0';
889 file->version_str = strdup_or_die(version_buf);
895 /*********************************************************************
897 * Function : edit_free_file
899 * Description : Free a complete file in memory.
902 * 1 : file = Data structure to free.
906 *********************************************************************/
907 void edit_free_file(struct editable_file * file)
911 /* Silently ignore NULL pointer */
915 edit_free_file_lines(file->lines);
916 freez(file->version_str);
918 file->parse_error_text = NULL; /* Statically allocated */
919 file->parse_error = NULL;
925 /*********************************************************************
927 * Function : edit_free_file_lines
929 * Description : Free an entire linked list of file lines.
932 * 1 : first_line = Data structure to free.
936 *********************************************************************/
937 static void edit_free_file_lines(struct file_line * first_line)
939 struct file_line * next_line;
941 while (first_line != NULL)
943 next_line = first_line->next;
944 first_line->next = NULL;
945 freez(first_line->raw);
946 freez(first_line->prefix);
947 freez(first_line->unprocessed);
948 switch(first_line->type)
950 case 0: /* special case if memory zeroed */
951 case FILE_LINE_UNPROCESSED:
952 case FILE_LINE_BLANK:
953 case FILE_LINE_ALIAS_HEADER:
954 case FILE_LINE_SETTINGS_HEADER:
955 case FILE_LINE_DESCRIPTION_HEADER:
956 case FILE_LINE_DESCRIPTION_ENTRY:
957 case FILE_LINE_ALIAS_ENTRY:
959 /* No data is stored for these */
962 case FILE_LINE_ACTION:
963 free_action(first_line->data.action);
966 case FILE_LINE_SETTINGS_ENTRY:
967 freez(first_line->data.setting.name);
968 freez(first_line->data.setting.svalue);
971 /* Should never happen */
975 first_line->type = 0; /* paranoia */
977 first_line = next_line;
982 /*********************************************************************
984 * Function : match_actions_file_header_line
986 * Description : Match an actions file {{header}} line
989 * 1 : line = String from file
990 * 2 : name = Header to match against
992 * Returns : 0 iff they match.
994 *********************************************************************/
995 static int match_actions_file_header_line(const char * line, const char * name)
1003 if ((line[0] != '{') || (line[1] != '{'))
1009 /* Look for optional whitespace */
1010 while ((*line == ' ') || (*line == '\t'))
1015 /* Look for the specified name (case-insensitive) */
1017 if (0 != strncmpic(line, name, len))
1023 /* Look for optional whitespace */
1024 while ((*line == ' ') || (*line == '\t'))
1029 /* Look for "}}" and end of string*/
1030 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1040 /*********************************************************************
1042 * Function : match_actions_file_header_line
1044 * Description : Match an actions file {{header}} line
1047 * 1 : line = String from file. Must not start with
1048 * whitespace (else infinite loop!)
1049 * 2 : pname = Destination for name
1050 * 2 : pvalue = Destination for value
1052 * Returns : JB_ERR_OK on success
1053 * JB_ERR_MEMORY on out-of-memory
1054 * JB_ERR_PARSE if there's no "=" sign, or if there's
1055 * nothing before the "=" sign (but empty
1056 * values *after* the "=" sign are legal).
1058 *********************************************************************/
1059 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1061 const char * name_end;
1062 const char * value_start;
1068 assert(*line != ' ');
1069 assert(*line != '\t');
1074 value_start = strchr(line, '=');
1075 if ((value_start == NULL) || (value_start == line))
1077 return JB_ERR_PARSE;
1080 name_end = value_start - 1;
1082 /* Eat any whitespace before the '=' */
1083 while ((*name_end == ' ') || (*name_end == '\t'))
1086 * we already know we must have at least 1 non-ws char
1087 * at start of buf - no need to check
1092 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1093 *pname = malloc_or_die(name_len + 1);
1094 strncpy(*pname, line, name_len);
1095 (*pname)[name_len] = '\0';
1097 /* Eat any the whitespace after the '=' */
1099 while ((*value_start == ' ') || (*value_start == '\t'))
1104 if (NULL == (*pvalue = strdup(value_start)))
1108 return JB_ERR_MEMORY;
1115 /*********************************************************************
1117 * Function : edit_parse_actions_file
1119 * Description : Parse an actions file in memory.
1121 * Passed linked list must have the "data" member
1122 * zeroed, and must contain valid "next" and
1123 * "unprocessed" fields. The "raw" and "prefix"
1124 * fields are ignored, and "type" is just overwritten.
1126 * Note that on error the file may have been
1130 * 1 : file = Actions file to be parsed in-place.
1132 * Returns : JB_ERR_OK on success
1133 * JB_ERR_MEMORY on out-of-memory
1134 * JB_ERR_PARSE on error
1136 *********************************************************************/
1137 jb_err edit_parse_actions_file(struct editable_file * file)
1139 struct file_line * cur_line;
1141 const char * text; /* Text from a line */
1142 char * name; /* For lines of the form name=value */
1143 char * value; /* For lines of the form name=value */
1144 struct action_alias * alias_list = NULL;
1145 jb_err err = JB_ERR_OK;
1147 /* alias_list contains the aliases defined in this file.
1148 * It might be better to use the "file_line.data" fields
1149 * in the relevant places instead.
1152 cur_line = file->lines;
1154 /* A note about blank line support: Blank lines should only
1155 * ever occur as the last line in the file. This function
1156 * is more forgiving than that - FILE_LINE_BLANK can occur
1160 /* Skip leading blanks. Should only happen if file is
1161 * empty (which is valid, but pointless).
1163 while ((cur_line != NULL)
1164 && (cur_line->unprocessed[0] == '\0'))
1167 cur_line->type = FILE_LINE_BLANK;
1168 cur_line = cur_line->next;
1171 if ((cur_line != NULL)
1172 && (cur_line->unprocessed[0] != '{'))
1174 /* File doesn't start with a header */
1175 file->parse_error = cur_line;
1176 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1177 return JB_ERR_PARSE;
1180 if ((cur_line != NULL) && (0 ==
1181 match_actions_file_header_line(cur_line->unprocessed, "settings")))
1183 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1185 cur_line = cur_line->next;
1186 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1188 if (cur_line->unprocessed[0])
1190 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1192 err = split_line_on_equals(cur_line->unprocessed,
1193 &cur_line->data.setting.name,
1194 &cur_line->data.setting.svalue);
1195 if (err == JB_ERR_MEMORY)
1199 else if (err != JB_ERR_OK)
1201 /* Line does not contain a name=value pair */
1202 file->parse_error = cur_line;
1203 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1204 return JB_ERR_PARSE;
1209 cur_line->type = FILE_LINE_BLANK;
1211 cur_line = cur_line->next;
1215 if ((cur_line != NULL) && (0 ==
1216 match_actions_file_header_line(cur_line->unprocessed, "description")))
1218 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1220 cur_line = cur_line->next;
1221 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1223 if (cur_line->unprocessed[0])
1225 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1229 cur_line->type = FILE_LINE_BLANK;
1231 cur_line = cur_line->next;
1235 if ((cur_line != NULL) && (0 ==
1236 match_actions_file_header_line(cur_line->unprocessed, "alias")))
1238 cur_line->type = FILE_LINE_ALIAS_HEADER;
1240 cur_line = cur_line->next;
1241 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1243 if (cur_line->unprocessed[0])
1245 /* define an alias */
1246 struct action_alias * new_alias;
1248 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1250 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1251 if (err == JB_ERR_MEMORY)
1253 free_alias_list(alias_list);
1256 else if (err != JB_ERR_OK)
1258 /* Line does not contain a name=value pair */
1259 file->parse_error = cur_line;
1260 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1261 free_alias_list(alias_list);
1262 return JB_ERR_PARSE;
1265 new_alias = zalloc_or_die(sizeof(*new_alias));
1267 err = get_actions(value, alias_list, new_alias->action);
1270 /* Invalid action or out of memory */
1274 free_alias_list(alias_list);
1275 if (err == JB_ERR_MEMORY)
1281 /* Line does not contain a name=value pair */
1282 file->parse_error = cur_line;
1283 file->parse_error_text = "This alias does not specify a valid set of actions.";
1284 return JB_ERR_PARSE;
1290 new_alias->name = name;
1293 new_alias->next = alias_list;
1294 alias_list = new_alias;
1298 cur_line->type = FILE_LINE_BLANK;
1300 cur_line = cur_line->next;
1304 /* Header done, process the main part of the file */
1305 while (cur_line != NULL)
1307 /* At this point, (cur_line->unprocessed[0] == '{') */
1308 assert(cur_line->unprocessed[0] == '{');
1309 text = cur_line->unprocessed + 1;
1310 len = strlen(text) - 1;
1311 if (text[len] != '}')
1313 /* No closing } on header */
1314 free_alias_list(alias_list);
1315 file->parse_error = cur_line;
1316 file->parse_error_text = "Headers starting with '{' must have a "
1317 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1318 "must close with two brackets ('}}').";
1319 return JB_ERR_PARSE;
1324 /* An invalid {{ header. */
1325 free_alias_list(alias_list);
1326 file->parse_error = cur_line;
1327 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1328 "Please remember that the system (two-bracket) headers must "
1329 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1330 "and must appear before any actions (one-bracket) headers. "
1331 "Also note that system headers may not be repeated.";
1332 return JB_ERR_PARSE;
1335 while ((*text == ' ') || (*text == '\t'))
1340 while ((len > (size_t)0)
1341 && ((text[len - 1] == ' ')
1342 || (text[len - 1] == '\t')))
1347 cur_line->type = FILE_LINE_ACTION;
1349 /* Remove {} and make copy */
1350 value = malloc_or_die(len + 1);
1351 strncpy(value, text, len);
1355 err = get_actions(value, alias_list, cur_line->data.action);
1358 /* Invalid action or out of memory */
1360 free_alias_list(alias_list);
1361 if (err == JB_ERR_MEMORY)
1367 /* Line does not contain a name=value pair */
1368 file->parse_error = cur_line;
1369 file->parse_error_text = "This header does not specify a valid set of actions.";
1370 return JB_ERR_PARSE;
1374 /* Done with string - it was clobbered anyway */
1377 /* Process next line */
1378 cur_line = cur_line->next;
1380 /* Loop processing URL patterns */
1381 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1383 if (cur_line->unprocessed[0])
1385 /* Could parse URL here, but this isn't currently needed */
1387 cur_line->type = FILE_LINE_URL;
1391 cur_line->type = FILE_LINE_BLANK;
1393 cur_line = cur_line->next;
1395 } /* End main while(cur_line != NULL) loop */
1397 free_alias_list(alias_list);
1403 /*********************************************************************
1405 * Function : edit_read_file_lines
1407 * Description : Read all the lines of a file into memory.
1408 * Handles whitespace, comments and line continuation.
1411 * 1 : fp = File to read from. On return, this will be
1412 * at EOF but it will not have been closed.
1413 * 2 : pfile = Destination for a linked list of file_lines.
1414 * Will be set to NULL on error.
1415 * 3 : newline = How to handle newlines.
1417 * Returns : JB_ERR_OK on success
1418 * JB_ERR_MEMORY on out-of-memory
1420 *********************************************************************/
1421 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1423 struct file_line * first_line; /* Keep for return value or to free */
1424 struct file_line * cur_line; /* Current line */
1425 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1433 cur_line = first_line = zalloc_or_die(sizeof(struct file_line));
1435 cur_line->type = FILE_LINE_UNPROCESSED;
1437 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1440 /* Out of memory or empty file. */
1441 /* Note that empty file is not an error we propagate up */
1443 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1448 prev_line = cur_line;
1449 cur_line = prev_line->next = zalloc_or_die(sizeof(struct file_line));
1451 cur_line->type = FILE_LINE_UNPROCESSED;
1453 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1454 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1457 edit_free_file_lines(first_line);
1458 return JB_ERR_MEMORY;
1462 while (rval != JB_ERR_FILE);
1466 /* We allocated one too many - free it */
1467 prev_line->next = NULL;
1470 *pfile = first_line;
1475 /*********************************************************************
1477 * Function : edit_read_file
1479 * Description : Read a complete file into memory.
1480 * Handles CGI parameter parsing. If requested, also
1481 * checks the file's modification timestamp.
1484 * 1 : csp = Current client state (buffers, headers, etc...)
1485 * 2 : parameters = map of cgi parameters.
1486 * 3 : require_version = true to check "ver" parameter.
1487 * 4 : pfile = Destination for the file. Will be set
1491 * f : The action file identifier.
1492 * ver : (Only if require_version is nonzero)
1493 * Timestamp of the actions file. If wrong, this
1494 * function fails with JB_ERR_MODIFIED.
1496 * Returns : JB_ERR_OK on success
1497 * JB_ERR_MEMORY on out-of-memory
1498 * JB_ERR_CGI_PARAMS if "filename" was not specified
1500 * JB_ERR_FILE if the file cannot be opened or
1502 * JB_ERR_MODIFIED if version checking was requested and
1503 * failed - the file was modified outside
1504 * of this CGI editor instance.
1506 *********************************************************************/
1507 jb_err edit_read_file(struct client_state *csp,
1508 const struct map *parameters,
1509 int require_version,
1510 struct editable_file **pfile)
1512 struct file_line * lines;
1515 const char *filename = NULL;
1516 struct editable_file * file;
1517 unsigned version = 0;
1518 struct stat statbuf[1];
1519 char version_buf[22];
1520 int newline = NEWLINE_UNKNOWN;
1529 err = get_number_param(csp, parameters, "f", &i);
1530 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1532 filename = csp->config->actions_file[i];
1534 else if (JB_ERR_CGI_PARAMS == err)
1537 * Probably an old-school URL like
1538 * http://config.privoxy.org/edit-actions-list?f=default
1540 get_file_name_param(csp, parameters, "f", &filename);
1543 if (NULL == filename || stat(filename, statbuf) < 0)
1545 /* Error, probably file not found. */
1548 version = (unsigned) statbuf->st_mtime;
1550 if (require_version)
1552 unsigned specified_version;
1553 err = get_number_param(csp, parameters, "v", &specified_version);
1559 if (version != specified_version)
1561 return JB_ERR_MODIFIED;
1565 if (NULL == (fp = fopen(filename,"rb")))
1570 err = edit_read_file_lines(fp, &lines, &newline);
1579 file = zalloc_or_die(sizeof(*file));
1581 file->lines = lines;
1582 file->newline = newline;
1583 file->filename = filename;
1584 file->version = version;
1585 file->identifier = i;
1587 /* Correct file->version_str */
1588 freez(file->version_str);
1589 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1590 version_buf[sizeof(version_buf)-1] = '\0';
1591 file->version_str = strdup_or_die(version_buf);
1598 /*********************************************************************
1600 * Function : edit_read_actions_file
1602 * Description : Read a complete actions file into memory.
1603 * Handles CGI parameter parsing. If requested, also
1604 * checks the file's modification timestamp.
1606 * If this function detects an error in the categories
1607 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1608 * then it handles it by filling in the specified
1609 * response structure and returning JB_ERR_FILE.
1612 * 1 : csp = Current client state (buffers, headers, etc...)
1613 * 2 : rsp = HTTP response. Only filled in on error.
1614 * 2 : parameters = map of cgi parameters.
1615 * 3 : require_version = true to check "ver" parameter.
1616 * 4 : pfile = Destination for the file. Will be set
1620 * f : The actions file identifier.
1621 * ver : (Only if require_version is nonzero)
1622 * Timestamp of the actions file. If wrong, this
1623 * function fails with JB_ERR_MODIFIED.
1625 * Returns : JB_ERR_OK on success
1626 * JB_ERR_MEMORY on out-of-memory
1627 * JB_ERR_CGI_PARAMS if "filename" was not specified
1629 * JB_ERR_FILE if the file does not contain valid data,
1630 * or if file cannot be opened or
1631 * contains no data, or if version
1632 * checking was requested and failed.
1634 *********************************************************************/
1635 jb_err edit_read_actions_file(struct client_state *csp,
1636 struct http_response *rsp,
1637 const struct map *parameters,
1638 int require_version,
1639 struct editable_file **pfile)
1642 struct editable_file *file;
1643 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
1651 err = edit_read_file(csp, parameters, require_version, &file);
1654 /* Try to handle if possible */
1655 if (err == JB_ERR_FILE)
1657 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1659 else if (err == JB_ERR_MODIFIED)
1661 assert(require_version);
1662 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1663 log_error(LOG_LEVEL_ERROR,
1664 "Blocking CGI edit request due to modification time mismatch.");
1665 if (acceptable_failures > 0)
1667 log_error(LOG_LEVEL_INFO,
1668 "The CGI editor will be turned off after another %d mismatche(s).",
1669 acceptable_failures);
1670 acceptable_failures--;
1674 log_error(LOG_LEVEL_INFO,
1675 "Timestamp mismatch limit reached, turning CGI editor off. "
1676 "Reload the configuration file to re-enable it.");
1677 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
1680 if (err == JB_ERR_OK)
1683 * Signal to higher-level CGI code that there was a problem but we
1684 * handled it, they should just return JB_ERR_OK.
1691 err = edit_parse_actions_file(file);
1694 if (err == JB_ERR_PARSE)
1696 err = cgi_error_parse(csp, rsp, file);
1697 if (err == JB_ERR_OK)
1700 * Signal to higher-level CGI code that there was a problem but we
1701 * handled it, they should just return JB_ERR_OK.
1706 edit_free_file(file);
1715 /*********************************************************************
1717 * Function : get_file_name_param
1719 * Description : Get the name of the file to edit from the parameters
1720 * passed to a CGI function using the old syntax.
1721 * This function handles security checks and only
1722 * accepts files that Privoxy already knows.
1725 * 1 : csp = Current client state (buffers, headers, etc...)
1726 * 2 : parameters = map of cgi parameters
1727 * 3 : param_name = The name of the parameter to read
1728 * 4 : pfilename = pointer to the filename in
1729 * csp->config->actions_file[] if found. Set to NULL on error.
1731 * Returns : JB_ERR_OK on success
1732 * JB_ERR_MEMORY on out-of-memory
1733 * JB_ERR_CGI_PARAMS if "filename" was not specified
1736 *********************************************************************/
1737 static jb_err get_file_name_param(struct client_state *csp,
1738 const struct map *parameters,
1739 const char *param_name,
1740 const char **pfilename)
1743 const char suffix[] = ".action";
1758 param = lookup(parameters, param_name);
1761 return JB_ERR_CGI_PARAMS;
1764 len = strlen(param);
1765 if (len >= FILENAME_MAX)
1768 return JB_ERR_CGI_PARAMS;
1772 * Check every character to see if it's legal.
1773 * Totally unnecessary but we do it anyway.
1776 while ((ch = *s++) != '\0')
1778 if ( ((ch < 'A') || (ch > 'Z'))
1779 && ((ch < 'a') || (ch > 'z'))
1780 && ((ch < '0') || (ch > '9'))
1784 /* Probable hack attempt. */
1785 return JB_ERR_CGI_PARAMS;
1789 /* Append extension */
1790 name_size = len + strlen(suffix) + 1;
1791 name = malloc_or_die(name_size);
1792 strlcpy(name, param, name_size);
1793 strlcat(name, suffix, name_size);
1796 fullpath = make_path(csp->config->confdir, name);
1799 if (fullpath == NULL)
1801 return JB_ERR_MEMORY;
1804 /* Check if the file is known */
1805 for (i = 0; i < MAX_AF_FILES; i++)
1807 if (NULL != csp->config->actions_file[i] &&
1808 !strcmp(fullpath, csp->config->actions_file[i]))
1811 *pfilename = csp->config->actions_file[i];
1819 return JB_ERR_CGI_PARAMS;
1823 /*********************************************************************
1825 * Function : get_url_spec_param
1827 * Description : Get a URL pattern from the parameters
1828 * passed to a CGI function. Removes leading/trailing
1829 * spaces and validates it.
1832 * 1 : csp = Current client state (buffers, headers, etc...)
1833 * 2 : parameters = map of cgi parameters
1834 * 3 : name = Name of CGI parameter to read
1835 * 4 : pvalue = destination for value. Will be malloc()'d.
1836 * Set to NULL on error.
1838 * Returns : JB_ERR_OK on success
1839 * JB_ERR_MEMORY on out-of-memory
1840 * JB_ERR_CGI_PARAMS if the parameter was not specified
1843 *********************************************************************/
1844 static jb_err get_url_spec_param(struct client_state *csp,
1845 const struct map *parameters,
1849 const char *orig_param;
1852 struct pattern_spec compiled[1];
1862 orig_param = lookup(parameters, name);
1865 return JB_ERR_CGI_PARAMS;
1868 /* Copy and trim whitespace */
1869 param = strdup(orig_param);
1872 return JB_ERR_MEMORY;
1876 /* Must be non-empty, and can't allow 1st character to be '{' */
1877 if (param[0] == '\0' || param[0] == '{')
1880 return JB_ERR_CGI_PARAMS;
1883 /* Check for embedded newlines */
1884 for (s = param; *s != '\0'; s++)
1886 if ((*s == '\r') || (*s == '\n'))
1889 return JB_ERR_CGI_PARAMS;
1893 /* Check that regex is valid */
1898 return JB_ERR_MEMORY;
1900 err = create_pattern_spec(compiled, s);
1902 free_pattern_spec(compiled);
1906 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1909 if (param[strlen(param) - 1] == '\\')
1912 * Must protect trailing '\\' from becoming line continuation character.
1913 * Two methods: 1) If it's a domain only, add a trailing '/'.
1914 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1916 if (strchr(param, '/') == NULL)
1918 err = string_append(¶m, "/");
1922 err = string_append(¶m, "(?:)");
1929 /* Check that the modified regex is valid */
1934 return JB_ERR_MEMORY;
1936 err = create_pattern_spec(compiled, s);
1938 free_pattern_spec(compiled);
1942 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1950 /*********************************************************************
1952 * Function : map_radio
1954 * Description : Map a set of radio button values. E.g. if you have
1955 * 3 radio buttons, declare them as:
1956 * <option type="radio" name="xyz" @xyz-a@>
1957 * <option type="radio" name="xyz" @xyz-b@>
1958 * <option type="radio" name="xyz" @xyz-c@>
1959 * Then map one of the @xyz-?@ variables to "checked"
1960 * and all the others to empty by calling:
1961 * map_radio(exports, "xyz", "abc", sel)
1962 * Where 'sel' is 'a', 'b', or 'c'.
1965 * 1 : exports = Exports map to modify.
1966 * 2 : optionname = name for map
1967 * 3 : values = null-terminated list of values;
1968 * 4 : value = Selected value.
1970 * CGI Parameters : None
1972 * Returns : JB_ERR_OK on success
1973 * JB_ERR_MEMORY on out-of-memory
1975 *********************************************************************/
1976 static jb_err map_radio(struct map * exports,
1977 const char * optionname,
1978 const char * values,
1984 const size_t len = strlen(optionname);
1985 const size_t buf_size = len + 3;
1991 buf = malloc_or_die(buf_size);
1993 strlcpy(buf, optionname, buf_size);
1995 /* XXX: this looks ... interesting */
2000 while ((c = *values++) != '\0')
2005 if (map(exports, buf, 1, "", 1))
2007 return JB_ERR_MEMORY;
2013 return map(exports, buf, 0, "checked", 1);
2017 /*********************************************************************
2019 * Function : cgi_error_modified
2021 * Description : CGI function that is called when a file is modified
2022 * outside the CGI editor.
2025 * 1 : csp = Current client state (buffers, headers, etc...)
2026 * 2 : rsp = http_response data structure for output
2027 * 3 : filename = The file that was modified.
2029 * CGI Parameters : none
2031 * Returns : JB_ERR_OK on success
2032 * JB_ERR_MEMORY on out-of-memory error.
2034 *********************************************************************/
2035 jb_err cgi_error_modified(struct client_state *csp,
2036 struct http_response *rsp,
2037 const char *filename)
2039 struct map *exports;
2046 if (NULL == (exports = default_exports(csp, NULL)))
2048 return JB_ERR_MEMORY;
2051 err = map(exports, "f", 1, html_encode(filename), 0);
2058 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2062 /*********************************************************************
2064 * Function : cgi_error_parse
2066 * Description : CGI function that is called when a file cannot
2067 * be parsed by the CGI editor.
2070 * 1 : csp = Current client state (buffers, headers, etc...)
2071 * 2 : rsp = http_response data structure for output
2072 * 3 : file = The file that was modified.
2074 * CGI Parameters : none
2076 * Returns : JB_ERR_OK on success
2077 * JB_ERR_MEMORY on out-of-memory error.
2079 *********************************************************************/
2080 jb_err cgi_error_parse(struct client_state *csp,
2081 struct http_response *rsp,
2082 struct editable_file *file)
2084 struct map *exports;
2086 struct file_line *cur_line;
2092 if (NULL == (exports = default_exports(csp, NULL)))
2094 return JB_ERR_MEMORY;
2097 err = map(exports, "f", 1, stringify(file->identifier), 0);
2098 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2100 cur_line = file->parse_error;
2103 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2104 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2112 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2116 /*********************************************************************
2118 * Function : cgi_error_file
2120 * Description : CGI function that is called when a file cannot be
2121 * opened by the CGI editor.
2124 * 1 : csp = Current client state (buffers, headers, etc...)
2125 * 2 : rsp = http_response data structure for output
2126 * 3 : filename = The file that was modified.
2128 * CGI Parameters : none
2130 * Returns : JB_ERR_OK on success
2131 * JB_ERR_MEMORY on out-of-memory error.
2133 *********************************************************************/
2134 jb_err cgi_error_file(struct client_state *csp,
2135 struct http_response *rsp,
2136 const char *filename)
2138 struct map *exports;
2145 if (NULL == (exports = default_exports(csp, NULL)))
2147 return JB_ERR_MEMORY;
2150 err = map(exports, "f", 1, html_encode(filename), 0);
2157 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2161 /*********************************************************************
2163 * Function : cgi_error_file_read_only
2165 * Description : CGI function that is called when a file cannot be
2166 * opened for writing by the CGI editor.
2169 * 1 : csp = Current client state (buffers, headers, etc...)
2170 * 2 : rsp = http_response data structure for output
2171 * 3 : filename = The file that we can't write to
2173 * CGI Parameters : none
2175 * Returns : JB_ERR_OK on success
2176 * JB_ERR_MEMORY on out-of-memory error.
2178 *********************************************************************/
2179 jb_err cgi_error_file_read_only(struct client_state *csp,
2180 struct http_response *rsp,
2181 const char *filename)
2183 struct map *exports;
2190 if (NULL == (exports = default_exports(csp, NULL)))
2192 return JB_ERR_MEMORY;
2195 err = map(exports, "f", 1, html_encode(filename), 0);
2202 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2206 /*********************************************************************
2208 * Function : cgi_edit_actions
2210 * Description : CGI function that allows the user to choose which
2211 * actions file to edit.
2214 * 1 : csp = Current client state (buffers, headers, etc...)
2215 * 2 : rsp = http_response data structure for output
2216 * 3 : parameters = map of cgi parameters
2218 * CGI Parameters : None
2220 * Returns : JB_ERR_OK on success
2221 * JB_ERR_MEMORY on out-of-memory error
2223 *********************************************************************/
2224 jb_err cgi_edit_actions(struct client_state *csp,
2225 struct http_response *rsp,
2226 const struct map *parameters)
2230 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2232 return cgi_error_disabled(csp, rsp);
2235 /* FIXME: Incomplete */
2237 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2242 /*********************************************************************
2244 * Function : cgi_edit_actions_list
2246 * Description : CGI function that edits the actions list.
2247 * FIXME: This function shouldn't FATAL ever.
2248 * FIXME: This function doesn't check the retval of map()
2250 * 1 : csp = Current client state (buffers, headers, etc...)
2251 * 2 : rsp = http_response data structure for output
2252 * 3 : parameters = map of cgi parameters
2254 * CGI Parameters : filename
2256 * Returns : JB_ERR_OK on success
2257 * JB_ERR_MEMORY on out-of-memory
2258 * JB_ERR_FILE if the file cannot be opened or
2260 * JB_ERR_CGI_PARAMS if "filename" was not specified
2263 *********************************************************************/
2264 jb_err cgi_edit_actions_list(struct client_state *csp,
2265 struct http_response *rsp,
2266 const struct map *parameters)
2268 char * section_template;
2269 char * url_template;
2274 struct map * exports;
2275 struct map * section_exports;
2276 struct map * url_exports;
2277 struct editable_file * file;
2278 struct file_line * cur_line;
2279 unsigned line_number = 0;
2280 unsigned prev_section_line_number = ((unsigned) (-1));
2282 struct file_list * fl;
2283 struct url_actions * b;
2284 char * buttons = NULL;
2287 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2289 return cgi_error_disabled(csp, rsp);
2292 if (NULL == (exports = default_exports(csp, NULL)))
2294 return JB_ERR_MEMORY;
2297 /* Load actions file */
2298 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2301 /* No filename specified, can't read file, or out of memory. */
2303 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2306 /* Find start of actions in file */
2307 cur_line = file->lines;
2309 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2311 cur_line = cur_line->next;
2316 * Conventional actions files should have a match all block
2318 * cur_line = {...global actions...}
2319 * cur_line->next = /
2320 * cur_line->next->next = {...actions...} or EOF
2322 if ( (cur_line != NULL)
2323 && (cur_line->type == FILE_LINE_ACTION)
2324 && (cur_line->next != NULL)
2325 && (cur_line->next->type == FILE_LINE_URL)
2326 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2327 && ( (cur_line->next->next == NULL)
2328 || (cur_line->next->next->type != FILE_LINE_URL)
2332 * Generate string with buttons to set actions for "/" to
2333 * any predefined set of actions (named standard.*, probably
2334 * residing in standard.action).
2337 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2340 edit_free_file(file);
2342 if (err == JB_ERR_FILE)
2344 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2349 err = template_fill(§ion_template, exports);
2352 edit_free_file(file);
2357 buttons = strdup("");
2358 for (i = 0; i < MAX_AF_FILES; i++)
2360 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2362 for (b = b->next; NULL != b; b = b->next)
2364 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2369 free(section_template);
2370 edit_free_file(file);
2372 return JB_ERR_MEMORY;
2375 section_exports = new_map();
2376 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2378 if (err || (NULL == (s = strdup(section_template))))
2380 free_map(section_exports);
2382 free(section_template);
2383 edit_free_file(file);
2385 return JB_ERR_MEMORY;
2388 if (!err) err = template_fill(&s, section_exports);
2389 free_map(section_exports);
2390 if (!err) err = string_join(&buttons, s);
2395 freez(section_template);
2396 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2399 * Conventional actions file, supply extra editing help.
2400 * (e.g. don't allow them to make it an unconventional one).
2402 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2404 snprintf(buf, sizeof(buf), "%u", line_number);
2405 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2406 snprintf(buf, sizeof(buf), "%u", line_number + 2);
2407 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2408 if (!err) err = map(exports, "all-urls-actions", 1,
2409 actions_to_html(csp, cur_line->data.action), 0);
2411 /* Skip the 2 lines */
2412 cur_line = cur_line->next->next;
2416 * Note that prev_section_line_number is NOT set here.
2417 * This is deliberate and not a bug. It stops a "Move up"
2418 * option appearing on the next section. Clicking "Move
2419 * up" would make the actions file unconventional, which
2420 * we don't want, so we hide this option.
2426 * Non-standard actions file - does not begin with
2427 * the "All URLs" section.
2429 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2432 /* Set up global exports */
2434 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2435 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2436 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2438 /* Discourage private additions to default.action */
2440 if (!err) err = map_conditional(exports, "default-action",
2441 (strstr("default.action", file->filename) != NULL));
2444 edit_free_file(file);
2449 /* Should do all global exports above this point */
2451 /* Load templates */
2453 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2456 edit_free_file(file);
2458 if (err == JB_ERR_FILE)
2460 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2465 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2468 free(section_template);
2469 edit_free_file(file);
2471 if (err == JB_ERR_FILE)
2473 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2478 err = template_fill(§ion_template, exports);
2482 edit_free_file(file);
2487 err = template_fill(&url_template, exports);
2490 free(section_template);
2491 edit_free_file(file);
2496 if (NULL == (sections = strdup("")))
2498 free(section_template);
2500 edit_free_file(file);
2502 return JB_ERR_MEMORY;
2505 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2507 section_exports = new_map();
2509 snprintf(buf, sizeof(buf), "%u", line_number);
2510 err = map(section_exports, "s", 1, buf, 1);
2511 if (!err) err = map(section_exports, "actions", 1,
2512 actions_to_html(csp, cur_line->data.action), 0);
2515 && (cur_line->next != NULL)
2516 && (cur_line->next->type == FILE_LINE_URL))
2518 /* This section contains at least one URL, don't allow delete */
2519 err = map_block_killer(section_exports, "empty-section");
2523 if (!err) err = map_block_keep(section_exports, "empty-section");
2526 if (prev_section_line_number != ((unsigned)(-1)))
2528 /* Not last section */
2529 snprintf(buf, sizeof(buf), "%u", prev_section_line_number);
2530 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2531 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2536 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2538 prev_section_line_number = line_number;
2543 free(section_template);
2545 edit_free_file(file);
2547 free_map(section_exports);
2551 /* Should do all section-specific exports above this point */
2553 if (NULL == (urls = strdup("")))
2556 free(section_template);
2558 edit_free_file(file);
2560 free_map(section_exports);
2561 return JB_ERR_MEMORY;
2566 cur_line = cur_line->next;
2569 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2571 url_exports = new_map();
2573 snprintf(buf, sizeof(buf), "%u", line_number);
2574 err = map(url_exports, "p", 1, buf, 1);
2576 snprintf(buf, sizeof(buf), "%d", url_1_2);
2577 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2579 if (!err) err = map(url_exports, "url-html", 1,
2580 html_encode(cur_line->unprocessed), 0);
2581 if (!err) err = map(url_exports, "url", 1,
2582 url_encode(cur_line->unprocessed), 0);
2588 free(section_template);
2590 edit_free_file(file);
2592 free_map(section_exports);
2593 free_map(url_exports);
2597 if (NULL == (s = strdup(url_template)))
2601 free(section_template);
2603 edit_free_file(file);
2605 free_map(section_exports);
2606 free_map(url_exports);
2607 return JB_ERR_MEMORY;
2610 err = template_fill(&s, section_exports);
2611 if (!err) err = template_fill(&s, url_exports);
2612 if (!err) err = string_append(&urls, s);
2614 free_map(url_exports);
2621 free(section_template);
2623 edit_free_file(file);
2625 free_map(section_exports);
2629 url_1_2 = 3 - url_1_2;
2631 cur_line = cur_line->next;
2635 err = map(section_exports, "urls", 1, urls, 0);
2637 /* Could also do section-specific exports here, but it wouldn't be as fast */
2639 snprintf(buf, sizeof(buf), "%u", line_number);
2640 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2642 if ((cur_line != NULL)
2643 && (cur_line->type == FILE_LINE_ACTION))
2645 /* Not last section */
2646 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2651 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2657 free(section_template);
2659 edit_free_file(file);
2661 free_map(section_exports);
2665 if (NULL == (s = strdup(section_template)))
2668 free(section_template);
2670 edit_free_file(file);
2672 free_map(section_exports);
2673 return JB_ERR_MEMORY;
2676 err = template_fill(&s, section_exports);
2677 if (!err) err = string_append(§ions, s);
2680 free_map(section_exports);
2685 free(section_template);
2687 edit_free_file(file);
2693 edit_free_file(file);
2694 free(section_template);
2697 err = map(exports, "sections", 1, sections, 0);
2704 /* Could also do global exports here, but it wouldn't be as fast */
2706 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2710 /*********************************************************************
2712 * Function : cgi_edit_actions_for_url
2714 * Description : CGI function that edits the Actions list.
2717 * 1 : csp = Current client state (buffers, headers, etc...)
2718 * 2 : rsp = http_response data structure for output
2719 * 3 : parameters = map of cgi parameters
2721 * CGI Parameters : None
2723 * Returns : JB_ERR_OK on success
2724 * JB_ERR_MEMORY on out-of-memory
2725 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2726 * specified or not valid.
2728 *********************************************************************/
2729 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2730 struct http_response *rsp,
2731 const struct map *parameters)
2733 struct map * exports;
2734 char *filter_template;
2736 struct editable_file * file;
2737 struct file_line * cur_line;
2738 unsigned line_number;
2740 struct re_filterfile_spec *filter_group;
2741 int i, have_filters = 0;
2743 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2745 return cgi_error_disabled(csp, rsp);
2748 err = get_number_param(csp, parameters, "s", §ionid);
2754 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2757 /* No filename specified, can't read file, modified, or out of memory. */
2758 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2761 cur_line = file->lines;
2763 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2765 cur_line = cur_line->next;
2768 if ( (cur_line == NULL)
2769 || (line_number != sectionid)
2771 || (cur_line->type != FILE_LINE_ACTION))
2773 /* Invalid "sectionid" parameter */
2774 edit_free_file(file);
2775 return JB_ERR_CGI_PARAMS;
2778 if (NULL == (exports = default_exports(csp, NULL)))
2780 edit_free_file(file);
2781 return JB_ERR_MEMORY;
2784 err = template_load(csp, &filter_template, "edit-actions-for-url-string-action", 0);
2787 edit_free_file(file);
2789 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-string-action");
2792 err = map(exports, "f", 1, stringify(file->identifier), 0);
2793 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2794 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2796 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2798 for (i = 0; !err && i < SZ(string_action_type_info); i++)
2800 err = action_render_string_actions_template(exports, cur_line->data.action, filter_template,
2801 &string_action_type_info[i]);
2803 freez(filter_template);
2806 * XXX: Some browsers (at least IE6 and IE7) have an artificial URL
2807 * length limitation and ignore clicks on the Submit buttons if
2808 * the resulting GET URL would be longer than their limit.
2810 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
2811 * reached this limit and action editing stopped working in these
2812 * browsers (BR #1570678).
2814 * The config option split-large-forms works around this browser
2815 * bug (HTTP has no URL length limitation) by dividing the action
2816 * list form into multiple smaller ones. It means the URLs are shorter
2817 * and work in broken browsers as well, but the user can no longer change
2818 * all actions with one submit.
2820 * A better solution would be to switch to POST requests,
2821 * but this will do for now.
2823 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
2825 /* Generate multiple smaller form by killing the big one. */
2826 err = map_block_killer(exports, "one-form-only");
2830 /* Default: Generate one large form by killing the smaller ones. */
2831 err = map_block_killer(exports, "multiple-forms");
2834 for (i = 0; i < MAX_AF_FILES; i++)
2836 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2838 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
2844 #ifndef FEATURE_EXTERNAL_FILTERS
2845 if (!err) err = map_block_killer(exports, "external-content-filters");
2847 #ifndef FEATURE_HTTPS_INSPECTION
2848 if (!err) err = map_block_killer(exports, "https-inspection");
2853 edit_free_file(file);
2858 if (0 == have_filters)
2860 err = map(exports, "filter-params", 1, "", 1);
2865 * List available filters and their settings.
2867 int filter_identifier = 0;
2868 char *prepared_templates[MAX_FILTER_TYPES];
2870 for (i = 0; i < MAX_FILTER_TYPES; i++)
2872 prepared_templates[i] = strdup("");
2875 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2878 edit_free_file(file);
2880 if (err == JB_ERR_FILE)
2882 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2887 err = template_fill(&filter_template, exports);
2889 for (i = 0; i < MAX_AF_FILES; i++)
2891 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2893 filter_group = csp->rlist[i]->f;
2894 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2896 char current_mode = 'x';
2898 struct list_entry *filter_name;
2899 struct map *line_exports;
2900 const enum filter_type type = filter_group->type;
2901 const int multi_action_index = action_type_info[type].multi_action_index;
2903 assert(type < MAX_FILTER_TYPES);
2905 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
2906 while ((filter_name != NULL)
2907 && (0 != strcmp(filter_group->name, filter_name->str)))
2909 filter_name = filter_name->next;
2912 if (filter_name != NULL)
2918 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
2919 while ((filter_name != NULL)
2920 && (0 != strcmp(filter_group->name, filter_name->str)))
2922 filter_name = filter_name->next;
2924 if (filter_name != NULL)
2930 /* Generate a unique serial number */
2931 snprintf(number, sizeof(number), "%x", filter_identifier++);
2932 number[sizeof(number) - 1] = '\0';
2934 line_exports = new_map();
2935 if (line_exports == NULL)
2937 err = JB_ERR_MEMORY;
2943 if (!err) err = map(line_exports, "index", 1, number, 1);
2944 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2945 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2946 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2947 if (!err) err = map(line_exports, "filter-type", 1, action_type_info[type].type, 1);
2948 if (!err) err = map(line_exports, "abbr-action-type", 1, action_type_info[type].abbr_type, 1);
2949 if (!err) err = map(line_exports, "anchor", 1, action_type_info[type].anchor, 1);
2953 filter_line = strdup(filter_template);
2954 if (filter_line == NULL) err = JB_ERR_MEMORY;
2956 if (!err) err = template_fill(&filter_line, line_exports);
2957 if (!err) err = string_join(&prepared_templates[type], filter_line);
2959 free_map(line_exports);
2964 freez(filter_template);
2966 /* Replace all filter macros with the aggregated templates */
2967 for (i = 0; i < MAX_FILTER_TYPES; i++)
2970 err = map(exports, action_type_info[i].macro_name, 1, prepared_templates[i], 0);
2975 /* Free aggregated templates */
2976 for (i = 0; i < MAX_FILTER_TYPES; i++)
2978 freez(prepared_templates[i]);
2983 /* Check or uncheck the "disable all of this type" radio buttons. */
2984 for (i = 0; i < MAX_FILTER_TYPES; i++)
2986 const int a = action_type_info[i].multi_action_index;
2987 const int disable_all = cur_line->data.action->multi_remove_all[a];
2989 err = map_radio(exports, action_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
2992 edit_free_file(file);
3000 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3004 /*********************************************************************
3006 * Function : get_number_of_filters
3008 * Description : Counts the number of filter available.
3011 * 1 : csp = Current client state (buffers, headers, etc...)
3013 * Returns : Number of filters available.
3015 *********************************************************************/
3016 static int get_number_of_filters(const struct client_state *csp)
3019 struct re_filterfile_spec *b;
3020 struct file_list *fl;
3021 int number_of_filters = 0;
3023 for (i = 0; i < MAX_AF_FILES; i++)
3026 if ((NULL == fl) || (NULL == fl->f))
3029 * Either there are no filter files left or this
3030 * filter file just contains no valid filters.
3032 * Continue to be sure we don't miss valid filter
3033 * files that are chained after empty or invalid ones.
3038 for (b = fl->f; b != NULL; b = b->next)
3040 number_of_filters++;
3044 return number_of_filters;
3049 /*********************************************************************
3051 * Function : cgi_edit_process_string_action
3053 * Description : Helper CGI function that actually edits the Actions list for
3054 * the action string parameters.
3057 * 1 : csp = Current client state (buffers, headers, etc...)
3058 * 2 : rsp = http_response data structure for output
3059 * 3 : parameters = map of cgi parameters
3060 * 4 : cur_line = current config file line
3061 * 5 : action_type = string filter type to process
3063 * Returns : JB_ERR_OK on success
3064 * JB_ERR_MEMORY on out-of-memory
3065 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3066 * specified or not valid.
3068 *********************************************************************/
3069 static jb_err cgi_edit_process_string_action(struct client_state *csp,
3070 struct http_response *rsp,
3071 const struct map *parameters,
3072 struct file_line *cur_line,
3073 enum filter_type action_type)
3075 jb_err err = JB_ERR_OK;
3076 const char *abbr_type = action_type_info[action_type].abbr_type;
3077 int action_identifier = 0;
3079 /* process existing string filter actions */
3080 for (action_identifier = 0; !err; action_identifier++)
3086 const char *name, *new_name;
3088 * Action state. Valid states are: 'Y' (active),
3089 * 'N' (inactive) and 'X' (no change).
3093 * Abbreviated filter type. Valid types are: 'U' (suppress tag), 'H' (add header)
3095 int multi_action_index = 0;
3097 /* Generate the keys */
3098 snprintf(key_value, sizeof(key_value), "string_action_%s_r%x", abbr_type, action_identifier);
3099 snprintf(key_name, sizeof(key_name), "string_action_%s_n%x", abbr_type, action_identifier);
3100 snprintf(old_name, sizeof(old_name), "string_action_%s_o%x", abbr_type, action_identifier);
3101 snprintf(key_type, sizeof(key_type), "string_action_%s_t%x", abbr_type, action_identifier);
3103 err = get_string_param(parameters, old_name, &name);
3108 /* The action identifier isn't present: we're done! */
3112 err = get_string_param(parameters, key_name, &new_name);
3114 if (new_name == NULL) new_name = name;
3116 type = get_char_param(parameters, key_type);
3120 multi_action_index = ACTION_MULTI_SUPPRESS_TAG;
3123 multi_action_index = ACTION_MULTI_ADD_HEADER;
3126 log_error(LOG_LEVEL_ERROR,
3127 "Unknown action type: %c for action %s. Action ignored.", type, name);
3131 value = get_char_param(parameters, key_value);
3132 if (value == 'X' || value == 'Y' || value == 'N')
3134 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3135 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3140 err = enlist(cur_line->data.action->multi_add[multi_action_index], new_name);
3142 else if (value == 'N')
3144 err = enlist(cur_line->data.action->multi_remove[multi_action_index], new_name);
3148 /* process new string filter actions */
3149 for (action_identifier = 0; !err; action_identifier++)
3156 * Action state. Valid states are: 'Y' (active),
3157 * 'N' (inactive) and 'X' (no change).
3161 * Abbreviated filter type. Valid types are: 'U' (suppress tag), 'H' (add header)
3163 int multi_action_index = 0;
3165 /* Generate the keys */
3166 snprintf(key_value, sizeof(key_value), "new_string_action_%s_r%x", abbr_type, action_identifier);
3167 snprintf(key_name, sizeof(key_name), "new_string_action_%s_n%x", abbr_type, action_identifier);
3168 snprintf(key_type, sizeof(key_type), "new_string_action_%s_t%x", abbr_type, action_identifier);
3170 err = get_string_param(parameters, key_name, &name);
3175 /* The action identifier isn't present: we've done! */
3179 type = get_char_param(parameters, key_type);
3183 multi_action_index = ACTION_MULTI_SUPPRESS_TAG;
3186 multi_action_index = ACTION_MULTI_ADD_HEADER;
3189 log_error(LOG_LEVEL_ERROR,
3190 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3194 value = get_char_param(parameters, key_value);
3197 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3198 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3199 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3201 else if (value == 'N')
3203 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3204 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3205 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3207 /* nothing to do if the value is 'X' */
3213 /*********************************************************************
3215 * Function : cgi_edit_actions_submit
3217 * Description : CGI function that actually edits the Actions list.
3220 * 1 : csp = Current client state (buffers, headers, etc...)
3221 * 2 : rsp = http_response data structure for output
3222 * 3 : parameters = map of cgi parameters
3224 * CGI Parameters : None
3226 * Returns : JB_ERR_OK on success
3227 * JB_ERR_MEMORY on out-of-memory
3228 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3229 * specified or not valid.
3231 *********************************************************************/
3232 jb_err cgi_edit_actions_submit(struct client_state *csp,
3233 struct http_response *rsp,
3234 const struct map *parameters)
3239 size_t newtext_size;
3241 struct editable_file * file;
3242 struct file_line * cur_line;
3243 unsigned line_number;
3246 int filter_identifier;
3248 const char * action_set_name;
3249 struct file_list * fl;
3250 struct url_actions * b;
3251 int number_of_filters;
3253 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3255 return cgi_error_disabled(csp, rsp);
3258 err = get_number_param(csp, parameters, "s", §ionid);
3264 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3267 /* No filename specified, can't read file, modified, or out of memory. */
3268 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3271 cur_line = file->lines;
3273 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3275 cur_line = cur_line->next;
3278 if ( (cur_line == NULL)
3279 || (line_number != sectionid)
3281 || (cur_line->type != FILE_LINE_ACTION))
3283 /* Invalid "sectionid" parameter */
3284 edit_free_file(file);
3285 return JB_ERR_CGI_PARAMS;
3288 get_string_param(parameters, "p", &action_set_name);
3289 if (action_set_name != NULL)
3291 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3293 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3295 for (b = b->next; NULL != b; b = b->next)
3297 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3299 copy_action(cur_line->data.action, b->action);
3305 edit_free_file(file);
3306 return JB_ERR_CGI_PARAMS;
3312 err = actions_from_radio(parameters, cur_line->data.action);
3318 edit_free_file(file);
3322 /* Check the "disable all of this type" parameters. */
3323 for (i = 0; i < MAX_FILTER_TYPES; i++)
3325 const int multi_action_index = action_type_info[i].multi_action_index;
3326 const char ch = get_char_param(parameters, action_type_info[i].disable_all_param);
3330 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3331 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3332 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3336 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3340 number_of_filters = get_number_of_filters(csp);
3342 for (filter_identifier = 0; filter_identifier < number_of_filters && !err; filter_identifier++)
3349 * Filter state. Valid states are: 'Y' (active),
3350 * 'N' (inactive) and 'X' (no change).
3354 * Abbreviated filter type. Valid types are: 'F' (content filter),
3355 * 'S' (server-header filter) and 'C' (client-header filter).
3357 int multi_action_index = 0;
3359 /* Generate the keys */
3360 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3361 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3362 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3363 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3364 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3366 err = get_string_param(parameters, key_name, &name);
3371 /* The filter identifier isn't present. Try the next one. */
3375 type = get_char_param(parameters, key_type);
3379 multi_action_index = ACTION_MULTI_FILTER;
3382 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3385 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3388 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3391 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3394 multi_action_index = ACTION_MULTI_CLIENT_BODY_FILTER;
3397 log_error(LOG_LEVEL_ERROR,
3398 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3401 assert(multi_action_index);
3403 value = get_char_param(parameters, key_value);
3406 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3407 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3408 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3410 else if (value == 'N')
3412 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3413 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3415 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3416 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3419 else if (value == 'X')
3421 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3422 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3426 /* process new string filters */
3427 for (i = 0; !err && i < SZ(string_action_type_info); i++)
3429 err = cgi_edit_process_string_action(csp, rsp, parameters, cur_line,
3430 string_action_type_info[i].action_type);
3436 edit_free_file(file);
3440 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3443 edit_free_file(file);
3444 return JB_ERR_MEMORY;
3447 len = strlen(actiontext);
3451 * Empty action - must special-case this.
3452 * Simply setting len to 1 is sufficient...
3457 newtext_size = len + 2;
3458 newtext = malloc_or_die(newtext_size);
3459 strlcpy(newtext, actiontext, newtext_size);
3463 newtext[len + 1] = '\0';
3465 freez(cur_line->raw);
3466 freez(cur_line->unprocessed);
3467 cur_line->unprocessed = newtext;
3469 err = edit_write_file(file);
3472 /* Error writing file */
3473 if (err == JB_ERR_FILE)
3475 /* Read-only file. */
3476 err = cgi_error_file_read_only(csp, rsp, file->filename);
3478 edit_free_file(file);
3482 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
3483 (unsigned long) time(NULL), file->identifier, sectionid);
3485 edit_free_file(file);
3487 return cgi_redirect(rsp, target);
3491 /*********************************************************************
3493 * Function : cgi_edit_actions_url
3495 * Description : CGI function that actually edits a URL pattern in
3499 * 1 : csp = Current client state (buffers, headers, etc...)
3500 * 2 : rsp = http_response data structure for output
3501 * 3 : parameters = map of cgi parameters
3504 * filename : Identifies the file to edit
3505 * ver : File's last-modified time
3506 * section : Line number of section to edit
3507 * pattern : Line number of pattern to edit
3508 * newval : New value for pattern
3510 * Returns : JB_ERR_OK on success
3511 * JB_ERR_MEMORY on out-of-memory
3512 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3513 * specified or not valid.
3515 *********************************************************************/
3516 jb_err cgi_edit_actions_url(struct client_state *csp,
3517 struct http_response *rsp,
3518 const struct map *parameters)
3522 struct editable_file * file;
3523 struct file_line * cur_line;
3524 unsigned line_number;
3525 unsigned section_start_line_number = 0;
3533 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3535 return cgi_error_disabled(csp, rsp);
3538 err = get_number_param(csp, parameters, "p", &patternid);
3545 return JB_ERR_CGI_PARAMS;
3548 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3554 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3557 /* No filename specified, can't read file, modified, or out of memory. */
3559 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3563 cur_line = file->lines;
3565 while ((cur_line != NULL) && (line_number < patternid))
3567 if (cur_line->type == FILE_LINE_ACTION)
3569 section_start_line_number = line_number;
3571 cur_line = cur_line->next;
3575 if ((cur_line == NULL)
3576 || (cur_line->type != FILE_LINE_URL))
3578 /* Invalid "patternid" parameter */
3580 edit_free_file(file);
3581 return JB_ERR_CGI_PARAMS;
3584 /* At this point, the line to edit is in cur_line */
3586 freez(cur_line->raw);
3587 freez(cur_line->unprocessed);
3588 cur_line->unprocessed = new_pattern;
3590 err = edit_write_file(file);
3593 /* Error writing file */
3594 if (err == JB_ERR_FILE)
3596 /* Read-only file. */
3597 err = cgi_error_file_read_only(csp, rsp, file->filename);
3599 edit_free_file(file);
3603 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
3604 (unsigned long) time(NULL), file->identifier, section_start_line_number);
3606 edit_free_file(file);
3608 return cgi_redirect(rsp, target);
3612 /*********************************************************************
3614 * Function : cgi_edit_actions_add_url
3616 * Description : CGI function that actually adds a URL pattern to
3620 * 1 : csp = Current client state (buffers, headers, etc...)
3621 * 2 : rsp = http_response data structure for output
3622 * 3 : parameters = map of cgi parameters
3625 * filename : Identifies the file to edit
3626 * ver : File's last-modified time
3627 * section : Line number of section to edit
3628 * newval : New pattern
3630 * Returns : JB_ERR_OK on success
3631 * JB_ERR_MEMORY on out-of-memory
3632 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3633 * specified or not valid.
3635 *********************************************************************/
3636 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3637 struct http_response *rsp,
3638 const struct map *parameters)
3642 struct file_line * new_line;
3643 struct editable_file * file;
3644 struct file_line * cur_line;
3645 unsigned line_number;
3649 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3651 return cgi_error_disabled(csp, rsp);
3654 err = get_number_param(csp, parameters, "s", §ionid);
3661 return JB_ERR_CGI_PARAMS;
3664 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3670 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3673 /* No filename specified, can't read file, modified, or out of memory. */
3675 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3679 cur_line = file->lines;
3681 while ((cur_line != NULL) && (line_number < sectionid))
3683 cur_line = cur_line->next;
3687 if ((cur_line == NULL)
3688 || (cur_line->type != FILE_LINE_ACTION))
3690 /* Invalid "sectionid" parameter */
3692 edit_free_file(file);
3693 return JB_ERR_CGI_PARAMS;
3696 /* At this point, the section header is in cur_line - add after this. */
3698 /* Allocate the new line */
3699 new_line = zalloc_or_die(sizeof(*new_line));
3701 /* Fill in the data members of the new line */
3702 new_line->raw = NULL;
3703 new_line->prefix = NULL;
3704 new_line->unprocessed = new_pattern;
3705 new_line->type = FILE_LINE_URL;
3707 /* Link new_line into the list, after cur_line */
3708 new_line->next = cur_line->next;
3709 cur_line->next = new_line;
3711 /* Done making changes, now commit */
3713 err = edit_write_file(file);
3716 /* Error writing file */
3717 if (err == JB_ERR_FILE)
3719 /* Read-only file. */
3720 err = cgi_error_file_read_only(csp, rsp, file->filename);
3722 edit_free_file(file);
3726 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
3727 (unsigned long) time(NULL), file->identifier, sectionid);
3729 edit_free_file(file);
3731 return cgi_redirect(rsp, target);
3735 /*********************************************************************
3737 * Function : cgi_edit_actions_remove_url
3739 * Description : CGI function that actually removes a URL pattern from
3743 * 1 : csp = Current client state (buffers, headers, etc...)
3744 * 2 : rsp = http_response data structure for output
3745 * 3 : parameters = map of cgi parameters
3748 * f : (filename) Identifies the file to edit
3749 * v : (version) File's last-modified time
3750 * p : (pattern) Line number of pattern to remove
3752 * Returns : JB_ERR_OK on success
3753 * JB_ERR_MEMORY on out-of-memory
3754 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3755 * specified or not valid.
3757 *********************************************************************/
3758 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3759 struct http_response *rsp,
3760 const struct map *parameters)
3763 struct editable_file * file;
3764 struct file_line * cur_line;
3765 struct file_line * prev_line;
3766 unsigned line_number;
3767 unsigned section_start_line_number = 0;
3771 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3773 return cgi_error_disabled(csp, rsp);
3776 err = get_number_param(csp, parameters, "p", &patternid);
3782 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3785 /* No filename specified, can't read file, modified, or out of memory. */
3786 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3791 cur_line = file->lines;
3793 while ((cur_line != NULL) && (line_number < patternid))
3795 if (cur_line->type == FILE_LINE_ACTION)
3797 section_start_line_number = line_number;
3799 prev_line = cur_line;
3800 cur_line = cur_line->next;
3804 if ( (cur_line == NULL)
3805 || (prev_line == NULL)
3806 || (cur_line->type != FILE_LINE_URL))
3808 /* Invalid "patternid" parameter */
3809 edit_free_file(file);
3810 return JB_ERR_CGI_PARAMS;
3813 /* At this point, the line to remove is in cur_line, and the previous
3814 * one is in prev_line
3817 /* Unlink cur_line */
3818 prev_line->next = cur_line->next;
3819 cur_line->next = NULL;
3822 edit_free_file_lines(cur_line);
3824 err = edit_write_file(file);
3827 /* Error writing file */
3828 if (err == JB_ERR_FILE)
3830 /* Read-only file. */
3831 err = cgi_error_file_read_only(csp, rsp, file->filename);
3833 edit_free_file(file);
3837 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
3838 (unsigned long) time(NULL), file->identifier, section_start_line_number);
3840 edit_free_file(file);
3842 return cgi_redirect(rsp, target);
3846 /*********************************************************************
3848 * Function : cgi_edit_actions_section_remove
3850 * Description : CGI function that actually removes a whole section from
3851 * the actions file. The section must be empty first
3852 * (else JB_ERR_CGI_PARAMS).
3855 * 1 : csp = Current client state (buffers, headers, etc...)
3856 * 2 : rsp = http_response data structure for output
3857 * 3 : parameters = map of cgi parameters
3860 * f : (filename) Identifies the file to edit
3861 * v : (version) File's last-modified time
3862 * s : (section) Line number of section to edit
3864 * Returns : JB_ERR_OK on success
3865 * JB_ERR_MEMORY on out-of-memory
3866 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3867 * specified or not valid.
3869 *********************************************************************/
3870 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3871 struct http_response *rsp,
3872 const struct map *parameters)
3875 struct editable_file * file;
3876 struct file_line * cur_line;
3877 struct file_line * prev_line;
3878 unsigned line_number;
3882 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3884 return cgi_error_disabled(csp, rsp);
3887 err = get_number_param(csp, parameters, "s", §ionid);
3893 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3896 /* No filename specified, can't read file, modified, or out of memory. */
3897 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3901 cur_line = file->lines;
3904 while ((cur_line != NULL) && (line_number < sectionid))
3906 prev_line = cur_line;
3907 cur_line = cur_line->next;
3911 if ((cur_line == NULL)
3912 || (cur_line->type != FILE_LINE_ACTION))
3914 /* Invalid "sectionid" parameter */
3915 edit_free_file(file);
3916 return JB_ERR_CGI_PARAMS;
3919 if ((cur_line->next != NULL)
3920 && (cur_line->next->type == FILE_LINE_URL))
3922 /* Section not empty. */
3923 edit_free_file(file);
3924 return JB_ERR_CGI_PARAMS;
3927 /* At this point, the line to remove is in cur_line, and the previous
3928 * one is in prev_line
3931 /* Unlink cur_line */
3932 if (prev_line == NULL)
3934 /* Removing the first line from the file */
3935 file->lines = cur_line->next;
3939 prev_line->next = cur_line->next;
3941 cur_line->next = NULL;
3944 edit_free_file_lines(cur_line);
3946 err = edit_write_file(file);
3949 /* Error writing file */
3950 if (err == JB_ERR_FILE)
3952 /* Read-only file. */
3953 err = cgi_error_file_read_only(csp, rsp, file->filename);
3955 edit_free_file(file);
3959 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
3960 (unsigned long) time(NULL), file->identifier);
3962 edit_free_file(file);
3964 return cgi_redirect(rsp, target);
3968 /*********************************************************************
3970 * Function : cgi_edit_actions_section_add
3972 * Description : CGI function that adds a new empty section to
3976 * 1 : csp = Current client state (buffers, headers, etc...)
3977 * 2 : rsp = http_response data structure for output
3978 * 3 : parameters = map of cgi parameters
3981 * f : (filename) Identifies the file to edit
3982 * v : (version) File's last-modified time
3983 * s : (section) Line number of section to add after, 0 for
3986 * Returns : JB_ERR_OK on success
3987 * JB_ERR_MEMORY on out-of-memory
3988 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3989 * specified or not valid.
3991 *********************************************************************/
3992 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3993 struct http_response *rsp,
3994 const struct map *parameters)
3997 struct file_line * new_line;
3999 struct editable_file * file;
4000 struct file_line * cur_line;
4001 unsigned line_number;
4005 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4007 return cgi_error_disabled(csp, rsp);
4010 err = get_number_param(csp, parameters, "s", §ionid);
4016 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4019 /* No filename specified, can't read file, modified, or out of memory. */
4020 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4024 cur_line = file->lines;
4026 if (sectionid <= 1U)
4028 /* Add to start of file */
4029 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4031 /* There's something in the file, find the line before the first
4034 while ((cur_line->next != NULL)
4035 && (cur_line->next->type != FILE_LINE_ACTION))
4037 cur_line = cur_line->next;
4043 /* File starts with action line, so insert at top */
4049 /* Add after stated section. */
4050 while ((cur_line != NULL) && (line_number < sectionid))
4052 cur_line = cur_line->next;
4056 if ((cur_line == NULL)
4057 || (cur_line->type != FILE_LINE_ACTION))
4059 /* Invalid "sectionid" parameter */
4060 edit_free_file(file);
4061 return JB_ERR_CGI_PARAMS;
4064 /* Skip through the section to find the last line in it. */
4065 while ((cur_line->next != NULL)
4066 && (cur_line->next->type != FILE_LINE_ACTION))
4068 cur_line = cur_line->next;
4073 /* At this point, the last line in the previous section is in cur_line
4074 * - add after this. (Or if we need to add as the first line, cur_line
4078 new_text = strdup("{}");
4079 if (NULL == new_text)
4081 edit_free_file(file);
4082 return JB_ERR_MEMORY;
4085 /* Allocate the new line */
4086 new_line = zalloc_or_die(sizeof(*new_line));
4088 /* Fill in the data members of the new line */
4089 new_line->raw = NULL;
4090 new_line->prefix = NULL;
4091 new_line->unprocessed = new_text;
4092 new_line->type = FILE_LINE_ACTION;
4094 if (cur_line != NULL)
4096 /* Link new_line into the list, after cur_line */
4097 new_line->next = cur_line->next;
4098 cur_line->next = new_line;
4102 /* Link new_line into the list, as first line */
4103 new_line->next = file->lines;
4104 file->lines = new_line;
4107 /* Done making changes, now commit */
4109 err = edit_write_file(file);
4112 /* Error writing file */
4113 if (err == JB_ERR_FILE)
4115 /* Read-only file. */
4116 err = cgi_error_file_read_only(csp, rsp, file->filename);
4118 edit_free_file(file);
4122 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
4123 (unsigned long) time(NULL), file->identifier);
4125 edit_free_file(file);
4127 return cgi_redirect(rsp, target);
4131 /*********************************************************************
4133 * Function : cgi_edit_actions_section_swap
4135 * Description : CGI function that swaps the order of two sections
4136 * in the actions file. Note that this CGI can actually
4137 * swap any two arbitrary sections, but the GUI interface
4138 * currently only allows consecutive sections to be
4142 * 1 : csp = Current client state (buffers, headers, etc...)
4143 * 2 : rsp = http_response data structure for output
4144 * 3 : parameters = map of cgi parameters
4147 * f : (filename) Identifies the file to edit
4148 * v : (version) File's last-modified time
4149 * s1 : (section1) Line number of first section to swap
4150 * s2 : (section2) Line number of second section to swap
4152 * Returns : JB_ERR_OK on success
4153 * JB_ERR_MEMORY on out-of-memory
4154 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4155 * specified or not valid.
4157 *********************************************************************/
4158 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4159 struct http_response *rsp,
4160 const struct map *parameters)
4164 struct editable_file * file;
4165 struct file_line * cur_line;
4166 struct file_line * prev_line;
4167 struct file_line * line_before_section1;
4168 struct file_line * line_start_section1;
4169 struct file_line * line_end_section1;
4170 struct file_line * line_after_section1;
4171 struct file_line * line_before_section2;
4172 struct file_line * line_start_section2;
4173 struct file_line * line_end_section2;
4174 struct file_line * line_after_section2;
4175 unsigned line_number;
4179 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4181 return cgi_error_disabled(csp, rsp);
4184 err = get_number_param(csp, parameters, "s1", §ion1);
4185 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4191 if (section1 > section2)
4193 unsigned temp = section2;
4194 section2 = section1;
4198 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4201 /* No filename specified, can't read file, modified, or out of memory. */
4202 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4205 /* Start at the beginning... */
4207 cur_line = file->lines;
4210 /* ... find section1 ... */
4211 while ((cur_line != NULL) && (line_number < section1))
4213 prev_line = cur_line;
4214 cur_line = cur_line->next;
4218 if ((cur_line == NULL)
4219 || (cur_line->type != FILE_LINE_ACTION))
4221 /* Invalid "section1" parameter */
4222 edit_free_file(file);
4223 return JB_ERR_CGI_PARAMS;
4226 /* If no-op, we've validated params and can skip the rest. */
4227 if (section1 != section2)
4229 /* ... find the end of section1 ... */
4230 line_before_section1 = prev_line;
4231 line_start_section1 = cur_line;
4234 prev_line = cur_line;
4235 cur_line = cur_line->next;
4238 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4239 line_end_section1 = prev_line;
4240 line_after_section1 = cur_line;
4242 /* ... find section2 ... */
4243 while ((cur_line != NULL) && (line_number < section2))
4245 prev_line = cur_line;
4246 cur_line = cur_line->next;
4250 if ((cur_line == NULL)
4251 || (cur_line->type != FILE_LINE_ACTION))
4253 /* Invalid "section2" parameter */
4254 edit_free_file(file);
4255 return JB_ERR_CGI_PARAMS;
4258 /* ... find the end of section2 ... */
4259 line_before_section2 = prev_line;
4260 line_start_section2 = cur_line;
4263 prev_line = cur_line;
4264 cur_line = cur_line->next;
4267 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4268 line_end_section2 = prev_line;
4269 line_after_section2 = cur_line;
4271 /* Now have all the pointers we need. Do the swap. */
4273 /* Change the pointer to section1 to point to section2 instead */
4274 if (line_before_section1 == NULL)
4276 file->lines = line_start_section2;
4280 line_before_section1->next = line_start_section2;
4283 if (line_before_section2 == line_end_section1)
4285 /* Consecutive sections */
4286 line_end_section2->next = line_start_section1;
4290 line_end_section2->next = line_after_section1;
4291 line_before_section2->next = line_start_section1;
4294 /* Set the pointer from the end of section1 to the rest of the file */
4295 line_end_section1->next = line_after_section2;
4297 err = edit_write_file(file);
4300 /* Error writing file */
4301 if (err == JB_ERR_FILE)
4303 /* Read-only file. */
4304 err = cgi_error_file_read_only(csp, rsp, file->filename);
4306 edit_free_file(file);
4309 } /* END if (section1 != section2) */
4311 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
4312 (unsigned long) time(NULL), file->identifier);
4314 edit_free_file(file);
4316 return cgi_redirect(rsp, target);
4320 /*********************************************************************
4322 * Function : javascriptify
4324 * Description : Converts a string into a form JavaScript will like.
4326 * Netscape 4's JavaScript sucks - it doesn't use
4327 * "id" parameters, so you have to set the "name"
4328 * used to submit a form element to something JavaScript
4329 * will like. (Or access the elements by index in an
4330 * array. That array contains >60 elements and will
4331 * be changed whenever we add a new action to the
4332 * editor, so I'm NOT going to use indexes that have
4333 * to be figured out by hand.)
4335 * Currently the only thing we have to worry about
4336 * is "-" ==> "_" conversion.
4338 * This is a length-preserving operation so it is
4339 * carried out in-place, no memory is allocated
4343 * 1 : identifier = String to make JavaScript-friendly.
4347 *********************************************************************/
4348 static void javascriptify(char * identifier)
4350 char * p = identifier;
4351 while (NULL != (p = strchr(p, '-')))
4358 /*********************************************************************
4360 * Function : actions_to_radio
4362 * Description : Converts a actionsfile entry into settings for
4363 * radio buttons and edit boxes on a HTML form.
4366 * 1 : exports = List of substitutions to add to.
4367 * 2 : action = Action to read
4369 * Returns : JB_ERR_OK on success
4370 * JB_ERR_MEMORY on out-of-memory
4372 *********************************************************************/
4373 static jb_err actions_to_radio(struct map * exports,
4374 const struct action_spec *action)
4385 mask = action->mask;
4388 /* sanity - prevents "-feature +feature" */
4392 #define DEFINE_ACTION_BOOL(name, bit) \
4393 if (!(mask & bit)) \
4395 current_mode = 'n'; \
4397 else if (add & bit) \
4399 current_mode = 'y'; \
4403 current_mode = 'x'; \
4405 if (map_radio(exports, name, "ynx", current_mode)) \
4407 return JB_ERR_MEMORY; \
4410 #define DEFINE_ACTION_STRING(name, bit, index) \
4411 DEFINE_ACTION_BOOL(name, bit); \
4414 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4417 checked = !strcmp(action->string[index], value); \
4421 checked = is_default; \
4423 mapped_param |= checked; \
4424 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4426 return JB_ERR_MEMORY; \
4429 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4430 if (map(exports, name "-param-custom", 1, \
4431 ((!mapped_param) ? "checked" : ""), 1)) \
4433 return JB_ERR_MEMORY; \
4435 if (map(exports, name "-param", 1, \
4436 (((add & bit) && !mapped_param) ? \
4437 action->string[index] : default_val), 1)) \
4439 return JB_ERR_MEMORY; \
4442 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4443 if (map(exports, name "-param", 1, \
4444 ((add & bit) ? action->string[index] : default_val), 1)) \
4446 return JB_ERR_MEMORY; \
4449 #define DEFINE_ACTION_MULTI(name, index) \
4450 if (action->multi_add[index]->first) \
4452 current_mode = 'y'; \
4454 else if (action->multi_remove_all[index]) \
4456 current_mode = 'n'; \
4458 else if (action->multi_remove[index]->first) \
4460 current_mode = 'y'; \
4464 current_mode = 'x'; \
4466 if (map_radio(exports, name, "ynx", current_mode)) \
4468 return JB_ERR_MEMORY; \
4471 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4473 #include "actionlist.h"
4475 #undef DEFINE_ACTION_MULTI
4476 #undef DEFINE_ACTION_STRING
4477 #undef DEFINE_ACTION_BOOL
4478 #undef DEFINE_ACTION_ALIAS
4479 #undef DEFINE_CGI_PARAM_CUSTOM
4480 #undef DEFINE_CGI_PARAM_RADIO
4481 #undef DEFINE_CGI_PARAM_NO_RADIO
4486 /*********************************************************************
4488 * Function : action_render_string_actions_template
4490 * Description : Converts a actionsfile entry into HTML template for actions with string
4491 * filters (currently SUPPRESS-TAG actions only)
4494 * 1 : exports = List of substitutions to add to.
4495 * 2 : action = Action to read
4496 * 3 : action_template = template to fill
4497 * 4 : type = filter type info for rendered values/macro name
4499 * Returns : JB_ERR_OK on success
4500 * JB_ERR_MEMORY on out-of-memory
4502 *********************************************************************/
4503 static jb_err action_render_string_actions_template(struct map * exports,
4504 const struct action_spec *action,
4505 const char* action_template,
4506 const struct string_action_type_info *string_action_type)
4508 jb_err err = JB_ERR_OK;
4509 int filter_identifier = 0;
4511 char *prepared_template = strdup("");
4512 const struct action_type_info *type = &action_type_info[string_action_type->action_type];
4514 struct action_multi {
4516 struct list_entry *list;
4519 struct action_multi desc[] = {
4520 { 'y', action->multi_add[type->multi_action_index][0].first },
4521 { 'n', action->multi_remove[type->multi_action_index][0].first }
4524 for (i = 0; i < SZ(desc); ++i)
4526 const char radio = desc[i].radio;
4527 struct list_entry *entry = desc[i].list;
4528 for (;(!err) && (entry != NULL); entry = entry->next)
4531 struct map *line_exports;
4533 /* Generate a unique serial number */
4534 snprintf(number, sizeof(number), "%x", filter_identifier++);
4536 line_exports = new_map();
4537 if (line_exports == NULL)
4539 err = JB_ERR_MEMORY;
4544 if (!err) err = map(line_exports, "index", 1, number, 1);
4545 if (!err) err = map(line_exports, "name", 1, entry->str, 1);
4546 if (!err) err = map_radio(line_exports, "this-filter", "ynx", radio);
4547 if (!err) err = map(line_exports, "filter-type", 1, type->type, 1);
4548 if (!err) err = map(line_exports, "abbr-action-type", 1, type->abbr_type, 1);
4549 if (!err) err = map(line_exports, "anchor", 1, type->anchor, 1);
4550 if (!err) err = map(line_exports, "desc", 1, string_action_type->description, 1);
4551 if (!err) err = map(line_exports, "input_desc", 1, string_action_type->input_description, 1);
4554 action_line = strdup(action_template);
4555 if (action_line == NULL) err = JB_ERR_MEMORY;
4557 if (!err) err = template_fill(&action_line, line_exports);
4558 if (!err) err = string_join(&prepared_template, action_line);
4560 free_map(line_exports);
4564 if (!err) map(exports, type->macro_name, 1, prepared_template, 1);
4565 freez(prepared_template);
4569 /*********************************************************************
4571 * Function : actions_from_radio
4573 * Description : Converts a map of parameters passed to a CGI function
4574 * into an actionsfile entry.
4577 * 1 : parameters = parameters to the CGI call
4578 * 2 : action = Action to change. Must be valid before
4579 * the call, actions not specified will be
4582 * Returns : JB_ERR_OK on success
4583 * JB_ERR_MEMORY on out-of-memory
4585 *********************************************************************/
4586 static jb_err actions_from_radio(const struct map * parameters,
4587 struct action_spec *action)
4592 const char * js_name;
4593 jb_err err = JB_ERR_OK;
4598 /* Statics are generally a potential race condition,
4599 * but in this case we're safe and don't need semaphores.
4600 * Be careful if you modify this function.
4602 * The js_name_arr's are never free()d, but this is no
4603 * problem, since they will only be created once and
4604 * used by all threads thereafter. -oes
4607 #define JAVASCRIPTIFY(dest_var, string) \
4609 static int first_time = 1; \
4610 static char *js_name_arr; \
4613 js_name_arr = strdup(string); \
4614 javascriptify(js_name_arr); \
4616 dest_var = js_name_arr; \
4620 #define DEFINE_ACTION_BOOL(name, bit) \
4621 JAVASCRIPTIFY(js_name, name); \
4622 ch = get_char_param(parameters, js_name); \
4625 action->add |= bit; \
4626 action->mask |= bit; \
4628 else if (ch == 'N') \
4630 action->add &= ~bit; \
4631 action->mask &= ~bit; \
4633 else if (ch == 'X') \
4635 action->add &= ~bit; \
4636 action->mask |= bit; \
4639 #define DEFINE_ACTION_STRING(name, bit, index) \
4640 JAVASCRIPTIFY(js_name, name); \
4641 ch = get_char_param(parameters, js_name); \
4645 JAVASCRIPTIFY(js_name, name "-mode"); \
4646 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4647 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4649 JAVASCRIPTIFY(js_name, name "-param"); \
4650 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4652 if (param != NULL) \
4654 if (NULL == (param_dup = strdup(param))) \
4656 return JB_ERR_MEMORY; \
4658 freez(action->string[index]); \
4659 action->add |= bit; \
4660 action->mask |= bit; \
4661 action->string[index] = param_dup; \
4664 else if (ch == 'N') \
4666 if (action->add & bit) \
4668 freez(action->string[index]); \
4670 action->add &= ~bit; \
4671 action->mask &= ~bit; \
4673 else if (ch == 'X') \
4675 if (action->add & bit) \
4677 freez(action->string[index]); \
4679 action->add &= ~bit; \
4680 action->mask |= bit; \
4683 #define DEFINE_ACTION_MULTI(name, index) \
4684 JAVASCRIPTIFY(js_name, name); \
4685 ch = get_char_param(parameters, js_name); \
4690 else if (ch == 'N') \
4692 list_remove_all(action->multi_add[index]); \
4693 list_remove_all(action->multi_remove[index]); \
4694 action->multi_remove_all[index] = 1; \
4696 else if (ch == 'X') \
4698 list_remove_all(action->multi_add[index]); \
4699 list_remove_all(action->multi_remove[index]); \
4700 action->multi_remove_all[index] = 0; \
4703 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4705 #include "actionlist.h"
4707 #undef DEFINE_ACTION_MULTI
4708 #undef DEFINE_ACTION_STRING
4709 #undef DEFINE_ACTION_BOOL
4710 #undef DEFINE_ACTION_ALIAS
4711 #undef JAVASCRIPTIFY
4715 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4718 #ifdef FEATURE_TOGGLE
4719 /*********************************************************************
4721 * Function : cgi_toggle
4723 * Description : CGI function that adds a new empty section to
4727 * 1 : csp = Current client state (buffers, headers, etc...)
4728 * 2 : rsp = http_response data structure for output
4729 * 3 : parameters = map of cgi parameters
4732 * set : If present, how to change toggle setting:
4733 * "enable", "disable", "toggle", or none (default).
4734 * mini : If present, use mini reply template.
4736 * Returns : JB_ERR_OK on success
4737 * JB_ERR_MEMORY on out-of-memory
4739 *********************************************************************/
4740 jb_err cgi_toggle(struct client_state *csp,
4741 struct http_response *rsp,
4742 const struct map *parameters)
4744 struct map *exports;
4746 const char *template_name;
4752 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4754 return cgi_error_disabled(csp, rsp);
4757 mode = get_char_param(parameters, "set");
4762 global_toggle_state = 1;
4764 else if (mode == 'D')
4767 global_toggle_state = 0;
4769 else if (mode == 'T')
4772 global_toggle_state = !global_toggle_state;
4775 log_error(LOG_LEVEL_INFO, "Now toggled %s.", global_toggle_state ? "ON" : "OFF");
4777 if (NULL == (exports = default_exports(csp, "toggle")))
4779 return JB_ERR_MEMORY;
4782 template_name = (get_char_param(parameters, "mini")
4786 return template_fill_for_cgi(csp, template_name, exports, rsp);
4788 #endif /* def FEATURE_TOGGLE */