1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.68 2011/01/22 12:30:22 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
10 * NOTE: The CGIs in this file use parameter names
11 * such as "f" and "s" which are really *BAD* choices.
12 * However, I'm trying to save bytes in the
13 * edit-actions-list HTML page - the standard actions
14 * file generated a 550kbyte page, which is ridiculous.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001-2008 the SourceForge
19 * Privoxy team. http://www.privoxy.org/
21 * Based on the Internet Junkbuster originally written
22 * by and Copyright (C) 1997 Anonymous Coders and
23 * Junkbusters Corporation. http://www.junkbusters.com
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General
27 * Public License as published by the Free Software
28 * Foundation; either version 2 of the License, or (at
29 * your option) any later version.
31 * This program is distributed in the hope that it will
32 * be useful, but WITHOUT ANY WARRANTY; without even the
33 * implied warranty of MERCHANTABILITY or FITNESS FOR A
34 * PARTICULAR PURPOSE. See the GNU General Public
35 * License for more details.
37 * The GNU General Public License should be included with
38 * this file. If not, you can view it at
39 * http://www.gnu.org/copyleft/gpl.html
40 * or write to the Free Software Foundation, Inc., 59
41 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
43 **********************************************************************/
49 * FIXME: Following includes copied from cgi.c - which are actually needed?
54 #include <sys/types.h>
63 #include "cgisimple.h"
71 /* loadcfg.h is for global_toggle_state only */
73 #endif /* def FEATURE_TOGGLE */
76 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
79 #ifdef FEATURE_CGI_EDIT_ACTIONS
82 * A line in an editable_file.
86 /** Next entry in the linked list */
87 struct file_line * next;
89 /** The raw data, to write out if this line is unmodified. */
92 /** Comments and/or whitespace to put before this line if it's modified
93 and then written out. */
96 /** The actual data, as a string. Line continuation and comment removal
97 are performed on the data read from file before it's stored here, so
98 it will be a single line of data. */
101 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
104 /** The actual data, processed into some sensible data type. */
108 /** An action specification. */
109 struct action_spec action[1];
111 /** A name=value pair. */
115 /** The name in the name=value pair. */
118 /** The value in the name=value pair, as a string. */
121 /** The value in the name=value pair, as an integer. */
126 /* Add more data types here... e.g.
129 struct url_spec url[1];
133 struct action_spec action[1];
143 /** This file_line has not been processed yet. */
144 #define FILE_LINE_UNPROCESSED 1
146 /** This file_line is blank. Can only appear at the end of a file, due to
147 the way the parser works. */
148 #define FILE_LINE_BLANK 2
150 /** This file_line says {{alias}}. */
151 #define FILE_LINE_ALIAS_HEADER 3
153 /** This file_line defines an alias. */
154 #define FILE_LINE_ALIAS_ENTRY 4
156 /** This file_line defines an {action}. */
157 #define FILE_LINE_ACTION 5
159 /** This file_line specifies a URL pattern. */
160 #define FILE_LINE_URL 6
162 /** This file_line says {{settings}}. */
163 #define FILE_LINE_SETTINGS_HEADER 7
165 /** This file_line is in a {{settings}} block. */
166 #define FILE_LINE_SETTINGS_ENTRY 8
168 /** This file_line says {{description}}. */
169 #define FILE_LINE_DESCRIPTION_HEADER 9
171 /** This file_line is in a {{description}} block. */
172 #define FILE_LINE_DESCRIPTION_ENTRY 10
175 * Number of file modification time mismatches
176 * before the CGI editor gets turned off.
178 #define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
181 * A configuration file, in a format that can be edited and written back to
186 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
187 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
188 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
189 const char * version_str; /**< Last modification time, as a string. For CGI param. */
190 /**< Can be used in URL without using url_param(). */
191 unsigned version; /**< Last modification time - prevents chaos with
192 the browser's "back" button. Note that this is a
193 time_t cast to an unsigned. When comparing, always
194 cast the time_t to an unsigned, and *NOT* vice-versa.
195 This may lose the top few bits, but they're not
196 significant anyway. */
197 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
198 Note that changing this after the file has been
199 read in will cause a mess. */
200 struct file_line * parse_error; /**< On parse error, this is the offending line. */
201 const char * parse_error_text; /**< On parse error, this is the problem.
202 (Statically allocated) */
206 * Information about the filter types.
207 * Used for macro replacement in cgi_edit_actions_for_url.
209 struct filter_type_info
211 const int multi_action_index; /**< The multi action index as defined in project.h */
212 const char *macro_name; /**< Name of the macro that has to be replaced
213 with the prepared templates.
214 For example "content-filter-params" */
215 const char *type; /**< Name of the filter type,
216 for example "server-header-filter". */
217 /* XXX: check if these two can be combined. */
218 const char *disable_all_option; /**< Name of the catch-all radio option that has
219 to be checked or unchecked for this filter type. */
220 const char *disable_all_param; /**< Name of the parameter that causes all filters of
221 this type to be disabled. */
222 const char *abbr_type; /**< Abbreviation of the filter type, usually the
223 first or second character capitalized */
224 const char *anchor; /**< Anchor for the User Manual link,
225 for example "SERVER-HEADER-FILTER" */
228 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
229 static const struct filter_type_info filter_type_info[] =
233 "content-filter-params", "filter",
234 "filter-all", "filter_all",
238 ACTION_MULTI_CLIENT_HEADER_FILTER,
239 "client-header-filter-params", "client-header-filter",
240 "client-header-filter-all", "client_header_filter_all",
241 "C", "CLIENT-HEADER-FILTER"
244 ACTION_MULTI_SERVER_HEADER_FILTER,
245 "server-header-filter-params", "server-header-filter",
246 "server-header-filter-all", "server_header_filter_all",
247 "S", "SERVER-HEADER-FILTER"
250 ACTION_MULTI_CLIENT_HEADER_TAGGER,
251 "client-header-tagger-params", "client-header-tagger",
252 "client-header-tagger-all", "client_header_tagger_all",
253 "L", "CLIENT-HEADER-TAGGER"
256 ACTION_MULTI_SERVER_HEADER_TAGGER,
257 "server-header-tagger-params", "server-header-tagger",
258 "server-header-tagger-all", "server_header_tagger_all",
259 "E", "SERVER-HEADER-TAGGER"
263 /* FIXME: Following non-static functions should be prototyped in .h or made static */
265 /* Functions to read and write arbitrary config files */
266 jb_err edit_read_file(struct client_state *csp,
267 const struct map *parameters,
269 struct editable_file **pfile);
270 jb_err edit_write_file(struct editable_file * file);
271 void edit_free_file(struct editable_file * file);
273 /* Functions to read and write actions files */
274 jb_err edit_parse_actions_file(struct editable_file * file);
275 jb_err edit_read_actions_file(struct client_state *csp,
276 struct http_response *rsp,
277 const struct map *parameters,
279 struct editable_file **pfile);
282 jb_err cgi_error_modified(struct client_state *csp,
283 struct http_response *rsp,
284 const char *filename);
285 jb_err cgi_error_parse(struct client_state *csp,
286 struct http_response *rsp,
287 struct editable_file *file);
288 jb_err cgi_error_file(struct client_state *csp,
289 struct http_response *rsp,
290 const char *filename);
291 jb_err cgi_error_file_read_only(struct client_state *csp,
292 struct http_response *rsp,
293 const char *filename);
295 /* Internal arbitrary config file support functions */
296 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
297 static void edit_free_file_lines(struct file_line * first_line);
299 /* Internal actions file support functions */
300 static int match_actions_file_header_line(const char * line, const char * name);
301 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
303 /* Internal parameter parsing functions */
304 static jb_err get_url_spec_param(struct client_state *csp,
305 const struct map *parameters,
310 /* Internal actionsfile <==> HTML conversion functions */
311 static jb_err map_radio(struct map * exports,
312 const char * optionname,
315 static jb_err actions_to_radio(struct map * exports,
316 const struct action_spec *action);
317 static jb_err actions_from_radio(const struct map * parameters,
318 struct action_spec *action);
321 static jb_err map_copy_parameter_html(struct map *out,
322 const struct map *in,
324 #if 0 /* unused function */
325 static jb_err map_copy_parameter_url(struct map *out,
326 const struct map *in,
328 #endif /* unused function */
330 static jb_err get_file_name_param(struct client_state *csp,
331 const struct map *parameters,
332 const char *param_name,
333 const char **pfilename);
335 /* Internal convenience functions */
336 static char *section_target(const unsigned sectionid);
338 /*********************************************************************
340 * Function : section_target
342 * Description : Given an unsigned (section id) n, produce a dynamically
343 * allocated string of the form #l<n>, for use in link
346 * XXX: The hash should be moved into the templates
347 * to make this function more generic and render
348 * stringify() obsolete.
351 * 1 : sectionid = start line number of section
353 * Returns : String with link target, or NULL if out of
356 *********************************************************************/
357 static char *section_target(const unsigned sectionid)
361 snprintf(buf, sizeof(buf), "#l%d", sectionid);
367 /*********************************************************************
369 * Function : stringify
371 * Description : Convert a number into a dynamically allocated string.
374 * 1 : number = The number to convert.
376 * Returns : String with link target, or NULL if out of memory
378 *********************************************************************/
379 static char *stringify(const unsigned number)
383 snprintf(buf, sizeof(buf), "%i", number);
388 /*********************************************************************
390 * Function : map_copy_parameter_html
392 * Description : Copy a CGI parameter from one map to another, HTML
396 * 1 : out = target map
397 * 2 : in = source map
398 * 3 : name = name of cgi parameter to copy
400 * Returns : JB_ERR_OK on success
401 * JB_ERR_MEMORY on out-of-memory
402 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
405 *********************************************************************/
406 static jb_err map_copy_parameter_html(struct map *out,
407 const struct map *in,
417 value = lookup(in, name);
418 err = map(out, name, 1, html_encode(value), 0);
425 else if (*value == '\0')
427 return JB_ERR_CGI_PARAMS;
436 #if 0 /* unused function */
437 /*********************************************************************
439 * Function : map_copy_parameter_url
441 * Description : Copy a CGI parameter from one map to another, URL
445 * 1 : out = target map
446 * 2 : in = source map
447 * 3 : name = name of cgi parameter to copy
449 * Returns : JB_ERR_OK on success
450 * JB_ERR_MEMORY on out-of-memory
451 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
454 *********************************************************************/
455 static jb_err map_copy_parameter_url(struct map *out,
456 const struct map *in,
466 value = lookup(in, name);
467 err = map(out, name, 1, url_encode(value), 0);
474 else if (*value == '\0')
476 return JB_ERR_CGI_PARAMS;
483 #endif /* 0 - unused function */
486 /*********************************************************************
488 * Function : cgi_edit_actions_url_form
490 * Description : CGI function that displays a form for
494 * 1 : csp = Current client state (buffers, headers, etc...)
495 * 2 : rsp = http_response data structure for output
496 * 3 : parameters = map of cgi parameters
499 * i : (action index) Identifies the file to edit
500 * v : (version) File's last-modified time
501 * p : (pattern) Line number of pattern to edit
503 * Returns : JB_ERR_OK on success
504 * JB_ERR_MEMORY on out-of-memory
505 * JB_ERR_CGI_PARAMS if the CGI parameters are not
506 * specified or not valid.
508 *********************************************************************/
509 jb_err cgi_edit_actions_url_form(struct client_state *csp,
510 struct http_response *rsp,
511 const struct map *parameters)
513 struct map * exports;
515 struct editable_file * file;
516 struct file_line * cur_line;
517 unsigned line_number;
518 unsigned section_start_line_number = 0;
525 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
527 return cgi_error_disabled(csp, rsp);
530 err = get_number_param(csp, parameters, "p", &patternid);
536 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
539 /* No filename specified, can't read file, modified, or out of memory. */
540 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
543 cur_line = file->lines;
545 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
547 if (cur_line->type == FILE_LINE_ACTION)
549 section_start_line_number = line_number;
551 cur_line = cur_line->next;
554 if ( (cur_line == NULL)
555 || (line_number != patternid)
557 || (cur_line->type != FILE_LINE_URL))
559 /* Invalid "patternid" parameter */
560 edit_free_file(file);
561 return JB_ERR_CGI_PARAMS;
564 if (NULL == (exports = default_exports(csp, NULL)))
566 edit_free_file(file);
567 return JB_ERR_MEMORY;
570 err = map(exports, "f", 1, stringify(file->identifier), 0);
571 if (!err) err = map(exports, "v", 1, file->version_str, 1);
572 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
573 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
574 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
576 edit_free_file(file);
584 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
588 /*********************************************************************
590 * Function : cgi_edit_actions_add_url_form
592 * Description : CGI function that displays a form for
596 * 1 : csp = Current client state (buffers, headers, etc...)
597 * 2 : rsp = http_response data structure for output
598 * 3 : parameters = map of cgi parameters
601 * f : (filename) Identifies the file to edit
602 * v : (version) File's last-modified time
603 * s : (section) Line number of section to edit
605 * Returns : JB_ERR_OK on success
606 * JB_ERR_MEMORY on out-of-memory
607 * JB_ERR_CGI_PARAMS if the CGI parameters are not
608 * specified or not valid.
610 *********************************************************************/
611 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
612 struct http_response *rsp,
613 const struct map *parameters)
622 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
624 return cgi_error_disabled(csp, rsp);
627 if (NULL == (exports = default_exports(csp, NULL)))
629 return JB_ERR_MEMORY;
632 err = map_copy_parameter_html(exports, parameters, "f");
633 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
634 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
642 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
646 /*********************************************************************
648 * Function : cgi_edit_actions_remove_url_form
650 * Description : CGI function that displays a form for
654 * 1 : csp = Current client state (buffers, headers, etc...)
655 * 2 : rsp = http_response data structure for output
656 * 3 : parameters = map of cgi parameters
659 * f : (number) The action file identifier.
660 * v : (version) File's last-modified time
661 * p : (pattern) Line number of pattern to edit
663 * Returns : JB_ERR_OK on success
664 * JB_ERR_MEMORY on out-of-memory
665 * JB_ERR_CGI_PARAMS if the CGI parameters are not
666 * specified or not valid.
668 *********************************************************************/
669 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
670 struct http_response *rsp,
671 const struct map *parameters)
673 struct map * exports;
675 struct editable_file * file;
676 struct file_line * cur_line;
677 unsigned line_number;
678 unsigned section_start_line_number = 0;
685 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
687 return cgi_error_disabled(csp, rsp);
690 err = get_number_param(csp, parameters, "p", &patternid);
696 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
699 /* No filename specified, can't read file, modified, or out of memory. */
700 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
703 cur_line = file->lines;
705 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
707 if (cur_line->type == FILE_LINE_ACTION)
709 section_start_line_number = line_number;
711 cur_line = cur_line->next;
714 if ( (cur_line == NULL)
715 || (line_number != patternid)
717 || (cur_line->type != FILE_LINE_URL))
719 /* Invalid "patternid" parameter */
720 edit_free_file(file);
721 return JB_ERR_CGI_PARAMS;
724 if (NULL == (exports = default_exports(csp, NULL)))
726 edit_free_file(file);
727 return JB_ERR_MEMORY;
730 err = map(exports, "f", 1, stringify(file->identifier), 0);
731 if (!err) err = map(exports, "v", 1, file->version_str, 1);
732 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
733 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
734 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
735 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
737 edit_free_file(file);
745 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
749 /*********************************************************************
751 * Function : edit_write_file
753 * Description : Write a complete file to disk.
756 * 1 : file = File to write.
758 * Returns : JB_ERR_OK on success
759 * JB_ERR_FILE on error writing to file.
760 * JB_ERR_MEMORY on out of memory
762 *********************************************************************/
763 jb_err edit_write_file(struct editable_file * file)
766 struct file_line * cur_line;
767 struct stat statbuf[1];
768 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
769 digits in time_t, assuming this is a 64-bit
770 machine, plus null terminator, plus one
774 assert(file->filename);
776 if (NULL == (fp = fopen(file->filename, "wb")))
781 cur_line = file->lines;
782 while (cur_line != NULL)
786 if (fputs(cur_line->raw, fp) < 0)
794 if (cur_line->prefix)
796 if (fputs(cur_line->prefix, fp) < 0)
802 if (cur_line->unprocessed)
805 if (NULL != strchr(cur_line->unprocessed, '#'))
807 /* Must quote '#' characters */
814 /* Count number of # characters, so we know length of output string */
815 src = cur_line->unprocessed;
816 while (NULL != (src = strchr(src, '#')))
823 /* Allocate new memory for string */
824 len = strlen(cur_line->unprocessed) + (size_t)numhash;
825 if (NULL == (str = malloc(len + 1)))
827 /* Uh oh, just trashed file! */
829 return JB_ERR_MEMORY;
832 /* Copy string but quote hashes */
833 src = cur_line->unprocessed;
841 assert(numhash >= 0);
847 assert(numhash == 0);
848 assert(strlen(str) == len);
849 assert(str == dest - len);
850 assert(src - len <= cur_line->unprocessed);
852 if ((strlen(str) != len) || (numhash != 0))
855 * Escaping didn't work as expected, go spread the news.
856 * Only reached in non-debugging builds.
858 log_error(LOG_LEVEL_ERROR,
859 "Looks like hash escaping failed. %s might be corrupted now.",
863 if (fputs(str, fp) < 0)
874 /* Can write without quoting '#' characters. */
875 if (fputs(cur_line->unprocessed, fp) < 0)
881 if (fputs(NEWLINE(file->newline), fp) < 0)
889 /* FIXME: Write data from file->data->whatever */
893 cur_line = cur_line->next;
899 /* Update the version stamp in the file structure, since we just
900 * wrote to the file & changed it's date.
902 if (stat(file->filename, statbuf) < 0)
904 /* Error, probably file not found. */
907 file->version = (unsigned)statbuf->st_mtime;
909 /* Correct file->version_str */
910 freez(file->version_str);
911 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
912 version_buf[sizeof(version_buf)-1] = '\0';
913 file->version_str = strdup(version_buf);
914 if (version_buf == NULL)
916 return JB_ERR_MEMORY;
923 /*********************************************************************
925 * Function : edit_free_file
927 * Description : Free a complete file in memory.
930 * 1 : file = Data structure to free.
934 *********************************************************************/
935 void edit_free_file(struct editable_file * file)
939 /* Silently ignore NULL pointer */
943 edit_free_file_lines(file->lines);
944 freez(file->version_str);
946 file->parse_error_text = NULL; /* Statically allocated */
947 file->parse_error = NULL;
953 /*********************************************************************
955 * Function : edit_free_file_lines
957 * Description : Free an entire linked list of file lines.
960 * 1 : first_line = Data structure to free.
964 *********************************************************************/
965 static void edit_free_file_lines(struct file_line * first_line)
967 struct file_line * next_line;
969 while (first_line != NULL)
971 next_line = first_line->next;
972 first_line->next = NULL;
973 freez(first_line->raw);
974 freez(first_line->prefix);
975 freez(first_line->unprocessed);
976 switch(first_line->type)
978 case 0: /* special case if memory zeroed */
979 case FILE_LINE_UNPROCESSED:
980 case FILE_LINE_BLANK:
981 case FILE_LINE_ALIAS_HEADER:
982 case FILE_LINE_SETTINGS_HEADER:
983 case FILE_LINE_DESCRIPTION_HEADER:
984 case FILE_LINE_DESCRIPTION_ENTRY:
985 case FILE_LINE_ALIAS_ENTRY:
987 /* No data is stored for these */
990 case FILE_LINE_ACTION:
991 free_action(first_line->data.action);
994 case FILE_LINE_SETTINGS_ENTRY:
995 freez(first_line->data.setting.name);
996 freez(first_line->data.setting.svalue);
999 /* Should never happen */
1003 first_line->type = 0; /* paranoia */
1005 first_line = next_line;
1010 /*********************************************************************
1012 * Function : match_actions_file_header_line
1014 * Description : Match an actions file {{header}} line
1017 * 1 : line = String from file
1018 * 2 : name = Header to match against
1020 * Returns : 0 iff they match.
1022 *********************************************************************/
1023 static int match_actions_file_header_line(const char * line, const char * name)
1031 if ((line[0] != '{') || (line[1] != '{'))
1037 /* Look for optional whitespace */
1038 while ( (*line == ' ') || (*line == '\t') )
1043 /* Look for the specified name (case-insensitive) */
1045 if (0 != strncmpic(line, name, len))
1051 /* Look for optional whitespace */
1052 while ( (*line == ' ') || (*line == '\t') )
1057 /* Look for "}}" and end of string*/
1058 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1068 /*********************************************************************
1070 * Function : match_actions_file_header_line
1072 * Description : Match an actions file {{header}} line
1075 * 1 : line = String from file. Must not start with
1076 * whitespace (else infinite loop!)
1077 * 2 : pname = Destination for name
1078 * 2 : pvalue = Destination for value
1080 * Returns : JB_ERR_OK on success
1081 * JB_ERR_MEMORY on out-of-memory
1082 * JB_ERR_PARSE if there's no "=" sign, or if there's
1083 * nothing before the "=" sign (but empty
1084 * values *after* the "=" sign are legal).
1086 *********************************************************************/
1087 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1089 const char * name_end;
1090 const char * value_start;
1096 assert(*line != ' ');
1097 assert(*line != '\t');
1102 value_start = strchr(line, '=');
1103 if ((value_start == NULL) || (value_start == line))
1105 return JB_ERR_PARSE;
1108 name_end = value_start - 1;
1110 /* Eat any whitespace before the '=' */
1111 while ((*name_end == ' ') || (*name_end == '\t'))
1114 * we already know we must have at least 1 non-ws char
1115 * at start of buf - no need to check
1120 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1121 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1123 return JB_ERR_MEMORY;
1125 strncpy(*pname, line, name_len);
1126 (*pname)[name_len] = '\0';
1128 /* Eat any the whitespace after the '=' */
1130 while ((*value_start == ' ') || (*value_start == '\t'))
1135 if (NULL == (*pvalue = strdup(value_start)))
1139 return JB_ERR_MEMORY;
1146 /*********************************************************************
1148 * Function : edit_parse_actions_file
1150 * Description : Parse an actions file in memory.
1152 * Passed linked list must have the "data" member
1153 * zeroed, and must contain valid "next" and
1154 * "unprocessed" fields. The "raw" and "prefix"
1155 * fields are ignored, and "type" is just overwritten.
1157 * Note that on error the file may have been
1161 * 1 : file = Actions file to be parsed in-place.
1163 * Returns : JB_ERR_OK on success
1164 * JB_ERR_MEMORY on out-of-memory
1165 * JB_ERR_PARSE on error
1167 *********************************************************************/
1168 jb_err edit_parse_actions_file(struct editable_file * file)
1170 struct file_line * cur_line;
1172 const char * text; /* Text from a line */
1173 char * name; /* For lines of the form name=value */
1174 char * value; /* For lines of the form name=value */
1175 struct action_alias * alias_list = NULL;
1176 jb_err err = JB_ERR_OK;
1178 /* alias_list contains the aliases defined in this file.
1179 * It might be better to use the "file_line.data" fields
1180 * in the relavent places instead.
1183 cur_line = file->lines;
1185 /* A note about blank line support: Blank lines should only
1186 * ever occur as the last line in the file. This function
1187 * is more forgiving than that - FILE_LINE_BLANK can occur
1191 /* Skip leading blanks. Should only happen if file is
1192 * empty (which is valid, but pointless).
1194 while ( (cur_line != NULL)
1195 && (cur_line->unprocessed[0] == '\0') )
1198 cur_line->type = FILE_LINE_BLANK;
1199 cur_line = cur_line->next;
1202 if ( (cur_line != NULL)
1203 && (cur_line->unprocessed[0] != '{') )
1205 /* File doesn't start with a header */
1206 file->parse_error = cur_line;
1207 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1208 return JB_ERR_PARSE;
1211 if ( (cur_line != NULL) && (0 ==
1212 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1214 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1216 cur_line = cur_line->next;
1217 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1219 if (cur_line->unprocessed[0])
1221 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1223 err = split_line_on_equals(cur_line->unprocessed,
1224 &cur_line->data.setting.name,
1225 &cur_line->data.setting.svalue);
1226 if (err == JB_ERR_MEMORY)
1230 else if (err != JB_ERR_OK)
1232 /* Line does not contain a name=value pair */
1233 file->parse_error = cur_line;
1234 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1235 return JB_ERR_PARSE;
1240 cur_line->type = FILE_LINE_BLANK;
1242 cur_line = cur_line->next;
1246 if ( (cur_line != NULL) && (0 ==
1247 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1249 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1251 cur_line = cur_line->next;
1252 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1254 if (cur_line->unprocessed[0])
1256 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1260 cur_line->type = FILE_LINE_BLANK;
1262 cur_line = cur_line->next;
1266 if ( (cur_line != NULL) && (0 ==
1267 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1269 cur_line->type = FILE_LINE_ALIAS_HEADER;
1271 cur_line = cur_line->next;
1272 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1274 if (cur_line->unprocessed[0])
1276 /* define an alias */
1277 struct action_alias * new_alias;
1279 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1281 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1282 if (err == JB_ERR_MEMORY)
1286 else if (err != JB_ERR_OK)
1288 /* Line does not contain a name=value pair */
1289 file->parse_error = cur_line;
1290 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1291 return JB_ERR_PARSE;
1294 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1299 free_alias_list(alias_list);
1300 return JB_ERR_MEMORY;
1303 err = get_actions(value, alias_list, new_alias->action);
1306 /* Invalid action or out of memory */
1310 free_alias_list(alias_list);
1311 if (err == JB_ERR_MEMORY)
1317 /* Line does not contain a name=value pair */
1318 file->parse_error = cur_line;
1319 file->parse_error_text = "This alias does not specify a valid set of actions.";
1320 return JB_ERR_PARSE;
1326 new_alias->name = name;
1329 new_alias->next = alias_list;
1330 alias_list = new_alias;
1334 cur_line->type = FILE_LINE_BLANK;
1336 cur_line = cur_line->next;
1340 /* Header done, process the main part of the file */
1341 while (cur_line != NULL)
1343 /* At this point, (cur_line->unprocessed[0] == '{') */
1344 assert(cur_line->unprocessed[0] == '{');
1345 text = cur_line->unprocessed + 1;
1346 len = strlen(text) - 1;
1347 if (text[len] != '}')
1349 /* No closing } on header */
1350 free_alias_list(alias_list);
1351 file->parse_error = cur_line;
1352 file->parse_error_text = "Headers starting with '{' must have a "
1353 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1354 "must close with two brackets ('}}').";
1355 return JB_ERR_PARSE;
1360 /* An invalid {{ header. */
1361 free_alias_list(alias_list);
1362 file->parse_error = cur_line;
1363 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1364 "Please remember that the system (two-bracket) headers must "
1365 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1366 "and must appear before any actions (one-bracket) headers. "
1367 "Also note that system headers may not be repeated.";
1368 return JB_ERR_PARSE;
1371 while ( (*text == ' ') || (*text == '\t') )
1376 while ( (len > (size_t)0)
1377 && ( (text[len - 1] == ' ')
1378 || (text[len - 1] == '\t') ) )
1383 cur_line->type = FILE_LINE_ACTION;
1385 /* Remove {} and make copy */
1386 if (NULL == (value = (char *) malloc(len + 1)))
1389 free_alias_list(alias_list);
1390 return JB_ERR_MEMORY;
1392 strncpy(value, text, len);
1396 err = get_actions(value, alias_list, cur_line->data.action);
1399 /* Invalid action or out of memory */
1401 free_alias_list(alias_list);
1402 if (err == JB_ERR_MEMORY)
1408 /* Line does not contain a name=value pair */
1409 file->parse_error = cur_line;
1410 file->parse_error_text = "This header does not specify a valid set of actions.";
1411 return JB_ERR_PARSE;
1415 /* Done with string - it was clobbered anyway */
1418 /* Process next line */
1419 cur_line = cur_line->next;
1421 /* Loop processing URL patterns */
1422 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1424 if (cur_line->unprocessed[0])
1426 /* Could parse URL here, but this isn't currently needed */
1428 cur_line->type = FILE_LINE_URL;
1432 cur_line->type = FILE_LINE_BLANK;
1434 cur_line = cur_line->next;
1436 } /* End main while(cur_line != NULL) loop */
1438 free_alias_list(alias_list);
1444 /*********************************************************************
1446 * Function : edit_read_file_lines
1448 * Description : Read all the lines of a file into memory.
1449 * Handles whitespace, comments and line continuation.
1452 * 1 : fp = File to read from. On return, this will be
1453 * at EOF but it will not have been closed.
1454 * 2 : pfile = Destination for a linked list of file_lines.
1455 * Will be set to NULL on error.
1456 * 3 : newline = How to handle newlines.
1458 * Returns : JB_ERR_OK on success
1459 * JB_ERR_MEMORY on out-of-memory
1461 *********************************************************************/
1462 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1464 struct file_line * first_line; /* Keep for return value or to free */
1465 struct file_line * cur_line; /* Current line */
1466 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1474 cur_line = first_line = zalloc(sizeof(struct file_line));
1475 if (cur_line == NULL)
1477 return JB_ERR_MEMORY;
1480 cur_line->type = FILE_LINE_UNPROCESSED;
1482 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1485 /* Out of memory or empty file. */
1486 /* Note that empty file is not an error we propagate up */
1488 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1493 prev_line = cur_line;
1494 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1495 if (cur_line == NULL)
1498 edit_free_file_lines(first_line);
1499 return JB_ERR_MEMORY;
1502 cur_line->type = FILE_LINE_UNPROCESSED;
1504 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1505 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1508 edit_free_file_lines(first_line);
1509 return JB_ERR_MEMORY;
1513 while (rval != JB_ERR_FILE);
1517 /* We allocated one too many - free it */
1518 prev_line->next = NULL;
1521 *pfile = first_line;
1526 /*********************************************************************
1528 * Function : edit_read_file
1530 * Description : Read a complete file into memory.
1531 * Handles CGI parameter parsing. If requested, also
1532 * checks the file's modification timestamp.
1535 * 1 : csp = Current client state (buffers, headers, etc...)
1536 * 2 : parameters = map of cgi parameters.
1537 * 3 : require_version = true to check "ver" parameter.
1538 * 4 : pfile = Destination for the file. Will be set
1542 * f : The action file identifier.
1543 * ver : (Only if require_version is nonzero)
1544 * Timestamp of the actions file. If wrong, this
1545 * function fails with JB_ERR_MODIFIED.
1547 * Returns : JB_ERR_OK on success
1548 * JB_ERR_MEMORY on out-of-memory
1549 * JB_ERR_CGI_PARAMS if "filename" was not specified
1551 * JB_ERR_FILE if the file cannot be opened or
1553 * JB_ERR_MODIFIED if version checking was requested and
1554 * failed - the file was modified outside
1555 * of this CGI editor instance.
1557 *********************************************************************/
1558 jb_err edit_read_file(struct client_state *csp,
1559 const struct map *parameters,
1560 int require_version,
1561 struct editable_file **pfile)
1563 struct file_line * lines;
1566 const char *filename = NULL;
1567 struct editable_file * file;
1568 unsigned version = 0;
1569 struct stat statbuf[1];
1570 char version_buf[22];
1571 int newline = NEWLINE_UNKNOWN;
1580 err = get_number_param(csp, parameters, "f", &i);
1581 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1583 filename = csp->config->actions_file[i];
1585 else if (JB_ERR_CGI_PARAMS == err)
1588 * Probably an old-school URL like
1589 * http://config.privoxy.org/edit-actions-list?f=default
1591 get_file_name_param(csp, parameters, "f", &filename);
1594 if (NULL == filename || stat(filename, statbuf) < 0)
1596 /* Error, probably file not found. */
1599 version = (unsigned) statbuf->st_mtime;
1601 if (require_version)
1603 unsigned specified_version;
1604 err = get_number_param(csp, parameters, "v", &specified_version);
1610 if (version != specified_version)
1612 return JB_ERR_MODIFIED;
1616 if (NULL == (fp = fopen(filename,"rb")))
1621 err = edit_read_file_lines(fp, &lines, &newline);
1630 file = (struct editable_file *) zalloc(sizeof(*file));
1633 edit_free_file_lines(lines);
1637 file->lines = lines;
1638 file->newline = newline;
1639 file->filename = filename;
1640 file->version = version;
1641 file->identifier = i;
1643 /* Correct file->version_str */
1644 freez(file->version_str);
1645 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1646 version_buf[sizeof(version_buf)-1] = '\0';
1647 file->version_str = strdup(version_buf);
1648 if (version_buf == NULL)
1650 edit_free_file(file);
1651 return JB_ERR_MEMORY;
1659 /*********************************************************************
1661 * Function : edit_read_actions_file
1663 * Description : Read a complete actions file into memory.
1664 * Handles CGI parameter parsing. If requested, also
1665 * checks the file's modification timestamp.
1667 * If this function detects an error in the categories
1668 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1669 * then it handles it by filling in the specified
1670 * response structure and returning JB_ERR_FILE.
1673 * 1 : csp = Current client state (buffers, headers, etc...)
1674 * 2 : rsp = HTTP response. Only filled in on error.
1675 * 2 : parameters = map of cgi parameters.
1676 * 3 : require_version = true to check "ver" parameter.
1677 * 4 : pfile = Destination for the file. Will be set
1681 * f : The actions file identifier.
1682 * ver : (Only if require_version is nonzero)
1683 * Timestamp of the actions file. If wrong, this
1684 * function fails with JB_ERR_MODIFIED.
1686 * Returns : JB_ERR_OK on success
1687 * JB_ERR_MEMORY on out-of-memory
1688 * JB_ERR_CGI_PARAMS if "filename" was not specified
1690 * JB_ERR_FILE if the file does not contain valid data,
1691 * or if file cannot be opened or
1692 * contains no data, or if version
1693 * checking was requested and failed.
1695 *********************************************************************/
1696 jb_err edit_read_actions_file(struct client_state *csp,
1697 struct http_response *rsp,
1698 const struct map *parameters,
1699 int require_version,
1700 struct editable_file **pfile)
1703 struct editable_file *file;
1704 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
1712 err = edit_read_file(csp, parameters, require_version, &file);
1715 /* Try to handle if possible */
1716 if (err == JB_ERR_FILE)
1718 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1720 else if (err == JB_ERR_MODIFIED)
1722 assert(require_version);
1723 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1724 log_error(LOG_LEVEL_ERROR,
1725 "Blocking CGI edit request due to modification time mismatch.");
1726 if (acceptable_failures > 0)
1728 log_error(LOG_LEVEL_INFO,
1729 "The CGI editor will be turned off after another %d mismatche(s).",
1730 acceptable_failures);
1731 acceptable_failures--;
1735 log_error(LOG_LEVEL_INFO,
1736 "Timestamp mismatch limit reached, turning CGI editor off. "
1737 "Reload the configuration file to re-enable it.");
1738 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
1741 if (err == JB_ERR_OK)
1744 * Signal to higher-level CGI code that there was a problem but we
1745 * handled it, they should just return JB_ERR_OK.
1752 err = edit_parse_actions_file(file);
1755 if (err == JB_ERR_PARSE)
1757 err = cgi_error_parse(csp, rsp, file);
1758 if (err == JB_ERR_OK)
1761 * Signal to higher-level CGI code that there was a problem but we
1762 * handled it, they should just return JB_ERR_OK.
1767 edit_free_file(file);
1776 /*********************************************************************
1778 * Function : get_file_name_param
1780 * Description : Get the name of the file to edit from the parameters
1781 * passed to a CGI function using the old syntax.
1782 * This function handles security checks and only
1783 * accepts files that Privoxy already knows.
1786 * 1 : csp = Current client state (buffers, headers, etc...)
1787 * 2 : parameters = map of cgi parameters
1788 * 3 : param_name = The name of the parameter to read
1789 * 4 : pfilename = pointer to the filename in
1790 * csp->config->actions_file[] if found. Set to NULL on error.
1792 * Returns : JB_ERR_OK on success
1793 * JB_ERR_MEMORY on out-of-memory
1794 * JB_ERR_CGI_PARAMS if "filename" was not specified
1797 *********************************************************************/
1798 static jb_err get_file_name_param(struct client_state *csp,
1799 const struct map *parameters,
1800 const char *param_name,
1801 const char **pfilename)
1804 const char suffix[] = ".action";
1819 param = lookup(parameters, param_name);
1822 return JB_ERR_CGI_PARAMS;
1825 len = strlen(param);
1826 if (len >= FILENAME_MAX)
1829 return JB_ERR_CGI_PARAMS;
1833 * Check every character to see if it's legal.
1834 * Totally unnecessary but we do it anyway.
1837 while ((ch = *s++) != '\0')
1839 if ( ((ch < 'A') || (ch > 'Z'))
1840 && ((ch < 'a') || (ch > 'z'))
1841 && ((ch < '0') || (ch > '9'))
1845 /* Probable hack attempt. */
1846 return JB_ERR_CGI_PARAMS;
1850 /* Append extension */
1851 name_size = len + strlen(suffix) + 1;
1852 name = malloc(name_size);
1855 return JB_ERR_MEMORY;
1857 strlcpy(name, param, name_size);
1858 strlcat(name, suffix, name_size);
1861 fullpath = make_path(csp->config->confdir, name);
1864 if (fullpath == NULL)
1866 return JB_ERR_MEMORY;
1869 /* Check if the file is known */
1870 for (i = 0; i < MAX_AF_FILES; i++)
1872 if (NULL != csp->config->actions_file[i] &&
1873 !strcmp(fullpath, csp->config->actions_file[i]))
1876 *pfilename = csp->config->actions_file[i];
1884 return JB_ERR_CGI_PARAMS;
1888 /*********************************************************************
1890 * Function : get_url_spec_param
1892 * Description : Get a URL pattern from the parameters
1893 * passed to a CGI function. Removes leading/trailing
1894 * spaces and validates it.
1897 * 1 : csp = Current client state (buffers, headers, etc...)
1898 * 2 : parameters = map of cgi parameters
1899 * 3 : name = Name of CGI parameter to read
1900 * 4 : pvalue = destination for value. Will be malloc()'d.
1901 * Set to NULL on error.
1903 * Returns : JB_ERR_OK on success
1904 * JB_ERR_MEMORY on out-of-memory
1905 * JB_ERR_CGI_PARAMS if the parameter was not specified
1908 *********************************************************************/
1909 static jb_err get_url_spec_param(struct client_state *csp,
1910 const struct map *parameters,
1914 const char *orig_param;
1917 struct url_spec compiled[1];
1927 orig_param = lookup(parameters, name);
1930 return JB_ERR_CGI_PARAMS;
1933 /* Copy and trim whitespace */
1934 param = strdup(orig_param);
1937 return JB_ERR_MEMORY;
1941 /* Must be non-empty, and can't allow 1st character to be '{' */
1942 if (param[0] == '\0' || param[0] == '{')
1945 return JB_ERR_CGI_PARAMS;
1948 /* Check for embedded newlines */
1949 for (s = param; *s != '\0'; s++)
1951 if ((*s == '\r') || (*s == '\n'))
1954 return JB_ERR_CGI_PARAMS;
1958 /* Check that regex is valid */
1963 return JB_ERR_MEMORY;
1965 err = create_url_spec(compiled, s);
1970 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1972 free_url_spec(compiled);
1974 if (param[strlen(param) - 1] == '\\')
1977 * Must protect trailing '\\' from becoming line continuation character.
1978 * Two methods: 1) If it's a domain only, add a trailing '/'.
1979 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1981 if (strchr(param, '/') == NULL)
1983 err = string_append(¶m, "/");
1987 err = string_append(¶m, "(?:)");
1994 /* Check that the modified regex is valid */
1999 return JB_ERR_MEMORY;
2001 err = create_url_spec(compiled, s);
2006 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2008 free_url_spec(compiled);
2015 /*********************************************************************
2017 * Function : map_radio
2019 * Description : Map a set of radio button values. E.g. if you have
2020 * 3 radio buttons, declare them as:
2021 * <option type="radio" name="xyz" @xyz-a@>
2022 * <option type="radio" name="xyz" @xyz-b@>
2023 * <option type="radio" name="xyz" @xyz-c@>
2024 * Then map one of the @xyz-?@ variables to "checked"
2025 * and all the others to empty by calling:
2026 * map_radio(exports, "xyz", "abc", sel)
2027 * Where 'sel' is 'a', 'b', or 'c'.
2030 * 1 : exports = Exports map to modify.
2031 * 2 : optionname = name for map
2032 * 3 : values = null-terminated list of values;
2033 * 4 : value = Selected value.
2035 * CGI Parameters : None
2037 * Returns : JB_ERR_OK on success
2038 * JB_ERR_MEMORY on out-of-memory
2040 *********************************************************************/
2041 static jb_err map_radio(struct map * exports,
2042 const char * optionname,
2043 const char * values,
2049 const size_t len = strlen(optionname);
2050 const size_t buf_size = len + 3;
2056 buf = malloc(buf_size);
2059 return JB_ERR_MEMORY;
2062 strlcpy(buf, optionname, buf_size);
2064 /* XXX: this looks ... interesting */
2069 while ((c = *values++) != '\0')
2074 if (map(exports, buf, 1, "", 1))
2076 return JB_ERR_MEMORY;
2082 return map(exports, buf, 0, "checked", 1);
2086 /*********************************************************************
2088 * Function : cgi_error_modified
2090 * Description : CGI function that is called when a file is modified
2091 * outside the CGI editor.
2094 * 1 : csp = Current client state (buffers, headers, etc...)
2095 * 2 : rsp = http_response data structure for output
2096 * 3 : filename = The file that was modified.
2098 * CGI Parameters : none
2100 * Returns : JB_ERR_OK on success
2101 * JB_ERR_MEMORY on out-of-memory error.
2103 *********************************************************************/
2104 jb_err cgi_error_modified(struct client_state *csp,
2105 struct http_response *rsp,
2106 const char *filename)
2108 struct map *exports;
2115 if (NULL == (exports = default_exports(csp, NULL)))
2117 return JB_ERR_MEMORY;
2120 err = map(exports, "f", 1, html_encode(filename), 0);
2127 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2131 /*********************************************************************
2133 * Function : cgi_error_parse
2135 * Description : CGI function that is called when a file cannot
2136 * be parsed by the CGI editor.
2139 * 1 : csp = Current client state (buffers, headers, etc...)
2140 * 2 : rsp = http_response data structure for output
2141 * 3 : file = The file that was modified.
2143 * CGI Parameters : none
2145 * Returns : JB_ERR_OK on success
2146 * JB_ERR_MEMORY on out-of-memory error.
2148 *********************************************************************/
2149 jb_err cgi_error_parse(struct client_state *csp,
2150 struct http_response *rsp,
2151 struct editable_file *file)
2153 struct map *exports;
2155 struct file_line *cur_line;
2161 if (NULL == (exports = default_exports(csp, NULL)))
2163 return JB_ERR_MEMORY;
2166 err = map(exports, "f", 1, stringify(file->identifier), 0);
2167 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2169 cur_line = file->parse_error;
2172 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2173 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2181 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2185 /*********************************************************************
2187 * Function : cgi_error_file
2189 * Description : CGI function that is called when a file cannot be
2190 * opened by the CGI editor.
2193 * 1 : csp = Current client state (buffers, headers, etc...)
2194 * 2 : rsp = http_response data structure for output
2195 * 3 : filename = The file that was modified.
2197 * CGI Parameters : none
2199 * Returns : JB_ERR_OK on success
2200 * JB_ERR_MEMORY on out-of-memory error.
2202 *********************************************************************/
2203 jb_err cgi_error_file(struct client_state *csp,
2204 struct http_response *rsp,
2205 const char *filename)
2207 struct map *exports;
2214 if (NULL == (exports = default_exports(csp, NULL)))
2216 return JB_ERR_MEMORY;
2219 err = map(exports, "f", 1, html_encode(filename), 0);
2226 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2230 /*********************************************************************
2232 * Function : cgi_error_file_read_only
2234 * Description : CGI function that is called when a file cannot be
2235 * opened for writing by the CGI editor.
2238 * 1 : csp = Current client state (buffers, headers, etc...)
2239 * 2 : rsp = http_response data structure for output
2240 * 3 : filename = The file that we can't write to
2242 * CGI Parameters : none
2244 * Returns : JB_ERR_OK on success
2245 * JB_ERR_MEMORY on out-of-memory error.
2247 *********************************************************************/
2248 jb_err cgi_error_file_read_only(struct client_state *csp,
2249 struct http_response *rsp,
2250 const char *filename)
2252 struct map *exports;
2259 if (NULL == (exports = default_exports(csp, NULL)))
2261 return JB_ERR_MEMORY;
2264 err = map(exports, "f", 1, html_encode(filename), 0);
2271 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2275 /*********************************************************************
2277 * Function : cgi_edit_actions
2279 * Description : CGI function that allows the user to choose which
2280 * actions file to edit.
2283 * 1 : csp = Current client state (buffers, headers, etc...)
2284 * 2 : rsp = http_response data structure for output
2285 * 3 : parameters = map of cgi parameters
2287 * CGI Parameters : None
2289 * Returns : JB_ERR_OK on success
2290 * JB_ERR_MEMORY on out-of-memory error
2292 *********************************************************************/
2293 jb_err cgi_edit_actions(struct client_state *csp,
2294 struct http_response *rsp,
2295 const struct map *parameters)
2299 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2301 return cgi_error_disabled(csp, rsp);
2304 /* FIXME: Incomplete */
2306 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2311 /*********************************************************************
2313 * Function : cgi_edit_actions_list
2315 * Description : CGI function that edits the actions list.
2316 * FIXME: This function shouldn't FATAL ever.
2317 * FIXME: This function doesn't check the retval of map()
2319 * 1 : csp = Current client state (buffers, headers, etc...)
2320 * 2 : rsp = http_response data structure for output
2321 * 3 : parameters = map of cgi parameters
2323 * CGI Parameters : filename
2325 * Returns : JB_ERR_OK on success
2326 * JB_ERR_MEMORY on out-of-memory
2327 * JB_ERR_FILE if the file cannot be opened or
2329 * JB_ERR_CGI_PARAMS if "filename" was not specified
2332 *********************************************************************/
2333 jb_err cgi_edit_actions_list(struct client_state *csp,
2334 struct http_response *rsp,
2335 const struct map *parameters)
2337 char * section_template;
2338 char * url_template;
2343 struct map * exports;
2344 struct map * section_exports;
2345 struct map * url_exports;
2346 struct editable_file * file;
2347 struct file_line * cur_line;
2348 unsigned line_number = 0;
2349 unsigned prev_section_line_number = ((unsigned) (-1));
2351 struct file_list * fl;
2352 struct url_actions * b;
2353 char * buttons = NULL;
2356 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2358 return cgi_error_disabled(csp, rsp);
2361 if (NULL == (exports = default_exports(csp, NULL)))
2363 return JB_ERR_MEMORY;
2366 /* Load actions file */
2367 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2370 /* No filename specified, can't read file, or out of memory. */
2372 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2375 /* Find start of actions in file */
2376 cur_line = file->lines;
2378 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2380 cur_line = cur_line->next;
2385 * Conventional actions files should have a match all block
2387 * cur_line = {...global actions...}
2388 * cur_line->next = /
2389 * cur_line->next->next = {...actions...} or EOF
2391 if ( (cur_line != NULL)
2392 && (cur_line->type == FILE_LINE_ACTION)
2393 && (cur_line->next != NULL)
2394 && (cur_line->next->type == FILE_LINE_URL)
2395 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2396 && ( (cur_line->next->next == NULL)
2397 || (cur_line->next->next->type != FILE_LINE_URL)
2401 * Generate string with buttons to set actions for "/" to
2402 * any predefined set of actions (named standard.*, probably
2403 * residing in standard.action).
2406 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2409 edit_free_file(file);
2411 if (err == JB_ERR_FILE)
2413 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2418 err = template_fill(§ion_template, exports);
2421 edit_free_file(file);
2426 buttons = strdup("");
2427 for (i = 0; i < MAX_AF_FILES; i++)
2429 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2431 for (b = b->next; NULL != b; b = b->next)
2433 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2435 if (err || (NULL == (section_exports = new_map())))
2438 free(section_template);
2439 edit_free_file(file);
2441 return JB_ERR_MEMORY;
2444 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2446 if (err || (NULL == (s = strdup(section_template))))
2448 free_map(section_exports);
2450 free(section_template);
2451 edit_free_file(file);
2453 return JB_ERR_MEMORY;
2456 if (!err) err = template_fill(&s, section_exports);
2457 free_map(section_exports);
2458 if (!err) err = string_join(&buttons, s);
2463 freez(section_template);
2464 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2467 * Conventional actions file, supply extra editing help.
2468 * (e.g. don't allow them to make it an unconventional one).
2470 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2472 snprintf(buf, sizeof(buf), "%d", line_number);
2473 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2474 snprintf(buf, sizeof(buf), "%d", line_number + 2);
2475 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2476 if (!err) err = map(exports, "all-urls-actions", 1,
2477 actions_to_html(csp, cur_line->data.action), 0);
2479 /* Skip the 2 lines */
2480 cur_line = cur_line->next->next;
2484 * Note that prev_section_line_number is NOT set here.
2485 * This is deliberate and not a bug. It stops a "Move up"
2486 * option appearing on the next section. Clicking "Move
2487 * up" would make the actions file unconventional, which
2488 * we don't want, so we hide this option.
2494 * Non-standard actions file - does not begin with
2495 * the "All URLs" section.
2497 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2500 /* Set up global exports */
2502 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2503 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2504 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2506 /* Discourage private additions to default.action */
2508 if (!err) err = map_conditional(exports, "default-action",
2509 (strstr("default.action", file->filename) != NULL));
2512 edit_free_file(file);
2517 /* Should do all global exports above this point */
2519 /* Load templates */
2521 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2524 edit_free_file(file);
2526 if (err == JB_ERR_FILE)
2528 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2533 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2536 free(section_template);
2537 edit_free_file(file);
2539 if (err == JB_ERR_FILE)
2541 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2546 err = template_fill(§ion_template, exports);
2550 edit_free_file(file);
2555 err = template_fill(&url_template, exports);
2558 free(section_template);
2559 edit_free_file(file);
2564 if (NULL == (sections = strdup("")))
2566 free(section_template);
2568 edit_free_file(file);
2570 return JB_ERR_MEMORY;
2573 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2575 if (NULL == (section_exports = new_map()))
2578 free(section_template);
2580 edit_free_file(file);
2582 return JB_ERR_MEMORY;
2585 snprintf(buf, sizeof(buf), "%d", line_number);
2586 err = map(section_exports, "s", 1, buf, 1);
2587 if (!err) err = map(section_exports, "actions", 1,
2588 actions_to_html(csp, cur_line->data.action), 0);
2591 && (cur_line->next != NULL)
2592 && (cur_line->next->type == FILE_LINE_URL))
2594 /* This section contains at least one URL, don't allow delete */
2595 err = map_block_killer(section_exports, "empty-section");
2599 if (!err) err = map_block_keep(section_exports, "empty-section");
2602 if (prev_section_line_number != ((unsigned)(-1)))
2604 /* Not last section */
2605 snprintf(buf, sizeof(buf), "%d", prev_section_line_number);
2606 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2607 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2612 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2614 prev_section_line_number = line_number;
2619 free(section_template);
2621 edit_free_file(file);
2623 free_map(section_exports);
2627 /* Should do all section-specific exports above this point */
2629 if (NULL == (urls = strdup("")))
2632 free(section_template);
2634 edit_free_file(file);
2636 free_map(section_exports);
2637 return JB_ERR_MEMORY;
2642 cur_line = cur_line->next;
2645 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2647 if (NULL == (url_exports = new_map()))
2651 free(section_template);
2653 edit_free_file(file);
2655 free_map(section_exports);
2656 return JB_ERR_MEMORY;
2659 snprintf(buf, sizeof(buf), "%d", line_number);
2660 err = map(url_exports, "p", 1, buf, 1);
2662 snprintf(buf, sizeof(buf), "%d", url_1_2);
2663 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2665 if (!err) err = map(url_exports, "url-html", 1,
2666 html_encode(cur_line->unprocessed), 0);
2667 if (!err) err = map(url_exports, "url", 1,
2668 url_encode(cur_line->unprocessed), 0);
2674 free(section_template);
2676 edit_free_file(file);
2678 free_map(section_exports);
2679 free_map(url_exports);
2683 if (NULL == (s = strdup(url_template)))
2687 free(section_template);
2689 edit_free_file(file);
2691 free_map(section_exports);
2692 free_map(url_exports);
2693 return JB_ERR_MEMORY;
2696 err = template_fill(&s, section_exports);
2697 if (!err) err = template_fill(&s, url_exports);
2698 if (!err) err = string_append(&urls, s);
2700 free_map(url_exports);
2707 free(section_template);
2709 edit_free_file(file);
2711 free_map(section_exports);
2715 url_1_2 = 3 - url_1_2;
2717 cur_line = cur_line->next;
2721 err = map(section_exports, "urls", 1, urls, 0);
2723 /* Could also do section-specific exports here, but it wouldn't be as fast */
2725 snprintf(buf, sizeof(buf), "%d", line_number);
2726 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2728 if ( (cur_line != NULL)
2729 && (cur_line->type == FILE_LINE_ACTION))
2731 /* Not last section */
2732 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2737 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2743 free(section_template);
2745 edit_free_file(file);
2747 free_map(section_exports);
2751 if (NULL == (s = strdup(section_template)))
2754 free(section_template);
2756 edit_free_file(file);
2758 free_map(section_exports);
2759 return JB_ERR_MEMORY;
2762 err = template_fill(&s, section_exports);
2763 if (!err) err = string_append(§ions, s);
2766 free_map(section_exports);
2771 free(section_template);
2773 edit_free_file(file);
2779 edit_free_file(file);
2780 free(section_template);
2783 err = map(exports, "sections", 1, sections, 0);
2790 /* Could also do global exports here, but it wouldn't be as fast */
2792 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2796 /*********************************************************************
2798 * Function : cgi_edit_actions_for_url
2800 * Description : CGI function that edits the Actions list.
2803 * 1 : csp = Current client state (buffers, headers, etc...)
2804 * 2 : rsp = http_response data structure for output
2805 * 3 : parameters = map of cgi parameters
2807 * CGI Parameters : None
2809 * Returns : JB_ERR_OK on success
2810 * JB_ERR_MEMORY on out-of-memory
2811 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2812 * specified or not valid.
2814 *********************************************************************/
2815 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2816 struct http_response *rsp,
2817 const struct map *parameters)
2819 struct map * exports;
2821 struct editable_file * file;
2822 struct file_line * cur_line;
2823 unsigned line_number;
2825 struct re_filterfile_spec *filter_group;
2826 int i, have_filters = 0;
2828 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2830 return cgi_error_disabled(csp, rsp);
2833 err = get_number_param(csp, parameters, "s", §ionid);
2839 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2842 /* No filename specified, can't read file, modified, or out of memory. */
2843 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2846 cur_line = file->lines;
2848 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2850 cur_line = cur_line->next;
2853 if ( (cur_line == NULL)
2854 || (line_number != sectionid)
2856 || (cur_line->type != FILE_LINE_ACTION))
2858 /* Invalid "sectionid" parameter */
2859 edit_free_file(file);
2860 return JB_ERR_CGI_PARAMS;
2863 if (NULL == (exports = default_exports(csp, NULL)))
2865 edit_free_file(file);
2866 return JB_ERR_MEMORY;
2869 err = map(exports, "f", 1, stringify(file->identifier), 0);
2870 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2871 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2873 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2876 * XXX: Some browsers (at least IE6 and IE7) have an artificial URL
2877 * length limitation and ignore clicks on the Submit buttons if
2878 * the resulting GET URL would be longer than their limit.
2880 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
2881 * reached this limit and action editing stopped working in these
2882 * browsers (BR #1570678).
2884 * The config option split-large-forms works around this browser
2885 * bug (HTTP has no URL length limitation) by deviding the action
2886 * list form into multiple smaller ones. It means the URLs are shorter
2887 * and work in broken browsers as well, but the user can no longer change
2888 * all actions with one submit.
2890 * A better solution would be to switch to POST requests,
2891 * but this will do for now.
2893 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
2895 /* Generate multiple smaller form by killing the big one. */
2896 err = map_block_killer(exports, "one-form-only");
2900 /* Default: Generate one large form by killing the smaller ones. */
2901 err = map_block_killer(exports, "multiple-forms");
2904 for (i = 0; i < MAX_AF_FILES; i++)
2906 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2908 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
2916 edit_free_file(file);
2921 if (0 == have_filters)
2923 err = map(exports, "filter-params", 1, "", 1);
2928 * List available filters and their settings.
2930 char *filter_template;
2931 int filter_identifier = 0;
2932 char *prepared_templates[MAX_FILTER_TYPES];
2934 for (i = 0; i < MAX_FILTER_TYPES; i++)
2936 prepared_templates[i] = strdup("");
2939 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2942 edit_free_file(file);
2944 if (err == JB_ERR_FILE)
2946 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2951 err = template_fill(&filter_template, exports);
2953 for (i = 0; i < MAX_AF_FILES; i++)
2955 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2957 filter_group = csp->rlist[i]->f;
2958 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2960 char current_mode = 'x';
2962 struct list_entry *filter_name;
2963 struct map *line_exports;
2964 const int type = filter_group->type;
2965 const int multi_action_index = filter_type_info[type].multi_action_index;
2967 assert(type < MAX_FILTER_TYPES);
2969 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
2970 while ((filter_name != NULL)
2971 && (0 != strcmp(filter_group->name, filter_name->str)))
2973 filter_name = filter_name->next;
2976 if (filter_name != NULL)
2982 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
2983 while ((filter_name != NULL)
2984 && (0 != strcmp(filter_group->name, filter_name->str)))
2986 filter_name = filter_name->next;
2988 if (filter_name != NULL)
2994 /* Generate a unique serial number */
2995 snprintf(number, sizeof(number), "%x", filter_identifier++);
2996 number[sizeof(number) - 1] = '\0';
2998 line_exports = new_map();
2999 if (line_exports == NULL)
3001 err = JB_ERR_MEMORY;
3007 if (!err) err = map(line_exports, "index", 1, number, 1);
3008 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3009 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3010 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3011 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
3012 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
3013 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
3017 filter_line = strdup(filter_template);
3018 if (filter_line == NULL) err = JB_ERR_MEMORY;
3020 if (!err) err = template_fill(&filter_line, line_exports);
3021 string_join(&prepared_templates[type], filter_line);
3023 free_map(line_exports);
3028 freez(filter_template);
3030 /* Replace all filter macros with the aggregated templates */
3031 for (i = 0; i < MAX_FILTER_TYPES; i++)
3034 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
3039 /* Free aggregated templates */
3040 for (i = 0; i < MAX_FILTER_TYPES; i++)
3042 freez(prepared_templates[i]);
3047 /* Check or uncheck the "disable all of this type" radio buttons. */
3048 for (i = 0; i < MAX_FILTER_TYPES; i++)
3050 const int a = filter_type_info[i].multi_action_index;
3051 const int disable_all = cur_line->data.action->multi_remove_all[a];
3053 err = map_radio(exports, filter_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
3056 edit_free_file(file);
3064 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3068 /*********************************************************************
3070 * Function : cgi_edit_actions_submit
3072 * Description : CGI function that actually edits the Actions list.
3075 * 1 : csp = Current client state (buffers, headers, etc...)
3076 * 2 : rsp = http_response data structure for output
3077 * 3 : parameters = map of cgi parameters
3079 * CGI Parameters : None
3081 * Returns : JB_ERR_OK on success
3082 * JB_ERR_MEMORY on out-of-memory
3083 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3084 * specified or not valid.
3086 *********************************************************************/
3087 jb_err cgi_edit_actions_submit(struct client_state *csp,
3088 struct http_response *rsp,
3089 const struct map *parameters)
3094 size_t newtext_size;
3096 struct editable_file * file;
3097 struct file_line * cur_line;
3098 unsigned line_number;
3101 int filter_identifier;
3103 const char * action_set_name;
3104 struct file_list * fl;
3105 struct url_actions * b;
3107 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3109 return cgi_error_disabled(csp, rsp);
3112 err = get_number_param(csp, parameters, "s", §ionid);
3118 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3121 /* No filename specified, can't read file, modified, or out of memory. */
3122 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3125 cur_line = file->lines;
3127 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3129 cur_line = cur_line->next;
3132 if ( (cur_line == NULL)
3133 || (line_number != sectionid)
3135 || (cur_line->type != FILE_LINE_ACTION))
3137 /* Invalid "sectionid" parameter */
3138 edit_free_file(file);
3139 return JB_ERR_CGI_PARAMS;
3142 get_string_param(parameters, "p", &action_set_name);
3143 if (action_set_name != NULL)
3145 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3147 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3149 for (b = b->next; NULL != b; b = b->next)
3151 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3153 copy_action(cur_line->data.action, b->action);
3159 edit_free_file(file);
3160 return JB_ERR_CGI_PARAMS;
3166 err = actions_from_radio(parameters, cur_line->data.action);
3172 edit_free_file(file);
3176 /* Check the "disable all of this type" parameters. */
3177 for (i = 0; i < MAX_FILTER_TYPES; i++)
3179 const int multi_action_index = filter_type_info[i].multi_action_index;
3180 const char ch = get_char_param(parameters, filter_type_info[i].disable_all_param);
3184 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3185 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3186 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3190 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3194 for (filter_identifier = 0; !err; filter_identifier++)
3201 * Filter state. Valid states are: 'Y' (active),
3202 * 'N' (inactive) and 'X' (no change).
3206 * Abbreviated filter type. Valid types are: 'F' (content filter),
3207 * 'S' (server-header filter) and 'C' (client-header filter).
3209 int multi_action_index = 0;
3211 /* Generate the keys */
3212 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3213 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3214 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3215 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3216 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3218 err = get_string_param(parameters, key_name, &name);
3227 type = get_char_param(parameters, key_type);
3231 multi_action_index = ACTION_MULTI_FILTER;
3234 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3237 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3240 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3243 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3246 log_error(LOG_LEVEL_ERROR,
3247 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3250 assert(multi_action_index);
3252 value = get_char_param(parameters, key_value);
3255 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3256 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3257 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3259 else if (value == 'N')
3261 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3262 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3264 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3265 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3268 else if (value == 'X')
3270 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3271 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3278 edit_free_file(file);
3282 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3285 edit_free_file(file);
3286 return JB_ERR_MEMORY;
3289 len = strlen(actiontext);
3293 * Empty action - must special-case this.
3294 * Simply setting len to 1 is sufficient...
3299 newtext_size = len + 2;
3300 if (NULL == (newtext = malloc(newtext_size)))
3304 edit_free_file(file);
3305 return JB_ERR_MEMORY;
3307 strlcpy(newtext, actiontext, newtext_size);
3311 newtext[len + 1] = '\0';
3313 freez(cur_line->raw);
3314 freez(cur_line->unprocessed);
3315 cur_line->unprocessed = newtext;
3317 err = edit_write_file(file);
3320 /* Error writing file */
3321 if (err == JB_ERR_FILE)
3323 /* Read-only file. */
3324 err = cgi_error_file_read_only(csp, rsp, file->filename);
3326 edit_free_file(file);
3330 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3331 (long) time(NULL), file->identifier, sectionid);
3333 edit_free_file(file);
3335 return cgi_redirect(rsp, target);
3339 /*********************************************************************
3341 * Function : cgi_edit_actions_url
3343 * Description : CGI function that actually edits a URL pattern in
3347 * 1 : csp = Current client state (buffers, headers, etc...)
3348 * 2 : rsp = http_response data structure for output
3349 * 3 : parameters = map of cgi parameters
3352 * filename : Identifies the file to edit
3353 * ver : File's last-modified time
3354 * section : Line number of section to edit
3355 * pattern : Line number of pattern to edit
3356 * newval : New value for pattern
3358 * Returns : JB_ERR_OK on success
3359 * JB_ERR_MEMORY on out-of-memory
3360 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3361 * specified or not valid.
3363 *********************************************************************/
3364 jb_err cgi_edit_actions_url(struct client_state *csp,
3365 struct http_response *rsp,
3366 const struct map *parameters)
3370 struct editable_file * file;
3371 struct file_line * cur_line;
3372 unsigned line_number;
3373 unsigned section_start_line_number = 0;
3381 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3383 return cgi_error_disabled(csp, rsp);
3386 err = get_number_param(csp, parameters, "p", &patternid);
3393 return JB_ERR_CGI_PARAMS;
3396 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3402 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3405 /* No filename specified, can't read file, modified, or out of memory. */
3407 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3411 cur_line = file->lines;
3413 while ((cur_line != NULL) && (line_number < patternid))
3415 if (cur_line->type == FILE_LINE_ACTION)
3417 section_start_line_number = line_number;
3419 cur_line = cur_line->next;
3423 if ( (cur_line == NULL)
3424 || (cur_line->type != FILE_LINE_URL))
3426 /* Invalid "patternid" parameter */
3428 edit_free_file(file);
3429 return JB_ERR_CGI_PARAMS;
3432 /* At this point, the line to edit is in cur_line */
3434 freez(cur_line->raw);
3435 freez(cur_line->unprocessed);
3436 cur_line->unprocessed = new_pattern;
3438 err = edit_write_file(file);
3441 /* Error writing file */
3442 if (err == JB_ERR_FILE)
3444 /* Read-only file. */
3445 err = cgi_error_file_read_only(csp, rsp, file->filename);
3447 edit_free_file(file);
3451 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3452 (long) time(NULL), file->identifier, section_start_line_number);
3454 edit_free_file(file);
3456 return cgi_redirect(rsp, target);
3460 /*********************************************************************
3462 * Function : cgi_edit_actions_add_url
3464 * Description : CGI function that actually adds a URL pattern to
3468 * 1 : csp = Current client state (buffers, headers, etc...)
3469 * 2 : rsp = http_response data structure for output
3470 * 3 : parameters = map of cgi parameters
3473 * filename : Identifies the file to edit
3474 * ver : File's last-modified time
3475 * section : Line number of section to edit
3476 * newval : New pattern
3478 * Returns : JB_ERR_OK on success
3479 * JB_ERR_MEMORY on out-of-memory
3480 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3481 * specified or not valid.
3483 *********************************************************************/
3484 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3485 struct http_response *rsp,
3486 const struct map *parameters)
3490 struct file_line * new_line;
3491 struct editable_file * file;
3492 struct file_line * cur_line;
3493 unsigned line_number;
3497 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3499 return cgi_error_disabled(csp, rsp);
3502 err = get_number_param(csp, parameters, "s", §ionid);
3509 return JB_ERR_CGI_PARAMS;
3512 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3518 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3521 /* No filename specified, can't read file, modified, or out of memory. */
3523 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3527 cur_line = file->lines;
3529 while ((cur_line != NULL) && (line_number < sectionid))
3531 cur_line = cur_line->next;
3535 if ( (cur_line == NULL)
3536 || (cur_line->type != FILE_LINE_ACTION))
3538 /* Invalid "sectionid" parameter */
3540 edit_free_file(file);
3541 return JB_ERR_CGI_PARAMS;
3544 /* At this point, the section header is in cur_line - add after this. */
3546 /* Allocate the new line */
3547 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3548 if (new_line == NULL)
3551 edit_free_file(file);
3552 return JB_ERR_MEMORY;
3555 /* Fill in the data members of the new line */
3556 new_line->raw = NULL;
3557 new_line->prefix = NULL;
3558 new_line->unprocessed = new_pattern;
3559 new_line->type = FILE_LINE_URL;
3561 /* Link new_line into the list, after cur_line */
3562 new_line->next = cur_line->next;
3563 cur_line->next = new_line;
3565 /* Done making changes, now commit */
3567 err = edit_write_file(file);
3570 /* Error writing file */
3571 if (err == JB_ERR_FILE)
3573 /* Read-only file. */
3574 err = cgi_error_file_read_only(csp, rsp, file->filename);
3576 edit_free_file(file);
3580 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3581 (long) time(NULL), file->identifier, sectionid);
3583 edit_free_file(file);
3585 return cgi_redirect(rsp, target);
3589 /*********************************************************************
3591 * Function : cgi_edit_actions_remove_url
3593 * Description : CGI function that actually removes a URL pattern from
3597 * 1 : csp = Current client state (buffers, headers, etc...)
3598 * 2 : rsp = http_response data structure for output
3599 * 3 : parameters = map of cgi parameters
3602 * f : (filename) Identifies the file to edit
3603 * v : (version) File's last-modified time
3604 * p : (pattern) Line number of pattern to remove
3606 * Returns : JB_ERR_OK on success
3607 * JB_ERR_MEMORY on out-of-memory
3608 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3609 * specified or not valid.
3611 *********************************************************************/
3612 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3613 struct http_response *rsp,
3614 const struct map *parameters)
3617 struct editable_file * file;
3618 struct file_line * cur_line;
3619 struct file_line * prev_line;
3620 unsigned line_number;
3621 unsigned section_start_line_number = 0;
3625 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3627 return cgi_error_disabled(csp, rsp);
3630 err = get_number_param(csp, parameters, "p", &patternid);
3636 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3639 /* No filename specified, can't read file, modified, or out of memory. */
3640 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3645 cur_line = file->lines;
3647 while ((cur_line != NULL) && (line_number < patternid))
3649 if (cur_line->type == FILE_LINE_ACTION)
3651 section_start_line_number = line_number;
3653 prev_line = cur_line;
3654 cur_line = cur_line->next;
3658 if ( (cur_line == NULL)
3659 || (prev_line == NULL)
3660 || (cur_line->type != FILE_LINE_URL))
3662 /* Invalid "patternid" parameter */
3663 edit_free_file(file);
3664 return JB_ERR_CGI_PARAMS;
3667 /* At this point, the line to remove is in cur_line, and the previous
3668 * one is in prev_line
3671 /* Unlink cur_line */
3672 prev_line->next = cur_line->next;
3673 cur_line->next = NULL;
3676 edit_free_file_lines(cur_line);
3678 err = edit_write_file(file);
3681 /* Error writing file */
3682 if (err == JB_ERR_FILE)
3684 /* Read-only file. */
3685 err = cgi_error_file_read_only(csp, rsp, file->filename);
3687 edit_free_file(file);
3691 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3692 (long) time(NULL), file->identifier, section_start_line_number);
3694 edit_free_file(file);
3696 return cgi_redirect(rsp, target);
3700 /*********************************************************************
3702 * Function : cgi_edit_actions_section_remove
3704 * Description : CGI function that actually removes a whole section from
3705 * the actions file. The section must be empty first
3706 * (else JB_ERR_CGI_PARAMS).
3709 * 1 : csp = Current client state (buffers, headers, etc...)
3710 * 2 : rsp = http_response data structure for output
3711 * 3 : parameters = map of cgi parameters
3714 * f : (filename) Identifies the file to edit
3715 * v : (version) File's last-modified time
3716 * s : (section) Line number of section to edit
3718 * Returns : JB_ERR_OK on success
3719 * JB_ERR_MEMORY on out-of-memory
3720 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3721 * specified or not valid.
3723 *********************************************************************/
3724 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3725 struct http_response *rsp,
3726 const struct map *parameters)
3729 struct editable_file * file;
3730 struct file_line * cur_line;
3731 struct file_line * prev_line;
3732 unsigned line_number;
3736 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3738 return cgi_error_disabled(csp, rsp);
3741 err = get_number_param(csp, parameters, "s", §ionid);
3747 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3750 /* No filename specified, can't read file, modified, or out of memory. */
3751 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3755 cur_line = file->lines;
3758 while ((cur_line != NULL) && (line_number < sectionid))
3760 prev_line = cur_line;
3761 cur_line = cur_line->next;
3765 if ( (cur_line == NULL)
3766 || (cur_line->type != FILE_LINE_ACTION) )
3768 /* Invalid "sectionid" parameter */
3769 edit_free_file(file);
3770 return JB_ERR_CGI_PARAMS;
3773 if ( (cur_line->next != NULL)
3774 && (cur_line->next->type == FILE_LINE_URL) )
3776 /* Section not empty. */
3777 edit_free_file(file);
3778 return JB_ERR_CGI_PARAMS;
3781 /* At this point, the line to remove is in cur_line, and the previous
3782 * one is in prev_line
3785 /* Unlink cur_line */
3786 if (prev_line == NULL)
3788 /* Removing the first line from the file */
3789 file->lines = cur_line->next;
3793 prev_line->next = cur_line->next;
3795 cur_line->next = NULL;
3798 edit_free_file_lines(cur_line);
3800 err = edit_write_file(file);
3803 /* Error writing file */
3804 if (err == JB_ERR_FILE)
3806 /* Read-only file. */
3807 err = cgi_error_file_read_only(csp, rsp, file->filename);
3809 edit_free_file(file);
3813 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
3814 (long) time(NULL), file->identifier);
3816 edit_free_file(file);
3818 return cgi_redirect(rsp, target);
3822 /*********************************************************************
3824 * Function : cgi_edit_actions_section_add
3826 * Description : CGI function that adds a new empty section to
3830 * 1 : csp = Current client state (buffers, headers, etc...)
3831 * 2 : rsp = http_response data structure for output
3832 * 3 : parameters = map of cgi parameters
3835 * f : (filename) Identifies the file to edit
3836 * v : (version) File's last-modified time
3837 * s : (section) Line number of section to add after, 0 for
3840 * Returns : JB_ERR_OK on success
3841 * JB_ERR_MEMORY on out-of-memory
3842 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3843 * specified or not valid.
3845 *********************************************************************/
3846 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3847 struct http_response *rsp,
3848 const struct map *parameters)
3851 struct file_line * new_line;
3853 struct editable_file * file;
3854 struct file_line * cur_line;
3855 unsigned line_number;
3859 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3861 return cgi_error_disabled(csp, rsp);
3864 err = get_number_param(csp, parameters, "s", §ionid);
3870 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3873 /* No filename specified, can't read file, modified, or out of memory. */
3874 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3878 cur_line = file->lines;
3880 if (sectionid <= 1U)
3882 /* Add to start of file */
3883 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
3885 /* There's something in the file, find the line before the first
3888 while ( (cur_line->next != NULL)
3889 && (cur_line->next->type != FILE_LINE_ACTION) )
3891 cur_line = cur_line->next;
3897 /* File starts with action line, so insert at top */
3903 /* Add after stated section. */
3904 while ((cur_line != NULL) && (line_number < sectionid))
3906 cur_line = cur_line->next;
3910 if ( (cur_line == NULL)
3911 || (cur_line->type != FILE_LINE_ACTION))
3913 /* Invalid "sectionid" parameter */
3914 edit_free_file(file);
3915 return JB_ERR_CGI_PARAMS;
3918 /* Skip through the section to find the last line in it. */
3919 while ( (cur_line->next != NULL)
3920 && (cur_line->next->type != FILE_LINE_ACTION) )
3922 cur_line = cur_line->next;
3927 /* At this point, the last line in the previous section is in cur_line
3928 * - add after this. (Or if we need to add as the first line, cur_line
3932 new_text = strdup("{}");
3933 if (NULL == new_text)
3935 edit_free_file(file);
3936 return JB_ERR_MEMORY;
3939 /* Allocate the new line */
3940 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3941 if (new_line == NULL)
3944 edit_free_file(file);
3945 return JB_ERR_MEMORY;
3948 /* Fill in the data members of the new line */
3949 new_line->raw = NULL;
3950 new_line->prefix = NULL;
3951 new_line->unprocessed = new_text;
3952 new_line->type = FILE_LINE_ACTION;
3954 if (cur_line != NULL)
3956 /* Link new_line into the list, after cur_line */
3957 new_line->next = cur_line->next;
3958 cur_line->next = new_line;
3962 /* Link new_line into the list, as first line */
3963 new_line->next = file->lines;
3964 file->lines = new_line;
3967 /* Done making changes, now commit */
3969 err = edit_write_file(file);
3972 /* Error writing file */
3973 if (err == JB_ERR_FILE)
3975 /* Read-only file. */
3976 err = cgi_error_file_read_only(csp, rsp, file->filename);
3978 edit_free_file(file);
3982 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
3983 (long) time(NULL), file->identifier);
3985 edit_free_file(file);
3987 return cgi_redirect(rsp, target);
3991 /*********************************************************************
3993 * Function : cgi_edit_actions_section_swap
3995 * Description : CGI function that swaps the order of two sections
3996 * in the actions file. Note that this CGI can actually
3997 * swap any two arbitrary sections, but the GUI interface
3998 * currently only allows consecutive sections to be
4002 * 1 : csp = Current client state (buffers, headers, etc...)
4003 * 2 : rsp = http_response data structure for output
4004 * 3 : parameters = map of cgi parameters
4007 * f : (filename) Identifies the file to edit
4008 * v : (version) File's last-modified time
4009 * s1 : (section1) Line number of first section to swap
4010 * s2 : (section2) Line number of second section to swap
4012 * Returns : JB_ERR_OK on success
4013 * JB_ERR_MEMORY on out-of-memory
4014 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4015 * specified or not valid.
4017 *********************************************************************/
4018 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4019 struct http_response *rsp,
4020 const struct map *parameters)
4024 struct editable_file * file;
4025 struct file_line * cur_line;
4026 struct file_line * prev_line;
4027 struct file_line * line_before_section1;
4028 struct file_line * line_start_section1;
4029 struct file_line * line_end_section1;
4030 struct file_line * line_after_section1;
4031 struct file_line * line_before_section2;
4032 struct file_line * line_start_section2;
4033 struct file_line * line_end_section2;
4034 struct file_line * line_after_section2;
4035 unsigned line_number;
4039 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4041 return cgi_error_disabled(csp, rsp);
4044 err = get_number_param(csp, parameters, "s1", §ion1);
4045 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4051 if (section1 > section2)
4053 unsigned temp = section2;
4054 section2 = section1;
4058 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4061 /* No filename specified, can't read file, modified, or out of memory. */
4062 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4065 /* Start at the beginning... */
4067 cur_line = file->lines;
4070 /* ... find section1 ... */
4071 while ((cur_line != NULL) && (line_number < section1))
4073 prev_line = cur_line;
4074 cur_line = cur_line->next;
4078 if ( (cur_line == NULL)
4079 || (cur_line->type != FILE_LINE_ACTION) )
4081 /* Invalid "section1" parameter */
4082 edit_free_file(file);
4083 return JB_ERR_CGI_PARAMS;
4086 /* If no-op, we've validated params and can skip the rest. */
4087 if (section1 != section2)
4089 /* ... find the end of section1 ... */
4090 line_before_section1 = prev_line;
4091 line_start_section1 = cur_line;
4094 prev_line = cur_line;
4095 cur_line = cur_line->next;
4098 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4099 line_end_section1 = prev_line;
4100 line_after_section1 = cur_line;
4102 /* ... find section2 ... */
4103 while ((cur_line != NULL) && (line_number < section2))
4105 prev_line = cur_line;
4106 cur_line = cur_line->next;
4110 if ( (cur_line == NULL)
4111 || (cur_line->type != FILE_LINE_ACTION) )
4113 /* Invalid "section2" parameter */
4114 edit_free_file(file);
4115 return JB_ERR_CGI_PARAMS;
4118 /* ... find the end of section2 ... */
4119 line_before_section2 = prev_line;
4120 line_start_section2 = cur_line;
4123 prev_line = cur_line;
4124 cur_line = cur_line->next;
4127 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4128 line_end_section2 = prev_line;
4129 line_after_section2 = cur_line;
4131 /* Now have all the pointers we need. Do the swap. */
4133 /* Change the pointer to section1 to point to section2 instead */
4134 if (line_before_section1 == NULL)
4136 file->lines = line_start_section2;
4140 line_before_section1->next = line_start_section2;
4143 if (line_before_section2 == line_end_section1)
4145 /* Consecutive sections */
4146 line_end_section2->next = line_start_section1;
4150 line_end_section2->next = line_after_section1;
4151 line_before_section2->next = line_start_section1;
4154 /* Set the pointer from the end of section1 to the rest of the file */
4155 line_end_section1->next = line_after_section2;
4157 err = edit_write_file(file);
4160 /* Error writing file */
4161 if (err == JB_ERR_FILE)
4163 /* Read-only file. */
4164 err = cgi_error_file_read_only(csp, rsp, file->filename);
4166 edit_free_file(file);
4169 } /* END if (section1 != section2) */
4171 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4172 (long) time(NULL), file->identifier);
4174 edit_free_file(file);
4176 return cgi_redirect(rsp, target);
4180 /*********************************************************************
4182 * Function : javascriptify
4184 * Description : Converts a string into a form JavaScript will like.
4186 * Netscape 4's JavaScript sucks - it doesn't use
4187 * "id" parameters, so you have to set the "name"
4188 * used to submit a form element to something JavaScript
4189 * will like. (Or access the elements by index in an
4190 * array. That array contains >60 elements and will
4191 * be changed whenever we add a new action to the
4192 * editor, so I'm NOT going to use indexes that have
4193 * to be figured out by hand.)
4195 * Currently the only thing we have to worry about
4196 * is "-" ==> "_" conversion.
4198 * This is a length-preserving operation so it is
4199 * carried out in-place, no memory is allocated
4203 * 1 : identifier = String to make JavaScript-friendly.
4207 *********************************************************************/
4208 static void javascriptify(char * identifier)
4210 char * p = identifier;
4211 while (NULL != (p = strchr(p, '-')))
4218 /*********************************************************************
4220 * Function : actions_to_radio
4222 * Description : Converts a actionsfile entry into settings for
4223 * radio buttons and edit boxes on a HTML form.
4226 * 1 : exports = List of substitutions to add to.
4227 * 2 : action = Action to read
4229 * Returns : JB_ERR_OK on success
4230 * JB_ERR_MEMORY on out-of-memory
4232 *********************************************************************/
4233 static jb_err actions_to_radio(struct map * exports,
4234 const struct action_spec *action)
4245 mask = action->mask;
4248 /* sanity - prevents "-feature +feature" */
4252 #define DEFINE_ACTION_BOOL(name, bit) \
4253 if (!(mask & bit)) \
4255 current_mode = 'n'; \
4257 else if (add & bit) \
4259 current_mode = 'y'; \
4263 current_mode = 'x'; \
4265 if (map_radio(exports, name, "ynx", current_mode)) \
4267 return JB_ERR_MEMORY; \
4270 #define DEFINE_ACTION_STRING(name, bit, index) \
4271 DEFINE_ACTION_BOOL(name, bit); \
4274 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4277 checked = !strcmp(action->string[index], value); \
4281 checked = is_default; \
4283 mapped_param |= checked; \
4284 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4286 return JB_ERR_MEMORY; \
4289 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4290 if (map(exports, name "-param-custom", 1, \
4291 ((!mapped_param) ? "checked" : ""), 1)) \
4293 return JB_ERR_MEMORY; \
4295 if (map(exports, name "-param", 1, \
4296 (((add & bit) && !mapped_param) ? \
4297 action->string[index] : default_val), 1)) \
4299 return JB_ERR_MEMORY; \
4302 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4303 if (map(exports, name "-param", 1, \
4304 ((add & bit) ? action->string[index] : default_val), 1)) \
4306 return JB_ERR_MEMORY; \
4309 #define DEFINE_ACTION_MULTI(name, index) \
4310 if (action->multi_add[index]->first) \
4312 current_mode = 'y'; \
4314 else if (action->multi_remove_all[index]) \
4316 current_mode = 'n'; \
4318 else if (action->multi_remove[index]->first) \
4320 current_mode = 'y'; \
4324 current_mode = 'x'; \
4326 if (map_radio(exports, name, "ynx", current_mode)) \
4328 return JB_ERR_MEMORY; \
4331 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4333 #include "actionlist.h"
4335 #undef DEFINE_ACTION_MULTI
4336 #undef DEFINE_ACTION_STRING
4337 #undef DEFINE_ACTION_BOOL
4338 #undef DEFINE_ACTION_ALIAS
4339 #undef DEFINE_CGI_PARAM_CUSTOM
4340 #undef DEFINE_CGI_PARAM_RADIO
4341 #undef DEFINE_CGI_PARAM_NO_RADIO
4347 /*********************************************************************
4349 * Function : actions_from_radio
4351 * Description : Converts a map of parameters passed to a CGI function
4352 * into an actionsfile entry.
4355 * 1 : parameters = parameters to the CGI call
4356 * 2 : action = Action to change. Must be valid before
4357 * the call, actions not specified will be
4360 * Returns : JB_ERR_OK on success
4361 * JB_ERR_MEMORY on out-of-memory
4363 *********************************************************************/
4364 static jb_err actions_from_radio(const struct map * parameters,
4365 struct action_spec *action)
4370 const char * js_name;
4371 jb_err err = JB_ERR_OK;
4376 /* Statics are generally a potential race condition,
4377 * but in this case we're safe and don't need semaphores.
4378 * Be careful if you modify this function.
4380 * The js_name_arr's are never free()d, but this is no
4381 * problem, since they will only be created once and
4382 * used by all threads thereafter. -oes
4385 #define JAVASCRIPTIFY(dest_var, string) \
4387 static int first_time = 1; \
4388 static char *js_name_arr; \
4391 js_name_arr = strdup(string); \
4392 javascriptify(js_name_arr); \
4394 dest_var = js_name_arr; \
4398 #define DEFINE_ACTION_BOOL(name, bit) \
4399 JAVASCRIPTIFY(js_name, name); \
4400 ch = get_char_param(parameters, js_name); \
4403 action->add |= bit; \
4404 action->mask |= bit; \
4406 else if (ch == 'N') \
4408 action->add &= ~bit; \
4409 action->mask &= ~bit; \
4411 else if (ch == 'X') \
4413 action->add &= ~bit; \
4414 action->mask |= bit; \
4417 #define DEFINE_ACTION_STRING(name, bit, index) \
4418 JAVASCRIPTIFY(js_name, name); \
4419 ch = get_char_param(parameters, js_name); \
4423 JAVASCRIPTIFY(js_name, name "-mode"); \
4424 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4425 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4427 JAVASCRIPTIFY(js_name, name "-param"); \
4428 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4430 if (param != NULL) \
4432 if (NULL == (param_dup = strdup(param))) \
4434 return JB_ERR_MEMORY; \
4436 freez(action->string[index]); \
4437 action->add |= bit; \
4438 action->mask |= bit; \
4439 action->string[index] = param_dup; \
4442 else if (ch == 'N') \
4444 if (action->add & bit) \
4446 freez(action->string[index]); \
4448 action->add &= ~bit; \
4449 action->mask &= ~bit; \
4451 else if (ch == 'X') \
4453 if (action->add & bit) \
4455 freez(action->string[index]); \
4457 action->add &= ~bit; \
4458 action->mask |= bit; \
4461 #define DEFINE_ACTION_MULTI(name, index) \
4462 JAVASCRIPTIFY(js_name, name); \
4463 ch = get_char_param(parameters, js_name); \
4468 else if (ch == 'N') \
4470 list_remove_all(action->multi_add[index]); \
4471 list_remove_all(action->multi_remove[index]); \
4472 action->multi_remove_all[index] = 1; \
4474 else if (ch == 'X') \
4476 list_remove_all(action->multi_add[index]); \
4477 list_remove_all(action->multi_remove[index]); \
4478 action->multi_remove_all[index] = 0; \
4481 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4483 #include "actionlist.h"
4485 #undef DEFINE_ACTION_MULTI
4486 #undef DEFINE_ACTION_STRING
4487 #undef DEFINE_ACTION_BOOL
4488 #undef DEFINE_ACTION_ALIAS
4489 #undef JAVASCRIPTIFY
4493 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4496 #ifdef FEATURE_TOGGLE
4497 /*********************************************************************
4499 * Function : cgi_toggle
4501 * Description : CGI function that adds a new empty section to
4505 * 1 : csp = Current client state (buffers, headers, etc...)
4506 * 2 : rsp = http_response data structure for output
4507 * 3 : parameters = map of cgi parameters
4510 * set : If present, how to change toggle setting:
4511 * "enable", "disable", "toggle", or none (default).
4512 * mini : If present, use mini reply template.
4514 * Returns : JB_ERR_OK on success
4515 * JB_ERR_MEMORY on out-of-memory
4517 *********************************************************************/
4518 jb_err cgi_toggle(struct client_state *csp,
4519 struct http_response *rsp,
4520 const struct map *parameters)
4522 struct map *exports;
4524 const char *template_name;
4530 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4532 return cgi_error_disabled(csp, rsp);
4535 mode = get_char_param(parameters, "set");
4540 global_toggle_state = 1;
4542 else if (mode == 'D')
4545 global_toggle_state = 0;
4547 else if (mode == 'T')
4550 global_toggle_state = !global_toggle_state;
4553 if (NULL == (exports = default_exports(csp, "toggle")))
4555 return JB_ERR_MEMORY;
4558 template_name = (get_char_param(parameters, "mini")
4562 return template_fill_for_cgi(csp, template_name, exports, rsp);
4564 #endif /* def FEATURE_TOGGLE */