1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.40 2002/05/19 11:34:35 jongfoster 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 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.
45 * Revision 1.40 2002/05/19 11:34:35 jongfoster
46 * Handling read-only actions files better - report the actual
47 * error, not "Out of memory"!
50 * http://sourceforge.net/tracker/index.php?func=detail
51 * &aid=557905&group_id=11118&atid=111118
53 * Revision 1.39 2002/05/12 21:39:15 jongfoster
54 * - Adding Doxygen-style comments to structures and #defines.
55 * - Correcting function comments
57 * Revision 1.38 2002/05/03 23:00:38 jongfoster
58 * Support for templates for "standard actions" buttons.
61 * Revision 1.37 2002/04/30 11:14:52 oes
62 * Made csp the first parameter in *action_to_html
64 * Revision 1.36 2002/04/26 21:53:30 jongfoster
65 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
67 * Revision 1.35 2002/04/26 21:50:02 jongfoster
68 * Honouring default exports in edit-actions-for-url-filter template.
70 * Revision 1.34 2002/04/26 12:54:17 oes
71 * Adaptions to changes in actions.c
73 * Revision 1.33 2002/04/24 02:17:47 oes
74 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
76 * - Activated Jon's code for editing multiple AFs
77 * - cgi_edit_list_actions now provides context-sensitive
78 * help, looks up all action sets from standard.action and
79 * makes buttons for them in the catchall section
80 * - cgi_edit_action_submit now honors a p parameter, looks up
81 * the corresponding action set, and sets the catchall pattern's
82 * actions accordingly.
84 * Revision 1.32 2002/04/19 16:55:31 jongfoster
85 * Fixing newline problems. If we do our own text file newline
86 * mangling, we don't want the library to do any, so we need to
87 * open the files in *binary* mode.
89 * Revision 1.31 2002/04/18 19:21:08 jongfoster
90 * Added code to detect "conventional" action files, that start
91 * with a set of actions for all URLs (the pattern "/").
92 * These are special-cased in the "edit-actions-list" CGI, so
93 * that a special UI can be written for them.
95 * Revision 1.30 2002/04/10 13:38:35 oes
96 * load_template signature changed
98 * Revision 1.29 2002/04/08 16:59:08 oes
101 * Revision 1.28 2002/03/27 12:30:29 oes
102 * Deleted unsused variable
104 * Revision 1.27 2002/03/26 23:06:04 jongfoster
105 * Removing duplicate @ifs on the toggle page
107 * Revision 1.26 2002/03/26 22:59:17 jongfoster
108 * Fixing /toggle to display status consistently.
110 * Revision 1.25 2002/03/26 22:29:54 swa
111 * we have a new homepage!
113 * Revision 1.24 2002/03/24 15:23:33 jongfoster
116 * Revision 1.23 2002/03/24 13:32:41 swa
117 * name change related issues
119 * Revision 1.22 2002/03/24 13:25:43 swa
120 * name change related issues
122 * Revision 1.21 2002/03/22 18:02:48 jongfoster
123 * Fixing remote toggle
125 * Revision 1.20 2002/03/16 20:28:34 oes
126 * Added descriptions to the filters so users will know what they select in the cgi editor
128 * Revision 1.19 2002/03/16 18:38:14 jongfoster
129 * Stopping stupid or malicious users from breaking the actions
130 * file using the web-based editor.
132 * Revision 1.18 2002/03/16 14:57:44 jongfoster
133 * Full support for enabling/disabling modular filters.
135 * Revision 1.17 2002/03/16 14:26:42 jongfoster
136 * First version of modular filters support - READ ONLY!
137 * Fixing a double-free bug in the out-of-memory handling in map_radio().
139 * Revision 1.16 2002/03/07 03:46:17 oes
140 * Fixed compiler warnings
142 * Revision 1.15 2002/03/06 22:54:35 jongfoster
143 * Automated function-comment nitpicking.
145 * Revision 1.14 2002/03/05 00:24:51 jongfoster
146 * Patch to always edit the current actions file.
148 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
149 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
151 * Revision 1.12 2002/03/03 09:18:03 joergs
152 * Made jumbjuster work on AmigaOS again.
154 * Revision 1.11 2002/01/23 01:03:31 jongfoster
155 * Fixing gcc [CygWin] compiler warnings
157 * Revision 1.10 2002/01/23 00:22:59 jongfoster
158 * Adding new function cgi_edit_actions_section_swap(), to reorder
161 * Adding get_url_spec_param() to get a validated URL pattern.
163 * Moving edit_read_line() out of this file and into loaders.c.
165 * Adding missing html_encode() to many CGI functions.
167 * Moving the functions that #include actionlist.h to the end of the file,
168 * because the Visual C++ 97 debugger gets extremely confused if you try
169 * to debug any code that comes after them in the file.
171 * Major optimizations in cgi_edit_actions_list() to reduce the size of
172 * the generated HTML (down 40% from 550k to 304k), with major side-effects
173 * throughout the editor and templates. In particular, the length of the
174 * URLs throughout the editor has been drastically reduced, by cutting
175 * paramater names down to 1 character and CGI names down to 3-4
176 * characters, by removing all non-essential CGI paramaters even at the
177 * expense of having to re-read the actions file for the most trivial
178 * page, and by using relative rather than absolute URLs. This means
179 * that this (typical example):
181 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
182 * filename=ijb&ver=1011487572&section=12&pattern=13
183 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
187 * <a href="eau?f=ijb&v=1011487572&p=13">
189 * Revision 1.9 2002/01/17 20:56:22 jongfoster
190 * Replacing hard references to the URL of the config interface
191 * with #defines from project.h
193 * Revision 1.8 2001/11/30 23:35:51 jongfoster
194 * Renaming actionsfile to ijb.action
196 * Revision 1.7 2001/11/13 00:28:24 jongfoster
197 * - Renaming parameters from edit-actions-for-url so that they only
198 * contain legal JavaScript characters. If we wanted to write
199 * JavaScript that worked with Netscape 4, this is nessacery.
200 * (Note that at the moment the JavaScript doesn't actually work
201 * with Netscape 4, but now this is purely a template issue, not
202 * one affecting code).
203 * - Adding new CGIs for use by non-JavaScript browsers:
204 * edit-actions-url-form
205 * edit-actions-add-url-form
206 * edit-actions-remove-url-form
209 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
210 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
211 * by and __OS2__ ifdef.
213 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
214 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
215 * threads to call select() simultaneously. So, it's time to do a real, live,
216 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
217 * (native). Both versions will work, but using __OS2__ offers multi-threading.
219 * Revision 1.4 2001/10/23 21:48:19 jongfoster
220 * Cleaning up error handling in CGI functions - they now send back
221 * a HTML error page and should never cause a FATAL error. (Fixes one
222 * potential source of "denial of service" attacks).
224 * CGI actions file editor that works and is actually useful.
226 * Ability to toggle JunkBuster remotely using a CGI call.
228 * You can turn off both the above features in the main configuration
229 * file, e.g. if you are running a multi-user proxy.
231 * Revision 1.3 2001/10/14 22:12:49 jongfoster
232 * New version of CGI-based actionsfile editor.
233 * Major changes, including:
234 * - Completely new file parser and file output routines
235 * - edit-actions CGI renamed edit-actions-for-url
236 * - All CGIs now need a filename parameter, except for...
237 * - New CGI edit-actions which doesn't need a filename,
238 * to allow you to start the editor up.
239 * - edit-actions-submit now works, and now automatically
240 * redirects you back to the main edit-actions-list handler.
242 * Revision 1.2 2001/09/16 17:05:14 jongfoster
243 * Removing unused #include showarg.h
245 * Revision 1.1 2001/09/16 15:47:37 jongfoster
246 * First version of CGI-based edit interface. This is very much a
247 * work-in-progress, and you can't actually use it to edit anything
248 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
249 * to have any effect.
252 **********************************************************************/
258 * FIXME: Following includes copied from cgi.c - which are actually needed?
263 #include <sys/types.h>
267 #include <sys/stat.h>
270 #define snprintf _snprintf
271 #endif /* def _WIN32 */
276 #include "cgisimple.h"
280 #include "miscutil.h"
284 /* loadcfg.h is for g_bToggleIJB only */
285 #include "urlmatch.h"
287 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
290 #ifdef FEATURE_CGI_EDIT_ACTIONS
293 * A line in an editable_file.
297 /** Next entry in the linked list */
298 struct file_line * next;
300 /** The raw data, to write out if this line is unmodified. */
303 /** Comments and/or whitespace to put before this line if it's modified
304 and then written out. */
307 /** The actual data, as a string. Line continuation and comment removal
308 are performed on the data read from file before it's stored here, so
309 it will be a single line of data. */
312 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
315 /** The actual data, processed into some sensible data type. */
319 /** An action specification. */
320 struct action_spec action[1];
322 /** A name=value pair. */
326 /** The name in the name=value pair. */
329 /** The value in the name=value pair, as a string. */
332 /** The value in the name=value pair, as an integer. */
337 /* Add more data types here... e.g.
340 struct url_spec url[1];
344 struct action_spec action[1];
354 /** This file_line has not been processed yet. */
355 #define FILE_LINE_UNPROCESSED 1
357 /** This file_line is blank. Can only appear at the end of a file, due to
358 the way the parser works. */
359 #define FILE_LINE_BLANK 2
361 /** This file_line says {{alias}}. */
362 #define FILE_LINE_ALIAS_HEADER 3
364 /** This file_line defines an alias. */
365 #define FILE_LINE_ALIAS_ENTRY 4
367 /** This file_line defines an {action}. */
368 #define FILE_LINE_ACTION 5
370 /** This file_line specifies a URL pattern. */
371 #define FILE_LINE_URL 6
373 /** This file_line says {{settings}}. */
374 #define FILE_LINE_SETTINGS_HEADER 7
376 /** This file_line is in a {{settings}} block. */
377 #define FILE_LINE_SETTINGS_ENTRY 8
379 /** This file_line says {{description}}. */
380 #define FILE_LINE_DESCRIPTION_HEADER 9
382 /** This file_line is in a {{description}} block. */
383 #define FILE_LINE_DESCRIPTION_ENTRY 10
387 * A configuration file, in a format that can be edited and written back to
392 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
393 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
394 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
395 /**< Pre-encoded with url_encode() for ease of use. */
396 const char * version_str; /**< Last modification time, as a string. For CGI param. */
397 /**< Can be used in URL without using url_param(). */
398 unsigned version; /**< Last modification time - prevents chaos with
399 the browser's "back" button. Note that this is a
400 time_t cast to an unsigned. When comparing, always
401 cast the time_t to an unsigned, and *NOT* vice-versa.
402 This may lose the top few bits, but they're not
403 significant anyway. */
404 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
405 Note that changing this after the file has been
406 read in will cause a mess. */
407 struct file_line * parse_error; /**< On parse error, this is the offending line. */
408 const char * parse_error_text; /**< On parse error, this is the problem.
409 (Statically allocated) */
412 /* FIXME: Following non-static functions should be prototyped in .h or made static */
414 /* Functions to read and write arbitrary config files */
415 jb_err edit_read_file(struct client_state *csp,
416 const struct map *parameters,
419 struct editable_file **pfile);
420 jb_err edit_write_file(struct editable_file * file);
421 void edit_free_file(struct editable_file * file);
423 /* Functions to read and write actions files */
424 jb_err edit_parse_actions_file(struct editable_file * file);
425 jb_err edit_read_actions_file(struct client_state *csp,
426 struct http_response *rsp,
427 const struct map *parameters,
429 struct editable_file **pfile);
432 jb_err cgi_error_modified(struct client_state *csp,
433 struct http_response *rsp,
434 const char *filename);
435 jb_err cgi_error_parse(struct client_state *csp,
436 struct http_response *rsp,
437 struct editable_file *file);
438 jb_err cgi_error_file(struct client_state *csp,
439 struct http_response *rsp,
440 const char *filename);
441 jb_err cgi_error_file_read_only(struct client_state *csp,
442 struct http_response *rsp,
443 const char *filename);
444 jb_err cgi_error_disabled(struct client_state *csp,
445 struct http_response *rsp);
447 /* Internal arbitrary config file support functions */
448 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
449 static void edit_free_file_lines(struct file_line * first_line);
451 /* Internal actions file support functions */
452 static int match_actions_file_header_line(const char * line, const char * name);
453 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
455 /* Internal parameter parsing functions */
456 static jb_err get_file_name_param(struct client_state *csp,
457 const struct map *parameters,
458 const char *param_name,
461 const char **pparam);
463 static jb_err get_url_spec_param(struct client_state *csp,
464 const struct map *parameters,
469 /* Internal actionsfile <==> HTML conversion functions */
470 static jb_err map_radio(struct map * exports,
471 const char * optionname,
474 static jb_err actions_to_radio(struct map * exports,
475 const struct action_spec *action);
476 static jb_err actions_from_radio(const struct map * parameters,
477 struct action_spec *action);
480 static jb_err map_copy_parameter_html(struct map *out,
481 const struct map *in,
483 #if 0 /* unused function */
484 static jb_err map_copy_parameter_url(struct map *out,
485 const struct map *in,
487 #endif /* unused function */
489 /* Internal convenience functions */
490 static char *section_target(const unsigned sectionid);
492 /*********************************************************************
494 * Function : section_target
496 * Description : Given an unsigned (section id) n, produce a dynamically
497 * allocated string of the form #l<n>, for use in link
501 * 1 : sectionid = start line number of section
503 * Returns : String with link target, or NULL if out of
506 *********************************************************************/
507 static char *section_target(const unsigned sectionid)
511 snprintf(buf, 30, "#l%d", sectionid);
517 /*********************************************************************
519 * Function : map_copy_parameter_html
521 * Description : Copy a CGI parameter from one map to another, HTML
525 * 1 : out = target map
526 * 2 : in = source map
527 * 3 : name = name of cgi parameter to copy
529 * Returns : JB_ERR_OK on success
530 * JB_ERR_MEMORY on out-of-memory
531 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
534 *********************************************************************/
535 static jb_err map_copy_parameter_html(struct map *out,
536 const struct map *in,
546 value = lookup(in, name);
547 err = map(out, name, 1, html_encode(value), 0);
554 else if (*value == '\0')
556 return JB_ERR_CGI_PARAMS;
565 #if 0 /* unused function */
566 /*********************************************************************
568 * Function : map_copy_parameter_url
570 * Description : Copy a CGI parameter from one map to another, URL
574 * 1 : out = target map
575 * 2 : in = source map
576 * 3 : name = name of cgi parameter to copy
578 * Returns : JB_ERR_OK on success
579 * JB_ERR_MEMORY on out-of-memory
580 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
583 *********************************************************************/
584 static jb_err map_copy_parameter_url(struct map *out,
585 const struct map *in,
595 value = lookup(in, name);
596 err = map(out, name, 1, url_encode(value), 0);
603 else if (*value == '\0')
605 return JB_ERR_CGI_PARAMS;
612 #endif /* 0 - unused function */
614 /*********************************************************************
616 * Function : cgi_edit_actions_url_form
618 * Description : CGI function that displays a form for
622 * 1 : csp = Current client state (buffers, headers, etc...)
623 * 2 : rsp = http_response data structure for output
624 * 3 : parameters = map of cgi parameters
627 * f : (filename) Identifies the file to edit
628 * v : (version) File's last-modified time
629 * p : (pattern) Line number of pattern to edit
631 * Returns : JB_ERR_OK on success
632 * JB_ERR_MEMORY on out-of-memory
633 * JB_ERR_CGI_PARAMS if the CGI parameters are not
634 * specified or not valid.
636 *********************************************************************/
637 jb_err cgi_edit_actions_url_form(struct client_state *csp,
638 struct http_response *rsp,
639 const struct map *parameters)
641 struct map * exports;
643 struct editable_file * file;
644 struct file_line * cur_line;
645 unsigned line_number;
646 unsigned section_start_line_number = 0;
653 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
655 return cgi_error_disabled(csp, rsp);
658 err = get_number_param(csp, parameters, "p", &patternid);
664 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
667 /* No filename specified, can't read file, modified, or out of memory. */
668 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
671 cur_line = file->lines;
673 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
675 if (cur_line->type == FILE_LINE_ACTION)
677 section_start_line_number = line_number;
679 cur_line = cur_line->next;
682 if ( (cur_line == NULL)
683 || (line_number != patternid)
685 || (cur_line->type != FILE_LINE_URL))
687 /* Invalid "patternid" parameter */
688 edit_free_file(file);
689 return JB_ERR_CGI_PARAMS;
692 if (NULL == (exports = default_exports(csp, NULL)))
694 edit_free_file(file);
695 return JB_ERR_MEMORY;
698 err = map(exports, "f", 1, file->identifier, 1);
699 if (!err) err = map(exports, "v", 1, file->version_str, 1);
700 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
701 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
702 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
704 edit_free_file(file);
712 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
716 /*********************************************************************
718 * Function : cgi_edit_actions_add_url_form
720 * Description : CGI function that displays a form for
724 * 1 : csp = Current client state (buffers, headers, etc...)
725 * 2 : rsp = http_response data structure for output
726 * 3 : parameters = map of cgi parameters
729 * f : (filename) Identifies the file to edit
730 * v : (version) File's last-modified time
731 * s : (section) Line number of section to edit
733 * Returns : JB_ERR_OK on success
734 * JB_ERR_MEMORY on out-of-memory
735 * JB_ERR_CGI_PARAMS if the CGI parameters are not
736 * specified or not valid.
738 *********************************************************************/
739 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
740 struct http_response *rsp,
741 const struct map *parameters)
750 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
752 return cgi_error_disabled(csp, rsp);
755 if (NULL == (exports = default_exports(csp, NULL)))
757 return JB_ERR_MEMORY;
760 err = map_copy_parameter_html(exports, parameters, "f");
761 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
762 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
770 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
774 /*********************************************************************
776 * Function : cgi_edit_actions_remove_url_form
778 * Description : CGI function that displays a form for
782 * 1 : csp = Current client state (buffers, headers, etc...)
783 * 2 : rsp = http_response data structure for output
784 * 3 : parameters = map of cgi parameters
787 * f : (filename) Identifies the file to edit
788 * v : (version) File's last-modified time
789 * p : (pattern) Line number of pattern to edit
791 * Returns : JB_ERR_OK on success
792 * JB_ERR_MEMORY on out-of-memory
793 * JB_ERR_CGI_PARAMS if the CGI parameters are not
794 * specified or not valid.
796 *********************************************************************/
797 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
798 struct http_response *rsp,
799 const struct map *parameters)
801 struct map * exports;
803 struct editable_file * file;
804 struct file_line * cur_line;
805 unsigned line_number;
806 unsigned section_start_line_number = 0;
813 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
815 return cgi_error_disabled(csp, rsp);
818 err = get_number_param(csp, parameters, "p", &patternid);
824 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
827 /* No filename specified, can't read file, modified, or out of memory. */
828 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
831 cur_line = file->lines;
833 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
835 if (cur_line->type == FILE_LINE_ACTION)
837 section_start_line_number = line_number;
839 cur_line = cur_line->next;
842 if ( (cur_line == NULL)
843 || (line_number != patternid)
845 || (cur_line->type != FILE_LINE_URL))
847 /* Invalid "patternid" parameter */
848 edit_free_file(file);
849 return JB_ERR_CGI_PARAMS;
852 if (NULL == (exports = default_exports(csp, NULL)))
854 edit_free_file(file);
855 return JB_ERR_MEMORY;
858 err = map(exports, "f", 1, file->identifier, 1);
859 if (!err) err = map(exports, "v", 1, file->version_str, 1);
860 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
861 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
862 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
864 edit_free_file(file);
872 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
876 /*********************************************************************
878 * Function : edit_write_file
880 * Description : Write a complete file to disk.
883 * 1 : file = File to write.
885 * Returns : JB_ERR_OK on success
886 * JB_ERR_FILE on error writing to file.
887 * JB_ERR_MEMORY on out of memory
889 *********************************************************************/
890 jb_err edit_write_file(struct editable_file * file)
893 struct file_line * cur_line;
894 struct stat statbuf[1];
895 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
896 digits in time_t, assuming this is a 64-bit
897 machine, plus null terminator, plus one
901 assert(file->filename);
903 if (NULL == (fp = fopen(file->filename, "wb")))
908 cur_line = file->lines;
909 while (cur_line != NULL)
913 if (fputs(cur_line->raw, fp) < 0)
921 if (cur_line->prefix)
923 if (fputs(cur_line->prefix, fp) < 0)
929 if (cur_line->unprocessed)
932 if (NULL != strchr(cur_line->unprocessed, '#'))
934 /* Must quote '#' characters */
941 /* Count number of # characters, so we know length of output string */
942 src = cur_line->unprocessed;
943 while (NULL != (src = strchr(src, '#')))
950 /* Allocate new memory for string */
951 len = strlen(cur_line->unprocessed);
952 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
954 /* Uh oh, just trashed file! */
956 return JB_ERR_MEMORY;
959 /* Loop through string from end */
960 src = cur_line->unprocessed + len;
961 dest = str + len + numhash;
962 for ( ; len >= 0; len--)
964 if ((*dest-- = *src--) == '#')
968 assert(numhash >= 0);
971 assert(numhash == 0);
972 assert(src + 1 == cur_line->unprocessed);
973 assert(dest + 1 == str);
975 if (fputs(str, fp) < 0)
986 /* Can write without quoting '#' characters. */
987 if (fputs(cur_line->unprocessed, fp) < 0)
993 if (fputs(NEWLINE(file->newline), fp) < 0)
1001 /* FIXME: Write data from file->data->whatever */
1005 cur_line = cur_line->next;
1011 /* Update the version stamp in the file structure, since we just
1012 * wrote to the file & changed it's date.
1014 if (stat(file->filename, statbuf) < 0)
1016 /* Error, probably file not found. */
1019 file->version = (unsigned)statbuf->st_mtime;
1021 /* Correct file->version_str */
1022 freez(file->version_str);
1023 snprintf(version_buf, 22, "%u", file->version);
1024 version_buf[21] = '\0';
1025 file->version_str = strdup(version_buf);
1026 if (version_buf == NULL)
1028 return JB_ERR_MEMORY;
1035 /*********************************************************************
1037 * Function : edit_free_file
1039 * Description : Free a complete file in memory.
1042 * 1 : file = Data structure to free.
1046 *********************************************************************/
1047 void edit_free_file(struct editable_file * file)
1051 /* Silently ignore NULL pointer */
1055 edit_free_file_lines(file->lines);
1056 freez(file->filename);
1057 freez(file->identifier);
1058 freez(file->version_str);
1060 file->parse_error_text = NULL; /* Statically allocated */
1061 file->parse_error = NULL;
1067 /*********************************************************************
1069 * Function : edit_free_file_lines
1071 * Description : Free an entire linked list of file lines.
1074 * 1 : first_line = Data structure to free.
1078 *********************************************************************/
1079 static void edit_free_file_lines(struct file_line * first_line)
1081 struct file_line * next_line;
1083 while (first_line != NULL)
1085 next_line = first_line->next;
1086 first_line->next = NULL;
1087 freez(first_line->raw);
1088 freez(first_line->prefix);
1089 freez(first_line->unprocessed);
1090 switch(first_line->type)
1092 case 0: /* special case if memory zeroed */
1093 case FILE_LINE_UNPROCESSED:
1094 case FILE_LINE_BLANK:
1095 case FILE_LINE_ALIAS_HEADER:
1096 case FILE_LINE_SETTINGS_HEADER:
1097 case FILE_LINE_DESCRIPTION_HEADER:
1098 case FILE_LINE_DESCRIPTION_ENTRY:
1099 case FILE_LINE_ALIAS_ENTRY:
1101 /* No data is stored for these */
1104 case FILE_LINE_ACTION:
1105 free_action(first_line->data.action);
1108 case FILE_LINE_SETTINGS_ENTRY:
1109 freez(first_line->data.setting.name);
1110 freez(first_line->data.setting.svalue);
1113 /* Should never happen */
1117 first_line->type = 0; /* paranoia */
1119 first_line = next_line;
1124 /*********************************************************************
1126 * Function : match_actions_file_header_line
1128 * Description : Match an actions file {{header}} line
1131 * 1 : line = String from file
1132 * 2 : name = Header to match against
1134 * Returns : 0 iff they match.
1136 *********************************************************************/
1137 static int match_actions_file_header_line(const char * line, const char * name)
1145 if ((line[0] != '{') || (line[1] != '{'))
1151 /* Look for optional whitespace */
1152 while ( (*line == ' ') || (*line == '\t') )
1157 /* Look for the specified name (case-insensitive) */
1159 if (0 != strncmpic(line, name, len))
1165 /* Look for optional whitespace */
1166 while ( (*line == ' ') || (*line == '\t') )
1171 /* Look for "}}" and end of string*/
1172 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1182 /*********************************************************************
1184 * Function : match_actions_file_header_line
1186 * Description : Match an actions file {{header}} line
1189 * 1 : line = String from file. Must not start with
1190 * whitespace (else infinite loop!)
1191 * 2 : pname = Destination for name
1192 * 2 : pvalue = Destination for value
1194 * Returns : JB_ERR_OK on success
1195 * JB_ERR_MEMORY on out-of-memory
1196 * JB_ERR_PARSE if there's no "=" sign, or if there's
1197 * nothing before the "=" sign (but empty
1198 * values *after* the "=" sign are legal).
1200 *********************************************************************/
1201 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1203 const char * name_end;
1204 const char * value_start;
1210 assert(*line != ' ');
1211 assert(*line != '\t');
1216 value_start = strchr(line, '=');
1217 if ((value_start == NULL) || (value_start == line))
1219 return JB_ERR_PARSE;
1222 name_end = value_start - 1;
1224 /* Eat any whitespace before the '=' */
1225 while ((*name_end == ' ') || (*name_end == '\t'))
1228 * we already know we must have at least 1 non-ws char
1229 * at start of buf - no need to check
1234 name_len = name_end - line + 1; /* Length excluding \0 */
1235 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1237 return JB_ERR_MEMORY;
1239 strncpy(*pname, line, name_len);
1240 (*pname)[name_len] = '\0';
1242 /* Eat any the whitespace after the '=' */
1244 while ((*value_start == ' ') || (*value_start == '\t'))
1249 if (NULL == (*pvalue = strdup(value_start)))
1253 return JB_ERR_MEMORY;
1260 /*********************************************************************
1262 * Function : edit_parse_actions_file
1264 * Description : Parse an actions file in memory.
1266 * Passed linked list must have the "data" member
1267 * zeroed, and must contain valid "next" and
1268 * "unprocessed" fields. The "raw" and "prefix"
1269 * fields are ignored, and "type" is just overwritten.
1271 * Note that on error the file may have been
1275 * 1 : file = Actions file to be parsed in-place.
1277 * Returns : JB_ERR_OK on success
1278 * JB_ERR_MEMORY on out-of-memory
1279 * JB_ERR_PARSE on error
1281 *********************************************************************/
1282 jb_err edit_parse_actions_file(struct editable_file * file)
1284 struct file_line * cur_line;
1286 const char * text; /* Text from a line */
1287 char * name; /* For lines of the form name=value */
1288 char * value; /* For lines of the form name=value */
1289 struct action_alias * alias_list = NULL;
1290 jb_err err = JB_ERR_OK;
1292 /* alias_list contains the aliases defined in this file.
1293 * It might be better to use the "file_line.data" fields
1294 * in the relavent places instead.
1297 cur_line = file->lines;
1299 /* A note about blank line support: Blank lines should only
1300 * ever occur as the last line in the file. This function
1301 * is more forgiving than that - FILE_LINE_BLANK can occur
1305 /* Skip leading blanks. Should only happen if file is
1306 * empty (which is valid, but pointless).
1308 while ( (cur_line != NULL)
1309 && (cur_line->unprocessed[0] == '\0') )
1312 cur_line->type = FILE_LINE_BLANK;
1313 cur_line = cur_line->next;
1316 if ( (cur_line != NULL)
1317 && (cur_line->unprocessed[0] != '{') )
1319 /* File doesn't start with a header */
1320 file->parse_error = cur_line;
1321 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1322 return JB_ERR_PARSE;
1325 if ( (cur_line != NULL) && (0 ==
1326 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1328 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1330 cur_line = cur_line->next;
1331 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1333 if (cur_line->unprocessed[0])
1335 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1337 err = split_line_on_equals(cur_line->unprocessed,
1338 &cur_line->data.setting.name,
1339 &cur_line->data.setting.svalue);
1340 if (err == JB_ERR_MEMORY)
1344 else if (err != JB_ERR_OK)
1346 /* Line does not contain a name=value pair */
1347 file->parse_error = cur_line;
1348 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1349 return JB_ERR_PARSE;
1354 cur_line->type = FILE_LINE_BLANK;
1356 cur_line = cur_line->next;
1360 if ( (cur_line != NULL) && (0 ==
1361 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1363 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1365 cur_line = cur_line->next;
1366 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1368 if (cur_line->unprocessed[0])
1370 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1374 cur_line->type = FILE_LINE_BLANK;
1376 cur_line = cur_line->next;
1380 if ( (cur_line != NULL) && (0 ==
1381 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1383 cur_line->type = FILE_LINE_ALIAS_HEADER;
1385 cur_line = cur_line->next;
1386 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1388 if (cur_line->unprocessed[0])
1390 /* define an alias */
1391 struct action_alias * new_alias;
1393 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1395 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1396 if (err == JB_ERR_MEMORY)
1400 else if (err != JB_ERR_OK)
1402 /* Line does not contain a name=value pair */
1403 file->parse_error = cur_line;
1404 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1405 return JB_ERR_PARSE;
1408 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1413 free_alias_list(alias_list);
1414 return JB_ERR_MEMORY;
1417 err = get_actions(value, alias_list, new_alias->action);
1420 /* Invalid action or out of memory */
1424 free_alias_list(alias_list);
1425 if (err == JB_ERR_MEMORY)
1431 /* Line does not contain a name=value pair */
1432 file->parse_error = cur_line;
1433 file->parse_error_text = "This alias does not specify a valid set of actions.";
1434 return JB_ERR_PARSE;
1440 new_alias->name = name;
1443 new_alias->next = alias_list;
1444 alias_list = new_alias;
1448 cur_line->type = FILE_LINE_BLANK;
1450 cur_line = cur_line->next;
1454 /* Header done, process the main part of the file */
1455 while (cur_line != NULL)
1457 /* At this point, (cur_line->unprocessed[0] == '{') */
1458 assert(cur_line->unprocessed[0] == '{');
1459 text = cur_line->unprocessed + 1;
1460 len = strlen(text) - 1;
1461 if (text[len] != '}')
1463 /* No closing } on header */
1464 free_alias_list(alias_list);
1465 file->parse_error = cur_line;
1466 file->parse_error_text = "Headers starting with '{' must have a "
1467 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1468 "must close with two brackets ('}}').";
1469 return JB_ERR_PARSE;
1474 /* An invalid {{ header. */
1475 free_alias_list(alias_list);
1476 file->parse_error = cur_line;
1477 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1478 "Please remember that the system (two-bracket) headers must "
1479 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1480 "and must appear before any actions (one-bracket) headers. "
1481 "Also note that system headers may not be repeated.";
1482 return JB_ERR_PARSE;
1485 while ( (*text == ' ') || (*text == '\t') )
1491 && ( (text[len - 1] == ' ')
1492 || (text[len - 1] == '\t') ) )
1497 cur_line->type = FILE_LINE_ACTION;
1499 /* Remove {} and make copy */
1500 if (NULL == (value = (char *) malloc(len + 1)))
1503 free_alias_list(alias_list);
1504 return JB_ERR_MEMORY;
1506 strncpy(value, text, len);
1510 err = get_actions(value, alias_list, cur_line->data.action);
1513 /* Invalid action or out of memory */
1515 free_alias_list(alias_list);
1516 if (err == JB_ERR_MEMORY)
1522 /* Line does not contain a name=value pair */
1523 file->parse_error = cur_line;
1524 file->parse_error_text = "This header does not specify a valid set of actions.";
1525 return JB_ERR_PARSE;
1529 /* Done with string - it was clobbered anyway */
1532 /* Process next line */
1533 cur_line = cur_line->next;
1535 /* Loop processing URL patterns */
1536 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1538 if (cur_line->unprocessed[0])
1540 /* Could parse URL here, but this isn't currently needed */
1542 cur_line->type = FILE_LINE_URL;
1546 cur_line->type = FILE_LINE_BLANK;
1548 cur_line = cur_line->next;
1550 } /* End main while(cur_line != NULL) loop */
1552 free_alias_list(alias_list);
1558 /*********************************************************************
1560 * Function : edit_read_file_lines
1562 * Description : Read all the lines of a file into memory.
1563 * Handles whitespace, comments and line continuation.
1566 * 1 : fp = File to read from. On return, this will be
1567 * at EOF but it will not have been closed.
1568 * 2 : pfile = Destination for a linked list of file_lines.
1569 * Will be set to NULL on error.
1570 * 3 : newline = How to handle newlines.
1572 * Returns : JB_ERR_OK on success
1573 * JB_ERR_MEMORY on out-of-memory
1575 *********************************************************************/
1576 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1578 struct file_line * first_line; /* Keep for return value or to free */
1579 struct file_line * cur_line; /* Current line */
1580 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1588 cur_line = first_line = zalloc(sizeof(struct file_line));
1589 if (cur_line == NULL)
1591 return JB_ERR_MEMORY;
1594 cur_line->type = FILE_LINE_UNPROCESSED;
1596 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1599 /* Out of memory or empty file. */
1600 /* Note that empty file is not an error we propogate up */
1602 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1607 prev_line = cur_line;
1608 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1609 if (cur_line == NULL)
1612 edit_free_file_lines(first_line);
1613 return JB_ERR_MEMORY;
1616 cur_line->type = FILE_LINE_UNPROCESSED;
1618 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1619 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1622 edit_free_file_lines(first_line);
1623 return JB_ERR_MEMORY;
1627 while (rval != JB_ERR_FILE);
1631 /* We allocated one too many - free it */
1632 prev_line->next = NULL;
1635 *pfile = first_line;
1640 /*********************************************************************
1642 * Function : edit_read_file
1644 * Description : Read a complete file into memory.
1645 * Handles CGI parameter parsing. If requested, also
1646 * checks the file's modification timestamp.
1649 * 1 : csp = Current client state (buffers, headers, etc...)
1650 * 2 : parameters = map of cgi parameters.
1651 * 3 : require_version = true to check "ver" parameter.
1652 * 4 : suffix = File extension, e.g. ".action".
1653 * 5 : pfile = Destination for the file. Will be set
1657 * filename : The name of the file to read, without the
1658 * path or ".action" extension.
1659 * ver : (Only if require_version is nonzero)
1660 * Timestamp of the actions file. If wrong, this
1661 * function fails with JB_ERR_MODIFIED.
1663 * Returns : JB_ERR_OK on success
1664 * JB_ERR_MEMORY on out-of-memory
1665 * JB_ERR_CGI_PARAMS if "filename" was not specified
1667 * JB_ERR_FILE if the file cannot be opened or
1669 * JB_ERR_MODIFIED if version checking was requested and
1670 * failed - the file was modified outside
1671 * of this CGI editor instance.
1673 *********************************************************************/
1674 jb_err edit_read_file(struct client_state *csp,
1675 const struct map *parameters,
1676 int require_version,
1678 struct editable_file **pfile)
1680 struct file_line * lines;
1684 const char * identifier;
1685 struct editable_file * file;
1686 unsigned version = 0;
1687 struct stat statbuf[1];
1688 char version_buf[22];
1689 int newline = NEWLINE_UNKNOWN;
1697 err = get_file_name_param(csp, parameters, "f", suffix,
1698 &filename, &identifier);
1704 if (stat(filename, statbuf) < 0)
1706 /* Error, probably file not found. */
1710 version = (unsigned) statbuf->st_mtime;
1712 if (require_version)
1714 unsigned specified_version;
1715 err = get_number_param(csp, parameters, "v", &specified_version);
1722 if (version != specified_version)
1724 return JB_ERR_MODIFIED;
1728 if (NULL == (fp = fopen(filename,"rb")))
1734 err = edit_read_file_lines(fp, &lines, &newline);
1744 file = (struct editable_file *) zalloc(sizeof(*file));
1748 edit_free_file_lines(lines);
1752 file->lines = lines;
1753 file->newline = newline;
1754 file->filename = filename;
1755 file->version = version;
1756 file->identifier = url_encode(identifier);
1758 if (file->identifier == NULL)
1760 edit_free_file(file);
1761 return JB_ERR_MEMORY;
1764 /* Correct file->version_str */
1765 freez(file->version_str);
1766 snprintf(version_buf, 22, "%u", file->version);
1767 version_buf[21] = '\0';
1768 file->version_str = strdup(version_buf);
1769 if (version_buf == NULL)
1771 edit_free_file(file);
1772 return JB_ERR_MEMORY;
1780 /*********************************************************************
1782 * Function : edit_read_actions_file
1784 * Description : Read a complete actions file into memory.
1785 * Handles CGI parameter parsing. If requested, also
1786 * checks the file's modification timestamp.
1788 * If this function detects an error in the categories
1789 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1790 * then it handles it by filling in the specified
1791 * response structure and returning JB_ERR_FILE.
1794 * 1 : csp = Current client state (buffers, headers, etc...)
1795 * 2 : rsp = HTTP response. Only filled in on error.
1796 * 2 : parameters = map of cgi parameters.
1797 * 3 : require_version = true to check "ver" parameter.
1798 * 4 : pfile = Destination for the file. Will be set
1802 * filename : The name of the actions file to read, without the
1803 * path or ".action" extension.
1804 * ver : (Only if require_version is nonzero)
1805 * Timestamp of the actions file. If wrong, this
1806 * function fails with JB_ERR_MODIFIED.
1808 * Returns : JB_ERR_OK on success
1809 * JB_ERR_MEMORY on out-of-memory
1810 * JB_ERR_CGI_PARAMS if "filename" was not specified
1812 * JB_ERR_FILE if the file does not contain valid data,
1813 * or if file cannot be opened or
1814 * contains no data, or if version
1815 * checking was requested and failed.
1817 *********************************************************************/
1818 jb_err edit_read_actions_file(struct client_state *csp,
1819 struct http_response *rsp,
1820 const struct map *parameters,
1821 int require_version,
1822 struct editable_file **pfile)
1825 struct editable_file *file;
1833 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1836 /* Try to handle if possible */
1837 if (err == JB_ERR_FILE)
1839 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1841 else if (err == JB_ERR_MODIFIED)
1843 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1845 if (err == JB_ERR_OK)
1848 * Signal to higher-level CGI code that there was a problem but we
1849 * handled it, they should just return JB_ERR_OK.
1856 err = edit_parse_actions_file(file);
1859 if (err == JB_ERR_PARSE)
1861 err = cgi_error_parse(csp, rsp, file);
1862 if (err == JB_ERR_OK)
1865 * Signal to higher-level CGI code that there was a problem but we
1866 * handled it, they should just return JB_ERR_OK.
1871 edit_free_file(file);
1880 /*********************************************************************
1882 * Function : get_file_name_param
1884 * Description : Get the name of the file to edit from the parameters
1885 * passed to a CGI function. This function handles
1886 * security checks such as blocking urls containing
1887 * "/" or ".", prepending the config file directory,
1888 * and adding the specified suffix.
1890 * (This is an essential security check, otherwise
1891 * users may be able to pass "../../../etc/passwd"
1892 * and overwrite the password file [linux], "prn:"
1893 * and print random data [Windows], etc...)
1895 * This function only allows filenames contining the
1896 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1897 * That's probably too restrictive but at least it's
1901 * 1 : csp = Current client state (buffers, headers, etc...)
1902 * 2 : parameters = map of cgi parameters
1903 * 3 : param_name = The name of the parameter to read
1904 * 4 : suffix = File extension, e.g. ".actions"
1905 * 5 : pfilename = destination for full filename. Caller
1906 * free()s. Set to NULL on error.
1907 * 6 : pparam = destination for partial filename,
1908 * suitable for use in another URL. Allocated as part
1909 * of the map "parameters", so don't free it.
1910 * Set to NULL if not specified.
1912 * Returns : JB_ERR_OK on success
1913 * JB_ERR_MEMORY on out-of-memory
1914 * JB_ERR_CGI_PARAMS if "filename" was not specified
1917 *********************************************************************/
1918 static jb_err get_file_name_param(struct client_state *csp,
1919 const struct map *parameters,
1920 const char *param_name,
1923 const char **pparam)
1941 param = lookup(parameters, param_name);
1944 return JB_ERR_CGI_PARAMS;
1949 len = strlen(param);
1950 if (len >= FILENAME_MAX)
1953 return JB_ERR_CGI_PARAMS;
1956 /* Check every character to see if it's legal */
1958 while ((ch = *s++) != '\0')
1960 if ( ((ch < 'A') || (ch > 'Z'))
1961 && ((ch < 'a') || (ch > 'z'))
1962 && ((ch < '0') || (ch > '9'))
1966 /* Probable hack attempt. */
1967 return JB_ERR_CGI_PARAMS;
1971 /* Append extension */
1972 name = malloc(len + strlen(suffix) + 1);
1975 return JB_ERR_MEMORY;
1977 strcpy(name, param);
1978 strcpy(name + len, suffix);
1981 fullpath = make_path(csp->config->confdir, name);
1984 if (fullpath == NULL)
1986 return JB_ERR_MEMORY;
1990 *pfilename = fullpath;
1996 /*********************************************************************
1998 * Function : get_url_spec_param
2000 * Description : Get a URL pattern from the parameters
2001 * passed to a CGI function. Removes leading/trailing
2002 * spaces and validates it.
2005 * 1 : csp = Current client state (buffers, headers, etc...)
2006 * 2 : parameters = map of cgi parameters
2007 * 3 : name = Name of CGI parameter to read
2008 * 4 : pvalue = destination for value. Will be malloc()'d.
2009 * Set to NULL on error.
2011 * Returns : JB_ERR_OK on success
2012 * JB_ERR_MEMORY on out-of-memory
2013 * JB_ERR_CGI_PARAMS if the parameter was not specified
2016 *********************************************************************/
2017 static jb_err get_url_spec_param(struct client_state *csp,
2018 const struct map *parameters,
2022 const char *orig_param;
2025 struct url_spec compiled[1];
2035 orig_param = lookup(parameters, name);
2038 return JB_ERR_CGI_PARAMS;
2041 /* Copy and trim whitespace */
2042 param = strdup(orig_param);
2045 return JB_ERR_MEMORY;
2049 /* Must be non-empty, and can't allow 1st character to be '{' */
2050 if (param[0] == '\0' || param[0] == '{')
2053 return JB_ERR_CGI_PARAMS;
2056 /* Check for embedded newlines */
2057 for (s = param; *s != '\0'; s++)
2059 if ((*s == '\r') || (*s == '\n'))
2062 return JB_ERR_CGI_PARAMS;
2066 /* Check that regex is valid */
2071 return JB_ERR_MEMORY;
2073 err = create_url_spec(compiled, s);
2078 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2080 free_url_spec(compiled);
2082 if (param[strlen(param) - 1] == '\\')
2085 * Must protect trailing '\\' from becoming line continuation character.
2086 * Two methods: 1) If it's a domain only, add a trailing '/'.
2087 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2089 if (strchr(param, '/') == NULL)
2091 err = string_append(¶m, "/");
2095 err = string_append(¶m, "(?:)");
2102 /* Check that the modified regex is valid */
2107 return JB_ERR_MEMORY;
2109 err = create_url_spec(compiled, s);
2114 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2116 free_url_spec(compiled);
2123 /*********************************************************************
2125 * Function : map_radio
2127 * Description : Map a set of radio button values. E.g. if you have
2128 * 3 radio buttons, declare them as:
2129 * <option type="radio" name="xyz" @xyz-a@>
2130 * <option type="radio" name="xyz" @xyz-b@>
2131 * <option type="radio" name="xyz" @xyz-c@>
2132 * Then map one of the @xyz-?@ variables to "checked"
2133 * and all the others to empty by calling:
2134 * map_radio(exports, "xyz", "abc", sel)
2135 * Where 'sel' is 'a', 'b', or 'c'.
2138 * 1 : exports = Exports map to modify.
2139 * 2 : optionname = name for map
2140 * 3 : values = null-terminated list of values;
2141 * 4 : value = Selected value.
2143 * CGI Parameters : None
2145 * Returns : JB_ERR_OK on success
2146 * JB_ERR_MEMORY on out-of-memory
2148 *********************************************************************/
2149 static jb_err map_radio(struct map * exports,
2150 const char * optionname,
2151 const char * values,
2163 len = strlen(optionname);
2164 buf = malloc(len + 3);
2167 return JB_ERR_MEMORY;
2170 strcpy(buf, optionname);
2175 while ((c = *values++) != '\0')
2180 if (map(exports, buf, 1, "", 1))
2182 return JB_ERR_MEMORY;
2188 return map(exports, buf, 0, "checked", 1);
2192 /*********************************************************************
2194 * Function : cgi_error_modified
2196 * Description : CGI function that is called when a file is modified
2197 * outside the CGI editor.
2200 * 1 : csp = Current client state (buffers, headers, etc...)
2201 * 2 : rsp = http_response data structure for output
2202 * 3 : filename = The file that was modified.
2204 * CGI Parameters : none
2206 * Returns : JB_ERR_OK on success
2207 * JB_ERR_MEMORY on out-of-memory error.
2209 *********************************************************************/
2210 jb_err cgi_error_modified(struct client_state *csp,
2211 struct http_response *rsp,
2212 const char *filename)
2214 struct map *exports;
2221 if (NULL == (exports = default_exports(csp, NULL)))
2223 return JB_ERR_MEMORY;
2226 err = map(exports, "f", 1, html_encode(filename), 0);
2233 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2237 /*********************************************************************
2239 * Function : cgi_error_parse
2241 * Description : CGI function that is called when a file cannot
2242 * be parsed by the CGI editor.
2245 * 1 : csp = Current client state (buffers, headers, etc...)
2246 * 2 : rsp = http_response data structure for output
2247 * 3 : file = The file that was modified.
2249 * CGI Parameters : none
2251 * Returns : JB_ERR_OK on success
2252 * JB_ERR_MEMORY on out-of-memory error.
2254 *********************************************************************/
2255 jb_err cgi_error_parse(struct client_state *csp,
2256 struct http_response *rsp,
2257 struct editable_file *file)
2259 struct map *exports;
2261 struct file_line *cur_line;
2267 if (NULL == (exports = default_exports(csp, NULL)))
2269 return JB_ERR_MEMORY;
2272 err = map(exports, "f", 1, file->identifier, 1);
2273 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2275 cur_line = file->parse_error;
2278 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2279 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2287 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2291 /*********************************************************************
2293 * Function : cgi_error_file
2295 * Description : CGI function that is called when a file cannot be
2296 * opened by the CGI editor.
2299 * 1 : csp = Current client state (buffers, headers, etc...)
2300 * 2 : rsp = http_response data structure for output
2301 * 3 : filename = The file that was modified.
2303 * CGI Parameters : none
2305 * Returns : JB_ERR_OK on success
2306 * JB_ERR_MEMORY on out-of-memory error.
2308 *********************************************************************/
2309 jb_err cgi_error_file(struct client_state *csp,
2310 struct http_response *rsp,
2311 const char *filename)
2313 struct map *exports;
2320 if (NULL == (exports = default_exports(csp, NULL)))
2322 return JB_ERR_MEMORY;
2325 err = map(exports, "f", 1, html_encode(filename), 0);
2332 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2336 /*********************************************************************
2338 * Function : cgi_error_file
2340 * Description : CGI function that is called when a file cannot be
2341 * opened for writing by the CGI editor.
2344 * 1 : csp = Current client state (buffers, headers, etc...)
2345 * 2 : rsp = http_response data structure for output
2346 * 3 : filename = The file that we can't write to
2348 * CGI Parameters : none
2350 * Returns : JB_ERR_OK on success
2351 * JB_ERR_MEMORY on out-of-memory error.
2353 *********************************************************************/
2354 jb_err cgi_error_file_read_only(struct client_state *csp,
2355 struct http_response *rsp,
2356 const char *filename)
2358 struct map *exports;
2365 if (NULL == (exports = default_exports(csp, NULL)))
2367 return JB_ERR_MEMORY;
2370 err = map(exports, "f", 1, html_encode(filename), 0);
2377 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2381 /*********************************************************************
2383 * Function : cgi_error_disabled
2385 * Description : CGI function that is called if the actions editor
2386 * is called although it's disabled in config
2389 * 1 : csp = Current client state (buffers, headers, etc...)
2390 * 2 : rsp = http_response data structure for output
2392 * CGI Parameters : none
2394 * Returns : JB_ERR_OK on success
2395 * JB_ERR_MEMORY on out-of-memory error.
2397 *********************************************************************/
2398 jb_err cgi_error_disabled(struct client_state *csp,
2399 struct http_response *rsp)
2401 struct map *exports;
2406 if (NULL == (exports = default_exports(csp, NULL)))
2408 return JB_ERR_MEMORY;
2411 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2415 /*********************************************************************
2417 * Function : cgi_edit_actions
2419 * Description : CGI function that allows the user to choose which
2420 * actions file to edit.
2423 * 1 : csp = Current client state (buffers, headers, etc...)
2424 * 2 : rsp = http_response data structure for output
2425 * 3 : parameters = map of cgi parameters
2427 * CGI Parameters : None
2429 * Returns : JB_ERR_OK on success
2430 * JB_ERR_MEMORY on out-of-memory error
2432 *********************************************************************/
2433 jb_err cgi_edit_actions(struct client_state *csp,
2434 struct http_response *rsp,
2435 const struct map *parameters)
2438 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2440 return cgi_error_disabled(csp, rsp);
2443 /* FIXME: Incomplete */
2444 rsp->status = strdup("302 Local Redirect from Privoxy");
2445 if (rsp->status == NULL)
2447 return JB_ERR_MEMORY;
2449 if (enlist_unique_header(rsp->headers, "Location",
2450 CGI_PREFIX "edit-actions-list?f=default"))
2454 return JB_ERR_MEMORY;
2461 /*********************************************************************
2463 * Function : cgi_edit_actions_list
2465 * Description : CGI function that edits the actions list.
2466 * FIXME: This function shouldn't FATAL ever.
2467 * FIXME: This function doesn't check the retval of map()
2469 * 1 : csp = Current client state (buffers, headers, etc...)
2470 * 2 : rsp = http_response data structure for output
2471 * 3 : parameters = map of cgi parameters
2473 * CGI Parameters : filename
2475 * Returns : JB_ERR_OK on success
2476 * JB_ERR_MEMORY on out-of-memory
2477 * JB_ERR_FILE if the file cannot be opened or
2479 * JB_ERR_CGI_PARAMS if "filename" was not specified
2482 *********************************************************************/
2483 jb_err cgi_edit_actions_list(struct client_state *csp,
2484 struct http_response *rsp,
2485 const struct map *parameters)
2487 char * section_template;
2488 char * url_template;
2493 struct map * exports;
2494 struct map * section_exports;
2495 struct map * url_exports;
2496 struct editable_file * file;
2497 struct file_line * cur_line;
2498 unsigned line_number = 0;
2499 unsigned prev_section_line_number = ((unsigned) (-1));
2501 struct file_list * fl;
2502 struct url_actions * b;
2503 char * buttons = NULL;
2506 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2508 return cgi_error_disabled(csp, rsp);
2511 if (NULL == (exports = default_exports(csp, NULL)))
2513 edit_free_file(file);
2514 return JB_ERR_MEMORY;
2517 /* Load actions file */
2518 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2521 /* No filename specified, can't read file, or out of memory. */
2522 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2525 /* Find start of actions in file */
2526 cur_line = file->lines;
2528 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2530 cur_line = cur_line->next;
2535 * Conventional actions files should have a match all block
2537 * cur_line = {...global actions...}
2538 * cur_line->next = /
2539 * cur_line->next->next = {...actions...} or EOF
2541 if ( (cur_line != NULL)
2542 && (cur_line->type == FILE_LINE_ACTION)
2543 && (cur_line->next != NULL)
2544 && (cur_line->next->type == FILE_LINE_URL)
2545 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2546 && ( (cur_line->next->next == NULL)
2547 || (cur_line->next->next->type != FILE_LINE_URL)
2551 * Generate string with buttons to set actions for "/" to
2552 * any predefined set of actions (named standard.*, probably
2553 * residing in standard.action).
2556 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2559 edit_free_file(file);
2561 if (err == JB_ERR_FILE)
2563 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2568 err = template_fill(§ion_template, exports);
2571 edit_free_file(file);
2576 buttons = strdup("");
2577 for (i = 0; i < MAX_ACTION_FILES; i++)
2579 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2581 for (b = b->next; NULL != b; b = b->next)
2583 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2585 if (err || (NULL == (section_exports = new_map())))
2588 free(section_template);
2589 edit_free_file(file);
2591 return JB_ERR_MEMORY;
2594 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2596 if (err || (NULL == (s = strdup(section_template))))
2598 free_map(section_exports);
2600 free(section_template);
2601 edit_free_file(file);
2603 return JB_ERR_MEMORY;
2606 if (!err) err = template_fill(&s, section_exports);
2607 free_map(section_exports);
2608 if (!err) err = string_join(&buttons, s);
2613 freez(section_template);
2614 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2617 * Conventional actions file, supply extra editing help.
2618 * (e.g. don't allow them to make it an unconventional one).
2620 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2622 snprintf(buf, 150, "%d", line_number);
2623 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2624 snprintf(buf, 150, "%d", line_number + 2);
2625 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2626 if (!err) err = map(exports, "all-urls-actions", 1,
2627 actions_to_html(csp, cur_line->data.action), 0);
2629 /* Skip the 2 lines */
2630 cur_line = cur_line->next->next;
2634 * Note that prev_section_line_number is NOT set here.
2635 * This is deliberate and not a bug. It stops a "Move up"
2636 * option appearing on the next section. Clicking "Move
2637 * up" would make the actions file unconventional, which
2638 * we don't want, so we hide this option.
2644 * Non-standard actions file - does not begin with
2645 * the "All URLs" section.
2647 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2650 /* Set up global exports */
2652 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2653 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2655 /* Discourage private additions to default.action */
2657 if (!err) err = map_conditional(exports, "default-action",
2658 (strcmp("default", lookup(parameters, "f")) == 0));
2661 edit_free_file(file);
2666 /* Should do all global exports above this point */
2668 /* Load templates */
2670 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2673 edit_free_file(file);
2675 if (err == JB_ERR_FILE)
2677 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2682 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2685 free(section_template);
2686 edit_free_file(file);
2688 if (err == JB_ERR_FILE)
2690 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2695 err = template_fill(§ion_template, exports);
2699 edit_free_file(file);
2705 err = template_fill(&url_template, exports);
2708 free(section_template);
2709 edit_free_file(file);
2714 if (NULL == (sections = strdup("")))
2716 free(section_template);
2718 edit_free_file(file);
2720 return JB_ERR_MEMORY;
2723 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2725 if (NULL == (section_exports = new_map()))
2728 free(section_template);
2730 edit_free_file(file);
2732 return JB_ERR_MEMORY;
2735 snprintf(buf, 150, "%d", line_number);
2736 err = map(section_exports, "s", 1, buf, 1);
2737 if (!err) err = map(section_exports, "actions", 1,
2738 actions_to_html(csp, cur_line->data.action), 0);
2741 && (cur_line->next != NULL)
2742 && (cur_line->next->type == FILE_LINE_URL))
2744 /* This section contains at least one URL, don't allow delete */
2745 err = map_block_killer(section_exports, "empty-section");
2749 if (!err) err = map_block_keep(section_exports, "empty-section");
2752 if (prev_section_line_number != ((unsigned)(-1)))
2754 /* Not last section */
2755 snprintf(buf, 150, "%d", prev_section_line_number);
2756 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2757 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2762 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2764 prev_section_line_number = line_number;
2769 free(section_template);
2771 edit_free_file(file);
2773 free_map(section_exports);
2777 /* Should do all section-specific exports above this point */
2779 if (NULL == (urls = strdup("")))
2782 free(section_template);
2784 edit_free_file(file);
2786 free_map(section_exports);
2787 return JB_ERR_MEMORY;
2792 cur_line = cur_line->next;
2795 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2797 if (NULL == (url_exports = new_map()))
2801 free(section_template);
2803 edit_free_file(file);
2805 free_map(section_exports);
2806 return JB_ERR_MEMORY;
2809 snprintf(buf, 150, "%d", line_number);
2810 err = map(url_exports, "p", 1, buf, 1);
2812 snprintf(buf, 150, "%d", url_1_2);
2813 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2815 if (!err) err = map(url_exports, "url-html", 1,
2816 html_encode(cur_line->unprocessed), 0);
2817 if (!err) err = map(url_exports, "url", 1,
2818 url_encode(cur_line->unprocessed), 0);
2824 free(section_template);
2826 edit_free_file(file);
2828 free_map(section_exports);
2829 free_map(url_exports);
2833 if (NULL == (s = strdup(url_template)))
2837 free(section_template);
2839 edit_free_file(file);
2841 free_map(section_exports);
2842 free_map(url_exports);
2843 return JB_ERR_MEMORY;
2846 err = template_fill(&s, section_exports);
2847 if (!err) err = template_fill(&s, url_exports);
2848 if (!err) err = string_append(&urls, s);
2850 free_map(url_exports);
2857 free(section_template);
2859 edit_free_file(file);
2861 free_map(section_exports);
2865 url_1_2 = 3 - url_1_2;
2867 cur_line = cur_line->next;
2871 err = map(section_exports, "urls", 1, urls, 0);
2873 /* Could also do section-specific exports here, but it wouldn't be as fast */
2875 if ( (cur_line != NULL)
2876 && (cur_line->type == FILE_LINE_ACTION))
2878 /* Not last section */
2879 snprintf(buf, 150, "%d", line_number);
2880 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2881 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2886 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2892 free(section_template);
2894 edit_free_file(file);
2896 free_map(section_exports);
2900 if (NULL == (s = strdup(section_template)))
2903 free(section_template);
2905 edit_free_file(file);
2907 free_map(section_exports);
2908 return JB_ERR_MEMORY;
2911 err = template_fill(&s, section_exports);
2912 if (!err) err = string_append(§ions, s);
2915 free_map(section_exports);
2920 free(section_template);
2922 edit_free_file(file);
2928 edit_free_file(file);
2929 free(section_template);
2932 err = map(exports, "sections", 1, sections, 0);
2939 /* Could also do global exports here, but it wouldn't be as fast */
2941 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2945 /*********************************************************************
2947 * Function : cgi_edit_actions_for_url
2949 * Description : CGI function that edits the Actions list.
2952 * 1 : csp = Current client state (buffers, headers, etc...)
2953 * 2 : rsp = http_response data structure for output
2954 * 3 : parameters = map of cgi parameters
2956 * CGI Parameters : None
2958 * Returns : JB_ERR_OK on success
2959 * JB_ERR_MEMORY on out-of-memory
2960 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2961 * specified or not valid.
2963 *********************************************************************/
2964 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2965 struct http_response *rsp,
2966 const struct map *parameters)
2968 struct map * exports;
2970 struct editable_file * file;
2971 struct file_line * cur_line;
2972 unsigned line_number;
2974 struct file_list *filter_file;
2975 struct re_filterfile_spec *filter_group;
2977 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2979 return cgi_error_disabled(csp, rsp);
2982 err = get_number_param(csp, parameters, "s", §ionid);
2988 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2991 /* No filename specified, can't read file, modified, or out of memory. */
2992 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2995 cur_line = file->lines;
2997 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2999 cur_line = cur_line->next;
3002 if ( (cur_line == NULL)
3003 || (line_number != sectionid)
3005 || (cur_line->type != FILE_LINE_ACTION))
3007 /* Invalid "sectionid" parameter */
3008 edit_free_file(file);
3009 return JB_ERR_CGI_PARAMS;
3012 if (NULL == (exports = default_exports(csp, NULL)))
3014 edit_free_file(file);
3015 return JB_ERR_MEMORY;
3018 err = map(exports, "f", 1, file->identifier, 1);
3019 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3020 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3022 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3024 filter_file = csp->rlist;
3025 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
3027 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
3031 edit_free_file(file);
3036 if (filter_group == NULL)
3038 err = map(exports, "filter-params", 1, "", 1);
3042 /* We have some entries in the filter list */
3045 char * filter_template;
3047 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3050 edit_free_file(file);
3052 if (err == JB_ERR_FILE)
3054 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3059 err = template_fill(&filter_template, exports);
3061 result = strdup("");
3063 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3065 char current_mode = 'x';
3066 struct list_entry *filter_name;
3068 struct map *line_exports;
3071 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
3072 while ((filter_name != NULL)
3073 && (0 != strcmp(filter_group->name, filter_name->str)))
3075 filter_name = filter_name->next;
3078 if (filter_name != NULL)
3084 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
3085 while ((filter_name != NULL)
3086 && (0 != strcmp(filter_group->name, filter_name->str)))
3088 filter_name = filter_name->next;
3090 if (filter_name != NULL)
3096 /* Generate a unique serial number */
3097 snprintf(number, sizeof(number), "%x", index++);
3098 number[sizeof(number) - 1] = '\0';
3100 line_exports = new_map();
3101 if (line_exports == NULL)
3103 err = JB_ERR_MEMORY;
3108 if (!err) err = map(line_exports, "index", 1, number, 1);
3109 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3110 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3111 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3116 this_line = strdup(filter_template);
3117 if (this_line == NULL) err = JB_ERR_MEMORY;
3119 if (!err) err = template_fill(&this_line, line_exports);
3120 string_join(&result, this_line);
3122 free_map(line_exports);
3126 freez(filter_template);
3130 err = map(exports, "filter-params", 1, result, 0);
3138 if (!err) err = map_radio(exports, "filter-all", "nx",
3139 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3141 edit_free_file(file);
3149 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3153 /*********************************************************************
3155 * Function : cgi_edit_actions_submit
3157 * Description : CGI function that actually edits the Actions list.
3160 * 1 : csp = Current client state (buffers, headers, etc...)
3161 * 2 : rsp = http_response data structure for output
3162 * 3 : parameters = map of cgi parameters
3164 * CGI Parameters : None
3166 * Returns : JB_ERR_OK on success
3167 * JB_ERR_MEMORY on out-of-memory
3168 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3169 * specified or not valid.
3171 *********************************************************************/
3172 jb_err cgi_edit_actions_submit(struct client_state *csp,
3173 struct http_response *rsp,
3174 const struct map *parameters)
3180 struct editable_file * file;
3181 struct file_line * cur_line;
3182 unsigned line_number;
3186 const char * action_set_name;
3188 struct file_list * fl;
3189 struct url_actions * b;
3191 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3193 return cgi_error_disabled(csp, rsp);
3196 err = get_number_param(csp, parameters, "s", §ionid);
3202 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3205 /* No filename specified, can't read file, modified, or out of memory. */
3206 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3209 cur_line = file->lines;
3211 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3213 cur_line = cur_line->next;
3216 if ( (cur_line == NULL)
3217 || (line_number != sectionid)
3219 || (cur_line->type != FILE_LINE_ACTION))
3221 /* Invalid "sectionid" parameter */
3222 edit_free_file(file);
3223 return JB_ERR_CGI_PARAMS;
3226 get_string_param(parameters, "p", &action_set_name);
3227 if (action_set_name != NULL)
3229 for (index = 0; index < MAX_ACTION_FILES; index++)
3231 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3233 for (b = b->next; NULL != b; b = b->next)
3235 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3237 copy_action(cur_line->data.action, b->action);
3243 edit_free_file(file);
3244 return JB_ERR_CGI_PARAMS;
3250 err = actions_from_radio(parameters, cur_line->data.action);
3256 edit_free_file(file);
3260 ch = get_char_param(parameters, "filter_all");
3263 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3264 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3265 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3269 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3272 for (index = 0; !err; index++)
3279 /* Generate the keys */
3280 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3281 key_value[sizeof(key_value) - 1] = '\0';
3282 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3283 key_name[sizeof(key_name) - 1] = '\0';
3285 err = get_string_param(parameters, key_name, &name);
3294 value = get_char_param(parameters, key_value);
3297 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3298 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3299 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3301 else if (value == 'N')
3303 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3304 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3306 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3307 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3310 else if (value == 'X')
3312 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3313 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3320 edit_free_file(file);
3324 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3327 edit_free_file(file);
3328 return JB_ERR_MEMORY;
3331 len = strlen(actiontext);
3335 * Empty action - must special-case this.
3336 * Simply setting len to 1 is sufficient...
3341 if (NULL == (newtext = malloc(len + 2)))
3345 edit_free_file(file);
3346 return JB_ERR_MEMORY;
3348 strcpy(newtext, actiontext);
3352 newtext[len + 1] = '\0';
3354 freez(cur_line->raw);
3355 freez(cur_line->unprocessed);
3356 cur_line->unprocessed = newtext;
3358 err = edit_write_file(file);
3361 /* Error writing file */
3362 if (err == JB_ERR_FILE)
3364 /* Read-only file. */
3365 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3367 edit_free_file(file);
3371 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3372 string_append(&target, file->identifier);
3373 string_join(&target, section_target(sectionid));
3376 edit_free_file(file);
3381 return JB_ERR_MEMORY;
3384 rsp->status = strdup("302 Local Redirect from Privoxy");
3385 if (rsp->status == NULL)
3388 return JB_ERR_MEMORY;
3390 err = enlist_unique_header(rsp->headers, "Location", target);
3397 /*********************************************************************
3399 * Function : cgi_edit_actions_url
3401 * Description : CGI function that actually edits a URL pattern in
3405 * 1 : csp = Current client state (buffers, headers, etc...)
3406 * 2 : rsp = http_response data structure for output
3407 * 3 : parameters = map of cgi parameters
3410 * filename : Identifies the file to edit
3411 * ver : File's last-modified time
3412 * section : Line number of section to edit
3413 * pattern : Line number of pattern to edit
3414 * newval : New value for pattern
3416 * Returns : JB_ERR_OK on success
3417 * JB_ERR_MEMORY on out-of-memory
3418 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3419 * specified or not valid.
3421 *********************************************************************/
3422 jb_err cgi_edit_actions_url(struct client_state *csp,
3423 struct http_response *rsp,
3424 const struct map *parameters)
3428 struct editable_file * file;
3429 struct file_line * cur_line;
3430 unsigned line_number;
3431 unsigned section_start_line_number = 0;
3435 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3437 return cgi_error_disabled(csp, rsp);
3440 err = get_number_param(csp, parameters, "p", &patternid);
3447 return JB_ERR_CGI_PARAMS;
3450 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3456 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3459 /* No filename specified, can't read file, modified, or out of memory. */
3461 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3465 cur_line = file->lines;
3467 while ((cur_line != NULL) && (line_number < patternid))
3469 if (cur_line->type == FILE_LINE_ACTION)
3471 section_start_line_number = line_number;
3473 cur_line = cur_line->next;
3477 if ( (cur_line == NULL)
3478 || (cur_line->type != FILE_LINE_URL))
3480 /* Invalid "patternid" parameter */
3482 edit_free_file(file);
3483 return JB_ERR_CGI_PARAMS;
3486 /* At this point, the line to edit is in cur_line */
3488 freez(cur_line->raw);
3489 freez(cur_line->unprocessed);
3490 cur_line->unprocessed = new_pattern;
3492 err = edit_write_file(file);
3495 /* Error writing file */
3496 if (err == JB_ERR_FILE)
3498 /* Read-only file. */
3499 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3501 edit_free_file(file);
3505 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3506 string_append(&target, file->identifier);
3507 string_join(&target, section_target(section_start_line_number));
3509 edit_free_file(file);
3514 return JB_ERR_MEMORY;
3517 rsp->status = strdup("302 Local Redirect from Privoxy");
3518 if (rsp->status == NULL)
3521 return JB_ERR_MEMORY;
3523 err = enlist_unique_header(rsp->headers, "Location", target);
3530 /*********************************************************************
3532 * Function : cgi_edit_actions_add_url
3534 * Description : CGI function that actually adds a URL pattern to
3538 * 1 : csp = Current client state (buffers, headers, etc...)
3539 * 2 : rsp = http_response data structure for output
3540 * 3 : parameters = map of cgi parameters
3543 * filename : Identifies the file to edit
3544 * ver : File's last-modified time
3545 * section : Line number of section to edit
3546 * newval : New pattern
3548 * Returns : JB_ERR_OK on success
3549 * JB_ERR_MEMORY on out-of-memory
3550 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3551 * specified or not valid.
3553 *********************************************************************/
3554 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3555 struct http_response *rsp,
3556 const struct map *parameters)
3560 struct file_line * new_line;
3561 struct editable_file * file;
3562 struct file_line * cur_line;
3563 unsigned line_number;
3567 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3569 return cgi_error_disabled(csp, rsp);
3572 err = get_number_param(csp, parameters, "s", §ionid);
3579 return JB_ERR_CGI_PARAMS;
3582 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3588 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3591 /* No filename specified, can't read file, modified, or out of memory. */
3593 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3597 cur_line = file->lines;
3599 while ((cur_line != NULL) && (line_number < sectionid))
3601 cur_line = cur_line->next;
3605 if ( (cur_line == NULL)
3606 || (cur_line->type != FILE_LINE_ACTION))
3608 /* Invalid "sectionid" parameter */
3610 edit_free_file(file);
3611 return JB_ERR_CGI_PARAMS;
3614 /* At this point, the section header is in cur_line - add after this. */
3616 /* Allocate the new line */
3617 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3618 if (new_line == NULL)
3621 edit_free_file(file);
3622 return JB_ERR_MEMORY;
3625 /* Fill in the data members of the new line */
3626 new_line->raw = NULL;
3627 new_line->prefix = NULL;
3628 new_line->unprocessed = new_pattern;
3629 new_line->type = FILE_LINE_URL;
3631 /* Link new_line into the list, after cur_line */
3632 new_line->next = cur_line->next;
3633 cur_line->next = new_line;
3635 /* Done making changes, now commit */
3637 err = edit_write_file(file);
3640 /* Error writing file */
3641 if (err == JB_ERR_FILE)
3643 /* Read-only file. */
3644 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3646 edit_free_file(file);
3650 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3651 string_append(&target, file->identifier);
3652 string_join(&target, section_target(sectionid));
3654 edit_free_file(file);
3659 return JB_ERR_MEMORY;
3662 rsp->status = strdup("302 Local Redirect from Privoxy");
3663 if (rsp->status == NULL)
3666 return JB_ERR_MEMORY;
3668 err = enlist_unique_header(rsp->headers, "Location", target);
3675 /*********************************************************************
3677 * Function : cgi_edit_actions_remove_url
3679 * Description : CGI function that actually removes a URL pattern from
3683 * 1 : csp = Current client state (buffers, headers, etc...)
3684 * 2 : rsp = http_response data structure for output
3685 * 3 : parameters = map of cgi parameters
3688 * f : (filename) Identifies the file to edit
3689 * v : (version) File's last-modified time
3690 * p : (pattern) Line number of pattern to remove
3692 * Returns : JB_ERR_OK on success
3693 * JB_ERR_MEMORY on out-of-memory
3694 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3695 * specified or not valid.
3697 *********************************************************************/
3698 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3699 struct http_response *rsp,
3700 const struct map *parameters)
3703 struct editable_file * file;
3704 struct file_line * cur_line;
3705 struct file_line * prev_line;
3706 unsigned line_number;
3707 unsigned section_start_line_number = 0;
3711 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3713 return cgi_error_disabled(csp, rsp);
3716 err = get_number_param(csp, parameters, "p", &patternid);
3722 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3725 /* No filename specified, can't read file, modified, or out of memory. */
3726 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3731 cur_line = file->lines;
3733 while ((cur_line != NULL) && (line_number < patternid))
3735 if (cur_line->type == FILE_LINE_ACTION)
3737 section_start_line_number = line_number;
3739 prev_line = cur_line;
3740 cur_line = cur_line->next;
3744 if ( (cur_line == NULL)
3745 || (prev_line == NULL)
3746 || (cur_line->type != FILE_LINE_URL))
3748 /* Invalid "patternid" parameter */
3749 edit_free_file(file);
3750 return JB_ERR_CGI_PARAMS;
3753 /* At this point, the line to remove is in cur_line, and the previous
3754 * one is in prev_line
3757 /* Unlink cur_line */
3758 prev_line->next = cur_line->next;
3759 cur_line->next = NULL;
3762 edit_free_file_lines(cur_line);
3764 err = edit_write_file(file);
3767 /* Error writing file */
3768 if (err == JB_ERR_FILE)
3770 /* Read-only file. */
3771 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3773 edit_free_file(file);
3777 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3778 string_append(&target, file->identifier);
3779 string_join(&target, section_target(section_start_line_number));
3781 edit_free_file(file);
3786 return JB_ERR_MEMORY;
3789 rsp->status = strdup("302 Local Redirect from Privoxy");
3790 if (rsp->status == NULL)
3793 return JB_ERR_MEMORY;
3795 err = enlist_unique_header(rsp->headers, "Location", target);
3802 /*********************************************************************
3804 * Function : cgi_edit_actions_section_remove
3806 * Description : CGI function that actually removes a whole section from
3807 * the actions file. The section must be empty first
3808 * (else JB_ERR_CGI_PARAMS).
3811 * 1 : csp = Current client state (buffers, headers, etc...)
3812 * 2 : rsp = http_response data structure for output
3813 * 3 : parameters = map of cgi parameters
3816 * f : (filename) Identifies the file to edit
3817 * v : (version) File's last-modified time
3818 * s : (section) Line number of section to edit
3820 * Returns : JB_ERR_OK on success
3821 * JB_ERR_MEMORY on out-of-memory
3822 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3823 * specified or not valid.
3825 *********************************************************************/
3826 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3827 struct http_response *rsp,
3828 const struct map *parameters)
3831 struct editable_file * file;
3832 struct file_line * cur_line;
3833 struct file_line * prev_line;
3834 unsigned line_number;
3838 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3840 return cgi_error_disabled(csp, rsp);
3843 err = get_number_param(csp, parameters, "s", §ionid);
3849 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3852 /* No filename specified, can't read file, modified, or out of memory. */
3853 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3857 cur_line = file->lines;
3860 while ((cur_line != NULL) && (line_number < sectionid))
3862 prev_line = cur_line;
3863 cur_line = cur_line->next;
3867 if ( (cur_line == NULL)
3868 || (cur_line->type != FILE_LINE_ACTION) )
3870 /* Invalid "sectionid" parameter */
3871 edit_free_file(file);
3872 return JB_ERR_CGI_PARAMS;
3875 if ( (cur_line->next != NULL)
3876 && (cur_line->next->type == FILE_LINE_URL) )
3878 /* Section not empty. */
3879 edit_free_file(file);
3880 return JB_ERR_CGI_PARAMS;
3883 /* At this point, the line to remove is in cur_line, and the previous
3884 * one is in prev_line
3887 /* Unlink cur_line */
3888 if (prev_line == NULL)
3890 /* Removing the first line from the file */
3891 file->lines = cur_line->next;
3895 prev_line->next = cur_line->next;
3897 cur_line->next = NULL;
3900 edit_free_file_lines(cur_line);
3902 err = edit_write_file(file);
3905 /* Error writing file */
3906 if (err == JB_ERR_FILE)
3908 /* Read-only file. */
3909 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3911 edit_free_file(file);
3915 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3916 string_append(&target, file->identifier);
3918 edit_free_file(file);
3923 return JB_ERR_MEMORY;
3926 rsp->status = strdup("302 Local Redirect from Privoxy");
3927 if (rsp->status == NULL)
3930 return JB_ERR_MEMORY;
3932 err = enlist_unique_header(rsp->headers, "Location", target);
3939 /*********************************************************************
3941 * Function : cgi_edit_actions_section_add
3943 * Description : CGI function that adds a new empty section to
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 * s : (section) Line number of section to add after, 0 for
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_add(struct client_state *csp,
3964 struct http_response *rsp,
3965 const struct map *parameters)
3968 struct file_line * new_line;
3970 struct editable_file * file;
3971 struct file_line * cur_line;
3972 unsigned line_number;
3976 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3978 return cgi_error_disabled(csp, rsp);
3981 err = get_number_param(csp, parameters, "s", §ionid);
3987 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3990 /* No filename specified, can't read file, modified, or out of memory. */
3991 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3995 cur_line = file->lines;
3999 /* Add to start of file */
4000 if (cur_line != NULL)
4002 /* There's something in the file, find the line before the first
4005 while ( (cur_line->next != NULL)
4006 && (cur_line->next->type != FILE_LINE_ACTION) )
4008 cur_line = cur_line->next;
4015 /* Add after stated section. */
4016 while ((cur_line != NULL) && (line_number < sectionid))
4018 cur_line = cur_line->next;
4022 if ( (cur_line == NULL)
4023 || (cur_line->type != FILE_LINE_ACTION))
4025 /* Invalid "sectionid" parameter */
4026 edit_free_file(file);
4027 return JB_ERR_CGI_PARAMS;
4030 /* Skip through the section to find the last line in it. */
4031 while ( (cur_line->next != NULL)
4032 && (cur_line->next->type != FILE_LINE_ACTION) )
4034 cur_line = cur_line->next;
4039 /* At this point, the last line in the previous section is in cur_line
4040 * - add after this. (Or if we need to add as the first line, cur_line
4044 new_text = strdup("{}");
4045 if (NULL == new_text)
4047 edit_free_file(file);
4048 return JB_ERR_MEMORY;
4051 /* Allocate the new line */
4052 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4053 if (new_line == NULL)
4056 edit_free_file(file);
4057 return JB_ERR_MEMORY;
4060 /* Fill in the data members of the new line */
4061 new_line->raw = NULL;
4062 new_line->prefix = NULL;
4063 new_line->unprocessed = new_text;
4064 new_line->type = FILE_LINE_ACTION;
4066 if (cur_line != NULL)
4068 /* Link new_line into the list, after cur_line */
4069 new_line->next = cur_line->next;
4070 cur_line->next = new_line;
4074 /* Link new_line into the list, as first line */
4075 new_line->next = file->lines;
4076 file->lines = new_line;
4079 /* Done making changes, now commit */
4081 err = edit_write_file(file);
4084 /* Error writing file */
4085 if (err == JB_ERR_FILE)
4087 /* Read-only file. */
4088 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4090 edit_free_file(file);
4094 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4095 string_append(&target, file->identifier);
4097 edit_free_file(file);
4102 return JB_ERR_MEMORY;
4105 rsp->status = strdup("302 Local Redirect from Privoxy");
4106 if (rsp->status == NULL)
4109 return JB_ERR_MEMORY;
4111 err = enlist_unique_header(rsp->headers, "Location", target);
4118 /*********************************************************************
4120 * Function : cgi_edit_actions_section_swap
4122 * Description : CGI function that swaps the order of two sections
4123 * in the actions file. Note that this CGI can actually
4124 * swap any two arbitrary sections, but the GUI interface
4125 * currently only allows consecutive sections to be
4129 * 1 : csp = Current client state (buffers, headers, etc...)
4130 * 2 : rsp = http_response data structure for output
4131 * 3 : parameters = map of cgi parameters
4134 * f : (filename) Identifies the file to edit
4135 * v : (version) File's last-modified time
4136 * s1 : (section1) Line number of first section to swap
4137 * s2 : (section2) Line number of second section to swap
4139 * Returns : JB_ERR_OK on success
4140 * JB_ERR_MEMORY on out-of-memory
4141 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4142 * specified or not valid.
4144 *********************************************************************/
4145 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4146 struct http_response *rsp,
4147 const struct map *parameters)
4151 struct editable_file * file;
4152 struct file_line * cur_line;
4153 struct file_line * prev_line;
4154 struct file_line * line_before_section1;
4155 struct file_line * line_start_section1;
4156 struct file_line * line_end_section1;
4157 struct file_line * line_after_section1;
4158 struct file_line * line_before_section2;
4159 struct file_line * line_start_section2;
4160 struct file_line * line_end_section2;
4161 struct file_line * line_after_section2;
4162 unsigned line_number;
4166 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4168 return cgi_error_disabled(csp, rsp);
4171 err = get_number_param(csp, parameters, "s1", §ion1);
4172 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4178 if (section1 > section2)
4180 unsigned temp = section2;
4181 section2 = section1;
4185 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4188 /* No filename specified, can't read file, modified, or out of memory. */
4189 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4192 /* Start at the beginning... */
4194 cur_line = file->lines;
4197 /* ... find section1 ... */
4198 while ((cur_line != NULL) && (line_number < section1))
4200 prev_line = cur_line;
4201 cur_line = cur_line->next;
4205 if ( (cur_line == NULL)
4206 || (cur_line->type != FILE_LINE_ACTION) )
4208 /* Invalid "section1" parameter */
4209 edit_free_file(file);
4210 return JB_ERR_CGI_PARAMS;
4213 /* If no-op, we've validated params and can skip the rest. */
4214 if (section1 != section2)
4216 /* ... find the end of section1 ... */
4217 line_before_section1 = prev_line;
4218 line_start_section1 = cur_line;
4221 prev_line = cur_line;
4222 cur_line = cur_line->next;
4225 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4226 line_end_section1 = prev_line;
4227 line_after_section1 = cur_line;
4229 /* ... find section2 ... */
4230 while ((cur_line != NULL) && (line_number < section2))
4232 prev_line = cur_line;
4233 cur_line = cur_line->next;
4237 if ( (cur_line == NULL)
4238 || (cur_line->type != FILE_LINE_ACTION) )
4240 /* Invalid "section2" parameter */
4241 edit_free_file(file);
4242 return JB_ERR_CGI_PARAMS;
4245 /* ... find the end of section2 ... */
4246 line_before_section2 = prev_line;
4247 line_start_section2 = cur_line;
4250 prev_line = cur_line;
4251 cur_line = cur_line->next;
4254 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4255 line_end_section2 = prev_line;
4256 line_after_section2 = cur_line;
4258 /* Now have all the pointers we need. Do the swap. */
4260 /* Change the pointer to section1 to point to section2 instead */
4261 if (line_before_section1 == NULL)
4263 file->lines = line_start_section2;
4267 line_before_section1->next = line_start_section2;
4270 if (line_before_section2 == line_end_section1)
4272 /* Consecutive sections */
4273 line_end_section2->next = line_start_section1;
4277 line_end_section2->next = line_after_section1;
4278 line_before_section2->next = line_start_section1;
4281 /* Set the pointer from the end of section1 to the rest of the file */
4282 line_end_section1->next = line_after_section2;
4284 err = edit_write_file(file);
4287 /* Error writing file */
4288 if (err == JB_ERR_FILE)
4290 /* Read-only file. */
4291 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4293 edit_free_file(file);
4296 } /* END if (section1 != section2) */
4298 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4299 string_append(&target, file->identifier);
4301 edit_free_file(file);
4306 return JB_ERR_MEMORY;
4309 rsp->status = strdup("302 Local Redirect from Privoxy");
4310 if (rsp->status == NULL)
4313 return JB_ERR_MEMORY;
4315 err = enlist_unique_header(rsp->headers, "Location", target);
4322 /*********************************************************************
4324 * Function : cgi_toggle
4326 * Description : CGI function that adds a new empty section to
4330 * 1 : csp = Current client state (buffers, headers, etc...)
4331 * 2 : rsp = http_response data structure for output
4332 * 3 : parameters = map of cgi parameters
4335 * set : If present, how to change toggle setting:
4336 * "enable", "disable", "toggle", or none (default).
4337 * mini : If present, use mini reply template.
4339 * Returns : JB_ERR_OK on success
4340 * JB_ERR_MEMORY on out-of-memory
4342 *********************************************************************/
4343 jb_err cgi_toggle(struct client_state *csp,
4344 struct http_response *rsp,
4345 const struct map *parameters)
4347 struct map *exports;
4349 const char *template_name;
4355 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4357 return cgi_error_disabled(csp, rsp);
4360 mode = get_char_param(parameters, "set");
4367 else if (mode == 'D')
4372 else if (mode == 'T')
4375 g_bToggleIJB = !g_bToggleIJB;
4378 if (NULL == (exports = default_exports(csp, "toggle")))
4380 return JB_ERR_MEMORY;
4383 template_name = (get_char_param(parameters, "mini")
4387 return template_fill_for_cgi(csp, template_name, exports, rsp);
4391 /*********************************************************************
4393 * Function : javascriptify
4395 * Description : Converts a string into a form JavaScript will like.
4397 * Netscape 4's JavaScript sucks - it doesn't use
4398 * "id" parameters, so you have to set the "name"
4399 * used to submit a form element to something JavaScript
4400 * will like. (Or access the elements by index in an
4401 * array. That array contains >60 elements and will
4402 * be changed whenever we add a new action to the
4403 * editor, so I'm NOT going to use indexes that have
4404 * to be figured out by hand.)
4406 * Currently the only thing we have to worry about
4407 * is "-" ==> "_" conversion.
4409 * This is a length-preserving operation so it is
4410 * carried out in-place, no memory is allocated
4414 * 1 : identifier = String to make JavaScript-friendly.
4418 *********************************************************************/
4419 static void javascriptify(char * identifier)
4421 char * p = identifier;
4422 while (NULL != (p = strchr(p, '-')))
4429 /*********************************************************************
4431 * Function : actions_to_radio
4433 * Description : Converts a actionsfile entry into settings for
4434 * radio buttons and edit boxes on a HTML form.
4437 * 1 : exports = List of substitutions to add to.
4438 * 2 : action = Action to read
4440 * Returns : JB_ERR_OK on success
4441 * JB_ERR_MEMORY on out-of-memory
4443 *********************************************************************/
4444 static jb_err actions_to_radio(struct map * exports,
4445 const struct action_spec *action)
4447 unsigned mask = action->mask;
4448 unsigned add = action->add;
4456 mask = action->mask;
4459 /* sanity - prevents "-feature +feature" */
4463 #define DEFINE_ACTION_BOOL(name, bit) \
4464 if (!(mask & bit)) \
4466 current_mode = 'n'; \
4468 else if (add & bit) \
4470 current_mode = 'y'; \
4474 current_mode = 'x'; \
4476 if (map_radio(exports, name, "ynx", current_mode)) \
4478 return JB_ERR_MEMORY; \
4481 #define DEFINE_ACTION_STRING(name, bit, index) \
4482 DEFINE_ACTION_BOOL(name, bit); \
4485 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4488 checked = !strcmp(action->string[index], value); \
4492 checked = is_default; \
4494 mapped_param |= checked; \
4495 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4497 return JB_ERR_MEMORY; \
4500 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4501 if (map(exports, name "-param-custom", 1, \
4502 ((!mapped_param) ? "checked" : ""), 1)) \
4504 return JB_ERR_MEMORY; \
4506 if (map(exports, name "-param", 1, \
4507 (((add & bit) && !mapped_param) ? \
4508 action->string[index] : default_val), 1)) \
4510 return JB_ERR_MEMORY; \
4513 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4514 if (map(exports, name "-param", 1, \
4515 ((add & bit) ? action->string[index] : default_val), 1)) \
4517 return JB_ERR_MEMORY; \
4520 #define DEFINE_ACTION_MULTI(name, index) \
4521 if (action->multi_add[index]->first) \
4523 current_mode = 'y'; \
4525 else if (action->multi_remove_all[index]) \
4527 current_mode = 'n'; \
4529 else if (action->multi_remove[index]->first) \
4531 current_mode = 'y'; \
4535 current_mode = 'x'; \
4537 if (map_radio(exports, name, "ynx", current_mode)) \
4539 return JB_ERR_MEMORY; \
4542 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4544 #include "actionlist.h"
4546 #undef DEFINE_ACTION_MULTI
4547 #undef DEFINE_ACTION_STRING
4548 #undef DEFINE_ACTION_BOOL
4549 #undef DEFINE_ACTION_ALIAS
4550 #undef DEFINE_CGI_PARAM_CUSTOM
4551 #undef DEFINE_CGI_PARAM_RADIO
4552 #undef DEFINE_CGI_PARAM_NO_RADIO
4558 /*********************************************************************
4560 * Function : actions_from_radio
4562 * Description : Converts a map of parameters passed to a CGI function
4563 * into an actionsfile entry.
4566 * 1 : parameters = parameters to the CGI call
4567 * 2 : action = Action to change. Must be valid before
4568 * the call, actions not specified will be
4571 * Returns : JB_ERR_OK on success
4572 * JB_ERR_MEMORY on out-of-memory
4574 *********************************************************************/
4575 static jb_err actions_from_radio(const struct map * parameters,
4576 struct action_spec *action)
4578 static int first_time = 1;
4582 const char * js_name;
4583 jb_err err = JB_ERR_OK;
4588 /* Statics are generally a potential race condition,
4589 * but in this case we're safe and don't need semaphores.
4590 * Be careful if you modify this function.
4594 #define JAVASCRIPTIFY(dest_var, string) \
4596 static char js_name_arr[] = string; \
4599 javascriptify(js_name_arr); \
4601 dest_var = js_name_arr; \
4604 #define DEFINE_ACTION_BOOL(name, bit) \
4605 JAVASCRIPTIFY(js_name, name); \
4606 ch = get_char_param(parameters, js_name); \
4609 action->add |= bit; \
4610 action->mask |= bit; \
4612 else if (ch == 'N') \
4614 action->add &= ~bit; \
4615 action->mask &= ~bit; \
4617 else if (ch == 'X') \
4619 action->add &= ~bit; \
4620 action->mask |= bit; \
4623 #define DEFINE_ACTION_STRING(name, bit, index) \
4624 JAVASCRIPTIFY(js_name, name); \
4625 ch = get_char_param(parameters, js_name); \
4629 JAVASCRIPTIFY(js_name, name "-mode"); \
4630 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4631 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4633 JAVASCRIPTIFY(js_name, name "-param"); \
4634 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4636 if (param != NULL) \
4638 if (NULL == (param_dup = strdup(param))) \
4640 return JB_ERR_MEMORY; \
4642 freez(action->string[index]); \
4643 action->add |= bit; \
4644 action->mask |= bit; \
4645 action->string[index] = param_dup; \
4648 else if (ch == 'N') \
4650 if (action->add & bit) \
4652 freez(action->string[index]); \
4654 action->add &= ~bit; \
4655 action->mask &= ~bit; \
4657 else if (ch == 'X') \
4659 if (action->add & bit) \
4661 freez(action->string[index]); \
4663 action->add &= ~bit; \
4664 action->mask |= bit; \
4667 #define DEFINE_ACTION_MULTI(name, index) \
4668 JAVASCRIPTIFY(js_name, name); \
4669 ch = get_char_param(parameters, js_name); \
4674 else if (ch == 'N') \
4676 list_remove_all(action->multi_add[index]); \
4677 list_remove_all(action->multi_remove[index]); \
4678 action->multi_remove_all[index] = 1; \
4680 else if (ch == 'X') \
4682 list_remove_all(action->multi_add[index]); \
4683 list_remove_all(action->multi_remove[index]); \
4684 action->multi_remove_all[index] = 0; \
4687 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4689 #include "actionlist.h"
4691 #undef DEFINE_ACTION_MULTI
4692 #undef DEFINE_ACTION_STRING
4693 #undef DEFINE_ACTION_BOOL
4694 #undef DEFINE_ACTION_ALIAS
4695 #undef JAVASCRIPTIFY
4703 #endif /* def FEATURE_CGI_EDIT_ACTIONS */