1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.70 2011/09/04 11:10:56 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,
325 static jb_err get_file_name_param(struct client_state *csp,
326 const struct map *parameters,
327 const char *param_name,
328 const char **pfilename);
330 /* Internal convenience functions */
331 static char *section_target(const unsigned sectionid);
333 /*********************************************************************
335 * Function : section_target
337 * Description : Given an unsigned (section id) n, produce a dynamically
338 * allocated string of the form #l<n>, for use in link
341 * XXX: The hash should be moved into the templates
342 * to make this function more generic and render
343 * stringify() obsolete.
346 * 1 : sectionid = start line number of section
348 * Returns : String with link target, or NULL if out of
351 *********************************************************************/
352 static char *section_target(const unsigned sectionid)
356 snprintf(buf, sizeof(buf), "#l%d", sectionid);
362 /*********************************************************************
364 * Function : stringify
366 * Description : Convert a number into a dynamically allocated string.
369 * 1 : number = The number to convert.
371 * Returns : String with link target, or NULL if out of memory
373 *********************************************************************/
374 static char *stringify(const unsigned number)
378 snprintf(buf, sizeof(buf), "%i", number);
383 /*********************************************************************
385 * Function : map_copy_parameter_html
387 * Description : Copy a CGI parameter from one map to another, HTML
391 * 1 : out = target map
392 * 2 : in = source map
393 * 3 : name = name of cgi parameter to copy
395 * Returns : JB_ERR_OK on success
396 * JB_ERR_MEMORY on out-of-memory
397 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
400 *********************************************************************/
401 static jb_err map_copy_parameter_html(struct map *out,
402 const struct map *in,
412 value = lookup(in, name);
413 err = map(out, name, 1, html_encode(value), 0);
420 else if (*value == '\0')
422 return JB_ERR_CGI_PARAMS;
431 /*********************************************************************
433 * Function : cgi_edit_actions_url_form
435 * Description : CGI function that displays a form for
439 * 1 : csp = Current client state (buffers, headers, etc...)
440 * 2 : rsp = http_response data structure for output
441 * 3 : parameters = map of cgi parameters
444 * i : (action index) Identifies the file to edit
445 * v : (version) File's last-modified time
446 * p : (pattern) Line number of pattern to edit
448 * Returns : JB_ERR_OK on success
449 * JB_ERR_MEMORY on out-of-memory
450 * JB_ERR_CGI_PARAMS if the CGI parameters are not
451 * specified or not valid.
453 *********************************************************************/
454 jb_err cgi_edit_actions_url_form(struct client_state *csp,
455 struct http_response *rsp,
456 const struct map *parameters)
458 struct map * exports;
460 struct editable_file * file;
461 struct file_line * cur_line;
462 unsigned line_number;
463 unsigned section_start_line_number = 0;
470 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
472 return cgi_error_disabled(csp, rsp);
475 err = get_number_param(csp, parameters, "p", &patternid);
481 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
484 /* No filename specified, can't read file, modified, or out of memory. */
485 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
488 cur_line = file->lines;
490 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
492 if (cur_line->type == FILE_LINE_ACTION)
494 section_start_line_number = line_number;
496 cur_line = cur_line->next;
499 if ( (cur_line == NULL)
500 || (line_number != patternid)
502 || (cur_line->type != FILE_LINE_URL))
504 /* Invalid "patternid" parameter */
505 edit_free_file(file);
506 return JB_ERR_CGI_PARAMS;
509 if (NULL == (exports = default_exports(csp, NULL)))
511 edit_free_file(file);
512 return JB_ERR_MEMORY;
515 err = map(exports, "f", 1, stringify(file->identifier), 0);
516 if (!err) err = map(exports, "v", 1, file->version_str, 1);
517 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
518 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
519 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
521 edit_free_file(file);
529 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
533 /*********************************************************************
535 * Function : cgi_edit_actions_add_url_form
537 * Description : CGI function that displays a form for
541 * 1 : csp = Current client state (buffers, headers, etc...)
542 * 2 : rsp = http_response data structure for output
543 * 3 : parameters = map of cgi parameters
546 * f : (filename) Identifies the file to edit
547 * v : (version) File's last-modified time
548 * s : (section) Line number of section to edit
550 * Returns : JB_ERR_OK on success
551 * JB_ERR_MEMORY on out-of-memory
552 * JB_ERR_CGI_PARAMS if the CGI parameters are not
553 * specified or not valid.
555 *********************************************************************/
556 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
557 struct http_response *rsp,
558 const struct map *parameters)
567 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
569 return cgi_error_disabled(csp, rsp);
572 if (NULL == (exports = default_exports(csp, NULL)))
574 return JB_ERR_MEMORY;
577 err = map_copy_parameter_html(exports, parameters, "f");
578 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
579 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
587 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
591 /*********************************************************************
593 * Function : cgi_edit_actions_remove_url_form
595 * Description : CGI function that displays a form for
599 * 1 : csp = Current client state (buffers, headers, etc...)
600 * 2 : rsp = http_response data structure for output
601 * 3 : parameters = map of cgi parameters
604 * f : (number) The action file identifier.
605 * v : (version) File's last-modified time
606 * p : (pattern) Line number of pattern to edit
608 * Returns : JB_ERR_OK on success
609 * JB_ERR_MEMORY on out-of-memory
610 * JB_ERR_CGI_PARAMS if the CGI parameters are not
611 * specified or not valid.
613 *********************************************************************/
614 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
615 struct http_response *rsp,
616 const struct map *parameters)
618 struct map * exports;
620 struct editable_file * file;
621 struct file_line * cur_line;
622 unsigned line_number;
623 unsigned section_start_line_number = 0;
630 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
632 return cgi_error_disabled(csp, rsp);
635 err = get_number_param(csp, parameters, "p", &patternid);
641 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
644 /* No filename specified, can't read file, modified, or out of memory. */
645 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
648 cur_line = file->lines;
650 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
652 if (cur_line->type == FILE_LINE_ACTION)
654 section_start_line_number = line_number;
656 cur_line = cur_line->next;
659 if ( (cur_line == NULL)
660 || (line_number != patternid)
662 || (cur_line->type != FILE_LINE_URL))
664 /* Invalid "patternid" parameter */
665 edit_free_file(file);
666 return JB_ERR_CGI_PARAMS;
669 if (NULL == (exports = default_exports(csp, NULL)))
671 edit_free_file(file);
672 return JB_ERR_MEMORY;
675 err = map(exports, "f", 1, stringify(file->identifier), 0);
676 if (!err) err = map(exports, "v", 1, file->version_str, 1);
677 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
678 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
679 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
680 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
682 edit_free_file(file);
690 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
694 /*********************************************************************
696 * Function : edit_write_file
698 * Description : Write a complete file to disk.
701 * 1 : file = File to write.
703 * Returns : JB_ERR_OK on success
704 * JB_ERR_FILE on error writing to file.
705 * JB_ERR_MEMORY on out of memory
707 *********************************************************************/
708 jb_err edit_write_file(struct editable_file * file)
711 struct file_line * cur_line;
712 struct stat statbuf[1];
713 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
714 digits in time_t, assuming this is a 64-bit
715 machine, plus null terminator, plus one
719 assert(file->filename);
721 if (NULL == (fp = fopen(file->filename, "wb")))
726 cur_line = file->lines;
727 while (cur_line != NULL)
731 if (fputs(cur_line->raw, fp) < 0)
739 if (cur_line->prefix)
741 if (fputs(cur_line->prefix, fp) < 0)
747 if (cur_line->unprocessed)
750 if (NULL != strchr(cur_line->unprocessed, '#'))
752 /* Must quote '#' characters */
759 /* Count number of # characters, so we know length of output string */
760 src = cur_line->unprocessed;
761 while (NULL != (src = strchr(src, '#')))
768 /* Allocate new memory for string */
769 len = strlen(cur_line->unprocessed) + (size_t)numhash;
770 if (NULL == (str = malloc(len + 1)))
772 /* Uh oh, just trashed file! */
774 return JB_ERR_MEMORY;
777 /* Copy string but quote hashes */
778 src = cur_line->unprocessed;
786 assert(numhash >= 0);
792 assert(numhash == 0);
793 assert(strlen(str) == len);
794 assert(str == dest - len);
795 assert(src - len <= cur_line->unprocessed);
797 if ((strlen(str) != len) || (numhash != 0))
800 * Escaping didn't work as expected, go spread the news.
801 * Only reached in non-debugging builds.
803 log_error(LOG_LEVEL_ERROR,
804 "Looks like hash escaping failed. %s might be corrupted now.",
808 if (fputs(str, fp) < 0)
819 /* Can write without quoting '#' characters. */
820 if (fputs(cur_line->unprocessed, fp) < 0)
826 if (fputs(NEWLINE(file->newline), fp) < 0)
834 /* FIXME: Write data from file->data->whatever */
838 cur_line = cur_line->next;
844 /* Update the version stamp in the file structure, since we just
845 * wrote to the file & changed it's date.
847 if (stat(file->filename, statbuf) < 0)
849 /* Error, probably file not found. */
852 file->version = (unsigned)statbuf->st_mtime;
854 /* Correct file->version_str */
855 freez(file->version_str);
856 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
857 version_buf[sizeof(version_buf)-1] = '\0';
858 file->version_str = strdup(version_buf);
859 if (version_buf == NULL)
861 return JB_ERR_MEMORY;
868 /*********************************************************************
870 * Function : edit_free_file
872 * Description : Free a complete file in memory.
875 * 1 : file = Data structure to free.
879 *********************************************************************/
880 void edit_free_file(struct editable_file * file)
884 /* Silently ignore NULL pointer */
888 edit_free_file_lines(file->lines);
889 freez(file->version_str);
891 file->parse_error_text = NULL; /* Statically allocated */
892 file->parse_error = NULL;
898 /*********************************************************************
900 * Function : edit_free_file_lines
902 * Description : Free an entire linked list of file lines.
905 * 1 : first_line = Data structure to free.
909 *********************************************************************/
910 static void edit_free_file_lines(struct file_line * first_line)
912 struct file_line * next_line;
914 while (first_line != NULL)
916 next_line = first_line->next;
917 first_line->next = NULL;
918 freez(first_line->raw);
919 freez(first_line->prefix);
920 freez(first_line->unprocessed);
921 switch(first_line->type)
923 case 0: /* special case if memory zeroed */
924 case FILE_LINE_UNPROCESSED:
925 case FILE_LINE_BLANK:
926 case FILE_LINE_ALIAS_HEADER:
927 case FILE_LINE_SETTINGS_HEADER:
928 case FILE_LINE_DESCRIPTION_HEADER:
929 case FILE_LINE_DESCRIPTION_ENTRY:
930 case FILE_LINE_ALIAS_ENTRY:
932 /* No data is stored for these */
935 case FILE_LINE_ACTION:
936 free_action(first_line->data.action);
939 case FILE_LINE_SETTINGS_ENTRY:
940 freez(first_line->data.setting.name);
941 freez(first_line->data.setting.svalue);
944 /* Should never happen */
948 first_line->type = 0; /* paranoia */
950 first_line = next_line;
955 /*********************************************************************
957 * Function : match_actions_file_header_line
959 * Description : Match an actions file {{header}} line
962 * 1 : line = String from file
963 * 2 : name = Header to match against
965 * Returns : 0 iff they match.
967 *********************************************************************/
968 static int match_actions_file_header_line(const char * line, const char * name)
976 if ((line[0] != '{') || (line[1] != '{'))
982 /* Look for optional whitespace */
983 while ( (*line == ' ') || (*line == '\t') )
988 /* Look for the specified name (case-insensitive) */
990 if (0 != strncmpic(line, name, len))
996 /* Look for optional whitespace */
997 while ( (*line == ' ') || (*line == '\t') )
1002 /* Look for "}}" and end of string*/
1003 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1013 /*********************************************************************
1015 * Function : match_actions_file_header_line
1017 * Description : Match an actions file {{header}} line
1020 * 1 : line = String from file. Must not start with
1021 * whitespace (else infinite loop!)
1022 * 2 : pname = Destination for name
1023 * 2 : pvalue = Destination for value
1025 * Returns : JB_ERR_OK on success
1026 * JB_ERR_MEMORY on out-of-memory
1027 * JB_ERR_PARSE if there's no "=" sign, or if there's
1028 * nothing before the "=" sign (but empty
1029 * values *after* the "=" sign are legal).
1031 *********************************************************************/
1032 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1034 const char * name_end;
1035 const char * value_start;
1041 assert(*line != ' ');
1042 assert(*line != '\t');
1047 value_start = strchr(line, '=');
1048 if ((value_start == NULL) || (value_start == line))
1050 return JB_ERR_PARSE;
1053 name_end = value_start - 1;
1055 /* Eat any whitespace before the '=' */
1056 while ((*name_end == ' ') || (*name_end == '\t'))
1059 * we already know we must have at least 1 non-ws char
1060 * at start of buf - no need to check
1065 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1066 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1068 return JB_ERR_MEMORY;
1070 strncpy(*pname, line, name_len);
1071 (*pname)[name_len] = '\0';
1073 /* Eat any the whitespace after the '=' */
1075 while ((*value_start == ' ') || (*value_start == '\t'))
1080 if (NULL == (*pvalue = strdup(value_start)))
1084 return JB_ERR_MEMORY;
1091 /*********************************************************************
1093 * Function : edit_parse_actions_file
1095 * Description : Parse an actions file in memory.
1097 * Passed linked list must have the "data" member
1098 * zeroed, and must contain valid "next" and
1099 * "unprocessed" fields. The "raw" and "prefix"
1100 * fields are ignored, and "type" is just overwritten.
1102 * Note that on error the file may have been
1106 * 1 : file = Actions file to be parsed in-place.
1108 * Returns : JB_ERR_OK on success
1109 * JB_ERR_MEMORY on out-of-memory
1110 * JB_ERR_PARSE on error
1112 *********************************************************************/
1113 jb_err edit_parse_actions_file(struct editable_file * file)
1115 struct file_line * cur_line;
1117 const char * text; /* Text from a line */
1118 char * name; /* For lines of the form name=value */
1119 char * value; /* For lines of the form name=value */
1120 struct action_alias * alias_list = NULL;
1121 jb_err err = JB_ERR_OK;
1123 /* alias_list contains the aliases defined in this file.
1124 * It might be better to use the "file_line.data" fields
1125 * in the relavent places instead.
1128 cur_line = file->lines;
1130 /* A note about blank line support: Blank lines should only
1131 * ever occur as the last line in the file. This function
1132 * is more forgiving than that - FILE_LINE_BLANK can occur
1136 /* Skip leading blanks. Should only happen if file is
1137 * empty (which is valid, but pointless).
1139 while ( (cur_line != NULL)
1140 && (cur_line->unprocessed[0] == '\0') )
1143 cur_line->type = FILE_LINE_BLANK;
1144 cur_line = cur_line->next;
1147 if ( (cur_line != NULL)
1148 && (cur_line->unprocessed[0] != '{') )
1150 /* File doesn't start with a header */
1151 file->parse_error = cur_line;
1152 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1153 return JB_ERR_PARSE;
1156 if ( (cur_line != NULL) && (0 ==
1157 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1159 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1161 cur_line = cur_line->next;
1162 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1164 if (cur_line->unprocessed[0])
1166 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1168 err = split_line_on_equals(cur_line->unprocessed,
1169 &cur_line->data.setting.name,
1170 &cur_line->data.setting.svalue);
1171 if (err == JB_ERR_MEMORY)
1175 else if (err != JB_ERR_OK)
1177 /* Line does not contain a name=value pair */
1178 file->parse_error = cur_line;
1179 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1180 return JB_ERR_PARSE;
1185 cur_line->type = FILE_LINE_BLANK;
1187 cur_line = cur_line->next;
1191 if ( (cur_line != NULL) && (0 ==
1192 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1194 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1196 cur_line = cur_line->next;
1197 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1199 if (cur_line->unprocessed[0])
1201 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1205 cur_line->type = FILE_LINE_BLANK;
1207 cur_line = cur_line->next;
1211 if ( (cur_line != NULL) && (0 ==
1212 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1214 cur_line->type = FILE_LINE_ALIAS_HEADER;
1216 cur_line = cur_line->next;
1217 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1219 if (cur_line->unprocessed[0])
1221 /* define an alias */
1222 struct action_alias * new_alias;
1224 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1226 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1227 if (err == JB_ERR_MEMORY)
1231 else if (err != JB_ERR_OK)
1233 /* Line does not contain a name=value pair */
1234 file->parse_error = cur_line;
1235 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1236 return JB_ERR_PARSE;
1239 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1244 free_alias_list(alias_list);
1245 return JB_ERR_MEMORY;
1248 err = get_actions(value, alias_list, new_alias->action);
1251 /* Invalid action or out of memory */
1255 free_alias_list(alias_list);
1256 if (err == JB_ERR_MEMORY)
1262 /* Line does not contain a name=value pair */
1263 file->parse_error = cur_line;
1264 file->parse_error_text = "This alias does not specify a valid set of actions.";
1265 return JB_ERR_PARSE;
1271 new_alias->name = name;
1274 new_alias->next = alias_list;
1275 alias_list = new_alias;
1279 cur_line->type = FILE_LINE_BLANK;
1281 cur_line = cur_line->next;
1285 /* Header done, process the main part of the file */
1286 while (cur_line != NULL)
1288 /* At this point, (cur_line->unprocessed[0] == '{') */
1289 assert(cur_line->unprocessed[0] == '{');
1290 text = cur_line->unprocessed + 1;
1291 len = strlen(text) - 1;
1292 if (text[len] != '}')
1294 /* No closing } on header */
1295 free_alias_list(alias_list);
1296 file->parse_error = cur_line;
1297 file->parse_error_text = "Headers starting with '{' must have a "
1298 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1299 "must close with two brackets ('}}').";
1300 return JB_ERR_PARSE;
1305 /* An invalid {{ header. */
1306 free_alias_list(alias_list);
1307 file->parse_error = cur_line;
1308 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1309 "Please remember that the system (two-bracket) headers must "
1310 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1311 "and must appear before any actions (one-bracket) headers. "
1312 "Also note that system headers may not be repeated.";
1313 return JB_ERR_PARSE;
1316 while ( (*text == ' ') || (*text == '\t') )
1321 while ( (len > (size_t)0)
1322 && ( (text[len - 1] == ' ')
1323 || (text[len - 1] == '\t') ) )
1328 cur_line->type = FILE_LINE_ACTION;
1330 /* Remove {} and make copy */
1331 if (NULL == (value = (char *) malloc(len + 1)))
1334 free_alias_list(alias_list);
1335 return JB_ERR_MEMORY;
1337 strncpy(value, text, len);
1341 err = get_actions(value, alias_list, cur_line->data.action);
1344 /* Invalid action or out of memory */
1346 free_alias_list(alias_list);
1347 if (err == JB_ERR_MEMORY)
1353 /* Line does not contain a name=value pair */
1354 file->parse_error = cur_line;
1355 file->parse_error_text = "This header does not specify a valid set of actions.";
1356 return JB_ERR_PARSE;
1360 /* Done with string - it was clobbered anyway */
1363 /* Process next line */
1364 cur_line = cur_line->next;
1366 /* Loop processing URL patterns */
1367 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1369 if (cur_line->unprocessed[0])
1371 /* Could parse URL here, but this isn't currently needed */
1373 cur_line->type = FILE_LINE_URL;
1377 cur_line->type = FILE_LINE_BLANK;
1379 cur_line = cur_line->next;
1381 } /* End main while(cur_line != NULL) loop */
1383 free_alias_list(alias_list);
1389 /*********************************************************************
1391 * Function : edit_read_file_lines
1393 * Description : Read all the lines of a file into memory.
1394 * Handles whitespace, comments and line continuation.
1397 * 1 : fp = File to read from. On return, this will be
1398 * at EOF but it will not have been closed.
1399 * 2 : pfile = Destination for a linked list of file_lines.
1400 * Will be set to NULL on error.
1401 * 3 : newline = How to handle newlines.
1403 * Returns : JB_ERR_OK on success
1404 * JB_ERR_MEMORY on out-of-memory
1406 *********************************************************************/
1407 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1409 struct file_line * first_line; /* Keep for return value or to free */
1410 struct file_line * cur_line; /* Current line */
1411 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1419 cur_line = first_line = zalloc(sizeof(struct file_line));
1420 if (cur_line == NULL)
1422 return JB_ERR_MEMORY;
1425 cur_line->type = FILE_LINE_UNPROCESSED;
1427 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1430 /* Out of memory or empty file. */
1431 /* Note that empty file is not an error we propagate up */
1433 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1438 prev_line = cur_line;
1439 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1440 if (cur_line == NULL)
1443 edit_free_file_lines(first_line);
1444 return JB_ERR_MEMORY;
1447 cur_line->type = FILE_LINE_UNPROCESSED;
1449 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1450 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1453 edit_free_file_lines(first_line);
1454 return JB_ERR_MEMORY;
1458 while (rval != JB_ERR_FILE);
1462 /* We allocated one too many - free it */
1463 prev_line->next = NULL;
1466 *pfile = first_line;
1471 /*********************************************************************
1473 * Function : edit_read_file
1475 * Description : Read a complete file into memory.
1476 * Handles CGI parameter parsing. If requested, also
1477 * checks the file's modification timestamp.
1480 * 1 : csp = Current client state (buffers, headers, etc...)
1481 * 2 : parameters = map of cgi parameters.
1482 * 3 : require_version = true to check "ver" parameter.
1483 * 4 : pfile = Destination for the file. Will be set
1487 * f : The action file identifier.
1488 * ver : (Only if require_version is nonzero)
1489 * Timestamp of the actions file. If wrong, this
1490 * function fails with JB_ERR_MODIFIED.
1492 * Returns : JB_ERR_OK on success
1493 * JB_ERR_MEMORY on out-of-memory
1494 * JB_ERR_CGI_PARAMS if "filename" was not specified
1496 * JB_ERR_FILE if the file cannot be opened or
1498 * JB_ERR_MODIFIED if version checking was requested and
1499 * failed - the file was modified outside
1500 * of this CGI editor instance.
1502 *********************************************************************/
1503 jb_err edit_read_file(struct client_state *csp,
1504 const struct map *parameters,
1505 int require_version,
1506 struct editable_file **pfile)
1508 struct file_line * lines;
1511 const char *filename = NULL;
1512 struct editable_file * file;
1513 unsigned version = 0;
1514 struct stat statbuf[1];
1515 char version_buf[22];
1516 int newline = NEWLINE_UNKNOWN;
1525 err = get_number_param(csp, parameters, "f", &i);
1526 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1528 filename = csp->config->actions_file[i];
1530 else if (JB_ERR_CGI_PARAMS == err)
1533 * Probably an old-school URL like
1534 * http://config.privoxy.org/edit-actions-list?f=default
1536 get_file_name_param(csp, parameters, "f", &filename);
1539 if (NULL == filename || stat(filename, statbuf) < 0)
1541 /* Error, probably file not found. */
1544 version = (unsigned) statbuf->st_mtime;
1546 if (require_version)
1548 unsigned specified_version;
1549 err = get_number_param(csp, parameters, "v", &specified_version);
1555 if (version != specified_version)
1557 return JB_ERR_MODIFIED;
1561 if (NULL == (fp = fopen(filename,"rb")))
1566 err = edit_read_file_lines(fp, &lines, &newline);
1575 file = (struct editable_file *) zalloc(sizeof(*file));
1578 edit_free_file_lines(lines);
1582 file->lines = lines;
1583 file->newline = newline;
1584 file->filename = filename;
1585 file->version = version;
1586 file->identifier = i;
1588 /* Correct file->version_str */
1589 freez(file->version_str);
1590 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1591 version_buf[sizeof(version_buf)-1] = '\0';
1592 file->version_str = strdup(version_buf);
1593 if (version_buf == NULL)
1595 edit_free_file(file);
1596 return JB_ERR_MEMORY;
1604 /*********************************************************************
1606 * Function : edit_read_actions_file
1608 * Description : Read a complete actions file into memory.
1609 * Handles CGI parameter parsing. If requested, also
1610 * checks the file's modification timestamp.
1612 * If this function detects an error in the categories
1613 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1614 * then it handles it by filling in the specified
1615 * response structure and returning JB_ERR_FILE.
1618 * 1 : csp = Current client state (buffers, headers, etc...)
1619 * 2 : rsp = HTTP response. Only filled in on error.
1620 * 2 : parameters = map of cgi parameters.
1621 * 3 : require_version = true to check "ver" parameter.
1622 * 4 : pfile = Destination for the file. Will be set
1626 * f : The actions file identifier.
1627 * ver : (Only if require_version is nonzero)
1628 * Timestamp of the actions file. If wrong, this
1629 * function fails with JB_ERR_MODIFIED.
1631 * Returns : JB_ERR_OK on success
1632 * JB_ERR_MEMORY on out-of-memory
1633 * JB_ERR_CGI_PARAMS if "filename" was not specified
1635 * JB_ERR_FILE if the file does not contain valid data,
1636 * or if file cannot be opened or
1637 * contains no data, or if version
1638 * checking was requested and failed.
1640 *********************************************************************/
1641 jb_err edit_read_actions_file(struct client_state *csp,
1642 struct http_response *rsp,
1643 const struct map *parameters,
1644 int require_version,
1645 struct editable_file **pfile)
1648 struct editable_file *file;
1649 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
1657 err = edit_read_file(csp, parameters, require_version, &file);
1660 /* Try to handle if possible */
1661 if (err == JB_ERR_FILE)
1663 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1665 else if (err == JB_ERR_MODIFIED)
1667 assert(require_version);
1668 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1669 log_error(LOG_LEVEL_ERROR,
1670 "Blocking CGI edit request due to modification time mismatch.");
1671 if (acceptable_failures > 0)
1673 log_error(LOG_LEVEL_INFO,
1674 "The CGI editor will be turned off after another %d mismatche(s).",
1675 acceptable_failures);
1676 acceptable_failures--;
1680 log_error(LOG_LEVEL_INFO,
1681 "Timestamp mismatch limit reached, turning CGI editor off. "
1682 "Reload the configuration file to re-enable it.");
1683 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
1686 if (err == JB_ERR_OK)
1689 * Signal to higher-level CGI code that there was a problem but we
1690 * handled it, they should just return JB_ERR_OK.
1697 err = edit_parse_actions_file(file);
1700 if (err == JB_ERR_PARSE)
1702 err = cgi_error_parse(csp, rsp, file);
1703 if (err == JB_ERR_OK)
1706 * Signal to higher-level CGI code that there was a problem but we
1707 * handled it, they should just return JB_ERR_OK.
1712 edit_free_file(file);
1721 /*********************************************************************
1723 * Function : get_file_name_param
1725 * Description : Get the name of the file to edit from the parameters
1726 * passed to a CGI function using the old syntax.
1727 * This function handles security checks and only
1728 * accepts files that Privoxy already knows.
1731 * 1 : csp = Current client state (buffers, headers, etc...)
1732 * 2 : parameters = map of cgi parameters
1733 * 3 : param_name = The name of the parameter to read
1734 * 4 : pfilename = pointer to the filename in
1735 * csp->config->actions_file[] if found. Set to NULL on error.
1737 * Returns : JB_ERR_OK on success
1738 * JB_ERR_MEMORY on out-of-memory
1739 * JB_ERR_CGI_PARAMS if "filename" was not specified
1742 *********************************************************************/
1743 static jb_err get_file_name_param(struct client_state *csp,
1744 const struct map *parameters,
1745 const char *param_name,
1746 const char **pfilename)
1749 const char suffix[] = ".action";
1764 param = lookup(parameters, param_name);
1767 return JB_ERR_CGI_PARAMS;
1770 len = strlen(param);
1771 if (len >= FILENAME_MAX)
1774 return JB_ERR_CGI_PARAMS;
1778 * Check every character to see if it's legal.
1779 * Totally unnecessary but we do it anyway.
1782 while ((ch = *s++) != '\0')
1784 if ( ((ch < 'A') || (ch > 'Z'))
1785 && ((ch < 'a') || (ch > 'z'))
1786 && ((ch < '0') || (ch > '9'))
1790 /* Probable hack attempt. */
1791 return JB_ERR_CGI_PARAMS;
1795 /* Append extension */
1796 name_size = len + strlen(suffix) + 1;
1797 name = malloc(name_size);
1800 return JB_ERR_MEMORY;
1802 strlcpy(name, param, name_size);
1803 strlcat(name, suffix, name_size);
1806 fullpath = make_path(csp->config->confdir, name);
1809 if (fullpath == NULL)
1811 return JB_ERR_MEMORY;
1814 /* Check if the file is known */
1815 for (i = 0; i < MAX_AF_FILES; i++)
1817 if (NULL != csp->config->actions_file[i] &&
1818 !strcmp(fullpath, csp->config->actions_file[i]))
1821 *pfilename = csp->config->actions_file[i];
1829 return JB_ERR_CGI_PARAMS;
1833 /*********************************************************************
1835 * Function : get_url_spec_param
1837 * Description : Get a URL pattern from the parameters
1838 * passed to a CGI function. Removes leading/trailing
1839 * spaces and validates it.
1842 * 1 : csp = Current client state (buffers, headers, etc...)
1843 * 2 : parameters = map of cgi parameters
1844 * 3 : name = Name of CGI parameter to read
1845 * 4 : pvalue = destination for value. Will be malloc()'d.
1846 * Set to NULL on error.
1848 * Returns : JB_ERR_OK on success
1849 * JB_ERR_MEMORY on out-of-memory
1850 * JB_ERR_CGI_PARAMS if the parameter was not specified
1853 *********************************************************************/
1854 static jb_err get_url_spec_param(struct client_state *csp,
1855 const struct map *parameters,
1859 const char *orig_param;
1862 struct url_spec compiled[1];
1872 orig_param = lookup(parameters, name);
1875 return JB_ERR_CGI_PARAMS;
1878 /* Copy and trim whitespace */
1879 param = strdup(orig_param);
1882 return JB_ERR_MEMORY;
1886 /* Must be non-empty, and can't allow 1st character to be '{' */
1887 if (param[0] == '\0' || param[0] == '{')
1890 return JB_ERR_CGI_PARAMS;
1893 /* Check for embedded newlines */
1894 for (s = param; *s != '\0'; s++)
1896 if ((*s == '\r') || (*s == '\n'))
1899 return JB_ERR_CGI_PARAMS;
1903 /* Check that regex is valid */
1908 return JB_ERR_MEMORY;
1910 err = create_url_spec(compiled, s);
1915 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1917 free_url_spec(compiled);
1919 if (param[strlen(param) - 1] == '\\')
1922 * Must protect trailing '\\' from becoming line continuation character.
1923 * Two methods: 1) If it's a domain only, add a trailing '/'.
1924 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1926 if (strchr(param, '/') == NULL)
1928 err = string_append(¶m, "/");
1932 err = string_append(¶m, "(?:)");
1939 /* Check that the modified regex is valid */
1944 return JB_ERR_MEMORY;
1946 err = create_url_spec(compiled, s);
1951 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1953 free_url_spec(compiled);
1960 /*********************************************************************
1962 * Function : map_radio
1964 * Description : Map a set of radio button values. E.g. if you have
1965 * 3 radio buttons, declare them as:
1966 * <option type="radio" name="xyz" @xyz-a@>
1967 * <option type="radio" name="xyz" @xyz-b@>
1968 * <option type="radio" name="xyz" @xyz-c@>
1969 * Then map one of the @xyz-?@ variables to "checked"
1970 * and all the others to empty by calling:
1971 * map_radio(exports, "xyz", "abc", sel)
1972 * Where 'sel' is 'a', 'b', or 'c'.
1975 * 1 : exports = Exports map to modify.
1976 * 2 : optionname = name for map
1977 * 3 : values = null-terminated list of values;
1978 * 4 : value = Selected value.
1980 * CGI Parameters : None
1982 * Returns : JB_ERR_OK on success
1983 * JB_ERR_MEMORY on out-of-memory
1985 *********************************************************************/
1986 static jb_err map_radio(struct map * exports,
1987 const char * optionname,
1988 const char * values,
1994 const size_t len = strlen(optionname);
1995 const size_t buf_size = len + 3;
2001 buf = malloc(buf_size);
2004 return JB_ERR_MEMORY;
2007 strlcpy(buf, optionname, buf_size);
2009 /* XXX: this looks ... interesting */
2014 while ((c = *values++) != '\0')
2019 if (map(exports, buf, 1, "", 1))
2021 return JB_ERR_MEMORY;
2027 return map(exports, buf, 0, "checked", 1);
2031 /*********************************************************************
2033 * Function : cgi_error_modified
2035 * Description : CGI function that is called when a file is modified
2036 * outside the CGI editor.
2039 * 1 : csp = Current client state (buffers, headers, etc...)
2040 * 2 : rsp = http_response data structure for output
2041 * 3 : filename = The file that was modified.
2043 * CGI Parameters : none
2045 * Returns : JB_ERR_OK on success
2046 * JB_ERR_MEMORY on out-of-memory error.
2048 *********************************************************************/
2049 jb_err cgi_error_modified(struct client_state *csp,
2050 struct http_response *rsp,
2051 const char *filename)
2053 struct map *exports;
2060 if (NULL == (exports = default_exports(csp, NULL)))
2062 return JB_ERR_MEMORY;
2065 err = map(exports, "f", 1, html_encode(filename), 0);
2072 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2076 /*********************************************************************
2078 * Function : cgi_error_parse
2080 * Description : CGI function that is called when a file cannot
2081 * be parsed by the CGI editor.
2084 * 1 : csp = Current client state (buffers, headers, etc...)
2085 * 2 : rsp = http_response data structure for output
2086 * 3 : file = The file that was modified.
2088 * CGI Parameters : none
2090 * Returns : JB_ERR_OK on success
2091 * JB_ERR_MEMORY on out-of-memory error.
2093 *********************************************************************/
2094 jb_err cgi_error_parse(struct client_state *csp,
2095 struct http_response *rsp,
2096 struct editable_file *file)
2098 struct map *exports;
2100 struct file_line *cur_line;
2106 if (NULL == (exports = default_exports(csp, NULL)))
2108 return JB_ERR_MEMORY;
2111 err = map(exports, "f", 1, stringify(file->identifier), 0);
2112 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2114 cur_line = file->parse_error;
2117 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2118 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2126 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2130 /*********************************************************************
2132 * Function : cgi_error_file
2134 * Description : CGI function that is called when a file cannot be
2135 * opened by the CGI editor.
2138 * 1 : csp = Current client state (buffers, headers, etc...)
2139 * 2 : rsp = http_response data structure for output
2140 * 3 : filename = The file that was modified.
2142 * CGI Parameters : none
2144 * Returns : JB_ERR_OK on success
2145 * JB_ERR_MEMORY on out-of-memory error.
2147 *********************************************************************/
2148 jb_err cgi_error_file(struct client_state *csp,
2149 struct http_response *rsp,
2150 const char *filename)
2152 struct map *exports;
2159 if (NULL == (exports = default_exports(csp, NULL)))
2161 return JB_ERR_MEMORY;
2164 err = map(exports, "f", 1, html_encode(filename), 0);
2171 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2175 /*********************************************************************
2177 * Function : cgi_error_file_read_only
2179 * Description : CGI function that is called when a file cannot be
2180 * opened for writing by the CGI editor.
2183 * 1 : csp = Current client state (buffers, headers, etc...)
2184 * 2 : rsp = http_response data structure for output
2185 * 3 : filename = The file that we can't write to
2187 * CGI Parameters : none
2189 * Returns : JB_ERR_OK on success
2190 * JB_ERR_MEMORY on out-of-memory error.
2192 *********************************************************************/
2193 jb_err cgi_error_file_read_only(struct client_state *csp,
2194 struct http_response *rsp,
2195 const char *filename)
2197 struct map *exports;
2204 if (NULL == (exports = default_exports(csp, NULL)))
2206 return JB_ERR_MEMORY;
2209 err = map(exports, "f", 1, html_encode(filename), 0);
2216 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2220 /*********************************************************************
2222 * Function : cgi_edit_actions
2224 * Description : CGI function that allows the user to choose which
2225 * actions file to edit.
2228 * 1 : csp = Current client state (buffers, headers, etc...)
2229 * 2 : rsp = http_response data structure for output
2230 * 3 : parameters = map of cgi parameters
2232 * CGI Parameters : None
2234 * Returns : JB_ERR_OK on success
2235 * JB_ERR_MEMORY on out-of-memory error
2237 *********************************************************************/
2238 jb_err cgi_edit_actions(struct client_state *csp,
2239 struct http_response *rsp,
2240 const struct map *parameters)
2244 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2246 return cgi_error_disabled(csp, rsp);
2249 /* FIXME: Incomplete */
2251 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2256 /*********************************************************************
2258 * Function : cgi_edit_actions_list
2260 * Description : CGI function that edits the actions list.
2261 * FIXME: This function shouldn't FATAL ever.
2262 * FIXME: This function doesn't check the retval of map()
2264 * 1 : csp = Current client state (buffers, headers, etc...)
2265 * 2 : rsp = http_response data structure for output
2266 * 3 : parameters = map of cgi parameters
2268 * CGI Parameters : filename
2270 * Returns : JB_ERR_OK on success
2271 * JB_ERR_MEMORY on out-of-memory
2272 * JB_ERR_FILE if the file cannot be opened or
2274 * JB_ERR_CGI_PARAMS if "filename" was not specified
2277 *********************************************************************/
2278 jb_err cgi_edit_actions_list(struct client_state *csp,
2279 struct http_response *rsp,
2280 const struct map *parameters)
2282 char * section_template;
2283 char * url_template;
2288 struct map * exports;
2289 struct map * section_exports;
2290 struct map * url_exports;
2291 struct editable_file * file;
2292 struct file_line * cur_line;
2293 unsigned line_number = 0;
2294 unsigned prev_section_line_number = ((unsigned) (-1));
2296 struct file_list * fl;
2297 struct url_actions * b;
2298 char * buttons = NULL;
2301 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2303 return cgi_error_disabled(csp, rsp);
2306 if (NULL == (exports = default_exports(csp, NULL)))
2308 return JB_ERR_MEMORY;
2311 /* Load actions file */
2312 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2315 /* No filename specified, can't read file, or out of memory. */
2317 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2320 /* Find start of actions in file */
2321 cur_line = file->lines;
2323 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2325 cur_line = cur_line->next;
2330 * Conventional actions files should have a match all block
2332 * cur_line = {...global actions...}
2333 * cur_line->next = /
2334 * cur_line->next->next = {...actions...} or EOF
2336 if ( (cur_line != NULL)
2337 && (cur_line->type == FILE_LINE_ACTION)
2338 && (cur_line->next != NULL)
2339 && (cur_line->next->type == FILE_LINE_URL)
2340 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2341 && ( (cur_line->next->next == NULL)
2342 || (cur_line->next->next->type != FILE_LINE_URL)
2346 * Generate string with buttons to set actions for "/" to
2347 * any predefined set of actions (named standard.*, probably
2348 * residing in standard.action).
2351 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2354 edit_free_file(file);
2356 if (err == JB_ERR_FILE)
2358 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2363 err = template_fill(§ion_template, exports);
2366 edit_free_file(file);
2371 buttons = strdup("");
2372 for (i = 0; i < MAX_AF_FILES; i++)
2374 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2376 for (b = b->next; NULL != b; b = b->next)
2378 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2380 if (err || (NULL == (section_exports = new_map())))
2383 free(section_template);
2384 edit_free_file(file);
2386 return JB_ERR_MEMORY;
2389 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2391 if (err || (NULL == (s = strdup(section_template))))
2393 free_map(section_exports);
2395 free(section_template);
2396 edit_free_file(file);
2398 return JB_ERR_MEMORY;
2401 if (!err) err = template_fill(&s, section_exports);
2402 free_map(section_exports);
2403 if (!err) err = string_join(&buttons, s);
2408 freez(section_template);
2409 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2412 * Conventional actions file, supply extra editing help.
2413 * (e.g. don't allow them to make it an unconventional one).
2415 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2417 snprintf(buf, sizeof(buf), "%d", line_number);
2418 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2419 snprintf(buf, sizeof(buf), "%d", line_number + 2);
2420 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2421 if (!err) err = map(exports, "all-urls-actions", 1,
2422 actions_to_html(csp, cur_line->data.action), 0);
2424 /* Skip the 2 lines */
2425 cur_line = cur_line->next->next;
2429 * Note that prev_section_line_number is NOT set here.
2430 * This is deliberate and not a bug. It stops a "Move up"
2431 * option appearing on the next section. Clicking "Move
2432 * up" would make the actions file unconventional, which
2433 * we don't want, so we hide this option.
2439 * Non-standard actions file - does not begin with
2440 * the "All URLs" section.
2442 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2445 /* Set up global exports */
2447 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2448 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2449 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2451 /* Discourage private additions to default.action */
2453 if (!err) err = map_conditional(exports, "default-action",
2454 (strstr("default.action", file->filename) != NULL));
2457 edit_free_file(file);
2462 /* Should do all global exports above this point */
2464 /* Load templates */
2466 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2469 edit_free_file(file);
2471 if (err == JB_ERR_FILE)
2473 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2478 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2481 free(section_template);
2482 edit_free_file(file);
2484 if (err == JB_ERR_FILE)
2486 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2491 err = template_fill(§ion_template, exports);
2495 edit_free_file(file);
2500 err = template_fill(&url_template, exports);
2503 free(section_template);
2504 edit_free_file(file);
2509 if (NULL == (sections = strdup("")))
2511 free(section_template);
2513 edit_free_file(file);
2515 return JB_ERR_MEMORY;
2518 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2520 if (NULL == (section_exports = new_map()))
2523 free(section_template);
2525 edit_free_file(file);
2527 return JB_ERR_MEMORY;
2530 snprintf(buf, sizeof(buf), "%d", line_number);
2531 err = map(section_exports, "s", 1, buf, 1);
2532 if (!err) err = map(section_exports, "actions", 1,
2533 actions_to_html(csp, cur_line->data.action), 0);
2536 && (cur_line->next != NULL)
2537 && (cur_line->next->type == FILE_LINE_URL))
2539 /* This section contains at least one URL, don't allow delete */
2540 err = map_block_killer(section_exports, "empty-section");
2544 if (!err) err = map_block_keep(section_exports, "empty-section");
2547 if (prev_section_line_number != ((unsigned)(-1)))
2549 /* Not last section */
2550 snprintf(buf, sizeof(buf), "%d", prev_section_line_number);
2551 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2552 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2557 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2559 prev_section_line_number = line_number;
2564 free(section_template);
2566 edit_free_file(file);
2568 free_map(section_exports);
2572 /* Should do all section-specific exports above this point */
2574 if (NULL == (urls = strdup("")))
2577 free(section_template);
2579 edit_free_file(file);
2581 free_map(section_exports);
2582 return JB_ERR_MEMORY;
2587 cur_line = cur_line->next;
2590 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2592 if (NULL == (url_exports = new_map()))
2596 free(section_template);
2598 edit_free_file(file);
2600 free_map(section_exports);
2601 return JB_ERR_MEMORY;
2604 snprintf(buf, sizeof(buf), "%d", line_number);
2605 err = map(url_exports, "p", 1, buf, 1);
2607 snprintf(buf, sizeof(buf), "%d", url_1_2);
2608 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2610 if (!err) err = map(url_exports, "url-html", 1,
2611 html_encode(cur_line->unprocessed), 0);
2612 if (!err) err = map(url_exports, "url", 1,
2613 url_encode(cur_line->unprocessed), 0);
2619 free(section_template);
2621 edit_free_file(file);
2623 free_map(section_exports);
2624 free_map(url_exports);
2628 if (NULL == (s = strdup(url_template)))
2632 free(section_template);
2634 edit_free_file(file);
2636 free_map(section_exports);
2637 free_map(url_exports);
2638 return JB_ERR_MEMORY;
2641 err = template_fill(&s, section_exports);
2642 if (!err) err = template_fill(&s, url_exports);
2643 if (!err) err = string_append(&urls, s);
2645 free_map(url_exports);
2652 free(section_template);
2654 edit_free_file(file);
2656 free_map(section_exports);
2660 url_1_2 = 3 - url_1_2;
2662 cur_line = cur_line->next;
2666 err = map(section_exports, "urls", 1, urls, 0);
2668 /* Could also do section-specific exports here, but it wouldn't be as fast */
2670 snprintf(buf, sizeof(buf), "%d", line_number);
2671 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2673 if ( (cur_line != NULL)
2674 && (cur_line->type == FILE_LINE_ACTION))
2676 /* Not last section */
2677 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2682 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2688 free(section_template);
2690 edit_free_file(file);
2692 free_map(section_exports);
2696 if (NULL == (s = strdup(section_template)))
2699 free(section_template);
2701 edit_free_file(file);
2703 free_map(section_exports);
2704 return JB_ERR_MEMORY;
2707 err = template_fill(&s, section_exports);
2708 if (!err) err = string_append(§ions, s);
2711 free_map(section_exports);
2716 free(section_template);
2718 edit_free_file(file);
2724 edit_free_file(file);
2725 free(section_template);
2728 err = map(exports, "sections", 1, sections, 0);
2735 /* Could also do global exports here, but it wouldn't be as fast */
2737 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2741 /*********************************************************************
2743 * Function : cgi_edit_actions_for_url
2745 * Description : CGI function that edits the Actions list.
2748 * 1 : csp = Current client state (buffers, headers, etc...)
2749 * 2 : rsp = http_response data structure for output
2750 * 3 : parameters = map of cgi parameters
2752 * CGI Parameters : None
2754 * Returns : JB_ERR_OK on success
2755 * JB_ERR_MEMORY on out-of-memory
2756 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2757 * specified or not valid.
2759 *********************************************************************/
2760 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2761 struct http_response *rsp,
2762 const struct map *parameters)
2764 struct map * exports;
2766 struct editable_file * file;
2767 struct file_line * cur_line;
2768 unsigned line_number;
2770 struct re_filterfile_spec *filter_group;
2771 int i, have_filters = 0;
2773 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2775 return cgi_error_disabled(csp, rsp);
2778 err = get_number_param(csp, parameters, "s", §ionid);
2784 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2787 /* No filename specified, can't read file, modified, or out of memory. */
2788 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2791 cur_line = file->lines;
2793 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2795 cur_line = cur_line->next;
2798 if ( (cur_line == NULL)
2799 || (line_number != sectionid)
2801 || (cur_line->type != FILE_LINE_ACTION))
2803 /* Invalid "sectionid" parameter */
2804 edit_free_file(file);
2805 return JB_ERR_CGI_PARAMS;
2808 if (NULL == (exports = default_exports(csp, NULL)))
2810 edit_free_file(file);
2811 return JB_ERR_MEMORY;
2814 err = map(exports, "f", 1, stringify(file->identifier), 0);
2815 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2816 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2818 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2821 * XXX: Some browsers (at least IE6 and IE7) have an artificial URL
2822 * length limitation and ignore clicks on the Submit buttons if
2823 * the resulting GET URL would be longer than their limit.
2825 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
2826 * reached this limit and action editing stopped working in these
2827 * browsers (BR #1570678).
2829 * The config option split-large-forms works around this browser
2830 * bug (HTTP has no URL length limitation) by deviding the action
2831 * list form into multiple smaller ones. It means the URLs are shorter
2832 * and work in broken browsers as well, but the user can no longer change
2833 * all actions with one submit.
2835 * A better solution would be to switch to POST requests,
2836 * but this will do for now.
2838 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
2840 /* Generate multiple smaller form by killing the big one. */
2841 err = map_block_killer(exports, "one-form-only");
2845 /* Default: Generate one large form by killing the smaller ones. */
2846 err = map_block_killer(exports, "multiple-forms");
2849 for (i = 0; i < MAX_AF_FILES; i++)
2851 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2853 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
2861 edit_free_file(file);
2866 if (0 == have_filters)
2868 err = map(exports, "filter-params", 1, "", 1);
2873 * List available filters and their settings.
2875 char *filter_template;
2876 int filter_identifier = 0;
2877 char *prepared_templates[MAX_FILTER_TYPES];
2879 for (i = 0; i < MAX_FILTER_TYPES; i++)
2881 prepared_templates[i] = strdup("");
2884 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2887 edit_free_file(file);
2889 if (err == JB_ERR_FILE)
2891 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2896 err = template_fill(&filter_template, exports);
2898 for (i = 0; i < MAX_AF_FILES; i++)
2900 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
2902 filter_group = csp->rlist[i]->f;
2903 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2905 char current_mode = 'x';
2907 struct list_entry *filter_name;
2908 struct map *line_exports;
2909 const int type = filter_group->type;
2910 const int multi_action_index = filter_type_info[type].multi_action_index;
2912 assert(type < MAX_FILTER_TYPES);
2914 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
2915 while ((filter_name != NULL)
2916 && (0 != strcmp(filter_group->name, filter_name->str)))
2918 filter_name = filter_name->next;
2921 if (filter_name != NULL)
2927 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
2928 while ((filter_name != NULL)
2929 && (0 != strcmp(filter_group->name, filter_name->str)))
2931 filter_name = filter_name->next;
2933 if (filter_name != NULL)
2939 /* Generate a unique serial number */
2940 snprintf(number, sizeof(number), "%x", filter_identifier++);
2941 number[sizeof(number) - 1] = '\0';
2943 line_exports = new_map();
2944 if (line_exports == NULL)
2946 err = JB_ERR_MEMORY;
2952 if (!err) err = map(line_exports, "index", 1, number, 1);
2953 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2954 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2955 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2956 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
2957 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
2958 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
2962 filter_line = strdup(filter_template);
2963 if (filter_line == NULL) err = JB_ERR_MEMORY;
2965 if (!err) err = template_fill(&filter_line, line_exports);
2966 string_join(&prepared_templates[type], filter_line);
2968 free_map(line_exports);
2973 freez(filter_template);
2975 /* Replace all filter macros with the aggregated templates */
2976 for (i = 0; i < MAX_FILTER_TYPES; i++)
2979 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
2984 /* Free aggregated templates */
2985 for (i = 0; i < MAX_FILTER_TYPES; i++)
2987 freez(prepared_templates[i]);
2992 /* Check or uncheck the "disable all of this type" radio buttons. */
2993 for (i = 0; i < MAX_FILTER_TYPES; i++)
2995 const int a = filter_type_info[i].multi_action_index;
2996 const int disable_all = cur_line->data.action->multi_remove_all[a];
2998 err = map_radio(exports, filter_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
3001 edit_free_file(file);
3009 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3013 /*********************************************************************
3015 * Function : cgi_edit_actions_submit
3017 * Description : CGI function that actually edits the Actions list.
3020 * 1 : csp = Current client state (buffers, headers, etc...)
3021 * 2 : rsp = http_response data structure for output
3022 * 3 : parameters = map of cgi parameters
3024 * CGI Parameters : None
3026 * Returns : JB_ERR_OK on success
3027 * JB_ERR_MEMORY on out-of-memory
3028 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3029 * specified or not valid.
3031 *********************************************************************/
3032 jb_err cgi_edit_actions_submit(struct client_state *csp,
3033 struct http_response *rsp,
3034 const struct map *parameters)
3039 size_t newtext_size;
3041 struct editable_file * file;
3042 struct file_line * cur_line;
3043 unsigned line_number;
3046 int filter_identifier;
3048 const char * action_set_name;
3049 struct file_list * fl;
3050 struct url_actions * b;
3052 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3054 return cgi_error_disabled(csp, rsp);
3057 err = get_number_param(csp, parameters, "s", §ionid);
3063 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3066 /* No filename specified, can't read file, modified, or out of memory. */
3067 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3070 cur_line = file->lines;
3072 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3074 cur_line = cur_line->next;
3077 if ( (cur_line == NULL)
3078 || (line_number != sectionid)
3080 || (cur_line->type != FILE_LINE_ACTION))
3082 /* Invalid "sectionid" parameter */
3083 edit_free_file(file);
3084 return JB_ERR_CGI_PARAMS;
3087 get_string_param(parameters, "p", &action_set_name);
3088 if (action_set_name != NULL)
3090 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3092 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3094 for (b = b->next; NULL != b; b = b->next)
3096 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3098 copy_action(cur_line->data.action, b->action);
3104 edit_free_file(file);
3105 return JB_ERR_CGI_PARAMS;
3111 err = actions_from_radio(parameters, cur_line->data.action);
3117 edit_free_file(file);
3121 /* Check the "disable all of this type" parameters. */
3122 for (i = 0; i < MAX_FILTER_TYPES; i++)
3124 const int multi_action_index = filter_type_info[i].multi_action_index;
3125 const char ch = get_char_param(parameters, filter_type_info[i].disable_all_param);
3129 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3130 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3131 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3135 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3139 for (filter_identifier = 0; !err; filter_identifier++)
3146 * Filter state. Valid states are: 'Y' (active),
3147 * 'N' (inactive) and 'X' (no change).
3151 * Abbreviated filter type. Valid types are: 'F' (content filter),
3152 * 'S' (server-header filter) and 'C' (client-header filter).
3154 int multi_action_index = 0;
3156 /* Generate the keys */
3157 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3158 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3159 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3160 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3161 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3163 err = get_string_param(parameters, key_name, &name);
3172 type = get_char_param(parameters, key_type);
3176 multi_action_index = ACTION_MULTI_FILTER;
3179 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3182 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3185 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3188 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3191 log_error(LOG_LEVEL_ERROR,
3192 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3195 assert(multi_action_index);
3197 value = get_char_param(parameters, key_value);
3200 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3201 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3202 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3204 else if (value == 'N')
3206 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3207 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3209 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3210 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3213 else if (value == 'X')
3215 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3216 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3223 edit_free_file(file);
3227 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3230 edit_free_file(file);
3231 return JB_ERR_MEMORY;
3234 len = strlen(actiontext);
3238 * Empty action - must special-case this.
3239 * Simply setting len to 1 is sufficient...
3244 newtext_size = len + 2;
3245 if (NULL == (newtext = malloc(newtext_size)))
3249 edit_free_file(file);
3250 return JB_ERR_MEMORY;
3252 strlcpy(newtext, actiontext, newtext_size);
3256 newtext[len + 1] = '\0';
3258 freez(cur_line->raw);
3259 freez(cur_line->unprocessed);
3260 cur_line->unprocessed = newtext;
3262 err = edit_write_file(file);
3265 /* Error writing file */
3266 if (err == JB_ERR_FILE)
3268 /* Read-only file. */
3269 err = cgi_error_file_read_only(csp, rsp, file->filename);
3271 edit_free_file(file);
3275 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3276 (long) time(NULL), file->identifier, sectionid);
3278 edit_free_file(file);
3280 return cgi_redirect(rsp, target);
3284 /*********************************************************************
3286 * Function : cgi_edit_actions_url
3288 * Description : CGI function that actually edits a URL pattern in
3292 * 1 : csp = Current client state (buffers, headers, etc...)
3293 * 2 : rsp = http_response data structure for output
3294 * 3 : parameters = map of cgi parameters
3297 * filename : Identifies the file to edit
3298 * ver : File's last-modified time
3299 * section : Line number of section to edit
3300 * pattern : Line number of pattern to edit
3301 * newval : New value for pattern
3303 * Returns : JB_ERR_OK on success
3304 * JB_ERR_MEMORY on out-of-memory
3305 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3306 * specified or not valid.
3308 *********************************************************************/
3309 jb_err cgi_edit_actions_url(struct client_state *csp,
3310 struct http_response *rsp,
3311 const struct map *parameters)
3315 struct editable_file * file;
3316 struct file_line * cur_line;
3317 unsigned line_number;
3318 unsigned section_start_line_number = 0;
3326 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3328 return cgi_error_disabled(csp, rsp);
3331 err = get_number_param(csp, parameters, "p", &patternid);
3338 return JB_ERR_CGI_PARAMS;
3341 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3347 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3350 /* No filename specified, can't read file, modified, or out of memory. */
3352 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3356 cur_line = file->lines;
3358 while ((cur_line != NULL) && (line_number < patternid))
3360 if (cur_line->type == FILE_LINE_ACTION)
3362 section_start_line_number = line_number;
3364 cur_line = cur_line->next;
3368 if ( (cur_line == NULL)
3369 || (cur_line->type != FILE_LINE_URL))
3371 /* Invalid "patternid" parameter */
3373 edit_free_file(file);
3374 return JB_ERR_CGI_PARAMS;
3377 /* At this point, the line to edit is in cur_line */
3379 freez(cur_line->raw);
3380 freez(cur_line->unprocessed);
3381 cur_line->unprocessed = new_pattern;
3383 err = edit_write_file(file);
3386 /* Error writing file */
3387 if (err == JB_ERR_FILE)
3389 /* Read-only file. */
3390 err = cgi_error_file_read_only(csp, rsp, file->filename);
3392 edit_free_file(file);
3396 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3397 (long) time(NULL), file->identifier, section_start_line_number);
3399 edit_free_file(file);
3401 return cgi_redirect(rsp, target);
3405 /*********************************************************************
3407 * Function : cgi_edit_actions_add_url
3409 * Description : CGI function that actually adds a URL pattern to
3413 * 1 : csp = Current client state (buffers, headers, etc...)
3414 * 2 : rsp = http_response data structure for output
3415 * 3 : parameters = map of cgi parameters
3418 * filename : Identifies the file to edit
3419 * ver : File's last-modified time
3420 * section : Line number of section to edit
3421 * newval : New pattern
3423 * Returns : JB_ERR_OK on success
3424 * JB_ERR_MEMORY on out-of-memory
3425 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3426 * specified or not valid.
3428 *********************************************************************/
3429 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3430 struct http_response *rsp,
3431 const struct map *parameters)
3435 struct file_line * new_line;
3436 struct editable_file * file;
3437 struct file_line * cur_line;
3438 unsigned line_number;
3442 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3444 return cgi_error_disabled(csp, rsp);
3447 err = get_number_param(csp, parameters, "s", §ionid);
3454 return JB_ERR_CGI_PARAMS;
3457 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3463 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3466 /* No filename specified, can't read file, modified, or out of memory. */
3468 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3472 cur_line = file->lines;
3474 while ((cur_line != NULL) && (line_number < sectionid))
3476 cur_line = cur_line->next;
3480 if ( (cur_line == NULL)
3481 || (cur_line->type != FILE_LINE_ACTION))
3483 /* Invalid "sectionid" parameter */
3485 edit_free_file(file);
3486 return JB_ERR_CGI_PARAMS;
3489 /* At this point, the section header is in cur_line - add after this. */
3491 /* Allocate the new line */
3492 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3493 if (new_line == NULL)
3496 edit_free_file(file);
3497 return JB_ERR_MEMORY;
3500 /* Fill in the data members of the new line */
3501 new_line->raw = NULL;
3502 new_line->prefix = NULL;
3503 new_line->unprocessed = new_pattern;
3504 new_line->type = FILE_LINE_URL;
3506 /* Link new_line into the list, after cur_line */
3507 new_line->next = cur_line->next;
3508 cur_line->next = new_line;
3510 /* Done making changes, now commit */
3512 err = edit_write_file(file);
3515 /* Error writing file */
3516 if (err == JB_ERR_FILE)
3518 /* Read-only file. */
3519 err = cgi_error_file_read_only(csp, rsp, file->filename);
3521 edit_free_file(file);
3525 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3526 (long) time(NULL), file->identifier, sectionid);
3528 edit_free_file(file);
3530 return cgi_redirect(rsp, target);
3534 /*********************************************************************
3536 * Function : cgi_edit_actions_remove_url
3538 * Description : CGI function that actually removes a URL pattern from
3542 * 1 : csp = Current client state (buffers, headers, etc...)
3543 * 2 : rsp = http_response data structure for output
3544 * 3 : parameters = map of cgi parameters
3547 * f : (filename) Identifies the file to edit
3548 * v : (version) File's last-modified time
3549 * p : (pattern) Line number of pattern to remove
3551 * Returns : JB_ERR_OK on success
3552 * JB_ERR_MEMORY on out-of-memory
3553 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3554 * specified or not valid.
3556 *********************************************************************/
3557 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3558 struct http_response *rsp,
3559 const struct map *parameters)
3562 struct editable_file * file;
3563 struct file_line * cur_line;
3564 struct file_line * prev_line;
3565 unsigned line_number;
3566 unsigned section_start_line_number = 0;
3570 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3572 return cgi_error_disabled(csp, rsp);
3575 err = get_number_param(csp, parameters, "p", &patternid);
3581 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3584 /* No filename specified, can't read file, modified, or out of memory. */
3585 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3590 cur_line = file->lines;
3592 while ((cur_line != NULL) && (line_number < patternid))
3594 if (cur_line->type == FILE_LINE_ACTION)
3596 section_start_line_number = line_number;
3598 prev_line = cur_line;
3599 cur_line = cur_line->next;
3603 if ( (cur_line == NULL)
3604 || (prev_line == NULL)
3605 || (cur_line->type != FILE_LINE_URL))
3607 /* Invalid "patternid" parameter */
3608 edit_free_file(file);
3609 return JB_ERR_CGI_PARAMS;
3612 /* At this point, the line to remove is in cur_line, and the previous
3613 * one is in prev_line
3616 /* Unlink cur_line */
3617 prev_line->next = cur_line->next;
3618 cur_line->next = NULL;
3621 edit_free_file_lines(cur_line);
3623 err = edit_write_file(file);
3626 /* Error writing file */
3627 if (err == JB_ERR_FILE)
3629 /* Read-only file. */
3630 err = cgi_error_file_read_only(csp, rsp, file->filename);
3632 edit_free_file(file);
3636 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3637 (long) time(NULL), file->identifier, section_start_line_number);
3639 edit_free_file(file);
3641 return cgi_redirect(rsp, target);
3645 /*********************************************************************
3647 * Function : cgi_edit_actions_section_remove
3649 * Description : CGI function that actually removes a whole section from
3650 * the actions file. The section must be empty first
3651 * (else JB_ERR_CGI_PARAMS).
3654 * 1 : csp = Current client state (buffers, headers, etc...)
3655 * 2 : rsp = http_response data structure for output
3656 * 3 : parameters = map of cgi parameters
3659 * f : (filename) Identifies the file to edit
3660 * v : (version) File's last-modified time
3661 * s : (section) Line number of section to edit
3663 * Returns : JB_ERR_OK on success
3664 * JB_ERR_MEMORY on out-of-memory
3665 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3666 * specified or not valid.
3668 *********************************************************************/
3669 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3670 struct http_response *rsp,
3671 const struct map *parameters)
3674 struct editable_file * file;
3675 struct file_line * cur_line;
3676 struct file_line * prev_line;
3677 unsigned line_number;
3681 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3683 return cgi_error_disabled(csp, rsp);
3686 err = get_number_param(csp, parameters, "s", §ionid);
3692 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3695 /* No filename specified, can't read file, modified, or out of memory. */
3696 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3700 cur_line = file->lines;
3703 while ((cur_line != NULL) && (line_number < sectionid))
3705 prev_line = cur_line;
3706 cur_line = cur_line->next;
3710 if ( (cur_line == NULL)
3711 || (cur_line->type != FILE_LINE_ACTION) )
3713 /* Invalid "sectionid" parameter */
3714 edit_free_file(file);
3715 return JB_ERR_CGI_PARAMS;
3718 if ( (cur_line->next != NULL)
3719 && (cur_line->next->type == FILE_LINE_URL) )
3721 /* Section not empty. */
3722 edit_free_file(file);
3723 return JB_ERR_CGI_PARAMS;
3726 /* At this point, the line to remove is in cur_line, and the previous
3727 * one is in prev_line
3730 /* Unlink cur_line */
3731 if (prev_line == NULL)
3733 /* Removing the first line from the file */
3734 file->lines = cur_line->next;
3738 prev_line->next = cur_line->next;
3740 cur_line->next = NULL;
3743 edit_free_file_lines(cur_line);
3745 err = edit_write_file(file);
3748 /* Error writing file */
3749 if (err == JB_ERR_FILE)
3751 /* Read-only file. */
3752 err = cgi_error_file_read_only(csp, rsp, file->filename);
3754 edit_free_file(file);
3758 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
3759 (long) time(NULL), file->identifier);
3761 edit_free_file(file);
3763 return cgi_redirect(rsp, target);
3767 /*********************************************************************
3769 * Function : cgi_edit_actions_section_add
3771 * Description : CGI function that adds a new empty section to
3775 * 1 : csp = Current client state (buffers, headers, etc...)
3776 * 2 : rsp = http_response data structure for output
3777 * 3 : parameters = map of cgi parameters
3780 * f : (filename) Identifies the file to edit
3781 * v : (version) File's last-modified time
3782 * s : (section) Line number of section to add after, 0 for
3785 * Returns : JB_ERR_OK on success
3786 * JB_ERR_MEMORY on out-of-memory
3787 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3788 * specified or not valid.
3790 *********************************************************************/
3791 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3792 struct http_response *rsp,
3793 const struct map *parameters)
3796 struct file_line * new_line;
3798 struct editable_file * file;
3799 struct file_line * cur_line;
3800 unsigned line_number;
3804 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3806 return cgi_error_disabled(csp, rsp);
3809 err = get_number_param(csp, parameters, "s", §ionid);
3815 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3818 /* No filename specified, can't read file, modified, or out of memory. */
3819 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3823 cur_line = file->lines;
3825 if (sectionid <= 1U)
3827 /* Add to start of file */
3828 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
3830 /* There's something in the file, find the line before the first
3833 while ( (cur_line->next != NULL)
3834 && (cur_line->next->type != FILE_LINE_ACTION) )
3836 cur_line = cur_line->next;
3842 /* File starts with action line, so insert at top */
3848 /* Add after stated section. */
3849 while ((cur_line != NULL) && (line_number < sectionid))
3851 cur_line = cur_line->next;
3855 if ( (cur_line == NULL)
3856 || (cur_line->type != FILE_LINE_ACTION))
3858 /* Invalid "sectionid" parameter */
3859 edit_free_file(file);
3860 return JB_ERR_CGI_PARAMS;
3863 /* Skip through the section to find the last line in it. */
3864 while ( (cur_line->next != NULL)
3865 && (cur_line->next->type != FILE_LINE_ACTION) )
3867 cur_line = cur_line->next;
3872 /* At this point, the last line in the previous section is in cur_line
3873 * - add after this. (Or if we need to add as the first line, cur_line
3877 new_text = strdup("{}");
3878 if (NULL == new_text)
3880 edit_free_file(file);
3881 return JB_ERR_MEMORY;
3884 /* Allocate the new line */
3885 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3886 if (new_line == NULL)
3889 edit_free_file(file);
3890 return JB_ERR_MEMORY;
3893 /* Fill in the data members of the new line */
3894 new_line->raw = NULL;
3895 new_line->prefix = NULL;
3896 new_line->unprocessed = new_text;
3897 new_line->type = FILE_LINE_ACTION;
3899 if (cur_line != NULL)
3901 /* Link new_line into the list, after cur_line */
3902 new_line->next = cur_line->next;
3903 cur_line->next = new_line;
3907 /* Link new_line into the list, as first line */
3908 new_line->next = file->lines;
3909 file->lines = new_line;
3912 /* Done making changes, now commit */
3914 err = edit_write_file(file);
3917 /* Error writing file */
3918 if (err == JB_ERR_FILE)
3920 /* Read-only file. */
3921 err = cgi_error_file_read_only(csp, rsp, file->filename);
3923 edit_free_file(file);
3927 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
3928 (long) time(NULL), file->identifier);
3930 edit_free_file(file);
3932 return cgi_redirect(rsp, target);
3936 /*********************************************************************
3938 * Function : cgi_edit_actions_section_swap
3940 * Description : CGI function that swaps the order of two sections
3941 * in the actions file. Note that this CGI can actually
3942 * swap any two arbitrary sections, but the GUI interface
3943 * currently only allows consecutive sections to be
3947 * 1 : csp = Current client state (buffers, headers, etc...)
3948 * 2 : rsp = http_response data structure for output
3949 * 3 : parameters = map of cgi parameters
3952 * f : (filename) Identifies the file to edit
3953 * v : (version) File's last-modified time
3954 * s1 : (section1) Line number of first section to swap
3955 * s2 : (section2) Line number of second section to swap
3957 * Returns : JB_ERR_OK on success
3958 * JB_ERR_MEMORY on out-of-memory
3959 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3960 * specified or not valid.
3962 *********************************************************************/
3963 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3964 struct http_response *rsp,
3965 const struct map *parameters)
3969 struct editable_file * file;
3970 struct file_line * cur_line;
3971 struct file_line * prev_line;
3972 struct file_line * line_before_section1;
3973 struct file_line * line_start_section1;
3974 struct file_line * line_end_section1;
3975 struct file_line * line_after_section1;
3976 struct file_line * line_before_section2;
3977 struct file_line * line_start_section2;
3978 struct file_line * line_end_section2;
3979 struct file_line * line_after_section2;
3980 unsigned line_number;
3984 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3986 return cgi_error_disabled(csp, rsp);
3989 err = get_number_param(csp, parameters, "s1", §ion1);
3990 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3996 if (section1 > section2)
3998 unsigned temp = section2;
3999 section2 = section1;
4003 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4006 /* No filename specified, can't read file, modified, or out of memory. */
4007 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4010 /* Start at the beginning... */
4012 cur_line = file->lines;
4015 /* ... find section1 ... */
4016 while ((cur_line != NULL) && (line_number < section1))
4018 prev_line = cur_line;
4019 cur_line = cur_line->next;
4023 if ( (cur_line == NULL)
4024 || (cur_line->type != FILE_LINE_ACTION) )
4026 /* Invalid "section1" parameter */
4027 edit_free_file(file);
4028 return JB_ERR_CGI_PARAMS;
4031 /* If no-op, we've validated params and can skip the rest. */
4032 if (section1 != section2)
4034 /* ... find the end of section1 ... */
4035 line_before_section1 = prev_line;
4036 line_start_section1 = cur_line;
4039 prev_line = cur_line;
4040 cur_line = cur_line->next;
4043 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4044 line_end_section1 = prev_line;
4045 line_after_section1 = cur_line;
4047 /* ... find section2 ... */
4048 while ((cur_line != NULL) && (line_number < section2))
4050 prev_line = cur_line;
4051 cur_line = cur_line->next;
4055 if ( (cur_line == NULL)
4056 || (cur_line->type != FILE_LINE_ACTION) )
4058 /* Invalid "section2" parameter */
4059 edit_free_file(file);
4060 return JB_ERR_CGI_PARAMS;
4063 /* ... find the end of section2 ... */
4064 line_before_section2 = prev_line;
4065 line_start_section2 = cur_line;
4068 prev_line = cur_line;
4069 cur_line = cur_line->next;
4072 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4073 line_end_section2 = prev_line;
4074 line_after_section2 = cur_line;
4076 /* Now have all the pointers we need. Do the swap. */
4078 /* Change the pointer to section1 to point to section2 instead */
4079 if (line_before_section1 == NULL)
4081 file->lines = line_start_section2;
4085 line_before_section1->next = line_start_section2;
4088 if (line_before_section2 == line_end_section1)
4090 /* Consecutive sections */
4091 line_end_section2->next = line_start_section1;
4095 line_end_section2->next = line_after_section1;
4096 line_before_section2->next = line_start_section1;
4099 /* Set the pointer from the end of section1 to the rest of the file */
4100 line_end_section1->next = line_after_section2;
4102 err = edit_write_file(file);
4105 /* Error writing file */
4106 if (err == JB_ERR_FILE)
4108 /* Read-only file. */
4109 err = cgi_error_file_read_only(csp, rsp, file->filename);
4111 edit_free_file(file);
4114 } /* END if (section1 != section2) */
4116 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4117 (long) time(NULL), file->identifier);
4119 edit_free_file(file);
4121 return cgi_redirect(rsp, target);
4125 /*********************************************************************
4127 * Function : javascriptify
4129 * Description : Converts a string into a form JavaScript will like.
4131 * Netscape 4's JavaScript sucks - it doesn't use
4132 * "id" parameters, so you have to set the "name"
4133 * used to submit a form element to something JavaScript
4134 * will like. (Or access the elements by index in an
4135 * array. That array contains >60 elements and will
4136 * be changed whenever we add a new action to the
4137 * editor, so I'm NOT going to use indexes that have
4138 * to be figured out by hand.)
4140 * Currently the only thing we have to worry about
4141 * is "-" ==> "_" conversion.
4143 * This is a length-preserving operation so it is
4144 * carried out in-place, no memory is allocated
4148 * 1 : identifier = String to make JavaScript-friendly.
4152 *********************************************************************/
4153 static void javascriptify(char * identifier)
4155 char * p = identifier;
4156 while (NULL != (p = strchr(p, '-')))
4163 /*********************************************************************
4165 * Function : actions_to_radio
4167 * Description : Converts a actionsfile entry into settings for
4168 * radio buttons and edit boxes on a HTML form.
4171 * 1 : exports = List of substitutions to add to.
4172 * 2 : action = Action to read
4174 * Returns : JB_ERR_OK on success
4175 * JB_ERR_MEMORY on out-of-memory
4177 *********************************************************************/
4178 static jb_err actions_to_radio(struct map * exports,
4179 const struct action_spec *action)
4190 mask = action->mask;
4193 /* sanity - prevents "-feature +feature" */
4197 #define DEFINE_ACTION_BOOL(name, bit) \
4198 if (!(mask & bit)) \
4200 current_mode = 'n'; \
4202 else if (add & bit) \
4204 current_mode = 'y'; \
4208 current_mode = 'x'; \
4210 if (map_radio(exports, name, "ynx", current_mode)) \
4212 return JB_ERR_MEMORY; \
4215 #define DEFINE_ACTION_STRING(name, bit, index) \
4216 DEFINE_ACTION_BOOL(name, bit); \
4219 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4222 checked = !strcmp(action->string[index], value); \
4226 checked = is_default; \
4228 mapped_param |= checked; \
4229 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4231 return JB_ERR_MEMORY; \
4234 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4235 if (map(exports, name "-param-custom", 1, \
4236 ((!mapped_param) ? "checked" : ""), 1)) \
4238 return JB_ERR_MEMORY; \
4240 if (map(exports, name "-param", 1, \
4241 (((add & bit) && !mapped_param) ? \
4242 action->string[index] : default_val), 1)) \
4244 return JB_ERR_MEMORY; \
4247 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4248 if (map(exports, name "-param", 1, \
4249 ((add & bit) ? action->string[index] : default_val), 1)) \
4251 return JB_ERR_MEMORY; \
4254 #define DEFINE_ACTION_MULTI(name, index) \
4255 if (action->multi_add[index]->first) \
4257 current_mode = 'y'; \
4259 else if (action->multi_remove_all[index]) \
4261 current_mode = 'n'; \
4263 else if (action->multi_remove[index]->first) \
4265 current_mode = 'y'; \
4269 current_mode = 'x'; \
4271 if (map_radio(exports, name, "ynx", current_mode)) \
4273 return JB_ERR_MEMORY; \
4276 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4278 #include "actionlist.h"
4280 #undef DEFINE_ACTION_MULTI
4281 #undef DEFINE_ACTION_STRING
4282 #undef DEFINE_ACTION_BOOL
4283 #undef DEFINE_ACTION_ALIAS
4284 #undef DEFINE_CGI_PARAM_CUSTOM
4285 #undef DEFINE_CGI_PARAM_RADIO
4286 #undef DEFINE_CGI_PARAM_NO_RADIO
4292 /*********************************************************************
4294 * Function : actions_from_radio
4296 * Description : Converts a map of parameters passed to a CGI function
4297 * into an actionsfile entry.
4300 * 1 : parameters = parameters to the CGI call
4301 * 2 : action = Action to change. Must be valid before
4302 * the call, actions not specified will be
4305 * Returns : JB_ERR_OK on success
4306 * JB_ERR_MEMORY on out-of-memory
4308 *********************************************************************/
4309 static jb_err actions_from_radio(const struct map * parameters,
4310 struct action_spec *action)
4315 const char * js_name;
4316 jb_err err = JB_ERR_OK;
4321 /* Statics are generally a potential race condition,
4322 * but in this case we're safe and don't need semaphores.
4323 * Be careful if you modify this function.
4325 * The js_name_arr's are never free()d, but this is no
4326 * problem, since they will only be created once and
4327 * used by all threads thereafter. -oes
4330 #define JAVASCRIPTIFY(dest_var, string) \
4332 static int first_time = 1; \
4333 static char *js_name_arr; \
4336 js_name_arr = strdup(string); \
4337 javascriptify(js_name_arr); \
4339 dest_var = js_name_arr; \
4343 #define DEFINE_ACTION_BOOL(name, bit) \
4344 JAVASCRIPTIFY(js_name, name); \
4345 ch = get_char_param(parameters, js_name); \
4348 action->add |= bit; \
4349 action->mask |= bit; \
4351 else if (ch == 'N') \
4353 action->add &= ~bit; \
4354 action->mask &= ~bit; \
4356 else if (ch == 'X') \
4358 action->add &= ~bit; \
4359 action->mask |= bit; \
4362 #define DEFINE_ACTION_STRING(name, bit, index) \
4363 JAVASCRIPTIFY(js_name, name); \
4364 ch = get_char_param(parameters, js_name); \
4368 JAVASCRIPTIFY(js_name, name "-mode"); \
4369 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4370 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4372 JAVASCRIPTIFY(js_name, name "-param"); \
4373 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4375 if (param != NULL) \
4377 if (NULL == (param_dup = strdup(param))) \
4379 return JB_ERR_MEMORY; \
4381 freez(action->string[index]); \
4382 action->add |= bit; \
4383 action->mask |= bit; \
4384 action->string[index] = param_dup; \
4387 else if (ch == 'N') \
4389 if (action->add & bit) \
4391 freez(action->string[index]); \
4393 action->add &= ~bit; \
4394 action->mask &= ~bit; \
4396 else if (ch == 'X') \
4398 if (action->add & bit) \
4400 freez(action->string[index]); \
4402 action->add &= ~bit; \
4403 action->mask |= bit; \
4406 #define DEFINE_ACTION_MULTI(name, index) \
4407 JAVASCRIPTIFY(js_name, name); \
4408 ch = get_char_param(parameters, js_name); \
4413 else if (ch == 'N') \
4415 list_remove_all(action->multi_add[index]); \
4416 list_remove_all(action->multi_remove[index]); \
4417 action->multi_remove_all[index] = 1; \
4419 else if (ch == 'X') \
4421 list_remove_all(action->multi_add[index]); \
4422 list_remove_all(action->multi_remove[index]); \
4423 action->multi_remove_all[index] = 0; \
4426 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4428 #include "actionlist.h"
4430 #undef DEFINE_ACTION_MULTI
4431 #undef DEFINE_ACTION_STRING
4432 #undef DEFINE_ACTION_BOOL
4433 #undef DEFINE_ACTION_ALIAS
4434 #undef JAVASCRIPTIFY
4438 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4441 #ifdef FEATURE_TOGGLE
4442 /*********************************************************************
4444 * Function : cgi_toggle
4446 * Description : CGI function that adds a new empty section to
4450 * 1 : csp = Current client state (buffers, headers, etc...)
4451 * 2 : rsp = http_response data structure for output
4452 * 3 : parameters = map of cgi parameters
4455 * set : If present, how to change toggle setting:
4456 * "enable", "disable", "toggle", or none (default).
4457 * mini : If present, use mini reply template.
4459 * Returns : JB_ERR_OK on success
4460 * JB_ERR_MEMORY on out-of-memory
4462 *********************************************************************/
4463 jb_err cgi_toggle(struct client_state *csp,
4464 struct http_response *rsp,
4465 const struct map *parameters)
4467 struct map *exports;
4469 const char *template_name;
4475 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4477 return cgi_error_disabled(csp, rsp);
4480 mode = get_char_param(parameters, "set");
4485 global_toggle_state = 1;
4487 else if (mode == 'D')
4490 global_toggle_state = 0;
4492 else if (mode == 'T')
4495 global_toggle_state = !global_toggle_state;
4498 if (NULL == (exports = default_exports(csp, "toggle")))
4500 return JB_ERR_MEMORY;
4503 template_name = (get_char_param(parameters, "mini")
4507 return template_fill_for_cgi(csp, template_name, exports, rsp);
4509 #endif /* def FEATURE_TOGGLE */