1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.63 2008/12/04 18:15:38 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.63 2008/12/04 18:15:38 fabiankeil
46 * Fix some cparser warnings.
48 * Revision 1.62 2008/08/31 15:59:02 fabiankeil
49 * There's no reason to let remote toggling support depend
50 * on FEATURE_CGI_EDIT_ACTIONS, so make sure it doesn't.
52 * Revision 1.61 2008/03/24 18:12:52 fabiankeil
53 * Use sizeof() more often.
55 * Revision 1.60 2008/03/15 14:52:35 fabiankeil
56 * Add CGI editor support for the "disable all filters of this type"
57 * directives "-client-header-filter", "-server-header-filter",
58 * "-client-header-tagger" and "-server-header-tagger".
60 * Revision 1.59 2008/03/08 16:25:56 fabiankeil
61 * After three file modification time mismatches, turn the CGI editor off.
63 * Revision 1.58 2007/11/28 17:57:01 fabiankeil
64 * Fix double free in cgi_edit_actions_list().
65 * Reported by adlab in BR#1840145.
67 * Revision 1.57 2007/10/27 13:32:23 fabiankeil
68 * Plug minor 5-year-old memory leak. Spotted by
69 * Valgrind and triggered by Privoxy-Regression-Test.
71 * Revision 1.56 2007/08/05 13:47:03 fabiankeil
72 * #1763173 from Stefan Huehner: s@const static@static const@.
74 * Revision 1.55 2007/05/31 11:50:20 fabiankeil
75 * Re-enable support for old-school URLs like
76 * http://config.privoxy.org/edit-actions-list?f=default
77 * in the action editor.
79 * They are no longer used by the CGI pages, but make it easier
80 * to reach the editor directly, without knowing the requested
81 * file's index in csp->config->actions_file[].
83 * Revision 1.54 2007/05/14 10:33:51 fabiankeil
84 * - Use strlcpy() and strlcat() instead of strcpy() and strcat().
86 * Revision 1.53 2007/04/15 16:39:20 fabiankeil
87 * Introduce tags as alternative way to specify which
88 * actions apply to a request. At the moment tags can be
89 * created based on client and server headers.
91 * Revision 1.52 2007/04/12 10:41:23 fabiankeil
92 * - Don't mistake VC++'s _snprintf() for a snprintf() replacement.
93 * - Move some cgi_edit_actions_for_url() variables into structs.
94 * - Remove bogus comment.
96 * Revision 1.51 2007/04/08 13:21:05 fabiankeil
97 * Reference action files in CGI URLs by id instead
98 * of using the first part of the file name.
99 * Fixes BR 1694250 and BR 1590556.
101 * Revision 1.50 2007/03/29 11:40:34 fabiankeil
102 * Divide @filter-params@ into @client-header-filter-params@
103 * @content-filter-params@ and @server-header-filter-params@.
105 * Revision 1.49 2007/03/20 15:16:34 fabiankeil
106 * Use dedicated header filter actions instead of abusing "filter".
107 * Replace "filter-client-headers" and "filter-client-headers"
108 * with "server-header-filter" and "client-header-filter".
110 * Revision 1.48 2007/02/13 14:35:25 fabiankeil
111 * Replace hash escaping code to prevent
112 * crashes, memory and file corruption.
114 * Revision 1.47 2006/12/28 18:04:25 fabiankeil
115 * Fixed gcc43 conversion warnings.
117 * Revision 1.46 2006/12/27 18:44:52 fabiankeil
118 * Stop shadowing string.h's index().
120 * Revision 1.45 2006/12/21 12:57:48 fabiankeil
121 * Add config option "split-large-forms"
122 * to work around the browser bug reported
125 * Revision 1.44 2006/12/09 13:49:16 fabiankeil
126 * Fix configure option --disable-toggle.
127 * Thanks to Peter Thoenen for reporting this.
129 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
130 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
131 * with what was really the latest development (the v_3_0_branch branch)
133 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
134 * Remove a little residual debugging info
136 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
137 * Multiple filter file support
139 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
140 * Undo some damaging memory leak patches
142 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
143 * Removing a double free
145 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
146 * A few memory leaks plugged (mostly on error paths)
148 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
149 * Moved cgi_error_disabled() from cgiedit.c to
150 * cgi.c to re-enable build with --disable-editor.
151 * Fixes Bug #892744. Thanks to Matthew Fischer
154 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
155 * One line lost in last commit
157 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
158 * - All edit functions that redirect back to the list page
159 * now use cgi_redirect
160 * - All redirects now contain useless parameter "foo", whose
161 * value are raw seconds since epoch, in order to force
162 * Opera and Konqueror to properly reload the list. Closes
165 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
166 * Cosmetic: Renamed cryptic variable
168 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
169 * Fix: Don't free uninitialized struct editable_file
171 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
172 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
174 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
175 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
177 * Revision 1.41 2002/05/21 19:09:45 oes
178 * - Made Add/Edit/Remove URL Submit and Cancel
179 * buttons jump back to relevant section in eal
180 * - Bugfix: remove-url-form needs p export
182 * Revision 1.40 2002/05/19 11:34:35 jongfoster
183 * Handling read-only actions files better - report the actual
184 * error, not "Out of memory"!
187 * http://sourceforge.net/tracker/index.php?func=detail
188 * &aid=557905&group_id=11118&atid=111118
190 * Revision 1.39 2002/05/12 21:39:15 jongfoster
191 * - Adding Doxygen-style comments to structures and #defines.
192 * - Correcting function comments
194 * Revision 1.38 2002/05/03 23:00:38 jongfoster
195 * Support for templates for "standard actions" buttons.
198 * Revision 1.37 2002/04/30 11:14:52 oes
199 * Made csp the first parameter in *action_to_html
201 * Revision 1.36 2002/04/26 21:53:30 jongfoster
202 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
204 * Revision 1.35 2002/04/26 21:50:02 jongfoster
205 * Honouring default exports in edit-actions-for-url-filter template.
207 * Revision 1.34 2002/04/26 12:54:17 oes
208 * Adaptions to changes in actions.c
210 * Revision 1.33 2002/04/24 02:17:47 oes
211 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
213 * - Activated Jon's code for editing multiple AFs
214 * - cgi_edit_list_actions now provides context-sensitive
215 * help, looks up all action sets from standard.action and
216 * makes buttons for them in the catchall section
217 * - cgi_edit_action_submit now honors a p parameter, looks up
218 * the corresponding action set, and sets the catchall pattern's
219 * actions accordingly.
221 * Revision 1.32 2002/04/19 16:55:31 jongfoster
222 * Fixing newline problems. If we do our own text file newline
223 * mangling, we don't want the library to do any, so we need to
224 * open the files in *binary* mode.
226 * Revision 1.31 2002/04/18 19:21:08 jongfoster
227 * Added code to detect "conventional" action files, that start
228 * with a set of actions for all URLs (the pattern "/").
229 * These are special-cased in the "edit-actions-list" CGI, so
230 * that a special UI can be written for them.
232 * Revision 1.30 2002/04/10 13:38:35 oes
233 * load_template signature changed
235 * Revision 1.29 2002/04/08 16:59:08 oes
238 * Revision 1.28 2002/03/27 12:30:29 oes
239 * Deleted unsused variable
241 * Revision 1.27 2002/03/26 23:06:04 jongfoster
242 * Removing duplicate @ifs on the toggle page
244 * Revision 1.26 2002/03/26 22:59:17 jongfoster
245 * Fixing /toggle to display status consistently.
247 * Revision 1.25 2002/03/26 22:29:54 swa
248 * we have a new homepage!
250 * Revision 1.24 2002/03/24 15:23:33 jongfoster
253 * Revision 1.23 2002/03/24 13:32:41 swa
254 * name change related issues
256 * Revision 1.22 2002/03/24 13:25:43 swa
257 * name change related issues
259 * Revision 1.21 2002/03/22 18:02:48 jongfoster
260 * Fixing remote toggle
262 * Revision 1.20 2002/03/16 20:28:34 oes
263 * Added descriptions to the filters so users will know what they select in the cgi editor
265 * Revision 1.19 2002/03/16 18:38:14 jongfoster
266 * Stopping stupid or malicious users from breaking the actions
267 * file using the web-based editor.
269 * Revision 1.18 2002/03/16 14:57:44 jongfoster
270 * Full support for enabling/disabling modular filters.
272 * Revision 1.17 2002/03/16 14:26:42 jongfoster
273 * First version of modular filters support - READ ONLY!
274 * Fixing a double-free bug in the out-of-memory handling in map_radio().
276 * Revision 1.16 2002/03/07 03:46:17 oes
277 * Fixed compiler warnings
279 * Revision 1.15 2002/03/06 22:54:35 jongfoster
280 * Automated function-comment nitpicking.
282 * Revision 1.14 2002/03/05 00:24:51 jongfoster
283 * Patch to always edit the current actions file.
285 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
286 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
288 * Revision 1.12 2002/03/03 09:18:03 joergs
289 * Made jumbjuster work on AmigaOS again.
291 * Revision 1.11 2002/01/23 01:03:31 jongfoster
292 * Fixing gcc [CygWin] compiler warnings
294 * Revision 1.10 2002/01/23 00:22:59 jongfoster
295 * Adding new function cgi_edit_actions_section_swap(), to reorder
298 * Adding get_url_spec_param() to get a validated URL pattern.
300 * Moving edit_read_line() out of this file and into loaders.c.
302 * Adding missing html_encode() to many CGI functions.
304 * Moving the functions that #include actionlist.h to the end of the file,
305 * because the Visual C++ 97 debugger gets extremely confused if you try
306 * to debug any code that comes after them in the file.
308 * Major optimizations in cgi_edit_actions_list() to reduce the size of
309 * the generated HTML (down 40% from 550k to 304k), with major side-effects
310 * throughout the editor and templates. In particular, the length of the
311 * URLs throughout the editor has been drastically reduced, by cutting
312 * paramater names down to 1 character and CGI names down to 3-4
313 * characters, by removing all non-essential CGI paramaters even at the
314 * expense of having to re-read the actions file for the most trivial
315 * page, and by using relative rather than absolute URLs. This means
316 * that this (typical example):
318 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
319 * filename=ijb&ver=1011487572&section=12&pattern=13
320 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
324 * <a href="eau?f=ijb&v=1011487572&p=13">
326 * Revision 1.9 2002/01/17 20:56:22 jongfoster
327 * Replacing hard references to the URL of the config interface
328 * with #defines from project.h
330 * Revision 1.8 2001/11/30 23:35:51 jongfoster
331 * Renaming actionsfile to ijb.action
333 * Revision 1.7 2001/11/13 00:28:24 jongfoster
334 * - Renaming parameters from edit-actions-for-url so that they only
335 * contain legal JavaScript characters. If we wanted to write
336 * JavaScript that worked with Netscape 4, this is nessacery.
337 * (Note that at the moment the JavaScript doesn't actually work
338 * with Netscape 4, but now this is purely a template issue, not
339 * one affecting code).
340 * - Adding new CGIs for use by non-JavaScript browsers:
341 * edit-actions-url-form
342 * edit-actions-add-url-form
343 * edit-actions-remove-url-form
346 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
347 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
348 * by and __OS2__ ifdef.
350 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
351 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
352 * threads to call select() simultaneously. So, it's time to do a real, live,
353 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
354 * (native). Both versions will work, but using __OS2__ offers multi-threading.
356 * Revision 1.4 2001/10/23 21:48:19 jongfoster
357 * Cleaning up error handling in CGI functions - they now send back
358 * a HTML error page and should never cause a FATAL error. (Fixes one
359 * potential source of "denial of service" attacks).
361 * CGI actions file editor that works and is actually useful.
363 * Ability to toggle JunkBuster remotely using a CGI call.
365 * You can turn off both the above features in the main configuration
366 * file, e.g. if you are running a multi-user proxy.
368 * Revision 1.3 2001/10/14 22:12:49 jongfoster
369 * New version of CGI-based actionsfile editor.
370 * Major changes, including:
371 * - Completely new file parser and file output routines
372 * - edit-actions CGI renamed edit-actions-for-url
373 * - All CGIs now need a filename parameter, except for...
374 * - New CGI edit-actions which doesn't need a filename,
375 * to allow you to start the editor up.
376 * - edit-actions-submit now works, and now automatically
377 * redirects you back to the main edit-actions-list handler.
379 * Revision 1.2 2001/09/16 17:05:14 jongfoster
380 * Removing unused #include showarg.h
382 * Revision 1.1 2001/09/16 15:47:37 jongfoster
383 * First version of CGI-based edit interface. This is very much a
384 * work-in-progress, and you can't actually use it to edit anything
385 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
386 * to have any effect.
389 **********************************************************************/
395 * FIXME: Following includes copied from cgi.c - which are actually needed?
400 #include <sys/types.h>
404 #include <sys/stat.h>
409 #include "cgisimple.h"
413 #include "miscutil.h"
416 #ifdef FEATURE_TOGGLE
417 /* loadcfg.h is for global_toggle_state only */
419 #endif /* def FEATURE_TOGGLE */
420 #include "urlmatch.h"
422 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
425 #ifdef FEATURE_CGI_EDIT_ACTIONS
428 * A line in an editable_file.
432 /** Next entry in the linked list */
433 struct file_line * next;
435 /** The raw data, to write out if this line is unmodified. */
438 /** Comments and/or whitespace to put before this line if it's modified
439 and then written out. */
442 /** The actual data, as a string. Line continuation and comment removal
443 are performed on the data read from file before it's stored here, so
444 it will be a single line of data. */
447 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
450 /** The actual data, processed into some sensible data type. */
454 /** An action specification. */
455 struct action_spec action[1];
457 /** A name=value pair. */
461 /** The name in the name=value pair. */
464 /** The value in the name=value pair, as a string. */
467 /** The value in the name=value pair, as an integer. */
472 /* Add more data types here... e.g.
475 struct url_spec url[1];
479 struct action_spec action[1];
489 /** This file_line has not been processed yet. */
490 #define FILE_LINE_UNPROCESSED 1
492 /** This file_line is blank. Can only appear at the end of a file, due to
493 the way the parser works. */
494 #define FILE_LINE_BLANK 2
496 /** This file_line says {{alias}}. */
497 #define FILE_LINE_ALIAS_HEADER 3
499 /** This file_line defines an alias. */
500 #define FILE_LINE_ALIAS_ENTRY 4
502 /** This file_line defines an {action}. */
503 #define FILE_LINE_ACTION 5
505 /** This file_line specifies a URL pattern. */
506 #define FILE_LINE_URL 6
508 /** This file_line says {{settings}}. */
509 #define FILE_LINE_SETTINGS_HEADER 7
511 /** This file_line is in a {{settings}} block. */
512 #define FILE_LINE_SETTINGS_ENTRY 8
514 /** This file_line says {{description}}. */
515 #define FILE_LINE_DESCRIPTION_HEADER 9
517 /** This file_line is in a {{description}} block. */
518 #define FILE_LINE_DESCRIPTION_ENTRY 10
521 * Number of file modification time mismatches
522 * before the CGI editor gets turned off.
524 #define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
527 * A configuration file, in a format that can be edited and written back to
532 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
533 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
534 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
535 const char * version_str; /**< Last modification time, as a string. For CGI param. */
536 /**< Can be used in URL without using url_param(). */
537 unsigned version; /**< Last modification time - prevents chaos with
538 the browser's "back" button. Note that this is a
539 time_t cast to an unsigned. When comparing, always
540 cast the time_t to an unsigned, and *NOT* vice-versa.
541 This may lose the top few bits, but they're not
542 significant anyway. */
543 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
544 Note that changing this after the file has been
545 read in will cause a mess. */
546 struct file_line * parse_error; /**< On parse error, this is the offending line. */
547 const char * parse_error_text; /**< On parse error, this is the problem.
548 (Statically allocated) */
552 * Information about the filter types.
553 * Used for macro replacement in cgi_edit_actions_for_url.
555 struct filter_type_info
557 const int multi_action_index; /**< The multi action index as defined in project.h */
558 const char *macro_name; /**< Name of the macro that has to be replaced
559 with the prepared templates.
560 For example "content-filter-params" */
561 const char *type; /**< Name of the filter type,
562 for example "server-header-filter". */
563 /* XXX: check if these two can be combined. */
564 const char *disable_all_option; /**< Name of the catch-all radio option that has
565 to be checked or unchecked for this filter type. */
566 const char *disable_all_param; /**< Name of the parameter that causes all filters of
567 this type to be disabled. */
568 const char *abbr_type; /**< Abbreviation of the filter type, usually the
569 first or second character capitalized */
570 const char *anchor; /**< Anchor for the User Manual link,
571 for example "SERVER-HEADER-FILTER" */
574 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
575 static const struct filter_type_info filter_type_info[] =
579 "content-filter-params", "filter",
580 "filter-all", "filter_all",
584 ACTION_MULTI_CLIENT_HEADER_FILTER,
585 "client-header-filter-params", "client-header-filter",
586 "client-header-filter-all", "client_header_filter_all",
587 "C", "CLIENT-HEADER-FILTER"
590 ACTION_MULTI_SERVER_HEADER_FILTER,
591 "server-header-filter-params", "server-header-filter",
592 "server-header-filter-all", "server_header_filter_all",
593 "S", "SERVER-HEADER-FILTER"
596 ACTION_MULTI_CLIENT_HEADER_TAGGER,
597 "client-header-tagger-params", "client-header-tagger",
598 "client-header-tagger-all", "client_header_tagger_all",
599 "L", "CLIENT-HEADER-TAGGER"
602 ACTION_MULTI_SERVER_HEADER_TAGGER,
603 "server-header-tagger-params", "server-header-tagger",
604 "server-header-tagger-all", "server_header_tagger_all",
605 "E", "SERVER-HEADER-TAGGER"
609 /* FIXME: Following non-static functions should be prototyped in .h or made static */
611 /* Functions to read and write arbitrary config files */
612 jb_err edit_read_file(struct client_state *csp,
613 const struct map *parameters,
615 struct editable_file **pfile);
616 jb_err edit_write_file(struct editable_file * file);
617 void edit_free_file(struct editable_file * file);
619 /* Functions to read and write actions files */
620 jb_err edit_parse_actions_file(struct editable_file * file);
621 jb_err edit_read_actions_file(struct client_state *csp,
622 struct http_response *rsp,
623 const struct map *parameters,
625 struct editable_file **pfile);
628 jb_err cgi_error_modified(struct client_state *csp,
629 struct http_response *rsp,
630 const char *filename);
631 jb_err cgi_error_parse(struct client_state *csp,
632 struct http_response *rsp,
633 struct editable_file *file);
634 jb_err cgi_error_file(struct client_state *csp,
635 struct http_response *rsp,
636 const char *filename);
637 jb_err cgi_error_file_read_only(struct client_state *csp,
638 struct http_response *rsp,
639 const char *filename);
641 /* Internal arbitrary config file support functions */
642 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
643 static void edit_free_file_lines(struct file_line * first_line);
645 /* Internal actions file support functions */
646 static int match_actions_file_header_line(const char * line, const char * name);
647 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
649 /* Internal parameter parsing functions */
650 static jb_err get_url_spec_param(struct client_state *csp,
651 const struct map *parameters,
656 /* Internal actionsfile <==> HTML conversion functions */
657 static jb_err map_radio(struct map * exports,
658 const char * optionname,
661 static jb_err actions_to_radio(struct map * exports,
662 const struct action_spec *action);
663 static jb_err actions_from_radio(const struct map * parameters,
664 struct action_spec *action);
667 static jb_err map_copy_parameter_html(struct map *out,
668 const struct map *in,
670 #if 0 /* unused function */
671 static jb_err map_copy_parameter_url(struct map *out,
672 const struct map *in,
674 #endif /* unused function */
676 static jb_err get_file_name_param(struct client_state *csp,
677 const struct map *parameters,
678 const char *param_name,
679 const char **pfilename);
681 /* Internal convenience functions */
682 static char *section_target(const unsigned sectionid);
684 /*********************************************************************
686 * Function : section_target
688 * Description : Given an unsigned (section id) n, produce a dynamically
689 * allocated string of the form #l<n>, for use in link
692 * XXX: The hash should be moved into the templates
693 * to make this function more generic and render
694 * stringify() obsolete.
697 * 1 : sectionid = start line number of section
699 * Returns : String with link target, or NULL if out of
702 *********************************************************************/
703 static char *section_target(const unsigned sectionid)
707 snprintf(buf, sizeof(buf), "#l%d", sectionid);
713 /*********************************************************************
715 * Function : stringify
717 * Description : Convert a number into a dynamically allocated string.
720 * 1 : number = The number to convert.
722 * Returns : String with link target, or NULL if out of memory
724 *********************************************************************/
725 static char *stringify(const unsigned number)
729 snprintf(buf, sizeof(buf), "%i", number);
734 /*********************************************************************
736 * Function : map_copy_parameter_html
738 * Description : Copy a CGI parameter from one map to another, HTML
742 * 1 : out = target map
743 * 2 : in = source map
744 * 3 : name = name of cgi parameter to copy
746 * Returns : JB_ERR_OK on success
747 * JB_ERR_MEMORY on out-of-memory
748 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
751 *********************************************************************/
752 static jb_err map_copy_parameter_html(struct map *out,
753 const struct map *in,
763 value = lookup(in, name);
764 err = map(out, name, 1, html_encode(value), 0);
771 else if (*value == '\0')
773 return JB_ERR_CGI_PARAMS;
782 #if 0 /* unused function */
783 /*********************************************************************
785 * Function : map_copy_parameter_url
787 * Description : Copy a CGI parameter from one map to another, URL
791 * 1 : out = target map
792 * 2 : in = source map
793 * 3 : name = name of cgi parameter to copy
795 * Returns : JB_ERR_OK on success
796 * JB_ERR_MEMORY on out-of-memory
797 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
800 *********************************************************************/
801 static jb_err map_copy_parameter_url(struct map *out,
802 const struct map *in,
812 value = lookup(in, name);
813 err = map(out, name, 1, url_encode(value), 0);
820 else if (*value == '\0')
822 return JB_ERR_CGI_PARAMS;
829 #endif /* 0 - unused function */
832 /*********************************************************************
834 * Function : cgi_edit_actions_url_form
836 * Description : CGI function that displays a form for
840 * 1 : csp = Current client state (buffers, headers, etc...)
841 * 2 : rsp = http_response data structure for output
842 * 3 : parameters = map of cgi parameters
845 * i : (action index) Identifies the file to edit
846 * v : (version) File's last-modified time
847 * p : (pattern) Line number of pattern to edit
849 * Returns : JB_ERR_OK on success
850 * JB_ERR_MEMORY on out-of-memory
851 * JB_ERR_CGI_PARAMS if the CGI parameters are not
852 * specified or not valid.
854 *********************************************************************/
855 jb_err cgi_edit_actions_url_form(struct client_state *csp,
856 struct http_response *rsp,
857 const struct map *parameters)
859 struct map * exports;
861 struct editable_file * file;
862 struct file_line * cur_line;
863 unsigned line_number;
864 unsigned section_start_line_number = 0;
871 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
873 return cgi_error_disabled(csp, rsp);
876 err = get_number_param(csp, parameters, "p", &patternid);
882 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
885 /* No filename specified, can't read file, modified, or out of memory. */
886 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
889 cur_line = file->lines;
891 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
893 if (cur_line->type == FILE_LINE_ACTION)
895 section_start_line_number = line_number;
897 cur_line = cur_line->next;
900 if ( (cur_line == NULL)
901 || (line_number != patternid)
903 || (cur_line->type != FILE_LINE_URL))
905 /* Invalid "patternid" parameter */
906 edit_free_file(file);
907 return JB_ERR_CGI_PARAMS;
910 if (NULL == (exports = default_exports(csp, NULL)))
912 edit_free_file(file);
913 return JB_ERR_MEMORY;
916 err = map(exports, "f", 1, stringify(file->identifier), 0);
917 if (!err) err = map(exports, "v", 1, file->version_str, 1);
918 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
919 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
920 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
922 edit_free_file(file);
930 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
934 /*********************************************************************
936 * Function : cgi_edit_actions_add_url_form
938 * Description : CGI function that displays a form for
942 * 1 : csp = Current client state (buffers, headers, etc...)
943 * 2 : rsp = http_response data structure for output
944 * 3 : parameters = map of cgi parameters
947 * f : (filename) Identifies the file to edit
948 * v : (version) File's last-modified time
949 * s : (section) Line number of section to edit
951 * Returns : JB_ERR_OK on success
952 * JB_ERR_MEMORY on out-of-memory
953 * JB_ERR_CGI_PARAMS if the CGI parameters are not
954 * specified or not valid.
956 *********************************************************************/
957 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
958 struct http_response *rsp,
959 const struct map *parameters)
968 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
970 return cgi_error_disabled(csp, rsp);
973 if (NULL == (exports = default_exports(csp, NULL)))
975 return JB_ERR_MEMORY;
978 err = map_copy_parameter_html(exports, parameters, "f");
979 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
980 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
988 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
992 /*********************************************************************
994 * Function : cgi_edit_actions_remove_url_form
996 * Description : CGI function that displays a form for
1000 * 1 : csp = Current client state (buffers, headers, etc...)
1001 * 2 : rsp = http_response data structure for output
1002 * 3 : parameters = map of cgi parameters
1005 * f : (number) The action file identifier.
1006 * v : (version) File's last-modified time
1007 * p : (pattern) Line number of pattern to edit
1009 * Returns : JB_ERR_OK on success
1010 * JB_ERR_MEMORY on out-of-memory
1011 * JB_ERR_CGI_PARAMS if the CGI parameters are not
1012 * specified or not valid.
1014 *********************************************************************/
1015 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
1016 struct http_response *rsp,
1017 const struct map *parameters)
1019 struct map * exports;
1021 struct editable_file * file;
1022 struct file_line * cur_line;
1023 unsigned line_number;
1024 unsigned section_start_line_number = 0;
1031 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1033 return cgi_error_disabled(csp, rsp);
1036 err = get_number_param(csp, parameters, "p", &patternid);
1042 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
1045 /* No filename specified, can't read file, modified, or out of memory. */
1046 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
1049 cur_line = file->lines;
1051 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
1053 if (cur_line->type == FILE_LINE_ACTION)
1055 section_start_line_number = line_number;
1057 cur_line = cur_line->next;
1060 if ( (cur_line == NULL)
1061 || (line_number != patternid)
1063 || (cur_line->type != FILE_LINE_URL))
1065 /* Invalid "patternid" parameter */
1066 edit_free_file(file);
1067 return JB_ERR_CGI_PARAMS;
1070 if (NULL == (exports = default_exports(csp, NULL)))
1072 edit_free_file(file);
1073 return JB_ERR_MEMORY;
1076 err = map(exports, "f", 1, stringify(file->identifier), 0);
1077 if (!err) err = map(exports, "v", 1, file->version_str, 1);
1078 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
1079 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
1080 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
1081 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
1083 edit_free_file(file);
1091 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
1095 /*********************************************************************
1097 * Function : edit_write_file
1099 * Description : Write a complete file to disk.
1102 * 1 : file = File to write.
1104 * Returns : JB_ERR_OK on success
1105 * JB_ERR_FILE on error writing to file.
1106 * JB_ERR_MEMORY on out of memory
1108 *********************************************************************/
1109 jb_err edit_write_file(struct editable_file * file)
1112 struct file_line * cur_line;
1113 struct stat statbuf[1];
1114 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
1115 digits in time_t, assuming this is a 64-bit
1116 machine, plus null terminator, plus one
1120 assert(file->filename);
1122 if (NULL == (fp = fopen(file->filename, "wb")))
1127 cur_line = file->lines;
1128 while (cur_line != NULL)
1132 if (fputs(cur_line->raw, fp) < 0)
1140 if (cur_line->prefix)
1142 if (fputs(cur_line->prefix, fp) < 0)
1148 if (cur_line->unprocessed)
1151 if (NULL != strchr(cur_line->unprocessed, '#'))
1153 /* Must quote '#' characters */
1160 /* Count number of # characters, so we know length of output string */
1161 src = cur_line->unprocessed;
1162 while (NULL != (src = strchr(src, '#')))
1167 assert(numhash > 0);
1169 /* Allocate new memory for string */
1170 len = strlen(cur_line->unprocessed) + (size_t)numhash;
1171 if (NULL == (str = malloc(len + 1)))
1173 /* Uh oh, just trashed file! */
1175 return JB_ERR_MEMORY;
1178 /* Copy string but quote hashes */
1179 src = cur_line->unprocessed;
1187 assert(numhash >= 0);
1193 assert(numhash == 0);
1194 assert(strlen(str) == len);
1195 assert(str == dest - len);
1196 assert(src - len <= cur_line->unprocessed);
1198 if ((strlen(str) != len) || (numhash != 0))
1201 * Escaping didn't work as expected, go spread the news.
1202 * Only reached in non-debugging builds.
1204 log_error(LOG_LEVEL_ERROR,
1205 "Looks like hash escaping failed. %s might be corrupted now.",
1209 if (fputs(str, fp) < 0)
1220 /* Can write without quoting '#' characters. */
1221 if (fputs(cur_line->unprocessed, fp) < 0)
1227 if (fputs(NEWLINE(file->newline), fp) < 0)
1235 /* FIXME: Write data from file->data->whatever */
1239 cur_line = cur_line->next;
1245 /* Update the version stamp in the file structure, since we just
1246 * wrote to the file & changed it's date.
1248 if (stat(file->filename, statbuf) < 0)
1250 /* Error, probably file not found. */
1253 file->version = (unsigned)statbuf->st_mtime;
1255 /* Correct file->version_str */
1256 freez(file->version_str);
1257 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1258 version_buf[sizeof(version_buf)-1] = '\0';
1259 file->version_str = strdup(version_buf);
1260 if (version_buf == NULL)
1262 return JB_ERR_MEMORY;
1269 /*********************************************************************
1271 * Function : edit_free_file
1273 * Description : Free a complete file in memory.
1276 * 1 : file = Data structure to free.
1280 *********************************************************************/
1281 void edit_free_file(struct editable_file * file)
1285 /* Silently ignore NULL pointer */
1289 edit_free_file_lines(file->lines);
1290 freez(file->version_str);
1292 file->parse_error_text = NULL; /* Statically allocated */
1293 file->parse_error = NULL;
1299 /*********************************************************************
1301 * Function : edit_free_file_lines
1303 * Description : Free an entire linked list of file lines.
1306 * 1 : first_line = Data structure to free.
1310 *********************************************************************/
1311 static void edit_free_file_lines(struct file_line * first_line)
1313 struct file_line * next_line;
1315 while (first_line != NULL)
1317 next_line = first_line->next;
1318 first_line->next = NULL;
1319 freez(first_line->raw);
1320 freez(first_line->prefix);
1321 freez(first_line->unprocessed);
1322 switch(first_line->type)
1324 case 0: /* special case if memory zeroed */
1325 case FILE_LINE_UNPROCESSED:
1326 case FILE_LINE_BLANK:
1327 case FILE_LINE_ALIAS_HEADER:
1328 case FILE_LINE_SETTINGS_HEADER:
1329 case FILE_LINE_DESCRIPTION_HEADER:
1330 case FILE_LINE_DESCRIPTION_ENTRY:
1331 case FILE_LINE_ALIAS_ENTRY:
1333 /* No data is stored for these */
1336 case FILE_LINE_ACTION:
1337 free_action(first_line->data.action);
1340 case FILE_LINE_SETTINGS_ENTRY:
1341 freez(first_line->data.setting.name);
1342 freez(first_line->data.setting.svalue);
1345 /* Should never happen */
1349 first_line->type = 0; /* paranoia */
1351 first_line = next_line;
1356 /*********************************************************************
1358 * Function : match_actions_file_header_line
1360 * Description : Match an actions file {{header}} line
1363 * 1 : line = String from file
1364 * 2 : name = Header to match against
1366 * Returns : 0 iff they match.
1368 *********************************************************************/
1369 static int match_actions_file_header_line(const char * line, const char * name)
1377 if ((line[0] != '{') || (line[1] != '{'))
1383 /* Look for optional whitespace */
1384 while ( (*line == ' ') || (*line == '\t') )
1389 /* Look for the specified name (case-insensitive) */
1391 if (0 != strncmpic(line, name, len))
1397 /* Look for optional whitespace */
1398 while ( (*line == ' ') || (*line == '\t') )
1403 /* Look for "}}" and end of string*/
1404 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1414 /*********************************************************************
1416 * Function : match_actions_file_header_line
1418 * Description : Match an actions file {{header}} line
1421 * 1 : line = String from file. Must not start with
1422 * whitespace (else infinite loop!)
1423 * 2 : pname = Destination for name
1424 * 2 : pvalue = Destination for value
1426 * Returns : JB_ERR_OK on success
1427 * JB_ERR_MEMORY on out-of-memory
1428 * JB_ERR_PARSE if there's no "=" sign, or if there's
1429 * nothing before the "=" sign (but empty
1430 * values *after* the "=" sign are legal).
1432 *********************************************************************/
1433 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1435 const char * name_end;
1436 const char * value_start;
1442 assert(*line != ' ');
1443 assert(*line != '\t');
1448 value_start = strchr(line, '=');
1449 if ((value_start == NULL) || (value_start == line))
1451 return JB_ERR_PARSE;
1454 name_end = value_start - 1;
1456 /* Eat any whitespace before the '=' */
1457 while ((*name_end == ' ') || (*name_end == '\t'))
1460 * we already know we must have at least 1 non-ws char
1461 * at start of buf - no need to check
1466 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1467 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1469 return JB_ERR_MEMORY;
1471 strncpy(*pname, line, name_len);
1472 (*pname)[name_len] = '\0';
1474 /* Eat any the whitespace after the '=' */
1476 while ((*value_start == ' ') || (*value_start == '\t'))
1481 if (NULL == (*pvalue = strdup(value_start)))
1485 return JB_ERR_MEMORY;
1492 /*********************************************************************
1494 * Function : edit_parse_actions_file
1496 * Description : Parse an actions file in memory.
1498 * Passed linked list must have the "data" member
1499 * zeroed, and must contain valid "next" and
1500 * "unprocessed" fields. The "raw" and "prefix"
1501 * fields are ignored, and "type" is just overwritten.
1503 * Note that on error the file may have been
1507 * 1 : file = Actions file to be parsed in-place.
1509 * Returns : JB_ERR_OK on success
1510 * JB_ERR_MEMORY on out-of-memory
1511 * JB_ERR_PARSE on error
1513 *********************************************************************/
1514 jb_err edit_parse_actions_file(struct editable_file * file)
1516 struct file_line * cur_line;
1518 const char * text; /* Text from a line */
1519 char * name; /* For lines of the form name=value */
1520 char * value; /* For lines of the form name=value */
1521 struct action_alias * alias_list = NULL;
1522 jb_err err = JB_ERR_OK;
1524 /* alias_list contains the aliases defined in this file.
1525 * It might be better to use the "file_line.data" fields
1526 * in the relavent places instead.
1529 cur_line = file->lines;
1531 /* A note about blank line support: Blank lines should only
1532 * ever occur as the last line in the file. This function
1533 * is more forgiving than that - FILE_LINE_BLANK can occur
1537 /* Skip leading blanks. Should only happen if file is
1538 * empty (which is valid, but pointless).
1540 while ( (cur_line != NULL)
1541 && (cur_line->unprocessed[0] == '\0') )
1544 cur_line->type = FILE_LINE_BLANK;
1545 cur_line = cur_line->next;
1548 if ( (cur_line != NULL)
1549 && (cur_line->unprocessed[0] != '{') )
1551 /* File doesn't start with a header */
1552 file->parse_error = cur_line;
1553 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1554 return JB_ERR_PARSE;
1557 if ( (cur_line != NULL) && (0 ==
1558 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1560 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1562 cur_line = cur_line->next;
1563 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1565 if (cur_line->unprocessed[0])
1567 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1569 err = split_line_on_equals(cur_line->unprocessed,
1570 &cur_line->data.setting.name,
1571 &cur_line->data.setting.svalue);
1572 if (err == JB_ERR_MEMORY)
1576 else if (err != JB_ERR_OK)
1578 /* Line does not contain a name=value pair */
1579 file->parse_error = cur_line;
1580 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1581 return JB_ERR_PARSE;
1586 cur_line->type = FILE_LINE_BLANK;
1588 cur_line = cur_line->next;
1592 if ( (cur_line != NULL) && (0 ==
1593 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1595 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1597 cur_line = cur_line->next;
1598 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1600 if (cur_line->unprocessed[0])
1602 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1606 cur_line->type = FILE_LINE_BLANK;
1608 cur_line = cur_line->next;
1612 if ( (cur_line != NULL) && (0 ==
1613 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1615 cur_line->type = FILE_LINE_ALIAS_HEADER;
1617 cur_line = cur_line->next;
1618 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1620 if (cur_line->unprocessed[0])
1622 /* define an alias */
1623 struct action_alias * new_alias;
1625 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1627 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1628 if (err == JB_ERR_MEMORY)
1632 else if (err != JB_ERR_OK)
1634 /* Line does not contain a name=value pair */
1635 file->parse_error = cur_line;
1636 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1637 return JB_ERR_PARSE;
1640 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1645 free_alias_list(alias_list);
1646 return JB_ERR_MEMORY;
1649 err = get_actions(value, alias_list, new_alias->action);
1652 /* Invalid action or out of memory */
1656 free_alias_list(alias_list);
1657 if (err == JB_ERR_MEMORY)
1663 /* Line does not contain a name=value pair */
1664 file->parse_error = cur_line;
1665 file->parse_error_text = "This alias does not specify a valid set of actions.";
1666 return JB_ERR_PARSE;
1672 new_alias->name = name;
1675 new_alias->next = alias_list;
1676 alias_list = new_alias;
1680 cur_line->type = FILE_LINE_BLANK;
1682 cur_line = cur_line->next;
1686 /* Header done, process the main part of the file */
1687 while (cur_line != NULL)
1689 /* At this point, (cur_line->unprocessed[0] == '{') */
1690 assert(cur_line->unprocessed[0] == '{');
1691 text = cur_line->unprocessed + 1;
1692 len = strlen(text) - 1;
1693 if (text[len] != '}')
1695 /* No closing } on header */
1696 free_alias_list(alias_list);
1697 file->parse_error = cur_line;
1698 file->parse_error_text = "Headers starting with '{' must have a "
1699 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1700 "must close with two brackets ('}}').";
1701 return JB_ERR_PARSE;
1706 /* An invalid {{ header. */
1707 free_alias_list(alias_list);
1708 file->parse_error = cur_line;
1709 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1710 "Please remember that the system (two-bracket) headers must "
1711 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1712 "and must appear before any actions (one-bracket) headers. "
1713 "Also note that system headers may not be repeated.";
1714 return JB_ERR_PARSE;
1717 while ( (*text == ' ') || (*text == '\t') )
1722 while ( (len > (size_t)0)
1723 && ( (text[len - 1] == ' ')
1724 || (text[len - 1] == '\t') ) )
1729 cur_line->type = FILE_LINE_ACTION;
1731 /* Remove {} and make copy */
1732 if (NULL == (value = (char *) malloc(len + 1)))
1735 free_alias_list(alias_list);
1736 return JB_ERR_MEMORY;
1738 strncpy(value, text, len);
1742 err = get_actions(value, alias_list, cur_line->data.action);
1745 /* Invalid action or out of memory */
1747 free_alias_list(alias_list);
1748 if (err == JB_ERR_MEMORY)
1754 /* Line does not contain a name=value pair */
1755 file->parse_error = cur_line;
1756 file->parse_error_text = "This header does not specify a valid set of actions.";
1757 return JB_ERR_PARSE;
1761 /* Done with string - it was clobbered anyway */
1764 /* Process next line */
1765 cur_line = cur_line->next;
1767 /* Loop processing URL patterns */
1768 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1770 if (cur_line->unprocessed[0])
1772 /* Could parse URL here, but this isn't currently needed */
1774 cur_line->type = FILE_LINE_URL;
1778 cur_line->type = FILE_LINE_BLANK;
1780 cur_line = cur_line->next;
1782 } /* End main while(cur_line != NULL) loop */
1784 free_alias_list(alias_list);
1790 /*********************************************************************
1792 * Function : edit_read_file_lines
1794 * Description : Read all the lines of a file into memory.
1795 * Handles whitespace, comments and line continuation.
1798 * 1 : fp = File to read from. On return, this will be
1799 * at EOF but it will not have been closed.
1800 * 2 : pfile = Destination for a linked list of file_lines.
1801 * Will be set to NULL on error.
1802 * 3 : newline = How to handle newlines.
1804 * Returns : JB_ERR_OK on success
1805 * JB_ERR_MEMORY on out-of-memory
1807 *********************************************************************/
1808 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1810 struct file_line * first_line; /* Keep for return value or to free */
1811 struct file_line * cur_line; /* Current line */
1812 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1820 cur_line = first_line = zalloc(sizeof(struct file_line));
1821 if (cur_line == NULL)
1823 return JB_ERR_MEMORY;
1826 cur_line->type = FILE_LINE_UNPROCESSED;
1828 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1831 /* Out of memory or empty file. */
1832 /* Note that empty file is not an error we propogate up */
1834 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1839 prev_line = cur_line;
1840 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1841 if (cur_line == NULL)
1844 edit_free_file_lines(first_line);
1845 return JB_ERR_MEMORY;
1848 cur_line->type = FILE_LINE_UNPROCESSED;
1850 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1851 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1854 edit_free_file_lines(first_line);
1855 return JB_ERR_MEMORY;
1859 while (rval != JB_ERR_FILE);
1863 /* We allocated one too many - free it */
1864 prev_line->next = NULL;
1867 *pfile = first_line;
1872 /*********************************************************************
1874 * Function : edit_read_file
1876 * Description : Read a complete file into memory.
1877 * Handles CGI parameter parsing. If requested, also
1878 * checks the file's modification timestamp.
1881 * 1 : csp = Current client state (buffers, headers, etc...)
1882 * 2 : parameters = map of cgi parameters.
1883 * 3 : require_version = true to check "ver" parameter.
1884 * 4 : pfile = Destination for the file. Will be set
1888 * f : The action file identifier.
1889 * ver : (Only if require_version is nonzero)
1890 * Timestamp of the actions file. If wrong, this
1891 * function fails with JB_ERR_MODIFIED.
1893 * Returns : JB_ERR_OK on success
1894 * JB_ERR_MEMORY on out-of-memory
1895 * JB_ERR_CGI_PARAMS if "filename" was not specified
1897 * JB_ERR_FILE if the file cannot be opened or
1899 * JB_ERR_MODIFIED if version checking was requested and
1900 * failed - the file was modified outside
1901 * of this CGI editor instance.
1903 *********************************************************************/
1904 jb_err edit_read_file(struct client_state *csp,
1905 const struct map *parameters,
1906 int require_version,
1907 struct editable_file **pfile)
1909 struct file_line * lines;
1912 const char *filename = NULL;
1913 struct editable_file * file;
1914 unsigned version = 0;
1915 struct stat statbuf[1];
1916 char version_buf[22];
1917 int newline = NEWLINE_UNKNOWN;
1926 err = get_number_param(csp, parameters, "f", &i);
1927 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1929 filename = csp->config->actions_file[i];
1931 else if (JB_ERR_CGI_PARAMS == err)
1934 * Probably an old-school URL like
1935 * http://config.privoxy.org/edit-actions-list?f=default
1937 err = get_file_name_param(csp, parameters, "f", &filename);
1940 if (NULL == filename || stat(filename, statbuf) < 0)
1942 /* Error, probably file not found. */
1945 version = (unsigned) statbuf->st_mtime;
1947 if (require_version)
1949 unsigned specified_version;
1950 err = get_number_param(csp, parameters, "v", &specified_version);
1956 if (version != specified_version)
1958 return JB_ERR_MODIFIED;
1962 if (NULL == (fp = fopen(filename,"rb")))
1967 err = edit_read_file_lines(fp, &lines, &newline);
1976 file = (struct editable_file *) zalloc(sizeof(*file));
1979 edit_free_file_lines(lines);
1983 file->lines = lines;
1984 file->newline = newline;
1985 file->filename = filename;
1986 file->version = version;
1987 file->identifier = i;
1989 /* Correct file->version_str */
1990 freez(file->version_str);
1991 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1992 version_buf[sizeof(version_buf)-1] = '\0';
1993 file->version_str = strdup(version_buf);
1994 if (version_buf == NULL)
1996 edit_free_file(file);
1997 return JB_ERR_MEMORY;
2005 /*********************************************************************
2007 * Function : edit_read_actions_file
2009 * Description : Read a complete actions file into memory.
2010 * Handles CGI parameter parsing. If requested, also
2011 * checks the file's modification timestamp.
2013 * If this function detects an error in the categories
2014 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
2015 * then it handles it by filling in the specified
2016 * response structure and returning JB_ERR_FILE.
2019 * 1 : csp = Current client state (buffers, headers, etc...)
2020 * 2 : rsp = HTTP response. Only filled in on error.
2021 * 2 : parameters = map of cgi parameters.
2022 * 3 : require_version = true to check "ver" parameter.
2023 * 4 : pfile = Destination for the file. Will be set
2027 * f : The actions file identifier.
2028 * ver : (Only if require_version is nonzero)
2029 * Timestamp of the actions file. If wrong, this
2030 * function fails with JB_ERR_MODIFIED.
2032 * Returns : JB_ERR_OK on success
2033 * JB_ERR_MEMORY on out-of-memory
2034 * JB_ERR_CGI_PARAMS if "filename" was not specified
2036 * JB_ERR_FILE if the file does not contain valid data,
2037 * or if file cannot be opened or
2038 * contains no data, or if version
2039 * checking was requested and failed.
2041 *********************************************************************/
2042 jb_err edit_read_actions_file(struct client_state *csp,
2043 struct http_response *rsp,
2044 const struct map *parameters,
2045 int require_version,
2046 struct editable_file **pfile)
2049 struct editable_file *file;
2050 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
2058 err = edit_read_file(csp, parameters, require_version, &file);
2061 /* Try to handle if possible */
2062 if (err == JB_ERR_FILE)
2064 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
2066 else if (err == JB_ERR_MODIFIED)
2068 assert(require_version);
2069 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
2070 log_error(LOG_LEVEL_ERROR,
2071 "Blocking CGI edit request due to modification time mismatch.");
2072 if (acceptable_failures > 0)
2074 log_error(LOG_LEVEL_INFO,
2075 "The CGI editor will be turned off after another %d mismatche(s).",
2076 acceptable_failures);
2077 acceptable_failures--;
2081 log_error(LOG_LEVEL_INFO,
2082 "Timestamp mismatch limit reached, turning CGI editor off. "
2083 "Reload the configuration file to reenable it.");
2084 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
2087 if (err == JB_ERR_OK)
2090 * Signal to higher-level CGI code that there was a problem but we
2091 * handled it, they should just return JB_ERR_OK.
2098 err = edit_parse_actions_file(file);
2101 if (err == JB_ERR_PARSE)
2103 err = cgi_error_parse(csp, rsp, file);
2104 if (err == JB_ERR_OK)
2107 * Signal to higher-level CGI code that there was a problem but we
2108 * handled it, they should just return JB_ERR_OK.
2113 edit_free_file(file);
2122 /*********************************************************************
2124 * Function : get_file_name_param
2126 * Description : Get the name of the file to edit from the parameters
2127 * passed to a CGI function using the old syntax.
2128 * This function handles security checks and only
2129 * accepts files that Privoxy already knows.
2132 * 1 : csp = Current client state (buffers, headers, etc...)
2133 * 2 : parameters = map of cgi parameters
2134 * 3 : param_name = The name of the parameter to read
2135 * 4 : pfilename = pointer to the filename in
2136 * csp->config->actions_file[] if found. Set to NULL on error.
2138 * Returns : JB_ERR_OK on success
2139 * JB_ERR_MEMORY on out-of-memory
2140 * JB_ERR_CGI_PARAMS if "filename" was not specified
2143 *********************************************************************/
2144 static jb_err get_file_name_param(struct client_state *csp,
2145 const struct map *parameters,
2146 const char *param_name,
2147 const char **pfilename)
2150 const char suffix[] = ".action";
2165 param = lookup(parameters, param_name);
2168 return JB_ERR_CGI_PARAMS;
2171 len = strlen(param);
2172 if (len >= FILENAME_MAX)
2175 return JB_ERR_CGI_PARAMS;
2179 * Check every character to see if it's legal.
2180 * Totally unnecessary but we do it anyway.
2183 while ((ch = *s++) != '\0')
2185 if ( ((ch < 'A') || (ch > 'Z'))
2186 && ((ch < 'a') || (ch > 'z'))
2187 && ((ch < '0') || (ch > '9'))
2191 /* Probable hack attempt. */
2192 return JB_ERR_CGI_PARAMS;
2196 /* Append extension */
2197 name_size = len + strlen(suffix) + 1;
2198 name = malloc(name_size);
2201 return JB_ERR_MEMORY;
2203 strlcpy(name, param, name_size);
2204 strlcat(name, suffix, name_size);
2207 fullpath = make_path(csp->config->confdir, name);
2210 if (fullpath == NULL)
2212 return JB_ERR_MEMORY;
2215 /* Check if the file is known */
2216 for (i = 0; i < MAX_AF_FILES; i++)
2218 if (NULL != csp->config->actions_file[i] &&
2219 !strcmp(fullpath, csp->config->actions_file[i]))
2222 *pfilename = csp->config->actions_file[i];
2230 return JB_ERR_CGI_PARAMS;
2234 /*********************************************************************
2236 * Function : get_url_spec_param
2238 * Description : Get a URL pattern from the parameters
2239 * passed to a CGI function. Removes leading/trailing
2240 * spaces and validates it.
2243 * 1 : csp = Current client state (buffers, headers, etc...)
2244 * 2 : parameters = map of cgi parameters
2245 * 3 : name = Name of CGI parameter to read
2246 * 4 : pvalue = destination for value. Will be malloc()'d.
2247 * Set to NULL on error.
2249 * Returns : JB_ERR_OK on success
2250 * JB_ERR_MEMORY on out-of-memory
2251 * JB_ERR_CGI_PARAMS if the parameter was not specified
2254 *********************************************************************/
2255 static jb_err get_url_spec_param(struct client_state *csp,
2256 const struct map *parameters,
2260 const char *orig_param;
2263 struct url_spec compiled[1];
2273 orig_param = lookup(parameters, name);
2276 return JB_ERR_CGI_PARAMS;
2279 /* Copy and trim whitespace */
2280 param = strdup(orig_param);
2283 return JB_ERR_MEMORY;
2287 /* Must be non-empty, and can't allow 1st character to be '{' */
2288 if (param[0] == '\0' || param[0] == '{')
2291 return JB_ERR_CGI_PARAMS;
2294 /* Check for embedded newlines */
2295 for (s = param; *s != '\0'; s++)
2297 if ((*s == '\r') || (*s == '\n'))
2300 return JB_ERR_CGI_PARAMS;
2304 /* Check that regex is valid */
2309 return JB_ERR_MEMORY;
2311 err = create_url_spec(compiled, s);
2316 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2318 free_url_spec(compiled);
2320 if (param[strlen(param) - 1] == '\\')
2323 * Must protect trailing '\\' from becoming line continuation character.
2324 * Two methods: 1) If it's a domain only, add a trailing '/'.
2325 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2327 if (strchr(param, '/') == NULL)
2329 err = string_append(¶m, "/");
2333 err = string_append(¶m, "(?:)");
2340 /* Check that the modified regex is valid */
2345 return JB_ERR_MEMORY;
2347 err = create_url_spec(compiled, s);
2352 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2354 free_url_spec(compiled);
2361 /*********************************************************************
2363 * Function : map_radio
2365 * Description : Map a set of radio button values. E.g. if you have
2366 * 3 radio buttons, declare them as:
2367 * <option type="radio" name="xyz" @xyz-a@>
2368 * <option type="radio" name="xyz" @xyz-b@>
2369 * <option type="radio" name="xyz" @xyz-c@>
2370 * Then map one of the @xyz-?@ variables to "checked"
2371 * and all the others to empty by calling:
2372 * map_radio(exports, "xyz", "abc", sel)
2373 * Where 'sel' is 'a', 'b', or 'c'.
2376 * 1 : exports = Exports map to modify.
2377 * 2 : optionname = name for map
2378 * 3 : values = null-terminated list of values;
2379 * 4 : value = Selected value.
2381 * CGI Parameters : None
2383 * Returns : JB_ERR_OK on success
2384 * JB_ERR_MEMORY on out-of-memory
2386 *********************************************************************/
2387 static jb_err map_radio(struct map * exports,
2388 const char * optionname,
2389 const char * values,
2395 const size_t len = strlen(optionname);
2396 const size_t buf_size = len + 3;
2402 buf = malloc(buf_size);
2405 return JB_ERR_MEMORY;
2408 strlcpy(buf, optionname, buf_size);
2410 /* XXX: this looks ... interesting */
2415 while ((c = *values++) != '\0')
2420 if (map(exports, buf, 1, "", 1))
2422 return JB_ERR_MEMORY;
2428 return map(exports, buf, 0, "checked", 1);
2432 /*********************************************************************
2434 * Function : cgi_error_modified
2436 * Description : CGI function that is called when a file is modified
2437 * outside the CGI editor.
2440 * 1 : csp = Current client state (buffers, headers, etc...)
2441 * 2 : rsp = http_response data structure for output
2442 * 3 : filename = The file that was modified.
2444 * CGI Parameters : none
2446 * Returns : JB_ERR_OK on success
2447 * JB_ERR_MEMORY on out-of-memory error.
2449 *********************************************************************/
2450 jb_err cgi_error_modified(struct client_state *csp,
2451 struct http_response *rsp,
2452 const char *filename)
2454 struct map *exports;
2461 if (NULL == (exports = default_exports(csp, NULL)))
2463 return JB_ERR_MEMORY;
2466 err = map(exports, "f", 1, html_encode(filename), 0);
2473 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2477 /*********************************************************************
2479 * Function : cgi_error_parse
2481 * Description : CGI function that is called when a file cannot
2482 * be parsed by the CGI editor.
2485 * 1 : csp = Current client state (buffers, headers, etc...)
2486 * 2 : rsp = http_response data structure for output
2487 * 3 : file = The file that was modified.
2489 * CGI Parameters : none
2491 * Returns : JB_ERR_OK on success
2492 * JB_ERR_MEMORY on out-of-memory error.
2494 *********************************************************************/
2495 jb_err cgi_error_parse(struct client_state *csp,
2496 struct http_response *rsp,
2497 struct editable_file *file)
2499 struct map *exports;
2501 struct file_line *cur_line;
2507 if (NULL == (exports = default_exports(csp, NULL)))
2509 return JB_ERR_MEMORY;
2512 err = map(exports, "f", 1, stringify(file->identifier), 0);
2513 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2515 cur_line = file->parse_error;
2518 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2519 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2527 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2531 /*********************************************************************
2533 * Function : cgi_error_file
2535 * Description : CGI function that is called when a file cannot be
2536 * opened by the CGI editor.
2539 * 1 : csp = Current client state (buffers, headers, etc...)
2540 * 2 : rsp = http_response data structure for output
2541 * 3 : filename = The file that was modified.
2543 * CGI Parameters : none
2545 * Returns : JB_ERR_OK on success
2546 * JB_ERR_MEMORY on out-of-memory error.
2548 *********************************************************************/
2549 jb_err cgi_error_file(struct client_state *csp,
2550 struct http_response *rsp,
2551 const char *filename)
2553 struct map *exports;
2560 if (NULL == (exports = default_exports(csp, NULL)))
2562 return JB_ERR_MEMORY;
2565 err = map(exports, "f", 1, html_encode(filename), 0);
2572 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2576 /*********************************************************************
2578 * Function : cgi_error_file_read_only
2580 * Description : CGI function that is called when a file cannot be
2581 * opened for writing by the CGI editor.
2584 * 1 : csp = Current client state (buffers, headers, etc...)
2585 * 2 : rsp = http_response data structure for output
2586 * 3 : filename = The file that we can't write to
2588 * CGI Parameters : none
2590 * Returns : JB_ERR_OK on success
2591 * JB_ERR_MEMORY on out-of-memory error.
2593 *********************************************************************/
2594 jb_err cgi_error_file_read_only(struct client_state *csp,
2595 struct http_response *rsp,
2596 const char *filename)
2598 struct map *exports;
2605 if (NULL == (exports = default_exports(csp, NULL)))
2607 return JB_ERR_MEMORY;
2610 err = map(exports, "f", 1, html_encode(filename), 0);
2617 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2621 /*********************************************************************
2623 * Function : cgi_edit_actions
2625 * Description : CGI function that allows the user to choose which
2626 * actions file to edit.
2629 * 1 : csp = Current client state (buffers, headers, etc...)
2630 * 2 : rsp = http_response data structure for output
2631 * 3 : parameters = map of cgi parameters
2633 * CGI Parameters : None
2635 * Returns : JB_ERR_OK on success
2636 * JB_ERR_MEMORY on out-of-memory error
2638 *********************************************************************/
2639 jb_err cgi_edit_actions(struct client_state *csp,
2640 struct http_response *rsp,
2641 const struct map *parameters)
2645 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2647 return cgi_error_disabled(csp, rsp);
2650 /* FIXME: Incomplete */
2652 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2657 /*********************************************************************
2659 * Function : cgi_edit_actions_list
2661 * Description : CGI function that edits the actions list.
2662 * FIXME: This function shouldn't FATAL ever.
2663 * FIXME: This function doesn't check the retval of map()
2665 * 1 : csp = Current client state (buffers, headers, etc...)
2666 * 2 : rsp = http_response data structure for output
2667 * 3 : parameters = map of cgi parameters
2669 * CGI Parameters : filename
2671 * Returns : JB_ERR_OK on success
2672 * JB_ERR_MEMORY on out-of-memory
2673 * JB_ERR_FILE if the file cannot be opened or
2675 * JB_ERR_CGI_PARAMS if "filename" was not specified
2678 *********************************************************************/
2679 jb_err cgi_edit_actions_list(struct client_state *csp,
2680 struct http_response *rsp,
2681 const struct map *parameters)
2683 char * section_template;
2684 char * url_template;
2689 struct map * exports;
2690 struct map * section_exports;
2691 struct map * url_exports;
2692 struct editable_file * file;
2693 struct file_line * cur_line;
2694 unsigned line_number = 0;
2695 unsigned prev_section_line_number = ((unsigned) (-1));
2697 struct file_list * fl;
2698 struct url_actions * b;
2699 char * buttons = NULL;
2702 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2704 return cgi_error_disabled(csp, rsp);
2707 if (NULL == (exports = default_exports(csp, NULL)))
2709 return JB_ERR_MEMORY;
2712 /* Load actions file */
2713 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2716 /* No filename specified, can't read file, or out of memory. */
2718 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2721 /* Find start of actions in file */
2722 cur_line = file->lines;
2724 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2726 cur_line = cur_line->next;
2731 * Conventional actions files should have a match all block
2733 * cur_line = {...global actions...}
2734 * cur_line->next = /
2735 * cur_line->next->next = {...actions...} or EOF
2737 if ( (cur_line != NULL)
2738 && (cur_line->type == FILE_LINE_ACTION)
2739 && (cur_line->next != NULL)
2740 && (cur_line->next->type == FILE_LINE_URL)
2741 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2742 && ( (cur_line->next->next == NULL)
2743 || (cur_line->next->next->type != FILE_LINE_URL)
2747 * Generate string with buttons to set actions for "/" to
2748 * any predefined set of actions (named standard.*, probably
2749 * residing in standard.action).
2752 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2755 edit_free_file(file);
2757 if (err == JB_ERR_FILE)
2759 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2764 err = template_fill(§ion_template, exports);
2767 edit_free_file(file);
2772 buttons = strdup("");
2773 for (i = 0; i < MAX_AF_FILES; i++)
2775 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2777 for (b = b->next; NULL != b; b = b->next)
2779 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2781 if (err || (NULL == (section_exports = new_map())))
2784 free(section_template);
2785 edit_free_file(file);
2787 return JB_ERR_MEMORY;
2790 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2792 if (err || (NULL == (s = strdup(section_template))))
2794 free_map(section_exports);
2796 free(section_template);
2797 edit_free_file(file);
2799 return JB_ERR_MEMORY;
2802 if (!err) err = template_fill(&s, section_exports);
2803 free_map(section_exports);
2804 if (!err) err = string_join(&buttons, s);
2809 freez(section_template);
2810 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2813 * Conventional actions file, supply extra editing help.
2814 * (e.g. don't allow them to make it an unconventional one).
2816 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2818 snprintf(buf, sizeof(buf), "%d", line_number);
2819 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2820 snprintf(buf, sizeof(buf), "%d", line_number + 2);
2821 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2822 if (!err) err = map(exports, "all-urls-actions", 1,
2823 actions_to_html(csp, cur_line->data.action), 0);
2825 /* Skip the 2 lines */
2826 cur_line = cur_line->next->next;
2830 * Note that prev_section_line_number is NOT set here.
2831 * This is deliberate and not a bug. It stops a "Move up"
2832 * option appearing on the next section. Clicking "Move
2833 * up" would make the actions file unconventional, which
2834 * we don't want, so we hide this option.
2840 * Non-standard actions file - does not begin with
2841 * the "All URLs" section.
2843 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2846 /* Set up global exports */
2848 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2849 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2850 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2852 /* Discourage private additions to default.action */
2854 if (!err) err = map_conditional(exports, "default-action",
2855 (strstr("default.action", file->filename) != NULL));
2858 edit_free_file(file);
2863 /* Should do all global exports above this point */
2865 /* Load templates */
2867 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2870 edit_free_file(file);
2872 if (err == JB_ERR_FILE)
2874 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2879 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2882 free(section_template);
2883 edit_free_file(file);
2885 if (err == JB_ERR_FILE)
2887 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2892 err = template_fill(§ion_template, exports);
2896 edit_free_file(file);
2901 err = template_fill(&url_template, exports);
2904 free(section_template);
2905 edit_free_file(file);
2910 if (NULL == (sections = strdup("")))
2912 free(section_template);
2914 edit_free_file(file);
2916 return JB_ERR_MEMORY;
2919 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2921 if (NULL == (section_exports = new_map()))
2924 free(section_template);
2926 edit_free_file(file);
2928 return JB_ERR_MEMORY;
2931 snprintf(buf, sizeof(buf), "%d", line_number);
2932 err = map(section_exports, "s", 1, buf, 1);
2933 if (!err) err = map(section_exports, "actions", 1,
2934 actions_to_html(csp, cur_line->data.action), 0);
2937 && (cur_line->next != NULL)
2938 && (cur_line->next->type == FILE_LINE_URL))
2940 /* This section contains at least one URL, don't allow delete */
2941 err = map_block_killer(section_exports, "empty-section");
2945 if (!err) err = map_block_keep(section_exports, "empty-section");
2948 if (prev_section_line_number != ((unsigned)(-1)))
2950 /* Not last section */
2951 snprintf(buf, sizeof(buf), "%d", prev_section_line_number);
2952 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2953 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2958 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2960 prev_section_line_number = line_number;
2965 free(section_template);
2967 edit_free_file(file);
2969 free_map(section_exports);
2973 /* Should do all section-specific exports above this point */
2975 if (NULL == (urls = strdup("")))
2978 free(section_template);
2980 edit_free_file(file);
2982 free_map(section_exports);
2983 return JB_ERR_MEMORY;
2988 cur_line = cur_line->next;
2991 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2993 if (NULL == (url_exports = new_map()))
2997 free(section_template);
2999 edit_free_file(file);
3001 free_map(section_exports);
3002 return JB_ERR_MEMORY;
3005 snprintf(buf, sizeof(buf), "%d", line_number);
3006 err = map(url_exports, "p", 1, buf, 1);
3008 snprintf(buf, sizeof(buf), "%d", url_1_2);
3009 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
3011 if (!err) err = map(url_exports, "url-html", 1,
3012 html_encode(cur_line->unprocessed), 0);
3013 if (!err) err = map(url_exports, "url", 1,
3014 url_encode(cur_line->unprocessed), 0);
3020 free(section_template);
3022 edit_free_file(file);
3024 free_map(section_exports);
3025 free_map(url_exports);
3029 if (NULL == (s = strdup(url_template)))
3033 free(section_template);
3035 edit_free_file(file);
3037 free_map(section_exports);
3038 free_map(url_exports);
3039 return JB_ERR_MEMORY;
3042 err = template_fill(&s, section_exports);
3043 if (!err) err = template_fill(&s, url_exports);
3044 if (!err) err = string_append(&urls, s);
3046 free_map(url_exports);
3053 free(section_template);
3055 edit_free_file(file);
3057 free_map(section_exports);
3061 url_1_2 = 3 - url_1_2;
3063 cur_line = cur_line->next;
3067 err = map(section_exports, "urls", 1, urls, 0);
3069 /* Could also do section-specific exports here, but it wouldn't be as fast */
3071 snprintf(buf, sizeof(buf), "%d", line_number);
3072 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
3074 if ( (cur_line != NULL)
3075 && (cur_line->type == FILE_LINE_ACTION))
3077 /* Not last section */
3078 if (!err) err = map_block_keep(section_exports, "s-next-exists");
3083 if (!err) err = map_block_killer(section_exports, "s-next-exists");
3089 free(section_template);
3091 edit_free_file(file);
3093 free_map(section_exports);
3097 if (NULL == (s = strdup(section_template)))
3100 free(section_template);
3102 edit_free_file(file);
3104 free_map(section_exports);
3105 return JB_ERR_MEMORY;
3108 err = template_fill(&s, section_exports);
3109 if (!err) err = string_append(§ions, s);
3112 free_map(section_exports);
3117 free(section_template);
3119 edit_free_file(file);
3125 edit_free_file(file);
3126 free(section_template);
3129 err = map(exports, "sections", 1, sections, 0);
3136 /* Could also do global exports here, but it wouldn't be as fast */
3138 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
3142 /*********************************************************************
3144 * Function : cgi_edit_actions_for_url
3146 * Description : CGI function that edits the Actions list.
3149 * 1 : csp = Current client state (buffers, headers, etc...)
3150 * 2 : rsp = http_response data structure for output
3151 * 3 : parameters = map of cgi parameters
3153 * CGI Parameters : None
3155 * Returns : JB_ERR_OK on success
3156 * JB_ERR_MEMORY on out-of-memory
3157 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3158 * specified or not valid.
3160 *********************************************************************/
3161 jb_err cgi_edit_actions_for_url(struct client_state *csp,
3162 struct http_response *rsp,
3163 const struct map *parameters)
3165 struct map * exports;
3167 struct editable_file * file;
3168 struct file_line * cur_line;
3169 unsigned line_number;
3171 struct re_filterfile_spec *filter_group;
3172 int i, have_filters = 0;
3174 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3176 return cgi_error_disabled(csp, rsp);
3179 err = get_number_param(csp, parameters, "s", §ionid);
3185 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3188 /* No filename specified, can't read file, modified, or out of memory. */
3189 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3192 cur_line = file->lines;
3194 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3196 cur_line = cur_line->next;
3199 if ( (cur_line == NULL)
3200 || (line_number != sectionid)
3202 || (cur_line->type != FILE_LINE_ACTION))
3204 /* Invalid "sectionid" parameter */
3205 edit_free_file(file);
3206 return JB_ERR_CGI_PARAMS;
3209 if (NULL == (exports = default_exports(csp, NULL)))
3211 edit_free_file(file);
3212 return JB_ERR_MEMORY;
3215 err = map(exports, "f", 1, stringify(file->identifier), 0);
3216 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3217 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3219 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3222 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3223 * length limitation and ignore clicks on the Submit buttons if
3224 * the resulting GET URL would be longer than their limit.
3226 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3227 * reached this limit and action editing stopped working in these
3228 * browsers (BR #1570678).
3230 * The config option split-large-forms works around this browser
3231 * bug (HTTP has no URL lenght limitation) by deviding the action
3232 * list form into multiple smaller ones. It means the URLs are shorter
3233 * and work in broken browsers as well, but the user can no longer change
3234 * all actions with one submit.
3236 * A better solution would be to switch to POST requests,
3237 * but this will do for now.
3239 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3241 /* Generate multiple smaller form by killing the big one. */
3242 err = map_block_killer(exports, "one-form-only");
3246 /* Default: Generate one large form by killing the smaller ones. */
3247 err = map_block_killer(exports, "multiple-forms");
3250 for (i = 0; i < MAX_AF_FILES; i++)
3252 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3254 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3262 edit_free_file(file);
3267 if (0 == have_filters)
3269 err = map(exports, "filter-params", 1, "", 1);
3274 * List available filters and their settings.
3276 char *filter_template;
3277 int filter_identifier = 0;
3278 char *prepared_templates[MAX_FILTER_TYPES];
3280 for (i = 0; i < MAX_FILTER_TYPES; i++)
3282 prepared_templates[i] = strdup("");
3285 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3288 edit_free_file(file);
3290 if (err == JB_ERR_FILE)
3292 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3297 err = template_fill(&filter_template, exports);
3299 for (i = 0; i < MAX_AF_FILES; i++)
3301 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3303 filter_group = csp->rlist[i]->f;
3304 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3306 char current_mode = 'x';
3308 struct list_entry *filter_name;
3309 struct map *line_exports;
3310 const int type = filter_group->type;
3311 const int multi_action_index = filter_type_info[type].multi_action_index;
3313 assert(type < MAX_FILTER_TYPES);
3315 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
3316 while ((filter_name != NULL)
3317 && (0 != strcmp(filter_group->name, filter_name->str)))
3319 filter_name = filter_name->next;
3322 if (filter_name != NULL)
3328 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
3329 while ((filter_name != NULL)
3330 && (0 != strcmp(filter_group->name, filter_name->str)))
3332 filter_name = filter_name->next;
3334 if (filter_name != NULL)
3340 /* Generate a unique serial number */
3341 snprintf(number, sizeof(number), "%x", filter_identifier++);
3342 number[sizeof(number) - 1] = '\0';
3344 line_exports = new_map();
3345 if (line_exports == NULL)
3347 err = JB_ERR_MEMORY;
3353 if (!err) err = map(line_exports, "index", 1, number, 1);
3354 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3355 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3356 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3357 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
3358 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
3359 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
3363 filter_line = strdup(filter_template);
3364 if (filter_line == NULL) err = JB_ERR_MEMORY;
3366 if (!err) err = template_fill(&filter_line, line_exports);
3367 string_join(&prepared_templates[type], filter_line);
3369 free_map(line_exports);
3374 freez(filter_template);
3376 /* Replace all filter macros with the aggregated templates */
3377 for (i = 0; i < MAX_FILTER_TYPES; i++)
3380 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
3385 /* Free aggregated templates */
3386 for (i = 0; i < MAX_FILTER_TYPES; i++)
3388 freez(prepared_templates[i]);
3393 /* Check or uncheck the "disable all of this type" radio buttons. */
3394 for (i = 0; i < MAX_FILTER_TYPES; i++)
3396 const int a = filter_type_info[i].multi_action_index;
3397 const int disable_all = cur_line->data.action->multi_remove_all[a];
3399 err = map_radio(exports, filter_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
3402 edit_free_file(file);
3410 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3414 /*********************************************************************
3416 * Function : cgi_edit_actions_submit
3418 * Description : CGI function that actually edits the Actions list.
3421 * 1 : csp = Current client state (buffers, headers, etc...)
3422 * 2 : rsp = http_response data structure for output
3423 * 3 : parameters = map of cgi parameters
3425 * CGI Parameters : None
3427 * Returns : JB_ERR_OK on success
3428 * JB_ERR_MEMORY on out-of-memory
3429 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3430 * specified or not valid.
3432 *********************************************************************/
3433 jb_err cgi_edit_actions_submit(struct client_state *csp,
3434 struct http_response *rsp,
3435 const struct map *parameters)
3440 size_t newtext_size;
3442 struct editable_file * file;
3443 struct file_line * cur_line;
3444 unsigned line_number;
3447 int filter_identifier;
3449 const char * action_set_name;
3450 struct file_list * fl;
3451 struct url_actions * b;
3453 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3455 return cgi_error_disabled(csp, rsp);
3458 err = get_number_param(csp, parameters, "s", §ionid);
3464 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3467 /* No filename specified, can't read file, modified, or out of memory. */
3468 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3471 cur_line = file->lines;
3473 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3475 cur_line = cur_line->next;
3478 if ( (cur_line == NULL)
3479 || (line_number != sectionid)
3481 || (cur_line->type != FILE_LINE_ACTION))
3483 /* Invalid "sectionid" parameter */
3484 edit_free_file(file);
3485 return JB_ERR_CGI_PARAMS;
3488 get_string_param(parameters, "p", &action_set_name);
3489 if (action_set_name != NULL)
3491 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3493 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3495 for (b = b->next; NULL != b; b = b->next)
3497 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3499 copy_action(cur_line->data.action, b->action);
3505 edit_free_file(file);
3506 return JB_ERR_CGI_PARAMS;
3512 err = actions_from_radio(parameters, cur_line->data.action);
3518 edit_free_file(file);
3522 /* Check the "disable all of this type" parameters. */
3523 for (i = 0; i < MAX_FILTER_TYPES; i++)
3525 const int multi_action_index = filter_type_info[i].multi_action_index;
3526 const char ch = get_char_param(parameters, filter_type_info[i].disable_all_param);
3530 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3531 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3532 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3536 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3540 for (filter_identifier = 0; !err; filter_identifier++)
3547 * Filter state. Valid states are: 'Y' (active),
3548 * 'N' (inactive) and 'X' (no change).
3552 * Abbreviated filter type. Valid types are: 'F' (content filter),
3553 * 'S' (server-header filter) and 'C' (client-header filter).
3555 int multi_action_index = 0;
3557 /* Generate the keys */
3558 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3559 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3560 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3561 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3562 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3564 err = get_string_param(parameters, key_name, &name);
3573 type = get_char_param(parameters, key_type);
3577 multi_action_index = ACTION_MULTI_FILTER;
3580 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3583 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3586 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3589 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3592 log_error(LOG_LEVEL_ERROR,
3593 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3596 assert(multi_action_index);
3598 value = get_char_param(parameters, key_value);
3601 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3602 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3603 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3605 else if (value == 'N')
3607 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3608 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3610 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3611 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3614 else if (value == 'X')
3616 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3617 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3624 edit_free_file(file);
3628 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3631 edit_free_file(file);
3632 return JB_ERR_MEMORY;
3635 len = strlen(actiontext);
3639 * Empty action - must special-case this.
3640 * Simply setting len to 1 is sufficient...
3645 newtext_size = len + 2;
3646 if (NULL == (newtext = malloc(newtext_size)))
3650 edit_free_file(file);
3651 return JB_ERR_MEMORY;
3653 strlcpy(newtext, actiontext, newtext_size);
3657 newtext[len + 1] = '\0';
3659 freez(cur_line->raw);
3660 freez(cur_line->unprocessed);
3661 cur_line->unprocessed = newtext;
3663 err = edit_write_file(file);
3666 /* Error writing file */
3667 if (err == JB_ERR_FILE)
3669 /* Read-only file. */
3670 err = cgi_error_file_read_only(csp, rsp, file->filename);
3672 edit_free_file(file);
3676 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3677 (long) time(NULL), file->identifier, sectionid);
3679 edit_free_file(file);
3681 return cgi_redirect(rsp, target);
3685 /*********************************************************************
3687 * Function : cgi_edit_actions_url
3689 * Description : CGI function that actually edits a URL pattern in
3693 * 1 : csp = Current client state (buffers, headers, etc...)
3694 * 2 : rsp = http_response data structure for output
3695 * 3 : parameters = map of cgi parameters
3698 * filename : Identifies the file to edit
3699 * ver : File's last-modified time
3700 * section : Line number of section to edit
3701 * pattern : Line number of pattern to edit
3702 * newval : New value for pattern
3704 * Returns : JB_ERR_OK on success
3705 * JB_ERR_MEMORY on out-of-memory
3706 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3707 * specified or not valid.
3709 *********************************************************************/
3710 jb_err cgi_edit_actions_url(struct client_state *csp,
3711 struct http_response *rsp,
3712 const struct map *parameters)
3716 struct editable_file * file;
3717 struct file_line * cur_line;
3718 unsigned line_number;
3719 unsigned section_start_line_number = 0;
3727 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3729 return cgi_error_disabled(csp, rsp);
3732 err = get_number_param(csp, parameters, "p", &patternid);
3739 return JB_ERR_CGI_PARAMS;
3742 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3748 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3751 /* No filename specified, can't read file, modified, or out of memory. */
3753 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3757 cur_line = file->lines;
3759 while ((cur_line != NULL) && (line_number < patternid))
3761 if (cur_line->type == FILE_LINE_ACTION)
3763 section_start_line_number = line_number;
3765 cur_line = cur_line->next;
3769 if ( (cur_line == NULL)
3770 || (cur_line->type != FILE_LINE_URL))
3772 /* Invalid "patternid" parameter */
3774 edit_free_file(file);
3775 return JB_ERR_CGI_PARAMS;
3778 /* At this point, the line to edit is in cur_line */
3780 freez(cur_line->raw);
3781 freez(cur_line->unprocessed);
3782 cur_line->unprocessed = new_pattern;
3784 err = edit_write_file(file);
3787 /* Error writing file */
3788 if (err == JB_ERR_FILE)
3790 /* Read-only file. */
3791 err = cgi_error_file_read_only(csp, rsp, file->filename);
3793 edit_free_file(file);
3797 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3798 (long) time(NULL), file->identifier, section_start_line_number);
3800 edit_free_file(file);
3802 return cgi_redirect(rsp, target);
3806 /*********************************************************************
3808 * Function : cgi_edit_actions_add_url
3810 * Description : CGI function that actually adds a URL pattern to
3814 * 1 : csp = Current client state (buffers, headers, etc...)
3815 * 2 : rsp = http_response data structure for output
3816 * 3 : parameters = map of cgi parameters
3819 * filename : Identifies the file to edit
3820 * ver : File's last-modified time
3821 * section : Line number of section to edit
3822 * newval : New pattern
3824 * Returns : JB_ERR_OK on success
3825 * JB_ERR_MEMORY on out-of-memory
3826 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3827 * specified or not valid.
3829 *********************************************************************/
3830 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3831 struct http_response *rsp,
3832 const struct map *parameters)
3836 struct file_line * new_line;
3837 struct editable_file * file;
3838 struct file_line * cur_line;
3839 unsigned line_number;
3843 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3845 return cgi_error_disabled(csp, rsp);
3848 err = get_number_param(csp, parameters, "s", §ionid);
3855 return JB_ERR_CGI_PARAMS;
3858 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3864 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3867 /* No filename specified, can't read file, modified, or out of memory. */
3869 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3873 cur_line = file->lines;
3875 while ((cur_line != NULL) && (line_number < sectionid))
3877 cur_line = cur_line->next;
3881 if ( (cur_line == NULL)
3882 || (cur_line->type != FILE_LINE_ACTION))
3884 /* Invalid "sectionid" parameter */
3886 edit_free_file(file);
3887 return JB_ERR_CGI_PARAMS;
3890 /* At this point, the section header is in cur_line - add after this. */
3892 /* Allocate the new line */
3893 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3894 if (new_line == NULL)
3897 edit_free_file(file);
3898 return JB_ERR_MEMORY;
3901 /* Fill in the data members of the new line */
3902 new_line->raw = NULL;
3903 new_line->prefix = NULL;
3904 new_line->unprocessed = new_pattern;
3905 new_line->type = FILE_LINE_URL;
3907 /* Link new_line into the list, after cur_line */
3908 new_line->next = cur_line->next;
3909 cur_line->next = new_line;
3911 /* Done making changes, now commit */
3913 err = edit_write_file(file);
3916 /* Error writing file */
3917 if (err == JB_ERR_FILE)
3919 /* Read-only file. */
3920 err = cgi_error_file_read_only(csp, rsp, file->filename);
3922 edit_free_file(file);
3926 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3927 (long) time(NULL), file->identifier, sectionid);
3929 edit_free_file(file);
3931 return cgi_redirect(rsp, target);
3935 /*********************************************************************
3937 * Function : cgi_edit_actions_remove_url
3939 * Description : CGI function that actually removes a URL pattern from
3943 * 1 : csp = Current client state (buffers, headers, etc...)
3944 * 2 : rsp = http_response data structure for output
3945 * 3 : parameters = map of cgi parameters
3948 * f : (filename) Identifies the file to edit
3949 * v : (version) File's last-modified time
3950 * p : (pattern) Line number of pattern to remove
3952 * Returns : JB_ERR_OK on success
3953 * JB_ERR_MEMORY on out-of-memory
3954 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3955 * specified or not valid.
3957 *********************************************************************/
3958 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3959 struct http_response *rsp,
3960 const struct map *parameters)
3963 struct editable_file * file;
3964 struct file_line * cur_line;
3965 struct file_line * prev_line;
3966 unsigned line_number;
3967 unsigned section_start_line_number = 0;
3971 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3973 return cgi_error_disabled(csp, rsp);
3976 err = get_number_param(csp, parameters, "p", &patternid);
3982 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3985 /* No filename specified, can't read file, modified, or out of memory. */
3986 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3991 cur_line = file->lines;
3993 while ((cur_line != NULL) && (line_number < patternid))
3995 if (cur_line->type == FILE_LINE_ACTION)
3997 section_start_line_number = line_number;
3999 prev_line = cur_line;
4000 cur_line = cur_line->next;
4004 if ( (cur_line == NULL)
4005 || (prev_line == NULL)
4006 || (cur_line->type != FILE_LINE_URL))
4008 /* Invalid "patternid" parameter */
4009 edit_free_file(file);
4010 return JB_ERR_CGI_PARAMS;
4013 /* At this point, the line to remove is in cur_line, and the previous
4014 * one is in prev_line
4017 /* Unlink cur_line */
4018 prev_line->next = cur_line->next;
4019 cur_line->next = NULL;
4022 edit_free_file_lines(cur_line);
4024 err = edit_write_file(file);
4027 /* Error writing file */
4028 if (err == JB_ERR_FILE)
4030 /* Read-only file. */
4031 err = cgi_error_file_read_only(csp, rsp, file->filename);
4033 edit_free_file(file);
4037 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
4038 (long) time(NULL), file->identifier, section_start_line_number);
4040 edit_free_file(file);
4042 return cgi_redirect(rsp, target);
4046 /*********************************************************************
4048 * Function : cgi_edit_actions_section_remove
4050 * Description : CGI function that actually removes a whole section from
4051 * the actions file. The section must be empty first
4052 * (else JB_ERR_CGI_PARAMS).
4055 * 1 : csp = Current client state (buffers, headers, etc...)
4056 * 2 : rsp = http_response data structure for output
4057 * 3 : parameters = map of cgi parameters
4060 * f : (filename) Identifies the file to edit
4061 * v : (version) File's last-modified time
4062 * s : (section) Line number of section to edit
4064 * Returns : JB_ERR_OK on success
4065 * JB_ERR_MEMORY on out-of-memory
4066 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4067 * specified or not valid.
4069 *********************************************************************/
4070 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
4071 struct http_response *rsp,
4072 const struct map *parameters)
4075 struct editable_file * file;
4076 struct file_line * cur_line;
4077 struct file_line * prev_line;
4078 unsigned line_number;
4082 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4084 return cgi_error_disabled(csp, rsp);
4087 err = get_number_param(csp, parameters, "s", §ionid);
4093 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4096 /* No filename specified, can't read file, modified, or out of memory. */
4097 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4101 cur_line = file->lines;
4104 while ((cur_line != NULL) && (line_number < sectionid))
4106 prev_line = cur_line;
4107 cur_line = cur_line->next;
4111 if ( (cur_line == NULL)
4112 || (cur_line->type != FILE_LINE_ACTION) )
4114 /* Invalid "sectionid" parameter */
4115 edit_free_file(file);
4116 return JB_ERR_CGI_PARAMS;
4119 if ( (cur_line->next != NULL)
4120 && (cur_line->next->type == FILE_LINE_URL) )
4122 /* Section not empty. */
4123 edit_free_file(file);
4124 return JB_ERR_CGI_PARAMS;
4127 /* At this point, the line to remove is in cur_line, and the previous
4128 * one is in prev_line
4131 /* Unlink cur_line */
4132 if (prev_line == NULL)
4134 /* Removing the first line from the file */
4135 file->lines = cur_line->next;
4139 prev_line->next = cur_line->next;
4141 cur_line->next = NULL;
4144 edit_free_file_lines(cur_line);
4146 err = edit_write_file(file);
4149 /* Error writing file */
4150 if (err == JB_ERR_FILE)
4152 /* Read-only file. */
4153 err = cgi_error_file_read_only(csp, rsp, file->filename);
4155 edit_free_file(file);
4159 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4160 (long) time(NULL), file->identifier);
4162 edit_free_file(file);
4164 return cgi_redirect(rsp, target);
4168 /*********************************************************************
4170 * Function : cgi_edit_actions_section_add
4172 * Description : CGI function that adds a new empty section to
4176 * 1 : csp = Current client state (buffers, headers, etc...)
4177 * 2 : rsp = http_response data structure for output
4178 * 3 : parameters = map of cgi parameters
4181 * f : (filename) Identifies the file to edit
4182 * v : (version) File's last-modified time
4183 * s : (section) Line number of section to add after, 0 for
4186 * Returns : JB_ERR_OK on success
4187 * JB_ERR_MEMORY on out-of-memory
4188 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4189 * specified or not valid.
4191 *********************************************************************/
4192 jb_err cgi_edit_actions_section_add(struct client_state *csp,
4193 struct http_response *rsp,
4194 const struct map *parameters)
4197 struct file_line * new_line;
4199 struct editable_file * file;
4200 struct file_line * cur_line;
4201 unsigned line_number;
4205 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4207 return cgi_error_disabled(csp, rsp);
4210 err = get_number_param(csp, parameters, "s", §ionid);
4216 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4219 /* No filename specified, can't read file, modified, or out of memory. */
4220 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4224 cur_line = file->lines;
4226 if (sectionid <= 1U)
4228 /* Add to start of file */
4229 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4231 /* There's something in the file, find the line before the first
4234 while ( (cur_line->next != NULL)
4235 && (cur_line->next->type != FILE_LINE_ACTION) )
4237 cur_line = cur_line->next;
4243 /* File starts with action line, so insert at top */
4249 /* Add after stated section. */
4250 while ((cur_line != NULL) && (line_number < sectionid))
4252 cur_line = cur_line->next;
4256 if ( (cur_line == NULL)
4257 || (cur_line->type != FILE_LINE_ACTION))
4259 /* Invalid "sectionid" parameter */
4260 edit_free_file(file);
4261 return JB_ERR_CGI_PARAMS;
4264 /* Skip through the section to find the last line in it. */
4265 while ( (cur_line->next != NULL)
4266 && (cur_line->next->type != FILE_LINE_ACTION) )
4268 cur_line = cur_line->next;
4273 /* At this point, the last line in the previous section is in cur_line
4274 * - add after this. (Or if we need to add as the first line, cur_line
4278 new_text = strdup("{}");
4279 if (NULL == new_text)
4281 edit_free_file(file);
4282 return JB_ERR_MEMORY;
4285 /* Allocate the new line */
4286 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4287 if (new_line == NULL)
4290 edit_free_file(file);
4291 return JB_ERR_MEMORY;
4294 /* Fill in the data members of the new line */
4295 new_line->raw = NULL;
4296 new_line->prefix = NULL;
4297 new_line->unprocessed = new_text;
4298 new_line->type = FILE_LINE_ACTION;
4300 if (cur_line != NULL)
4302 /* Link new_line into the list, after cur_line */
4303 new_line->next = cur_line->next;
4304 cur_line->next = new_line;
4308 /* Link new_line into the list, as first line */
4309 new_line->next = file->lines;
4310 file->lines = new_line;
4313 /* Done making changes, now commit */
4315 err = edit_write_file(file);
4318 /* Error writing file */
4319 if (err == JB_ERR_FILE)
4321 /* Read-only file. */
4322 err = cgi_error_file_read_only(csp, rsp, file->filename);
4324 edit_free_file(file);
4328 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4329 (long) time(NULL), file->identifier);
4331 edit_free_file(file);
4333 return cgi_redirect(rsp, target);
4337 /*********************************************************************
4339 * Function : cgi_edit_actions_section_swap
4341 * Description : CGI function that swaps the order of two sections
4342 * in the actions file. Note that this CGI can actually
4343 * swap any two arbitrary sections, but the GUI interface
4344 * currently only allows consecutive sections to be
4348 * 1 : csp = Current client state (buffers, headers, etc...)
4349 * 2 : rsp = http_response data structure for output
4350 * 3 : parameters = map of cgi parameters
4353 * f : (filename) Identifies the file to edit
4354 * v : (version) File's last-modified time
4355 * s1 : (section1) Line number of first section to swap
4356 * s2 : (section2) Line number of second section to swap
4358 * Returns : JB_ERR_OK on success
4359 * JB_ERR_MEMORY on out-of-memory
4360 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4361 * specified or not valid.
4363 *********************************************************************/
4364 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4365 struct http_response *rsp,
4366 const struct map *parameters)
4370 struct editable_file * file;
4371 struct file_line * cur_line;
4372 struct file_line * prev_line;
4373 struct file_line * line_before_section1;
4374 struct file_line * line_start_section1;
4375 struct file_line * line_end_section1;
4376 struct file_line * line_after_section1;
4377 struct file_line * line_before_section2;
4378 struct file_line * line_start_section2;
4379 struct file_line * line_end_section2;
4380 struct file_line * line_after_section2;
4381 unsigned line_number;
4385 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4387 return cgi_error_disabled(csp, rsp);
4390 err = get_number_param(csp, parameters, "s1", §ion1);
4391 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4397 if (section1 > section2)
4399 unsigned temp = section2;
4400 section2 = section1;
4404 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4407 /* No filename specified, can't read file, modified, or out of memory. */
4408 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4411 /* Start at the beginning... */
4413 cur_line = file->lines;
4416 /* ... find section1 ... */
4417 while ((cur_line != NULL) && (line_number < section1))
4419 prev_line = cur_line;
4420 cur_line = cur_line->next;
4424 if ( (cur_line == NULL)
4425 || (cur_line->type != FILE_LINE_ACTION) )
4427 /* Invalid "section1" parameter */
4428 edit_free_file(file);
4429 return JB_ERR_CGI_PARAMS;
4432 /* If no-op, we've validated params and can skip the rest. */
4433 if (section1 != section2)
4435 /* ... find the end of section1 ... */
4436 line_before_section1 = prev_line;
4437 line_start_section1 = cur_line;
4440 prev_line = cur_line;
4441 cur_line = cur_line->next;
4444 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4445 line_end_section1 = prev_line;
4446 line_after_section1 = cur_line;
4448 /* ... find section2 ... */
4449 while ((cur_line != NULL) && (line_number < section2))
4451 prev_line = cur_line;
4452 cur_line = cur_line->next;
4456 if ( (cur_line == NULL)
4457 || (cur_line->type != FILE_LINE_ACTION) )
4459 /* Invalid "section2" parameter */
4460 edit_free_file(file);
4461 return JB_ERR_CGI_PARAMS;
4464 /* ... find the end of section2 ... */
4465 line_before_section2 = prev_line;
4466 line_start_section2 = cur_line;
4469 prev_line = cur_line;
4470 cur_line = cur_line->next;
4473 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4474 line_end_section2 = prev_line;
4475 line_after_section2 = cur_line;
4477 /* Now have all the pointers we need. Do the swap. */
4479 /* Change the pointer to section1 to point to section2 instead */
4480 if (line_before_section1 == NULL)
4482 file->lines = line_start_section2;
4486 line_before_section1->next = line_start_section2;
4489 if (line_before_section2 == line_end_section1)
4491 /* Consecutive sections */
4492 line_end_section2->next = line_start_section1;
4496 line_end_section2->next = line_after_section1;
4497 line_before_section2->next = line_start_section1;
4500 /* Set the pointer from the end of section1 to the rest of the file */
4501 line_end_section1->next = line_after_section2;
4503 err = edit_write_file(file);
4506 /* Error writing file */
4507 if (err == JB_ERR_FILE)
4509 /* Read-only file. */
4510 err = cgi_error_file_read_only(csp, rsp, file->filename);
4512 edit_free_file(file);
4515 } /* END if (section1 != section2) */
4517 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4518 (long) time(NULL), file->identifier);
4520 edit_free_file(file);
4522 return cgi_redirect(rsp, target);
4526 /*********************************************************************
4528 * Function : javascriptify
4530 * Description : Converts a string into a form JavaScript will like.
4532 * Netscape 4's JavaScript sucks - it doesn't use
4533 * "id" parameters, so you have to set the "name"
4534 * used to submit a form element to something JavaScript
4535 * will like. (Or access the elements by index in an
4536 * array. That array contains >60 elements and will
4537 * be changed whenever we add a new action to the
4538 * editor, so I'm NOT going to use indexes that have
4539 * to be figured out by hand.)
4541 * Currently the only thing we have to worry about
4542 * is "-" ==> "_" conversion.
4544 * This is a length-preserving operation so it is
4545 * carried out in-place, no memory is allocated
4549 * 1 : identifier = String to make JavaScript-friendly.
4553 *********************************************************************/
4554 static void javascriptify(char * identifier)
4556 char * p = identifier;
4557 while (NULL != (p = strchr(p, '-')))
4564 /*********************************************************************
4566 * Function : actions_to_radio
4568 * Description : Converts a actionsfile entry into settings for
4569 * radio buttons and edit boxes on a HTML form.
4572 * 1 : exports = List of substitutions to add to.
4573 * 2 : action = Action to read
4575 * Returns : JB_ERR_OK on success
4576 * JB_ERR_MEMORY on out-of-memory
4578 *********************************************************************/
4579 static jb_err actions_to_radio(struct map * exports,
4580 const struct action_spec *action)
4582 unsigned mask = action->mask;
4583 unsigned add = action->add;
4591 mask = action->mask;
4594 /* sanity - prevents "-feature +feature" */
4598 #define DEFINE_ACTION_BOOL(name, bit) \
4599 if (!(mask & bit)) \
4601 current_mode = 'n'; \
4603 else if (add & bit) \
4605 current_mode = 'y'; \
4609 current_mode = 'x'; \
4611 if (map_radio(exports, name, "ynx", current_mode)) \
4613 return JB_ERR_MEMORY; \
4616 #define DEFINE_ACTION_STRING(name, bit, index) \
4617 DEFINE_ACTION_BOOL(name, bit); \
4620 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4623 checked = !strcmp(action->string[index], value); \
4627 checked = is_default; \
4629 mapped_param |= checked; \
4630 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4632 return JB_ERR_MEMORY; \
4635 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4636 if (map(exports, name "-param-custom", 1, \
4637 ((!mapped_param) ? "checked" : ""), 1)) \
4639 return JB_ERR_MEMORY; \
4641 if (map(exports, name "-param", 1, \
4642 (((add & bit) && !mapped_param) ? \
4643 action->string[index] : default_val), 1)) \
4645 return JB_ERR_MEMORY; \
4648 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4649 if (map(exports, name "-param", 1, \
4650 ((add & bit) ? action->string[index] : default_val), 1)) \
4652 return JB_ERR_MEMORY; \
4655 #define DEFINE_ACTION_MULTI(name, index) \
4656 if (action->multi_add[index]->first) \
4658 current_mode = 'y'; \
4660 else if (action->multi_remove_all[index]) \
4662 current_mode = 'n'; \
4664 else if (action->multi_remove[index]->first) \
4666 current_mode = 'y'; \
4670 current_mode = 'x'; \
4672 if (map_radio(exports, name, "ynx", current_mode)) \
4674 return JB_ERR_MEMORY; \
4677 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4679 #include "actionlist.h"
4681 #undef DEFINE_ACTION_MULTI
4682 #undef DEFINE_ACTION_STRING
4683 #undef DEFINE_ACTION_BOOL
4684 #undef DEFINE_ACTION_ALIAS
4685 #undef DEFINE_CGI_PARAM_CUSTOM
4686 #undef DEFINE_CGI_PARAM_RADIO
4687 #undef DEFINE_CGI_PARAM_NO_RADIO
4693 /*********************************************************************
4695 * Function : actions_from_radio
4697 * Description : Converts a map of parameters passed to a CGI function
4698 * into an actionsfile entry.
4701 * 1 : parameters = parameters to the CGI call
4702 * 2 : action = Action to change. Must be valid before
4703 * the call, actions not specified will be
4706 * Returns : JB_ERR_OK on success
4707 * JB_ERR_MEMORY on out-of-memory
4709 *********************************************************************/
4710 static jb_err actions_from_radio(const struct map * parameters,
4711 struct action_spec *action)
4716 const char * js_name;
4717 jb_err err = JB_ERR_OK;
4722 /* Statics are generally a potential race condition,
4723 * but in this case we're safe and don't need semaphores.
4724 * Be careful if you modify this function.
4726 * The js_name_arr's are never free()d, but this is no
4727 * problem, since they will only be created once and
4728 * used by all threads thereafter. -oes
4731 #define JAVASCRIPTIFY(dest_var, string) \
4733 static int first_time = 1; \
4734 static char *js_name_arr; \
4737 js_name_arr = strdup(string); \
4738 javascriptify(js_name_arr); \
4740 dest_var = js_name_arr; \
4744 #define DEFINE_ACTION_BOOL(name, bit) \
4745 JAVASCRIPTIFY(js_name, name); \
4746 ch = get_char_param(parameters, js_name); \
4749 action->add |= bit; \
4750 action->mask |= bit; \
4752 else if (ch == 'N') \
4754 action->add &= ~bit; \
4755 action->mask &= ~bit; \
4757 else if (ch == 'X') \
4759 action->add &= ~bit; \
4760 action->mask |= bit; \
4763 #define DEFINE_ACTION_STRING(name, bit, index) \
4764 JAVASCRIPTIFY(js_name, name); \
4765 ch = get_char_param(parameters, js_name); \
4769 JAVASCRIPTIFY(js_name, name "-mode"); \
4770 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4771 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4773 JAVASCRIPTIFY(js_name, name "-param"); \
4774 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4776 if (param != NULL) \
4778 if (NULL == (param_dup = strdup(param))) \
4780 return JB_ERR_MEMORY; \
4782 freez(action->string[index]); \
4783 action->add |= bit; \
4784 action->mask |= bit; \
4785 action->string[index] = param_dup; \
4788 else if (ch == 'N') \
4790 if (action->add & bit) \
4792 freez(action->string[index]); \
4794 action->add &= ~bit; \
4795 action->mask &= ~bit; \
4797 else if (ch == 'X') \
4799 if (action->add & bit) \
4801 freez(action->string[index]); \
4803 action->add &= ~bit; \
4804 action->mask |= bit; \
4807 #define DEFINE_ACTION_MULTI(name, index) \
4808 JAVASCRIPTIFY(js_name, name); \
4809 ch = get_char_param(parameters, js_name); \
4814 else if (ch == 'N') \
4816 list_remove_all(action->multi_add[index]); \
4817 list_remove_all(action->multi_remove[index]); \
4818 action->multi_remove_all[index] = 1; \
4820 else if (ch == 'X') \
4822 list_remove_all(action->multi_add[index]); \
4823 list_remove_all(action->multi_remove[index]); \
4824 action->multi_remove_all[index] = 0; \
4827 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4829 #include "actionlist.h"
4831 #undef DEFINE_ACTION_MULTI
4832 #undef DEFINE_ACTION_STRING
4833 #undef DEFINE_ACTION_BOOL
4834 #undef DEFINE_ACTION_ALIAS
4835 #undef JAVASCRIPTIFY
4839 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4842 #ifdef FEATURE_TOGGLE
4843 /*********************************************************************
4845 * Function : cgi_toggle
4847 * Description : CGI function that adds a new empty section to
4851 * 1 : csp = Current client state (buffers, headers, etc...)
4852 * 2 : rsp = http_response data structure for output
4853 * 3 : parameters = map of cgi parameters
4856 * set : If present, how to change toggle setting:
4857 * "enable", "disable", "toggle", or none (default).
4858 * mini : If present, use mini reply template.
4860 * Returns : JB_ERR_OK on success
4861 * JB_ERR_MEMORY on out-of-memory
4863 *********************************************************************/
4864 jb_err cgi_toggle(struct client_state *csp,
4865 struct http_response *rsp,
4866 const struct map *parameters)
4868 struct map *exports;
4870 const char *template_name;
4876 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4878 return cgi_error_disabled(csp, rsp);
4881 mode = get_char_param(parameters, "set");
4886 global_toggle_state = 1;
4888 else if (mode == 'D')
4891 global_toggle_state = 0;
4893 else if (mode == 'T')
4896 global_toggle_state = !global_toggle_state;
4899 if (NULL == (exports = default_exports(csp, "toggle")))
4901 return JB_ERR_MEMORY;
4904 template_name = (get_char_param(parameters, "mini")
4908 return template_fill_for_cgi(csp, template_name, exports, rsp);
4910 #endif /* def FEATURE_TOGGLE */