1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.41.2.2 2002/08/05 20:02:59 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.2 2002/08/05 20:02:59 oes
46 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
48 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
49 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
51 * Revision 1.41 2002/05/21 19:09:45 oes
52 * - Made Add/Edit/Remove URL Submit and Cancel
53 * buttons jump back to relevant section in eal
54 * - Bugfix: remove-url-form needs p export
56 * Revision 1.40 2002/05/19 11:34:35 jongfoster
57 * Handling read-only actions files better - report the actual
58 * error, not "Out of memory"!
61 * http://sourceforge.net/tracker/index.php?func=detail
62 * &aid=557905&group_id=11118&atid=111118
64 * Revision 1.39 2002/05/12 21:39:15 jongfoster
65 * - Adding Doxygen-style comments to structures and #defines.
66 * - Correcting function comments
68 * Revision 1.38 2002/05/03 23:00:38 jongfoster
69 * Support for templates for "standard actions" buttons.
72 * Revision 1.37 2002/04/30 11:14:52 oes
73 * Made csp the first parameter in *action_to_html
75 * Revision 1.36 2002/04/26 21:53:30 jongfoster
76 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
78 * Revision 1.35 2002/04/26 21:50:02 jongfoster
79 * Honouring default exports in edit-actions-for-url-filter template.
81 * Revision 1.34 2002/04/26 12:54:17 oes
82 * Adaptions to changes in actions.c
84 * Revision 1.33 2002/04/24 02:17:47 oes
85 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
87 * - Activated Jon's code for editing multiple AFs
88 * - cgi_edit_list_actions now provides context-sensitive
89 * help, looks up all action sets from standard.action and
90 * makes buttons for them in the catchall section
91 * - cgi_edit_action_submit now honors a p parameter, looks up
92 * the corresponding action set, and sets the catchall pattern's
93 * actions accordingly.
95 * Revision 1.32 2002/04/19 16:55:31 jongfoster
96 * Fixing newline problems. If we do our own text file newline
97 * mangling, we don't want the library to do any, so we need to
98 * open the files in *binary* mode.
100 * Revision 1.31 2002/04/18 19:21:08 jongfoster
101 * Added code to detect "conventional" action files, that start
102 * with a set of actions for all URLs (the pattern "/").
103 * These are special-cased in the "edit-actions-list" CGI, so
104 * that a special UI can be written for them.
106 * Revision 1.30 2002/04/10 13:38:35 oes
107 * load_template signature changed
109 * Revision 1.29 2002/04/08 16:59:08 oes
112 * Revision 1.28 2002/03/27 12:30:29 oes
113 * Deleted unsused variable
115 * Revision 1.27 2002/03/26 23:06:04 jongfoster
116 * Removing duplicate @ifs on the toggle page
118 * Revision 1.26 2002/03/26 22:59:17 jongfoster
119 * Fixing /toggle to display status consistently.
121 * Revision 1.25 2002/03/26 22:29:54 swa
122 * we have a new homepage!
124 * Revision 1.24 2002/03/24 15:23:33 jongfoster
127 * Revision 1.23 2002/03/24 13:32:41 swa
128 * name change related issues
130 * Revision 1.22 2002/03/24 13:25:43 swa
131 * name change related issues
133 * Revision 1.21 2002/03/22 18:02:48 jongfoster
134 * Fixing remote toggle
136 * Revision 1.20 2002/03/16 20:28:34 oes
137 * Added descriptions to the filters so users will know what they select in the cgi editor
139 * Revision 1.19 2002/03/16 18:38:14 jongfoster
140 * Stopping stupid or malicious users from breaking the actions
141 * file using the web-based editor.
143 * Revision 1.18 2002/03/16 14:57:44 jongfoster
144 * Full support for enabling/disabling modular filters.
146 * Revision 1.17 2002/03/16 14:26:42 jongfoster
147 * First version of modular filters support - READ ONLY!
148 * Fixing a double-free bug in the out-of-memory handling in map_radio().
150 * Revision 1.16 2002/03/07 03:46:17 oes
151 * Fixed compiler warnings
153 * Revision 1.15 2002/03/06 22:54:35 jongfoster
154 * Automated function-comment nitpicking.
156 * Revision 1.14 2002/03/05 00:24:51 jongfoster
157 * Patch to always edit the current actions file.
159 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
160 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
162 * Revision 1.12 2002/03/03 09:18:03 joergs
163 * Made jumbjuster work on AmigaOS again.
165 * Revision 1.11 2002/01/23 01:03:31 jongfoster
166 * Fixing gcc [CygWin] compiler warnings
168 * Revision 1.10 2002/01/23 00:22:59 jongfoster
169 * Adding new function cgi_edit_actions_section_swap(), to reorder
172 * Adding get_url_spec_param() to get a validated URL pattern.
174 * Moving edit_read_line() out of this file and into loaders.c.
176 * Adding missing html_encode() to many CGI functions.
178 * Moving the functions that #include actionlist.h to the end of the file,
179 * because the Visual C++ 97 debugger gets extremely confused if you try
180 * to debug any code that comes after them in the file.
182 * Major optimizations in cgi_edit_actions_list() to reduce the size of
183 * the generated HTML (down 40% from 550k to 304k), with major side-effects
184 * throughout the editor and templates. In particular, the length of the
185 * URLs throughout the editor has been drastically reduced, by cutting
186 * paramater names down to 1 character and CGI names down to 3-4
187 * characters, by removing all non-essential CGI paramaters even at the
188 * expense of having to re-read the actions file for the most trivial
189 * page, and by using relative rather than absolute URLs. This means
190 * that this (typical example):
192 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
193 * filename=ijb&ver=1011487572&section=12&pattern=13
194 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
198 * <a href="eau?f=ijb&v=1011487572&p=13">
200 * Revision 1.9 2002/01/17 20:56:22 jongfoster
201 * Replacing hard references to the URL of the config interface
202 * with #defines from project.h
204 * Revision 1.8 2001/11/30 23:35:51 jongfoster
205 * Renaming actionsfile to ijb.action
207 * Revision 1.7 2001/11/13 00:28:24 jongfoster
208 * - Renaming parameters from edit-actions-for-url so that they only
209 * contain legal JavaScript characters. If we wanted to write
210 * JavaScript that worked with Netscape 4, this is nessacery.
211 * (Note that at the moment the JavaScript doesn't actually work
212 * with Netscape 4, but now this is purely a template issue, not
213 * one affecting code).
214 * - Adding new CGIs for use by non-JavaScript browsers:
215 * edit-actions-url-form
216 * edit-actions-add-url-form
217 * edit-actions-remove-url-form
220 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
221 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
222 * by and __OS2__ ifdef.
224 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
225 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
226 * threads to call select() simultaneously. So, it's time to do a real, live,
227 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
228 * (native). Both versions will work, but using __OS2__ offers multi-threading.
230 * Revision 1.4 2001/10/23 21:48:19 jongfoster
231 * Cleaning up error handling in CGI functions - they now send back
232 * a HTML error page and should never cause a FATAL error. (Fixes one
233 * potential source of "denial of service" attacks).
235 * CGI actions file editor that works and is actually useful.
237 * Ability to toggle JunkBuster remotely using a CGI call.
239 * You can turn off both the above features in the main configuration
240 * file, e.g. if you are running a multi-user proxy.
242 * Revision 1.3 2001/10/14 22:12:49 jongfoster
243 * New version of CGI-based actionsfile editor.
244 * Major changes, including:
245 * - Completely new file parser and file output routines
246 * - edit-actions CGI renamed edit-actions-for-url
247 * - All CGIs now need a filename parameter, except for...
248 * - New CGI edit-actions which doesn't need a filename,
249 * to allow you to start the editor up.
250 * - edit-actions-submit now works, and now automatically
251 * redirects you back to the main edit-actions-list handler.
253 * Revision 1.2 2001/09/16 17:05:14 jongfoster
254 * Removing unused #include showarg.h
256 * Revision 1.1 2001/09/16 15:47:37 jongfoster
257 * First version of CGI-based edit interface. This is very much a
258 * work-in-progress, and you can't actually use it to edit anything
259 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
260 * to have any effect.
263 **********************************************************************/
269 * FIXME: Following includes copied from cgi.c - which are actually needed?
274 #include <sys/types.h>
278 #include <sys/stat.h>
281 #define snprintf _snprintf
282 #endif /* def _WIN32 */
287 #include "cgisimple.h"
291 #include "miscutil.h"
295 /* loadcfg.h is for g_bToggleIJB only */
296 #include "urlmatch.h"
298 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
301 #ifdef FEATURE_CGI_EDIT_ACTIONS
304 * A line in an editable_file.
308 /** Next entry in the linked list */
309 struct file_line * next;
311 /** The raw data, to write out if this line is unmodified. */
314 /** Comments and/or whitespace to put before this line if it's modified
315 and then written out. */
318 /** The actual data, as a string. Line continuation and comment removal
319 are performed on the data read from file before it's stored here, so
320 it will be a single line of data. */
323 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
326 /** The actual data, processed into some sensible data type. */
330 /** An action specification. */
331 struct action_spec action[1];
333 /** A name=value pair. */
337 /** The name in the name=value pair. */
340 /** The value in the name=value pair, as a string. */
343 /** The value in the name=value pair, as an integer. */
348 /* Add more data types here... e.g.
351 struct url_spec url[1];
355 struct action_spec action[1];
365 /** This file_line has not been processed yet. */
366 #define FILE_LINE_UNPROCESSED 1
368 /** This file_line is blank. Can only appear at the end of a file, due to
369 the way the parser works. */
370 #define FILE_LINE_BLANK 2
372 /** This file_line says {{alias}}. */
373 #define FILE_LINE_ALIAS_HEADER 3
375 /** This file_line defines an alias. */
376 #define FILE_LINE_ALIAS_ENTRY 4
378 /** This file_line defines an {action}. */
379 #define FILE_LINE_ACTION 5
381 /** This file_line specifies a URL pattern. */
382 #define FILE_LINE_URL 6
384 /** This file_line says {{settings}}. */
385 #define FILE_LINE_SETTINGS_HEADER 7
387 /** This file_line is in a {{settings}} block. */
388 #define FILE_LINE_SETTINGS_ENTRY 8
390 /** This file_line says {{description}}. */
391 #define FILE_LINE_DESCRIPTION_HEADER 9
393 /** This file_line is in a {{description}} block. */
394 #define FILE_LINE_DESCRIPTION_ENTRY 10
398 * A configuration file, in a format that can be edited and written back to
403 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
404 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
405 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
406 /**< Pre-encoded with url_encode() for ease of use. */
407 const char * version_str; /**< Last modification time, as a string. For CGI param. */
408 /**< Can be used in URL without using url_param(). */
409 unsigned version; /**< Last modification time - prevents chaos with
410 the browser's "back" button. Note that this is a
411 time_t cast to an unsigned. When comparing, always
412 cast the time_t to an unsigned, and *NOT* vice-versa.
413 This may lose the top few bits, but they're not
414 significant anyway. */
415 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
416 Note that changing this after the file has been
417 read in will cause a mess. */
418 struct file_line * parse_error; /**< On parse error, this is the offending line. */
419 const char * parse_error_text; /**< On parse error, this is the problem.
420 (Statically allocated) */
423 /* FIXME: Following non-static functions should be prototyped in .h or made static */
425 /* Functions to read and write arbitrary config files */
426 jb_err edit_read_file(struct client_state *csp,
427 const struct map *parameters,
430 struct editable_file **pfile);
431 jb_err edit_write_file(struct editable_file * file);
432 void edit_free_file(struct editable_file * file);
434 /* Functions to read and write actions files */
435 jb_err edit_parse_actions_file(struct editable_file * file);
436 jb_err edit_read_actions_file(struct client_state *csp,
437 struct http_response *rsp,
438 const struct map *parameters,
440 struct editable_file **pfile);
443 jb_err cgi_error_modified(struct client_state *csp,
444 struct http_response *rsp,
445 const char *filename);
446 jb_err cgi_error_parse(struct client_state *csp,
447 struct http_response *rsp,
448 struct editable_file *file);
449 jb_err cgi_error_file(struct client_state *csp,
450 struct http_response *rsp,
451 const char *filename);
452 jb_err cgi_error_file_read_only(struct client_state *csp,
453 struct http_response *rsp,
454 const char *filename);
455 jb_err cgi_error_disabled(struct client_state *csp,
456 struct http_response *rsp);
458 /* Internal arbitrary config file support functions */
459 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
460 static void edit_free_file_lines(struct file_line * first_line);
462 /* Internal actions file support functions */
463 static int match_actions_file_header_line(const char * line, const char * name);
464 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
466 /* Internal parameter parsing functions */
467 static jb_err get_file_name_param(struct client_state *csp,
468 const struct map *parameters,
469 const char *param_name,
472 const char **pparam);
474 static jb_err get_url_spec_param(struct client_state *csp,
475 const struct map *parameters,
480 /* Internal actionsfile <==> HTML conversion functions */
481 static jb_err map_radio(struct map * exports,
482 const char * optionname,
485 static jb_err actions_to_radio(struct map * exports,
486 const struct action_spec *action);
487 static jb_err actions_from_radio(const struct map * parameters,
488 struct action_spec *action);
491 static jb_err map_copy_parameter_html(struct map *out,
492 const struct map *in,
494 #if 0 /* unused function */
495 static jb_err map_copy_parameter_url(struct map *out,
496 const struct map *in,
498 #endif /* unused function */
500 /* Internal convenience functions */
501 static char *section_target(const unsigned sectionid);
503 /*********************************************************************
505 * Function : section_target
507 * Description : Given an unsigned (section id) n, produce a dynamically
508 * allocated string of the form #l<n>, for use in link
512 * 1 : sectionid = start line number of section
514 * Returns : String with link target, or NULL if out of
517 *********************************************************************/
518 static char *section_target(const unsigned sectionid)
522 snprintf(buf, 30, "#l%d", sectionid);
528 /*********************************************************************
530 * Function : map_copy_parameter_html
532 * Description : Copy a CGI parameter from one map to another, HTML
536 * 1 : out = target map
537 * 2 : in = source map
538 * 3 : name = name of cgi parameter to copy
540 * Returns : JB_ERR_OK on success
541 * JB_ERR_MEMORY on out-of-memory
542 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
545 *********************************************************************/
546 static jb_err map_copy_parameter_html(struct map *out,
547 const struct map *in,
557 value = lookup(in, name);
558 err = map(out, name, 1, html_encode(value), 0);
565 else if (*value == '\0')
567 return JB_ERR_CGI_PARAMS;
576 #if 0 /* unused function */
577 /*********************************************************************
579 * Function : map_copy_parameter_url
581 * Description : Copy a CGI parameter from one map to another, URL
585 * 1 : out = target map
586 * 2 : in = source map
587 * 3 : name = name of cgi parameter to copy
589 * Returns : JB_ERR_OK on success
590 * JB_ERR_MEMORY on out-of-memory
591 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
594 *********************************************************************/
595 static jb_err map_copy_parameter_url(struct map *out,
596 const struct map *in,
606 value = lookup(in, name);
607 err = map(out, name, 1, url_encode(value), 0);
614 else if (*value == '\0')
616 return JB_ERR_CGI_PARAMS;
623 #endif /* 0 - unused function */
625 /*********************************************************************
627 * Function : cgi_edit_actions_url_form
629 * Description : CGI function that displays a form for
633 * 1 : csp = Current client state (buffers, headers, etc...)
634 * 2 : rsp = http_response data structure for output
635 * 3 : parameters = map of cgi parameters
638 * f : (filename) Identifies the file to edit
639 * v : (version) File's last-modified time
640 * p : (pattern) Line number of pattern to edit
642 * Returns : JB_ERR_OK on success
643 * JB_ERR_MEMORY on out-of-memory
644 * JB_ERR_CGI_PARAMS if the CGI parameters are not
645 * specified or not valid.
647 *********************************************************************/
648 jb_err cgi_edit_actions_url_form(struct client_state *csp,
649 struct http_response *rsp,
650 const struct map *parameters)
652 struct map * exports;
654 struct editable_file * file;
655 struct file_line * cur_line;
656 unsigned line_number;
657 unsigned section_start_line_number = 0;
664 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
666 return cgi_error_disabled(csp, rsp);
669 err = get_number_param(csp, parameters, "p", &patternid);
675 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
678 /* No filename specified, can't read file, modified, or out of memory. */
679 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
682 cur_line = file->lines;
684 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
686 if (cur_line->type == FILE_LINE_ACTION)
688 section_start_line_number = line_number;
690 cur_line = cur_line->next;
693 if ( (cur_line == NULL)
694 || (line_number != patternid)
696 || (cur_line->type != FILE_LINE_URL))
698 /* Invalid "patternid" parameter */
699 edit_free_file(file);
700 return JB_ERR_CGI_PARAMS;
703 if (NULL == (exports = default_exports(csp, NULL)))
705 edit_free_file(file);
706 return JB_ERR_MEMORY;
709 err = map(exports, "f", 1, file->identifier, 1);
710 if (!err) err = map(exports, "v", 1, file->version_str, 1);
711 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
712 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
713 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
715 edit_free_file(file);
723 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
727 /*********************************************************************
729 * Function : cgi_edit_actions_add_url_form
731 * Description : CGI function that displays a form for
735 * 1 : csp = Current client state (buffers, headers, etc...)
736 * 2 : rsp = http_response data structure for output
737 * 3 : parameters = map of cgi parameters
740 * f : (filename) Identifies the file to edit
741 * v : (version) File's last-modified time
742 * s : (section) Line number of section to edit
744 * Returns : JB_ERR_OK on success
745 * JB_ERR_MEMORY on out-of-memory
746 * JB_ERR_CGI_PARAMS if the CGI parameters are not
747 * specified or not valid.
749 *********************************************************************/
750 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
751 struct http_response *rsp,
752 const struct map *parameters)
761 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
763 return cgi_error_disabled(csp, rsp);
766 if (NULL == (exports = default_exports(csp, NULL)))
768 return JB_ERR_MEMORY;
771 err = map_copy_parameter_html(exports, parameters, "f");
772 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
773 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
781 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
785 /*********************************************************************
787 * Function : cgi_edit_actions_remove_url_form
789 * Description : CGI function that displays a form for
793 * 1 : csp = Current client state (buffers, headers, etc...)
794 * 2 : rsp = http_response data structure for output
795 * 3 : parameters = map of cgi parameters
798 * f : (filename) Identifies the file to edit
799 * v : (version) File's last-modified time
800 * p : (pattern) Line number of pattern to edit
802 * Returns : JB_ERR_OK on success
803 * JB_ERR_MEMORY on out-of-memory
804 * JB_ERR_CGI_PARAMS if the CGI parameters are not
805 * specified or not valid.
807 *********************************************************************/
808 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
809 struct http_response *rsp,
810 const struct map *parameters)
812 struct map * exports;
814 struct editable_file * file;
815 struct file_line * cur_line;
816 unsigned line_number;
817 unsigned section_start_line_number = 0;
824 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
826 return cgi_error_disabled(csp, rsp);
829 err = get_number_param(csp, parameters, "p", &patternid);
835 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
838 /* No filename specified, can't read file, modified, or out of memory. */
839 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
842 cur_line = file->lines;
844 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
846 if (cur_line->type == FILE_LINE_ACTION)
848 section_start_line_number = line_number;
850 cur_line = cur_line->next;
853 if ( (cur_line == NULL)
854 || (line_number != patternid)
856 || (cur_line->type != FILE_LINE_URL))
858 /* Invalid "patternid" parameter */
859 edit_free_file(file);
860 return JB_ERR_CGI_PARAMS;
863 if (NULL == (exports = default_exports(csp, NULL)))
865 edit_free_file(file);
866 return JB_ERR_MEMORY;
869 err = map(exports, "f", 1, file->identifier, 1);
870 if (!err) err = map(exports, "v", 1, file->version_str, 1);
871 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
872 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
873 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
875 edit_free_file(file);
883 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
887 /*********************************************************************
889 * Function : edit_write_file
891 * Description : Write a complete file to disk.
894 * 1 : file = File to write.
896 * Returns : JB_ERR_OK on success
897 * JB_ERR_FILE on error writing to file.
898 * JB_ERR_MEMORY on out of memory
900 *********************************************************************/
901 jb_err edit_write_file(struct editable_file * file)
904 struct file_line * cur_line;
905 struct stat statbuf[1];
906 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
907 digits in time_t, assuming this is a 64-bit
908 machine, plus null terminator, plus one
912 assert(file->filename);
914 if (NULL == (fp = fopen(file->filename, "wb")))
919 cur_line = file->lines;
920 while (cur_line != NULL)
924 if (fputs(cur_line->raw, fp) < 0)
932 if (cur_line->prefix)
934 if (fputs(cur_line->prefix, fp) < 0)
940 if (cur_line->unprocessed)
943 if (NULL != strchr(cur_line->unprocessed, '#'))
945 /* Must quote '#' characters */
952 /* Count number of # characters, so we know length of output string */
953 src = cur_line->unprocessed;
954 while (NULL != (src = strchr(src, '#')))
961 /* Allocate new memory for string */
962 len = strlen(cur_line->unprocessed);
963 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
965 /* Uh oh, just trashed file! */
967 return JB_ERR_MEMORY;
970 /* Loop through string from end */
971 src = cur_line->unprocessed + len;
972 dest = str + len + numhash;
973 for ( ; len >= 0; len--)
975 if ((*dest-- = *src--) == '#')
979 assert(numhash >= 0);
982 assert(numhash == 0);
983 assert(src + 1 == cur_line->unprocessed);
984 assert(dest + 1 == str);
986 if (fputs(str, fp) < 0)
997 /* Can write without quoting '#' characters. */
998 if (fputs(cur_line->unprocessed, fp) < 0)
1004 if (fputs(NEWLINE(file->newline), fp) < 0)
1012 /* FIXME: Write data from file->data->whatever */
1016 cur_line = cur_line->next;
1022 /* Update the version stamp in the file structure, since we just
1023 * wrote to the file & changed it's date.
1025 if (stat(file->filename, statbuf) < 0)
1027 /* Error, probably file not found. */
1030 file->version = (unsigned)statbuf->st_mtime;
1032 /* Correct file->version_str */
1033 freez(file->version_str);
1034 snprintf(version_buf, 22, "%u", file->version);
1035 version_buf[21] = '\0';
1036 file->version_str = strdup(version_buf);
1037 if (version_buf == NULL)
1039 return JB_ERR_MEMORY;
1046 /*********************************************************************
1048 * Function : edit_free_file
1050 * Description : Free a complete file in memory.
1053 * 1 : file = Data structure to free.
1057 *********************************************************************/
1058 void edit_free_file(struct editable_file * file)
1062 /* Silently ignore NULL pointer */
1066 edit_free_file_lines(file->lines);
1067 freez(file->filename);
1068 freez(file->identifier);
1069 freez(file->version_str);
1071 file->parse_error_text = NULL; /* Statically allocated */
1072 file->parse_error = NULL;
1078 /*********************************************************************
1080 * Function : edit_free_file_lines
1082 * Description : Free an entire linked list of file lines.
1085 * 1 : first_line = Data structure to free.
1089 *********************************************************************/
1090 static void edit_free_file_lines(struct file_line * first_line)
1092 struct file_line * next_line;
1094 while (first_line != NULL)
1096 next_line = first_line->next;
1097 first_line->next = NULL;
1098 freez(first_line->raw);
1099 freez(first_line->prefix);
1100 freez(first_line->unprocessed);
1101 switch(first_line->type)
1103 case 0: /* special case if memory zeroed */
1104 case FILE_LINE_UNPROCESSED:
1105 case FILE_LINE_BLANK:
1106 case FILE_LINE_ALIAS_HEADER:
1107 case FILE_LINE_SETTINGS_HEADER:
1108 case FILE_LINE_DESCRIPTION_HEADER:
1109 case FILE_LINE_DESCRIPTION_ENTRY:
1110 case FILE_LINE_ALIAS_ENTRY:
1112 /* No data is stored for these */
1115 case FILE_LINE_ACTION:
1116 free_action(first_line->data.action);
1119 case FILE_LINE_SETTINGS_ENTRY:
1120 freez(first_line->data.setting.name);
1121 freez(first_line->data.setting.svalue);
1124 /* Should never happen */
1128 first_line->type = 0; /* paranoia */
1130 first_line = next_line;
1135 /*********************************************************************
1137 * Function : match_actions_file_header_line
1139 * Description : Match an actions file {{header}} line
1142 * 1 : line = String from file
1143 * 2 : name = Header to match against
1145 * Returns : 0 iff they match.
1147 *********************************************************************/
1148 static int match_actions_file_header_line(const char * line, const char * name)
1156 if ((line[0] != '{') || (line[1] != '{'))
1162 /* Look for optional whitespace */
1163 while ( (*line == ' ') || (*line == '\t') )
1168 /* Look for the specified name (case-insensitive) */
1170 if (0 != strncmpic(line, name, len))
1176 /* Look for optional whitespace */
1177 while ( (*line == ' ') || (*line == '\t') )
1182 /* Look for "}}" and end of string*/
1183 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1193 /*********************************************************************
1195 * Function : match_actions_file_header_line
1197 * Description : Match an actions file {{header}} line
1200 * 1 : line = String from file. Must not start with
1201 * whitespace (else infinite loop!)
1202 * 2 : pname = Destination for name
1203 * 2 : pvalue = Destination for value
1205 * Returns : JB_ERR_OK on success
1206 * JB_ERR_MEMORY on out-of-memory
1207 * JB_ERR_PARSE if there's no "=" sign, or if there's
1208 * nothing before the "=" sign (but empty
1209 * values *after* the "=" sign are legal).
1211 *********************************************************************/
1212 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1214 const char * name_end;
1215 const char * value_start;
1221 assert(*line != ' ');
1222 assert(*line != '\t');
1227 value_start = strchr(line, '=');
1228 if ((value_start == NULL) || (value_start == line))
1230 return JB_ERR_PARSE;
1233 name_end = value_start - 1;
1235 /* Eat any whitespace before the '=' */
1236 while ((*name_end == ' ') || (*name_end == '\t'))
1239 * we already know we must have at least 1 non-ws char
1240 * at start of buf - no need to check
1245 name_len = name_end - line + 1; /* Length excluding \0 */
1246 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1248 return JB_ERR_MEMORY;
1250 strncpy(*pname, line, name_len);
1251 (*pname)[name_len] = '\0';
1253 /* Eat any the whitespace after the '=' */
1255 while ((*value_start == ' ') || (*value_start == '\t'))
1260 if (NULL == (*pvalue = strdup(value_start)))
1264 return JB_ERR_MEMORY;
1271 /*********************************************************************
1273 * Function : edit_parse_actions_file
1275 * Description : Parse an actions file in memory.
1277 * Passed linked list must have the "data" member
1278 * zeroed, and must contain valid "next" and
1279 * "unprocessed" fields. The "raw" and "prefix"
1280 * fields are ignored, and "type" is just overwritten.
1282 * Note that on error the file may have been
1286 * 1 : file = Actions file to be parsed in-place.
1288 * Returns : JB_ERR_OK on success
1289 * JB_ERR_MEMORY on out-of-memory
1290 * JB_ERR_PARSE on error
1292 *********************************************************************/
1293 jb_err edit_parse_actions_file(struct editable_file * file)
1295 struct file_line * cur_line;
1297 const char * text; /* Text from a line */
1298 char * name; /* For lines of the form name=value */
1299 char * value; /* For lines of the form name=value */
1300 struct action_alias * alias_list = NULL;
1301 jb_err err = JB_ERR_OK;
1303 /* alias_list contains the aliases defined in this file.
1304 * It might be better to use the "file_line.data" fields
1305 * in the relavent places instead.
1308 cur_line = file->lines;
1310 /* A note about blank line support: Blank lines should only
1311 * ever occur as the last line in the file. This function
1312 * is more forgiving than that - FILE_LINE_BLANK can occur
1316 /* Skip leading blanks. Should only happen if file is
1317 * empty (which is valid, but pointless).
1319 while ( (cur_line != NULL)
1320 && (cur_line->unprocessed[0] == '\0') )
1323 cur_line->type = FILE_LINE_BLANK;
1324 cur_line = cur_line->next;
1327 if ( (cur_line != NULL)
1328 && (cur_line->unprocessed[0] != '{') )
1330 /* File doesn't start with a header */
1331 file->parse_error = cur_line;
1332 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1333 return JB_ERR_PARSE;
1336 if ( (cur_line != NULL) && (0 ==
1337 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1339 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1341 cur_line = cur_line->next;
1342 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1344 if (cur_line->unprocessed[0])
1346 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1348 err = split_line_on_equals(cur_line->unprocessed,
1349 &cur_line->data.setting.name,
1350 &cur_line->data.setting.svalue);
1351 if (err == JB_ERR_MEMORY)
1355 else if (err != JB_ERR_OK)
1357 /* Line does not contain a name=value pair */
1358 file->parse_error = cur_line;
1359 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1360 return JB_ERR_PARSE;
1365 cur_line->type = FILE_LINE_BLANK;
1367 cur_line = cur_line->next;
1371 if ( (cur_line != NULL) && (0 ==
1372 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1374 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1376 cur_line = cur_line->next;
1377 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1379 if (cur_line->unprocessed[0])
1381 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1385 cur_line->type = FILE_LINE_BLANK;
1387 cur_line = cur_line->next;
1391 if ( (cur_line != NULL) && (0 ==
1392 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1394 cur_line->type = FILE_LINE_ALIAS_HEADER;
1396 cur_line = cur_line->next;
1397 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1399 if (cur_line->unprocessed[0])
1401 /* define an alias */
1402 struct action_alias * new_alias;
1404 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1406 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1407 if (err == JB_ERR_MEMORY)
1411 else if (err != JB_ERR_OK)
1413 /* Line does not contain a name=value pair */
1414 file->parse_error = cur_line;
1415 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1416 return JB_ERR_PARSE;
1419 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1424 free_alias_list(alias_list);
1425 return JB_ERR_MEMORY;
1428 err = get_actions(value, alias_list, new_alias->action);
1431 /* Invalid action or out of memory */
1435 free_alias_list(alias_list);
1436 if (err == JB_ERR_MEMORY)
1442 /* Line does not contain a name=value pair */
1443 file->parse_error = cur_line;
1444 file->parse_error_text = "This alias does not specify a valid set of actions.";
1445 return JB_ERR_PARSE;
1451 new_alias->name = name;
1454 new_alias->next = alias_list;
1455 alias_list = new_alias;
1459 cur_line->type = FILE_LINE_BLANK;
1461 cur_line = cur_line->next;
1465 /* Header done, process the main part of the file */
1466 while (cur_line != NULL)
1468 /* At this point, (cur_line->unprocessed[0] == '{') */
1469 assert(cur_line->unprocessed[0] == '{');
1470 text = cur_line->unprocessed + 1;
1471 len = strlen(text) - 1;
1472 if (text[len] != '}')
1474 /* No closing } on header */
1475 free_alias_list(alias_list);
1476 file->parse_error = cur_line;
1477 file->parse_error_text = "Headers starting with '{' must have a "
1478 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1479 "must close with two brackets ('}}').";
1480 return JB_ERR_PARSE;
1485 /* An invalid {{ header. */
1486 free_alias_list(alias_list);
1487 file->parse_error = cur_line;
1488 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1489 "Please remember that the system (two-bracket) headers must "
1490 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1491 "and must appear before any actions (one-bracket) headers. "
1492 "Also note that system headers may not be repeated.";
1493 return JB_ERR_PARSE;
1496 while ( (*text == ' ') || (*text == '\t') )
1502 && ( (text[len - 1] == ' ')
1503 || (text[len - 1] == '\t') ) )
1508 cur_line->type = FILE_LINE_ACTION;
1510 /* Remove {} and make copy */
1511 if (NULL == (value = (char *) malloc(len + 1)))
1514 free_alias_list(alias_list);
1515 return JB_ERR_MEMORY;
1517 strncpy(value, text, len);
1521 err = get_actions(value, alias_list, cur_line->data.action);
1524 /* Invalid action or out of memory */
1526 free_alias_list(alias_list);
1527 if (err == JB_ERR_MEMORY)
1533 /* Line does not contain a name=value pair */
1534 file->parse_error = cur_line;
1535 file->parse_error_text = "This header does not specify a valid set of actions.";
1536 return JB_ERR_PARSE;
1540 /* Done with string - it was clobbered anyway */
1543 /* Process next line */
1544 cur_line = cur_line->next;
1546 /* Loop processing URL patterns */
1547 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1549 if (cur_line->unprocessed[0])
1551 /* Could parse URL here, but this isn't currently needed */
1553 cur_line->type = FILE_LINE_URL;
1557 cur_line->type = FILE_LINE_BLANK;
1559 cur_line = cur_line->next;
1561 } /* End main while(cur_line != NULL) loop */
1563 free_alias_list(alias_list);
1569 /*********************************************************************
1571 * Function : edit_read_file_lines
1573 * Description : Read all the lines of a file into memory.
1574 * Handles whitespace, comments and line continuation.
1577 * 1 : fp = File to read from. On return, this will be
1578 * at EOF but it will not have been closed.
1579 * 2 : pfile = Destination for a linked list of file_lines.
1580 * Will be set to NULL on error.
1581 * 3 : newline = How to handle newlines.
1583 * Returns : JB_ERR_OK on success
1584 * JB_ERR_MEMORY on out-of-memory
1586 *********************************************************************/
1587 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1589 struct file_line * first_line; /* Keep for return value or to free */
1590 struct file_line * cur_line; /* Current line */
1591 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1599 cur_line = first_line = zalloc(sizeof(struct file_line));
1600 if (cur_line == NULL)
1602 return JB_ERR_MEMORY;
1605 cur_line->type = FILE_LINE_UNPROCESSED;
1607 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1610 /* Out of memory or empty file. */
1611 /* Note that empty file is not an error we propogate up */
1613 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1618 prev_line = cur_line;
1619 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1620 if (cur_line == NULL)
1623 edit_free_file_lines(first_line);
1624 return JB_ERR_MEMORY;
1627 cur_line->type = FILE_LINE_UNPROCESSED;
1629 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1630 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1633 edit_free_file_lines(first_line);
1634 return JB_ERR_MEMORY;
1638 while (rval != JB_ERR_FILE);
1642 /* We allocated one too many - free it */
1643 prev_line->next = NULL;
1646 *pfile = first_line;
1651 /*********************************************************************
1653 * Function : edit_read_file
1655 * Description : Read a complete file into memory.
1656 * Handles CGI parameter parsing. If requested, also
1657 * checks the file's modification timestamp.
1660 * 1 : csp = Current client state (buffers, headers, etc...)
1661 * 2 : parameters = map of cgi parameters.
1662 * 3 : require_version = true to check "ver" parameter.
1663 * 4 : suffix = File extension, e.g. ".action".
1664 * 5 : pfile = Destination for the file. Will be set
1668 * filename : The name of the file to read, without the
1669 * path or ".action" extension.
1670 * ver : (Only if require_version is nonzero)
1671 * Timestamp of the actions file. If wrong, this
1672 * function fails with JB_ERR_MODIFIED.
1674 * Returns : JB_ERR_OK on success
1675 * JB_ERR_MEMORY on out-of-memory
1676 * JB_ERR_CGI_PARAMS if "filename" was not specified
1678 * JB_ERR_FILE if the file cannot be opened or
1680 * JB_ERR_MODIFIED if version checking was requested and
1681 * failed - the file was modified outside
1682 * of this CGI editor instance.
1684 *********************************************************************/
1685 jb_err edit_read_file(struct client_state *csp,
1686 const struct map *parameters,
1687 int require_version,
1689 struct editable_file **pfile)
1691 struct file_line * lines;
1695 const char * identifier;
1696 struct editable_file * file;
1697 unsigned version = 0;
1698 struct stat statbuf[1];
1699 char version_buf[22];
1700 int newline = NEWLINE_UNKNOWN;
1708 err = get_file_name_param(csp, parameters, "f", suffix,
1709 &filename, &identifier);
1715 if (stat(filename, statbuf) < 0)
1717 /* Error, probably file not found. */
1721 version = (unsigned) statbuf->st_mtime;
1723 if (require_version)
1725 unsigned specified_version;
1726 err = get_number_param(csp, parameters, "v", &specified_version);
1733 if (version != specified_version)
1735 return JB_ERR_MODIFIED;
1739 if (NULL == (fp = fopen(filename,"rb")))
1745 err = edit_read_file_lines(fp, &lines, &newline);
1755 file = (struct editable_file *) zalloc(sizeof(*file));
1759 edit_free_file_lines(lines);
1763 file->lines = lines;
1764 file->newline = newline;
1765 file->filename = filename;
1766 file->version = version;
1767 file->identifier = url_encode(identifier);
1769 if (file->identifier == NULL)
1771 edit_free_file(file);
1772 return JB_ERR_MEMORY;
1775 /* Correct file->version_str */
1776 freez(file->version_str);
1777 snprintf(version_buf, 22, "%u", file->version);
1778 version_buf[21] = '\0';
1779 file->version_str = strdup(version_buf);
1780 if (version_buf == NULL)
1782 edit_free_file(file);
1783 return JB_ERR_MEMORY;
1791 /*********************************************************************
1793 * Function : edit_read_actions_file
1795 * Description : Read a complete actions file into memory.
1796 * Handles CGI parameter parsing. If requested, also
1797 * checks the file's modification timestamp.
1799 * If this function detects an error in the categories
1800 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1801 * then it handles it by filling in the specified
1802 * response structure and returning JB_ERR_FILE.
1805 * 1 : csp = Current client state (buffers, headers, etc...)
1806 * 2 : rsp = HTTP response. Only filled in on error.
1807 * 2 : parameters = map of cgi parameters.
1808 * 3 : require_version = true to check "ver" parameter.
1809 * 4 : pfile = Destination for the file. Will be set
1813 * filename : The name of the actions file to read, without the
1814 * path or ".action" extension.
1815 * ver : (Only if require_version is nonzero)
1816 * Timestamp of the actions file. If wrong, this
1817 * function fails with JB_ERR_MODIFIED.
1819 * Returns : JB_ERR_OK on success
1820 * JB_ERR_MEMORY on out-of-memory
1821 * JB_ERR_CGI_PARAMS if "filename" was not specified
1823 * JB_ERR_FILE if the file does not contain valid data,
1824 * or if file cannot be opened or
1825 * contains no data, or if version
1826 * checking was requested and failed.
1828 *********************************************************************/
1829 jb_err edit_read_actions_file(struct client_state *csp,
1830 struct http_response *rsp,
1831 const struct map *parameters,
1832 int require_version,
1833 struct editable_file **pfile)
1836 struct editable_file *file;
1844 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1847 /* Try to handle if possible */
1848 if (err == JB_ERR_FILE)
1850 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1852 else if (err == JB_ERR_MODIFIED)
1854 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1856 if (err == JB_ERR_OK)
1859 * Signal to higher-level CGI code that there was a problem but we
1860 * handled it, they should just return JB_ERR_OK.
1867 err = edit_parse_actions_file(file);
1870 if (err == JB_ERR_PARSE)
1872 err = cgi_error_parse(csp, rsp, file);
1873 if (err == JB_ERR_OK)
1876 * Signal to higher-level CGI code that there was a problem but we
1877 * handled it, they should just return JB_ERR_OK.
1882 edit_free_file(file);
1891 /*********************************************************************
1893 * Function : get_file_name_param
1895 * Description : Get the name of the file to edit from the parameters
1896 * passed to a CGI function. This function handles
1897 * security checks such as blocking urls containing
1898 * "/" or ".", prepending the config file directory,
1899 * and adding the specified suffix.
1901 * (This is an essential security check, otherwise
1902 * users may be able to pass "../../../etc/passwd"
1903 * and overwrite the password file [linux], "prn:"
1904 * and print random data [Windows], etc...)
1906 * This function only allows filenames contining the
1907 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1908 * That's probably too restrictive but at least it's
1912 * 1 : csp = Current client state (buffers, headers, etc...)
1913 * 2 : parameters = map of cgi parameters
1914 * 3 : param_name = The name of the parameter to read
1915 * 4 : suffix = File extension, e.g. ".actions"
1916 * 5 : pfilename = destination for full filename. Caller
1917 * free()s. Set to NULL on error.
1918 * 6 : pparam = destination for partial filename,
1919 * suitable for use in another URL. Allocated as part
1920 * of the map "parameters", so don't free it.
1921 * Set to NULL if not specified.
1923 * Returns : JB_ERR_OK on success
1924 * JB_ERR_MEMORY on out-of-memory
1925 * JB_ERR_CGI_PARAMS if "filename" was not specified
1928 *********************************************************************/
1929 static jb_err get_file_name_param(struct client_state *csp,
1930 const struct map *parameters,
1931 const char *param_name,
1934 const char **pparam)
1952 param = lookup(parameters, param_name);
1955 return JB_ERR_CGI_PARAMS;
1960 len = strlen(param);
1961 if (len >= FILENAME_MAX)
1964 return JB_ERR_CGI_PARAMS;
1967 /* Check every character to see if it's legal */
1969 while ((ch = *s++) != '\0')
1971 if ( ((ch < 'A') || (ch > 'Z'))
1972 && ((ch < 'a') || (ch > 'z'))
1973 && ((ch < '0') || (ch > '9'))
1977 /* Probable hack attempt. */
1978 return JB_ERR_CGI_PARAMS;
1982 /* Append extension */
1983 name = malloc(len + strlen(suffix) + 1);
1986 return JB_ERR_MEMORY;
1988 strcpy(name, param);
1989 strcpy(name + len, suffix);
1992 fullpath = make_path(csp->config->confdir, name);
1995 if (fullpath == NULL)
1997 return JB_ERR_MEMORY;
2001 *pfilename = fullpath;
2007 /*********************************************************************
2009 * Function : get_url_spec_param
2011 * Description : Get a URL pattern from the parameters
2012 * passed to a CGI function. Removes leading/trailing
2013 * spaces and validates it.
2016 * 1 : csp = Current client state (buffers, headers, etc...)
2017 * 2 : parameters = map of cgi parameters
2018 * 3 : name = Name of CGI parameter to read
2019 * 4 : pvalue = destination for value. Will be malloc()'d.
2020 * Set to NULL on error.
2022 * Returns : JB_ERR_OK on success
2023 * JB_ERR_MEMORY on out-of-memory
2024 * JB_ERR_CGI_PARAMS if the parameter was not specified
2027 *********************************************************************/
2028 static jb_err get_url_spec_param(struct client_state *csp,
2029 const struct map *parameters,
2033 const char *orig_param;
2036 struct url_spec compiled[1];
2046 orig_param = lookup(parameters, name);
2049 return JB_ERR_CGI_PARAMS;
2052 /* Copy and trim whitespace */
2053 param = strdup(orig_param);
2056 return JB_ERR_MEMORY;
2060 /* Must be non-empty, and can't allow 1st character to be '{' */
2061 if (param[0] == '\0' || param[0] == '{')
2064 return JB_ERR_CGI_PARAMS;
2067 /* Check for embedded newlines */
2068 for (s = param; *s != '\0'; s++)
2070 if ((*s == '\r') || (*s == '\n'))
2073 return JB_ERR_CGI_PARAMS;
2077 /* Check that regex is valid */
2082 return JB_ERR_MEMORY;
2084 err = create_url_spec(compiled, s);
2089 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2091 free_url_spec(compiled);
2093 if (param[strlen(param) - 1] == '\\')
2096 * Must protect trailing '\\' from becoming line continuation character.
2097 * Two methods: 1) If it's a domain only, add a trailing '/'.
2098 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2100 if (strchr(param, '/') == NULL)
2102 err = string_append(¶m, "/");
2106 err = string_append(¶m, "(?:)");
2113 /* Check that the modified regex is valid */
2118 return JB_ERR_MEMORY;
2120 err = create_url_spec(compiled, s);
2125 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2127 free_url_spec(compiled);
2134 /*********************************************************************
2136 * Function : map_radio
2138 * Description : Map a set of radio button values. E.g. if you have
2139 * 3 radio buttons, declare them as:
2140 * <option type="radio" name="xyz" @xyz-a@>
2141 * <option type="radio" name="xyz" @xyz-b@>
2142 * <option type="radio" name="xyz" @xyz-c@>
2143 * Then map one of the @xyz-?@ variables to "checked"
2144 * and all the others to empty by calling:
2145 * map_radio(exports, "xyz", "abc", sel)
2146 * Where 'sel' is 'a', 'b', or 'c'.
2149 * 1 : exports = Exports map to modify.
2150 * 2 : optionname = name for map
2151 * 3 : values = null-terminated list of values;
2152 * 4 : value = Selected value.
2154 * CGI Parameters : None
2156 * Returns : JB_ERR_OK on success
2157 * JB_ERR_MEMORY on out-of-memory
2159 *********************************************************************/
2160 static jb_err map_radio(struct map * exports,
2161 const char * optionname,
2162 const char * values,
2174 len = strlen(optionname);
2175 buf = malloc(len + 3);
2178 return JB_ERR_MEMORY;
2181 strcpy(buf, optionname);
2186 while ((c = *values++) != '\0')
2191 if (map(exports, buf, 1, "", 1))
2193 return JB_ERR_MEMORY;
2199 return map(exports, buf, 0, "checked", 1);
2203 /*********************************************************************
2205 * Function : cgi_error_modified
2207 * Description : CGI function that is called when a file is modified
2208 * outside the CGI editor.
2211 * 1 : csp = Current client state (buffers, headers, etc...)
2212 * 2 : rsp = http_response data structure for output
2213 * 3 : filename = The file that was modified.
2215 * CGI Parameters : none
2217 * Returns : JB_ERR_OK on success
2218 * JB_ERR_MEMORY on out-of-memory error.
2220 *********************************************************************/
2221 jb_err cgi_error_modified(struct client_state *csp,
2222 struct http_response *rsp,
2223 const char *filename)
2225 struct map *exports;
2232 if (NULL == (exports = default_exports(csp, NULL)))
2234 return JB_ERR_MEMORY;
2237 err = map(exports, "f", 1, html_encode(filename), 0);
2244 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2248 /*********************************************************************
2250 * Function : cgi_error_parse
2252 * Description : CGI function that is called when a file cannot
2253 * be parsed by the CGI editor.
2256 * 1 : csp = Current client state (buffers, headers, etc...)
2257 * 2 : rsp = http_response data structure for output
2258 * 3 : file = The file that was modified.
2260 * CGI Parameters : none
2262 * Returns : JB_ERR_OK on success
2263 * JB_ERR_MEMORY on out-of-memory error.
2265 *********************************************************************/
2266 jb_err cgi_error_parse(struct client_state *csp,
2267 struct http_response *rsp,
2268 struct editable_file *file)
2270 struct map *exports;
2272 struct file_line *cur_line;
2278 if (NULL == (exports = default_exports(csp, NULL)))
2280 return JB_ERR_MEMORY;
2283 err = map(exports, "f", 1, file->identifier, 1);
2284 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2286 cur_line = file->parse_error;
2289 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2290 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2298 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2302 /*********************************************************************
2304 * Function : cgi_error_file
2306 * Description : CGI function that is called when a file cannot be
2307 * opened by the CGI editor.
2310 * 1 : csp = Current client state (buffers, headers, etc...)
2311 * 2 : rsp = http_response data structure for output
2312 * 3 : filename = The file that was modified.
2314 * CGI Parameters : none
2316 * Returns : JB_ERR_OK on success
2317 * JB_ERR_MEMORY on out-of-memory error.
2319 *********************************************************************/
2320 jb_err cgi_error_file(struct client_state *csp,
2321 struct http_response *rsp,
2322 const char *filename)
2324 struct map *exports;
2331 if (NULL == (exports = default_exports(csp, NULL)))
2333 return JB_ERR_MEMORY;
2336 err = map(exports, "f", 1, html_encode(filename), 0);
2343 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2347 /*********************************************************************
2349 * Function : cgi_error_file
2351 * Description : CGI function that is called when a file cannot be
2352 * opened for writing by the CGI editor.
2355 * 1 : csp = Current client state (buffers, headers, etc...)
2356 * 2 : rsp = http_response data structure for output
2357 * 3 : filename = The file that we can't write to
2359 * CGI Parameters : none
2361 * Returns : JB_ERR_OK on success
2362 * JB_ERR_MEMORY on out-of-memory error.
2364 *********************************************************************/
2365 jb_err cgi_error_file_read_only(struct client_state *csp,
2366 struct http_response *rsp,
2367 const char *filename)
2369 struct map *exports;
2376 if (NULL == (exports = default_exports(csp, NULL)))
2378 return JB_ERR_MEMORY;
2381 err = map(exports, "f", 1, html_encode(filename), 0);
2388 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2392 /*********************************************************************
2394 * Function : cgi_error_disabled
2396 * Description : CGI function that is called if the actions editor
2397 * is called although it's disabled in config
2400 * 1 : csp = Current client state (buffers, headers, etc...)
2401 * 2 : rsp = http_response data structure for output
2403 * CGI Parameters : none
2405 * Returns : JB_ERR_OK on success
2406 * JB_ERR_MEMORY on out-of-memory error.
2408 *********************************************************************/
2409 jb_err cgi_error_disabled(struct client_state *csp,
2410 struct http_response *rsp)
2412 struct map *exports;
2417 if (NULL == (exports = default_exports(csp, NULL)))
2419 return JB_ERR_MEMORY;
2422 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2426 /*********************************************************************
2428 * Function : cgi_edit_actions
2430 * Description : CGI function that allows the user to choose which
2431 * actions file to edit.
2434 * 1 : csp = Current client state (buffers, headers, etc...)
2435 * 2 : rsp = http_response data structure for output
2436 * 3 : parameters = map of cgi parameters
2438 * CGI Parameters : None
2440 * Returns : JB_ERR_OK on success
2441 * JB_ERR_MEMORY on out-of-memory error
2443 *********************************************************************/
2444 jb_err cgi_edit_actions(struct client_state *csp,
2445 struct http_response *rsp,
2446 const struct map *parameters)
2449 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2451 return cgi_error_disabled(csp, rsp);
2454 /* FIXME: Incomplete */
2455 rsp->status = strdup("302 Local Redirect from Privoxy");
2456 if (rsp->status == NULL)
2458 return JB_ERR_MEMORY;
2460 if (enlist_unique_header(rsp->headers, "Location",
2461 CGI_PREFIX "edit-actions-list?f=default"))
2465 return JB_ERR_MEMORY;
2472 /*********************************************************************
2474 * Function : cgi_edit_actions_list
2476 * Description : CGI function that edits the actions list.
2477 * FIXME: This function shouldn't FATAL ever.
2478 * FIXME: This function doesn't check the retval of map()
2480 * 1 : csp = Current client state (buffers, headers, etc...)
2481 * 2 : rsp = http_response data structure for output
2482 * 3 : parameters = map of cgi parameters
2484 * CGI Parameters : filename
2486 * Returns : JB_ERR_OK on success
2487 * JB_ERR_MEMORY on out-of-memory
2488 * JB_ERR_FILE if the file cannot be opened or
2490 * JB_ERR_CGI_PARAMS if "filename" was not specified
2493 *********************************************************************/
2494 jb_err cgi_edit_actions_list(struct client_state *csp,
2495 struct http_response *rsp,
2496 const struct map *parameters)
2498 char * section_template;
2499 char * url_template;
2504 struct map * exports;
2505 struct map * section_exports;
2506 struct map * url_exports;
2507 struct editable_file * file;
2508 struct file_line * cur_line;
2509 unsigned line_number = 0;
2510 unsigned prev_section_line_number = ((unsigned) (-1));
2512 struct file_list * fl;
2513 struct url_actions * b;
2514 char * buttons = NULL;
2517 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2519 return cgi_error_disabled(csp, rsp);
2522 if (NULL == (exports = default_exports(csp, NULL)))
2524 return JB_ERR_MEMORY;
2527 /* Load actions file */
2528 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2531 /* No filename specified, can't read file, or out of memory. */
2532 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2535 /* Find start of actions in file */
2536 cur_line = file->lines;
2538 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2540 cur_line = cur_line->next;
2545 * Conventional actions files should have a match all block
2547 * cur_line = {...global actions...}
2548 * cur_line->next = /
2549 * cur_line->next->next = {...actions...} or EOF
2551 if ( (cur_line != NULL)
2552 && (cur_line->type == FILE_LINE_ACTION)
2553 && (cur_line->next != NULL)
2554 && (cur_line->next->type == FILE_LINE_URL)
2555 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2556 && ( (cur_line->next->next == NULL)
2557 || (cur_line->next->next->type != FILE_LINE_URL)
2561 * Generate string with buttons to set actions for "/" to
2562 * any predefined set of actions (named standard.*, probably
2563 * residing in standard.action).
2566 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2569 edit_free_file(file);
2571 if (err == JB_ERR_FILE)
2573 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2578 err = template_fill(§ion_template, exports);
2581 edit_free_file(file);
2586 buttons = strdup("");
2587 for (i = 0; i < MAX_ACTION_FILES; i++)
2589 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2591 for (b = b->next; NULL != b; b = b->next)
2593 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2595 if (err || (NULL == (section_exports = new_map())))
2598 free(section_template);
2599 edit_free_file(file);
2601 return JB_ERR_MEMORY;
2604 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2606 if (err || (NULL == (s = strdup(section_template))))
2608 free_map(section_exports);
2610 free(section_template);
2611 edit_free_file(file);
2613 return JB_ERR_MEMORY;
2616 if (!err) err = template_fill(&s, section_exports);
2617 free_map(section_exports);
2618 if (!err) err = string_join(&buttons, s);
2623 freez(section_template);
2624 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2627 * Conventional actions file, supply extra editing help.
2628 * (e.g. don't allow them to make it an unconventional one).
2630 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2632 snprintf(buf, 150, "%d", line_number);
2633 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2634 snprintf(buf, 150, "%d", line_number + 2);
2635 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2636 if (!err) err = map(exports, "all-urls-actions", 1,
2637 actions_to_html(csp, cur_line->data.action), 0);
2639 /* Skip the 2 lines */
2640 cur_line = cur_line->next->next;
2644 * Note that prev_section_line_number is NOT set here.
2645 * This is deliberate and not a bug. It stops a "Move up"
2646 * option appearing on the next section. Clicking "Move
2647 * up" would make the actions file unconventional, which
2648 * we don't want, so we hide this option.
2654 * Non-standard actions file - does not begin with
2655 * the "All URLs" section.
2657 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2660 /* Set up global exports */
2662 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2663 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2665 /* Discourage private additions to default.action */
2667 if (!err) err = map_conditional(exports, "default-action",
2668 (strcmp("default", lookup(parameters, "f")) == 0));
2671 edit_free_file(file);
2676 /* Should do all global exports above this point */
2678 /* Load templates */
2680 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2683 edit_free_file(file);
2685 if (err == JB_ERR_FILE)
2687 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2692 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2695 free(section_template);
2696 edit_free_file(file);
2698 if (err == JB_ERR_FILE)
2700 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2705 err = template_fill(§ion_template, exports);
2709 edit_free_file(file);
2715 err = template_fill(&url_template, exports);
2718 free(section_template);
2719 edit_free_file(file);
2724 if (NULL == (sections = strdup("")))
2726 free(section_template);
2728 edit_free_file(file);
2730 return JB_ERR_MEMORY;
2733 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2735 if (NULL == (section_exports = new_map()))
2738 free(section_template);
2740 edit_free_file(file);
2742 return JB_ERR_MEMORY;
2745 snprintf(buf, 150, "%d", line_number);
2746 err = map(section_exports, "s", 1, buf, 1);
2747 if (!err) err = map(section_exports, "actions", 1,
2748 actions_to_html(csp, cur_line->data.action), 0);
2751 && (cur_line->next != NULL)
2752 && (cur_line->next->type == FILE_LINE_URL))
2754 /* This section contains at least one URL, don't allow delete */
2755 err = map_block_killer(section_exports, "empty-section");
2759 if (!err) err = map_block_keep(section_exports, "empty-section");
2762 if (prev_section_line_number != ((unsigned)(-1)))
2764 /* Not last section */
2765 snprintf(buf, 150, "%d", prev_section_line_number);
2766 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2767 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2772 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2774 prev_section_line_number = line_number;
2779 free(section_template);
2781 edit_free_file(file);
2783 free_map(section_exports);
2787 /* Should do all section-specific exports above this point */
2789 if (NULL == (urls = strdup("")))
2792 free(section_template);
2794 edit_free_file(file);
2796 free_map(section_exports);
2797 return JB_ERR_MEMORY;
2802 cur_line = cur_line->next;
2805 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2807 if (NULL == (url_exports = new_map()))
2811 free(section_template);
2813 edit_free_file(file);
2815 free_map(section_exports);
2816 return JB_ERR_MEMORY;
2819 snprintf(buf, 150, "%d", line_number);
2820 err = map(url_exports, "p", 1, buf, 1);
2822 snprintf(buf, 150, "%d", url_1_2);
2823 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2825 if (!err) err = map(url_exports, "url-html", 1,
2826 html_encode(cur_line->unprocessed), 0);
2827 if (!err) err = map(url_exports, "url", 1,
2828 url_encode(cur_line->unprocessed), 0);
2834 free(section_template);
2836 edit_free_file(file);
2838 free_map(section_exports);
2839 free_map(url_exports);
2843 if (NULL == (s = strdup(url_template)))
2847 free(section_template);
2849 edit_free_file(file);
2851 free_map(section_exports);
2852 free_map(url_exports);
2853 return JB_ERR_MEMORY;
2856 err = template_fill(&s, section_exports);
2857 if (!err) err = template_fill(&s, url_exports);
2858 if (!err) err = string_append(&urls, s);
2860 free_map(url_exports);
2867 free(section_template);
2869 edit_free_file(file);
2871 free_map(section_exports);
2875 url_1_2 = 3 - url_1_2;
2877 cur_line = cur_line->next;
2881 err = map(section_exports, "urls", 1, urls, 0);
2883 /* Could also do section-specific exports here, but it wouldn't be as fast */
2885 snprintf(buf, 150, "%d", line_number);
2886 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2888 if ( (cur_line != NULL)
2889 && (cur_line->type == FILE_LINE_ACTION))
2891 /* Not last section */
2892 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2897 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2903 free(section_template);
2905 edit_free_file(file);
2907 free_map(section_exports);
2911 if (NULL == (s = strdup(section_template)))
2914 free(section_template);
2916 edit_free_file(file);
2918 free_map(section_exports);
2919 return JB_ERR_MEMORY;
2922 err = template_fill(&s, section_exports);
2923 if (!err) err = string_append(§ions, s);
2926 free_map(section_exports);
2931 free(section_template);
2933 edit_free_file(file);
2939 edit_free_file(file);
2940 free(section_template);
2943 err = map(exports, "sections", 1, sections, 0);
2950 /* Could also do global exports here, but it wouldn't be as fast */
2952 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2956 /*********************************************************************
2958 * Function : cgi_edit_actions_for_url
2960 * Description : CGI function that edits the Actions list.
2963 * 1 : csp = Current client state (buffers, headers, etc...)
2964 * 2 : rsp = http_response data structure for output
2965 * 3 : parameters = map of cgi parameters
2967 * CGI Parameters : None
2969 * Returns : JB_ERR_OK on success
2970 * JB_ERR_MEMORY on out-of-memory
2971 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2972 * specified or not valid.
2974 *********************************************************************/
2975 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2976 struct http_response *rsp,
2977 const struct map *parameters)
2979 struct map * exports;
2981 struct editable_file * file;
2982 struct file_line * cur_line;
2983 unsigned line_number;
2985 struct file_list *filter_file;
2986 struct re_filterfile_spec *filter_group;
2988 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2990 return cgi_error_disabled(csp, rsp);
2993 err = get_number_param(csp, parameters, "s", §ionid);
2999 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3002 /* No filename specified, can't read file, modified, or out of memory. */
3003 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3006 cur_line = file->lines;
3008 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3010 cur_line = cur_line->next;
3013 if ( (cur_line == NULL)
3014 || (line_number != sectionid)
3016 || (cur_line->type != FILE_LINE_ACTION))
3018 /* Invalid "sectionid" parameter */
3019 edit_free_file(file);
3020 return JB_ERR_CGI_PARAMS;
3023 if (NULL == (exports = default_exports(csp, NULL)))
3025 edit_free_file(file);
3026 return JB_ERR_MEMORY;
3029 err = map(exports, "f", 1, file->identifier, 1);
3030 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3031 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3033 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3035 filter_file = csp->rlist;
3036 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
3038 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
3042 edit_free_file(file);
3047 if (filter_group == NULL)
3049 err = map(exports, "filter-params", 1, "", 1);
3053 /* We have some entries in the filter list */
3056 char * filter_template;
3058 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3061 edit_free_file(file);
3063 if (err == JB_ERR_FILE)
3065 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3070 err = template_fill(&filter_template, exports);
3072 result = strdup("");
3074 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3076 char current_mode = 'x';
3077 struct list_entry *filter_name;
3079 struct map *line_exports;
3082 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
3083 while ((filter_name != NULL)
3084 && (0 != strcmp(filter_group->name, filter_name->str)))
3086 filter_name = filter_name->next;
3089 if (filter_name != NULL)
3095 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
3096 while ((filter_name != NULL)
3097 && (0 != strcmp(filter_group->name, filter_name->str)))
3099 filter_name = filter_name->next;
3101 if (filter_name != NULL)
3107 /* Generate a unique serial number */
3108 snprintf(number, sizeof(number), "%x", index++);
3109 number[sizeof(number) - 1] = '\0';
3111 line_exports = new_map();
3112 if (line_exports == NULL)
3114 err = JB_ERR_MEMORY;
3119 if (!err) err = map(line_exports, "index", 1, number, 1);
3120 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3121 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3122 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3127 this_line = strdup(filter_template);
3128 if (this_line == NULL) err = JB_ERR_MEMORY;
3130 if (!err) err = template_fill(&this_line, line_exports);
3131 string_join(&result, this_line);
3133 free_map(line_exports);
3137 freez(filter_template);
3141 err = map(exports, "filter-params", 1, result, 0);
3149 if (!err) err = map_radio(exports, "filter-all", "nx",
3150 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3152 edit_free_file(file);
3160 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3164 /*********************************************************************
3166 * Function : cgi_edit_actions_submit
3168 * Description : CGI function that actually edits the Actions list.
3171 * 1 : csp = Current client state (buffers, headers, etc...)
3172 * 2 : rsp = http_response data structure for output
3173 * 3 : parameters = map of cgi parameters
3175 * CGI Parameters : None
3177 * Returns : JB_ERR_OK on success
3178 * JB_ERR_MEMORY on out-of-memory
3179 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3180 * specified or not valid.
3182 *********************************************************************/
3183 jb_err cgi_edit_actions_submit(struct client_state *csp,
3184 struct http_response *rsp,
3185 const struct map *parameters)
3191 struct editable_file * file;
3192 struct file_line * cur_line;
3193 unsigned line_number;
3197 const char * action_set_name;
3199 struct file_list * fl;
3200 struct url_actions * b;
3202 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3204 return cgi_error_disabled(csp, rsp);
3207 err = get_number_param(csp, parameters, "s", §ionid);
3213 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3216 /* No filename specified, can't read file, modified, or out of memory. */
3217 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3220 cur_line = file->lines;
3222 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3224 cur_line = cur_line->next;
3227 if ( (cur_line == NULL)
3228 || (line_number != sectionid)
3230 || (cur_line->type != FILE_LINE_ACTION))
3232 /* Invalid "sectionid" parameter */
3233 edit_free_file(file);
3234 return JB_ERR_CGI_PARAMS;
3237 get_string_param(parameters, "p", &action_set_name);
3238 if (action_set_name != NULL)
3240 for (index = 0; index < MAX_ACTION_FILES; index++)
3242 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3244 for (b = b->next; NULL != b; b = b->next)
3246 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3248 copy_action(cur_line->data.action, b->action);
3254 edit_free_file(file);
3255 return JB_ERR_CGI_PARAMS;
3261 err = actions_from_radio(parameters, cur_line->data.action);
3267 edit_free_file(file);
3271 ch = get_char_param(parameters, "filter_all");
3274 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3275 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3276 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3280 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3283 for (index = 0; !err; index++)
3290 /* Generate the keys */
3291 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3292 key_value[sizeof(key_value) - 1] = '\0';
3293 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3294 key_name[sizeof(key_name) - 1] = '\0';
3296 err = get_string_param(parameters, key_name, &name);
3305 value = get_char_param(parameters, key_value);
3308 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3309 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3310 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3312 else if (value == 'N')
3314 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3315 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3317 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3318 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3321 else if (value == 'X')
3323 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3324 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3331 edit_free_file(file);
3335 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3338 edit_free_file(file);
3339 return JB_ERR_MEMORY;
3342 len = strlen(actiontext);
3346 * Empty action - must special-case this.
3347 * Simply setting len to 1 is sufficient...
3352 if (NULL == (newtext = malloc(len + 2)))
3356 edit_free_file(file);
3357 return JB_ERR_MEMORY;
3359 strcpy(newtext, actiontext);
3363 newtext[len + 1] = '\0';
3365 freez(cur_line->raw);
3366 freez(cur_line->unprocessed);
3367 cur_line->unprocessed = newtext;
3369 err = edit_write_file(file);
3372 /* Error writing file */
3373 if (err == JB_ERR_FILE)
3375 /* Read-only file. */
3376 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3378 edit_free_file(file);
3382 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3383 string_append(&target, file->identifier);
3384 string_join(&target, section_target(sectionid));
3387 edit_free_file(file);
3392 return JB_ERR_MEMORY;
3395 rsp->status = strdup("302 Local Redirect from Privoxy");
3396 if (rsp->status == NULL)
3399 return JB_ERR_MEMORY;
3401 err = enlist_unique_header(rsp->headers, "Location", target);
3408 /*********************************************************************
3410 * Function : cgi_edit_actions_url
3412 * Description : CGI function that actually edits a URL pattern in
3416 * 1 : csp = Current client state (buffers, headers, etc...)
3417 * 2 : rsp = http_response data structure for output
3418 * 3 : parameters = map of cgi parameters
3421 * filename : Identifies the file to edit
3422 * ver : File's last-modified time
3423 * section : Line number of section to edit
3424 * pattern : Line number of pattern to edit
3425 * newval : New value for pattern
3427 * Returns : JB_ERR_OK on success
3428 * JB_ERR_MEMORY on out-of-memory
3429 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3430 * specified or not valid.
3432 *********************************************************************/
3433 jb_err cgi_edit_actions_url(struct client_state *csp,
3434 struct http_response *rsp,
3435 const struct map *parameters)
3439 struct editable_file * file;
3440 struct file_line * cur_line;
3441 unsigned line_number;
3442 unsigned section_start_line_number = 0;
3446 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3448 return cgi_error_disabled(csp, rsp);
3451 err = get_number_param(csp, parameters, "p", &patternid);
3458 return JB_ERR_CGI_PARAMS;
3461 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3467 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3470 /* No filename specified, can't read file, modified, or out of memory. */
3472 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3476 cur_line = file->lines;
3478 while ((cur_line != NULL) && (line_number < patternid))
3480 if (cur_line->type == FILE_LINE_ACTION)
3482 section_start_line_number = line_number;
3484 cur_line = cur_line->next;
3488 if ( (cur_line == NULL)
3489 || (cur_line->type != FILE_LINE_URL))
3491 /* Invalid "patternid" parameter */
3493 edit_free_file(file);
3494 return JB_ERR_CGI_PARAMS;
3497 /* At this point, the line to edit is in cur_line */
3499 freez(cur_line->raw);
3500 freez(cur_line->unprocessed);
3501 cur_line->unprocessed = new_pattern;
3503 err = edit_write_file(file);
3506 /* Error writing file */
3507 if (err == JB_ERR_FILE)
3509 /* Read-only file. */
3510 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3512 edit_free_file(file);
3516 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3517 string_append(&target, file->identifier);
3518 string_join(&target, section_target(section_start_line_number));
3520 edit_free_file(file);
3525 return JB_ERR_MEMORY;
3528 rsp->status = strdup("302 Local Redirect from Privoxy");
3529 if (rsp->status == NULL)
3532 return JB_ERR_MEMORY;
3534 err = enlist_unique_header(rsp->headers, "Location", target);
3541 /*********************************************************************
3543 * Function : cgi_edit_actions_add_url
3545 * Description : CGI function that actually adds a URL pattern to
3549 * 1 : csp = Current client state (buffers, headers, etc...)
3550 * 2 : rsp = http_response data structure for output
3551 * 3 : parameters = map of cgi parameters
3554 * filename : Identifies the file to edit
3555 * ver : File's last-modified time
3556 * section : Line number of section to edit
3557 * newval : New pattern
3559 * Returns : JB_ERR_OK on success
3560 * JB_ERR_MEMORY on out-of-memory
3561 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3562 * specified or not valid.
3564 *********************************************************************/
3565 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3566 struct http_response *rsp,
3567 const struct map *parameters)
3571 struct file_line * new_line;
3572 struct editable_file * file;
3573 struct file_line * cur_line;
3574 unsigned line_number;
3578 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3580 return cgi_error_disabled(csp, rsp);
3583 err = get_number_param(csp, parameters, "s", §ionid);
3590 return JB_ERR_CGI_PARAMS;
3593 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3599 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3602 /* No filename specified, can't read file, modified, or out of memory. */
3604 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3608 cur_line = file->lines;
3610 while ((cur_line != NULL) && (line_number < sectionid))
3612 cur_line = cur_line->next;
3616 if ( (cur_line == NULL)
3617 || (cur_line->type != FILE_LINE_ACTION))
3619 /* Invalid "sectionid" parameter */
3621 edit_free_file(file);
3622 return JB_ERR_CGI_PARAMS;
3625 /* At this point, the section header is in cur_line - add after this. */
3627 /* Allocate the new line */
3628 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3629 if (new_line == NULL)
3632 edit_free_file(file);
3633 return JB_ERR_MEMORY;
3636 /* Fill in the data members of the new line */
3637 new_line->raw = NULL;
3638 new_line->prefix = NULL;
3639 new_line->unprocessed = new_pattern;
3640 new_line->type = FILE_LINE_URL;
3642 /* Link new_line into the list, after cur_line */
3643 new_line->next = cur_line->next;
3644 cur_line->next = new_line;
3646 /* Done making changes, now commit */
3648 err = edit_write_file(file);
3651 /* Error writing file */
3652 if (err == JB_ERR_FILE)
3654 /* Read-only file. */
3655 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3657 edit_free_file(file);
3661 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3662 string_append(&target, file->identifier);
3663 string_join(&target, section_target(sectionid));
3665 edit_free_file(file);
3670 return JB_ERR_MEMORY;
3673 rsp->status = strdup("302 Local Redirect from Privoxy");
3674 if (rsp->status == NULL)
3677 return JB_ERR_MEMORY;
3679 err = enlist_unique_header(rsp->headers, "Location", target);
3686 /*********************************************************************
3688 * Function : cgi_edit_actions_remove_url
3690 * Description : CGI function that actually removes a URL pattern from
3694 * 1 : csp = Current client state (buffers, headers, etc...)
3695 * 2 : rsp = http_response data structure for output
3696 * 3 : parameters = map of cgi parameters
3699 * f : (filename) Identifies the file to edit
3700 * v : (version) File's last-modified time
3701 * p : (pattern) Line number of pattern to remove
3703 * Returns : JB_ERR_OK on success
3704 * JB_ERR_MEMORY on out-of-memory
3705 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3706 * specified or not valid.
3708 *********************************************************************/
3709 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3710 struct http_response *rsp,
3711 const struct map *parameters)
3714 struct editable_file * file;
3715 struct file_line * cur_line;
3716 struct file_line * prev_line;
3717 unsigned line_number;
3718 unsigned section_start_line_number = 0;
3722 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3724 return cgi_error_disabled(csp, rsp);
3727 err = get_number_param(csp, parameters, "p", &patternid);
3733 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3736 /* No filename specified, can't read file, modified, or out of memory. */
3737 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3742 cur_line = file->lines;
3744 while ((cur_line != NULL) && (line_number < patternid))
3746 if (cur_line->type == FILE_LINE_ACTION)
3748 section_start_line_number = line_number;
3750 prev_line = cur_line;
3751 cur_line = cur_line->next;
3755 if ( (cur_line == NULL)
3756 || (prev_line == NULL)
3757 || (cur_line->type != FILE_LINE_URL))
3759 /* Invalid "patternid" parameter */
3760 edit_free_file(file);
3761 return JB_ERR_CGI_PARAMS;
3764 /* At this point, the line to remove is in cur_line, and the previous
3765 * one is in prev_line
3768 /* Unlink cur_line */
3769 prev_line->next = cur_line->next;
3770 cur_line->next = NULL;
3773 edit_free_file_lines(cur_line);
3775 err = edit_write_file(file);
3778 /* Error writing file */
3779 if (err == JB_ERR_FILE)
3781 /* Read-only file. */
3782 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3784 edit_free_file(file);
3788 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3789 string_append(&target, file->identifier);
3790 string_join(&target, section_target(section_start_line_number));
3792 edit_free_file(file);
3797 return JB_ERR_MEMORY;
3800 rsp->status = strdup("302 Local Redirect from Privoxy");
3801 if (rsp->status == NULL)
3804 return JB_ERR_MEMORY;
3806 err = enlist_unique_header(rsp->headers, "Location", target);
3813 /*********************************************************************
3815 * Function : cgi_edit_actions_section_remove
3817 * Description : CGI function that actually removes a whole section from
3818 * the actions file. The section must be empty first
3819 * (else JB_ERR_CGI_PARAMS).
3822 * 1 : csp = Current client state (buffers, headers, etc...)
3823 * 2 : rsp = http_response data structure for output
3824 * 3 : parameters = map of cgi parameters
3827 * f : (filename) Identifies the file to edit
3828 * v : (version) File's last-modified time
3829 * s : (section) Line number of section to edit
3831 * Returns : JB_ERR_OK on success
3832 * JB_ERR_MEMORY on out-of-memory
3833 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3834 * specified or not valid.
3836 *********************************************************************/
3837 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3838 struct http_response *rsp,
3839 const struct map *parameters)
3842 struct editable_file * file;
3843 struct file_line * cur_line;
3844 struct file_line * prev_line;
3845 unsigned line_number;
3849 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3851 return cgi_error_disabled(csp, rsp);
3854 err = get_number_param(csp, parameters, "s", §ionid);
3860 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3863 /* No filename specified, can't read file, modified, or out of memory. */
3864 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3868 cur_line = file->lines;
3871 while ((cur_line != NULL) && (line_number < sectionid))
3873 prev_line = cur_line;
3874 cur_line = cur_line->next;
3878 if ( (cur_line == NULL)
3879 || (cur_line->type != FILE_LINE_ACTION) )
3881 /* Invalid "sectionid" parameter */
3882 edit_free_file(file);
3883 return JB_ERR_CGI_PARAMS;
3886 if ( (cur_line->next != NULL)
3887 && (cur_line->next->type == FILE_LINE_URL) )
3889 /* Section not empty. */
3890 edit_free_file(file);
3891 return JB_ERR_CGI_PARAMS;
3894 /* At this point, the line to remove is in cur_line, and the previous
3895 * one is in prev_line
3898 /* Unlink cur_line */
3899 if (prev_line == NULL)
3901 /* Removing the first line from the file */
3902 file->lines = cur_line->next;
3906 prev_line->next = cur_line->next;
3908 cur_line->next = NULL;
3911 edit_free_file_lines(cur_line);
3913 err = edit_write_file(file);
3916 /* Error writing file */
3917 if (err == JB_ERR_FILE)
3919 /* Read-only file. */
3920 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3922 edit_free_file(file);
3926 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3927 string_append(&target, file->identifier);
3929 edit_free_file(file);
3934 return JB_ERR_MEMORY;
3937 rsp->status = strdup("302 Local Redirect from Privoxy");
3938 if (rsp->status == NULL)
3941 return JB_ERR_MEMORY;
3943 err = enlist_unique_header(rsp->headers, "Location", target);
3950 /*********************************************************************
3952 * Function : cgi_edit_actions_section_add
3954 * Description : CGI function that adds a new empty section to
3958 * 1 : csp = Current client state (buffers, headers, etc...)
3959 * 2 : rsp = http_response data structure for output
3960 * 3 : parameters = map of cgi parameters
3963 * f : (filename) Identifies the file to edit
3964 * v : (version) File's last-modified time
3965 * s : (section) Line number of section to add after, 0 for
3968 * Returns : JB_ERR_OK on success
3969 * JB_ERR_MEMORY on out-of-memory
3970 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3971 * specified or not valid.
3973 *********************************************************************/
3974 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3975 struct http_response *rsp,
3976 const struct map *parameters)
3979 struct file_line * new_line;
3981 struct editable_file * file;
3982 struct file_line * cur_line;
3983 unsigned line_number;
3987 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3989 return cgi_error_disabled(csp, rsp);
3992 err = get_number_param(csp, parameters, "s", §ionid);
3998 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4001 /* No filename specified, can't read file, modified, or out of memory. */
4002 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4006 cur_line = file->lines;
4008 if (sectionid <= 1U)
4010 /* Add to start of file */
4011 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4013 /* There's something in the file, find the line before the first
4016 while ( (cur_line->next != NULL)
4017 && (cur_line->next->type != FILE_LINE_ACTION) )
4019 cur_line = cur_line->next;
4025 /* File starts with action line, so insert at top */
4031 /* Add after stated section. */
4032 while ((cur_line != NULL) && (line_number < sectionid))
4034 cur_line = cur_line->next;
4038 if ( (cur_line == NULL)
4039 || (cur_line->type != FILE_LINE_ACTION))
4041 /* Invalid "sectionid" parameter */
4042 edit_free_file(file);
4043 return JB_ERR_CGI_PARAMS;
4046 /* Skip through the section to find the last line in it. */
4047 while ( (cur_line->next != NULL)
4048 && (cur_line->next->type != FILE_LINE_ACTION) )
4050 cur_line = cur_line->next;
4055 /* At this point, the last line in the previous section is in cur_line
4056 * - add after this. (Or if we need to add as the first line, cur_line
4060 new_text = strdup("{}");
4061 if (NULL == new_text)
4063 edit_free_file(file);
4064 return JB_ERR_MEMORY;
4067 /* Allocate the new line */
4068 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4069 if (new_line == NULL)
4072 edit_free_file(file);
4073 return JB_ERR_MEMORY;
4076 /* Fill in the data members of the new line */
4077 new_line->raw = NULL;
4078 new_line->prefix = NULL;
4079 new_line->unprocessed = new_text;
4080 new_line->type = FILE_LINE_ACTION;
4082 if (cur_line != NULL)
4084 /* Link new_line into the list, after cur_line */
4085 new_line->next = cur_line->next;
4086 cur_line->next = new_line;
4090 /* Link new_line into the list, as first line */
4091 new_line->next = file->lines;
4092 file->lines = new_line;
4095 /* Done making changes, now commit */
4097 err = edit_write_file(file);
4100 /* Error writing file */
4101 if (err == JB_ERR_FILE)
4103 /* Read-only file. */
4104 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4106 edit_free_file(file);
4110 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4111 string_append(&target, file->identifier);
4113 edit_free_file(file);
4118 return JB_ERR_MEMORY;
4121 rsp->status = strdup("302 Local Redirect from Privoxy");
4122 if (rsp->status == NULL)
4125 return JB_ERR_MEMORY;
4127 err = enlist_unique_header(rsp->headers, "Location", target);
4134 /*********************************************************************
4136 * Function : cgi_edit_actions_section_swap
4138 * Description : CGI function that swaps the order of two sections
4139 * in the actions file. Note that this CGI can actually
4140 * swap any two arbitrary sections, but the GUI interface
4141 * currently only allows consecutive sections to be
4145 * 1 : csp = Current client state (buffers, headers, etc...)
4146 * 2 : rsp = http_response data structure for output
4147 * 3 : parameters = map of cgi parameters
4150 * f : (filename) Identifies the file to edit
4151 * v : (version) File's last-modified time
4152 * s1 : (section1) Line number of first section to swap
4153 * s2 : (section2) Line number of second section to swap
4155 * Returns : JB_ERR_OK on success
4156 * JB_ERR_MEMORY on out-of-memory
4157 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4158 * specified or not valid.
4160 *********************************************************************/
4161 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4162 struct http_response *rsp,
4163 const struct map *parameters)
4167 struct editable_file * file;
4168 struct file_line * cur_line;
4169 struct file_line * prev_line;
4170 struct file_line * line_before_section1;
4171 struct file_line * line_start_section1;
4172 struct file_line * line_end_section1;
4173 struct file_line * line_after_section1;
4174 struct file_line * line_before_section2;
4175 struct file_line * line_start_section2;
4176 struct file_line * line_end_section2;
4177 struct file_line * line_after_section2;
4178 unsigned line_number;
4182 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4184 return cgi_error_disabled(csp, rsp);
4187 err = get_number_param(csp, parameters, "s1", §ion1);
4188 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4194 if (section1 > section2)
4196 unsigned temp = section2;
4197 section2 = section1;
4201 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4204 /* No filename specified, can't read file, modified, or out of memory. */
4205 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4208 /* Start at the beginning... */
4210 cur_line = file->lines;
4213 /* ... find section1 ... */
4214 while ((cur_line != NULL) && (line_number < section1))
4216 prev_line = cur_line;
4217 cur_line = cur_line->next;
4221 if ( (cur_line == NULL)
4222 || (cur_line->type != FILE_LINE_ACTION) )
4224 /* Invalid "section1" parameter */
4225 edit_free_file(file);
4226 return JB_ERR_CGI_PARAMS;
4229 /* If no-op, we've validated params and can skip the rest. */
4230 if (section1 != section2)
4232 /* ... find the end of section1 ... */
4233 line_before_section1 = prev_line;
4234 line_start_section1 = cur_line;
4237 prev_line = cur_line;
4238 cur_line = cur_line->next;
4241 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4242 line_end_section1 = prev_line;
4243 line_after_section1 = cur_line;
4245 /* ... find section2 ... */
4246 while ((cur_line != NULL) && (line_number < section2))
4248 prev_line = cur_line;
4249 cur_line = cur_line->next;
4253 if ( (cur_line == NULL)
4254 || (cur_line->type != FILE_LINE_ACTION) )
4256 /* Invalid "section2" parameter */
4257 edit_free_file(file);
4258 return JB_ERR_CGI_PARAMS;
4261 /* ... find the end of section2 ... */
4262 line_before_section2 = prev_line;
4263 line_start_section2 = cur_line;
4266 prev_line = cur_line;
4267 cur_line = cur_line->next;
4270 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4271 line_end_section2 = prev_line;
4272 line_after_section2 = cur_line;
4274 /* Now have all the pointers we need. Do the swap. */
4276 /* Change the pointer to section1 to point to section2 instead */
4277 if (line_before_section1 == NULL)
4279 file->lines = line_start_section2;
4283 line_before_section1->next = line_start_section2;
4286 if (line_before_section2 == line_end_section1)
4288 /* Consecutive sections */
4289 line_end_section2->next = line_start_section1;
4293 line_end_section2->next = line_after_section1;
4294 line_before_section2->next = line_start_section1;
4297 /* Set the pointer from the end of section1 to the rest of the file */
4298 line_end_section1->next = line_after_section2;
4300 err = edit_write_file(file);
4303 /* Error writing file */
4304 if (err == JB_ERR_FILE)
4306 /* Read-only file. */
4307 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4309 edit_free_file(file);
4312 } /* END if (section1 != section2) */
4314 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4315 string_append(&target, file->identifier);
4317 edit_free_file(file);
4322 return JB_ERR_MEMORY;
4325 rsp->status = strdup("302 Local Redirect from Privoxy");
4326 if (rsp->status == NULL)
4329 return JB_ERR_MEMORY;
4331 err = enlist_unique_header(rsp->headers, "Location", target);
4338 /*********************************************************************
4340 * Function : cgi_toggle
4342 * Description : CGI function that adds a new empty section to
4346 * 1 : csp = Current client state (buffers, headers, etc...)
4347 * 2 : rsp = http_response data structure for output
4348 * 3 : parameters = map of cgi parameters
4351 * set : If present, how to change toggle setting:
4352 * "enable", "disable", "toggle", or none (default).
4353 * mini : If present, use mini reply template.
4355 * Returns : JB_ERR_OK on success
4356 * JB_ERR_MEMORY on out-of-memory
4358 *********************************************************************/
4359 jb_err cgi_toggle(struct client_state *csp,
4360 struct http_response *rsp,
4361 const struct map *parameters)
4363 struct map *exports;
4365 const char *template_name;
4371 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4373 return cgi_error_disabled(csp, rsp);
4376 mode = get_char_param(parameters, "set");
4383 else if (mode == 'D')
4388 else if (mode == 'T')
4391 g_bToggleIJB = !g_bToggleIJB;
4394 if (NULL == (exports = default_exports(csp, "toggle")))
4396 return JB_ERR_MEMORY;
4399 template_name = (get_char_param(parameters, "mini")
4403 return template_fill_for_cgi(csp, template_name, exports, rsp);
4407 /*********************************************************************
4409 * Function : javascriptify
4411 * Description : Converts a string into a form JavaScript will like.
4413 * Netscape 4's JavaScript sucks - it doesn't use
4414 * "id" parameters, so you have to set the "name"
4415 * used to submit a form element to something JavaScript
4416 * will like. (Or access the elements by index in an
4417 * array. That array contains >60 elements and will
4418 * be changed whenever we add a new action to the
4419 * editor, so I'm NOT going to use indexes that have
4420 * to be figured out by hand.)
4422 * Currently the only thing we have to worry about
4423 * is "-" ==> "_" conversion.
4425 * This is a length-preserving operation so it is
4426 * carried out in-place, no memory is allocated
4430 * 1 : identifier = String to make JavaScript-friendly.
4434 *********************************************************************/
4435 static void javascriptify(char * identifier)
4437 char * p = identifier;
4438 while (NULL != (p = strchr(p, '-')))
4445 /*********************************************************************
4447 * Function : actions_to_radio
4449 * Description : Converts a actionsfile entry into settings for
4450 * radio buttons and edit boxes on a HTML form.
4453 * 1 : exports = List of substitutions to add to.
4454 * 2 : action = Action to read
4456 * Returns : JB_ERR_OK on success
4457 * JB_ERR_MEMORY on out-of-memory
4459 *********************************************************************/
4460 static jb_err actions_to_radio(struct map * exports,
4461 const struct action_spec *action)
4463 unsigned mask = action->mask;
4464 unsigned add = action->add;
4472 mask = action->mask;
4475 /* sanity - prevents "-feature +feature" */
4479 #define DEFINE_ACTION_BOOL(name, bit) \
4480 if (!(mask & bit)) \
4482 current_mode = 'n'; \
4484 else if (add & bit) \
4486 current_mode = 'y'; \
4490 current_mode = 'x'; \
4492 if (map_radio(exports, name, "ynx", current_mode)) \
4494 return JB_ERR_MEMORY; \
4497 #define DEFINE_ACTION_STRING(name, bit, index) \
4498 DEFINE_ACTION_BOOL(name, bit); \
4501 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4504 checked = !strcmp(action->string[index], value); \
4508 checked = is_default; \
4510 mapped_param |= checked; \
4511 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4513 return JB_ERR_MEMORY; \
4516 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4517 if (map(exports, name "-param-custom", 1, \
4518 ((!mapped_param) ? "checked" : ""), 1)) \
4520 return JB_ERR_MEMORY; \
4522 if (map(exports, name "-param", 1, \
4523 (((add & bit) && !mapped_param) ? \
4524 action->string[index] : default_val), 1)) \
4526 return JB_ERR_MEMORY; \
4529 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4530 if (map(exports, name "-param", 1, \
4531 ((add & bit) ? action->string[index] : default_val), 1)) \
4533 return JB_ERR_MEMORY; \
4536 #define DEFINE_ACTION_MULTI(name, index) \
4537 if (action->multi_add[index]->first) \
4539 current_mode = 'y'; \
4541 else if (action->multi_remove_all[index]) \
4543 current_mode = 'n'; \
4545 else if (action->multi_remove[index]->first) \
4547 current_mode = 'y'; \
4551 current_mode = 'x'; \
4553 if (map_radio(exports, name, "ynx", current_mode)) \
4555 return JB_ERR_MEMORY; \
4558 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4560 #include "actionlist.h"
4562 #undef DEFINE_ACTION_MULTI
4563 #undef DEFINE_ACTION_STRING
4564 #undef DEFINE_ACTION_BOOL
4565 #undef DEFINE_ACTION_ALIAS
4566 #undef DEFINE_CGI_PARAM_CUSTOM
4567 #undef DEFINE_CGI_PARAM_RADIO
4568 #undef DEFINE_CGI_PARAM_NO_RADIO
4574 /*********************************************************************
4576 * Function : actions_from_radio
4578 * Description : Converts a map of parameters passed to a CGI function
4579 * into an actionsfile entry.
4582 * 1 : parameters = parameters to the CGI call
4583 * 2 : action = Action to change. Must be valid before
4584 * the call, actions not specified will be
4587 * Returns : JB_ERR_OK on success
4588 * JB_ERR_MEMORY on out-of-memory
4590 *********************************************************************/
4591 static jb_err actions_from_radio(const struct map * parameters,
4592 struct action_spec *action)
4597 const char * js_name;
4598 jb_err err = JB_ERR_OK;
4603 /* Statics are generally a potential race condition,
4604 * but in this case we're safe and don't need semaphores.
4605 * Be careful if you modify this function.
4607 * The js_name_arr's are never free()d, but this is no
4608 * problem, since they will only be created once and
4609 * used by all threads thereafter. -oes
4612 #define JAVASCRIPTIFY(dest_var, string) \
4614 static int first_time = 1; \
4615 static char *js_name_arr; \
4618 js_name_arr = strdup(string); \
4619 javascriptify(js_name_arr); \
4621 dest_var = js_name_arr; \
4625 #define DEFINE_ACTION_BOOL(name, bit) \
4626 JAVASCRIPTIFY(js_name, name); \
4627 ch = get_char_param(parameters, js_name); \
4630 action->add |= bit; \
4631 action->mask |= bit; \
4633 else if (ch == 'N') \
4635 action->add &= ~bit; \
4636 action->mask &= ~bit; \
4638 else if (ch == 'X') \
4640 action->add &= ~bit; \
4641 action->mask |= bit; \
4644 #define DEFINE_ACTION_STRING(name, bit, index) \
4645 JAVASCRIPTIFY(js_name, name); \
4646 ch = get_char_param(parameters, js_name); \
4650 JAVASCRIPTIFY(js_name, name "-mode"); \
4651 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4652 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4654 JAVASCRIPTIFY(js_name, name "-param"); \
4655 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4657 if (param != NULL) \
4659 if (NULL == (param_dup = strdup(param))) \
4661 return JB_ERR_MEMORY; \
4663 freez(action->string[index]); \
4664 action->add |= bit; \
4665 action->mask |= bit; \
4666 action->string[index] = param_dup; \
4669 else if (ch == 'N') \
4671 if (action->add & bit) \
4673 freez(action->string[index]); \
4675 action->add &= ~bit; \
4676 action->mask &= ~bit; \
4678 else if (ch == 'X') \
4680 if (action->add & bit) \
4682 freez(action->string[index]); \
4684 action->add &= ~bit; \
4685 action->mask |= bit; \
4688 #define DEFINE_ACTION_MULTI(name, index) \
4689 JAVASCRIPTIFY(js_name, name); \
4690 ch = get_char_param(parameters, js_name); \
4695 else if (ch == 'N') \
4697 list_remove_all(action->multi_add[index]); \
4698 list_remove_all(action->multi_remove[index]); \
4699 action->multi_remove_all[index] = 1; \
4701 else if (ch == 'X') \
4703 list_remove_all(action->multi_add[index]); \
4704 list_remove_all(action->multi_remove[index]); \
4705 action->multi_remove_all[index] = 0; \
4708 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4710 #include "actionlist.h"
4712 #undef DEFINE_ACTION_MULTI
4713 #undef DEFINE_ACTION_STRING
4714 #undef DEFINE_ACTION_BOOL
4715 #undef DEFINE_ACTION_ALIAS
4716 #undef JAVASCRIPTIFY
4722 #endif /* def FEATURE_CGI_EDIT_ACTIONS */