1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 2.1 2002/09/04 15:21:18 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa//current/src/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 2.1 2002/09/04 15:21:18 oes
46 * Synced with the stable branch:
47 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
48 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
50 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
51 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
53 * Revision 2.0 2002/06/04 14:34:21 jongfoster
54 * Moving source files to src/
56 * Revision 1.41 2002/05/21 19:09:45 oes
57 * - Made Add/Edit/Remove URL Submit and Cancel
58 * buttons jump back to relevant section in eal
59 * - Bugfix: remove-url-form needs p export
61 * Revision 1.40 2002/05/19 11:34:35 jongfoster
62 * Handling read-only actions files better - report the actual
63 * error, not "Out of memory"!
66 * http://sourceforge.net/tracker/index.php?func=detail
67 * &aid=557905&group_id=11118&atid=111118
69 * Revision 1.39 2002/05/12 21:39:15 jongfoster
70 * - Adding Doxygen-style comments to structures and #defines.
71 * - Correcting function comments
73 * Revision 1.38 2002/05/03 23:00:38 jongfoster
74 * Support for templates for "standard actions" buttons.
77 * Revision 1.37 2002/04/30 11:14:52 oes
78 * Made csp the first parameter in *action_to_html
80 * Revision 1.36 2002/04/26 21:53:30 jongfoster
81 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
83 * Revision 1.35 2002/04/26 21:50:02 jongfoster
84 * Honouring default exports in edit-actions-for-url-filter template.
86 * Revision 1.34 2002/04/26 12:54:17 oes
87 * Adaptions to changes in actions.c
89 * Revision 1.33 2002/04/24 02:17:47 oes
90 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
92 * - Activated Jon's code for editing multiple AFs
93 * - cgi_edit_list_actions now provides context-sensitive
94 * help, looks up all action sets from standard.action and
95 * makes buttons for them in the catchall section
96 * - cgi_edit_action_submit now honors a p parameter, looks up
97 * the corresponding action set, and sets the catchall pattern's
98 * actions accordingly.
100 * Revision 1.32 2002/04/19 16:55:31 jongfoster
101 * Fixing newline problems. If we do our own text file newline
102 * mangling, we don't want the library to do any, so we need to
103 * open the files in *binary* mode.
105 * Revision 1.31 2002/04/18 19:21:08 jongfoster
106 * Added code to detect "conventional" action files, that start
107 * with a set of actions for all URLs (the pattern "/").
108 * These are special-cased in the "edit-actions-list" CGI, so
109 * that a special UI can be written for them.
111 * Revision 1.30 2002/04/10 13:38:35 oes
112 * load_template signature changed
114 * Revision 1.29 2002/04/08 16:59:08 oes
117 * Revision 1.28 2002/03/27 12:30:29 oes
118 * Deleted unsused variable
120 * Revision 1.27 2002/03/26 23:06:04 jongfoster
121 * Removing duplicate @ifs on the toggle page
123 * Revision 1.26 2002/03/26 22:59:17 jongfoster
124 * Fixing /toggle to display status consistently.
126 * Revision 1.25 2002/03/26 22:29:54 swa
127 * we have a new homepage!
129 * Revision 1.24 2002/03/24 15:23:33 jongfoster
132 * Revision 1.23 2002/03/24 13:32:41 swa
133 * name change related issues
135 * Revision 1.22 2002/03/24 13:25:43 swa
136 * name change related issues
138 * Revision 1.21 2002/03/22 18:02:48 jongfoster
139 * Fixing remote toggle
141 * Revision 1.20 2002/03/16 20:28:34 oes
142 * Added descriptions to the filters so users will know what they select in the cgi editor
144 * Revision 1.19 2002/03/16 18:38:14 jongfoster
145 * Stopping stupid or malicious users from breaking the actions
146 * file using the web-based editor.
148 * Revision 1.18 2002/03/16 14:57:44 jongfoster
149 * Full support for enabling/disabling modular filters.
151 * Revision 1.17 2002/03/16 14:26:42 jongfoster
152 * First version of modular filters support - READ ONLY!
153 * Fixing a double-free bug in the out-of-memory handling in map_radio().
155 * Revision 1.16 2002/03/07 03:46:17 oes
156 * Fixed compiler warnings
158 * Revision 1.15 2002/03/06 22:54:35 jongfoster
159 * Automated function-comment nitpicking.
161 * Revision 1.14 2002/03/05 00:24:51 jongfoster
162 * Patch to always edit the current actions file.
164 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
165 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
167 * Revision 1.12 2002/03/03 09:18:03 joergs
168 * Made jumbjuster work on AmigaOS again.
170 * Revision 1.11 2002/01/23 01:03:31 jongfoster
171 * Fixing gcc [CygWin] compiler warnings
173 * Revision 1.10 2002/01/23 00:22:59 jongfoster
174 * Adding new function cgi_edit_actions_section_swap(), to reorder
177 * Adding get_url_spec_param() to get a validated URL pattern.
179 * Moving edit_read_line() out of this file and into loaders.c.
181 * Adding missing html_encode() to many CGI functions.
183 * Moving the functions that #include actionlist.h to the end of the file,
184 * because the Visual C++ 97 debugger gets extremely confused if you try
185 * to debug any code that comes after them in the file.
187 * Major optimizations in cgi_edit_actions_list() to reduce the size of
188 * the generated HTML (down 40% from 550k to 304k), with major side-effects
189 * throughout the editor and templates. In particular, the length of the
190 * URLs throughout the editor has been drastically reduced, by cutting
191 * paramater names down to 1 character and CGI names down to 3-4
192 * characters, by removing all non-essential CGI paramaters even at the
193 * expense of having to re-read the actions file for the most trivial
194 * page, and by using relative rather than absolute URLs. This means
195 * that this (typical example):
197 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
198 * filename=ijb&ver=1011487572&section=12&pattern=13
199 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
203 * <a href="eau?f=ijb&v=1011487572&p=13">
205 * Revision 1.9 2002/01/17 20:56:22 jongfoster
206 * Replacing hard references to the URL of the config interface
207 * with #defines from project.h
209 * Revision 1.8 2001/11/30 23:35:51 jongfoster
210 * Renaming actionsfile to ijb.action
212 * Revision 1.7 2001/11/13 00:28:24 jongfoster
213 * - Renaming parameters from edit-actions-for-url so that they only
214 * contain legal JavaScript characters. If we wanted to write
215 * JavaScript that worked with Netscape 4, this is nessacery.
216 * (Note that at the moment the JavaScript doesn't actually work
217 * with Netscape 4, but now this is purely a template issue, not
218 * one affecting code).
219 * - Adding new CGIs for use by non-JavaScript browsers:
220 * edit-actions-url-form
221 * edit-actions-add-url-form
222 * edit-actions-remove-url-form
225 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
226 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
227 * by and __OS2__ ifdef.
229 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
230 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
231 * threads to call select() simultaneously. So, it's time to do a real, live,
232 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
233 * (native). Both versions will work, but using __OS2__ offers multi-threading.
235 * Revision 1.4 2001/10/23 21:48:19 jongfoster
236 * Cleaning up error handling in CGI functions - they now send back
237 * a HTML error page and should never cause a FATAL error. (Fixes one
238 * potential source of "denial of service" attacks).
240 * CGI actions file editor that works and is actually useful.
242 * Ability to toggle JunkBuster remotely using a CGI call.
244 * You can turn off both the above features in the main configuration
245 * file, e.g. if you are running a multi-user proxy.
247 * Revision 1.3 2001/10/14 22:12:49 jongfoster
248 * New version of CGI-based actionsfile editor.
249 * Major changes, including:
250 * - Completely new file parser and file output routines
251 * - edit-actions CGI renamed edit-actions-for-url
252 * - All CGIs now need a filename parameter, except for...
253 * - New CGI edit-actions which doesn't need a filename,
254 * to allow you to start the editor up.
255 * - edit-actions-submit now works, and now automatically
256 * redirects you back to the main edit-actions-list handler.
258 * Revision 1.2 2001/09/16 17:05:14 jongfoster
259 * Removing unused #include showarg.h
261 * Revision 1.1 2001/09/16 15:47:37 jongfoster
262 * First version of CGI-based edit interface. This is very much a
263 * work-in-progress, and you can't actually use it to edit anything
264 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
265 * to have any effect.
268 **********************************************************************/
274 * FIXME: Following includes copied from cgi.c - which are actually needed?
279 #include <sys/types.h>
283 #include <sys/stat.h>
286 #define snprintf _snprintf
287 #endif /* def _WIN32 */
292 #include "cgisimple.h"
296 #include "miscutil.h"
300 /* loadcfg.h is for g_bToggleIJB only */
301 #include "urlmatch.h"
303 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
306 #ifdef FEATURE_CGI_EDIT_ACTIONS
309 * A line in an editable_file.
313 /** Next entry in the linked list */
314 struct file_line * next;
316 /** The raw data, to write out if this line is unmodified. */
319 /** Comments and/or whitespace to put before this line if it's modified
320 and then written out. */
323 /** The actual data, as a string. Line continuation and comment removal
324 are performed on the data read from file before it's stored here, so
325 it will be a single line of data. */
328 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
331 /** The actual data, processed into some sensible data type. */
335 /** An action specification. */
336 struct action_spec action[1];
338 /** A name=value pair. */
342 /** The name in the name=value pair. */
345 /** The value in the name=value pair, as a string. */
348 /** The value in the name=value pair, as an integer. */
353 /* Add more data types here... e.g.
356 struct url_spec url[1];
360 struct action_spec action[1];
370 /** This file_line has not been processed yet. */
371 #define FILE_LINE_UNPROCESSED 1
373 /** This file_line is blank. Can only appear at the end of a file, due to
374 the way the parser works. */
375 #define FILE_LINE_BLANK 2
377 /** This file_line says {{alias}}. */
378 #define FILE_LINE_ALIAS_HEADER 3
380 /** This file_line defines an alias. */
381 #define FILE_LINE_ALIAS_ENTRY 4
383 /** This file_line defines an {action}. */
384 #define FILE_LINE_ACTION 5
386 /** This file_line specifies a URL pattern. */
387 #define FILE_LINE_URL 6
389 /** This file_line says {{settings}}. */
390 #define FILE_LINE_SETTINGS_HEADER 7
392 /** This file_line is in a {{settings}} block. */
393 #define FILE_LINE_SETTINGS_ENTRY 8
395 /** This file_line says {{description}}. */
396 #define FILE_LINE_DESCRIPTION_HEADER 9
398 /** This file_line is in a {{description}} block. */
399 #define FILE_LINE_DESCRIPTION_ENTRY 10
403 * A configuration file, in a format that can be edited and written back to
408 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
409 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
410 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
411 /**< Pre-encoded with url_encode() for ease of use. */
412 const char * version_str; /**< Last modification time, as a string. For CGI param. */
413 /**< Can be used in URL without using url_param(). */
414 unsigned version; /**< Last modification time - prevents chaos with
415 the browser's "back" button. Note that this is a
416 time_t cast to an unsigned. When comparing, always
417 cast the time_t to an unsigned, and *NOT* vice-versa.
418 This may lose the top few bits, but they're not
419 significant anyway. */
420 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
421 Note that changing this after the file has been
422 read in will cause a mess. */
423 struct file_line * parse_error; /**< On parse error, this is the offending line. */
424 const char * parse_error_text; /**< On parse error, this is the problem.
425 (Statically allocated) */
428 /* FIXME: Following non-static functions should be prototyped in .h or made static */
430 /* Functions to read and write arbitrary config files */
431 jb_err edit_read_file(struct client_state *csp,
432 const struct map *parameters,
435 struct editable_file **pfile);
436 jb_err edit_write_file(struct editable_file * file);
437 void edit_free_file(struct editable_file * file);
439 /* Functions to read and write actions files */
440 jb_err edit_parse_actions_file(struct editable_file * file);
441 jb_err edit_read_actions_file(struct client_state *csp,
442 struct http_response *rsp,
443 const struct map *parameters,
445 struct editable_file **pfile);
448 jb_err cgi_error_modified(struct client_state *csp,
449 struct http_response *rsp,
450 const char *filename);
451 jb_err cgi_error_parse(struct client_state *csp,
452 struct http_response *rsp,
453 struct editable_file *file);
454 jb_err cgi_error_file(struct client_state *csp,
455 struct http_response *rsp,
456 const char *filename);
457 jb_err cgi_error_file_read_only(struct client_state *csp,
458 struct http_response *rsp,
459 const char *filename);
460 jb_err cgi_error_disabled(struct client_state *csp,
461 struct http_response *rsp);
463 /* Internal arbitrary config file support functions */
464 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
465 static void edit_free_file_lines(struct file_line * first_line);
467 /* Internal actions file support functions */
468 static int match_actions_file_header_line(const char * line, const char * name);
469 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
471 /* Internal parameter parsing functions */
472 static jb_err get_file_name_param(struct client_state *csp,
473 const struct map *parameters,
474 const char *param_name,
477 const char **pparam);
479 static jb_err get_url_spec_param(struct client_state *csp,
480 const struct map *parameters,
485 /* Internal actionsfile <==> HTML conversion functions */
486 static jb_err map_radio(struct map * exports,
487 const char * optionname,
490 static jb_err actions_to_radio(struct map * exports,
491 const struct action_spec *action);
492 static jb_err actions_from_radio(const struct map * parameters,
493 struct action_spec *action);
496 static jb_err map_copy_parameter_html(struct map *out,
497 const struct map *in,
499 #if 0 /* unused function */
500 static jb_err map_copy_parameter_url(struct map *out,
501 const struct map *in,
503 #endif /* unused function */
505 /* Internal convenience functions */
506 static char *section_target(const unsigned sectionid);
508 /*********************************************************************
510 * Function : section_target
512 * Description : Given an unsigned (section id) n, produce a dynamically
513 * allocated string of the form #l<n>, for use in link
517 * 1 : sectionid = start line number of section
519 * Returns : String with link target, or NULL if out of
522 *********************************************************************/
523 static char *section_target(const unsigned sectionid)
527 snprintf(buf, 30, "#l%d", sectionid);
533 /*********************************************************************
535 * Function : map_copy_parameter_html
537 * Description : Copy a CGI parameter from one map to another, HTML
541 * 1 : out = target map
542 * 2 : in = source map
543 * 3 : name = name of cgi parameter to copy
545 * Returns : JB_ERR_OK on success
546 * JB_ERR_MEMORY on out-of-memory
547 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
550 *********************************************************************/
551 static jb_err map_copy_parameter_html(struct map *out,
552 const struct map *in,
562 value = lookup(in, name);
563 err = map(out, name, 1, html_encode(value), 0);
570 else if (*value == '\0')
572 return JB_ERR_CGI_PARAMS;
581 #if 0 /* unused function */
582 /*********************************************************************
584 * Function : map_copy_parameter_url
586 * Description : Copy a CGI parameter from one map to another, URL
590 * 1 : out = target map
591 * 2 : in = source map
592 * 3 : name = name of cgi parameter to copy
594 * Returns : JB_ERR_OK on success
595 * JB_ERR_MEMORY on out-of-memory
596 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
599 *********************************************************************/
600 static jb_err map_copy_parameter_url(struct map *out,
601 const struct map *in,
611 value = lookup(in, name);
612 err = map(out, name, 1, url_encode(value), 0);
619 else if (*value == '\0')
621 return JB_ERR_CGI_PARAMS;
628 #endif /* 0 - unused function */
630 /*********************************************************************
632 * Function : cgi_edit_actions_url_form
634 * Description : CGI function that displays a form for
638 * 1 : csp = Current client state (buffers, headers, etc...)
639 * 2 : rsp = http_response data structure for output
640 * 3 : parameters = map of cgi parameters
643 * f : (filename) Identifies the file to edit
644 * v : (version) File's last-modified time
645 * p : (pattern) Line number of pattern to edit
647 * Returns : JB_ERR_OK on success
648 * JB_ERR_MEMORY on out-of-memory
649 * JB_ERR_CGI_PARAMS if the CGI parameters are not
650 * specified or not valid.
652 *********************************************************************/
653 jb_err cgi_edit_actions_url_form(struct client_state *csp,
654 struct http_response *rsp,
655 const struct map *parameters)
657 struct map * exports;
659 struct editable_file * file;
660 struct file_line * cur_line;
661 unsigned line_number;
662 unsigned section_start_line_number = 0;
669 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
671 return cgi_error_disabled(csp, rsp);
674 err = get_number_param(csp, parameters, "p", &patternid);
680 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
683 /* No filename specified, can't read file, modified, or out of memory. */
684 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
687 cur_line = file->lines;
689 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
691 if (cur_line->type == FILE_LINE_ACTION)
693 section_start_line_number = line_number;
695 cur_line = cur_line->next;
698 if ( (cur_line == NULL)
699 || (line_number != patternid)
701 || (cur_line->type != FILE_LINE_URL))
703 /* Invalid "patternid" parameter */
704 edit_free_file(file);
705 return JB_ERR_CGI_PARAMS;
708 if (NULL == (exports = default_exports(csp, NULL)))
710 edit_free_file(file);
711 return JB_ERR_MEMORY;
714 err = map(exports, "f", 1, file->identifier, 1);
715 if (!err) err = map(exports, "v", 1, file->version_str, 1);
716 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
717 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
718 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
720 edit_free_file(file);
728 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
732 /*********************************************************************
734 * Function : cgi_edit_actions_add_url_form
736 * Description : CGI function that displays a form for
740 * 1 : csp = Current client state (buffers, headers, etc...)
741 * 2 : rsp = http_response data structure for output
742 * 3 : parameters = map of cgi parameters
745 * f : (filename) Identifies the file to edit
746 * v : (version) File's last-modified time
747 * s : (section) Line number of section to edit
749 * Returns : JB_ERR_OK on success
750 * JB_ERR_MEMORY on out-of-memory
751 * JB_ERR_CGI_PARAMS if the CGI parameters are not
752 * specified or not valid.
754 *********************************************************************/
755 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
756 struct http_response *rsp,
757 const struct map *parameters)
766 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
768 return cgi_error_disabled(csp, rsp);
771 if (NULL == (exports = default_exports(csp, NULL)))
773 return JB_ERR_MEMORY;
776 err = map_copy_parameter_html(exports, parameters, "f");
777 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
778 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
786 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
790 /*********************************************************************
792 * Function : cgi_edit_actions_remove_url_form
794 * Description : CGI function that displays a form for
798 * 1 : csp = Current client state (buffers, headers, etc...)
799 * 2 : rsp = http_response data structure for output
800 * 3 : parameters = map of cgi parameters
803 * f : (filename) Identifies the file to edit
804 * v : (version) File's last-modified time
805 * p : (pattern) Line number of pattern to edit
807 * Returns : JB_ERR_OK on success
808 * JB_ERR_MEMORY on out-of-memory
809 * JB_ERR_CGI_PARAMS if the CGI parameters are not
810 * specified or not valid.
812 *********************************************************************/
813 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
814 struct http_response *rsp,
815 const struct map *parameters)
817 struct map * exports;
819 struct editable_file * file;
820 struct file_line * cur_line;
821 unsigned line_number;
822 unsigned section_start_line_number = 0;
829 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
831 return cgi_error_disabled(csp, rsp);
834 err = get_number_param(csp, parameters, "p", &patternid);
840 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
843 /* No filename specified, can't read file, modified, or out of memory. */
844 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
847 cur_line = file->lines;
849 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
851 if (cur_line->type == FILE_LINE_ACTION)
853 section_start_line_number = line_number;
855 cur_line = cur_line->next;
858 if ( (cur_line == NULL)
859 || (line_number != patternid)
861 || (cur_line->type != FILE_LINE_URL))
863 /* Invalid "patternid" parameter */
864 edit_free_file(file);
865 return JB_ERR_CGI_PARAMS;
868 if (NULL == (exports = default_exports(csp, NULL)))
870 edit_free_file(file);
871 return JB_ERR_MEMORY;
874 err = map(exports, "f", 1, file->identifier, 1);
875 if (!err) err = map(exports, "v", 1, file->version_str, 1);
876 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
877 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
878 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
880 edit_free_file(file);
888 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
892 /*********************************************************************
894 * Function : edit_write_file
896 * Description : Write a complete file to disk.
899 * 1 : file = File to write.
901 * Returns : JB_ERR_OK on success
902 * JB_ERR_FILE on error writing to file.
903 * JB_ERR_MEMORY on out of memory
905 *********************************************************************/
906 jb_err edit_write_file(struct editable_file * file)
909 struct file_line * cur_line;
910 struct stat statbuf[1];
911 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
912 digits in time_t, assuming this is a 64-bit
913 machine, plus null terminator, plus one
917 assert(file->filename);
919 if (NULL == (fp = fopen(file->filename, "wb")))
924 cur_line = file->lines;
925 while (cur_line != NULL)
929 if (fputs(cur_line->raw, fp) < 0)
937 if (cur_line->prefix)
939 if (fputs(cur_line->prefix, fp) < 0)
945 if (cur_line->unprocessed)
948 if (NULL != strchr(cur_line->unprocessed, '#'))
950 /* Must quote '#' characters */
957 /* Count number of # characters, so we know length of output string */
958 src = cur_line->unprocessed;
959 while (NULL != (src = strchr(src, '#')))
966 /* Allocate new memory for string */
967 len = strlen(cur_line->unprocessed);
968 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
970 /* Uh oh, just trashed file! */
972 return JB_ERR_MEMORY;
975 /* Loop through string from end */
976 src = cur_line->unprocessed + len;
977 dest = str + len + numhash;
978 for ( ; len >= 0; len--)
980 if ((*dest-- = *src--) == '#')
984 assert(numhash >= 0);
987 assert(numhash == 0);
988 assert(src + 1 == cur_line->unprocessed);
989 assert(dest + 1 == str);
991 if (fputs(str, fp) < 0)
1002 /* Can write without quoting '#' characters. */
1003 if (fputs(cur_line->unprocessed, fp) < 0)
1009 if (fputs(NEWLINE(file->newline), fp) < 0)
1017 /* FIXME: Write data from file->data->whatever */
1021 cur_line = cur_line->next;
1027 /* Update the version stamp in the file structure, since we just
1028 * wrote to the file & changed it's date.
1030 if (stat(file->filename, statbuf) < 0)
1032 /* Error, probably file not found. */
1035 file->version = (unsigned)statbuf->st_mtime;
1037 /* Correct file->version_str */
1038 freez(file->version_str);
1039 snprintf(version_buf, 22, "%u", file->version);
1040 version_buf[21] = '\0';
1041 file->version_str = strdup(version_buf);
1042 if (version_buf == NULL)
1044 return JB_ERR_MEMORY;
1051 /*********************************************************************
1053 * Function : edit_free_file
1055 * Description : Free a complete file in memory.
1058 * 1 : file = Data structure to free.
1062 *********************************************************************/
1063 void edit_free_file(struct editable_file * file)
1067 /* Silently ignore NULL pointer */
1071 edit_free_file_lines(file->lines);
1072 freez(file->filename);
1073 freez(file->identifier);
1074 freez(file->version_str);
1076 file->parse_error_text = NULL; /* Statically allocated */
1077 file->parse_error = NULL;
1083 /*********************************************************************
1085 * Function : edit_free_file_lines
1087 * Description : Free an entire linked list of file lines.
1090 * 1 : first_line = Data structure to free.
1094 *********************************************************************/
1095 static void edit_free_file_lines(struct file_line * first_line)
1097 struct file_line * next_line;
1099 while (first_line != NULL)
1101 next_line = first_line->next;
1102 first_line->next = NULL;
1103 freez(first_line->raw);
1104 freez(first_line->prefix);
1105 freez(first_line->unprocessed);
1106 switch(first_line->type)
1108 case 0: /* special case if memory zeroed */
1109 case FILE_LINE_UNPROCESSED:
1110 case FILE_LINE_BLANK:
1111 case FILE_LINE_ALIAS_HEADER:
1112 case FILE_LINE_SETTINGS_HEADER:
1113 case FILE_LINE_DESCRIPTION_HEADER:
1114 case FILE_LINE_DESCRIPTION_ENTRY:
1115 case FILE_LINE_ALIAS_ENTRY:
1117 /* No data is stored for these */
1120 case FILE_LINE_ACTION:
1121 free_action(first_line->data.action);
1124 case FILE_LINE_SETTINGS_ENTRY:
1125 freez(first_line->data.setting.name);
1126 freez(first_line->data.setting.svalue);
1129 /* Should never happen */
1133 first_line->type = 0; /* paranoia */
1135 first_line = next_line;
1140 /*********************************************************************
1142 * Function : match_actions_file_header_line
1144 * Description : Match an actions file {{header}} line
1147 * 1 : line = String from file
1148 * 2 : name = Header to match against
1150 * Returns : 0 iff they match.
1152 *********************************************************************/
1153 static int match_actions_file_header_line(const char * line, const char * name)
1161 if ((line[0] != '{') || (line[1] != '{'))
1167 /* Look for optional whitespace */
1168 while ( (*line == ' ') || (*line == '\t') )
1173 /* Look for the specified name (case-insensitive) */
1175 if (0 != strncmpic(line, name, len))
1181 /* Look for optional whitespace */
1182 while ( (*line == ' ') || (*line == '\t') )
1187 /* Look for "}}" and end of string*/
1188 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1198 /*********************************************************************
1200 * Function : match_actions_file_header_line
1202 * Description : Match an actions file {{header}} line
1205 * 1 : line = String from file. Must not start with
1206 * whitespace (else infinite loop!)
1207 * 2 : pname = Destination for name
1208 * 2 : pvalue = Destination for value
1210 * Returns : JB_ERR_OK on success
1211 * JB_ERR_MEMORY on out-of-memory
1212 * JB_ERR_PARSE if there's no "=" sign, or if there's
1213 * nothing before the "=" sign (but empty
1214 * values *after* the "=" sign are legal).
1216 *********************************************************************/
1217 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1219 const char * name_end;
1220 const char * value_start;
1226 assert(*line != ' ');
1227 assert(*line != '\t');
1232 value_start = strchr(line, '=');
1233 if ((value_start == NULL) || (value_start == line))
1235 return JB_ERR_PARSE;
1238 name_end = value_start - 1;
1240 /* Eat any whitespace before the '=' */
1241 while ((*name_end == ' ') || (*name_end == '\t'))
1244 * we already know we must have at least 1 non-ws char
1245 * at start of buf - no need to check
1250 name_len = name_end - line + 1; /* Length excluding \0 */
1251 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1253 return JB_ERR_MEMORY;
1255 strncpy(*pname, line, name_len);
1256 (*pname)[name_len] = '\0';
1258 /* Eat any the whitespace after the '=' */
1260 while ((*value_start == ' ') || (*value_start == '\t'))
1265 if (NULL == (*pvalue = strdup(value_start)))
1269 return JB_ERR_MEMORY;
1276 /*********************************************************************
1278 * Function : edit_parse_actions_file
1280 * Description : Parse an actions file in memory.
1282 * Passed linked list must have the "data" member
1283 * zeroed, and must contain valid "next" and
1284 * "unprocessed" fields. The "raw" and "prefix"
1285 * fields are ignored, and "type" is just overwritten.
1287 * Note that on error the file may have been
1291 * 1 : file = Actions file to be parsed in-place.
1293 * Returns : JB_ERR_OK on success
1294 * JB_ERR_MEMORY on out-of-memory
1295 * JB_ERR_PARSE on error
1297 *********************************************************************/
1298 jb_err edit_parse_actions_file(struct editable_file * file)
1300 struct file_line * cur_line;
1302 const char * text; /* Text from a line */
1303 char * name; /* For lines of the form name=value */
1304 char * value; /* For lines of the form name=value */
1305 struct action_alias * alias_list = NULL;
1306 jb_err err = JB_ERR_OK;
1308 /* alias_list contains the aliases defined in this file.
1309 * It might be better to use the "file_line.data" fields
1310 * in the relavent places instead.
1313 cur_line = file->lines;
1315 /* A note about blank line support: Blank lines should only
1316 * ever occur as the last line in the file. This function
1317 * is more forgiving than that - FILE_LINE_BLANK can occur
1321 /* Skip leading blanks. Should only happen if file is
1322 * empty (which is valid, but pointless).
1324 while ( (cur_line != NULL)
1325 && (cur_line->unprocessed[0] == '\0') )
1328 cur_line->type = FILE_LINE_BLANK;
1329 cur_line = cur_line->next;
1332 if ( (cur_line != NULL)
1333 && (cur_line->unprocessed[0] != '{') )
1335 /* File doesn't start with a header */
1336 file->parse_error = cur_line;
1337 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1338 return JB_ERR_PARSE;
1341 if ( (cur_line != NULL) && (0 ==
1342 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1344 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1346 cur_line = cur_line->next;
1347 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1349 if (cur_line->unprocessed[0])
1351 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1353 err = split_line_on_equals(cur_line->unprocessed,
1354 &cur_line->data.setting.name,
1355 &cur_line->data.setting.svalue);
1356 if (err == JB_ERR_MEMORY)
1360 else if (err != JB_ERR_OK)
1362 /* Line does not contain a name=value pair */
1363 file->parse_error = cur_line;
1364 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1365 return JB_ERR_PARSE;
1370 cur_line->type = FILE_LINE_BLANK;
1372 cur_line = cur_line->next;
1376 if ( (cur_line != NULL) && (0 ==
1377 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1379 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1381 cur_line = cur_line->next;
1382 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1384 if (cur_line->unprocessed[0])
1386 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1390 cur_line->type = FILE_LINE_BLANK;
1392 cur_line = cur_line->next;
1396 if ( (cur_line != NULL) && (0 ==
1397 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1399 cur_line->type = FILE_LINE_ALIAS_HEADER;
1401 cur_line = cur_line->next;
1402 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1404 if (cur_line->unprocessed[0])
1406 /* define an alias */
1407 struct action_alias * new_alias;
1409 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1411 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1412 if (err == JB_ERR_MEMORY)
1416 else if (err != JB_ERR_OK)
1418 /* Line does not contain a name=value pair */
1419 file->parse_error = cur_line;
1420 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1421 return JB_ERR_PARSE;
1424 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1429 free_alias_list(alias_list);
1430 return JB_ERR_MEMORY;
1433 err = get_actions(value, alias_list, new_alias->action);
1436 /* Invalid action or out of memory */
1440 free_alias_list(alias_list);
1441 if (err == JB_ERR_MEMORY)
1447 /* Line does not contain a name=value pair */
1448 file->parse_error = cur_line;
1449 file->parse_error_text = "This alias does not specify a valid set of actions.";
1450 return JB_ERR_PARSE;
1456 new_alias->name = name;
1459 new_alias->next = alias_list;
1460 alias_list = new_alias;
1464 cur_line->type = FILE_LINE_BLANK;
1466 cur_line = cur_line->next;
1470 /* Header done, process the main part of the file */
1471 while (cur_line != NULL)
1473 /* At this point, (cur_line->unprocessed[0] == '{') */
1474 assert(cur_line->unprocessed[0] == '{');
1475 text = cur_line->unprocessed + 1;
1476 len = strlen(text) - 1;
1477 if (text[len] != '}')
1479 /* No closing } on header */
1480 free_alias_list(alias_list);
1481 file->parse_error = cur_line;
1482 file->parse_error_text = "Headers starting with '{' must have a "
1483 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1484 "must close with two brackets ('}}').";
1485 return JB_ERR_PARSE;
1490 /* An invalid {{ header. */
1491 free_alias_list(alias_list);
1492 file->parse_error = cur_line;
1493 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1494 "Please remember that the system (two-bracket) headers must "
1495 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1496 "and must appear before any actions (one-bracket) headers. "
1497 "Also note that system headers may not be repeated.";
1498 return JB_ERR_PARSE;
1501 while ( (*text == ' ') || (*text == '\t') )
1507 && ( (text[len - 1] == ' ')
1508 || (text[len - 1] == '\t') ) )
1513 cur_line->type = FILE_LINE_ACTION;
1515 /* Remove {} and make copy */
1516 if (NULL == (value = (char *) malloc(len + 1)))
1519 free_alias_list(alias_list);
1520 return JB_ERR_MEMORY;
1522 strncpy(value, text, len);
1526 err = get_actions(value, alias_list, cur_line->data.action);
1529 /* Invalid action or out of memory */
1531 free_alias_list(alias_list);
1532 if (err == JB_ERR_MEMORY)
1538 /* Line does not contain a name=value pair */
1539 file->parse_error = cur_line;
1540 file->parse_error_text = "This header does not specify a valid set of actions.";
1541 return JB_ERR_PARSE;
1545 /* Done with string - it was clobbered anyway */
1548 /* Process next line */
1549 cur_line = cur_line->next;
1551 /* Loop processing URL patterns */
1552 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1554 if (cur_line->unprocessed[0])
1556 /* Could parse URL here, but this isn't currently needed */
1558 cur_line->type = FILE_LINE_URL;
1562 cur_line->type = FILE_LINE_BLANK;
1564 cur_line = cur_line->next;
1566 } /* End main while(cur_line != NULL) loop */
1568 free_alias_list(alias_list);
1574 /*********************************************************************
1576 * Function : edit_read_file_lines
1578 * Description : Read all the lines of a file into memory.
1579 * Handles whitespace, comments and line continuation.
1582 * 1 : fp = File to read from. On return, this will be
1583 * at EOF but it will not have been closed.
1584 * 2 : pfile = Destination for a linked list of file_lines.
1585 * Will be set to NULL on error.
1586 * 3 : newline = How to handle newlines.
1588 * Returns : JB_ERR_OK on success
1589 * JB_ERR_MEMORY on out-of-memory
1591 *********************************************************************/
1592 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1594 struct file_line * first_line; /* Keep for return value or to free */
1595 struct file_line * cur_line; /* Current line */
1596 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1604 cur_line = first_line = zalloc(sizeof(struct file_line));
1605 if (cur_line == NULL)
1607 return JB_ERR_MEMORY;
1610 cur_line->type = FILE_LINE_UNPROCESSED;
1612 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1615 /* Out of memory or empty file. */
1616 /* Note that empty file is not an error we propogate up */
1618 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1623 prev_line = cur_line;
1624 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1625 if (cur_line == NULL)
1628 edit_free_file_lines(first_line);
1629 return JB_ERR_MEMORY;
1632 cur_line->type = FILE_LINE_UNPROCESSED;
1634 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1635 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1638 edit_free_file_lines(first_line);
1639 return JB_ERR_MEMORY;
1643 while (rval != JB_ERR_FILE);
1647 /* We allocated one too many - free it */
1648 prev_line->next = NULL;
1651 *pfile = first_line;
1656 /*********************************************************************
1658 * Function : edit_read_file
1660 * Description : Read a complete file into memory.
1661 * Handles CGI parameter parsing. If requested, also
1662 * checks the file's modification timestamp.
1665 * 1 : csp = Current client state (buffers, headers, etc...)
1666 * 2 : parameters = map of cgi parameters.
1667 * 3 : require_version = true to check "ver" parameter.
1668 * 4 : suffix = File extension, e.g. ".action".
1669 * 5 : pfile = Destination for the file. Will be set
1673 * filename : The name of the file to read, without the
1674 * path or ".action" extension.
1675 * ver : (Only if require_version is nonzero)
1676 * Timestamp of the actions file. If wrong, this
1677 * function fails with JB_ERR_MODIFIED.
1679 * Returns : JB_ERR_OK on success
1680 * JB_ERR_MEMORY on out-of-memory
1681 * JB_ERR_CGI_PARAMS if "filename" was not specified
1683 * JB_ERR_FILE if the file cannot be opened or
1685 * JB_ERR_MODIFIED if version checking was requested and
1686 * failed - the file was modified outside
1687 * of this CGI editor instance.
1689 *********************************************************************/
1690 jb_err edit_read_file(struct client_state *csp,
1691 const struct map *parameters,
1692 int require_version,
1694 struct editable_file **pfile)
1696 struct file_line * lines;
1700 const char * identifier;
1701 struct editable_file * file;
1702 unsigned version = 0;
1703 struct stat statbuf[1];
1704 char version_buf[22];
1705 int newline = NEWLINE_UNKNOWN;
1713 err = get_file_name_param(csp, parameters, "f", suffix,
1714 &filename, &identifier);
1720 if (stat(filename, statbuf) < 0)
1722 /* Error, probably file not found. */
1726 version = (unsigned) statbuf->st_mtime;
1728 if (require_version)
1730 unsigned specified_version;
1731 err = get_number_param(csp, parameters, "v", &specified_version);
1738 if (version != specified_version)
1740 return JB_ERR_MODIFIED;
1744 if (NULL == (fp = fopen(filename,"rb")))
1750 err = edit_read_file_lines(fp, &lines, &newline);
1760 file = (struct editable_file *) zalloc(sizeof(*file));
1764 edit_free_file_lines(lines);
1768 file->lines = lines;
1769 file->newline = newline;
1770 file->filename = filename;
1771 file->version = version;
1772 file->identifier = url_encode(identifier);
1774 if (file->identifier == NULL)
1776 edit_free_file(file);
1777 return JB_ERR_MEMORY;
1780 /* Correct file->version_str */
1781 freez(file->version_str);
1782 snprintf(version_buf, 22, "%u", file->version);
1783 version_buf[21] = '\0';
1784 file->version_str = strdup(version_buf);
1785 if (version_buf == NULL)
1787 edit_free_file(file);
1788 return JB_ERR_MEMORY;
1796 /*********************************************************************
1798 * Function : edit_read_actions_file
1800 * Description : Read a complete actions file into memory.
1801 * Handles CGI parameter parsing. If requested, also
1802 * checks the file's modification timestamp.
1804 * If this function detects an error in the categories
1805 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1806 * then it handles it by filling in the specified
1807 * response structure and returning JB_ERR_FILE.
1810 * 1 : csp = Current client state (buffers, headers, etc...)
1811 * 2 : rsp = HTTP response. Only filled in on error.
1812 * 2 : parameters = map of cgi parameters.
1813 * 3 : require_version = true to check "ver" parameter.
1814 * 4 : pfile = Destination for the file. Will be set
1818 * filename : The name of the actions file to read, without the
1819 * path or ".action" extension.
1820 * ver : (Only if require_version is nonzero)
1821 * Timestamp of the actions file. If wrong, this
1822 * function fails with JB_ERR_MODIFIED.
1824 * Returns : JB_ERR_OK on success
1825 * JB_ERR_MEMORY on out-of-memory
1826 * JB_ERR_CGI_PARAMS if "filename" was not specified
1828 * JB_ERR_FILE if the file does not contain valid data,
1829 * or if file cannot be opened or
1830 * contains no data, or if version
1831 * checking was requested and failed.
1833 *********************************************************************/
1834 jb_err edit_read_actions_file(struct client_state *csp,
1835 struct http_response *rsp,
1836 const struct map *parameters,
1837 int require_version,
1838 struct editable_file **pfile)
1841 struct editable_file *file;
1849 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1852 /* Try to handle if possible */
1853 if (err == JB_ERR_FILE)
1855 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1857 else if (err == JB_ERR_MODIFIED)
1859 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1861 if (err == JB_ERR_OK)
1864 * Signal to higher-level CGI code that there was a problem but we
1865 * handled it, they should just return JB_ERR_OK.
1872 err = edit_parse_actions_file(file);
1875 if (err == JB_ERR_PARSE)
1877 err = cgi_error_parse(csp, rsp, file);
1878 if (err == JB_ERR_OK)
1881 * Signal to higher-level CGI code that there was a problem but we
1882 * handled it, they should just return JB_ERR_OK.
1887 edit_free_file(file);
1896 /*********************************************************************
1898 * Function : get_file_name_param
1900 * Description : Get the name of the file to edit from the parameters
1901 * passed to a CGI function. This function handles
1902 * security checks such as blocking urls containing
1903 * "/" or ".", prepending the config file directory,
1904 * and adding the specified suffix.
1906 * (This is an essential security check, otherwise
1907 * users may be able to pass "../../../etc/passwd"
1908 * and overwrite the password file [linux], "prn:"
1909 * and print random data [Windows], etc...)
1911 * This function only allows filenames contining the
1912 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1913 * That's probably too restrictive but at least it's
1917 * 1 : csp = Current client state (buffers, headers, etc...)
1918 * 2 : parameters = map of cgi parameters
1919 * 3 : param_name = The name of the parameter to read
1920 * 4 : suffix = File extension, e.g. ".actions"
1921 * 5 : pfilename = destination for full filename. Caller
1922 * free()s. Set to NULL on error.
1923 * 6 : pparam = destination for partial filename,
1924 * suitable for use in another URL. Allocated as part
1925 * of the map "parameters", so don't free it.
1926 * Set to NULL if not specified.
1928 * Returns : JB_ERR_OK on success
1929 * JB_ERR_MEMORY on out-of-memory
1930 * JB_ERR_CGI_PARAMS if "filename" was not specified
1933 *********************************************************************/
1934 static jb_err get_file_name_param(struct client_state *csp,
1935 const struct map *parameters,
1936 const char *param_name,
1939 const char **pparam)
1957 param = lookup(parameters, param_name);
1960 return JB_ERR_CGI_PARAMS;
1965 len = strlen(param);
1966 if (len >= FILENAME_MAX)
1969 return JB_ERR_CGI_PARAMS;
1972 /* Check every character to see if it's legal */
1974 while ((ch = *s++) != '\0')
1976 if ( ((ch < 'A') || (ch > 'Z'))
1977 && ((ch < 'a') || (ch > 'z'))
1978 && ((ch < '0') || (ch > '9'))
1982 /* Probable hack attempt. */
1983 return JB_ERR_CGI_PARAMS;
1987 /* Append extension */
1988 name = malloc(len + strlen(suffix) + 1);
1991 return JB_ERR_MEMORY;
1993 strcpy(name, param);
1994 strcpy(name + len, suffix);
1997 fullpath = make_path(csp->config->confdir, name);
2000 if (fullpath == NULL)
2002 return JB_ERR_MEMORY;
2006 *pfilename = fullpath;
2012 /*********************************************************************
2014 * Function : get_url_spec_param
2016 * Description : Get a URL pattern from the parameters
2017 * passed to a CGI function. Removes leading/trailing
2018 * spaces and validates it.
2021 * 1 : csp = Current client state (buffers, headers, etc...)
2022 * 2 : parameters = map of cgi parameters
2023 * 3 : name = Name of CGI parameter to read
2024 * 4 : pvalue = destination for value. Will be malloc()'d.
2025 * Set to NULL on error.
2027 * Returns : JB_ERR_OK on success
2028 * JB_ERR_MEMORY on out-of-memory
2029 * JB_ERR_CGI_PARAMS if the parameter was not specified
2032 *********************************************************************/
2033 static jb_err get_url_spec_param(struct client_state *csp,
2034 const struct map *parameters,
2038 const char *orig_param;
2041 struct url_spec compiled[1];
2051 orig_param = lookup(parameters, name);
2054 return JB_ERR_CGI_PARAMS;
2057 /* Copy and trim whitespace */
2058 param = strdup(orig_param);
2061 return JB_ERR_MEMORY;
2065 /* Must be non-empty, and can't allow 1st character to be '{' */
2066 if (param[0] == '\0' || param[0] == '{')
2069 return JB_ERR_CGI_PARAMS;
2072 /* Check for embedded newlines */
2073 for (s = param; *s != '\0'; s++)
2075 if ((*s == '\r') || (*s == '\n'))
2078 return JB_ERR_CGI_PARAMS;
2082 /* Check that regex is valid */
2087 return JB_ERR_MEMORY;
2089 err = create_url_spec(compiled, s);
2094 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2096 free_url_spec(compiled);
2098 if (param[strlen(param) - 1] == '\\')
2101 * Must protect trailing '\\' from becoming line continuation character.
2102 * Two methods: 1) If it's a domain only, add a trailing '/'.
2103 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2105 if (strchr(param, '/') == NULL)
2107 err = string_append(¶m, "/");
2111 err = string_append(¶m, "(?:)");
2118 /* Check that the modified regex is valid */
2123 return JB_ERR_MEMORY;
2125 err = create_url_spec(compiled, s);
2130 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2132 free_url_spec(compiled);
2139 /*********************************************************************
2141 * Function : map_radio
2143 * Description : Map a set of radio button values. E.g. if you have
2144 * 3 radio buttons, declare them as:
2145 * <option type="radio" name="xyz" @xyz-a@>
2146 * <option type="radio" name="xyz" @xyz-b@>
2147 * <option type="radio" name="xyz" @xyz-c@>
2148 * Then map one of the @xyz-?@ variables to "checked"
2149 * and all the others to empty by calling:
2150 * map_radio(exports, "xyz", "abc", sel)
2151 * Where 'sel' is 'a', 'b', or 'c'.
2154 * 1 : exports = Exports map to modify.
2155 * 2 : optionname = name for map
2156 * 3 : values = null-terminated list of values;
2157 * 4 : value = Selected value.
2159 * CGI Parameters : None
2161 * Returns : JB_ERR_OK on success
2162 * JB_ERR_MEMORY on out-of-memory
2164 *********************************************************************/
2165 static jb_err map_radio(struct map * exports,
2166 const char * optionname,
2167 const char * values,
2179 len = strlen(optionname);
2180 buf = malloc(len + 3);
2183 return JB_ERR_MEMORY;
2186 strcpy(buf, optionname);
2191 while ((c = *values++) != '\0')
2196 if (map(exports, buf, 1, "", 1))
2198 return JB_ERR_MEMORY;
2204 return map(exports, buf, 0, "checked", 1);
2208 /*********************************************************************
2210 * Function : cgi_error_modified
2212 * Description : CGI function that is called when a file is modified
2213 * outside the CGI editor.
2216 * 1 : csp = Current client state (buffers, headers, etc...)
2217 * 2 : rsp = http_response data structure for output
2218 * 3 : filename = The file that was modified.
2220 * CGI Parameters : none
2222 * Returns : JB_ERR_OK on success
2223 * JB_ERR_MEMORY on out-of-memory error.
2225 *********************************************************************/
2226 jb_err cgi_error_modified(struct client_state *csp,
2227 struct http_response *rsp,
2228 const char *filename)
2230 struct map *exports;
2237 if (NULL == (exports = default_exports(csp, NULL)))
2239 return JB_ERR_MEMORY;
2242 err = map(exports, "f", 1, html_encode(filename), 0);
2249 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2253 /*********************************************************************
2255 * Function : cgi_error_parse
2257 * Description : CGI function that is called when a file cannot
2258 * be parsed by the CGI editor.
2261 * 1 : csp = Current client state (buffers, headers, etc...)
2262 * 2 : rsp = http_response data structure for output
2263 * 3 : file = The file that was modified.
2265 * CGI Parameters : none
2267 * Returns : JB_ERR_OK on success
2268 * JB_ERR_MEMORY on out-of-memory error.
2270 *********************************************************************/
2271 jb_err cgi_error_parse(struct client_state *csp,
2272 struct http_response *rsp,
2273 struct editable_file *file)
2275 struct map *exports;
2277 struct file_line *cur_line;
2283 if (NULL == (exports = default_exports(csp, NULL)))
2285 return JB_ERR_MEMORY;
2288 err = map(exports, "f", 1, file->identifier, 1);
2289 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2291 cur_line = file->parse_error;
2294 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2295 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2303 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2307 /*********************************************************************
2309 * Function : cgi_error_file
2311 * Description : CGI function that is called when a file cannot be
2312 * opened by the CGI editor.
2315 * 1 : csp = Current client state (buffers, headers, etc...)
2316 * 2 : rsp = http_response data structure for output
2317 * 3 : filename = The file that was modified.
2319 * CGI Parameters : none
2321 * Returns : JB_ERR_OK on success
2322 * JB_ERR_MEMORY on out-of-memory error.
2324 *********************************************************************/
2325 jb_err cgi_error_file(struct client_state *csp,
2326 struct http_response *rsp,
2327 const char *filename)
2329 struct map *exports;
2336 if (NULL == (exports = default_exports(csp, NULL)))
2338 return JB_ERR_MEMORY;
2341 err = map(exports, "f", 1, html_encode(filename), 0);
2348 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2352 /*********************************************************************
2354 * Function : cgi_error_file
2356 * Description : CGI function that is called when a file cannot be
2357 * opened for writing by the CGI editor.
2360 * 1 : csp = Current client state (buffers, headers, etc...)
2361 * 2 : rsp = http_response data structure for output
2362 * 3 : filename = The file that we can't write to
2364 * CGI Parameters : none
2366 * Returns : JB_ERR_OK on success
2367 * JB_ERR_MEMORY on out-of-memory error.
2369 *********************************************************************/
2370 jb_err cgi_error_file_read_only(struct client_state *csp,
2371 struct http_response *rsp,
2372 const char *filename)
2374 struct map *exports;
2381 if (NULL == (exports = default_exports(csp, NULL)))
2383 return JB_ERR_MEMORY;
2386 err = map(exports, "f", 1, html_encode(filename), 0);
2393 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2397 /*********************************************************************
2399 * Function : cgi_error_disabled
2401 * Description : CGI function that is called if the actions editor
2402 * is called although it's disabled in config
2405 * 1 : csp = Current client state (buffers, headers, etc...)
2406 * 2 : rsp = http_response data structure for output
2408 * CGI Parameters : none
2410 * Returns : JB_ERR_OK on success
2411 * JB_ERR_MEMORY on out-of-memory error.
2413 *********************************************************************/
2414 jb_err cgi_error_disabled(struct client_state *csp,
2415 struct http_response *rsp)
2417 struct map *exports;
2422 if (NULL == (exports = default_exports(csp, NULL)))
2424 return JB_ERR_MEMORY;
2427 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2431 /*********************************************************************
2433 * Function : cgi_edit_actions
2435 * Description : CGI function that allows the user to choose which
2436 * actions file to edit.
2439 * 1 : csp = Current client state (buffers, headers, etc...)
2440 * 2 : rsp = http_response data structure for output
2441 * 3 : parameters = map of cgi parameters
2443 * CGI Parameters : None
2445 * Returns : JB_ERR_OK on success
2446 * JB_ERR_MEMORY on out-of-memory error
2448 *********************************************************************/
2449 jb_err cgi_edit_actions(struct client_state *csp,
2450 struct http_response *rsp,
2451 const struct map *parameters)
2454 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2456 return cgi_error_disabled(csp, rsp);
2459 /* FIXME: Incomplete */
2460 rsp->status = strdup("302 Local Redirect from Privoxy");
2461 if (rsp->status == NULL)
2463 return JB_ERR_MEMORY;
2465 if (enlist_unique_header(rsp->headers, "Location",
2466 CGI_PREFIX "edit-actions-list?f=default"))
2470 return JB_ERR_MEMORY;
2477 /*********************************************************************
2479 * Function : cgi_edit_actions_list
2481 * Description : CGI function that edits the actions list.
2482 * FIXME: This function shouldn't FATAL ever.
2483 * FIXME: This function doesn't check the retval of map()
2485 * 1 : csp = Current client state (buffers, headers, etc...)
2486 * 2 : rsp = http_response data structure for output
2487 * 3 : parameters = map of cgi parameters
2489 * CGI Parameters : filename
2491 * Returns : JB_ERR_OK on success
2492 * JB_ERR_MEMORY on out-of-memory
2493 * JB_ERR_FILE if the file cannot be opened or
2495 * JB_ERR_CGI_PARAMS if "filename" was not specified
2498 *********************************************************************/
2499 jb_err cgi_edit_actions_list(struct client_state *csp,
2500 struct http_response *rsp,
2501 const struct map *parameters)
2503 char * section_template;
2504 char * url_template;
2509 struct map * exports;
2510 struct map * section_exports;
2511 struct map * url_exports;
2512 struct editable_file * file;
2513 struct file_line * cur_line;
2514 unsigned line_number = 0;
2515 unsigned prev_section_line_number = ((unsigned) (-1));
2517 struct file_list * fl;
2518 struct url_actions * b;
2519 char * buttons = NULL;
2522 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2524 return cgi_error_disabled(csp, rsp);
2527 if (NULL == (exports = default_exports(csp, NULL)))
2529 return JB_ERR_MEMORY;
2532 /* Load actions file */
2533 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2536 /* No filename specified, can't read file, or out of memory. */
2537 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2540 /* Find start of actions in file */
2541 cur_line = file->lines;
2543 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2545 cur_line = cur_line->next;
2550 * Conventional actions files should have a match all block
2552 * cur_line = {...global actions...}
2553 * cur_line->next = /
2554 * cur_line->next->next = {...actions...} or EOF
2556 if ( (cur_line != NULL)
2557 && (cur_line->type == FILE_LINE_ACTION)
2558 && (cur_line->next != NULL)
2559 && (cur_line->next->type == FILE_LINE_URL)
2560 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2561 && ( (cur_line->next->next == NULL)
2562 || (cur_line->next->next->type != FILE_LINE_URL)
2566 * Generate string with buttons to set actions for "/" to
2567 * any predefined set of actions (named standard.*, probably
2568 * residing in standard.action).
2571 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2574 edit_free_file(file);
2576 if (err == JB_ERR_FILE)
2578 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2583 err = template_fill(§ion_template, exports);
2586 edit_free_file(file);
2591 buttons = strdup("");
2592 for (i = 0; i < MAX_ACTION_FILES; i++)
2594 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2596 for (b = b->next; NULL != b; b = b->next)
2598 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2600 if (err || (NULL == (section_exports = new_map())))
2603 free(section_template);
2604 edit_free_file(file);
2606 return JB_ERR_MEMORY;
2609 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2611 if (err || (NULL == (s = strdup(section_template))))
2613 free_map(section_exports);
2615 free(section_template);
2616 edit_free_file(file);
2618 return JB_ERR_MEMORY;
2621 if (!err) err = template_fill(&s, section_exports);
2622 free_map(section_exports);
2623 if (!err) err = string_join(&buttons, s);
2628 freez(section_template);
2629 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2632 * Conventional actions file, supply extra editing help.
2633 * (e.g. don't allow them to make it an unconventional one).
2635 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2637 snprintf(buf, 150, "%d", line_number);
2638 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2639 snprintf(buf, 150, "%d", line_number + 2);
2640 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2641 if (!err) err = map(exports, "all-urls-actions", 1,
2642 actions_to_html(csp, cur_line->data.action), 0);
2644 /* Skip the 2 lines */
2645 cur_line = cur_line->next->next;
2649 * Note that prev_section_line_number is NOT set here.
2650 * This is deliberate and not a bug. It stops a "Move up"
2651 * option appearing on the next section. Clicking "Move
2652 * up" would make the actions file unconventional, which
2653 * we don't want, so we hide this option.
2659 * Non-standard actions file - does not begin with
2660 * the "All URLs" section.
2662 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2665 /* Set up global exports */
2667 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2668 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2670 /* Discourage private additions to default.action */
2672 if (!err) err = map_conditional(exports, "default-action",
2673 (strcmp("default", lookup(parameters, "f")) == 0));
2676 edit_free_file(file);
2681 /* Should do all global exports above this point */
2683 /* Load templates */
2685 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2688 edit_free_file(file);
2690 if (err == JB_ERR_FILE)
2692 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2697 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2700 free(section_template);
2701 edit_free_file(file);
2703 if (err == JB_ERR_FILE)
2705 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2710 err = template_fill(§ion_template, exports);
2714 edit_free_file(file);
2720 err = template_fill(&url_template, exports);
2723 free(section_template);
2724 edit_free_file(file);
2729 if (NULL == (sections = strdup("")))
2731 free(section_template);
2733 edit_free_file(file);
2735 return JB_ERR_MEMORY;
2738 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2740 if (NULL == (section_exports = new_map()))
2743 free(section_template);
2745 edit_free_file(file);
2747 return JB_ERR_MEMORY;
2750 snprintf(buf, 150, "%d", line_number);
2751 err = map(section_exports, "s", 1, buf, 1);
2752 if (!err) err = map(section_exports, "actions", 1,
2753 actions_to_html(csp, cur_line->data.action), 0);
2756 && (cur_line->next != NULL)
2757 && (cur_line->next->type == FILE_LINE_URL))
2759 /* This section contains at least one URL, don't allow delete */
2760 err = map_block_killer(section_exports, "empty-section");
2764 if (!err) err = map_block_keep(section_exports, "empty-section");
2767 if (prev_section_line_number != ((unsigned)(-1)))
2769 /* Not last section */
2770 snprintf(buf, 150, "%d", prev_section_line_number);
2771 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2772 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2777 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2779 prev_section_line_number = line_number;
2784 free(section_template);
2786 edit_free_file(file);
2788 free_map(section_exports);
2792 /* Should do all section-specific exports above this point */
2794 if (NULL == (urls = strdup("")))
2797 free(section_template);
2799 edit_free_file(file);
2801 free_map(section_exports);
2802 return JB_ERR_MEMORY;
2807 cur_line = cur_line->next;
2810 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2812 if (NULL == (url_exports = new_map()))
2816 free(section_template);
2818 edit_free_file(file);
2820 free_map(section_exports);
2821 return JB_ERR_MEMORY;
2824 snprintf(buf, 150, "%d", line_number);
2825 err = map(url_exports, "p", 1, buf, 1);
2827 snprintf(buf, 150, "%d", url_1_2);
2828 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2830 if (!err) err = map(url_exports, "url-html", 1,
2831 html_encode(cur_line->unprocessed), 0);
2832 if (!err) err = map(url_exports, "url", 1,
2833 url_encode(cur_line->unprocessed), 0);
2839 free(section_template);
2841 edit_free_file(file);
2843 free_map(section_exports);
2844 free_map(url_exports);
2848 if (NULL == (s = strdup(url_template)))
2852 free(section_template);
2854 edit_free_file(file);
2856 free_map(section_exports);
2857 free_map(url_exports);
2858 return JB_ERR_MEMORY;
2861 err = template_fill(&s, section_exports);
2862 if (!err) err = template_fill(&s, url_exports);
2863 if (!err) err = string_append(&urls, s);
2865 free_map(url_exports);
2872 free(section_template);
2874 edit_free_file(file);
2876 free_map(section_exports);
2880 url_1_2 = 3 - url_1_2;
2882 cur_line = cur_line->next;
2886 err = map(section_exports, "urls", 1, urls, 0);
2888 /* Could also do section-specific exports here, but it wouldn't be as fast */
2890 snprintf(buf, 150, "%d", line_number);
2891 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2893 if ( (cur_line != NULL)
2894 && (cur_line->type == FILE_LINE_ACTION))
2896 /* Not last section */
2897 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2902 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2908 free(section_template);
2910 edit_free_file(file);
2912 free_map(section_exports);
2916 if (NULL == (s = strdup(section_template)))
2919 free(section_template);
2921 edit_free_file(file);
2923 free_map(section_exports);
2924 return JB_ERR_MEMORY;
2927 err = template_fill(&s, section_exports);
2928 if (!err) err = string_append(§ions, s);
2931 free_map(section_exports);
2936 free(section_template);
2938 edit_free_file(file);
2944 edit_free_file(file);
2945 free(section_template);
2948 err = map(exports, "sections", 1, sections, 0);
2955 /* Could also do global exports here, but it wouldn't be as fast */
2957 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2961 /*********************************************************************
2963 * Function : cgi_edit_actions_for_url
2965 * Description : CGI function that edits the Actions list.
2968 * 1 : csp = Current client state (buffers, headers, etc...)
2969 * 2 : rsp = http_response data structure for output
2970 * 3 : parameters = map of cgi parameters
2972 * CGI Parameters : None
2974 * Returns : JB_ERR_OK on success
2975 * JB_ERR_MEMORY on out-of-memory
2976 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2977 * specified or not valid.
2979 *********************************************************************/
2980 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2981 struct http_response *rsp,
2982 const struct map *parameters)
2984 struct map * exports;
2986 struct editable_file * file;
2987 struct file_line * cur_line;
2988 unsigned line_number;
2990 struct file_list *filter_file;
2991 struct re_filterfile_spec *filter_group;
2993 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2995 return cgi_error_disabled(csp, rsp);
2998 err = get_number_param(csp, parameters, "s", §ionid);
3004 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3007 /* No filename specified, can't read file, modified, or out of memory. */
3008 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3011 cur_line = file->lines;
3013 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3015 cur_line = cur_line->next;
3018 if ( (cur_line == NULL)
3019 || (line_number != sectionid)
3021 || (cur_line->type != FILE_LINE_ACTION))
3023 /* Invalid "sectionid" parameter */
3024 edit_free_file(file);
3025 return JB_ERR_CGI_PARAMS;
3028 if (NULL == (exports = default_exports(csp, NULL)))
3030 edit_free_file(file);
3031 return JB_ERR_MEMORY;
3034 err = map(exports, "f", 1, file->identifier, 1);
3035 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3036 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3038 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3040 filter_file = csp->rlist;
3041 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
3043 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
3047 edit_free_file(file);
3052 if (filter_group == NULL)
3054 err = map(exports, "filter-params", 1, "", 1);
3058 /* We have some entries in the filter list */
3061 char * filter_template;
3063 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3066 edit_free_file(file);
3068 if (err == JB_ERR_FILE)
3070 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3075 err = template_fill(&filter_template, exports);
3077 result = strdup("");
3079 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3081 char current_mode = 'x';
3082 struct list_entry *filter_name;
3084 struct map *line_exports;
3087 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
3088 while ((filter_name != NULL)
3089 && (0 != strcmp(filter_group->name, filter_name->str)))
3091 filter_name = filter_name->next;
3094 if (filter_name != NULL)
3100 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
3101 while ((filter_name != NULL)
3102 && (0 != strcmp(filter_group->name, filter_name->str)))
3104 filter_name = filter_name->next;
3106 if (filter_name != NULL)
3112 /* Generate a unique serial number */
3113 snprintf(number, sizeof(number), "%x", index++);
3114 number[sizeof(number) - 1] = '\0';
3116 line_exports = new_map();
3117 if (line_exports == NULL)
3119 err = JB_ERR_MEMORY;
3124 if (!err) err = map(line_exports, "index", 1, number, 1);
3125 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3126 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3127 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3132 this_line = strdup(filter_template);
3133 if (this_line == NULL) err = JB_ERR_MEMORY;
3135 if (!err) err = template_fill(&this_line, line_exports);
3136 string_join(&result, this_line);
3138 free_map(line_exports);
3142 freez(filter_template);
3146 err = map(exports, "filter-params", 1, result, 0);
3154 if (!err) err = map_radio(exports, "filter-all", "nx",
3155 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3157 edit_free_file(file);
3165 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3169 /*********************************************************************
3171 * Function : cgi_edit_actions_submit
3173 * Description : CGI function that actually edits the Actions list.
3176 * 1 : csp = Current client state (buffers, headers, etc...)
3177 * 2 : rsp = http_response data structure for output
3178 * 3 : parameters = map of cgi parameters
3180 * CGI Parameters : None
3182 * Returns : JB_ERR_OK on success
3183 * JB_ERR_MEMORY on out-of-memory
3184 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3185 * specified or not valid.
3187 *********************************************************************/
3188 jb_err cgi_edit_actions_submit(struct client_state *csp,
3189 struct http_response *rsp,
3190 const struct map *parameters)
3196 struct editable_file * file;
3197 struct file_line * cur_line;
3198 unsigned line_number;
3202 const char * action_set_name;
3204 struct file_list * fl;
3205 struct url_actions * b;
3207 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3209 return cgi_error_disabled(csp, rsp);
3212 err = get_number_param(csp, parameters, "s", §ionid);
3218 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3221 /* No filename specified, can't read file, modified, or out of memory. */
3222 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3225 cur_line = file->lines;
3227 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3229 cur_line = cur_line->next;
3232 if ( (cur_line == NULL)
3233 || (line_number != sectionid)
3235 || (cur_line->type != FILE_LINE_ACTION))
3237 /* Invalid "sectionid" parameter */
3238 edit_free_file(file);
3239 return JB_ERR_CGI_PARAMS;
3242 get_string_param(parameters, "p", &action_set_name);
3243 if (action_set_name != NULL)
3245 for (index = 0; index < MAX_ACTION_FILES; index++)
3247 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3249 for (b = b->next; NULL != b; b = b->next)
3251 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3253 copy_action(cur_line->data.action, b->action);
3259 edit_free_file(file);
3260 return JB_ERR_CGI_PARAMS;
3266 err = actions_from_radio(parameters, cur_line->data.action);
3272 edit_free_file(file);
3276 ch = get_char_param(parameters, "filter_all");
3279 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3280 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3281 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3285 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3288 for (index = 0; !err; index++)
3295 /* Generate the keys */
3296 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3297 key_value[sizeof(key_value) - 1] = '\0';
3298 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3299 key_name[sizeof(key_name) - 1] = '\0';
3301 err = get_string_param(parameters, key_name, &name);
3310 value = get_char_param(parameters, key_value);
3313 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3314 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3315 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3317 else if (value == 'N')
3319 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3320 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3322 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3323 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3326 else if (value == 'X')
3328 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3329 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3336 edit_free_file(file);
3340 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3343 edit_free_file(file);
3344 return JB_ERR_MEMORY;
3347 len = strlen(actiontext);
3351 * Empty action - must special-case this.
3352 * Simply setting len to 1 is sufficient...
3357 if (NULL == (newtext = malloc(len + 2)))
3361 edit_free_file(file);
3362 return JB_ERR_MEMORY;
3364 strcpy(newtext, actiontext);
3368 newtext[len + 1] = '\0';
3370 freez(cur_line->raw);
3371 freez(cur_line->unprocessed);
3372 cur_line->unprocessed = newtext;
3374 err = edit_write_file(file);
3377 /* Error writing file */
3378 if (err == JB_ERR_FILE)
3380 /* Read-only file. */
3381 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3383 edit_free_file(file);
3387 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3388 string_append(&target, file->identifier);
3389 string_join(&target, section_target(sectionid));
3392 edit_free_file(file);
3397 return JB_ERR_MEMORY;
3400 rsp->status = strdup("302 Local Redirect from Privoxy");
3401 if (rsp->status == NULL)
3404 return JB_ERR_MEMORY;
3406 err = enlist_unique_header(rsp->headers, "Location", target);
3413 /*********************************************************************
3415 * Function : cgi_edit_actions_url
3417 * Description : CGI function that actually edits a URL pattern in
3421 * 1 : csp = Current client state (buffers, headers, etc...)
3422 * 2 : rsp = http_response data structure for output
3423 * 3 : parameters = map of cgi parameters
3426 * filename : Identifies the file to edit
3427 * ver : File's last-modified time
3428 * section : Line number of section to edit
3429 * pattern : Line number of pattern to edit
3430 * newval : New value for pattern
3432 * Returns : JB_ERR_OK on success
3433 * JB_ERR_MEMORY on out-of-memory
3434 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3435 * specified or not valid.
3437 *********************************************************************/
3438 jb_err cgi_edit_actions_url(struct client_state *csp,
3439 struct http_response *rsp,
3440 const struct map *parameters)
3444 struct editable_file * file;
3445 struct file_line * cur_line;
3446 unsigned line_number;
3447 unsigned section_start_line_number = 0;
3451 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3453 return cgi_error_disabled(csp, rsp);
3456 err = get_number_param(csp, parameters, "p", &patternid);
3463 return JB_ERR_CGI_PARAMS;
3466 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3472 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3475 /* No filename specified, can't read file, modified, or out of memory. */
3477 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3481 cur_line = file->lines;
3483 while ((cur_line != NULL) && (line_number < patternid))
3485 if (cur_line->type == FILE_LINE_ACTION)
3487 section_start_line_number = line_number;
3489 cur_line = cur_line->next;
3493 if ( (cur_line == NULL)
3494 || (cur_line->type != FILE_LINE_URL))
3496 /* Invalid "patternid" parameter */
3498 edit_free_file(file);
3499 return JB_ERR_CGI_PARAMS;
3502 /* At this point, the line to edit is in cur_line */
3504 freez(cur_line->raw);
3505 freez(cur_line->unprocessed);
3506 cur_line->unprocessed = new_pattern;
3508 err = edit_write_file(file);
3511 /* Error writing file */
3512 if (err == JB_ERR_FILE)
3514 /* Read-only file. */
3515 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3517 edit_free_file(file);
3521 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3522 string_append(&target, file->identifier);
3523 string_join(&target, section_target(section_start_line_number));
3525 edit_free_file(file);
3530 return JB_ERR_MEMORY;
3533 rsp->status = strdup("302 Local Redirect from Privoxy");
3534 if (rsp->status == NULL)
3537 return JB_ERR_MEMORY;
3539 err = enlist_unique_header(rsp->headers, "Location", target);
3546 /*********************************************************************
3548 * Function : cgi_edit_actions_add_url
3550 * Description : CGI function that actually adds a URL pattern to
3554 * 1 : csp = Current client state (buffers, headers, etc...)
3555 * 2 : rsp = http_response data structure for output
3556 * 3 : parameters = map of cgi parameters
3559 * filename : Identifies the file to edit
3560 * ver : File's last-modified time
3561 * section : Line number of section to edit
3562 * newval : New pattern
3564 * Returns : JB_ERR_OK on success
3565 * JB_ERR_MEMORY on out-of-memory
3566 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3567 * specified or not valid.
3569 *********************************************************************/
3570 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3571 struct http_response *rsp,
3572 const struct map *parameters)
3576 struct file_line * new_line;
3577 struct editable_file * file;
3578 struct file_line * cur_line;
3579 unsigned line_number;
3583 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3585 return cgi_error_disabled(csp, rsp);
3588 err = get_number_param(csp, parameters, "s", §ionid);
3595 return JB_ERR_CGI_PARAMS;
3598 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3604 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3607 /* No filename specified, can't read file, modified, or out of memory. */
3609 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3613 cur_line = file->lines;
3615 while ((cur_line != NULL) && (line_number < sectionid))
3617 cur_line = cur_line->next;
3621 if ( (cur_line == NULL)
3622 || (cur_line->type != FILE_LINE_ACTION))
3624 /* Invalid "sectionid" parameter */
3626 edit_free_file(file);
3627 return JB_ERR_CGI_PARAMS;
3630 /* At this point, the section header is in cur_line - add after this. */
3632 /* Allocate the new line */
3633 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3634 if (new_line == NULL)
3637 edit_free_file(file);
3638 return JB_ERR_MEMORY;
3641 /* Fill in the data members of the new line */
3642 new_line->raw = NULL;
3643 new_line->prefix = NULL;
3644 new_line->unprocessed = new_pattern;
3645 new_line->type = FILE_LINE_URL;
3647 /* Link new_line into the list, after cur_line */
3648 new_line->next = cur_line->next;
3649 cur_line->next = new_line;
3651 /* Done making changes, now commit */
3653 err = edit_write_file(file);
3656 /* Error writing file */
3657 if (err == JB_ERR_FILE)
3659 /* Read-only file. */
3660 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3662 edit_free_file(file);
3666 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3667 string_append(&target, file->identifier);
3668 string_join(&target, section_target(sectionid));
3670 edit_free_file(file);
3675 return JB_ERR_MEMORY;
3678 rsp->status = strdup("302 Local Redirect from Privoxy");
3679 if (rsp->status == NULL)
3682 return JB_ERR_MEMORY;
3684 err = enlist_unique_header(rsp->headers, "Location", target);
3691 /*********************************************************************
3693 * Function : cgi_edit_actions_remove_url
3695 * Description : CGI function that actually removes a URL pattern from
3699 * 1 : csp = Current client state (buffers, headers, etc...)
3700 * 2 : rsp = http_response data structure for output
3701 * 3 : parameters = map of cgi parameters
3704 * f : (filename) Identifies the file to edit
3705 * v : (version) File's last-modified time
3706 * p : (pattern) Line number of pattern to remove
3708 * Returns : JB_ERR_OK on success
3709 * JB_ERR_MEMORY on out-of-memory
3710 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3711 * specified or not valid.
3713 *********************************************************************/
3714 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3715 struct http_response *rsp,
3716 const struct map *parameters)
3719 struct editable_file * file;
3720 struct file_line * cur_line;
3721 struct file_line * prev_line;
3722 unsigned line_number;
3723 unsigned section_start_line_number = 0;
3727 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3729 return cgi_error_disabled(csp, rsp);
3732 err = get_number_param(csp, parameters, "p", &patternid);
3738 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3741 /* No filename specified, can't read file, modified, or out of memory. */
3742 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3747 cur_line = file->lines;
3749 while ((cur_line != NULL) && (line_number < patternid))
3751 if (cur_line->type == FILE_LINE_ACTION)
3753 section_start_line_number = line_number;
3755 prev_line = cur_line;
3756 cur_line = cur_line->next;
3760 if ( (cur_line == NULL)
3761 || (prev_line == NULL)
3762 || (cur_line->type != FILE_LINE_URL))
3764 /* Invalid "patternid" parameter */
3765 edit_free_file(file);
3766 return JB_ERR_CGI_PARAMS;
3769 /* At this point, the line to remove is in cur_line, and the previous
3770 * one is in prev_line
3773 /* Unlink cur_line */
3774 prev_line->next = cur_line->next;
3775 cur_line->next = NULL;
3778 edit_free_file_lines(cur_line);
3780 err = edit_write_file(file);
3783 /* Error writing file */
3784 if (err == JB_ERR_FILE)
3786 /* Read-only file. */
3787 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3789 edit_free_file(file);
3793 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3794 string_append(&target, file->identifier);
3795 string_join(&target, section_target(section_start_line_number));
3797 edit_free_file(file);
3802 return JB_ERR_MEMORY;
3805 rsp->status = strdup("302 Local Redirect from Privoxy");
3806 if (rsp->status == NULL)
3809 return JB_ERR_MEMORY;
3811 err = enlist_unique_header(rsp->headers, "Location", target);
3818 /*********************************************************************
3820 * Function : cgi_edit_actions_section_remove
3822 * Description : CGI function that actually removes a whole section from
3823 * the actions file. The section must be empty first
3824 * (else JB_ERR_CGI_PARAMS).
3827 * 1 : csp = Current client state (buffers, headers, etc...)
3828 * 2 : rsp = http_response data structure for output
3829 * 3 : parameters = map of cgi parameters
3832 * f : (filename) Identifies the file to edit
3833 * v : (version) File's last-modified time
3834 * s : (section) Line number of section to edit
3836 * Returns : JB_ERR_OK on success
3837 * JB_ERR_MEMORY on out-of-memory
3838 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3839 * specified or not valid.
3841 *********************************************************************/
3842 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3843 struct http_response *rsp,
3844 const struct map *parameters)
3847 struct editable_file * file;
3848 struct file_line * cur_line;
3849 struct file_line * prev_line;
3850 unsigned line_number;
3854 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3856 return cgi_error_disabled(csp, rsp);
3859 err = get_number_param(csp, parameters, "s", §ionid);
3865 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3868 /* No filename specified, can't read file, modified, or out of memory. */
3869 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3873 cur_line = file->lines;
3876 while ((cur_line != NULL) && (line_number < sectionid))
3878 prev_line = cur_line;
3879 cur_line = cur_line->next;
3883 if ( (cur_line == NULL)
3884 || (cur_line->type != FILE_LINE_ACTION) )
3886 /* Invalid "sectionid" parameter */
3887 edit_free_file(file);
3888 return JB_ERR_CGI_PARAMS;
3891 if ( (cur_line->next != NULL)
3892 && (cur_line->next->type == FILE_LINE_URL) )
3894 /* Section not empty. */
3895 edit_free_file(file);
3896 return JB_ERR_CGI_PARAMS;
3899 /* At this point, the line to remove is in cur_line, and the previous
3900 * one is in prev_line
3903 /* Unlink cur_line */
3904 if (prev_line == NULL)
3906 /* Removing the first line from the file */
3907 file->lines = cur_line->next;
3911 prev_line->next = cur_line->next;
3913 cur_line->next = NULL;
3916 edit_free_file_lines(cur_line);
3918 err = edit_write_file(file);
3921 /* Error writing file */
3922 if (err == JB_ERR_FILE)
3924 /* Read-only file. */
3925 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3927 edit_free_file(file);
3931 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3932 string_append(&target, file->identifier);
3934 edit_free_file(file);
3939 return JB_ERR_MEMORY;
3942 rsp->status = strdup("302 Local Redirect from Privoxy");
3943 if (rsp->status == NULL)
3946 return JB_ERR_MEMORY;
3948 err = enlist_unique_header(rsp->headers, "Location", target);
3955 /*********************************************************************
3957 * Function : cgi_edit_actions_section_add
3959 * Description : CGI function that adds a new empty section to
3963 * 1 : csp = Current client state (buffers, headers, etc...)
3964 * 2 : rsp = http_response data structure for output
3965 * 3 : parameters = map of cgi parameters
3968 * f : (filename) Identifies the file to edit
3969 * v : (version) File's last-modified time
3970 * s : (section) Line number of section to add after, 0 for
3973 * Returns : JB_ERR_OK on success
3974 * JB_ERR_MEMORY on out-of-memory
3975 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3976 * specified or not valid.
3978 *********************************************************************/
3979 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3980 struct http_response *rsp,
3981 const struct map *parameters)
3984 struct file_line * new_line;
3986 struct editable_file * file;
3987 struct file_line * cur_line;
3988 unsigned line_number;
3992 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3994 return cgi_error_disabled(csp, rsp);
3997 err = get_number_param(csp, parameters, "s", §ionid);
4003 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4006 /* No filename specified, can't read file, modified, or out of memory. */
4007 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4011 cur_line = file->lines;
4013 if (sectionid <= 1U)
4015 /* Add to start of file */
4016 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4018 /* There's something in the file, find the line before the first
4021 while ( (cur_line->next != NULL)
4022 && (cur_line->next->type != FILE_LINE_ACTION) )
4024 cur_line = cur_line->next;
4030 /* File starts with action line, so insert at top */
4036 /* Add after stated section. */
4037 while ((cur_line != NULL) && (line_number < sectionid))
4039 cur_line = cur_line->next;
4043 if ( (cur_line == NULL)
4044 || (cur_line->type != FILE_LINE_ACTION))
4046 /* Invalid "sectionid" parameter */
4047 edit_free_file(file);
4048 return JB_ERR_CGI_PARAMS;
4051 /* Skip through the section to find the last line in it. */
4052 while ( (cur_line->next != NULL)
4053 && (cur_line->next->type != FILE_LINE_ACTION) )
4055 cur_line = cur_line->next;
4060 /* At this point, the last line in the previous section is in cur_line
4061 * - add after this. (Or if we need to add as the first line, cur_line
4065 new_text = strdup("{}");
4066 if (NULL == new_text)
4068 edit_free_file(file);
4069 return JB_ERR_MEMORY;
4072 /* Allocate the new line */
4073 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4074 if (new_line == NULL)
4077 edit_free_file(file);
4078 return JB_ERR_MEMORY;
4081 /* Fill in the data members of the new line */
4082 new_line->raw = NULL;
4083 new_line->prefix = NULL;
4084 new_line->unprocessed = new_text;
4085 new_line->type = FILE_LINE_ACTION;
4087 if (cur_line != NULL)
4089 /* Link new_line into the list, after cur_line */
4090 new_line->next = cur_line->next;
4091 cur_line->next = new_line;
4095 /* Link new_line into the list, as first line */
4096 new_line->next = file->lines;
4097 file->lines = new_line;
4100 /* Done making changes, now commit */
4102 err = edit_write_file(file);
4105 /* Error writing file */
4106 if (err == JB_ERR_FILE)
4108 /* Read-only file. */
4109 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4111 edit_free_file(file);
4115 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4116 string_append(&target, file->identifier);
4118 edit_free_file(file);
4123 return JB_ERR_MEMORY;
4126 rsp->status = strdup("302 Local Redirect from Privoxy");
4127 if (rsp->status == NULL)
4130 return JB_ERR_MEMORY;
4132 err = enlist_unique_header(rsp->headers, "Location", target);
4139 /*********************************************************************
4141 * Function : cgi_edit_actions_section_swap
4143 * Description : CGI function that swaps the order of two sections
4144 * in the actions file. Note that this CGI can actually
4145 * swap any two arbitrary sections, but the GUI interface
4146 * currently only allows consecutive sections to be
4150 * 1 : csp = Current client state (buffers, headers, etc...)
4151 * 2 : rsp = http_response data structure for output
4152 * 3 : parameters = map of cgi parameters
4155 * f : (filename) Identifies the file to edit
4156 * v : (version) File's last-modified time
4157 * s1 : (section1) Line number of first section to swap
4158 * s2 : (section2) Line number of second section to swap
4160 * Returns : JB_ERR_OK on success
4161 * JB_ERR_MEMORY on out-of-memory
4162 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4163 * specified or not valid.
4165 *********************************************************************/
4166 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4167 struct http_response *rsp,
4168 const struct map *parameters)
4172 struct editable_file * file;
4173 struct file_line * cur_line;
4174 struct file_line * prev_line;
4175 struct file_line * line_before_section1;
4176 struct file_line * line_start_section1;
4177 struct file_line * line_end_section1;
4178 struct file_line * line_after_section1;
4179 struct file_line * line_before_section2;
4180 struct file_line * line_start_section2;
4181 struct file_line * line_end_section2;
4182 struct file_line * line_after_section2;
4183 unsigned line_number;
4187 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4189 return cgi_error_disabled(csp, rsp);
4192 err = get_number_param(csp, parameters, "s1", §ion1);
4193 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4199 if (section1 > section2)
4201 unsigned temp = section2;
4202 section2 = section1;
4206 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4209 /* No filename specified, can't read file, modified, or out of memory. */
4210 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4213 /* Start at the beginning... */
4215 cur_line = file->lines;
4218 /* ... find section1 ... */
4219 while ((cur_line != NULL) && (line_number < section1))
4221 prev_line = cur_line;
4222 cur_line = cur_line->next;
4226 if ( (cur_line == NULL)
4227 || (cur_line->type != FILE_LINE_ACTION) )
4229 /* Invalid "section1" parameter */
4230 edit_free_file(file);
4231 return JB_ERR_CGI_PARAMS;
4234 /* If no-op, we've validated params and can skip the rest. */
4235 if (section1 != section2)
4237 /* ... find the end of section1 ... */
4238 line_before_section1 = prev_line;
4239 line_start_section1 = cur_line;
4242 prev_line = cur_line;
4243 cur_line = cur_line->next;
4246 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4247 line_end_section1 = prev_line;
4248 line_after_section1 = cur_line;
4250 /* ... find section2 ... */
4251 while ((cur_line != NULL) && (line_number < section2))
4253 prev_line = cur_line;
4254 cur_line = cur_line->next;
4258 if ( (cur_line == NULL)
4259 || (cur_line->type != FILE_LINE_ACTION) )
4261 /* Invalid "section2" parameter */
4262 edit_free_file(file);
4263 return JB_ERR_CGI_PARAMS;
4266 /* ... find the end of section2 ... */
4267 line_before_section2 = prev_line;
4268 line_start_section2 = cur_line;
4271 prev_line = cur_line;
4272 cur_line = cur_line->next;
4275 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4276 line_end_section2 = prev_line;
4277 line_after_section2 = cur_line;
4279 /* Now have all the pointers we need. Do the swap. */
4281 /* Change the pointer to section1 to point to section2 instead */
4282 if (line_before_section1 == NULL)
4284 file->lines = line_start_section2;
4288 line_before_section1->next = line_start_section2;
4291 if (line_before_section2 == line_end_section1)
4293 /* Consecutive sections */
4294 line_end_section2->next = line_start_section1;
4298 line_end_section2->next = line_after_section1;
4299 line_before_section2->next = line_start_section1;
4302 /* Set the pointer from the end of section1 to the rest of the file */
4303 line_end_section1->next = line_after_section2;
4305 err = edit_write_file(file);
4308 /* Error writing file */
4309 if (err == JB_ERR_FILE)
4311 /* Read-only file. */
4312 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4314 edit_free_file(file);
4317 } /* END if (section1 != section2) */
4319 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4320 string_append(&target, file->identifier);
4322 edit_free_file(file);
4327 return JB_ERR_MEMORY;
4330 rsp->status = strdup("302 Local Redirect from Privoxy");
4331 if (rsp->status == NULL)
4334 return JB_ERR_MEMORY;
4336 err = enlist_unique_header(rsp->headers, "Location", target);
4343 /*********************************************************************
4345 * Function : cgi_toggle
4347 * Description : CGI function that adds a new empty section to
4351 * 1 : csp = Current client state (buffers, headers, etc...)
4352 * 2 : rsp = http_response data structure for output
4353 * 3 : parameters = map of cgi parameters
4356 * set : If present, how to change toggle setting:
4357 * "enable", "disable", "toggle", or none (default).
4358 * mini : If present, use mini reply template.
4360 * Returns : JB_ERR_OK on success
4361 * JB_ERR_MEMORY on out-of-memory
4363 *********************************************************************/
4364 jb_err cgi_toggle(struct client_state *csp,
4365 struct http_response *rsp,
4366 const struct map *parameters)
4368 struct map *exports;
4370 const char *template_name;
4376 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4378 return cgi_error_disabled(csp, rsp);
4381 mode = get_char_param(parameters, "set");
4388 else if (mode == 'D')
4393 else if (mode == 'T')
4396 g_bToggleIJB = !g_bToggleIJB;
4399 if (NULL == (exports = default_exports(csp, "toggle")))
4401 return JB_ERR_MEMORY;
4404 template_name = (get_char_param(parameters, "mini")
4408 return template_fill_for_cgi(csp, template_name, exports, rsp);
4412 /*********************************************************************
4414 * Function : javascriptify
4416 * Description : Converts a string into a form JavaScript will like.
4418 * Netscape 4's JavaScript sucks - it doesn't use
4419 * "id" parameters, so you have to set the "name"
4420 * used to submit a form element to something JavaScript
4421 * will like. (Or access the elements by index in an
4422 * array. That array contains >60 elements and will
4423 * be changed whenever we add a new action to the
4424 * editor, so I'm NOT going to use indexes that have
4425 * to be figured out by hand.)
4427 * Currently the only thing we have to worry about
4428 * is "-" ==> "_" conversion.
4430 * This is a length-preserving operation so it is
4431 * carried out in-place, no memory is allocated
4435 * 1 : identifier = String to make JavaScript-friendly.
4439 *********************************************************************/
4440 static void javascriptify(char * identifier)
4442 char * p = identifier;
4443 while (NULL != (p = strchr(p, '-')))
4450 /*********************************************************************
4452 * Function : actions_to_radio
4454 * Description : Converts a actionsfile entry into settings for
4455 * radio buttons and edit boxes on a HTML form.
4458 * 1 : exports = List of substitutions to add to.
4459 * 2 : action = Action to read
4461 * Returns : JB_ERR_OK on success
4462 * JB_ERR_MEMORY on out-of-memory
4464 *********************************************************************/
4465 static jb_err actions_to_radio(struct map * exports,
4466 const struct action_spec *action)
4468 unsigned mask = action->mask;
4469 unsigned add = action->add;
4477 mask = action->mask;
4480 /* sanity - prevents "-feature +feature" */
4484 #define DEFINE_ACTION_BOOL(name, bit) \
4485 if (!(mask & bit)) \
4487 current_mode = 'n'; \
4489 else if (add & bit) \
4491 current_mode = 'y'; \
4495 current_mode = 'x'; \
4497 if (map_radio(exports, name, "ynx", current_mode)) \
4499 return JB_ERR_MEMORY; \
4502 #define DEFINE_ACTION_STRING(name, bit, index) \
4503 DEFINE_ACTION_BOOL(name, bit); \
4506 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4509 checked = !strcmp(action->string[index], value); \
4513 checked = is_default; \
4515 mapped_param |= checked; \
4516 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4518 return JB_ERR_MEMORY; \
4521 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4522 if (map(exports, name "-param-custom", 1, \
4523 ((!mapped_param) ? "checked" : ""), 1)) \
4525 return JB_ERR_MEMORY; \
4527 if (map(exports, name "-param", 1, \
4528 (((add & bit) && !mapped_param) ? \
4529 action->string[index] : default_val), 1)) \
4531 return JB_ERR_MEMORY; \
4534 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4535 if (map(exports, name "-param", 1, \
4536 ((add & bit) ? action->string[index] : default_val), 1)) \
4538 return JB_ERR_MEMORY; \
4541 #define DEFINE_ACTION_MULTI(name, index) \
4542 if (action->multi_add[index]->first) \
4544 current_mode = 'y'; \
4546 else if (action->multi_remove_all[index]) \
4548 current_mode = 'n'; \
4550 else if (action->multi_remove[index]->first) \
4552 current_mode = 'y'; \
4556 current_mode = 'x'; \
4558 if (map_radio(exports, name, "ynx", current_mode)) \
4560 return JB_ERR_MEMORY; \
4563 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4565 #include "actionlist.h"
4567 #undef DEFINE_ACTION_MULTI
4568 #undef DEFINE_ACTION_STRING
4569 #undef DEFINE_ACTION_BOOL
4570 #undef DEFINE_ACTION_ALIAS
4571 #undef DEFINE_CGI_PARAM_CUSTOM
4572 #undef DEFINE_CGI_PARAM_RADIO
4573 #undef DEFINE_CGI_PARAM_NO_RADIO
4579 /*********************************************************************
4581 * Function : actions_from_radio
4583 * Description : Converts a map of parameters passed to a CGI function
4584 * into an actionsfile entry.
4587 * 1 : parameters = parameters to the CGI call
4588 * 2 : action = Action to change. Must be valid before
4589 * the call, actions not specified will be
4592 * Returns : JB_ERR_OK on success
4593 * JB_ERR_MEMORY on out-of-memory
4595 *********************************************************************/
4596 static jb_err actions_from_radio(const struct map * parameters,
4597 struct action_spec *action)
4602 const char * js_name;
4603 jb_err err = JB_ERR_OK;
4608 /* Statics are generally a potential race condition,
4609 * but in this case we're safe and don't need semaphores.
4610 * Be careful if you modify this function.
4612 * The js_name_arr's are never free()d, but this is no
4613 * problem, since they will only be created once and
4614 * used by all threads thereafter. -oes
4617 #define JAVASCRIPTIFY(dest_var, string) \
4619 static int first_time = 1; \
4620 static char *js_name_arr; \
4623 js_name_arr = strdup(string); \
4624 javascriptify(js_name_arr); \
4626 dest_var = js_name_arr; \
4630 #define DEFINE_ACTION_BOOL(name, bit) \
4631 JAVASCRIPTIFY(js_name, name); \
4632 ch = get_char_param(parameters, js_name); \
4635 action->add |= bit; \
4636 action->mask |= bit; \
4638 else if (ch == 'N') \
4640 action->add &= ~bit; \
4641 action->mask &= ~bit; \
4643 else if (ch == 'X') \
4645 action->add &= ~bit; \
4646 action->mask |= bit; \
4649 #define DEFINE_ACTION_STRING(name, bit, index) \
4650 JAVASCRIPTIFY(js_name, name); \
4651 ch = get_char_param(parameters, js_name); \
4655 JAVASCRIPTIFY(js_name, name "-mode"); \
4656 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4657 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4659 JAVASCRIPTIFY(js_name, name "-param"); \
4660 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4662 if (param != NULL) \
4664 if (NULL == (param_dup = strdup(param))) \
4666 return JB_ERR_MEMORY; \
4668 freez(action->string[index]); \
4669 action->add |= bit; \
4670 action->mask |= bit; \
4671 action->string[index] = param_dup; \
4674 else if (ch == 'N') \
4676 if (action->add & bit) \
4678 freez(action->string[index]); \
4680 action->add &= ~bit; \
4681 action->mask &= ~bit; \
4683 else if (ch == 'X') \
4685 if (action->add & bit) \
4687 freez(action->string[index]); \
4689 action->add &= ~bit; \
4690 action->mask |= bit; \
4693 #define DEFINE_ACTION_MULTI(name, index) \
4694 JAVASCRIPTIFY(js_name, name); \
4695 ch = get_char_param(parameters, js_name); \
4700 else if (ch == 'N') \
4702 list_remove_all(action->multi_add[index]); \
4703 list_remove_all(action->multi_remove[index]); \
4704 action->multi_remove_all[index] = 1; \
4706 else if (ch == 'X') \
4708 list_remove_all(action->multi_add[index]); \
4709 list_remove_all(action->multi_remove[index]); \
4710 action->multi_remove_all[index] = 0; \
4713 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4715 #include "actionlist.h"
4717 #undef DEFINE_ACTION_MULTI
4718 #undef DEFINE_ACTION_STRING
4719 #undef DEFINE_ACTION_BOOL
4720 #undef DEFINE_ACTION_ALIAS
4721 #undef JAVASCRIPTIFY
4727 #endif /* def FEATURE_CGI_EDIT_ACTIONS */