1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.64 2009/03/01 18:43:09 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.64 2009/03/01 18:43:09 fabiankeil
46 * Fix cparser warnings.
48 * Revision 1.63 2008/12/04 18:15:38 fabiankeil
49 * Fix some cparser warnings.
51 * Revision 1.62 2008/08/31 15:59:02 fabiankeil
52 * There's no reason to let remote toggling support depend
53 * on FEATURE_CGI_EDIT_ACTIONS, so make sure it doesn't.
55 * Revision 1.61 2008/03/24 18:12:52 fabiankeil
56 * Use sizeof() more often.
58 * Revision 1.60 2008/03/15 14:52:35 fabiankeil
59 * Add CGI editor support for the "disable all filters of this type"
60 * directives "-client-header-filter", "-server-header-filter",
61 * "-client-header-tagger" and "-server-header-tagger".
63 * Revision 1.59 2008/03/08 16:25:56 fabiankeil
64 * After three file modification time mismatches, turn the CGI editor off.
66 * Revision 1.58 2007/11/28 17:57:01 fabiankeil
67 * Fix double free in cgi_edit_actions_list().
68 * Reported by adlab in BR#1840145.
70 * Revision 1.57 2007/10/27 13:32:23 fabiankeil
71 * Plug minor 5-year-old memory leak. Spotted by
72 * Valgrind and triggered by Privoxy-Regression-Test.
74 * Revision 1.56 2007/08/05 13:47:03 fabiankeil
75 * #1763173 from Stefan Huehner: s@const static@static const@.
77 * Revision 1.55 2007/05/31 11:50:20 fabiankeil
78 * Re-enable support for old-school URLs like
79 * http://config.privoxy.org/edit-actions-list?f=default
80 * in the action editor.
82 * They are no longer used by the CGI pages, but make it easier
83 * to reach the editor directly, without knowing the requested
84 * file's index in csp->config->actions_file[].
86 * Revision 1.54 2007/05/14 10:33:51 fabiankeil
87 * - Use strlcpy() and strlcat() instead of strcpy() and strcat().
89 * Revision 1.53 2007/04/15 16:39:20 fabiankeil
90 * Introduce tags as alternative way to specify which
91 * actions apply to a request. At the moment tags can be
92 * created based on client and server headers.
94 * Revision 1.52 2007/04/12 10:41:23 fabiankeil
95 * - Don't mistake VC++'s _snprintf() for a snprintf() replacement.
96 * - Move some cgi_edit_actions_for_url() variables into structs.
97 * - Remove bogus comment.
99 * Revision 1.51 2007/04/08 13:21:05 fabiankeil
100 * Reference action files in CGI URLs by id instead
101 * of using the first part of the file name.
102 * Fixes BR 1694250 and BR 1590556.
104 * Revision 1.50 2007/03/29 11:40:34 fabiankeil
105 * Divide @filter-params@ into @client-header-filter-params@
106 * @content-filter-params@ and @server-header-filter-params@.
108 * Revision 1.49 2007/03/20 15:16:34 fabiankeil
109 * Use dedicated header filter actions instead of abusing "filter".
110 * Replace "filter-client-headers" and "filter-client-headers"
111 * with "server-header-filter" and "client-header-filter".
113 * Revision 1.48 2007/02/13 14:35:25 fabiankeil
114 * Replace hash escaping code to prevent
115 * crashes, memory and file corruption.
117 * Revision 1.47 2006/12/28 18:04:25 fabiankeil
118 * Fixed gcc43 conversion warnings.
120 * Revision 1.46 2006/12/27 18:44:52 fabiankeil
121 * Stop shadowing string.h's index().
123 * Revision 1.45 2006/12/21 12:57:48 fabiankeil
124 * Add config option "split-large-forms"
125 * to work around the browser bug reported
128 * Revision 1.44 2006/12/09 13:49:16 fabiankeil
129 * Fix configure option --disable-toggle.
130 * Thanks to Peter Thoenen for reporting this.
132 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
133 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
134 * with what was really the latest development (the v_3_0_branch branch)
136 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
137 * Remove a little residual debugging info
139 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
140 * Multiple filter file support
142 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
143 * Undo some damaging memory leak patches
145 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
146 * Removing a double free
148 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
149 * A few memory leaks plugged (mostly on error paths)
151 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
152 * Moved cgi_error_disabled() from cgiedit.c to
153 * cgi.c to re-enable build with --disable-editor.
154 * Fixes Bug #892744. Thanks to Matthew Fischer
157 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
158 * One line lost in last commit
160 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
161 * - All edit functions that redirect back to the list page
162 * now use cgi_redirect
163 * - All redirects now contain useless parameter "foo", whose
164 * value are raw seconds since epoch, in order to force
165 * Opera and Konqueror to properly reload the list. Closes
168 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
169 * Cosmetic: Renamed cryptic variable
171 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
172 * Fix: Don't free uninitialized struct editable_file
174 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
175 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
177 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
178 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
180 * Revision 1.41 2002/05/21 19:09:45 oes
181 * - Made Add/Edit/Remove URL Submit and Cancel
182 * buttons jump back to relevant section in eal
183 * - Bugfix: remove-url-form needs p export
185 * Revision 1.40 2002/05/19 11:34:35 jongfoster
186 * Handling read-only actions files better - report the actual
187 * error, not "Out of memory"!
190 * http://sourceforge.net/tracker/index.php?func=detail
191 * &aid=557905&group_id=11118&atid=111118
193 * Revision 1.39 2002/05/12 21:39:15 jongfoster
194 * - Adding Doxygen-style comments to structures and #defines.
195 * - Correcting function comments
197 * Revision 1.38 2002/05/03 23:00:38 jongfoster
198 * Support for templates for "standard actions" buttons.
201 * Revision 1.37 2002/04/30 11:14:52 oes
202 * Made csp the first parameter in *action_to_html
204 * Revision 1.36 2002/04/26 21:53:30 jongfoster
205 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
207 * Revision 1.35 2002/04/26 21:50:02 jongfoster
208 * Honouring default exports in edit-actions-for-url-filter template.
210 * Revision 1.34 2002/04/26 12:54:17 oes
211 * Adaptions to changes in actions.c
213 * Revision 1.33 2002/04/24 02:17:47 oes
214 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
216 * - Activated Jon's code for editing multiple AFs
217 * - cgi_edit_list_actions now provides context-sensitive
218 * help, looks up all action sets from standard.action and
219 * makes buttons for them in the catchall section
220 * - cgi_edit_action_submit now honors a p parameter, looks up
221 * the corresponding action set, and sets the catchall pattern's
222 * actions accordingly.
224 * Revision 1.32 2002/04/19 16:55:31 jongfoster
225 * Fixing newline problems. If we do our own text file newline
226 * mangling, we don't want the library to do any, so we need to
227 * open the files in *binary* mode.
229 * Revision 1.31 2002/04/18 19:21:08 jongfoster
230 * Added code to detect "conventional" action files, that start
231 * with a set of actions for all URLs (the pattern "/").
232 * These are special-cased in the "edit-actions-list" CGI, so
233 * that a special UI can be written for them.
235 * Revision 1.30 2002/04/10 13:38:35 oes
236 * load_template signature changed
238 * Revision 1.29 2002/04/08 16:59:08 oes
241 * Revision 1.28 2002/03/27 12:30:29 oes
242 * Deleted unsused variable
244 * Revision 1.27 2002/03/26 23:06:04 jongfoster
245 * Removing duplicate @ifs on the toggle page
247 * Revision 1.26 2002/03/26 22:59:17 jongfoster
248 * Fixing /toggle to display status consistently.
250 * Revision 1.25 2002/03/26 22:29:54 swa
251 * we have a new homepage!
253 * Revision 1.24 2002/03/24 15:23:33 jongfoster
256 * Revision 1.23 2002/03/24 13:32:41 swa
257 * name change related issues
259 * Revision 1.22 2002/03/24 13:25:43 swa
260 * name change related issues
262 * Revision 1.21 2002/03/22 18:02:48 jongfoster
263 * Fixing remote toggle
265 * Revision 1.20 2002/03/16 20:28:34 oes
266 * Added descriptions to the filters so users will know what they select in the cgi editor
268 * Revision 1.19 2002/03/16 18:38:14 jongfoster
269 * Stopping stupid or malicious users from breaking the actions
270 * file using the web-based editor.
272 * Revision 1.18 2002/03/16 14:57:44 jongfoster
273 * Full support for enabling/disabling modular filters.
275 * Revision 1.17 2002/03/16 14:26:42 jongfoster
276 * First version of modular filters support - READ ONLY!
277 * Fixing a double-free bug in the out-of-memory handling in map_radio().
279 * Revision 1.16 2002/03/07 03:46:17 oes
280 * Fixed compiler warnings
282 * Revision 1.15 2002/03/06 22:54:35 jongfoster
283 * Automated function-comment nitpicking.
285 * Revision 1.14 2002/03/05 00:24:51 jongfoster
286 * Patch to always edit the current actions file.
288 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
289 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
291 * Revision 1.12 2002/03/03 09:18:03 joergs
292 * Made jumbjuster work on AmigaOS again.
294 * Revision 1.11 2002/01/23 01:03:31 jongfoster
295 * Fixing gcc [CygWin] compiler warnings
297 * Revision 1.10 2002/01/23 00:22:59 jongfoster
298 * Adding new function cgi_edit_actions_section_swap(), to reorder
301 * Adding get_url_spec_param() to get a validated URL pattern.
303 * Moving edit_read_line() out of this file and into loaders.c.
305 * Adding missing html_encode() to many CGI functions.
307 * Moving the functions that #include actionlist.h to the end of the file,
308 * because the Visual C++ 97 debugger gets extremely confused if you try
309 * to debug any code that comes after them in the file.
311 * Major optimizations in cgi_edit_actions_list() to reduce the size of
312 * the generated HTML (down 40% from 550k to 304k), with major side-effects
313 * throughout the editor and templates. In particular, the length of the
314 * URLs throughout the editor has been drastically reduced, by cutting
315 * paramater names down to 1 character and CGI names down to 3-4
316 * characters, by removing all non-essential CGI paramaters even at the
317 * expense of having to re-read the actions file for the most trivial
318 * page, and by using relative rather than absolute URLs. This means
319 * that this (typical example):
321 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
322 * filename=ijb&ver=1011487572&section=12&pattern=13
323 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
327 * <a href="eau?f=ijb&v=1011487572&p=13">
329 * Revision 1.9 2002/01/17 20:56:22 jongfoster
330 * Replacing hard references to the URL of the config interface
331 * with #defines from project.h
333 * Revision 1.8 2001/11/30 23:35:51 jongfoster
334 * Renaming actionsfile to ijb.action
336 * Revision 1.7 2001/11/13 00:28:24 jongfoster
337 * - Renaming parameters from edit-actions-for-url so that they only
338 * contain legal JavaScript characters. If we wanted to write
339 * JavaScript that worked with Netscape 4, this is nessacery.
340 * (Note that at the moment the JavaScript doesn't actually work
341 * with Netscape 4, but now this is purely a template issue, not
342 * one affecting code).
343 * - Adding new CGIs for use by non-JavaScript browsers:
344 * edit-actions-url-form
345 * edit-actions-add-url-form
346 * edit-actions-remove-url-form
349 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
350 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
351 * by and __OS2__ ifdef.
353 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
354 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
355 * threads to call select() simultaneously. So, it's time to do a real, live,
356 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
357 * (native). Both versions will work, but using __OS2__ offers multi-threading.
359 * Revision 1.4 2001/10/23 21:48:19 jongfoster
360 * Cleaning up error handling in CGI functions - they now send back
361 * a HTML error page and should never cause a FATAL error. (Fixes one
362 * potential source of "denial of service" attacks).
364 * CGI actions file editor that works and is actually useful.
366 * Ability to toggle JunkBuster remotely using a CGI call.
368 * You can turn off both the above features in the main configuration
369 * file, e.g. if you are running a multi-user proxy.
371 * Revision 1.3 2001/10/14 22:12:49 jongfoster
372 * New version of CGI-based actionsfile editor.
373 * Major changes, including:
374 * - Completely new file parser and file output routines
375 * - edit-actions CGI renamed edit-actions-for-url
376 * - All CGIs now need a filename parameter, except for...
377 * - New CGI edit-actions which doesn't need a filename,
378 * to allow you to start the editor up.
379 * - edit-actions-submit now works, and now automatically
380 * redirects you back to the main edit-actions-list handler.
382 * Revision 1.2 2001/09/16 17:05:14 jongfoster
383 * Removing unused #include showarg.h
385 * Revision 1.1 2001/09/16 15:47:37 jongfoster
386 * First version of CGI-based edit interface. This is very much a
387 * work-in-progress, and you can't actually use it to edit anything
388 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
389 * to have any effect.
392 **********************************************************************/
398 * FIXME: Following includes copied from cgi.c - which are actually needed?
403 #include <sys/types.h>
407 #include <sys/stat.h>
412 #include "cgisimple.h"
416 #include "miscutil.h"
419 #ifdef FEATURE_TOGGLE
420 /* loadcfg.h is for global_toggle_state only */
422 #endif /* def FEATURE_TOGGLE */
423 #include "urlmatch.h"
425 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
428 #ifdef FEATURE_CGI_EDIT_ACTIONS
431 * A line in an editable_file.
435 /** Next entry in the linked list */
436 struct file_line * next;
438 /** The raw data, to write out if this line is unmodified. */
441 /** Comments and/or whitespace to put before this line if it's modified
442 and then written out. */
445 /** The actual data, as a string. Line continuation and comment removal
446 are performed on the data read from file before it's stored here, so
447 it will be a single line of data. */
450 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
453 /** The actual data, processed into some sensible data type. */
457 /** An action specification. */
458 struct action_spec action[1];
460 /** A name=value pair. */
464 /** The name in the name=value pair. */
467 /** The value in the name=value pair, as a string. */
470 /** The value in the name=value pair, as an integer. */
475 /* Add more data types here... e.g.
478 struct url_spec url[1];
482 struct action_spec action[1];
492 /** This file_line has not been processed yet. */
493 #define FILE_LINE_UNPROCESSED 1
495 /** This file_line is blank. Can only appear at the end of a file, due to
496 the way the parser works. */
497 #define FILE_LINE_BLANK 2
499 /** This file_line says {{alias}}. */
500 #define FILE_LINE_ALIAS_HEADER 3
502 /** This file_line defines an alias. */
503 #define FILE_LINE_ALIAS_ENTRY 4
505 /** This file_line defines an {action}. */
506 #define FILE_LINE_ACTION 5
508 /** This file_line specifies a URL pattern. */
509 #define FILE_LINE_URL 6
511 /** This file_line says {{settings}}. */
512 #define FILE_LINE_SETTINGS_HEADER 7
514 /** This file_line is in a {{settings}} block. */
515 #define FILE_LINE_SETTINGS_ENTRY 8
517 /** This file_line says {{description}}. */
518 #define FILE_LINE_DESCRIPTION_HEADER 9
520 /** This file_line is in a {{description}} block. */
521 #define FILE_LINE_DESCRIPTION_ENTRY 10
524 * Number of file modification time mismatches
525 * before the CGI editor gets turned off.
527 #define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
530 * A configuration file, in a format that can be edited and written back to
535 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
536 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
537 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
538 const char * version_str; /**< Last modification time, as a string. For CGI param. */
539 /**< Can be used in URL without using url_param(). */
540 unsigned version; /**< Last modification time - prevents chaos with
541 the browser's "back" button. Note that this is a
542 time_t cast to an unsigned. When comparing, always
543 cast the time_t to an unsigned, and *NOT* vice-versa.
544 This may lose the top few bits, but they're not
545 significant anyway. */
546 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
547 Note that changing this after the file has been
548 read in will cause a mess. */
549 struct file_line * parse_error; /**< On parse error, this is the offending line. */
550 const char * parse_error_text; /**< On parse error, this is the problem.
551 (Statically allocated) */
555 * Information about the filter types.
556 * Used for macro replacement in cgi_edit_actions_for_url.
558 struct filter_type_info
560 const int multi_action_index; /**< The multi action index as defined in project.h */
561 const char *macro_name; /**< Name of the macro that has to be replaced
562 with the prepared templates.
563 For example "content-filter-params" */
564 const char *type; /**< Name of the filter type,
565 for example "server-header-filter". */
566 /* XXX: check if these two can be combined. */
567 const char *disable_all_option; /**< Name of the catch-all radio option that has
568 to be checked or unchecked for this filter type. */
569 const char *disable_all_param; /**< Name of the parameter that causes all filters of
570 this type to be disabled. */
571 const char *abbr_type; /**< Abbreviation of the filter type, usually the
572 first or second character capitalized */
573 const char *anchor; /**< Anchor for the User Manual link,
574 for example "SERVER-HEADER-FILTER" */
577 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
578 static const struct filter_type_info filter_type_info[] =
582 "content-filter-params", "filter",
583 "filter-all", "filter_all",
587 ACTION_MULTI_CLIENT_HEADER_FILTER,
588 "client-header-filter-params", "client-header-filter",
589 "client-header-filter-all", "client_header_filter_all",
590 "C", "CLIENT-HEADER-FILTER"
593 ACTION_MULTI_SERVER_HEADER_FILTER,
594 "server-header-filter-params", "server-header-filter",
595 "server-header-filter-all", "server_header_filter_all",
596 "S", "SERVER-HEADER-FILTER"
599 ACTION_MULTI_CLIENT_HEADER_TAGGER,
600 "client-header-tagger-params", "client-header-tagger",
601 "client-header-tagger-all", "client_header_tagger_all",
602 "L", "CLIENT-HEADER-TAGGER"
605 ACTION_MULTI_SERVER_HEADER_TAGGER,
606 "server-header-tagger-params", "server-header-tagger",
607 "server-header-tagger-all", "server_header_tagger_all",
608 "E", "SERVER-HEADER-TAGGER"
612 /* FIXME: Following non-static functions should be prototyped in .h or made static */
614 /* Functions to read and write arbitrary config files */
615 jb_err edit_read_file(struct client_state *csp,
616 const struct map *parameters,
618 struct editable_file **pfile);
619 jb_err edit_write_file(struct editable_file * file);
620 void edit_free_file(struct editable_file * file);
622 /* Functions to read and write actions files */
623 jb_err edit_parse_actions_file(struct editable_file * file);
624 jb_err edit_read_actions_file(struct client_state *csp,
625 struct http_response *rsp,
626 const struct map *parameters,
628 struct editable_file **pfile);
631 jb_err cgi_error_modified(struct client_state *csp,
632 struct http_response *rsp,
633 const char *filename);
634 jb_err cgi_error_parse(struct client_state *csp,
635 struct http_response *rsp,
636 struct editable_file *file);
637 jb_err cgi_error_file(struct client_state *csp,
638 struct http_response *rsp,
639 const char *filename);
640 jb_err cgi_error_file_read_only(struct client_state *csp,
641 struct http_response *rsp,
642 const char *filename);
644 /* Internal arbitrary config file support functions */
645 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
646 static void edit_free_file_lines(struct file_line * first_line);
648 /* Internal actions file support functions */
649 static int match_actions_file_header_line(const char * line, const char * name);
650 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
652 /* Internal parameter parsing functions */
653 static jb_err get_url_spec_param(struct client_state *csp,
654 const struct map *parameters,
659 /* Internal actionsfile <==> HTML conversion functions */
660 static jb_err map_radio(struct map * exports,
661 const char * optionname,
664 static jb_err actions_to_radio(struct map * exports,
665 const struct action_spec *action);
666 static jb_err actions_from_radio(const struct map * parameters,
667 struct action_spec *action);
670 static jb_err map_copy_parameter_html(struct map *out,
671 const struct map *in,
673 #if 0 /* unused function */
674 static jb_err map_copy_parameter_url(struct map *out,
675 const struct map *in,
677 #endif /* unused function */
679 static jb_err get_file_name_param(struct client_state *csp,
680 const struct map *parameters,
681 const char *param_name,
682 const char **pfilename);
684 /* Internal convenience functions */
685 static char *section_target(const unsigned sectionid);
687 /*********************************************************************
689 * Function : section_target
691 * Description : Given an unsigned (section id) n, produce a dynamically
692 * allocated string of the form #l<n>, for use in link
695 * XXX: The hash should be moved into the templates
696 * to make this function more generic and render
697 * stringify() obsolete.
700 * 1 : sectionid = start line number of section
702 * Returns : String with link target, or NULL if out of
705 *********************************************************************/
706 static char *section_target(const unsigned sectionid)
710 snprintf(buf, sizeof(buf), "#l%d", sectionid);
716 /*********************************************************************
718 * Function : stringify
720 * Description : Convert a number into a dynamically allocated string.
723 * 1 : number = The number to convert.
725 * Returns : String with link target, or NULL if out of memory
727 *********************************************************************/
728 static char *stringify(const unsigned number)
732 snprintf(buf, sizeof(buf), "%i", number);
737 /*********************************************************************
739 * Function : map_copy_parameter_html
741 * Description : Copy a CGI parameter from one map to another, HTML
745 * 1 : out = target map
746 * 2 : in = source map
747 * 3 : name = name of cgi parameter to copy
749 * Returns : JB_ERR_OK on success
750 * JB_ERR_MEMORY on out-of-memory
751 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
754 *********************************************************************/
755 static jb_err map_copy_parameter_html(struct map *out,
756 const struct map *in,
766 value = lookup(in, name);
767 err = map(out, name, 1, html_encode(value), 0);
774 else if (*value == '\0')
776 return JB_ERR_CGI_PARAMS;
785 #if 0 /* unused function */
786 /*********************************************************************
788 * Function : map_copy_parameter_url
790 * Description : Copy a CGI parameter from one map to another, URL
794 * 1 : out = target map
795 * 2 : in = source map
796 * 3 : name = name of cgi parameter to copy
798 * Returns : JB_ERR_OK on success
799 * JB_ERR_MEMORY on out-of-memory
800 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
803 *********************************************************************/
804 static jb_err map_copy_parameter_url(struct map *out,
805 const struct map *in,
815 value = lookup(in, name);
816 err = map(out, name, 1, url_encode(value), 0);
823 else if (*value == '\0')
825 return JB_ERR_CGI_PARAMS;
832 #endif /* 0 - unused function */
835 /*********************************************************************
837 * Function : cgi_edit_actions_url_form
839 * Description : CGI function that displays a form for
843 * 1 : csp = Current client state (buffers, headers, etc...)
844 * 2 : rsp = http_response data structure for output
845 * 3 : parameters = map of cgi parameters
848 * i : (action index) Identifies the file to edit
849 * v : (version) File's last-modified time
850 * p : (pattern) Line number of pattern to edit
852 * Returns : JB_ERR_OK on success
853 * JB_ERR_MEMORY on out-of-memory
854 * JB_ERR_CGI_PARAMS if the CGI parameters are not
855 * specified or not valid.
857 *********************************************************************/
858 jb_err cgi_edit_actions_url_form(struct client_state *csp,
859 struct http_response *rsp,
860 const struct map *parameters)
862 struct map * exports;
864 struct editable_file * file;
865 struct file_line * cur_line;
866 unsigned line_number;
867 unsigned section_start_line_number = 0;
874 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
876 return cgi_error_disabled(csp, rsp);
879 err = get_number_param(csp, parameters, "p", &patternid);
885 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
888 /* No filename specified, can't read file, modified, or out of memory. */
889 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
892 cur_line = file->lines;
894 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
896 if (cur_line->type == FILE_LINE_ACTION)
898 section_start_line_number = line_number;
900 cur_line = cur_line->next;
903 if ( (cur_line == NULL)
904 || (line_number != patternid)
906 || (cur_line->type != FILE_LINE_URL))
908 /* Invalid "patternid" parameter */
909 edit_free_file(file);
910 return JB_ERR_CGI_PARAMS;
913 if (NULL == (exports = default_exports(csp, NULL)))
915 edit_free_file(file);
916 return JB_ERR_MEMORY;
919 err = map(exports, "f", 1, stringify(file->identifier), 0);
920 if (!err) err = map(exports, "v", 1, file->version_str, 1);
921 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
922 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
923 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
925 edit_free_file(file);
933 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
937 /*********************************************************************
939 * Function : cgi_edit_actions_add_url_form
941 * Description : CGI function that displays a form for
945 * 1 : csp = Current client state (buffers, headers, etc...)
946 * 2 : rsp = http_response data structure for output
947 * 3 : parameters = map of cgi parameters
950 * f : (filename) Identifies the file to edit
951 * v : (version) File's last-modified time
952 * s : (section) Line number of section to edit
954 * Returns : JB_ERR_OK on success
955 * JB_ERR_MEMORY on out-of-memory
956 * JB_ERR_CGI_PARAMS if the CGI parameters are not
957 * specified or not valid.
959 *********************************************************************/
960 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
961 struct http_response *rsp,
962 const struct map *parameters)
971 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
973 return cgi_error_disabled(csp, rsp);
976 if (NULL == (exports = default_exports(csp, NULL)))
978 return JB_ERR_MEMORY;
981 err = map_copy_parameter_html(exports, parameters, "f");
982 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
983 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
991 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
995 /*********************************************************************
997 * Function : cgi_edit_actions_remove_url_form
999 * Description : CGI function that displays a form for
1003 * 1 : csp = Current client state (buffers, headers, etc...)
1004 * 2 : rsp = http_response data structure for output
1005 * 3 : parameters = map of cgi parameters
1008 * f : (number) The action file identifier.
1009 * v : (version) File's last-modified time
1010 * p : (pattern) Line number of pattern to edit
1012 * Returns : JB_ERR_OK on success
1013 * JB_ERR_MEMORY on out-of-memory
1014 * JB_ERR_CGI_PARAMS if the CGI parameters are not
1015 * specified or not valid.
1017 *********************************************************************/
1018 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
1019 struct http_response *rsp,
1020 const struct map *parameters)
1022 struct map * exports;
1024 struct editable_file * file;
1025 struct file_line * cur_line;
1026 unsigned line_number;
1027 unsigned section_start_line_number = 0;
1034 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1036 return cgi_error_disabled(csp, rsp);
1039 err = get_number_param(csp, parameters, "p", &patternid);
1045 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
1048 /* No filename specified, can't read file, modified, or out of memory. */
1049 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
1052 cur_line = file->lines;
1054 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
1056 if (cur_line->type == FILE_LINE_ACTION)
1058 section_start_line_number = line_number;
1060 cur_line = cur_line->next;
1063 if ( (cur_line == NULL)
1064 || (line_number != patternid)
1066 || (cur_line->type != FILE_LINE_URL))
1068 /* Invalid "patternid" parameter */
1069 edit_free_file(file);
1070 return JB_ERR_CGI_PARAMS;
1073 if (NULL == (exports = default_exports(csp, NULL)))
1075 edit_free_file(file);
1076 return JB_ERR_MEMORY;
1079 err = map(exports, "f", 1, stringify(file->identifier), 0);
1080 if (!err) err = map(exports, "v", 1, file->version_str, 1);
1081 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
1082 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
1083 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
1084 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
1086 edit_free_file(file);
1094 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
1098 /*********************************************************************
1100 * Function : edit_write_file
1102 * Description : Write a complete file to disk.
1105 * 1 : file = File to write.
1107 * Returns : JB_ERR_OK on success
1108 * JB_ERR_FILE on error writing to file.
1109 * JB_ERR_MEMORY on out of memory
1111 *********************************************************************/
1112 jb_err edit_write_file(struct editable_file * file)
1115 struct file_line * cur_line;
1116 struct stat statbuf[1];
1117 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
1118 digits in time_t, assuming this is a 64-bit
1119 machine, plus null terminator, plus one
1123 assert(file->filename);
1125 if (NULL == (fp = fopen(file->filename, "wb")))
1130 cur_line = file->lines;
1131 while (cur_line != NULL)
1135 if (fputs(cur_line->raw, fp) < 0)
1143 if (cur_line->prefix)
1145 if (fputs(cur_line->prefix, fp) < 0)
1151 if (cur_line->unprocessed)
1154 if (NULL != strchr(cur_line->unprocessed, '#'))
1156 /* Must quote '#' characters */
1163 /* Count number of # characters, so we know length of output string */
1164 src = cur_line->unprocessed;
1165 while (NULL != (src = strchr(src, '#')))
1170 assert(numhash > 0);
1172 /* Allocate new memory for string */
1173 len = strlen(cur_line->unprocessed) + (size_t)numhash;
1174 if (NULL == (str = malloc(len + 1)))
1176 /* Uh oh, just trashed file! */
1178 return JB_ERR_MEMORY;
1181 /* Copy string but quote hashes */
1182 src = cur_line->unprocessed;
1190 assert(numhash >= 0);
1196 assert(numhash == 0);
1197 assert(strlen(str) == len);
1198 assert(str == dest - len);
1199 assert(src - len <= cur_line->unprocessed);
1201 if ((strlen(str) != len) || (numhash != 0))
1204 * Escaping didn't work as expected, go spread the news.
1205 * Only reached in non-debugging builds.
1207 log_error(LOG_LEVEL_ERROR,
1208 "Looks like hash escaping failed. %s might be corrupted now.",
1212 if (fputs(str, fp) < 0)
1223 /* Can write without quoting '#' characters. */
1224 if (fputs(cur_line->unprocessed, fp) < 0)
1230 if (fputs(NEWLINE(file->newline), fp) < 0)
1238 /* FIXME: Write data from file->data->whatever */
1242 cur_line = cur_line->next;
1248 /* Update the version stamp in the file structure, since we just
1249 * wrote to the file & changed it's date.
1251 if (stat(file->filename, statbuf) < 0)
1253 /* Error, probably file not found. */
1256 file->version = (unsigned)statbuf->st_mtime;
1258 /* Correct file->version_str */
1259 freez(file->version_str);
1260 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1261 version_buf[sizeof(version_buf)-1] = '\0';
1262 file->version_str = strdup(version_buf);
1263 if (version_buf == NULL)
1265 return JB_ERR_MEMORY;
1272 /*********************************************************************
1274 * Function : edit_free_file
1276 * Description : Free a complete file in memory.
1279 * 1 : file = Data structure to free.
1283 *********************************************************************/
1284 void edit_free_file(struct editable_file * file)
1288 /* Silently ignore NULL pointer */
1292 edit_free_file_lines(file->lines);
1293 freez(file->version_str);
1295 file->parse_error_text = NULL; /* Statically allocated */
1296 file->parse_error = NULL;
1302 /*********************************************************************
1304 * Function : edit_free_file_lines
1306 * Description : Free an entire linked list of file lines.
1309 * 1 : first_line = Data structure to free.
1313 *********************************************************************/
1314 static void edit_free_file_lines(struct file_line * first_line)
1316 struct file_line * next_line;
1318 while (first_line != NULL)
1320 next_line = first_line->next;
1321 first_line->next = NULL;
1322 freez(first_line->raw);
1323 freez(first_line->prefix);
1324 freez(first_line->unprocessed);
1325 switch(first_line->type)
1327 case 0: /* special case if memory zeroed */
1328 case FILE_LINE_UNPROCESSED:
1329 case FILE_LINE_BLANK:
1330 case FILE_LINE_ALIAS_HEADER:
1331 case FILE_LINE_SETTINGS_HEADER:
1332 case FILE_LINE_DESCRIPTION_HEADER:
1333 case FILE_LINE_DESCRIPTION_ENTRY:
1334 case FILE_LINE_ALIAS_ENTRY:
1336 /* No data is stored for these */
1339 case FILE_LINE_ACTION:
1340 free_action(first_line->data.action);
1343 case FILE_LINE_SETTINGS_ENTRY:
1344 freez(first_line->data.setting.name);
1345 freez(first_line->data.setting.svalue);
1348 /* Should never happen */
1352 first_line->type = 0; /* paranoia */
1354 first_line = next_line;
1359 /*********************************************************************
1361 * Function : match_actions_file_header_line
1363 * Description : Match an actions file {{header}} line
1366 * 1 : line = String from file
1367 * 2 : name = Header to match against
1369 * Returns : 0 iff they match.
1371 *********************************************************************/
1372 static int match_actions_file_header_line(const char * line, const char * name)
1380 if ((line[0] != '{') || (line[1] != '{'))
1386 /* Look for optional whitespace */
1387 while ( (*line == ' ') || (*line == '\t') )
1392 /* Look for the specified name (case-insensitive) */
1394 if (0 != strncmpic(line, name, len))
1400 /* Look for optional whitespace */
1401 while ( (*line == ' ') || (*line == '\t') )
1406 /* Look for "}}" and end of string*/
1407 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1417 /*********************************************************************
1419 * Function : match_actions_file_header_line
1421 * Description : Match an actions file {{header}} line
1424 * 1 : line = String from file. Must not start with
1425 * whitespace (else infinite loop!)
1426 * 2 : pname = Destination for name
1427 * 2 : pvalue = Destination for value
1429 * Returns : JB_ERR_OK on success
1430 * JB_ERR_MEMORY on out-of-memory
1431 * JB_ERR_PARSE if there's no "=" sign, or if there's
1432 * nothing before the "=" sign (but empty
1433 * values *after* the "=" sign are legal).
1435 *********************************************************************/
1436 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1438 const char * name_end;
1439 const char * value_start;
1445 assert(*line != ' ');
1446 assert(*line != '\t');
1451 value_start = strchr(line, '=');
1452 if ((value_start == NULL) || (value_start == line))
1454 return JB_ERR_PARSE;
1457 name_end = value_start - 1;
1459 /* Eat any whitespace before the '=' */
1460 while ((*name_end == ' ') || (*name_end == '\t'))
1463 * we already know we must have at least 1 non-ws char
1464 * at start of buf - no need to check
1469 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1470 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1472 return JB_ERR_MEMORY;
1474 strncpy(*pname, line, name_len);
1475 (*pname)[name_len] = '\0';
1477 /* Eat any the whitespace after the '=' */
1479 while ((*value_start == ' ') || (*value_start == '\t'))
1484 if (NULL == (*pvalue = strdup(value_start)))
1488 return JB_ERR_MEMORY;
1495 /*********************************************************************
1497 * Function : edit_parse_actions_file
1499 * Description : Parse an actions file in memory.
1501 * Passed linked list must have the "data" member
1502 * zeroed, and must contain valid "next" and
1503 * "unprocessed" fields. The "raw" and "prefix"
1504 * fields are ignored, and "type" is just overwritten.
1506 * Note that on error the file may have been
1510 * 1 : file = Actions file to be parsed in-place.
1512 * Returns : JB_ERR_OK on success
1513 * JB_ERR_MEMORY on out-of-memory
1514 * JB_ERR_PARSE on error
1516 *********************************************************************/
1517 jb_err edit_parse_actions_file(struct editable_file * file)
1519 struct file_line * cur_line;
1521 const char * text; /* Text from a line */
1522 char * name; /* For lines of the form name=value */
1523 char * value; /* For lines of the form name=value */
1524 struct action_alias * alias_list = NULL;
1525 jb_err err = JB_ERR_OK;
1527 /* alias_list contains the aliases defined in this file.
1528 * It might be better to use the "file_line.data" fields
1529 * in the relavent places instead.
1532 cur_line = file->lines;
1534 /* A note about blank line support: Blank lines should only
1535 * ever occur as the last line in the file. This function
1536 * is more forgiving than that - FILE_LINE_BLANK can occur
1540 /* Skip leading blanks. Should only happen if file is
1541 * empty (which is valid, but pointless).
1543 while ( (cur_line != NULL)
1544 && (cur_line->unprocessed[0] == '\0') )
1547 cur_line->type = FILE_LINE_BLANK;
1548 cur_line = cur_line->next;
1551 if ( (cur_line != NULL)
1552 && (cur_line->unprocessed[0] != '{') )
1554 /* File doesn't start with a header */
1555 file->parse_error = cur_line;
1556 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1557 return JB_ERR_PARSE;
1560 if ( (cur_line != NULL) && (0 ==
1561 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1563 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1565 cur_line = cur_line->next;
1566 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1568 if (cur_line->unprocessed[0])
1570 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1572 err = split_line_on_equals(cur_line->unprocessed,
1573 &cur_line->data.setting.name,
1574 &cur_line->data.setting.svalue);
1575 if (err == JB_ERR_MEMORY)
1579 else if (err != JB_ERR_OK)
1581 /* Line does not contain a name=value pair */
1582 file->parse_error = cur_line;
1583 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1584 return JB_ERR_PARSE;
1589 cur_line->type = FILE_LINE_BLANK;
1591 cur_line = cur_line->next;
1595 if ( (cur_line != NULL) && (0 ==
1596 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1598 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1600 cur_line = cur_line->next;
1601 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1603 if (cur_line->unprocessed[0])
1605 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1609 cur_line->type = FILE_LINE_BLANK;
1611 cur_line = cur_line->next;
1615 if ( (cur_line != NULL) && (0 ==
1616 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1618 cur_line->type = FILE_LINE_ALIAS_HEADER;
1620 cur_line = cur_line->next;
1621 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1623 if (cur_line->unprocessed[0])
1625 /* define an alias */
1626 struct action_alias * new_alias;
1628 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1630 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1631 if (err == JB_ERR_MEMORY)
1635 else if (err != JB_ERR_OK)
1637 /* Line does not contain a name=value pair */
1638 file->parse_error = cur_line;
1639 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1640 return JB_ERR_PARSE;
1643 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1648 free_alias_list(alias_list);
1649 return JB_ERR_MEMORY;
1652 err = get_actions(value, alias_list, new_alias->action);
1655 /* Invalid action or out of memory */
1659 free_alias_list(alias_list);
1660 if (err == JB_ERR_MEMORY)
1666 /* Line does not contain a name=value pair */
1667 file->parse_error = cur_line;
1668 file->parse_error_text = "This alias does not specify a valid set of actions.";
1669 return JB_ERR_PARSE;
1675 new_alias->name = name;
1678 new_alias->next = alias_list;
1679 alias_list = new_alias;
1683 cur_line->type = FILE_LINE_BLANK;
1685 cur_line = cur_line->next;
1689 /* Header done, process the main part of the file */
1690 while (cur_line != NULL)
1692 /* At this point, (cur_line->unprocessed[0] == '{') */
1693 assert(cur_line->unprocessed[0] == '{');
1694 text = cur_line->unprocessed + 1;
1695 len = strlen(text) - 1;
1696 if (text[len] != '}')
1698 /* No closing } on header */
1699 free_alias_list(alias_list);
1700 file->parse_error = cur_line;
1701 file->parse_error_text = "Headers starting with '{' must have a "
1702 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1703 "must close with two brackets ('}}').";
1704 return JB_ERR_PARSE;
1709 /* An invalid {{ header. */
1710 free_alias_list(alias_list);
1711 file->parse_error = cur_line;
1712 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1713 "Please remember that the system (two-bracket) headers must "
1714 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1715 "and must appear before any actions (one-bracket) headers. "
1716 "Also note that system headers may not be repeated.";
1717 return JB_ERR_PARSE;
1720 while ( (*text == ' ') || (*text == '\t') )
1725 while ( (len > (size_t)0)
1726 && ( (text[len - 1] == ' ')
1727 || (text[len - 1] == '\t') ) )
1732 cur_line->type = FILE_LINE_ACTION;
1734 /* Remove {} and make copy */
1735 if (NULL == (value = (char *) malloc(len + 1)))
1738 free_alias_list(alias_list);
1739 return JB_ERR_MEMORY;
1741 strncpy(value, text, len);
1745 err = get_actions(value, alias_list, cur_line->data.action);
1748 /* Invalid action or out of memory */
1750 free_alias_list(alias_list);
1751 if (err == JB_ERR_MEMORY)
1757 /* Line does not contain a name=value pair */
1758 file->parse_error = cur_line;
1759 file->parse_error_text = "This header does not specify a valid set of actions.";
1760 return JB_ERR_PARSE;
1764 /* Done with string - it was clobbered anyway */
1767 /* Process next line */
1768 cur_line = cur_line->next;
1770 /* Loop processing URL patterns */
1771 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1773 if (cur_line->unprocessed[0])
1775 /* Could parse URL here, but this isn't currently needed */
1777 cur_line->type = FILE_LINE_URL;
1781 cur_line->type = FILE_LINE_BLANK;
1783 cur_line = cur_line->next;
1785 } /* End main while(cur_line != NULL) loop */
1787 free_alias_list(alias_list);
1793 /*********************************************************************
1795 * Function : edit_read_file_lines
1797 * Description : Read all the lines of a file into memory.
1798 * Handles whitespace, comments and line continuation.
1801 * 1 : fp = File to read from. On return, this will be
1802 * at EOF but it will not have been closed.
1803 * 2 : pfile = Destination for a linked list of file_lines.
1804 * Will be set to NULL on error.
1805 * 3 : newline = How to handle newlines.
1807 * Returns : JB_ERR_OK on success
1808 * JB_ERR_MEMORY on out-of-memory
1810 *********************************************************************/
1811 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1813 struct file_line * first_line; /* Keep for return value or to free */
1814 struct file_line * cur_line; /* Current line */
1815 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1823 cur_line = first_line = zalloc(sizeof(struct file_line));
1824 if (cur_line == NULL)
1826 return JB_ERR_MEMORY;
1829 cur_line->type = FILE_LINE_UNPROCESSED;
1831 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1834 /* Out of memory or empty file. */
1835 /* Note that empty file is not an error we propogate up */
1837 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1842 prev_line = cur_line;
1843 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1844 if (cur_line == NULL)
1847 edit_free_file_lines(first_line);
1848 return JB_ERR_MEMORY;
1851 cur_line->type = FILE_LINE_UNPROCESSED;
1853 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1854 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1857 edit_free_file_lines(first_line);
1858 return JB_ERR_MEMORY;
1862 while (rval != JB_ERR_FILE);
1866 /* We allocated one too many - free it */
1867 prev_line->next = NULL;
1870 *pfile = first_line;
1875 /*********************************************************************
1877 * Function : edit_read_file
1879 * Description : Read a complete file into memory.
1880 * Handles CGI parameter parsing. If requested, also
1881 * checks the file's modification timestamp.
1884 * 1 : csp = Current client state (buffers, headers, etc...)
1885 * 2 : parameters = map of cgi parameters.
1886 * 3 : require_version = true to check "ver" parameter.
1887 * 4 : pfile = Destination for the file. Will be set
1891 * f : The action file identifier.
1892 * ver : (Only if require_version is nonzero)
1893 * Timestamp of the actions file. If wrong, this
1894 * function fails with JB_ERR_MODIFIED.
1896 * Returns : JB_ERR_OK on success
1897 * JB_ERR_MEMORY on out-of-memory
1898 * JB_ERR_CGI_PARAMS if "filename" was not specified
1900 * JB_ERR_FILE if the file cannot be opened or
1902 * JB_ERR_MODIFIED if version checking was requested and
1903 * failed - the file was modified outside
1904 * of this CGI editor instance.
1906 *********************************************************************/
1907 jb_err edit_read_file(struct client_state *csp,
1908 const struct map *parameters,
1909 int require_version,
1910 struct editable_file **pfile)
1912 struct file_line * lines;
1915 const char *filename = NULL;
1916 struct editable_file * file;
1917 unsigned version = 0;
1918 struct stat statbuf[1];
1919 char version_buf[22];
1920 int newline = NEWLINE_UNKNOWN;
1929 err = get_number_param(csp, parameters, "f", &i);
1930 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1932 filename = csp->config->actions_file[i];
1934 else if (JB_ERR_CGI_PARAMS == err)
1937 * Probably an old-school URL like
1938 * http://config.privoxy.org/edit-actions-list?f=default
1940 err = get_file_name_param(csp, parameters, "f", &filename);
1943 if (NULL == filename || stat(filename, statbuf) < 0)
1945 /* Error, probably file not found. */
1948 version = (unsigned) statbuf->st_mtime;
1950 if (require_version)
1952 unsigned specified_version;
1953 err = get_number_param(csp, parameters, "v", &specified_version);
1959 if (version != specified_version)
1961 return JB_ERR_MODIFIED;
1965 if (NULL == (fp = fopen(filename,"rb")))
1970 err = edit_read_file_lines(fp, &lines, &newline);
1979 file = (struct editable_file *) zalloc(sizeof(*file));
1982 edit_free_file_lines(lines);
1986 file->lines = lines;
1987 file->newline = newline;
1988 file->filename = filename;
1989 file->version = version;
1990 file->identifier = i;
1992 /* Correct file->version_str */
1993 freez(file->version_str);
1994 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1995 version_buf[sizeof(version_buf)-1] = '\0';
1996 file->version_str = strdup(version_buf);
1997 if (version_buf == NULL)
1999 edit_free_file(file);
2000 return JB_ERR_MEMORY;
2008 /*********************************************************************
2010 * Function : edit_read_actions_file
2012 * Description : Read a complete actions file into memory.
2013 * Handles CGI parameter parsing. If requested, also
2014 * checks the file's modification timestamp.
2016 * If this function detects an error in the categories
2017 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
2018 * then it handles it by filling in the specified
2019 * response structure and returning JB_ERR_FILE.
2022 * 1 : csp = Current client state (buffers, headers, etc...)
2023 * 2 : rsp = HTTP response. Only filled in on error.
2024 * 2 : parameters = map of cgi parameters.
2025 * 3 : require_version = true to check "ver" parameter.
2026 * 4 : pfile = Destination for the file. Will be set
2030 * f : The actions file identifier.
2031 * ver : (Only if require_version is nonzero)
2032 * Timestamp of the actions file. If wrong, this
2033 * function fails with JB_ERR_MODIFIED.
2035 * Returns : JB_ERR_OK on success
2036 * JB_ERR_MEMORY on out-of-memory
2037 * JB_ERR_CGI_PARAMS if "filename" was not specified
2039 * JB_ERR_FILE if the file does not contain valid data,
2040 * or if file cannot be opened or
2041 * contains no data, or if version
2042 * checking was requested and failed.
2044 *********************************************************************/
2045 jb_err edit_read_actions_file(struct client_state *csp,
2046 struct http_response *rsp,
2047 const struct map *parameters,
2048 int require_version,
2049 struct editable_file **pfile)
2052 struct editable_file *file;
2053 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
2061 err = edit_read_file(csp, parameters, require_version, &file);
2064 /* Try to handle if possible */
2065 if (err == JB_ERR_FILE)
2067 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
2069 else if (err == JB_ERR_MODIFIED)
2071 assert(require_version);
2072 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
2073 log_error(LOG_LEVEL_ERROR,
2074 "Blocking CGI edit request due to modification time mismatch.");
2075 if (acceptable_failures > 0)
2077 log_error(LOG_LEVEL_INFO,
2078 "The CGI editor will be turned off after another %d mismatche(s).",
2079 acceptable_failures);
2080 acceptable_failures--;
2084 log_error(LOG_LEVEL_INFO,
2085 "Timestamp mismatch limit reached, turning CGI editor off. "
2086 "Reload the configuration file to reenable it.");
2087 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
2090 if (err == JB_ERR_OK)
2093 * Signal to higher-level CGI code that there was a problem but we
2094 * handled it, they should just return JB_ERR_OK.
2101 err = edit_parse_actions_file(file);
2104 if (err == JB_ERR_PARSE)
2106 err = cgi_error_parse(csp, rsp, file);
2107 if (err == JB_ERR_OK)
2110 * Signal to higher-level CGI code that there was a problem but we
2111 * handled it, they should just return JB_ERR_OK.
2116 edit_free_file(file);
2125 /*********************************************************************
2127 * Function : get_file_name_param
2129 * Description : Get the name of the file to edit from the parameters
2130 * passed to a CGI function using the old syntax.
2131 * This function handles security checks and only
2132 * accepts files that Privoxy already knows.
2135 * 1 : csp = Current client state (buffers, headers, etc...)
2136 * 2 : parameters = map of cgi parameters
2137 * 3 : param_name = The name of the parameter to read
2138 * 4 : pfilename = pointer to the filename in
2139 * csp->config->actions_file[] if found. Set to NULL on error.
2141 * Returns : JB_ERR_OK on success
2142 * JB_ERR_MEMORY on out-of-memory
2143 * JB_ERR_CGI_PARAMS if "filename" was not specified
2146 *********************************************************************/
2147 static jb_err get_file_name_param(struct client_state *csp,
2148 const struct map *parameters,
2149 const char *param_name,
2150 const char **pfilename)
2153 const char suffix[] = ".action";
2168 param = lookup(parameters, param_name);
2171 return JB_ERR_CGI_PARAMS;
2174 len = strlen(param);
2175 if (len >= FILENAME_MAX)
2178 return JB_ERR_CGI_PARAMS;
2182 * Check every character to see if it's legal.
2183 * Totally unnecessary but we do it anyway.
2186 while ((ch = *s++) != '\0')
2188 if ( ((ch < 'A') || (ch > 'Z'))
2189 && ((ch < 'a') || (ch > 'z'))
2190 && ((ch < '0') || (ch > '9'))
2194 /* Probable hack attempt. */
2195 return JB_ERR_CGI_PARAMS;
2199 /* Append extension */
2200 name_size = len + strlen(suffix) + 1;
2201 name = malloc(name_size);
2204 return JB_ERR_MEMORY;
2206 strlcpy(name, param, name_size);
2207 strlcat(name, suffix, name_size);
2210 fullpath = make_path(csp->config->confdir, name);
2213 if (fullpath == NULL)
2215 return JB_ERR_MEMORY;
2218 /* Check if the file is known */
2219 for (i = 0; i < MAX_AF_FILES; i++)
2221 if (NULL != csp->config->actions_file[i] &&
2222 !strcmp(fullpath, csp->config->actions_file[i]))
2225 *pfilename = csp->config->actions_file[i];
2233 return JB_ERR_CGI_PARAMS;
2237 /*********************************************************************
2239 * Function : get_url_spec_param
2241 * Description : Get a URL pattern from the parameters
2242 * passed to a CGI function. Removes leading/trailing
2243 * spaces and validates it.
2246 * 1 : csp = Current client state (buffers, headers, etc...)
2247 * 2 : parameters = map of cgi parameters
2248 * 3 : name = Name of CGI parameter to read
2249 * 4 : pvalue = destination for value. Will be malloc()'d.
2250 * Set to NULL on error.
2252 * Returns : JB_ERR_OK on success
2253 * JB_ERR_MEMORY on out-of-memory
2254 * JB_ERR_CGI_PARAMS if the parameter was not specified
2257 *********************************************************************/
2258 static jb_err get_url_spec_param(struct client_state *csp,
2259 const struct map *parameters,
2263 const char *orig_param;
2266 struct url_spec compiled[1];
2276 orig_param = lookup(parameters, name);
2279 return JB_ERR_CGI_PARAMS;
2282 /* Copy and trim whitespace */
2283 param = strdup(orig_param);
2286 return JB_ERR_MEMORY;
2290 /* Must be non-empty, and can't allow 1st character to be '{' */
2291 if (param[0] == '\0' || param[0] == '{')
2294 return JB_ERR_CGI_PARAMS;
2297 /* Check for embedded newlines */
2298 for (s = param; *s != '\0'; s++)
2300 if ((*s == '\r') || (*s == '\n'))
2303 return JB_ERR_CGI_PARAMS;
2307 /* Check that regex is valid */
2312 return JB_ERR_MEMORY;
2314 err = create_url_spec(compiled, s);
2319 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2321 free_url_spec(compiled);
2323 if (param[strlen(param) - 1] == '\\')
2326 * Must protect trailing '\\' from becoming line continuation character.
2327 * Two methods: 1) If it's a domain only, add a trailing '/'.
2328 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2330 if (strchr(param, '/') == NULL)
2332 err = string_append(¶m, "/");
2336 err = string_append(¶m, "(?:)");
2343 /* Check that the modified regex is valid */
2348 return JB_ERR_MEMORY;
2350 err = create_url_spec(compiled, s);
2355 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2357 free_url_spec(compiled);
2364 /*********************************************************************
2366 * Function : map_radio
2368 * Description : Map a set of radio button values. E.g. if you have
2369 * 3 radio buttons, declare them as:
2370 * <option type="radio" name="xyz" @xyz-a@>
2371 * <option type="radio" name="xyz" @xyz-b@>
2372 * <option type="radio" name="xyz" @xyz-c@>
2373 * Then map one of the @xyz-?@ variables to "checked"
2374 * and all the others to empty by calling:
2375 * map_radio(exports, "xyz", "abc", sel)
2376 * Where 'sel' is 'a', 'b', or 'c'.
2379 * 1 : exports = Exports map to modify.
2380 * 2 : optionname = name for map
2381 * 3 : values = null-terminated list of values;
2382 * 4 : value = Selected value.
2384 * CGI Parameters : None
2386 * Returns : JB_ERR_OK on success
2387 * JB_ERR_MEMORY on out-of-memory
2389 *********************************************************************/
2390 static jb_err map_radio(struct map * exports,
2391 const char * optionname,
2392 const char * values,
2398 const size_t len = strlen(optionname);
2399 const size_t buf_size = len + 3;
2405 buf = malloc(buf_size);
2408 return JB_ERR_MEMORY;
2411 strlcpy(buf, optionname, buf_size);
2413 /* XXX: this looks ... interesting */
2418 while ((c = *values++) != '\0')
2423 if (map(exports, buf, 1, "", 1))
2425 return JB_ERR_MEMORY;
2431 return map(exports, buf, 0, "checked", 1);
2435 /*********************************************************************
2437 * Function : cgi_error_modified
2439 * Description : CGI function that is called when a file is modified
2440 * outside the CGI editor.
2443 * 1 : csp = Current client state (buffers, headers, etc...)
2444 * 2 : rsp = http_response data structure for output
2445 * 3 : filename = The file that was modified.
2447 * CGI Parameters : none
2449 * Returns : JB_ERR_OK on success
2450 * JB_ERR_MEMORY on out-of-memory error.
2452 *********************************************************************/
2453 jb_err cgi_error_modified(struct client_state *csp,
2454 struct http_response *rsp,
2455 const char *filename)
2457 struct map *exports;
2464 if (NULL == (exports = default_exports(csp, NULL)))
2466 return JB_ERR_MEMORY;
2469 err = map(exports, "f", 1, html_encode(filename), 0);
2476 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2480 /*********************************************************************
2482 * Function : cgi_error_parse
2484 * Description : CGI function that is called when a file cannot
2485 * be parsed by the CGI editor.
2488 * 1 : csp = Current client state (buffers, headers, etc...)
2489 * 2 : rsp = http_response data structure for output
2490 * 3 : file = The file that was modified.
2492 * CGI Parameters : none
2494 * Returns : JB_ERR_OK on success
2495 * JB_ERR_MEMORY on out-of-memory error.
2497 *********************************************************************/
2498 jb_err cgi_error_parse(struct client_state *csp,
2499 struct http_response *rsp,
2500 struct editable_file *file)
2502 struct map *exports;
2504 struct file_line *cur_line;
2510 if (NULL == (exports = default_exports(csp, NULL)))
2512 return JB_ERR_MEMORY;
2515 err = map(exports, "f", 1, stringify(file->identifier), 0);
2516 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2518 cur_line = file->parse_error;
2521 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2522 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2530 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2534 /*********************************************************************
2536 * Function : cgi_error_file
2538 * Description : CGI function that is called when a file cannot be
2539 * opened by the CGI editor.
2542 * 1 : csp = Current client state (buffers, headers, etc...)
2543 * 2 : rsp = http_response data structure for output
2544 * 3 : filename = The file that was modified.
2546 * CGI Parameters : none
2548 * Returns : JB_ERR_OK on success
2549 * JB_ERR_MEMORY on out-of-memory error.
2551 *********************************************************************/
2552 jb_err cgi_error_file(struct client_state *csp,
2553 struct http_response *rsp,
2554 const char *filename)
2556 struct map *exports;
2563 if (NULL == (exports = default_exports(csp, NULL)))
2565 return JB_ERR_MEMORY;
2568 err = map(exports, "f", 1, html_encode(filename), 0);
2575 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2579 /*********************************************************************
2581 * Function : cgi_error_file_read_only
2583 * Description : CGI function that is called when a file cannot be
2584 * opened for writing by the CGI editor.
2587 * 1 : csp = Current client state (buffers, headers, etc...)
2588 * 2 : rsp = http_response data structure for output
2589 * 3 : filename = The file that we can't write to
2591 * CGI Parameters : none
2593 * Returns : JB_ERR_OK on success
2594 * JB_ERR_MEMORY on out-of-memory error.
2596 *********************************************************************/
2597 jb_err cgi_error_file_read_only(struct client_state *csp,
2598 struct http_response *rsp,
2599 const char *filename)
2601 struct map *exports;
2608 if (NULL == (exports = default_exports(csp, NULL)))
2610 return JB_ERR_MEMORY;
2613 err = map(exports, "f", 1, html_encode(filename), 0);
2620 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2624 /*********************************************************************
2626 * Function : cgi_edit_actions
2628 * Description : CGI function that allows the user to choose which
2629 * actions file to edit.
2632 * 1 : csp = Current client state (buffers, headers, etc...)
2633 * 2 : rsp = http_response data structure for output
2634 * 3 : parameters = map of cgi parameters
2636 * CGI Parameters : None
2638 * Returns : JB_ERR_OK on success
2639 * JB_ERR_MEMORY on out-of-memory error
2641 *********************************************************************/
2642 jb_err cgi_edit_actions(struct client_state *csp,
2643 struct http_response *rsp,
2644 const struct map *parameters)
2648 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2650 return cgi_error_disabled(csp, rsp);
2653 /* FIXME: Incomplete */
2655 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2660 /*********************************************************************
2662 * Function : cgi_edit_actions_list
2664 * Description : CGI function that edits the actions list.
2665 * FIXME: This function shouldn't FATAL ever.
2666 * FIXME: This function doesn't check the retval of map()
2668 * 1 : csp = Current client state (buffers, headers, etc...)
2669 * 2 : rsp = http_response data structure for output
2670 * 3 : parameters = map of cgi parameters
2672 * CGI Parameters : filename
2674 * Returns : JB_ERR_OK on success
2675 * JB_ERR_MEMORY on out-of-memory
2676 * JB_ERR_FILE if the file cannot be opened or
2678 * JB_ERR_CGI_PARAMS if "filename" was not specified
2681 *********************************************************************/
2682 jb_err cgi_edit_actions_list(struct client_state *csp,
2683 struct http_response *rsp,
2684 const struct map *parameters)
2686 char * section_template;
2687 char * url_template;
2692 struct map * exports;
2693 struct map * section_exports;
2694 struct map * url_exports;
2695 struct editable_file * file;
2696 struct file_line * cur_line;
2697 unsigned line_number = 0;
2698 unsigned prev_section_line_number = ((unsigned) (-1));
2700 struct file_list * fl;
2701 struct url_actions * b;
2702 char * buttons = NULL;
2705 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2707 return cgi_error_disabled(csp, rsp);
2710 if (NULL == (exports = default_exports(csp, NULL)))
2712 return JB_ERR_MEMORY;
2715 /* Load actions file */
2716 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2719 /* No filename specified, can't read file, or out of memory. */
2721 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2724 /* Find start of actions in file */
2725 cur_line = file->lines;
2727 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2729 cur_line = cur_line->next;
2734 * Conventional actions files should have a match all block
2736 * cur_line = {...global actions...}
2737 * cur_line->next = /
2738 * cur_line->next->next = {...actions...} or EOF
2740 if ( (cur_line != NULL)
2741 && (cur_line->type == FILE_LINE_ACTION)
2742 && (cur_line->next != NULL)
2743 && (cur_line->next->type == FILE_LINE_URL)
2744 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2745 && ( (cur_line->next->next == NULL)
2746 || (cur_line->next->next->type != FILE_LINE_URL)
2750 * Generate string with buttons to set actions for "/" to
2751 * any predefined set of actions (named standard.*, probably
2752 * residing in standard.action).
2755 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2758 edit_free_file(file);
2760 if (err == JB_ERR_FILE)
2762 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2767 err = template_fill(§ion_template, exports);
2770 edit_free_file(file);
2775 buttons = strdup("");
2776 for (i = 0; i < MAX_AF_FILES; i++)
2778 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2780 for (b = b->next; NULL != b; b = b->next)
2782 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2784 if (err || (NULL == (section_exports = new_map())))
2787 free(section_template);
2788 edit_free_file(file);
2790 return JB_ERR_MEMORY;
2793 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2795 if (err || (NULL == (s = strdup(section_template))))
2797 free_map(section_exports);
2799 free(section_template);
2800 edit_free_file(file);
2802 return JB_ERR_MEMORY;
2805 if (!err) err = template_fill(&s, section_exports);
2806 free_map(section_exports);
2807 if (!err) err = string_join(&buttons, s);
2812 freez(section_template);
2813 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2816 * Conventional actions file, supply extra editing help.
2817 * (e.g. don't allow them to make it an unconventional one).
2819 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2821 snprintf(buf, sizeof(buf), "%d", line_number);
2822 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2823 snprintf(buf, sizeof(buf), "%d", line_number + 2);
2824 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2825 if (!err) err = map(exports, "all-urls-actions", 1,
2826 actions_to_html(csp, cur_line->data.action), 0);
2828 /* Skip the 2 lines */
2829 cur_line = cur_line->next->next;
2833 * Note that prev_section_line_number is NOT set here.
2834 * This is deliberate and not a bug. It stops a "Move up"
2835 * option appearing on the next section. Clicking "Move
2836 * up" would make the actions file unconventional, which
2837 * we don't want, so we hide this option.
2843 * Non-standard actions file - does not begin with
2844 * the "All URLs" section.
2846 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2849 /* Set up global exports */
2851 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2852 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2853 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2855 /* Discourage private additions to default.action */
2857 if (!err) err = map_conditional(exports, "default-action",
2858 (strstr("default.action", file->filename) != NULL));
2861 edit_free_file(file);
2866 /* Should do all global exports above this point */
2868 /* Load templates */
2870 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2873 edit_free_file(file);
2875 if (err == JB_ERR_FILE)
2877 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2882 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2885 free(section_template);
2886 edit_free_file(file);
2888 if (err == JB_ERR_FILE)
2890 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2895 err = template_fill(§ion_template, exports);
2899 edit_free_file(file);
2904 err = template_fill(&url_template, exports);
2907 free(section_template);
2908 edit_free_file(file);
2913 if (NULL == (sections = strdup("")))
2915 free(section_template);
2917 edit_free_file(file);
2919 return JB_ERR_MEMORY;
2922 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2924 if (NULL == (section_exports = new_map()))
2927 free(section_template);
2929 edit_free_file(file);
2931 return JB_ERR_MEMORY;
2934 snprintf(buf, sizeof(buf), "%d", line_number);
2935 err = map(section_exports, "s", 1, buf, 1);
2936 if (!err) err = map(section_exports, "actions", 1,
2937 actions_to_html(csp, cur_line->data.action), 0);
2940 && (cur_line->next != NULL)
2941 && (cur_line->next->type == FILE_LINE_URL))
2943 /* This section contains at least one URL, don't allow delete */
2944 err = map_block_killer(section_exports, "empty-section");
2948 if (!err) err = map_block_keep(section_exports, "empty-section");
2951 if (prev_section_line_number != ((unsigned)(-1)))
2953 /* Not last section */
2954 snprintf(buf, sizeof(buf), "%d", prev_section_line_number);
2955 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2956 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2961 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2963 prev_section_line_number = line_number;
2968 free(section_template);
2970 edit_free_file(file);
2972 free_map(section_exports);
2976 /* Should do all section-specific exports above this point */
2978 if (NULL == (urls = strdup("")))
2981 free(section_template);
2983 edit_free_file(file);
2985 free_map(section_exports);
2986 return JB_ERR_MEMORY;
2991 cur_line = cur_line->next;
2994 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2996 if (NULL == (url_exports = new_map()))
3000 free(section_template);
3002 edit_free_file(file);
3004 free_map(section_exports);
3005 return JB_ERR_MEMORY;
3008 snprintf(buf, sizeof(buf), "%d", line_number);
3009 err = map(url_exports, "p", 1, buf, 1);
3011 snprintf(buf, sizeof(buf), "%d", url_1_2);
3012 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
3014 if (!err) err = map(url_exports, "url-html", 1,
3015 html_encode(cur_line->unprocessed), 0);
3016 if (!err) err = map(url_exports, "url", 1,
3017 url_encode(cur_line->unprocessed), 0);
3023 free(section_template);
3025 edit_free_file(file);
3027 free_map(section_exports);
3028 free_map(url_exports);
3032 if (NULL == (s = strdup(url_template)))
3036 free(section_template);
3038 edit_free_file(file);
3040 free_map(section_exports);
3041 free_map(url_exports);
3042 return JB_ERR_MEMORY;
3045 err = template_fill(&s, section_exports);
3046 if (!err) err = template_fill(&s, url_exports);
3047 if (!err) err = string_append(&urls, s);
3049 free_map(url_exports);
3056 free(section_template);
3058 edit_free_file(file);
3060 free_map(section_exports);
3064 url_1_2 = 3 - url_1_2;
3066 cur_line = cur_line->next;
3070 err = map(section_exports, "urls", 1, urls, 0);
3072 /* Could also do section-specific exports here, but it wouldn't be as fast */
3074 snprintf(buf, sizeof(buf), "%d", line_number);
3075 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
3077 if ( (cur_line != NULL)
3078 && (cur_line->type == FILE_LINE_ACTION))
3080 /* Not last section */
3081 if (!err) err = map_block_keep(section_exports, "s-next-exists");
3086 if (!err) err = map_block_killer(section_exports, "s-next-exists");
3092 free(section_template);
3094 edit_free_file(file);
3096 free_map(section_exports);
3100 if (NULL == (s = strdup(section_template)))
3103 free(section_template);
3105 edit_free_file(file);
3107 free_map(section_exports);
3108 return JB_ERR_MEMORY;
3111 err = template_fill(&s, section_exports);
3112 if (!err) err = string_append(§ions, s);
3115 free_map(section_exports);
3120 free(section_template);
3122 edit_free_file(file);
3128 edit_free_file(file);
3129 free(section_template);
3132 err = map(exports, "sections", 1, sections, 0);
3139 /* Could also do global exports here, but it wouldn't be as fast */
3141 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
3145 /*********************************************************************
3147 * Function : cgi_edit_actions_for_url
3149 * Description : CGI function that edits the Actions list.
3152 * 1 : csp = Current client state (buffers, headers, etc...)
3153 * 2 : rsp = http_response data structure for output
3154 * 3 : parameters = map of cgi parameters
3156 * CGI Parameters : None
3158 * Returns : JB_ERR_OK on success
3159 * JB_ERR_MEMORY on out-of-memory
3160 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3161 * specified or not valid.
3163 *********************************************************************/
3164 jb_err cgi_edit_actions_for_url(struct client_state *csp,
3165 struct http_response *rsp,
3166 const struct map *parameters)
3168 struct map * exports;
3170 struct editable_file * file;
3171 struct file_line * cur_line;
3172 unsigned line_number;
3174 struct re_filterfile_spec *filter_group;
3175 int i, have_filters = 0;
3177 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3179 return cgi_error_disabled(csp, rsp);
3182 err = get_number_param(csp, parameters, "s", §ionid);
3188 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3191 /* No filename specified, can't read file, modified, or out of memory. */
3192 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3195 cur_line = file->lines;
3197 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3199 cur_line = cur_line->next;
3202 if ( (cur_line == NULL)
3203 || (line_number != sectionid)
3205 || (cur_line->type != FILE_LINE_ACTION))
3207 /* Invalid "sectionid" parameter */
3208 edit_free_file(file);
3209 return JB_ERR_CGI_PARAMS;
3212 if (NULL == (exports = default_exports(csp, NULL)))
3214 edit_free_file(file);
3215 return JB_ERR_MEMORY;
3218 err = map(exports, "f", 1, stringify(file->identifier), 0);
3219 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3220 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3222 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3225 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3226 * length limitation and ignore clicks on the Submit buttons if
3227 * the resulting GET URL would be longer than their limit.
3229 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3230 * reached this limit and action editing stopped working in these
3231 * browsers (BR #1570678).
3233 * The config option split-large-forms works around this browser
3234 * bug (HTTP has no URL lenght limitation) by deviding the action
3235 * list form into multiple smaller ones. It means the URLs are shorter
3236 * and work in broken browsers as well, but the user can no longer change
3237 * all actions with one submit.
3239 * A better solution would be to switch to POST requests,
3240 * but this will do for now.
3242 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3244 /* Generate multiple smaller form by killing the big one. */
3245 err = map_block_killer(exports, "one-form-only");
3249 /* Default: Generate one large form by killing the smaller ones. */
3250 err = map_block_killer(exports, "multiple-forms");
3253 for (i = 0; i < MAX_AF_FILES; i++)
3255 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3257 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3265 edit_free_file(file);
3270 if (0 == have_filters)
3272 err = map(exports, "filter-params", 1, "", 1);
3277 * List available filters and their settings.
3279 char *filter_template;
3280 int filter_identifier = 0;
3281 char *prepared_templates[MAX_FILTER_TYPES];
3283 for (i = 0; i < MAX_FILTER_TYPES; i++)
3285 prepared_templates[i] = strdup("");
3288 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3291 edit_free_file(file);
3293 if (err == JB_ERR_FILE)
3295 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3300 err = template_fill(&filter_template, exports);
3302 for (i = 0; i < MAX_AF_FILES; i++)
3304 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3306 filter_group = csp->rlist[i]->f;
3307 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3309 char current_mode = 'x';
3311 struct list_entry *filter_name;
3312 struct map *line_exports;
3313 const int type = filter_group->type;
3314 const int multi_action_index = filter_type_info[type].multi_action_index;
3316 assert(type < MAX_FILTER_TYPES);
3318 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
3319 while ((filter_name != NULL)
3320 && (0 != strcmp(filter_group->name, filter_name->str)))
3322 filter_name = filter_name->next;
3325 if (filter_name != NULL)
3331 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
3332 while ((filter_name != NULL)
3333 && (0 != strcmp(filter_group->name, filter_name->str)))
3335 filter_name = filter_name->next;
3337 if (filter_name != NULL)
3343 /* Generate a unique serial number */
3344 snprintf(number, sizeof(number), "%x", filter_identifier++);
3345 number[sizeof(number) - 1] = '\0';
3347 line_exports = new_map();
3348 if (line_exports == NULL)
3350 err = JB_ERR_MEMORY;
3356 if (!err) err = map(line_exports, "index", 1, number, 1);
3357 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3358 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3359 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3360 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
3361 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
3362 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
3366 filter_line = strdup(filter_template);
3367 if (filter_line == NULL) err = JB_ERR_MEMORY;
3369 if (!err) err = template_fill(&filter_line, line_exports);
3370 string_join(&prepared_templates[type], filter_line);
3372 free_map(line_exports);
3377 freez(filter_template);
3379 /* Replace all filter macros with the aggregated templates */
3380 for (i = 0; i < MAX_FILTER_TYPES; i++)
3383 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
3388 /* Free aggregated templates */
3389 for (i = 0; i < MAX_FILTER_TYPES; i++)
3391 freez(prepared_templates[i]);
3396 /* Check or uncheck the "disable all of this type" radio buttons. */
3397 for (i = 0; i < MAX_FILTER_TYPES; i++)
3399 const int a = filter_type_info[i].multi_action_index;
3400 const int disable_all = cur_line->data.action->multi_remove_all[a];
3402 err = map_radio(exports, filter_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
3405 edit_free_file(file);
3413 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3417 /*********************************************************************
3419 * Function : cgi_edit_actions_submit
3421 * Description : CGI function that actually edits the Actions list.
3424 * 1 : csp = Current client state (buffers, headers, etc...)
3425 * 2 : rsp = http_response data structure for output
3426 * 3 : parameters = map of cgi parameters
3428 * CGI Parameters : None
3430 * Returns : JB_ERR_OK on success
3431 * JB_ERR_MEMORY on out-of-memory
3432 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3433 * specified or not valid.
3435 *********************************************************************/
3436 jb_err cgi_edit_actions_submit(struct client_state *csp,
3437 struct http_response *rsp,
3438 const struct map *parameters)
3443 size_t newtext_size;
3445 struct editable_file * file;
3446 struct file_line * cur_line;
3447 unsigned line_number;
3450 int filter_identifier;
3452 const char * action_set_name;
3453 struct file_list * fl;
3454 struct url_actions * b;
3456 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3458 return cgi_error_disabled(csp, rsp);
3461 err = get_number_param(csp, parameters, "s", §ionid);
3467 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3470 /* No filename specified, can't read file, modified, or out of memory. */
3471 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3474 cur_line = file->lines;
3476 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3478 cur_line = cur_line->next;
3481 if ( (cur_line == NULL)
3482 || (line_number != sectionid)
3484 || (cur_line->type != FILE_LINE_ACTION))
3486 /* Invalid "sectionid" parameter */
3487 edit_free_file(file);
3488 return JB_ERR_CGI_PARAMS;
3491 get_string_param(parameters, "p", &action_set_name);
3492 if (action_set_name != NULL)
3494 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3496 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3498 for (b = b->next; NULL != b; b = b->next)
3500 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3502 copy_action(cur_line->data.action, b->action);
3508 edit_free_file(file);
3509 return JB_ERR_CGI_PARAMS;
3515 err = actions_from_radio(parameters, cur_line->data.action);
3521 edit_free_file(file);
3525 /* Check the "disable all of this type" parameters. */
3526 for (i = 0; i < MAX_FILTER_TYPES; i++)
3528 const int multi_action_index = filter_type_info[i].multi_action_index;
3529 const char ch = get_char_param(parameters, filter_type_info[i].disable_all_param);
3533 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3534 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3535 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3539 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3543 for (filter_identifier = 0; !err; filter_identifier++)
3550 * Filter state. Valid states are: 'Y' (active),
3551 * 'N' (inactive) and 'X' (no change).
3555 * Abbreviated filter type. Valid types are: 'F' (content filter),
3556 * 'S' (server-header filter) and 'C' (client-header filter).
3558 int multi_action_index = 0;
3560 /* Generate the keys */
3561 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3562 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3563 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3564 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3565 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3567 err = get_string_param(parameters, key_name, &name);
3576 type = get_char_param(parameters, key_type);
3580 multi_action_index = ACTION_MULTI_FILTER;
3583 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3586 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3589 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3592 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3595 log_error(LOG_LEVEL_ERROR,
3596 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3599 assert(multi_action_index);
3601 value = get_char_param(parameters, key_value);
3604 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3605 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3606 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3608 else if (value == 'N')
3610 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3611 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3613 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3614 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3617 else if (value == 'X')
3619 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3620 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3627 edit_free_file(file);
3631 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3634 edit_free_file(file);
3635 return JB_ERR_MEMORY;
3638 len = strlen(actiontext);
3642 * Empty action - must special-case this.
3643 * Simply setting len to 1 is sufficient...
3648 newtext_size = len + 2;
3649 if (NULL == (newtext = malloc(newtext_size)))
3653 edit_free_file(file);
3654 return JB_ERR_MEMORY;
3656 strlcpy(newtext, actiontext, newtext_size);
3660 newtext[len + 1] = '\0';
3662 freez(cur_line->raw);
3663 freez(cur_line->unprocessed);
3664 cur_line->unprocessed = newtext;
3666 err = edit_write_file(file);
3669 /* Error writing file */
3670 if (err == JB_ERR_FILE)
3672 /* Read-only file. */
3673 err = cgi_error_file_read_only(csp, rsp, file->filename);
3675 edit_free_file(file);
3679 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3680 (long) time(NULL), file->identifier, sectionid);
3682 edit_free_file(file);
3684 return cgi_redirect(rsp, target);
3688 /*********************************************************************
3690 * Function : cgi_edit_actions_url
3692 * Description : CGI function that actually edits a URL pattern in
3696 * 1 : csp = Current client state (buffers, headers, etc...)
3697 * 2 : rsp = http_response data structure for output
3698 * 3 : parameters = map of cgi parameters
3701 * filename : Identifies the file to edit
3702 * ver : File's last-modified time
3703 * section : Line number of section to edit
3704 * pattern : Line number of pattern to edit
3705 * newval : New value for pattern
3707 * Returns : JB_ERR_OK on success
3708 * JB_ERR_MEMORY on out-of-memory
3709 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3710 * specified or not valid.
3712 *********************************************************************/
3713 jb_err cgi_edit_actions_url(struct client_state *csp,
3714 struct http_response *rsp,
3715 const struct map *parameters)
3719 struct editable_file * file;
3720 struct file_line * cur_line;
3721 unsigned line_number;
3722 unsigned section_start_line_number = 0;
3730 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3732 return cgi_error_disabled(csp, rsp);
3735 err = get_number_param(csp, parameters, "p", &patternid);
3742 return JB_ERR_CGI_PARAMS;
3745 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3751 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3754 /* No filename specified, can't read file, modified, or out of memory. */
3756 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3760 cur_line = file->lines;
3762 while ((cur_line != NULL) && (line_number < patternid))
3764 if (cur_line->type == FILE_LINE_ACTION)
3766 section_start_line_number = line_number;
3768 cur_line = cur_line->next;
3772 if ( (cur_line == NULL)
3773 || (cur_line->type != FILE_LINE_URL))
3775 /* Invalid "patternid" parameter */
3777 edit_free_file(file);
3778 return JB_ERR_CGI_PARAMS;
3781 /* At this point, the line to edit is in cur_line */
3783 freez(cur_line->raw);
3784 freez(cur_line->unprocessed);
3785 cur_line->unprocessed = new_pattern;
3787 err = edit_write_file(file);
3790 /* Error writing file */
3791 if (err == JB_ERR_FILE)
3793 /* Read-only file. */
3794 err = cgi_error_file_read_only(csp, rsp, file->filename);
3796 edit_free_file(file);
3800 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3801 (long) time(NULL), file->identifier, section_start_line_number);
3803 edit_free_file(file);
3805 return cgi_redirect(rsp, target);
3809 /*********************************************************************
3811 * Function : cgi_edit_actions_add_url
3813 * Description : CGI function that actually adds a URL pattern to
3817 * 1 : csp = Current client state (buffers, headers, etc...)
3818 * 2 : rsp = http_response data structure for output
3819 * 3 : parameters = map of cgi parameters
3822 * filename : Identifies the file to edit
3823 * ver : File's last-modified time
3824 * section : Line number of section to edit
3825 * newval : New pattern
3827 * Returns : JB_ERR_OK on success
3828 * JB_ERR_MEMORY on out-of-memory
3829 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3830 * specified or not valid.
3832 *********************************************************************/
3833 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3834 struct http_response *rsp,
3835 const struct map *parameters)
3839 struct file_line * new_line;
3840 struct editable_file * file;
3841 struct file_line * cur_line;
3842 unsigned line_number;
3846 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3848 return cgi_error_disabled(csp, rsp);
3851 err = get_number_param(csp, parameters, "s", §ionid);
3858 return JB_ERR_CGI_PARAMS;
3861 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3867 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3870 /* No filename specified, can't read file, modified, or out of memory. */
3872 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3876 cur_line = file->lines;
3878 while ((cur_line != NULL) && (line_number < sectionid))
3880 cur_line = cur_line->next;
3884 if ( (cur_line == NULL)
3885 || (cur_line->type != FILE_LINE_ACTION))
3887 /* Invalid "sectionid" parameter */
3889 edit_free_file(file);
3890 return JB_ERR_CGI_PARAMS;
3893 /* At this point, the section header is in cur_line - add after this. */
3895 /* Allocate the new line */
3896 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3897 if (new_line == NULL)
3900 edit_free_file(file);
3901 return JB_ERR_MEMORY;
3904 /* Fill in the data members of the new line */
3905 new_line->raw = NULL;
3906 new_line->prefix = NULL;
3907 new_line->unprocessed = new_pattern;
3908 new_line->type = FILE_LINE_URL;
3910 /* Link new_line into the list, after cur_line */
3911 new_line->next = cur_line->next;
3912 cur_line->next = new_line;
3914 /* Done making changes, now commit */
3916 err = edit_write_file(file);
3919 /* Error writing file */
3920 if (err == JB_ERR_FILE)
3922 /* Read-only file. */
3923 err = cgi_error_file_read_only(csp, rsp, file->filename);
3925 edit_free_file(file);
3929 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3930 (long) time(NULL), file->identifier, sectionid);
3932 edit_free_file(file);
3934 return cgi_redirect(rsp, target);
3938 /*********************************************************************
3940 * Function : cgi_edit_actions_remove_url
3942 * Description : CGI function that actually removes a URL pattern from
3946 * 1 : csp = Current client state (buffers, headers, etc...)
3947 * 2 : rsp = http_response data structure for output
3948 * 3 : parameters = map of cgi parameters
3951 * f : (filename) Identifies the file to edit
3952 * v : (version) File's last-modified time
3953 * p : (pattern) Line number of pattern to remove
3955 * Returns : JB_ERR_OK on success
3956 * JB_ERR_MEMORY on out-of-memory
3957 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3958 * specified or not valid.
3960 *********************************************************************/
3961 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3962 struct http_response *rsp,
3963 const struct map *parameters)
3966 struct editable_file * file;
3967 struct file_line * cur_line;
3968 struct file_line * prev_line;
3969 unsigned line_number;
3970 unsigned section_start_line_number = 0;
3974 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3976 return cgi_error_disabled(csp, rsp);
3979 err = get_number_param(csp, parameters, "p", &patternid);
3985 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3988 /* No filename specified, can't read file, modified, or out of memory. */
3989 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3994 cur_line = file->lines;
3996 while ((cur_line != NULL) && (line_number < patternid))
3998 if (cur_line->type == FILE_LINE_ACTION)
4000 section_start_line_number = line_number;
4002 prev_line = cur_line;
4003 cur_line = cur_line->next;
4007 if ( (cur_line == NULL)
4008 || (prev_line == NULL)
4009 || (cur_line->type != FILE_LINE_URL))
4011 /* Invalid "patternid" parameter */
4012 edit_free_file(file);
4013 return JB_ERR_CGI_PARAMS;
4016 /* At this point, the line to remove is in cur_line, and the previous
4017 * one is in prev_line
4020 /* Unlink cur_line */
4021 prev_line->next = cur_line->next;
4022 cur_line->next = NULL;
4025 edit_free_file_lines(cur_line);
4027 err = edit_write_file(file);
4030 /* Error writing file */
4031 if (err == JB_ERR_FILE)
4033 /* Read-only file. */
4034 err = cgi_error_file_read_only(csp, rsp, file->filename);
4036 edit_free_file(file);
4040 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
4041 (long) time(NULL), file->identifier, section_start_line_number);
4043 edit_free_file(file);
4045 return cgi_redirect(rsp, target);
4049 /*********************************************************************
4051 * Function : cgi_edit_actions_section_remove
4053 * Description : CGI function that actually removes a whole section from
4054 * the actions file. The section must be empty first
4055 * (else JB_ERR_CGI_PARAMS).
4058 * 1 : csp = Current client state (buffers, headers, etc...)
4059 * 2 : rsp = http_response data structure for output
4060 * 3 : parameters = map of cgi parameters
4063 * f : (filename) Identifies the file to edit
4064 * v : (version) File's last-modified time
4065 * s : (section) Line number of section to edit
4067 * Returns : JB_ERR_OK on success
4068 * JB_ERR_MEMORY on out-of-memory
4069 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4070 * specified or not valid.
4072 *********************************************************************/
4073 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
4074 struct http_response *rsp,
4075 const struct map *parameters)
4078 struct editable_file * file;
4079 struct file_line * cur_line;
4080 struct file_line * prev_line;
4081 unsigned line_number;
4085 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4087 return cgi_error_disabled(csp, rsp);
4090 err = get_number_param(csp, parameters, "s", §ionid);
4096 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4099 /* No filename specified, can't read file, modified, or out of memory. */
4100 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4104 cur_line = file->lines;
4107 while ((cur_line != NULL) && (line_number < sectionid))
4109 prev_line = cur_line;
4110 cur_line = cur_line->next;
4114 if ( (cur_line == NULL)
4115 || (cur_line->type != FILE_LINE_ACTION) )
4117 /* Invalid "sectionid" parameter */
4118 edit_free_file(file);
4119 return JB_ERR_CGI_PARAMS;
4122 if ( (cur_line->next != NULL)
4123 && (cur_line->next->type == FILE_LINE_URL) )
4125 /* Section not empty. */
4126 edit_free_file(file);
4127 return JB_ERR_CGI_PARAMS;
4130 /* At this point, the line to remove is in cur_line, and the previous
4131 * one is in prev_line
4134 /* Unlink cur_line */
4135 if (prev_line == NULL)
4137 /* Removing the first line from the file */
4138 file->lines = cur_line->next;
4142 prev_line->next = cur_line->next;
4144 cur_line->next = NULL;
4147 edit_free_file_lines(cur_line);
4149 err = edit_write_file(file);
4152 /* Error writing file */
4153 if (err == JB_ERR_FILE)
4155 /* Read-only file. */
4156 err = cgi_error_file_read_only(csp, rsp, file->filename);
4158 edit_free_file(file);
4162 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4163 (long) time(NULL), file->identifier);
4165 edit_free_file(file);
4167 return cgi_redirect(rsp, target);
4171 /*********************************************************************
4173 * Function : cgi_edit_actions_section_add
4175 * Description : CGI function that adds a new empty section to
4179 * 1 : csp = Current client state (buffers, headers, etc...)
4180 * 2 : rsp = http_response data structure for output
4181 * 3 : parameters = map of cgi parameters
4184 * f : (filename) Identifies the file to edit
4185 * v : (version) File's last-modified time
4186 * s : (section) Line number of section to add after, 0 for
4189 * Returns : JB_ERR_OK on success
4190 * JB_ERR_MEMORY on out-of-memory
4191 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4192 * specified or not valid.
4194 *********************************************************************/
4195 jb_err cgi_edit_actions_section_add(struct client_state *csp,
4196 struct http_response *rsp,
4197 const struct map *parameters)
4200 struct file_line * new_line;
4202 struct editable_file * file;
4203 struct file_line * cur_line;
4204 unsigned line_number;
4208 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4210 return cgi_error_disabled(csp, rsp);
4213 err = get_number_param(csp, parameters, "s", §ionid);
4219 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4222 /* No filename specified, can't read file, modified, or out of memory. */
4223 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4227 cur_line = file->lines;
4229 if (sectionid <= 1U)
4231 /* Add to start of file */
4232 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4234 /* There's something in the file, find the line before the first
4237 while ( (cur_line->next != NULL)
4238 && (cur_line->next->type != FILE_LINE_ACTION) )
4240 cur_line = cur_line->next;
4246 /* File starts with action line, so insert at top */
4252 /* Add after stated section. */
4253 while ((cur_line != NULL) && (line_number < sectionid))
4255 cur_line = cur_line->next;
4259 if ( (cur_line == NULL)
4260 || (cur_line->type != FILE_LINE_ACTION))
4262 /* Invalid "sectionid" parameter */
4263 edit_free_file(file);
4264 return JB_ERR_CGI_PARAMS;
4267 /* Skip through the section to find the last line in it. */
4268 while ( (cur_line->next != NULL)
4269 && (cur_line->next->type != FILE_LINE_ACTION) )
4271 cur_line = cur_line->next;
4276 /* At this point, the last line in the previous section is in cur_line
4277 * - add after this. (Or if we need to add as the first line, cur_line
4281 new_text = strdup("{}");
4282 if (NULL == new_text)
4284 edit_free_file(file);
4285 return JB_ERR_MEMORY;
4288 /* Allocate the new line */
4289 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4290 if (new_line == NULL)
4293 edit_free_file(file);
4294 return JB_ERR_MEMORY;
4297 /* Fill in the data members of the new line */
4298 new_line->raw = NULL;
4299 new_line->prefix = NULL;
4300 new_line->unprocessed = new_text;
4301 new_line->type = FILE_LINE_ACTION;
4303 if (cur_line != NULL)
4305 /* Link new_line into the list, after cur_line */
4306 new_line->next = cur_line->next;
4307 cur_line->next = new_line;
4311 /* Link new_line into the list, as first line */
4312 new_line->next = file->lines;
4313 file->lines = new_line;
4316 /* Done making changes, now commit */
4318 err = edit_write_file(file);
4321 /* Error writing file */
4322 if (err == JB_ERR_FILE)
4324 /* Read-only file. */
4325 err = cgi_error_file_read_only(csp, rsp, file->filename);
4327 edit_free_file(file);
4331 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4332 (long) time(NULL), file->identifier);
4334 edit_free_file(file);
4336 return cgi_redirect(rsp, target);
4340 /*********************************************************************
4342 * Function : cgi_edit_actions_section_swap
4344 * Description : CGI function that swaps the order of two sections
4345 * in the actions file. Note that this CGI can actually
4346 * swap any two arbitrary sections, but the GUI interface
4347 * currently only allows consecutive sections to be
4351 * 1 : csp = Current client state (buffers, headers, etc...)
4352 * 2 : rsp = http_response data structure for output
4353 * 3 : parameters = map of cgi parameters
4356 * f : (filename) Identifies the file to edit
4357 * v : (version) File's last-modified time
4358 * s1 : (section1) Line number of first section to swap
4359 * s2 : (section2) Line number of second section to swap
4361 * Returns : JB_ERR_OK on success
4362 * JB_ERR_MEMORY on out-of-memory
4363 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4364 * specified or not valid.
4366 *********************************************************************/
4367 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4368 struct http_response *rsp,
4369 const struct map *parameters)
4373 struct editable_file * file;
4374 struct file_line * cur_line;
4375 struct file_line * prev_line;
4376 struct file_line * line_before_section1;
4377 struct file_line * line_start_section1;
4378 struct file_line * line_end_section1;
4379 struct file_line * line_after_section1;
4380 struct file_line * line_before_section2;
4381 struct file_line * line_start_section2;
4382 struct file_line * line_end_section2;
4383 struct file_line * line_after_section2;
4384 unsigned line_number;
4388 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4390 return cgi_error_disabled(csp, rsp);
4393 err = get_number_param(csp, parameters, "s1", §ion1);
4394 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4400 if (section1 > section2)
4402 unsigned temp = section2;
4403 section2 = section1;
4407 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4410 /* No filename specified, can't read file, modified, or out of memory. */
4411 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4414 /* Start at the beginning... */
4416 cur_line = file->lines;
4419 /* ... find section1 ... */
4420 while ((cur_line != NULL) && (line_number < section1))
4422 prev_line = cur_line;
4423 cur_line = cur_line->next;
4427 if ( (cur_line == NULL)
4428 || (cur_line->type != FILE_LINE_ACTION) )
4430 /* Invalid "section1" parameter */
4431 edit_free_file(file);
4432 return JB_ERR_CGI_PARAMS;
4435 /* If no-op, we've validated params and can skip the rest. */
4436 if (section1 != section2)
4438 /* ... find the end of section1 ... */
4439 line_before_section1 = prev_line;
4440 line_start_section1 = cur_line;
4443 prev_line = cur_line;
4444 cur_line = cur_line->next;
4447 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4448 line_end_section1 = prev_line;
4449 line_after_section1 = cur_line;
4451 /* ... find section2 ... */
4452 while ((cur_line != NULL) && (line_number < section2))
4454 prev_line = cur_line;
4455 cur_line = cur_line->next;
4459 if ( (cur_line == NULL)
4460 || (cur_line->type != FILE_LINE_ACTION) )
4462 /* Invalid "section2" parameter */
4463 edit_free_file(file);
4464 return JB_ERR_CGI_PARAMS;
4467 /* ... find the end of section2 ... */
4468 line_before_section2 = prev_line;
4469 line_start_section2 = cur_line;
4472 prev_line = cur_line;
4473 cur_line = cur_line->next;
4476 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4477 line_end_section2 = prev_line;
4478 line_after_section2 = cur_line;
4480 /* Now have all the pointers we need. Do the swap. */
4482 /* Change the pointer to section1 to point to section2 instead */
4483 if (line_before_section1 == NULL)
4485 file->lines = line_start_section2;
4489 line_before_section1->next = line_start_section2;
4492 if (line_before_section2 == line_end_section1)
4494 /* Consecutive sections */
4495 line_end_section2->next = line_start_section1;
4499 line_end_section2->next = line_after_section1;
4500 line_before_section2->next = line_start_section1;
4503 /* Set the pointer from the end of section1 to the rest of the file */
4504 line_end_section1->next = line_after_section2;
4506 err = edit_write_file(file);
4509 /* Error writing file */
4510 if (err == JB_ERR_FILE)
4512 /* Read-only file. */
4513 err = cgi_error_file_read_only(csp, rsp, file->filename);
4515 edit_free_file(file);
4518 } /* END if (section1 != section2) */
4520 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4521 (long) time(NULL), file->identifier);
4523 edit_free_file(file);
4525 return cgi_redirect(rsp, target);
4529 /*********************************************************************
4531 * Function : javascriptify
4533 * Description : Converts a string into a form JavaScript will like.
4535 * Netscape 4's JavaScript sucks - it doesn't use
4536 * "id" parameters, so you have to set the "name"
4537 * used to submit a form element to something JavaScript
4538 * will like. (Or access the elements by index in an
4539 * array. That array contains >60 elements and will
4540 * be changed whenever we add a new action to the
4541 * editor, so I'm NOT going to use indexes that have
4542 * to be figured out by hand.)
4544 * Currently the only thing we have to worry about
4545 * is "-" ==> "_" conversion.
4547 * This is a length-preserving operation so it is
4548 * carried out in-place, no memory is allocated
4552 * 1 : identifier = String to make JavaScript-friendly.
4556 *********************************************************************/
4557 static void javascriptify(char * identifier)
4559 char * p = identifier;
4560 while (NULL != (p = strchr(p, '-')))
4567 /*********************************************************************
4569 * Function : actions_to_radio
4571 * Description : Converts a actionsfile entry into settings for
4572 * radio buttons and edit boxes on a HTML form.
4575 * 1 : exports = List of substitutions to add to.
4576 * 2 : action = Action to read
4578 * Returns : JB_ERR_OK on success
4579 * JB_ERR_MEMORY on out-of-memory
4581 *********************************************************************/
4582 static jb_err actions_to_radio(struct map * exports,
4583 const struct action_spec *action)
4594 mask = action->mask;
4597 /* sanity - prevents "-feature +feature" */
4601 #define DEFINE_ACTION_BOOL(name, bit) \
4602 if (!(mask & bit)) \
4604 current_mode = 'n'; \
4606 else if (add & bit) \
4608 current_mode = 'y'; \
4612 current_mode = 'x'; \
4614 if (map_radio(exports, name, "ynx", current_mode)) \
4616 return JB_ERR_MEMORY; \
4619 #define DEFINE_ACTION_STRING(name, bit, index) \
4620 DEFINE_ACTION_BOOL(name, bit); \
4623 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4626 checked = !strcmp(action->string[index], value); \
4630 checked = is_default; \
4632 mapped_param |= checked; \
4633 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4635 return JB_ERR_MEMORY; \
4638 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4639 if (map(exports, name "-param-custom", 1, \
4640 ((!mapped_param) ? "checked" : ""), 1)) \
4642 return JB_ERR_MEMORY; \
4644 if (map(exports, name "-param", 1, \
4645 (((add & bit) && !mapped_param) ? \
4646 action->string[index] : default_val), 1)) \
4648 return JB_ERR_MEMORY; \
4651 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4652 if (map(exports, name "-param", 1, \
4653 ((add & bit) ? action->string[index] : default_val), 1)) \
4655 return JB_ERR_MEMORY; \
4658 #define DEFINE_ACTION_MULTI(name, index) \
4659 if (action->multi_add[index]->first) \
4661 current_mode = 'y'; \
4663 else if (action->multi_remove_all[index]) \
4665 current_mode = 'n'; \
4667 else if (action->multi_remove[index]->first) \
4669 current_mode = 'y'; \
4673 current_mode = 'x'; \
4675 if (map_radio(exports, name, "ynx", current_mode)) \
4677 return JB_ERR_MEMORY; \
4680 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4682 #include "actionlist.h"
4684 #undef DEFINE_ACTION_MULTI
4685 #undef DEFINE_ACTION_STRING
4686 #undef DEFINE_ACTION_BOOL
4687 #undef DEFINE_ACTION_ALIAS
4688 #undef DEFINE_CGI_PARAM_CUSTOM
4689 #undef DEFINE_CGI_PARAM_RADIO
4690 #undef DEFINE_CGI_PARAM_NO_RADIO
4696 /*********************************************************************
4698 * Function : actions_from_radio
4700 * Description : Converts a map of parameters passed to a CGI function
4701 * into an actionsfile entry.
4704 * 1 : parameters = parameters to the CGI call
4705 * 2 : action = Action to change. Must be valid before
4706 * the call, actions not specified will be
4709 * Returns : JB_ERR_OK on success
4710 * JB_ERR_MEMORY on out-of-memory
4712 *********************************************************************/
4713 static jb_err actions_from_radio(const struct map * parameters,
4714 struct action_spec *action)
4719 const char * js_name;
4720 jb_err err = JB_ERR_OK;
4725 /* Statics are generally a potential race condition,
4726 * but in this case we're safe and don't need semaphores.
4727 * Be careful if you modify this function.
4729 * The js_name_arr's are never free()d, but this is no
4730 * problem, since they will only be created once and
4731 * used by all threads thereafter. -oes
4734 #define JAVASCRIPTIFY(dest_var, string) \
4736 static int first_time = 1; \
4737 static char *js_name_arr; \
4740 js_name_arr = strdup(string); \
4741 javascriptify(js_name_arr); \
4743 dest_var = js_name_arr; \
4747 #define DEFINE_ACTION_BOOL(name, bit) \
4748 JAVASCRIPTIFY(js_name, name); \
4749 ch = get_char_param(parameters, js_name); \
4752 action->add |= bit; \
4753 action->mask |= bit; \
4755 else if (ch == 'N') \
4757 action->add &= ~bit; \
4758 action->mask &= ~bit; \
4760 else if (ch == 'X') \
4762 action->add &= ~bit; \
4763 action->mask |= bit; \
4766 #define DEFINE_ACTION_STRING(name, bit, index) \
4767 JAVASCRIPTIFY(js_name, name); \
4768 ch = get_char_param(parameters, js_name); \
4772 JAVASCRIPTIFY(js_name, name "-mode"); \
4773 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4774 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4776 JAVASCRIPTIFY(js_name, name "-param"); \
4777 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4779 if (param != NULL) \
4781 if (NULL == (param_dup = strdup(param))) \
4783 return JB_ERR_MEMORY; \
4785 freez(action->string[index]); \
4786 action->add |= bit; \
4787 action->mask |= bit; \
4788 action->string[index] = param_dup; \
4791 else if (ch == 'N') \
4793 if (action->add & bit) \
4795 freez(action->string[index]); \
4797 action->add &= ~bit; \
4798 action->mask &= ~bit; \
4800 else if (ch == 'X') \
4802 if (action->add & bit) \
4804 freez(action->string[index]); \
4806 action->add &= ~bit; \
4807 action->mask |= bit; \
4810 #define DEFINE_ACTION_MULTI(name, index) \
4811 JAVASCRIPTIFY(js_name, name); \
4812 ch = get_char_param(parameters, js_name); \
4817 else if (ch == 'N') \
4819 list_remove_all(action->multi_add[index]); \
4820 list_remove_all(action->multi_remove[index]); \
4821 action->multi_remove_all[index] = 1; \
4823 else if (ch == 'X') \
4825 list_remove_all(action->multi_add[index]); \
4826 list_remove_all(action->multi_remove[index]); \
4827 action->multi_remove_all[index] = 0; \
4830 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4832 #include "actionlist.h"
4834 #undef DEFINE_ACTION_MULTI
4835 #undef DEFINE_ACTION_STRING
4836 #undef DEFINE_ACTION_BOOL
4837 #undef DEFINE_ACTION_ALIAS
4838 #undef JAVASCRIPTIFY
4842 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4845 #ifdef FEATURE_TOGGLE
4846 /*********************************************************************
4848 * Function : cgi_toggle
4850 * Description : CGI function that adds a new empty section to
4854 * 1 : csp = Current client state (buffers, headers, etc...)
4855 * 2 : rsp = http_response data structure for output
4856 * 3 : parameters = map of cgi parameters
4859 * set : If present, how to change toggle setting:
4860 * "enable", "disable", "toggle", or none (default).
4861 * mini : If present, use mini reply template.
4863 * Returns : JB_ERR_OK on success
4864 * JB_ERR_MEMORY on out-of-memory
4866 *********************************************************************/
4867 jb_err cgi_toggle(struct client_state *csp,
4868 struct http_response *rsp,
4869 const struct map *parameters)
4871 struct map *exports;
4873 const char *template_name;
4879 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4881 return cgi_error_disabled(csp, rsp);
4884 mode = get_char_param(parameters, "set");
4889 global_toggle_state = 1;
4891 else if (mode == 'D')
4894 global_toggle_state = 0;
4896 else if (mode == 'T')
4899 global_toggle_state = !global_toggle_state;
4902 if (NULL == (exports = default_exports(csp, "toggle")))
4904 return JB_ERR_MEMORY;
4907 template_name = (get_char_param(parameters, "mini")
4911 return template_fill_for_cgi(csp, template_name, exports, rsp);
4913 #endif /* def FEATURE_TOGGLE */