1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.41 2002/05/21 19:09:45 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
10 * NOTE: The CGIs in this file use parameter names
11 * such as "f" and "s" which are really *BAD* choices.
12 * However, I'm trying to save bytes in the
13 * edit-actions-list HTML page - the standard actions
14 * file generated a 550kbyte page, which is ridiculous.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001 the SourceForge
19 * Privoxy team. http://www.privoxy.org/
21 * Based on the Internet Junkbuster originally written
22 * by and Copyright (C) 1997 Anonymous Coders and
23 * Junkbusters Corporation. http://www.junkbusters.com
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General
27 * Public License as published by the Free Software
28 * Foundation; either version 2 of the License, or (at
29 * your option) any later version.
31 * This program is distributed in the hope that it will
32 * be useful, but WITHOUT ANY WARRANTY; without even the
33 * implied warranty of MERCHANTABILITY or FITNESS FOR A
34 * PARTICULAR PURPOSE. See the GNU General Public
35 * License for more details.
37 * The GNU General Public License should be included with
38 * this file. If not, you can view it at
39 * http://www.gnu.org/copyleft/gpl.html
40 * or write to the Free Software Foundation, Inc., 59
41 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 * Revision 1.41 2002/05/21 19:09:45 oes
46 * - Made Add/Edit/Remove URL Submit and Cancel
47 * buttons jump back to relevant section in eal
48 * - Bugfix: remove-url-form needs p export
50 * Revision 1.40 2002/05/19 11:34:35 jongfoster
51 * Handling read-only actions files better - report the actual
52 * error, not "Out of memory"!
55 * http://sourceforge.net/tracker/index.php?func=detail
56 * &aid=557905&group_id=11118&atid=111118
58 * Revision 1.39 2002/05/12 21:39:15 jongfoster
59 * - Adding Doxygen-style comments to structures and #defines.
60 * - Correcting function comments
62 * Revision 1.38 2002/05/03 23:00:38 jongfoster
63 * Support for templates for "standard actions" buttons.
66 * Revision 1.37 2002/04/30 11:14:52 oes
67 * Made csp the first parameter in *action_to_html
69 * Revision 1.36 2002/04/26 21:53:30 jongfoster
70 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
72 * Revision 1.35 2002/04/26 21:50:02 jongfoster
73 * Honouring default exports in edit-actions-for-url-filter template.
75 * Revision 1.34 2002/04/26 12:54:17 oes
76 * Adaptions to changes in actions.c
78 * Revision 1.33 2002/04/24 02:17:47 oes
79 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
81 * - Activated Jon's code for editing multiple AFs
82 * - cgi_edit_list_actions now provides context-sensitive
83 * help, looks up all action sets from standard.action and
84 * makes buttons for them in the catchall section
85 * - cgi_edit_action_submit now honors a p parameter, looks up
86 * the corresponding action set, and sets the catchall pattern's
87 * actions accordingly.
89 * Revision 1.32 2002/04/19 16:55:31 jongfoster
90 * Fixing newline problems. If we do our own text file newline
91 * mangling, we don't want the library to do any, so we need to
92 * open the files in *binary* mode.
94 * Revision 1.31 2002/04/18 19:21:08 jongfoster
95 * Added code to detect "conventional" action files, that start
96 * with a set of actions for all URLs (the pattern "/").
97 * These are special-cased in the "edit-actions-list" CGI, so
98 * that a special UI can be written for them.
100 * Revision 1.30 2002/04/10 13:38:35 oes
101 * load_template signature changed
103 * Revision 1.29 2002/04/08 16:59:08 oes
106 * Revision 1.28 2002/03/27 12:30:29 oes
107 * Deleted unsused variable
109 * Revision 1.27 2002/03/26 23:06:04 jongfoster
110 * Removing duplicate @ifs on the toggle page
112 * Revision 1.26 2002/03/26 22:59:17 jongfoster
113 * Fixing /toggle to display status consistently.
115 * Revision 1.25 2002/03/26 22:29:54 swa
116 * we have a new homepage!
118 * Revision 1.24 2002/03/24 15:23:33 jongfoster
121 * Revision 1.23 2002/03/24 13:32:41 swa
122 * name change related issues
124 * Revision 1.22 2002/03/24 13:25:43 swa
125 * name change related issues
127 * Revision 1.21 2002/03/22 18:02:48 jongfoster
128 * Fixing remote toggle
130 * Revision 1.20 2002/03/16 20:28:34 oes
131 * Added descriptions to the filters so users will know what they select in the cgi editor
133 * Revision 1.19 2002/03/16 18:38:14 jongfoster
134 * Stopping stupid or malicious users from breaking the actions
135 * file using the web-based editor.
137 * Revision 1.18 2002/03/16 14:57:44 jongfoster
138 * Full support for enabling/disabling modular filters.
140 * Revision 1.17 2002/03/16 14:26:42 jongfoster
141 * First version of modular filters support - READ ONLY!
142 * Fixing a double-free bug in the out-of-memory handling in map_radio().
144 * Revision 1.16 2002/03/07 03:46:17 oes
145 * Fixed compiler warnings
147 * Revision 1.15 2002/03/06 22:54:35 jongfoster
148 * Automated function-comment nitpicking.
150 * Revision 1.14 2002/03/05 00:24:51 jongfoster
151 * Patch to always edit the current actions file.
153 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
154 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
156 * Revision 1.12 2002/03/03 09:18:03 joergs
157 * Made jumbjuster work on AmigaOS again.
159 * Revision 1.11 2002/01/23 01:03:31 jongfoster
160 * Fixing gcc [CygWin] compiler warnings
162 * Revision 1.10 2002/01/23 00:22:59 jongfoster
163 * Adding new function cgi_edit_actions_section_swap(), to reorder
166 * Adding get_url_spec_param() to get a validated URL pattern.
168 * Moving edit_read_line() out of this file and into loaders.c.
170 * Adding missing html_encode() to many CGI functions.
172 * Moving the functions that #include actionlist.h to the end of the file,
173 * because the Visual C++ 97 debugger gets extremely confused if you try
174 * to debug any code that comes after them in the file.
176 * Major optimizations in cgi_edit_actions_list() to reduce the size of
177 * the generated HTML (down 40% from 550k to 304k), with major side-effects
178 * throughout the editor and templates. In particular, the length of the
179 * URLs throughout the editor has been drastically reduced, by cutting
180 * paramater names down to 1 character and CGI names down to 3-4
181 * characters, by removing all non-essential CGI paramaters even at the
182 * expense of having to re-read the actions file for the most trivial
183 * page, and by using relative rather than absolute URLs. This means
184 * that this (typical example):
186 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
187 * filename=ijb&ver=1011487572&section=12&pattern=13
188 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
192 * <a href="eau?f=ijb&v=1011487572&p=13">
194 * Revision 1.9 2002/01/17 20:56:22 jongfoster
195 * Replacing hard references to the URL of the config interface
196 * with #defines from project.h
198 * Revision 1.8 2001/11/30 23:35:51 jongfoster
199 * Renaming actionsfile to ijb.action
201 * Revision 1.7 2001/11/13 00:28:24 jongfoster
202 * - Renaming parameters from edit-actions-for-url so that they only
203 * contain legal JavaScript characters. If we wanted to write
204 * JavaScript that worked with Netscape 4, this is nessacery.
205 * (Note that at the moment the JavaScript doesn't actually work
206 * with Netscape 4, but now this is purely a template issue, not
207 * one affecting code).
208 * - Adding new CGIs for use by non-JavaScript browsers:
209 * edit-actions-url-form
210 * edit-actions-add-url-form
211 * edit-actions-remove-url-form
214 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
215 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
216 * by and __OS2__ ifdef.
218 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
219 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
220 * threads to call select() simultaneously. So, it's time to do a real, live,
221 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
222 * (native). Both versions will work, but using __OS2__ offers multi-threading.
224 * Revision 1.4 2001/10/23 21:48:19 jongfoster
225 * Cleaning up error handling in CGI functions - they now send back
226 * a HTML error page and should never cause a FATAL error. (Fixes one
227 * potential source of "denial of service" attacks).
229 * CGI actions file editor that works and is actually useful.
231 * Ability to toggle JunkBuster remotely using a CGI call.
233 * You can turn off both the above features in the main configuration
234 * file, e.g. if you are running a multi-user proxy.
236 * Revision 1.3 2001/10/14 22:12:49 jongfoster
237 * New version of CGI-based actionsfile editor.
238 * Major changes, including:
239 * - Completely new file parser and file output routines
240 * - edit-actions CGI renamed edit-actions-for-url
241 * - All CGIs now need a filename parameter, except for...
242 * - New CGI edit-actions which doesn't need a filename,
243 * to allow you to start the editor up.
244 * - edit-actions-submit now works, and now automatically
245 * redirects you back to the main edit-actions-list handler.
247 * Revision 1.2 2001/09/16 17:05:14 jongfoster
248 * Removing unused #include showarg.h
250 * Revision 1.1 2001/09/16 15:47:37 jongfoster
251 * First version of CGI-based edit interface. This is very much a
252 * work-in-progress, and you can't actually use it to edit anything
253 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
254 * to have any effect.
257 **********************************************************************/
263 * FIXME: Following includes copied from cgi.c - which are actually needed?
268 #include <sys/types.h>
272 #include <sys/stat.h>
275 #define snprintf _snprintf
276 #endif /* def _WIN32 */
281 #include "cgisimple.h"
285 #include "miscutil.h"
289 /* loadcfg.h is for g_bToggleIJB only */
290 #include "urlmatch.h"
292 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
295 #ifdef FEATURE_CGI_EDIT_ACTIONS
298 * A line in an editable_file.
302 /** Next entry in the linked list */
303 struct file_line * next;
305 /** The raw data, to write out if this line is unmodified. */
308 /** Comments and/or whitespace to put before this line if it's modified
309 and then written out. */
312 /** The actual data, as a string. Line continuation and comment removal
313 are performed on the data read from file before it's stored here, so
314 it will be a single line of data. */
317 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
320 /** The actual data, processed into some sensible data type. */
324 /** An action specification. */
325 struct action_spec action[1];
327 /** A name=value pair. */
331 /** The name in the name=value pair. */
334 /** The value in the name=value pair, as a string. */
337 /** The value in the name=value pair, as an integer. */
342 /* Add more data types here... e.g.
345 struct url_spec url[1];
349 struct action_spec action[1];
359 /** This file_line has not been processed yet. */
360 #define FILE_LINE_UNPROCESSED 1
362 /** This file_line is blank. Can only appear at the end of a file, due to
363 the way the parser works. */
364 #define FILE_LINE_BLANK 2
366 /** This file_line says {{alias}}. */
367 #define FILE_LINE_ALIAS_HEADER 3
369 /** This file_line defines an alias. */
370 #define FILE_LINE_ALIAS_ENTRY 4
372 /** This file_line defines an {action}. */
373 #define FILE_LINE_ACTION 5
375 /** This file_line specifies a URL pattern. */
376 #define FILE_LINE_URL 6
378 /** This file_line says {{settings}}. */
379 #define FILE_LINE_SETTINGS_HEADER 7
381 /** This file_line is in a {{settings}} block. */
382 #define FILE_LINE_SETTINGS_ENTRY 8
384 /** This file_line says {{description}}. */
385 #define FILE_LINE_DESCRIPTION_HEADER 9
387 /** This file_line is in a {{description}} block. */
388 #define FILE_LINE_DESCRIPTION_ENTRY 10
392 * A configuration file, in a format that can be edited and written back to
397 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
398 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
399 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
400 /**< Pre-encoded with url_encode() for ease of use. */
401 const char * version_str; /**< Last modification time, as a string. For CGI param. */
402 /**< Can be used in URL without using url_param(). */
403 unsigned version; /**< Last modification time - prevents chaos with
404 the browser's "back" button. Note that this is a
405 time_t cast to an unsigned. When comparing, always
406 cast the time_t to an unsigned, and *NOT* vice-versa.
407 This may lose the top few bits, but they're not
408 significant anyway. */
409 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
410 Note that changing this after the file has been
411 read in will cause a mess. */
412 struct file_line * parse_error; /**< On parse error, this is the offending line. */
413 const char * parse_error_text; /**< On parse error, this is the problem.
414 (Statically allocated) */
417 /* FIXME: Following non-static functions should be prototyped in .h or made static */
419 /* Functions to read and write arbitrary config files */
420 jb_err edit_read_file(struct client_state *csp,
421 const struct map *parameters,
424 struct editable_file **pfile);
425 jb_err edit_write_file(struct editable_file * file);
426 void edit_free_file(struct editable_file * file);
428 /* Functions to read and write actions files */
429 jb_err edit_parse_actions_file(struct editable_file * file);
430 jb_err edit_read_actions_file(struct client_state *csp,
431 struct http_response *rsp,
432 const struct map *parameters,
434 struct editable_file **pfile);
437 jb_err cgi_error_modified(struct client_state *csp,
438 struct http_response *rsp,
439 const char *filename);
440 jb_err cgi_error_parse(struct client_state *csp,
441 struct http_response *rsp,
442 struct editable_file *file);
443 jb_err cgi_error_file(struct client_state *csp,
444 struct http_response *rsp,
445 const char *filename);
446 jb_err cgi_error_file_read_only(struct client_state *csp,
447 struct http_response *rsp,
448 const char *filename);
449 jb_err cgi_error_disabled(struct client_state *csp,
450 struct http_response *rsp);
452 /* Internal arbitrary config file support functions */
453 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
454 static void edit_free_file_lines(struct file_line * first_line);
456 /* Internal actions file support functions */
457 static int match_actions_file_header_line(const char * line, const char * name);
458 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
460 /* Internal parameter parsing functions */
461 static jb_err get_file_name_param(struct client_state *csp,
462 const struct map *parameters,
463 const char *param_name,
466 const char **pparam);
468 static jb_err get_url_spec_param(struct client_state *csp,
469 const struct map *parameters,
474 /* Internal actionsfile <==> HTML conversion functions */
475 static jb_err map_radio(struct map * exports,
476 const char * optionname,
479 static jb_err actions_to_radio(struct map * exports,
480 const struct action_spec *action);
481 static jb_err actions_from_radio(const struct map * parameters,
482 struct action_spec *action);
485 static jb_err map_copy_parameter_html(struct map *out,
486 const struct map *in,
488 #if 0 /* unused function */
489 static jb_err map_copy_parameter_url(struct map *out,
490 const struct map *in,
492 #endif /* unused function */
494 /* Internal convenience functions */
495 static char *section_target(const unsigned sectionid);
497 /*********************************************************************
499 * Function : section_target
501 * Description : Given an unsigned (section id) n, produce a dynamically
502 * allocated string of the form #l<n>, for use in link
506 * 1 : sectionid = start line number of section
508 * Returns : String with link target, or NULL if out of
511 *********************************************************************/
512 static char *section_target(const unsigned sectionid)
516 snprintf(buf, 30, "#l%d", sectionid);
522 /*********************************************************************
524 * Function : map_copy_parameter_html
526 * Description : Copy a CGI parameter from one map to another, HTML
530 * 1 : out = target map
531 * 2 : in = source map
532 * 3 : name = name of cgi parameter to copy
534 * Returns : JB_ERR_OK on success
535 * JB_ERR_MEMORY on out-of-memory
536 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
539 *********************************************************************/
540 static jb_err map_copy_parameter_html(struct map *out,
541 const struct map *in,
551 value = lookup(in, name);
552 err = map(out, name, 1, html_encode(value), 0);
559 else if (*value == '\0')
561 return JB_ERR_CGI_PARAMS;
570 #if 0 /* unused function */
571 /*********************************************************************
573 * Function : map_copy_parameter_url
575 * Description : Copy a CGI parameter from one map to another, URL
579 * 1 : out = target map
580 * 2 : in = source map
581 * 3 : name = name of cgi parameter to copy
583 * Returns : JB_ERR_OK on success
584 * JB_ERR_MEMORY on out-of-memory
585 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
588 *********************************************************************/
589 static jb_err map_copy_parameter_url(struct map *out,
590 const struct map *in,
600 value = lookup(in, name);
601 err = map(out, name, 1, url_encode(value), 0);
608 else if (*value == '\0')
610 return JB_ERR_CGI_PARAMS;
617 #endif /* 0 - unused function */
619 /*********************************************************************
621 * Function : cgi_edit_actions_url_form
623 * Description : CGI function that displays a form for
627 * 1 : csp = Current client state (buffers, headers, etc...)
628 * 2 : rsp = http_response data structure for output
629 * 3 : parameters = map of cgi parameters
632 * f : (filename) Identifies the file to edit
633 * v : (version) File's last-modified time
634 * p : (pattern) Line number of pattern to edit
636 * Returns : JB_ERR_OK on success
637 * JB_ERR_MEMORY on out-of-memory
638 * JB_ERR_CGI_PARAMS if the CGI parameters are not
639 * specified or not valid.
641 *********************************************************************/
642 jb_err cgi_edit_actions_url_form(struct client_state *csp,
643 struct http_response *rsp,
644 const struct map *parameters)
646 struct map * exports;
648 struct editable_file * file;
649 struct file_line * cur_line;
650 unsigned line_number;
651 unsigned section_start_line_number = 0;
658 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
660 return cgi_error_disabled(csp, rsp);
663 err = get_number_param(csp, parameters, "p", &patternid);
669 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
672 /* No filename specified, can't read file, modified, or out of memory. */
673 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
676 cur_line = file->lines;
678 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
680 if (cur_line->type == FILE_LINE_ACTION)
682 section_start_line_number = line_number;
684 cur_line = cur_line->next;
687 if ( (cur_line == NULL)
688 || (line_number != patternid)
690 || (cur_line->type != FILE_LINE_URL))
692 /* Invalid "patternid" parameter */
693 edit_free_file(file);
694 return JB_ERR_CGI_PARAMS;
697 if (NULL == (exports = default_exports(csp, NULL)))
699 edit_free_file(file);
700 return JB_ERR_MEMORY;
703 err = map(exports, "f", 1, file->identifier, 1);
704 if (!err) err = map(exports, "v", 1, file->version_str, 1);
705 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
706 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
707 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
709 edit_free_file(file);
717 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
721 /*********************************************************************
723 * Function : cgi_edit_actions_add_url_form
725 * Description : CGI function that displays a form for
729 * 1 : csp = Current client state (buffers, headers, etc...)
730 * 2 : rsp = http_response data structure for output
731 * 3 : parameters = map of cgi parameters
734 * f : (filename) Identifies the file to edit
735 * v : (version) File's last-modified time
736 * s : (section) Line number of section to edit
738 * Returns : JB_ERR_OK on success
739 * JB_ERR_MEMORY on out-of-memory
740 * JB_ERR_CGI_PARAMS if the CGI parameters are not
741 * specified or not valid.
743 *********************************************************************/
744 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
745 struct http_response *rsp,
746 const struct map *parameters)
755 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
757 return cgi_error_disabled(csp, rsp);
760 if (NULL == (exports = default_exports(csp, NULL)))
762 return JB_ERR_MEMORY;
765 err = map_copy_parameter_html(exports, parameters, "f");
766 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
767 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
775 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
779 /*********************************************************************
781 * Function : cgi_edit_actions_remove_url_form
783 * Description : CGI function that displays a form for
787 * 1 : csp = Current client state (buffers, headers, etc...)
788 * 2 : rsp = http_response data structure for output
789 * 3 : parameters = map of cgi parameters
792 * f : (filename) Identifies the file to edit
793 * v : (version) File's last-modified time
794 * p : (pattern) Line number of pattern to edit
796 * Returns : JB_ERR_OK on success
797 * JB_ERR_MEMORY on out-of-memory
798 * JB_ERR_CGI_PARAMS if the CGI parameters are not
799 * specified or not valid.
801 *********************************************************************/
802 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
803 struct http_response *rsp,
804 const struct map *parameters)
806 struct map * exports;
808 struct editable_file * file;
809 struct file_line * cur_line;
810 unsigned line_number;
811 unsigned section_start_line_number = 0;
818 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
820 return cgi_error_disabled(csp, rsp);
823 err = get_number_param(csp, parameters, "p", &patternid);
829 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
832 /* No filename specified, can't read file, modified, or out of memory. */
833 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
836 cur_line = file->lines;
838 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
840 if (cur_line->type == FILE_LINE_ACTION)
842 section_start_line_number = line_number;
844 cur_line = cur_line->next;
847 if ( (cur_line == NULL)
848 || (line_number != patternid)
850 || (cur_line->type != FILE_LINE_URL))
852 /* Invalid "patternid" parameter */
853 edit_free_file(file);
854 return JB_ERR_CGI_PARAMS;
857 if (NULL == (exports = default_exports(csp, NULL)))
859 edit_free_file(file);
860 return JB_ERR_MEMORY;
863 err = map(exports, "f", 1, file->identifier, 1);
864 if (!err) err = map(exports, "v", 1, file->version_str, 1);
865 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
866 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
867 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
869 edit_free_file(file);
877 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
881 /*********************************************************************
883 * Function : edit_write_file
885 * Description : Write a complete file to disk.
888 * 1 : file = File to write.
890 * Returns : JB_ERR_OK on success
891 * JB_ERR_FILE on error writing to file.
892 * JB_ERR_MEMORY on out of memory
894 *********************************************************************/
895 jb_err edit_write_file(struct editable_file * file)
898 struct file_line * cur_line;
899 struct stat statbuf[1];
900 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
901 digits in time_t, assuming this is a 64-bit
902 machine, plus null terminator, plus one
906 assert(file->filename);
908 if (NULL == (fp = fopen(file->filename, "wb")))
913 cur_line = file->lines;
914 while (cur_line != NULL)
918 if (fputs(cur_line->raw, fp) < 0)
926 if (cur_line->prefix)
928 if (fputs(cur_line->prefix, fp) < 0)
934 if (cur_line->unprocessed)
937 if (NULL != strchr(cur_line->unprocessed, '#'))
939 /* Must quote '#' characters */
946 /* Count number of # characters, so we know length of output string */
947 src = cur_line->unprocessed;
948 while (NULL != (src = strchr(src, '#')))
955 /* Allocate new memory for string */
956 len = strlen(cur_line->unprocessed);
957 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
959 /* Uh oh, just trashed file! */
961 return JB_ERR_MEMORY;
964 /* Loop through string from end */
965 src = cur_line->unprocessed + len;
966 dest = str + len + numhash;
967 for ( ; len >= 0; len--)
969 if ((*dest-- = *src--) == '#')
973 assert(numhash >= 0);
976 assert(numhash == 0);
977 assert(src + 1 == cur_line->unprocessed);
978 assert(dest + 1 == str);
980 if (fputs(str, fp) < 0)
991 /* Can write without quoting '#' characters. */
992 if (fputs(cur_line->unprocessed, fp) < 0)
998 if (fputs(NEWLINE(file->newline), fp) < 0)
1006 /* FIXME: Write data from file->data->whatever */
1010 cur_line = cur_line->next;
1016 /* Update the version stamp in the file structure, since we just
1017 * wrote to the file & changed it's date.
1019 if (stat(file->filename, statbuf) < 0)
1021 /* Error, probably file not found. */
1024 file->version = (unsigned)statbuf->st_mtime;
1026 /* Correct file->version_str */
1027 freez(file->version_str);
1028 snprintf(version_buf, 22, "%u", file->version);
1029 version_buf[21] = '\0';
1030 file->version_str = strdup(version_buf);
1031 if (version_buf == NULL)
1033 return JB_ERR_MEMORY;
1040 /*********************************************************************
1042 * Function : edit_free_file
1044 * Description : Free a complete file in memory.
1047 * 1 : file = Data structure to free.
1051 *********************************************************************/
1052 void edit_free_file(struct editable_file * file)
1056 /* Silently ignore NULL pointer */
1060 edit_free_file_lines(file->lines);
1061 freez(file->filename);
1062 freez(file->identifier);
1063 freez(file->version_str);
1065 file->parse_error_text = NULL; /* Statically allocated */
1066 file->parse_error = NULL;
1072 /*********************************************************************
1074 * Function : edit_free_file_lines
1076 * Description : Free an entire linked list of file lines.
1079 * 1 : first_line = Data structure to free.
1083 *********************************************************************/
1084 static void edit_free_file_lines(struct file_line * first_line)
1086 struct file_line * next_line;
1088 while (first_line != NULL)
1090 next_line = first_line->next;
1091 first_line->next = NULL;
1092 freez(first_line->raw);
1093 freez(first_line->prefix);
1094 freez(first_line->unprocessed);
1095 switch(first_line->type)
1097 case 0: /* special case if memory zeroed */
1098 case FILE_LINE_UNPROCESSED:
1099 case FILE_LINE_BLANK:
1100 case FILE_LINE_ALIAS_HEADER:
1101 case FILE_LINE_SETTINGS_HEADER:
1102 case FILE_LINE_DESCRIPTION_HEADER:
1103 case FILE_LINE_DESCRIPTION_ENTRY:
1104 case FILE_LINE_ALIAS_ENTRY:
1106 /* No data is stored for these */
1109 case FILE_LINE_ACTION:
1110 free_action(first_line->data.action);
1113 case FILE_LINE_SETTINGS_ENTRY:
1114 freez(first_line->data.setting.name);
1115 freez(first_line->data.setting.svalue);
1118 /* Should never happen */
1122 first_line->type = 0; /* paranoia */
1124 first_line = next_line;
1129 /*********************************************************************
1131 * Function : match_actions_file_header_line
1133 * Description : Match an actions file {{header}} line
1136 * 1 : line = String from file
1137 * 2 : name = Header to match against
1139 * Returns : 0 iff they match.
1141 *********************************************************************/
1142 static int match_actions_file_header_line(const char * line, const char * name)
1150 if ((line[0] != '{') || (line[1] != '{'))
1156 /* Look for optional whitespace */
1157 while ( (*line == ' ') || (*line == '\t') )
1162 /* Look for the specified name (case-insensitive) */
1164 if (0 != strncmpic(line, name, len))
1170 /* Look for optional whitespace */
1171 while ( (*line == ' ') || (*line == '\t') )
1176 /* Look for "}}" and end of string*/
1177 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1187 /*********************************************************************
1189 * Function : match_actions_file_header_line
1191 * Description : Match an actions file {{header}} line
1194 * 1 : line = String from file. Must not start with
1195 * whitespace (else infinite loop!)
1196 * 2 : pname = Destination for name
1197 * 2 : pvalue = Destination for value
1199 * Returns : JB_ERR_OK on success
1200 * JB_ERR_MEMORY on out-of-memory
1201 * JB_ERR_PARSE if there's no "=" sign, or if there's
1202 * nothing before the "=" sign (but empty
1203 * values *after* the "=" sign are legal).
1205 *********************************************************************/
1206 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1208 const char * name_end;
1209 const char * value_start;
1215 assert(*line != ' ');
1216 assert(*line != '\t');
1221 value_start = strchr(line, '=');
1222 if ((value_start == NULL) || (value_start == line))
1224 return JB_ERR_PARSE;
1227 name_end = value_start - 1;
1229 /* Eat any whitespace before the '=' */
1230 while ((*name_end == ' ') || (*name_end == '\t'))
1233 * we already know we must have at least 1 non-ws char
1234 * at start of buf - no need to check
1239 name_len = name_end - line + 1; /* Length excluding \0 */
1240 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1242 return JB_ERR_MEMORY;
1244 strncpy(*pname, line, name_len);
1245 (*pname)[name_len] = '\0';
1247 /* Eat any the whitespace after the '=' */
1249 while ((*value_start == ' ') || (*value_start == '\t'))
1254 if (NULL == (*pvalue = strdup(value_start)))
1258 return JB_ERR_MEMORY;
1265 /*********************************************************************
1267 * Function : edit_parse_actions_file
1269 * Description : Parse an actions file in memory.
1271 * Passed linked list must have the "data" member
1272 * zeroed, and must contain valid "next" and
1273 * "unprocessed" fields. The "raw" and "prefix"
1274 * fields are ignored, and "type" is just overwritten.
1276 * Note that on error the file may have been
1280 * 1 : file = Actions file to be parsed in-place.
1282 * Returns : JB_ERR_OK on success
1283 * JB_ERR_MEMORY on out-of-memory
1284 * JB_ERR_PARSE on error
1286 *********************************************************************/
1287 jb_err edit_parse_actions_file(struct editable_file * file)
1289 struct file_line * cur_line;
1291 const char * text; /* Text from a line */
1292 char * name; /* For lines of the form name=value */
1293 char * value; /* For lines of the form name=value */
1294 struct action_alias * alias_list = NULL;
1295 jb_err err = JB_ERR_OK;
1297 /* alias_list contains the aliases defined in this file.
1298 * It might be better to use the "file_line.data" fields
1299 * in the relavent places instead.
1302 cur_line = file->lines;
1304 /* A note about blank line support: Blank lines should only
1305 * ever occur as the last line in the file. This function
1306 * is more forgiving than that - FILE_LINE_BLANK can occur
1310 /* Skip leading blanks. Should only happen if file is
1311 * empty (which is valid, but pointless).
1313 while ( (cur_line != NULL)
1314 && (cur_line->unprocessed[0] == '\0') )
1317 cur_line->type = FILE_LINE_BLANK;
1318 cur_line = cur_line->next;
1321 if ( (cur_line != NULL)
1322 && (cur_line->unprocessed[0] != '{') )
1324 /* File doesn't start with a header */
1325 file->parse_error = cur_line;
1326 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1327 return JB_ERR_PARSE;
1330 if ( (cur_line != NULL) && (0 ==
1331 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1333 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1335 cur_line = cur_line->next;
1336 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1338 if (cur_line->unprocessed[0])
1340 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1342 err = split_line_on_equals(cur_line->unprocessed,
1343 &cur_line->data.setting.name,
1344 &cur_line->data.setting.svalue);
1345 if (err == JB_ERR_MEMORY)
1349 else if (err != JB_ERR_OK)
1351 /* Line does not contain a name=value pair */
1352 file->parse_error = cur_line;
1353 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1354 return JB_ERR_PARSE;
1359 cur_line->type = FILE_LINE_BLANK;
1361 cur_line = cur_line->next;
1365 if ( (cur_line != NULL) && (0 ==
1366 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1368 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1370 cur_line = cur_line->next;
1371 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1373 if (cur_line->unprocessed[0])
1375 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1379 cur_line->type = FILE_LINE_BLANK;
1381 cur_line = cur_line->next;
1385 if ( (cur_line != NULL) && (0 ==
1386 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1388 cur_line->type = FILE_LINE_ALIAS_HEADER;
1390 cur_line = cur_line->next;
1391 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1393 if (cur_line->unprocessed[0])
1395 /* define an alias */
1396 struct action_alias * new_alias;
1398 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1400 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1401 if (err == JB_ERR_MEMORY)
1405 else if (err != JB_ERR_OK)
1407 /* Line does not contain a name=value pair */
1408 file->parse_error = cur_line;
1409 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1410 return JB_ERR_PARSE;
1413 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1418 free_alias_list(alias_list);
1419 return JB_ERR_MEMORY;
1422 err = get_actions(value, alias_list, new_alias->action);
1425 /* Invalid action or out of memory */
1429 free_alias_list(alias_list);
1430 if (err == JB_ERR_MEMORY)
1436 /* Line does not contain a name=value pair */
1437 file->parse_error = cur_line;
1438 file->parse_error_text = "This alias does not specify a valid set of actions.";
1439 return JB_ERR_PARSE;
1445 new_alias->name = name;
1448 new_alias->next = alias_list;
1449 alias_list = new_alias;
1453 cur_line->type = FILE_LINE_BLANK;
1455 cur_line = cur_line->next;
1459 /* Header done, process the main part of the file */
1460 while (cur_line != NULL)
1462 /* At this point, (cur_line->unprocessed[0] == '{') */
1463 assert(cur_line->unprocessed[0] == '{');
1464 text = cur_line->unprocessed + 1;
1465 len = strlen(text) - 1;
1466 if (text[len] != '}')
1468 /* No closing } on header */
1469 free_alias_list(alias_list);
1470 file->parse_error = cur_line;
1471 file->parse_error_text = "Headers starting with '{' must have a "
1472 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1473 "must close with two brackets ('}}').";
1474 return JB_ERR_PARSE;
1479 /* An invalid {{ header. */
1480 free_alias_list(alias_list);
1481 file->parse_error = cur_line;
1482 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1483 "Please remember that the system (two-bracket) headers must "
1484 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1485 "and must appear before any actions (one-bracket) headers. "
1486 "Also note that system headers may not be repeated.";
1487 return JB_ERR_PARSE;
1490 while ( (*text == ' ') || (*text == '\t') )
1496 && ( (text[len - 1] == ' ')
1497 || (text[len - 1] == '\t') ) )
1502 cur_line->type = FILE_LINE_ACTION;
1504 /* Remove {} and make copy */
1505 if (NULL == (value = (char *) malloc(len + 1)))
1508 free_alias_list(alias_list);
1509 return JB_ERR_MEMORY;
1511 strncpy(value, text, len);
1515 err = get_actions(value, alias_list, cur_line->data.action);
1518 /* Invalid action or out of memory */
1520 free_alias_list(alias_list);
1521 if (err == JB_ERR_MEMORY)
1527 /* Line does not contain a name=value pair */
1528 file->parse_error = cur_line;
1529 file->parse_error_text = "This header does not specify a valid set of actions.";
1530 return JB_ERR_PARSE;
1534 /* Done with string - it was clobbered anyway */
1537 /* Process next line */
1538 cur_line = cur_line->next;
1540 /* Loop processing URL patterns */
1541 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1543 if (cur_line->unprocessed[0])
1545 /* Could parse URL here, but this isn't currently needed */
1547 cur_line->type = FILE_LINE_URL;
1551 cur_line->type = FILE_LINE_BLANK;
1553 cur_line = cur_line->next;
1555 } /* End main while(cur_line != NULL) loop */
1557 free_alias_list(alias_list);
1563 /*********************************************************************
1565 * Function : edit_read_file_lines
1567 * Description : Read all the lines of a file into memory.
1568 * Handles whitespace, comments and line continuation.
1571 * 1 : fp = File to read from. On return, this will be
1572 * at EOF but it will not have been closed.
1573 * 2 : pfile = Destination for a linked list of file_lines.
1574 * Will be set to NULL on error.
1575 * 3 : newline = How to handle newlines.
1577 * Returns : JB_ERR_OK on success
1578 * JB_ERR_MEMORY on out-of-memory
1580 *********************************************************************/
1581 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1583 struct file_line * first_line; /* Keep for return value or to free */
1584 struct file_line * cur_line; /* Current line */
1585 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1593 cur_line = first_line = zalloc(sizeof(struct file_line));
1594 if (cur_line == NULL)
1596 return JB_ERR_MEMORY;
1599 cur_line->type = FILE_LINE_UNPROCESSED;
1601 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1604 /* Out of memory or empty file. */
1605 /* Note that empty file is not an error we propogate up */
1607 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1612 prev_line = cur_line;
1613 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1614 if (cur_line == NULL)
1617 edit_free_file_lines(first_line);
1618 return JB_ERR_MEMORY;
1621 cur_line->type = FILE_LINE_UNPROCESSED;
1623 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1624 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1627 edit_free_file_lines(first_line);
1628 return JB_ERR_MEMORY;
1632 while (rval != JB_ERR_FILE);
1636 /* We allocated one too many - free it */
1637 prev_line->next = NULL;
1640 *pfile = first_line;
1645 /*********************************************************************
1647 * Function : edit_read_file
1649 * Description : Read a complete file into memory.
1650 * Handles CGI parameter parsing. If requested, also
1651 * checks the file's modification timestamp.
1654 * 1 : csp = Current client state (buffers, headers, etc...)
1655 * 2 : parameters = map of cgi parameters.
1656 * 3 : require_version = true to check "ver" parameter.
1657 * 4 : suffix = File extension, e.g. ".action".
1658 * 5 : pfile = Destination for the file. Will be set
1662 * filename : The name of the file to read, without the
1663 * path or ".action" extension.
1664 * ver : (Only if require_version is nonzero)
1665 * Timestamp of the actions file. If wrong, this
1666 * function fails with JB_ERR_MODIFIED.
1668 * Returns : JB_ERR_OK on success
1669 * JB_ERR_MEMORY on out-of-memory
1670 * JB_ERR_CGI_PARAMS if "filename" was not specified
1672 * JB_ERR_FILE if the file cannot be opened or
1674 * JB_ERR_MODIFIED if version checking was requested and
1675 * failed - the file was modified outside
1676 * of this CGI editor instance.
1678 *********************************************************************/
1679 jb_err edit_read_file(struct client_state *csp,
1680 const struct map *parameters,
1681 int require_version,
1683 struct editable_file **pfile)
1685 struct file_line * lines;
1689 const char * identifier;
1690 struct editable_file * file;
1691 unsigned version = 0;
1692 struct stat statbuf[1];
1693 char version_buf[22];
1694 int newline = NEWLINE_UNKNOWN;
1702 err = get_file_name_param(csp, parameters, "f", suffix,
1703 &filename, &identifier);
1709 if (stat(filename, statbuf) < 0)
1711 /* Error, probably file not found. */
1715 version = (unsigned) statbuf->st_mtime;
1717 if (require_version)
1719 unsigned specified_version;
1720 err = get_number_param(csp, parameters, "v", &specified_version);
1727 if (version != specified_version)
1729 return JB_ERR_MODIFIED;
1733 if (NULL == (fp = fopen(filename,"rb")))
1739 err = edit_read_file_lines(fp, &lines, &newline);
1749 file = (struct editable_file *) zalloc(sizeof(*file));
1753 edit_free_file_lines(lines);
1757 file->lines = lines;
1758 file->newline = newline;
1759 file->filename = filename;
1760 file->version = version;
1761 file->identifier = url_encode(identifier);
1763 if (file->identifier == NULL)
1765 edit_free_file(file);
1766 return JB_ERR_MEMORY;
1769 /* Correct file->version_str */
1770 freez(file->version_str);
1771 snprintf(version_buf, 22, "%u", file->version);
1772 version_buf[21] = '\0';
1773 file->version_str = strdup(version_buf);
1774 if (version_buf == NULL)
1776 edit_free_file(file);
1777 return JB_ERR_MEMORY;
1785 /*********************************************************************
1787 * Function : edit_read_actions_file
1789 * Description : Read a complete actions file into memory.
1790 * Handles CGI parameter parsing. If requested, also
1791 * checks the file's modification timestamp.
1793 * If this function detects an error in the categories
1794 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1795 * then it handles it by filling in the specified
1796 * response structure and returning JB_ERR_FILE.
1799 * 1 : csp = Current client state (buffers, headers, etc...)
1800 * 2 : rsp = HTTP response. Only filled in on error.
1801 * 2 : parameters = map of cgi parameters.
1802 * 3 : require_version = true to check "ver" parameter.
1803 * 4 : pfile = Destination for the file. Will be set
1807 * filename : The name of the actions file to read, without the
1808 * path or ".action" extension.
1809 * ver : (Only if require_version is nonzero)
1810 * Timestamp of the actions file. If wrong, this
1811 * function fails with JB_ERR_MODIFIED.
1813 * Returns : JB_ERR_OK on success
1814 * JB_ERR_MEMORY on out-of-memory
1815 * JB_ERR_CGI_PARAMS if "filename" was not specified
1817 * JB_ERR_FILE if the file does not contain valid data,
1818 * or if file cannot be opened or
1819 * contains no data, or if version
1820 * checking was requested and failed.
1822 *********************************************************************/
1823 jb_err edit_read_actions_file(struct client_state *csp,
1824 struct http_response *rsp,
1825 const struct map *parameters,
1826 int require_version,
1827 struct editable_file **pfile)
1830 struct editable_file *file;
1838 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1841 /* Try to handle if possible */
1842 if (err == JB_ERR_FILE)
1844 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1846 else if (err == JB_ERR_MODIFIED)
1848 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1850 if (err == JB_ERR_OK)
1853 * Signal to higher-level CGI code that there was a problem but we
1854 * handled it, they should just return JB_ERR_OK.
1861 err = edit_parse_actions_file(file);
1864 if (err == JB_ERR_PARSE)
1866 err = cgi_error_parse(csp, rsp, file);
1867 if (err == JB_ERR_OK)
1870 * Signal to higher-level CGI code that there was a problem but we
1871 * handled it, they should just return JB_ERR_OK.
1876 edit_free_file(file);
1885 /*********************************************************************
1887 * Function : get_file_name_param
1889 * Description : Get the name of the file to edit from the parameters
1890 * passed to a CGI function. This function handles
1891 * security checks such as blocking urls containing
1892 * "/" or ".", prepending the config file directory,
1893 * and adding the specified suffix.
1895 * (This is an essential security check, otherwise
1896 * users may be able to pass "../../../etc/passwd"
1897 * and overwrite the password file [linux], "prn:"
1898 * and print random data [Windows], etc...)
1900 * This function only allows filenames contining the
1901 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1902 * That's probably too restrictive but at least it's
1906 * 1 : csp = Current client state (buffers, headers, etc...)
1907 * 2 : parameters = map of cgi parameters
1908 * 3 : param_name = The name of the parameter to read
1909 * 4 : suffix = File extension, e.g. ".actions"
1910 * 5 : pfilename = destination for full filename. Caller
1911 * free()s. Set to NULL on error.
1912 * 6 : pparam = destination for partial filename,
1913 * suitable for use in another URL. Allocated as part
1914 * of the map "parameters", so don't free it.
1915 * Set to NULL if not specified.
1917 * Returns : JB_ERR_OK on success
1918 * JB_ERR_MEMORY on out-of-memory
1919 * JB_ERR_CGI_PARAMS if "filename" was not specified
1922 *********************************************************************/
1923 static jb_err get_file_name_param(struct client_state *csp,
1924 const struct map *parameters,
1925 const char *param_name,
1928 const char **pparam)
1946 param = lookup(parameters, param_name);
1949 return JB_ERR_CGI_PARAMS;
1954 len = strlen(param);
1955 if (len >= FILENAME_MAX)
1958 return JB_ERR_CGI_PARAMS;
1961 /* Check every character to see if it's legal */
1963 while ((ch = *s++) != '\0')
1965 if ( ((ch < 'A') || (ch > 'Z'))
1966 && ((ch < 'a') || (ch > 'z'))
1967 && ((ch < '0') || (ch > '9'))
1971 /* Probable hack attempt. */
1972 return JB_ERR_CGI_PARAMS;
1976 /* Append extension */
1977 name = malloc(len + strlen(suffix) + 1);
1980 return JB_ERR_MEMORY;
1982 strcpy(name, param);
1983 strcpy(name + len, suffix);
1986 fullpath = make_path(csp->config->confdir, name);
1989 if (fullpath == NULL)
1991 return JB_ERR_MEMORY;
1995 *pfilename = fullpath;
2001 /*********************************************************************
2003 * Function : get_url_spec_param
2005 * Description : Get a URL pattern from the parameters
2006 * passed to a CGI function. Removes leading/trailing
2007 * spaces and validates it.
2010 * 1 : csp = Current client state (buffers, headers, etc...)
2011 * 2 : parameters = map of cgi parameters
2012 * 3 : name = Name of CGI parameter to read
2013 * 4 : pvalue = destination for value. Will be malloc()'d.
2014 * Set to NULL on error.
2016 * Returns : JB_ERR_OK on success
2017 * JB_ERR_MEMORY on out-of-memory
2018 * JB_ERR_CGI_PARAMS if the parameter was not specified
2021 *********************************************************************/
2022 static jb_err get_url_spec_param(struct client_state *csp,
2023 const struct map *parameters,
2027 const char *orig_param;
2030 struct url_spec compiled[1];
2040 orig_param = lookup(parameters, name);
2043 return JB_ERR_CGI_PARAMS;
2046 /* Copy and trim whitespace */
2047 param = strdup(orig_param);
2050 return JB_ERR_MEMORY;
2054 /* Must be non-empty, and can't allow 1st character to be '{' */
2055 if (param[0] == '\0' || param[0] == '{')
2058 return JB_ERR_CGI_PARAMS;
2061 /* Check for embedded newlines */
2062 for (s = param; *s != '\0'; s++)
2064 if ((*s == '\r') || (*s == '\n'))
2067 return JB_ERR_CGI_PARAMS;
2071 /* Check that regex is valid */
2076 return JB_ERR_MEMORY;
2078 err = create_url_spec(compiled, s);
2083 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2085 free_url_spec(compiled);
2087 if (param[strlen(param) - 1] == '\\')
2090 * Must protect trailing '\\' from becoming line continuation character.
2091 * Two methods: 1) If it's a domain only, add a trailing '/'.
2092 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2094 if (strchr(param, '/') == NULL)
2096 err = string_append(¶m, "/");
2100 err = string_append(¶m, "(?:)");
2107 /* Check that the modified regex is valid */
2112 return JB_ERR_MEMORY;
2114 err = create_url_spec(compiled, s);
2119 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2121 free_url_spec(compiled);
2128 /*********************************************************************
2130 * Function : map_radio
2132 * Description : Map a set of radio button values. E.g. if you have
2133 * 3 radio buttons, declare them as:
2134 * <option type="radio" name="xyz" @xyz-a@>
2135 * <option type="radio" name="xyz" @xyz-b@>
2136 * <option type="radio" name="xyz" @xyz-c@>
2137 * Then map one of the @xyz-?@ variables to "checked"
2138 * and all the others to empty by calling:
2139 * map_radio(exports, "xyz", "abc", sel)
2140 * Where 'sel' is 'a', 'b', or 'c'.
2143 * 1 : exports = Exports map to modify.
2144 * 2 : optionname = name for map
2145 * 3 : values = null-terminated list of values;
2146 * 4 : value = Selected value.
2148 * CGI Parameters : None
2150 * Returns : JB_ERR_OK on success
2151 * JB_ERR_MEMORY on out-of-memory
2153 *********************************************************************/
2154 static jb_err map_radio(struct map * exports,
2155 const char * optionname,
2156 const char * values,
2168 len = strlen(optionname);
2169 buf = malloc(len + 3);
2172 return JB_ERR_MEMORY;
2175 strcpy(buf, optionname);
2180 while ((c = *values++) != '\0')
2185 if (map(exports, buf, 1, "", 1))
2187 return JB_ERR_MEMORY;
2193 return map(exports, buf, 0, "checked", 1);
2197 /*********************************************************************
2199 * Function : cgi_error_modified
2201 * Description : CGI function that is called when a file is modified
2202 * outside the CGI editor.
2205 * 1 : csp = Current client state (buffers, headers, etc...)
2206 * 2 : rsp = http_response data structure for output
2207 * 3 : filename = The file that was modified.
2209 * CGI Parameters : none
2211 * Returns : JB_ERR_OK on success
2212 * JB_ERR_MEMORY on out-of-memory error.
2214 *********************************************************************/
2215 jb_err cgi_error_modified(struct client_state *csp,
2216 struct http_response *rsp,
2217 const char *filename)
2219 struct map *exports;
2226 if (NULL == (exports = default_exports(csp, NULL)))
2228 return JB_ERR_MEMORY;
2231 err = map(exports, "f", 1, html_encode(filename), 0);
2238 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2242 /*********************************************************************
2244 * Function : cgi_error_parse
2246 * Description : CGI function that is called when a file cannot
2247 * be parsed by the CGI editor.
2250 * 1 : csp = Current client state (buffers, headers, etc...)
2251 * 2 : rsp = http_response data structure for output
2252 * 3 : file = The file that was modified.
2254 * CGI Parameters : none
2256 * Returns : JB_ERR_OK on success
2257 * JB_ERR_MEMORY on out-of-memory error.
2259 *********************************************************************/
2260 jb_err cgi_error_parse(struct client_state *csp,
2261 struct http_response *rsp,
2262 struct editable_file *file)
2264 struct map *exports;
2266 struct file_line *cur_line;
2272 if (NULL == (exports = default_exports(csp, NULL)))
2274 return JB_ERR_MEMORY;
2277 err = map(exports, "f", 1, file->identifier, 1);
2278 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2280 cur_line = file->parse_error;
2283 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2284 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2292 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2296 /*********************************************************************
2298 * Function : cgi_error_file
2300 * Description : CGI function that is called when a file cannot be
2301 * opened by the CGI editor.
2304 * 1 : csp = Current client state (buffers, headers, etc...)
2305 * 2 : rsp = http_response data structure for output
2306 * 3 : filename = The file that was modified.
2308 * CGI Parameters : none
2310 * Returns : JB_ERR_OK on success
2311 * JB_ERR_MEMORY on out-of-memory error.
2313 *********************************************************************/
2314 jb_err cgi_error_file(struct client_state *csp,
2315 struct http_response *rsp,
2316 const char *filename)
2318 struct map *exports;
2325 if (NULL == (exports = default_exports(csp, NULL)))
2327 return JB_ERR_MEMORY;
2330 err = map(exports, "f", 1, html_encode(filename), 0);
2337 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2341 /*********************************************************************
2343 * Function : cgi_error_file
2345 * Description : CGI function that is called when a file cannot be
2346 * opened for writing by the CGI editor.
2349 * 1 : csp = Current client state (buffers, headers, etc...)
2350 * 2 : rsp = http_response data structure for output
2351 * 3 : filename = The file that we can't write to
2353 * CGI Parameters : none
2355 * Returns : JB_ERR_OK on success
2356 * JB_ERR_MEMORY on out-of-memory error.
2358 *********************************************************************/
2359 jb_err cgi_error_file_read_only(struct client_state *csp,
2360 struct http_response *rsp,
2361 const char *filename)
2363 struct map *exports;
2370 if (NULL == (exports = default_exports(csp, NULL)))
2372 return JB_ERR_MEMORY;
2375 err = map(exports, "f", 1, html_encode(filename), 0);
2382 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2386 /*********************************************************************
2388 * Function : cgi_error_disabled
2390 * Description : CGI function that is called if the actions editor
2391 * is called although it's disabled in config
2394 * 1 : csp = Current client state (buffers, headers, etc...)
2395 * 2 : rsp = http_response data structure for output
2397 * CGI Parameters : none
2399 * Returns : JB_ERR_OK on success
2400 * JB_ERR_MEMORY on out-of-memory error.
2402 *********************************************************************/
2403 jb_err cgi_error_disabled(struct client_state *csp,
2404 struct http_response *rsp)
2406 struct map *exports;
2411 if (NULL == (exports = default_exports(csp, NULL)))
2413 return JB_ERR_MEMORY;
2416 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2420 /*********************************************************************
2422 * Function : cgi_edit_actions
2424 * Description : CGI function that allows the user to choose which
2425 * actions file to edit.
2428 * 1 : csp = Current client state (buffers, headers, etc...)
2429 * 2 : rsp = http_response data structure for output
2430 * 3 : parameters = map of cgi parameters
2432 * CGI Parameters : None
2434 * Returns : JB_ERR_OK on success
2435 * JB_ERR_MEMORY on out-of-memory error
2437 *********************************************************************/
2438 jb_err cgi_edit_actions(struct client_state *csp,
2439 struct http_response *rsp,
2440 const struct map *parameters)
2443 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2445 return cgi_error_disabled(csp, rsp);
2448 /* FIXME: Incomplete */
2449 rsp->status = strdup("302 Local Redirect from Privoxy");
2450 if (rsp->status == NULL)
2452 return JB_ERR_MEMORY;
2454 if (enlist_unique_header(rsp->headers, "Location",
2455 CGI_PREFIX "edit-actions-list?f=default"))
2459 return JB_ERR_MEMORY;
2466 /*********************************************************************
2468 * Function : cgi_edit_actions_list
2470 * Description : CGI function that edits the actions list.
2471 * FIXME: This function shouldn't FATAL ever.
2472 * FIXME: This function doesn't check the retval of map()
2474 * 1 : csp = Current client state (buffers, headers, etc...)
2475 * 2 : rsp = http_response data structure for output
2476 * 3 : parameters = map of cgi parameters
2478 * CGI Parameters : filename
2480 * Returns : JB_ERR_OK on success
2481 * JB_ERR_MEMORY on out-of-memory
2482 * JB_ERR_FILE if the file cannot be opened or
2484 * JB_ERR_CGI_PARAMS if "filename" was not specified
2487 *********************************************************************/
2488 jb_err cgi_edit_actions_list(struct client_state *csp,
2489 struct http_response *rsp,
2490 const struct map *parameters)
2492 char * section_template;
2493 char * url_template;
2498 struct map * exports;
2499 struct map * section_exports;
2500 struct map * url_exports;
2501 struct editable_file * file;
2502 struct file_line * cur_line;
2503 unsigned line_number = 0;
2504 unsigned prev_section_line_number = ((unsigned) (-1));
2506 struct file_list * fl;
2507 struct url_actions * b;
2508 char * buttons = NULL;
2511 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2513 return cgi_error_disabled(csp, rsp);
2516 if (NULL == (exports = default_exports(csp, NULL)))
2518 edit_free_file(file);
2519 return JB_ERR_MEMORY;
2522 /* Load actions file */
2523 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2526 /* No filename specified, can't read file, or out of memory. */
2527 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2530 /* Find start of actions in file */
2531 cur_line = file->lines;
2533 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2535 cur_line = cur_line->next;
2540 * Conventional actions files should have a match all block
2542 * cur_line = {...global actions...}
2543 * cur_line->next = /
2544 * cur_line->next->next = {...actions...} or EOF
2546 if ( (cur_line != NULL)
2547 && (cur_line->type == FILE_LINE_ACTION)
2548 && (cur_line->next != NULL)
2549 && (cur_line->next->type == FILE_LINE_URL)
2550 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2551 && ( (cur_line->next->next == NULL)
2552 || (cur_line->next->next->type != FILE_LINE_URL)
2556 * Generate string with buttons to set actions for "/" to
2557 * any predefined set of actions (named standard.*, probably
2558 * residing in standard.action).
2561 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2564 edit_free_file(file);
2566 if (err == JB_ERR_FILE)
2568 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2573 err = template_fill(§ion_template, exports);
2576 edit_free_file(file);
2581 buttons = strdup("");
2582 for (i = 0; i < MAX_ACTION_FILES; i++)
2584 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2586 for (b = b->next; NULL != b; b = b->next)
2588 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2590 if (err || (NULL == (section_exports = new_map())))
2593 free(section_template);
2594 edit_free_file(file);
2596 return JB_ERR_MEMORY;
2599 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2601 if (err || (NULL == (s = strdup(section_template))))
2603 free_map(section_exports);
2605 free(section_template);
2606 edit_free_file(file);
2608 return JB_ERR_MEMORY;
2611 if (!err) err = template_fill(&s, section_exports);
2612 free_map(section_exports);
2613 if (!err) err = string_join(&buttons, s);
2618 freez(section_template);
2619 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2622 * Conventional actions file, supply extra editing help.
2623 * (e.g. don't allow them to make it an unconventional one).
2625 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2627 snprintf(buf, 150, "%d", line_number);
2628 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2629 snprintf(buf, 150, "%d", line_number + 2);
2630 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2631 if (!err) err = map(exports, "all-urls-actions", 1,
2632 actions_to_html(csp, cur_line->data.action), 0);
2634 /* Skip the 2 lines */
2635 cur_line = cur_line->next->next;
2639 * Note that prev_section_line_number is NOT set here.
2640 * This is deliberate and not a bug. It stops a "Move up"
2641 * option appearing on the next section. Clicking "Move
2642 * up" would make the actions file unconventional, which
2643 * we don't want, so we hide this option.
2649 * Non-standard actions file - does not begin with
2650 * the "All URLs" section.
2652 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2655 /* Set up global exports */
2657 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2658 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2660 /* Discourage private additions to default.action */
2662 if (!err) err = map_conditional(exports, "default-action",
2663 (strcmp("default", lookup(parameters, "f")) == 0));
2666 edit_free_file(file);
2671 /* Should do all global exports above this point */
2673 /* Load templates */
2675 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2678 edit_free_file(file);
2680 if (err == JB_ERR_FILE)
2682 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2687 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2690 free(section_template);
2691 edit_free_file(file);
2693 if (err == JB_ERR_FILE)
2695 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2700 err = template_fill(§ion_template, exports);
2704 edit_free_file(file);
2710 err = template_fill(&url_template, exports);
2713 free(section_template);
2714 edit_free_file(file);
2719 if (NULL == (sections = strdup("")))
2721 free(section_template);
2723 edit_free_file(file);
2725 return JB_ERR_MEMORY;
2728 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2730 if (NULL == (section_exports = new_map()))
2733 free(section_template);
2735 edit_free_file(file);
2737 return JB_ERR_MEMORY;
2740 snprintf(buf, 150, "%d", line_number);
2741 err = map(section_exports, "s", 1, buf, 1);
2742 if (!err) err = map(section_exports, "actions", 1,
2743 actions_to_html(csp, cur_line->data.action), 0);
2746 && (cur_line->next != NULL)
2747 && (cur_line->next->type == FILE_LINE_URL))
2749 /* This section contains at least one URL, don't allow delete */
2750 err = map_block_killer(section_exports, "empty-section");
2754 if (!err) err = map_block_keep(section_exports, "empty-section");
2757 if (prev_section_line_number != ((unsigned)(-1)))
2759 /* Not last section */
2760 snprintf(buf, 150, "%d", prev_section_line_number);
2761 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2762 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2767 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2769 prev_section_line_number = line_number;
2774 free(section_template);
2776 edit_free_file(file);
2778 free_map(section_exports);
2782 /* Should do all section-specific exports above this point */
2784 if (NULL == (urls = strdup("")))
2787 free(section_template);
2789 edit_free_file(file);
2791 free_map(section_exports);
2792 return JB_ERR_MEMORY;
2797 cur_line = cur_line->next;
2800 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2802 if (NULL == (url_exports = new_map()))
2806 free(section_template);
2808 edit_free_file(file);
2810 free_map(section_exports);
2811 return JB_ERR_MEMORY;
2814 snprintf(buf, 150, "%d", line_number);
2815 err = map(url_exports, "p", 1, buf, 1);
2817 snprintf(buf, 150, "%d", url_1_2);
2818 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2820 if (!err) err = map(url_exports, "url-html", 1,
2821 html_encode(cur_line->unprocessed), 0);
2822 if (!err) err = map(url_exports, "url", 1,
2823 url_encode(cur_line->unprocessed), 0);
2829 free(section_template);
2831 edit_free_file(file);
2833 free_map(section_exports);
2834 free_map(url_exports);
2838 if (NULL == (s = strdup(url_template)))
2842 free(section_template);
2844 edit_free_file(file);
2846 free_map(section_exports);
2847 free_map(url_exports);
2848 return JB_ERR_MEMORY;
2851 err = template_fill(&s, section_exports);
2852 if (!err) err = template_fill(&s, url_exports);
2853 if (!err) err = string_append(&urls, s);
2855 free_map(url_exports);
2862 free(section_template);
2864 edit_free_file(file);
2866 free_map(section_exports);
2870 url_1_2 = 3 - url_1_2;
2872 cur_line = cur_line->next;
2876 err = map(section_exports, "urls", 1, urls, 0);
2878 /* Could also do section-specific exports here, but it wouldn't be as fast */
2880 if ( (cur_line != NULL)
2881 && (cur_line->type == FILE_LINE_ACTION))
2883 /* Not last section */
2884 snprintf(buf, 150, "%d", line_number);
2885 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2886 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2891 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2897 free(section_template);
2899 edit_free_file(file);
2901 free_map(section_exports);
2905 if (NULL == (s = strdup(section_template)))
2908 free(section_template);
2910 edit_free_file(file);
2912 free_map(section_exports);
2913 return JB_ERR_MEMORY;
2916 err = template_fill(&s, section_exports);
2917 if (!err) err = string_append(§ions, s);
2920 free_map(section_exports);
2925 free(section_template);
2927 edit_free_file(file);
2933 edit_free_file(file);
2934 free(section_template);
2937 err = map(exports, "sections", 1, sections, 0);
2944 /* Could also do global exports here, but it wouldn't be as fast */
2946 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2950 /*********************************************************************
2952 * Function : cgi_edit_actions_for_url
2954 * Description : CGI function that edits the Actions list.
2957 * 1 : csp = Current client state (buffers, headers, etc...)
2958 * 2 : rsp = http_response data structure for output
2959 * 3 : parameters = map of cgi parameters
2961 * CGI Parameters : None
2963 * Returns : JB_ERR_OK on success
2964 * JB_ERR_MEMORY on out-of-memory
2965 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2966 * specified or not valid.
2968 *********************************************************************/
2969 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2970 struct http_response *rsp,
2971 const struct map *parameters)
2973 struct map * exports;
2975 struct editable_file * file;
2976 struct file_line * cur_line;
2977 unsigned line_number;
2979 struct file_list *filter_file;
2980 struct re_filterfile_spec *filter_group;
2982 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2984 return cgi_error_disabled(csp, rsp);
2987 err = get_number_param(csp, parameters, "s", §ionid);
2993 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2996 /* No filename specified, can't read file, modified, or out of memory. */
2997 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3000 cur_line = file->lines;
3002 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3004 cur_line = cur_line->next;
3007 if ( (cur_line == NULL)
3008 || (line_number != sectionid)
3010 || (cur_line->type != FILE_LINE_ACTION))
3012 /* Invalid "sectionid" parameter */
3013 edit_free_file(file);
3014 return JB_ERR_CGI_PARAMS;
3017 if (NULL == (exports = default_exports(csp, NULL)))
3019 edit_free_file(file);
3020 return JB_ERR_MEMORY;
3023 err = map(exports, "f", 1, file->identifier, 1);
3024 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3025 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3027 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3029 filter_file = csp->rlist;
3030 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
3032 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
3036 edit_free_file(file);
3041 if (filter_group == NULL)
3043 err = map(exports, "filter-params", 1, "", 1);
3047 /* We have some entries in the filter list */
3050 char * filter_template;
3052 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3055 edit_free_file(file);
3057 if (err == JB_ERR_FILE)
3059 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3064 err = template_fill(&filter_template, exports);
3066 result = strdup("");
3068 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3070 char current_mode = 'x';
3071 struct list_entry *filter_name;
3073 struct map *line_exports;
3076 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
3077 while ((filter_name != NULL)
3078 && (0 != strcmp(filter_group->name, filter_name->str)))
3080 filter_name = filter_name->next;
3083 if (filter_name != NULL)
3089 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
3090 while ((filter_name != NULL)
3091 && (0 != strcmp(filter_group->name, filter_name->str)))
3093 filter_name = filter_name->next;
3095 if (filter_name != NULL)
3101 /* Generate a unique serial number */
3102 snprintf(number, sizeof(number), "%x", index++);
3103 number[sizeof(number) - 1] = '\0';
3105 line_exports = new_map();
3106 if (line_exports == NULL)
3108 err = JB_ERR_MEMORY;
3113 if (!err) err = map(line_exports, "index", 1, number, 1);
3114 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3115 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3116 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3121 this_line = strdup(filter_template);
3122 if (this_line == NULL) err = JB_ERR_MEMORY;
3124 if (!err) err = template_fill(&this_line, line_exports);
3125 string_join(&result, this_line);
3127 free_map(line_exports);
3131 freez(filter_template);
3135 err = map(exports, "filter-params", 1, result, 0);
3143 if (!err) err = map_radio(exports, "filter-all", "nx",
3144 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3146 edit_free_file(file);
3154 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3158 /*********************************************************************
3160 * Function : cgi_edit_actions_submit
3162 * Description : CGI function that actually edits the Actions list.
3165 * 1 : csp = Current client state (buffers, headers, etc...)
3166 * 2 : rsp = http_response data structure for output
3167 * 3 : parameters = map of cgi parameters
3169 * CGI Parameters : None
3171 * Returns : JB_ERR_OK on success
3172 * JB_ERR_MEMORY on out-of-memory
3173 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3174 * specified or not valid.
3176 *********************************************************************/
3177 jb_err cgi_edit_actions_submit(struct client_state *csp,
3178 struct http_response *rsp,
3179 const struct map *parameters)
3185 struct editable_file * file;
3186 struct file_line * cur_line;
3187 unsigned line_number;
3191 const char * action_set_name;
3193 struct file_list * fl;
3194 struct url_actions * b;
3196 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3198 return cgi_error_disabled(csp, rsp);
3201 err = get_number_param(csp, parameters, "s", §ionid);
3207 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3210 /* No filename specified, can't read file, modified, or out of memory. */
3211 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3214 cur_line = file->lines;
3216 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3218 cur_line = cur_line->next;
3221 if ( (cur_line == NULL)
3222 || (line_number != sectionid)
3224 || (cur_line->type != FILE_LINE_ACTION))
3226 /* Invalid "sectionid" parameter */
3227 edit_free_file(file);
3228 return JB_ERR_CGI_PARAMS;
3231 get_string_param(parameters, "p", &action_set_name);
3232 if (action_set_name != NULL)
3234 for (index = 0; index < MAX_ACTION_FILES; index++)
3236 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3238 for (b = b->next; NULL != b; b = b->next)
3240 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3242 copy_action(cur_line->data.action, b->action);
3248 edit_free_file(file);
3249 return JB_ERR_CGI_PARAMS;
3255 err = actions_from_radio(parameters, cur_line->data.action);
3261 edit_free_file(file);
3265 ch = get_char_param(parameters, "filter_all");
3268 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3269 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3270 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3274 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3277 for (index = 0; !err; index++)
3284 /* Generate the keys */
3285 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3286 key_value[sizeof(key_value) - 1] = '\0';
3287 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3288 key_name[sizeof(key_name) - 1] = '\0';
3290 err = get_string_param(parameters, key_name, &name);
3299 value = get_char_param(parameters, key_value);
3302 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3303 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3304 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3306 else if (value == 'N')
3308 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3309 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3311 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3312 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3315 else if (value == 'X')
3317 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3318 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3325 edit_free_file(file);
3329 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3332 edit_free_file(file);
3333 return JB_ERR_MEMORY;
3336 len = strlen(actiontext);
3340 * Empty action - must special-case this.
3341 * Simply setting len to 1 is sufficient...
3346 if (NULL == (newtext = malloc(len + 2)))
3350 edit_free_file(file);
3351 return JB_ERR_MEMORY;
3353 strcpy(newtext, actiontext);
3357 newtext[len + 1] = '\0';
3359 freez(cur_line->raw);
3360 freez(cur_line->unprocessed);
3361 cur_line->unprocessed = newtext;
3363 err = edit_write_file(file);
3366 /* Error writing file */
3367 if (err == JB_ERR_FILE)
3369 /* Read-only file. */
3370 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3372 edit_free_file(file);
3376 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3377 string_append(&target, file->identifier);
3378 string_join(&target, section_target(sectionid));
3381 edit_free_file(file);
3386 return JB_ERR_MEMORY;
3389 rsp->status = strdup("302 Local Redirect from Privoxy");
3390 if (rsp->status == NULL)
3393 return JB_ERR_MEMORY;
3395 err = enlist_unique_header(rsp->headers, "Location", target);
3402 /*********************************************************************
3404 * Function : cgi_edit_actions_url
3406 * Description : CGI function that actually edits a URL pattern in
3410 * 1 : csp = Current client state (buffers, headers, etc...)
3411 * 2 : rsp = http_response data structure for output
3412 * 3 : parameters = map of cgi parameters
3415 * filename : Identifies the file to edit
3416 * ver : File's last-modified time
3417 * section : Line number of section to edit
3418 * pattern : Line number of pattern to edit
3419 * newval : New value for pattern
3421 * Returns : JB_ERR_OK on success
3422 * JB_ERR_MEMORY on out-of-memory
3423 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3424 * specified or not valid.
3426 *********************************************************************/
3427 jb_err cgi_edit_actions_url(struct client_state *csp,
3428 struct http_response *rsp,
3429 const struct map *parameters)
3433 struct editable_file * file;
3434 struct file_line * cur_line;
3435 unsigned line_number;
3436 unsigned section_start_line_number = 0;
3440 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3442 return cgi_error_disabled(csp, rsp);
3445 err = get_number_param(csp, parameters, "p", &patternid);
3452 return JB_ERR_CGI_PARAMS;
3455 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3461 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3464 /* No filename specified, can't read file, modified, or out of memory. */
3466 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3470 cur_line = file->lines;
3472 while ((cur_line != NULL) && (line_number < patternid))
3474 if (cur_line->type == FILE_LINE_ACTION)
3476 section_start_line_number = line_number;
3478 cur_line = cur_line->next;
3482 if ( (cur_line == NULL)
3483 || (cur_line->type != FILE_LINE_URL))
3485 /* Invalid "patternid" parameter */
3487 edit_free_file(file);
3488 return JB_ERR_CGI_PARAMS;
3491 /* At this point, the line to edit is in cur_line */
3493 freez(cur_line->raw);
3494 freez(cur_line->unprocessed);
3495 cur_line->unprocessed = new_pattern;
3497 err = edit_write_file(file);
3500 /* Error writing file */
3501 if (err == JB_ERR_FILE)
3503 /* Read-only file. */
3504 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3506 edit_free_file(file);
3510 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3511 string_append(&target, file->identifier);
3512 string_join(&target, section_target(section_start_line_number));
3514 edit_free_file(file);
3519 return JB_ERR_MEMORY;
3522 rsp->status = strdup("302 Local Redirect from Privoxy");
3523 if (rsp->status == NULL)
3526 return JB_ERR_MEMORY;
3528 err = enlist_unique_header(rsp->headers, "Location", target);
3535 /*********************************************************************
3537 * Function : cgi_edit_actions_add_url
3539 * Description : CGI function that actually adds a URL pattern to
3543 * 1 : csp = Current client state (buffers, headers, etc...)
3544 * 2 : rsp = http_response data structure for output
3545 * 3 : parameters = map of cgi parameters
3548 * filename : Identifies the file to edit
3549 * ver : File's last-modified time
3550 * section : Line number of section to edit
3551 * newval : New pattern
3553 * Returns : JB_ERR_OK on success
3554 * JB_ERR_MEMORY on out-of-memory
3555 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3556 * specified or not valid.
3558 *********************************************************************/
3559 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3560 struct http_response *rsp,
3561 const struct map *parameters)
3565 struct file_line * new_line;
3566 struct editable_file * file;
3567 struct file_line * cur_line;
3568 unsigned line_number;
3572 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3574 return cgi_error_disabled(csp, rsp);
3577 err = get_number_param(csp, parameters, "s", §ionid);
3584 return JB_ERR_CGI_PARAMS;
3587 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3593 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3596 /* No filename specified, can't read file, modified, or out of memory. */
3598 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3602 cur_line = file->lines;
3604 while ((cur_line != NULL) && (line_number < sectionid))
3606 cur_line = cur_line->next;
3610 if ( (cur_line == NULL)
3611 || (cur_line->type != FILE_LINE_ACTION))
3613 /* Invalid "sectionid" parameter */
3615 edit_free_file(file);
3616 return JB_ERR_CGI_PARAMS;
3619 /* At this point, the section header is in cur_line - add after this. */
3621 /* Allocate the new line */
3622 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3623 if (new_line == NULL)
3626 edit_free_file(file);
3627 return JB_ERR_MEMORY;
3630 /* Fill in the data members of the new line */
3631 new_line->raw = NULL;
3632 new_line->prefix = NULL;
3633 new_line->unprocessed = new_pattern;
3634 new_line->type = FILE_LINE_URL;
3636 /* Link new_line into the list, after cur_line */
3637 new_line->next = cur_line->next;
3638 cur_line->next = new_line;
3640 /* Done making changes, now commit */
3642 err = edit_write_file(file);
3645 /* Error writing file */
3646 if (err == JB_ERR_FILE)
3648 /* Read-only file. */
3649 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3651 edit_free_file(file);
3655 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3656 string_append(&target, file->identifier);
3657 string_join(&target, section_target(sectionid));
3659 edit_free_file(file);
3664 return JB_ERR_MEMORY;
3667 rsp->status = strdup("302 Local Redirect from Privoxy");
3668 if (rsp->status == NULL)
3671 return JB_ERR_MEMORY;
3673 err = enlist_unique_header(rsp->headers, "Location", target);
3680 /*********************************************************************
3682 * Function : cgi_edit_actions_remove_url
3684 * Description : CGI function that actually removes a URL pattern from
3688 * 1 : csp = Current client state (buffers, headers, etc...)
3689 * 2 : rsp = http_response data structure for output
3690 * 3 : parameters = map of cgi parameters
3693 * f : (filename) Identifies the file to edit
3694 * v : (version) File's last-modified time
3695 * p : (pattern) Line number of pattern to remove
3697 * Returns : JB_ERR_OK on success
3698 * JB_ERR_MEMORY on out-of-memory
3699 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3700 * specified or not valid.
3702 *********************************************************************/
3703 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3704 struct http_response *rsp,
3705 const struct map *parameters)
3708 struct editable_file * file;
3709 struct file_line * cur_line;
3710 struct file_line * prev_line;
3711 unsigned line_number;
3712 unsigned section_start_line_number = 0;
3716 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3718 return cgi_error_disabled(csp, rsp);
3721 err = get_number_param(csp, parameters, "p", &patternid);
3727 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3730 /* No filename specified, can't read file, modified, or out of memory. */
3731 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3736 cur_line = file->lines;
3738 while ((cur_line != NULL) && (line_number < patternid))
3740 if (cur_line->type == FILE_LINE_ACTION)
3742 section_start_line_number = line_number;
3744 prev_line = cur_line;
3745 cur_line = cur_line->next;
3749 if ( (cur_line == NULL)
3750 || (prev_line == NULL)
3751 || (cur_line->type != FILE_LINE_URL))
3753 /* Invalid "patternid" parameter */
3754 edit_free_file(file);
3755 return JB_ERR_CGI_PARAMS;
3758 /* At this point, the line to remove is in cur_line, and the previous
3759 * one is in prev_line
3762 /* Unlink cur_line */
3763 prev_line->next = cur_line->next;
3764 cur_line->next = NULL;
3767 edit_free_file_lines(cur_line);
3769 err = edit_write_file(file);
3772 /* Error writing file */
3773 if (err == JB_ERR_FILE)
3775 /* Read-only file. */
3776 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3778 edit_free_file(file);
3782 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3783 string_append(&target, file->identifier);
3784 string_join(&target, section_target(section_start_line_number));
3786 edit_free_file(file);
3791 return JB_ERR_MEMORY;
3794 rsp->status = strdup("302 Local Redirect from Privoxy");
3795 if (rsp->status == NULL)
3798 return JB_ERR_MEMORY;
3800 err = enlist_unique_header(rsp->headers, "Location", target);
3807 /*********************************************************************
3809 * Function : cgi_edit_actions_section_remove
3811 * Description : CGI function that actually removes a whole section from
3812 * the actions file. The section must be empty first
3813 * (else JB_ERR_CGI_PARAMS).
3816 * 1 : csp = Current client state (buffers, headers, etc...)
3817 * 2 : rsp = http_response data structure for output
3818 * 3 : parameters = map of cgi parameters
3821 * f : (filename) Identifies the file to edit
3822 * v : (version) File's last-modified time
3823 * s : (section) Line number of section to edit
3825 * Returns : JB_ERR_OK on success
3826 * JB_ERR_MEMORY on out-of-memory
3827 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3828 * specified or not valid.
3830 *********************************************************************/
3831 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3832 struct http_response *rsp,
3833 const struct map *parameters)
3836 struct editable_file * file;
3837 struct file_line * cur_line;
3838 struct file_line * prev_line;
3839 unsigned line_number;
3843 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3845 return cgi_error_disabled(csp, rsp);
3848 err = get_number_param(csp, parameters, "s", §ionid);
3854 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3857 /* No filename specified, can't read file, modified, or out of memory. */
3858 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3862 cur_line = file->lines;
3865 while ((cur_line != NULL) && (line_number < sectionid))
3867 prev_line = cur_line;
3868 cur_line = cur_line->next;
3872 if ( (cur_line == NULL)
3873 || (cur_line->type != FILE_LINE_ACTION) )
3875 /* Invalid "sectionid" parameter */
3876 edit_free_file(file);
3877 return JB_ERR_CGI_PARAMS;
3880 if ( (cur_line->next != NULL)
3881 && (cur_line->next->type == FILE_LINE_URL) )
3883 /* Section not empty. */
3884 edit_free_file(file);
3885 return JB_ERR_CGI_PARAMS;
3888 /* At this point, the line to remove is in cur_line, and the previous
3889 * one is in prev_line
3892 /* Unlink cur_line */
3893 if (prev_line == NULL)
3895 /* Removing the first line from the file */
3896 file->lines = cur_line->next;
3900 prev_line->next = cur_line->next;
3902 cur_line->next = NULL;
3905 edit_free_file_lines(cur_line);
3907 err = edit_write_file(file);
3910 /* Error writing file */
3911 if (err == JB_ERR_FILE)
3913 /* Read-only file. */
3914 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3916 edit_free_file(file);
3920 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3921 string_append(&target, file->identifier);
3923 edit_free_file(file);
3928 return JB_ERR_MEMORY;
3931 rsp->status = strdup("302 Local Redirect from Privoxy");
3932 if (rsp->status == NULL)
3935 return JB_ERR_MEMORY;
3937 err = enlist_unique_header(rsp->headers, "Location", target);
3944 /*********************************************************************
3946 * Function : cgi_edit_actions_section_add
3948 * Description : CGI function that adds a new empty section to
3952 * 1 : csp = Current client state (buffers, headers, etc...)
3953 * 2 : rsp = http_response data structure for output
3954 * 3 : parameters = map of cgi parameters
3957 * f : (filename) Identifies the file to edit
3958 * v : (version) File's last-modified time
3959 * s : (section) Line number of section to add after, 0 for
3962 * Returns : JB_ERR_OK on success
3963 * JB_ERR_MEMORY on out-of-memory
3964 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3965 * specified or not valid.
3967 *********************************************************************/
3968 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3969 struct http_response *rsp,
3970 const struct map *parameters)
3973 struct file_line * new_line;
3975 struct editable_file * file;
3976 struct file_line * cur_line;
3977 unsigned line_number;
3981 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3983 return cgi_error_disabled(csp, rsp);
3986 err = get_number_param(csp, parameters, "s", §ionid);
3992 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3995 /* No filename specified, can't read file, modified, or out of memory. */
3996 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4000 cur_line = file->lines;
4004 /* Add to start of file */
4005 if (cur_line != NULL)
4007 /* There's something in the file, find the line before the first
4010 while ( (cur_line->next != NULL)
4011 && (cur_line->next->type != FILE_LINE_ACTION) )
4013 cur_line = cur_line->next;
4020 /* Add after stated section. */
4021 while ((cur_line != NULL) && (line_number < sectionid))
4023 cur_line = cur_line->next;
4027 if ( (cur_line == NULL)
4028 || (cur_line->type != FILE_LINE_ACTION))
4030 /* Invalid "sectionid" parameter */
4031 edit_free_file(file);
4032 return JB_ERR_CGI_PARAMS;
4035 /* Skip through the section to find the last line in it. */
4036 while ( (cur_line->next != NULL)
4037 && (cur_line->next->type != FILE_LINE_ACTION) )
4039 cur_line = cur_line->next;
4044 /* At this point, the last line in the previous section is in cur_line
4045 * - add after this. (Or if we need to add as the first line, cur_line
4049 new_text = strdup("{}");
4050 if (NULL == new_text)
4052 edit_free_file(file);
4053 return JB_ERR_MEMORY;
4056 /* Allocate the new line */
4057 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4058 if (new_line == NULL)
4061 edit_free_file(file);
4062 return JB_ERR_MEMORY;
4065 /* Fill in the data members of the new line */
4066 new_line->raw = NULL;
4067 new_line->prefix = NULL;
4068 new_line->unprocessed = new_text;
4069 new_line->type = FILE_LINE_ACTION;
4071 if (cur_line != NULL)
4073 /* Link new_line into the list, after cur_line */
4074 new_line->next = cur_line->next;
4075 cur_line->next = new_line;
4079 /* Link new_line into the list, as first line */
4080 new_line->next = file->lines;
4081 file->lines = new_line;
4084 /* Done making changes, now commit */
4086 err = edit_write_file(file);
4089 /* Error writing file */
4090 if (err == JB_ERR_FILE)
4092 /* Read-only file. */
4093 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4095 edit_free_file(file);
4099 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4100 string_append(&target, file->identifier);
4102 edit_free_file(file);
4107 return JB_ERR_MEMORY;
4110 rsp->status = strdup("302 Local Redirect from Privoxy");
4111 if (rsp->status == NULL)
4114 return JB_ERR_MEMORY;
4116 err = enlist_unique_header(rsp->headers, "Location", target);
4123 /*********************************************************************
4125 * Function : cgi_edit_actions_section_swap
4127 * Description : CGI function that swaps the order of two sections
4128 * in the actions file. Note that this CGI can actually
4129 * swap any two arbitrary sections, but the GUI interface
4130 * currently only allows consecutive sections to be
4134 * 1 : csp = Current client state (buffers, headers, etc...)
4135 * 2 : rsp = http_response data structure for output
4136 * 3 : parameters = map of cgi parameters
4139 * f : (filename) Identifies the file to edit
4140 * v : (version) File's last-modified time
4141 * s1 : (section1) Line number of first section to swap
4142 * s2 : (section2) Line number of second section to swap
4144 * Returns : JB_ERR_OK on success
4145 * JB_ERR_MEMORY on out-of-memory
4146 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4147 * specified or not valid.
4149 *********************************************************************/
4150 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4151 struct http_response *rsp,
4152 const struct map *parameters)
4156 struct editable_file * file;
4157 struct file_line * cur_line;
4158 struct file_line * prev_line;
4159 struct file_line * line_before_section1;
4160 struct file_line * line_start_section1;
4161 struct file_line * line_end_section1;
4162 struct file_line * line_after_section1;
4163 struct file_line * line_before_section2;
4164 struct file_line * line_start_section2;
4165 struct file_line * line_end_section2;
4166 struct file_line * line_after_section2;
4167 unsigned line_number;
4171 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4173 return cgi_error_disabled(csp, rsp);
4176 err = get_number_param(csp, parameters, "s1", §ion1);
4177 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4183 if (section1 > section2)
4185 unsigned temp = section2;
4186 section2 = section1;
4190 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4193 /* No filename specified, can't read file, modified, or out of memory. */
4194 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4197 /* Start at the beginning... */
4199 cur_line = file->lines;
4202 /* ... find section1 ... */
4203 while ((cur_line != NULL) && (line_number < section1))
4205 prev_line = cur_line;
4206 cur_line = cur_line->next;
4210 if ( (cur_line == NULL)
4211 || (cur_line->type != FILE_LINE_ACTION) )
4213 /* Invalid "section1" parameter */
4214 edit_free_file(file);
4215 return JB_ERR_CGI_PARAMS;
4218 /* If no-op, we've validated params and can skip the rest. */
4219 if (section1 != section2)
4221 /* ... find the end of section1 ... */
4222 line_before_section1 = prev_line;
4223 line_start_section1 = cur_line;
4226 prev_line = cur_line;
4227 cur_line = cur_line->next;
4230 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4231 line_end_section1 = prev_line;
4232 line_after_section1 = cur_line;
4234 /* ... find section2 ... */
4235 while ((cur_line != NULL) && (line_number < section2))
4237 prev_line = cur_line;
4238 cur_line = cur_line->next;
4242 if ( (cur_line == NULL)
4243 || (cur_line->type != FILE_LINE_ACTION) )
4245 /* Invalid "section2" parameter */
4246 edit_free_file(file);
4247 return JB_ERR_CGI_PARAMS;
4250 /* ... find the end of section2 ... */
4251 line_before_section2 = prev_line;
4252 line_start_section2 = cur_line;
4255 prev_line = cur_line;
4256 cur_line = cur_line->next;
4259 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4260 line_end_section2 = prev_line;
4261 line_after_section2 = cur_line;
4263 /* Now have all the pointers we need. Do the swap. */
4265 /* Change the pointer to section1 to point to section2 instead */
4266 if (line_before_section1 == NULL)
4268 file->lines = line_start_section2;
4272 line_before_section1->next = line_start_section2;
4275 if (line_before_section2 == line_end_section1)
4277 /* Consecutive sections */
4278 line_end_section2->next = line_start_section1;
4282 line_end_section2->next = line_after_section1;
4283 line_before_section2->next = line_start_section1;
4286 /* Set the pointer from the end of section1 to the rest of the file */
4287 line_end_section1->next = line_after_section2;
4289 err = edit_write_file(file);
4292 /* Error writing file */
4293 if (err == JB_ERR_FILE)
4295 /* Read-only file. */
4296 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4298 edit_free_file(file);
4301 } /* END if (section1 != section2) */
4303 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4304 string_append(&target, file->identifier);
4306 edit_free_file(file);
4311 return JB_ERR_MEMORY;
4314 rsp->status = strdup("302 Local Redirect from Privoxy");
4315 if (rsp->status == NULL)
4318 return JB_ERR_MEMORY;
4320 err = enlist_unique_header(rsp->headers, "Location", target);
4327 /*********************************************************************
4329 * Function : cgi_toggle
4331 * Description : CGI function that adds a new empty section to
4335 * 1 : csp = Current client state (buffers, headers, etc...)
4336 * 2 : rsp = http_response data structure for output
4337 * 3 : parameters = map of cgi parameters
4340 * set : If present, how to change toggle setting:
4341 * "enable", "disable", "toggle", or none (default).
4342 * mini : If present, use mini reply template.
4344 * Returns : JB_ERR_OK on success
4345 * JB_ERR_MEMORY on out-of-memory
4347 *********************************************************************/
4348 jb_err cgi_toggle(struct client_state *csp,
4349 struct http_response *rsp,
4350 const struct map *parameters)
4352 struct map *exports;
4354 const char *template_name;
4360 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4362 return cgi_error_disabled(csp, rsp);
4365 mode = get_char_param(parameters, "set");
4372 else if (mode == 'D')
4377 else if (mode == 'T')
4380 g_bToggleIJB = !g_bToggleIJB;
4383 if (NULL == (exports = default_exports(csp, "toggle")))
4385 return JB_ERR_MEMORY;
4388 template_name = (get_char_param(parameters, "mini")
4392 return template_fill_for_cgi(csp, template_name, exports, rsp);
4396 /*********************************************************************
4398 * Function : javascriptify
4400 * Description : Converts a string into a form JavaScript will like.
4402 * Netscape 4's JavaScript sucks - it doesn't use
4403 * "id" parameters, so you have to set the "name"
4404 * used to submit a form element to something JavaScript
4405 * will like. (Or access the elements by index in an
4406 * array. That array contains >60 elements and will
4407 * be changed whenever we add a new action to the
4408 * editor, so I'm NOT going to use indexes that have
4409 * to be figured out by hand.)
4411 * Currently the only thing we have to worry about
4412 * is "-" ==> "_" conversion.
4414 * This is a length-preserving operation so it is
4415 * carried out in-place, no memory is allocated
4419 * 1 : identifier = String to make JavaScript-friendly.
4423 *********************************************************************/
4424 static void javascriptify(char * identifier)
4426 char * p = identifier;
4427 while (NULL != (p = strchr(p, '-')))
4434 /*********************************************************************
4436 * Function : actions_to_radio
4438 * Description : Converts a actionsfile entry into settings for
4439 * radio buttons and edit boxes on a HTML form.
4442 * 1 : exports = List of substitutions to add to.
4443 * 2 : action = Action to read
4445 * Returns : JB_ERR_OK on success
4446 * JB_ERR_MEMORY on out-of-memory
4448 *********************************************************************/
4449 static jb_err actions_to_radio(struct map * exports,
4450 const struct action_spec *action)
4452 unsigned mask = action->mask;
4453 unsigned add = action->add;
4461 mask = action->mask;
4464 /* sanity - prevents "-feature +feature" */
4468 #define DEFINE_ACTION_BOOL(name, bit) \
4469 if (!(mask & bit)) \
4471 current_mode = 'n'; \
4473 else if (add & bit) \
4475 current_mode = 'y'; \
4479 current_mode = 'x'; \
4481 if (map_radio(exports, name, "ynx", current_mode)) \
4483 return JB_ERR_MEMORY; \
4486 #define DEFINE_ACTION_STRING(name, bit, index) \
4487 DEFINE_ACTION_BOOL(name, bit); \
4490 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4493 checked = !strcmp(action->string[index], value); \
4497 checked = is_default; \
4499 mapped_param |= checked; \
4500 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4502 return JB_ERR_MEMORY; \
4505 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4506 if (map(exports, name "-param-custom", 1, \
4507 ((!mapped_param) ? "checked" : ""), 1)) \
4509 return JB_ERR_MEMORY; \
4511 if (map(exports, name "-param", 1, \
4512 (((add & bit) && !mapped_param) ? \
4513 action->string[index] : default_val), 1)) \
4515 return JB_ERR_MEMORY; \
4518 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4519 if (map(exports, name "-param", 1, \
4520 ((add & bit) ? action->string[index] : default_val), 1)) \
4522 return JB_ERR_MEMORY; \
4525 #define DEFINE_ACTION_MULTI(name, index) \
4526 if (action->multi_add[index]->first) \
4528 current_mode = 'y'; \
4530 else if (action->multi_remove_all[index]) \
4532 current_mode = 'n'; \
4534 else if (action->multi_remove[index]->first) \
4536 current_mode = 'y'; \
4540 current_mode = 'x'; \
4542 if (map_radio(exports, name, "ynx", current_mode)) \
4544 return JB_ERR_MEMORY; \
4547 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4549 #include "actionlist.h"
4551 #undef DEFINE_ACTION_MULTI
4552 #undef DEFINE_ACTION_STRING
4553 #undef DEFINE_ACTION_BOOL
4554 #undef DEFINE_ACTION_ALIAS
4555 #undef DEFINE_CGI_PARAM_CUSTOM
4556 #undef DEFINE_CGI_PARAM_RADIO
4557 #undef DEFINE_CGI_PARAM_NO_RADIO
4563 /*********************************************************************
4565 * Function : actions_from_radio
4567 * Description : Converts a map of parameters passed to a CGI function
4568 * into an actionsfile entry.
4571 * 1 : parameters = parameters to the CGI call
4572 * 2 : action = Action to change. Must be valid before
4573 * the call, actions not specified will be
4576 * Returns : JB_ERR_OK on success
4577 * JB_ERR_MEMORY on out-of-memory
4579 *********************************************************************/
4580 static jb_err actions_from_radio(const struct map * parameters,
4581 struct action_spec *action)
4583 static int first_time = 1;
4587 const char * js_name;
4588 jb_err err = JB_ERR_OK;
4593 /* Statics are generally a potential race condition,
4594 * but in this case we're safe and don't need semaphores.
4595 * Be careful if you modify this function.
4599 #define JAVASCRIPTIFY(dest_var, string) \
4601 static char js_name_arr[] = string; \
4604 javascriptify(js_name_arr); \
4606 dest_var = js_name_arr; \
4609 #define DEFINE_ACTION_BOOL(name, bit) \
4610 JAVASCRIPTIFY(js_name, name); \
4611 ch = get_char_param(parameters, js_name); \
4614 action->add |= bit; \
4615 action->mask |= bit; \
4617 else if (ch == 'N') \
4619 action->add &= ~bit; \
4620 action->mask &= ~bit; \
4622 else if (ch == 'X') \
4624 action->add &= ~bit; \
4625 action->mask |= bit; \
4628 #define DEFINE_ACTION_STRING(name, bit, index) \
4629 JAVASCRIPTIFY(js_name, name); \
4630 ch = get_char_param(parameters, js_name); \
4634 JAVASCRIPTIFY(js_name, name "-mode"); \
4635 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4636 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4638 JAVASCRIPTIFY(js_name, name "-param"); \
4639 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4641 if (param != NULL) \
4643 if (NULL == (param_dup = strdup(param))) \
4645 return JB_ERR_MEMORY; \
4647 freez(action->string[index]); \
4648 action->add |= bit; \
4649 action->mask |= bit; \
4650 action->string[index] = param_dup; \
4653 else if (ch == 'N') \
4655 if (action->add & bit) \
4657 freez(action->string[index]); \
4659 action->add &= ~bit; \
4660 action->mask &= ~bit; \
4662 else if (ch == 'X') \
4664 if (action->add & bit) \
4666 freez(action->string[index]); \
4668 action->add &= ~bit; \
4669 action->mask |= bit; \
4672 #define DEFINE_ACTION_MULTI(name, index) \
4673 JAVASCRIPTIFY(js_name, name); \
4674 ch = get_char_param(parameters, js_name); \
4679 else if (ch == 'N') \
4681 list_remove_all(action->multi_add[index]); \
4682 list_remove_all(action->multi_remove[index]); \
4683 action->multi_remove_all[index] = 1; \
4685 else if (ch == 'X') \
4687 list_remove_all(action->multi_add[index]); \
4688 list_remove_all(action->multi_remove[index]); \
4689 action->multi_remove_all[index] = 0; \
4692 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4694 #include "actionlist.h"
4696 #undef DEFINE_ACTION_MULTI
4697 #undef DEFINE_ACTION_STRING
4698 #undef DEFINE_ACTION_BOOL
4699 #undef DEFINE_ACTION_ALIAS
4700 #undef JAVASCRIPTIFY
4708 #endif /* def FEATURE_CGI_EDIT_ACTIONS */