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_CLIENT_BODY_TAGGER,
257 "client-body-tagger-params", "client-body-tagger",
258 "client-body-tagger-all", "client_body_tagger_all",
259 "Q", "CLIENT-BODY-TAGGER"
262 ACTION_MULTI_ADD_HEADER,
263 "add-header-params", "add-header",
264 "add-header-all", "add_header_all",
267 #ifdef FEATURE_EXTERNAL_FILTERS
269 ACTION_MULTI_EXTERNAL_FILTER,
270 "external-content-filter-params", "external-filter",
271 "external-content-filter-all", "external_content_filter_all",
272 "E", "EXTERNAL-CONTENT-FILTER"
278 * Information about the string filter actions.
279 * Used for macro replacement in action_render_string_actions_template
281 struct string_action_type_info
283 const enum filter_type action_type; /**< Action type */
284 const char *description; /**< Action description */
285 const char *input_description; /**< Input field description */
288 /* String action types: special CGI handling */
289 static const struct string_action_type_info string_action_type_info[] =
293 "Suppress tag", "Tag to suppress"
297 "Add HTTP client header", "HTTP client header to add"
301 /* FIXME: Following non-static functions should be prototyped in .h or made static */
303 /* Functions to read and write arbitrary config files */
304 jb_err edit_read_file(struct client_state *csp,
305 const struct map *parameters,
307 struct editable_file **pfile);
308 jb_err edit_write_file(struct editable_file * file);
309 void edit_free_file(struct editable_file * file);
311 /* Functions to read and write actions files */
312 jb_err edit_parse_actions_file(struct editable_file * file);
313 jb_err edit_read_actions_file(struct client_state *csp,
314 struct http_response *rsp,
315 const struct map *parameters,
317 struct editable_file **pfile);
320 jb_err cgi_error_modified(struct client_state *csp,
321 struct http_response *rsp,
322 const char *filename);
323 jb_err cgi_error_parse(struct client_state *csp,
324 struct http_response *rsp,
325 struct editable_file *file);
326 jb_err cgi_error_file(struct client_state *csp,
327 struct http_response *rsp,
328 const char *filename);
329 jb_err cgi_error_file_read_only(struct client_state *csp,
330 struct http_response *rsp,
331 const char *filename);
333 /* Internal arbitrary config file support functions */
334 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
335 static void edit_free_file_lines(struct file_line * first_line);
337 /* Internal actions file support functions */
338 static int match_actions_file_header_line(const char * line, const char * name);
339 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
341 /* Internal parameter parsing functions */
342 static jb_err get_url_spec_param(struct client_state *csp,
343 const struct map *parameters,
348 /* Internal actionsfile <==> HTML conversion functions */
349 static jb_err map_radio(struct map * exports,
350 const char * optionname,
353 static jb_err actions_to_radio(struct map * exports,
354 const struct action_spec *action);
355 static jb_err actions_from_radio(const struct map * parameters,
356 struct action_spec *action);
357 static jb_err action_render_string_actions_template(struct map * exports,
358 const struct action_spec *action,
359 const char* action_template,
360 const struct string_action_type_info *string_action_type);
363 static jb_err map_copy_parameter_html(struct map *out,
364 const struct map *in,
367 static jb_err get_file_name_param(struct client_state *csp,
368 const struct map *parameters,
369 const char *param_name,
370 const char **pfilename);
372 /* Internal convenience functions */
373 static char *section_target(const unsigned sectionid);
375 /*********************************************************************
377 * Function : section_target
379 * Description : Given an unsigned (section id) n, produce a dynamically
380 * allocated string of the form #l<n>, for use in link
383 * XXX: The hash should be moved into the templates
384 * to make this function more generic and render
385 * stringify() obsolete.
388 * 1 : sectionid = start line number of section
390 * Returns : String with link target, or NULL if out of
393 *********************************************************************/
394 static char *section_target(const unsigned sectionid)
398 snprintf(buf, sizeof(buf), "#l%u", sectionid);
404 /*********************************************************************
406 * Function : stringify
408 * Description : Convert a number into a dynamically allocated string.
411 * 1 : number = The number to convert.
413 * Returns : String with link target, or NULL if out of memory
415 *********************************************************************/
416 static char *stringify(const unsigned number)
420 snprintf(buf, sizeof(buf), "%u", number);
425 /*********************************************************************
427 * Function : map_copy_parameter_html
429 * Description : Copy a CGI parameter from one map to another, HTML
433 * 1 : out = target map
434 * 2 : in = source map
435 * 3 : name = name of cgi parameter to copy
437 * Returns : JB_ERR_OK on success
438 * JB_ERR_MEMORY on out-of-memory
439 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
442 *********************************************************************/
443 static jb_err map_copy_parameter_html(struct map *out,
444 const struct map *in,
454 value = lookup(in, name);
455 err = map(out, name, 1, html_encode(value), 0);
462 else if (*value == '\0')
464 return JB_ERR_CGI_PARAMS;
473 /*********************************************************************
475 * Function : cgi_edit_actions_url_form
477 * Description : CGI function that displays a form for
481 * 1 : csp = Current client state (buffers, headers, etc...)
482 * 2 : rsp = http_response data structure for output
483 * 3 : parameters = map of cgi parameters
486 * i : (action index) Identifies the file to edit
487 * v : (version) File's last-modified time
488 * p : (pattern) Line number of pattern to edit
490 * Returns : JB_ERR_OK on success
491 * JB_ERR_MEMORY on out-of-memory
492 * JB_ERR_CGI_PARAMS if the CGI parameters are not
493 * specified or not valid.
495 *********************************************************************/
496 jb_err cgi_edit_actions_url_form(struct client_state *csp,
497 struct http_response *rsp,
498 const struct map *parameters)
500 struct map * exports;
502 struct editable_file * file;
503 struct file_line * cur_line;
504 unsigned line_number;
505 unsigned section_start_line_number = 0;
512 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
514 return cgi_error_disabled(csp, rsp);
517 err = get_number_param(csp, parameters, "p", &patternid);
523 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
526 /* No filename specified, can't read file, modified, or out of memory. */
527 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
530 cur_line = file->lines;
532 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
534 if (cur_line->type == FILE_LINE_ACTION)
536 section_start_line_number = line_number;
538 cur_line = cur_line->next;
541 if ( (cur_line == NULL)
542 || (line_number != patternid)
544 || (cur_line->type != FILE_LINE_URL))
546 /* Invalid "patternid" parameter */
547 edit_free_file(file);
548 return JB_ERR_CGI_PARAMS;
551 if (NULL == (exports = default_exports(csp, NULL)))
553 edit_free_file(file);
554 return JB_ERR_MEMORY;
557 err = map(exports, "f", 1, stringify(file->identifier), 0);
558 if (!err) err = map(exports, "v", 1, file->version_str, 1);
559 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
560 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
561 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
563 edit_free_file(file);
571 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
575 /*********************************************************************
577 * Function : cgi_edit_actions_add_url_form
579 * Description : CGI function that displays a form for
583 * 1 : csp = Current client state (buffers, headers, etc...)
584 * 2 : rsp = http_response data structure for output
585 * 3 : parameters = map of cgi parameters
588 * f : (filename) Identifies the file to edit
589 * v : (version) File's last-modified time
590 * s : (section) Line number of section to edit
592 * Returns : JB_ERR_OK on success
593 * JB_ERR_MEMORY on out-of-memory
594 * JB_ERR_CGI_PARAMS if the CGI parameters are not
595 * specified or not valid.
597 *********************************************************************/
598 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
599 struct http_response *rsp,
600 const struct map *parameters)
609 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
611 return cgi_error_disabled(csp, rsp);
614 if (NULL == (exports = default_exports(csp, NULL)))
616 return JB_ERR_MEMORY;
619 err = map_copy_parameter_html(exports, parameters, "f");
620 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
621 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
629 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
633 /*********************************************************************
635 * Function : cgi_edit_actions_remove_url_form
637 * Description : CGI function that displays a form for
641 * 1 : csp = Current client state (buffers, headers, etc...)
642 * 2 : rsp = http_response data structure for output
643 * 3 : parameters = map of cgi parameters
646 * f : (number) The action file identifier.
647 * v : (version) File's last-modified time
648 * p : (pattern) Line number of pattern to edit
650 * Returns : JB_ERR_OK on success
651 * JB_ERR_MEMORY on out-of-memory
652 * JB_ERR_CGI_PARAMS if the CGI parameters are not
653 * specified or not valid.
655 *********************************************************************/
656 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
657 struct http_response *rsp,
658 const struct map *parameters)
660 struct map * exports;
662 struct editable_file * file;
663 struct file_line * cur_line;
664 unsigned line_number;
665 unsigned section_start_line_number = 0;
672 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
674 return cgi_error_disabled(csp, rsp);
677 err = get_number_param(csp, parameters, "p", &patternid);
683 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
686 /* No filename specified, can't read file, modified, or out of memory. */
687 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
690 cur_line = file->lines;
692 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
694 if (cur_line->type == FILE_LINE_ACTION)
696 section_start_line_number = line_number;
698 cur_line = cur_line->next;
701 if ( (cur_line == NULL)
702 || (line_number != patternid)
704 || (cur_line->type != FILE_LINE_URL))
706 /* Invalid "patternid" parameter */
707 edit_free_file(file);
708 return JB_ERR_CGI_PARAMS;
711 if (NULL == (exports = default_exports(csp, NULL)))
713 edit_free_file(file);
714 return JB_ERR_MEMORY;
717 err = map(exports, "f", 1, stringify(file->identifier), 0);
718 if (!err) err = map(exports, "v", 1, file->version_str, 1);
719 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
720 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
721 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
722 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
724 edit_free_file(file);
732 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
736 /*********************************************************************
738 * Function : edit_write_file
740 * Description : Write a complete file to disk.
743 * 1 : file = File to write.
745 * Returns : JB_ERR_OK on success
746 * JB_ERR_FILE on error writing to file.
747 * JB_ERR_MEMORY on out of memory
749 *********************************************************************/
750 jb_err edit_write_file(struct editable_file * file)
753 struct file_line * cur_line;
754 struct stat statbuf[1];
755 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
756 digits in time_t, assuming this is a 64-bit
757 machine, plus null terminator, plus one
761 assert(file->filename);
763 if (NULL == (fp = fopen(file->filename, "wb")))
768 cur_line = file->lines;
769 while (cur_line != NULL)
773 if (fputs(cur_line->raw, fp) < 0)
781 if (cur_line->prefix)
783 if (fputs(cur_line->prefix, fp) < 0)
789 if (cur_line->unprocessed)
792 if (NULL != strchr(cur_line->unprocessed, '#'))
794 /* Must quote '#' characters */
801 /* Count number of # characters, so we know length of output string */
802 src = cur_line->unprocessed;
803 while (NULL != (src = strchr(src, '#')))
810 /* Allocate new memory for string */
811 len = strlen(cur_line->unprocessed) + (size_t)numhash;
812 str = malloc_or_die(len + 1);
814 /* Copy string but quote hashes */
815 src = cur_line->unprocessed;
823 assert(numhash >= 0);
829 assert(numhash == 0);
830 assert(strlen(str) == len);
831 assert(str == dest - len);
832 assert(src - len <= cur_line->unprocessed);
834 if ((strlen(str) != len) || (numhash != 0))
837 * Escaping didn't work as expected, go spread the news.
838 * Only reached in non-debugging builds.
840 log_error(LOG_LEVEL_ERROR,
841 "Looks like hash escaping failed. %s might be corrupted now.",
845 if (fputs(str, fp) < 0)
856 /* Can write without quoting '#' characters. */
857 if (fputs(cur_line->unprocessed, fp) < 0)
863 if (fputs(NEWLINE(file->newline), fp) < 0)
871 /* FIXME: Write data from file->data->whatever */
875 cur_line = cur_line->next;
881 /* Update the version stamp in the file structure, since we just
882 * wrote to the file & changed it's date.
884 if (stat(file->filename, statbuf) < 0)
886 /* Error, probably file not found. */
889 file->version = (unsigned)statbuf->st_mtime;
891 /* Correct file->version_str */
892 freez(file->version_str);
893 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
894 version_buf[sizeof(version_buf)-1] = '\0';
895 file->version_str = strdup_or_die(version_buf);
901 /*********************************************************************
903 * Function : edit_free_file
905 * Description : Free a complete file in memory.
908 * 1 : file = Data structure to free.
912 *********************************************************************/
913 void edit_free_file(struct editable_file * file)
917 /* Silently ignore NULL pointer */
921 edit_free_file_lines(file->lines);
922 freez(file->version_str);
924 file->parse_error_text = NULL; /* Statically allocated */
925 file->parse_error = NULL;
931 /*********************************************************************
933 * Function : edit_free_file_lines
935 * Description : Free an entire linked list of file lines.
938 * 1 : first_line = Data structure to free.
942 *********************************************************************/
943 static void edit_free_file_lines(struct file_line * first_line)
945 struct file_line * next_line;
947 while (first_line != NULL)
949 next_line = first_line->next;
950 first_line->next = NULL;
951 freez(first_line->raw);
952 freez(first_line->prefix);
953 freez(first_line->unprocessed);
954 switch(first_line->type)
956 case 0: /* special case if memory zeroed */
957 case FILE_LINE_UNPROCESSED:
958 case FILE_LINE_BLANK:
959 case FILE_LINE_ALIAS_HEADER:
960 case FILE_LINE_SETTINGS_HEADER:
961 case FILE_LINE_DESCRIPTION_HEADER:
962 case FILE_LINE_DESCRIPTION_ENTRY:
963 case FILE_LINE_ALIAS_ENTRY:
965 /* No data is stored for these */
968 case FILE_LINE_ACTION:
969 free_action(first_line->data.action);
972 case FILE_LINE_SETTINGS_ENTRY:
973 freez(first_line->data.setting.name);
974 freez(first_line->data.setting.svalue);
977 /* Should never happen */
981 first_line->type = 0; /* paranoia */
983 first_line = next_line;
988 /*********************************************************************
990 * Function : match_actions_file_header_line
992 * Description : Match an actions file {{header}} line
995 * 1 : line = String from file
996 * 2 : name = Header to match against
998 * Returns : 0 iff they match.
1000 *********************************************************************/
1001 static int match_actions_file_header_line(const char * line, const char * name)
1009 if ((line[0] != '{') || (line[1] != '{'))
1015 /* Look for optional whitespace */
1016 while ((*line == ' ') || (*line == '\t'))
1021 /* Look for the specified name (case-insensitive) */
1023 if (0 != strncmpic(line, name, len))
1029 /* Look for optional whitespace */
1030 while ((*line == ' ') || (*line == '\t'))
1035 /* Look for "}}" and end of string*/
1036 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1046 /*********************************************************************
1048 * Function : match_actions_file_header_line
1050 * Description : Match an actions file {{header}} line
1053 * 1 : line = String from file. Must not start with
1054 * whitespace (else infinite loop!)
1055 * 2 : pname = Destination for name
1056 * 2 : pvalue = Destination for value
1058 * Returns : JB_ERR_OK on success
1059 * JB_ERR_MEMORY on out-of-memory
1060 * JB_ERR_PARSE if there's no "=" sign, or if there's
1061 * nothing before the "=" sign (but empty
1062 * values *after* the "=" sign are legal).
1064 *********************************************************************/
1065 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1067 const char * name_end;
1068 const char * value_start;
1074 assert(*line != ' ');
1075 assert(*line != '\t');
1080 value_start = strchr(line, '=');
1081 if ((value_start == NULL) || (value_start == line))
1083 return JB_ERR_PARSE;
1086 name_end = value_start - 1;
1088 /* Eat any whitespace before the '=' */
1089 while ((*name_end == ' ') || (*name_end == '\t'))
1092 * we already know we must have at least 1 non-ws char
1093 * at start of buf - no need to check
1098 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1099 *pname = malloc_or_die(name_len + 1);
1100 strncpy(*pname, line, name_len);
1101 (*pname)[name_len] = '\0';
1103 /* Eat any the whitespace after the '=' */
1105 while ((*value_start == ' ') || (*value_start == '\t'))
1110 if (NULL == (*pvalue = strdup(value_start)))
1114 return JB_ERR_MEMORY;
1121 /*********************************************************************
1123 * Function : edit_parse_actions_file
1125 * Description : Parse an actions file in memory.
1127 * Passed linked list must have the "data" member
1128 * zeroed, and must contain valid "next" and
1129 * "unprocessed" fields. The "raw" and "prefix"
1130 * fields are ignored, and "type" is just overwritten.
1132 * Note that on error the file may have been
1136 * 1 : file = Actions file to be parsed in-place.
1138 * Returns : JB_ERR_OK on success
1139 * JB_ERR_MEMORY on out-of-memory
1140 * JB_ERR_PARSE on error
1142 *********************************************************************/
1143 jb_err edit_parse_actions_file(struct editable_file * file)
1145 struct file_line * cur_line;
1147 const char * text; /* Text from a line */
1148 char * name; /* For lines of the form name=value */
1149 char * value; /* For lines of the form name=value */
1150 struct action_alias * alias_list = NULL;
1151 jb_err err = JB_ERR_OK;
1153 /* alias_list contains the aliases defined in this file.
1154 * It might be better to use the "file_line.data" fields
1155 * in the relevant places instead.
1158 cur_line = file->lines;
1160 /* A note about blank line support: Blank lines should only
1161 * ever occur as the last line in the file. This function
1162 * is more forgiving than that - FILE_LINE_BLANK can occur
1166 /* Skip leading blanks. Should only happen if file is
1167 * empty (which is valid, but pointless).
1169 while ((cur_line != NULL)
1170 && (cur_line->unprocessed[0] == '\0'))
1173 cur_line->type = FILE_LINE_BLANK;
1174 cur_line = cur_line->next;
1177 if ((cur_line != NULL)
1178 && (cur_line->unprocessed[0] != '{'))
1180 /* File doesn't start with a header */
1181 file->parse_error = cur_line;
1182 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1183 return JB_ERR_PARSE;
1186 if ((cur_line != NULL) && (0 ==
1187 match_actions_file_header_line(cur_line->unprocessed, "settings")))
1189 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1191 cur_line = cur_line->next;
1192 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1194 if (cur_line->unprocessed[0])
1196 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1198 err = split_line_on_equals(cur_line->unprocessed,
1199 &cur_line->data.setting.name,
1200 &cur_line->data.setting.svalue);
1201 if (err == JB_ERR_MEMORY)
1205 else if (err != JB_ERR_OK)
1207 /* Line does not contain a name=value pair */
1208 file->parse_error = cur_line;
1209 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1210 return JB_ERR_PARSE;
1215 cur_line->type = FILE_LINE_BLANK;
1217 cur_line = cur_line->next;
1221 if ((cur_line != NULL) && (0 ==
1222 match_actions_file_header_line(cur_line->unprocessed, "description")))
1224 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1226 cur_line = cur_line->next;
1227 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1229 if (cur_line->unprocessed[0])
1231 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1235 cur_line->type = FILE_LINE_BLANK;
1237 cur_line = cur_line->next;
1241 if ((cur_line != NULL) && (0 ==
1242 match_actions_file_header_line(cur_line->unprocessed, "alias")))
1244 cur_line->type = FILE_LINE_ALIAS_HEADER;
1246 cur_line = cur_line->next;
1247 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1249 if (cur_line->unprocessed[0])
1251 /* define an alias */
1252 struct action_alias * new_alias;
1254 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1256 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1257 if (err == JB_ERR_MEMORY)
1259 free_alias_list(alias_list);
1262 else if (err != JB_ERR_OK)
1264 /* Line does not contain a name=value pair */
1265 file->parse_error = cur_line;
1266 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1267 free_alias_list(alias_list);
1268 return JB_ERR_PARSE;
1271 new_alias = zalloc_or_die(sizeof(*new_alias));
1273 err = get_actions(value, alias_list, new_alias->action);
1276 /* Invalid action or out of memory */
1280 free_alias_list(alias_list);
1281 if (err == JB_ERR_MEMORY)
1287 /* Line does not contain a name=value pair */
1288 file->parse_error = cur_line;
1289 file->parse_error_text = "This alias does not specify a valid set of actions.";
1290 return JB_ERR_PARSE;
1296 new_alias->name = name;
1299 new_alias->next = alias_list;
1300 alias_list = new_alias;
1304 cur_line->type = FILE_LINE_BLANK;
1306 cur_line = cur_line->next;
1310 /* Header done, process the main part of the file */
1311 while (cur_line != NULL)
1313 /* At this point, (cur_line->unprocessed[0] == '{') */
1314 assert(cur_line->unprocessed[0] == '{');
1315 text = cur_line->unprocessed + 1;
1316 len = strlen(text) - 1;
1317 if (text[len] != '}')
1319 /* No closing } on header */
1320 free_alias_list(alias_list);
1321 file->parse_error = cur_line;
1322 file->parse_error_text = "Headers starting with '{' must have a "
1323 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1324 "must close with two brackets ('}}').";
1325 return JB_ERR_PARSE;
1330 /* An invalid {{ header. */
1331 free_alias_list(alias_list);
1332 file->parse_error = cur_line;
1333 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1334 "Please remember that the system (two-bracket) headers must "
1335 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1336 "and must appear before any actions (one-bracket) headers. "
1337 "Also note that system headers may not be repeated.";
1338 return JB_ERR_PARSE;
1341 while ((*text == ' ') || (*text == '\t'))
1346 while ((len > (size_t)0)
1347 && ((text[len - 1] == ' ')
1348 || (text[len - 1] == '\t')))
1353 cur_line->type = FILE_LINE_ACTION;
1355 /* Remove {} and make copy */
1356 value = malloc_or_die(len + 1);
1357 strncpy(value, text, len);
1361 err = get_actions(value, alias_list, cur_line->data.action);
1364 /* Invalid action or out of memory */
1366 free_alias_list(alias_list);
1367 if (err == JB_ERR_MEMORY)
1373 /* Line does not contain a name=value pair */
1374 file->parse_error = cur_line;
1375 file->parse_error_text = "This header does not specify a valid set of actions.";
1376 return JB_ERR_PARSE;
1380 /* Done with string - it was clobbered anyway */
1383 /* Process next line */
1384 cur_line = cur_line->next;
1386 /* Loop processing URL patterns */
1387 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1389 if (cur_line->unprocessed[0])
1391 /* Could parse URL here, but this isn't currently needed */
1393 cur_line->type = FILE_LINE_URL;
1397 cur_line->type = FILE_LINE_BLANK;
1399 cur_line = cur_line->next;
1401 } /* End main while(cur_line != NULL) loop */
1403 free_alias_list(alias_list);
1409 /*********************************************************************
1411 * Function : edit_read_file_lines
1413 * Description : Read all the lines of a file into memory.
1414 * Handles whitespace, comments and line continuation.
1417 * 1 : fp = File to read from. On return, this will be
1418 * at EOF but it will not have been closed.
1419 * 2 : pfile = Destination for a linked list of file_lines.
1420 * Will be set to NULL on error.
1421 * 3 : newline = How to handle newlines.
1423 * Returns : JB_ERR_OK on success
1424 * JB_ERR_MEMORY on out-of-memory
1426 *********************************************************************/
1427 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1429 struct file_line * first_line; /* Keep for return value or to free */
1430 struct file_line * cur_line; /* Current line */
1431 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1439 cur_line = first_line = zalloc_or_die(sizeof(struct file_line));
1441 cur_line->type = FILE_LINE_UNPROCESSED;
1443 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1446 /* Out of memory or empty file. */
1447 /* Note that empty file is not an error we propagate up */
1449 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1454 prev_line = cur_line;
1455 cur_line = prev_line->next = zalloc_or_die(sizeof(struct file_line));
1457 cur_line->type = FILE_LINE_UNPROCESSED;
1459 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1460 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1463 edit_free_file_lines(first_line);
1464 return JB_ERR_MEMORY;
1468 while (rval != JB_ERR_FILE);
1472 /* We allocated one too many - free it */
1473 prev_line->next = NULL;
1476 *pfile = first_line;
1481 /*********************************************************************
1483 * Function : edit_read_file
1485 * Description : Read a complete file into memory.
1486 * Handles CGI parameter parsing. If requested, also
1487 * checks the file's modification timestamp.
1490 * 1 : csp = Current client state (buffers, headers, etc...)
1491 * 2 : parameters = map of cgi parameters.
1492 * 3 : require_version = true to check "ver" parameter.
1493 * 4 : pfile = Destination for the file. Will be set
1497 * f : The action file identifier.
1498 * ver : (Only if require_version is nonzero)
1499 * Timestamp of the actions file. If wrong, this
1500 * function fails with JB_ERR_MODIFIED.
1502 * Returns : JB_ERR_OK on success
1503 * JB_ERR_MEMORY on out-of-memory
1504 * JB_ERR_CGI_PARAMS if "filename" was not specified
1506 * JB_ERR_FILE if the file cannot be opened or
1508 * JB_ERR_MODIFIED if version checking was requested and
1509 * failed - the file was modified outside
1510 * of this CGI editor instance.
1512 *********************************************************************/
1513 jb_err edit_read_file(struct client_state *csp,
1514 const struct map *parameters,
1515 int require_version,
1516 struct editable_file **pfile)
1518 struct file_line * lines;
1521 const char *filename = NULL;
1522 struct editable_file * file;
1523 unsigned version = 0;
1524 struct stat statbuf[1];
1525 char version_buf[22];
1526 int newline = NEWLINE_UNKNOWN;
1535 err = get_number_param(csp, parameters, "f", &i);
1536 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1538 filename = csp->config->actions_file[i];
1540 else if (JB_ERR_CGI_PARAMS == err)
1543 * Probably an old-school URL like
1544 * http://config.privoxy.org/edit-actions-list?f=default
1546 get_file_name_param(csp, parameters, "f", &filename);
1549 if (NULL == filename || stat(filename, statbuf) < 0)
1551 /* Error, probably file not found. */
1554 version = (unsigned) statbuf->st_mtime;
1556 if (require_version)
1558 unsigned specified_version;
1559 err = get_number_param(csp, parameters, "v", &specified_version);
1565 if (version != specified_version)
1567 return JB_ERR_MODIFIED;
1571 if (NULL == (fp = fopen(filename,"rb")))
1576 err = edit_read_file_lines(fp, &lines, &newline);
1585 file = zalloc_or_die(sizeof(*file));
1587 file->lines = lines;
1588 file->newline = newline;
1589 file->filename = filename;
1590 file->version = version;
1591 file->identifier = i;
1593 /* Correct file->version_str */
1594 freez(file->version_str);
1595 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1596 version_buf[sizeof(version_buf)-1] = '\0';
1597 file->version_str = strdup_or_die(version_buf);
1604 /*********************************************************************
1606 * Function : edit_read_actions_file
1608 * Description : Read a complete actions file into memory.
1609 * Handles CGI parameter parsing. If requested, also
1610 * checks the file's modification timestamp.
1612 * If this function detects an error in the categories
1613 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1614 * then it handles it by filling in the specified
1615 * response structure and returning JB_ERR_FILE.
1618 * 1 : csp = Current client state (buffers, headers, etc...)
1619 * 2 : rsp = HTTP response. Only filled in on error.
1620 * 2 : parameters = map of cgi parameters.
1621 * 3 : require_version = true to check "ver" parameter.
1622 * 4 : pfile = Destination for the file. Will be set
1626 * f : The actions file identifier.
1627 * ver : (Only if require_version is nonzero)
1628 * Timestamp of the actions file. If wrong, this
1629 * function fails with JB_ERR_MODIFIED.
1631 * Returns : JB_ERR_OK on success
1632 * JB_ERR_MEMORY on out-of-memory
1633 * JB_ERR_CGI_PARAMS if "filename" was not specified
1635 * JB_ERR_FILE if the file does not contain valid data,
1636 * or if file cannot be opened or
1637 * contains no data, or if version
1638 * checking was requested and failed.
1640 *********************************************************************/
1641 jb_err edit_read_actions_file(struct client_state *csp,
1642 struct http_response *rsp,
1643 const struct map *parameters,
1644 int require_version,
1645 struct editable_file **pfile)
1648 struct editable_file *file;
1649 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
1657 err = edit_read_file(csp, parameters, require_version, &file);
1660 /* Try to handle if possible */
1661 if (err == JB_ERR_FILE)
1663 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1665 else if (err == JB_ERR_MODIFIED)
1667 assert(require_version);
1668 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1669 log_error(LOG_LEVEL_ERROR,
1670 "Blocking CGI edit request due to modification time mismatch.");
1671 if (acceptable_failures > 0)
1673 log_error(LOG_LEVEL_INFO,
1674 "The CGI editor will be turned off after another %d mismatche(s).",
1675 acceptable_failures);
1676 acceptable_failures--;
1680 log_error(LOG_LEVEL_INFO,
1681 "Timestamp mismatch limit reached, turning CGI editor off. "
1682 "Reload the configuration file to re-enable it.");
1683 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
1686 if (err == JB_ERR_OK)
1689 * Signal to higher-level CGI code that there was a problem but we
1690 * handled it, they should just return JB_ERR_OK.
1697 err = edit_parse_actions_file(file);
1700 if (err == JB_ERR_PARSE)
1702 err = cgi_error_parse(csp, rsp, file);
1703 if (err == JB_ERR_OK)
1706 * Signal to higher-level CGI code that there was a problem but we
1707 * handled it, they should just return JB_ERR_OK.
1712 edit_free_file(file);
1721 /*********************************************************************
1723 * Function : get_file_name_param
1725 * Description : Get the name of the file to edit from the parameters
1726 * passed to a CGI function using the old syntax.
1727 * This function handles security checks and only
1728 * accepts files that Privoxy already knows.
1731 * 1 : csp = Current client state (buffers, headers, etc...)
1732 * 2 : parameters = map of cgi parameters
1733 * 3 : param_name = The name of the parameter to read
1734 * 4 : pfilename = pointer to the filename in
1735 * csp->config->actions_file[] if found. Set to NULL on error.
1737 * Returns : JB_ERR_OK on success
1738 * JB_ERR_MEMORY on out-of-memory
1739 * JB_ERR_CGI_PARAMS if "filename" was not specified
1742 *********************************************************************/
1743 static jb_err get_file_name_param(struct client_state *csp,
1744 const struct map *parameters,
1745 const char *param_name,
1746 const char **pfilename)
1749 const char suffix[] = ".action";
1764 param = lookup(parameters, param_name);
1767 return JB_ERR_CGI_PARAMS;
1770 len = strlen(param);
1771 if (len >= FILENAME_MAX)
1774 return JB_ERR_CGI_PARAMS;
1778 * Check every character to see if it's legal.
1779 * Totally unnecessary but we do it anyway.
1782 while ((ch = *s++) != '\0')
1784 if ( ((ch < 'A') || (ch > 'Z'))
1785 && ((ch < 'a') || (ch > 'z'))
1786 && ((ch < '0') || (ch > '9'))
1790 /* Probable hack attempt. */
1791 return JB_ERR_CGI_PARAMS;
1795 /* Append extension */
1796 name_size = len + strlen(suffix) + 1;
1797 name = malloc_or_die(name_size);
1798 strlcpy(name, param, name_size);
1799 strlcat(name, suffix, name_size);
1802 fullpath = make_path(csp->config->confdir, name);
1805 if (fullpath == NULL)
1807 return JB_ERR_MEMORY;
1810 /* Check if the file is known */
1811 for (i = 0; i < MAX_AF_FILES; i++)
1813 if (NULL != csp->config->actions_file[i] &&
1814 !strcmp(fullpath, csp->config->actions_file[i]))
1817 *pfilename = csp->config->actions_file[i];
1825 return JB_ERR_CGI_PARAMS;
1829 /*********************************************************************
1831 * Function : get_url_spec_param
1833 * Description : Get a URL pattern from the parameters
1834 * passed to a CGI function. Removes leading/trailing
1835 * spaces and validates it.
1838 * 1 : csp = Current client state (buffers, headers, etc...)
1839 * 2 : parameters = map of cgi parameters
1840 * 3 : name = Name of CGI parameter to read
1841 * 4 : pvalue = destination for value. Will be malloc()'d.
1842 * Set to NULL on error.
1844 * Returns : JB_ERR_OK on success
1845 * JB_ERR_MEMORY on out-of-memory
1846 * JB_ERR_CGI_PARAMS if the parameter was not specified
1849 *********************************************************************/
1850 static jb_err get_url_spec_param(struct client_state *csp,
1851 const struct map *parameters,
1855 const char *orig_param;
1858 struct pattern_spec compiled[1];
1868 orig_param = lookup(parameters, name);
1871 return JB_ERR_CGI_PARAMS;
1874 /* Copy and trim whitespace */
1875 param = strdup(orig_param);
1878 return JB_ERR_MEMORY;
1882 /* Must be non-empty, and can't allow 1st character to be '{' */
1883 if (param[0] == '\0' || param[0] == '{')
1886 return JB_ERR_CGI_PARAMS;
1889 /* Check for embedded newlines */
1890 for (s = param; *s != '\0'; s++)
1892 if ((*s == '\r') || (*s == '\n'))
1895 return JB_ERR_CGI_PARAMS;
1899 /* Check that regex is valid */
1904 return JB_ERR_MEMORY;
1906 err = create_pattern_spec(compiled, s);
1908 free_pattern_spec(compiled);
1912 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1915 if (param[strlen(param) - 1] == '\\')
1918 * Must protect trailing '\\' from becoming line continuation character.
1919 * Two methods: 1) If it's a domain only, add a trailing '/'.
1920 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1922 if (strchr(param, '/') == NULL)
1924 err = string_append(¶m, "/");
1928 err = string_append(¶m, "(?:)");
1935 /* Check that the modified regex is valid */
1940 return JB_ERR_MEMORY;
1942 err = create_pattern_spec(compiled, s);
1944 free_pattern_spec(compiled);
1948 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1956 /*********************************************************************
1958 * Function : map_radio
1960 * Description : Map a set of radio button values. E.g. if you have
1961 * 3 radio buttons, declare them as:
1962 * <option type="radio" name="xyz" @xyz-a@>
1963 * <option type="radio" name="xyz" @xyz-b@>
1964 * <option type="radio" name="xyz" @xyz-c@>
1965 * Then map one of the @xyz-?@ variables to "checked"
1966 * and all the others to empty by calling:
1967 * map_radio(exports, "xyz", "abc", sel)
1968 * Where 'sel' is 'a', 'b', or 'c'.
1971 * 1 : exports = Exports map to modify.
1972 * 2 : optionname = name for map
1973 * 3 : values = null-terminated list of values;
1974 * 4 : value = Selected value.
1976 * CGI Parameters : None
1978 * Returns : JB_ERR_OK on success
1979 * JB_ERR_MEMORY on out-of-memory
1981 *********************************************************************/
1982 static jb_err map_radio(struct map * exports,
1983 const char * optionname,
1984 const char * values,
1990 const size_t len = strlen(optionname);
1991 const size_t buf_size = len + 3;
1997 buf = malloc_or_die(buf_size);
1999 strlcpy(buf, optionname, buf_size);
2001 /* XXX: this looks ... interesting */
2006 while ((c = *values++) != '\0')
2011 if (map(exports, buf, 1, "", 1))
2013 return JB_ERR_MEMORY;
2019 return map(exports, buf, 0, "checked", 1);
2023 /*********************************************************************
2025 * Function : cgi_error_modified
2027 * Description : CGI function that is called when a file is modified
2028 * outside the CGI editor.
2031 * 1 : csp = Current client state (buffers, headers, etc...)
2032 * 2 : rsp = http_response data structure for output
2033 * 3 : filename = The file that was modified.
2035 * CGI Parameters : none
2037 * Returns : JB_ERR_OK on success
2038 * JB_ERR_MEMORY on out-of-memory error.
2040 *********************************************************************/
2041 jb_err cgi_error_modified(struct client_state *csp,
2042 struct http_response *rsp,
2043 const char *filename)
2045 struct map *exports;
2052 if (NULL == (exports = default_exports(csp, NULL)))
2054 return JB_ERR_MEMORY;
2057 err = map(exports, "f", 1, html_encode(filename), 0);
2064 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2068 /*********************************************************************
2070 * Function : cgi_error_parse
2072 * Description : CGI function that is called when a file cannot
2073 * be parsed by the CGI editor.
2076 * 1 : csp = Current client state (buffers, headers, etc...)
2077 * 2 : rsp = http_response data structure for output
2078 * 3 : file = The file that was modified.
2080 * CGI Parameters : none
2082 * Returns : JB_ERR_OK on success
2083 * JB_ERR_MEMORY on out-of-memory error.
2085 *********************************************************************/
2086 jb_err cgi_error_parse(struct client_state *csp,
2087 struct http_response *rsp,
2088 struct editable_file *file)
2090 struct map *exports;
2092 struct file_line *cur_line;
2098 if (NULL == (exports = default_exports(csp, NULL)))
2100 return JB_ERR_MEMORY;
2103 err = map(exports, "f", 1, stringify(file->identifier), 0);
2104 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2106 cur_line = file->parse_error;
2109 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2110 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2118 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2122 /*********************************************************************
2124 * Function : cgi_error_file
2126 * Description : CGI function that is called when a file cannot be
2127 * opened by the CGI editor.
2130 * 1 : csp = Current client state (buffers, headers, etc...)
2131 * 2 : rsp = http_response data structure for output
2132 * 3 : filename = The file that was modified.
2134 * CGI Parameters : none
2136 * Returns : JB_ERR_OK on success
2137 * JB_ERR_MEMORY on out-of-memory error.
2139 *********************************************************************/
2140 jb_err cgi_error_file(struct client_state *csp,
2141 struct http_response *rsp,
2142 const char *filename)
2144 struct map *exports;
2151 if (NULL == (exports = default_exports(csp, NULL)))
2153 return JB_ERR_MEMORY;
2156 err = map(exports, "f", 1, html_encode(filename), 0);
2163 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2167 /*********************************************************************
2169 * Function : cgi_error_file_read_only
2171 * Description : CGI function that is called when a file cannot be
2172 * opened for writing by the CGI editor.
2175 * 1 : csp = Current client state (buffers, headers, etc...)
2176 * 2 : rsp = http_response data structure for output
2177 * 3 : filename = The file that we can't write to
2179 * CGI Parameters : none
2181 * Returns : JB_ERR_OK on success
2182 * JB_ERR_MEMORY on out-of-memory error.
2184 *********************************************************************/
2185 jb_err cgi_error_file_read_only(struct client_state *csp,
2186 struct http_response *rsp,
2187 const char *filename)
2189 struct map *exports;
2196 if (NULL == (exports = default_exports(csp, NULL)))
2198 return JB_ERR_MEMORY;
2201 err = map(exports, "f", 1, html_encode(filename), 0);
2208 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2212 /*********************************************************************
2214 * Function : cgi_edit_actions
2216 * Description : CGI function that allows the user to choose which
2217 * actions file to edit.
2220 * 1 : csp = Current client state (buffers, headers, etc...)
2221 * 2 : rsp = http_response data structure for output
2222 * 3 : parameters = map of cgi parameters
2224 * CGI Parameters : None
2226 * Returns : JB_ERR_OK on success
2227 * JB_ERR_MEMORY on out-of-memory error
2229 *********************************************************************/
2230 jb_err cgi_edit_actions(struct client_state *csp,
2231 struct http_response *rsp,
2232 const struct map *parameters)
2236 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2238 return cgi_error_disabled(csp, rsp);
2241 /* FIXME: Incomplete */
2243 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2248 /*********************************************************************
2250 * Function : cgi_edit_actions_list
2252 * Description : CGI function that edits the actions list.
2253 * FIXME: This function shouldn't FATAL ever.
2254 * FIXME: This function doesn't check the retval of map()
2256 * 1 : csp = Current client state (buffers, headers, etc...)
2257 * 2 : rsp = http_response data structure for output
2258 * 3 : parameters = map of cgi parameters
2260 * CGI Parameters : filename
2262 * Returns : JB_ERR_OK on success
2263 * JB_ERR_MEMORY on out-of-memory
2264 * JB_ERR_FILE if the file cannot be opened or
2266 * JB_ERR_CGI_PARAMS if "filename" was not specified
2269 *********************************************************************/
2270 jb_err cgi_edit_actions_list(struct client_state *csp,
2271 struct http_response *rsp,
2272 const struct map *parameters)
2274 char * section_template;
2275 char * url_template;
2280 struct map * exports;
2281 struct map * section_exports;
2282 struct map * url_exports;
2283 struct editable_file * file;
2284 struct file_line * cur_line;
2285 unsigned line_number = 0;
2286 unsigned prev_section_line_number = ((unsigned) (-1));
2288 struct file_list * fl;
2289 struct url_actions * b;
2290 char * buttons = NULL;
2293 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2295 return cgi_error_disabled(csp, rsp);
2298 if (NULL == (exports = default_exports(csp, NULL)))
2300 return JB_ERR_MEMORY;
2303 /* Load actions file */
2304 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2307 /* No filename specified, can't read file, or out of memory. */
2309 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2312 /* Find start of actions in file */
2313 cur_line = file->lines;
2315 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2317 cur_line = cur_line->next;
2322 * Conventional actions files should have a match all block
2324 * cur_line = {...global actions...}
2325 * cur_line->next = /
2326 * cur_line->next->next = {...actions...} or EOF
2328 if ( (cur_line != NULL)
2329 && (cur_line->type == FILE_LINE_ACTION)
2330 && (cur_line->next != NULL)
2331 && (cur_line->next->type == FILE_LINE_URL)
2332 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2333 && ( (cur_line->next->next == NULL)
2334 || (cur_line->next->next->type != FILE_LINE_URL)
2338 * Generate string with buttons to set actions for "/" to
2339 * any predefined set of actions (named standard.*, probably
2340 * residing in standard.action).
2343 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2346 edit_free_file(file);
2348 if (err == JB_ERR_FILE)
2350 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2355 err = template_fill(§ion_template, exports);
2358 edit_free_file(file);
2363 buttons = strdup("");
2364 for (i = 0; i < MAX_AF_FILES; i++)
2366 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2368 for (b = b->next; NULL != b; b = b->next)
2370 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2375 free(section_template);
2376 edit_free_file(file);
2378 return JB_ERR_MEMORY;
2381 section_exports = new_map();
2382 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2384 if (err || (NULL == (s = strdup(section_template))))
2386 free_map(section_exports);
2388 free(section_template);
2389 edit_free_file(file);
2391 return JB_ERR_MEMORY;
2394 if (!err) err = template_fill(&s, section_exports);
2395 free_map(section_exports);
2396 if (!err) err = string_join(&buttons, s);
2401 freez(section_template);
2402 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2405 * Conventional actions file, supply extra editing help.
2406 * (e.g. don't allow them to make it an unconventional one).
2408 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2410 snprintf(buf, sizeof(buf), "%u", line_number);
2411 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2412 snprintf(buf, sizeof(buf), "%u", line_number + 2);
2413 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2414 if (!err) err = map(exports, "all-urls-actions", 1,
2415 actions_to_html(csp, cur_line->data.action), 0);
2417 /* Skip the 2 lines */
2418 cur_line = cur_line->next->next;
2422 * Note that prev_section_line_number is NOT set here.
2423 * This is deliberate and not a bug. It stops a "Move up"
2424 * option appearing on the next section. Clicking "Move
2425 * up" would make the actions file unconventional, which
2426 * we don't want, so we hide this option.
2432 * Non-standard actions file - does not begin with
2433 * the "All URLs" section.
2435 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2438 /* Set up global exports */
2440 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2441 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2442 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2444 /* Discourage private additions to default.action */
2446 if (!err) err = map_conditional(exports, "default-action",
2447 (strstr("default.action", file->filename) != NULL));
2450 edit_free_file(file);
2455 /* Should do all global exports above this point */
2457 /* Load templates */
2459 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2462 edit_free_file(file);
2464 if (err == JB_ERR_FILE)
2466 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2471 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2474 free(section_template);
2475 edit_free_file(file);
2477 if (err == JB_ERR_FILE)
2479 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2484 err = template_fill(§ion_template, exports);
2488 edit_free_file(file);
2493 err = template_fill(&url_template, exports);
2496 free(section_template);
2497 edit_free_file(file);
2502 if (NULL == (sections = strdup("")))
2504 free(section_template);
2506 edit_free_file(file);
2508 return JB_ERR_MEMORY;
2511 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2513 section_exports = new_map();
2515 snprintf(buf, sizeof(buf), "%u", line_number);
2516 err = map(section_exports, "s", 1, buf, 1);
2517 if (!err) err = map(section_exports, "actions", 1,
2518 actions_to_html(csp, cur_line->data.action), 0);
2521 && (cur_line->next != NULL)
2522 && (cur_line->next->type == FILE_LINE_URL))
2524 /* This section contains at least one URL, don't allow delete */
2525 err = map_block_killer(section_exports, "empty-section");
2529 if (!err) err = map_block_keep(section_exports, "empty-section");
2532 if (prev_section_line_number != ((unsigned)(-1)))
2534 /* Not last section */
2535 snprintf(buf, sizeof(buf), "%u", prev_section_line_number);
2536 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2537 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2542 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2544 prev_section_line_number = line_number;
2549 free(section_template);
2551 edit_free_file(file);
2553 free_map(section_exports);
2557 /* Should do all section-specific exports above this point */
2559 if (NULL == (urls = strdup("")))
2562 free(section_template);
2564 edit_free_file(file);
2566 free_map(section_exports);
2567 return JB_ERR_MEMORY;
2572 cur_line = cur_line->next;
2575 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2577 url_exports = new_map();
2579 snprintf(buf, sizeof(buf), "%u", line_number);
2580 err = map(url_exports, "p", 1, buf, 1);
2582 snprintf(buf, sizeof(buf), "%d", url_1_2);
2583 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2585 if (!err) err = map(url_exports, "url-html", 1,
2586 html_encode(cur_line->unprocessed), 0);
2587 if (!err) err = map(url_exports, "url", 1,
2588 url_encode(cur_line->unprocessed), 0);
2594 free(section_template);
2596 edit_free_file(file);
2598 free_map(section_exports);
2599 free_map(url_exports);
2603 if (NULL == (s = strdup(url_template)))
2607 free(section_template);
2609 edit_free_file(file);
2611 free_map(section_exports);
2612 free_map(url_exports);
2613 return JB_ERR_MEMORY;
2616 err = template_fill(&s, section_exports);
2617 if (!err) err = template_fill(&s, url_exports);
2618 if (!err) err = string_append(&urls, s);
2620 free_map(url_exports);
2627 free(section_template);
2629 edit_free_file(file);
2631 free_map(section_exports);
2635 url_1_2 = 3 - url_1_2;
2637 cur_line = cur_line->next;
2641 err = map(section_exports, "urls", 1, urls, 0);
2643 /* Could also do section-specific exports here, but it wouldn't be as fast */
2645 snprintf(buf, sizeof(buf), "%u", line_number);
2646 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2648 if ((cur_line != NULL)
2649 && (cur_line->type == FILE_LINE_ACTION))
2651 /* Not last section */
2652 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2657 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2663 free(section_template);
2665 edit_free_file(file);
2667 free_map(section_exports);
2671 if (NULL == (s = strdup(section_template)))
2674 free(section_template);
2676 edit_free_file(file);
2678 free_map(section_exports);
2679 return JB_ERR_MEMORY;
2682 err = template_fill(&s, section_exports);
2683 if (!err) err = string_append(§ions, s);
2686 free_map(section_exports);
2691 free(section_template);
2693 edit_free_file(file);
2699 edit_free_file(file);
2700 free(section_template);
2703 err = map(exports, "sections", 1, sections, 0);
2710 /* Could also do global exports here, but it wouldn't be as fast */
2712 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2716 /*********************************************************************
2718 * Function : cgi_edit_actions_for_url
2720 * Description : CGI function that edits the Actions list.
2723 * 1 : csp = Current client state (buffers, headers, etc...)
2724 * 2 : rsp = http_response data structure for output
2725 * 3 : parameters = map of cgi parameters
2727 * CGI Parameters : None
2729 * Returns : JB_ERR_OK on success
2730 * JB_ERR_MEMORY on out-of-memory
2731 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2732 * specified or not valid.
2734 *********************************************************************/
2735 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2736 struct http_response *rsp,
2737 const struct map *parameters)
2739 struct map * exports;
2740 char *filter_template;
2742 struct editable_file * file;
2743 struct file_line * cur_line;
2744 unsigned line_number;
2746 struct re_filterfile_spec *filter_group;
2747 int i, have_filters = 0;
2749 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2751 return cgi_error_disabled(csp, rsp);
2754 err = get_number_param(csp, parameters, "s", §ionid);
2760 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2763 /* No filename specified, can't read file, modified, or out of memory. */
2764 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2767 cur_line = file->lines;
2769 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2771 cur_line = cur_line->next;
2774 if ( (cur_line == NULL)
2775 || (line_number != sectionid)
2777 || (cur_line->type != FILE_LINE_ACTION))
2779 /* Invalid "sectionid" parameter */
2780 edit_free_file(file);
2781 return JB_ERR_CGI_PARAMS;
2784 if (NULL == (exports = default_exports(csp, NULL)))
2786 edit_free_file(file);
2787 return JB_ERR_MEMORY;
2790 err = template_load(csp, &filter_template, "edit-actions-for-url-string-action", 0);
2793 edit_free_file(file);
2795 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-string-action");
2798 err = map(exports, "f", 1, stringify(file->identifier), 0);
2799 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2800 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2802 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2804 for (i = 0; !err && i < SZ(string_action_type_info); i++)
2806 err = action_render_string_actions_template(exports,
2807 cur_line->data.action, filter_template, &string_action_type_info[i]);
2809 freez(filter_template);
2812 * XXX: Some browsers (at least IE6 and IE7) have an artificial URL
2813 * length limitation and ignore clicks on the Submit buttons if
2814 * the resulting GET URL would be longer than their limit.
2816 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
2817 * reached this limit and action editing stopped working in these
2818 * browsers (BR #1570678).
2820 * The config option split-large-forms works around this browser
2821 * bug (HTTP has no URL length limitation) by dividing the action
2822 * list form into multiple smaller ones. It means the URLs are shorter
2823 * and work in broken browsers as well, but the user can no longer change
2824 * all actions with one submit.
2826 * A better solution would be to switch to POST requests,
2827 * but this will do for now.
2829 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
2831 /* Generate multiple smaller form by killing the big one. */
2832 err = map_block_killer(exports, "one-form-only");
2836 /* Default: Generate one large form by killing the smaller ones. */
2837 err = map_block_killer(exports, "multiple-forms");
2840 for (i = 0; i < MAX_AF_FILES; i++)
2842 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2844 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
2850 #ifndef FEATURE_EXTERNAL_FILTERS
2851 if (!err) err = map_block_killer(exports, "external-content-filters");
2853 #ifndef FEATURE_HTTPS_INSPECTION
2854 if (!err) err = map_block_killer(exports, "https-inspection");
2859 edit_free_file(file);
2864 if (0 == have_filters)
2866 err = map(exports, "filter-params", 1, "", 1);
2871 * List available filters and their settings.
2873 int filter_identifier = 0;
2874 char *prepared_templates[MAX_FILTER_TYPES];
2876 for (i = 0; i < MAX_FILTER_TYPES; i++)
2878 prepared_templates[i] = strdup("");
2881 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2884 edit_free_file(file);
2886 if (err == JB_ERR_FILE)
2888 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2893 err = template_fill(&filter_template, exports);
2895 for (i = 0; i < MAX_AF_FILES; i++)
2897 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2899 filter_group = csp->rlist[i]->f;
2900 for (; (!err) && (filter_group != NULL); filter_group = filter_group->next)
2902 char current_mode = 'x';
2904 struct list_entry *filter_name;
2905 struct map *line_exports;
2906 const enum filter_type type = filter_group->type;
2907 const int multi_action_index = action_type_info[type].multi_action_index;
2909 assert(type < MAX_FILTER_TYPES);
2910 assert(multi_action_index < ACTION_MULTI_COUNT);
2912 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
2913 while ((filter_name != NULL)
2914 && (0 != strcmp(filter_group->name, filter_name->str)))
2916 filter_name = filter_name->next;
2919 if (filter_name != NULL)
2925 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
2926 while ((filter_name != NULL)
2927 && (0 != strcmp(filter_group->name, filter_name->str)))
2929 filter_name = filter_name->next;
2931 if (filter_name != NULL)
2937 /* Generate a unique serial number */
2938 snprintf(number, sizeof(number), "%x", filter_identifier++);
2939 number[sizeof(number) - 1] = '\0';
2941 line_exports = new_map();
2942 if (line_exports == NULL)
2944 err = JB_ERR_MEMORY;
2950 if (!err) err = map(line_exports, "index", 1, number, 1);
2951 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2952 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2953 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2954 if (!err) err = map(line_exports, "filter-type", 1, action_type_info[type].type, 1);
2955 if (!err) err = map(line_exports, "abbr-action-type", 1, action_type_info[type].abbr_type, 1);
2956 if (!err) err = map(line_exports, "anchor", 1, action_type_info[type].anchor, 1);
2960 filter_line = strdup(filter_template);
2961 if (filter_line == NULL) err = JB_ERR_MEMORY;
2963 if (!err) err = template_fill(&filter_line, line_exports);
2964 if (!err) err = string_join(&prepared_templates[type], filter_line);
2966 free_map(line_exports);
2971 freez(filter_template);
2973 /* Replace all filter macros with the aggregated templates */
2974 for (i = 0; i < MAX_FILTER_TYPES; i++)
2977 err = map(exports, action_type_info[i].macro_name, 1, prepared_templates[i], 0);
2982 /* Free aggregated templates */
2983 for (i = 0; i < MAX_FILTER_TYPES; i++)
2985 freez(prepared_templates[i]);
2990 /* Check or uncheck the "disable all of this type" radio buttons. */
2991 for (i = 0; i < MAX_FILTER_TYPES; i++)
2993 const int a = action_type_info[i].multi_action_index;
2994 const int disable_all = cur_line->data.action->multi_remove_all[a];
2996 err = map_radio(exports, action_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
2999 edit_free_file(file);
3007 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3011 /*********************************************************************
3013 * Function : get_number_of_filters
3015 * Description : Counts the number of filter available.
3018 * 1 : csp = Current client state (buffers, headers, etc...)
3020 * Returns : Number of filters available.
3022 *********************************************************************/
3023 static int get_number_of_filters(const struct client_state *csp)
3026 struct re_filterfile_spec *b;
3027 struct file_list *fl;
3028 int number_of_filters = 0;
3030 for (i = 0; i < MAX_AF_FILES; i++)
3033 if ((NULL == fl) || (NULL == fl->f))
3036 * Either there are no filter files left or this
3037 * filter file just contains no valid filters.
3039 * Continue to be sure we don't miss valid filter
3040 * files that are chained after empty or invalid ones.
3045 for (b = fl->f; b != NULL; b = b->next)
3047 number_of_filters++;
3051 return number_of_filters;
3056 /*********************************************************************
3058 * Function : cgi_edit_process_string_action
3060 * Description : Helper CGI function that actually edits the Actions list for
3061 * the action string parameters.
3064 * 1 : csp = Current client state (buffers, headers, etc...)
3065 * 2 : rsp = http_response data structure for output
3066 * 3 : parameters = map of cgi parameters
3067 * 4 : cur_line = current config file line
3068 * 5 : action_type = string filter type to process
3070 * Returns : JB_ERR_OK on success
3071 * JB_ERR_MEMORY on out-of-memory
3072 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3073 * specified or not valid.
3075 *********************************************************************/
3076 static jb_err cgi_edit_process_string_action(struct client_state *csp,
3077 struct http_response *rsp,
3078 const struct map *parameters,
3079 struct file_line *cur_line,
3080 enum filter_type action_type)
3082 jb_err err = JB_ERR_OK;
3083 const char *abbr_type = action_type_info[action_type].abbr_type;
3084 int action_identifier = 0;
3086 /* process existing string filter actions */
3087 for (action_identifier = 0; !err; action_identifier++)
3093 const char *name, *new_name;
3095 * Action state. Valid states are: 'Y' (active),
3096 * 'N' (inactive) and 'X' (no change).
3100 * Abbreviated filter type. Valid types are: 'U' (suppress tag), 'H' (add header)
3102 int multi_action_index = 0;
3104 /* Generate the keys */
3105 snprintf(key_value, sizeof(key_value), "string_action_%s_r%x", abbr_type, action_identifier);
3106 snprintf(key_name, sizeof(key_name), "string_action_%s_n%x", abbr_type, action_identifier);
3107 snprintf(old_name, sizeof(old_name), "string_action_%s_o%x", abbr_type, action_identifier);
3108 snprintf(key_type, sizeof(key_type), "string_action_%s_t%x", abbr_type, action_identifier);
3110 err = get_string_param(parameters, old_name, &name);
3115 /* The action identifier isn't present: we're done! */
3119 err = get_string_param(parameters, key_name, &new_name);
3121 if (new_name == NULL) new_name = name;
3123 type = get_char_param(parameters, key_type);
3127 multi_action_index = ACTION_MULTI_SUPPRESS_TAG;
3130 multi_action_index = ACTION_MULTI_ADD_HEADER;
3133 log_error(LOG_LEVEL_ERROR,
3134 "Unknown action type: %c for action %s. Action ignored.", type, name);
3138 value = get_char_param(parameters, key_value);
3139 if (value == 'X' || value == 'Y' || value == 'N')
3141 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3142 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3147 err = enlist(cur_line->data.action->multi_add[multi_action_index], new_name);
3149 else if (value == 'N')
3151 err = enlist(cur_line->data.action->multi_remove[multi_action_index], new_name);
3155 /* process new string filter actions */
3156 for (action_identifier = 0; !err; action_identifier++)
3163 * Action state. Valid states are: 'Y' (active),
3164 * 'N' (inactive) and 'X' (no change).
3168 * Abbreviated filter type. Valid types are: 'U' (suppress tag), 'H' (add header)
3170 int multi_action_index = 0;
3172 /* Generate the keys */
3173 snprintf(key_value, sizeof(key_value), "new_string_action_%s_r%x", abbr_type, action_identifier);
3174 snprintf(key_name, sizeof(key_name), "new_string_action_%s_n%x", abbr_type, action_identifier);
3175 snprintf(key_type, sizeof(key_type), "new_string_action_%s_t%x", abbr_type, action_identifier);
3177 err = get_string_param(parameters, key_name, &name);
3182 /* The action identifier isn't present: we've done! */
3186 type = get_char_param(parameters, key_type);
3190 multi_action_index = ACTION_MULTI_SUPPRESS_TAG;
3193 multi_action_index = ACTION_MULTI_ADD_HEADER;
3196 log_error(LOG_LEVEL_ERROR,
3197 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3201 value = get_char_param(parameters, key_value);
3204 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3205 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3206 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3208 else if (value == 'N')
3210 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3211 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3212 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3214 /* nothing to do if the value is 'X' */
3220 /*********************************************************************
3222 * Function : cgi_edit_actions_submit
3224 * Description : CGI function that actually edits the Actions list.
3227 * 1 : csp = Current client state (buffers, headers, etc...)
3228 * 2 : rsp = http_response data structure for output
3229 * 3 : parameters = map of cgi parameters
3231 * CGI Parameters : None
3233 * Returns : JB_ERR_OK on success
3234 * JB_ERR_MEMORY on out-of-memory
3235 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3236 * specified or not valid.
3238 *********************************************************************/
3239 jb_err cgi_edit_actions_submit(struct client_state *csp,
3240 struct http_response *rsp,
3241 const struct map *parameters)
3246 size_t newtext_size;
3248 struct editable_file * file;
3249 struct file_line * cur_line;
3250 unsigned line_number;
3253 int filter_identifier;
3255 const char * action_set_name;
3256 struct file_list * fl;
3257 struct url_actions * b;
3258 int number_of_filters;
3260 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3262 return cgi_error_disabled(csp, rsp);
3265 err = get_number_param(csp, parameters, "s", §ionid);
3271 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3274 /* No filename specified, can't read file, modified, or out of memory. */
3275 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3278 cur_line = file->lines;
3280 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3282 cur_line = cur_line->next;
3285 if ( (cur_line == NULL)
3286 || (line_number != sectionid)
3288 || (cur_line->type != FILE_LINE_ACTION))
3290 /* Invalid "sectionid" parameter */
3291 edit_free_file(file);
3292 return JB_ERR_CGI_PARAMS;
3295 get_string_param(parameters, "p", &action_set_name);
3296 if (action_set_name != NULL)
3298 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3300 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3302 for (b = b->next; NULL != b; b = b->next)
3304 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3306 copy_action(cur_line->data.action, b->action);
3312 edit_free_file(file);
3313 return JB_ERR_CGI_PARAMS;
3319 err = actions_from_radio(parameters, cur_line->data.action);
3325 edit_free_file(file);
3329 /* Check the "disable all of this type" parameters. */
3330 for (i = 0; i < MAX_FILTER_TYPES; i++)
3332 const int multi_action_index = action_type_info[i].multi_action_index;
3333 const char ch = get_char_param(parameters, action_type_info[i].disable_all_param);
3337 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3338 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3339 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3343 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3347 number_of_filters = get_number_of_filters(csp);
3349 for (filter_identifier = 0; filter_identifier < number_of_filters && !err; filter_identifier++)
3356 * Filter state. Valid states are: 'Y' (active),
3357 * 'N' (inactive) and 'X' (no change).
3361 * Abbreviated filter type. Valid types are: 'F' (content filter),
3362 * 'S' (server-header filter) and 'C' (client-header filter).
3364 int multi_action_index = 0;
3366 /* Generate the keys */
3367 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3368 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3369 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3370 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3371 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3373 err = get_string_param(parameters, key_name, &name);
3378 /* The filter identifier isn't present. Try the next one. */
3382 type = get_char_param(parameters, key_type);
3386 multi_action_index = ACTION_MULTI_FILTER;
3389 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3392 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3395 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3398 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3401 multi_action_index = ACTION_MULTI_CLIENT_BODY_FILTER;
3404 log_error(LOG_LEVEL_ERROR,
3405 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3408 assert(multi_action_index);
3410 value = get_char_param(parameters, key_value);
3413 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3414 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3415 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3417 else if (value == 'N')
3419 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3420 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3422 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3423 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3426 else if (value == 'X')
3428 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3429 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3433 /* process new string filters */
3434 for (i = 0; !err && i < SZ(string_action_type_info); i++)
3436 err = cgi_edit_process_string_action(csp, rsp, parameters, cur_line,
3437 string_action_type_info[i].action_type);
3443 edit_free_file(file);
3447 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3450 edit_free_file(file);
3451 return JB_ERR_MEMORY;
3454 len = strlen(actiontext);
3458 * Empty action - must special-case this.
3459 * Simply setting len to 1 is sufficient...
3464 newtext_size = len + 2;
3465 newtext = malloc_or_die(newtext_size);
3466 strlcpy(newtext, actiontext, newtext_size);
3470 newtext[len + 1] = '\0';
3472 freez(cur_line->raw);
3473 freez(cur_line->unprocessed);
3474 cur_line->unprocessed = newtext;
3476 err = edit_write_file(file);
3479 /* Error writing file */
3480 if (err == JB_ERR_FILE)
3482 /* Read-only file. */
3483 err = cgi_error_file_read_only(csp, rsp, file->filename);
3485 edit_free_file(file);
3489 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
3490 (unsigned long) time(NULL), file->identifier, sectionid);
3492 edit_free_file(file);
3494 return cgi_redirect(rsp, target);
3498 /*********************************************************************
3500 * Function : cgi_edit_actions_url
3502 * Description : CGI function that actually edits a URL pattern in
3506 * 1 : csp = Current client state (buffers, headers, etc...)
3507 * 2 : rsp = http_response data structure for output
3508 * 3 : parameters = map of cgi parameters
3511 * filename : Identifies the file to edit
3512 * ver : File's last-modified time
3513 * section : Line number of section to edit
3514 * pattern : Line number of pattern to edit
3515 * newval : New value for pattern
3517 * Returns : JB_ERR_OK on success
3518 * JB_ERR_MEMORY on out-of-memory
3519 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3520 * specified or not valid.
3522 *********************************************************************/
3523 jb_err cgi_edit_actions_url(struct client_state *csp,
3524 struct http_response *rsp,
3525 const struct map *parameters)
3529 struct editable_file * file;
3530 struct file_line * cur_line;
3531 unsigned line_number;
3532 unsigned section_start_line_number = 0;
3540 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3542 return cgi_error_disabled(csp, rsp);
3545 err = get_number_param(csp, parameters, "p", &patternid);
3552 return JB_ERR_CGI_PARAMS;
3555 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3561 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3564 /* No filename specified, can't read file, modified, or out of memory. */
3566 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3570 cur_line = file->lines;
3572 while ((cur_line != NULL) && (line_number < patternid))
3574 if (cur_line->type == FILE_LINE_ACTION)
3576 section_start_line_number = line_number;
3578 cur_line = cur_line->next;
3582 if ((cur_line == NULL)
3583 || (cur_line->type != FILE_LINE_URL))
3585 /* Invalid "patternid" parameter */
3587 edit_free_file(file);
3588 return JB_ERR_CGI_PARAMS;
3591 /* At this point, the line to edit is in cur_line */
3593 freez(cur_line->raw);
3594 freez(cur_line->unprocessed);
3595 cur_line->unprocessed = new_pattern;
3597 err = edit_write_file(file);
3600 /* Error writing file */
3601 if (err == JB_ERR_FILE)
3603 /* Read-only file. */
3604 err = cgi_error_file_read_only(csp, rsp, file->filename);
3606 edit_free_file(file);
3610 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
3611 (unsigned long) time(NULL), file->identifier, section_start_line_number);
3613 edit_free_file(file);
3615 return cgi_redirect(rsp, target);
3619 /*********************************************************************
3621 * Function : cgi_edit_actions_add_url
3623 * Description : CGI function that actually adds a URL pattern to
3627 * 1 : csp = Current client state (buffers, headers, etc...)
3628 * 2 : rsp = http_response data structure for output
3629 * 3 : parameters = map of cgi parameters
3632 * filename : Identifies the file to edit
3633 * ver : File's last-modified time
3634 * section : Line number of section to edit
3635 * newval : New pattern
3637 * Returns : JB_ERR_OK on success
3638 * JB_ERR_MEMORY on out-of-memory
3639 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3640 * specified or not valid.
3642 *********************************************************************/
3643 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3644 struct http_response *rsp,
3645 const struct map *parameters)
3649 struct file_line * new_line;
3650 struct editable_file * file;
3651 struct file_line * cur_line;
3652 unsigned line_number;
3656 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3658 return cgi_error_disabled(csp, rsp);
3661 err = get_number_param(csp, parameters, "s", §ionid);
3668 return JB_ERR_CGI_PARAMS;
3671 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3677 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3680 /* No filename specified, can't read file, modified, or out of memory. */
3682 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3686 cur_line = file->lines;
3688 while ((cur_line != NULL) && (line_number < sectionid))
3690 cur_line = cur_line->next;
3694 if ((cur_line == NULL)
3695 || (cur_line->type != FILE_LINE_ACTION))
3697 /* Invalid "sectionid" parameter */
3699 edit_free_file(file);
3700 return JB_ERR_CGI_PARAMS;
3703 /* At this point, the section header is in cur_line - add after this. */
3705 /* Allocate the new line */
3706 new_line = zalloc_or_die(sizeof(*new_line));
3708 /* Fill in the data members of the new line */
3709 new_line->raw = NULL;
3710 new_line->prefix = NULL;
3711 new_line->unprocessed = new_pattern;
3712 new_line->type = FILE_LINE_URL;
3714 /* Link new_line into the list, after cur_line */
3715 new_line->next = cur_line->next;
3716 cur_line->next = new_line;
3718 /* Done making changes, now commit */
3720 err = edit_write_file(file);
3723 /* Error writing file */
3724 if (err == JB_ERR_FILE)
3726 /* Read-only file. */
3727 err = cgi_error_file_read_only(csp, rsp, file->filename);
3729 edit_free_file(file);
3733 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
3734 (unsigned long) time(NULL), file->identifier, sectionid);
3736 edit_free_file(file);
3738 return cgi_redirect(rsp, target);
3742 /*********************************************************************
3744 * Function : cgi_edit_actions_remove_url
3746 * Description : CGI function that actually removes a URL pattern from
3750 * 1 : csp = Current client state (buffers, headers, etc...)
3751 * 2 : rsp = http_response data structure for output
3752 * 3 : parameters = map of cgi parameters
3755 * f : (filename) Identifies the file to edit
3756 * v : (version) File's last-modified time
3757 * p : (pattern) Line number of pattern to remove
3759 * Returns : JB_ERR_OK on success
3760 * JB_ERR_MEMORY on out-of-memory
3761 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3762 * specified or not valid.
3764 *********************************************************************/
3765 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3766 struct http_response *rsp,
3767 const struct map *parameters)
3770 struct editable_file * file;
3771 struct file_line * cur_line;
3772 struct file_line * prev_line;
3773 unsigned line_number;
3774 unsigned section_start_line_number = 0;
3778 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3780 return cgi_error_disabled(csp, rsp);
3783 err = get_number_param(csp, parameters, "p", &patternid);
3789 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3792 /* No filename specified, can't read file, modified, or out of memory. */
3793 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3798 cur_line = file->lines;
3800 while ((cur_line != NULL) && (line_number < patternid))
3802 if (cur_line->type == FILE_LINE_ACTION)
3804 section_start_line_number = line_number;
3806 prev_line = cur_line;
3807 cur_line = cur_line->next;
3811 if ( (cur_line == NULL)
3812 || (prev_line == NULL)
3813 || (cur_line->type != FILE_LINE_URL))
3815 /* Invalid "patternid" parameter */
3816 edit_free_file(file);
3817 return JB_ERR_CGI_PARAMS;
3820 /* At this point, the line to remove is in cur_line, and the previous
3821 * one is in prev_line
3824 /* Unlink cur_line */
3825 prev_line->next = cur_line->next;
3826 cur_line->next = NULL;
3829 edit_free_file_lines(cur_line);
3831 err = edit_write_file(file);
3834 /* Error writing file */
3835 if (err == JB_ERR_FILE)
3837 /* Read-only file. */
3838 err = cgi_error_file_read_only(csp, rsp, file->filename);
3840 edit_free_file(file);
3844 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
3845 (unsigned long) time(NULL), file->identifier, section_start_line_number);
3847 edit_free_file(file);
3849 return cgi_redirect(rsp, target);
3853 /*********************************************************************
3855 * Function : cgi_edit_actions_section_remove
3857 * Description : CGI function that actually removes a whole section from
3858 * the actions file. The section must be empty first
3859 * (else JB_ERR_CGI_PARAMS).
3862 * 1 : csp = Current client state (buffers, headers, etc...)
3863 * 2 : rsp = http_response data structure for output
3864 * 3 : parameters = map of cgi parameters
3867 * f : (filename) Identifies the file to edit
3868 * v : (version) File's last-modified time
3869 * s : (section) Line number of section to edit
3871 * Returns : JB_ERR_OK on success
3872 * JB_ERR_MEMORY on out-of-memory
3873 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3874 * specified or not valid.
3876 *********************************************************************/
3877 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3878 struct http_response *rsp,
3879 const struct map *parameters)
3882 struct editable_file * file;
3883 struct file_line * cur_line;
3884 struct file_line * prev_line;
3885 unsigned line_number;
3889 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3891 return cgi_error_disabled(csp, rsp);
3894 err = get_number_param(csp, parameters, "s", §ionid);
3900 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3903 /* No filename specified, can't read file, modified, or out of memory. */
3904 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3908 cur_line = file->lines;
3911 while ((cur_line != NULL) && (line_number < sectionid))
3913 prev_line = cur_line;
3914 cur_line = cur_line->next;
3918 if ((cur_line == NULL)
3919 || (cur_line->type != FILE_LINE_ACTION))
3921 /* Invalid "sectionid" parameter */
3922 edit_free_file(file);
3923 return JB_ERR_CGI_PARAMS;
3926 if ((cur_line->next != NULL)
3927 && (cur_line->next->type == FILE_LINE_URL))
3929 /* Section not empty. */
3930 edit_free_file(file);
3931 return JB_ERR_CGI_PARAMS;
3934 /* At this point, the line to remove is in cur_line, and the previous
3935 * one is in prev_line
3938 /* Unlink cur_line */
3939 if (prev_line == NULL)
3941 /* Removing the first line from the file */
3942 file->lines = cur_line->next;
3946 prev_line->next = cur_line->next;
3948 cur_line->next = NULL;
3951 edit_free_file_lines(cur_line);
3953 err = edit_write_file(file);
3956 /* Error writing file */
3957 if (err == JB_ERR_FILE)
3959 /* Read-only file. */
3960 err = cgi_error_file_read_only(csp, rsp, file->filename);
3962 edit_free_file(file);
3966 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
3967 (unsigned long) time(NULL), file->identifier);
3969 edit_free_file(file);
3971 return cgi_redirect(rsp, target);
3975 /*********************************************************************
3977 * Function : cgi_edit_actions_section_add
3979 * Description : CGI function that adds a new empty section to
3983 * 1 : csp = Current client state (buffers, headers, etc...)
3984 * 2 : rsp = http_response data structure for output
3985 * 3 : parameters = map of cgi parameters
3988 * f : (filename) Identifies the file to edit
3989 * v : (version) File's last-modified time
3990 * s : (section) Line number of section to add after, 0 for
3993 * Returns : JB_ERR_OK on success
3994 * JB_ERR_MEMORY on out-of-memory
3995 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3996 * specified or not valid.
3998 *********************************************************************/
3999 jb_err cgi_edit_actions_section_add(struct client_state *csp,
4000 struct http_response *rsp,
4001 const struct map *parameters)
4004 struct file_line * new_line;
4006 struct editable_file * file;
4007 struct file_line * cur_line;
4008 unsigned line_number;
4012 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4014 return cgi_error_disabled(csp, rsp);
4017 err = get_number_param(csp, parameters, "s", §ionid);
4023 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4026 /* No filename specified, can't read file, modified, or out of memory. */
4027 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4031 cur_line = file->lines;
4033 if (sectionid <= 1U)
4035 /* Add to start of file */
4036 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4038 /* There's something in the file, find the line before the first
4041 while ((cur_line->next != NULL)
4042 && (cur_line->next->type != FILE_LINE_ACTION))
4044 cur_line = cur_line->next;
4050 /* File starts with action line, so insert at top */
4056 /* Add after stated section. */
4057 while ((cur_line != NULL) && (line_number < sectionid))
4059 cur_line = cur_line->next;
4063 if ((cur_line == NULL)
4064 || (cur_line->type != FILE_LINE_ACTION))
4066 /* Invalid "sectionid" parameter */
4067 edit_free_file(file);
4068 return JB_ERR_CGI_PARAMS;
4071 /* Skip through the section to find the last line in it. */
4072 while ((cur_line->next != NULL)
4073 && (cur_line->next->type != FILE_LINE_ACTION))
4075 cur_line = cur_line->next;
4080 /* At this point, the last line in the previous section is in cur_line
4081 * - add after this. (Or if we need to add as the first line, cur_line
4085 new_text = strdup("{}");
4086 if (NULL == new_text)
4088 edit_free_file(file);
4089 return JB_ERR_MEMORY;
4092 /* Allocate the new line */
4093 new_line = zalloc_or_die(sizeof(*new_line));
4095 /* Fill in the data members of the new line */
4096 new_line->raw = NULL;
4097 new_line->prefix = NULL;
4098 new_line->unprocessed = new_text;
4099 new_line->type = FILE_LINE_ACTION;
4101 if (cur_line != NULL)
4103 /* Link new_line into the list, after cur_line */
4104 new_line->next = cur_line->next;
4105 cur_line->next = new_line;
4109 /* Link new_line into the list, as first line */
4110 new_line->next = file->lines;
4111 file->lines = new_line;
4114 /* Done making changes, now commit */
4116 err = edit_write_file(file);
4119 /* Error writing file */
4120 if (err == JB_ERR_FILE)
4122 /* Read-only file. */
4123 err = cgi_error_file_read_only(csp, rsp, file->filename);
4125 edit_free_file(file);
4129 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
4130 (unsigned long) time(NULL), file->identifier);
4132 edit_free_file(file);
4134 return cgi_redirect(rsp, target);
4138 /*********************************************************************
4140 * Function : cgi_edit_actions_section_swap
4142 * Description : CGI function that swaps the order of two sections
4143 * in the actions file. Note that this CGI can actually
4144 * swap any two arbitrary sections, but the GUI interface
4145 * currently only allows consecutive sections to be
4149 * 1 : csp = Current client state (buffers, headers, etc...)
4150 * 2 : rsp = http_response data structure for output
4151 * 3 : parameters = map of cgi parameters
4154 * f : (filename) Identifies the file to edit
4155 * v : (version) File's last-modified time
4156 * s1 : (section1) Line number of first section to swap
4157 * s2 : (section2) Line number of second section to swap
4159 * Returns : JB_ERR_OK on success
4160 * JB_ERR_MEMORY on out-of-memory
4161 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4162 * specified or not valid.
4164 *********************************************************************/
4165 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4166 struct http_response *rsp,
4167 const struct map *parameters)
4171 struct editable_file * file;
4172 struct file_line * cur_line;
4173 struct file_line * prev_line;
4174 struct file_line * line_before_section1;
4175 struct file_line * line_start_section1;
4176 struct file_line * line_end_section1;
4177 struct file_line * line_after_section1;
4178 struct file_line * line_before_section2;
4179 struct file_line * line_start_section2;
4180 struct file_line * line_end_section2;
4181 struct file_line * line_after_section2;
4182 unsigned line_number;
4186 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4188 return cgi_error_disabled(csp, rsp);
4191 err = get_number_param(csp, parameters, "s1", §ion1);
4192 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4198 if (section1 > section2)
4200 unsigned temp = section2;
4201 section2 = section1;
4205 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4208 /* No filename specified, can't read file, modified, or out of memory. */
4209 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4212 /* Start at the beginning... */
4214 cur_line = file->lines;
4217 /* ... find section1 ... */
4218 while ((cur_line != NULL) && (line_number < section1))
4220 prev_line = cur_line;
4221 cur_line = cur_line->next;
4225 if ((cur_line == NULL)
4226 || (cur_line->type != FILE_LINE_ACTION))
4228 /* Invalid "section1" parameter */
4229 edit_free_file(file);
4230 return JB_ERR_CGI_PARAMS;
4233 /* If no-op, we've validated params and can skip the rest. */
4234 if (section1 != section2)
4236 /* ... find the end of section1 ... */
4237 line_before_section1 = prev_line;
4238 line_start_section1 = cur_line;
4241 prev_line = cur_line;
4242 cur_line = cur_line->next;
4245 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4246 line_end_section1 = prev_line;
4247 line_after_section1 = cur_line;
4249 /* ... find section2 ... */
4250 while ((cur_line != NULL) && (line_number < section2))
4252 prev_line = cur_line;
4253 cur_line = cur_line->next;
4257 if ((cur_line == NULL)
4258 || (cur_line->type != FILE_LINE_ACTION))
4260 /* Invalid "section2" parameter */
4261 edit_free_file(file);
4262 return JB_ERR_CGI_PARAMS;
4265 /* ... find the end of section2 ... */
4266 line_before_section2 = prev_line;
4267 line_start_section2 = cur_line;
4270 prev_line = cur_line;
4271 cur_line = cur_line->next;
4274 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4275 line_end_section2 = prev_line;
4276 line_after_section2 = cur_line;
4278 /* Now have all the pointers we need. Do the swap. */
4280 /* Change the pointer to section1 to point to section2 instead */
4281 if (line_before_section1 == NULL)
4283 file->lines = line_start_section2;
4287 line_before_section1->next = line_start_section2;
4290 if (line_before_section2 == line_end_section1)
4292 /* Consecutive sections */
4293 line_end_section2->next = line_start_section1;
4297 line_end_section2->next = line_after_section1;
4298 line_before_section2->next = line_start_section1;
4301 /* Set the pointer from the end of section1 to the rest of the file */
4302 line_end_section1->next = line_after_section2;
4304 err = edit_write_file(file);
4307 /* Error writing file */
4308 if (err == JB_ERR_FILE)
4310 /* Read-only file. */
4311 err = cgi_error_file_read_only(csp, rsp, file->filename);
4313 edit_free_file(file);
4316 } /* END if (section1 != section2) */
4318 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
4319 (unsigned long) time(NULL), file->identifier);
4321 edit_free_file(file);
4323 return cgi_redirect(rsp, target);
4327 /*********************************************************************
4329 * Function : javascriptify
4331 * Description : Converts a string into a form JavaScript will like.
4333 * Netscape 4's JavaScript sucks - it doesn't use
4334 * "id" parameters, so you have to set the "name"
4335 * used to submit a form element to something JavaScript
4336 * will like. (Or access the elements by index in an
4337 * array. That array contains >60 elements and will
4338 * be changed whenever we add a new action to the
4339 * editor, so I'm NOT going to use indexes that have
4340 * to be figured out by hand.)
4342 * Currently the only thing we have to worry about
4343 * is "-" ==> "_" conversion.
4345 * This is a length-preserving operation so it is
4346 * carried out in-place, no memory is allocated
4350 * 1 : identifier = String to make JavaScript-friendly.
4354 *********************************************************************/
4355 static void javascriptify(char * identifier)
4357 char * p = identifier;
4358 while (NULL != (p = strchr(p, '-')))
4365 /*********************************************************************
4367 * Function : actions_to_radio
4369 * Description : Converts a actionsfile entry into settings for
4370 * radio buttons and edit boxes on a HTML form.
4373 * 1 : exports = List of substitutions to add to.
4374 * 2 : action = Action to read
4376 * Returns : JB_ERR_OK on success
4377 * JB_ERR_MEMORY on out-of-memory
4379 *********************************************************************/
4380 static jb_err actions_to_radio(struct map * exports,
4381 const struct action_spec *action)
4392 mask = action->mask;
4395 /* sanity - prevents "-feature +feature" */
4399 #define DEFINE_ACTION_BOOL(name, bit) \
4400 if (!(mask & bit)) \
4402 current_mode = 'n'; \
4404 else if (add & bit) \
4406 current_mode = 'y'; \
4410 current_mode = 'x'; \
4412 if (map_radio(exports, name, "ynx", current_mode)) \
4414 return JB_ERR_MEMORY; \
4417 #define DEFINE_ACTION_STRING(name, bit, index) \
4418 DEFINE_ACTION_BOOL(name, bit); \
4421 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4424 checked = !strcmp(action->string[index], value); \
4428 checked = is_default; \
4430 mapped_param |= checked; \
4431 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4433 return JB_ERR_MEMORY; \
4436 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4437 if (map(exports, name "-param-custom", 1, \
4438 ((!mapped_param) ? "checked" : ""), 1)) \
4440 return JB_ERR_MEMORY; \
4442 if (map(exports, name "-param", 1, \
4443 (((add & bit) && !mapped_param) ? \
4444 action->string[index] : default_val), 1)) \
4446 return JB_ERR_MEMORY; \
4449 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4450 if (map(exports, name "-param", 1, \
4451 ((add & bit) ? action->string[index] : default_val), 1)) \
4453 return JB_ERR_MEMORY; \
4456 #define DEFINE_ACTION_MULTI(name, index) \
4457 if (action->multi_add[index]->first) \
4459 current_mode = 'y'; \
4461 else if (action->multi_remove_all[index]) \
4463 current_mode = 'n'; \
4465 else if (action->multi_remove[index]->first) \
4467 current_mode = 'y'; \
4471 current_mode = 'x'; \
4473 if (map_radio(exports, name, "ynx", current_mode)) \
4475 return JB_ERR_MEMORY; \
4478 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4480 #include "actionlist.h"
4482 #undef DEFINE_ACTION_MULTI
4483 #undef DEFINE_ACTION_STRING
4484 #undef DEFINE_ACTION_BOOL
4485 #undef DEFINE_ACTION_ALIAS
4486 #undef DEFINE_CGI_PARAM_CUSTOM
4487 #undef DEFINE_CGI_PARAM_RADIO
4488 #undef DEFINE_CGI_PARAM_NO_RADIO
4493 /*********************************************************************
4495 * Function : action_render_string_actions_template
4497 * Description : Converts an actionsfile entry into HTML template for
4498 * actions with string filters (currently SUPPRESS-TAG
4502 * 1 : exports = List of substitutions to add to.
4503 * 2 : action = Action to read
4504 * 3 : action_template = template to fill
4505 * 4 : type = filter type info for rendered values/macro name
4507 * Returns : JB_ERR_OK on success
4508 * JB_ERR_MEMORY on out-of-memory
4510 *********************************************************************/
4511 static jb_err action_render_string_actions_template(struct map *exports,
4512 const struct action_spec *action,
4513 const char *action_template,
4514 const struct string_action_type_info *string_action_type)
4516 jb_err err = JB_ERR_OK;
4517 int filter_identifier = 0;
4519 char *prepared_template = strdup("");
4520 const struct action_type_info *type = &action_type_info[string_action_type->action_type];
4522 struct action_multi {
4524 struct list_entry *list;
4527 assert(type->multi_action_index < ACTION_MULTI_COUNT);
4529 struct action_multi desc[] = {
4530 { 'y', action->multi_add[type->multi_action_index][0].first },
4531 { 'n', action->multi_remove[type->multi_action_index][0].first }
4534 for (i = 0; i < SZ(desc); ++i)
4536 const char radio = desc[i].radio;
4537 struct list_entry *entry = desc[i].list;
4538 for (;(!err) && (entry != NULL); entry = entry->next)
4541 struct map *line_exports;
4543 /* Generate a unique serial number */
4544 snprintf(number, sizeof(number), "%x", filter_identifier++);
4546 line_exports = new_map();
4547 if (line_exports == NULL)
4549 err = JB_ERR_MEMORY;
4554 if (!err) err = map(line_exports, "index", 1, number, 1);
4555 if (!err) err = map(line_exports, "name", 1, entry->str, 1);
4556 if (!err) err = map_radio(line_exports, "this-filter", "ynx", radio);
4557 if (!err) err = map(line_exports, "filter-type", 1, type->type, 1);
4558 if (!err) err = map(line_exports, "abbr-action-type", 1, type->abbr_type, 1);
4559 if (!err) err = map(line_exports, "anchor", 1, type->anchor, 1);
4560 if (!err) err = map(line_exports, "desc", 1, string_action_type->description, 1);
4561 if (!err) err = map(line_exports, "input_desc", 1, string_action_type->input_description, 1);
4564 action_line = strdup(action_template);
4565 if (action_line == NULL) err = JB_ERR_MEMORY;
4567 if (!err) err = template_fill(&action_line, line_exports);
4568 if (!err) err = string_join(&prepared_template, action_line);
4570 free_map(line_exports);
4574 if (!err) map(exports, type->macro_name, 1, prepared_template, 1);
4575 freez(prepared_template);
4579 /*********************************************************************
4581 * Function : actions_from_radio
4583 * Description : Converts a map of parameters passed to a CGI function
4584 * into an actionsfile entry.
4587 * 1 : parameters = parameters to the CGI call
4588 * 2 : action = Action to change. Must be valid before
4589 * the call, actions not specified will be
4592 * Returns : JB_ERR_OK on success
4593 * JB_ERR_MEMORY on out-of-memory
4595 *********************************************************************/
4596 static jb_err actions_from_radio(const struct map * parameters,
4597 struct action_spec *action)
4602 const char * js_name;
4603 jb_err err = JB_ERR_OK;
4608 /* Statics are generally a potential race condition,
4609 * but in this case we're safe and don't need semaphores.
4610 * Be careful if you modify this function.
4612 * The js_name_arr's are never free()d, but this is no
4613 * problem, since they will only be created once and
4614 * used by all threads thereafter. -oes
4617 #define JAVASCRIPTIFY(dest_var, string) \
4619 static int first_time = 1; \
4620 static char *js_name_arr; \
4623 js_name_arr = strdup(string); \
4624 javascriptify(js_name_arr); \
4626 dest_var = js_name_arr; \
4630 #define DEFINE_ACTION_BOOL(name, bit) \
4631 JAVASCRIPTIFY(js_name, name); \
4632 ch = get_char_param(parameters, js_name); \
4635 action->add |= bit; \
4636 action->mask |= bit; \
4638 else if (ch == 'N') \
4640 action->add &= ~bit; \
4641 action->mask &= ~bit; \
4643 else if (ch == 'X') \
4645 action->add &= ~bit; \
4646 action->mask |= bit; \
4649 #define DEFINE_ACTION_STRING(name, bit, index) \
4650 JAVASCRIPTIFY(js_name, name); \
4651 ch = get_char_param(parameters, js_name); \
4655 JAVASCRIPTIFY(js_name, name "-mode"); \
4656 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4657 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4659 JAVASCRIPTIFY(js_name, name "-param"); \
4660 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4662 if (param != NULL) \
4664 if (NULL == (param_dup = strdup(param))) \
4666 return JB_ERR_MEMORY; \
4668 freez(action->string[index]); \
4669 action->add |= bit; \
4670 action->mask |= bit; \
4671 action->string[index] = param_dup; \
4674 else if (ch == 'N') \
4676 if (action->add & bit) \
4678 freez(action->string[index]); \
4680 action->add &= ~bit; \
4681 action->mask &= ~bit; \
4683 else if (ch == 'X') \
4685 if (action->add & bit) \
4687 freez(action->string[index]); \
4689 action->add &= ~bit; \
4690 action->mask |= bit; \
4693 #define DEFINE_ACTION_MULTI(name, index) \
4694 JAVASCRIPTIFY(js_name, name); \
4695 ch = get_char_param(parameters, js_name); \
4700 else if (ch == 'N') \
4702 list_remove_all(action->multi_add[index]); \
4703 list_remove_all(action->multi_remove[index]); \
4704 action->multi_remove_all[index] = 1; \
4706 else if (ch == 'X') \
4708 list_remove_all(action->multi_add[index]); \
4709 list_remove_all(action->multi_remove[index]); \
4710 action->multi_remove_all[index] = 0; \
4713 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4715 #include "actionlist.h"
4717 #undef DEFINE_ACTION_MULTI
4718 #undef DEFINE_ACTION_STRING
4719 #undef DEFINE_ACTION_BOOL
4720 #undef DEFINE_ACTION_ALIAS
4721 #undef JAVASCRIPTIFY
4725 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4728 #ifdef FEATURE_TOGGLE
4729 /*********************************************************************
4731 * Function : cgi_toggle
4733 * Description : CGI function that adds a new empty section to
4737 * 1 : csp = Current client state (buffers, headers, etc...)
4738 * 2 : rsp = http_response data structure for output
4739 * 3 : parameters = map of cgi parameters
4742 * set : If present, how to change toggle setting:
4743 * "enable", "disable", "toggle", or none (default).
4744 * mini : If present, use mini reply template.
4746 * Returns : JB_ERR_OK on success
4747 * JB_ERR_MEMORY on out-of-memory
4749 *********************************************************************/
4750 jb_err cgi_toggle(struct client_state *csp,
4751 struct http_response *rsp,
4752 const struct map *parameters)
4754 struct map *exports;
4756 const char *template_name;
4762 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4764 return cgi_error_disabled(csp, rsp);
4767 mode = get_char_param(parameters, "set");
4772 global_toggle_state = 1;
4774 else if (mode == 'D')
4777 global_toggle_state = 0;
4779 else if (mode == 'T')
4782 global_toggle_state = !global_toggle_state;
4785 log_error(LOG_LEVEL_INFO, "Now toggled %s.", global_toggle_state ? "ON" : "OFF");
4787 if (NULL == (exports = default_exports(csp, "toggle")))
4789 return JB_ERR_MEMORY;
4792 template_name = (get_char_param(parameters, "mini")
4796 return template_fill_for_cgi(csp, template_name, exports, rsp);
4798 #endif /* def FEATURE_TOGGLE */