1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.60 2008/03/15 14:52:35 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
10 * NOTE: The CGIs in this file use parameter names
11 * such as "f" and "s" which are really *BAD* choices.
12 * However, I'm trying to save bytes in the
13 * edit-actions-list HTML page - the standard actions
14 * file generated a 550kbyte page, which is ridiculous.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001-2008 the SourceForge
19 * Privoxy team. http://www.privoxy.org/
21 * Based on the Internet Junkbuster originally written
22 * by and Copyright (C) 1997 Anonymous Coders and
23 * Junkbusters Corporation. http://www.junkbusters.com
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General
27 * Public License as published by the Free Software
28 * Foundation; either version 2 of the License, or (at
29 * your option) any later version.
31 * This program is distributed in the hope that it will
32 * be useful, but WITHOUT ANY WARRANTY; without even the
33 * implied warranty of MERCHANTABILITY or FITNESS FOR A
34 * PARTICULAR PURPOSE. See the GNU General Public
35 * License for more details.
37 * The GNU General Public License should be included with
38 * this file. If not, you can view it at
39 * http://www.gnu.org/copyleft/gpl.html
40 * or write to the Free Software Foundation, Inc., 59
41 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 * Revision 1.60 2008/03/15 14:52:35 fabiankeil
46 * Add CGI editor support for the "disable all filters of this type"
47 * directives "-client-header-filter", "-server-header-filter",
48 * "-client-header-tagger" and "-server-header-tagger".
50 * Revision 1.59 2008/03/08 16:25:56 fabiankeil
51 * After three file modification time mismatches, turn the CGI editor off.
53 * Revision 1.58 2007/11/28 17:57:01 fabiankeil
54 * Fix double free in cgi_edit_actions_list().
55 * Reported by adlab in BR#1840145.
57 * Revision 1.57 2007/10/27 13:32:23 fabiankeil
58 * Plug minor 5-year-old memory leak. Spotted by
59 * Valgrind and triggered by Privoxy-Regression-Test.
61 * Revision 1.56 2007/08/05 13:47:03 fabiankeil
62 * #1763173 from Stefan Huehner: s@const static@static const@.
64 * Revision 1.55 2007/05/31 11:50:20 fabiankeil
65 * Re-enable support for old-school URLs like
66 * http://config.privoxy.org/edit-actions-list?f=default
67 * in the action editor.
69 * They are no longer used by the CGI pages, but make it easier
70 * to reach the editor directly, without knowing the requested
71 * file's index in csp->config->actions_file[].
73 * Revision 1.54 2007/05/14 10:33:51 fabiankeil
74 * - Use strlcpy() and strlcat() instead of strcpy() and strcat().
76 * Revision 1.53 2007/04/15 16:39:20 fabiankeil
77 * Introduce tags as alternative way to specify which
78 * actions apply to a request. At the moment tags can be
79 * created based on client and server headers.
81 * Revision 1.52 2007/04/12 10:41:23 fabiankeil
82 * - Don't mistake VC++'s _snprintf() for a snprintf() replacement.
83 * - Move some cgi_edit_actions_for_url() variables into structs.
84 * - Remove bogus comment.
86 * Revision 1.51 2007/04/08 13:21:05 fabiankeil
87 * Reference action files in CGI URLs by id instead
88 * of using the first part of the file name.
89 * Fixes BR 1694250 and BR 1590556.
91 * Revision 1.50 2007/03/29 11:40:34 fabiankeil
92 * Divide @filter-params@ into @client-header-filter-params@
93 * @content-filter-params@ and @server-header-filter-params@.
95 * Revision 1.49 2007/03/20 15:16:34 fabiankeil
96 * Use dedicated header filter actions instead of abusing "filter".
97 * Replace "filter-client-headers" and "filter-client-headers"
98 * with "server-header-filter" and "client-header-filter".
100 * Revision 1.48 2007/02/13 14:35:25 fabiankeil
101 * Replace hash escaping code to prevent
102 * crashes, memory and file corruption.
104 * Revision 1.47 2006/12/28 18:04:25 fabiankeil
105 * Fixed gcc43 conversion warnings.
107 * Revision 1.46 2006/12/27 18:44:52 fabiankeil
108 * Stop shadowing string.h's index().
110 * Revision 1.45 2006/12/21 12:57:48 fabiankeil
111 * Add config option "split-large-forms"
112 * to work around the browser bug reported
115 * Revision 1.44 2006/12/09 13:49:16 fabiankeil
116 * Fix configure option --disable-toggle.
117 * Thanks to Peter Thoenen for reporting this.
119 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
120 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
121 * with what was really the latest development (the v_3_0_branch branch)
123 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
124 * Remove a little residual debugging info
126 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
127 * Multiple filter file support
129 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
130 * Undo some damaging memory leak patches
132 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
133 * Removing a double free
135 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
136 * A few memory leaks plugged (mostly on error paths)
138 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
139 * Moved cgi_error_disabled() from cgiedit.c to
140 * cgi.c to re-enable build with --disable-editor.
141 * Fixes Bug #892744. Thanks to Matthew Fischer
144 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
145 * One line lost in last commit
147 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
148 * - All edit functions that redirect back to the list page
149 * now use cgi_redirect
150 * - All redirects now contain useless parameter "foo", whose
151 * value are raw seconds since epoch, in order to force
152 * Opera and Konqueror to properly reload the list. Closes
155 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
156 * Cosmetic: Renamed cryptic variable
158 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
159 * Fix: Don't free uninitialized struct editable_file
161 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
162 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
164 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
165 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
167 * Revision 1.41 2002/05/21 19:09:45 oes
168 * - Made Add/Edit/Remove URL Submit and Cancel
169 * buttons jump back to relevant section in eal
170 * - Bugfix: remove-url-form needs p export
172 * Revision 1.40 2002/05/19 11:34:35 jongfoster
173 * Handling read-only actions files better - report the actual
174 * error, not "Out of memory"!
177 * http://sourceforge.net/tracker/index.php?func=detail
178 * &aid=557905&group_id=11118&atid=111118
180 * Revision 1.39 2002/05/12 21:39:15 jongfoster
181 * - Adding Doxygen-style comments to structures and #defines.
182 * - Correcting function comments
184 * Revision 1.38 2002/05/03 23:00:38 jongfoster
185 * Support for templates for "standard actions" buttons.
188 * Revision 1.37 2002/04/30 11:14:52 oes
189 * Made csp the first parameter in *action_to_html
191 * Revision 1.36 2002/04/26 21:53:30 jongfoster
192 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
194 * Revision 1.35 2002/04/26 21:50:02 jongfoster
195 * Honouring default exports in edit-actions-for-url-filter template.
197 * Revision 1.34 2002/04/26 12:54:17 oes
198 * Adaptions to changes in actions.c
200 * Revision 1.33 2002/04/24 02:17:47 oes
201 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
203 * - Activated Jon's code for editing multiple AFs
204 * - cgi_edit_list_actions now provides context-sensitive
205 * help, looks up all action sets from standard.action and
206 * makes buttons for them in the catchall section
207 * - cgi_edit_action_submit now honors a p parameter, looks up
208 * the corresponding action set, and sets the catchall pattern's
209 * actions accordingly.
211 * Revision 1.32 2002/04/19 16:55:31 jongfoster
212 * Fixing newline problems. If we do our own text file newline
213 * mangling, we don't want the library to do any, so we need to
214 * open the files in *binary* mode.
216 * Revision 1.31 2002/04/18 19:21:08 jongfoster
217 * Added code to detect "conventional" action files, that start
218 * with a set of actions for all URLs (the pattern "/").
219 * These are special-cased in the "edit-actions-list" CGI, so
220 * that a special UI can be written for them.
222 * Revision 1.30 2002/04/10 13:38:35 oes
223 * load_template signature changed
225 * Revision 1.29 2002/04/08 16:59:08 oes
228 * Revision 1.28 2002/03/27 12:30:29 oes
229 * Deleted unsused variable
231 * Revision 1.27 2002/03/26 23:06:04 jongfoster
232 * Removing duplicate @ifs on the toggle page
234 * Revision 1.26 2002/03/26 22:59:17 jongfoster
235 * Fixing /toggle to display status consistently.
237 * Revision 1.25 2002/03/26 22:29:54 swa
238 * we have a new homepage!
240 * Revision 1.24 2002/03/24 15:23:33 jongfoster
243 * Revision 1.23 2002/03/24 13:32:41 swa
244 * name change related issues
246 * Revision 1.22 2002/03/24 13:25:43 swa
247 * name change related issues
249 * Revision 1.21 2002/03/22 18:02:48 jongfoster
250 * Fixing remote toggle
252 * Revision 1.20 2002/03/16 20:28:34 oes
253 * Added descriptions to the filters so users will know what they select in the cgi editor
255 * Revision 1.19 2002/03/16 18:38:14 jongfoster
256 * Stopping stupid or malicious users from breaking the actions
257 * file using the web-based editor.
259 * Revision 1.18 2002/03/16 14:57:44 jongfoster
260 * Full support for enabling/disabling modular filters.
262 * Revision 1.17 2002/03/16 14:26:42 jongfoster
263 * First version of modular filters support - READ ONLY!
264 * Fixing a double-free bug in the out-of-memory handling in map_radio().
266 * Revision 1.16 2002/03/07 03:46:17 oes
267 * Fixed compiler warnings
269 * Revision 1.15 2002/03/06 22:54:35 jongfoster
270 * Automated function-comment nitpicking.
272 * Revision 1.14 2002/03/05 00:24:51 jongfoster
273 * Patch to always edit the current actions file.
275 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
276 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
278 * Revision 1.12 2002/03/03 09:18:03 joergs
279 * Made jumbjuster work on AmigaOS again.
281 * Revision 1.11 2002/01/23 01:03:31 jongfoster
282 * Fixing gcc [CygWin] compiler warnings
284 * Revision 1.10 2002/01/23 00:22:59 jongfoster
285 * Adding new function cgi_edit_actions_section_swap(), to reorder
288 * Adding get_url_spec_param() to get a validated URL pattern.
290 * Moving edit_read_line() out of this file and into loaders.c.
292 * Adding missing html_encode() to many CGI functions.
294 * Moving the functions that #include actionlist.h to the end of the file,
295 * because the Visual C++ 97 debugger gets extremely confused if you try
296 * to debug any code that comes after them in the file.
298 * Major optimizations in cgi_edit_actions_list() to reduce the size of
299 * the generated HTML (down 40% from 550k to 304k), with major side-effects
300 * throughout the editor and templates. In particular, the length of the
301 * URLs throughout the editor has been drastically reduced, by cutting
302 * paramater names down to 1 character and CGI names down to 3-4
303 * characters, by removing all non-essential CGI paramaters even at the
304 * expense of having to re-read the actions file for the most trivial
305 * page, and by using relative rather than absolute URLs. This means
306 * that this (typical example):
308 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
309 * filename=ijb&ver=1011487572&section=12&pattern=13
310 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
314 * <a href="eau?f=ijb&v=1011487572&p=13">
316 * Revision 1.9 2002/01/17 20:56:22 jongfoster
317 * Replacing hard references to the URL of the config interface
318 * with #defines from project.h
320 * Revision 1.8 2001/11/30 23:35:51 jongfoster
321 * Renaming actionsfile to ijb.action
323 * Revision 1.7 2001/11/13 00:28:24 jongfoster
324 * - Renaming parameters from edit-actions-for-url so that they only
325 * contain legal JavaScript characters. If we wanted to write
326 * JavaScript that worked with Netscape 4, this is nessacery.
327 * (Note that at the moment the JavaScript doesn't actually work
328 * with Netscape 4, but now this is purely a template issue, not
329 * one affecting code).
330 * - Adding new CGIs for use by non-JavaScript browsers:
331 * edit-actions-url-form
332 * edit-actions-add-url-form
333 * edit-actions-remove-url-form
336 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
337 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
338 * by and __OS2__ ifdef.
340 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
341 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
342 * threads to call select() simultaneously. So, it's time to do a real, live,
343 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
344 * (native). Both versions will work, but using __OS2__ offers multi-threading.
346 * Revision 1.4 2001/10/23 21:48:19 jongfoster
347 * Cleaning up error handling in CGI functions - they now send back
348 * a HTML error page and should never cause a FATAL error. (Fixes one
349 * potential source of "denial of service" attacks).
351 * CGI actions file editor that works and is actually useful.
353 * Ability to toggle JunkBuster remotely using a CGI call.
355 * You can turn off both the above features in the main configuration
356 * file, e.g. if you are running a multi-user proxy.
358 * Revision 1.3 2001/10/14 22:12:49 jongfoster
359 * New version of CGI-based actionsfile editor.
360 * Major changes, including:
361 * - Completely new file parser and file output routines
362 * - edit-actions CGI renamed edit-actions-for-url
363 * - All CGIs now need a filename parameter, except for...
364 * - New CGI edit-actions which doesn't need a filename,
365 * to allow you to start the editor up.
366 * - edit-actions-submit now works, and now automatically
367 * redirects you back to the main edit-actions-list handler.
369 * Revision 1.2 2001/09/16 17:05:14 jongfoster
370 * Removing unused #include showarg.h
372 * Revision 1.1 2001/09/16 15:47:37 jongfoster
373 * First version of CGI-based edit interface. This is very much a
374 * work-in-progress, and you can't actually use it to edit anything
375 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
376 * to have any effect.
379 **********************************************************************/
385 * FIXME: Following includes copied from cgi.c - which are actually needed?
390 #include <sys/types.h>
394 #include <sys/stat.h>
399 #include "cgisimple.h"
403 #include "miscutil.h"
406 #ifdef FEATURE_TOGGLE
407 /* loadcfg.h is for global_toggle_state only */
409 #endif /* def FEATURE_TOGGLE */
410 #include "urlmatch.h"
412 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
415 #ifdef FEATURE_CGI_EDIT_ACTIONS
418 * A line in an editable_file.
422 /** Next entry in the linked list */
423 struct file_line * next;
425 /** The raw data, to write out if this line is unmodified. */
428 /** Comments and/or whitespace to put before this line if it's modified
429 and then written out. */
432 /** The actual data, as a string. Line continuation and comment removal
433 are performed on the data read from file before it's stored here, so
434 it will be a single line of data. */
437 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
440 /** The actual data, processed into some sensible data type. */
444 /** An action specification. */
445 struct action_spec action[1];
447 /** A name=value pair. */
451 /** The name in the name=value pair. */
454 /** The value in the name=value pair, as a string. */
457 /** The value in the name=value pair, as an integer. */
462 /* Add more data types here... e.g.
465 struct url_spec url[1];
469 struct action_spec action[1];
479 /** This file_line has not been processed yet. */
480 #define FILE_LINE_UNPROCESSED 1
482 /** This file_line is blank. Can only appear at the end of a file, due to
483 the way the parser works. */
484 #define FILE_LINE_BLANK 2
486 /** This file_line says {{alias}}. */
487 #define FILE_LINE_ALIAS_HEADER 3
489 /** This file_line defines an alias. */
490 #define FILE_LINE_ALIAS_ENTRY 4
492 /** This file_line defines an {action}. */
493 #define FILE_LINE_ACTION 5
495 /** This file_line specifies a URL pattern. */
496 #define FILE_LINE_URL 6
498 /** This file_line says {{settings}}. */
499 #define FILE_LINE_SETTINGS_HEADER 7
501 /** This file_line is in a {{settings}} block. */
502 #define FILE_LINE_SETTINGS_ENTRY 8
504 /** This file_line says {{description}}. */
505 #define FILE_LINE_DESCRIPTION_HEADER 9
507 /** This file_line is in a {{description}} block. */
508 #define FILE_LINE_DESCRIPTION_ENTRY 10
511 * Number of file modification time mismatches
512 * before the CGI editor gets turned off.
514 #define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
517 * A configuration file, in a format that can be edited and written back to
522 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
523 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
524 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
525 const char * version_str; /**< Last modification time, as a string. For CGI param. */
526 /**< Can be used in URL without using url_param(). */
527 unsigned version; /**< Last modification time - prevents chaos with
528 the browser's "back" button. Note that this is a
529 time_t cast to an unsigned. When comparing, always
530 cast the time_t to an unsigned, and *NOT* vice-versa.
531 This may lose the top few bits, but they're not
532 significant anyway. */
533 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
534 Note that changing this after the file has been
535 read in will cause a mess. */
536 struct file_line * parse_error; /**< On parse error, this is the offending line. */
537 const char * parse_error_text; /**< On parse error, this is the problem.
538 (Statically allocated) */
542 * Information about the filter types.
543 * Used for macro replacement in cgi_edit_actions_for_url.
545 struct filter_type_info
547 const int multi_action_index; /**< The multi action index as defined in project.h */
548 const char *macro_name; /**< Name of the macro that has to be replaced
549 with the prepared templates.
550 For example "content-filter-params" */
551 const char *type; /**< Name of the filter type,
552 for example "server-header-filter". */
553 /* XXX: check if these two can be combined. */
554 const char *disable_all_option; /**< Name of the catch-all radio option that has
555 to be checked or unchecked for this filter type. */
556 const char *disable_all_param; /**< Name of the parameter that causes all filters of
557 this type to be disabled. */
558 const char *abbr_type; /**< Abbreviation of the filter type, usually the
559 first or second character capitalized */
560 const char *anchor; /**< Anchor for the User Manual link,
561 for example "SERVER-HEADER-FILTER" */
564 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
565 static const struct filter_type_info filter_type_info[] =
569 "content-filter-params", "filter",
570 "filter-all", "filter_all",
574 ACTION_MULTI_CLIENT_HEADER_FILTER,
575 "client-header-filter-params", "client-header-filter",
576 "client-header-filter-all", "client_header_filter_all",
577 "C", "CLIENT-HEADER-FILTER"
580 ACTION_MULTI_SERVER_HEADER_FILTER,
581 "server-header-filter-params", "server-header-filter",
582 "server-header-filter-all", "server_header_filter_all",
583 "S", "SERVER-HEADER-FILTER"
586 ACTION_MULTI_CLIENT_HEADER_TAGGER,
587 "client-header-tagger-params", "client-header-tagger",
588 "client-header-tagger-all", "client_header_tagger_all",
589 "L", "CLIENT-HEADER-TAGGER"
592 ACTION_MULTI_SERVER_HEADER_TAGGER,
593 "server-header-tagger-params", "server-header-tagger",
594 "server-header-tagger-all", "server_header_tagger_all",
595 "E", "SERVER-HEADER-TAGGER"
599 /* FIXME: Following non-static functions should be prototyped in .h or made static */
601 /* Functions to read and write arbitrary config files */
602 jb_err edit_read_file(struct client_state *csp,
603 const struct map *parameters,
605 struct editable_file **pfile);
606 jb_err edit_write_file(struct editable_file * file);
607 void edit_free_file(struct editable_file * file);
609 /* Functions to read and write actions files */
610 jb_err edit_parse_actions_file(struct editable_file * file);
611 jb_err edit_read_actions_file(struct client_state *csp,
612 struct http_response *rsp,
613 const struct map *parameters,
615 struct editable_file **pfile);
618 jb_err cgi_error_modified(struct client_state *csp,
619 struct http_response *rsp,
620 const char *filename);
621 jb_err cgi_error_parse(struct client_state *csp,
622 struct http_response *rsp,
623 struct editable_file *file);
624 jb_err cgi_error_file(struct client_state *csp,
625 struct http_response *rsp,
626 const char *filename);
627 jb_err cgi_error_file_read_only(struct client_state *csp,
628 struct http_response *rsp,
629 const char *filename);
631 /* Internal arbitrary config file support functions */
632 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
633 static void edit_free_file_lines(struct file_line * first_line);
635 /* Internal actions file support functions */
636 static int match_actions_file_header_line(const char * line, const char * name);
637 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
639 /* Internal parameter parsing functions */
640 static jb_err get_url_spec_param(struct client_state *csp,
641 const struct map *parameters,
646 /* Internal actionsfile <==> HTML conversion functions */
647 static jb_err map_radio(struct map * exports,
648 const char * optionname,
651 static jb_err actions_to_radio(struct map * exports,
652 const struct action_spec *action);
653 static jb_err actions_from_radio(const struct map * parameters,
654 struct action_spec *action);
657 static jb_err map_copy_parameter_html(struct map *out,
658 const struct map *in,
660 #if 0 /* unused function */
661 static jb_err map_copy_parameter_url(struct map *out,
662 const struct map *in,
664 #endif /* unused function */
666 static jb_err get_file_name_param(struct client_state *csp,
667 const struct map *parameters,
668 const char *param_name,
669 const char **pfilename);
671 /* Internal convenience functions */
672 static char *section_target(const unsigned sectionid);
674 /*********************************************************************
676 * Function : section_target
678 * Description : Given an unsigned (section id) n, produce a dynamically
679 * allocated string of the form #l<n>, for use in link
682 * XXX: The hash should be moved into the templates
683 * to make this function more generic and render
684 * stringify() obsolete.
687 * 1 : sectionid = start line number of section
689 * Returns : String with link target, or NULL if out of
692 *********************************************************************/
693 static char *section_target(const unsigned sectionid)
697 snprintf(buf, sizeof(buf), "#l%d", sectionid);
703 /*********************************************************************
705 * Function : stringify
707 * Description : Convert a number into a dynamically allocated string.
710 * 1 : number = The number to convert.
712 * Returns : String with link target, or NULL if out of memory
714 *********************************************************************/
715 static char *stringify(const unsigned number)
719 snprintf(buf, sizeof(buf), "%i", number);
724 /*********************************************************************
726 * Function : map_copy_parameter_html
728 * Description : Copy a CGI parameter from one map to another, HTML
732 * 1 : out = target map
733 * 2 : in = source map
734 * 3 : name = name of cgi parameter to copy
736 * Returns : JB_ERR_OK on success
737 * JB_ERR_MEMORY on out-of-memory
738 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
741 *********************************************************************/
742 static jb_err map_copy_parameter_html(struct map *out,
743 const struct map *in,
753 value = lookup(in, name);
754 err = map(out, name, 1, html_encode(value), 0);
761 else if (*value == '\0')
763 return JB_ERR_CGI_PARAMS;
772 #if 0 /* unused function */
773 /*********************************************************************
775 * Function : map_copy_parameter_url
777 * Description : Copy a CGI parameter from one map to another, URL
781 * 1 : out = target map
782 * 2 : in = source map
783 * 3 : name = name of cgi parameter to copy
785 * Returns : JB_ERR_OK on success
786 * JB_ERR_MEMORY on out-of-memory
787 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
790 *********************************************************************/
791 static jb_err map_copy_parameter_url(struct map *out,
792 const struct map *in,
802 value = lookup(in, name);
803 err = map(out, name, 1, url_encode(value), 0);
810 else if (*value == '\0')
812 return JB_ERR_CGI_PARAMS;
819 #endif /* 0 - unused function */
822 /*********************************************************************
824 * Function : cgi_edit_actions_url_form
826 * Description : CGI function that displays a form for
830 * 1 : csp = Current client state (buffers, headers, etc...)
831 * 2 : rsp = http_response data structure for output
832 * 3 : parameters = map of cgi parameters
835 * i : (action index) Identifies the file to edit
836 * v : (version) File's last-modified time
837 * p : (pattern) Line number of pattern to edit
839 * Returns : JB_ERR_OK on success
840 * JB_ERR_MEMORY on out-of-memory
841 * JB_ERR_CGI_PARAMS if the CGI parameters are not
842 * specified or not valid.
844 *********************************************************************/
845 jb_err cgi_edit_actions_url_form(struct client_state *csp,
846 struct http_response *rsp,
847 const struct map *parameters)
849 struct map * exports;
851 struct editable_file * file;
852 struct file_line * cur_line;
853 unsigned line_number;
854 unsigned section_start_line_number = 0;
861 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
863 return cgi_error_disabled(csp, rsp);
866 err = get_number_param(csp, parameters, "p", &patternid);
872 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
875 /* No filename specified, can't read file, modified, or out of memory. */
876 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
879 cur_line = file->lines;
881 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
883 if (cur_line->type == FILE_LINE_ACTION)
885 section_start_line_number = line_number;
887 cur_line = cur_line->next;
890 if ( (cur_line == NULL)
891 || (line_number != patternid)
893 || (cur_line->type != FILE_LINE_URL))
895 /* Invalid "patternid" parameter */
896 edit_free_file(file);
897 return JB_ERR_CGI_PARAMS;
900 if (NULL == (exports = default_exports(csp, NULL)))
902 edit_free_file(file);
903 return JB_ERR_MEMORY;
906 err = map(exports, "f", 1, stringify(file->identifier), 0);
907 if (!err) err = map(exports, "v", 1, file->version_str, 1);
908 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
909 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
910 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
912 edit_free_file(file);
920 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
924 /*********************************************************************
926 * Function : cgi_edit_actions_add_url_form
928 * Description : CGI function that displays a form for
932 * 1 : csp = Current client state (buffers, headers, etc...)
933 * 2 : rsp = http_response data structure for output
934 * 3 : parameters = map of cgi parameters
937 * f : (filename) Identifies the file to edit
938 * v : (version) File's last-modified time
939 * s : (section) Line number of section to edit
941 * Returns : JB_ERR_OK on success
942 * JB_ERR_MEMORY on out-of-memory
943 * JB_ERR_CGI_PARAMS if the CGI parameters are not
944 * specified or not valid.
946 *********************************************************************/
947 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
948 struct http_response *rsp,
949 const struct map *parameters)
958 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
960 return cgi_error_disabled(csp, rsp);
963 if (NULL == (exports = default_exports(csp, NULL)))
965 return JB_ERR_MEMORY;
968 err = map_copy_parameter_html(exports, parameters, "f");
969 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
970 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
978 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
982 /*********************************************************************
984 * Function : cgi_edit_actions_remove_url_form
986 * Description : CGI function that displays a form for
990 * 1 : csp = Current client state (buffers, headers, etc...)
991 * 2 : rsp = http_response data structure for output
992 * 3 : parameters = map of cgi parameters
995 * f : (number) The action file identifier.
996 * v : (version) File's last-modified time
997 * p : (pattern) Line number of pattern to edit
999 * Returns : JB_ERR_OK on success
1000 * JB_ERR_MEMORY on out-of-memory
1001 * JB_ERR_CGI_PARAMS if the CGI parameters are not
1002 * specified or not valid.
1004 *********************************************************************/
1005 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
1006 struct http_response *rsp,
1007 const struct map *parameters)
1009 struct map * exports;
1011 struct editable_file * file;
1012 struct file_line * cur_line;
1013 unsigned line_number;
1014 unsigned section_start_line_number = 0;
1021 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1023 return cgi_error_disabled(csp, rsp);
1026 err = get_number_param(csp, parameters, "p", &patternid);
1032 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
1035 /* No filename specified, can't read file, modified, or out of memory. */
1036 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
1039 cur_line = file->lines;
1041 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
1043 if (cur_line->type == FILE_LINE_ACTION)
1045 section_start_line_number = line_number;
1047 cur_line = cur_line->next;
1050 if ( (cur_line == NULL)
1051 || (line_number != patternid)
1053 || (cur_line->type != FILE_LINE_URL))
1055 /* Invalid "patternid" parameter */
1056 edit_free_file(file);
1057 return JB_ERR_CGI_PARAMS;
1060 if (NULL == (exports = default_exports(csp, NULL)))
1062 edit_free_file(file);
1063 return JB_ERR_MEMORY;
1066 err = map(exports, "f", 1, stringify(file->identifier), 0);
1067 if (!err) err = map(exports, "v", 1, file->version_str, 1);
1068 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
1069 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
1070 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
1071 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
1073 edit_free_file(file);
1081 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
1085 /*********************************************************************
1087 * Function : edit_write_file
1089 * Description : Write a complete file to disk.
1092 * 1 : file = File to write.
1094 * Returns : JB_ERR_OK on success
1095 * JB_ERR_FILE on error writing to file.
1096 * JB_ERR_MEMORY on out of memory
1098 *********************************************************************/
1099 jb_err edit_write_file(struct editable_file * file)
1102 struct file_line * cur_line;
1103 struct stat statbuf[1];
1104 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
1105 digits in time_t, assuming this is a 64-bit
1106 machine, plus null terminator, plus one
1110 assert(file->filename);
1112 if (NULL == (fp = fopen(file->filename, "wb")))
1117 cur_line = file->lines;
1118 while (cur_line != NULL)
1122 if (fputs(cur_line->raw, fp) < 0)
1130 if (cur_line->prefix)
1132 if (fputs(cur_line->prefix, fp) < 0)
1138 if (cur_line->unprocessed)
1141 if (NULL != strchr(cur_line->unprocessed, '#'))
1143 /* Must quote '#' characters */
1150 /* Count number of # characters, so we know length of output string */
1151 src = cur_line->unprocessed;
1152 while (NULL != (src = strchr(src, '#')))
1157 assert(numhash > 0);
1159 /* Allocate new memory for string */
1160 len = strlen(cur_line->unprocessed) + (size_t)numhash;
1161 if (NULL == (str = malloc(len + 1)))
1163 /* Uh oh, just trashed file! */
1165 return JB_ERR_MEMORY;
1168 /* Copy string but quote hashes */
1169 src = cur_line->unprocessed;
1177 assert(numhash >= 0);
1183 assert(numhash == 0);
1184 assert(strlen(str) == len);
1185 assert(str == dest - len);
1186 assert(src - len <= cur_line->unprocessed);
1188 if ((strlen(str) != len) || (numhash != 0))
1191 * Escaping didn't work as expected, go spread the news.
1192 * Only reached in non-debugging builds.
1194 log_error(LOG_LEVEL_ERROR,
1195 "Looks like hash escaping failed. %s might be corrupted now.",
1199 if (fputs(str, fp) < 0)
1210 /* Can write without quoting '#' characters. */
1211 if (fputs(cur_line->unprocessed, fp) < 0)
1217 if (fputs(NEWLINE(file->newline), fp) < 0)
1225 /* FIXME: Write data from file->data->whatever */
1229 cur_line = cur_line->next;
1235 /* Update the version stamp in the file structure, since we just
1236 * wrote to the file & changed it's date.
1238 if (stat(file->filename, statbuf) < 0)
1240 /* Error, probably file not found. */
1243 file->version = (unsigned)statbuf->st_mtime;
1245 /* Correct file->version_str */
1246 freez(file->version_str);
1247 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1248 version_buf[sizeof(version_buf)-1] = '\0';
1249 file->version_str = strdup(version_buf);
1250 if (version_buf == NULL)
1252 return JB_ERR_MEMORY;
1259 /*********************************************************************
1261 * Function : edit_free_file
1263 * Description : Free a complete file in memory.
1266 * 1 : file = Data structure to free.
1270 *********************************************************************/
1271 void edit_free_file(struct editable_file * file)
1275 /* Silently ignore NULL pointer */
1279 edit_free_file_lines(file->lines);
1280 freez(file->version_str);
1282 file->parse_error_text = NULL; /* Statically allocated */
1283 file->parse_error = NULL;
1289 /*********************************************************************
1291 * Function : edit_free_file_lines
1293 * Description : Free an entire linked list of file lines.
1296 * 1 : first_line = Data structure to free.
1300 *********************************************************************/
1301 static void edit_free_file_lines(struct file_line * first_line)
1303 struct file_line * next_line;
1305 while (first_line != NULL)
1307 next_line = first_line->next;
1308 first_line->next = NULL;
1309 freez(first_line->raw);
1310 freez(first_line->prefix);
1311 freez(first_line->unprocessed);
1312 switch(first_line->type)
1314 case 0: /* special case if memory zeroed */
1315 case FILE_LINE_UNPROCESSED:
1316 case FILE_LINE_BLANK:
1317 case FILE_LINE_ALIAS_HEADER:
1318 case FILE_LINE_SETTINGS_HEADER:
1319 case FILE_LINE_DESCRIPTION_HEADER:
1320 case FILE_LINE_DESCRIPTION_ENTRY:
1321 case FILE_LINE_ALIAS_ENTRY:
1323 /* No data is stored for these */
1326 case FILE_LINE_ACTION:
1327 free_action(first_line->data.action);
1330 case FILE_LINE_SETTINGS_ENTRY:
1331 freez(first_line->data.setting.name);
1332 freez(first_line->data.setting.svalue);
1335 /* Should never happen */
1339 first_line->type = 0; /* paranoia */
1341 first_line = next_line;
1346 /*********************************************************************
1348 * Function : match_actions_file_header_line
1350 * Description : Match an actions file {{header}} line
1353 * 1 : line = String from file
1354 * 2 : name = Header to match against
1356 * Returns : 0 iff they match.
1358 *********************************************************************/
1359 static int match_actions_file_header_line(const char * line, const char * name)
1367 if ((line[0] != '{') || (line[1] != '{'))
1373 /* Look for optional whitespace */
1374 while ( (*line == ' ') || (*line == '\t') )
1379 /* Look for the specified name (case-insensitive) */
1381 if (0 != strncmpic(line, name, len))
1387 /* Look for optional whitespace */
1388 while ( (*line == ' ') || (*line == '\t') )
1393 /* Look for "}}" and end of string*/
1394 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1404 /*********************************************************************
1406 * Function : match_actions_file_header_line
1408 * Description : Match an actions file {{header}} line
1411 * 1 : line = String from file. Must not start with
1412 * whitespace (else infinite loop!)
1413 * 2 : pname = Destination for name
1414 * 2 : pvalue = Destination for value
1416 * Returns : JB_ERR_OK on success
1417 * JB_ERR_MEMORY on out-of-memory
1418 * JB_ERR_PARSE if there's no "=" sign, or if there's
1419 * nothing before the "=" sign (but empty
1420 * values *after* the "=" sign are legal).
1422 *********************************************************************/
1423 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1425 const char * name_end;
1426 const char * value_start;
1432 assert(*line != ' ');
1433 assert(*line != '\t');
1438 value_start = strchr(line, '=');
1439 if ((value_start == NULL) || (value_start == line))
1441 return JB_ERR_PARSE;
1444 name_end = value_start - 1;
1446 /* Eat any whitespace before the '=' */
1447 while ((*name_end == ' ') || (*name_end == '\t'))
1450 * we already know we must have at least 1 non-ws char
1451 * at start of buf - no need to check
1456 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1457 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1459 return JB_ERR_MEMORY;
1461 strncpy(*pname, line, name_len);
1462 (*pname)[name_len] = '\0';
1464 /* Eat any the whitespace after the '=' */
1466 while ((*value_start == ' ') || (*value_start == '\t'))
1471 if (NULL == (*pvalue = strdup(value_start)))
1475 return JB_ERR_MEMORY;
1482 /*********************************************************************
1484 * Function : edit_parse_actions_file
1486 * Description : Parse an actions file in memory.
1488 * Passed linked list must have the "data" member
1489 * zeroed, and must contain valid "next" and
1490 * "unprocessed" fields. The "raw" and "prefix"
1491 * fields are ignored, and "type" is just overwritten.
1493 * Note that on error the file may have been
1497 * 1 : file = Actions file to be parsed in-place.
1499 * Returns : JB_ERR_OK on success
1500 * JB_ERR_MEMORY on out-of-memory
1501 * JB_ERR_PARSE on error
1503 *********************************************************************/
1504 jb_err edit_parse_actions_file(struct editable_file * file)
1506 struct file_line * cur_line;
1508 const char * text; /* Text from a line */
1509 char * name; /* For lines of the form name=value */
1510 char * value; /* For lines of the form name=value */
1511 struct action_alias * alias_list = NULL;
1512 jb_err err = JB_ERR_OK;
1514 /* alias_list contains the aliases defined in this file.
1515 * It might be better to use the "file_line.data" fields
1516 * in the relavent places instead.
1519 cur_line = file->lines;
1521 /* A note about blank line support: Blank lines should only
1522 * ever occur as the last line in the file. This function
1523 * is more forgiving than that - FILE_LINE_BLANK can occur
1527 /* Skip leading blanks. Should only happen if file is
1528 * empty (which is valid, but pointless).
1530 while ( (cur_line != NULL)
1531 && (cur_line->unprocessed[0] == '\0') )
1534 cur_line->type = FILE_LINE_BLANK;
1535 cur_line = cur_line->next;
1538 if ( (cur_line != NULL)
1539 && (cur_line->unprocessed[0] != '{') )
1541 /* File doesn't start with a header */
1542 file->parse_error = cur_line;
1543 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1544 return JB_ERR_PARSE;
1547 if ( (cur_line != NULL) && (0 ==
1548 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1550 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1552 cur_line = cur_line->next;
1553 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1555 if (cur_line->unprocessed[0])
1557 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1559 err = split_line_on_equals(cur_line->unprocessed,
1560 &cur_line->data.setting.name,
1561 &cur_line->data.setting.svalue);
1562 if (err == JB_ERR_MEMORY)
1566 else if (err != JB_ERR_OK)
1568 /* Line does not contain a name=value pair */
1569 file->parse_error = cur_line;
1570 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1571 return JB_ERR_PARSE;
1576 cur_line->type = FILE_LINE_BLANK;
1578 cur_line = cur_line->next;
1582 if ( (cur_line != NULL) && (0 ==
1583 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1585 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1587 cur_line = cur_line->next;
1588 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1590 if (cur_line->unprocessed[0])
1592 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1596 cur_line->type = FILE_LINE_BLANK;
1598 cur_line = cur_line->next;
1602 if ( (cur_line != NULL) && (0 ==
1603 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1605 cur_line->type = FILE_LINE_ALIAS_HEADER;
1607 cur_line = cur_line->next;
1608 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1610 if (cur_line->unprocessed[0])
1612 /* define an alias */
1613 struct action_alias * new_alias;
1615 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1617 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1618 if (err == JB_ERR_MEMORY)
1622 else if (err != JB_ERR_OK)
1624 /* Line does not contain a name=value pair */
1625 file->parse_error = cur_line;
1626 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1627 return JB_ERR_PARSE;
1630 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1635 free_alias_list(alias_list);
1636 return JB_ERR_MEMORY;
1639 err = get_actions(value, alias_list, new_alias->action);
1642 /* Invalid action or out of memory */
1646 free_alias_list(alias_list);
1647 if (err == JB_ERR_MEMORY)
1653 /* Line does not contain a name=value pair */
1654 file->parse_error = cur_line;
1655 file->parse_error_text = "This alias does not specify a valid set of actions.";
1656 return JB_ERR_PARSE;
1662 new_alias->name = name;
1665 new_alias->next = alias_list;
1666 alias_list = new_alias;
1670 cur_line->type = FILE_LINE_BLANK;
1672 cur_line = cur_line->next;
1676 /* Header done, process the main part of the file */
1677 while (cur_line != NULL)
1679 /* At this point, (cur_line->unprocessed[0] == '{') */
1680 assert(cur_line->unprocessed[0] == '{');
1681 text = cur_line->unprocessed + 1;
1682 len = strlen(text) - 1;
1683 if (text[len] != '}')
1685 /* No closing } on header */
1686 free_alias_list(alias_list);
1687 file->parse_error = cur_line;
1688 file->parse_error_text = "Headers starting with '{' must have a "
1689 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1690 "must close with two brackets ('}}').";
1691 return JB_ERR_PARSE;
1696 /* An invalid {{ header. */
1697 free_alias_list(alias_list);
1698 file->parse_error = cur_line;
1699 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1700 "Please remember that the system (two-bracket) headers must "
1701 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1702 "and must appear before any actions (one-bracket) headers. "
1703 "Also note that system headers may not be repeated.";
1704 return JB_ERR_PARSE;
1707 while ( (*text == ' ') || (*text == '\t') )
1713 && ( (text[len - 1] == ' ')
1714 || (text[len - 1] == '\t') ) )
1719 cur_line->type = FILE_LINE_ACTION;
1721 /* Remove {} and make copy */
1722 if (NULL == (value = (char *) malloc(len + 1)))
1725 free_alias_list(alias_list);
1726 return JB_ERR_MEMORY;
1728 strncpy(value, text, len);
1732 err = get_actions(value, alias_list, cur_line->data.action);
1735 /* Invalid action or out of memory */
1737 free_alias_list(alias_list);
1738 if (err == JB_ERR_MEMORY)
1744 /* Line does not contain a name=value pair */
1745 file->parse_error = cur_line;
1746 file->parse_error_text = "This header does not specify a valid set of actions.";
1747 return JB_ERR_PARSE;
1751 /* Done with string - it was clobbered anyway */
1754 /* Process next line */
1755 cur_line = cur_line->next;
1757 /* Loop processing URL patterns */
1758 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1760 if (cur_line->unprocessed[0])
1762 /* Could parse URL here, but this isn't currently needed */
1764 cur_line->type = FILE_LINE_URL;
1768 cur_line->type = FILE_LINE_BLANK;
1770 cur_line = cur_line->next;
1772 } /* End main while(cur_line != NULL) loop */
1774 free_alias_list(alias_list);
1780 /*********************************************************************
1782 * Function : edit_read_file_lines
1784 * Description : Read all the lines of a file into memory.
1785 * Handles whitespace, comments and line continuation.
1788 * 1 : fp = File to read from. On return, this will be
1789 * at EOF but it will not have been closed.
1790 * 2 : pfile = Destination for a linked list of file_lines.
1791 * Will be set to NULL on error.
1792 * 3 : newline = How to handle newlines.
1794 * Returns : JB_ERR_OK on success
1795 * JB_ERR_MEMORY on out-of-memory
1797 *********************************************************************/
1798 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1800 struct file_line * first_line; /* Keep for return value or to free */
1801 struct file_line * cur_line; /* Current line */
1802 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1810 cur_line = first_line = zalloc(sizeof(struct file_line));
1811 if (cur_line == NULL)
1813 return JB_ERR_MEMORY;
1816 cur_line->type = FILE_LINE_UNPROCESSED;
1818 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1821 /* Out of memory or empty file. */
1822 /* Note that empty file is not an error we propogate up */
1824 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1829 prev_line = cur_line;
1830 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1831 if (cur_line == NULL)
1834 edit_free_file_lines(first_line);
1835 return JB_ERR_MEMORY;
1838 cur_line->type = FILE_LINE_UNPROCESSED;
1840 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1841 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1844 edit_free_file_lines(first_line);
1845 return JB_ERR_MEMORY;
1849 while (rval != JB_ERR_FILE);
1853 /* We allocated one too many - free it */
1854 prev_line->next = NULL;
1857 *pfile = first_line;
1862 /*********************************************************************
1864 * Function : edit_read_file
1866 * Description : Read a complete file into memory.
1867 * Handles CGI parameter parsing. If requested, also
1868 * checks the file's modification timestamp.
1871 * 1 : csp = Current client state (buffers, headers, etc...)
1872 * 2 : parameters = map of cgi parameters.
1873 * 3 : require_version = true to check "ver" parameter.
1874 * 4 : pfile = Destination for the file. Will be set
1878 * f : The action file identifier.
1879 * ver : (Only if require_version is nonzero)
1880 * Timestamp of the actions file. If wrong, this
1881 * function fails with JB_ERR_MODIFIED.
1883 * Returns : JB_ERR_OK on success
1884 * JB_ERR_MEMORY on out-of-memory
1885 * JB_ERR_CGI_PARAMS if "filename" was not specified
1887 * JB_ERR_FILE if the file cannot be opened or
1889 * JB_ERR_MODIFIED if version checking was requested and
1890 * failed - the file was modified outside
1891 * of this CGI editor instance.
1893 *********************************************************************/
1894 jb_err edit_read_file(struct client_state *csp,
1895 const struct map *parameters,
1896 int require_version,
1897 struct editable_file **pfile)
1899 struct file_line * lines;
1902 const char *filename = NULL;
1903 struct editable_file * file;
1904 unsigned version = 0;
1905 struct stat statbuf[1];
1906 char version_buf[22];
1907 int newline = NEWLINE_UNKNOWN;
1916 err = get_number_param(csp, parameters, "f", &i);
1917 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1919 filename = csp->config->actions_file[i];
1921 else if (JB_ERR_CGI_PARAMS == err)
1924 * Probably an old-school URL like
1925 * http://config.privoxy.org/edit-actions-list?f=default
1927 err = get_file_name_param(csp, parameters, "f", &filename);
1930 if (NULL == filename || stat(filename, statbuf) < 0)
1932 /* Error, probably file not found. */
1935 version = (unsigned) statbuf->st_mtime;
1937 if (require_version)
1939 unsigned specified_version;
1940 err = get_number_param(csp, parameters, "v", &specified_version);
1946 if (version != specified_version)
1948 return JB_ERR_MODIFIED;
1952 if (NULL == (fp = fopen(filename,"rb")))
1957 err = edit_read_file_lines(fp, &lines, &newline);
1966 file = (struct editable_file *) zalloc(sizeof(*file));
1969 edit_free_file_lines(lines);
1973 file->lines = lines;
1974 file->newline = newline;
1975 file->filename = filename;
1976 file->version = version;
1977 file->identifier = i;
1979 /* Correct file->version_str */
1980 freez(file->version_str);
1981 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1982 version_buf[sizeof(version_buf)-1] = '\0';
1983 file->version_str = strdup(version_buf);
1984 if (version_buf == NULL)
1986 edit_free_file(file);
1987 return JB_ERR_MEMORY;
1995 /*********************************************************************
1997 * Function : edit_read_actions_file
1999 * Description : Read a complete actions file into memory.
2000 * Handles CGI parameter parsing. If requested, also
2001 * checks the file's modification timestamp.
2003 * If this function detects an error in the categories
2004 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
2005 * then it handles it by filling in the specified
2006 * response structure and returning JB_ERR_FILE.
2009 * 1 : csp = Current client state (buffers, headers, etc...)
2010 * 2 : rsp = HTTP response. Only filled in on error.
2011 * 2 : parameters = map of cgi parameters.
2012 * 3 : require_version = true to check "ver" parameter.
2013 * 4 : pfile = Destination for the file. Will be set
2017 * f : The actions file identifier.
2018 * ver : (Only if require_version is nonzero)
2019 * Timestamp of the actions file. If wrong, this
2020 * function fails with JB_ERR_MODIFIED.
2022 * Returns : JB_ERR_OK on success
2023 * JB_ERR_MEMORY on out-of-memory
2024 * JB_ERR_CGI_PARAMS if "filename" was not specified
2026 * JB_ERR_FILE if the file does not contain valid data,
2027 * or if file cannot be opened or
2028 * contains no data, or if version
2029 * checking was requested and failed.
2031 *********************************************************************/
2032 jb_err edit_read_actions_file(struct client_state *csp,
2033 struct http_response *rsp,
2034 const struct map *parameters,
2035 int require_version,
2036 struct editable_file **pfile)
2039 struct editable_file *file;
2040 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
2048 err = edit_read_file(csp, parameters, require_version, &file);
2051 /* Try to handle if possible */
2052 if (err == JB_ERR_FILE)
2054 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
2056 else if (err == JB_ERR_MODIFIED)
2058 assert(require_version);
2059 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
2060 log_error(LOG_LEVEL_ERROR,
2061 "Blocking CGI edit request due to modification time mismatch.");
2062 if (acceptable_failures > 0)
2064 log_error(LOG_LEVEL_INFO,
2065 "The CGI editor will be turned off after another %d mismatche(s).",
2066 acceptable_failures);
2067 acceptable_failures--;
2071 log_error(LOG_LEVEL_INFO,
2072 "Timestamp mismatch limit reached, turning CGI editor off. "
2073 "Reload the configuration file to reenable it.");
2074 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
2077 if (err == JB_ERR_OK)
2080 * Signal to higher-level CGI code that there was a problem but we
2081 * handled it, they should just return JB_ERR_OK.
2088 err = edit_parse_actions_file(file);
2091 if (err == JB_ERR_PARSE)
2093 err = cgi_error_parse(csp, rsp, file);
2094 if (err == JB_ERR_OK)
2097 * Signal to higher-level CGI code that there was a problem but we
2098 * handled it, they should just return JB_ERR_OK.
2103 edit_free_file(file);
2112 /*********************************************************************
2114 * Function : get_file_name_param
2116 * Description : Get the name of the file to edit from the parameters
2117 * passed to a CGI function using the old syntax.
2118 * This function handles security checks and only
2119 * accepts files that Privoxy already knows.
2122 * 1 : csp = Current client state (buffers, headers, etc...)
2123 * 2 : parameters = map of cgi parameters
2124 * 3 : param_name = The name of the parameter to read
2125 * 4 : pfilename = pointer to the filename in
2126 * csp->config->actions_file[] if found. Set to NULL on error.
2128 * Returns : JB_ERR_OK on success
2129 * JB_ERR_MEMORY on out-of-memory
2130 * JB_ERR_CGI_PARAMS if "filename" was not specified
2133 *********************************************************************/
2134 static jb_err get_file_name_param(struct client_state *csp,
2135 const struct map *parameters,
2136 const char *param_name,
2137 const char **pfilename)
2140 const char suffix[] = ".action";
2155 param = lookup(parameters, param_name);
2158 return JB_ERR_CGI_PARAMS;
2161 len = strlen(param);
2162 if (len >= FILENAME_MAX)
2165 return JB_ERR_CGI_PARAMS;
2169 * Check every character to see if it's legal.
2170 * Totally unnecessary but we do it anyway.
2173 while ((ch = *s++) != '\0')
2175 if ( ((ch < 'A') || (ch > 'Z'))
2176 && ((ch < 'a') || (ch > 'z'))
2177 && ((ch < '0') || (ch > '9'))
2181 /* Probable hack attempt. */
2182 return JB_ERR_CGI_PARAMS;
2186 /* Append extension */
2187 name_size = len + strlen(suffix) + 1;
2188 name = malloc(name_size);
2191 return JB_ERR_MEMORY;
2193 strlcpy(name, param, name_size);
2194 strlcat(name, suffix, name_size);
2197 fullpath = make_path(csp->config->confdir, name);
2200 if (fullpath == NULL)
2202 return JB_ERR_MEMORY;
2205 /* Check if the file is known */
2206 for (i = 0; i < MAX_AF_FILES; i++)
2208 if (NULL != csp->config->actions_file[i] &&
2209 !strcmp(fullpath, csp->config->actions_file[i]))
2212 *pfilename = csp->config->actions_file[i];
2220 return JB_ERR_CGI_PARAMS;
2224 /*********************************************************************
2226 * Function : get_url_spec_param
2228 * Description : Get a URL pattern from the parameters
2229 * passed to a CGI function. Removes leading/trailing
2230 * spaces and validates it.
2233 * 1 : csp = Current client state (buffers, headers, etc...)
2234 * 2 : parameters = map of cgi parameters
2235 * 3 : name = Name of CGI parameter to read
2236 * 4 : pvalue = destination for value. Will be malloc()'d.
2237 * Set to NULL on error.
2239 * Returns : JB_ERR_OK on success
2240 * JB_ERR_MEMORY on out-of-memory
2241 * JB_ERR_CGI_PARAMS if the parameter was not specified
2244 *********************************************************************/
2245 static jb_err get_url_spec_param(struct client_state *csp,
2246 const struct map *parameters,
2250 const char *orig_param;
2253 struct url_spec compiled[1];
2263 orig_param = lookup(parameters, name);
2266 return JB_ERR_CGI_PARAMS;
2269 /* Copy and trim whitespace */
2270 param = strdup(orig_param);
2273 return JB_ERR_MEMORY;
2277 /* Must be non-empty, and can't allow 1st character to be '{' */
2278 if (param[0] == '\0' || param[0] == '{')
2281 return JB_ERR_CGI_PARAMS;
2284 /* Check for embedded newlines */
2285 for (s = param; *s != '\0'; s++)
2287 if ((*s == '\r') || (*s == '\n'))
2290 return JB_ERR_CGI_PARAMS;
2294 /* Check that regex is valid */
2299 return JB_ERR_MEMORY;
2301 err = create_url_spec(compiled, s);
2306 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2308 free_url_spec(compiled);
2310 if (param[strlen(param) - 1] == '\\')
2313 * Must protect trailing '\\' from becoming line continuation character.
2314 * Two methods: 1) If it's a domain only, add a trailing '/'.
2315 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2317 if (strchr(param, '/') == NULL)
2319 err = string_append(¶m, "/");
2323 err = string_append(¶m, "(?:)");
2330 /* Check that the modified regex is valid */
2335 return JB_ERR_MEMORY;
2337 err = create_url_spec(compiled, s);
2342 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2344 free_url_spec(compiled);
2351 /*********************************************************************
2353 * Function : map_radio
2355 * Description : Map a set of radio button values. E.g. if you have
2356 * 3 radio buttons, declare them as:
2357 * <option type="radio" name="xyz" @xyz-a@>
2358 * <option type="radio" name="xyz" @xyz-b@>
2359 * <option type="radio" name="xyz" @xyz-c@>
2360 * Then map one of the @xyz-?@ variables to "checked"
2361 * and all the others to empty by calling:
2362 * map_radio(exports, "xyz", "abc", sel)
2363 * Where 'sel' is 'a', 'b', or 'c'.
2366 * 1 : exports = Exports map to modify.
2367 * 2 : optionname = name for map
2368 * 3 : values = null-terminated list of values;
2369 * 4 : value = Selected value.
2371 * CGI Parameters : None
2373 * Returns : JB_ERR_OK on success
2374 * JB_ERR_MEMORY on out-of-memory
2376 *********************************************************************/
2377 static jb_err map_radio(struct map * exports,
2378 const char * optionname,
2379 const char * values,
2385 const size_t len = strlen(optionname);
2386 const size_t buf_size = len + 3;
2392 buf = malloc(buf_size);
2395 return JB_ERR_MEMORY;
2398 strlcpy(buf, optionname, buf_size);
2400 /* XXX: this looks ... interesting */
2405 while ((c = *values++) != '\0')
2410 if (map(exports, buf, 1, "", 1))
2412 return JB_ERR_MEMORY;
2418 return map(exports, buf, 0, "checked", 1);
2422 /*********************************************************************
2424 * Function : cgi_error_modified
2426 * Description : CGI function that is called when a file is modified
2427 * outside the CGI editor.
2430 * 1 : csp = Current client state (buffers, headers, etc...)
2431 * 2 : rsp = http_response data structure for output
2432 * 3 : filename = The file that was modified.
2434 * CGI Parameters : none
2436 * Returns : JB_ERR_OK on success
2437 * JB_ERR_MEMORY on out-of-memory error.
2439 *********************************************************************/
2440 jb_err cgi_error_modified(struct client_state *csp,
2441 struct http_response *rsp,
2442 const char *filename)
2444 struct map *exports;
2451 if (NULL == (exports = default_exports(csp, NULL)))
2453 return JB_ERR_MEMORY;
2456 err = map(exports, "f", 1, html_encode(filename), 0);
2463 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2467 /*********************************************************************
2469 * Function : cgi_error_parse
2471 * Description : CGI function that is called when a file cannot
2472 * be parsed by the CGI editor.
2475 * 1 : csp = Current client state (buffers, headers, etc...)
2476 * 2 : rsp = http_response data structure for output
2477 * 3 : file = The file that was modified.
2479 * CGI Parameters : none
2481 * Returns : JB_ERR_OK on success
2482 * JB_ERR_MEMORY on out-of-memory error.
2484 *********************************************************************/
2485 jb_err cgi_error_parse(struct client_state *csp,
2486 struct http_response *rsp,
2487 struct editable_file *file)
2489 struct map *exports;
2491 struct file_line *cur_line;
2497 if (NULL == (exports = default_exports(csp, NULL)))
2499 return JB_ERR_MEMORY;
2502 err = map(exports, "f", 1, stringify(file->identifier), 0);
2503 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2505 cur_line = file->parse_error;
2508 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2509 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2517 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2521 /*********************************************************************
2523 * Function : cgi_error_file
2525 * Description : CGI function that is called when a file cannot be
2526 * opened by the CGI editor.
2529 * 1 : csp = Current client state (buffers, headers, etc...)
2530 * 2 : rsp = http_response data structure for output
2531 * 3 : filename = The file that was modified.
2533 * CGI Parameters : none
2535 * Returns : JB_ERR_OK on success
2536 * JB_ERR_MEMORY on out-of-memory error.
2538 *********************************************************************/
2539 jb_err cgi_error_file(struct client_state *csp,
2540 struct http_response *rsp,
2541 const char *filename)
2543 struct map *exports;
2550 if (NULL == (exports = default_exports(csp, NULL)))
2552 return JB_ERR_MEMORY;
2555 err = map(exports, "f", 1, html_encode(filename), 0);
2562 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2566 /*********************************************************************
2568 * Function : cgi_error_file_read_only
2570 * Description : CGI function that is called when a file cannot be
2571 * opened for writing by the CGI editor.
2574 * 1 : csp = Current client state (buffers, headers, etc...)
2575 * 2 : rsp = http_response data structure for output
2576 * 3 : filename = The file that we can't write to
2578 * CGI Parameters : none
2580 * Returns : JB_ERR_OK on success
2581 * JB_ERR_MEMORY on out-of-memory error.
2583 *********************************************************************/
2584 jb_err cgi_error_file_read_only(struct client_state *csp,
2585 struct http_response *rsp,
2586 const char *filename)
2588 struct map *exports;
2595 if (NULL == (exports = default_exports(csp, NULL)))
2597 return JB_ERR_MEMORY;
2600 err = map(exports, "f", 1, html_encode(filename), 0);
2607 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2611 /*********************************************************************
2613 * Function : cgi_edit_actions
2615 * Description : CGI function that allows the user to choose which
2616 * actions file to edit.
2619 * 1 : csp = Current client state (buffers, headers, etc...)
2620 * 2 : rsp = http_response data structure for output
2621 * 3 : parameters = map of cgi parameters
2623 * CGI Parameters : None
2625 * Returns : JB_ERR_OK on success
2626 * JB_ERR_MEMORY on out-of-memory error
2628 *********************************************************************/
2629 jb_err cgi_edit_actions(struct client_state *csp,
2630 struct http_response *rsp,
2631 const struct map *parameters)
2634 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2636 return cgi_error_disabled(csp, rsp);
2639 /* FIXME: Incomplete */
2641 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2646 /*********************************************************************
2648 * Function : cgi_edit_actions_list
2650 * Description : CGI function that edits the actions list.
2651 * FIXME: This function shouldn't FATAL ever.
2652 * FIXME: This function doesn't check the retval of map()
2654 * 1 : csp = Current client state (buffers, headers, etc...)
2655 * 2 : rsp = http_response data structure for output
2656 * 3 : parameters = map of cgi parameters
2658 * CGI Parameters : filename
2660 * Returns : JB_ERR_OK on success
2661 * JB_ERR_MEMORY on out-of-memory
2662 * JB_ERR_FILE if the file cannot be opened or
2664 * JB_ERR_CGI_PARAMS if "filename" was not specified
2667 *********************************************************************/
2668 jb_err cgi_edit_actions_list(struct client_state *csp,
2669 struct http_response *rsp,
2670 const struct map *parameters)
2672 char * section_template;
2673 char * url_template;
2678 struct map * exports;
2679 struct map * section_exports;
2680 struct map * url_exports;
2681 struct editable_file * file;
2682 struct file_line * cur_line;
2683 unsigned line_number = 0;
2684 unsigned prev_section_line_number = ((unsigned) (-1));
2686 struct file_list * fl;
2687 struct url_actions * b;
2688 char * buttons = NULL;
2691 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2693 return cgi_error_disabled(csp, rsp);
2696 if (NULL == (exports = default_exports(csp, NULL)))
2698 return JB_ERR_MEMORY;
2701 /* Load actions file */
2702 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2705 /* No filename specified, can't read file, or out of memory. */
2707 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2710 /* Find start of actions in file */
2711 cur_line = file->lines;
2713 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2715 cur_line = cur_line->next;
2720 * Conventional actions files should have a match all block
2722 * cur_line = {...global actions...}
2723 * cur_line->next = /
2724 * cur_line->next->next = {...actions...} or EOF
2726 if ( (cur_line != NULL)
2727 && (cur_line->type == FILE_LINE_ACTION)
2728 && (cur_line->next != NULL)
2729 && (cur_line->next->type == FILE_LINE_URL)
2730 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2731 && ( (cur_line->next->next == NULL)
2732 || (cur_line->next->next->type != FILE_LINE_URL)
2736 * Generate string with buttons to set actions for "/" to
2737 * any predefined set of actions (named standard.*, probably
2738 * residing in standard.action).
2741 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2744 edit_free_file(file);
2746 if (err == JB_ERR_FILE)
2748 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2753 err = template_fill(§ion_template, exports);
2756 edit_free_file(file);
2761 buttons = strdup("");
2762 for (i = 0; i < MAX_AF_FILES; i++)
2764 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2766 for (b = b->next; NULL != b; b = b->next)
2768 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2770 if (err || (NULL == (section_exports = new_map())))
2773 free(section_template);
2774 edit_free_file(file);
2776 return JB_ERR_MEMORY;
2779 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2781 if (err || (NULL == (s = strdup(section_template))))
2783 free_map(section_exports);
2785 free(section_template);
2786 edit_free_file(file);
2788 return JB_ERR_MEMORY;
2791 if (!err) err = template_fill(&s, section_exports);
2792 free_map(section_exports);
2793 if (!err) err = string_join(&buttons, s);
2798 freez(section_template);
2799 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2802 * Conventional actions file, supply extra editing help.
2803 * (e.g. don't allow them to make it an unconventional one).
2805 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2807 snprintf(buf, sizeof(buf), "%d", line_number);
2808 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2809 snprintf(buf, sizeof(buf), "%d", line_number + 2);
2810 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2811 if (!err) err = map(exports, "all-urls-actions", 1,
2812 actions_to_html(csp, cur_line->data.action), 0);
2814 /* Skip the 2 lines */
2815 cur_line = cur_line->next->next;
2819 * Note that prev_section_line_number is NOT set here.
2820 * This is deliberate and not a bug. It stops a "Move up"
2821 * option appearing on the next section. Clicking "Move
2822 * up" would make the actions file unconventional, which
2823 * we don't want, so we hide this option.
2829 * Non-standard actions file - does not begin with
2830 * the "All URLs" section.
2832 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2835 /* Set up global exports */
2837 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2838 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2839 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2841 /* Discourage private additions to default.action */
2843 if (!err) err = map_conditional(exports, "default-action",
2844 (strstr("default.action", file->filename) != NULL));
2847 edit_free_file(file);
2852 /* Should do all global exports above this point */
2854 /* Load templates */
2856 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2859 edit_free_file(file);
2861 if (err == JB_ERR_FILE)
2863 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2868 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2871 free(section_template);
2872 edit_free_file(file);
2874 if (err == JB_ERR_FILE)
2876 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2881 err = template_fill(§ion_template, exports);
2885 edit_free_file(file);
2890 err = template_fill(&url_template, exports);
2893 free(section_template);
2894 edit_free_file(file);
2899 if (NULL == (sections = strdup("")))
2901 free(section_template);
2903 edit_free_file(file);
2905 return JB_ERR_MEMORY;
2908 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2910 if (NULL == (section_exports = new_map()))
2913 free(section_template);
2915 edit_free_file(file);
2917 return JB_ERR_MEMORY;
2920 snprintf(buf, sizeof(buf), "%d", line_number);
2921 err = map(section_exports, "s", 1, buf, 1);
2922 if (!err) err = map(section_exports, "actions", 1,
2923 actions_to_html(csp, cur_line->data.action), 0);
2926 && (cur_line->next != NULL)
2927 && (cur_line->next->type == FILE_LINE_URL))
2929 /* This section contains at least one URL, don't allow delete */
2930 err = map_block_killer(section_exports, "empty-section");
2934 if (!err) err = map_block_keep(section_exports, "empty-section");
2937 if (prev_section_line_number != ((unsigned)(-1)))
2939 /* Not last section */
2940 snprintf(buf, sizeof(buf), "%d", prev_section_line_number);
2941 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2942 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2947 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2949 prev_section_line_number = line_number;
2954 free(section_template);
2956 edit_free_file(file);
2958 free_map(section_exports);
2962 /* Should do all section-specific exports above this point */
2964 if (NULL == (urls = strdup("")))
2967 free(section_template);
2969 edit_free_file(file);
2971 free_map(section_exports);
2972 return JB_ERR_MEMORY;
2977 cur_line = cur_line->next;
2980 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2982 if (NULL == (url_exports = new_map()))
2986 free(section_template);
2988 edit_free_file(file);
2990 free_map(section_exports);
2991 return JB_ERR_MEMORY;
2994 snprintf(buf, sizeof(buf), "%d", line_number);
2995 err = map(url_exports, "p", 1, buf, 1);
2997 snprintf(buf, sizeof(buf), "%d", url_1_2);
2998 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
3000 if (!err) err = map(url_exports, "url-html", 1,
3001 html_encode(cur_line->unprocessed), 0);
3002 if (!err) err = map(url_exports, "url", 1,
3003 url_encode(cur_line->unprocessed), 0);
3009 free(section_template);
3011 edit_free_file(file);
3013 free_map(section_exports);
3014 free_map(url_exports);
3018 if (NULL == (s = strdup(url_template)))
3022 free(section_template);
3024 edit_free_file(file);
3026 free_map(section_exports);
3027 free_map(url_exports);
3028 return JB_ERR_MEMORY;
3031 err = template_fill(&s, section_exports);
3032 if (!err) err = template_fill(&s, url_exports);
3033 if (!err) err = string_append(&urls, s);
3035 free_map(url_exports);
3042 free(section_template);
3044 edit_free_file(file);
3046 free_map(section_exports);
3050 url_1_2 = 3 - url_1_2;
3052 cur_line = cur_line->next;
3056 err = map(section_exports, "urls", 1, urls, 0);
3058 /* Could also do section-specific exports here, but it wouldn't be as fast */
3060 snprintf(buf, sizeof(buf), "%d", line_number);
3061 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
3063 if ( (cur_line != NULL)
3064 && (cur_line->type == FILE_LINE_ACTION))
3066 /* Not last section */
3067 if (!err) err = map_block_keep(section_exports, "s-next-exists");
3072 if (!err) err = map_block_killer(section_exports, "s-next-exists");
3078 free(section_template);
3080 edit_free_file(file);
3082 free_map(section_exports);
3086 if (NULL == (s = strdup(section_template)))
3089 free(section_template);
3091 edit_free_file(file);
3093 free_map(section_exports);
3094 return JB_ERR_MEMORY;
3097 err = template_fill(&s, section_exports);
3098 if (!err) err = string_append(§ions, s);
3101 free_map(section_exports);
3106 free(section_template);
3108 edit_free_file(file);
3114 edit_free_file(file);
3115 free(section_template);
3118 err = map(exports, "sections", 1, sections, 0);
3125 /* Could also do global exports here, but it wouldn't be as fast */
3127 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
3131 /*********************************************************************
3133 * Function : cgi_edit_actions_for_url
3135 * Description : CGI function that edits the Actions list.
3138 * 1 : csp = Current client state (buffers, headers, etc...)
3139 * 2 : rsp = http_response data structure for output
3140 * 3 : parameters = map of cgi parameters
3142 * CGI Parameters : None
3144 * Returns : JB_ERR_OK on success
3145 * JB_ERR_MEMORY on out-of-memory
3146 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3147 * specified or not valid.
3149 *********************************************************************/
3150 jb_err cgi_edit_actions_for_url(struct client_state *csp,
3151 struct http_response *rsp,
3152 const struct map *parameters)
3154 struct map * exports;
3156 struct editable_file * file;
3157 struct file_line * cur_line;
3158 unsigned line_number;
3160 struct re_filterfile_spec *filter_group;
3161 int i, have_filters = 0;
3163 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3165 return cgi_error_disabled(csp, rsp);
3168 err = get_number_param(csp, parameters, "s", §ionid);
3174 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3177 /* No filename specified, can't read file, modified, or out of memory. */
3178 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3181 cur_line = file->lines;
3183 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3185 cur_line = cur_line->next;
3188 if ( (cur_line == NULL)
3189 || (line_number != sectionid)
3191 || (cur_line->type != FILE_LINE_ACTION))
3193 /* Invalid "sectionid" parameter */
3194 edit_free_file(file);
3195 return JB_ERR_CGI_PARAMS;
3198 if (NULL == (exports = default_exports(csp, NULL)))
3200 edit_free_file(file);
3201 return JB_ERR_MEMORY;
3204 err = map(exports, "f", 1, stringify(file->identifier), 0);
3205 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3206 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3208 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3211 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3212 * length limitation and ignore clicks on the Submit buttons if
3213 * the resulting GET URL would be longer than their limit.
3215 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3216 * reached this limit and action editing stopped working in these
3217 * browsers (BR #1570678).
3219 * The config option split-large-forms works around this browser
3220 * bug (HTTP has no URL lenght limitation) by deviding the action
3221 * list form into multiple smaller ones. It means the URLs are shorter
3222 * and work in broken browsers as well, but the user can no longer change
3223 * all actions with one submit.
3225 * A better solution would be to switch to POST requests,
3226 * but this will do for now.
3228 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3230 /* Generate multiple smaller form by killing the big one. */
3231 err = map_block_killer(exports, "one-form-only");
3235 /* Default: Generate one large form by killing the smaller ones. */
3236 err = map_block_killer(exports, "multiple-forms");
3239 for (i = 0; i < MAX_AF_FILES; i++)
3241 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3243 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3251 edit_free_file(file);
3256 if (0 == have_filters)
3258 err = map(exports, "filter-params", 1, "", 1);
3263 * List available filters and their settings.
3265 char *filter_template;
3266 int filter_identifier = 0;
3267 char *prepared_templates[MAX_FILTER_TYPES];
3269 for (i = 0; i < MAX_FILTER_TYPES; i++)
3271 prepared_templates[i] = strdup("");
3274 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3277 edit_free_file(file);
3279 if (err == JB_ERR_FILE)
3281 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3286 err = template_fill(&filter_template, exports);
3288 for (i = 0; i < MAX_AF_FILES; i++)
3290 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3292 filter_group = csp->rlist[i]->f;
3293 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3295 char current_mode = 'x';
3297 struct list_entry *filter_name;
3298 struct map *line_exports;
3299 const int type = filter_group->type;
3300 const int multi_action_index = filter_type_info[type].multi_action_index;
3302 assert(type < MAX_FILTER_TYPES);
3304 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
3305 while ((filter_name != NULL)
3306 && (0 != strcmp(filter_group->name, filter_name->str)))
3308 filter_name = filter_name->next;
3311 if (filter_name != NULL)
3317 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
3318 while ((filter_name != NULL)
3319 && (0 != strcmp(filter_group->name, filter_name->str)))
3321 filter_name = filter_name->next;
3323 if (filter_name != NULL)
3329 /* Generate a unique serial number */
3330 snprintf(number, sizeof(number), "%x", filter_identifier++);
3331 number[sizeof(number) - 1] = '\0';
3333 line_exports = new_map();
3334 if (line_exports == NULL)
3336 err = JB_ERR_MEMORY;
3342 if (!err) err = map(line_exports, "index", 1, number, 1);
3343 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3344 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3345 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3346 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
3347 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
3348 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
3352 filter_line = strdup(filter_template);
3353 if (filter_line == NULL) err = JB_ERR_MEMORY;
3355 if (!err) err = template_fill(&filter_line, line_exports);
3356 string_join(&prepared_templates[type], filter_line);
3358 free_map(line_exports);
3363 freez(filter_template);
3365 /* Replace all filter macros with the aggregated templates */
3366 for (i = 0; i < MAX_FILTER_TYPES; i++)
3369 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
3374 /* Free aggregated templates */
3375 for (i = 0; i < MAX_FILTER_TYPES; i++)
3377 freez(prepared_templates[i]);
3382 /* Check or uncheck the "disable all of this type" radio buttons. */
3383 for (i = 0; i < MAX_FILTER_TYPES; i++)
3385 const int a = filter_type_info[i].multi_action_index;
3386 const int disable_all = cur_line->data.action->multi_remove_all[a];
3388 err = map_radio(exports, filter_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
3391 edit_free_file(file);
3399 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3403 /*********************************************************************
3405 * Function : cgi_edit_actions_submit
3407 * Description : CGI function that actually edits the Actions list.
3410 * 1 : csp = Current client state (buffers, headers, etc...)
3411 * 2 : rsp = http_response data structure for output
3412 * 3 : parameters = map of cgi parameters
3414 * CGI Parameters : None
3416 * Returns : JB_ERR_OK on success
3417 * JB_ERR_MEMORY on out-of-memory
3418 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3419 * specified or not valid.
3421 *********************************************************************/
3422 jb_err cgi_edit_actions_submit(struct client_state *csp,
3423 struct http_response *rsp,
3424 const struct map *parameters)
3429 size_t newtext_size;
3431 struct editable_file * file;
3432 struct file_line * cur_line;
3433 unsigned line_number;
3436 int filter_identifier;
3438 const char * action_set_name;
3439 struct file_list * fl;
3440 struct url_actions * b;
3442 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3444 return cgi_error_disabled(csp, rsp);
3447 err = get_number_param(csp, parameters, "s", §ionid);
3453 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3456 /* No filename specified, can't read file, modified, or out of memory. */
3457 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3460 cur_line = file->lines;
3462 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3464 cur_line = cur_line->next;
3467 if ( (cur_line == NULL)
3468 || (line_number != sectionid)
3470 || (cur_line->type != FILE_LINE_ACTION))
3472 /* Invalid "sectionid" parameter */
3473 edit_free_file(file);
3474 return JB_ERR_CGI_PARAMS;
3477 get_string_param(parameters, "p", &action_set_name);
3478 if (action_set_name != NULL)
3480 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3482 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3484 for (b = b->next; NULL != b; b = b->next)
3486 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3488 copy_action(cur_line->data.action, b->action);
3494 edit_free_file(file);
3495 return JB_ERR_CGI_PARAMS;
3501 err = actions_from_radio(parameters, cur_line->data.action);
3507 edit_free_file(file);
3511 /* Check the "disable all of this type" parameters. */
3512 for (i = 0; i < MAX_FILTER_TYPES; i++)
3514 const int multi_action_index = filter_type_info[i].multi_action_index;
3515 const char ch = get_char_param(parameters, filter_type_info[i].disable_all_param);
3519 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3520 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3521 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3525 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3529 for (filter_identifier = 0; !err; filter_identifier++)
3536 * Filter state. Valid states are: 'Y' (active),
3537 * 'N' (inactive) and 'X' (no change).
3541 * Abbreviated filter type. Valid types are: 'F' (content filter),
3542 * 'S' (server-header filter) and 'C' (client-header filter).
3544 int multi_action_index = 0;
3546 /* Generate the keys */
3547 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3548 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3549 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3550 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3551 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3553 err = get_string_param(parameters, key_name, &name);
3562 type = get_char_param(parameters, key_type);
3566 multi_action_index = ACTION_MULTI_FILTER;
3569 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3572 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3575 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3578 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3581 log_error(LOG_LEVEL_ERROR,
3582 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3585 assert(multi_action_index);
3587 value = get_char_param(parameters, key_value);
3590 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3591 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3592 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3594 else if (value == 'N')
3596 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3597 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3599 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3600 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3603 else if (value == 'X')
3605 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3606 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3613 edit_free_file(file);
3617 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3620 edit_free_file(file);
3621 return JB_ERR_MEMORY;
3624 len = strlen(actiontext);
3628 * Empty action - must special-case this.
3629 * Simply setting len to 1 is sufficient...
3634 newtext_size = len + 2;
3635 if (NULL == (newtext = malloc(newtext_size)))
3639 edit_free_file(file);
3640 return JB_ERR_MEMORY;
3642 strlcpy(newtext, actiontext, newtext_size);
3646 newtext[len + 1] = '\0';
3648 freez(cur_line->raw);
3649 freez(cur_line->unprocessed);
3650 cur_line->unprocessed = newtext;
3652 err = edit_write_file(file);
3655 /* Error writing file */
3656 if (err == JB_ERR_FILE)
3658 /* Read-only file. */
3659 err = cgi_error_file_read_only(csp, rsp, file->filename);
3661 edit_free_file(file);
3665 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3666 (long) time(NULL), file->identifier, sectionid);
3668 edit_free_file(file);
3670 return cgi_redirect(rsp, target);
3674 /*********************************************************************
3676 * Function : cgi_edit_actions_url
3678 * Description : CGI function that actually edits a URL pattern in
3682 * 1 : csp = Current client state (buffers, headers, etc...)
3683 * 2 : rsp = http_response data structure for output
3684 * 3 : parameters = map of cgi parameters
3687 * filename : Identifies the file to edit
3688 * ver : File's last-modified time
3689 * section : Line number of section to edit
3690 * pattern : Line number of pattern to edit
3691 * newval : New value for pattern
3693 * Returns : JB_ERR_OK on success
3694 * JB_ERR_MEMORY on out-of-memory
3695 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3696 * specified or not valid.
3698 *********************************************************************/
3699 jb_err cgi_edit_actions_url(struct client_state *csp,
3700 struct http_response *rsp,
3701 const struct map *parameters)
3705 struct editable_file * file;
3706 struct file_line * cur_line;
3707 unsigned line_number;
3708 unsigned section_start_line_number = 0;
3716 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3718 return cgi_error_disabled(csp, rsp);
3721 err = get_number_param(csp, parameters, "p", &patternid);
3728 return JB_ERR_CGI_PARAMS;
3731 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3737 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3740 /* No filename specified, can't read file, modified, or out of memory. */
3742 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3746 cur_line = file->lines;
3748 while ((cur_line != NULL) && (line_number < patternid))
3750 if (cur_line->type == FILE_LINE_ACTION)
3752 section_start_line_number = line_number;
3754 cur_line = cur_line->next;
3758 if ( (cur_line == NULL)
3759 || (cur_line->type != FILE_LINE_URL))
3761 /* Invalid "patternid" parameter */
3763 edit_free_file(file);
3764 return JB_ERR_CGI_PARAMS;
3767 /* At this point, the line to edit is in cur_line */
3769 freez(cur_line->raw);
3770 freez(cur_line->unprocessed);
3771 cur_line->unprocessed = new_pattern;
3773 err = edit_write_file(file);
3776 /* Error writing file */
3777 if (err == JB_ERR_FILE)
3779 /* Read-only file. */
3780 err = cgi_error_file_read_only(csp, rsp, file->filename);
3782 edit_free_file(file);
3786 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3787 (long) time(NULL), file->identifier, section_start_line_number);
3789 edit_free_file(file);
3791 return cgi_redirect(rsp, target);
3795 /*********************************************************************
3797 * Function : cgi_edit_actions_add_url
3799 * Description : CGI function that actually adds a URL pattern to
3803 * 1 : csp = Current client state (buffers, headers, etc...)
3804 * 2 : rsp = http_response data structure for output
3805 * 3 : parameters = map of cgi parameters
3808 * filename : Identifies the file to edit
3809 * ver : File's last-modified time
3810 * section : Line number of section to edit
3811 * newval : New pattern
3813 * Returns : JB_ERR_OK on success
3814 * JB_ERR_MEMORY on out-of-memory
3815 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3816 * specified or not valid.
3818 *********************************************************************/
3819 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3820 struct http_response *rsp,
3821 const struct map *parameters)
3825 struct file_line * new_line;
3826 struct editable_file * file;
3827 struct file_line * cur_line;
3828 unsigned line_number;
3832 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3834 return cgi_error_disabled(csp, rsp);
3837 err = get_number_param(csp, parameters, "s", §ionid);
3844 return JB_ERR_CGI_PARAMS;
3847 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3853 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3856 /* No filename specified, can't read file, modified, or out of memory. */
3858 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3862 cur_line = file->lines;
3864 while ((cur_line != NULL) && (line_number < sectionid))
3866 cur_line = cur_line->next;
3870 if ( (cur_line == NULL)
3871 || (cur_line->type != FILE_LINE_ACTION))
3873 /* Invalid "sectionid" parameter */
3875 edit_free_file(file);
3876 return JB_ERR_CGI_PARAMS;
3879 /* At this point, the section header is in cur_line - add after this. */
3881 /* Allocate the new line */
3882 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3883 if (new_line == NULL)
3886 edit_free_file(file);
3887 return JB_ERR_MEMORY;
3890 /* Fill in the data members of the new line */
3891 new_line->raw = NULL;
3892 new_line->prefix = NULL;
3893 new_line->unprocessed = new_pattern;
3894 new_line->type = FILE_LINE_URL;
3896 /* Link new_line into the list, after cur_line */
3897 new_line->next = cur_line->next;
3898 cur_line->next = new_line;
3900 /* Done making changes, now commit */
3902 err = edit_write_file(file);
3905 /* Error writing file */
3906 if (err == JB_ERR_FILE)
3908 /* Read-only file. */
3909 err = cgi_error_file_read_only(csp, rsp, file->filename);
3911 edit_free_file(file);
3915 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3916 (long) time(NULL), file->identifier, sectionid);
3918 edit_free_file(file);
3920 return cgi_redirect(rsp, target);
3924 /*********************************************************************
3926 * Function : cgi_edit_actions_remove_url
3928 * Description : CGI function that actually removes a URL pattern from
3932 * 1 : csp = Current client state (buffers, headers, etc...)
3933 * 2 : rsp = http_response data structure for output
3934 * 3 : parameters = map of cgi parameters
3937 * f : (filename) Identifies the file to edit
3938 * v : (version) File's last-modified time
3939 * p : (pattern) Line number of pattern to remove
3941 * Returns : JB_ERR_OK on success
3942 * JB_ERR_MEMORY on out-of-memory
3943 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3944 * specified or not valid.
3946 *********************************************************************/
3947 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3948 struct http_response *rsp,
3949 const struct map *parameters)
3952 struct editable_file * file;
3953 struct file_line * cur_line;
3954 struct file_line * prev_line;
3955 unsigned line_number;
3956 unsigned section_start_line_number = 0;
3960 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3962 return cgi_error_disabled(csp, rsp);
3965 err = get_number_param(csp, parameters, "p", &patternid);
3971 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3974 /* No filename specified, can't read file, modified, or out of memory. */
3975 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3980 cur_line = file->lines;
3982 while ((cur_line != NULL) && (line_number < patternid))
3984 if (cur_line->type == FILE_LINE_ACTION)
3986 section_start_line_number = line_number;
3988 prev_line = cur_line;
3989 cur_line = cur_line->next;
3993 if ( (cur_line == NULL)
3994 || (prev_line == NULL)
3995 || (cur_line->type != FILE_LINE_URL))
3997 /* Invalid "patternid" parameter */
3998 edit_free_file(file);
3999 return JB_ERR_CGI_PARAMS;
4002 /* At this point, the line to remove is in cur_line, and the previous
4003 * one is in prev_line
4006 /* Unlink cur_line */
4007 prev_line->next = cur_line->next;
4008 cur_line->next = NULL;
4011 edit_free_file_lines(cur_line);
4013 err = edit_write_file(file);
4016 /* Error writing file */
4017 if (err == JB_ERR_FILE)
4019 /* Read-only file. */
4020 err = cgi_error_file_read_only(csp, rsp, file->filename);
4022 edit_free_file(file);
4026 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
4027 (long) time(NULL), file->identifier, section_start_line_number);
4029 edit_free_file(file);
4031 return cgi_redirect(rsp, target);
4035 /*********************************************************************
4037 * Function : cgi_edit_actions_section_remove
4039 * Description : CGI function that actually removes a whole section from
4040 * the actions file. The section must be empty first
4041 * (else JB_ERR_CGI_PARAMS).
4044 * 1 : csp = Current client state (buffers, headers, etc...)
4045 * 2 : rsp = http_response data structure for output
4046 * 3 : parameters = map of cgi parameters
4049 * f : (filename) Identifies the file to edit
4050 * v : (version) File's last-modified time
4051 * s : (section) Line number of section to edit
4053 * Returns : JB_ERR_OK on success
4054 * JB_ERR_MEMORY on out-of-memory
4055 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4056 * specified or not valid.
4058 *********************************************************************/
4059 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
4060 struct http_response *rsp,
4061 const struct map *parameters)
4064 struct editable_file * file;
4065 struct file_line * cur_line;
4066 struct file_line * prev_line;
4067 unsigned line_number;
4071 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4073 return cgi_error_disabled(csp, rsp);
4076 err = get_number_param(csp, parameters, "s", §ionid);
4082 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4085 /* No filename specified, can't read file, modified, or out of memory. */
4086 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4090 cur_line = file->lines;
4093 while ((cur_line != NULL) && (line_number < sectionid))
4095 prev_line = cur_line;
4096 cur_line = cur_line->next;
4100 if ( (cur_line == NULL)
4101 || (cur_line->type != FILE_LINE_ACTION) )
4103 /* Invalid "sectionid" parameter */
4104 edit_free_file(file);
4105 return JB_ERR_CGI_PARAMS;
4108 if ( (cur_line->next != NULL)
4109 && (cur_line->next->type == FILE_LINE_URL) )
4111 /* Section not empty. */
4112 edit_free_file(file);
4113 return JB_ERR_CGI_PARAMS;
4116 /* At this point, the line to remove is in cur_line, and the previous
4117 * one is in prev_line
4120 /* Unlink cur_line */
4121 if (prev_line == NULL)
4123 /* Removing the first line from the file */
4124 file->lines = cur_line->next;
4128 prev_line->next = cur_line->next;
4130 cur_line->next = NULL;
4133 edit_free_file_lines(cur_line);
4135 err = edit_write_file(file);
4138 /* Error writing file */
4139 if (err == JB_ERR_FILE)
4141 /* Read-only file. */
4142 err = cgi_error_file_read_only(csp, rsp, file->filename);
4144 edit_free_file(file);
4148 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4149 (long) time(NULL), file->identifier);
4151 edit_free_file(file);
4153 return cgi_redirect(rsp, target);
4157 /*********************************************************************
4159 * Function : cgi_edit_actions_section_add
4161 * Description : CGI function that adds a new empty section to
4165 * 1 : csp = Current client state (buffers, headers, etc...)
4166 * 2 : rsp = http_response data structure for output
4167 * 3 : parameters = map of cgi parameters
4170 * f : (filename) Identifies the file to edit
4171 * v : (version) File's last-modified time
4172 * s : (section) Line number of section to add after, 0 for
4175 * Returns : JB_ERR_OK on success
4176 * JB_ERR_MEMORY on out-of-memory
4177 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4178 * specified or not valid.
4180 *********************************************************************/
4181 jb_err cgi_edit_actions_section_add(struct client_state *csp,
4182 struct http_response *rsp,
4183 const struct map *parameters)
4186 struct file_line * new_line;
4188 struct editable_file * file;
4189 struct file_line * cur_line;
4190 unsigned line_number;
4194 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4196 return cgi_error_disabled(csp, rsp);
4199 err = get_number_param(csp, parameters, "s", §ionid);
4205 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4208 /* No filename specified, can't read file, modified, or out of memory. */
4209 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4213 cur_line = file->lines;
4215 if (sectionid <= 1U)
4217 /* Add to start of file */
4218 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4220 /* There's something in the file, find the line before the first
4223 while ( (cur_line->next != NULL)
4224 && (cur_line->next->type != FILE_LINE_ACTION) )
4226 cur_line = cur_line->next;
4232 /* File starts with action line, so insert at top */
4238 /* Add after stated section. */
4239 while ((cur_line != NULL) && (line_number < sectionid))
4241 cur_line = cur_line->next;
4245 if ( (cur_line == NULL)
4246 || (cur_line->type != FILE_LINE_ACTION))
4248 /* Invalid "sectionid" parameter */
4249 edit_free_file(file);
4250 return JB_ERR_CGI_PARAMS;
4253 /* Skip through the section to find the last line in it. */
4254 while ( (cur_line->next != NULL)
4255 && (cur_line->next->type != FILE_LINE_ACTION) )
4257 cur_line = cur_line->next;
4262 /* At this point, the last line in the previous section is in cur_line
4263 * - add after this. (Or if we need to add as the first line, cur_line
4267 new_text = strdup("{}");
4268 if (NULL == new_text)
4270 edit_free_file(file);
4271 return JB_ERR_MEMORY;
4274 /* Allocate the new line */
4275 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4276 if (new_line == NULL)
4279 edit_free_file(file);
4280 return JB_ERR_MEMORY;
4283 /* Fill in the data members of the new line */
4284 new_line->raw = NULL;
4285 new_line->prefix = NULL;
4286 new_line->unprocessed = new_text;
4287 new_line->type = FILE_LINE_ACTION;
4289 if (cur_line != NULL)
4291 /* Link new_line into the list, after cur_line */
4292 new_line->next = cur_line->next;
4293 cur_line->next = new_line;
4297 /* Link new_line into the list, as first line */
4298 new_line->next = file->lines;
4299 file->lines = new_line;
4302 /* Done making changes, now commit */
4304 err = edit_write_file(file);
4307 /* Error writing file */
4308 if (err == JB_ERR_FILE)
4310 /* Read-only file. */
4311 err = cgi_error_file_read_only(csp, rsp, file->filename);
4313 edit_free_file(file);
4317 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4318 (long) time(NULL), file->identifier);
4320 edit_free_file(file);
4322 return cgi_redirect(rsp, target);
4326 /*********************************************************************
4328 * Function : cgi_edit_actions_section_swap
4330 * Description : CGI function that swaps the order of two sections
4331 * in the actions file. Note that this CGI can actually
4332 * swap any two arbitrary sections, but the GUI interface
4333 * currently only allows consecutive sections to be
4337 * 1 : csp = Current client state (buffers, headers, etc...)
4338 * 2 : rsp = http_response data structure for output
4339 * 3 : parameters = map of cgi parameters
4342 * f : (filename) Identifies the file to edit
4343 * v : (version) File's last-modified time
4344 * s1 : (section1) Line number of first section to swap
4345 * s2 : (section2) Line number of second section to swap
4347 * Returns : JB_ERR_OK on success
4348 * JB_ERR_MEMORY on out-of-memory
4349 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4350 * specified or not valid.
4352 *********************************************************************/
4353 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4354 struct http_response *rsp,
4355 const struct map *parameters)
4359 struct editable_file * file;
4360 struct file_line * cur_line;
4361 struct file_line * prev_line;
4362 struct file_line * line_before_section1;
4363 struct file_line * line_start_section1;
4364 struct file_line * line_end_section1;
4365 struct file_line * line_after_section1;
4366 struct file_line * line_before_section2;
4367 struct file_line * line_start_section2;
4368 struct file_line * line_end_section2;
4369 struct file_line * line_after_section2;
4370 unsigned line_number;
4374 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4376 return cgi_error_disabled(csp, rsp);
4379 err = get_number_param(csp, parameters, "s1", §ion1);
4380 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4386 if (section1 > section2)
4388 unsigned temp = section2;
4389 section2 = section1;
4393 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4396 /* No filename specified, can't read file, modified, or out of memory. */
4397 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4400 /* Start at the beginning... */
4402 cur_line = file->lines;
4405 /* ... find section1 ... */
4406 while ((cur_line != NULL) && (line_number < section1))
4408 prev_line = cur_line;
4409 cur_line = cur_line->next;
4413 if ( (cur_line == NULL)
4414 || (cur_line->type != FILE_LINE_ACTION) )
4416 /* Invalid "section1" parameter */
4417 edit_free_file(file);
4418 return JB_ERR_CGI_PARAMS;
4421 /* If no-op, we've validated params and can skip the rest. */
4422 if (section1 != section2)
4424 /* ... find the end of section1 ... */
4425 line_before_section1 = prev_line;
4426 line_start_section1 = cur_line;
4429 prev_line = cur_line;
4430 cur_line = cur_line->next;
4433 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4434 line_end_section1 = prev_line;
4435 line_after_section1 = cur_line;
4437 /* ... find section2 ... */
4438 while ((cur_line != NULL) && (line_number < section2))
4440 prev_line = cur_line;
4441 cur_line = cur_line->next;
4445 if ( (cur_line == NULL)
4446 || (cur_line->type != FILE_LINE_ACTION) )
4448 /* Invalid "section2" parameter */
4449 edit_free_file(file);
4450 return JB_ERR_CGI_PARAMS;
4453 /* ... find the end of section2 ... */
4454 line_before_section2 = prev_line;
4455 line_start_section2 = cur_line;
4458 prev_line = cur_line;
4459 cur_line = cur_line->next;
4462 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4463 line_end_section2 = prev_line;
4464 line_after_section2 = cur_line;
4466 /* Now have all the pointers we need. Do the swap. */
4468 /* Change the pointer to section1 to point to section2 instead */
4469 if (line_before_section1 == NULL)
4471 file->lines = line_start_section2;
4475 line_before_section1->next = line_start_section2;
4478 if (line_before_section2 == line_end_section1)
4480 /* Consecutive sections */
4481 line_end_section2->next = line_start_section1;
4485 line_end_section2->next = line_after_section1;
4486 line_before_section2->next = line_start_section1;
4489 /* Set the pointer from the end of section1 to the rest of the file */
4490 line_end_section1->next = line_after_section2;
4492 err = edit_write_file(file);
4495 /* Error writing file */
4496 if (err == JB_ERR_FILE)
4498 /* Read-only file. */
4499 err = cgi_error_file_read_only(csp, rsp, file->filename);
4501 edit_free_file(file);
4504 } /* END if (section1 != section2) */
4506 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4507 (long) time(NULL), file->identifier);
4509 edit_free_file(file);
4511 return cgi_redirect(rsp, target);
4514 #ifdef FEATURE_TOGGLE
4515 /*********************************************************************
4517 * Function : cgi_toggle
4519 * Description : CGI function that adds a new empty section to
4523 * 1 : csp = Current client state (buffers, headers, etc...)
4524 * 2 : rsp = http_response data structure for output
4525 * 3 : parameters = map of cgi parameters
4528 * set : If present, how to change toggle setting:
4529 * "enable", "disable", "toggle", or none (default).
4530 * mini : If present, use mini reply template.
4532 * Returns : JB_ERR_OK on success
4533 * JB_ERR_MEMORY on out-of-memory
4535 *********************************************************************/
4536 jb_err cgi_toggle(struct client_state *csp,
4537 struct http_response *rsp,
4538 const struct map *parameters)
4540 struct map *exports;
4542 const char *template_name;
4548 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4550 return cgi_error_disabled(csp, rsp);
4553 mode = get_char_param(parameters, "set");
4558 global_toggle_state = 1;
4560 else if (mode == 'D')
4563 global_toggle_state = 0;
4565 else if (mode == 'T')
4568 global_toggle_state = !global_toggle_state;
4571 if (NULL == (exports = default_exports(csp, "toggle")))
4573 return JB_ERR_MEMORY;
4576 template_name = (get_char_param(parameters, "mini")
4580 return template_fill_for_cgi(csp, template_name, exports, rsp);
4582 #endif /* def FEATURE_TOGGLE */
4584 /*********************************************************************
4586 * Function : javascriptify
4588 * Description : Converts a string into a form JavaScript will like.
4590 * Netscape 4's JavaScript sucks - it doesn't use
4591 * "id" parameters, so you have to set the "name"
4592 * used to submit a form element to something JavaScript
4593 * will like. (Or access the elements by index in an
4594 * array. That array contains >60 elements and will
4595 * be changed whenever we add a new action to the
4596 * editor, so I'm NOT going to use indexes that have
4597 * to be figured out by hand.)
4599 * Currently the only thing we have to worry about
4600 * is "-" ==> "_" conversion.
4602 * This is a length-preserving operation so it is
4603 * carried out in-place, no memory is allocated
4607 * 1 : identifier = String to make JavaScript-friendly.
4611 *********************************************************************/
4612 static void javascriptify(char * identifier)
4614 char * p = identifier;
4615 while (NULL != (p = strchr(p, '-')))
4622 /*********************************************************************
4624 * Function : actions_to_radio
4626 * Description : Converts a actionsfile entry into settings for
4627 * radio buttons and edit boxes on a HTML form.
4630 * 1 : exports = List of substitutions to add to.
4631 * 2 : action = Action to read
4633 * Returns : JB_ERR_OK on success
4634 * JB_ERR_MEMORY on out-of-memory
4636 *********************************************************************/
4637 static jb_err actions_to_radio(struct map * exports,
4638 const struct action_spec *action)
4640 unsigned mask = action->mask;
4641 unsigned add = action->add;
4649 mask = action->mask;
4652 /* sanity - prevents "-feature +feature" */
4656 #define DEFINE_ACTION_BOOL(name, bit) \
4657 if (!(mask & bit)) \
4659 current_mode = 'n'; \
4661 else if (add & bit) \
4663 current_mode = 'y'; \
4667 current_mode = 'x'; \
4669 if (map_radio(exports, name, "ynx", current_mode)) \
4671 return JB_ERR_MEMORY; \
4674 #define DEFINE_ACTION_STRING(name, bit, index) \
4675 DEFINE_ACTION_BOOL(name, bit); \
4678 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4681 checked = !strcmp(action->string[index], value); \
4685 checked = is_default; \
4687 mapped_param |= checked; \
4688 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4690 return JB_ERR_MEMORY; \
4693 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4694 if (map(exports, name "-param-custom", 1, \
4695 ((!mapped_param) ? "checked" : ""), 1)) \
4697 return JB_ERR_MEMORY; \
4699 if (map(exports, name "-param", 1, \
4700 (((add & bit) && !mapped_param) ? \
4701 action->string[index] : default_val), 1)) \
4703 return JB_ERR_MEMORY; \
4706 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4707 if (map(exports, name "-param", 1, \
4708 ((add & bit) ? action->string[index] : default_val), 1)) \
4710 return JB_ERR_MEMORY; \
4713 #define DEFINE_ACTION_MULTI(name, index) \
4714 if (action->multi_add[index]->first) \
4716 current_mode = 'y'; \
4718 else if (action->multi_remove_all[index]) \
4720 current_mode = 'n'; \
4722 else if (action->multi_remove[index]->first) \
4724 current_mode = 'y'; \
4728 current_mode = 'x'; \
4730 if (map_radio(exports, name, "ynx", current_mode)) \
4732 return JB_ERR_MEMORY; \
4735 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4737 #include "actionlist.h"
4739 #undef DEFINE_ACTION_MULTI
4740 #undef DEFINE_ACTION_STRING
4741 #undef DEFINE_ACTION_BOOL
4742 #undef DEFINE_ACTION_ALIAS
4743 #undef DEFINE_CGI_PARAM_CUSTOM
4744 #undef DEFINE_CGI_PARAM_RADIO
4745 #undef DEFINE_CGI_PARAM_NO_RADIO
4751 /*********************************************************************
4753 * Function : actions_from_radio
4755 * Description : Converts a map of parameters passed to a CGI function
4756 * into an actionsfile entry.
4759 * 1 : parameters = parameters to the CGI call
4760 * 2 : action = Action to change. Must be valid before
4761 * the call, actions not specified will be
4764 * Returns : JB_ERR_OK on success
4765 * JB_ERR_MEMORY on out-of-memory
4767 *********************************************************************/
4768 static jb_err actions_from_radio(const struct map * parameters,
4769 struct action_spec *action)
4774 const char * js_name;
4775 jb_err err = JB_ERR_OK;
4780 /* Statics are generally a potential race condition,
4781 * but in this case we're safe and don't need semaphores.
4782 * Be careful if you modify this function.
4784 * The js_name_arr's are never free()d, but this is no
4785 * problem, since they will only be created once and
4786 * used by all threads thereafter. -oes
4789 #define JAVASCRIPTIFY(dest_var, string) \
4791 static int first_time = 1; \
4792 static char *js_name_arr; \
4795 js_name_arr = strdup(string); \
4796 javascriptify(js_name_arr); \
4798 dest_var = js_name_arr; \
4802 #define DEFINE_ACTION_BOOL(name, bit) \
4803 JAVASCRIPTIFY(js_name, name); \
4804 ch = get_char_param(parameters, js_name); \
4807 action->add |= bit; \
4808 action->mask |= bit; \
4810 else if (ch == 'N') \
4812 action->add &= ~bit; \
4813 action->mask &= ~bit; \
4815 else if (ch == 'X') \
4817 action->add &= ~bit; \
4818 action->mask |= bit; \
4821 #define DEFINE_ACTION_STRING(name, bit, index) \
4822 JAVASCRIPTIFY(js_name, name); \
4823 ch = get_char_param(parameters, js_name); \
4827 JAVASCRIPTIFY(js_name, name "-mode"); \
4828 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4829 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4831 JAVASCRIPTIFY(js_name, name "-param"); \
4832 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4834 if (param != NULL) \
4836 if (NULL == (param_dup = strdup(param))) \
4838 return JB_ERR_MEMORY; \
4840 freez(action->string[index]); \
4841 action->add |= bit; \
4842 action->mask |= bit; \
4843 action->string[index] = param_dup; \
4846 else if (ch == 'N') \
4848 if (action->add & bit) \
4850 freez(action->string[index]); \
4852 action->add &= ~bit; \
4853 action->mask &= ~bit; \
4855 else if (ch == 'X') \
4857 if (action->add & bit) \
4859 freez(action->string[index]); \
4861 action->add &= ~bit; \
4862 action->mask |= bit; \
4865 #define DEFINE_ACTION_MULTI(name, index) \
4866 JAVASCRIPTIFY(js_name, name); \
4867 ch = get_char_param(parameters, js_name); \
4872 else if (ch == 'N') \
4874 list_remove_all(action->multi_add[index]); \
4875 list_remove_all(action->multi_remove[index]); \
4876 action->multi_remove_all[index] = 1; \
4878 else if (ch == 'X') \
4880 list_remove_all(action->multi_add[index]); \
4881 list_remove_all(action->multi_remove[index]); \
4882 action->multi_remove_all[index] = 0; \
4885 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4887 #include "actionlist.h"
4889 #undef DEFINE_ACTION_MULTI
4890 #undef DEFINE_ACTION_STRING
4891 #undef DEFINE_ACTION_BOOL
4892 #undef DEFINE_ACTION_ALIAS
4893 #undef JAVASCRIPTIFY
4899 #endif /* def FEATURE_CGI_EDIT_ACTIONS */