1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.62 2008/08/31 15:59:02 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.62 2008/08/31 15:59:02 fabiankeil
46 * There's no reason to let remote toggling support depend
47 * on FEATURE_CGI_EDIT_ACTIONS, so make sure it doesn't.
49 * Revision 1.61 2008/03/24 18:12:52 fabiankeil
50 * Use sizeof() more often.
52 * Revision 1.60 2008/03/15 14:52:35 fabiankeil
53 * Add CGI editor support for the "disable all filters of this type"
54 * directives "-client-header-filter", "-server-header-filter",
55 * "-client-header-tagger" and "-server-header-tagger".
57 * Revision 1.59 2008/03/08 16:25:56 fabiankeil
58 * After three file modification time mismatches, turn the CGI editor off.
60 * Revision 1.58 2007/11/28 17:57:01 fabiankeil
61 * Fix double free in cgi_edit_actions_list().
62 * Reported by adlab in BR#1840145.
64 * Revision 1.57 2007/10/27 13:32:23 fabiankeil
65 * Plug minor 5-year-old memory leak. Spotted by
66 * Valgrind and triggered by Privoxy-Regression-Test.
68 * Revision 1.56 2007/08/05 13:47:03 fabiankeil
69 * #1763173 from Stefan Huehner: s@const static@static const@.
71 * Revision 1.55 2007/05/31 11:50:20 fabiankeil
72 * Re-enable support for old-school URLs like
73 * http://config.privoxy.org/edit-actions-list?f=default
74 * in the action editor.
76 * They are no longer used by the CGI pages, but make it easier
77 * to reach the editor directly, without knowing the requested
78 * file's index in csp->config->actions_file[].
80 * Revision 1.54 2007/05/14 10:33:51 fabiankeil
81 * - Use strlcpy() and strlcat() instead of strcpy() and strcat().
83 * Revision 1.53 2007/04/15 16:39:20 fabiankeil
84 * Introduce tags as alternative way to specify which
85 * actions apply to a request. At the moment tags can be
86 * created based on client and server headers.
88 * Revision 1.52 2007/04/12 10:41:23 fabiankeil
89 * - Don't mistake VC++'s _snprintf() for a snprintf() replacement.
90 * - Move some cgi_edit_actions_for_url() variables into structs.
91 * - Remove bogus comment.
93 * Revision 1.51 2007/04/08 13:21:05 fabiankeil
94 * Reference action files in CGI URLs by id instead
95 * of using the first part of the file name.
96 * Fixes BR 1694250 and BR 1590556.
98 * Revision 1.50 2007/03/29 11:40:34 fabiankeil
99 * Divide @filter-params@ into @client-header-filter-params@
100 * @content-filter-params@ and @server-header-filter-params@.
102 * Revision 1.49 2007/03/20 15:16:34 fabiankeil
103 * Use dedicated header filter actions instead of abusing "filter".
104 * Replace "filter-client-headers" and "filter-client-headers"
105 * with "server-header-filter" and "client-header-filter".
107 * Revision 1.48 2007/02/13 14:35:25 fabiankeil
108 * Replace hash escaping code to prevent
109 * crashes, memory and file corruption.
111 * Revision 1.47 2006/12/28 18:04:25 fabiankeil
112 * Fixed gcc43 conversion warnings.
114 * Revision 1.46 2006/12/27 18:44:52 fabiankeil
115 * Stop shadowing string.h's index().
117 * Revision 1.45 2006/12/21 12:57:48 fabiankeil
118 * Add config option "split-large-forms"
119 * to work around the browser bug reported
122 * Revision 1.44 2006/12/09 13:49:16 fabiankeil
123 * Fix configure option --disable-toggle.
124 * Thanks to Peter Thoenen for reporting this.
126 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
127 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
128 * with what was really the latest development (the v_3_0_branch branch)
130 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
131 * Remove a little residual debugging info
133 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
134 * Multiple filter file support
136 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
137 * Undo some damaging memory leak patches
139 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
140 * Removing a double free
142 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
143 * A few memory leaks plugged (mostly on error paths)
145 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
146 * Moved cgi_error_disabled() from cgiedit.c to
147 * cgi.c to re-enable build with --disable-editor.
148 * Fixes Bug #892744. Thanks to Matthew Fischer
151 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
152 * One line lost in last commit
154 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
155 * - All edit functions that redirect back to the list page
156 * now use cgi_redirect
157 * - All redirects now contain useless parameter "foo", whose
158 * value are raw seconds since epoch, in order to force
159 * Opera and Konqueror to properly reload the list. Closes
162 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
163 * Cosmetic: Renamed cryptic variable
165 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
166 * Fix: Don't free uninitialized struct editable_file
168 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
169 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
171 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
172 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
174 * Revision 1.41 2002/05/21 19:09:45 oes
175 * - Made Add/Edit/Remove URL Submit and Cancel
176 * buttons jump back to relevant section in eal
177 * - Bugfix: remove-url-form needs p export
179 * Revision 1.40 2002/05/19 11:34:35 jongfoster
180 * Handling read-only actions files better - report the actual
181 * error, not "Out of memory"!
184 * http://sourceforge.net/tracker/index.php?func=detail
185 * &aid=557905&group_id=11118&atid=111118
187 * Revision 1.39 2002/05/12 21:39:15 jongfoster
188 * - Adding Doxygen-style comments to structures and #defines.
189 * - Correcting function comments
191 * Revision 1.38 2002/05/03 23:00:38 jongfoster
192 * Support for templates for "standard actions" buttons.
195 * Revision 1.37 2002/04/30 11:14:52 oes
196 * Made csp the first parameter in *action_to_html
198 * Revision 1.36 2002/04/26 21:53:30 jongfoster
199 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
201 * Revision 1.35 2002/04/26 21:50:02 jongfoster
202 * Honouring default exports in edit-actions-for-url-filter template.
204 * Revision 1.34 2002/04/26 12:54:17 oes
205 * Adaptions to changes in actions.c
207 * Revision 1.33 2002/04/24 02:17:47 oes
208 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
210 * - Activated Jon's code for editing multiple AFs
211 * - cgi_edit_list_actions now provides context-sensitive
212 * help, looks up all action sets from standard.action and
213 * makes buttons for them in the catchall section
214 * - cgi_edit_action_submit now honors a p parameter, looks up
215 * the corresponding action set, and sets the catchall pattern's
216 * actions accordingly.
218 * Revision 1.32 2002/04/19 16:55:31 jongfoster
219 * Fixing newline problems. If we do our own text file newline
220 * mangling, we don't want the library to do any, so we need to
221 * open the files in *binary* mode.
223 * Revision 1.31 2002/04/18 19:21:08 jongfoster
224 * Added code to detect "conventional" action files, that start
225 * with a set of actions for all URLs (the pattern "/").
226 * These are special-cased in the "edit-actions-list" CGI, so
227 * that a special UI can be written for them.
229 * Revision 1.30 2002/04/10 13:38:35 oes
230 * load_template signature changed
232 * Revision 1.29 2002/04/08 16:59:08 oes
235 * Revision 1.28 2002/03/27 12:30:29 oes
236 * Deleted unsused variable
238 * Revision 1.27 2002/03/26 23:06:04 jongfoster
239 * Removing duplicate @ifs on the toggle page
241 * Revision 1.26 2002/03/26 22:59:17 jongfoster
242 * Fixing /toggle to display status consistently.
244 * Revision 1.25 2002/03/26 22:29:54 swa
245 * we have a new homepage!
247 * Revision 1.24 2002/03/24 15:23:33 jongfoster
250 * Revision 1.23 2002/03/24 13:32:41 swa
251 * name change related issues
253 * Revision 1.22 2002/03/24 13:25:43 swa
254 * name change related issues
256 * Revision 1.21 2002/03/22 18:02:48 jongfoster
257 * Fixing remote toggle
259 * Revision 1.20 2002/03/16 20:28:34 oes
260 * Added descriptions to the filters so users will know what they select in the cgi editor
262 * Revision 1.19 2002/03/16 18:38:14 jongfoster
263 * Stopping stupid or malicious users from breaking the actions
264 * file using the web-based editor.
266 * Revision 1.18 2002/03/16 14:57:44 jongfoster
267 * Full support for enabling/disabling modular filters.
269 * Revision 1.17 2002/03/16 14:26:42 jongfoster
270 * First version of modular filters support - READ ONLY!
271 * Fixing a double-free bug in the out-of-memory handling in map_radio().
273 * Revision 1.16 2002/03/07 03:46:17 oes
274 * Fixed compiler warnings
276 * Revision 1.15 2002/03/06 22:54:35 jongfoster
277 * Automated function-comment nitpicking.
279 * Revision 1.14 2002/03/05 00:24:51 jongfoster
280 * Patch to always edit the current actions file.
282 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
283 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
285 * Revision 1.12 2002/03/03 09:18:03 joergs
286 * Made jumbjuster work on AmigaOS again.
288 * Revision 1.11 2002/01/23 01:03:31 jongfoster
289 * Fixing gcc [CygWin] compiler warnings
291 * Revision 1.10 2002/01/23 00:22:59 jongfoster
292 * Adding new function cgi_edit_actions_section_swap(), to reorder
295 * Adding get_url_spec_param() to get a validated URL pattern.
297 * Moving edit_read_line() out of this file and into loaders.c.
299 * Adding missing html_encode() to many CGI functions.
301 * Moving the functions that #include actionlist.h to the end of the file,
302 * because the Visual C++ 97 debugger gets extremely confused if you try
303 * to debug any code that comes after them in the file.
305 * Major optimizations in cgi_edit_actions_list() to reduce the size of
306 * the generated HTML (down 40% from 550k to 304k), with major side-effects
307 * throughout the editor and templates. In particular, the length of the
308 * URLs throughout the editor has been drastically reduced, by cutting
309 * paramater names down to 1 character and CGI names down to 3-4
310 * characters, by removing all non-essential CGI paramaters even at the
311 * expense of having to re-read the actions file for the most trivial
312 * page, and by using relative rather than absolute URLs. This means
313 * that this (typical example):
315 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
316 * filename=ijb&ver=1011487572&section=12&pattern=13
317 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
321 * <a href="eau?f=ijb&v=1011487572&p=13">
323 * Revision 1.9 2002/01/17 20:56:22 jongfoster
324 * Replacing hard references to the URL of the config interface
325 * with #defines from project.h
327 * Revision 1.8 2001/11/30 23:35:51 jongfoster
328 * Renaming actionsfile to ijb.action
330 * Revision 1.7 2001/11/13 00:28:24 jongfoster
331 * - Renaming parameters from edit-actions-for-url so that they only
332 * contain legal JavaScript characters. If we wanted to write
333 * JavaScript that worked with Netscape 4, this is nessacery.
334 * (Note that at the moment the JavaScript doesn't actually work
335 * with Netscape 4, but now this is purely a template issue, not
336 * one affecting code).
337 * - Adding new CGIs for use by non-JavaScript browsers:
338 * edit-actions-url-form
339 * edit-actions-add-url-form
340 * edit-actions-remove-url-form
343 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
344 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
345 * by and __OS2__ ifdef.
347 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
348 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
349 * threads to call select() simultaneously. So, it's time to do a real, live,
350 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
351 * (native). Both versions will work, but using __OS2__ offers multi-threading.
353 * Revision 1.4 2001/10/23 21:48:19 jongfoster
354 * Cleaning up error handling in CGI functions - they now send back
355 * a HTML error page and should never cause a FATAL error. (Fixes one
356 * potential source of "denial of service" attacks).
358 * CGI actions file editor that works and is actually useful.
360 * Ability to toggle JunkBuster remotely using a CGI call.
362 * You can turn off both the above features in the main configuration
363 * file, e.g. if you are running a multi-user proxy.
365 * Revision 1.3 2001/10/14 22:12:49 jongfoster
366 * New version of CGI-based actionsfile editor.
367 * Major changes, including:
368 * - Completely new file parser and file output routines
369 * - edit-actions CGI renamed edit-actions-for-url
370 * - All CGIs now need a filename parameter, except for...
371 * - New CGI edit-actions which doesn't need a filename,
372 * to allow you to start the editor up.
373 * - edit-actions-submit now works, and now automatically
374 * redirects you back to the main edit-actions-list handler.
376 * Revision 1.2 2001/09/16 17:05:14 jongfoster
377 * Removing unused #include showarg.h
379 * Revision 1.1 2001/09/16 15:47:37 jongfoster
380 * First version of CGI-based edit interface. This is very much a
381 * work-in-progress, and you can't actually use it to edit anything
382 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
383 * to have any effect.
386 **********************************************************************/
392 * FIXME: Following includes copied from cgi.c - which are actually needed?
397 #include <sys/types.h>
401 #include <sys/stat.h>
406 #include "cgisimple.h"
410 #include "miscutil.h"
413 #ifdef FEATURE_TOGGLE
414 /* loadcfg.h is for global_toggle_state only */
416 #endif /* def FEATURE_TOGGLE */
417 #include "urlmatch.h"
419 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
422 #ifdef FEATURE_CGI_EDIT_ACTIONS
425 * A line in an editable_file.
429 /** Next entry in the linked list */
430 struct file_line * next;
432 /** The raw data, to write out if this line is unmodified. */
435 /** Comments and/or whitespace to put before this line if it's modified
436 and then written out. */
439 /** The actual data, as a string. Line continuation and comment removal
440 are performed on the data read from file before it's stored here, so
441 it will be a single line of data. */
444 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
447 /** The actual data, processed into some sensible data type. */
451 /** An action specification. */
452 struct action_spec action[1];
454 /** A name=value pair. */
458 /** The name in the name=value pair. */
461 /** The value in the name=value pair, as a string. */
464 /** The value in the name=value pair, as an integer. */
469 /* Add more data types here... e.g.
472 struct url_spec url[1];
476 struct action_spec action[1];
486 /** This file_line has not been processed yet. */
487 #define FILE_LINE_UNPROCESSED 1
489 /** This file_line is blank. Can only appear at the end of a file, due to
490 the way the parser works. */
491 #define FILE_LINE_BLANK 2
493 /** This file_line says {{alias}}. */
494 #define FILE_LINE_ALIAS_HEADER 3
496 /** This file_line defines an alias. */
497 #define FILE_LINE_ALIAS_ENTRY 4
499 /** This file_line defines an {action}. */
500 #define FILE_LINE_ACTION 5
502 /** This file_line specifies a URL pattern. */
503 #define FILE_LINE_URL 6
505 /** This file_line says {{settings}}. */
506 #define FILE_LINE_SETTINGS_HEADER 7
508 /** This file_line is in a {{settings}} block. */
509 #define FILE_LINE_SETTINGS_ENTRY 8
511 /** This file_line says {{description}}. */
512 #define FILE_LINE_DESCRIPTION_HEADER 9
514 /** This file_line is in a {{description}} block. */
515 #define FILE_LINE_DESCRIPTION_ENTRY 10
518 * Number of file modification time mismatches
519 * before the CGI editor gets turned off.
521 #define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
524 * A configuration file, in a format that can be edited and written back to
529 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
530 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
531 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
532 const char * version_str; /**< Last modification time, as a string. For CGI param. */
533 /**< Can be used in URL without using url_param(). */
534 unsigned version; /**< Last modification time - prevents chaos with
535 the browser's "back" button. Note that this is a
536 time_t cast to an unsigned. When comparing, always
537 cast the time_t to an unsigned, and *NOT* vice-versa.
538 This may lose the top few bits, but they're not
539 significant anyway. */
540 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
541 Note that changing this after the file has been
542 read in will cause a mess. */
543 struct file_line * parse_error; /**< On parse error, this is the offending line. */
544 const char * parse_error_text; /**< On parse error, this is the problem.
545 (Statically allocated) */
549 * Information about the filter types.
550 * Used for macro replacement in cgi_edit_actions_for_url.
552 struct filter_type_info
554 const int multi_action_index; /**< The multi action index as defined in project.h */
555 const char *macro_name; /**< Name of the macro that has to be replaced
556 with the prepared templates.
557 For example "content-filter-params" */
558 const char *type; /**< Name of the filter type,
559 for example "server-header-filter". */
560 /* XXX: check if these two can be combined. */
561 const char *disable_all_option; /**< Name of the catch-all radio option that has
562 to be checked or unchecked for this filter type. */
563 const char *disable_all_param; /**< Name of the parameter that causes all filters of
564 this type to be disabled. */
565 const char *abbr_type; /**< Abbreviation of the filter type, usually the
566 first or second character capitalized */
567 const char *anchor; /**< Anchor for the User Manual link,
568 for example "SERVER-HEADER-FILTER" */
571 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
572 static const struct filter_type_info filter_type_info[] =
576 "content-filter-params", "filter",
577 "filter-all", "filter_all",
581 ACTION_MULTI_CLIENT_HEADER_FILTER,
582 "client-header-filter-params", "client-header-filter",
583 "client-header-filter-all", "client_header_filter_all",
584 "C", "CLIENT-HEADER-FILTER"
587 ACTION_MULTI_SERVER_HEADER_FILTER,
588 "server-header-filter-params", "server-header-filter",
589 "server-header-filter-all", "server_header_filter_all",
590 "S", "SERVER-HEADER-FILTER"
593 ACTION_MULTI_CLIENT_HEADER_TAGGER,
594 "client-header-tagger-params", "client-header-tagger",
595 "client-header-tagger-all", "client_header_tagger_all",
596 "L", "CLIENT-HEADER-TAGGER"
599 ACTION_MULTI_SERVER_HEADER_TAGGER,
600 "server-header-tagger-params", "server-header-tagger",
601 "server-header-tagger-all", "server_header_tagger_all",
602 "E", "SERVER-HEADER-TAGGER"
606 /* FIXME: Following non-static functions should be prototyped in .h or made static */
608 /* Functions to read and write arbitrary config files */
609 jb_err edit_read_file(struct client_state *csp,
610 const struct map *parameters,
612 struct editable_file **pfile);
613 jb_err edit_write_file(struct editable_file * file);
614 void edit_free_file(struct editable_file * file);
616 /* Functions to read and write actions files */
617 jb_err edit_parse_actions_file(struct editable_file * file);
618 jb_err edit_read_actions_file(struct client_state *csp,
619 struct http_response *rsp,
620 const struct map *parameters,
622 struct editable_file **pfile);
625 jb_err cgi_error_modified(struct client_state *csp,
626 struct http_response *rsp,
627 const char *filename);
628 jb_err cgi_error_parse(struct client_state *csp,
629 struct http_response *rsp,
630 struct editable_file *file);
631 jb_err cgi_error_file(struct client_state *csp,
632 struct http_response *rsp,
633 const char *filename);
634 jb_err cgi_error_file_read_only(struct client_state *csp,
635 struct http_response *rsp,
636 const char *filename);
638 /* Internal arbitrary config file support functions */
639 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
640 static void edit_free_file_lines(struct file_line * first_line);
642 /* Internal actions file support functions */
643 static int match_actions_file_header_line(const char * line, const char * name);
644 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
646 /* Internal parameter parsing functions */
647 static jb_err get_url_spec_param(struct client_state *csp,
648 const struct map *parameters,
653 /* Internal actionsfile <==> HTML conversion functions */
654 static jb_err map_radio(struct map * exports,
655 const char * optionname,
658 static jb_err actions_to_radio(struct map * exports,
659 const struct action_spec *action);
660 static jb_err actions_from_radio(const struct map * parameters,
661 struct action_spec *action);
664 static jb_err map_copy_parameter_html(struct map *out,
665 const struct map *in,
667 #if 0 /* unused function */
668 static jb_err map_copy_parameter_url(struct map *out,
669 const struct map *in,
671 #endif /* unused function */
673 static jb_err get_file_name_param(struct client_state *csp,
674 const struct map *parameters,
675 const char *param_name,
676 const char **pfilename);
678 /* Internal convenience functions */
679 static char *section_target(const unsigned sectionid);
681 /*********************************************************************
683 * Function : section_target
685 * Description : Given an unsigned (section id) n, produce a dynamically
686 * allocated string of the form #l<n>, for use in link
689 * XXX: The hash should be moved into the templates
690 * to make this function more generic and render
691 * stringify() obsolete.
694 * 1 : sectionid = start line number of section
696 * Returns : String with link target, or NULL if out of
699 *********************************************************************/
700 static char *section_target(const unsigned sectionid)
704 snprintf(buf, sizeof(buf), "#l%d", sectionid);
710 /*********************************************************************
712 * Function : stringify
714 * Description : Convert a number into a dynamically allocated string.
717 * 1 : number = The number to convert.
719 * Returns : String with link target, or NULL if out of memory
721 *********************************************************************/
722 static char *stringify(const unsigned number)
726 snprintf(buf, sizeof(buf), "%i", number);
731 /*********************************************************************
733 * Function : map_copy_parameter_html
735 * Description : Copy a CGI parameter from one map to another, HTML
739 * 1 : out = target map
740 * 2 : in = source map
741 * 3 : name = name of cgi parameter to copy
743 * Returns : JB_ERR_OK on success
744 * JB_ERR_MEMORY on out-of-memory
745 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
748 *********************************************************************/
749 static jb_err map_copy_parameter_html(struct map *out,
750 const struct map *in,
760 value = lookup(in, name);
761 err = map(out, name, 1, html_encode(value), 0);
768 else if (*value == '\0')
770 return JB_ERR_CGI_PARAMS;
779 #if 0 /* unused function */
780 /*********************************************************************
782 * Function : map_copy_parameter_url
784 * Description : Copy a CGI parameter from one map to another, URL
788 * 1 : out = target map
789 * 2 : in = source map
790 * 3 : name = name of cgi parameter to copy
792 * Returns : JB_ERR_OK on success
793 * JB_ERR_MEMORY on out-of-memory
794 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
797 *********************************************************************/
798 static jb_err map_copy_parameter_url(struct map *out,
799 const struct map *in,
809 value = lookup(in, name);
810 err = map(out, name, 1, url_encode(value), 0);
817 else if (*value == '\0')
819 return JB_ERR_CGI_PARAMS;
826 #endif /* 0 - unused function */
829 /*********************************************************************
831 * Function : cgi_edit_actions_url_form
833 * Description : CGI function that displays a form for
837 * 1 : csp = Current client state (buffers, headers, etc...)
838 * 2 : rsp = http_response data structure for output
839 * 3 : parameters = map of cgi parameters
842 * i : (action index) Identifies the file to edit
843 * v : (version) File's last-modified time
844 * p : (pattern) Line number of pattern to edit
846 * Returns : JB_ERR_OK on success
847 * JB_ERR_MEMORY on out-of-memory
848 * JB_ERR_CGI_PARAMS if the CGI parameters are not
849 * specified or not valid.
851 *********************************************************************/
852 jb_err cgi_edit_actions_url_form(struct client_state *csp,
853 struct http_response *rsp,
854 const struct map *parameters)
856 struct map * exports;
858 struct editable_file * file;
859 struct file_line * cur_line;
860 unsigned line_number;
861 unsigned section_start_line_number = 0;
868 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
870 return cgi_error_disabled(csp, rsp);
873 err = get_number_param(csp, parameters, "p", &patternid);
879 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
882 /* No filename specified, can't read file, modified, or out of memory. */
883 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
886 cur_line = file->lines;
888 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
890 if (cur_line->type == FILE_LINE_ACTION)
892 section_start_line_number = line_number;
894 cur_line = cur_line->next;
897 if ( (cur_line == NULL)
898 || (line_number != patternid)
900 || (cur_line->type != FILE_LINE_URL))
902 /* Invalid "patternid" parameter */
903 edit_free_file(file);
904 return JB_ERR_CGI_PARAMS;
907 if (NULL == (exports = default_exports(csp, NULL)))
909 edit_free_file(file);
910 return JB_ERR_MEMORY;
913 err = map(exports, "f", 1, stringify(file->identifier), 0);
914 if (!err) err = map(exports, "v", 1, file->version_str, 1);
915 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
916 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
917 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
919 edit_free_file(file);
927 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
931 /*********************************************************************
933 * Function : cgi_edit_actions_add_url_form
935 * Description : CGI function that displays a form for
939 * 1 : csp = Current client state (buffers, headers, etc...)
940 * 2 : rsp = http_response data structure for output
941 * 3 : parameters = map of cgi parameters
944 * f : (filename) Identifies the file to edit
945 * v : (version) File's last-modified time
946 * s : (section) Line number of section to edit
948 * Returns : JB_ERR_OK on success
949 * JB_ERR_MEMORY on out-of-memory
950 * JB_ERR_CGI_PARAMS if the CGI parameters are not
951 * specified or not valid.
953 *********************************************************************/
954 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
955 struct http_response *rsp,
956 const struct map *parameters)
965 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
967 return cgi_error_disabled(csp, rsp);
970 if (NULL == (exports = default_exports(csp, NULL)))
972 return JB_ERR_MEMORY;
975 err = map_copy_parameter_html(exports, parameters, "f");
976 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
977 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
985 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
989 /*********************************************************************
991 * Function : cgi_edit_actions_remove_url_form
993 * Description : CGI function that displays a form for
997 * 1 : csp = Current client state (buffers, headers, etc...)
998 * 2 : rsp = http_response data structure for output
999 * 3 : parameters = map of cgi parameters
1002 * f : (number) The action file identifier.
1003 * v : (version) File's last-modified time
1004 * p : (pattern) Line number of pattern to edit
1006 * Returns : JB_ERR_OK on success
1007 * JB_ERR_MEMORY on out-of-memory
1008 * JB_ERR_CGI_PARAMS if the CGI parameters are not
1009 * specified or not valid.
1011 *********************************************************************/
1012 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
1013 struct http_response *rsp,
1014 const struct map *parameters)
1016 struct map * exports;
1018 struct editable_file * file;
1019 struct file_line * cur_line;
1020 unsigned line_number;
1021 unsigned section_start_line_number = 0;
1028 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1030 return cgi_error_disabled(csp, rsp);
1033 err = get_number_param(csp, parameters, "p", &patternid);
1039 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
1042 /* No filename specified, can't read file, modified, or out of memory. */
1043 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
1046 cur_line = file->lines;
1048 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
1050 if (cur_line->type == FILE_LINE_ACTION)
1052 section_start_line_number = line_number;
1054 cur_line = cur_line->next;
1057 if ( (cur_line == NULL)
1058 || (line_number != patternid)
1060 || (cur_line->type != FILE_LINE_URL))
1062 /* Invalid "patternid" parameter */
1063 edit_free_file(file);
1064 return JB_ERR_CGI_PARAMS;
1067 if (NULL == (exports = default_exports(csp, NULL)))
1069 edit_free_file(file);
1070 return JB_ERR_MEMORY;
1073 err = map(exports, "f", 1, stringify(file->identifier), 0);
1074 if (!err) err = map(exports, "v", 1, file->version_str, 1);
1075 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
1076 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
1077 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
1078 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
1080 edit_free_file(file);
1088 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
1092 /*********************************************************************
1094 * Function : edit_write_file
1096 * Description : Write a complete file to disk.
1099 * 1 : file = File to write.
1101 * Returns : JB_ERR_OK on success
1102 * JB_ERR_FILE on error writing to file.
1103 * JB_ERR_MEMORY on out of memory
1105 *********************************************************************/
1106 jb_err edit_write_file(struct editable_file * file)
1109 struct file_line * cur_line;
1110 struct stat statbuf[1];
1111 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
1112 digits in time_t, assuming this is a 64-bit
1113 machine, plus null terminator, plus one
1117 assert(file->filename);
1119 if (NULL == (fp = fopen(file->filename, "wb")))
1124 cur_line = file->lines;
1125 while (cur_line != NULL)
1129 if (fputs(cur_line->raw, fp) < 0)
1137 if (cur_line->prefix)
1139 if (fputs(cur_line->prefix, fp) < 0)
1145 if (cur_line->unprocessed)
1148 if (NULL != strchr(cur_line->unprocessed, '#'))
1150 /* Must quote '#' characters */
1157 /* Count number of # characters, so we know length of output string */
1158 src = cur_line->unprocessed;
1159 while (NULL != (src = strchr(src, '#')))
1164 assert(numhash > 0);
1166 /* Allocate new memory for string */
1167 len = strlen(cur_line->unprocessed) + (size_t)numhash;
1168 if (NULL == (str = malloc(len + 1)))
1170 /* Uh oh, just trashed file! */
1172 return JB_ERR_MEMORY;
1175 /* Copy string but quote hashes */
1176 src = cur_line->unprocessed;
1184 assert(numhash >= 0);
1190 assert(numhash == 0);
1191 assert(strlen(str) == len);
1192 assert(str == dest - len);
1193 assert(src - len <= cur_line->unprocessed);
1195 if ((strlen(str) != len) || (numhash != 0))
1198 * Escaping didn't work as expected, go spread the news.
1199 * Only reached in non-debugging builds.
1201 log_error(LOG_LEVEL_ERROR,
1202 "Looks like hash escaping failed. %s might be corrupted now.",
1206 if (fputs(str, fp) < 0)
1217 /* Can write without quoting '#' characters. */
1218 if (fputs(cur_line->unprocessed, fp) < 0)
1224 if (fputs(NEWLINE(file->newline), fp) < 0)
1232 /* FIXME: Write data from file->data->whatever */
1236 cur_line = cur_line->next;
1242 /* Update the version stamp in the file structure, since we just
1243 * wrote to the file & changed it's date.
1245 if (stat(file->filename, statbuf) < 0)
1247 /* Error, probably file not found. */
1250 file->version = (unsigned)statbuf->st_mtime;
1252 /* Correct file->version_str */
1253 freez(file->version_str);
1254 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1255 version_buf[sizeof(version_buf)-1] = '\0';
1256 file->version_str = strdup(version_buf);
1257 if (version_buf == NULL)
1259 return JB_ERR_MEMORY;
1266 /*********************************************************************
1268 * Function : edit_free_file
1270 * Description : Free a complete file in memory.
1273 * 1 : file = Data structure to free.
1277 *********************************************************************/
1278 void edit_free_file(struct editable_file * file)
1282 /* Silently ignore NULL pointer */
1286 edit_free_file_lines(file->lines);
1287 freez(file->version_str);
1289 file->parse_error_text = NULL; /* Statically allocated */
1290 file->parse_error = NULL;
1296 /*********************************************************************
1298 * Function : edit_free_file_lines
1300 * Description : Free an entire linked list of file lines.
1303 * 1 : first_line = Data structure to free.
1307 *********************************************************************/
1308 static void edit_free_file_lines(struct file_line * first_line)
1310 struct file_line * next_line;
1312 while (first_line != NULL)
1314 next_line = first_line->next;
1315 first_line->next = NULL;
1316 freez(first_line->raw);
1317 freez(first_line->prefix);
1318 freez(first_line->unprocessed);
1319 switch(first_line->type)
1321 case 0: /* special case if memory zeroed */
1322 case FILE_LINE_UNPROCESSED:
1323 case FILE_LINE_BLANK:
1324 case FILE_LINE_ALIAS_HEADER:
1325 case FILE_LINE_SETTINGS_HEADER:
1326 case FILE_LINE_DESCRIPTION_HEADER:
1327 case FILE_LINE_DESCRIPTION_ENTRY:
1328 case FILE_LINE_ALIAS_ENTRY:
1330 /* No data is stored for these */
1333 case FILE_LINE_ACTION:
1334 free_action(first_line->data.action);
1337 case FILE_LINE_SETTINGS_ENTRY:
1338 freez(first_line->data.setting.name);
1339 freez(first_line->data.setting.svalue);
1342 /* Should never happen */
1346 first_line->type = 0; /* paranoia */
1348 first_line = next_line;
1353 /*********************************************************************
1355 * Function : match_actions_file_header_line
1357 * Description : Match an actions file {{header}} line
1360 * 1 : line = String from file
1361 * 2 : name = Header to match against
1363 * Returns : 0 iff they match.
1365 *********************************************************************/
1366 static int match_actions_file_header_line(const char * line, const char * name)
1374 if ((line[0] != '{') || (line[1] != '{'))
1380 /* Look for optional whitespace */
1381 while ( (*line == ' ') || (*line == '\t') )
1386 /* Look for the specified name (case-insensitive) */
1388 if (0 != strncmpic(line, name, len))
1394 /* Look for optional whitespace */
1395 while ( (*line == ' ') || (*line == '\t') )
1400 /* Look for "}}" and end of string*/
1401 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1411 /*********************************************************************
1413 * Function : match_actions_file_header_line
1415 * Description : Match an actions file {{header}} line
1418 * 1 : line = String from file. Must not start with
1419 * whitespace (else infinite loop!)
1420 * 2 : pname = Destination for name
1421 * 2 : pvalue = Destination for value
1423 * Returns : JB_ERR_OK on success
1424 * JB_ERR_MEMORY on out-of-memory
1425 * JB_ERR_PARSE if there's no "=" sign, or if there's
1426 * nothing before the "=" sign (but empty
1427 * values *after* the "=" sign are legal).
1429 *********************************************************************/
1430 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1432 const char * name_end;
1433 const char * value_start;
1439 assert(*line != ' ');
1440 assert(*line != '\t');
1445 value_start = strchr(line, '=');
1446 if ((value_start == NULL) || (value_start == line))
1448 return JB_ERR_PARSE;
1451 name_end = value_start - 1;
1453 /* Eat any whitespace before the '=' */
1454 while ((*name_end == ' ') || (*name_end == '\t'))
1457 * we already know we must have at least 1 non-ws char
1458 * at start of buf - no need to check
1463 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1464 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1466 return JB_ERR_MEMORY;
1468 strncpy(*pname, line, name_len);
1469 (*pname)[name_len] = '\0';
1471 /* Eat any the whitespace after the '=' */
1473 while ((*value_start == ' ') || (*value_start == '\t'))
1478 if (NULL == (*pvalue = strdup(value_start)))
1482 return JB_ERR_MEMORY;
1489 /*********************************************************************
1491 * Function : edit_parse_actions_file
1493 * Description : Parse an actions file in memory.
1495 * Passed linked list must have the "data" member
1496 * zeroed, and must contain valid "next" and
1497 * "unprocessed" fields. The "raw" and "prefix"
1498 * fields are ignored, and "type" is just overwritten.
1500 * Note that on error the file may have been
1504 * 1 : file = Actions file to be parsed in-place.
1506 * Returns : JB_ERR_OK on success
1507 * JB_ERR_MEMORY on out-of-memory
1508 * JB_ERR_PARSE on error
1510 *********************************************************************/
1511 jb_err edit_parse_actions_file(struct editable_file * file)
1513 struct file_line * cur_line;
1515 const char * text; /* Text from a line */
1516 char * name; /* For lines of the form name=value */
1517 char * value; /* For lines of the form name=value */
1518 struct action_alias * alias_list = NULL;
1519 jb_err err = JB_ERR_OK;
1521 /* alias_list contains the aliases defined in this file.
1522 * It might be better to use the "file_line.data" fields
1523 * in the relavent places instead.
1526 cur_line = file->lines;
1528 /* A note about blank line support: Blank lines should only
1529 * ever occur as the last line in the file. This function
1530 * is more forgiving than that - FILE_LINE_BLANK can occur
1534 /* Skip leading blanks. Should only happen if file is
1535 * empty (which is valid, but pointless).
1537 while ( (cur_line != NULL)
1538 && (cur_line->unprocessed[0] == '\0') )
1541 cur_line->type = FILE_LINE_BLANK;
1542 cur_line = cur_line->next;
1545 if ( (cur_line != NULL)
1546 && (cur_line->unprocessed[0] != '{') )
1548 /* File doesn't start with a header */
1549 file->parse_error = cur_line;
1550 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1551 return JB_ERR_PARSE;
1554 if ( (cur_line != NULL) && (0 ==
1555 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1557 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1559 cur_line = cur_line->next;
1560 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1562 if (cur_line->unprocessed[0])
1564 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1566 err = split_line_on_equals(cur_line->unprocessed,
1567 &cur_line->data.setting.name,
1568 &cur_line->data.setting.svalue);
1569 if (err == JB_ERR_MEMORY)
1573 else if (err != JB_ERR_OK)
1575 /* Line does not contain a name=value pair */
1576 file->parse_error = cur_line;
1577 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1578 return JB_ERR_PARSE;
1583 cur_line->type = FILE_LINE_BLANK;
1585 cur_line = cur_line->next;
1589 if ( (cur_line != NULL) && (0 ==
1590 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1592 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1594 cur_line = cur_line->next;
1595 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1597 if (cur_line->unprocessed[0])
1599 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1603 cur_line->type = FILE_LINE_BLANK;
1605 cur_line = cur_line->next;
1609 if ( (cur_line != NULL) && (0 ==
1610 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1612 cur_line->type = FILE_LINE_ALIAS_HEADER;
1614 cur_line = cur_line->next;
1615 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1617 if (cur_line->unprocessed[0])
1619 /* define an alias */
1620 struct action_alias * new_alias;
1622 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1624 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1625 if (err == JB_ERR_MEMORY)
1629 else if (err != JB_ERR_OK)
1631 /* Line does not contain a name=value pair */
1632 file->parse_error = cur_line;
1633 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1634 return JB_ERR_PARSE;
1637 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1642 free_alias_list(alias_list);
1643 return JB_ERR_MEMORY;
1646 err = get_actions(value, alias_list, new_alias->action);
1649 /* Invalid action or out of memory */
1653 free_alias_list(alias_list);
1654 if (err == JB_ERR_MEMORY)
1660 /* Line does not contain a name=value pair */
1661 file->parse_error = cur_line;
1662 file->parse_error_text = "This alias does not specify a valid set of actions.";
1663 return JB_ERR_PARSE;
1669 new_alias->name = name;
1672 new_alias->next = alias_list;
1673 alias_list = new_alias;
1677 cur_line->type = FILE_LINE_BLANK;
1679 cur_line = cur_line->next;
1683 /* Header done, process the main part of the file */
1684 while (cur_line != NULL)
1686 /* At this point, (cur_line->unprocessed[0] == '{') */
1687 assert(cur_line->unprocessed[0] == '{');
1688 text = cur_line->unprocessed + 1;
1689 len = strlen(text) - 1;
1690 if (text[len] != '}')
1692 /* No closing } on header */
1693 free_alias_list(alias_list);
1694 file->parse_error = cur_line;
1695 file->parse_error_text = "Headers starting with '{' must have a "
1696 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1697 "must close with two brackets ('}}').";
1698 return JB_ERR_PARSE;
1703 /* An invalid {{ header. */
1704 free_alias_list(alias_list);
1705 file->parse_error = cur_line;
1706 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1707 "Please remember that the system (two-bracket) headers must "
1708 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1709 "and must appear before any actions (one-bracket) headers. "
1710 "Also note that system headers may not be repeated.";
1711 return JB_ERR_PARSE;
1714 while ( (*text == ' ') || (*text == '\t') )
1719 while ( (len > (size_t)0)
1720 && ( (text[len - 1] == ' ')
1721 || (text[len - 1] == '\t') ) )
1726 cur_line->type = FILE_LINE_ACTION;
1728 /* Remove {} and make copy */
1729 if (NULL == (value = (char *) malloc(len + 1)))
1732 free_alias_list(alias_list);
1733 return JB_ERR_MEMORY;
1735 strncpy(value, text, len);
1739 err = get_actions(value, alias_list, cur_line->data.action);
1742 /* Invalid action or out of memory */
1744 free_alias_list(alias_list);
1745 if (err == JB_ERR_MEMORY)
1751 /* Line does not contain a name=value pair */
1752 file->parse_error = cur_line;
1753 file->parse_error_text = "This header does not specify a valid set of actions.";
1754 return JB_ERR_PARSE;
1758 /* Done with string - it was clobbered anyway */
1761 /* Process next line */
1762 cur_line = cur_line->next;
1764 /* Loop processing URL patterns */
1765 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1767 if (cur_line->unprocessed[0])
1769 /* Could parse URL here, but this isn't currently needed */
1771 cur_line->type = FILE_LINE_URL;
1775 cur_line->type = FILE_LINE_BLANK;
1777 cur_line = cur_line->next;
1779 } /* End main while(cur_line != NULL) loop */
1781 free_alias_list(alias_list);
1787 /*********************************************************************
1789 * Function : edit_read_file_lines
1791 * Description : Read all the lines of a file into memory.
1792 * Handles whitespace, comments and line continuation.
1795 * 1 : fp = File to read from. On return, this will be
1796 * at EOF but it will not have been closed.
1797 * 2 : pfile = Destination for a linked list of file_lines.
1798 * Will be set to NULL on error.
1799 * 3 : newline = How to handle newlines.
1801 * Returns : JB_ERR_OK on success
1802 * JB_ERR_MEMORY on out-of-memory
1804 *********************************************************************/
1805 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1807 struct file_line * first_line; /* Keep for return value or to free */
1808 struct file_line * cur_line; /* Current line */
1809 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1817 cur_line = first_line = zalloc(sizeof(struct file_line));
1818 if (cur_line == NULL)
1820 return JB_ERR_MEMORY;
1823 cur_line->type = FILE_LINE_UNPROCESSED;
1825 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1828 /* Out of memory or empty file. */
1829 /* Note that empty file is not an error we propogate up */
1831 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1836 prev_line = cur_line;
1837 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1838 if (cur_line == NULL)
1841 edit_free_file_lines(first_line);
1842 return JB_ERR_MEMORY;
1845 cur_line->type = FILE_LINE_UNPROCESSED;
1847 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1848 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1851 edit_free_file_lines(first_line);
1852 return JB_ERR_MEMORY;
1856 while (rval != JB_ERR_FILE);
1860 /* We allocated one too many - free it */
1861 prev_line->next = NULL;
1864 *pfile = first_line;
1869 /*********************************************************************
1871 * Function : edit_read_file
1873 * Description : Read a complete file into memory.
1874 * Handles CGI parameter parsing. If requested, also
1875 * checks the file's modification timestamp.
1878 * 1 : csp = Current client state (buffers, headers, etc...)
1879 * 2 : parameters = map of cgi parameters.
1880 * 3 : require_version = true to check "ver" parameter.
1881 * 4 : pfile = Destination for the file. Will be set
1885 * f : The action file identifier.
1886 * ver : (Only if require_version is nonzero)
1887 * Timestamp of the actions file. If wrong, this
1888 * function fails with JB_ERR_MODIFIED.
1890 * Returns : JB_ERR_OK on success
1891 * JB_ERR_MEMORY on out-of-memory
1892 * JB_ERR_CGI_PARAMS if "filename" was not specified
1894 * JB_ERR_FILE if the file cannot be opened or
1896 * JB_ERR_MODIFIED if version checking was requested and
1897 * failed - the file was modified outside
1898 * of this CGI editor instance.
1900 *********************************************************************/
1901 jb_err edit_read_file(struct client_state *csp,
1902 const struct map *parameters,
1903 int require_version,
1904 struct editable_file **pfile)
1906 struct file_line * lines;
1909 const char *filename = NULL;
1910 struct editable_file * file;
1911 unsigned version = 0;
1912 struct stat statbuf[1];
1913 char version_buf[22];
1914 int newline = NEWLINE_UNKNOWN;
1923 err = get_number_param(csp, parameters, "f", &i);
1924 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1926 filename = csp->config->actions_file[i];
1928 else if (JB_ERR_CGI_PARAMS == err)
1931 * Probably an old-school URL like
1932 * http://config.privoxy.org/edit-actions-list?f=default
1934 err = get_file_name_param(csp, parameters, "f", &filename);
1937 if (NULL == filename || stat(filename, statbuf) < 0)
1939 /* Error, probably file not found. */
1942 version = (unsigned) statbuf->st_mtime;
1944 if (require_version)
1946 unsigned specified_version;
1947 err = get_number_param(csp, parameters, "v", &specified_version);
1953 if (version != specified_version)
1955 return JB_ERR_MODIFIED;
1959 if (NULL == (fp = fopen(filename,"rb")))
1964 err = edit_read_file_lines(fp, &lines, &newline);
1973 file = (struct editable_file *) zalloc(sizeof(*file));
1976 edit_free_file_lines(lines);
1980 file->lines = lines;
1981 file->newline = newline;
1982 file->filename = filename;
1983 file->version = version;
1984 file->identifier = i;
1986 /* Correct file->version_str */
1987 freez(file->version_str);
1988 snprintf(version_buf, sizeof(version_buf), "%u", file->version);
1989 version_buf[sizeof(version_buf)-1] = '\0';
1990 file->version_str = strdup(version_buf);
1991 if (version_buf == NULL)
1993 edit_free_file(file);
1994 return JB_ERR_MEMORY;
2002 /*********************************************************************
2004 * Function : edit_read_actions_file
2006 * Description : Read a complete actions file into memory.
2007 * Handles CGI parameter parsing. If requested, also
2008 * checks the file's modification timestamp.
2010 * If this function detects an error in the categories
2011 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
2012 * then it handles it by filling in the specified
2013 * response structure and returning JB_ERR_FILE.
2016 * 1 : csp = Current client state (buffers, headers, etc...)
2017 * 2 : rsp = HTTP response. Only filled in on error.
2018 * 2 : parameters = map of cgi parameters.
2019 * 3 : require_version = true to check "ver" parameter.
2020 * 4 : pfile = Destination for the file. Will be set
2024 * f : The actions file identifier.
2025 * ver : (Only if require_version is nonzero)
2026 * Timestamp of the actions file. If wrong, this
2027 * function fails with JB_ERR_MODIFIED.
2029 * Returns : JB_ERR_OK on success
2030 * JB_ERR_MEMORY on out-of-memory
2031 * JB_ERR_CGI_PARAMS if "filename" was not specified
2033 * JB_ERR_FILE if the file does not contain valid data,
2034 * or if file cannot be opened or
2035 * contains no data, or if version
2036 * checking was requested and failed.
2038 *********************************************************************/
2039 jb_err edit_read_actions_file(struct client_state *csp,
2040 struct http_response *rsp,
2041 const struct map *parameters,
2042 int require_version,
2043 struct editable_file **pfile)
2046 struct editable_file *file;
2047 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
2055 err = edit_read_file(csp, parameters, require_version, &file);
2058 /* Try to handle if possible */
2059 if (err == JB_ERR_FILE)
2061 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
2063 else if (err == JB_ERR_MODIFIED)
2065 assert(require_version);
2066 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
2067 log_error(LOG_LEVEL_ERROR,
2068 "Blocking CGI edit request due to modification time mismatch.");
2069 if (acceptable_failures > 0)
2071 log_error(LOG_LEVEL_INFO,
2072 "The CGI editor will be turned off after another %d mismatche(s).",
2073 acceptable_failures);
2074 acceptable_failures--;
2078 log_error(LOG_LEVEL_INFO,
2079 "Timestamp mismatch limit reached, turning CGI editor off. "
2080 "Reload the configuration file to reenable it.");
2081 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
2084 if (err == JB_ERR_OK)
2087 * Signal to higher-level CGI code that there was a problem but we
2088 * handled it, they should just return JB_ERR_OK.
2095 err = edit_parse_actions_file(file);
2098 if (err == JB_ERR_PARSE)
2100 err = cgi_error_parse(csp, rsp, file);
2101 if (err == JB_ERR_OK)
2104 * Signal to higher-level CGI code that there was a problem but we
2105 * handled it, they should just return JB_ERR_OK.
2110 edit_free_file(file);
2119 /*********************************************************************
2121 * Function : get_file_name_param
2123 * Description : Get the name of the file to edit from the parameters
2124 * passed to a CGI function using the old syntax.
2125 * This function handles security checks and only
2126 * accepts files that Privoxy already knows.
2129 * 1 : csp = Current client state (buffers, headers, etc...)
2130 * 2 : parameters = map of cgi parameters
2131 * 3 : param_name = The name of the parameter to read
2132 * 4 : pfilename = pointer to the filename in
2133 * csp->config->actions_file[] if found. Set to NULL on error.
2135 * Returns : JB_ERR_OK on success
2136 * JB_ERR_MEMORY on out-of-memory
2137 * JB_ERR_CGI_PARAMS if "filename" was not specified
2140 *********************************************************************/
2141 static jb_err get_file_name_param(struct client_state *csp,
2142 const struct map *parameters,
2143 const char *param_name,
2144 const char **pfilename)
2147 const char suffix[] = ".action";
2162 param = lookup(parameters, param_name);
2165 return JB_ERR_CGI_PARAMS;
2168 len = strlen(param);
2169 if (len >= FILENAME_MAX)
2172 return JB_ERR_CGI_PARAMS;
2176 * Check every character to see if it's legal.
2177 * Totally unnecessary but we do it anyway.
2180 while ((ch = *s++) != '\0')
2182 if ( ((ch < 'A') || (ch > 'Z'))
2183 && ((ch < 'a') || (ch > 'z'))
2184 && ((ch < '0') || (ch > '9'))
2188 /* Probable hack attempt. */
2189 return JB_ERR_CGI_PARAMS;
2193 /* Append extension */
2194 name_size = len + strlen(suffix) + 1;
2195 name = malloc(name_size);
2198 return JB_ERR_MEMORY;
2200 strlcpy(name, param, name_size);
2201 strlcat(name, suffix, name_size);
2204 fullpath = make_path(csp->config->confdir, name);
2207 if (fullpath == NULL)
2209 return JB_ERR_MEMORY;
2212 /* Check if the file is known */
2213 for (i = 0; i < MAX_AF_FILES; i++)
2215 if (NULL != csp->config->actions_file[i] &&
2216 !strcmp(fullpath, csp->config->actions_file[i]))
2219 *pfilename = csp->config->actions_file[i];
2227 return JB_ERR_CGI_PARAMS;
2231 /*********************************************************************
2233 * Function : get_url_spec_param
2235 * Description : Get a URL pattern from the parameters
2236 * passed to a CGI function. Removes leading/trailing
2237 * spaces and validates it.
2240 * 1 : csp = Current client state (buffers, headers, etc...)
2241 * 2 : parameters = map of cgi parameters
2242 * 3 : name = Name of CGI parameter to read
2243 * 4 : pvalue = destination for value. Will be malloc()'d.
2244 * Set to NULL on error.
2246 * Returns : JB_ERR_OK on success
2247 * JB_ERR_MEMORY on out-of-memory
2248 * JB_ERR_CGI_PARAMS if the parameter was not specified
2251 *********************************************************************/
2252 static jb_err get_url_spec_param(struct client_state *csp,
2253 const struct map *parameters,
2257 const char *orig_param;
2260 struct url_spec compiled[1];
2270 orig_param = lookup(parameters, name);
2273 return JB_ERR_CGI_PARAMS;
2276 /* Copy and trim whitespace */
2277 param = strdup(orig_param);
2280 return JB_ERR_MEMORY;
2284 /* Must be non-empty, and can't allow 1st character to be '{' */
2285 if (param[0] == '\0' || param[0] == '{')
2288 return JB_ERR_CGI_PARAMS;
2291 /* Check for embedded newlines */
2292 for (s = param; *s != '\0'; s++)
2294 if ((*s == '\r') || (*s == '\n'))
2297 return JB_ERR_CGI_PARAMS;
2301 /* Check that regex is valid */
2306 return JB_ERR_MEMORY;
2308 err = create_url_spec(compiled, s);
2313 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2315 free_url_spec(compiled);
2317 if (param[strlen(param) - 1] == '\\')
2320 * Must protect trailing '\\' from becoming line continuation character.
2321 * Two methods: 1) If it's a domain only, add a trailing '/'.
2322 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2324 if (strchr(param, '/') == NULL)
2326 err = string_append(¶m, "/");
2330 err = string_append(¶m, "(?:)");
2337 /* Check that the modified regex is valid */
2342 return JB_ERR_MEMORY;
2344 err = create_url_spec(compiled, s);
2349 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2351 free_url_spec(compiled);
2358 /*********************************************************************
2360 * Function : map_radio
2362 * Description : Map a set of radio button values. E.g. if you have
2363 * 3 radio buttons, declare them as:
2364 * <option type="radio" name="xyz" @xyz-a@>
2365 * <option type="radio" name="xyz" @xyz-b@>
2366 * <option type="radio" name="xyz" @xyz-c@>
2367 * Then map one of the @xyz-?@ variables to "checked"
2368 * and all the others to empty by calling:
2369 * map_radio(exports, "xyz", "abc", sel)
2370 * Where 'sel' is 'a', 'b', or 'c'.
2373 * 1 : exports = Exports map to modify.
2374 * 2 : optionname = name for map
2375 * 3 : values = null-terminated list of values;
2376 * 4 : value = Selected value.
2378 * CGI Parameters : None
2380 * Returns : JB_ERR_OK on success
2381 * JB_ERR_MEMORY on out-of-memory
2383 *********************************************************************/
2384 static jb_err map_radio(struct map * exports,
2385 const char * optionname,
2386 const char * values,
2392 const size_t len = strlen(optionname);
2393 const size_t buf_size = len + 3;
2399 buf = malloc(buf_size);
2402 return JB_ERR_MEMORY;
2405 strlcpy(buf, optionname, buf_size);
2407 /* XXX: this looks ... interesting */
2412 while ((c = *values++) != '\0')
2417 if (map(exports, buf, 1, "", 1))
2419 return JB_ERR_MEMORY;
2425 return map(exports, buf, 0, "checked", 1);
2429 /*********************************************************************
2431 * Function : cgi_error_modified
2433 * Description : CGI function that is called when a file is modified
2434 * outside the CGI editor.
2437 * 1 : csp = Current client state (buffers, headers, etc...)
2438 * 2 : rsp = http_response data structure for output
2439 * 3 : filename = The file that was modified.
2441 * CGI Parameters : none
2443 * Returns : JB_ERR_OK on success
2444 * JB_ERR_MEMORY on out-of-memory error.
2446 *********************************************************************/
2447 jb_err cgi_error_modified(struct client_state *csp,
2448 struct http_response *rsp,
2449 const char *filename)
2451 struct map *exports;
2458 if (NULL == (exports = default_exports(csp, NULL)))
2460 return JB_ERR_MEMORY;
2463 err = map(exports, "f", 1, html_encode(filename), 0);
2470 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2474 /*********************************************************************
2476 * Function : cgi_error_parse
2478 * Description : CGI function that is called when a file cannot
2479 * be parsed by the CGI editor.
2482 * 1 : csp = Current client state (buffers, headers, etc...)
2483 * 2 : rsp = http_response data structure for output
2484 * 3 : file = The file that was modified.
2486 * CGI Parameters : none
2488 * Returns : JB_ERR_OK on success
2489 * JB_ERR_MEMORY on out-of-memory error.
2491 *********************************************************************/
2492 jb_err cgi_error_parse(struct client_state *csp,
2493 struct http_response *rsp,
2494 struct editable_file *file)
2496 struct map *exports;
2498 struct file_line *cur_line;
2504 if (NULL == (exports = default_exports(csp, NULL)))
2506 return JB_ERR_MEMORY;
2509 err = map(exports, "f", 1, stringify(file->identifier), 0);
2510 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2512 cur_line = file->parse_error;
2515 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2516 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2524 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2528 /*********************************************************************
2530 * Function : cgi_error_file
2532 * Description : CGI function that is called when a file cannot be
2533 * opened by the CGI editor.
2536 * 1 : csp = Current client state (buffers, headers, etc...)
2537 * 2 : rsp = http_response data structure for output
2538 * 3 : filename = The file that was modified.
2540 * CGI Parameters : none
2542 * Returns : JB_ERR_OK on success
2543 * JB_ERR_MEMORY on out-of-memory error.
2545 *********************************************************************/
2546 jb_err cgi_error_file(struct client_state *csp,
2547 struct http_response *rsp,
2548 const char *filename)
2550 struct map *exports;
2557 if (NULL == (exports = default_exports(csp, NULL)))
2559 return JB_ERR_MEMORY;
2562 err = map(exports, "f", 1, html_encode(filename), 0);
2569 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2573 /*********************************************************************
2575 * Function : cgi_error_file_read_only
2577 * Description : CGI function that is called when a file cannot be
2578 * opened for writing by the CGI editor.
2581 * 1 : csp = Current client state (buffers, headers, etc...)
2582 * 2 : rsp = http_response data structure for output
2583 * 3 : filename = The file that we can't write to
2585 * CGI Parameters : none
2587 * Returns : JB_ERR_OK on success
2588 * JB_ERR_MEMORY on out-of-memory error.
2590 *********************************************************************/
2591 jb_err cgi_error_file_read_only(struct client_state *csp,
2592 struct http_response *rsp,
2593 const char *filename)
2595 struct map *exports;
2602 if (NULL == (exports = default_exports(csp, NULL)))
2604 return JB_ERR_MEMORY;
2607 err = map(exports, "f", 1, html_encode(filename), 0);
2614 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2618 /*********************************************************************
2620 * Function : cgi_edit_actions
2622 * Description : CGI function that allows the user to choose which
2623 * actions file to edit.
2626 * 1 : csp = Current client state (buffers, headers, etc...)
2627 * 2 : rsp = http_response data structure for output
2628 * 3 : parameters = map of cgi parameters
2630 * CGI Parameters : None
2632 * Returns : JB_ERR_OK on success
2633 * JB_ERR_MEMORY on out-of-memory error
2635 *********************************************************************/
2636 jb_err cgi_edit_actions(struct client_state *csp,
2637 struct http_response *rsp,
2638 const struct map *parameters)
2641 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2643 return cgi_error_disabled(csp, rsp);
2646 /* FIXME: Incomplete */
2648 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2653 /*********************************************************************
2655 * Function : cgi_edit_actions_list
2657 * Description : CGI function that edits the actions list.
2658 * FIXME: This function shouldn't FATAL ever.
2659 * FIXME: This function doesn't check the retval of map()
2661 * 1 : csp = Current client state (buffers, headers, etc...)
2662 * 2 : rsp = http_response data structure for output
2663 * 3 : parameters = map of cgi parameters
2665 * CGI Parameters : filename
2667 * Returns : JB_ERR_OK on success
2668 * JB_ERR_MEMORY on out-of-memory
2669 * JB_ERR_FILE if the file cannot be opened or
2671 * JB_ERR_CGI_PARAMS if "filename" was not specified
2674 *********************************************************************/
2675 jb_err cgi_edit_actions_list(struct client_state *csp,
2676 struct http_response *rsp,
2677 const struct map *parameters)
2679 char * section_template;
2680 char * url_template;
2685 struct map * exports;
2686 struct map * section_exports;
2687 struct map * url_exports;
2688 struct editable_file * file;
2689 struct file_line * cur_line;
2690 unsigned line_number = 0;
2691 unsigned prev_section_line_number = ((unsigned) (-1));
2693 struct file_list * fl;
2694 struct url_actions * b;
2695 char * buttons = NULL;
2698 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2700 return cgi_error_disabled(csp, rsp);
2703 if (NULL == (exports = default_exports(csp, NULL)))
2705 return JB_ERR_MEMORY;
2708 /* Load actions file */
2709 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2712 /* No filename specified, can't read file, or out of memory. */
2714 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2717 /* Find start of actions in file */
2718 cur_line = file->lines;
2720 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2722 cur_line = cur_line->next;
2727 * Conventional actions files should have a match all block
2729 * cur_line = {...global actions...}
2730 * cur_line->next = /
2731 * cur_line->next->next = {...actions...} or EOF
2733 if ( (cur_line != NULL)
2734 && (cur_line->type == FILE_LINE_ACTION)
2735 && (cur_line->next != NULL)
2736 && (cur_line->next->type == FILE_LINE_URL)
2737 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2738 && ( (cur_line->next->next == NULL)
2739 || (cur_line->next->next->type != FILE_LINE_URL)
2743 * Generate string with buttons to set actions for "/" to
2744 * any predefined set of actions (named standard.*, probably
2745 * residing in standard.action).
2748 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2751 edit_free_file(file);
2753 if (err == JB_ERR_FILE)
2755 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2760 err = template_fill(§ion_template, exports);
2763 edit_free_file(file);
2768 buttons = strdup("");
2769 for (i = 0; i < MAX_AF_FILES; i++)
2771 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2773 for (b = b->next; NULL != b; b = b->next)
2775 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2777 if (err || (NULL == (section_exports = new_map())))
2780 free(section_template);
2781 edit_free_file(file);
2783 return JB_ERR_MEMORY;
2786 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2788 if (err || (NULL == (s = strdup(section_template))))
2790 free_map(section_exports);
2792 free(section_template);
2793 edit_free_file(file);
2795 return JB_ERR_MEMORY;
2798 if (!err) err = template_fill(&s, section_exports);
2799 free_map(section_exports);
2800 if (!err) err = string_join(&buttons, s);
2805 freez(section_template);
2806 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2809 * Conventional actions file, supply extra editing help.
2810 * (e.g. don't allow them to make it an unconventional one).
2812 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2814 snprintf(buf, sizeof(buf), "%d", line_number);
2815 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2816 snprintf(buf, sizeof(buf), "%d", line_number + 2);
2817 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2818 if (!err) err = map(exports, "all-urls-actions", 1,
2819 actions_to_html(csp, cur_line->data.action), 0);
2821 /* Skip the 2 lines */
2822 cur_line = cur_line->next->next;
2826 * Note that prev_section_line_number is NOT set here.
2827 * This is deliberate and not a bug. It stops a "Move up"
2828 * option appearing on the next section. Clicking "Move
2829 * up" would make the actions file unconventional, which
2830 * we don't want, so we hide this option.
2836 * Non-standard actions file - does not begin with
2837 * the "All URLs" section.
2839 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2842 /* Set up global exports */
2844 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2845 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2846 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2848 /* Discourage private additions to default.action */
2850 if (!err) err = map_conditional(exports, "default-action",
2851 (strstr("default.action", file->filename) != NULL));
2854 edit_free_file(file);
2859 /* Should do all global exports above this point */
2861 /* Load templates */
2863 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2866 edit_free_file(file);
2868 if (err == JB_ERR_FILE)
2870 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2875 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2878 free(section_template);
2879 edit_free_file(file);
2881 if (err == JB_ERR_FILE)
2883 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2888 err = template_fill(§ion_template, exports);
2892 edit_free_file(file);
2897 err = template_fill(&url_template, exports);
2900 free(section_template);
2901 edit_free_file(file);
2906 if (NULL == (sections = strdup("")))
2908 free(section_template);
2910 edit_free_file(file);
2912 return JB_ERR_MEMORY;
2915 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2917 if (NULL == (section_exports = new_map()))
2920 free(section_template);
2922 edit_free_file(file);
2924 return JB_ERR_MEMORY;
2927 snprintf(buf, sizeof(buf), "%d", line_number);
2928 err = map(section_exports, "s", 1, buf, 1);
2929 if (!err) err = map(section_exports, "actions", 1,
2930 actions_to_html(csp, cur_line->data.action), 0);
2933 && (cur_line->next != NULL)
2934 && (cur_line->next->type == FILE_LINE_URL))
2936 /* This section contains at least one URL, don't allow delete */
2937 err = map_block_killer(section_exports, "empty-section");
2941 if (!err) err = map_block_keep(section_exports, "empty-section");
2944 if (prev_section_line_number != ((unsigned)(-1)))
2946 /* Not last section */
2947 snprintf(buf, sizeof(buf), "%d", prev_section_line_number);
2948 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2949 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2954 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2956 prev_section_line_number = line_number;
2961 free(section_template);
2963 edit_free_file(file);
2965 free_map(section_exports);
2969 /* Should do all section-specific exports above this point */
2971 if (NULL == (urls = strdup("")))
2974 free(section_template);
2976 edit_free_file(file);
2978 free_map(section_exports);
2979 return JB_ERR_MEMORY;
2984 cur_line = cur_line->next;
2987 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2989 if (NULL == (url_exports = new_map()))
2993 free(section_template);
2995 edit_free_file(file);
2997 free_map(section_exports);
2998 return JB_ERR_MEMORY;
3001 snprintf(buf, sizeof(buf), "%d", line_number);
3002 err = map(url_exports, "p", 1, buf, 1);
3004 snprintf(buf, sizeof(buf), "%d", url_1_2);
3005 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
3007 if (!err) err = map(url_exports, "url-html", 1,
3008 html_encode(cur_line->unprocessed), 0);
3009 if (!err) err = map(url_exports, "url", 1,
3010 url_encode(cur_line->unprocessed), 0);
3016 free(section_template);
3018 edit_free_file(file);
3020 free_map(section_exports);
3021 free_map(url_exports);
3025 if (NULL == (s = strdup(url_template)))
3029 free(section_template);
3031 edit_free_file(file);
3033 free_map(section_exports);
3034 free_map(url_exports);
3035 return JB_ERR_MEMORY;
3038 err = template_fill(&s, section_exports);
3039 if (!err) err = template_fill(&s, url_exports);
3040 if (!err) err = string_append(&urls, s);
3042 free_map(url_exports);
3049 free(section_template);
3051 edit_free_file(file);
3053 free_map(section_exports);
3057 url_1_2 = 3 - url_1_2;
3059 cur_line = cur_line->next;
3063 err = map(section_exports, "urls", 1, urls, 0);
3065 /* Could also do section-specific exports here, but it wouldn't be as fast */
3067 snprintf(buf, sizeof(buf), "%d", line_number);
3068 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
3070 if ( (cur_line != NULL)
3071 && (cur_line->type == FILE_LINE_ACTION))
3073 /* Not last section */
3074 if (!err) err = map_block_keep(section_exports, "s-next-exists");
3079 if (!err) err = map_block_killer(section_exports, "s-next-exists");
3085 free(section_template);
3087 edit_free_file(file);
3089 free_map(section_exports);
3093 if (NULL == (s = strdup(section_template)))
3096 free(section_template);
3098 edit_free_file(file);
3100 free_map(section_exports);
3101 return JB_ERR_MEMORY;
3104 err = template_fill(&s, section_exports);
3105 if (!err) err = string_append(§ions, s);
3108 free_map(section_exports);
3113 free(section_template);
3115 edit_free_file(file);
3121 edit_free_file(file);
3122 free(section_template);
3125 err = map(exports, "sections", 1, sections, 0);
3132 /* Could also do global exports here, but it wouldn't be as fast */
3134 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
3138 /*********************************************************************
3140 * Function : cgi_edit_actions_for_url
3142 * Description : CGI function that edits the Actions list.
3145 * 1 : csp = Current client state (buffers, headers, etc...)
3146 * 2 : rsp = http_response data structure for output
3147 * 3 : parameters = map of cgi parameters
3149 * CGI Parameters : None
3151 * Returns : JB_ERR_OK on success
3152 * JB_ERR_MEMORY on out-of-memory
3153 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3154 * specified or not valid.
3156 *********************************************************************/
3157 jb_err cgi_edit_actions_for_url(struct client_state *csp,
3158 struct http_response *rsp,
3159 const struct map *parameters)
3161 struct map * exports;
3163 struct editable_file * file;
3164 struct file_line * cur_line;
3165 unsigned line_number;
3167 struct re_filterfile_spec *filter_group;
3168 int i, have_filters = 0;
3170 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3172 return cgi_error_disabled(csp, rsp);
3175 err = get_number_param(csp, parameters, "s", §ionid);
3181 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3184 /* No filename specified, can't read file, modified, or out of memory. */
3185 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3188 cur_line = file->lines;
3190 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3192 cur_line = cur_line->next;
3195 if ( (cur_line == NULL)
3196 || (line_number != sectionid)
3198 || (cur_line->type != FILE_LINE_ACTION))
3200 /* Invalid "sectionid" parameter */
3201 edit_free_file(file);
3202 return JB_ERR_CGI_PARAMS;
3205 if (NULL == (exports = default_exports(csp, NULL)))
3207 edit_free_file(file);
3208 return JB_ERR_MEMORY;
3211 err = map(exports, "f", 1, stringify(file->identifier), 0);
3212 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3213 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3215 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3218 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3219 * length limitation and ignore clicks on the Submit buttons if
3220 * the resulting GET URL would be longer than their limit.
3222 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3223 * reached this limit and action editing stopped working in these
3224 * browsers (BR #1570678).
3226 * The config option split-large-forms works around this browser
3227 * bug (HTTP has no URL lenght limitation) by deviding the action
3228 * list form into multiple smaller ones. It means the URLs are shorter
3229 * and work in broken browsers as well, but the user can no longer change
3230 * all actions with one submit.
3232 * A better solution would be to switch to POST requests,
3233 * but this will do for now.
3235 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3237 /* Generate multiple smaller form by killing the big one. */
3238 err = map_block_killer(exports, "one-form-only");
3242 /* Default: Generate one large form by killing the smaller ones. */
3243 err = map_block_killer(exports, "multiple-forms");
3246 for (i = 0; i < MAX_AF_FILES; i++)
3248 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3250 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3258 edit_free_file(file);
3263 if (0 == have_filters)
3265 err = map(exports, "filter-params", 1, "", 1);
3270 * List available filters and their settings.
3272 char *filter_template;
3273 int filter_identifier = 0;
3274 char *prepared_templates[MAX_FILTER_TYPES];
3276 for (i = 0; i < MAX_FILTER_TYPES; i++)
3278 prepared_templates[i] = strdup("");
3281 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3284 edit_free_file(file);
3286 if (err == JB_ERR_FILE)
3288 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3293 err = template_fill(&filter_template, exports);
3295 for (i = 0; i < MAX_AF_FILES; i++)
3297 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3299 filter_group = csp->rlist[i]->f;
3300 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3302 char current_mode = 'x';
3304 struct list_entry *filter_name;
3305 struct map *line_exports;
3306 const int type = filter_group->type;
3307 const int multi_action_index = filter_type_info[type].multi_action_index;
3309 assert(type < MAX_FILTER_TYPES);
3311 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
3312 while ((filter_name != NULL)
3313 && (0 != strcmp(filter_group->name, filter_name->str)))
3315 filter_name = filter_name->next;
3318 if (filter_name != NULL)
3324 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
3325 while ((filter_name != NULL)
3326 && (0 != strcmp(filter_group->name, filter_name->str)))
3328 filter_name = filter_name->next;
3330 if (filter_name != NULL)
3336 /* Generate a unique serial number */
3337 snprintf(number, sizeof(number), "%x", filter_identifier++);
3338 number[sizeof(number) - 1] = '\0';
3340 line_exports = new_map();
3341 if (line_exports == NULL)
3343 err = JB_ERR_MEMORY;
3349 if (!err) err = map(line_exports, "index", 1, number, 1);
3350 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3351 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3352 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3353 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
3354 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
3355 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
3359 filter_line = strdup(filter_template);
3360 if (filter_line == NULL) err = JB_ERR_MEMORY;
3362 if (!err) err = template_fill(&filter_line, line_exports);
3363 string_join(&prepared_templates[type], filter_line);
3365 free_map(line_exports);
3370 freez(filter_template);
3372 /* Replace all filter macros with the aggregated templates */
3373 for (i = 0; i < MAX_FILTER_TYPES; i++)
3376 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
3381 /* Free aggregated templates */
3382 for (i = 0; i < MAX_FILTER_TYPES; i++)
3384 freez(prepared_templates[i]);
3389 /* Check or uncheck the "disable all of this type" radio buttons. */
3390 for (i = 0; i < MAX_FILTER_TYPES; i++)
3392 const int a = filter_type_info[i].multi_action_index;
3393 const int disable_all = cur_line->data.action->multi_remove_all[a];
3395 err = map_radio(exports, filter_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
3398 edit_free_file(file);
3406 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3410 /*********************************************************************
3412 * Function : cgi_edit_actions_submit
3414 * Description : CGI function that actually edits the Actions list.
3417 * 1 : csp = Current client state (buffers, headers, etc...)
3418 * 2 : rsp = http_response data structure for output
3419 * 3 : parameters = map of cgi parameters
3421 * CGI Parameters : None
3423 * Returns : JB_ERR_OK on success
3424 * JB_ERR_MEMORY on out-of-memory
3425 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3426 * specified or not valid.
3428 *********************************************************************/
3429 jb_err cgi_edit_actions_submit(struct client_state *csp,
3430 struct http_response *rsp,
3431 const struct map *parameters)
3436 size_t newtext_size;
3438 struct editable_file * file;
3439 struct file_line * cur_line;
3440 unsigned line_number;
3443 int filter_identifier;
3445 const char * action_set_name;
3446 struct file_list * fl;
3447 struct url_actions * b;
3449 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3451 return cgi_error_disabled(csp, rsp);
3454 err = get_number_param(csp, parameters, "s", §ionid);
3460 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3463 /* No filename specified, can't read file, modified, or out of memory. */
3464 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3467 cur_line = file->lines;
3469 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3471 cur_line = cur_line->next;
3474 if ( (cur_line == NULL)
3475 || (line_number != sectionid)
3477 || (cur_line->type != FILE_LINE_ACTION))
3479 /* Invalid "sectionid" parameter */
3480 edit_free_file(file);
3481 return JB_ERR_CGI_PARAMS;
3484 get_string_param(parameters, "p", &action_set_name);
3485 if (action_set_name != NULL)
3487 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3489 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3491 for (b = b->next; NULL != b; b = b->next)
3493 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3495 copy_action(cur_line->data.action, b->action);
3501 edit_free_file(file);
3502 return JB_ERR_CGI_PARAMS;
3508 err = actions_from_radio(parameters, cur_line->data.action);
3514 edit_free_file(file);
3518 /* Check the "disable all of this type" parameters. */
3519 for (i = 0; i < MAX_FILTER_TYPES; i++)
3521 const int multi_action_index = filter_type_info[i].multi_action_index;
3522 const char ch = get_char_param(parameters, filter_type_info[i].disable_all_param);
3526 list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
3527 list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
3528 cur_line->data.action->multi_remove_all[multi_action_index] = 1;
3532 cur_line->data.action->multi_remove_all[multi_action_index] = 0;
3536 for (filter_identifier = 0; !err; filter_identifier++)
3543 * Filter state. Valid states are: 'Y' (active),
3544 * 'N' (inactive) and 'X' (no change).
3548 * Abbreviated filter type. Valid types are: 'F' (content filter),
3549 * 'S' (server-header filter) and 'C' (client-header filter).
3551 int multi_action_index = 0;
3553 /* Generate the keys */
3554 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3555 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3556 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3557 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3558 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3560 err = get_string_param(parameters, key_name, &name);
3569 type = get_char_param(parameters, key_type);
3573 multi_action_index = ACTION_MULTI_FILTER;
3576 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3579 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3582 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3585 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3588 log_error(LOG_LEVEL_ERROR,
3589 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3592 assert(multi_action_index);
3594 value = get_char_param(parameters, key_value);
3597 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3598 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3599 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3601 else if (value == 'N')
3603 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3604 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3606 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3607 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3610 else if (value == 'X')
3612 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3613 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3620 edit_free_file(file);
3624 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3627 edit_free_file(file);
3628 return JB_ERR_MEMORY;
3631 len = strlen(actiontext);
3635 * Empty action - must special-case this.
3636 * Simply setting len to 1 is sufficient...
3641 newtext_size = len + 2;
3642 if (NULL == (newtext = malloc(newtext_size)))
3646 edit_free_file(file);
3647 return JB_ERR_MEMORY;
3649 strlcpy(newtext, actiontext, newtext_size);
3653 newtext[len + 1] = '\0';
3655 freez(cur_line->raw);
3656 freez(cur_line->unprocessed);
3657 cur_line->unprocessed = newtext;
3659 err = edit_write_file(file);
3662 /* Error writing file */
3663 if (err == JB_ERR_FILE)
3665 /* Read-only file. */
3666 err = cgi_error_file_read_only(csp, rsp, file->filename);
3668 edit_free_file(file);
3672 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3673 (long) time(NULL), file->identifier, sectionid);
3675 edit_free_file(file);
3677 return cgi_redirect(rsp, target);
3681 /*********************************************************************
3683 * Function : cgi_edit_actions_url
3685 * Description : CGI function that actually edits a URL pattern in
3689 * 1 : csp = Current client state (buffers, headers, etc...)
3690 * 2 : rsp = http_response data structure for output
3691 * 3 : parameters = map of cgi parameters
3694 * filename : Identifies the file to edit
3695 * ver : File's last-modified time
3696 * section : Line number of section to edit
3697 * pattern : Line number of pattern to edit
3698 * newval : New value for pattern
3700 * Returns : JB_ERR_OK on success
3701 * JB_ERR_MEMORY on out-of-memory
3702 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3703 * specified or not valid.
3705 *********************************************************************/
3706 jb_err cgi_edit_actions_url(struct client_state *csp,
3707 struct http_response *rsp,
3708 const struct map *parameters)
3712 struct editable_file * file;
3713 struct file_line * cur_line;
3714 unsigned line_number;
3715 unsigned section_start_line_number = 0;
3723 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3725 return cgi_error_disabled(csp, rsp);
3728 err = get_number_param(csp, parameters, "p", &patternid);
3735 return JB_ERR_CGI_PARAMS;
3738 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3744 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3747 /* No filename specified, can't read file, modified, or out of memory. */
3749 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3753 cur_line = file->lines;
3755 while ((cur_line != NULL) && (line_number < patternid))
3757 if (cur_line->type == FILE_LINE_ACTION)
3759 section_start_line_number = line_number;
3761 cur_line = cur_line->next;
3765 if ( (cur_line == NULL)
3766 || (cur_line->type != FILE_LINE_URL))
3768 /* Invalid "patternid" parameter */
3770 edit_free_file(file);
3771 return JB_ERR_CGI_PARAMS;
3774 /* At this point, the line to edit is in cur_line */
3776 freez(cur_line->raw);
3777 freez(cur_line->unprocessed);
3778 cur_line->unprocessed = new_pattern;
3780 err = edit_write_file(file);
3783 /* Error writing file */
3784 if (err == JB_ERR_FILE)
3786 /* Read-only file. */
3787 err = cgi_error_file_read_only(csp, rsp, file->filename);
3789 edit_free_file(file);
3793 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3794 (long) time(NULL), file->identifier, section_start_line_number);
3796 edit_free_file(file);
3798 return cgi_redirect(rsp, target);
3802 /*********************************************************************
3804 * Function : cgi_edit_actions_add_url
3806 * Description : CGI function that actually adds a URL pattern to
3810 * 1 : csp = Current client state (buffers, headers, etc...)
3811 * 2 : rsp = http_response data structure for output
3812 * 3 : parameters = map of cgi parameters
3815 * filename : Identifies the file to edit
3816 * ver : File's last-modified time
3817 * section : Line number of section to edit
3818 * newval : New pattern
3820 * Returns : JB_ERR_OK on success
3821 * JB_ERR_MEMORY on out-of-memory
3822 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3823 * specified or not valid.
3825 *********************************************************************/
3826 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3827 struct http_response *rsp,
3828 const struct map *parameters)
3832 struct file_line * new_line;
3833 struct editable_file * file;
3834 struct file_line * cur_line;
3835 unsigned line_number;
3839 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3841 return cgi_error_disabled(csp, rsp);
3844 err = get_number_param(csp, parameters, "s", §ionid);
3851 return JB_ERR_CGI_PARAMS;
3854 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3860 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3863 /* No filename specified, can't read file, modified, or out of memory. */
3865 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3869 cur_line = file->lines;
3871 while ((cur_line != NULL) && (line_number < sectionid))
3873 cur_line = cur_line->next;
3877 if ( (cur_line == NULL)
3878 || (cur_line->type != FILE_LINE_ACTION))
3880 /* Invalid "sectionid" parameter */
3882 edit_free_file(file);
3883 return JB_ERR_CGI_PARAMS;
3886 /* At this point, the section header is in cur_line - add after this. */
3888 /* Allocate the new line */
3889 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3890 if (new_line == NULL)
3893 edit_free_file(file);
3894 return JB_ERR_MEMORY;
3897 /* Fill in the data members of the new line */
3898 new_line->raw = NULL;
3899 new_line->prefix = NULL;
3900 new_line->unprocessed = new_pattern;
3901 new_line->type = FILE_LINE_URL;
3903 /* Link new_line into the list, after cur_line */
3904 new_line->next = cur_line->next;
3905 cur_line->next = new_line;
3907 /* Done making changes, now commit */
3909 err = edit_write_file(file);
3912 /* Error writing file */
3913 if (err == JB_ERR_FILE)
3915 /* Read-only file. */
3916 err = cgi_error_file_read_only(csp, rsp, file->filename);
3918 edit_free_file(file);
3922 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3923 (long) time(NULL), file->identifier, sectionid);
3925 edit_free_file(file);
3927 return cgi_redirect(rsp, target);
3931 /*********************************************************************
3933 * Function : cgi_edit_actions_remove_url
3935 * Description : CGI function that actually removes a URL pattern from
3939 * 1 : csp = Current client state (buffers, headers, etc...)
3940 * 2 : rsp = http_response data structure for output
3941 * 3 : parameters = map of cgi parameters
3944 * f : (filename) Identifies the file to edit
3945 * v : (version) File's last-modified time
3946 * p : (pattern) Line number of pattern to remove
3948 * Returns : JB_ERR_OK on success
3949 * JB_ERR_MEMORY on out-of-memory
3950 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3951 * specified or not valid.
3953 *********************************************************************/
3954 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3955 struct http_response *rsp,
3956 const struct map *parameters)
3959 struct editable_file * file;
3960 struct file_line * cur_line;
3961 struct file_line * prev_line;
3962 unsigned line_number;
3963 unsigned section_start_line_number = 0;
3967 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3969 return cgi_error_disabled(csp, rsp);
3972 err = get_number_param(csp, parameters, "p", &patternid);
3978 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3981 /* No filename specified, can't read file, modified, or out of memory. */
3982 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3987 cur_line = file->lines;
3989 while ((cur_line != NULL) && (line_number < patternid))
3991 if (cur_line->type == FILE_LINE_ACTION)
3993 section_start_line_number = line_number;
3995 prev_line = cur_line;
3996 cur_line = cur_line->next;
4000 if ( (cur_line == NULL)
4001 || (prev_line == NULL)
4002 || (cur_line->type != FILE_LINE_URL))
4004 /* Invalid "patternid" parameter */
4005 edit_free_file(file);
4006 return JB_ERR_CGI_PARAMS;
4009 /* At this point, the line to remove is in cur_line, and the previous
4010 * one is in prev_line
4013 /* Unlink cur_line */
4014 prev_line->next = cur_line->next;
4015 cur_line->next = NULL;
4018 edit_free_file_lines(cur_line);
4020 err = edit_write_file(file);
4023 /* Error writing file */
4024 if (err == JB_ERR_FILE)
4026 /* Read-only file. */
4027 err = cgi_error_file_read_only(csp, rsp, file->filename);
4029 edit_free_file(file);
4033 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
4034 (long) time(NULL), file->identifier, section_start_line_number);
4036 edit_free_file(file);
4038 return cgi_redirect(rsp, target);
4042 /*********************************************************************
4044 * Function : cgi_edit_actions_section_remove
4046 * Description : CGI function that actually removes a whole section from
4047 * the actions file. The section must be empty first
4048 * (else JB_ERR_CGI_PARAMS).
4051 * 1 : csp = Current client state (buffers, headers, etc...)
4052 * 2 : rsp = http_response data structure for output
4053 * 3 : parameters = map of cgi parameters
4056 * f : (filename) Identifies the file to edit
4057 * v : (version) File's last-modified time
4058 * s : (section) Line number of section to edit
4060 * Returns : JB_ERR_OK on success
4061 * JB_ERR_MEMORY on out-of-memory
4062 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4063 * specified or not valid.
4065 *********************************************************************/
4066 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
4067 struct http_response *rsp,
4068 const struct map *parameters)
4071 struct editable_file * file;
4072 struct file_line * cur_line;
4073 struct file_line * prev_line;
4074 unsigned line_number;
4078 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4080 return cgi_error_disabled(csp, rsp);
4083 err = get_number_param(csp, parameters, "s", §ionid);
4089 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4092 /* No filename specified, can't read file, modified, or out of memory. */
4093 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4097 cur_line = file->lines;
4100 while ((cur_line != NULL) && (line_number < sectionid))
4102 prev_line = cur_line;
4103 cur_line = cur_line->next;
4107 if ( (cur_line == NULL)
4108 || (cur_line->type != FILE_LINE_ACTION) )
4110 /* Invalid "sectionid" parameter */
4111 edit_free_file(file);
4112 return JB_ERR_CGI_PARAMS;
4115 if ( (cur_line->next != NULL)
4116 && (cur_line->next->type == FILE_LINE_URL) )
4118 /* Section not empty. */
4119 edit_free_file(file);
4120 return JB_ERR_CGI_PARAMS;
4123 /* At this point, the line to remove is in cur_line, and the previous
4124 * one is in prev_line
4127 /* Unlink cur_line */
4128 if (prev_line == NULL)
4130 /* Removing the first line from the file */
4131 file->lines = cur_line->next;
4135 prev_line->next = cur_line->next;
4137 cur_line->next = NULL;
4140 edit_free_file_lines(cur_line);
4142 err = edit_write_file(file);
4145 /* Error writing file */
4146 if (err == JB_ERR_FILE)
4148 /* Read-only file. */
4149 err = cgi_error_file_read_only(csp, rsp, file->filename);
4151 edit_free_file(file);
4155 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4156 (long) time(NULL), file->identifier);
4158 edit_free_file(file);
4160 return cgi_redirect(rsp, target);
4164 /*********************************************************************
4166 * Function : cgi_edit_actions_section_add
4168 * Description : CGI function that adds a new empty section to
4172 * 1 : csp = Current client state (buffers, headers, etc...)
4173 * 2 : rsp = http_response data structure for output
4174 * 3 : parameters = map of cgi parameters
4177 * f : (filename) Identifies the file to edit
4178 * v : (version) File's last-modified time
4179 * s : (section) Line number of section to add after, 0 for
4182 * Returns : JB_ERR_OK on success
4183 * JB_ERR_MEMORY on out-of-memory
4184 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4185 * specified or not valid.
4187 *********************************************************************/
4188 jb_err cgi_edit_actions_section_add(struct client_state *csp,
4189 struct http_response *rsp,
4190 const struct map *parameters)
4193 struct file_line * new_line;
4195 struct editable_file * file;
4196 struct file_line * cur_line;
4197 unsigned line_number;
4201 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4203 return cgi_error_disabled(csp, rsp);
4206 err = get_number_param(csp, parameters, "s", §ionid);
4212 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4215 /* No filename specified, can't read file, modified, or out of memory. */
4216 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4220 cur_line = file->lines;
4222 if (sectionid <= 1U)
4224 /* Add to start of file */
4225 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4227 /* There's something in the file, find the line before the first
4230 while ( (cur_line->next != NULL)
4231 && (cur_line->next->type != FILE_LINE_ACTION) )
4233 cur_line = cur_line->next;
4239 /* File starts with action line, so insert at top */
4245 /* Add after stated section. */
4246 while ((cur_line != NULL) && (line_number < sectionid))
4248 cur_line = cur_line->next;
4252 if ( (cur_line == NULL)
4253 || (cur_line->type != FILE_LINE_ACTION))
4255 /* Invalid "sectionid" parameter */
4256 edit_free_file(file);
4257 return JB_ERR_CGI_PARAMS;
4260 /* Skip through the section to find the last line in it. */
4261 while ( (cur_line->next != NULL)
4262 && (cur_line->next->type != FILE_LINE_ACTION) )
4264 cur_line = cur_line->next;
4269 /* At this point, the last line in the previous section is in cur_line
4270 * - add after this. (Or if we need to add as the first line, cur_line
4274 new_text = strdup("{}");
4275 if (NULL == new_text)
4277 edit_free_file(file);
4278 return JB_ERR_MEMORY;
4281 /* Allocate the new line */
4282 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4283 if (new_line == NULL)
4286 edit_free_file(file);
4287 return JB_ERR_MEMORY;
4290 /* Fill in the data members of the new line */
4291 new_line->raw = NULL;
4292 new_line->prefix = NULL;
4293 new_line->unprocessed = new_text;
4294 new_line->type = FILE_LINE_ACTION;
4296 if (cur_line != NULL)
4298 /* Link new_line into the list, after cur_line */
4299 new_line->next = cur_line->next;
4300 cur_line->next = new_line;
4304 /* Link new_line into the list, as first line */
4305 new_line->next = file->lines;
4306 file->lines = new_line;
4309 /* Done making changes, now commit */
4311 err = edit_write_file(file);
4314 /* Error writing file */
4315 if (err == JB_ERR_FILE)
4317 /* Read-only file. */
4318 err = cgi_error_file_read_only(csp, rsp, file->filename);
4320 edit_free_file(file);
4324 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4325 (long) time(NULL), file->identifier);
4327 edit_free_file(file);
4329 return cgi_redirect(rsp, target);
4333 /*********************************************************************
4335 * Function : cgi_edit_actions_section_swap
4337 * Description : CGI function that swaps the order of two sections
4338 * in the actions file. Note that this CGI can actually
4339 * swap any two arbitrary sections, but the GUI interface
4340 * currently only allows consecutive sections to be
4344 * 1 : csp = Current client state (buffers, headers, etc...)
4345 * 2 : rsp = http_response data structure for output
4346 * 3 : parameters = map of cgi parameters
4349 * f : (filename) Identifies the file to edit
4350 * v : (version) File's last-modified time
4351 * s1 : (section1) Line number of first section to swap
4352 * s2 : (section2) Line number of second section to swap
4354 * Returns : JB_ERR_OK on success
4355 * JB_ERR_MEMORY on out-of-memory
4356 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4357 * specified or not valid.
4359 *********************************************************************/
4360 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4361 struct http_response *rsp,
4362 const struct map *parameters)
4366 struct editable_file * file;
4367 struct file_line * cur_line;
4368 struct file_line * prev_line;
4369 struct file_line * line_before_section1;
4370 struct file_line * line_start_section1;
4371 struct file_line * line_end_section1;
4372 struct file_line * line_after_section1;
4373 struct file_line * line_before_section2;
4374 struct file_line * line_start_section2;
4375 struct file_line * line_end_section2;
4376 struct file_line * line_after_section2;
4377 unsigned line_number;
4381 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4383 return cgi_error_disabled(csp, rsp);
4386 err = get_number_param(csp, parameters, "s1", §ion1);
4387 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4393 if (section1 > section2)
4395 unsigned temp = section2;
4396 section2 = section1;
4400 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4403 /* No filename specified, can't read file, modified, or out of memory. */
4404 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4407 /* Start at the beginning... */
4409 cur_line = file->lines;
4412 /* ... find section1 ... */
4413 while ((cur_line != NULL) && (line_number < section1))
4415 prev_line = cur_line;
4416 cur_line = cur_line->next;
4420 if ( (cur_line == NULL)
4421 || (cur_line->type != FILE_LINE_ACTION) )
4423 /* Invalid "section1" parameter */
4424 edit_free_file(file);
4425 return JB_ERR_CGI_PARAMS;
4428 /* If no-op, we've validated params and can skip the rest. */
4429 if (section1 != section2)
4431 /* ... find the end of section1 ... */
4432 line_before_section1 = prev_line;
4433 line_start_section1 = cur_line;
4436 prev_line = cur_line;
4437 cur_line = cur_line->next;
4440 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4441 line_end_section1 = prev_line;
4442 line_after_section1 = cur_line;
4444 /* ... find section2 ... */
4445 while ((cur_line != NULL) && (line_number < section2))
4447 prev_line = cur_line;
4448 cur_line = cur_line->next;
4452 if ( (cur_line == NULL)
4453 || (cur_line->type != FILE_LINE_ACTION) )
4455 /* Invalid "section2" parameter */
4456 edit_free_file(file);
4457 return JB_ERR_CGI_PARAMS;
4460 /* ... find the end of section2 ... */
4461 line_before_section2 = prev_line;
4462 line_start_section2 = cur_line;
4465 prev_line = cur_line;
4466 cur_line = cur_line->next;
4469 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4470 line_end_section2 = prev_line;
4471 line_after_section2 = cur_line;
4473 /* Now have all the pointers we need. Do the swap. */
4475 /* Change the pointer to section1 to point to section2 instead */
4476 if (line_before_section1 == NULL)
4478 file->lines = line_start_section2;
4482 line_before_section1->next = line_start_section2;
4485 if (line_before_section2 == line_end_section1)
4487 /* Consecutive sections */
4488 line_end_section2->next = line_start_section1;
4492 line_end_section2->next = line_after_section1;
4493 line_before_section2->next = line_start_section1;
4496 /* Set the pointer from the end of section1 to the rest of the file */
4497 line_end_section1->next = line_after_section2;
4499 err = edit_write_file(file);
4502 /* Error writing file */
4503 if (err == JB_ERR_FILE)
4505 /* Read-only file. */
4506 err = cgi_error_file_read_only(csp, rsp, file->filename);
4508 edit_free_file(file);
4511 } /* END if (section1 != section2) */
4513 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4514 (long) time(NULL), file->identifier);
4516 edit_free_file(file);
4518 return cgi_redirect(rsp, target);
4522 /*********************************************************************
4524 * Function : javascriptify
4526 * Description : Converts a string into a form JavaScript will like.
4528 * Netscape 4's JavaScript sucks - it doesn't use
4529 * "id" parameters, so you have to set the "name"
4530 * used to submit a form element to something JavaScript
4531 * will like. (Or access the elements by index in an
4532 * array. That array contains >60 elements and will
4533 * be changed whenever we add a new action to the
4534 * editor, so I'm NOT going to use indexes that have
4535 * to be figured out by hand.)
4537 * Currently the only thing we have to worry about
4538 * is "-" ==> "_" conversion.
4540 * This is a length-preserving operation so it is
4541 * carried out in-place, no memory is allocated
4545 * 1 : identifier = String to make JavaScript-friendly.
4549 *********************************************************************/
4550 static void javascriptify(char * identifier)
4552 char * p = identifier;
4553 while (NULL != (p = strchr(p, '-')))
4560 /*********************************************************************
4562 * Function : actions_to_radio
4564 * Description : Converts a actionsfile entry into settings for
4565 * radio buttons and edit boxes on a HTML form.
4568 * 1 : exports = List of substitutions to add to.
4569 * 2 : action = Action to read
4571 * Returns : JB_ERR_OK on success
4572 * JB_ERR_MEMORY on out-of-memory
4574 *********************************************************************/
4575 static jb_err actions_to_radio(struct map * exports,
4576 const struct action_spec *action)
4578 unsigned mask = action->mask;
4579 unsigned add = action->add;
4587 mask = action->mask;
4590 /* sanity - prevents "-feature +feature" */
4594 #define DEFINE_ACTION_BOOL(name, bit) \
4595 if (!(mask & bit)) \
4597 current_mode = 'n'; \
4599 else if (add & bit) \
4601 current_mode = 'y'; \
4605 current_mode = 'x'; \
4607 if (map_radio(exports, name, "ynx", current_mode)) \
4609 return JB_ERR_MEMORY; \
4612 #define DEFINE_ACTION_STRING(name, bit, index) \
4613 DEFINE_ACTION_BOOL(name, bit); \
4616 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4619 checked = !strcmp(action->string[index], value); \
4623 checked = is_default; \
4625 mapped_param |= checked; \
4626 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4628 return JB_ERR_MEMORY; \
4631 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4632 if (map(exports, name "-param-custom", 1, \
4633 ((!mapped_param) ? "checked" : ""), 1)) \
4635 return JB_ERR_MEMORY; \
4637 if (map(exports, name "-param", 1, \
4638 (((add & bit) && !mapped_param) ? \
4639 action->string[index] : default_val), 1)) \
4641 return JB_ERR_MEMORY; \
4644 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4645 if (map(exports, name "-param", 1, \
4646 ((add & bit) ? action->string[index] : default_val), 1)) \
4648 return JB_ERR_MEMORY; \
4651 #define DEFINE_ACTION_MULTI(name, index) \
4652 if (action->multi_add[index]->first) \
4654 current_mode = 'y'; \
4656 else if (action->multi_remove_all[index]) \
4658 current_mode = 'n'; \
4660 else if (action->multi_remove[index]->first) \
4662 current_mode = 'y'; \
4666 current_mode = 'x'; \
4668 if (map_radio(exports, name, "ynx", current_mode)) \
4670 return JB_ERR_MEMORY; \
4673 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4675 #include "actionlist.h"
4677 #undef DEFINE_ACTION_MULTI
4678 #undef DEFINE_ACTION_STRING
4679 #undef DEFINE_ACTION_BOOL
4680 #undef DEFINE_ACTION_ALIAS
4681 #undef DEFINE_CGI_PARAM_CUSTOM
4682 #undef DEFINE_CGI_PARAM_RADIO
4683 #undef DEFINE_CGI_PARAM_NO_RADIO
4689 /*********************************************************************
4691 * Function : actions_from_radio
4693 * Description : Converts a map of parameters passed to a CGI function
4694 * into an actionsfile entry.
4697 * 1 : parameters = parameters to the CGI call
4698 * 2 : action = Action to change. Must be valid before
4699 * the call, actions not specified will be
4702 * Returns : JB_ERR_OK on success
4703 * JB_ERR_MEMORY on out-of-memory
4705 *********************************************************************/
4706 static jb_err actions_from_radio(const struct map * parameters,
4707 struct action_spec *action)
4712 const char * js_name;
4713 jb_err err = JB_ERR_OK;
4718 /* Statics are generally a potential race condition,
4719 * but in this case we're safe and don't need semaphores.
4720 * Be careful if you modify this function.
4722 * The js_name_arr's are never free()d, but this is no
4723 * problem, since they will only be created once and
4724 * used by all threads thereafter. -oes
4727 #define JAVASCRIPTIFY(dest_var, string) \
4729 static int first_time = 1; \
4730 static char *js_name_arr; \
4733 js_name_arr = strdup(string); \
4734 javascriptify(js_name_arr); \
4736 dest_var = js_name_arr; \
4740 #define DEFINE_ACTION_BOOL(name, bit) \
4741 JAVASCRIPTIFY(js_name, name); \
4742 ch = get_char_param(parameters, js_name); \
4745 action->add |= bit; \
4746 action->mask |= bit; \
4748 else if (ch == 'N') \
4750 action->add &= ~bit; \
4751 action->mask &= ~bit; \
4753 else if (ch == 'X') \
4755 action->add &= ~bit; \
4756 action->mask |= bit; \
4759 #define DEFINE_ACTION_STRING(name, bit, index) \
4760 JAVASCRIPTIFY(js_name, name); \
4761 ch = get_char_param(parameters, js_name); \
4765 JAVASCRIPTIFY(js_name, name "-mode"); \
4766 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4767 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4769 JAVASCRIPTIFY(js_name, name "-param"); \
4770 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4772 if (param != NULL) \
4774 if (NULL == (param_dup = strdup(param))) \
4776 return JB_ERR_MEMORY; \
4778 freez(action->string[index]); \
4779 action->add |= bit; \
4780 action->mask |= bit; \
4781 action->string[index] = param_dup; \
4784 else if (ch == 'N') \
4786 if (action->add & bit) \
4788 freez(action->string[index]); \
4790 action->add &= ~bit; \
4791 action->mask &= ~bit; \
4793 else if (ch == 'X') \
4795 if (action->add & bit) \
4797 freez(action->string[index]); \
4799 action->add &= ~bit; \
4800 action->mask |= bit; \
4803 #define DEFINE_ACTION_MULTI(name, index) \
4804 JAVASCRIPTIFY(js_name, name); \
4805 ch = get_char_param(parameters, js_name); \
4810 else if (ch == 'N') \
4812 list_remove_all(action->multi_add[index]); \
4813 list_remove_all(action->multi_remove[index]); \
4814 action->multi_remove_all[index] = 1; \
4816 else if (ch == 'X') \
4818 list_remove_all(action->multi_add[index]); \
4819 list_remove_all(action->multi_remove[index]); \
4820 action->multi_remove_all[index] = 0; \
4823 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4825 #include "actionlist.h"
4827 #undef DEFINE_ACTION_MULTI
4828 #undef DEFINE_ACTION_STRING
4829 #undef DEFINE_ACTION_BOOL
4830 #undef DEFINE_ACTION_ALIAS
4831 #undef JAVASCRIPTIFY
4835 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
4838 #ifdef FEATURE_TOGGLE
4839 /*********************************************************************
4841 * Function : cgi_toggle
4843 * Description : CGI function that adds a new empty section to
4847 * 1 : csp = Current client state (buffers, headers, etc...)
4848 * 2 : rsp = http_response data structure for output
4849 * 3 : parameters = map of cgi parameters
4852 * set : If present, how to change toggle setting:
4853 * "enable", "disable", "toggle", or none (default).
4854 * mini : If present, use mini reply template.
4856 * Returns : JB_ERR_OK on success
4857 * JB_ERR_MEMORY on out-of-memory
4859 *********************************************************************/
4860 jb_err cgi_toggle(struct client_state *csp,
4861 struct http_response *rsp,
4862 const struct map *parameters)
4864 struct map *exports;
4866 const char *template_name;
4872 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4874 return cgi_error_disabled(csp, rsp);
4877 mode = get_char_param(parameters, "set");
4882 global_toggle_state = 1;
4884 else if (mode == 'D')
4887 global_toggle_state = 0;
4889 else if (mode == 'T')
4892 global_toggle_state = !global_toggle_state;
4895 if (NULL == (exports = default_exports(csp, "toggle")))
4897 return JB_ERR_MEMORY;
4900 template_name = (get_char_param(parameters, "mini")
4904 return template_fill_for_cgi(csp, template_name, exports, rsp);
4906 #endif /* def FEATURE_TOGGLE */