1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.41.2.3 2002/11/12 15:01:41 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/Attic/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.41.2.3 2002/11/12 15:01:41 oes
46 * Fix: Don't free uninitialized struct editable_file
48 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
49 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
51 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
52 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
54 * Revision 1.41 2002/05/21 19:09:45 oes
55 * - Made Add/Edit/Remove URL Submit and Cancel
56 * buttons jump back to relevant section in eal
57 * - Bugfix: remove-url-form needs p export
59 * Revision 1.40 2002/05/19 11:34:35 jongfoster
60 * Handling read-only actions files better - report the actual
61 * error, not "Out of memory"!
64 * http://sourceforge.net/tracker/index.php?func=detail
65 * &aid=557905&group_id=11118&atid=111118
67 * Revision 1.39 2002/05/12 21:39:15 jongfoster
68 * - Adding Doxygen-style comments to structures and #defines.
69 * - Correcting function comments
71 * Revision 1.38 2002/05/03 23:00:38 jongfoster
72 * Support for templates for "standard actions" buttons.
75 * Revision 1.37 2002/04/30 11:14:52 oes
76 * Made csp the first parameter in *action_to_html
78 * Revision 1.36 2002/04/26 21:53:30 jongfoster
79 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
81 * Revision 1.35 2002/04/26 21:50:02 jongfoster
82 * Honouring default exports in edit-actions-for-url-filter template.
84 * Revision 1.34 2002/04/26 12:54:17 oes
85 * Adaptions to changes in actions.c
87 * Revision 1.33 2002/04/24 02:17:47 oes
88 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
90 * - Activated Jon's code for editing multiple AFs
91 * - cgi_edit_list_actions now provides context-sensitive
92 * help, looks up all action sets from standard.action and
93 * makes buttons for them in the catchall section
94 * - cgi_edit_action_submit now honors a p parameter, looks up
95 * the corresponding action set, and sets the catchall pattern's
96 * actions accordingly.
98 * Revision 1.32 2002/04/19 16:55:31 jongfoster
99 * Fixing newline problems. If we do our own text file newline
100 * mangling, we don't want the library to do any, so we need to
101 * open the files in *binary* mode.
103 * Revision 1.31 2002/04/18 19:21:08 jongfoster
104 * Added code to detect "conventional" action files, that start
105 * with a set of actions for all URLs (the pattern "/").
106 * These are special-cased in the "edit-actions-list" CGI, so
107 * that a special UI can be written for them.
109 * Revision 1.30 2002/04/10 13:38:35 oes
110 * load_template signature changed
112 * Revision 1.29 2002/04/08 16:59:08 oes
115 * Revision 1.28 2002/03/27 12:30:29 oes
116 * Deleted unsused variable
118 * Revision 1.27 2002/03/26 23:06:04 jongfoster
119 * Removing duplicate @ifs on the toggle page
121 * Revision 1.26 2002/03/26 22:59:17 jongfoster
122 * Fixing /toggle to display status consistently.
124 * Revision 1.25 2002/03/26 22:29:54 swa
125 * we have a new homepage!
127 * Revision 1.24 2002/03/24 15:23:33 jongfoster
130 * Revision 1.23 2002/03/24 13:32:41 swa
131 * name change related issues
133 * Revision 1.22 2002/03/24 13:25:43 swa
134 * name change related issues
136 * Revision 1.21 2002/03/22 18:02:48 jongfoster
137 * Fixing remote toggle
139 * Revision 1.20 2002/03/16 20:28:34 oes
140 * Added descriptions to the filters so users will know what they select in the cgi editor
142 * Revision 1.19 2002/03/16 18:38:14 jongfoster
143 * Stopping stupid or malicious users from breaking the actions
144 * file using the web-based editor.
146 * Revision 1.18 2002/03/16 14:57:44 jongfoster
147 * Full support for enabling/disabling modular filters.
149 * Revision 1.17 2002/03/16 14:26:42 jongfoster
150 * First version of modular filters support - READ ONLY!
151 * Fixing a double-free bug in the out-of-memory handling in map_radio().
153 * Revision 1.16 2002/03/07 03:46:17 oes
154 * Fixed compiler warnings
156 * Revision 1.15 2002/03/06 22:54:35 jongfoster
157 * Automated function-comment nitpicking.
159 * Revision 1.14 2002/03/05 00:24:51 jongfoster
160 * Patch to always edit the current actions file.
162 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
163 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
165 * Revision 1.12 2002/03/03 09:18:03 joergs
166 * Made jumbjuster work on AmigaOS again.
168 * Revision 1.11 2002/01/23 01:03:31 jongfoster
169 * Fixing gcc [CygWin] compiler warnings
171 * Revision 1.10 2002/01/23 00:22:59 jongfoster
172 * Adding new function cgi_edit_actions_section_swap(), to reorder
175 * Adding get_url_spec_param() to get a validated URL pattern.
177 * Moving edit_read_line() out of this file and into loaders.c.
179 * Adding missing html_encode() to many CGI functions.
181 * Moving the functions that #include actionlist.h to the end of the file,
182 * because the Visual C++ 97 debugger gets extremely confused if you try
183 * to debug any code that comes after them in the file.
185 * Major optimizations in cgi_edit_actions_list() to reduce the size of
186 * the generated HTML (down 40% from 550k to 304k), with major side-effects
187 * throughout the editor and templates. In particular, the length of the
188 * URLs throughout the editor has been drastically reduced, by cutting
189 * paramater names down to 1 character and CGI names down to 3-4
190 * characters, by removing all non-essential CGI paramaters even at the
191 * expense of having to re-read the actions file for the most trivial
192 * page, and by using relative rather than absolute URLs. This means
193 * that this (typical example):
195 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
196 * filename=ijb&ver=1011487572&section=12&pattern=13
197 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
201 * <a href="eau?f=ijb&v=1011487572&p=13">
203 * Revision 1.9 2002/01/17 20:56:22 jongfoster
204 * Replacing hard references to the URL of the config interface
205 * with #defines from project.h
207 * Revision 1.8 2001/11/30 23:35:51 jongfoster
208 * Renaming actionsfile to ijb.action
210 * Revision 1.7 2001/11/13 00:28:24 jongfoster
211 * - Renaming parameters from edit-actions-for-url so that they only
212 * contain legal JavaScript characters. If we wanted to write
213 * JavaScript that worked with Netscape 4, this is nessacery.
214 * (Note that at the moment the JavaScript doesn't actually work
215 * with Netscape 4, but now this is purely a template issue, not
216 * one affecting code).
217 * - Adding new CGIs for use by non-JavaScript browsers:
218 * edit-actions-url-form
219 * edit-actions-add-url-form
220 * edit-actions-remove-url-form
223 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
224 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
225 * by and __OS2__ ifdef.
227 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
228 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
229 * threads to call select() simultaneously. So, it's time to do a real, live,
230 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
231 * (native). Both versions will work, but using __OS2__ offers multi-threading.
233 * Revision 1.4 2001/10/23 21:48:19 jongfoster
234 * Cleaning up error handling in CGI functions - they now send back
235 * a HTML error page and should never cause a FATAL error. (Fixes one
236 * potential source of "denial of service" attacks).
238 * CGI actions file editor that works and is actually useful.
240 * Ability to toggle JunkBuster remotely using a CGI call.
242 * You can turn off both the above features in the main configuration
243 * file, e.g. if you are running a multi-user proxy.
245 * Revision 1.3 2001/10/14 22:12:49 jongfoster
246 * New version of CGI-based actionsfile editor.
247 * Major changes, including:
248 * - Completely new file parser and file output routines
249 * - edit-actions CGI renamed edit-actions-for-url
250 * - All CGIs now need a filename parameter, except for...
251 * - New CGI edit-actions which doesn't need a filename,
252 * to allow you to start the editor up.
253 * - edit-actions-submit now works, and now automatically
254 * redirects you back to the main edit-actions-list handler.
256 * Revision 1.2 2001/09/16 17:05:14 jongfoster
257 * Removing unused #include showarg.h
259 * Revision 1.1 2001/09/16 15:47:37 jongfoster
260 * First version of CGI-based edit interface. This is very much a
261 * work-in-progress, and you can't actually use it to edit anything
262 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
263 * to have any effect.
266 **********************************************************************/
272 * FIXME: Following includes copied from cgi.c - which are actually needed?
277 #include <sys/types.h>
281 #include <sys/stat.h>
284 #define snprintf _snprintf
285 #endif /* def _WIN32 */
290 #include "cgisimple.h"
294 #include "miscutil.h"
298 /* loadcfg.h is for global_toggle_state only */
299 #include "urlmatch.h"
301 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
304 #ifdef FEATURE_CGI_EDIT_ACTIONS
307 * A line in an editable_file.
311 /** Next entry in the linked list */
312 struct file_line * next;
314 /** The raw data, to write out if this line is unmodified. */
317 /** Comments and/or whitespace to put before this line if it's modified
318 and then written out. */
321 /** The actual data, as a string. Line continuation and comment removal
322 are performed on the data read from file before it's stored here, so
323 it will be a single line of data. */
326 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
329 /** The actual data, processed into some sensible data type. */
333 /** An action specification. */
334 struct action_spec action[1];
336 /** A name=value pair. */
340 /** The name in the name=value pair. */
343 /** The value in the name=value pair, as a string. */
346 /** The value in the name=value pair, as an integer. */
351 /* Add more data types here... e.g.
354 struct url_spec url[1];
358 struct action_spec action[1];
368 /** This file_line has not been processed yet. */
369 #define FILE_LINE_UNPROCESSED 1
371 /** This file_line is blank. Can only appear at the end of a file, due to
372 the way the parser works. */
373 #define FILE_LINE_BLANK 2
375 /** This file_line says {{alias}}. */
376 #define FILE_LINE_ALIAS_HEADER 3
378 /** This file_line defines an alias. */
379 #define FILE_LINE_ALIAS_ENTRY 4
381 /** This file_line defines an {action}. */
382 #define FILE_LINE_ACTION 5
384 /** This file_line specifies a URL pattern. */
385 #define FILE_LINE_URL 6
387 /** This file_line says {{settings}}. */
388 #define FILE_LINE_SETTINGS_HEADER 7
390 /** This file_line is in a {{settings}} block. */
391 #define FILE_LINE_SETTINGS_ENTRY 8
393 /** This file_line says {{description}}. */
394 #define FILE_LINE_DESCRIPTION_HEADER 9
396 /** This file_line is in a {{description}} block. */
397 #define FILE_LINE_DESCRIPTION_ENTRY 10
401 * A configuration file, in a format that can be edited and written back to
406 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
407 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
408 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
409 /**< Pre-encoded with url_encode() for ease of use. */
410 const char * version_str; /**< Last modification time, as a string. For CGI param. */
411 /**< Can be used in URL without using url_param(). */
412 unsigned version; /**< Last modification time - prevents chaos with
413 the browser's "back" button. Note that this is a
414 time_t cast to an unsigned. When comparing, always
415 cast the time_t to an unsigned, and *NOT* vice-versa.
416 This may lose the top few bits, but they're not
417 significant anyway. */
418 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
419 Note that changing this after the file has been
420 read in will cause a mess. */
421 struct file_line * parse_error; /**< On parse error, this is the offending line. */
422 const char * parse_error_text; /**< On parse error, this is the problem.
423 (Statically allocated) */
426 /* FIXME: Following non-static functions should be prototyped in .h or made static */
428 /* Functions to read and write arbitrary config files */
429 jb_err edit_read_file(struct client_state *csp,
430 const struct map *parameters,
433 struct editable_file **pfile);
434 jb_err edit_write_file(struct editable_file * file);
435 void edit_free_file(struct editable_file * file);
437 /* Functions to read and write actions files */
438 jb_err edit_parse_actions_file(struct editable_file * file);
439 jb_err edit_read_actions_file(struct client_state *csp,
440 struct http_response *rsp,
441 const struct map *parameters,
443 struct editable_file **pfile);
446 jb_err cgi_error_modified(struct client_state *csp,
447 struct http_response *rsp,
448 const char *filename);
449 jb_err cgi_error_parse(struct client_state *csp,
450 struct http_response *rsp,
451 struct editable_file *file);
452 jb_err cgi_error_file(struct client_state *csp,
453 struct http_response *rsp,
454 const char *filename);
455 jb_err cgi_error_file_read_only(struct client_state *csp,
456 struct http_response *rsp,
457 const char *filename);
458 jb_err cgi_error_disabled(struct client_state *csp,
459 struct http_response *rsp);
461 /* Internal arbitrary config file support functions */
462 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
463 static void edit_free_file_lines(struct file_line * first_line);
465 /* Internal actions file support functions */
466 static int match_actions_file_header_line(const char * line, const char * name);
467 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
469 /* Internal parameter parsing functions */
470 static jb_err get_file_name_param(struct client_state *csp,
471 const struct map *parameters,
472 const char *param_name,
475 const char **pparam);
477 static jb_err get_url_spec_param(struct client_state *csp,
478 const struct map *parameters,
483 /* Internal actionsfile <==> HTML conversion functions */
484 static jb_err map_radio(struct map * exports,
485 const char * optionname,
488 static jb_err actions_to_radio(struct map * exports,
489 const struct action_spec *action);
490 static jb_err actions_from_radio(const struct map * parameters,
491 struct action_spec *action);
494 static jb_err map_copy_parameter_html(struct map *out,
495 const struct map *in,
497 #if 0 /* unused function */
498 static jb_err map_copy_parameter_url(struct map *out,
499 const struct map *in,
501 #endif /* unused function */
503 /* Internal convenience functions */
504 static char *section_target(const unsigned sectionid);
506 /*********************************************************************
508 * Function : section_target
510 * Description : Given an unsigned (section id) n, produce a dynamically
511 * allocated string of the form #l<n>, for use in link
515 * 1 : sectionid = start line number of section
517 * Returns : String with link target, or NULL if out of
520 *********************************************************************/
521 static char *section_target(const unsigned sectionid)
525 snprintf(buf, 30, "#l%d", sectionid);
531 /*********************************************************************
533 * Function : map_copy_parameter_html
535 * Description : Copy a CGI parameter from one map to another, HTML
539 * 1 : out = target map
540 * 2 : in = source map
541 * 3 : name = name of cgi parameter to copy
543 * Returns : JB_ERR_OK on success
544 * JB_ERR_MEMORY on out-of-memory
545 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
548 *********************************************************************/
549 static jb_err map_copy_parameter_html(struct map *out,
550 const struct map *in,
560 value = lookup(in, name);
561 err = map(out, name, 1, html_encode(value), 0);
568 else if (*value == '\0')
570 return JB_ERR_CGI_PARAMS;
579 #if 0 /* unused function */
580 /*********************************************************************
582 * Function : map_copy_parameter_url
584 * Description : Copy a CGI parameter from one map to another, URL
588 * 1 : out = target map
589 * 2 : in = source map
590 * 3 : name = name of cgi parameter to copy
592 * Returns : JB_ERR_OK on success
593 * JB_ERR_MEMORY on out-of-memory
594 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
597 *********************************************************************/
598 static jb_err map_copy_parameter_url(struct map *out,
599 const struct map *in,
609 value = lookup(in, name);
610 err = map(out, name, 1, url_encode(value), 0);
617 else if (*value == '\0')
619 return JB_ERR_CGI_PARAMS;
626 #endif /* 0 - unused function */
628 /*********************************************************************
630 * Function : cgi_edit_actions_url_form
632 * Description : CGI function that displays a form for
636 * 1 : csp = Current client state (buffers, headers, etc...)
637 * 2 : rsp = http_response data structure for output
638 * 3 : parameters = map of cgi parameters
641 * f : (filename) Identifies the file to edit
642 * v : (version) File's last-modified time
643 * p : (pattern) Line number of pattern to edit
645 * Returns : JB_ERR_OK on success
646 * JB_ERR_MEMORY on out-of-memory
647 * JB_ERR_CGI_PARAMS if the CGI parameters are not
648 * specified or not valid.
650 *********************************************************************/
651 jb_err cgi_edit_actions_url_form(struct client_state *csp,
652 struct http_response *rsp,
653 const struct map *parameters)
655 struct map * exports;
657 struct editable_file * file;
658 struct file_line * cur_line;
659 unsigned line_number;
660 unsigned section_start_line_number = 0;
667 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
669 return cgi_error_disabled(csp, rsp);
672 err = get_number_param(csp, parameters, "p", &patternid);
678 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
681 /* No filename specified, can't read file, modified, or out of memory. */
682 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
685 cur_line = file->lines;
687 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
689 if (cur_line->type == FILE_LINE_ACTION)
691 section_start_line_number = line_number;
693 cur_line = cur_line->next;
696 if ( (cur_line == NULL)
697 || (line_number != patternid)
699 || (cur_line->type != FILE_LINE_URL))
701 /* Invalid "patternid" parameter */
702 edit_free_file(file);
703 return JB_ERR_CGI_PARAMS;
706 if (NULL == (exports = default_exports(csp, NULL)))
708 edit_free_file(file);
709 return JB_ERR_MEMORY;
712 err = map(exports, "f", 1, file->identifier, 1);
713 if (!err) err = map(exports, "v", 1, file->version_str, 1);
714 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
715 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
716 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
718 edit_free_file(file);
726 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
730 /*********************************************************************
732 * Function : cgi_edit_actions_add_url_form
734 * Description : CGI function that displays a form for
738 * 1 : csp = Current client state (buffers, headers, etc...)
739 * 2 : rsp = http_response data structure for output
740 * 3 : parameters = map of cgi parameters
743 * f : (filename) Identifies the file to edit
744 * v : (version) File's last-modified time
745 * s : (section) Line number of section to edit
747 * Returns : JB_ERR_OK on success
748 * JB_ERR_MEMORY on out-of-memory
749 * JB_ERR_CGI_PARAMS if the CGI parameters are not
750 * specified or not valid.
752 *********************************************************************/
753 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
754 struct http_response *rsp,
755 const struct map *parameters)
764 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
766 return cgi_error_disabled(csp, rsp);
769 if (NULL == (exports = default_exports(csp, NULL)))
771 return JB_ERR_MEMORY;
774 err = map_copy_parameter_html(exports, parameters, "f");
775 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
776 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
784 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
788 /*********************************************************************
790 * Function : cgi_edit_actions_remove_url_form
792 * Description : CGI function that displays a form for
796 * 1 : csp = Current client state (buffers, headers, etc...)
797 * 2 : rsp = http_response data structure for output
798 * 3 : parameters = map of cgi parameters
801 * f : (filename) Identifies the file to edit
802 * v : (version) File's last-modified time
803 * p : (pattern) Line number of pattern to edit
805 * Returns : JB_ERR_OK on success
806 * JB_ERR_MEMORY on out-of-memory
807 * JB_ERR_CGI_PARAMS if the CGI parameters are not
808 * specified or not valid.
810 *********************************************************************/
811 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
812 struct http_response *rsp,
813 const struct map *parameters)
815 struct map * exports;
817 struct editable_file * file;
818 struct file_line * cur_line;
819 unsigned line_number;
820 unsigned section_start_line_number = 0;
827 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
829 return cgi_error_disabled(csp, rsp);
832 err = get_number_param(csp, parameters, "p", &patternid);
838 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
841 /* No filename specified, can't read file, modified, or out of memory. */
842 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
845 cur_line = file->lines;
847 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
849 if (cur_line->type == FILE_LINE_ACTION)
851 section_start_line_number = line_number;
853 cur_line = cur_line->next;
856 if ( (cur_line == NULL)
857 || (line_number != patternid)
859 || (cur_line->type != FILE_LINE_URL))
861 /* Invalid "patternid" parameter */
862 edit_free_file(file);
863 return JB_ERR_CGI_PARAMS;
866 if (NULL == (exports = default_exports(csp, NULL)))
868 edit_free_file(file);
869 return JB_ERR_MEMORY;
872 err = map(exports, "f", 1, file->identifier, 1);
873 if (!err) err = map(exports, "v", 1, file->version_str, 1);
874 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
875 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
876 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
878 edit_free_file(file);
886 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
890 /*********************************************************************
892 * Function : edit_write_file
894 * Description : Write a complete file to disk.
897 * 1 : file = File to write.
899 * Returns : JB_ERR_OK on success
900 * JB_ERR_FILE on error writing to file.
901 * JB_ERR_MEMORY on out of memory
903 *********************************************************************/
904 jb_err edit_write_file(struct editable_file * file)
907 struct file_line * cur_line;
908 struct stat statbuf[1];
909 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
910 digits in time_t, assuming this is a 64-bit
911 machine, plus null terminator, plus one
915 assert(file->filename);
917 if (NULL == (fp = fopen(file->filename, "wb")))
922 cur_line = file->lines;
923 while (cur_line != NULL)
927 if (fputs(cur_line->raw, fp) < 0)
935 if (cur_line->prefix)
937 if (fputs(cur_line->prefix, fp) < 0)
943 if (cur_line->unprocessed)
946 if (NULL != strchr(cur_line->unprocessed, '#'))
948 /* Must quote '#' characters */
955 /* Count number of # characters, so we know length of output string */
956 src = cur_line->unprocessed;
957 while (NULL != (src = strchr(src, '#')))
964 /* Allocate new memory for string */
965 len = strlen(cur_line->unprocessed);
966 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
968 /* Uh oh, just trashed file! */
970 return JB_ERR_MEMORY;
973 /* Loop through string from end */
974 src = cur_line->unprocessed + len;
975 dest = str + len + numhash;
976 for ( ; len >= 0; len--)
978 if ((*dest-- = *src--) == '#')
982 assert(numhash >= 0);
985 assert(numhash == 0);
986 assert(src + 1 == cur_line->unprocessed);
987 assert(dest + 1 == str);
989 if (fputs(str, fp) < 0)
1000 /* Can write without quoting '#' characters. */
1001 if (fputs(cur_line->unprocessed, fp) < 0)
1007 if (fputs(NEWLINE(file->newline), fp) < 0)
1015 /* FIXME: Write data from file->data->whatever */
1019 cur_line = cur_line->next;
1025 /* Update the version stamp in the file structure, since we just
1026 * wrote to the file & changed it's date.
1028 if (stat(file->filename, statbuf) < 0)
1030 /* Error, probably file not found. */
1033 file->version = (unsigned)statbuf->st_mtime;
1035 /* Correct file->version_str */
1036 freez(file->version_str);
1037 snprintf(version_buf, 22, "%u", file->version);
1038 version_buf[21] = '\0';
1039 file->version_str = strdup(version_buf);
1040 if (version_buf == NULL)
1042 return JB_ERR_MEMORY;
1049 /*********************************************************************
1051 * Function : edit_free_file
1053 * Description : Free a complete file in memory.
1056 * 1 : file = Data structure to free.
1060 *********************************************************************/
1061 void edit_free_file(struct editable_file * file)
1065 /* Silently ignore NULL pointer */
1069 edit_free_file_lines(file->lines);
1070 freez(file->filename);
1071 freez(file->identifier);
1072 freez(file->version_str);
1074 file->parse_error_text = NULL; /* Statically allocated */
1075 file->parse_error = NULL;
1081 /*********************************************************************
1083 * Function : edit_free_file_lines
1085 * Description : Free an entire linked list of file lines.
1088 * 1 : first_line = Data structure to free.
1092 *********************************************************************/
1093 static void edit_free_file_lines(struct file_line * first_line)
1095 struct file_line * next_line;
1097 while (first_line != NULL)
1099 next_line = first_line->next;
1100 first_line->next = NULL;
1101 freez(first_line->raw);
1102 freez(first_line->prefix);
1103 freez(first_line->unprocessed);
1104 switch(first_line->type)
1106 case 0: /* special case if memory zeroed */
1107 case FILE_LINE_UNPROCESSED:
1108 case FILE_LINE_BLANK:
1109 case FILE_LINE_ALIAS_HEADER:
1110 case FILE_LINE_SETTINGS_HEADER:
1111 case FILE_LINE_DESCRIPTION_HEADER:
1112 case FILE_LINE_DESCRIPTION_ENTRY:
1113 case FILE_LINE_ALIAS_ENTRY:
1115 /* No data is stored for these */
1118 case FILE_LINE_ACTION:
1119 free_action(first_line->data.action);
1122 case FILE_LINE_SETTINGS_ENTRY:
1123 freez(first_line->data.setting.name);
1124 freez(first_line->data.setting.svalue);
1127 /* Should never happen */
1131 first_line->type = 0; /* paranoia */
1133 first_line = next_line;
1138 /*********************************************************************
1140 * Function : match_actions_file_header_line
1142 * Description : Match an actions file {{header}} line
1145 * 1 : line = String from file
1146 * 2 : name = Header to match against
1148 * Returns : 0 iff they match.
1150 *********************************************************************/
1151 static int match_actions_file_header_line(const char * line, const char * name)
1159 if ((line[0] != '{') || (line[1] != '{'))
1165 /* Look for optional whitespace */
1166 while ( (*line == ' ') || (*line == '\t') )
1171 /* Look for the specified name (case-insensitive) */
1173 if (0 != strncmpic(line, name, len))
1179 /* Look for optional whitespace */
1180 while ( (*line == ' ') || (*line == '\t') )
1185 /* Look for "}}" and end of string*/
1186 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1196 /*********************************************************************
1198 * Function : match_actions_file_header_line
1200 * Description : Match an actions file {{header}} line
1203 * 1 : line = String from file. Must not start with
1204 * whitespace (else infinite loop!)
1205 * 2 : pname = Destination for name
1206 * 2 : pvalue = Destination for value
1208 * Returns : JB_ERR_OK on success
1209 * JB_ERR_MEMORY on out-of-memory
1210 * JB_ERR_PARSE if there's no "=" sign, or if there's
1211 * nothing before the "=" sign (but empty
1212 * values *after* the "=" sign are legal).
1214 *********************************************************************/
1215 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1217 const char * name_end;
1218 const char * value_start;
1224 assert(*line != ' ');
1225 assert(*line != '\t');
1230 value_start = strchr(line, '=');
1231 if ((value_start == NULL) || (value_start == line))
1233 return JB_ERR_PARSE;
1236 name_end = value_start - 1;
1238 /* Eat any whitespace before the '=' */
1239 while ((*name_end == ' ') || (*name_end == '\t'))
1242 * we already know we must have at least 1 non-ws char
1243 * at start of buf - no need to check
1248 name_len = name_end - line + 1; /* Length excluding \0 */
1249 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1251 return JB_ERR_MEMORY;
1253 strncpy(*pname, line, name_len);
1254 (*pname)[name_len] = '\0';
1256 /* Eat any the whitespace after the '=' */
1258 while ((*value_start == ' ') || (*value_start == '\t'))
1263 if (NULL == (*pvalue = strdup(value_start)))
1267 return JB_ERR_MEMORY;
1274 /*********************************************************************
1276 * Function : edit_parse_actions_file
1278 * Description : Parse an actions file in memory.
1280 * Passed linked list must have the "data" member
1281 * zeroed, and must contain valid "next" and
1282 * "unprocessed" fields. The "raw" and "prefix"
1283 * fields are ignored, and "type" is just overwritten.
1285 * Note that on error the file may have been
1289 * 1 : file = Actions file to be parsed in-place.
1291 * Returns : JB_ERR_OK on success
1292 * JB_ERR_MEMORY on out-of-memory
1293 * JB_ERR_PARSE on error
1295 *********************************************************************/
1296 jb_err edit_parse_actions_file(struct editable_file * file)
1298 struct file_line * cur_line;
1300 const char * text; /* Text from a line */
1301 char * name; /* For lines of the form name=value */
1302 char * value; /* For lines of the form name=value */
1303 struct action_alias * alias_list = NULL;
1304 jb_err err = JB_ERR_OK;
1306 /* alias_list contains the aliases defined in this file.
1307 * It might be better to use the "file_line.data" fields
1308 * in the relavent places instead.
1311 cur_line = file->lines;
1313 /* A note about blank line support: Blank lines should only
1314 * ever occur as the last line in the file. This function
1315 * is more forgiving than that - FILE_LINE_BLANK can occur
1319 /* Skip leading blanks. Should only happen if file is
1320 * empty (which is valid, but pointless).
1322 while ( (cur_line != NULL)
1323 && (cur_line->unprocessed[0] == '\0') )
1326 cur_line->type = FILE_LINE_BLANK;
1327 cur_line = cur_line->next;
1330 if ( (cur_line != NULL)
1331 && (cur_line->unprocessed[0] != '{') )
1333 /* File doesn't start with a header */
1334 file->parse_error = cur_line;
1335 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1336 return JB_ERR_PARSE;
1339 if ( (cur_line != NULL) && (0 ==
1340 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1342 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1344 cur_line = cur_line->next;
1345 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1347 if (cur_line->unprocessed[0])
1349 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1351 err = split_line_on_equals(cur_line->unprocessed,
1352 &cur_line->data.setting.name,
1353 &cur_line->data.setting.svalue);
1354 if (err == JB_ERR_MEMORY)
1358 else if (err != JB_ERR_OK)
1360 /* Line does not contain a name=value pair */
1361 file->parse_error = cur_line;
1362 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1363 return JB_ERR_PARSE;
1368 cur_line->type = FILE_LINE_BLANK;
1370 cur_line = cur_line->next;
1374 if ( (cur_line != NULL) && (0 ==
1375 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1377 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1379 cur_line = cur_line->next;
1380 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1382 if (cur_line->unprocessed[0])
1384 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1388 cur_line->type = FILE_LINE_BLANK;
1390 cur_line = cur_line->next;
1394 if ( (cur_line != NULL) && (0 ==
1395 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1397 cur_line->type = FILE_LINE_ALIAS_HEADER;
1399 cur_line = cur_line->next;
1400 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1402 if (cur_line->unprocessed[0])
1404 /* define an alias */
1405 struct action_alias * new_alias;
1407 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1409 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1410 if (err == JB_ERR_MEMORY)
1414 else if (err != JB_ERR_OK)
1416 /* Line does not contain a name=value pair */
1417 file->parse_error = cur_line;
1418 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1419 return JB_ERR_PARSE;
1422 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1427 free_alias_list(alias_list);
1428 return JB_ERR_MEMORY;
1431 err = get_actions(value, alias_list, new_alias->action);
1434 /* Invalid action or out of memory */
1438 free_alias_list(alias_list);
1439 if (err == JB_ERR_MEMORY)
1445 /* Line does not contain a name=value pair */
1446 file->parse_error = cur_line;
1447 file->parse_error_text = "This alias does not specify a valid set of actions.";
1448 return JB_ERR_PARSE;
1454 new_alias->name = name;
1457 new_alias->next = alias_list;
1458 alias_list = new_alias;
1462 cur_line->type = FILE_LINE_BLANK;
1464 cur_line = cur_line->next;
1468 /* Header done, process the main part of the file */
1469 while (cur_line != NULL)
1471 /* At this point, (cur_line->unprocessed[0] == '{') */
1472 assert(cur_line->unprocessed[0] == '{');
1473 text = cur_line->unprocessed + 1;
1474 len = strlen(text) - 1;
1475 if (text[len] != '}')
1477 /* No closing } on header */
1478 free_alias_list(alias_list);
1479 file->parse_error = cur_line;
1480 file->parse_error_text = "Headers starting with '{' must have a "
1481 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1482 "must close with two brackets ('}}').";
1483 return JB_ERR_PARSE;
1488 /* An invalid {{ header. */
1489 free_alias_list(alias_list);
1490 file->parse_error = cur_line;
1491 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1492 "Please remember that the system (two-bracket) headers must "
1493 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1494 "and must appear before any actions (one-bracket) headers. "
1495 "Also note that system headers may not be repeated.";
1496 return JB_ERR_PARSE;
1499 while ( (*text == ' ') || (*text == '\t') )
1505 && ( (text[len - 1] == ' ')
1506 || (text[len - 1] == '\t') ) )
1511 cur_line->type = FILE_LINE_ACTION;
1513 /* Remove {} and make copy */
1514 if (NULL == (value = (char *) malloc(len + 1)))
1517 free_alias_list(alias_list);
1518 return JB_ERR_MEMORY;
1520 strncpy(value, text, len);
1524 err = get_actions(value, alias_list, cur_line->data.action);
1527 /* Invalid action or out of memory */
1529 free_alias_list(alias_list);
1530 if (err == JB_ERR_MEMORY)
1536 /* Line does not contain a name=value pair */
1537 file->parse_error = cur_line;
1538 file->parse_error_text = "This header does not specify a valid set of actions.";
1539 return JB_ERR_PARSE;
1543 /* Done with string - it was clobbered anyway */
1546 /* Process next line */
1547 cur_line = cur_line->next;
1549 /* Loop processing URL patterns */
1550 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1552 if (cur_line->unprocessed[0])
1554 /* Could parse URL here, but this isn't currently needed */
1556 cur_line->type = FILE_LINE_URL;
1560 cur_line->type = FILE_LINE_BLANK;
1562 cur_line = cur_line->next;
1564 } /* End main while(cur_line != NULL) loop */
1566 free_alias_list(alias_list);
1572 /*********************************************************************
1574 * Function : edit_read_file_lines
1576 * Description : Read all the lines of a file into memory.
1577 * Handles whitespace, comments and line continuation.
1580 * 1 : fp = File to read from. On return, this will be
1581 * at EOF but it will not have been closed.
1582 * 2 : pfile = Destination for a linked list of file_lines.
1583 * Will be set to NULL on error.
1584 * 3 : newline = How to handle newlines.
1586 * Returns : JB_ERR_OK on success
1587 * JB_ERR_MEMORY on out-of-memory
1589 *********************************************************************/
1590 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1592 struct file_line * first_line; /* Keep for return value or to free */
1593 struct file_line * cur_line; /* Current line */
1594 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1602 cur_line = first_line = zalloc(sizeof(struct file_line));
1603 if (cur_line == NULL)
1605 return JB_ERR_MEMORY;
1608 cur_line->type = FILE_LINE_UNPROCESSED;
1610 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1613 /* Out of memory or empty file. */
1614 /* Note that empty file is not an error we propogate up */
1616 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1621 prev_line = cur_line;
1622 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1623 if (cur_line == NULL)
1626 edit_free_file_lines(first_line);
1627 return JB_ERR_MEMORY;
1630 cur_line->type = FILE_LINE_UNPROCESSED;
1632 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1633 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1636 edit_free_file_lines(first_line);
1637 return JB_ERR_MEMORY;
1641 while (rval != JB_ERR_FILE);
1645 /* We allocated one too many - free it */
1646 prev_line->next = NULL;
1649 *pfile = first_line;
1654 /*********************************************************************
1656 * Function : edit_read_file
1658 * Description : Read a complete file into memory.
1659 * Handles CGI parameter parsing. If requested, also
1660 * checks the file's modification timestamp.
1663 * 1 : csp = Current client state (buffers, headers, etc...)
1664 * 2 : parameters = map of cgi parameters.
1665 * 3 : require_version = true to check "ver" parameter.
1666 * 4 : suffix = File extension, e.g. ".action".
1667 * 5 : pfile = Destination for the file. Will be set
1671 * filename : The name of the file to read, without the
1672 * path or ".action" extension.
1673 * ver : (Only if require_version is nonzero)
1674 * Timestamp of the actions file. If wrong, this
1675 * function fails with JB_ERR_MODIFIED.
1677 * Returns : JB_ERR_OK on success
1678 * JB_ERR_MEMORY on out-of-memory
1679 * JB_ERR_CGI_PARAMS if "filename" was not specified
1681 * JB_ERR_FILE if the file cannot be opened or
1683 * JB_ERR_MODIFIED if version checking was requested and
1684 * failed - the file was modified outside
1685 * of this CGI editor instance.
1687 *********************************************************************/
1688 jb_err edit_read_file(struct client_state *csp,
1689 const struct map *parameters,
1690 int require_version,
1692 struct editable_file **pfile)
1694 struct file_line * lines;
1698 const char * identifier;
1699 struct editable_file * file;
1700 unsigned version = 0;
1701 struct stat statbuf[1];
1702 char version_buf[22];
1703 int newline = NEWLINE_UNKNOWN;
1711 err = get_file_name_param(csp, parameters, "f", suffix,
1712 &filename, &identifier);
1718 if (stat(filename, statbuf) < 0)
1720 /* Error, probably file not found. */
1724 version = (unsigned) statbuf->st_mtime;
1726 if (require_version)
1728 unsigned specified_version;
1729 err = get_number_param(csp, parameters, "v", &specified_version);
1736 if (version != specified_version)
1738 return JB_ERR_MODIFIED;
1742 if (NULL == (fp = fopen(filename,"rb")))
1748 err = edit_read_file_lines(fp, &lines, &newline);
1758 file = (struct editable_file *) zalloc(sizeof(*file));
1762 edit_free_file_lines(lines);
1766 file->lines = lines;
1767 file->newline = newline;
1768 file->filename = filename;
1769 file->version = version;
1770 file->identifier = url_encode(identifier);
1772 if (file->identifier == NULL)
1774 edit_free_file(file);
1775 return JB_ERR_MEMORY;
1778 /* Correct file->version_str */
1779 freez(file->version_str);
1780 snprintf(version_buf, 22, "%u", file->version);
1781 version_buf[21] = '\0';
1782 file->version_str = strdup(version_buf);
1783 if (version_buf == NULL)
1785 edit_free_file(file);
1786 return JB_ERR_MEMORY;
1794 /*********************************************************************
1796 * Function : edit_read_actions_file
1798 * Description : Read a complete actions file into memory.
1799 * Handles CGI parameter parsing. If requested, also
1800 * checks the file's modification timestamp.
1802 * If this function detects an error in the categories
1803 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1804 * then it handles it by filling in the specified
1805 * response structure and returning JB_ERR_FILE.
1808 * 1 : csp = Current client state (buffers, headers, etc...)
1809 * 2 : rsp = HTTP response. Only filled in on error.
1810 * 2 : parameters = map of cgi parameters.
1811 * 3 : require_version = true to check "ver" parameter.
1812 * 4 : pfile = Destination for the file. Will be set
1816 * filename : The name of the actions file to read, without the
1817 * path or ".action" extension.
1818 * ver : (Only if require_version is nonzero)
1819 * Timestamp of the actions file. If wrong, this
1820 * function fails with JB_ERR_MODIFIED.
1822 * Returns : JB_ERR_OK on success
1823 * JB_ERR_MEMORY on out-of-memory
1824 * JB_ERR_CGI_PARAMS if "filename" was not specified
1826 * JB_ERR_FILE if the file does not contain valid data,
1827 * or if file cannot be opened or
1828 * contains no data, or if version
1829 * checking was requested and failed.
1831 *********************************************************************/
1832 jb_err edit_read_actions_file(struct client_state *csp,
1833 struct http_response *rsp,
1834 const struct map *parameters,
1835 int require_version,
1836 struct editable_file **pfile)
1839 struct editable_file *file;
1847 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1850 /* Try to handle if possible */
1851 if (err == JB_ERR_FILE)
1853 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1855 else if (err == JB_ERR_MODIFIED)
1857 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1859 if (err == JB_ERR_OK)
1862 * Signal to higher-level CGI code that there was a problem but we
1863 * handled it, they should just return JB_ERR_OK.
1870 err = edit_parse_actions_file(file);
1873 if (err == JB_ERR_PARSE)
1875 err = cgi_error_parse(csp, rsp, file);
1876 if (err == JB_ERR_OK)
1879 * Signal to higher-level CGI code that there was a problem but we
1880 * handled it, they should just return JB_ERR_OK.
1885 edit_free_file(file);
1894 /*********************************************************************
1896 * Function : get_file_name_param
1898 * Description : Get the name of the file to edit from the parameters
1899 * passed to a CGI function. This function handles
1900 * security checks such as blocking urls containing
1901 * "/" or ".", prepending the config file directory,
1902 * and adding the specified suffix.
1904 * (This is an essential security check, otherwise
1905 * users may be able to pass "../../../etc/passwd"
1906 * and overwrite the password file [linux], "prn:"
1907 * and print random data [Windows], etc...)
1909 * This function only allows filenames contining the
1910 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1911 * That's probably too restrictive but at least it's
1915 * 1 : csp = Current client state (buffers, headers, etc...)
1916 * 2 : parameters = map of cgi parameters
1917 * 3 : param_name = The name of the parameter to read
1918 * 4 : suffix = File extension, e.g. ".actions"
1919 * 5 : pfilename = destination for full filename. Caller
1920 * free()s. Set to NULL on error.
1921 * 6 : pparam = destination for partial filename,
1922 * suitable for use in another URL. Allocated as part
1923 * of the map "parameters", so don't free it.
1924 * Set to NULL if not specified.
1926 * Returns : JB_ERR_OK on success
1927 * JB_ERR_MEMORY on out-of-memory
1928 * JB_ERR_CGI_PARAMS if "filename" was not specified
1931 *********************************************************************/
1932 static jb_err get_file_name_param(struct client_state *csp,
1933 const struct map *parameters,
1934 const char *param_name,
1937 const char **pparam)
1955 param = lookup(parameters, param_name);
1958 return JB_ERR_CGI_PARAMS;
1963 len = strlen(param);
1964 if (len >= FILENAME_MAX)
1967 return JB_ERR_CGI_PARAMS;
1970 /* Check every character to see if it's legal */
1972 while ((ch = *s++) != '\0')
1974 if ( ((ch < 'A') || (ch > 'Z'))
1975 && ((ch < 'a') || (ch > 'z'))
1976 && ((ch < '0') || (ch > '9'))
1980 /* Probable hack attempt. */
1981 return JB_ERR_CGI_PARAMS;
1985 /* Append extension */
1986 name = malloc(len + strlen(suffix) + 1);
1989 return JB_ERR_MEMORY;
1991 strcpy(name, param);
1992 strcpy(name + len, suffix);
1995 fullpath = make_path(csp->config->confdir, name);
1998 if (fullpath == NULL)
2000 return JB_ERR_MEMORY;
2004 *pfilename = fullpath;
2010 /*********************************************************************
2012 * Function : get_url_spec_param
2014 * Description : Get a URL pattern from the parameters
2015 * passed to a CGI function. Removes leading/trailing
2016 * spaces and validates it.
2019 * 1 : csp = Current client state (buffers, headers, etc...)
2020 * 2 : parameters = map of cgi parameters
2021 * 3 : name = Name of CGI parameter to read
2022 * 4 : pvalue = destination for value. Will be malloc()'d.
2023 * Set to NULL on error.
2025 * Returns : JB_ERR_OK on success
2026 * JB_ERR_MEMORY on out-of-memory
2027 * JB_ERR_CGI_PARAMS if the parameter was not specified
2030 *********************************************************************/
2031 static jb_err get_url_spec_param(struct client_state *csp,
2032 const struct map *parameters,
2036 const char *orig_param;
2039 struct url_spec compiled[1];
2049 orig_param = lookup(parameters, name);
2052 return JB_ERR_CGI_PARAMS;
2055 /* Copy and trim whitespace */
2056 param = strdup(orig_param);
2059 return JB_ERR_MEMORY;
2063 /* Must be non-empty, and can't allow 1st character to be '{' */
2064 if (param[0] == '\0' || param[0] == '{')
2067 return JB_ERR_CGI_PARAMS;
2070 /* Check for embedded newlines */
2071 for (s = param; *s != '\0'; s++)
2073 if ((*s == '\r') || (*s == '\n'))
2076 return JB_ERR_CGI_PARAMS;
2080 /* Check that regex is valid */
2085 return JB_ERR_MEMORY;
2087 err = create_url_spec(compiled, s);
2092 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2094 free_url_spec(compiled);
2096 if (param[strlen(param) - 1] == '\\')
2099 * Must protect trailing '\\' from becoming line continuation character.
2100 * Two methods: 1) If it's a domain only, add a trailing '/'.
2101 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2103 if (strchr(param, '/') == NULL)
2105 err = string_append(¶m, "/");
2109 err = string_append(¶m, "(?:)");
2116 /* Check that the modified regex is valid */
2121 return JB_ERR_MEMORY;
2123 err = create_url_spec(compiled, s);
2128 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2130 free_url_spec(compiled);
2137 /*********************************************************************
2139 * Function : map_radio
2141 * Description : Map a set of radio button values. E.g. if you have
2142 * 3 radio buttons, declare them as:
2143 * <option type="radio" name="xyz" @xyz-a@>
2144 * <option type="radio" name="xyz" @xyz-b@>
2145 * <option type="radio" name="xyz" @xyz-c@>
2146 * Then map one of the @xyz-?@ variables to "checked"
2147 * and all the others to empty by calling:
2148 * map_radio(exports, "xyz", "abc", sel)
2149 * Where 'sel' is 'a', 'b', or 'c'.
2152 * 1 : exports = Exports map to modify.
2153 * 2 : optionname = name for map
2154 * 3 : values = null-terminated list of values;
2155 * 4 : value = Selected value.
2157 * CGI Parameters : None
2159 * Returns : JB_ERR_OK on success
2160 * JB_ERR_MEMORY on out-of-memory
2162 *********************************************************************/
2163 static jb_err map_radio(struct map * exports,
2164 const char * optionname,
2165 const char * values,
2177 len = strlen(optionname);
2178 buf = malloc(len + 3);
2181 return JB_ERR_MEMORY;
2184 strcpy(buf, optionname);
2189 while ((c = *values++) != '\0')
2194 if (map(exports, buf, 1, "", 1))
2196 return JB_ERR_MEMORY;
2202 return map(exports, buf, 0, "checked", 1);
2206 /*********************************************************************
2208 * Function : cgi_error_modified
2210 * Description : CGI function that is called when a file is modified
2211 * outside the CGI editor.
2214 * 1 : csp = Current client state (buffers, headers, etc...)
2215 * 2 : rsp = http_response data structure for output
2216 * 3 : filename = The file that was modified.
2218 * CGI Parameters : none
2220 * Returns : JB_ERR_OK on success
2221 * JB_ERR_MEMORY on out-of-memory error.
2223 *********************************************************************/
2224 jb_err cgi_error_modified(struct client_state *csp,
2225 struct http_response *rsp,
2226 const char *filename)
2228 struct map *exports;
2235 if (NULL == (exports = default_exports(csp, NULL)))
2237 return JB_ERR_MEMORY;
2240 err = map(exports, "f", 1, html_encode(filename), 0);
2247 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2251 /*********************************************************************
2253 * Function : cgi_error_parse
2255 * Description : CGI function that is called when a file cannot
2256 * be parsed by the CGI editor.
2259 * 1 : csp = Current client state (buffers, headers, etc...)
2260 * 2 : rsp = http_response data structure for output
2261 * 3 : file = The file that was modified.
2263 * CGI Parameters : none
2265 * Returns : JB_ERR_OK on success
2266 * JB_ERR_MEMORY on out-of-memory error.
2268 *********************************************************************/
2269 jb_err cgi_error_parse(struct client_state *csp,
2270 struct http_response *rsp,
2271 struct editable_file *file)
2273 struct map *exports;
2275 struct file_line *cur_line;
2281 if (NULL == (exports = default_exports(csp, NULL)))
2283 return JB_ERR_MEMORY;
2286 err = map(exports, "f", 1, file->identifier, 1);
2287 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2289 cur_line = file->parse_error;
2292 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2293 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2301 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2305 /*********************************************************************
2307 * Function : cgi_error_file
2309 * Description : CGI function that is called when a file cannot be
2310 * opened by the CGI editor.
2313 * 1 : csp = Current client state (buffers, headers, etc...)
2314 * 2 : rsp = http_response data structure for output
2315 * 3 : filename = The file that was modified.
2317 * CGI Parameters : none
2319 * Returns : JB_ERR_OK on success
2320 * JB_ERR_MEMORY on out-of-memory error.
2322 *********************************************************************/
2323 jb_err cgi_error_file(struct client_state *csp,
2324 struct http_response *rsp,
2325 const char *filename)
2327 struct map *exports;
2334 if (NULL == (exports = default_exports(csp, NULL)))
2336 return JB_ERR_MEMORY;
2339 err = map(exports, "f", 1, html_encode(filename), 0);
2346 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2350 /*********************************************************************
2352 * Function : cgi_error_file
2354 * Description : CGI function that is called when a file cannot be
2355 * opened for writing by the CGI editor.
2358 * 1 : csp = Current client state (buffers, headers, etc...)
2359 * 2 : rsp = http_response data structure for output
2360 * 3 : filename = The file that we can't write to
2362 * CGI Parameters : none
2364 * Returns : JB_ERR_OK on success
2365 * JB_ERR_MEMORY on out-of-memory error.
2367 *********************************************************************/
2368 jb_err cgi_error_file_read_only(struct client_state *csp,
2369 struct http_response *rsp,
2370 const char *filename)
2372 struct map *exports;
2379 if (NULL == (exports = default_exports(csp, NULL)))
2381 return JB_ERR_MEMORY;
2384 err = map(exports, "f", 1, html_encode(filename), 0);
2391 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2395 /*********************************************************************
2397 * Function : cgi_error_disabled
2399 * Description : CGI function that is called if the actions editor
2400 * is called although it's disabled in config
2403 * 1 : csp = Current client state (buffers, headers, etc...)
2404 * 2 : rsp = http_response data structure for output
2406 * CGI Parameters : none
2408 * Returns : JB_ERR_OK on success
2409 * JB_ERR_MEMORY on out-of-memory error.
2411 *********************************************************************/
2412 jb_err cgi_error_disabled(struct client_state *csp,
2413 struct http_response *rsp)
2415 struct map *exports;
2420 if (NULL == (exports = default_exports(csp, NULL)))
2422 return JB_ERR_MEMORY;
2425 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2429 /*********************************************************************
2431 * Function : cgi_edit_actions
2433 * Description : CGI function that allows the user to choose which
2434 * actions file to edit.
2437 * 1 : csp = Current client state (buffers, headers, etc...)
2438 * 2 : rsp = http_response data structure for output
2439 * 3 : parameters = map of cgi parameters
2441 * CGI Parameters : None
2443 * Returns : JB_ERR_OK on success
2444 * JB_ERR_MEMORY on out-of-memory error
2446 *********************************************************************/
2447 jb_err cgi_edit_actions(struct client_state *csp,
2448 struct http_response *rsp,
2449 const struct map *parameters)
2452 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2454 return cgi_error_disabled(csp, rsp);
2457 /* FIXME: Incomplete */
2458 rsp->status = strdup("302 Local Redirect from Privoxy");
2459 if (rsp->status == NULL)
2461 return JB_ERR_MEMORY;
2463 if (enlist_unique_header(rsp->headers, "Location",
2464 CGI_PREFIX "edit-actions-list?f=default"))
2468 return JB_ERR_MEMORY;
2475 /*********************************************************************
2477 * Function : cgi_edit_actions_list
2479 * Description : CGI function that edits the actions list.
2480 * FIXME: This function shouldn't FATAL ever.
2481 * FIXME: This function doesn't check the retval of map()
2483 * 1 : csp = Current client state (buffers, headers, etc...)
2484 * 2 : rsp = http_response data structure for output
2485 * 3 : parameters = map of cgi parameters
2487 * CGI Parameters : filename
2489 * Returns : JB_ERR_OK on success
2490 * JB_ERR_MEMORY on out-of-memory
2491 * JB_ERR_FILE if the file cannot be opened or
2493 * JB_ERR_CGI_PARAMS if "filename" was not specified
2496 *********************************************************************/
2497 jb_err cgi_edit_actions_list(struct client_state *csp,
2498 struct http_response *rsp,
2499 const struct map *parameters)
2501 char * section_template;
2502 char * url_template;
2507 struct map * exports;
2508 struct map * section_exports;
2509 struct map * url_exports;
2510 struct editable_file * file;
2511 struct file_line * cur_line;
2512 unsigned line_number = 0;
2513 unsigned prev_section_line_number = ((unsigned) (-1));
2515 struct file_list * fl;
2516 struct url_actions * b;
2517 char * buttons = NULL;
2520 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2522 return cgi_error_disabled(csp, rsp);
2525 if (NULL == (exports = default_exports(csp, NULL)))
2527 return JB_ERR_MEMORY;
2530 /* Load actions file */
2531 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2534 /* No filename specified, can't read file, or out of memory. */
2535 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2538 /* Find start of actions in file */
2539 cur_line = file->lines;
2541 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2543 cur_line = cur_line->next;
2548 * Conventional actions files should have a match all block
2550 * cur_line = {...global actions...}
2551 * cur_line->next = /
2552 * cur_line->next->next = {...actions...} or EOF
2554 if ( (cur_line != NULL)
2555 && (cur_line->type == FILE_LINE_ACTION)
2556 && (cur_line->next != NULL)
2557 && (cur_line->next->type == FILE_LINE_URL)
2558 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2559 && ( (cur_line->next->next == NULL)
2560 || (cur_line->next->next->type != FILE_LINE_URL)
2564 * Generate string with buttons to set actions for "/" to
2565 * any predefined set of actions (named standard.*, probably
2566 * residing in standard.action).
2569 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2572 edit_free_file(file);
2574 if (err == JB_ERR_FILE)
2576 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2581 err = template_fill(§ion_template, exports);
2584 edit_free_file(file);
2589 buttons = strdup("");
2590 for (i = 0; i < MAX_ACTION_FILES; i++)
2592 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2594 for (b = b->next; NULL != b; b = b->next)
2596 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2598 if (err || (NULL == (section_exports = new_map())))
2601 free(section_template);
2602 edit_free_file(file);
2604 return JB_ERR_MEMORY;
2607 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2609 if (err || (NULL == (s = strdup(section_template))))
2611 free_map(section_exports);
2613 free(section_template);
2614 edit_free_file(file);
2616 return JB_ERR_MEMORY;
2619 if (!err) err = template_fill(&s, section_exports);
2620 free_map(section_exports);
2621 if (!err) err = string_join(&buttons, s);
2626 freez(section_template);
2627 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2630 * Conventional actions file, supply extra editing help.
2631 * (e.g. don't allow them to make it an unconventional one).
2633 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2635 snprintf(buf, 150, "%d", line_number);
2636 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2637 snprintf(buf, 150, "%d", line_number + 2);
2638 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2639 if (!err) err = map(exports, "all-urls-actions", 1,
2640 actions_to_html(csp, cur_line->data.action), 0);
2642 /* Skip the 2 lines */
2643 cur_line = cur_line->next->next;
2647 * Note that prev_section_line_number is NOT set here.
2648 * This is deliberate and not a bug. It stops a "Move up"
2649 * option appearing on the next section. Clicking "Move
2650 * up" would make the actions file unconventional, which
2651 * we don't want, so we hide this option.
2657 * Non-standard actions file - does not begin with
2658 * the "All URLs" section.
2660 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2663 /* Set up global exports */
2665 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2666 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2668 /* Discourage private additions to default.action */
2670 if (!err) err = map_conditional(exports, "default-action",
2671 (strcmp("default", lookup(parameters, "f")) == 0));
2674 edit_free_file(file);
2679 /* Should do all global exports above this point */
2681 /* Load templates */
2683 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2686 edit_free_file(file);
2688 if (err == JB_ERR_FILE)
2690 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2695 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2698 free(section_template);
2699 edit_free_file(file);
2701 if (err == JB_ERR_FILE)
2703 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2708 err = template_fill(§ion_template, exports);
2712 edit_free_file(file);
2718 err = template_fill(&url_template, exports);
2721 free(section_template);
2722 edit_free_file(file);
2727 if (NULL == (sections = strdup("")))
2729 free(section_template);
2731 edit_free_file(file);
2733 return JB_ERR_MEMORY;
2736 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2738 if (NULL == (section_exports = new_map()))
2741 free(section_template);
2743 edit_free_file(file);
2745 return JB_ERR_MEMORY;
2748 snprintf(buf, 150, "%d", line_number);
2749 err = map(section_exports, "s", 1, buf, 1);
2750 if (!err) err = map(section_exports, "actions", 1,
2751 actions_to_html(csp, cur_line->data.action), 0);
2754 && (cur_line->next != NULL)
2755 && (cur_line->next->type == FILE_LINE_URL))
2757 /* This section contains at least one URL, don't allow delete */
2758 err = map_block_killer(section_exports, "empty-section");
2762 if (!err) err = map_block_keep(section_exports, "empty-section");
2765 if (prev_section_line_number != ((unsigned)(-1)))
2767 /* Not last section */
2768 snprintf(buf, 150, "%d", prev_section_line_number);
2769 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2770 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2775 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2777 prev_section_line_number = line_number;
2782 free(section_template);
2784 edit_free_file(file);
2786 free_map(section_exports);
2790 /* Should do all section-specific exports above this point */
2792 if (NULL == (urls = strdup("")))
2795 free(section_template);
2797 edit_free_file(file);
2799 free_map(section_exports);
2800 return JB_ERR_MEMORY;
2805 cur_line = cur_line->next;
2808 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2810 if (NULL == (url_exports = new_map()))
2814 free(section_template);
2816 edit_free_file(file);
2818 free_map(section_exports);
2819 return JB_ERR_MEMORY;
2822 snprintf(buf, 150, "%d", line_number);
2823 err = map(url_exports, "p", 1, buf, 1);
2825 snprintf(buf, 150, "%d", url_1_2);
2826 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2828 if (!err) err = map(url_exports, "url-html", 1,
2829 html_encode(cur_line->unprocessed), 0);
2830 if (!err) err = map(url_exports, "url", 1,
2831 url_encode(cur_line->unprocessed), 0);
2837 free(section_template);
2839 edit_free_file(file);
2841 free_map(section_exports);
2842 free_map(url_exports);
2846 if (NULL == (s = strdup(url_template)))
2850 free(section_template);
2852 edit_free_file(file);
2854 free_map(section_exports);
2855 free_map(url_exports);
2856 return JB_ERR_MEMORY;
2859 err = template_fill(&s, section_exports);
2860 if (!err) err = template_fill(&s, url_exports);
2861 if (!err) err = string_append(&urls, s);
2863 free_map(url_exports);
2870 free(section_template);
2872 edit_free_file(file);
2874 free_map(section_exports);
2878 url_1_2 = 3 - url_1_2;
2880 cur_line = cur_line->next;
2884 err = map(section_exports, "urls", 1, urls, 0);
2886 /* Could also do section-specific exports here, but it wouldn't be as fast */
2888 snprintf(buf, 150, "%d", line_number);
2889 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2891 if ( (cur_line != NULL)
2892 && (cur_line->type == FILE_LINE_ACTION))
2894 /* Not last section */
2895 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2900 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2906 free(section_template);
2908 edit_free_file(file);
2910 free_map(section_exports);
2914 if (NULL == (s = strdup(section_template)))
2917 free(section_template);
2919 edit_free_file(file);
2921 free_map(section_exports);
2922 return JB_ERR_MEMORY;
2925 err = template_fill(&s, section_exports);
2926 if (!err) err = string_append(§ions, s);
2929 free_map(section_exports);
2934 free(section_template);
2936 edit_free_file(file);
2942 edit_free_file(file);
2943 free(section_template);
2946 err = map(exports, "sections", 1, sections, 0);
2953 /* Could also do global exports here, but it wouldn't be as fast */
2955 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2959 /*********************************************************************
2961 * Function : cgi_edit_actions_for_url
2963 * Description : CGI function that edits the Actions list.
2966 * 1 : csp = Current client state (buffers, headers, etc...)
2967 * 2 : rsp = http_response data structure for output
2968 * 3 : parameters = map of cgi parameters
2970 * CGI Parameters : None
2972 * Returns : JB_ERR_OK on success
2973 * JB_ERR_MEMORY on out-of-memory
2974 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2975 * specified or not valid.
2977 *********************************************************************/
2978 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2979 struct http_response *rsp,
2980 const struct map *parameters)
2982 struct map * exports;
2984 struct editable_file * file;
2985 struct file_line * cur_line;
2986 unsigned line_number;
2988 struct file_list *filter_file;
2989 struct re_filterfile_spec *filter_group;
2991 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2993 return cgi_error_disabled(csp, rsp);
2996 err = get_number_param(csp, parameters, "s", §ionid);
3002 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3005 /* No filename specified, can't read file, modified, or out of memory. */
3006 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3009 cur_line = file->lines;
3011 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3013 cur_line = cur_line->next;
3016 if ( (cur_line == NULL)
3017 || (line_number != sectionid)
3019 || (cur_line->type != FILE_LINE_ACTION))
3021 /* Invalid "sectionid" parameter */
3022 edit_free_file(file);
3023 return JB_ERR_CGI_PARAMS;
3026 if (NULL == (exports = default_exports(csp, NULL)))
3028 edit_free_file(file);
3029 return JB_ERR_MEMORY;
3032 err = map(exports, "f", 1, file->identifier, 1);
3033 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3034 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3036 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3038 filter_file = csp->rlist;
3039 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
3041 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
3045 edit_free_file(file);
3050 if (filter_group == NULL)
3052 err = map(exports, "filter-params", 1, "", 1);
3056 /* We have some entries in the filter list */
3059 char * filter_template;
3061 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3064 edit_free_file(file);
3066 if (err == JB_ERR_FILE)
3068 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3073 err = template_fill(&filter_template, exports);
3075 result = strdup("");
3077 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3079 char current_mode = 'x';
3080 struct list_entry *filter_name;
3082 struct map *line_exports;
3085 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
3086 while ((filter_name != NULL)
3087 && (0 != strcmp(filter_group->name, filter_name->str)))
3089 filter_name = filter_name->next;
3092 if (filter_name != NULL)
3098 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
3099 while ((filter_name != NULL)
3100 && (0 != strcmp(filter_group->name, filter_name->str)))
3102 filter_name = filter_name->next;
3104 if (filter_name != NULL)
3110 /* Generate a unique serial number */
3111 snprintf(number, sizeof(number), "%x", index++);
3112 number[sizeof(number) - 1] = '\0';
3114 line_exports = new_map();
3115 if (line_exports == NULL)
3117 err = JB_ERR_MEMORY;
3122 if (!err) err = map(line_exports, "index", 1, number, 1);
3123 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3124 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3125 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3130 this_line = strdup(filter_template);
3131 if (this_line == NULL) err = JB_ERR_MEMORY;
3133 if (!err) err = template_fill(&this_line, line_exports);
3134 string_join(&result, this_line);
3136 free_map(line_exports);
3140 freez(filter_template);
3144 err = map(exports, "filter-params", 1, result, 0);
3152 if (!err) err = map_radio(exports, "filter-all", "nx",
3153 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3155 edit_free_file(file);
3163 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3167 /*********************************************************************
3169 * Function : cgi_edit_actions_submit
3171 * Description : CGI function that actually edits the Actions list.
3174 * 1 : csp = Current client state (buffers, headers, etc...)
3175 * 2 : rsp = http_response data structure for output
3176 * 3 : parameters = map of cgi parameters
3178 * CGI Parameters : None
3180 * Returns : JB_ERR_OK on success
3181 * JB_ERR_MEMORY on out-of-memory
3182 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3183 * specified or not valid.
3185 *********************************************************************/
3186 jb_err cgi_edit_actions_submit(struct client_state *csp,
3187 struct http_response *rsp,
3188 const struct map *parameters)
3194 struct editable_file * file;
3195 struct file_line * cur_line;
3196 unsigned line_number;
3200 const char * action_set_name;
3202 struct file_list * fl;
3203 struct url_actions * b;
3205 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3207 return cgi_error_disabled(csp, rsp);
3210 err = get_number_param(csp, parameters, "s", §ionid);
3216 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3219 /* No filename specified, can't read file, modified, or out of memory. */
3220 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3223 cur_line = file->lines;
3225 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3227 cur_line = cur_line->next;
3230 if ( (cur_line == NULL)
3231 || (line_number != sectionid)
3233 || (cur_line->type != FILE_LINE_ACTION))
3235 /* Invalid "sectionid" parameter */
3236 edit_free_file(file);
3237 return JB_ERR_CGI_PARAMS;
3240 get_string_param(parameters, "p", &action_set_name);
3241 if (action_set_name != NULL)
3243 for (index = 0; index < MAX_ACTION_FILES; index++)
3245 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3247 for (b = b->next; NULL != b; b = b->next)
3249 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3251 copy_action(cur_line->data.action, b->action);
3257 edit_free_file(file);
3258 return JB_ERR_CGI_PARAMS;
3264 err = actions_from_radio(parameters, cur_line->data.action);
3270 edit_free_file(file);
3274 ch = get_char_param(parameters, "filter_all");
3277 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3278 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3279 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3283 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3286 for (index = 0; !err; index++)
3293 /* Generate the keys */
3294 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3295 key_value[sizeof(key_value) - 1] = '\0';
3296 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3297 key_name[sizeof(key_name) - 1] = '\0';
3299 err = get_string_param(parameters, key_name, &name);
3308 value = get_char_param(parameters, key_value);
3311 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3312 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3313 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3315 else if (value == 'N')
3317 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3318 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3320 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3321 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3324 else if (value == 'X')
3326 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3327 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3334 edit_free_file(file);
3338 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3341 edit_free_file(file);
3342 return JB_ERR_MEMORY;
3345 len = strlen(actiontext);
3349 * Empty action - must special-case this.
3350 * Simply setting len to 1 is sufficient...
3355 if (NULL == (newtext = malloc(len + 2)))
3359 edit_free_file(file);
3360 return JB_ERR_MEMORY;
3362 strcpy(newtext, actiontext);
3366 newtext[len + 1] = '\0';
3368 freez(cur_line->raw);
3369 freez(cur_line->unprocessed);
3370 cur_line->unprocessed = newtext;
3372 err = edit_write_file(file);
3375 /* Error writing file */
3376 if (err == JB_ERR_FILE)
3378 /* Read-only file. */
3379 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3381 edit_free_file(file);
3385 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3386 string_append(&target, file->identifier);
3387 string_join(&target, section_target(sectionid));
3390 edit_free_file(file);
3395 return JB_ERR_MEMORY;
3398 rsp->status = strdup("302 Local Redirect from Privoxy");
3399 if (rsp->status == NULL)
3402 return JB_ERR_MEMORY;
3404 err = enlist_unique_header(rsp->headers, "Location", target);
3411 /*********************************************************************
3413 * Function : cgi_edit_actions_url
3415 * Description : CGI function that actually edits a URL pattern in
3419 * 1 : csp = Current client state (buffers, headers, etc...)
3420 * 2 : rsp = http_response data structure for output
3421 * 3 : parameters = map of cgi parameters
3424 * filename : Identifies the file to edit
3425 * ver : File's last-modified time
3426 * section : Line number of section to edit
3427 * pattern : Line number of pattern to edit
3428 * newval : New value for pattern
3430 * Returns : JB_ERR_OK on success
3431 * JB_ERR_MEMORY on out-of-memory
3432 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3433 * specified or not valid.
3435 *********************************************************************/
3436 jb_err cgi_edit_actions_url(struct client_state *csp,
3437 struct http_response *rsp,
3438 const struct map *parameters)
3442 struct editable_file * file;
3443 struct file_line * cur_line;
3444 unsigned line_number;
3445 unsigned section_start_line_number = 0;
3449 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3451 return cgi_error_disabled(csp, rsp);
3454 err = get_number_param(csp, parameters, "p", &patternid);
3461 return JB_ERR_CGI_PARAMS;
3464 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3470 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3473 /* No filename specified, can't read file, modified, or out of memory. */
3475 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3479 cur_line = file->lines;
3481 while ((cur_line != NULL) && (line_number < patternid))
3483 if (cur_line->type == FILE_LINE_ACTION)
3485 section_start_line_number = line_number;
3487 cur_line = cur_line->next;
3491 if ( (cur_line == NULL)
3492 || (cur_line->type != FILE_LINE_URL))
3494 /* Invalid "patternid" parameter */
3496 edit_free_file(file);
3497 return JB_ERR_CGI_PARAMS;
3500 /* At this point, the line to edit is in cur_line */
3502 freez(cur_line->raw);
3503 freez(cur_line->unprocessed);
3504 cur_line->unprocessed = new_pattern;
3506 err = edit_write_file(file);
3509 /* Error writing file */
3510 if (err == JB_ERR_FILE)
3512 /* Read-only file. */
3513 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3515 edit_free_file(file);
3519 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3520 string_append(&target, file->identifier);
3521 string_join(&target, section_target(section_start_line_number));
3523 edit_free_file(file);
3528 return JB_ERR_MEMORY;
3531 rsp->status = strdup("302 Local Redirect from Privoxy");
3532 if (rsp->status == NULL)
3535 return JB_ERR_MEMORY;
3537 err = enlist_unique_header(rsp->headers, "Location", target);
3544 /*********************************************************************
3546 * Function : cgi_edit_actions_add_url
3548 * Description : CGI function that actually adds a URL pattern to
3552 * 1 : csp = Current client state (buffers, headers, etc...)
3553 * 2 : rsp = http_response data structure for output
3554 * 3 : parameters = map of cgi parameters
3557 * filename : Identifies the file to edit
3558 * ver : File's last-modified time
3559 * section : Line number of section to edit
3560 * newval : New pattern
3562 * Returns : JB_ERR_OK on success
3563 * JB_ERR_MEMORY on out-of-memory
3564 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3565 * specified or not valid.
3567 *********************************************************************/
3568 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3569 struct http_response *rsp,
3570 const struct map *parameters)
3574 struct file_line * new_line;
3575 struct editable_file * file;
3576 struct file_line * cur_line;
3577 unsigned line_number;
3581 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3583 return cgi_error_disabled(csp, rsp);
3586 err = get_number_param(csp, parameters, "s", §ionid);
3593 return JB_ERR_CGI_PARAMS;
3596 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3602 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3605 /* No filename specified, can't read file, modified, or out of memory. */
3607 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3611 cur_line = file->lines;
3613 while ((cur_line != NULL) && (line_number < sectionid))
3615 cur_line = cur_line->next;
3619 if ( (cur_line == NULL)
3620 || (cur_line->type != FILE_LINE_ACTION))
3622 /* Invalid "sectionid" parameter */
3624 edit_free_file(file);
3625 return JB_ERR_CGI_PARAMS;
3628 /* At this point, the section header is in cur_line - add after this. */
3630 /* Allocate the new line */
3631 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3632 if (new_line == NULL)
3635 edit_free_file(file);
3636 return JB_ERR_MEMORY;
3639 /* Fill in the data members of the new line */
3640 new_line->raw = NULL;
3641 new_line->prefix = NULL;
3642 new_line->unprocessed = new_pattern;
3643 new_line->type = FILE_LINE_URL;
3645 /* Link new_line into the list, after cur_line */
3646 new_line->next = cur_line->next;
3647 cur_line->next = new_line;
3649 /* Done making changes, now commit */
3651 err = edit_write_file(file);
3654 /* Error writing file */
3655 if (err == JB_ERR_FILE)
3657 /* Read-only file. */
3658 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3660 edit_free_file(file);
3664 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3665 string_append(&target, file->identifier);
3666 string_join(&target, section_target(sectionid));
3668 edit_free_file(file);
3673 return JB_ERR_MEMORY;
3676 rsp->status = strdup("302 Local Redirect from Privoxy");
3677 if (rsp->status == NULL)
3680 return JB_ERR_MEMORY;
3682 err = enlist_unique_header(rsp->headers, "Location", target);
3689 /*********************************************************************
3691 * Function : cgi_edit_actions_remove_url
3693 * Description : CGI function that actually removes a URL pattern from
3697 * 1 : csp = Current client state (buffers, headers, etc...)
3698 * 2 : rsp = http_response data structure for output
3699 * 3 : parameters = map of cgi parameters
3702 * f : (filename) Identifies the file to edit
3703 * v : (version) File's last-modified time
3704 * p : (pattern) Line number of pattern to remove
3706 * Returns : JB_ERR_OK on success
3707 * JB_ERR_MEMORY on out-of-memory
3708 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3709 * specified or not valid.
3711 *********************************************************************/
3712 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3713 struct http_response *rsp,
3714 const struct map *parameters)
3717 struct editable_file * file;
3718 struct file_line * cur_line;
3719 struct file_line * prev_line;
3720 unsigned line_number;
3721 unsigned section_start_line_number = 0;
3725 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3727 return cgi_error_disabled(csp, rsp);
3730 err = get_number_param(csp, parameters, "p", &patternid);
3736 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3739 /* No filename specified, can't read file, modified, or out of memory. */
3740 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3745 cur_line = file->lines;
3747 while ((cur_line != NULL) && (line_number < patternid))
3749 if (cur_line->type == FILE_LINE_ACTION)
3751 section_start_line_number = line_number;
3753 prev_line = cur_line;
3754 cur_line = cur_line->next;
3758 if ( (cur_line == NULL)
3759 || (prev_line == NULL)
3760 || (cur_line->type != FILE_LINE_URL))
3762 /* Invalid "patternid" parameter */
3763 edit_free_file(file);
3764 return JB_ERR_CGI_PARAMS;
3767 /* At this point, the line to remove is in cur_line, and the previous
3768 * one is in prev_line
3771 /* Unlink cur_line */
3772 prev_line->next = cur_line->next;
3773 cur_line->next = NULL;
3776 edit_free_file_lines(cur_line);
3778 err = edit_write_file(file);
3781 /* Error writing file */
3782 if (err == JB_ERR_FILE)
3784 /* Read-only file. */
3785 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3787 edit_free_file(file);
3791 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3792 string_append(&target, file->identifier);
3793 string_join(&target, section_target(section_start_line_number));
3795 edit_free_file(file);
3800 return JB_ERR_MEMORY;
3803 rsp->status = strdup("302 Local Redirect from Privoxy");
3804 if (rsp->status == NULL)
3807 return JB_ERR_MEMORY;
3809 err = enlist_unique_header(rsp->headers, "Location", target);
3816 /*********************************************************************
3818 * Function : cgi_edit_actions_section_remove
3820 * Description : CGI function that actually removes a whole section from
3821 * the actions file. The section must be empty first
3822 * (else JB_ERR_CGI_PARAMS).
3825 * 1 : csp = Current client state (buffers, headers, etc...)
3826 * 2 : rsp = http_response data structure for output
3827 * 3 : parameters = map of cgi parameters
3830 * f : (filename) Identifies the file to edit
3831 * v : (version) File's last-modified time
3832 * s : (section) Line number of section to edit
3834 * Returns : JB_ERR_OK on success
3835 * JB_ERR_MEMORY on out-of-memory
3836 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3837 * specified or not valid.
3839 *********************************************************************/
3840 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3841 struct http_response *rsp,
3842 const struct map *parameters)
3845 struct editable_file * file;
3846 struct file_line * cur_line;
3847 struct file_line * prev_line;
3848 unsigned line_number;
3852 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3854 return cgi_error_disabled(csp, rsp);
3857 err = get_number_param(csp, parameters, "s", §ionid);
3863 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3866 /* No filename specified, can't read file, modified, or out of memory. */
3867 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3871 cur_line = file->lines;
3874 while ((cur_line != NULL) && (line_number < sectionid))
3876 prev_line = cur_line;
3877 cur_line = cur_line->next;
3881 if ( (cur_line == NULL)
3882 || (cur_line->type != FILE_LINE_ACTION) )
3884 /* Invalid "sectionid" parameter */
3885 edit_free_file(file);
3886 return JB_ERR_CGI_PARAMS;
3889 if ( (cur_line->next != NULL)
3890 && (cur_line->next->type == FILE_LINE_URL) )
3892 /* Section not empty. */
3893 edit_free_file(file);
3894 return JB_ERR_CGI_PARAMS;
3897 /* At this point, the line to remove is in cur_line, and the previous
3898 * one is in prev_line
3901 /* Unlink cur_line */
3902 if (prev_line == NULL)
3904 /* Removing the first line from the file */
3905 file->lines = cur_line->next;
3909 prev_line->next = cur_line->next;
3911 cur_line->next = NULL;
3914 edit_free_file_lines(cur_line);
3916 err = edit_write_file(file);
3919 /* Error writing file */
3920 if (err == JB_ERR_FILE)
3922 /* Read-only file. */
3923 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3925 edit_free_file(file);
3929 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3930 string_append(&target, file->identifier);
3932 edit_free_file(file);
3937 return JB_ERR_MEMORY;
3940 rsp->status = strdup("302 Local Redirect from Privoxy");
3941 if (rsp->status == NULL)
3944 return JB_ERR_MEMORY;
3946 err = enlist_unique_header(rsp->headers, "Location", target);
3953 /*********************************************************************
3955 * Function : cgi_edit_actions_section_add
3957 * Description : CGI function that adds a new empty section to
3961 * 1 : csp = Current client state (buffers, headers, etc...)
3962 * 2 : rsp = http_response data structure for output
3963 * 3 : parameters = map of cgi parameters
3966 * f : (filename) Identifies the file to edit
3967 * v : (version) File's last-modified time
3968 * s : (section) Line number of section to add after, 0 for
3971 * Returns : JB_ERR_OK on success
3972 * JB_ERR_MEMORY on out-of-memory
3973 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3974 * specified or not valid.
3976 *********************************************************************/
3977 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3978 struct http_response *rsp,
3979 const struct map *parameters)
3982 struct file_line * new_line;
3984 struct editable_file * file;
3985 struct file_line * cur_line;
3986 unsigned line_number;
3990 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3992 return cgi_error_disabled(csp, rsp);
3995 err = get_number_param(csp, parameters, "s", §ionid);
4001 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4004 /* No filename specified, can't read file, modified, or out of memory. */
4005 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4009 cur_line = file->lines;
4011 if (sectionid <= 1U)
4013 /* Add to start of file */
4014 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4016 /* There's something in the file, find the line before the first
4019 while ( (cur_line->next != NULL)
4020 && (cur_line->next->type != FILE_LINE_ACTION) )
4022 cur_line = cur_line->next;
4028 /* File starts with action line, so insert at top */
4034 /* Add after stated section. */
4035 while ((cur_line != NULL) && (line_number < sectionid))
4037 cur_line = cur_line->next;
4041 if ( (cur_line == NULL)
4042 || (cur_line->type != FILE_LINE_ACTION))
4044 /* Invalid "sectionid" parameter */
4045 edit_free_file(file);
4046 return JB_ERR_CGI_PARAMS;
4049 /* Skip through the section to find the last line in it. */
4050 while ( (cur_line->next != NULL)
4051 && (cur_line->next->type != FILE_LINE_ACTION) )
4053 cur_line = cur_line->next;
4058 /* At this point, the last line in the previous section is in cur_line
4059 * - add after this. (Or if we need to add as the first line, cur_line
4063 new_text = strdup("{}");
4064 if (NULL == new_text)
4066 edit_free_file(file);
4067 return JB_ERR_MEMORY;
4070 /* Allocate the new line */
4071 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4072 if (new_line == NULL)
4075 edit_free_file(file);
4076 return JB_ERR_MEMORY;
4079 /* Fill in the data members of the new line */
4080 new_line->raw = NULL;
4081 new_line->prefix = NULL;
4082 new_line->unprocessed = new_text;
4083 new_line->type = FILE_LINE_ACTION;
4085 if (cur_line != NULL)
4087 /* Link new_line into the list, after cur_line */
4088 new_line->next = cur_line->next;
4089 cur_line->next = new_line;
4093 /* Link new_line into the list, as first line */
4094 new_line->next = file->lines;
4095 file->lines = new_line;
4098 /* Done making changes, now commit */
4100 err = edit_write_file(file);
4103 /* Error writing file */
4104 if (err == JB_ERR_FILE)
4106 /* Read-only file. */
4107 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4109 edit_free_file(file);
4113 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4114 string_append(&target, file->identifier);
4116 edit_free_file(file);
4121 return JB_ERR_MEMORY;
4124 rsp->status = strdup("302 Local Redirect from Privoxy");
4125 if (rsp->status == NULL)
4128 return JB_ERR_MEMORY;
4130 err = enlist_unique_header(rsp->headers, "Location", target);
4137 /*********************************************************************
4139 * Function : cgi_edit_actions_section_swap
4141 * Description : CGI function that swaps the order of two sections
4142 * in the actions file. Note that this CGI can actually
4143 * swap any two arbitrary sections, but the GUI interface
4144 * currently only allows consecutive sections to be
4148 * 1 : csp = Current client state (buffers, headers, etc...)
4149 * 2 : rsp = http_response data structure for output
4150 * 3 : parameters = map of cgi parameters
4153 * f : (filename) Identifies the file to edit
4154 * v : (version) File's last-modified time
4155 * s1 : (section1) Line number of first section to swap
4156 * s2 : (section2) Line number of second section to swap
4158 * Returns : JB_ERR_OK on success
4159 * JB_ERR_MEMORY on out-of-memory
4160 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4161 * specified or not valid.
4163 *********************************************************************/
4164 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4165 struct http_response *rsp,
4166 const struct map *parameters)
4170 struct editable_file * file;
4171 struct file_line * cur_line;
4172 struct file_line * prev_line;
4173 struct file_line * line_before_section1;
4174 struct file_line * line_start_section1;
4175 struct file_line * line_end_section1;
4176 struct file_line * line_after_section1;
4177 struct file_line * line_before_section2;
4178 struct file_line * line_start_section2;
4179 struct file_line * line_end_section2;
4180 struct file_line * line_after_section2;
4181 unsigned line_number;
4185 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4187 return cgi_error_disabled(csp, rsp);
4190 err = get_number_param(csp, parameters, "s1", §ion1);
4191 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4197 if (section1 > section2)
4199 unsigned temp = section2;
4200 section2 = section1;
4204 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4207 /* No filename specified, can't read file, modified, or out of memory. */
4208 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4211 /* Start at the beginning... */
4213 cur_line = file->lines;
4216 /* ... find section1 ... */
4217 while ((cur_line != NULL) && (line_number < section1))
4219 prev_line = cur_line;
4220 cur_line = cur_line->next;
4224 if ( (cur_line == NULL)
4225 || (cur_line->type != FILE_LINE_ACTION) )
4227 /* Invalid "section1" parameter */
4228 edit_free_file(file);
4229 return JB_ERR_CGI_PARAMS;
4232 /* If no-op, we've validated params and can skip the rest. */
4233 if (section1 != section2)
4235 /* ... find the end of section1 ... */
4236 line_before_section1 = prev_line;
4237 line_start_section1 = cur_line;
4240 prev_line = cur_line;
4241 cur_line = cur_line->next;
4244 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4245 line_end_section1 = prev_line;
4246 line_after_section1 = cur_line;
4248 /* ... find section2 ... */
4249 while ((cur_line != NULL) && (line_number < section2))
4251 prev_line = cur_line;
4252 cur_line = cur_line->next;
4256 if ( (cur_line == NULL)
4257 || (cur_line->type != FILE_LINE_ACTION) )
4259 /* Invalid "section2" parameter */
4260 edit_free_file(file);
4261 return JB_ERR_CGI_PARAMS;
4264 /* ... find the end of section2 ... */
4265 line_before_section2 = prev_line;
4266 line_start_section2 = cur_line;
4269 prev_line = cur_line;
4270 cur_line = cur_line->next;
4273 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4274 line_end_section2 = prev_line;
4275 line_after_section2 = cur_line;
4277 /* Now have all the pointers we need. Do the swap. */
4279 /* Change the pointer to section1 to point to section2 instead */
4280 if (line_before_section1 == NULL)
4282 file->lines = line_start_section2;
4286 line_before_section1->next = line_start_section2;
4289 if (line_before_section2 == line_end_section1)
4291 /* Consecutive sections */
4292 line_end_section2->next = line_start_section1;
4296 line_end_section2->next = line_after_section1;
4297 line_before_section2->next = line_start_section1;
4300 /* Set the pointer from the end of section1 to the rest of the file */
4301 line_end_section1->next = line_after_section2;
4303 err = edit_write_file(file);
4306 /* Error writing file */
4307 if (err == JB_ERR_FILE)
4309 /* Read-only file. */
4310 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4312 edit_free_file(file);
4315 } /* END if (section1 != section2) */
4317 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4318 string_append(&target, file->identifier);
4320 edit_free_file(file);
4325 return JB_ERR_MEMORY;
4328 rsp->status = strdup("302 Local Redirect from Privoxy");
4329 if (rsp->status == NULL)
4332 return JB_ERR_MEMORY;
4334 err = enlist_unique_header(rsp->headers, "Location", target);
4341 /*********************************************************************
4343 * Function : cgi_toggle
4345 * Description : CGI function that adds a new empty section to
4349 * 1 : csp = Current client state (buffers, headers, etc...)
4350 * 2 : rsp = http_response data structure for output
4351 * 3 : parameters = map of cgi parameters
4354 * set : If present, how to change toggle setting:
4355 * "enable", "disable", "toggle", or none (default).
4356 * mini : If present, use mini reply template.
4358 * Returns : JB_ERR_OK on success
4359 * JB_ERR_MEMORY on out-of-memory
4361 *********************************************************************/
4362 jb_err cgi_toggle(struct client_state *csp,
4363 struct http_response *rsp,
4364 const struct map *parameters)
4366 struct map *exports;
4368 const char *template_name;
4374 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4376 return cgi_error_disabled(csp, rsp);
4379 mode = get_char_param(parameters, "set");
4384 global_toggle_state = 1;
4386 else if (mode == 'D')
4389 global_toggle_state = 0;
4391 else if (mode == 'T')
4394 global_toggle_state = !global_toggle_state;
4397 if (NULL == (exports = default_exports(csp, "toggle")))
4399 return JB_ERR_MEMORY;
4402 template_name = (get_char_param(parameters, "mini")
4406 return template_fill_for_cgi(csp, template_name, exports, rsp);
4410 /*********************************************************************
4412 * Function : javascriptify
4414 * Description : Converts a string into a form JavaScript will like.
4416 * Netscape 4's JavaScript sucks - it doesn't use
4417 * "id" parameters, so you have to set the "name"
4418 * used to submit a form element to something JavaScript
4419 * will like. (Or access the elements by index in an
4420 * array. That array contains >60 elements and will
4421 * be changed whenever we add a new action to the
4422 * editor, so I'm NOT going to use indexes that have
4423 * to be figured out by hand.)
4425 * Currently the only thing we have to worry about
4426 * is "-" ==> "_" conversion.
4428 * This is a length-preserving operation so it is
4429 * carried out in-place, no memory is allocated
4433 * 1 : identifier = String to make JavaScript-friendly.
4437 *********************************************************************/
4438 static void javascriptify(char * identifier)
4440 char * p = identifier;
4441 while (NULL != (p = strchr(p, '-')))
4448 /*********************************************************************
4450 * Function : actions_to_radio
4452 * Description : Converts a actionsfile entry into settings for
4453 * radio buttons and edit boxes on a HTML form.
4456 * 1 : exports = List of substitutions to add to.
4457 * 2 : action = Action to read
4459 * Returns : JB_ERR_OK on success
4460 * JB_ERR_MEMORY on out-of-memory
4462 *********************************************************************/
4463 static jb_err actions_to_radio(struct map * exports,
4464 const struct action_spec *action)
4466 unsigned mask = action->mask;
4467 unsigned add = action->add;
4475 mask = action->mask;
4478 /* sanity - prevents "-feature +feature" */
4482 #define DEFINE_ACTION_BOOL(name, bit) \
4483 if (!(mask & bit)) \
4485 current_mode = 'n'; \
4487 else if (add & bit) \
4489 current_mode = 'y'; \
4493 current_mode = 'x'; \
4495 if (map_radio(exports, name, "ynx", current_mode)) \
4497 return JB_ERR_MEMORY; \
4500 #define DEFINE_ACTION_STRING(name, bit, index) \
4501 DEFINE_ACTION_BOOL(name, bit); \
4504 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4507 checked = !strcmp(action->string[index], value); \
4511 checked = is_default; \
4513 mapped_param |= checked; \
4514 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4516 return JB_ERR_MEMORY; \
4519 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4520 if (map(exports, name "-param-custom", 1, \
4521 ((!mapped_param) ? "checked" : ""), 1)) \
4523 return JB_ERR_MEMORY; \
4525 if (map(exports, name "-param", 1, \
4526 (((add & bit) && !mapped_param) ? \
4527 action->string[index] : default_val), 1)) \
4529 return JB_ERR_MEMORY; \
4532 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4533 if (map(exports, name "-param", 1, \
4534 ((add & bit) ? action->string[index] : default_val), 1)) \
4536 return JB_ERR_MEMORY; \
4539 #define DEFINE_ACTION_MULTI(name, index) \
4540 if (action->multi_add[index]->first) \
4542 current_mode = 'y'; \
4544 else if (action->multi_remove_all[index]) \
4546 current_mode = 'n'; \
4548 else if (action->multi_remove[index]->first) \
4550 current_mode = 'y'; \
4554 current_mode = 'x'; \
4556 if (map_radio(exports, name, "ynx", current_mode)) \
4558 return JB_ERR_MEMORY; \
4561 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4563 #include "actionlist.h"
4565 #undef DEFINE_ACTION_MULTI
4566 #undef DEFINE_ACTION_STRING
4567 #undef DEFINE_ACTION_BOOL
4568 #undef DEFINE_ACTION_ALIAS
4569 #undef DEFINE_CGI_PARAM_CUSTOM
4570 #undef DEFINE_CGI_PARAM_RADIO
4571 #undef DEFINE_CGI_PARAM_NO_RADIO
4577 /*********************************************************************
4579 * Function : actions_from_radio
4581 * Description : Converts a map of parameters passed to a CGI function
4582 * into an actionsfile entry.
4585 * 1 : parameters = parameters to the CGI call
4586 * 2 : action = Action to change. Must be valid before
4587 * the call, actions not specified will be
4590 * Returns : JB_ERR_OK on success
4591 * JB_ERR_MEMORY on out-of-memory
4593 *********************************************************************/
4594 static jb_err actions_from_radio(const struct map * parameters,
4595 struct action_spec *action)
4600 const char * js_name;
4601 jb_err err = JB_ERR_OK;
4606 /* Statics are generally a potential race condition,
4607 * but in this case we're safe and don't need semaphores.
4608 * Be careful if you modify this function.
4610 * The js_name_arr's are never free()d, but this is no
4611 * problem, since they will only be created once and
4612 * used by all threads thereafter. -oes
4615 #define JAVASCRIPTIFY(dest_var, string) \
4617 static int first_time = 1; \
4618 static char *js_name_arr; \
4621 js_name_arr = strdup(string); \
4622 javascriptify(js_name_arr); \
4624 dest_var = js_name_arr; \
4628 #define DEFINE_ACTION_BOOL(name, bit) \
4629 JAVASCRIPTIFY(js_name, name); \
4630 ch = get_char_param(parameters, js_name); \
4633 action->add |= bit; \
4634 action->mask |= bit; \
4636 else if (ch == 'N') \
4638 action->add &= ~bit; \
4639 action->mask &= ~bit; \
4641 else if (ch == 'X') \
4643 action->add &= ~bit; \
4644 action->mask |= bit; \
4647 #define DEFINE_ACTION_STRING(name, bit, index) \
4648 JAVASCRIPTIFY(js_name, name); \
4649 ch = get_char_param(parameters, js_name); \
4653 JAVASCRIPTIFY(js_name, name "-mode"); \
4654 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4655 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4657 JAVASCRIPTIFY(js_name, name "-param"); \
4658 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4660 if (param != NULL) \
4662 if (NULL == (param_dup = strdup(param))) \
4664 return JB_ERR_MEMORY; \
4666 freez(action->string[index]); \
4667 action->add |= bit; \
4668 action->mask |= bit; \
4669 action->string[index] = param_dup; \
4672 else if (ch == 'N') \
4674 if (action->add & bit) \
4676 freez(action->string[index]); \
4678 action->add &= ~bit; \
4679 action->mask &= ~bit; \
4681 else if (ch == 'X') \
4683 if (action->add & bit) \
4685 freez(action->string[index]); \
4687 action->add &= ~bit; \
4688 action->mask |= bit; \
4691 #define DEFINE_ACTION_MULTI(name, index) \
4692 JAVASCRIPTIFY(js_name, name); \
4693 ch = get_char_param(parameters, js_name); \
4698 else if (ch == 'N') \
4700 list_remove_all(action->multi_add[index]); \
4701 list_remove_all(action->multi_remove[index]); \
4702 action->multi_remove_all[index] = 1; \
4704 else if (ch == 'X') \
4706 list_remove_all(action->multi_add[index]); \
4707 list_remove_all(action->multi_remove[index]); \
4708 action->multi_remove_all[index] = 0; \
4711 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4713 #include "actionlist.h"
4715 #undef DEFINE_ACTION_MULTI
4716 #undef DEFINE_ACTION_STRING
4717 #undef DEFINE_ACTION_BOOL
4718 #undef DEFINE_ACTION_ALIAS
4719 #undef JAVASCRIPTIFY
4725 #endif /* def FEATURE_CGI_EDIT_ACTIONS */