1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.41.2.1 2002/08/02 12:43:14 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.1 2002/08/02 12:43:14 oes
46 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
48 * Revision 1.41 2002/05/21 19:09:45 oes
49 * - Made Add/Edit/Remove URL Submit and Cancel
50 * buttons jump back to relevant section in eal
51 * - Bugfix: remove-url-form needs p export
53 * Revision 1.40 2002/05/19 11:34:35 jongfoster
54 * Handling read-only actions files better - report the actual
55 * error, not "Out of memory"!
58 * http://sourceforge.net/tracker/index.php?func=detail
59 * &aid=557905&group_id=11118&atid=111118
61 * Revision 1.39 2002/05/12 21:39:15 jongfoster
62 * - Adding Doxygen-style comments to structures and #defines.
63 * - Correcting function comments
65 * Revision 1.38 2002/05/03 23:00:38 jongfoster
66 * Support for templates for "standard actions" buttons.
69 * Revision 1.37 2002/04/30 11:14:52 oes
70 * Made csp the first parameter in *action_to_html
72 * Revision 1.36 2002/04/26 21:53:30 jongfoster
73 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
75 * Revision 1.35 2002/04/26 21:50:02 jongfoster
76 * Honouring default exports in edit-actions-for-url-filter template.
78 * Revision 1.34 2002/04/26 12:54:17 oes
79 * Adaptions to changes in actions.c
81 * Revision 1.33 2002/04/24 02:17:47 oes
82 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
84 * - Activated Jon's code for editing multiple AFs
85 * - cgi_edit_list_actions now provides context-sensitive
86 * help, looks up all action sets from standard.action and
87 * makes buttons for them in the catchall section
88 * - cgi_edit_action_submit now honors a p parameter, looks up
89 * the corresponding action set, and sets the catchall pattern's
90 * actions accordingly.
92 * Revision 1.32 2002/04/19 16:55:31 jongfoster
93 * Fixing newline problems. If we do our own text file newline
94 * mangling, we don't want the library to do any, so we need to
95 * open the files in *binary* mode.
97 * Revision 1.31 2002/04/18 19:21:08 jongfoster
98 * Added code to detect "conventional" action files, that start
99 * with a set of actions for all URLs (the pattern "/").
100 * These are special-cased in the "edit-actions-list" CGI, so
101 * that a special UI can be written for them.
103 * Revision 1.30 2002/04/10 13:38:35 oes
104 * load_template signature changed
106 * Revision 1.29 2002/04/08 16:59:08 oes
109 * Revision 1.28 2002/03/27 12:30:29 oes
110 * Deleted unsused variable
112 * Revision 1.27 2002/03/26 23:06:04 jongfoster
113 * Removing duplicate @ifs on the toggle page
115 * Revision 1.26 2002/03/26 22:59:17 jongfoster
116 * Fixing /toggle to display status consistently.
118 * Revision 1.25 2002/03/26 22:29:54 swa
119 * we have a new homepage!
121 * Revision 1.24 2002/03/24 15:23:33 jongfoster
124 * Revision 1.23 2002/03/24 13:32:41 swa
125 * name change related issues
127 * Revision 1.22 2002/03/24 13:25:43 swa
128 * name change related issues
130 * Revision 1.21 2002/03/22 18:02:48 jongfoster
131 * Fixing remote toggle
133 * Revision 1.20 2002/03/16 20:28:34 oes
134 * Added descriptions to the filters so users will know what they select in the cgi editor
136 * Revision 1.19 2002/03/16 18:38:14 jongfoster
137 * Stopping stupid or malicious users from breaking the actions
138 * file using the web-based editor.
140 * Revision 1.18 2002/03/16 14:57:44 jongfoster
141 * Full support for enabling/disabling modular filters.
143 * Revision 1.17 2002/03/16 14:26:42 jongfoster
144 * First version of modular filters support - READ ONLY!
145 * Fixing a double-free bug in the out-of-memory handling in map_radio().
147 * Revision 1.16 2002/03/07 03:46:17 oes
148 * Fixed compiler warnings
150 * Revision 1.15 2002/03/06 22:54:35 jongfoster
151 * Automated function-comment nitpicking.
153 * Revision 1.14 2002/03/05 00:24:51 jongfoster
154 * Patch to always edit the current actions file.
156 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
157 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
159 * Revision 1.12 2002/03/03 09:18:03 joergs
160 * Made jumbjuster work on AmigaOS again.
162 * Revision 1.11 2002/01/23 01:03:31 jongfoster
163 * Fixing gcc [CygWin] compiler warnings
165 * Revision 1.10 2002/01/23 00:22:59 jongfoster
166 * Adding new function cgi_edit_actions_section_swap(), to reorder
169 * Adding get_url_spec_param() to get a validated URL pattern.
171 * Moving edit_read_line() out of this file and into loaders.c.
173 * Adding missing html_encode() to many CGI functions.
175 * Moving the functions that #include actionlist.h to the end of the file,
176 * because the Visual C++ 97 debugger gets extremely confused if you try
177 * to debug any code that comes after them in the file.
179 * Major optimizations in cgi_edit_actions_list() to reduce the size of
180 * the generated HTML (down 40% from 550k to 304k), with major side-effects
181 * throughout the editor and templates. In particular, the length of the
182 * URLs throughout the editor has been drastically reduced, by cutting
183 * paramater names down to 1 character and CGI names down to 3-4
184 * characters, by removing all non-essential CGI paramaters even at the
185 * expense of having to re-read the actions file for the most trivial
186 * page, and by using relative rather than absolute URLs. This means
187 * that this (typical example):
189 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
190 * filename=ijb&ver=1011487572&section=12&pattern=13
191 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
195 * <a href="eau?f=ijb&v=1011487572&p=13">
197 * Revision 1.9 2002/01/17 20:56:22 jongfoster
198 * Replacing hard references to the URL of the config interface
199 * with #defines from project.h
201 * Revision 1.8 2001/11/30 23:35:51 jongfoster
202 * Renaming actionsfile to ijb.action
204 * Revision 1.7 2001/11/13 00:28:24 jongfoster
205 * - Renaming parameters from edit-actions-for-url so that they only
206 * contain legal JavaScript characters. If we wanted to write
207 * JavaScript that worked with Netscape 4, this is nessacery.
208 * (Note that at the moment the JavaScript doesn't actually work
209 * with Netscape 4, but now this is purely a template issue, not
210 * one affecting code).
211 * - Adding new CGIs for use by non-JavaScript browsers:
212 * edit-actions-url-form
213 * edit-actions-add-url-form
214 * edit-actions-remove-url-form
217 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
218 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
219 * by and __OS2__ ifdef.
221 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
222 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
223 * threads to call select() simultaneously. So, it's time to do a real, live,
224 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
225 * (native). Both versions will work, but using __OS2__ offers multi-threading.
227 * Revision 1.4 2001/10/23 21:48:19 jongfoster
228 * Cleaning up error handling in CGI functions - they now send back
229 * a HTML error page and should never cause a FATAL error. (Fixes one
230 * potential source of "denial of service" attacks).
232 * CGI actions file editor that works and is actually useful.
234 * Ability to toggle JunkBuster remotely using a CGI call.
236 * You can turn off both the above features in the main configuration
237 * file, e.g. if you are running a multi-user proxy.
239 * Revision 1.3 2001/10/14 22:12:49 jongfoster
240 * New version of CGI-based actionsfile editor.
241 * Major changes, including:
242 * - Completely new file parser and file output routines
243 * - edit-actions CGI renamed edit-actions-for-url
244 * - All CGIs now need a filename parameter, except for...
245 * - New CGI edit-actions which doesn't need a filename,
246 * to allow you to start the editor up.
247 * - edit-actions-submit now works, and now automatically
248 * redirects you back to the main edit-actions-list handler.
250 * Revision 1.2 2001/09/16 17:05:14 jongfoster
251 * Removing unused #include showarg.h
253 * Revision 1.1 2001/09/16 15:47:37 jongfoster
254 * First version of CGI-based edit interface. This is very much a
255 * work-in-progress, and you can't actually use it to edit anything
256 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
257 * to have any effect.
260 **********************************************************************/
266 * FIXME: Following includes copied from cgi.c - which are actually needed?
271 #include <sys/types.h>
275 #include <sys/stat.h>
278 #define snprintf _snprintf
279 #endif /* def _WIN32 */
284 #include "cgisimple.h"
288 #include "miscutil.h"
292 /* loadcfg.h is for g_bToggleIJB only */
293 #include "urlmatch.h"
295 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
298 #ifdef FEATURE_CGI_EDIT_ACTIONS
301 * A line in an editable_file.
305 /** Next entry in the linked list */
306 struct file_line * next;
308 /** The raw data, to write out if this line is unmodified. */
311 /** Comments and/or whitespace to put before this line if it's modified
312 and then written out. */
315 /** The actual data, as a string. Line continuation and comment removal
316 are performed on the data read from file before it's stored here, so
317 it will be a single line of data. */
320 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
323 /** The actual data, processed into some sensible data type. */
327 /** An action specification. */
328 struct action_spec action[1];
330 /** A name=value pair. */
334 /** The name in the name=value pair. */
337 /** The value in the name=value pair, as a string. */
340 /** The value in the name=value pair, as an integer. */
345 /* Add more data types here... e.g.
348 struct url_spec url[1];
352 struct action_spec action[1];
362 /** This file_line has not been processed yet. */
363 #define FILE_LINE_UNPROCESSED 1
365 /** This file_line is blank. Can only appear at the end of a file, due to
366 the way the parser works. */
367 #define FILE_LINE_BLANK 2
369 /** This file_line says {{alias}}. */
370 #define FILE_LINE_ALIAS_HEADER 3
372 /** This file_line defines an alias. */
373 #define FILE_LINE_ALIAS_ENTRY 4
375 /** This file_line defines an {action}. */
376 #define FILE_LINE_ACTION 5
378 /** This file_line specifies a URL pattern. */
379 #define FILE_LINE_URL 6
381 /** This file_line says {{settings}}. */
382 #define FILE_LINE_SETTINGS_HEADER 7
384 /** This file_line is in a {{settings}} block. */
385 #define FILE_LINE_SETTINGS_ENTRY 8
387 /** This file_line says {{description}}. */
388 #define FILE_LINE_DESCRIPTION_HEADER 9
390 /** This file_line is in a {{description}} block. */
391 #define FILE_LINE_DESCRIPTION_ENTRY 10
395 * A configuration file, in a format that can be edited and written back to
400 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
401 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
402 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
403 /**< Pre-encoded with url_encode() for ease of use. */
404 const char * version_str; /**< Last modification time, as a string. For CGI param. */
405 /**< Can be used in URL without using url_param(). */
406 unsigned version; /**< Last modification time - prevents chaos with
407 the browser's "back" button. Note that this is a
408 time_t cast to an unsigned. When comparing, always
409 cast the time_t to an unsigned, and *NOT* vice-versa.
410 This may lose the top few bits, but they're not
411 significant anyway. */
412 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
413 Note that changing this after the file has been
414 read in will cause a mess. */
415 struct file_line * parse_error; /**< On parse error, this is the offending line. */
416 const char * parse_error_text; /**< On parse error, this is the problem.
417 (Statically allocated) */
420 /* FIXME: Following non-static functions should be prototyped in .h or made static */
422 /* Functions to read and write arbitrary config files */
423 jb_err edit_read_file(struct client_state *csp,
424 const struct map *parameters,
427 struct editable_file **pfile);
428 jb_err edit_write_file(struct editable_file * file);
429 void edit_free_file(struct editable_file * file);
431 /* Functions to read and write actions files */
432 jb_err edit_parse_actions_file(struct editable_file * file);
433 jb_err edit_read_actions_file(struct client_state *csp,
434 struct http_response *rsp,
435 const struct map *parameters,
437 struct editable_file **pfile);
440 jb_err cgi_error_modified(struct client_state *csp,
441 struct http_response *rsp,
442 const char *filename);
443 jb_err cgi_error_parse(struct client_state *csp,
444 struct http_response *rsp,
445 struct editable_file *file);
446 jb_err cgi_error_file(struct client_state *csp,
447 struct http_response *rsp,
448 const char *filename);
449 jb_err cgi_error_file_read_only(struct client_state *csp,
450 struct http_response *rsp,
451 const char *filename);
452 jb_err cgi_error_disabled(struct client_state *csp,
453 struct http_response *rsp);
455 /* Internal arbitrary config file support functions */
456 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
457 static void edit_free_file_lines(struct file_line * first_line);
459 /* Internal actions file support functions */
460 static int match_actions_file_header_line(const char * line, const char * name);
461 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
463 /* Internal parameter parsing functions */
464 static jb_err get_file_name_param(struct client_state *csp,
465 const struct map *parameters,
466 const char *param_name,
469 const char **pparam);
471 static jb_err get_url_spec_param(struct client_state *csp,
472 const struct map *parameters,
477 /* Internal actionsfile <==> HTML conversion functions */
478 static jb_err map_radio(struct map * exports,
479 const char * optionname,
482 static jb_err actions_to_radio(struct map * exports,
483 const struct action_spec *action);
484 static jb_err actions_from_radio(const struct map * parameters,
485 struct action_spec *action);
488 static jb_err map_copy_parameter_html(struct map *out,
489 const struct map *in,
491 #if 0 /* unused function */
492 static jb_err map_copy_parameter_url(struct map *out,
493 const struct map *in,
495 #endif /* unused function */
497 /* Internal convenience functions */
498 static char *section_target(const unsigned sectionid);
500 /*********************************************************************
502 * Function : section_target
504 * Description : Given an unsigned (section id) n, produce a dynamically
505 * allocated string of the form #l<n>, for use in link
509 * 1 : sectionid = start line number of section
511 * Returns : String with link target, or NULL if out of
514 *********************************************************************/
515 static char *section_target(const unsigned sectionid)
519 snprintf(buf, 30, "#l%d", sectionid);
525 /*********************************************************************
527 * Function : map_copy_parameter_html
529 * Description : Copy a CGI parameter from one map to another, HTML
533 * 1 : out = target map
534 * 2 : in = source map
535 * 3 : name = name of cgi parameter to copy
537 * Returns : JB_ERR_OK on success
538 * JB_ERR_MEMORY on out-of-memory
539 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
542 *********************************************************************/
543 static jb_err map_copy_parameter_html(struct map *out,
544 const struct map *in,
554 value = lookup(in, name);
555 err = map(out, name, 1, html_encode(value), 0);
562 else if (*value == '\0')
564 return JB_ERR_CGI_PARAMS;
573 #if 0 /* unused function */
574 /*********************************************************************
576 * Function : map_copy_parameter_url
578 * Description : Copy a CGI parameter from one map to another, URL
582 * 1 : out = target map
583 * 2 : in = source map
584 * 3 : name = name of cgi parameter to copy
586 * Returns : JB_ERR_OK on success
587 * JB_ERR_MEMORY on out-of-memory
588 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
591 *********************************************************************/
592 static jb_err map_copy_parameter_url(struct map *out,
593 const struct map *in,
603 value = lookup(in, name);
604 err = map(out, name, 1, url_encode(value), 0);
611 else if (*value == '\0')
613 return JB_ERR_CGI_PARAMS;
620 #endif /* 0 - unused function */
622 /*********************************************************************
624 * Function : cgi_edit_actions_url_form
626 * Description : CGI function that displays a form for
630 * 1 : csp = Current client state (buffers, headers, etc...)
631 * 2 : rsp = http_response data structure for output
632 * 3 : parameters = map of cgi parameters
635 * f : (filename) Identifies the file to edit
636 * v : (version) File's last-modified time
637 * p : (pattern) Line number of pattern to edit
639 * Returns : JB_ERR_OK on success
640 * JB_ERR_MEMORY on out-of-memory
641 * JB_ERR_CGI_PARAMS if the CGI parameters are not
642 * specified or not valid.
644 *********************************************************************/
645 jb_err cgi_edit_actions_url_form(struct client_state *csp,
646 struct http_response *rsp,
647 const struct map *parameters)
649 struct map * exports;
651 struct editable_file * file;
652 struct file_line * cur_line;
653 unsigned line_number;
654 unsigned section_start_line_number = 0;
661 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
663 return cgi_error_disabled(csp, rsp);
666 err = get_number_param(csp, parameters, "p", &patternid);
672 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
675 /* No filename specified, can't read file, modified, or out of memory. */
676 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
679 cur_line = file->lines;
681 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
683 if (cur_line->type == FILE_LINE_ACTION)
685 section_start_line_number = line_number;
687 cur_line = cur_line->next;
690 if ( (cur_line == NULL)
691 || (line_number != patternid)
693 || (cur_line->type != FILE_LINE_URL))
695 /* Invalid "patternid" parameter */
696 edit_free_file(file);
697 return JB_ERR_CGI_PARAMS;
700 if (NULL == (exports = default_exports(csp, NULL)))
702 edit_free_file(file);
703 return JB_ERR_MEMORY;
706 err = map(exports, "f", 1, file->identifier, 1);
707 if (!err) err = map(exports, "v", 1, file->version_str, 1);
708 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
709 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
710 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
712 edit_free_file(file);
720 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
724 /*********************************************************************
726 * Function : cgi_edit_actions_add_url_form
728 * Description : CGI function that displays a form for
732 * 1 : csp = Current client state (buffers, headers, etc...)
733 * 2 : rsp = http_response data structure for output
734 * 3 : parameters = map of cgi parameters
737 * f : (filename) Identifies the file to edit
738 * v : (version) File's last-modified time
739 * s : (section) Line number of section to edit
741 * Returns : JB_ERR_OK on success
742 * JB_ERR_MEMORY on out-of-memory
743 * JB_ERR_CGI_PARAMS if the CGI parameters are not
744 * specified or not valid.
746 *********************************************************************/
747 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
748 struct http_response *rsp,
749 const struct map *parameters)
758 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
760 return cgi_error_disabled(csp, rsp);
763 if (NULL == (exports = default_exports(csp, NULL)))
765 return JB_ERR_MEMORY;
768 err = map_copy_parameter_html(exports, parameters, "f");
769 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
770 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
778 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
782 /*********************************************************************
784 * Function : cgi_edit_actions_remove_url_form
786 * Description : CGI function that displays a form for
790 * 1 : csp = Current client state (buffers, headers, etc...)
791 * 2 : rsp = http_response data structure for output
792 * 3 : parameters = map of cgi parameters
795 * f : (filename) Identifies the file to edit
796 * v : (version) File's last-modified time
797 * p : (pattern) Line number of pattern to edit
799 * Returns : JB_ERR_OK on success
800 * JB_ERR_MEMORY on out-of-memory
801 * JB_ERR_CGI_PARAMS if the CGI parameters are not
802 * specified or not valid.
804 *********************************************************************/
805 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
806 struct http_response *rsp,
807 const struct map *parameters)
809 struct map * exports;
811 struct editable_file * file;
812 struct file_line * cur_line;
813 unsigned line_number;
814 unsigned section_start_line_number = 0;
821 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
823 return cgi_error_disabled(csp, rsp);
826 err = get_number_param(csp, parameters, "p", &patternid);
832 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
835 /* No filename specified, can't read file, modified, or out of memory. */
836 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
839 cur_line = file->lines;
841 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
843 if (cur_line->type == FILE_LINE_ACTION)
845 section_start_line_number = line_number;
847 cur_line = cur_line->next;
850 if ( (cur_line == NULL)
851 || (line_number != patternid)
853 || (cur_line->type != FILE_LINE_URL))
855 /* Invalid "patternid" parameter */
856 edit_free_file(file);
857 return JB_ERR_CGI_PARAMS;
860 if (NULL == (exports = default_exports(csp, NULL)))
862 edit_free_file(file);
863 return JB_ERR_MEMORY;
866 err = map(exports, "f", 1, file->identifier, 1);
867 if (!err) err = map(exports, "v", 1, file->version_str, 1);
868 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
869 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
870 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
872 edit_free_file(file);
880 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
884 /*********************************************************************
886 * Function : edit_write_file
888 * Description : Write a complete file to disk.
891 * 1 : file = File to write.
893 * Returns : JB_ERR_OK on success
894 * JB_ERR_FILE on error writing to file.
895 * JB_ERR_MEMORY on out of memory
897 *********************************************************************/
898 jb_err edit_write_file(struct editable_file * file)
901 struct file_line * cur_line;
902 struct stat statbuf[1];
903 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
904 digits in time_t, assuming this is a 64-bit
905 machine, plus null terminator, plus one
909 assert(file->filename);
911 if (NULL == (fp = fopen(file->filename, "wb")))
916 cur_line = file->lines;
917 while (cur_line != NULL)
921 if (fputs(cur_line->raw, fp) < 0)
929 if (cur_line->prefix)
931 if (fputs(cur_line->prefix, fp) < 0)
937 if (cur_line->unprocessed)
940 if (NULL != strchr(cur_line->unprocessed, '#'))
942 /* Must quote '#' characters */
949 /* Count number of # characters, so we know length of output string */
950 src = cur_line->unprocessed;
951 while (NULL != (src = strchr(src, '#')))
958 /* Allocate new memory for string */
959 len = strlen(cur_line->unprocessed);
960 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
962 /* Uh oh, just trashed file! */
964 return JB_ERR_MEMORY;
967 /* Loop through string from end */
968 src = cur_line->unprocessed + len;
969 dest = str + len + numhash;
970 for ( ; len >= 0; len--)
972 if ((*dest-- = *src--) == '#')
976 assert(numhash >= 0);
979 assert(numhash == 0);
980 assert(src + 1 == cur_line->unprocessed);
981 assert(dest + 1 == str);
983 if (fputs(str, fp) < 0)
994 /* Can write without quoting '#' characters. */
995 if (fputs(cur_line->unprocessed, fp) < 0)
1001 if (fputs(NEWLINE(file->newline), fp) < 0)
1009 /* FIXME: Write data from file->data->whatever */
1013 cur_line = cur_line->next;
1019 /* Update the version stamp in the file structure, since we just
1020 * wrote to the file & changed it's date.
1022 if (stat(file->filename, statbuf) < 0)
1024 /* Error, probably file not found. */
1027 file->version = (unsigned)statbuf->st_mtime;
1029 /* Correct file->version_str */
1030 freez(file->version_str);
1031 snprintf(version_buf, 22, "%u", file->version);
1032 version_buf[21] = '\0';
1033 file->version_str = strdup(version_buf);
1034 if (version_buf == NULL)
1036 return JB_ERR_MEMORY;
1043 /*********************************************************************
1045 * Function : edit_free_file
1047 * Description : Free a complete file in memory.
1050 * 1 : file = Data structure to free.
1054 *********************************************************************/
1055 void edit_free_file(struct editable_file * file)
1059 /* Silently ignore NULL pointer */
1063 edit_free_file_lines(file->lines);
1064 freez(file->filename);
1065 freez(file->identifier);
1066 freez(file->version_str);
1068 file->parse_error_text = NULL; /* Statically allocated */
1069 file->parse_error = NULL;
1075 /*********************************************************************
1077 * Function : edit_free_file_lines
1079 * Description : Free an entire linked list of file lines.
1082 * 1 : first_line = Data structure to free.
1086 *********************************************************************/
1087 static void edit_free_file_lines(struct file_line * first_line)
1089 struct file_line * next_line;
1091 while (first_line != NULL)
1093 next_line = first_line->next;
1094 first_line->next = NULL;
1095 freez(first_line->raw);
1096 freez(first_line->prefix);
1097 freez(first_line->unprocessed);
1098 switch(first_line->type)
1100 case 0: /* special case if memory zeroed */
1101 case FILE_LINE_UNPROCESSED:
1102 case FILE_LINE_BLANK:
1103 case FILE_LINE_ALIAS_HEADER:
1104 case FILE_LINE_SETTINGS_HEADER:
1105 case FILE_LINE_DESCRIPTION_HEADER:
1106 case FILE_LINE_DESCRIPTION_ENTRY:
1107 case FILE_LINE_ALIAS_ENTRY:
1109 /* No data is stored for these */
1112 case FILE_LINE_ACTION:
1113 free_action(first_line->data.action);
1116 case FILE_LINE_SETTINGS_ENTRY:
1117 freez(first_line->data.setting.name);
1118 freez(first_line->data.setting.svalue);
1121 /* Should never happen */
1125 first_line->type = 0; /* paranoia */
1127 first_line = next_line;
1132 /*********************************************************************
1134 * Function : match_actions_file_header_line
1136 * Description : Match an actions file {{header}} line
1139 * 1 : line = String from file
1140 * 2 : name = Header to match against
1142 * Returns : 0 iff they match.
1144 *********************************************************************/
1145 static int match_actions_file_header_line(const char * line, const char * name)
1153 if ((line[0] != '{') || (line[1] != '{'))
1159 /* Look for optional whitespace */
1160 while ( (*line == ' ') || (*line == '\t') )
1165 /* Look for the specified name (case-insensitive) */
1167 if (0 != strncmpic(line, name, len))
1173 /* Look for optional whitespace */
1174 while ( (*line == ' ') || (*line == '\t') )
1179 /* Look for "}}" and end of string*/
1180 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1190 /*********************************************************************
1192 * Function : match_actions_file_header_line
1194 * Description : Match an actions file {{header}} line
1197 * 1 : line = String from file. Must not start with
1198 * whitespace (else infinite loop!)
1199 * 2 : pname = Destination for name
1200 * 2 : pvalue = Destination for value
1202 * Returns : JB_ERR_OK on success
1203 * JB_ERR_MEMORY on out-of-memory
1204 * JB_ERR_PARSE if there's no "=" sign, or if there's
1205 * nothing before the "=" sign (but empty
1206 * values *after* the "=" sign are legal).
1208 *********************************************************************/
1209 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1211 const char * name_end;
1212 const char * value_start;
1218 assert(*line != ' ');
1219 assert(*line != '\t');
1224 value_start = strchr(line, '=');
1225 if ((value_start == NULL) || (value_start == line))
1227 return JB_ERR_PARSE;
1230 name_end = value_start - 1;
1232 /* Eat any whitespace before the '=' */
1233 while ((*name_end == ' ') || (*name_end == '\t'))
1236 * we already know we must have at least 1 non-ws char
1237 * at start of buf - no need to check
1242 name_len = name_end - line + 1; /* Length excluding \0 */
1243 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1245 return JB_ERR_MEMORY;
1247 strncpy(*pname, line, name_len);
1248 (*pname)[name_len] = '\0';
1250 /* Eat any the whitespace after the '=' */
1252 while ((*value_start == ' ') || (*value_start == '\t'))
1257 if (NULL == (*pvalue = strdup(value_start)))
1261 return JB_ERR_MEMORY;
1268 /*********************************************************************
1270 * Function : edit_parse_actions_file
1272 * Description : Parse an actions file in memory.
1274 * Passed linked list must have the "data" member
1275 * zeroed, and must contain valid "next" and
1276 * "unprocessed" fields. The "raw" and "prefix"
1277 * fields are ignored, and "type" is just overwritten.
1279 * Note that on error the file may have been
1283 * 1 : file = Actions file to be parsed in-place.
1285 * Returns : JB_ERR_OK on success
1286 * JB_ERR_MEMORY on out-of-memory
1287 * JB_ERR_PARSE on error
1289 *********************************************************************/
1290 jb_err edit_parse_actions_file(struct editable_file * file)
1292 struct file_line * cur_line;
1294 const char * text; /* Text from a line */
1295 char * name; /* For lines of the form name=value */
1296 char * value; /* For lines of the form name=value */
1297 struct action_alias * alias_list = NULL;
1298 jb_err err = JB_ERR_OK;
1300 /* alias_list contains the aliases defined in this file.
1301 * It might be better to use the "file_line.data" fields
1302 * in the relavent places instead.
1305 cur_line = file->lines;
1307 /* A note about blank line support: Blank lines should only
1308 * ever occur as the last line in the file. This function
1309 * is more forgiving than that - FILE_LINE_BLANK can occur
1313 /* Skip leading blanks. Should only happen if file is
1314 * empty (which is valid, but pointless).
1316 while ( (cur_line != NULL)
1317 && (cur_line->unprocessed[0] == '\0') )
1320 cur_line->type = FILE_LINE_BLANK;
1321 cur_line = cur_line->next;
1324 if ( (cur_line != NULL)
1325 && (cur_line->unprocessed[0] != '{') )
1327 /* File doesn't start with a header */
1328 file->parse_error = cur_line;
1329 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1330 return JB_ERR_PARSE;
1333 if ( (cur_line != NULL) && (0 ==
1334 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1336 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1338 cur_line = cur_line->next;
1339 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1341 if (cur_line->unprocessed[0])
1343 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1345 err = split_line_on_equals(cur_line->unprocessed,
1346 &cur_line->data.setting.name,
1347 &cur_line->data.setting.svalue);
1348 if (err == JB_ERR_MEMORY)
1352 else if (err != JB_ERR_OK)
1354 /* Line does not contain a name=value pair */
1355 file->parse_error = cur_line;
1356 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1357 return JB_ERR_PARSE;
1362 cur_line->type = FILE_LINE_BLANK;
1364 cur_line = cur_line->next;
1368 if ( (cur_line != NULL) && (0 ==
1369 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1371 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1373 cur_line = cur_line->next;
1374 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1376 if (cur_line->unprocessed[0])
1378 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1382 cur_line->type = FILE_LINE_BLANK;
1384 cur_line = cur_line->next;
1388 if ( (cur_line != NULL) && (0 ==
1389 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1391 cur_line->type = FILE_LINE_ALIAS_HEADER;
1393 cur_line = cur_line->next;
1394 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1396 if (cur_line->unprocessed[0])
1398 /* define an alias */
1399 struct action_alias * new_alias;
1401 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1403 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1404 if (err == JB_ERR_MEMORY)
1408 else if (err != JB_ERR_OK)
1410 /* Line does not contain a name=value pair */
1411 file->parse_error = cur_line;
1412 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1413 return JB_ERR_PARSE;
1416 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1421 free_alias_list(alias_list);
1422 return JB_ERR_MEMORY;
1425 err = get_actions(value, alias_list, new_alias->action);
1428 /* Invalid action or out of memory */
1432 free_alias_list(alias_list);
1433 if (err == JB_ERR_MEMORY)
1439 /* Line does not contain a name=value pair */
1440 file->parse_error = cur_line;
1441 file->parse_error_text = "This alias does not specify a valid set of actions.";
1442 return JB_ERR_PARSE;
1448 new_alias->name = name;
1451 new_alias->next = alias_list;
1452 alias_list = new_alias;
1456 cur_line->type = FILE_LINE_BLANK;
1458 cur_line = cur_line->next;
1462 /* Header done, process the main part of the file */
1463 while (cur_line != NULL)
1465 /* At this point, (cur_line->unprocessed[0] == '{') */
1466 assert(cur_line->unprocessed[0] == '{');
1467 text = cur_line->unprocessed + 1;
1468 len = strlen(text) - 1;
1469 if (text[len] != '}')
1471 /* No closing } on header */
1472 free_alias_list(alias_list);
1473 file->parse_error = cur_line;
1474 file->parse_error_text = "Headers starting with '{' must have a "
1475 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1476 "must close with two brackets ('}}').";
1477 return JB_ERR_PARSE;
1482 /* An invalid {{ header. */
1483 free_alias_list(alias_list);
1484 file->parse_error = cur_line;
1485 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1486 "Please remember that the system (two-bracket) headers must "
1487 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1488 "and must appear before any actions (one-bracket) headers. "
1489 "Also note that system headers may not be repeated.";
1490 return JB_ERR_PARSE;
1493 while ( (*text == ' ') || (*text == '\t') )
1499 && ( (text[len - 1] == ' ')
1500 || (text[len - 1] == '\t') ) )
1505 cur_line->type = FILE_LINE_ACTION;
1507 /* Remove {} and make copy */
1508 if (NULL == (value = (char *) malloc(len + 1)))
1511 free_alias_list(alias_list);
1512 return JB_ERR_MEMORY;
1514 strncpy(value, text, len);
1518 err = get_actions(value, alias_list, cur_line->data.action);
1521 /* Invalid action or out of memory */
1523 free_alias_list(alias_list);
1524 if (err == JB_ERR_MEMORY)
1530 /* Line does not contain a name=value pair */
1531 file->parse_error = cur_line;
1532 file->parse_error_text = "This header does not specify a valid set of actions.";
1533 return JB_ERR_PARSE;
1537 /* Done with string - it was clobbered anyway */
1540 /* Process next line */
1541 cur_line = cur_line->next;
1543 /* Loop processing URL patterns */
1544 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1546 if (cur_line->unprocessed[0])
1548 /* Could parse URL here, but this isn't currently needed */
1550 cur_line->type = FILE_LINE_URL;
1554 cur_line->type = FILE_LINE_BLANK;
1556 cur_line = cur_line->next;
1558 } /* End main while(cur_line != NULL) loop */
1560 free_alias_list(alias_list);
1566 /*********************************************************************
1568 * Function : edit_read_file_lines
1570 * Description : Read all the lines of a file into memory.
1571 * Handles whitespace, comments and line continuation.
1574 * 1 : fp = File to read from. On return, this will be
1575 * at EOF but it will not have been closed.
1576 * 2 : pfile = Destination for a linked list of file_lines.
1577 * Will be set to NULL on error.
1578 * 3 : newline = How to handle newlines.
1580 * Returns : JB_ERR_OK on success
1581 * JB_ERR_MEMORY on out-of-memory
1583 *********************************************************************/
1584 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1586 struct file_line * first_line; /* Keep for return value or to free */
1587 struct file_line * cur_line; /* Current line */
1588 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1596 cur_line = first_line = zalloc(sizeof(struct file_line));
1597 if (cur_line == NULL)
1599 return JB_ERR_MEMORY;
1602 cur_line->type = FILE_LINE_UNPROCESSED;
1604 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1607 /* Out of memory or empty file. */
1608 /* Note that empty file is not an error we propogate up */
1610 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1615 prev_line = cur_line;
1616 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1617 if (cur_line == NULL)
1620 edit_free_file_lines(first_line);
1621 return JB_ERR_MEMORY;
1624 cur_line->type = FILE_LINE_UNPROCESSED;
1626 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1627 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1630 edit_free_file_lines(first_line);
1631 return JB_ERR_MEMORY;
1635 while (rval != JB_ERR_FILE);
1639 /* We allocated one too many - free it */
1640 prev_line->next = NULL;
1643 *pfile = first_line;
1648 /*********************************************************************
1650 * Function : edit_read_file
1652 * Description : Read a complete file into memory.
1653 * Handles CGI parameter parsing. If requested, also
1654 * checks the file's modification timestamp.
1657 * 1 : csp = Current client state (buffers, headers, etc...)
1658 * 2 : parameters = map of cgi parameters.
1659 * 3 : require_version = true to check "ver" parameter.
1660 * 4 : suffix = File extension, e.g. ".action".
1661 * 5 : pfile = Destination for the file. Will be set
1665 * filename : The name of the file to read, without the
1666 * path or ".action" extension.
1667 * ver : (Only if require_version is nonzero)
1668 * Timestamp of the actions file. If wrong, this
1669 * function fails with JB_ERR_MODIFIED.
1671 * Returns : JB_ERR_OK on success
1672 * JB_ERR_MEMORY on out-of-memory
1673 * JB_ERR_CGI_PARAMS if "filename" was not specified
1675 * JB_ERR_FILE if the file cannot be opened or
1677 * JB_ERR_MODIFIED if version checking was requested and
1678 * failed - the file was modified outside
1679 * of this CGI editor instance.
1681 *********************************************************************/
1682 jb_err edit_read_file(struct client_state *csp,
1683 const struct map *parameters,
1684 int require_version,
1686 struct editable_file **pfile)
1688 struct file_line * lines;
1692 const char * identifier;
1693 struct editable_file * file;
1694 unsigned version = 0;
1695 struct stat statbuf[1];
1696 char version_buf[22];
1697 int newline = NEWLINE_UNKNOWN;
1705 err = get_file_name_param(csp, parameters, "f", suffix,
1706 &filename, &identifier);
1712 if (stat(filename, statbuf) < 0)
1714 /* Error, probably file not found. */
1718 version = (unsigned) statbuf->st_mtime;
1720 if (require_version)
1722 unsigned specified_version;
1723 err = get_number_param(csp, parameters, "v", &specified_version);
1730 if (version != specified_version)
1732 return JB_ERR_MODIFIED;
1736 if (NULL == (fp = fopen(filename,"rb")))
1742 err = edit_read_file_lines(fp, &lines, &newline);
1752 file = (struct editable_file *) zalloc(sizeof(*file));
1756 edit_free_file_lines(lines);
1760 file->lines = lines;
1761 file->newline = newline;
1762 file->filename = filename;
1763 file->version = version;
1764 file->identifier = url_encode(identifier);
1766 if (file->identifier == NULL)
1768 edit_free_file(file);
1769 return JB_ERR_MEMORY;
1772 /* Correct file->version_str */
1773 freez(file->version_str);
1774 snprintf(version_buf, 22, "%u", file->version);
1775 version_buf[21] = '\0';
1776 file->version_str = strdup(version_buf);
1777 if (version_buf == NULL)
1779 edit_free_file(file);
1780 return JB_ERR_MEMORY;
1788 /*********************************************************************
1790 * Function : edit_read_actions_file
1792 * Description : Read a complete actions file into memory.
1793 * Handles CGI parameter parsing. If requested, also
1794 * checks the file's modification timestamp.
1796 * If this function detects an error in the categories
1797 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1798 * then it handles it by filling in the specified
1799 * response structure and returning JB_ERR_FILE.
1802 * 1 : csp = Current client state (buffers, headers, etc...)
1803 * 2 : rsp = HTTP response. Only filled in on error.
1804 * 2 : parameters = map of cgi parameters.
1805 * 3 : require_version = true to check "ver" parameter.
1806 * 4 : pfile = Destination for the file. Will be set
1810 * filename : The name of the actions file to read, without the
1811 * path or ".action" extension.
1812 * ver : (Only if require_version is nonzero)
1813 * Timestamp of the actions file. If wrong, this
1814 * function fails with JB_ERR_MODIFIED.
1816 * Returns : JB_ERR_OK on success
1817 * JB_ERR_MEMORY on out-of-memory
1818 * JB_ERR_CGI_PARAMS if "filename" was not specified
1820 * JB_ERR_FILE if the file does not contain valid data,
1821 * or if file cannot be opened or
1822 * contains no data, or if version
1823 * checking was requested and failed.
1825 *********************************************************************/
1826 jb_err edit_read_actions_file(struct client_state *csp,
1827 struct http_response *rsp,
1828 const struct map *parameters,
1829 int require_version,
1830 struct editable_file **pfile)
1833 struct editable_file *file;
1841 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1844 /* Try to handle if possible */
1845 if (err == JB_ERR_FILE)
1847 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1849 else if (err == JB_ERR_MODIFIED)
1851 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1853 if (err == JB_ERR_OK)
1856 * Signal to higher-level CGI code that there was a problem but we
1857 * handled it, they should just return JB_ERR_OK.
1864 err = edit_parse_actions_file(file);
1867 if (err == JB_ERR_PARSE)
1869 err = cgi_error_parse(csp, rsp, file);
1870 if (err == JB_ERR_OK)
1873 * Signal to higher-level CGI code that there was a problem but we
1874 * handled it, they should just return JB_ERR_OK.
1879 edit_free_file(file);
1888 /*********************************************************************
1890 * Function : get_file_name_param
1892 * Description : Get the name of the file to edit from the parameters
1893 * passed to a CGI function. This function handles
1894 * security checks such as blocking urls containing
1895 * "/" or ".", prepending the config file directory,
1896 * and adding the specified suffix.
1898 * (This is an essential security check, otherwise
1899 * users may be able to pass "../../../etc/passwd"
1900 * and overwrite the password file [linux], "prn:"
1901 * and print random data [Windows], etc...)
1903 * This function only allows filenames contining the
1904 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1905 * That's probably too restrictive but at least it's
1909 * 1 : csp = Current client state (buffers, headers, etc...)
1910 * 2 : parameters = map of cgi parameters
1911 * 3 : param_name = The name of the parameter to read
1912 * 4 : suffix = File extension, e.g. ".actions"
1913 * 5 : pfilename = destination for full filename. Caller
1914 * free()s. Set to NULL on error.
1915 * 6 : pparam = destination for partial filename,
1916 * suitable for use in another URL. Allocated as part
1917 * of the map "parameters", so don't free it.
1918 * Set to NULL if not specified.
1920 * Returns : JB_ERR_OK on success
1921 * JB_ERR_MEMORY on out-of-memory
1922 * JB_ERR_CGI_PARAMS if "filename" was not specified
1925 *********************************************************************/
1926 static jb_err get_file_name_param(struct client_state *csp,
1927 const struct map *parameters,
1928 const char *param_name,
1931 const char **pparam)
1949 param = lookup(parameters, param_name);
1952 return JB_ERR_CGI_PARAMS;
1957 len = strlen(param);
1958 if (len >= FILENAME_MAX)
1961 return JB_ERR_CGI_PARAMS;
1964 /* Check every character to see if it's legal */
1966 while ((ch = *s++) != '\0')
1968 if ( ((ch < 'A') || (ch > 'Z'))
1969 && ((ch < 'a') || (ch > 'z'))
1970 && ((ch < '0') || (ch > '9'))
1974 /* Probable hack attempt. */
1975 return JB_ERR_CGI_PARAMS;
1979 /* Append extension */
1980 name = malloc(len + strlen(suffix) + 1);
1983 return JB_ERR_MEMORY;
1985 strcpy(name, param);
1986 strcpy(name + len, suffix);
1989 fullpath = make_path(csp->config->confdir, name);
1992 if (fullpath == NULL)
1994 return JB_ERR_MEMORY;
1998 *pfilename = fullpath;
2004 /*********************************************************************
2006 * Function : get_url_spec_param
2008 * Description : Get a URL pattern from the parameters
2009 * passed to a CGI function. Removes leading/trailing
2010 * spaces and validates it.
2013 * 1 : csp = Current client state (buffers, headers, etc...)
2014 * 2 : parameters = map of cgi parameters
2015 * 3 : name = Name of CGI parameter to read
2016 * 4 : pvalue = destination for value. Will be malloc()'d.
2017 * Set to NULL on error.
2019 * Returns : JB_ERR_OK on success
2020 * JB_ERR_MEMORY on out-of-memory
2021 * JB_ERR_CGI_PARAMS if the parameter was not specified
2024 *********************************************************************/
2025 static jb_err get_url_spec_param(struct client_state *csp,
2026 const struct map *parameters,
2030 const char *orig_param;
2033 struct url_spec compiled[1];
2043 orig_param = lookup(parameters, name);
2046 return JB_ERR_CGI_PARAMS;
2049 /* Copy and trim whitespace */
2050 param = strdup(orig_param);
2053 return JB_ERR_MEMORY;
2057 /* Must be non-empty, and can't allow 1st character to be '{' */
2058 if (param[0] == '\0' || param[0] == '{')
2061 return JB_ERR_CGI_PARAMS;
2064 /* Check for embedded newlines */
2065 for (s = param; *s != '\0'; s++)
2067 if ((*s == '\r') || (*s == '\n'))
2070 return JB_ERR_CGI_PARAMS;
2074 /* Check that regex is valid */
2079 return JB_ERR_MEMORY;
2081 err = create_url_spec(compiled, s);
2086 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2088 free_url_spec(compiled);
2090 if (param[strlen(param) - 1] == '\\')
2093 * Must protect trailing '\\' from becoming line continuation character.
2094 * Two methods: 1) If it's a domain only, add a trailing '/'.
2095 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2097 if (strchr(param, '/') == NULL)
2099 err = string_append(¶m, "/");
2103 err = string_append(¶m, "(?:)");
2110 /* Check that the modified regex is valid */
2115 return JB_ERR_MEMORY;
2117 err = create_url_spec(compiled, s);
2122 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2124 free_url_spec(compiled);
2131 /*********************************************************************
2133 * Function : map_radio
2135 * Description : Map a set of radio button values. E.g. if you have
2136 * 3 radio buttons, declare them as:
2137 * <option type="radio" name="xyz" @xyz-a@>
2138 * <option type="radio" name="xyz" @xyz-b@>
2139 * <option type="radio" name="xyz" @xyz-c@>
2140 * Then map one of the @xyz-?@ variables to "checked"
2141 * and all the others to empty by calling:
2142 * map_radio(exports, "xyz", "abc", sel)
2143 * Where 'sel' is 'a', 'b', or 'c'.
2146 * 1 : exports = Exports map to modify.
2147 * 2 : optionname = name for map
2148 * 3 : values = null-terminated list of values;
2149 * 4 : value = Selected value.
2151 * CGI Parameters : None
2153 * Returns : JB_ERR_OK on success
2154 * JB_ERR_MEMORY on out-of-memory
2156 *********************************************************************/
2157 static jb_err map_radio(struct map * exports,
2158 const char * optionname,
2159 const char * values,
2171 len = strlen(optionname);
2172 buf = malloc(len + 3);
2175 return JB_ERR_MEMORY;
2178 strcpy(buf, optionname);
2183 while ((c = *values++) != '\0')
2188 if (map(exports, buf, 1, "", 1))
2190 return JB_ERR_MEMORY;
2196 return map(exports, buf, 0, "checked", 1);
2200 /*********************************************************************
2202 * Function : cgi_error_modified
2204 * Description : CGI function that is called when a file is modified
2205 * outside the CGI editor.
2208 * 1 : csp = Current client state (buffers, headers, etc...)
2209 * 2 : rsp = http_response data structure for output
2210 * 3 : filename = The file that was modified.
2212 * CGI Parameters : none
2214 * Returns : JB_ERR_OK on success
2215 * JB_ERR_MEMORY on out-of-memory error.
2217 *********************************************************************/
2218 jb_err cgi_error_modified(struct client_state *csp,
2219 struct http_response *rsp,
2220 const char *filename)
2222 struct map *exports;
2229 if (NULL == (exports = default_exports(csp, NULL)))
2231 return JB_ERR_MEMORY;
2234 err = map(exports, "f", 1, html_encode(filename), 0);
2241 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2245 /*********************************************************************
2247 * Function : cgi_error_parse
2249 * Description : CGI function that is called when a file cannot
2250 * be parsed by the CGI editor.
2253 * 1 : csp = Current client state (buffers, headers, etc...)
2254 * 2 : rsp = http_response data structure for output
2255 * 3 : file = The file that was modified.
2257 * CGI Parameters : none
2259 * Returns : JB_ERR_OK on success
2260 * JB_ERR_MEMORY on out-of-memory error.
2262 *********************************************************************/
2263 jb_err cgi_error_parse(struct client_state *csp,
2264 struct http_response *rsp,
2265 struct editable_file *file)
2267 struct map *exports;
2269 struct file_line *cur_line;
2275 if (NULL == (exports = default_exports(csp, NULL)))
2277 return JB_ERR_MEMORY;
2280 err = map(exports, "f", 1, file->identifier, 1);
2281 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2283 cur_line = file->parse_error;
2286 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2287 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2295 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2299 /*********************************************************************
2301 * Function : cgi_error_file
2303 * Description : CGI function that is called when a file cannot be
2304 * opened by the CGI editor.
2307 * 1 : csp = Current client state (buffers, headers, etc...)
2308 * 2 : rsp = http_response data structure for output
2309 * 3 : filename = The file that was modified.
2311 * CGI Parameters : none
2313 * Returns : JB_ERR_OK on success
2314 * JB_ERR_MEMORY on out-of-memory error.
2316 *********************************************************************/
2317 jb_err cgi_error_file(struct client_state *csp,
2318 struct http_response *rsp,
2319 const char *filename)
2321 struct map *exports;
2328 if (NULL == (exports = default_exports(csp, NULL)))
2330 return JB_ERR_MEMORY;
2333 err = map(exports, "f", 1, html_encode(filename), 0);
2340 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2344 /*********************************************************************
2346 * Function : cgi_error_file
2348 * Description : CGI function that is called when a file cannot be
2349 * opened for writing by the CGI editor.
2352 * 1 : csp = Current client state (buffers, headers, etc...)
2353 * 2 : rsp = http_response data structure for output
2354 * 3 : filename = The file that we can't write to
2356 * CGI Parameters : none
2358 * Returns : JB_ERR_OK on success
2359 * JB_ERR_MEMORY on out-of-memory error.
2361 *********************************************************************/
2362 jb_err cgi_error_file_read_only(struct client_state *csp,
2363 struct http_response *rsp,
2364 const char *filename)
2366 struct map *exports;
2373 if (NULL == (exports = default_exports(csp, NULL)))
2375 return JB_ERR_MEMORY;
2378 err = map(exports, "f", 1, html_encode(filename), 0);
2385 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2389 /*********************************************************************
2391 * Function : cgi_error_disabled
2393 * Description : CGI function that is called if the actions editor
2394 * is called although it's disabled in config
2397 * 1 : csp = Current client state (buffers, headers, etc...)
2398 * 2 : rsp = http_response data structure for output
2400 * CGI Parameters : none
2402 * Returns : JB_ERR_OK on success
2403 * JB_ERR_MEMORY on out-of-memory error.
2405 *********************************************************************/
2406 jb_err cgi_error_disabled(struct client_state *csp,
2407 struct http_response *rsp)
2409 struct map *exports;
2414 if (NULL == (exports = default_exports(csp, NULL)))
2416 return JB_ERR_MEMORY;
2419 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2423 /*********************************************************************
2425 * Function : cgi_edit_actions
2427 * Description : CGI function that allows the user to choose which
2428 * actions file to edit.
2431 * 1 : csp = Current client state (buffers, headers, etc...)
2432 * 2 : rsp = http_response data structure for output
2433 * 3 : parameters = map of cgi parameters
2435 * CGI Parameters : None
2437 * Returns : JB_ERR_OK on success
2438 * JB_ERR_MEMORY on out-of-memory error
2440 *********************************************************************/
2441 jb_err cgi_edit_actions(struct client_state *csp,
2442 struct http_response *rsp,
2443 const struct map *parameters)
2446 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2448 return cgi_error_disabled(csp, rsp);
2451 /* FIXME: Incomplete */
2452 rsp->status = strdup("302 Local Redirect from Privoxy");
2453 if (rsp->status == NULL)
2455 return JB_ERR_MEMORY;
2457 if (enlist_unique_header(rsp->headers, "Location",
2458 CGI_PREFIX "edit-actions-list?f=default"))
2462 return JB_ERR_MEMORY;
2469 /*********************************************************************
2471 * Function : cgi_edit_actions_list
2473 * Description : CGI function that edits the actions list.
2474 * FIXME: This function shouldn't FATAL ever.
2475 * FIXME: This function doesn't check the retval of map()
2477 * 1 : csp = Current client state (buffers, headers, etc...)
2478 * 2 : rsp = http_response data structure for output
2479 * 3 : parameters = map of cgi parameters
2481 * CGI Parameters : filename
2483 * Returns : JB_ERR_OK on success
2484 * JB_ERR_MEMORY on out-of-memory
2485 * JB_ERR_FILE if the file cannot be opened or
2487 * JB_ERR_CGI_PARAMS if "filename" was not specified
2490 *********************************************************************/
2491 jb_err cgi_edit_actions_list(struct client_state *csp,
2492 struct http_response *rsp,
2493 const struct map *parameters)
2495 char * section_template;
2496 char * url_template;
2501 struct map * exports;
2502 struct map * section_exports;
2503 struct map * url_exports;
2504 struct editable_file * file;
2505 struct file_line * cur_line;
2506 unsigned line_number = 0;
2507 unsigned prev_section_line_number = ((unsigned) (-1));
2509 struct file_list * fl;
2510 struct url_actions * b;
2511 char * buttons = NULL;
2514 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2516 return cgi_error_disabled(csp, rsp);
2519 if (NULL == (exports = default_exports(csp, NULL)))
2521 edit_free_file(file);
2522 return JB_ERR_MEMORY;
2525 /* Load actions file */
2526 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2529 /* No filename specified, can't read file, or out of memory. */
2530 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2533 /* Find start of actions in file */
2534 cur_line = file->lines;
2536 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2538 cur_line = cur_line->next;
2543 * Conventional actions files should have a match all block
2545 * cur_line = {...global actions...}
2546 * cur_line->next = /
2547 * cur_line->next->next = {...actions...} or EOF
2549 if ( (cur_line != NULL)
2550 && (cur_line->type == FILE_LINE_ACTION)
2551 && (cur_line->next != NULL)
2552 && (cur_line->next->type == FILE_LINE_URL)
2553 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2554 && ( (cur_line->next->next == NULL)
2555 || (cur_line->next->next->type != FILE_LINE_URL)
2559 * Generate string with buttons to set actions for "/" to
2560 * any predefined set of actions (named standard.*, probably
2561 * residing in standard.action).
2564 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2567 edit_free_file(file);
2569 if (err == JB_ERR_FILE)
2571 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2576 err = template_fill(§ion_template, exports);
2579 edit_free_file(file);
2584 buttons = strdup("");
2585 for (i = 0; i < MAX_ACTION_FILES; i++)
2587 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2589 for (b = b->next; NULL != b; b = b->next)
2591 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2593 if (err || (NULL == (section_exports = new_map())))
2596 free(section_template);
2597 edit_free_file(file);
2599 return JB_ERR_MEMORY;
2602 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2604 if (err || (NULL == (s = strdup(section_template))))
2606 free_map(section_exports);
2608 free(section_template);
2609 edit_free_file(file);
2611 return JB_ERR_MEMORY;
2614 if (!err) err = template_fill(&s, section_exports);
2615 free_map(section_exports);
2616 if (!err) err = string_join(&buttons, s);
2621 freez(section_template);
2622 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2625 * Conventional actions file, supply extra editing help.
2626 * (e.g. don't allow them to make it an unconventional one).
2628 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2630 snprintf(buf, 150, "%d", line_number);
2631 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2632 snprintf(buf, 150, "%d", line_number + 2);
2633 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2634 if (!err) err = map(exports, "all-urls-actions", 1,
2635 actions_to_html(csp, cur_line->data.action), 0);
2637 /* Skip the 2 lines */
2638 cur_line = cur_line->next->next;
2642 * Note that prev_section_line_number is NOT set here.
2643 * This is deliberate and not a bug. It stops a "Move up"
2644 * option appearing on the next section. Clicking "Move
2645 * up" would make the actions file unconventional, which
2646 * we don't want, so we hide this option.
2652 * Non-standard actions file - does not begin with
2653 * the "All URLs" section.
2655 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2658 /* Set up global exports */
2660 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2661 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2663 /* Discourage private additions to default.action */
2665 if (!err) err = map_conditional(exports, "default-action",
2666 (strcmp("default", lookup(parameters, "f")) == 0));
2669 edit_free_file(file);
2674 /* Should do all global exports above this point */
2676 /* Load templates */
2678 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2681 edit_free_file(file);
2683 if (err == JB_ERR_FILE)
2685 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2690 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2693 free(section_template);
2694 edit_free_file(file);
2696 if (err == JB_ERR_FILE)
2698 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2703 err = template_fill(§ion_template, exports);
2707 edit_free_file(file);
2713 err = template_fill(&url_template, exports);
2716 free(section_template);
2717 edit_free_file(file);
2722 if (NULL == (sections = strdup("")))
2724 free(section_template);
2726 edit_free_file(file);
2728 return JB_ERR_MEMORY;
2731 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2733 if (NULL == (section_exports = new_map()))
2736 free(section_template);
2738 edit_free_file(file);
2740 return JB_ERR_MEMORY;
2743 snprintf(buf, 150, "%d", line_number);
2744 err = map(section_exports, "s", 1, buf, 1);
2745 if (!err) err = map(section_exports, "actions", 1,
2746 actions_to_html(csp, cur_line->data.action), 0);
2749 && (cur_line->next != NULL)
2750 && (cur_line->next->type == FILE_LINE_URL))
2752 /* This section contains at least one URL, don't allow delete */
2753 err = map_block_killer(section_exports, "empty-section");
2757 if (!err) err = map_block_keep(section_exports, "empty-section");
2760 if (prev_section_line_number != ((unsigned)(-1)))
2762 /* Not last section */
2763 snprintf(buf, 150, "%d", prev_section_line_number);
2764 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2765 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2770 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2772 prev_section_line_number = line_number;
2777 free(section_template);
2779 edit_free_file(file);
2781 free_map(section_exports);
2785 /* Should do all section-specific exports above this point */
2787 if (NULL == (urls = strdup("")))
2790 free(section_template);
2792 edit_free_file(file);
2794 free_map(section_exports);
2795 return JB_ERR_MEMORY;
2800 cur_line = cur_line->next;
2803 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2805 if (NULL == (url_exports = new_map()))
2809 free(section_template);
2811 edit_free_file(file);
2813 free_map(section_exports);
2814 return JB_ERR_MEMORY;
2817 snprintf(buf, 150, "%d", line_number);
2818 err = map(url_exports, "p", 1, buf, 1);
2820 snprintf(buf, 150, "%d", url_1_2);
2821 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2823 if (!err) err = map(url_exports, "url-html", 1,
2824 html_encode(cur_line->unprocessed), 0);
2825 if (!err) err = map(url_exports, "url", 1,
2826 url_encode(cur_line->unprocessed), 0);
2832 free(section_template);
2834 edit_free_file(file);
2836 free_map(section_exports);
2837 free_map(url_exports);
2841 if (NULL == (s = strdup(url_template)))
2845 free(section_template);
2847 edit_free_file(file);
2849 free_map(section_exports);
2850 free_map(url_exports);
2851 return JB_ERR_MEMORY;
2854 err = template_fill(&s, section_exports);
2855 if (!err) err = template_fill(&s, url_exports);
2856 if (!err) err = string_append(&urls, s);
2858 free_map(url_exports);
2865 free(section_template);
2867 edit_free_file(file);
2869 free_map(section_exports);
2873 url_1_2 = 3 - url_1_2;
2875 cur_line = cur_line->next;
2879 err = map(section_exports, "urls", 1, urls, 0);
2881 /* Could also do section-specific exports here, but it wouldn't be as fast */
2883 snprintf(buf, 150, "%d", line_number);
2884 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2886 if ( (cur_line != NULL)
2887 && (cur_line->type == FILE_LINE_ACTION))
2889 /* Not last section */
2890 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2895 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2901 free(section_template);
2903 edit_free_file(file);
2905 free_map(section_exports);
2909 if (NULL == (s = strdup(section_template)))
2912 free(section_template);
2914 edit_free_file(file);
2916 free_map(section_exports);
2917 return JB_ERR_MEMORY;
2920 err = template_fill(&s, section_exports);
2921 if (!err) err = string_append(§ions, s);
2924 free_map(section_exports);
2929 free(section_template);
2931 edit_free_file(file);
2937 edit_free_file(file);
2938 free(section_template);
2941 err = map(exports, "sections", 1, sections, 0);
2948 /* Could also do global exports here, but it wouldn't be as fast */
2950 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2954 /*********************************************************************
2956 * Function : cgi_edit_actions_for_url
2958 * Description : CGI function that edits the Actions list.
2961 * 1 : csp = Current client state (buffers, headers, etc...)
2962 * 2 : rsp = http_response data structure for output
2963 * 3 : parameters = map of cgi parameters
2965 * CGI Parameters : None
2967 * Returns : JB_ERR_OK on success
2968 * JB_ERR_MEMORY on out-of-memory
2969 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2970 * specified or not valid.
2972 *********************************************************************/
2973 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2974 struct http_response *rsp,
2975 const struct map *parameters)
2977 struct map * exports;
2979 struct editable_file * file;
2980 struct file_line * cur_line;
2981 unsigned line_number;
2983 struct file_list *filter_file;
2984 struct re_filterfile_spec *filter_group;
2986 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2988 return cgi_error_disabled(csp, rsp);
2991 err = get_number_param(csp, parameters, "s", §ionid);
2997 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3000 /* No filename specified, can't read file, modified, or out of memory. */
3001 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3004 cur_line = file->lines;
3006 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3008 cur_line = cur_line->next;
3011 if ( (cur_line == NULL)
3012 || (line_number != sectionid)
3014 || (cur_line->type != FILE_LINE_ACTION))
3016 /* Invalid "sectionid" parameter */
3017 edit_free_file(file);
3018 return JB_ERR_CGI_PARAMS;
3021 if (NULL == (exports = default_exports(csp, NULL)))
3023 edit_free_file(file);
3024 return JB_ERR_MEMORY;
3027 err = map(exports, "f", 1, file->identifier, 1);
3028 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3029 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3031 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3033 filter_file = csp->rlist;
3034 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
3036 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
3040 edit_free_file(file);
3045 if (filter_group == NULL)
3047 err = map(exports, "filter-params", 1, "", 1);
3051 /* We have some entries in the filter list */
3054 char * filter_template;
3056 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3059 edit_free_file(file);
3061 if (err == JB_ERR_FILE)
3063 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3068 err = template_fill(&filter_template, exports);
3070 result = strdup("");
3072 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3074 char current_mode = 'x';
3075 struct list_entry *filter_name;
3077 struct map *line_exports;
3080 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
3081 while ((filter_name != NULL)
3082 && (0 != strcmp(filter_group->name, filter_name->str)))
3084 filter_name = filter_name->next;
3087 if (filter_name != NULL)
3093 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
3094 while ((filter_name != NULL)
3095 && (0 != strcmp(filter_group->name, filter_name->str)))
3097 filter_name = filter_name->next;
3099 if (filter_name != NULL)
3105 /* Generate a unique serial number */
3106 snprintf(number, sizeof(number), "%x", index++);
3107 number[sizeof(number) - 1] = '\0';
3109 line_exports = new_map();
3110 if (line_exports == NULL)
3112 err = JB_ERR_MEMORY;
3117 if (!err) err = map(line_exports, "index", 1, number, 1);
3118 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3119 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3120 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3125 this_line = strdup(filter_template);
3126 if (this_line == NULL) err = JB_ERR_MEMORY;
3128 if (!err) err = template_fill(&this_line, line_exports);
3129 string_join(&result, this_line);
3131 free_map(line_exports);
3135 freez(filter_template);
3139 err = map(exports, "filter-params", 1, result, 0);
3147 if (!err) err = map_radio(exports, "filter-all", "nx",
3148 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3150 edit_free_file(file);
3158 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3162 /*********************************************************************
3164 * Function : cgi_edit_actions_submit
3166 * Description : CGI function that actually edits the Actions list.
3169 * 1 : csp = Current client state (buffers, headers, etc...)
3170 * 2 : rsp = http_response data structure for output
3171 * 3 : parameters = map of cgi parameters
3173 * CGI Parameters : None
3175 * Returns : JB_ERR_OK on success
3176 * JB_ERR_MEMORY on out-of-memory
3177 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3178 * specified or not valid.
3180 *********************************************************************/
3181 jb_err cgi_edit_actions_submit(struct client_state *csp,
3182 struct http_response *rsp,
3183 const struct map *parameters)
3189 struct editable_file * file;
3190 struct file_line * cur_line;
3191 unsigned line_number;
3195 const char * action_set_name;
3197 struct file_list * fl;
3198 struct url_actions * b;
3200 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3202 return cgi_error_disabled(csp, rsp);
3205 err = get_number_param(csp, parameters, "s", §ionid);
3211 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3214 /* No filename specified, can't read file, modified, or out of memory. */
3215 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3218 cur_line = file->lines;
3220 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3222 cur_line = cur_line->next;
3225 if ( (cur_line == NULL)
3226 || (line_number != sectionid)
3228 || (cur_line->type != FILE_LINE_ACTION))
3230 /* Invalid "sectionid" parameter */
3231 edit_free_file(file);
3232 return JB_ERR_CGI_PARAMS;
3235 get_string_param(parameters, "p", &action_set_name);
3236 if (action_set_name != NULL)
3238 for (index = 0; index < MAX_ACTION_FILES; index++)
3240 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3242 for (b = b->next; NULL != b; b = b->next)
3244 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3246 copy_action(cur_line->data.action, b->action);
3252 edit_free_file(file);
3253 return JB_ERR_CGI_PARAMS;
3259 err = actions_from_radio(parameters, cur_line->data.action);
3265 edit_free_file(file);
3269 ch = get_char_param(parameters, "filter_all");
3272 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3273 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3274 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3278 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3281 for (index = 0; !err; index++)
3288 /* Generate the keys */
3289 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3290 key_value[sizeof(key_value) - 1] = '\0';
3291 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3292 key_name[sizeof(key_name) - 1] = '\0';
3294 err = get_string_param(parameters, key_name, &name);
3303 value = get_char_param(parameters, key_value);
3306 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3307 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3308 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3310 else if (value == 'N')
3312 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3313 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3315 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3316 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3319 else if (value == 'X')
3321 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3322 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3329 edit_free_file(file);
3333 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3336 edit_free_file(file);
3337 return JB_ERR_MEMORY;
3340 len = strlen(actiontext);
3344 * Empty action - must special-case this.
3345 * Simply setting len to 1 is sufficient...
3350 if (NULL == (newtext = malloc(len + 2)))
3354 edit_free_file(file);
3355 return JB_ERR_MEMORY;
3357 strcpy(newtext, actiontext);
3361 newtext[len + 1] = '\0';
3363 freez(cur_line->raw);
3364 freez(cur_line->unprocessed);
3365 cur_line->unprocessed = newtext;
3367 err = edit_write_file(file);
3370 /* Error writing file */
3371 if (err == JB_ERR_FILE)
3373 /* Read-only file. */
3374 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3376 edit_free_file(file);
3380 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3381 string_append(&target, file->identifier);
3382 string_join(&target, section_target(sectionid));
3385 edit_free_file(file);
3390 return JB_ERR_MEMORY;
3393 rsp->status = strdup("302 Local Redirect from Privoxy");
3394 if (rsp->status == NULL)
3397 return JB_ERR_MEMORY;
3399 err = enlist_unique_header(rsp->headers, "Location", target);
3406 /*********************************************************************
3408 * Function : cgi_edit_actions_url
3410 * Description : CGI function that actually edits a URL pattern in
3414 * 1 : csp = Current client state (buffers, headers, etc...)
3415 * 2 : rsp = http_response data structure for output
3416 * 3 : parameters = map of cgi parameters
3419 * filename : Identifies the file to edit
3420 * ver : File's last-modified time
3421 * section : Line number of section to edit
3422 * pattern : Line number of pattern to edit
3423 * newval : New value for pattern
3425 * Returns : JB_ERR_OK on success
3426 * JB_ERR_MEMORY on out-of-memory
3427 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3428 * specified or not valid.
3430 *********************************************************************/
3431 jb_err cgi_edit_actions_url(struct client_state *csp,
3432 struct http_response *rsp,
3433 const struct map *parameters)
3437 struct editable_file * file;
3438 struct file_line * cur_line;
3439 unsigned line_number;
3440 unsigned section_start_line_number = 0;
3444 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3446 return cgi_error_disabled(csp, rsp);
3449 err = get_number_param(csp, parameters, "p", &patternid);
3456 return JB_ERR_CGI_PARAMS;
3459 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3465 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3468 /* No filename specified, can't read file, modified, or out of memory. */
3470 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3474 cur_line = file->lines;
3476 while ((cur_line != NULL) && (line_number < patternid))
3478 if (cur_line->type == FILE_LINE_ACTION)
3480 section_start_line_number = line_number;
3482 cur_line = cur_line->next;
3486 if ( (cur_line == NULL)
3487 || (cur_line->type != FILE_LINE_URL))
3489 /* Invalid "patternid" parameter */
3491 edit_free_file(file);
3492 return JB_ERR_CGI_PARAMS;
3495 /* At this point, the line to edit is in cur_line */
3497 freez(cur_line->raw);
3498 freez(cur_line->unprocessed);
3499 cur_line->unprocessed = new_pattern;
3501 err = edit_write_file(file);
3504 /* Error writing file */
3505 if (err == JB_ERR_FILE)
3507 /* Read-only file. */
3508 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3510 edit_free_file(file);
3514 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3515 string_append(&target, file->identifier);
3516 string_join(&target, section_target(section_start_line_number));
3518 edit_free_file(file);
3523 return JB_ERR_MEMORY;
3526 rsp->status = strdup("302 Local Redirect from Privoxy");
3527 if (rsp->status == NULL)
3530 return JB_ERR_MEMORY;
3532 err = enlist_unique_header(rsp->headers, "Location", target);
3539 /*********************************************************************
3541 * Function : cgi_edit_actions_add_url
3543 * Description : CGI function that actually adds a URL pattern to
3547 * 1 : csp = Current client state (buffers, headers, etc...)
3548 * 2 : rsp = http_response data structure for output
3549 * 3 : parameters = map of cgi parameters
3552 * filename : Identifies the file to edit
3553 * ver : File's last-modified time
3554 * section : Line number of section to edit
3555 * newval : New pattern
3557 * Returns : JB_ERR_OK on success
3558 * JB_ERR_MEMORY on out-of-memory
3559 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3560 * specified or not valid.
3562 *********************************************************************/
3563 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3564 struct http_response *rsp,
3565 const struct map *parameters)
3569 struct file_line * new_line;
3570 struct editable_file * file;
3571 struct file_line * cur_line;
3572 unsigned line_number;
3576 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3578 return cgi_error_disabled(csp, rsp);
3581 err = get_number_param(csp, parameters, "s", §ionid);
3588 return JB_ERR_CGI_PARAMS;
3591 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3597 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3600 /* No filename specified, can't read file, modified, or out of memory. */
3602 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3606 cur_line = file->lines;
3608 while ((cur_line != NULL) && (line_number < sectionid))
3610 cur_line = cur_line->next;
3614 if ( (cur_line == NULL)
3615 || (cur_line->type != FILE_LINE_ACTION))
3617 /* Invalid "sectionid" parameter */
3619 edit_free_file(file);
3620 return JB_ERR_CGI_PARAMS;
3623 /* At this point, the section header is in cur_line - add after this. */
3625 /* Allocate the new line */
3626 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3627 if (new_line == NULL)
3630 edit_free_file(file);
3631 return JB_ERR_MEMORY;
3634 /* Fill in the data members of the new line */
3635 new_line->raw = NULL;
3636 new_line->prefix = NULL;
3637 new_line->unprocessed = new_pattern;
3638 new_line->type = FILE_LINE_URL;
3640 /* Link new_line into the list, after cur_line */
3641 new_line->next = cur_line->next;
3642 cur_line->next = new_line;
3644 /* Done making changes, now commit */
3646 err = edit_write_file(file);
3649 /* Error writing file */
3650 if (err == JB_ERR_FILE)
3652 /* Read-only file. */
3653 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3655 edit_free_file(file);
3659 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3660 string_append(&target, file->identifier);
3661 string_join(&target, section_target(sectionid));
3663 edit_free_file(file);
3668 return JB_ERR_MEMORY;
3671 rsp->status = strdup("302 Local Redirect from Privoxy");
3672 if (rsp->status == NULL)
3675 return JB_ERR_MEMORY;
3677 err = enlist_unique_header(rsp->headers, "Location", target);
3684 /*********************************************************************
3686 * Function : cgi_edit_actions_remove_url
3688 * Description : CGI function that actually removes a URL pattern from
3692 * 1 : csp = Current client state (buffers, headers, etc...)
3693 * 2 : rsp = http_response data structure for output
3694 * 3 : parameters = map of cgi parameters
3697 * f : (filename) Identifies the file to edit
3698 * v : (version) File's last-modified time
3699 * p : (pattern) Line number of pattern to remove
3701 * Returns : JB_ERR_OK on success
3702 * JB_ERR_MEMORY on out-of-memory
3703 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3704 * specified or not valid.
3706 *********************************************************************/
3707 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3708 struct http_response *rsp,
3709 const struct map *parameters)
3712 struct editable_file * file;
3713 struct file_line * cur_line;
3714 struct file_line * prev_line;
3715 unsigned line_number;
3716 unsigned section_start_line_number = 0;
3720 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3722 return cgi_error_disabled(csp, rsp);
3725 err = get_number_param(csp, parameters, "p", &patternid);
3731 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3734 /* No filename specified, can't read file, modified, or out of memory. */
3735 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3740 cur_line = file->lines;
3742 while ((cur_line != NULL) && (line_number < patternid))
3744 if (cur_line->type == FILE_LINE_ACTION)
3746 section_start_line_number = line_number;
3748 prev_line = cur_line;
3749 cur_line = cur_line->next;
3753 if ( (cur_line == NULL)
3754 || (prev_line == NULL)
3755 || (cur_line->type != FILE_LINE_URL))
3757 /* Invalid "patternid" parameter */
3758 edit_free_file(file);
3759 return JB_ERR_CGI_PARAMS;
3762 /* At this point, the line to remove is in cur_line, and the previous
3763 * one is in prev_line
3766 /* Unlink cur_line */
3767 prev_line->next = cur_line->next;
3768 cur_line->next = NULL;
3771 edit_free_file_lines(cur_line);
3773 err = edit_write_file(file);
3776 /* Error writing file */
3777 if (err == JB_ERR_FILE)
3779 /* Read-only file. */
3780 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3782 edit_free_file(file);
3786 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3787 string_append(&target, file->identifier);
3788 string_join(&target, section_target(section_start_line_number));
3790 edit_free_file(file);
3795 return JB_ERR_MEMORY;
3798 rsp->status = strdup("302 Local Redirect from Privoxy");
3799 if (rsp->status == NULL)
3802 return JB_ERR_MEMORY;
3804 err = enlist_unique_header(rsp->headers, "Location", target);
3811 /*********************************************************************
3813 * Function : cgi_edit_actions_section_remove
3815 * Description : CGI function that actually removes a whole section from
3816 * the actions file. The section must be empty first
3817 * (else JB_ERR_CGI_PARAMS).
3820 * 1 : csp = Current client state (buffers, headers, etc...)
3821 * 2 : rsp = http_response data structure for output
3822 * 3 : parameters = map of cgi parameters
3825 * f : (filename) Identifies the file to edit
3826 * v : (version) File's last-modified time
3827 * s : (section) Line number of section to edit
3829 * Returns : JB_ERR_OK on success
3830 * JB_ERR_MEMORY on out-of-memory
3831 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3832 * specified or not valid.
3834 *********************************************************************/
3835 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3836 struct http_response *rsp,
3837 const struct map *parameters)
3840 struct editable_file * file;
3841 struct file_line * cur_line;
3842 struct file_line * prev_line;
3843 unsigned line_number;
3847 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3849 return cgi_error_disabled(csp, rsp);
3852 err = get_number_param(csp, parameters, "s", §ionid);
3858 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3861 /* No filename specified, can't read file, modified, or out of memory. */
3862 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3866 cur_line = file->lines;
3869 while ((cur_line != NULL) && (line_number < sectionid))
3871 prev_line = cur_line;
3872 cur_line = cur_line->next;
3876 if ( (cur_line == NULL)
3877 || (cur_line->type != FILE_LINE_ACTION) )
3879 /* Invalid "sectionid" parameter */
3880 edit_free_file(file);
3881 return JB_ERR_CGI_PARAMS;
3884 if ( (cur_line->next != NULL)
3885 && (cur_line->next->type == FILE_LINE_URL) )
3887 /* Section not empty. */
3888 edit_free_file(file);
3889 return JB_ERR_CGI_PARAMS;
3892 /* At this point, the line to remove is in cur_line, and the previous
3893 * one is in prev_line
3896 /* Unlink cur_line */
3897 if (prev_line == NULL)
3899 /* Removing the first line from the file */
3900 file->lines = cur_line->next;
3904 prev_line->next = cur_line->next;
3906 cur_line->next = NULL;
3909 edit_free_file_lines(cur_line);
3911 err = edit_write_file(file);
3914 /* Error writing file */
3915 if (err == JB_ERR_FILE)
3917 /* Read-only file. */
3918 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3920 edit_free_file(file);
3924 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3925 string_append(&target, file->identifier);
3927 edit_free_file(file);
3932 return JB_ERR_MEMORY;
3935 rsp->status = strdup("302 Local Redirect from Privoxy");
3936 if (rsp->status == NULL)
3939 return JB_ERR_MEMORY;
3941 err = enlist_unique_header(rsp->headers, "Location", target);
3948 /*********************************************************************
3950 * Function : cgi_edit_actions_section_add
3952 * Description : CGI function that adds a new empty section to
3956 * 1 : csp = Current client state (buffers, headers, etc...)
3957 * 2 : rsp = http_response data structure for output
3958 * 3 : parameters = map of cgi parameters
3961 * f : (filename) Identifies the file to edit
3962 * v : (version) File's last-modified time
3963 * s : (section) Line number of section to add after, 0 for
3966 * Returns : JB_ERR_OK on success
3967 * JB_ERR_MEMORY on out-of-memory
3968 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3969 * specified or not valid.
3971 *********************************************************************/
3972 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3973 struct http_response *rsp,
3974 const struct map *parameters)
3977 struct file_line * new_line;
3979 struct editable_file * file;
3980 struct file_line * cur_line;
3981 unsigned line_number;
3985 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3987 return cgi_error_disabled(csp, rsp);
3990 err = get_number_param(csp, parameters, "s", §ionid);
3996 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3999 /* No filename specified, can't read file, modified, or out of memory. */
4000 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4004 cur_line = file->lines;
4006 if (sectionid <= 1U)
4008 /* Add to start of file */
4009 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4011 /* There's something in the file, find the line before the first
4014 while ( (cur_line->next != NULL)
4015 && (cur_line->next->type != FILE_LINE_ACTION) )
4017 cur_line = cur_line->next;
4023 /* File starts with action line, so insert at top */
4029 /* Add after stated section. */
4030 while ((cur_line != NULL) && (line_number < sectionid))
4032 cur_line = cur_line->next;
4036 if ( (cur_line == NULL)
4037 || (cur_line->type != FILE_LINE_ACTION))
4039 /* Invalid "sectionid" parameter */
4040 edit_free_file(file);
4041 return JB_ERR_CGI_PARAMS;
4044 /* Skip through the section to find the last line in it. */
4045 while ( (cur_line->next != NULL)
4046 && (cur_line->next->type != FILE_LINE_ACTION) )
4048 cur_line = cur_line->next;
4053 /* At this point, the last line in the previous section is in cur_line
4054 * - add after this. (Or if we need to add as the first line, cur_line
4058 new_text = strdup("{}");
4059 if (NULL == new_text)
4061 edit_free_file(file);
4062 return JB_ERR_MEMORY;
4065 /* Allocate the new line */
4066 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4067 if (new_line == NULL)
4070 edit_free_file(file);
4071 return JB_ERR_MEMORY;
4074 /* Fill in the data members of the new line */
4075 new_line->raw = NULL;
4076 new_line->prefix = NULL;
4077 new_line->unprocessed = new_text;
4078 new_line->type = FILE_LINE_ACTION;
4080 if (cur_line != NULL)
4082 /* Link new_line into the list, after cur_line */
4083 new_line->next = cur_line->next;
4084 cur_line->next = new_line;
4088 /* Link new_line into the list, as first line */
4089 new_line->next = file->lines;
4090 file->lines = new_line;
4093 /* Done making changes, now commit */
4095 err = edit_write_file(file);
4098 /* Error writing file */
4099 if (err == JB_ERR_FILE)
4101 /* Read-only file. */
4102 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4104 edit_free_file(file);
4108 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4109 string_append(&target, file->identifier);
4111 edit_free_file(file);
4116 return JB_ERR_MEMORY;
4119 rsp->status = strdup("302 Local Redirect from Privoxy");
4120 if (rsp->status == NULL)
4123 return JB_ERR_MEMORY;
4125 err = enlist_unique_header(rsp->headers, "Location", target);
4132 /*********************************************************************
4134 * Function : cgi_edit_actions_section_swap
4136 * Description : CGI function that swaps the order of two sections
4137 * in the actions file. Note that this CGI can actually
4138 * swap any two arbitrary sections, but the GUI interface
4139 * currently only allows consecutive sections to be
4143 * 1 : csp = Current client state (buffers, headers, etc...)
4144 * 2 : rsp = http_response data structure for output
4145 * 3 : parameters = map of cgi parameters
4148 * f : (filename) Identifies the file to edit
4149 * v : (version) File's last-modified time
4150 * s1 : (section1) Line number of first section to swap
4151 * s2 : (section2) Line number of second section to swap
4153 * Returns : JB_ERR_OK on success
4154 * JB_ERR_MEMORY on out-of-memory
4155 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4156 * specified or not valid.
4158 *********************************************************************/
4159 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4160 struct http_response *rsp,
4161 const struct map *parameters)
4165 struct editable_file * file;
4166 struct file_line * cur_line;
4167 struct file_line * prev_line;
4168 struct file_line * line_before_section1;
4169 struct file_line * line_start_section1;
4170 struct file_line * line_end_section1;
4171 struct file_line * line_after_section1;
4172 struct file_line * line_before_section2;
4173 struct file_line * line_start_section2;
4174 struct file_line * line_end_section2;
4175 struct file_line * line_after_section2;
4176 unsigned line_number;
4180 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4182 return cgi_error_disabled(csp, rsp);
4185 err = get_number_param(csp, parameters, "s1", §ion1);
4186 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4192 if (section1 > section2)
4194 unsigned temp = section2;
4195 section2 = section1;
4199 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4202 /* No filename specified, can't read file, modified, or out of memory. */
4203 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4206 /* Start at the beginning... */
4208 cur_line = file->lines;
4211 /* ... find section1 ... */
4212 while ((cur_line != NULL) && (line_number < section1))
4214 prev_line = cur_line;
4215 cur_line = cur_line->next;
4219 if ( (cur_line == NULL)
4220 || (cur_line->type != FILE_LINE_ACTION) )
4222 /* Invalid "section1" parameter */
4223 edit_free_file(file);
4224 return JB_ERR_CGI_PARAMS;
4227 /* If no-op, we've validated params and can skip the rest. */
4228 if (section1 != section2)
4230 /* ... find the end of section1 ... */
4231 line_before_section1 = prev_line;
4232 line_start_section1 = cur_line;
4235 prev_line = cur_line;
4236 cur_line = cur_line->next;
4239 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4240 line_end_section1 = prev_line;
4241 line_after_section1 = cur_line;
4243 /* ... find section2 ... */
4244 while ((cur_line != NULL) && (line_number < section2))
4246 prev_line = cur_line;
4247 cur_line = cur_line->next;
4251 if ( (cur_line == NULL)
4252 || (cur_line->type != FILE_LINE_ACTION) )
4254 /* Invalid "section2" parameter */
4255 edit_free_file(file);
4256 return JB_ERR_CGI_PARAMS;
4259 /* ... find the end of section2 ... */
4260 line_before_section2 = prev_line;
4261 line_start_section2 = cur_line;
4264 prev_line = cur_line;
4265 cur_line = cur_line->next;
4268 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4269 line_end_section2 = prev_line;
4270 line_after_section2 = cur_line;
4272 /* Now have all the pointers we need. Do the swap. */
4274 /* Change the pointer to section1 to point to section2 instead */
4275 if (line_before_section1 == NULL)
4277 file->lines = line_start_section2;
4281 line_before_section1->next = line_start_section2;
4284 if (line_before_section2 == line_end_section1)
4286 /* Consecutive sections */
4287 line_end_section2->next = line_start_section1;
4291 line_end_section2->next = line_after_section1;
4292 line_before_section2->next = line_start_section1;
4295 /* Set the pointer from the end of section1 to the rest of the file */
4296 line_end_section1->next = line_after_section2;
4298 err = edit_write_file(file);
4301 /* Error writing file */
4302 if (err == JB_ERR_FILE)
4304 /* Read-only file. */
4305 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4307 edit_free_file(file);
4310 } /* END if (section1 != section2) */
4312 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4313 string_append(&target, file->identifier);
4315 edit_free_file(file);
4320 return JB_ERR_MEMORY;
4323 rsp->status = strdup("302 Local Redirect from Privoxy");
4324 if (rsp->status == NULL)
4327 return JB_ERR_MEMORY;
4329 err = enlist_unique_header(rsp->headers, "Location", target);
4336 /*********************************************************************
4338 * Function : cgi_toggle
4340 * Description : CGI function that adds a new empty section to
4344 * 1 : csp = Current client state (buffers, headers, etc...)
4345 * 2 : rsp = http_response data structure for output
4346 * 3 : parameters = map of cgi parameters
4349 * set : If present, how to change toggle setting:
4350 * "enable", "disable", "toggle", or none (default).
4351 * mini : If present, use mini reply template.
4353 * Returns : JB_ERR_OK on success
4354 * JB_ERR_MEMORY on out-of-memory
4356 *********************************************************************/
4357 jb_err cgi_toggle(struct client_state *csp,
4358 struct http_response *rsp,
4359 const struct map *parameters)
4361 struct map *exports;
4363 const char *template_name;
4369 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4371 return cgi_error_disabled(csp, rsp);
4374 mode = get_char_param(parameters, "set");
4381 else if (mode == 'D')
4386 else if (mode == 'T')
4389 g_bToggleIJB = !g_bToggleIJB;
4392 if (NULL == (exports = default_exports(csp, "toggle")))
4394 return JB_ERR_MEMORY;
4397 template_name = (get_char_param(parameters, "mini")
4401 return template_fill_for_cgi(csp, template_name, exports, rsp);
4405 /*********************************************************************
4407 * Function : javascriptify
4409 * Description : Converts a string into a form JavaScript will like.
4411 * Netscape 4's JavaScript sucks - it doesn't use
4412 * "id" parameters, so you have to set the "name"
4413 * used to submit a form element to something JavaScript
4414 * will like. (Or access the elements by index in an
4415 * array. That array contains >60 elements and will
4416 * be changed whenever we add a new action to the
4417 * editor, so I'm NOT going to use indexes that have
4418 * to be figured out by hand.)
4420 * Currently the only thing we have to worry about
4421 * is "-" ==> "_" conversion.
4423 * This is a length-preserving operation so it is
4424 * carried out in-place, no memory is allocated
4428 * 1 : identifier = String to make JavaScript-friendly.
4432 *********************************************************************/
4433 static void javascriptify(char * identifier)
4435 char * p = identifier;
4436 while (NULL != (p = strchr(p, '-')))
4443 /*********************************************************************
4445 * Function : actions_to_radio
4447 * Description : Converts a actionsfile entry into settings for
4448 * radio buttons and edit boxes on a HTML form.
4451 * 1 : exports = List of substitutions to add to.
4452 * 2 : action = Action to read
4454 * Returns : JB_ERR_OK on success
4455 * JB_ERR_MEMORY on out-of-memory
4457 *********************************************************************/
4458 static jb_err actions_to_radio(struct map * exports,
4459 const struct action_spec *action)
4461 unsigned mask = action->mask;
4462 unsigned add = action->add;
4470 mask = action->mask;
4473 /* sanity - prevents "-feature +feature" */
4477 #define DEFINE_ACTION_BOOL(name, bit) \
4478 if (!(mask & bit)) \
4480 current_mode = 'n'; \
4482 else if (add & bit) \
4484 current_mode = 'y'; \
4488 current_mode = 'x'; \
4490 if (map_radio(exports, name, "ynx", current_mode)) \
4492 return JB_ERR_MEMORY; \
4495 #define DEFINE_ACTION_STRING(name, bit, index) \
4496 DEFINE_ACTION_BOOL(name, bit); \
4499 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4502 checked = !strcmp(action->string[index], value); \
4506 checked = is_default; \
4508 mapped_param |= checked; \
4509 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4511 return JB_ERR_MEMORY; \
4514 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4515 if (map(exports, name "-param-custom", 1, \
4516 ((!mapped_param) ? "checked" : ""), 1)) \
4518 return JB_ERR_MEMORY; \
4520 if (map(exports, name "-param", 1, \
4521 (((add & bit) && !mapped_param) ? \
4522 action->string[index] : default_val), 1)) \
4524 return JB_ERR_MEMORY; \
4527 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4528 if (map(exports, name "-param", 1, \
4529 ((add & bit) ? action->string[index] : default_val), 1)) \
4531 return JB_ERR_MEMORY; \
4534 #define DEFINE_ACTION_MULTI(name, index) \
4535 if (action->multi_add[index]->first) \
4537 current_mode = 'y'; \
4539 else if (action->multi_remove_all[index]) \
4541 current_mode = 'n'; \
4543 else if (action->multi_remove[index]->first) \
4545 current_mode = 'y'; \
4549 current_mode = 'x'; \
4551 if (map_radio(exports, name, "ynx", current_mode)) \
4553 return JB_ERR_MEMORY; \
4556 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4558 #include "actionlist.h"
4560 #undef DEFINE_ACTION_MULTI
4561 #undef DEFINE_ACTION_STRING
4562 #undef DEFINE_ACTION_BOOL
4563 #undef DEFINE_ACTION_ALIAS
4564 #undef DEFINE_CGI_PARAM_CUSTOM
4565 #undef DEFINE_CGI_PARAM_RADIO
4566 #undef DEFINE_CGI_PARAM_NO_RADIO
4572 /*********************************************************************
4574 * Function : actions_from_radio
4576 * Description : Converts a map of parameters passed to a CGI function
4577 * into an actionsfile entry.
4580 * 1 : parameters = parameters to the CGI call
4581 * 2 : action = Action to change. Must be valid before
4582 * the call, actions not specified will be
4585 * Returns : JB_ERR_OK on success
4586 * JB_ERR_MEMORY on out-of-memory
4588 *********************************************************************/
4589 static jb_err actions_from_radio(const struct map * parameters,
4590 struct action_spec *action)
4595 const char * js_name;
4596 jb_err err = JB_ERR_OK;
4601 /* Statics are generally a potential race condition,
4602 * but in this case we're safe and don't need semaphores.
4603 * Be careful if you modify this function.
4605 * The js_name_arr's are never free()d, but this is no
4606 * problem, since they will only be created once and
4607 * used by all threads thereafter. -oes
4610 #define JAVASCRIPTIFY(dest_var, string) \
4612 static int first_time = 1; \
4613 static char *js_name_arr; \
4616 js_name_arr = strdup(string); \
4617 javascriptify(js_name_arr); \
4619 dest_var = js_name_arr; \
4623 #define DEFINE_ACTION_BOOL(name, bit) \
4624 JAVASCRIPTIFY(js_name, name); \
4625 ch = get_char_param(parameters, js_name); \
4628 action->add |= bit; \
4629 action->mask |= bit; \
4631 else if (ch == 'N') \
4633 action->add &= ~bit; \
4634 action->mask &= ~bit; \
4636 else if (ch == 'X') \
4638 action->add &= ~bit; \
4639 action->mask |= bit; \
4642 #define DEFINE_ACTION_STRING(name, bit, index) \
4643 JAVASCRIPTIFY(js_name, name); \
4644 ch = get_char_param(parameters, js_name); \
4648 JAVASCRIPTIFY(js_name, name "-mode"); \
4649 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4650 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4652 JAVASCRIPTIFY(js_name, name "-param"); \
4653 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4655 if (param != NULL) \
4657 if (NULL == (param_dup = strdup(param))) \
4659 return JB_ERR_MEMORY; \
4661 freez(action->string[index]); \
4662 action->add |= bit; \
4663 action->mask |= bit; \
4664 action->string[index] = param_dup; \
4667 else if (ch == 'N') \
4669 if (action->add & bit) \
4671 freez(action->string[index]); \
4673 action->add &= ~bit; \
4674 action->mask &= ~bit; \
4676 else if (ch == 'X') \
4678 if (action->add & bit) \
4680 freez(action->string[index]); \
4682 action->add &= ~bit; \
4683 action->mask |= bit; \
4686 #define DEFINE_ACTION_MULTI(name, index) \
4687 JAVASCRIPTIFY(js_name, name); \
4688 ch = get_char_param(parameters, js_name); \
4693 else if (ch == 'N') \
4695 list_remove_all(action->multi_add[index]); \
4696 list_remove_all(action->multi_remove[index]); \
4697 action->multi_remove_all[index] = 1; \
4699 else if (ch == 'X') \
4701 list_remove_all(action->multi_add[index]); \
4702 list_remove_all(action->multi_remove[index]); \
4703 action->multi_remove_all[index] = 0; \
4706 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4708 #include "actionlist.h"
4710 #undef DEFINE_ACTION_MULTI
4711 #undef DEFINE_ACTION_STRING
4712 #undef DEFINE_ACTION_BOOL
4713 #undef DEFINE_ACTION_ALIAS
4714 #undef JAVASCRIPTIFY
4720 #endif /* def FEATURE_CGI_EDIT_ACTIONS */