1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.61 2008/03/24 18:12:52 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.61 2008/03/24 18:12:52 fabiankeil
46 * Use sizeof() more often.
48 * Revision 1.60 2008/03/15 14:52:35 fabiankeil
49 * Add CGI editor support for the "disable all filters of this type"
50 * directives "-client-header-filter", "-server-header-filter",
51 * "-client-header-tagger" and "-server-header-tagger".
53 * Revision 1.59 2008/03/08 16:25:56 fabiankeil
54 * After three file modification time mismatches, turn the CGI editor off.
56 * Revision 1.58 2007/11/28 17:57:01 fabiankeil
57 * Fix double free in cgi_edit_actions_list().
58 * Reported by adlab in BR#1840145.
60 * Revision 1.57 2007/10/27 13:32:23 fabiankeil
61 * Plug minor 5-year-old memory leak. Spotted by
62 * Valgrind and triggered by Privoxy-Regression-Test.
64 * Revision 1.56 2007/08/05 13:47:03 fabiankeil
65 * #1763173 from Stefan Huehner: s@const static@static const@.
67 * Revision 1.55 2007/05/31 11:50:20 fabiankeil
68 * Re-enable support for old-school URLs like
69 * http://config.privoxy.org/edit-actions-list?f=default
70 * in the action editor.
72 * They are no longer used by the CGI pages, but make it easier
73 * to reach the editor directly, without knowing the requested
74 * file's index in csp->config->actions_file[].
76 * Revision 1.54 2007/05/14 10:33:51 fabiankeil
77 * - Use strlcpy() and strlcat() instead of strcpy() and strcat().
79 * Revision 1.53 2007/04/15 16:39:20 fabiankeil
80 * Introduce tags as alternative way to specify which
81 * actions apply to a request. At the moment tags can be
82 * created based on client and server headers.
84 * Revision 1.52 2007/04/12 10:41:23 fabiankeil
85 * - Don't mistake VC++'s _snprintf() for a snprintf() replacement.
86 * - Move some cgi_edit_actions_for_url() variables into structs.
87 * - Remove bogus comment.
89 * Revision 1.51 2007/04/08 13:21:05 fabiankeil
90 * Reference action files in CGI URLs by id instead
91 * of using the first part of the file name.
92 * Fixes BR 1694250 and BR 1590556.
94 * Revision 1.50 2007/03/29 11:40:34 fabiankeil
95 * Divide @filter-params@ into @client-header-filter-params@
96 * @content-filter-params@ and @server-header-filter-params@.
98 * Revision 1.49 2007/03/20 15:16:34 fabiankeil
99 * Use dedicated header filter actions instead of abusing "filter".
100 * Replace "filter-client-headers" and "filter-client-headers"
101 * with "server-header-filter" and "client-header-filter".
103 * Revision 1.48 2007/02/13 14:35:25 fabiankeil
104 * Replace hash escaping code to prevent
105 * crashes, memory and file corruption.
107 * Revision 1.47 2006/12/28 18:04:25 fabiankeil
108 * Fixed gcc43 conversion warnings.
110 * Revision 1.46 2006/12/27 18:44:52 fabiankeil
111 * Stop shadowing string.h's index().
113 * Revision 1.45 2006/12/21 12:57:48 fabiankeil
114 * Add config option "split-large-forms"
115 * to work around the browser bug reported
118 * Revision 1.44 2006/12/09 13:49:16 fabiankeil
119 * Fix configure option --disable-toggle.
120 * Thanks to Peter Thoenen for reporting this.
122 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
123 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
124 * with what was really the latest development (the v_3_0_branch branch)
126 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
127 * Remove a little residual debugging info
129 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
130 * Multiple filter file support
132 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
133 * Undo some damaging memory leak patches
135 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
136 * Removing a double free
138 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
139 * A few memory leaks plugged (mostly on error paths)
141 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
142 * Moved cgi_error_disabled() from cgiedit.c to
143 * cgi.c to re-enable build with --disable-editor.
144 * Fixes Bug #892744. Thanks to Matthew Fischer
147 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
148 * One line lost in last commit
150 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
151 * - All edit functions that redirect back to the list page
152 * now use cgi_redirect
153 * - All redirects now contain useless parameter "foo", whose
154 * value are raw seconds since epoch, in order to force
155 * Opera and Konqueror to properly reload the list. Closes
158 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
159 * Cosmetic: Renamed cryptic variable
161 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
162 * Fix: Don't free uninitialized struct editable_file
164 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
165 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
167 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
168 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
170 * Revision 1.41 2002/05/21 19:09:45 oes
171 * - Made Add/Edit/Remove URL Submit and Cancel
172 * buttons jump back to relevant section in eal
173 * - Bugfix: remove-url-form needs p export
175 * Revision 1.40 2002/05/19 11:34:35 jongfoster
176 * Handling read-only actions files better - report the actual
177 * error, not "Out of memory"!
180 * http://sourceforge.net/tracker/index.php?func=detail
181 * &aid=557905&group_id=11118&atid=111118
183 * Revision 1.39 2002/05/12 21:39:15 jongfoster
184 * - Adding Doxygen-style comments to structures and #defines.
185 * - Correcting function comments
187 * Revision 1.38 2002/05/03 23:00:38 jongfoster
188 * Support for templates for "standard actions" buttons.
191 * Revision 1.37 2002/04/30 11:14:52 oes
192 * Made csp the first parameter in *action_to_html
194 * Revision 1.36 2002/04/26 21:53:30 jongfoster
195 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
197 * Revision 1.35 2002/04/26 21:50:02 jongfoster
198 * Honouring default exports in edit-actions-for-url-filter template.
200 * Revision 1.34 2002/04/26 12:54:17 oes
201 * Adaptions to changes in actions.c
203 * Revision 1.33 2002/04/24 02:17:47 oes
204 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
206 * - Activated Jon's code for editing multiple AFs
207 * - cgi_edit_list_actions now provides context-sensitive
208 * help, looks up all action sets from standard.action and
209 * makes buttons for them in the catchall section
210 * - cgi_edit_action_submit now honors a p parameter, looks up
211 * the corresponding action set, and sets the catchall pattern's
212 * actions accordingly.
214 * Revision 1.32 2002/04/19 16:55:31 jongfoster
215 * Fixing newline problems. If we do our own text file newline
216 * mangling, we don't want the library to do any, so we need to
217 * open the files in *binary* mode.
219 * Revision 1.31 2002/04/18 19:21:08 jongfoster
220 * Added code to detect "conventional" action files, that start
221 * with a set of actions for all URLs (the pattern "/").
222 * These are special-cased in the "edit-actions-list" CGI, so
223 * that a special UI can be written for them.
225 * Revision 1.30 2002/04/10 13:38:35 oes
226 * load_template signature changed
228 * Revision 1.29 2002/04/08 16:59:08 oes
231 * Revision 1.28 2002/03/27 12:30:29 oes
232 * Deleted unsused variable
234 * Revision 1.27 2002/03/26 23:06:04 jongfoster
235 * Removing duplicate @ifs on the toggle page
237 * Revision 1.26 2002/03/26 22:59:17 jongfoster
238 * Fixing /toggle to display status consistently.
240 * Revision 1.25 2002/03/26 22:29:54 swa
241 * we have a new homepage!
243 * Revision 1.24 2002/03/24 15:23:33 jongfoster
246 * Revision 1.23 2002/03/24 13:32:41 swa
247 * name change related issues
249 * Revision 1.22 2002/03/24 13:25:43 swa
250 * name change related issues
252 * Revision 1.21 2002/03/22 18:02:48 jongfoster
253 * Fixing remote toggle
255 * Revision 1.20 2002/03/16 20:28:34 oes
256 * Added descriptions to the filters so users will know what they select in the cgi editor
258 * Revision 1.19 2002/03/16 18:38:14 jongfoster
259 * Stopping stupid or malicious users from breaking the actions
260 * file using the web-based editor.
262 * Revision 1.18 2002/03/16 14:57:44 jongfoster
263 * Full support for enabling/disabling modular filters.
265 * Revision 1.17 2002/03/16 14:26:42 jongfoster
266 * First version of modular filters support - READ ONLY!
267 * Fixing a double-free bug in the out-of-memory handling in map_radio().
269 * Revision 1.16 2002/03/07 03:46:17 oes
270 * Fixed compiler warnings
272 * Revision 1.15 2002/03/06 22:54:35 jongfoster
273 * Automated function-comment nitpicking.
275 * Revision 1.14 2002/03/05 00:24:51 jongfoster
276 * Patch to always edit the current actions file.
278 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
279 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
281 * Revision 1.12 2002/03/03 09:18:03 joergs
282 * Made jumbjuster work on AmigaOS again.
284 * Revision 1.11 2002/01/23 01:03:31 jongfoster
285 * Fixing gcc [CygWin] compiler warnings
287 * Revision 1.10 2002/01/23 00:22:59 jongfoster
288 * Adding new function cgi_edit_actions_section_swap(), to reorder
291 * Adding get_url_spec_param() to get a validated URL pattern.
293 * Moving edit_read_line() out of this file and into loaders.c.
295 * Adding missing html_encode() to many CGI functions.
297 * Moving the functions that #include actionlist.h to the end of the file,
298 * because the Visual C++ 97 debugger gets extremely confused if you try
299 * to debug any code that comes after them in the file.
301 * Major optimizations in cgi_edit_actions_list() to reduce the size of
302 * the generated HTML (down 40% from 550k to 304k), with major side-effects
303 * throughout the editor and templates. In particular, the length of the
304 * URLs throughout the editor has been drastically reduced, by cutting
305 * paramater names down to 1 character and CGI names down to 3-4
306 * characters, by removing all non-essential CGI paramaters even at the
307 * expense of having to re-read the actions file for the most trivial
308 * page, and by using relative rather than absolute URLs. This means
309 * that this (typical example):
311 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
312 * filename=ijb&ver=1011487572&section=12&pattern=13
313 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
317 * <a href="eau?f=ijb&v=1011487572&p=13">
319 * Revision 1.9 2002/01/17 20:56:22 jongfoster
320 * Replacing hard references to the URL of the config interface
321 * with #defines from project.h
323 * Revision 1.8 2001/11/30 23:35:51 jongfoster
324 * Renaming actionsfile to ijb.action
326 * Revision 1.7 2001/11/13 00:28:24 jongfoster
327 * - Renaming parameters from edit-actions-for-url so that they only
328 * contain legal JavaScript characters. If we wanted to write
329 * JavaScript that worked with Netscape 4, this is nessacery.
330 * (Note that at the moment the JavaScript doesn't actually work
331 * with Netscape 4, but now this is purely a template issue, not
332 * one affecting code).
333 * - Adding new CGIs for use by non-JavaScript browsers:
334 * edit-actions-url-form
335 * edit-actions-add-url-form
336 * edit-actions-remove-url-form
339 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
340 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
341 * by and __OS2__ ifdef.
343 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
344 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
345 * threads to call select() simultaneously. So, it's time to do a real, live,
346 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
347 * (native). Both versions will work, but using __OS2__ offers multi-threading.
349 * Revision 1.4 2001/10/23 21:48:19 jongfoster
350 * Cleaning up error handling in CGI functions - they now send back
351 * a HTML error page and should never cause a FATAL error. (Fixes one
352 * potential source of "denial of service" attacks).
354 * CGI actions file editor that works and is actually useful.
356 * Ability to toggle JunkBuster remotely using a CGI call.
358 * You can turn off both the above features in the main configuration
359 * file, e.g. if you are running a multi-user proxy.
361 * Revision 1.3 2001/10/14 22:12:49 jongfoster
362 * New version of CGI-based actionsfile editor.
363 * Major changes, including:
364 * - Completely new file parser and file output routines
365 * - edit-actions CGI renamed edit-actions-for-url
366 * - All CGIs now need a filename parameter, except for...
367 * - New CGI edit-actions which doesn't need a filename,
368 * to allow you to start the editor up.
369 * - edit-actions-submit now works, and now automatically
370 * redirects you back to the main edit-actions-list handler.
372 * Revision 1.2 2001/09/16 17:05:14 jongfoster
373 * Removing unused #include showarg.h
375 * Revision 1.1 2001/09/16 15:47:37 jongfoster
376 * First version of CGI-based edit interface. This is very much a
377 * work-in-progress, and you can't actually use it to edit anything
378 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
379 * to have any effect.
382 **********************************************************************/
388 * FIXME: Following includes copied from cgi.c - which are actually needed?
393 #include <sys/types.h>
397 #include <sys/stat.h>
402 #include "cgisimple.h"
406 #include "miscutil.h"
409 #ifdef FEATURE_TOGGLE
410 /* loadcfg.h is for global_toggle_state only */
412 #endif /* def FEATURE_TOGGLE */
413 #include "urlmatch.h"
415 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
418 #ifdef FEATURE_CGI_EDIT_ACTIONS
421 * A line in an editable_file.
425 /** Next entry in the linked list */
426 struct file_line * next;
428 /** The raw data, to write out if this line is unmodified. */
431 /** Comments and/or whitespace to put before this line if it's modified
432 and then written out. */
435 /** The actual data, as a string. Line continuation and comment removal
436 are performed on the data read from file before it's stored here, so
437 it will be a single line of data. */
440 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
443 /** The actual data, processed into some sensible data type. */
447 /** An action specification. */
448 struct action_spec action[1];
450 /** A name=value pair. */
454 /** The name in the name=value pair. */
457 /** The value in the name=value pair, as a string. */
460 /** The value in the name=value pair, as an integer. */
465 /* Add more data types here... e.g.
468 struct url_spec url[1];
472 struct action_spec action[1];
482 /** This file_line has not been processed yet. */
483 #define FILE_LINE_UNPROCESSED 1
485 /** This file_line is blank. Can only appear at the end of a file, due to
486 the way the parser works. */
487 #define FILE_LINE_BLANK 2
489 /** This file_line says {{alias}}. */
490 #define FILE_LINE_ALIAS_HEADER 3
492 /** This file_line defines an alias. */
493 #define FILE_LINE_ALIAS_ENTRY 4
495 /** This file_line defines an {action}. */
496 #define FILE_LINE_ACTION 5
498 /** This file_line specifies a URL pattern. */
499 #define FILE_LINE_URL 6
501 /** This file_line says {{settings}}. */
502 #define FILE_LINE_SETTINGS_HEADER 7
504 /** This file_line is in a {{settings}} block. */
505 #define FILE_LINE_SETTINGS_ENTRY 8
507 /** This file_line says {{description}}. */
508 #define FILE_LINE_DESCRIPTION_HEADER 9
510 /** This file_line is in a {{description}} block. */
511 #define FILE_LINE_DESCRIPTION_ENTRY 10
514 * Number of file modification time mismatches
515 * before the CGI editor gets turned off.
517 #define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
520 * A configuration file, in a format that can be edited and written back to
525 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
526 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
527 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
528 const char * version_str; /**< Last modification time, as a string. For CGI param. */
529 /**< Can be used in URL without using url_param(). */
530 unsigned version; /**< Last modification time - prevents chaos with
531 the browser's "back" button. Note that this is a
532 time_t cast to an unsigned. When comparing, always
533 cast the time_t to an unsigned, and *NOT* vice-versa.
534 This may lose the top few bits, but they're not
535 significant anyway. */
536 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
537 Note that changing this after the file has been
538 read in will cause a mess. */
539 struct file_line * parse_error; /**< On parse error, this is the offending line. */
540 const char * parse_error_text; /**< On parse error, this is the problem.
541 (Statically allocated) */
545 * Information about the filter types.
546 * Used for macro replacement in cgi_edit_actions_for_url.
548 struct filter_type_info
550 const int multi_action_index; /**< The multi action index as defined in project.h */
551 const char *macro_name; /**< Name of the macro that has to be replaced
552 with the prepared templates.
553 For example "content-filter-params" */
554 const char *type; /**< Name of the filter type,
555 for example "server-header-filter". */
556 /* XXX: check if these two can be combined. */
557 const char *disable_all_option; /**< Name of the catch-all radio option that has
558 to be checked or unchecked for this filter type. */
559 const char *disable_all_param; /**< Name of the parameter that causes all filters of
560 this type to be disabled. */
561 const char *abbr_type; /**< Abbreviation of the filter type, usually the
562 first or second character capitalized */
563 const char *anchor; /**< Anchor for the User Manual link,
564 for example "SERVER-HEADER-FILTER" */
567 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
568 static const struct filter_type_info filter_type_info[] =
572 "content-filter-params", "filter",
573 "filter-all", "filter_all",
577 ACTION_MULTI_CLIENT_HEADER_FILTER,
578 "client-header-filter-params", "client-header-filter",
579 "client-header-filter-all", "client_header_filter_all",
580 "C", "CLIENT-HEADER-FILTER"
583 ACTION_MULTI_SERVER_HEADER_FILTER,
584 "server-header-filter-params", "server-header-filter",
585 "server-header-filter-all", "server_header_filter_all",
586 "S", "SERVER-HEADER-FILTER"
589 ACTION_MULTI_CLIENT_HEADER_TAGGER,
590 "client-header-tagger-params", "client-header-tagger",
591 "client-header-tagger-all", "client_header_tagger_all",
592 "L", "CLIENT-HEADER-TAGGER"
595 ACTION_MULTI_SERVER_HEADER_TAGGER,
596 "server-header-tagger-params", "server-header-tagger",
597 "server-header-tagger-all", "server_header_tagger_all",
598 "E", "SERVER-HEADER-TAGGER"
602 /* FIXME: Following non-static functions should be prototyped in .h or made static */
604 /* Functions to read and write arbitrary config files */
605 jb_err edit_read_file(struct client_state *csp,
606 const struct map *parameters,
608 struct editable_file **pfile);
609 jb_err edit_write_file(struct editable_file * file);
610 void edit_free_file(struct editable_file * file);
612 /* Functions to read and write actions files */
613 jb_err edit_parse_actions_file(struct editable_file * file);
614 jb_err edit_read_actions_file(struct client_state *csp,
615 struct http_response *rsp,
616 const struct map *parameters,
618 struct editable_file **pfile);
621 jb_err cgi_error_modified(struct client_state *csp,
622 struct http_response *rsp,
623 const char *filename);
624 jb_err cgi_error_parse(struct client_state *csp,
625 struct http_response *rsp,
626 struct editable_file *file);
627 jb_err cgi_error_file(struct client_state *csp,
628 struct http_response *rsp,
629 const char *filename);
630 jb_err cgi_error_file_read_only(struct client_state *csp,
631 struct http_response *rsp,
632 const char *filename);
634 /* Internal arbitrary config file support functions */
635 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
636 static void edit_free_file_lines(struct file_line * first_line);
638 /* Internal actions file support functions */
639 static int match_actions_file_header_line(const char * line, const char * name);
640 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
642 /* Internal parameter parsing functions */
643 static jb_err get_url_spec_param(struct client_state *csp,
644 const struct map *parameters,
649 /* Internal actionsfile <==> HTML conversion functions */
650 static jb_err map_radio(struct map * exports,
651 const char * optionname,
654 static jb_err actions_to_radio(struct map * exports,
655 const struct action_spec *action);
656 static jb_err actions_from_radio(const struct map * parameters,
657 struct action_spec *action);
660 static jb_err map_copy_parameter_html(struct map *out,
661 const struct map *in,
663 #if 0 /* unused function */
664 static jb_err map_copy_parameter_url(struct map *out,
665 const struct map *in,
667 #endif /* unused function */
669 static jb_err get_file_name_param(struct client_state *csp,
670 const struct map *parameters,
671 const char *param_name,
672 const char **pfilename);
674 /* Internal convenience functions */
675 static char *section_target(const unsigned sectionid);
677 /*********************************************************************
679 * Function : section_target
681 * Description : Given an unsigned (section id) n, produce a dynamically
682 * allocated string of the form #l<n>, for use in link
685 * XXX: The hash should be moved into the templates
686 * to make this function more generic and render
687 * stringify() obsolete.
690 * 1 : sectionid = start line number of section
692 * Returns : String with link target, or NULL if out of
695 *********************************************************************/
696 static char *section_target(const unsigned sectionid)
700 snprintf(buf, sizeof(buf), "#l%d", sectionid);
706 /*********************************************************************
708 * Function : stringify
710 * Description : Convert a number into a dynamically allocated string.
713 * 1 : number = The number to convert.
715 * Returns : String with link target, or NULL if out of memory
717 *********************************************************************/
718 static char *stringify(const unsigned number)
722 snprintf(buf, sizeof(buf), "%i", number);
727 /*********************************************************************
729 * Function : map_copy_parameter_html
731 * Description : Copy a CGI parameter from one map to another, HTML
735 * 1 : out = target map
736 * 2 : in = source map
737 * 3 : name = name of cgi parameter to copy
739 * Returns : JB_ERR_OK on success
740 * JB_ERR_MEMORY on out-of-memory
741 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
744 *********************************************************************/
745 static jb_err map_copy_parameter_html(struct map *out,
746 const struct map *in,
756 value = lookup(in, name);
757 err = map(out, name, 1, html_encode(value), 0);
764 else if (*value == '\0')
766 return JB_ERR_CGI_PARAMS;
775 #if 0 /* unused function */
776 /*********************************************************************
778 * Function : map_copy_parameter_url
780 * Description : Copy a CGI parameter from one map to another, URL
784 * 1 : out = target map
785 * 2 : in = source map
786 * 3 : name = name of cgi parameter to copy
788 * Returns : JB_ERR_OK on success
789 * JB_ERR_MEMORY on out-of-memory
790 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
793 *********************************************************************/
794 static jb_err map_copy_parameter_url(struct map *out,
795 const struct map *in,
805 value = lookup(in, name);
806 err = map(out, name, 1, url_encode(value), 0);
813 else if (*value == '\0')
815 return JB_ERR_CGI_PARAMS;
822 #endif /* 0 - unused function */
825 /*********************************************************************
827 * Function : cgi_edit_actions_url_form
829 * Description : CGI function that displays a form for
833 * 1 : csp = Current client state (buffers, headers, etc...)
834 * 2 : rsp = http_response data structure for output
835 * 3 : parameters = map of cgi parameters
838 * i : (action index) Identifies the file to edit
839 * v : (version) File's last-modified time
840 * p : (pattern) Line number of pattern to edit
842 * Returns : JB_ERR_OK on success
843 * JB_ERR_MEMORY on out-of-memory
844 * JB_ERR_CGI_PARAMS if the CGI parameters are not
845 * specified or not valid.
847 *********************************************************************/
848 jb_err cgi_edit_actions_url_form(struct client_state *csp,
849 struct http_response *rsp,
850 const struct map *parameters)
852 struct map * exports;
854 struct editable_file * file;
855 struct file_line * cur_line;
856 unsigned line_number;
857 unsigned section_start_line_number = 0;
864 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
866 return cgi_error_disabled(csp, rsp);
869 err = get_number_param(csp, parameters, "p", &patternid);
875 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
878 /* No filename specified, can't read file, modified, or out of memory. */
879 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
882 cur_line = file->lines;
884 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
886 if (cur_line->type == FILE_LINE_ACTION)
888 section_start_line_number = line_number;
890 cur_line = cur_line->next;
893 if ( (cur_line == NULL)
894 || (line_number != patternid)
896 || (cur_line->type != FILE_LINE_URL))
898 /* Invalid "patternid" parameter */
899 edit_free_file(file);
900 return JB_ERR_CGI_PARAMS;
903 if (NULL == (exports = default_exports(csp, NULL)))
905 edit_free_file(file);
906 return JB_ERR_MEMORY;
909 err = map(exports, "f", 1, stringify(file->identifier), 0);
910 if (!err) err = map(exports, "v", 1, file->version_str, 1);
911 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
912 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
913 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
915 edit_free_file(file);
923 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
927 /*********************************************************************
929 * Function : cgi_edit_actions_add_url_form
931 * Description : CGI function that displays a form for
935 * 1 : csp = Current client state (buffers, headers, etc...)
936 * 2 : rsp = http_response data structure for output
937 * 3 : parameters = map of cgi parameters
940 * f : (filename) Identifies the file to edit
941 * v : (version) File's last-modified time
942 * s : (section) Line number of section to edit
944 * Returns : JB_ERR_OK on success
945 * JB_ERR_MEMORY on out-of-memory
946 * JB_ERR_CGI_PARAMS if the CGI parameters are not
947 * specified or not valid.
949 *********************************************************************/
950 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
951 struct http_response *rsp,
952 const struct map *parameters)
961 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
963 return cgi_error_disabled(csp, rsp);
966 if (NULL == (exports = default_exports(csp, NULL)))
968 return JB_ERR_MEMORY;
971 err = map_copy_parameter_html(exports, parameters, "f");
972 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
973 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
981 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
985 /*********************************************************************
987 * Function : cgi_edit_actions_remove_url_form
989 * Description : CGI function that displays a form for
993 * 1 : csp = Current client state (buffers, headers, etc...)
994 * 2 : rsp = http_response data structure for output
995 * 3 : parameters = map of cgi parameters
998 * f : (number) The action file identifier.
999 * v : (version) File's last-modified time
1000 * p : (pattern) Line number of pattern to edit
1002 * Returns : JB_ERR_OK on success
1003 * JB_ERR_MEMORY on out-of-memory
1004 * JB_ERR_CGI_PARAMS if the CGI parameters are not
1005 * specified or not valid.
1007 *********************************************************************/
1008 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
1009 struct http_response *rsp,
1010 const struct map *parameters)
1012 struct map * exports;
1014 struct editable_file * file;
1015 struct file_line * cur_line;
1016 unsigned line_number;
1017 unsigned section_start_line_number = 0;
1024 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1026 return cgi_error_disabled(csp, rsp);
1029 err = get_number_param(csp, parameters, "p", &patternid);
1035 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
1038 /* No filename specified, can't read file, modified, or out of memory. */
1039 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
1042 cur_line = file->lines;
1044 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
1046 if (cur_line->type == FILE_LINE_ACTION)
1048 section_start_line_number = line_number;
1050 cur_line = cur_line->next;
1053 if ( (cur_line == NULL)
1054 || (line_number != patternid)
1056 || (cur_line->type != FILE_LINE_URL))
1058 /* Invalid "patternid" parameter */
1059 edit_free_file(file);
1060 return JB_ERR_CGI_PARAMS;
1063 if (NULL == (exports = default_exports(csp, NULL)))
1065 edit_free_file(file);
1066 return JB_ERR_MEMORY;
1069 err = map(exports, "f", 1, stringify(file->identifier), 0);
1070 if (!err) err = map(exports, "v", 1, file->version_str, 1);
1071 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
1072 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
1073 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
1074 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
1076 edit_free_file(file);
1084 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
1088 /*********************************************************************
1090 * Function : edit_write_file
1092 * Description : Write a complete file to disk.
1095 * 1 : file = File to write.
1097 * Returns : JB_ERR_OK on success
1098 * JB_ERR_FILE on error writing to file.
1099 * JB_ERR_MEMORY on out of memory
1101 *********************************************************************/
1102 jb_err edit_write_file(struct editable_file * file)
1105 struct file_line * cur_line;
1106 struct stat statbuf[1];
1107 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
1108 digits in time_t, assuming this is a 64-bit
1109 machine, plus null terminator, plus one
1113 assert(file->filename);
1115 if (NULL == (fp = fopen(file->filename, "wb")))
1120 cur_line = file->lines;
1121 while (cur_line != NULL)
1125 if (fputs(cur_line->raw, fp) < 0)
1133 if (cur_line->prefix)
1135 if (fputs(cur_line->prefix, fp) < 0)
1141 if (cur_line->unprocessed)
1144 if (NULL != strchr(cur_line->unprocessed, '#'))
1146 /* Must quote '#' characters */
1153 /* Count number of # characters, so we know length of output string */
1154 src = cur_line->unprocessed;
1155 while (NULL != (src = strchr(src, '#')))
1160 assert(numhash > 0);
1162 /* Allocate new memory for string */
1163 len = strlen(cur_line->unprocessed) + (size_t)numhash;
1164 if (NULL == (str = malloc(len + 1)))
1166 /* Uh oh, just trashed file! */
1168 return JB_ERR_MEMORY;
1171 /* Copy string but quote hashes */
1172 src = cur_line->unprocessed;
1180 assert(numhash >= 0);
1186 assert(numhash == 0);
1187 assert(strlen(str) == len);
1188 assert(str == dest - len);
1189 assert(src - len <= cur_line->unprocessed);
1191 if ((strlen(str) != len) || (numhash != 0))
1194 * Escaping didn't work as expected, go spread the news.
1195 * Only reached in non-debugging builds.
1197 log_error(LOG_LEVEL_ERROR,
1198 "Looks like hash escaping failed. %s might be corrupted now.",
1202 if (fputs(str, fp) < 0)
1213 /* Can write without quoting '#' characters. */
1214 if (fputs(cur_line->unprocessed, fp) < 0)
1220 if (fputs(NEWLINE(file->newline), fp) < 0)
1228 /* FIXME: Write data from file->data->whatever */
1232 cur_line = cur_line->next;
1238 /* Update the version stamp in the file structure, since we just
1239 * wrote to the file & changed it's date.
1241 if (stat(file->filename, statbuf) < 0)
1243 /* Error, probably file not found. */
1246 file->version = (unsigned)statbuf->st_mtime;
1248 /* Correct file->version_str */
1249 freez(file->version_str);
1250 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1251 version_buf[sizeof(version_buf)-1] = '\0';
1252 file->version_str = strdup(version_buf);
1253 if (version_buf == NULL)
1255 return JB_ERR_MEMORY;
1262 /*********************************************************************
1264 * Function : edit_free_file
1266 * Description : Free a complete file in memory.
1269 * 1 : file = Data structure to free.
1273 *********************************************************************/
1274 void edit_free_file(struct editable_file * file)
1278 /* Silently ignore NULL pointer */
1282 edit_free_file_lines(file->lines);
1283 freez(file->version_str);
1285 file->parse_error_text = NULL; /* Statically allocated */
1286 file->parse_error = NULL;
1292 /*********************************************************************
1294 * Function : edit_free_file_lines
1296 * Description : Free an entire linked list of file lines.
1299 * 1 : first_line = Data structure to free.
1303 *********************************************************************/
1304 static void edit_free_file_lines(struct file_line * first_line)
1306 struct file_line * next_line;
1308 while (first_line != NULL)
1310 next_line = first_line->next;
1311 first_line->next = NULL;
1312 freez(first_line->raw);
1313 freez(first_line->prefix);
1314 freez(first_line->unprocessed);
1315 switch(first_line->type)
1317 case 0: /* special case if memory zeroed */
1318 case FILE_LINE_UNPROCESSED:
1319 case FILE_LINE_BLANK:
1320 case FILE_LINE_ALIAS_HEADER:
1321 case FILE_LINE_SETTINGS_HEADER:
1322 case FILE_LINE_DESCRIPTION_HEADER:
1323 case FILE_LINE_DESCRIPTION_ENTRY:
1324 case FILE_LINE_ALIAS_ENTRY:
1326 /* No data is stored for these */
1329 case FILE_LINE_ACTION:
1330 free_action(first_line->data.action);
1333 case FILE_LINE_SETTINGS_ENTRY:
1334 freez(first_line->data.setting.name);
1335 freez(first_line->data.setting.svalue);
1338 /* Should never happen */
1342 first_line->type = 0; /* paranoia */
1344 first_line = next_line;
1349 /*********************************************************************
1351 * Function : match_actions_file_header_line
1353 * Description : Match an actions file {{header}} line
1356 * 1 : line = String from file
1357 * 2 : name = Header to match against
1359 * Returns : 0 iff they match.
1361 *********************************************************************/
1362 static int match_actions_file_header_line(const char * line, const char * name)
1370 if ((line[0] != '{') || (line[1] != '{'))
1376 /* Look for optional whitespace */
1377 while ( (*line == ' ') || (*line == '\t') )
1382 /* Look for the specified name (case-insensitive) */
1384 if (0 != strncmpic(line, name, len))
1390 /* Look for optional whitespace */
1391 while ( (*line == ' ') || (*line == '\t') )
1396 /* Look for "}}" and end of string*/
1397 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1407 /*********************************************************************
1409 * Function : match_actions_file_header_line
1411 * Description : Match an actions file {{header}} line
1414 * 1 : line = String from file. Must not start with
1415 * whitespace (else infinite loop!)
1416 * 2 : pname = Destination for name
1417 * 2 : pvalue = Destination for value
1419 * Returns : JB_ERR_OK on success
1420 * JB_ERR_MEMORY on out-of-memory
1421 * JB_ERR_PARSE if there's no "=" sign, or if there's
1422 * nothing before the "=" sign (but empty
1423 * values *after* the "=" sign are legal).
1425 *********************************************************************/
1426 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1428 const char * name_end;
1429 const char * value_start;
1435 assert(*line != ' ');
1436 assert(*line != '\t');
1441 value_start = strchr(line, '=');
1442 if ((value_start == NULL) || (value_start == line))
1444 return JB_ERR_PARSE;
1447 name_end = value_start - 1;
1449 /* Eat any whitespace before the '=' */
1450 while ((*name_end == ' ') || (*name_end == '\t'))
1453 * we already know we must have at least 1 non-ws char
1454 * at start of buf - no need to check
1459 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1460 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1462 return JB_ERR_MEMORY;
1464 strncpy(*pname, line, name_len);
1465 (*pname)[name_len] = '\0';
1467 /* Eat any the whitespace after the '=' */
1469 while ((*value_start == ' ') || (*value_start == '\t'))
1474 if (NULL == (*pvalue = strdup(value_start)))
1478 return JB_ERR_MEMORY;
1485 /*********************************************************************
1487 * Function : edit_parse_actions_file
1489 * Description : Parse an actions file in memory.
1491 * Passed linked list must have the "data" member
1492 * zeroed, and must contain valid "next" and
1493 * "unprocessed" fields. The "raw" and "prefix"
1494 * fields are ignored, and "type" is just overwritten.
1496 * Note that on error the file may have been
1500 * 1 : file = Actions file to be parsed in-place.
1502 * Returns : JB_ERR_OK on success
1503 * JB_ERR_MEMORY on out-of-memory
1504 * JB_ERR_PARSE on error
1506 *********************************************************************/
1507 jb_err edit_parse_actions_file(struct editable_file * file)
1509 struct file_line * cur_line;
1511 const char * text; /* Text from a line */
1512 char * name; /* For lines of the form name=value */
1513 char * value; /* For lines of the form name=value */
1514 struct action_alias * alias_list = NULL;
1515 jb_err err = JB_ERR_OK;
1517 /* alias_list contains the aliases defined in this file.
1518 * It might be better to use the "file_line.data" fields
1519 * in the relavent places instead.
1522 cur_line = file->lines;
1524 /* A note about blank line support: Blank lines should only
1525 * ever occur as the last line in the file. This function
1526 * is more forgiving than that - FILE_LINE_BLANK can occur
1530 /* Skip leading blanks. Should only happen if file is
1531 * empty (which is valid, but pointless).
1533 while ( (cur_line != NULL)
1534 && (cur_line->unprocessed[0] == '\0') )
1537 cur_line->type = FILE_LINE_BLANK;
1538 cur_line = cur_line->next;
1541 if ( (cur_line != NULL)
1542 && (cur_line->unprocessed[0] != '{') )
1544 /* File doesn't start with a header */
1545 file->parse_error = cur_line;
1546 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1547 return JB_ERR_PARSE;
1550 if ( (cur_line != NULL) && (0 ==
1551 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1553 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1555 cur_line = cur_line->next;
1556 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1558 if (cur_line->unprocessed[0])
1560 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1562 err = split_line_on_equals(cur_line->unprocessed,
1563 &cur_line->data.setting.name,
1564 &cur_line->data.setting.svalue);
1565 if (err == JB_ERR_MEMORY)
1569 else if (err != JB_ERR_OK)
1571 /* Line does not contain a name=value pair */
1572 file->parse_error = cur_line;
1573 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1574 return JB_ERR_PARSE;
1579 cur_line->type = FILE_LINE_BLANK;
1581 cur_line = cur_line->next;
1585 if ( (cur_line != NULL) && (0 ==
1586 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1588 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1590 cur_line = cur_line->next;
1591 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1593 if (cur_line->unprocessed[0])
1595 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1599 cur_line->type = FILE_LINE_BLANK;
1601 cur_line = cur_line->next;
1605 if ( (cur_line != NULL) && (0 ==
1606 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1608 cur_line->type = FILE_LINE_ALIAS_HEADER;
1610 cur_line = cur_line->next;
1611 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1613 if (cur_line->unprocessed[0])
1615 /* define an alias */
1616 struct action_alias * new_alias;
1618 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1620 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1621 if (err == JB_ERR_MEMORY)
1625 else if (err != JB_ERR_OK)
1627 /* Line does not contain a name=value pair */
1628 file->parse_error = cur_line;
1629 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1630 return JB_ERR_PARSE;
1633 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1638 free_alias_list(alias_list);
1639 return JB_ERR_MEMORY;
1642 err = get_actions(value, alias_list, new_alias->action);
1645 /* Invalid action or out of memory */
1649 free_alias_list(alias_list);
1650 if (err == JB_ERR_MEMORY)
1656 /* Line does not contain a name=value pair */
1657 file->parse_error = cur_line;
1658 file->parse_error_text = "This alias does not specify a valid set of actions.";
1659 return JB_ERR_PARSE;
1665 new_alias->name = name;
1668 new_alias->next = alias_list;
1669 alias_list = new_alias;
1673 cur_line->type = FILE_LINE_BLANK;
1675 cur_line = cur_line->next;
1679 /* Header done, process the main part of the file */
1680 while (cur_line != NULL)
1682 /* At this point, (cur_line->unprocessed[0] == '{') */
1683 assert(cur_line->unprocessed[0] == '{');
1684 text = cur_line->unprocessed + 1;
1685 len = strlen(text) - 1;
1686 if (text[len] != '}')
1688 /* No closing } on header */
1689 free_alias_list(alias_list);
1690 file->parse_error = cur_line;
1691 file->parse_error_text = "Headers starting with '{' must have a "
1692 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1693 "must close with two brackets ('}}').";
1694 return JB_ERR_PARSE;
1699 /* An invalid {{ header. */
1700 free_alias_list(alias_list);
1701 file->parse_error = cur_line;
1702 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1703 "Please remember that the system (two-bracket) headers must "
1704 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1705 "and must appear before any actions (one-bracket) headers. "
1706 "Also note that system headers may not be repeated.";
1707 return JB_ERR_PARSE;
1710 while ( (*text == ' ') || (*text == '\t') )
1716 && ( (text[len - 1] == ' ')
1717 || (text[len - 1] == '\t') ) )
1722 cur_line->type = FILE_LINE_ACTION;
1724 /* Remove {} and make copy */
1725 if (NULL == (value = (char *) malloc(len + 1)))
1728 free_alias_list(alias_list);
1729 return JB_ERR_MEMORY;
1731 strncpy(value, text, len);
1735 err = get_actions(value, alias_list, cur_line->data.action);
1738 /* Invalid action or out of memory */
1740 free_alias_list(alias_list);
1741 if (err == JB_ERR_MEMORY)
1747 /* Line does not contain a name=value pair */
1748 file->parse_error = cur_line;
1749 file->parse_error_text = "This header does not specify a valid set of actions.";
1750 return JB_ERR_PARSE;
1754 /* Done with string - it was clobbered anyway */
1757 /* Process next line */
1758 cur_line = cur_line->next;
1760 /* Loop processing URL patterns */
1761 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1763 if (cur_line->unprocessed[0])
1765 /* Could parse URL here, but this isn't currently needed */
1767 cur_line->type = FILE_LINE_URL;
1771 cur_line->type = FILE_LINE_BLANK;
1773 cur_line = cur_line->next;
1775 } /* End main while(cur_line != NULL) loop */
1777 free_alias_list(alias_list);
1783 /*********************************************************************
1785 * Function : edit_read_file_lines
1787 * Description : Read all the lines of a file into memory.
1788 * Handles whitespace, comments and line continuation.
1791 * 1 : fp = File to read from. On return, this will be
1792 * at EOF but it will not have been closed.
1793 * 2 : pfile = Destination for a linked list of file_lines.
1794 * Will be set to NULL on error.
1795 * 3 : newline = How to handle newlines.
1797 * Returns : JB_ERR_OK on success
1798 * JB_ERR_MEMORY on out-of-memory
1800 *********************************************************************/
1801 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1803 struct file_line * first_line; /* Keep for return value or to free */
1804 struct file_line * cur_line; /* Current line */
1805 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1813 cur_line = first_line = zalloc(sizeof(struct file_line));
1814 if (cur_line == NULL)
1816 return JB_ERR_MEMORY;
1819 cur_line->type = FILE_LINE_UNPROCESSED;
1821 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1824 /* Out of memory or empty file. */
1825 /* Note that empty file is not an error we propogate up */
1827 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1832 prev_line = cur_line;
1833 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1834 if (cur_line == NULL)
1837 edit_free_file_lines(first_line);
1838 return JB_ERR_MEMORY;
1841 cur_line->type = FILE_LINE_UNPROCESSED;
1843 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1844 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1847 edit_free_file_lines(first_line);
1848 return JB_ERR_MEMORY;
1852 while (rval != JB_ERR_FILE);
1856 /* We allocated one too many - free it */
1857 prev_line->next = NULL;
1860 *pfile = first_line;
1865 /*********************************************************************
1867 * Function : edit_read_file
1869 * Description : Read a complete file into memory.
1870 * Handles CGI parameter parsing. If requested, also
1871 * checks the file's modification timestamp.
1874 * 1 : csp = Current client state (buffers, headers, etc...)
1875 * 2 : parameters = map of cgi parameters.
1876 * 3 : require_version = true to check "ver" parameter.
1877 * 4 : pfile = Destination for the file. Will be set
1881 * f : The action file identifier.
1882 * ver : (Only if require_version is nonzero)
1883 * Timestamp of the actions file. If wrong, this
1884 * function fails with JB_ERR_MODIFIED.
1886 * Returns : JB_ERR_OK on success
1887 * JB_ERR_MEMORY on out-of-memory
1888 * JB_ERR_CGI_PARAMS if "filename" was not specified
1890 * JB_ERR_FILE if the file cannot be opened or
1892 * JB_ERR_MODIFIED if version checking was requested and
1893 * failed - the file was modified outside
1894 * of this CGI editor instance.
1896 *********************************************************************/
1897 jb_err edit_read_file(struct client_state *csp,
1898 const struct map *parameters,
1899 int require_version,
1900 struct editable_file **pfile)
1902 struct file_line * lines;
1905 const char *filename = NULL;
1906 struct editable_file * file;
1907 unsigned version = 0;
1908 struct stat statbuf[1];
1909 char version_buf[22];
1910 int newline = NEWLINE_UNKNOWN;
1919 err = get_number_param(csp, parameters, "f", &i);
1920 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1922 filename = csp->config->actions_file[i];
1924 else if (JB_ERR_CGI_PARAMS == err)
1927 * Probably an old-school URL like
1928 * http://config.privoxy.org/edit-actions-list?f=default
1930 err = get_file_name_param(csp, parameters, "f", &filename);
1933 if (NULL == filename || stat(filename, statbuf) < 0)
1935 /* Error, probably file not found. */
1938 version = (unsigned) statbuf->st_mtime;
1940 if (require_version)
1942 unsigned specified_version;
1943 err = get_number_param(csp, parameters, "v", &specified_version);
1949 if (version != specified_version)
1951 return JB_ERR_MODIFIED;
1955 if (NULL == (fp = fopen(filename,"rb")))
1960 err = edit_read_file_lines(fp, &lines, &newline);
1969 file = (struct editable_file *) zalloc(sizeof(*file));
1972 edit_free_file_lines(lines);
1976 file->lines = lines;
1977 file->newline = newline;
1978 file->filename = filename;
1979 file->version = version;
1980 file->identifier = i;
1982 /* Correct file->version_str */
1983 freez(file->version_str);
1984 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1985 version_buf[sizeof(version_buf)-1] = '\0';
1986 file->version_str = strdup(version_buf);
1987 if (version_buf == NULL)
1989 edit_free_file(file);
1990 return JB_ERR_MEMORY;
1998 /*********************************************************************
2000 * Function : edit_read_actions_file
2002 * Description : Read a complete actions file into memory.
2003 * Handles CGI parameter parsing. If requested, also
2004 * checks the file's modification timestamp.
2006 * If this function detects an error in the categories
2007 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
2008 * then it handles it by filling in the specified
2009 * response structure and returning JB_ERR_FILE.
2012 * 1 : csp = Current client state (buffers, headers, etc...)
2013 * 2 : rsp = HTTP response. Only filled in on error.
2014 * 2 : parameters = map of cgi parameters.
2015 * 3 : require_version = true to check "ver" parameter.
2016 * 4 : pfile = Destination for the file. Will be set
2020 * f : The actions file identifier.
2021 * ver : (Only if require_version is nonzero)
2022 * Timestamp of the actions file. If wrong, this
2023 * function fails with JB_ERR_MODIFIED.
2025 * Returns : JB_ERR_OK on success
2026 * JB_ERR_MEMORY on out-of-memory
2027 * JB_ERR_CGI_PARAMS if "filename" was not specified
2029 * JB_ERR_FILE if the file does not contain valid data,
2030 * or if file cannot be opened or
2031 * contains no data, or if version
2032 * checking was requested and failed.
2034 *********************************************************************/
2035 jb_err edit_read_actions_file(struct client_state *csp,
2036 struct http_response *rsp,
2037 const struct map *parameters,
2038 int require_version,
2039 struct editable_file **pfile)
2042 struct editable_file *file;
2043 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
2051 err = edit_read_file(csp, parameters, require_version, &file);
2054 /* Try to handle if possible */
2055 if (err == JB_ERR_FILE)
2057 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
2059 else if (err == JB_ERR_MODIFIED)
2061 assert(require_version);
2062 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
2063 log_error(LOG_LEVEL_ERROR,
2064 "Blocking CGI edit request due to modification time mismatch.");
2065 if (acceptable_failures > 0)
2067 log_error(LOG_LEVEL_INFO,
2068 "The CGI editor will be turned off after another %d mismatche(s).",
2069 acceptable_failures);
2070 acceptable_failures--;
2074 log_error(LOG_LEVEL_INFO,
2075 "Timestamp mismatch limit reached, turning CGI editor off. "
2076 "Reload the configuration file to reenable it.");
2077 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
2080 if (err == JB_ERR_OK)
2083 * Signal to higher-level CGI code that there was a problem but we
2084 * handled it, they should just return JB_ERR_OK.
2091 err = edit_parse_actions_file(file);
2094 if (err == JB_ERR_PARSE)
2096 err = cgi_error_parse(csp, rsp, file);
2097 if (err == JB_ERR_OK)
2100 * Signal to higher-level CGI code that there was a problem but we
2101 * handled it, they should just return JB_ERR_OK.
2106 edit_free_file(file);
2115 /*********************************************************************
2117 * Function : get_file_name_param
2119 * Description : Get the name of the file to edit from the parameters
2120 * passed to a CGI function using the old syntax.
2121 * This function handles security checks and only
2122 * accepts files that Privoxy already knows.
2125 * 1 : csp = Current client state (buffers, headers, etc...)
2126 * 2 : parameters = map of cgi parameters
2127 * 3 : param_name = The name of the parameter to read
2128 * 4 : pfilename = pointer to the filename in
2129 * csp->config->actions_file[] if found. Set to NULL on error.
2131 * Returns : JB_ERR_OK on success
2132 * JB_ERR_MEMORY on out-of-memory
2133 * JB_ERR_CGI_PARAMS if "filename" was not specified
2136 *********************************************************************/
2137 static jb_err get_file_name_param(struct client_state *csp,
2138 const struct map *parameters,
2139 const char *param_name,
2140 const char **pfilename)
2143 const char suffix[] = ".action";
2158 param = lookup(parameters, param_name);
2161 return JB_ERR_CGI_PARAMS;
2164 len = strlen(param);
2165 if (len >= FILENAME_MAX)
2168 return JB_ERR_CGI_PARAMS;
2172 * Check every character to see if it's legal.
2173 * Totally unnecessary but we do it anyway.
2176 while ((ch = *s++) != '\0')
2178 if ( ((ch < 'A') || (ch > 'Z'))
2179 && ((ch < 'a') || (ch > 'z'))
2180 && ((ch < '0') || (ch > '9'))
2184 /* Probable hack attempt. */
2185 return JB_ERR_CGI_PARAMS;
2189 /* Append extension */
2190 name_size = len + strlen(suffix) + 1;
2191 name = malloc(name_size);
2194 return JB_ERR_MEMORY;
2196 strlcpy(name, param, name_size);
2197 strlcat(name, suffix, name_size);
2200 fullpath = make_path(csp->config->confdir, name);
2203 if (fullpath == NULL)
2205 return JB_ERR_MEMORY;
2208 /* Check if the file is known */
2209 for (i = 0; i < MAX_AF_FILES; i++)
2211 if (NULL != csp->config->actions_file[i] &&
2212 !strcmp(fullpath, csp->config->actions_file[i]))
2215 *pfilename = csp->config->actions_file[i];
2223 return JB_ERR_CGI_PARAMS;
2227 /*********************************************************************
2229 * Function : get_url_spec_param
2231 * Description : Get a URL pattern from the parameters
2232 * passed to a CGI function. Removes leading/trailing
2233 * spaces and validates it.
2236 * 1 : csp = Current client state (buffers, headers, etc...)
2237 * 2 : parameters = map of cgi parameters
2238 * 3 : name = Name of CGI parameter to read
2239 * 4 : pvalue = destination for value. Will be malloc()'d.
2240 * Set to NULL on error.
2242 * Returns : JB_ERR_OK on success
2243 * JB_ERR_MEMORY on out-of-memory
2244 * JB_ERR_CGI_PARAMS if the parameter was not specified
2247 *********************************************************************/
2248 static jb_err get_url_spec_param(struct client_state *csp,
2249 const struct map *parameters,
2253 const char *orig_param;
2256 struct url_spec compiled[1];
2266 orig_param = lookup(parameters, name);
2269 return JB_ERR_CGI_PARAMS;
2272 /* Copy and trim whitespace */
2273 param = strdup(orig_param);
2276 return JB_ERR_MEMORY;
2280 /* Must be non-empty, and can't allow 1st character to be '{' */
2281 if (param[0] == '\0' || param[0] == '{')
2284 return JB_ERR_CGI_PARAMS;
2287 /* Check for embedded newlines */
2288 for (s = param; *s != '\0'; s++)
2290 if ((*s == '\r') || (*s == '\n'))
2293 return JB_ERR_CGI_PARAMS;
2297 /* Check that regex is valid */
2302 return JB_ERR_MEMORY;
2304 err = create_url_spec(compiled, s);
2309 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2311 free_url_spec(compiled);
2313 if (param[strlen(param) - 1] == '\\')
2316 * Must protect trailing '\\' from becoming line continuation character.
2317 * Two methods: 1) If it's a domain only, add a trailing '/'.
2318 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2320 if (strchr(param, '/') == NULL)
2322 err = string_append(¶m, "/");
2326 err = string_append(¶m, "(?:)");
2333 /* Check that the modified regex is valid */
2338 return JB_ERR_MEMORY;
2340 err = create_url_spec(compiled, s);
2345 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2347 free_url_spec(compiled);
2354 /*********************************************************************
2356 * Function : map_radio
2358 * Description : Map a set of radio button values. E.g. if you have
2359 * 3 radio buttons, declare them as:
2360 * <option type="radio" name="xyz" @xyz-a@>
2361 * <option type="radio" name="xyz" @xyz-b@>
2362 * <option type="radio" name="xyz" @xyz-c@>
2363 * Then map one of the @xyz-?@ variables to "checked"
2364 * and all the others to empty by calling:
2365 * map_radio(exports, "xyz", "abc", sel)
2366 * Where 'sel' is 'a', 'b', or 'c'.
2369 * 1 : exports = Exports map to modify.
2370 * 2 : optionname = name for map
2371 * 3 : values = null-terminated list of values;
2372 * 4 : value = Selected value.
2374 * CGI Parameters : None
2376 * Returns : JB_ERR_OK on success
2377 * JB_ERR_MEMORY on out-of-memory
2379 *********************************************************************/
2380 static jb_err map_radio(struct map * exports,
2381 const char * optionname,
2382 const char * values,
2388 const size_t len = strlen(optionname);
2389 const size_t buf_size = len + 3;
2395 buf = malloc(buf_size);
2398 return JB_ERR_MEMORY;
2401 strlcpy(buf, optionname, buf_size);
2403 /* XXX: this looks ... interesting */
2408 while ((c = *values++) != '\0')
2413 if (map(exports, buf, 1, "", 1))
2415 return JB_ERR_MEMORY;
2421 return map(exports, buf, 0, "checked", 1);
2425 /*********************************************************************
2427 * Function : cgi_error_modified
2429 * Description : CGI function that is called when a file is modified
2430 * outside the CGI editor.
2433 * 1 : csp = Current client state (buffers, headers, etc...)
2434 * 2 : rsp = http_response data structure for output
2435 * 3 : filename = The file that was modified.
2437 * CGI Parameters : none
2439 * Returns : JB_ERR_OK on success
2440 * JB_ERR_MEMORY on out-of-memory error.
2442 *********************************************************************/
2443 jb_err cgi_error_modified(struct client_state *csp,
2444 struct http_response *rsp,
2445 const char *filename)
2447 struct map *exports;
2454 if (NULL == (exports = default_exports(csp, NULL)))
2456 return JB_ERR_MEMORY;
2459 err = map(exports, "f", 1, html_encode(filename), 0);
2466 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2470 /*********************************************************************
2472 * Function : cgi_error_parse
2474 * Description : CGI function that is called when a file cannot
2475 * be parsed by the CGI editor.
2478 * 1 : csp = Current client state (buffers, headers, etc...)
2479 * 2 : rsp = http_response data structure for output
2480 * 3 : file = The file that was modified.
2482 * CGI Parameters : none
2484 * Returns : JB_ERR_OK on success
2485 * JB_ERR_MEMORY on out-of-memory error.
2487 *********************************************************************/
2488 jb_err cgi_error_parse(struct client_state *csp,
2489 struct http_response *rsp,
2490 struct editable_file *file)
2492 struct map *exports;
2494 struct file_line *cur_line;
2500 if (NULL == (exports = default_exports(csp, NULL)))
2502 return JB_ERR_MEMORY;
2505 err = map(exports, "f", 1, stringify(file->identifier), 0);
2506 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2508 cur_line = file->parse_error;
2511 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2512 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2520 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2524 /*********************************************************************
2526 * Function : cgi_error_file
2528 * Description : CGI function that is called when a file cannot be
2529 * opened by the CGI editor.
2532 * 1 : csp = Current client state (buffers, headers, etc...)
2533 * 2 : rsp = http_response data structure for output
2534 * 3 : filename = The file that was modified.
2536 * CGI Parameters : none
2538 * Returns : JB_ERR_OK on success
2539 * JB_ERR_MEMORY on out-of-memory error.
2541 *********************************************************************/
2542 jb_err cgi_error_file(struct client_state *csp,
2543 struct http_response *rsp,
2544 const char *filename)
2546 struct map *exports;
2553 if (NULL == (exports = default_exports(csp, NULL)))
2555 return JB_ERR_MEMORY;
2558 err = map(exports, "f", 1, html_encode(filename), 0);
2565 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2569 /*********************************************************************
2571 * Function : cgi_error_file_read_only
2573 * Description : CGI function that is called when a file cannot be
2574 * opened for writing by the CGI editor.
2577 * 1 : csp = Current client state (buffers, headers, etc...)
2578 * 2 : rsp = http_response data structure for output
2579 * 3 : filename = The file that we can't write to
2581 * CGI Parameters : none
2583 * Returns : JB_ERR_OK on success
2584 * JB_ERR_MEMORY on out-of-memory error.
2586 *********************************************************************/
2587 jb_err cgi_error_file_read_only(struct client_state *csp,
2588 struct http_response *rsp,
2589 const char *filename)
2591 struct map *exports;
2598 if (NULL == (exports = default_exports(csp, NULL)))
2600 return JB_ERR_MEMORY;
2603 err = map(exports, "f", 1, html_encode(filename), 0);
2610 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2614 /*********************************************************************
2616 * Function : cgi_edit_actions
2618 * Description : CGI function that allows the user to choose which
2619 * actions file to edit.
2622 * 1 : csp = Current client state (buffers, headers, etc...)
2623 * 2 : rsp = http_response data structure for output
2624 * 3 : parameters = map of cgi parameters
2626 * CGI Parameters : None
2628 * Returns : JB_ERR_OK on success
2629 * JB_ERR_MEMORY on out-of-memory error
2631 *********************************************************************/
2632 jb_err cgi_edit_actions(struct client_state *csp,
2633 struct http_response *rsp,
2634 const struct map *parameters)
2637 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2639 return cgi_error_disabled(csp, rsp);
2642 /* FIXME: Incomplete */
2644 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2649 /*********************************************************************
2651 * Function : cgi_edit_actions_list
2653 * Description : CGI function that edits the actions list.
2654 * FIXME: This function shouldn't FATAL ever.
2655 * FIXME: This function doesn't check the retval of map()
2657 * 1 : csp = Current client state (buffers, headers, etc...)
2658 * 2 : rsp = http_response data structure for output
2659 * 3 : parameters = map of cgi parameters
2661 * CGI Parameters : filename
2663 * Returns : JB_ERR_OK on success
2664 * JB_ERR_MEMORY on out-of-memory
2665 * JB_ERR_FILE if the file cannot be opened or
2667 * JB_ERR_CGI_PARAMS if "filename" was not specified
2670 *********************************************************************/
2671 jb_err cgi_edit_actions_list(struct client_state *csp,
2672 struct http_response *rsp,
2673 const struct map *parameters)
2675 char * section_template;
2676 char * url_template;
2681 struct map * exports;
2682 struct map * section_exports;
2683 struct map * url_exports;
2684 struct editable_file * file;
2685 struct file_line * cur_line;
2686 unsigned line_number = 0;
2687 unsigned prev_section_line_number = ((unsigned) (-1));
2689 struct file_list * fl;
2690 struct url_actions * b;
2691 char * buttons = NULL;
2694 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2696 return cgi_error_disabled(csp, rsp);
2699 if (NULL == (exports = default_exports(csp, NULL)))
2701 return JB_ERR_MEMORY;
2704 /* Load actions file */
2705 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2708 /* No filename specified, can't read file, or out of memory. */
2710 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2713 /* Find start of actions in file */
2714 cur_line = file->lines;
2716 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2718 cur_line = cur_line->next;
2723 * Conventional actions files should have a match all block
2725 * cur_line = {...global actions...}
2726 * cur_line->next = /
2727 * cur_line->next->next = {...actions...} or EOF
2729 if ( (cur_line != NULL)
2730 && (cur_line->type == FILE_LINE_ACTION)
2731 && (cur_line->next != NULL)
2732 && (cur_line->next->type == FILE_LINE_URL)
2733 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2734 && ( (cur_line->next->next == NULL)
2735 || (cur_line->next->next->type != FILE_LINE_URL)
2739 * Generate string with buttons to set actions for "/" to
2740 * any predefined set of actions (named standard.*, probably
2741 * residing in standard.action).
2744 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2747 edit_free_file(file);
2749 if (err == JB_ERR_FILE)
2751 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2756 err = template_fill(§ion_template, exports);
2759 edit_free_file(file);
2764 buttons = strdup("");
2765 for (i = 0; i < MAX_AF_FILES; i++)
2767 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2769 for (b = b->next; NULL != b; b = b->next)
2771 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2773 if (err || (NULL == (section_exports = new_map())))
2776 free(section_template);
2777 edit_free_file(file);
2779 return JB_ERR_MEMORY;
2782 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2784 if (err || (NULL == (s = strdup(section_template))))
2786 free_map(section_exports);
2788 free(section_template);
2789 edit_free_file(file);
2791 return JB_ERR_MEMORY;
2794 if (!err) err = template_fill(&s, section_exports);
2795 free_map(section_exports);
2796 if (!err) err = string_join(&buttons, s);
2801 freez(section_template);
2802 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2805 * Conventional actions file, supply extra editing help.
2806 * (e.g. don't allow them to make it an unconventional one).
2808 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2810 snprintf(buf, sizeof(buf), "%d", line_number);
2811 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2812 snprintf(buf, sizeof(buf), "%d", line_number + 2);
2813 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2814 if (!err) err = map(exports, "all-urls-actions", 1,
2815 actions_to_html(csp, cur_line->data.action), 0);
2817 /* Skip the 2 lines */
2818 cur_line = cur_line->next->next;
2822 * Note that prev_section_line_number is NOT set here.
2823 * This is deliberate and not a bug. It stops a "Move up"
2824 * option appearing on the next section. Clicking "Move
2825 * up" would make the actions file unconventional, which
2826 * we don't want, so we hide this option.
2832 * Non-standard actions file - does not begin with
2833 * the "All URLs" section.
2835 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2838 /* Set up global exports */
2840 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2841 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2842 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2844 /* Discourage private additions to default.action */
2846 if (!err) err = map_conditional(exports, "default-action",
2847 (strstr("default.action", file->filename) != NULL));
2850 edit_free_file(file);
2855 /* Should do all global exports above this point */
2857 /* Load templates */
2859 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2862 edit_free_file(file);
2864 if (err == JB_ERR_FILE)
2866 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2871 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2874 free(section_template);
2875 edit_free_file(file);
2877 if (err == JB_ERR_FILE)
2879 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2884 err = template_fill(§ion_template, exports);
2888 edit_free_file(file);
2893 err = template_fill(&url_template, exports);
2896 free(section_template);
2897 edit_free_file(file);
2902 if (NULL == (sections = strdup("")))
2904 free(section_template);
2906 edit_free_file(file);
2908 return JB_ERR_MEMORY;
2911 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2913 if (NULL == (section_exports = new_map()))
2916 free(section_template);
2918 edit_free_file(file);
2920 return JB_ERR_MEMORY;
2923 snprintf(buf, sizeof(buf), "%d", line_number);
2924 err = map(section_exports, "s", 1, buf, 1);
2925 if (!err) err = map(section_exports, "actions", 1,
2926 actions_to_html(csp, cur_line->data.action), 0);
2929 && (cur_line->next != NULL)
2930 && (cur_line->next->type == FILE_LINE_URL))
2932 /* This section contains at least one URL, don't allow delete */
2933 err = map_block_killer(section_exports, "empty-section");
2937 if (!err) err = map_block_keep(section_exports, "empty-section");
2940 if (prev_section_line_number != ((unsigned)(-1)))
2942 /* Not last section */
2943 snprintf(buf, sizeof(buf), "%d", prev_section_line_number);
2944 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2945 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2950 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2952 prev_section_line_number = line_number;
2957 free(section_template);
2959 edit_free_file(file);
2961 free_map(section_exports);
2965 /* Should do all section-specific exports above this point */
2967 if (NULL == (urls = strdup("")))
2970 free(section_template);
2972 edit_free_file(file);
2974 free_map(section_exports);
2975 return JB_ERR_MEMORY;
2980 cur_line = cur_line->next;
2983 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2985 if (NULL == (url_exports = new_map()))
2989 free(section_template);
2991 edit_free_file(file);
2993 free_map(section_exports);
2994 return JB_ERR_MEMORY;
2997 snprintf(buf, sizeof(buf), "%d", line_number);
2998 err = map(url_exports, "p", 1, buf, 1);
3000 snprintf(buf, sizeof(buf), "%d", url_1_2);
3001 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
3003 if (!err) err = map(url_exports, "url-html", 1,
3004 html_encode(cur_line->unprocessed), 0);
3005 if (!err) err = map(url_exports, "url", 1,
3006 url_encode(cur_line->unprocessed), 0);
3012 free(section_template);
3014 edit_free_file(file);
3016 free_map(section_exports);
3017 free_map(url_exports);
3021 if (NULL == (s = strdup(url_template)))
3025 free(section_template);
3027 edit_free_file(file);
3029 free_map(section_exports);
3030 free_map(url_exports);
3031 return JB_ERR_MEMORY;
3034 err = template_fill(&s, section_exports);
3035 if (!err) err = template_fill(&s, url_exports);
3036 if (!err) err = string_append(&urls, s);
3038 free_map(url_exports);
3045 free(section_template);
3047 edit_free_file(file);
3049 free_map(section_exports);
3053 url_1_2 = 3 - url_1_2;
3055 cur_line = cur_line->next;
3059 err = map(section_exports, "urls", 1, urls, 0);
3061 /* Could also do section-specific exports here, but it wouldn't be as fast */
3063 snprintf(buf, sizeof(buf), "%d", line_number);
3064 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
3066 if ( (cur_line != NULL)
3067 && (cur_line->type == FILE_LINE_ACTION))
3069 /* Not last section */
3070 if (!err) err = map_block_keep(section_exports, "s-next-exists");
3075 if (!err) err = map_block_killer(section_exports, "s-next-exists");
3081 free(section_template);
3083 edit_free_file(file);
3085 free_map(section_exports);
3089 if (NULL == (s = strdup(section_template)))
3092 free(section_template);
3094 edit_free_file(file);
3096 free_map(section_exports);
3097 return JB_ERR_MEMORY;
3100 err = template_fill(&s, section_exports);
3101 if (!err) err = string_append(§ions, s);
3104 free_map(section_exports);
3109 free(section_template);
3111 edit_free_file(file);
3117 edit_free_file(file);
3118 free(section_template);
3121 err = map(exports, "sections", 1, sections, 0);
3128 /* Could also do global exports here, but it wouldn't be as fast */
3130 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
3134 /*********************************************************************
3136 * Function : cgi_edit_actions_for_url
3138 * Description : CGI function that edits the Actions list.
3141 * 1 : csp = Current client state (buffers, headers, etc...)
3142 * 2 : rsp = http_response data structure for output
3143 * 3 : parameters = map of cgi parameters
3145 * CGI Parameters : None
3147 * Returns : JB_ERR_OK on success
3148 * JB_ERR_MEMORY on out-of-memory
3149 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3150 * specified or not valid.
3152 *********************************************************************/
3153 jb_err cgi_edit_actions_for_url(struct client_state *csp,
3154 struct http_response *rsp,
3155 const struct map *parameters)
3157 struct map * exports;
3159 struct editable_file * file;
3160 struct file_line * cur_line;
3161 unsigned line_number;
3163 struct re_filterfile_spec *filter_group;
3164 int i, have_filters = 0;
3166 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3168 return cgi_error_disabled(csp, rsp);
3171 err = get_number_param(csp, parameters, "s", §ionid);
3177 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3180 /* No filename specified, can't read file, modified, or out of memory. */
3181 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3184 cur_line = file->lines;
3186 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3188 cur_line = cur_line->next;
3191 if ( (cur_line == NULL)
3192 || (line_number != sectionid)
3194 || (cur_line->type != FILE_LINE_ACTION))
3196 /* Invalid "sectionid" parameter */
3197 edit_free_file(file);
3198 return JB_ERR_CGI_PARAMS;
3201 if (NULL == (exports = default_exports(csp, NULL)))
3203 edit_free_file(file);
3204 return JB_ERR_MEMORY;
3207 err = map(exports, "f", 1, stringify(file->identifier), 0);
3208 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3209 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3211 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3214 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3215 * length limitation and ignore clicks on the Submit buttons if
3216 * the resulting GET URL would be longer than their limit.
3218 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3219 * reached this limit and action editing stopped working in these
3220 * browsers (BR #1570678).
3222 * The config option split-large-forms works around this browser
3223 * bug (HTTP has no URL lenght limitation) by deviding the action
3224 * list form into multiple smaller ones. It means the URLs are shorter
3225 * and work in broken browsers as well, but the user can no longer change
3226 * all actions with one submit.
3228 * A better solution would be to switch to POST requests,
3229 * but this will do for now.
3231 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3233 /* Generate multiple smaller form by killing the big one. */
3234 err = map_block_killer(exports, "one-form-only");
3238 /* Default: Generate one large form by killing the smaller ones. */
3239 err = map_block_killer(exports, "multiple-forms");
3242 for (i = 0; i < MAX_AF_FILES; i++)
3244 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3246 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3254 edit_free_file(file);
3259 if (0 == have_filters)
3261 err = map(exports, "filter-params", 1, "", 1);
3266 * List available filters and their settings.
3268 char *filter_template;
3269 int filter_identifier = 0;
3270 char *prepared_templates[MAX_FILTER_TYPES];
3272 for (i = 0; i < MAX_FILTER_TYPES; i++)
3274 prepared_templates[i] = strdup("");
3277 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3280 edit_free_file(file);
3282 if (err == JB_ERR_FILE)
3284 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3289 err = template_fill(&filter_template, exports);
3291 for (i = 0; i < MAX_AF_FILES; i++)
3293 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3295 filter_group = csp->rlist[i]->f;
3296 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3298 char current_mode = 'x';
3300 struct list_entry *filter_name;
3301 struct map *line_exports;
3302 const int type = filter_group->type;
3303 const int multi_action_index = filter_type_info[type].multi_action_index;
3305 assert(type < MAX_FILTER_TYPES);
3307 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
3308 while ((filter_name != NULL)
3309 && (0 != strcmp(filter_group->name, filter_name->str)))
3311 filter_name = filter_name->next;
3314 if (filter_name != NULL)
3320 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
3321 while ((filter_name != NULL)
3322 && (0 != strcmp(filter_group->name, filter_name->str)))
3324 filter_name = filter_name->next;
3326 if (filter_name != NULL)
3332 /* Generate a unique serial number */
3333 snprintf(number, sizeof(number), "%x", filter_identifier++);
3334 number[sizeof(number) - 1] = '\0';
3336 line_exports = new_map();
3337 if (line_exports == NULL)
3339 err = JB_ERR_MEMORY;
3345 if (!err) err = map(line_exports, "index", 1, number, 1);
3346 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3347 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3348 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3349 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
3350 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
3351 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
3355 filter_line = strdup(filter_template);
3356 if (filter_line == NULL) err = JB_ERR_MEMORY;
3358 if (!err) err = template_fill(&filter_line, line_exports);
3359 string_join(&prepared_templates[type], filter_line);
3361 free_map(line_exports);
3366 freez(filter_template);
3368 /* Replace all filter macros with the aggregated templates */
3369 for (i = 0; i < MAX_FILTER_TYPES; i++)
3372 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
3377 /* Free aggregated templates */
3378 for (i = 0; i < MAX_FILTER_TYPES; i++)
3380 freez(prepared_templates[i]);
3385 /* Check or uncheck the "disable all of this type" radio buttons. */
3386 for (i = 0; i < MAX_FILTER_TYPES; i++)
3388 const int a = filter_type_info[i].multi_action_index;
3389 const int disable_all = cur_line->data.action->multi_remove_all[a];
3391 err = map_radio(exports, filter_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
3394 edit_free_file(file);
3402 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3406 /*********************************************************************
3408 * Function : cgi_edit_actions_submit
3410 * Description : CGI function that actually edits the Actions list.
3413 * 1 : csp = Current client state (buffers, headers, etc...)
3414 * 2 : rsp = http_response data structure for output
3415 * 3 : parameters = map of cgi parameters
3417 * CGI Parameters : None
3419 * Returns : JB_ERR_OK on success
3420 * JB_ERR_MEMORY on out-of-memory
3421 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3422 * specified or not valid.
3424 *********************************************************************/
3425 jb_err cgi_edit_actions_submit(struct client_state *csp,
3426 struct http_response *rsp,
3427 const struct map *parameters)
3432 size_t newtext_size;
3434 struct editable_file * file;
3435 struct file_line * cur_line;
3436 unsigned line_number;
3439 int filter_identifier;
3441 const char * action_set_name;
3442 struct file_list * fl;
3443 struct url_actions * b;
3445 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3447 return cgi_error_disabled(csp, rsp);
3450 err = get_number_param(csp, parameters, "s", §ionid);
3456 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3459 /* No filename specified, can't read file, modified, or out of memory. */
3460 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3463 cur_line = file->lines;
3465 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3467 cur_line = cur_line->next;
3470 if ( (cur_line == NULL)
3471 || (line_number != sectionid)
3473 || (cur_line->type != FILE_LINE_ACTION))
3475 /* Invalid "sectionid" parameter */
3476 edit_free_file(file);
3477 return JB_ERR_CGI_PARAMS;
3480 get_string_param(parameters, "p", &action_set_name);
3481 if (action_set_name != NULL)
3483 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3485 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3487 for (b = b->next; NULL != b; b = b->next)
3489 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3491 copy_action(cur_line->data.action, b->action);
3497 edit_free_file(file);
3498 return JB_ERR_CGI_PARAMS;
3504 err = actions_from_radio(parameters, cur_line->data.action);
3510 edit_free_file(file);
3514 /* Check the "disable all of this type" parameters. */
3515 for (i = 0; i < MAX_FILTER_TYPES; i++)
3517 const int multi_action_index = filter_type_info[i].multi_action_index;
3518 const char ch = get_char_param(parameters, filter_type_info[i].disable_all_param);
3522 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3523 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3524 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3528 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3532 for (filter_identifier = 0; !err; filter_identifier++)
3539 * Filter state. Valid states are: 'Y' (active),
3540 * 'N' (inactive) and 'X' (no change).
3544 * Abbreviated filter type. Valid types are: 'F' (content filter),
3545 * 'S' (server-header filter) and 'C' (client-header filter).
3547 int multi_action_index = 0;
3549 /* Generate the keys */
3550 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3551 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3552 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3553 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3554 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3556 err = get_string_param(parameters, key_name, &name);
3565 type = get_char_param(parameters, key_type);
3569 multi_action_index = ACTION_MULTI_FILTER;
3572 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3575 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3578 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3581 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3584 log_error(LOG_LEVEL_ERROR,
3585 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3588 assert(multi_action_index);
3590 value = get_char_param(parameters, key_value);
3593 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3594 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3595 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3597 else if (value == 'N')
3599 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3600 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3602 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3603 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3606 else if (value == 'X')
3608 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3609 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3616 edit_free_file(file);
3620 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3623 edit_free_file(file);
3624 return JB_ERR_MEMORY;
3627 len = strlen(actiontext);
3631 * Empty action - must special-case this.
3632 * Simply setting len to 1 is sufficient...
3637 newtext_size = len + 2;
3638 if (NULL == (newtext = malloc(newtext_size)))
3642 edit_free_file(file);
3643 return JB_ERR_MEMORY;
3645 strlcpy(newtext, actiontext, newtext_size);
3649 newtext[len + 1] = '\0';
3651 freez(cur_line->raw);
3652 freez(cur_line->unprocessed);
3653 cur_line->unprocessed = newtext;
3655 err = edit_write_file(file);
3658 /* Error writing file */
3659 if (err == JB_ERR_FILE)
3661 /* Read-only file. */
3662 err = cgi_error_file_read_only(csp, rsp, file->filename);
3664 edit_free_file(file);
3668 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3669 (long) time(NULL), file->identifier, sectionid);
3671 edit_free_file(file);
3673 return cgi_redirect(rsp, target);
3677 /*********************************************************************
3679 * Function : cgi_edit_actions_url
3681 * Description : CGI function that actually edits a URL pattern in
3685 * 1 : csp = Current client state (buffers, headers, etc...)
3686 * 2 : rsp = http_response data structure for output
3687 * 3 : parameters = map of cgi parameters
3690 * filename : Identifies the file to edit
3691 * ver : File's last-modified time
3692 * section : Line number of section to edit
3693 * pattern : Line number of pattern to edit
3694 * newval : New value for pattern
3696 * Returns : JB_ERR_OK on success
3697 * JB_ERR_MEMORY on out-of-memory
3698 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3699 * specified or not valid.
3701 *********************************************************************/
3702 jb_err cgi_edit_actions_url(struct client_state *csp,
3703 struct http_response *rsp,
3704 const struct map *parameters)
3708 struct editable_file * file;
3709 struct file_line * cur_line;
3710 unsigned line_number;
3711 unsigned section_start_line_number = 0;
3719 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3721 return cgi_error_disabled(csp, rsp);
3724 err = get_number_param(csp, parameters, "p", &patternid);
3731 return JB_ERR_CGI_PARAMS;
3734 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3740 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3743 /* No filename specified, can't read file, modified, or out of memory. */
3745 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3749 cur_line = file->lines;
3751 while ((cur_line != NULL) && (line_number < patternid))
3753 if (cur_line->type == FILE_LINE_ACTION)
3755 section_start_line_number = line_number;
3757 cur_line = cur_line->next;
3761 if ( (cur_line == NULL)
3762 || (cur_line->type != FILE_LINE_URL))
3764 /* Invalid "patternid" parameter */
3766 edit_free_file(file);
3767 return JB_ERR_CGI_PARAMS;
3770 /* At this point, the line to edit is in cur_line */
3772 freez(cur_line->raw);
3773 freez(cur_line->unprocessed);
3774 cur_line->unprocessed = new_pattern;
3776 err = edit_write_file(file);
3779 /* Error writing file */
3780 if (err == JB_ERR_FILE)
3782 /* Read-only file. */
3783 err = cgi_error_file_read_only(csp, rsp, file->filename);
3785 edit_free_file(file);
3789 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3790 (long) time(NULL), file->identifier, section_start_line_number);
3792 edit_free_file(file);
3794 return cgi_redirect(rsp, target);
3798 /*********************************************************************
3800 * Function : cgi_edit_actions_add_url
3802 * Description : CGI function that actually adds a URL pattern to
3806 * 1 : csp = Current client state (buffers, headers, etc...)
3807 * 2 : rsp = http_response data structure for output
3808 * 3 : parameters = map of cgi parameters
3811 * filename : Identifies the file to edit
3812 * ver : File's last-modified time
3813 * section : Line number of section to edit
3814 * newval : New pattern
3816 * Returns : JB_ERR_OK on success
3817 * JB_ERR_MEMORY on out-of-memory
3818 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3819 * specified or not valid.
3821 *********************************************************************/
3822 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3823 struct http_response *rsp,
3824 const struct map *parameters)
3828 struct file_line * new_line;
3829 struct editable_file * file;
3830 struct file_line * cur_line;
3831 unsigned line_number;
3835 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3837 return cgi_error_disabled(csp, rsp);
3840 err = get_number_param(csp, parameters, "s", §ionid);
3847 return JB_ERR_CGI_PARAMS;
3850 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3856 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3859 /* No filename specified, can't read file, modified, or out of memory. */
3861 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3865 cur_line = file->lines;
3867 while ((cur_line != NULL) && (line_number < sectionid))
3869 cur_line = cur_line->next;
3873 if ( (cur_line == NULL)
3874 || (cur_line->type != FILE_LINE_ACTION))
3876 /* Invalid "sectionid" parameter */
3878 edit_free_file(file);
3879 return JB_ERR_CGI_PARAMS;
3882 /* At this point, the section header is in cur_line - add after this. */
3884 /* Allocate the new line */
3885 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3886 if (new_line == NULL)
3889 edit_free_file(file);
3890 return JB_ERR_MEMORY;
3893 /* Fill in the data members of the new line */
3894 new_line->raw = NULL;
3895 new_line->prefix = NULL;
3896 new_line->unprocessed = new_pattern;
3897 new_line->type = FILE_LINE_URL;
3899 /* Link new_line into the list, after cur_line */
3900 new_line->next = cur_line->next;
3901 cur_line->next = new_line;
3903 /* Done making changes, now commit */
3905 err = edit_write_file(file);
3908 /* Error writing file */
3909 if (err == JB_ERR_FILE)
3911 /* Read-only file. */
3912 err = cgi_error_file_read_only(csp, rsp, file->filename);
3914 edit_free_file(file);
3918 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3919 (long) time(NULL), file->identifier, sectionid);
3921 edit_free_file(file);
3923 return cgi_redirect(rsp, target);
3927 /*********************************************************************
3929 * Function : cgi_edit_actions_remove_url
3931 * Description : CGI function that actually removes a URL pattern from
3935 * 1 : csp = Current client state (buffers, headers, etc...)
3936 * 2 : rsp = http_response data structure for output
3937 * 3 : parameters = map of cgi parameters
3940 * f : (filename) Identifies the file to edit
3941 * v : (version) File's last-modified time
3942 * p : (pattern) Line number of pattern to remove
3944 * Returns : JB_ERR_OK on success
3945 * JB_ERR_MEMORY on out-of-memory
3946 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3947 * specified or not valid.
3949 *********************************************************************/
3950 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3951 struct http_response *rsp,
3952 const struct map *parameters)
3955 struct editable_file * file;
3956 struct file_line * cur_line;
3957 struct file_line * prev_line;
3958 unsigned line_number;
3959 unsigned section_start_line_number = 0;
3963 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3965 return cgi_error_disabled(csp, rsp);
3968 err = get_number_param(csp, parameters, "p", &patternid);
3974 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3977 /* No filename specified, can't read file, modified, or out of memory. */
3978 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3983 cur_line = file->lines;
3985 while ((cur_line != NULL) && (line_number < patternid))
3987 if (cur_line->type == FILE_LINE_ACTION)
3989 section_start_line_number = line_number;
3991 prev_line = cur_line;
3992 cur_line = cur_line->next;
3996 if ( (cur_line == NULL)
3997 || (prev_line == NULL)
3998 || (cur_line->type != FILE_LINE_URL))
4000 /* Invalid "patternid" parameter */
4001 edit_free_file(file);
4002 return JB_ERR_CGI_PARAMS;
4005 /* At this point, the line to remove is in cur_line, and the previous
4006 * one is in prev_line
4009 /* Unlink cur_line */
4010 prev_line->next = cur_line->next;
4011 cur_line->next = NULL;
4014 edit_free_file_lines(cur_line);
4016 err = edit_write_file(file);
4019 /* Error writing file */
4020 if (err == JB_ERR_FILE)
4022 /* Read-only file. */
4023 err = cgi_error_file_read_only(csp, rsp, file->filename);
4025 edit_free_file(file);
4029 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
4030 (long) time(NULL), file->identifier, section_start_line_number);
4032 edit_free_file(file);
4034 return cgi_redirect(rsp, target);
4038 /*********************************************************************
4040 * Function : cgi_edit_actions_section_remove
4042 * Description : CGI function that actually removes a whole section from
4043 * the actions file. The section must be empty first
4044 * (else JB_ERR_CGI_PARAMS).
4047 * 1 : csp = Current client state (buffers, headers, etc...)
4048 * 2 : rsp = http_response data structure for output
4049 * 3 : parameters = map of cgi parameters
4052 * f : (filename) Identifies the file to edit
4053 * v : (version) File's last-modified time
4054 * s : (section) Line number of section to edit
4056 * Returns : JB_ERR_OK on success
4057 * JB_ERR_MEMORY on out-of-memory
4058 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4059 * specified or not valid.
4061 *********************************************************************/
4062 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
4063 struct http_response *rsp,
4064 const struct map *parameters)
4067 struct editable_file * file;
4068 struct file_line * cur_line;
4069 struct file_line * prev_line;
4070 unsigned line_number;
4074 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4076 return cgi_error_disabled(csp, rsp);
4079 err = get_number_param(csp, parameters, "s", §ionid);
4085 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4088 /* No filename specified, can't read file, modified, or out of memory. */
4089 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4093 cur_line = file->lines;
4096 while ((cur_line != NULL) && (line_number < sectionid))
4098 prev_line = cur_line;
4099 cur_line = cur_line->next;
4103 if ( (cur_line == NULL)
4104 || (cur_line->type != FILE_LINE_ACTION) )
4106 /* Invalid "sectionid" parameter */
4107 edit_free_file(file);
4108 return JB_ERR_CGI_PARAMS;
4111 if ( (cur_line->next != NULL)
4112 && (cur_line->next->type == FILE_LINE_URL) )
4114 /* Section not empty. */
4115 edit_free_file(file);
4116 return JB_ERR_CGI_PARAMS;
4119 /* At this point, the line to remove is in cur_line, and the previous
4120 * one is in prev_line
4123 /* Unlink cur_line */
4124 if (prev_line == NULL)
4126 /* Removing the first line from the file */
4127 file->lines = cur_line->next;
4131 prev_line->next = cur_line->next;
4133 cur_line->next = NULL;
4136 edit_free_file_lines(cur_line);
4138 err = edit_write_file(file);
4141 /* Error writing file */
4142 if (err == JB_ERR_FILE)
4144 /* Read-only file. */
4145 err = cgi_error_file_read_only(csp, rsp, file->filename);
4147 edit_free_file(file);
4151 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4152 (long) time(NULL), file->identifier);
4154 edit_free_file(file);
4156 return cgi_redirect(rsp, target);
4160 /*********************************************************************
4162 * Function : cgi_edit_actions_section_add
4164 * Description : CGI function that adds a new empty section to
4168 * 1 : csp = Current client state (buffers, headers, etc...)
4169 * 2 : rsp = http_response data structure for output
4170 * 3 : parameters = map of cgi parameters
4173 * f : (filename) Identifies the file to edit
4174 * v : (version) File's last-modified time
4175 * s : (section) Line number of section to add after, 0 for
4178 * Returns : JB_ERR_OK on success
4179 * JB_ERR_MEMORY on out-of-memory
4180 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4181 * specified or not valid.
4183 *********************************************************************/
4184 jb_err cgi_edit_actions_section_add(struct client_state *csp,
4185 struct http_response *rsp,
4186 const struct map *parameters)
4189 struct file_line * new_line;
4191 struct editable_file * file;
4192 struct file_line * cur_line;
4193 unsigned line_number;
4197 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4199 return cgi_error_disabled(csp, rsp);
4202 err = get_number_param(csp, parameters, "s", §ionid);
4208 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4211 /* No filename specified, can't read file, modified, or out of memory. */
4212 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4216 cur_line = file->lines;
4218 if (sectionid <= 1U)
4220 /* Add to start of file */
4221 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4223 /* There's something in the file, find the line before the first
4226 while ( (cur_line->next != NULL)
4227 && (cur_line->next->type != FILE_LINE_ACTION) )
4229 cur_line = cur_line->next;
4235 /* File starts with action line, so insert at top */
4241 /* Add after stated section. */
4242 while ((cur_line != NULL) && (line_number < sectionid))
4244 cur_line = cur_line->next;
4248 if ( (cur_line == NULL)
4249 || (cur_line->type != FILE_LINE_ACTION))
4251 /* Invalid "sectionid" parameter */
4252 edit_free_file(file);
4253 return JB_ERR_CGI_PARAMS;
4256 /* Skip through the section to find the last line in it. */
4257 while ( (cur_line->next != NULL)
4258 && (cur_line->next->type != FILE_LINE_ACTION) )
4260 cur_line = cur_line->next;
4265 /* At this point, the last line in the previous section is in cur_line
4266 * - add after this. (Or if we need to add as the first line, cur_line
4270 new_text = strdup("{}");
4271 if (NULL == new_text)
4273 edit_free_file(file);
4274 return JB_ERR_MEMORY;
4277 /* Allocate the new line */
4278 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4279 if (new_line == NULL)
4282 edit_free_file(file);
4283 return JB_ERR_MEMORY;
4286 /* Fill in the data members of the new line */
4287 new_line->raw = NULL;
4288 new_line->prefix = NULL;
4289 new_line->unprocessed = new_text;
4290 new_line->type = FILE_LINE_ACTION;
4292 if (cur_line != NULL)
4294 /* Link new_line into the list, after cur_line */
4295 new_line->next = cur_line->next;
4296 cur_line->next = new_line;
4300 /* Link new_line into the list, as first line */
4301 new_line->next = file->lines;
4302 file->lines = new_line;
4305 /* Done making changes, now commit */
4307 err = edit_write_file(file);
4310 /* Error writing file */
4311 if (err == JB_ERR_FILE)
4313 /* Read-only file. */
4314 err = cgi_error_file_read_only(csp, rsp, file->filename);
4316 edit_free_file(file);
4320 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4321 (long) time(NULL), file->identifier);
4323 edit_free_file(file);
4325 return cgi_redirect(rsp, target);
4329 /*********************************************************************
4331 * Function : cgi_edit_actions_section_swap
4333 * Description : CGI function that swaps the order of two sections
4334 * in the actions file. Note that this CGI can actually
4335 * swap any two arbitrary sections, but the GUI interface
4336 * currently only allows consecutive sections to be
4340 * 1 : csp = Current client state (buffers, headers, etc...)
4341 * 2 : rsp = http_response data structure for output
4342 * 3 : parameters = map of cgi parameters
4345 * f : (filename) Identifies the file to edit
4346 * v : (version) File's last-modified time
4347 * s1 : (section1) Line number of first section to swap
4348 * s2 : (section2) Line number of second section to swap
4350 * Returns : JB_ERR_OK on success
4351 * JB_ERR_MEMORY on out-of-memory
4352 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4353 * specified or not valid.
4355 *********************************************************************/
4356 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4357 struct http_response *rsp,
4358 const struct map *parameters)
4362 struct editable_file * file;
4363 struct file_line * cur_line;
4364 struct file_line * prev_line;
4365 struct file_line * line_before_section1;
4366 struct file_line * line_start_section1;
4367 struct file_line * line_end_section1;
4368 struct file_line * line_after_section1;
4369 struct file_line * line_before_section2;
4370 struct file_line * line_start_section2;
4371 struct file_line * line_end_section2;
4372 struct file_line * line_after_section2;
4373 unsigned line_number;
4377 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4379 return cgi_error_disabled(csp, rsp);
4382 err = get_number_param(csp, parameters, "s1", §ion1);
4383 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4389 if (section1 > section2)
4391 unsigned temp = section2;
4392 section2 = section1;
4396 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4399 /* No filename specified, can't read file, modified, or out of memory. */
4400 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4403 /* Start at the beginning... */
4405 cur_line = file->lines;
4408 /* ... find section1 ... */
4409 while ((cur_line != NULL) && (line_number < section1))
4411 prev_line = cur_line;
4412 cur_line = cur_line->next;
4416 if ( (cur_line == NULL)
4417 || (cur_line->type != FILE_LINE_ACTION) )
4419 /* Invalid "section1" parameter */
4420 edit_free_file(file);
4421 return JB_ERR_CGI_PARAMS;
4424 /* If no-op, we've validated params and can skip the rest. */
4425 if (section1 != section2)
4427 /* ... find the end of section1 ... */
4428 line_before_section1 = prev_line;
4429 line_start_section1 = cur_line;
4432 prev_line = cur_line;
4433 cur_line = cur_line->next;
4436 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4437 line_end_section1 = prev_line;
4438 line_after_section1 = cur_line;
4440 /* ... find section2 ... */
4441 while ((cur_line != NULL) && (line_number < section2))
4443 prev_line = cur_line;
4444 cur_line = cur_line->next;
4448 if ( (cur_line == NULL)
4449 || (cur_line->type != FILE_LINE_ACTION) )
4451 /* Invalid "section2" parameter */
4452 edit_free_file(file);
4453 return JB_ERR_CGI_PARAMS;
4456 /* ... find the end of section2 ... */
4457 line_before_section2 = prev_line;
4458 line_start_section2 = cur_line;
4461 prev_line = cur_line;
4462 cur_line = cur_line->next;
4465 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4466 line_end_section2 = prev_line;
4467 line_after_section2 = cur_line;
4469 /* Now have all the pointers we need. Do the swap. */
4471 /* Change the pointer to section1 to point to section2 instead */
4472 if (line_before_section1 == NULL)
4474 file->lines = line_start_section2;
4478 line_before_section1->next = line_start_section2;
4481 if (line_before_section2 == line_end_section1)
4483 /* Consecutive sections */
4484 line_end_section2->next = line_start_section1;
4488 line_end_section2->next = line_after_section1;
4489 line_before_section2->next = line_start_section1;
4492 /* Set the pointer from the end of section1 to the rest of the file */
4493 line_end_section1->next = line_after_section2;
4495 err = edit_write_file(file);
4498 /* Error writing file */
4499 if (err == JB_ERR_FILE)
4501 /* Read-only file. */
4502 err = cgi_error_file_read_only(csp, rsp, file->filename);
4504 edit_free_file(file);
4507 } /* END if (section1 != section2) */
4509 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4510 (long) time(NULL), file->identifier);
4512 edit_free_file(file);
4514 return cgi_redirect(rsp, target);
4518 /*********************************************************************
4520 * Function : javascriptify
4522 * Description : Converts a string into a form JavaScript will like.
4524 * Netscape 4's JavaScript sucks - it doesn't use
4525 * "id" parameters, so you have to set the "name"
4526 * used to submit a form element to something JavaScript
4527 * will like. (Or access the elements by index in an
4528 * array. That array contains >60 elements and will
4529 * be changed whenever we add a new action to the
4530 * editor, so I'm NOT going to use indexes that have
4531 * to be figured out by hand.)
4533 * Currently the only thing we have to worry about
4534 * is "-" ==> "_" conversion.
4536 * This is a length-preserving operation so it is
4537 * carried out in-place, no memory is allocated
4541 * 1 : identifier = String to make JavaScript-friendly.
4545 *********************************************************************/
4546 static void javascriptify(char * identifier)
4548 char * p = identifier;
4549 while (NULL != (p = strchr(p, '-')))
4556 /*********************************************************************
4558 * Function : actions_to_radio
4560 * Description : Converts a actionsfile entry into settings for
4561 * radio buttons and edit boxes on a HTML form.
4564 * 1 : exports = List of substitutions to add to.
4565 * 2 : action = Action to read
4567 * Returns : JB_ERR_OK on success
4568 * JB_ERR_MEMORY on out-of-memory
4570 *********************************************************************/
4571 static jb_err actions_to_radio(struct map * exports,
4572 const struct action_spec *action)
4574 unsigned mask = action->mask;
4575 unsigned add = action->add;
4583 mask = action->mask;
4586 /* sanity - prevents "-feature +feature" */
4590 #define DEFINE_ACTION_BOOL(name, bit) \
4591 if (!(mask & bit)) \
4593 current_mode = 'n'; \
4595 else if (add & bit) \
4597 current_mode = 'y'; \
4601 current_mode = 'x'; \
4603 if (map_radio(exports, name, "ynx", current_mode)) \
4605 return JB_ERR_MEMORY; \
4608 #define DEFINE_ACTION_STRING(name, bit, index) \
4609 DEFINE_ACTION_BOOL(name, bit); \
4612 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4615 checked = !strcmp(action->string[index], value); \
4619 checked = is_default; \
4621 mapped_param |= checked; \
4622 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4624 return JB_ERR_MEMORY; \
4627 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4628 if (map(exports, name "-param-custom", 1, \
4629 ((!mapped_param) ? "checked" : ""), 1)) \
4631 return JB_ERR_MEMORY; \
4633 if (map(exports, name "-param", 1, \
4634 (((add & bit) && !mapped_param) ? \
4635 action->string[index] : default_val), 1)) \
4637 return JB_ERR_MEMORY; \
4640 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4641 if (map(exports, name "-param", 1, \
4642 ((add & bit) ? action->string[index] : default_val), 1)) \
4644 return JB_ERR_MEMORY; \
4647 #define DEFINE_ACTION_MULTI(name, index) \
4648 if (action->multi_add[index]->first) \
4650 current_mode = 'y'; \
4652 else if (action->multi_remove_all[index]) \
4654 current_mode = 'n'; \
4656 else if (action->multi_remove[index]->first) \
4658 current_mode = 'y'; \
4662 current_mode = 'x'; \
4664 if (map_radio(exports, name, "ynx", current_mode)) \
4666 return JB_ERR_MEMORY; \
4669 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4671 #include "actionlist.h"
4673 #undef DEFINE_ACTION_MULTI
4674 #undef DEFINE_ACTION_STRING
4675 #undef DEFINE_ACTION_BOOL
4676 #undef DEFINE_ACTION_ALIAS
4677 #undef DEFINE_CGI_PARAM_CUSTOM
4678 #undef DEFINE_CGI_PARAM_RADIO
4679 #undef DEFINE_CGI_PARAM_NO_RADIO
4685 /*********************************************************************
4687 * Function : actions_from_radio
4689 * Description : Converts a map of parameters passed to a CGI function
4690 * into an actionsfile entry.
4693 * 1 : parameters = parameters to the CGI call
4694 * 2 : action = Action to change. Must be valid before
4695 * the call, actions not specified will be
4698 * Returns : JB_ERR_OK on success
4699 * JB_ERR_MEMORY on out-of-memory
4701 *********************************************************************/
4702 static jb_err actions_from_radio(const struct map * parameters,
4703 struct action_spec *action)
4708 const char * js_name;
4709 jb_err err = JB_ERR_OK;
4714 /* Statics are generally a potential race condition,
4715 * but in this case we're safe and don't need semaphores.
4716 * Be careful if you modify this function.
4718 * The js_name_arr's are never free()d, but this is no
4719 * problem, since they will only be created once and
4720 * used by all threads thereafter. -oes
4723 #define JAVASCRIPTIFY(dest_var, string) \
4725 static int first_time = 1; \
4726 static char *js_name_arr; \
4729 js_name_arr = strdup(string); \
4730 javascriptify(js_name_arr); \
4732 dest_var = js_name_arr; \
4736 #define DEFINE_ACTION_BOOL(name, bit) \
4737 JAVASCRIPTIFY(js_name, name); \
4738 ch = get_char_param(parameters, js_name); \
4741 action->add |= bit; \
4742 action->mask |= bit; \
4744 else if (ch == 'N') \
4746 action->add &= ~bit; \
4747 action->mask &= ~bit; \
4749 else if (ch == 'X') \
4751 action->add &= ~bit; \
4752 action->mask |= bit; \
4755 #define DEFINE_ACTION_STRING(name, bit, index) \
4756 JAVASCRIPTIFY(js_name, name); \
4757 ch = get_char_param(parameters, js_name); \
4761 JAVASCRIPTIFY(js_name, name "-mode"); \
4762 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4763 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4765 JAVASCRIPTIFY(js_name, name "-param"); \
4766 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4768 if (param != NULL) \
4770 if (NULL == (param_dup = strdup(param))) \
4772 return JB_ERR_MEMORY; \
4774 freez(action->string[index]); \
4775 action->add |= bit; \
4776 action->mask |= bit; \
4777 action->string[index] = param_dup; \
4780 else if (ch == 'N') \
4782 if (action->add & bit) \
4784 freez(action->string[index]); \
4786 action->add &= ~bit; \
4787 action->mask &= ~bit; \
4789 else if (ch == 'X') \
4791 if (action->add & bit) \
4793 freez(action->string[index]); \
4795 action->add &= ~bit; \
4796 action->mask |= bit; \
4799 #define DEFINE_ACTION_MULTI(name, index) \
4800 JAVASCRIPTIFY(js_name, name); \
4801 ch = get_char_param(parameters, js_name); \
4806 else if (ch == 'N') \
4808 list_remove_all(action->multi_add[index]); \
4809 list_remove_all(action->multi_remove[index]); \
4810 action->multi_remove_all[index] = 1; \
4812 else if (ch == 'X') \
4814 list_remove_all(action->multi_add[index]); \
4815 list_remove_all(action->multi_remove[index]); \
4816 action->multi_remove_all[index] = 0; \
4819 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4821 #include "actionlist.h"
4823 #undef DEFINE_ACTION_MULTI
4824 #undef DEFINE_ACTION_STRING
4825 #undef DEFINE_ACTION_BOOL
4826 #undef DEFINE_ACTION_ALIAS
4827 #undef JAVASCRIPTIFY
4831 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4834 #ifdef FEATURE_TOGGLE
4835 /*********************************************************************
4837 * Function : cgi_toggle
4839 * Description : CGI function that adds a new empty section to
4843 * 1 : csp = Current client state (buffers, headers, etc...)
4844 * 2 : rsp = http_response data structure for output
4845 * 3 : parameters = map of cgi parameters
4848 * set : If present, how to change toggle setting:
4849 * "enable", "disable", "toggle", or none (default).
4850 * mini : If present, use mini reply template.
4852 * Returns : JB_ERR_OK on success
4853 * JB_ERR_MEMORY on out-of-memory
4855 *********************************************************************/
4856 jb_err cgi_toggle(struct client_state *csp,
4857 struct http_response *rsp,
4858 const struct map *parameters)
4860 struct map *exports;
4862 const char *template_name;
4868 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4870 return cgi_error_disabled(csp, rsp);
4873 mode = get_char_param(parameters, "set");
4878 global_toggle_state = 1;
4880 else if (mode == 'D')
4883 global_toggle_state = 0;
4885 else if (mode == 'T')
4888 global_toggle_state = !global_toggle_state;
4891 if (NULL == (exports = default_exports(csp, "toggle")))
4893 return JB_ERR_MEMORY;
4896 template_name = (get_char_param(parameters, "mini")
4900 return template_fill_for_cgi(csp, template_name, exports, rsp);
4902 #endif /* def FEATURE_TOGGLE */