1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.58 2007/11/28 17:57:01 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-2007 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.58 2007/11/28 17:57:01 fabiankeil
46 * Fix double free in cgi_edit_actions_list().
47 * Reported by adlab in BR#1840145.
49 * Revision 1.57 2007/10/27 13:32:23 fabiankeil
50 * Plug minor 5-year-old memory leak. Spotted by
51 * Valgrind and triggered by Privoxy-Regression-Test.
53 * Revision 1.56 2007/08/05 13:47:03 fabiankeil
54 * #1763173 from Stefan Huehner: s@const static@static const@.
56 * Revision 1.55 2007/05/31 11:50:20 fabiankeil
57 * Re-enable support for old-school URLs like
58 * http://config.privoxy.org/edit-actions-list?f=default
59 * in the action editor.
61 * They are no longer used by the CGI pages, but make it easier
62 * to reach the editor directly, without knowing the requested
63 * file's index in csp->config->actions_file[].
65 * Revision 1.54 2007/05/14 10:33:51 fabiankeil
66 * - Use strlcpy() and strlcat() instead of strcpy() and strcat().
68 * Revision 1.53 2007/04/15 16:39:20 fabiankeil
69 * Introduce tags as alternative way to specify which
70 * actions apply to a request. At the moment tags can be
71 * created based on client and server headers.
73 * Revision 1.52 2007/04/12 10:41:23 fabiankeil
74 * - Don't mistake VC++'s _snprintf() for a snprintf() replacement.
75 * - Move some cgi_edit_actions_for_url() variables into structs.
76 * - Remove bogus comment.
78 * Revision 1.51 2007/04/08 13:21:05 fabiankeil
79 * Reference action files in CGI URLs by id instead
80 * of using the first part of the file name.
81 * Fixes BR 1694250 and BR 1590556.
83 * Revision 1.50 2007/03/29 11:40:34 fabiankeil
84 * Divide @filter-params@ into @client-header-filter-params@
85 * @content-filter-params@ and @server-header-filter-params@.
87 * Revision 1.49 2007/03/20 15:16:34 fabiankeil
88 * Use dedicated header filter actions instead of abusing "filter".
89 * Replace "filter-client-headers" and "filter-client-headers"
90 * with "server-header-filter" and "client-header-filter".
92 * Revision 1.48 2007/02/13 14:35:25 fabiankeil
93 * Replace hash escaping code to prevent
94 * crashes, memory and file corruption.
96 * Revision 1.47 2006/12/28 18:04:25 fabiankeil
97 * Fixed gcc43 conversion warnings.
99 * Revision 1.46 2006/12/27 18:44:52 fabiankeil
100 * Stop shadowing string.h's index().
102 * Revision 1.45 2006/12/21 12:57:48 fabiankeil
103 * Add config option "split-large-forms"
104 * to work around the browser bug reported
107 * Revision 1.44 2006/12/09 13:49:16 fabiankeil
108 * Fix configure option --disable-toggle.
109 * Thanks to Peter Thoenen for reporting this.
111 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
112 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
113 * with what was really the latest development (the v_3_0_branch branch)
115 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
116 * Remove a little residual debugging info
118 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
119 * Multiple filter file support
121 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
122 * Undo some damaging memory leak patches
124 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
125 * Removing a double free
127 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
128 * A few memory leaks plugged (mostly on error paths)
130 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
131 * Moved cgi_error_disabled() from cgiedit.c to
132 * cgi.c to re-enable build with --disable-editor.
133 * Fixes Bug #892744. Thanks to Matthew Fischer
136 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
137 * One line lost in last commit
139 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
140 * - All edit functions that redirect back to the list page
141 * now use cgi_redirect
142 * - All redirects now contain useless parameter "foo", whose
143 * value are raw seconds since epoch, in order to force
144 * Opera and Konqueror to properly reload the list. Closes
147 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
148 * Cosmetic: Renamed cryptic variable
150 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
151 * Fix: Don't free uninitialized struct editable_file
153 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
154 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
156 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
157 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
159 * Revision 1.41 2002/05/21 19:09:45 oes
160 * - Made Add/Edit/Remove URL Submit and Cancel
161 * buttons jump back to relevant section in eal
162 * - Bugfix: remove-url-form needs p export
164 * Revision 1.40 2002/05/19 11:34:35 jongfoster
165 * Handling read-only actions files better - report the actual
166 * error, not "Out of memory"!
169 * http://sourceforge.net/tracker/index.php?func=detail
170 * &aid=557905&group_id=11118&atid=111118
172 * Revision 1.39 2002/05/12 21:39:15 jongfoster
173 * - Adding Doxygen-style comments to structures and #defines.
174 * - Correcting function comments
176 * Revision 1.38 2002/05/03 23:00:38 jongfoster
177 * Support for templates for "standard actions" buttons.
180 * Revision 1.37 2002/04/30 11:14:52 oes
181 * Made csp the first parameter in *action_to_html
183 * Revision 1.36 2002/04/26 21:53:30 jongfoster
184 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
186 * Revision 1.35 2002/04/26 21:50:02 jongfoster
187 * Honouring default exports in edit-actions-for-url-filter template.
189 * Revision 1.34 2002/04/26 12:54:17 oes
190 * Adaptions to changes in actions.c
192 * Revision 1.33 2002/04/24 02:17:47 oes
193 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
195 * - Activated Jon's code for editing multiple AFs
196 * - cgi_edit_list_actions now provides context-sensitive
197 * help, looks up all action sets from standard.action and
198 * makes buttons for them in the catchall section
199 * - cgi_edit_action_submit now honors a p parameter, looks up
200 * the corresponding action set, and sets the catchall pattern's
201 * actions accordingly.
203 * Revision 1.32 2002/04/19 16:55:31 jongfoster
204 * Fixing newline problems. If we do our own text file newline
205 * mangling, we don't want the library to do any, so we need to
206 * open the files in *binary* mode.
208 * Revision 1.31 2002/04/18 19:21:08 jongfoster
209 * Added code to detect "conventional" action files, that start
210 * with a set of actions for all URLs (the pattern "/").
211 * These are special-cased in the "edit-actions-list" CGI, so
212 * that a special UI can be written for them.
214 * Revision 1.30 2002/04/10 13:38:35 oes
215 * load_template signature changed
217 * Revision 1.29 2002/04/08 16:59:08 oes
220 * Revision 1.28 2002/03/27 12:30:29 oes
221 * Deleted unsused variable
223 * Revision 1.27 2002/03/26 23:06:04 jongfoster
224 * Removing duplicate @ifs on the toggle page
226 * Revision 1.26 2002/03/26 22:59:17 jongfoster
227 * Fixing /toggle to display status consistently.
229 * Revision 1.25 2002/03/26 22:29:54 swa
230 * we have a new homepage!
232 * Revision 1.24 2002/03/24 15:23:33 jongfoster
235 * Revision 1.23 2002/03/24 13:32:41 swa
236 * name change related issues
238 * Revision 1.22 2002/03/24 13:25:43 swa
239 * name change related issues
241 * Revision 1.21 2002/03/22 18:02:48 jongfoster
242 * Fixing remote toggle
244 * Revision 1.20 2002/03/16 20:28:34 oes
245 * Added descriptions to the filters so users will know what they select in the cgi editor
247 * Revision 1.19 2002/03/16 18:38:14 jongfoster
248 * Stopping stupid or malicious users from breaking the actions
249 * file using the web-based editor.
251 * Revision 1.18 2002/03/16 14:57:44 jongfoster
252 * Full support for enabling/disabling modular filters.
254 * Revision 1.17 2002/03/16 14:26:42 jongfoster
255 * First version of modular filters support - READ ONLY!
256 * Fixing a double-free bug in the out-of-memory handling in map_radio().
258 * Revision 1.16 2002/03/07 03:46:17 oes
259 * Fixed compiler warnings
261 * Revision 1.15 2002/03/06 22:54:35 jongfoster
262 * Automated function-comment nitpicking.
264 * Revision 1.14 2002/03/05 00:24:51 jongfoster
265 * Patch to always edit the current actions file.
267 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
268 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
270 * Revision 1.12 2002/03/03 09:18:03 joergs
271 * Made jumbjuster work on AmigaOS again.
273 * Revision 1.11 2002/01/23 01:03:31 jongfoster
274 * Fixing gcc [CygWin] compiler warnings
276 * Revision 1.10 2002/01/23 00:22:59 jongfoster
277 * Adding new function cgi_edit_actions_section_swap(), to reorder
280 * Adding get_url_spec_param() to get a validated URL pattern.
282 * Moving edit_read_line() out of this file and into loaders.c.
284 * Adding missing html_encode() to many CGI functions.
286 * Moving the functions that #include actionlist.h to the end of the file,
287 * because the Visual C++ 97 debugger gets extremely confused if you try
288 * to debug any code that comes after them in the file.
290 * Major optimizations in cgi_edit_actions_list() to reduce the size of
291 * the generated HTML (down 40% from 550k to 304k), with major side-effects
292 * throughout the editor and templates. In particular, the length of the
293 * URLs throughout the editor has been drastically reduced, by cutting
294 * paramater names down to 1 character and CGI names down to 3-4
295 * characters, by removing all non-essential CGI paramaters even at the
296 * expense of having to re-read the actions file for the most trivial
297 * page, and by using relative rather than absolute URLs. This means
298 * that this (typical example):
300 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
301 * filename=ijb&ver=1011487572&section=12&pattern=13
302 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
306 * <a href="eau?f=ijb&v=1011487572&p=13">
308 * Revision 1.9 2002/01/17 20:56:22 jongfoster
309 * Replacing hard references to the URL of the config interface
310 * with #defines from project.h
312 * Revision 1.8 2001/11/30 23:35:51 jongfoster
313 * Renaming actionsfile to ijb.action
315 * Revision 1.7 2001/11/13 00:28:24 jongfoster
316 * - Renaming parameters from edit-actions-for-url so that they only
317 * contain legal JavaScript characters. If we wanted to write
318 * JavaScript that worked with Netscape 4, this is nessacery.
319 * (Note that at the moment the JavaScript doesn't actually work
320 * with Netscape 4, but now this is purely a template issue, not
321 * one affecting code).
322 * - Adding new CGIs for use by non-JavaScript browsers:
323 * edit-actions-url-form
324 * edit-actions-add-url-form
325 * edit-actions-remove-url-form
328 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
329 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
330 * by and __OS2__ ifdef.
332 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
333 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
334 * threads to call select() simultaneously. So, it's time to do a real, live,
335 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
336 * (native). Both versions will work, but using __OS2__ offers multi-threading.
338 * Revision 1.4 2001/10/23 21:48:19 jongfoster
339 * Cleaning up error handling in CGI functions - they now send back
340 * a HTML error page and should never cause a FATAL error. (Fixes one
341 * potential source of "denial of service" attacks).
343 * CGI actions file editor that works and is actually useful.
345 * Ability to toggle JunkBuster remotely using a CGI call.
347 * You can turn off both the above features in the main configuration
348 * file, e.g. if you are running a multi-user proxy.
350 * Revision 1.3 2001/10/14 22:12:49 jongfoster
351 * New version of CGI-based actionsfile editor.
352 * Major changes, including:
353 * - Completely new file parser and file output routines
354 * - edit-actions CGI renamed edit-actions-for-url
355 * - All CGIs now need a filename parameter, except for...
356 * - New CGI edit-actions which doesn't need a filename,
357 * to allow you to start the editor up.
358 * - edit-actions-submit now works, and now automatically
359 * redirects you back to the main edit-actions-list handler.
361 * Revision 1.2 2001/09/16 17:05:14 jongfoster
362 * Removing unused #include showarg.h
364 * Revision 1.1 2001/09/16 15:47:37 jongfoster
365 * First version of CGI-based edit interface. This is very much a
366 * work-in-progress, and you can't actually use it to edit anything
367 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
368 * to have any effect.
371 **********************************************************************/
377 * FIXME: Following includes copied from cgi.c - which are actually needed?
382 #include <sys/types.h>
386 #include <sys/stat.h>
391 #include "cgisimple.h"
395 #include "miscutil.h"
398 #ifdef FEATURE_TOGGLE
399 /* loadcfg.h is for global_toggle_state only */
401 #endif /* def FEATURE_TOGGLE */
402 #include "urlmatch.h"
404 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
407 #ifdef FEATURE_CGI_EDIT_ACTIONS
410 * A line in an editable_file.
414 /** Next entry in the linked list */
415 struct file_line * next;
417 /** The raw data, to write out if this line is unmodified. */
420 /** Comments and/or whitespace to put before this line if it's modified
421 and then written out. */
424 /** The actual data, as a string. Line continuation and comment removal
425 are performed on the data read from file before it's stored here, so
426 it will be a single line of data. */
429 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
432 /** The actual data, processed into some sensible data type. */
436 /** An action specification. */
437 struct action_spec action[1];
439 /** A name=value pair. */
443 /** The name in the name=value pair. */
446 /** The value in the name=value pair, as a string. */
449 /** The value in the name=value pair, as an integer. */
454 /* Add more data types here... e.g.
457 struct url_spec url[1];
461 struct action_spec action[1];
471 /** This file_line has not been processed yet. */
472 #define FILE_LINE_UNPROCESSED 1
474 /** This file_line is blank. Can only appear at the end of a file, due to
475 the way the parser works. */
476 #define FILE_LINE_BLANK 2
478 /** This file_line says {{alias}}. */
479 #define FILE_LINE_ALIAS_HEADER 3
481 /** This file_line defines an alias. */
482 #define FILE_LINE_ALIAS_ENTRY 4
484 /** This file_line defines an {action}. */
485 #define FILE_LINE_ACTION 5
487 /** This file_line specifies a URL pattern. */
488 #define FILE_LINE_URL 6
490 /** This file_line says {{settings}}. */
491 #define FILE_LINE_SETTINGS_HEADER 7
493 /** This file_line is in a {{settings}} block. */
494 #define FILE_LINE_SETTINGS_ENTRY 8
496 /** This file_line says {{description}}. */
497 #define FILE_LINE_DESCRIPTION_HEADER 9
499 /** This file_line is in a {{description}} block. */
500 #define FILE_LINE_DESCRIPTION_ENTRY 10
503 * Number of file modification time mismatches
504 * before the CGI editor gets turned off.
506 #define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
509 * A configuration file, in a format that can be edited and written back to
514 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
515 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
516 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
517 const char * version_str; /**< Last modification time, as a string. For CGI param. */
518 /**< Can be used in URL without using url_param(). */
519 unsigned version; /**< Last modification time - prevents chaos with
520 the browser's "back" button. Note that this is a
521 time_t cast to an unsigned. When comparing, always
522 cast the time_t to an unsigned, and *NOT* vice-versa.
523 This may lose the top few bits, but they're not
524 significant anyway. */
525 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
526 Note that changing this after the file has been
527 read in will cause a mess. */
528 struct file_line * parse_error; /**< On parse error, this is the offending line. */
529 const char * parse_error_text; /**< On parse error, this is the problem.
530 (Statically allocated) */
534 * Information about the filter types.
535 * Used for macro replacement in cgi_edit_actions_for_url.
537 struct filter_type_info
539 const int multi_action_index; /**< The multi action index as defined in project.h */
540 const char *macro_name; /**< Name of the macro that has to be replaced
541 with the prepared templates.
542 For example "content-filter-params" */
543 const char *type; /**< Name of the filter type,
544 for example "server-header-filter". */
545 const char *abbr_type; /**< Abbreviation of the filter type, usually the
546 first or second character capitalized */
547 const char *anchor; /**< Anchor for the User Manual link,
548 for example "SERVER-HEADER-FILTER" */
551 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
552 static const struct filter_type_info filter_type_info[] =
556 "content-filter-params", "filter",
560 ACTION_MULTI_CLIENT_HEADER_FILTER,
561 "client-header-filter-params", "client-header-filter",
562 "C", "CLIENT-HEADER-FILTER"
565 ACTION_MULTI_SERVER_HEADER_FILTER,
566 "server-header-filter-params", "server-header-filter",
567 "S", "SERVER-HEADER-FILTER"
570 ACTION_MULTI_CLIENT_HEADER_TAGGER,
571 "client-header-tagger-params", "client-header-tagger",
572 "L", "CLIENT-HEADER-TAGGER"
575 ACTION_MULTI_SERVER_HEADER_TAGGER,
576 "server-header-tagger-params", "server-header-tagger",
577 "E", "SERVER-HEADER-TAGGER"
581 /* FIXME: Following non-static functions should be prototyped in .h or made static */
583 /* Functions to read and write arbitrary config files */
584 jb_err edit_read_file(struct client_state *csp,
585 const struct map *parameters,
587 struct editable_file **pfile);
588 jb_err edit_write_file(struct editable_file * file);
589 void edit_free_file(struct editable_file * file);
591 /* Functions to read and write actions files */
592 jb_err edit_parse_actions_file(struct editable_file * file);
593 jb_err edit_read_actions_file(struct client_state *csp,
594 struct http_response *rsp,
595 const struct map *parameters,
597 struct editable_file **pfile);
600 jb_err cgi_error_modified(struct client_state *csp,
601 struct http_response *rsp,
602 const char *filename);
603 jb_err cgi_error_parse(struct client_state *csp,
604 struct http_response *rsp,
605 struct editable_file *file);
606 jb_err cgi_error_file(struct client_state *csp,
607 struct http_response *rsp,
608 const char *filename);
609 jb_err cgi_error_file_read_only(struct client_state *csp,
610 struct http_response *rsp,
611 const char *filename);
613 /* Internal arbitrary config file support functions */
614 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
615 static void edit_free_file_lines(struct file_line * first_line);
617 /* Internal actions file support functions */
618 static int match_actions_file_header_line(const char * line, const char * name);
619 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
621 /* Internal parameter parsing functions */
622 static jb_err get_url_spec_param(struct client_state *csp,
623 const struct map *parameters,
628 /* Internal actionsfile <==> HTML conversion functions */
629 static jb_err map_radio(struct map * exports,
630 const char * optionname,
633 static jb_err actions_to_radio(struct map * exports,
634 const struct action_spec *action);
635 static jb_err actions_from_radio(const struct map * parameters,
636 struct action_spec *action);
639 static jb_err map_copy_parameter_html(struct map *out,
640 const struct map *in,
642 #if 0 /* unused function */
643 static jb_err map_copy_parameter_url(struct map *out,
644 const struct map *in,
646 #endif /* unused function */
648 static jb_err get_file_name_param(struct client_state *csp,
649 const struct map *parameters,
650 const char *param_name,
651 const char **pfilename);
653 /* Internal convenience functions */
654 static char *section_target(const unsigned sectionid);
656 /*********************************************************************
658 * Function : section_target
660 * Description : Given an unsigned (section id) n, produce a dynamically
661 * allocated string of the form #l<n>, for use in link
664 * XXX: The hash should be moved into the templates
665 * to make this function more generic and render
666 * stringify() obsolete.
669 * 1 : sectionid = start line number of section
671 * Returns : String with link target, or NULL if out of
674 *********************************************************************/
675 static char *section_target(const unsigned sectionid)
679 snprintf(buf, 30, "#l%d", sectionid);
685 /*********************************************************************
687 * Function : stringify
689 * Description : Convert a number into a dynamically allocated string.
692 * 1 : number = The number to convert.
694 * Returns : String with link target, or NULL if out of memory
696 *********************************************************************/
697 static char *stringify(const unsigned number)
701 snprintf(buf, sizeof(buf), "%i", number);
706 /*********************************************************************
708 * Function : map_copy_parameter_html
710 * Description : Copy a CGI parameter from one map to another, HTML
714 * 1 : out = target map
715 * 2 : in = source map
716 * 3 : name = name of cgi parameter to copy
718 * Returns : JB_ERR_OK on success
719 * JB_ERR_MEMORY on out-of-memory
720 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
723 *********************************************************************/
724 static jb_err map_copy_parameter_html(struct map *out,
725 const struct map *in,
735 value = lookup(in, name);
736 err = map(out, name, 1, html_encode(value), 0);
743 else if (*value == '\0')
745 return JB_ERR_CGI_PARAMS;
754 #if 0 /* unused function */
755 /*********************************************************************
757 * Function : map_copy_parameter_url
759 * Description : Copy a CGI parameter from one map to another, URL
763 * 1 : out = target map
764 * 2 : in = source map
765 * 3 : name = name of cgi parameter to copy
767 * Returns : JB_ERR_OK on success
768 * JB_ERR_MEMORY on out-of-memory
769 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
772 *********************************************************************/
773 static jb_err map_copy_parameter_url(struct map *out,
774 const struct map *in,
784 value = lookup(in, name);
785 err = map(out, name, 1, url_encode(value), 0);
792 else if (*value == '\0')
794 return JB_ERR_CGI_PARAMS;
801 #endif /* 0 - unused function */
804 /*********************************************************************
806 * Function : cgi_edit_actions_url_form
808 * Description : CGI function that displays a form for
812 * 1 : csp = Current client state (buffers, headers, etc...)
813 * 2 : rsp = http_response data structure for output
814 * 3 : parameters = map of cgi parameters
817 * i : (action index) Identifies the file to edit
818 * v : (version) File's last-modified time
819 * p : (pattern) Line number of pattern to edit
821 * Returns : JB_ERR_OK on success
822 * JB_ERR_MEMORY on out-of-memory
823 * JB_ERR_CGI_PARAMS if the CGI parameters are not
824 * specified or not valid.
826 *********************************************************************/
827 jb_err cgi_edit_actions_url_form(struct client_state *csp,
828 struct http_response *rsp,
829 const struct map *parameters)
831 struct map * exports;
833 struct editable_file * file;
834 struct file_line * cur_line;
835 unsigned line_number;
836 unsigned section_start_line_number = 0;
843 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
845 return cgi_error_disabled(csp, rsp);
848 err = get_number_param(csp, parameters, "p", &patternid);
854 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
857 /* No filename specified, can't read file, modified, or out of memory. */
858 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
861 cur_line = file->lines;
863 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
865 if (cur_line->type == FILE_LINE_ACTION)
867 section_start_line_number = line_number;
869 cur_line = cur_line->next;
872 if ( (cur_line == NULL)
873 || (line_number != patternid)
875 || (cur_line->type != FILE_LINE_URL))
877 /* Invalid "patternid" parameter */
878 edit_free_file(file);
879 return JB_ERR_CGI_PARAMS;
882 if (NULL == (exports = default_exports(csp, NULL)))
884 edit_free_file(file);
885 return JB_ERR_MEMORY;
888 err = map(exports, "f", 1, stringify(file->identifier), 0);
889 if (!err) err = map(exports, "v", 1, file->version_str, 1);
890 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
891 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
892 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
894 edit_free_file(file);
902 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
906 /*********************************************************************
908 * Function : cgi_edit_actions_add_url_form
910 * Description : CGI function that displays a form for
914 * 1 : csp = Current client state (buffers, headers, etc...)
915 * 2 : rsp = http_response data structure for output
916 * 3 : parameters = map of cgi parameters
919 * f : (filename) Identifies the file to edit
920 * v : (version) File's last-modified time
921 * s : (section) Line number of section to edit
923 * Returns : JB_ERR_OK on success
924 * JB_ERR_MEMORY on out-of-memory
925 * JB_ERR_CGI_PARAMS if the CGI parameters are not
926 * specified or not valid.
928 *********************************************************************/
929 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
930 struct http_response *rsp,
931 const struct map *parameters)
940 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
942 return cgi_error_disabled(csp, rsp);
945 if (NULL == (exports = default_exports(csp, NULL)))
947 return JB_ERR_MEMORY;
950 err = map_copy_parameter_html(exports, parameters, "f");
951 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
952 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
960 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
964 /*********************************************************************
966 * Function : cgi_edit_actions_remove_url_form
968 * Description : CGI function that displays a form for
972 * 1 : csp = Current client state (buffers, headers, etc...)
973 * 2 : rsp = http_response data structure for output
974 * 3 : parameters = map of cgi parameters
977 * f : (number) The action file identifier.
978 * v : (version) File's last-modified time
979 * p : (pattern) Line number of pattern to edit
981 * Returns : JB_ERR_OK on success
982 * JB_ERR_MEMORY on out-of-memory
983 * JB_ERR_CGI_PARAMS if the CGI parameters are not
984 * specified or not valid.
986 *********************************************************************/
987 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
988 struct http_response *rsp,
989 const struct map *parameters)
991 struct map * exports;
993 struct editable_file * file;
994 struct file_line * cur_line;
995 unsigned line_number;
996 unsigned section_start_line_number = 0;
1003 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1005 return cgi_error_disabled(csp, rsp);
1008 err = get_number_param(csp, parameters, "p", &patternid);
1014 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
1017 /* No filename specified, can't read file, modified, or out of memory. */
1018 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
1021 cur_line = file->lines;
1023 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
1025 if (cur_line->type == FILE_LINE_ACTION)
1027 section_start_line_number = line_number;
1029 cur_line = cur_line->next;
1032 if ( (cur_line == NULL)
1033 || (line_number != patternid)
1035 || (cur_line->type != FILE_LINE_URL))
1037 /* Invalid "patternid" parameter */
1038 edit_free_file(file);
1039 return JB_ERR_CGI_PARAMS;
1042 if (NULL == (exports = default_exports(csp, NULL)))
1044 edit_free_file(file);
1045 return JB_ERR_MEMORY;
1048 err = map(exports, "f", 1, stringify(file->identifier), 0);
1049 if (!err) err = map(exports, "v", 1, file->version_str, 1);
1050 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
1051 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
1052 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
1053 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
1055 edit_free_file(file);
1063 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
1067 /*********************************************************************
1069 * Function : edit_write_file
1071 * Description : Write a complete file to disk.
1074 * 1 : file = File to write.
1076 * Returns : JB_ERR_OK on success
1077 * JB_ERR_FILE on error writing to file.
1078 * JB_ERR_MEMORY on out of memory
1080 *********************************************************************/
1081 jb_err edit_write_file(struct editable_file * file)
1084 struct file_line * cur_line;
1085 struct stat statbuf[1];
1086 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
1087 digits in time_t, assuming this is a 64-bit
1088 machine, plus null terminator, plus one
1092 assert(file->filename);
1094 if (NULL == (fp = fopen(file->filename, "wb")))
1099 cur_line = file->lines;
1100 while (cur_line != NULL)
1104 if (fputs(cur_line->raw, fp) < 0)
1112 if (cur_line->prefix)
1114 if (fputs(cur_line->prefix, fp) < 0)
1120 if (cur_line->unprocessed)
1123 if (NULL != strchr(cur_line->unprocessed, '#'))
1125 /* Must quote '#' characters */
1132 /* Count number of # characters, so we know length of output string */
1133 src = cur_line->unprocessed;
1134 while (NULL != (src = strchr(src, '#')))
1139 assert(numhash > 0);
1141 /* Allocate new memory for string */
1142 len = strlen(cur_line->unprocessed) + (size_t)numhash;
1143 if (NULL == (str = malloc(len + 1)))
1145 /* Uh oh, just trashed file! */
1147 return JB_ERR_MEMORY;
1150 /* Copy string but quote hashes */
1151 src = cur_line->unprocessed;
1159 assert(numhash >= 0);
1165 assert(numhash == 0);
1166 assert(strlen(str) == len);
1167 assert(str == dest - len);
1168 assert(src - len <= cur_line->unprocessed);
1170 if ((strlen(str) != len) || (numhash != 0))
1173 * Escaping didn't work as expected, go spread the news.
1174 * Only reached in non-debugging builds.
1176 log_error(LOG_LEVEL_ERROR,
1177 "Looks like hash escaping failed. %s might be corrupted now.",
1181 if (fputs(str, fp) < 0)
1192 /* Can write without quoting '#' characters. */
1193 if (fputs(cur_line->unprocessed, fp) < 0)
1199 if (fputs(NEWLINE(file->newline), fp) < 0)
1207 /* FIXME: Write data from file->data->whatever */
1211 cur_line = cur_line->next;
1217 /* Update the version stamp in the file structure, since we just
1218 * wrote to the file & changed it's date.
1220 if (stat(file->filename, statbuf) < 0)
1222 /* Error, probably file not found. */
1225 file->version = (unsigned)statbuf->st_mtime;
1227 /* Correct file->version_str */
1228 freez(file->version_str);
1229 snprintf(version_buf, 22, "%u", file->version);
1230 version_buf[21] = '\0';
1231 file->version_str = strdup(version_buf);
1232 if (version_buf == NULL)
1234 return JB_ERR_MEMORY;
1241 /*********************************************************************
1243 * Function : edit_free_file
1245 * Description : Free a complete file in memory.
1248 * 1 : file = Data structure to free.
1252 *********************************************************************/
1253 void edit_free_file(struct editable_file * file)
1257 /* Silently ignore NULL pointer */
1261 edit_free_file_lines(file->lines);
1262 freez(file->version_str);
1264 file->parse_error_text = NULL; /* Statically allocated */
1265 file->parse_error = NULL;
1271 /*********************************************************************
1273 * Function : edit_free_file_lines
1275 * Description : Free an entire linked list of file lines.
1278 * 1 : first_line = Data structure to free.
1282 *********************************************************************/
1283 static void edit_free_file_lines(struct file_line * first_line)
1285 struct file_line * next_line;
1287 while (first_line != NULL)
1289 next_line = first_line->next;
1290 first_line->next = NULL;
1291 freez(first_line->raw);
1292 freez(first_line->prefix);
1293 freez(first_line->unprocessed);
1294 switch(first_line->type)
1296 case 0: /* special case if memory zeroed */
1297 case FILE_LINE_UNPROCESSED:
1298 case FILE_LINE_BLANK:
1299 case FILE_LINE_ALIAS_HEADER:
1300 case FILE_LINE_SETTINGS_HEADER:
1301 case FILE_LINE_DESCRIPTION_HEADER:
1302 case FILE_LINE_DESCRIPTION_ENTRY:
1303 case FILE_LINE_ALIAS_ENTRY:
1305 /* No data is stored for these */
1308 case FILE_LINE_ACTION:
1309 free_action(first_line->data.action);
1312 case FILE_LINE_SETTINGS_ENTRY:
1313 freez(first_line->data.setting.name);
1314 freez(first_line->data.setting.svalue);
1317 /* Should never happen */
1321 first_line->type = 0; /* paranoia */
1323 first_line = next_line;
1328 /*********************************************************************
1330 * Function : match_actions_file_header_line
1332 * Description : Match an actions file {{header}} line
1335 * 1 : line = String from file
1336 * 2 : name = Header to match against
1338 * Returns : 0 iff they match.
1340 *********************************************************************/
1341 static int match_actions_file_header_line(const char * line, const char * name)
1349 if ((line[0] != '{') || (line[1] != '{'))
1355 /* Look for optional whitespace */
1356 while ( (*line == ' ') || (*line == '\t') )
1361 /* Look for the specified name (case-insensitive) */
1363 if (0 != strncmpic(line, name, len))
1369 /* Look for optional whitespace */
1370 while ( (*line == ' ') || (*line == '\t') )
1375 /* Look for "}}" and end of string*/
1376 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1386 /*********************************************************************
1388 * Function : match_actions_file_header_line
1390 * Description : Match an actions file {{header}} line
1393 * 1 : line = String from file. Must not start with
1394 * whitespace (else infinite loop!)
1395 * 2 : pname = Destination for name
1396 * 2 : pvalue = Destination for value
1398 * Returns : JB_ERR_OK on success
1399 * JB_ERR_MEMORY on out-of-memory
1400 * JB_ERR_PARSE if there's no "=" sign, or if there's
1401 * nothing before the "=" sign (but empty
1402 * values *after* the "=" sign are legal).
1404 *********************************************************************/
1405 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1407 const char * name_end;
1408 const char * value_start;
1414 assert(*line != ' ');
1415 assert(*line != '\t');
1420 value_start = strchr(line, '=');
1421 if ((value_start == NULL) || (value_start == line))
1423 return JB_ERR_PARSE;
1426 name_end = value_start - 1;
1428 /* Eat any whitespace before the '=' */
1429 while ((*name_end == ' ') || (*name_end == '\t'))
1432 * we already know we must have at least 1 non-ws char
1433 * at start of buf - no need to check
1438 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1439 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1441 return JB_ERR_MEMORY;
1443 strncpy(*pname, line, name_len);
1444 (*pname)[name_len] = '\0';
1446 /* Eat any the whitespace after the '=' */
1448 while ((*value_start == ' ') || (*value_start == '\t'))
1453 if (NULL == (*pvalue = strdup(value_start)))
1457 return JB_ERR_MEMORY;
1464 /*********************************************************************
1466 * Function : edit_parse_actions_file
1468 * Description : Parse an actions file in memory.
1470 * Passed linked list must have the "data" member
1471 * zeroed, and must contain valid "next" and
1472 * "unprocessed" fields. The "raw" and "prefix"
1473 * fields are ignored, and "type" is just overwritten.
1475 * Note that on error the file may have been
1479 * 1 : file = Actions file to be parsed in-place.
1481 * Returns : JB_ERR_OK on success
1482 * JB_ERR_MEMORY on out-of-memory
1483 * JB_ERR_PARSE on error
1485 *********************************************************************/
1486 jb_err edit_parse_actions_file(struct editable_file * file)
1488 struct file_line * cur_line;
1490 const char * text; /* Text from a line */
1491 char * name; /* For lines of the form name=value */
1492 char * value; /* For lines of the form name=value */
1493 struct action_alias * alias_list = NULL;
1494 jb_err err = JB_ERR_OK;
1496 /* alias_list contains the aliases defined in this file.
1497 * It might be better to use the "file_line.data" fields
1498 * in the relavent places instead.
1501 cur_line = file->lines;
1503 /* A note about blank line support: Blank lines should only
1504 * ever occur as the last line in the file. This function
1505 * is more forgiving than that - FILE_LINE_BLANK can occur
1509 /* Skip leading blanks. Should only happen if file is
1510 * empty (which is valid, but pointless).
1512 while ( (cur_line != NULL)
1513 && (cur_line->unprocessed[0] == '\0') )
1516 cur_line->type = FILE_LINE_BLANK;
1517 cur_line = cur_line->next;
1520 if ( (cur_line != NULL)
1521 && (cur_line->unprocessed[0] != '{') )
1523 /* File doesn't start with a header */
1524 file->parse_error = cur_line;
1525 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1526 return JB_ERR_PARSE;
1529 if ( (cur_line != NULL) && (0 ==
1530 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1532 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1534 cur_line = cur_line->next;
1535 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1537 if (cur_line->unprocessed[0])
1539 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1541 err = split_line_on_equals(cur_line->unprocessed,
1542 &cur_line->data.setting.name,
1543 &cur_line->data.setting.svalue);
1544 if (err == JB_ERR_MEMORY)
1548 else if (err != JB_ERR_OK)
1550 /* Line does not contain a name=value pair */
1551 file->parse_error = cur_line;
1552 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1553 return JB_ERR_PARSE;
1558 cur_line->type = FILE_LINE_BLANK;
1560 cur_line = cur_line->next;
1564 if ( (cur_line != NULL) && (0 ==
1565 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1567 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1569 cur_line = cur_line->next;
1570 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1572 if (cur_line->unprocessed[0])
1574 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1578 cur_line->type = FILE_LINE_BLANK;
1580 cur_line = cur_line->next;
1584 if ( (cur_line != NULL) && (0 ==
1585 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1587 cur_line->type = FILE_LINE_ALIAS_HEADER;
1589 cur_line = cur_line->next;
1590 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1592 if (cur_line->unprocessed[0])
1594 /* define an alias */
1595 struct action_alias * new_alias;
1597 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1599 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1600 if (err == JB_ERR_MEMORY)
1604 else if (err != JB_ERR_OK)
1606 /* Line does not contain a name=value pair */
1607 file->parse_error = cur_line;
1608 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1609 return JB_ERR_PARSE;
1612 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1617 free_alias_list(alias_list);
1618 return JB_ERR_MEMORY;
1621 err = get_actions(value, alias_list, new_alias->action);
1624 /* Invalid action or out of memory */
1628 free_alias_list(alias_list);
1629 if (err == JB_ERR_MEMORY)
1635 /* Line does not contain a name=value pair */
1636 file->parse_error = cur_line;
1637 file->parse_error_text = "This alias does not specify a valid set of actions.";
1638 return JB_ERR_PARSE;
1644 new_alias->name = name;
1647 new_alias->next = alias_list;
1648 alias_list = new_alias;
1652 cur_line->type = FILE_LINE_BLANK;
1654 cur_line = cur_line->next;
1658 /* Header done, process the main part of the file */
1659 while (cur_line != NULL)
1661 /* At this point, (cur_line->unprocessed[0] == '{') */
1662 assert(cur_line->unprocessed[0] == '{');
1663 text = cur_line->unprocessed + 1;
1664 len = strlen(text) - 1;
1665 if (text[len] != '}')
1667 /* No closing } on header */
1668 free_alias_list(alias_list);
1669 file->parse_error = cur_line;
1670 file->parse_error_text = "Headers starting with '{' must have a "
1671 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1672 "must close with two brackets ('}}').";
1673 return JB_ERR_PARSE;
1678 /* An invalid {{ header. */
1679 free_alias_list(alias_list);
1680 file->parse_error = cur_line;
1681 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1682 "Please remember that the system (two-bracket) headers must "
1683 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1684 "and must appear before any actions (one-bracket) headers. "
1685 "Also note that system headers may not be repeated.";
1686 return JB_ERR_PARSE;
1689 while ( (*text == ' ') || (*text == '\t') )
1695 && ( (text[len - 1] == ' ')
1696 || (text[len - 1] == '\t') ) )
1701 cur_line->type = FILE_LINE_ACTION;
1703 /* Remove {} and make copy */
1704 if (NULL == (value = (char *) malloc(len + 1)))
1707 free_alias_list(alias_list);
1708 return JB_ERR_MEMORY;
1710 strncpy(value, text, len);
1714 err = get_actions(value, alias_list, cur_line->data.action);
1717 /* Invalid action or out of memory */
1719 free_alias_list(alias_list);
1720 if (err == JB_ERR_MEMORY)
1726 /* Line does not contain a name=value pair */
1727 file->parse_error = cur_line;
1728 file->parse_error_text = "This header does not specify a valid set of actions.";
1729 return JB_ERR_PARSE;
1733 /* Done with string - it was clobbered anyway */
1736 /* Process next line */
1737 cur_line = cur_line->next;
1739 /* Loop processing URL patterns */
1740 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1742 if (cur_line->unprocessed[0])
1744 /* Could parse URL here, but this isn't currently needed */
1746 cur_line->type = FILE_LINE_URL;
1750 cur_line->type = FILE_LINE_BLANK;
1752 cur_line = cur_line->next;
1754 } /* End main while(cur_line != NULL) loop */
1756 free_alias_list(alias_list);
1762 /*********************************************************************
1764 * Function : edit_read_file_lines
1766 * Description : Read all the lines of a file into memory.
1767 * Handles whitespace, comments and line continuation.
1770 * 1 : fp = File to read from. On return, this will be
1771 * at EOF but it will not have been closed.
1772 * 2 : pfile = Destination for a linked list of file_lines.
1773 * Will be set to NULL on error.
1774 * 3 : newline = How to handle newlines.
1776 * Returns : JB_ERR_OK on success
1777 * JB_ERR_MEMORY on out-of-memory
1779 *********************************************************************/
1780 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1782 struct file_line * first_line; /* Keep for return value or to free */
1783 struct file_line * cur_line; /* Current line */
1784 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1792 cur_line = first_line = zalloc(sizeof(struct file_line));
1793 if (cur_line == NULL)
1795 return JB_ERR_MEMORY;
1798 cur_line->type = FILE_LINE_UNPROCESSED;
1800 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1803 /* Out of memory or empty file. */
1804 /* Note that empty file is not an error we propogate up */
1806 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1811 prev_line = cur_line;
1812 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1813 if (cur_line == NULL)
1816 edit_free_file_lines(first_line);
1817 return JB_ERR_MEMORY;
1820 cur_line->type = FILE_LINE_UNPROCESSED;
1822 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1823 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1826 edit_free_file_lines(first_line);
1827 return JB_ERR_MEMORY;
1831 while (rval != JB_ERR_FILE);
1835 /* We allocated one too many - free it */
1836 prev_line->next = NULL;
1839 *pfile = first_line;
1844 /*********************************************************************
1846 * Function : edit_read_file
1848 * Description : Read a complete file into memory.
1849 * Handles CGI parameter parsing. If requested, also
1850 * checks the file's modification timestamp.
1853 * 1 : csp = Current client state (buffers, headers, etc...)
1854 * 2 : parameters = map of cgi parameters.
1855 * 3 : require_version = true to check "ver" parameter.
1856 * 4 : pfile = Destination for the file. Will be set
1860 * f : The action file identifier.
1861 * ver : (Only if require_version is nonzero)
1862 * Timestamp of the actions file. If wrong, this
1863 * function fails with JB_ERR_MODIFIED.
1865 * Returns : JB_ERR_OK on success
1866 * JB_ERR_MEMORY on out-of-memory
1867 * JB_ERR_CGI_PARAMS if "filename" was not specified
1869 * JB_ERR_FILE if the file cannot be opened or
1871 * JB_ERR_MODIFIED if version checking was requested and
1872 * failed - the file was modified outside
1873 * of this CGI editor instance.
1875 *********************************************************************/
1876 jb_err edit_read_file(struct client_state *csp,
1877 const struct map *parameters,
1878 int require_version,
1879 struct editable_file **pfile)
1881 struct file_line * lines;
1884 const char *filename = NULL;
1885 struct editable_file * file;
1886 unsigned version = 0;
1887 struct stat statbuf[1];
1888 char version_buf[22];
1889 int newline = NEWLINE_UNKNOWN;
1898 err = get_number_param(csp, parameters, "f", &i);
1899 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1901 filename = csp->config->actions_file[i];
1903 else if (JB_ERR_CGI_PARAMS == err)
1906 * Probably an old-school URL like
1907 * http://config.privoxy.org/edit-actions-list?f=default
1909 err = get_file_name_param(csp, parameters, "f", &filename);
1912 if (NULL == filename || stat(filename, statbuf) < 0)
1914 /* Error, probably file not found. */
1917 version = (unsigned) statbuf->st_mtime;
1919 if (require_version)
1921 unsigned specified_version;
1922 err = get_number_param(csp, parameters, "v", &specified_version);
1928 if (version != specified_version)
1930 return JB_ERR_MODIFIED;
1934 if (NULL == (fp = fopen(filename,"rb")))
1939 err = edit_read_file_lines(fp, &lines, &newline);
1948 file = (struct editable_file *) zalloc(sizeof(*file));
1951 edit_free_file_lines(lines);
1955 file->lines = lines;
1956 file->newline = newline;
1957 file->filename = filename;
1958 file->version = version;
1959 file->identifier = i;
1961 /* Correct file->version_str */
1962 freez(file->version_str);
1963 snprintf(version_buf, 22, "%u", file->version);
1964 version_buf[21] = '\0';
1965 file->version_str = strdup(version_buf);
1966 if (version_buf == NULL)
1968 edit_free_file(file);
1969 return JB_ERR_MEMORY;
1977 /*********************************************************************
1979 * Function : edit_read_actions_file
1981 * Description : Read a complete actions file into memory.
1982 * Handles CGI parameter parsing. If requested, also
1983 * checks the file's modification timestamp.
1985 * If this function detects an error in the categories
1986 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1987 * then it handles it by filling in the specified
1988 * response structure and returning JB_ERR_FILE.
1991 * 1 : csp = Current client state (buffers, headers, etc...)
1992 * 2 : rsp = HTTP response. Only filled in on error.
1993 * 2 : parameters = map of cgi parameters.
1994 * 3 : require_version = true to check "ver" parameter.
1995 * 4 : pfile = Destination for the file. Will be set
1999 * f : The actions file identifier.
2000 * ver : (Only if require_version is nonzero)
2001 * Timestamp of the actions file. If wrong, this
2002 * function fails with JB_ERR_MODIFIED.
2004 * Returns : JB_ERR_OK on success
2005 * JB_ERR_MEMORY on out-of-memory
2006 * JB_ERR_CGI_PARAMS if "filename" was not specified
2008 * JB_ERR_FILE if the file does not contain valid data,
2009 * or if file cannot be opened or
2010 * contains no data, or if version
2011 * checking was requested and failed.
2013 *********************************************************************/
2014 jb_err edit_read_actions_file(struct client_state *csp,
2015 struct http_response *rsp,
2016 const struct map *parameters,
2017 int require_version,
2018 struct editable_file **pfile)
2021 struct editable_file *file;
2022 static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
2030 err = edit_read_file(csp, parameters, require_version, &file);
2033 /* Try to handle if possible */
2034 if (err == JB_ERR_FILE)
2036 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
2038 else if (err == JB_ERR_MODIFIED)
2040 assert(require_version);
2041 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
2042 log_error(LOG_LEVEL_ERROR,
2043 "Blocking CGI edit request due to modification time mismatch.");
2044 if (acceptable_failures > 0)
2046 log_error(LOG_LEVEL_INFO,
2047 "The CGI editor will be turned off after another %d mismatche(s).",
2048 acceptable_failures);
2049 acceptable_failures--;
2053 log_error(LOG_LEVEL_INFO,
2054 "Timestamp mismatch limit reached, turning CGI editor off. "
2055 "Reload the configuration file to reenable it.");
2056 csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
2059 if (err == JB_ERR_OK)
2062 * Signal to higher-level CGI code that there was a problem but we
2063 * handled it, they should just return JB_ERR_OK.
2070 err = edit_parse_actions_file(file);
2073 if (err == JB_ERR_PARSE)
2075 err = cgi_error_parse(csp, rsp, file);
2076 if (err == JB_ERR_OK)
2079 * Signal to higher-level CGI code that there was a problem but we
2080 * handled it, they should just return JB_ERR_OK.
2085 edit_free_file(file);
2094 /*********************************************************************
2096 * Function : get_file_name_param
2098 * Description : Get the name of the file to edit from the parameters
2099 * passed to a CGI function using the old syntax.
2100 * This function handles security checks and only
2101 * accepts files that Privoxy already knows.
2104 * 1 : csp = Current client state (buffers, headers, etc...)
2105 * 2 : parameters = map of cgi parameters
2106 * 3 : param_name = The name of the parameter to read
2107 * 4 : pfilename = pointer to the filename in
2108 * csp->config->actions_file[] if found. Set to NULL on error.
2110 * Returns : JB_ERR_OK on success
2111 * JB_ERR_MEMORY on out-of-memory
2112 * JB_ERR_CGI_PARAMS if "filename" was not specified
2115 *********************************************************************/
2116 static jb_err get_file_name_param(struct client_state *csp,
2117 const struct map *parameters,
2118 const char *param_name,
2119 const char **pfilename)
2122 const char suffix[] = ".action";
2137 param = lookup(parameters, param_name);
2140 return JB_ERR_CGI_PARAMS;
2143 len = strlen(param);
2144 if (len >= FILENAME_MAX)
2147 return JB_ERR_CGI_PARAMS;
2151 * Check every character to see if it's legal.
2152 * Totally unnecessary but we do it anyway.
2155 while ((ch = *s++) != '\0')
2157 if ( ((ch < 'A') || (ch > 'Z'))
2158 && ((ch < 'a') || (ch > 'z'))
2159 && ((ch < '0') || (ch > '9'))
2163 /* Probable hack attempt. */
2164 return JB_ERR_CGI_PARAMS;
2168 /* Append extension */
2169 name_size = len + strlen(suffix) + 1;
2170 name = malloc(name_size);
2173 return JB_ERR_MEMORY;
2175 strlcpy(name, param, name_size);
2176 strlcat(name, suffix, name_size);
2179 fullpath = make_path(csp->config->confdir, name);
2182 if (fullpath == NULL)
2184 return JB_ERR_MEMORY;
2187 /* Check if the file is known */
2188 for (i = 0; i < MAX_AF_FILES; i++)
2190 if (NULL != csp->config->actions_file[i] &&
2191 !strcmp(fullpath, csp->config->actions_file[i]))
2194 *pfilename = csp->config->actions_file[i];
2202 return JB_ERR_CGI_PARAMS;
2206 /*********************************************************************
2208 * Function : get_url_spec_param
2210 * Description : Get a URL pattern from the parameters
2211 * passed to a CGI function. Removes leading/trailing
2212 * spaces and validates it.
2215 * 1 : csp = Current client state (buffers, headers, etc...)
2216 * 2 : parameters = map of cgi parameters
2217 * 3 : name = Name of CGI parameter to read
2218 * 4 : pvalue = destination for value. Will be malloc()'d.
2219 * Set to NULL on error.
2221 * Returns : JB_ERR_OK on success
2222 * JB_ERR_MEMORY on out-of-memory
2223 * JB_ERR_CGI_PARAMS if the parameter was not specified
2226 *********************************************************************/
2227 static jb_err get_url_spec_param(struct client_state *csp,
2228 const struct map *parameters,
2232 const char *orig_param;
2235 struct url_spec compiled[1];
2245 orig_param = lookup(parameters, name);
2248 return JB_ERR_CGI_PARAMS;
2251 /* Copy and trim whitespace */
2252 param = strdup(orig_param);
2255 return JB_ERR_MEMORY;
2259 /* Must be non-empty, and can't allow 1st character to be '{' */
2260 if (param[0] == '\0' || param[0] == '{')
2263 return JB_ERR_CGI_PARAMS;
2266 /* Check for embedded newlines */
2267 for (s = param; *s != '\0'; s++)
2269 if ((*s == '\r') || (*s == '\n'))
2272 return JB_ERR_CGI_PARAMS;
2276 /* Check that regex is valid */
2281 return JB_ERR_MEMORY;
2283 err = create_url_spec(compiled, s);
2288 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2290 free_url_spec(compiled);
2292 if (param[strlen(param) - 1] == '\\')
2295 * Must protect trailing '\\' from becoming line continuation character.
2296 * Two methods: 1) If it's a domain only, add a trailing '/'.
2297 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2299 if (strchr(param, '/') == NULL)
2301 err = string_append(¶m, "/");
2305 err = string_append(¶m, "(?:)");
2312 /* Check that the modified regex is valid */
2317 return JB_ERR_MEMORY;
2319 err = create_url_spec(compiled, s);
2324 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2326 free_url_spec(compiled);
2333 /*********************************************************************
2335 * Function : map_radio
2337 * Description : Map a set of radio button values. E.g. if you have
2338 * 3 radio buttons, declare them as:
2339 * <option type="radio" name="xyz" @xyz-a@>
2340 * <option type="radio" name="xyz" @xyz-b@>
2341 * <option type="radio" name="xyz" @xyz-c@>
2342 * Then map one of the @xyz-?@ variables to "checked"
2343 * and all the others to empty by calling:
2344 * map_radio(exports, "xyz", "abc", sel)
2345 * Where 'sel' is 'a', 'b', or 'c'.
2348 * 1 : exports = Exports map to modify.
2349 * 2 : optionname = name for map
2350 * 3 : values = null-terminated list of values;
2351 * 4 : value = Selected value.
2353 * CGI Parameters : None
2355 * Returns : JB_ERR_OK on success
2356 * JB_ERR_MEMORY on out-of-memory
2358 *********************************************************************/
2359 static jb_err map_radio(struct map * exports,
2360 const char * optionname,
2361 const char * values,
2367 const size_t len = strlen(optionname);
2368 const size_t buf_size = len + 3;
2374 buf = malloc(buf_size);
2377 return JB_ERR_MEMORY;
2380 strlcpy(buf, optionname, buf_size);
2382 /* XXX: this looks ... interesting */
2387 while ((c = *values++) != '\0')
2392 if (map(exports, buf, 1, "", 1))
2394 return JB_ERR_MEMORY;
2400 return map(exports, buf, 0, "checked", 1);
2404 /*********************************************************************
2406 * Function : cgi_error_modified
2408 * Description : CGI function that is called when a file is modified
2409 * outside the CGI editor.
2412 * 1 : csp = Current client state (buffers, headers, etc...)
2413 * 2 : rsp = http_response data structure for output
2414 * 3 : filename = The file that was modified.
2416 * CGI Parameters : none
2418 * Returns : JB_ERR_OK on success
2419 * JB_ERR_MEMORY on out-of-memory error.
2421 *********************************************************************/
2422 jb_err cgi_error_modified(struct client_state *csp,
2423 struct http_response *rsp,
2424 const char *filename)
2426 struct map *exports;
2433 if (NULL == (exports = default_exports(csp, NULL)))
2435 return JB_ERR_MEMORY;
2438 err = map(exports, "f", 1, html_encode(filename), 0);
2445 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2449 /*********************************************************************
2451 * Function : cgi_error_parse
2453 * Description : CGI function that is called when a file cannot
2454 * be parsed by the CGI editor.
2457 * 1 : csp = Current client state (buffers, headers, etc...)
2458 * 2 : rsp = http_response data structure for output
2459 * 3 : file = The file that was modified.
2461 * CGI Parameters : none
2463 * Returns : JB_ERR_OK on success
2464 * JB_ERR_MEMORY on out-of-memory error.
2466 *********************************************************************/
2467 jb_err cgi_error_parse(struct client_state *csp,
2468 struct http_response *rsp,
2469 struct editable_file *file)
2471 struct map *exports;
2473 struct file_line *cur_line;
2479 if (NULL == (exports = default_exports(csp, NULL)))
2481 return JB_ERR_MEMORY;
2484 err = map(exports, "f", 1, stringify(file->identifier), 0);
2485 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2487 cur_line = file->parse_error;
2490 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2491 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2499 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2503 /*********************************************************************
2505 * Function : cgi_error_file
2507 * Description : CGI function that is called when a file cannot be
2508 * opened by the CGI editor.
2511 * 1 : csp = Current client state (buffers, headers, etc...)
2512 * 2 : rsp = http_response data structure for output
2513 * 3 : filename = The file that was modified.
2515 * CGI Parameters : none
2517 * Returns : JB_ERR_OK on success
2518 * JB_ERR_MEMORY on out-of-memory error.
2520 *********************************************************************/
2521 jb_err cgi_error_file(struct client_state *csp,
2522 struct http_response *rsp,
2523 const char *filename)
2525 struct map *exports;
2532 if (NULL == (exports = default_exports(csp, NULL)))
2534 return JB_ERR_MEMORY;
2537 err = map(exports, "f", 1, html_encode(filename), 0);
2544 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2548 /*********************************************************************
2550 * Function : cgi_error_file_read_only
2552 * Description : CGI function that is called when a file cannot be
2553 * opened for writing by the CGI editor.
2556 * 1 : csp = Current client state (buffers, headers, etc...)
2557 * 2 : rsp = http_response data structure for output
2558 * 3 : filename = The file that we can't write to
2560 * CGI Parameters : none
2562 * Returns : JB_ERR_OK on success
2563 * JB_ERR_MEMORY on out-of-memory error.
2565 *********************************************************************/
2566 jb_err cgi_error_file_read_only(struct client_state *csp,
2567 struct http_response *rsp,
2568 const char *filename)
2570 struct map *exports;
2577 if (NULL == (exports = default_exports(csp, NULL)))
2579 return JB_ERR_MEMORY;
2582 err = map(exports, "f", 1, html_encode(filename), 0);
2589 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2593 /*********************************************************************
2595 * Function : cgi_edit_actions
2597 * Description : CGI function that allows the user to choose which
2598 * actions file to edit.
2601 * 1 : csp = Current client state (buffers, headers, etc...)
2602 * 2 : rsp = http_response data structure for output
2603 * 3 : parameters = map of cgi parameters
2605 * CGI Parameters : None
2607 * Returns : JB_ERR_OK on success
2608 * JB_ERR_MEMORY on out-of-memory error
2610 *********************************************************************/
2611 jb_err cgi_edit_actions(struct client_state *csp,
2612 struct http_response *rsp,
2613 const struct map *parameters)
2616 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2618 return cgi_error_disabled(csp, rsp);
2621 /* FIXME: Incomplete */
2623 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2628 /*********************************************************************
2630 * Function : cgi_edit_actions_list
2632 * Description : CGI function that edits the actions list.
2633 * FIXME: This function shouldn't FATAL ever.
2634 * FIXME: This function doesn't check the retval of map()
2636 * 1 : csp = Current client state (buffers, headers, etc...)
2637 * 2 : rsp = http_response data structure for output
2638 * 3 : parameters = map of cgi parameters
2640 * CGI Parameters : filename
2642 * Returns : JB_ERR_OK on success
2643 * JB_ERR_MEMORY on out-of-memory
2644 * JB_ERR_FILE if the file cannot be opened or
2646 * JB_ERR_CGI_PARAMS if "filename" was not specified
2649 *********************************************************************/
2650 jb_err cgi_edit_actions_list(struct client_state *csp,
2651 struct http_response *rsp,
2652 const struct map *parameters)
2654 char * section_template;
2655 char * url_template;
2660 struct map * exports;
2661 struct map * section_exports;
2662 struct map * url_exports;
2663 struct editable_file * file;
2664 struct file_line * cur_line;
2665 unsigned line_number = 0;
2666 unsigned prev_section_line_number = ((unsigned) (-1));
2668 struct file_list * fl;
2669 struct url_actions * b;
2670 char * buttons = NULL;
2673 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2675 return cgi_error_disabled(csp, rsp);
2678 if (NULL == (exports = default_exports(csp, NULL)))
2680 return JB_ERR_MEMORY;
2683 /* Load actions file */
2684 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2687 /* No filename specified, can't read file, or out of memory. */
2689 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2692 /* Find start of actions in file */
2693 cur_line = file->lines;
2695 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2697 cur_line = cur_line->next;
2702 * Conventional actions files should have a match all block
2704 * cur_line = {...global actions...}
2705 * cur_line->next = /
2706 * cur_line->next->next = {...actions...} or EOF
2708 if ( (cur_line != NULL)
2709 && (cur_line->type == FILE_LINE_ACTION)
2710 && (cur_line->next != NULL)
2711 && (cur_line->next->type == FILE_LINE_URL)
2712 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2713 && ( (cur_line->next->next == NULL)
2714 || (cur_line->next->next->type != FILE_LINE_URL)
2718 * Generate string with buttons to set actions for "/" to
2719 * any predefined set of actions (named standard.*, probably
2720 * residing in standard.action).
2723 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2726 edit_free_file(file);
2728 if (err == JB_ERR_FILE)
2730 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2735 err = template_fill(§ion_template, exports);
2738 edit_free_file(file);
2743 buttons = strdup("");
2744 for (i = 0; i < MAX_AF_FILES; i++)
2746 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2748 for (b = b->next; NULL != b; b = b->next)
2750 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2752 if (err || (NULL == (section_exports = new_map())))
2755 free(section_template);
2756 edit_free_file(file);
2758 return JB_ERR_MEMORY;
2761 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2763 if (err || (NULL == (s = strdup(section_template))))
2765 free_map(section_exports);
2767 free(section_template);
2768 edit_free_file(file);
2770 return JB_ERR_MEMORY;
2773 if (!err) err = template_fill(&s, section_exports);
2774 free_map(section_exports);
2775 if (!err) err = string_join(&buttons, s);
2780 freez(section_template);
2781 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2784 * Conventional actions file, supply extra editing help.
2785 * (e.g. don't allow them to make it an unconventional one).
2787 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2789 snprintf(buf, 150, "%d", line_number);
2790 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2791 snprintf(buf, 150, "%d", line_number + 2);
2792 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2793 if (!err) err = map(exports, "all-urls-actions", 1,
2794 actions_to_html(csp, cur_line->data.action), 0);
2796 /* Skip the 2 lines */
2797 cur_line = cur_line->next->next;
2801 * Note that prev_section_line_number is NOT set here.
2802 * This is deliberate and not a bug. It stops a "Move up"
2803 * option appearing on the next section. Clicking "Move
2804 * up" would make the actions file unconventional, which
2805 * we don't want, so we hide this option.
2811 * Non-standard actions file - does not begin with
2812 * the "All URLs" section.
2814 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2817 /* Set up global exports */
2819 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2820 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2821 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2823 /* Discourage private additions to default.action */
2825 if (!err) err = map_conditional(exports, "default-action",
2826 (strstr("default.action", file->filename) != NULL));
2829 edit_free_file(file);
2834 /* Should do all global exports above this point */
2836 /* Load templates */
2838 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2841 edit_free_file(file);
2843 if (err == JB_ERR_FILE)
2845 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2850 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2853 free(section_template);
2854 edit_free_file(file);
2856 if (err == JB_ERR_FILE)
2858 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2863 err = template_fill(§ion_template, exports);
2867 edit_free_file(file);
2872 err = template_fill(&url_template, exports);
2875 free(section_template);
2876 edit_free_file(file);
2881 if (NULL == (sections = strdup("")))
2883 free(section_template);
2885 edit_free_file(file);
2887 return JB_ERR_MEMORY;
2890 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2892 if (NULL == (section_exports = new_map()))
2895 free(section_template);
2897 edit_free_file(file);
2899 return JB_ERR_MEMORY;
2902 snprintf(buf, 150, "%d", line_number);
2903 err = map(section_exports, "s", 1, buf, 1);
2904 if (!err) err = map(section_exports, "actions", 1,
2905 actions_to_html(csp, cur_line->data.action), 0);
2908 && (cur_line->next != NULL)
2909 && (cur_line->next->type == FILE_LINE_URL))
2911 /* This section contains at least one URL, don't allow delete */
2912 err = map_block_killer(section_exports, "empty-section");
2916 if (!err) err = map_block_keep(section_exports, "empty-section");
2919 if (prev_section_line_number != ((unsigned)(-1)))
2921 /* Not last section */
2922 snprintf(buf, 150, "%d", prev_section_line_number);
2923 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2924 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2929 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2931 prev_section_line_number = line_number;
2936 free(section_template);
2938 edit_free_file(file);
2940 free_map(section_exports);
2944 /* Should do all section-specific exports above this point */
2946 if (NULL == (urls = strdup("")))
2949 free(section_template);
2951 edit_free_file(file);
2953 free_map(section_exports);
2954 return JB_ERR_MEMORY;
2959 cur_line = cur_line->next;
2962 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2964 if (NULL == (url_exports = new_map()))
2968 free(section_template);
2970 edit_free_file(file);
2972 free_map(section_exports);
2973 return JB_ERR_MEMORY;
2976 snprintf(buf, 150, "%d", line_number);
2977 err = map(url_exports, "p", 1, buf, 1);
2979 snprintf(buf, 150, "%d", url_1_2);
2980 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2982 if (!err) err = map(url_exports, "url-html", 1,
2983 html_encode(cur_line->unprocessed), 0);
2984 if (!err) err = map(url_exports, "url", 1,
2985 url_encode(cur_line->unprocessed), 0);
2991 free(section_template);
2993 edit_free_file(file);
2995 free_map(section_exports);
2996 free_map(url_exports);
3000 if (NULL == (s = strdup(url_template)))
3004 free(section_template);
3006 edit_free_file(file);
3008 free_map(section_exports);
3009 free_map(url_exports);
3010 return JB_ERR_MEMORY;
3013 err = template_fill(&s, section_exports);
3014 if (!err) err = template_fill(&s, url_exports);
3015 if (!err) err = string_append(&urls, s);
3017 free_map(url_exports);
3024 free(section_template);
3026 edit_free_file(file);
3028 free_map(section_exports);
3032 url_1_2 = 3 - url_1_2;
3034 cur_line = cur_line->next;
3038 err = map(section_exports, "urls", 1, urls, 0);
3040 /* Could also do section-specific exports here, but it wouldn't be as fast */
3042 snprintf(buf, 150, "%d", line_number);
3043 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
3045 if ( (cur_line != NULL)
3046 && (cur_line->type == FILE_LINE_ACTION))
3048 /* Not last section */
3049 if (!err) err = map_block_keep(section_exports, "s-next-exists");
3054 if (!err) err = map_block_killer(section_exports, "s-next-exists");
3060 free(section_template);
3062 edit_free_file(file);
3064 free_map(section_exports);
3068 if (NULL == (s = strdup(section_template)))
3071 free(section_template);
3073 edit_free_file(file);
3075 free_map(section_exports);
3076 return JB_ERR_MEMORY;
3079 err = template_fill(&s, section_exports);
3080 if (!err) err = string_append(§ions, s);
3083 free_map(section_exports);
3088 free(section_template);
3090 edit_free_file(file);
3096 edit_free_file(file);
3097 free(section_template);
3100 err = map(exports, "sections", 1, sections, 0);
3107 /* Could also do global exports here, but it wouldn't be as fast */
3109 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
3113 /*********************************************************************
3115 * Function : cgi_edit_actions_for_url
3117 * Description : CGI function that edits the Actions list.
3120 * 1 : csp = Current client state (buffers, headers, etc...)
3121 * 2 : rsp = http_response data structure for output
3122 * 3 : parameters = map of cgi parameters
3124 * CGI Parameters : None
3126 * Returns : JB_ERR_OK on success
3127 * JB_ERR_MEMORY on out-of-memory
3128 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3129 * specified or not valid.
3131 *********************************************************************/
3132 jb_err cgi_edit_actions_for_url(struct client_state *csp,
3133 struct http_response *rsp,
3134 const struct map *parameters)
3136 struct map * exports;
3138 struct editable_file * file;
3139 struct file_line * cur_line;
3140 unsigned line_number;
3142 struct re_filterfile_spec *filter_group;
3143 int i, have_filters = 0;
3145 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3147 return cgi_error_disabled(csp, rsp);
3150 err = get_number_param(csp, parameters, "s", §ionid);
3156 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3159 /* No filename specified, can't read file, modified, or out of memory. */
3160 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3163 cur_line = file->lines;
3165 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3167 cur_line = cur_line->next;
3170 if ( (cur_line == NULL)
3171 || (line_number != sectionid)
3173 || (cur_line->type != FILE_LINE_ACTION))
3175 /* Invalid "sectionid" parameter */
3176 edit_free_file(file);
3177 return JB_ERR_CGI_PARAMS;
3180 if (NULL == (exports = default_exports(csp, NULL)))
3182 edit_free_file(file);
3183 return JB_ERR_MEMORY;
3186 err = map(exports, "f", 1, stringify(file->identifier), 0);
3187 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3188 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3190 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3193 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3194 * length limitation and ignore clicks on the Submit buttons if
3195 * the resulting GET URL would be longer than their limit.
3197 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3198 * reached this limit and action editing stopped working in these
3199 * browsers (BR #1570678).
3201 * The config option split-large-forms works around this browser
3202 * bug (HTTP has no URL lenght limitation) by deviding the action
3203 * list form into multiple smaller ones. It means the URLs are shorter
3204 * and work in broken browsers as well, but the user can no longer change
3205 * all actions with one submit.
3207 * A better solution would be to switch to POST requests,
3208 * but this will do for now.
3210 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3212 /* Generate multiple smaller form by killing the big one. */
3213 err = map_block_killer(exports, "one-form-only");
3217 /* Default: Generate one large form by killing the smaller ones. */
3218 err = map_block_killer(exports, "multiple-forms");
3221 for (i = 0; i < MAX_AF_FILES; i++)
3223 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3225 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3233 edit_free_file(file);
3238 if (0 == have_filters)
3240 err = map(exports, "filter-params", 1, "", 1);
3245 * List available filters and their settings.
3247 char *filter_template;
3248 int filter_identifier = 0;
3249 char *prepared_templates[MAX_FILTER_TYPES];
3251 for (i = 0; i < MAX_FILTER_TYPES; i++)
3253 prepared_templates[i] = strdup("");
3256 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3259 edit_free_file(file);
3261 if (err == JB_ERR_FILE)
3263 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3268 err = template_fill(&filter_template, exports);
3270 for (i = 0; i < MAX_AF_FILES; i++)
3272 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3274 filter_group = csp->rlist[i]->f;
3275 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3277 char current_mode = 'x';
3279 struct list_entry *filter_name;
3280 struct map *line_exports;
3281 const int type = filter_group->type;
3282 const int multi_action_index = filter_type_info[type].multi_action_index;
3284 assert(type < MAX_FILTER_TYPES);
3286 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
3287 while ((filter_name != NULL)
3288 && (0 != strcmp(filter_group->name, filter_name->str)))
3290 filter_name = filter_name->next;
3293 if (filter_name != NULL)
3299 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
3300 while ((filter_name != NULL)
3301 && (0 != strcmp(filter_group->name, filter_name->str)))
3303 filter_name = filter_name->next;
3305 if (filter_name != NULL)
3311 /* Generate a unique serial number */
3312 snprintf(number, sizeof(number), "%x", filter_identifier++);
3313 number[sizeof(number) - 1] = '\0';
3315 line_exports = new_map();
3316 if (line_exports == NULL)
3318 err = JB_ERR_MEMORY;
3324 if (!err) err = map(line_exports, "index", 1, number, 1);
3325 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3326 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3327 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3328 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
3329 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
3330 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
3334 filter_line = strdup(filter_template);
3335 if (filter_line == NULL) err = JB_ERR_MEMORY;
3337 if (!err) err = template_fill(&filter_line, line_exports);
3338 string_join(&prepared_templates[type], filter_line);
3340 free_map(line_exports);
3345 freez(filter_template);
3347 /* Replace all filter macros with the aggregated templates */
3348 for (i = 0; i < MAX_FILTER_TYPES; i++)
3351 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
3356 /* Free aggregated templates */
3357 for (i = 0; i < MAX_FILTER_TYPES; i++)
3359 freez(prepared_templates[i]);
3364 if (!err) err = map_radio(exports, "filter-all", "nx",
3365 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3367 edit_free_file(file);
3375 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3379 /*********************************************************************
3381 * Function : cgi_edit_actions_submit
3383 * Description : CGI function that actually edits the Actions list.
3386 * 1 : csp = Current client state (buffers, headers, etc...)
3387 * 2 : rsp = http_response data structure for output
3388 * 3 : parameters = map of cgi parameters
3390 * CGI Parameters : None
3392 * Returns : JB_ERR_OK on success
3393 * JB_ERR_MEMORY on out-of-memory
3394 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3395 * specified or not valid.
3397 *********************************************************************/
3398 jb_err cgi_edit_actions_submit(struct client_state *csp,
3399 struct http_response *rsp,
3400 const struct map *parameters)
3405 size_t newtext_size;
3407 struct editable_file * file;
3408 struct file_line * cur_line;
3409 unsigned line_number;
3412 int filter_identifier;
3413 const char * action_set_name;
3415 struct file_list * fl;
3416 struct url_actions * b;
3418 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3420 return cgi_error_disabled(csp, rsp);
3423 err = get_number_param(csp, parameters, "s", §ionid);
3429 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3432 /* No filename specified, can't read file, modified, or out of memory. */
3433 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3436 cur_line = file->lines;
3438 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3440 cur_line = cur_line->next;
3443 if ( (cur_line == NULL)
3444 || (line_number != sectionid)
3446 || (cur_line->type != FILE_LINE_ACTION))
3448 /* Invalid "sectionid" parameter */
3449 edit_free_file(file);
3450 return JB_ERR_CGI_PARAMS;
3453 get_string_param(parameters, "p", &action_set_name);
3454 if (action_set_name != NULL)
3456 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3458 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3460 for (b = b->next; NULL != b; b = b->next)
3462 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3464 copy_action(cur_line->data.action, b->action);
3470 edit_free_file(file);
3471 return JB_ERR_CGI_PARAMS;
3477 err = actions_from_radio(parameters, cur_line->data.action);
3483 edit_free_file(file);
3487 ch = get_char_param(parameters, "filter_all");
3490 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3491 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3492 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3496 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3499 for (filter_identifier = 0; !err; filter_identifier++)
3506 * Filter state. Valid states are: 'Y' (active),
3507 * 'N' (inactive) and 'X' (no change).
3511 * Abbreviated filter type. Valid types are: 'F' (content filter),
3512 * 'S' (server-header filter) and 'C' (client-header filter).
3514 int multi_action_index = 0;
3516 /* Generate the keys */
3517 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3518 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3519 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3520 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3521 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3523 err = get_string_param(parameters, key_name, &name);
3532 type = get_char_param(parameters, key_type);
3536 multi_action_index = ACTION_MULTI_FILTER;
3539 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3542 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3545 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3548 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3551 log_error(LOG_LEVEL_ERROR,
3552 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3555 assert(multi_action_index);
3557 value = get_char_param(parameters, key_value);
3560 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3561 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3562 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3564 else if (value == 'N')
3566 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3567 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3569 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3570 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3573 else if (value == 'X')
3575 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3576 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3583 edit_free_file(file);
3587 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3590 edit_free_file(file);
3591 return JB_ERR_MEMORY;
3594 len = strlen(actiontext);
3598 * Empty action - must special-case this.
3599 * Simply setting len to 1 is sufficient...
3604 newtext_size = len + 2;
3605 if (NULL == (newtext = malloc(newtext_size)))
3609 edit_free_file(file);
3610 return JB_ERR_MEMORY;
3612 strlcpy(newtext, actiontext, newtext_size);
3616 newtext[len + 1] = '\0';
3618 freez(cur_line->raw);
3619 freez(cur_line->unprocessed);
3620 cur_line->unprocessed = newtext;
3622 err = edit_write_file(file);
3625 /* Error writing file */
3626 if (err == JB_ERR_FILE)
3628 /* Read-only file. */
3629 err = cgi_error_file_read_only(csp, rsp, file->filename);
3631 edit_free_file(file);
3635 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3636 (long) time(NULL), file->identifier, sectionid);
3638 edit_free_file(file);
3640 return cgi_redirect(rsp, target);
3644 /*********************************************************************
3646 * Function : cgi_edit_actions_url
3648 * Description : CGI function that actually edits a URL pattern in
3652 * 1 : csp = Current client state (buffers, headers, etc...)
3653 * 2 : rsp = http_response data structure for output
3654 * 3 : parameters = map of cgi parameters
3657 * filename : Identifies the file to edit
3658 * ver : File's last-modified time
3659 * section : Line number of section to edit
3660 * pattern : Line number of pattern to edit
3661 * newval : New value for pattern
3663 * Returns : JB_ERR_OK on success
3664 * JB_ERR_MEMORY on out-of-memory
3665 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3666 * specified or not valid.
3668 *********************************************************************/
3669 jb_err cgi_edit_actions_url(struct client_state *csp,
3670 struct http_response *rsp,
3671 const struct map *parameters)
3675 struct editable_file * file;
3676 struct file_line * cur_line;
3677 unsigned line_number;
3678 unsigned section_start_line_number = 0;
3686 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3688 return cgi_error_disabled(csp, rsp);
3691 err = get_number_param(csp, parameters, "p", &patternid);
3698 return JB_ERR_CGI_PARAMS;
3701 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3707 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3710 /* No filename specified, can't read file, modified, or out of memory. */
3712 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3716 cur_line = file->lines;
3718 while ((cur_line != NULL) && (line_number < patternid))
3720 if (cur_line->type == FILE_LINE_ACTION)
3722 section_start_line_number = line_number;
3724 cur_line = cur_line->next;
3728 if ( (cur_line == NULL)
3729 || (cur_line->type != FILE_LINE_URL))
3731 /* Invalid "patternid" parameter */
3733 edit_free_file(file);
3734 return JB_ERR_CGI_PARAMS;
3737 /* At this point, the line to edit is in cur_line */
3739 freez(cur_line->raw);
3740 freez(cur_line->unprocessed);
3741 cur_line->unprocessed = new_pattern;
3743 err = edit_write_file(file);
3746 /* Error writing file */
3747 if (err == JB_ERR_FILE)
3749 /* Read-only file. */
3750 err = cgi_error_file_read_only(csp, rsp, file->filename);
3752 edit_free_file(file);
3756 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3757 (long) time(NULL), file->identifier, section_start_line_number);
3759 edit_free_file(file);
3761 return cgi_redirect(rsp, target);
3765 /*********************************************************************
3767 * Function : cgi_edit_actions_add_url
3769 * Description : CGI function that actually adds a URL pattern to
3773 * 1 : csp = Current client state (buffers, headers, etc...)
3774 * 2 : rsp = http_response data structure for output
3775 * 3 : parameters = map of cgi parameters
3778 * filename : Identifies the file to edit
3779 * ver : File's last-modified time
3780 * section : Line number of section to edit
3781 * newval : New pattern
3783 * Returns : JB_ERR_OK on success
3784 * JB_ERR_MEMORY on out-of-memory
3785 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3786 * specified or not valid.
3788 *********************************************************************/
3789 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3790 struct http_response *rsp,
3791 const struct map *parameters)
3795 struct file_line * new_line;
3796 struct editable_file * file;
3797 struct file_line * cur_line;
3798 unsigned line_number;
3802 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3804 return cgi_error_disabled(csp, rsp);
3807 err = get_number_param(csp, parameters, "s", §ionid);
3814 return JB_ERR_CGI_PARAMS;
3817 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3823 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3826 /* No filename specified, can't read file, modified, or out of memory. */
3828 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3832 cur_line = file->lines;
3834 while ((cur_line != NULL) && (line_number < sectionid))
3836 cur_line = cur_line->next;
3840 if ( (cur_line == NULL)
3841 || (cur_line->type != FILE_LINE_ACTION))
3843 /* Invalid "sectionid" parameter */
3845 edit_free_file(file);
3846 return JB_ERR_CGI_PARAMS;
3849 /* At this point, the section header is in cur_line - add after this. */
3851 /* Allocate the new line */
3852 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3853 if (new_line == NULL)
3856 edit_free_file(file);
3857 return JB_ERR_MEMORY;
3860 /* Fill in the data members of the new line */
3861 new_line->raw = NULL;
3862 new_line->prefix = NULL;
3863 new_line->unprocessed = new_pattern;
3864 new_line->type = FILE_LINE_URL;
3866 /* Link new_line into the list, after cur_line */
3867 new_line->next = cur_line->next;
3868 cur_line->next = new_line;
3870 /* Done making changes, now commit */
3872 err = edit_write_file(file);
3875 /* Error writing file */
3876 if (err == JB_ERR_FILE)
3878 /* Read-only file. */
3879 err = cgi_error_file_read_only(csp, rsp, file->filename);
3881 edit_free_file(file);
3885 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3886 (long) time(NULL), file->identifier, sectionid);
3888 edit_free_file(file);
3890 return cgi_redirect(rsp, target);
3894 /*********************************************************************
3896 * Function : cgi_edit_actions_remove_url
3898 * Description : CGI function that actually removes a URL pattern from
3902 * 1 : csp = Current client state (buffers, headers, etc...)
3903 * 2 : rsp = http_response data structure for output
3904 * 3 : parameters = map of cgi parameters
3907 * f : (filename) Identifies the file to edit
3908 * v : (version) File's last-modified time
3909 * p : (pattern) Line number of pattern to remove
3911 * Returns : JB_ERR_OK on success
3912 * JB_ERR_MEMORY on out-of-memory
3913 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3914 * specified or not valid.
3916 *********************************************************************/
3917 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3918 struct http_response *rsp,
3919 const struct map *parameters)
3922 struct editable_file * file;
3923 struct file_line * cur_line;
3924 struct file_line * prev_line;
3925 unsigned line_number;
3926 unsigned section_start_line_number = 0;
3930 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3932 return cgi_error_disabled(csp, rsp);
3935 err = get_number_param(csp, parameters, "p", &patternid);
3941 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3944 /* No filename specified, can't read file, modified, or out of memory. */
3945 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3950 cur_line = file->lines;
3952 while ((cur_line != NULL) && (line_number < patternid))
3954 if (cur_line->type == FILE_LINE_ACTION)
3956 section_start_line_number = line_number;
3958 prev_line = cur_line;
3959 cur_line = cur_line->next;
3963 if ( (cur_line == NULL)
3964 || (prev_line == NULL)
3965 || (cur_line->type != FILE_LINE_URL))
3967 /* Invalid "patternid" parameter */
3968 edit_free_file(file);
3969 return JB_ERR_CGI_PARAMS;
3972 /* At this point, the line to remove is in cur_line, and the previous
3973 * one is in prev_line
3976 /* Unlink cur_line */
3977 prev_line->next = cur_line->next;
3978 cur_line->next = NULL;
3981 edit_free_file_lines(cur_line);
3983 err = edit_write_file(file);
3986 /* Error writing file */
3987 if (err == JB_ERR_FILE)
3989 /* Read-only file. */
3990 err = cgi_error_file_read_only(csp, rsp, file->filename);
3992 edit_free_file(file);
3996 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3997 (long) time(NULL), file->identifier, section_start_line_number);
3999 edit_free_file(file);
4001 return cgi_redirect(rsp, target);
4005 /*********************************************************************
4007 * Function : cgi_edit_actions_section_remove
4009 * Description : CGI function that actually removes a whole section from
4010 * the actions file. The section must be empty first
4011 * (else JB_ERR_CGI_PARAMS).
4014 * 1 : csp = Current client state (buffers, headers, etc...)
4015 * 2 : rsp = http_response data structure for output
4016 * 3 : parameters = map of cgi parameters
4019 * f : (filename) Identifies the file to edit
4020 * v : (version) File's last-modified time
4021 * s : (section) Line number of section to edit
4023 * Returns : JB_ERR_OK on success
4024 * JB_ERR_MEMORY on out-of-memory
4025 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4026 * specified or not valid.
4028 *********************************************************************/
4029 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
4030 struct http_response *rsp,
4031 const struct map *parameters)
4034 struct editable_file * file;
4035 struct file_line * cur_line;
4036 struct file_line * prev_line;
4037 unsigned line_number;
4041 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4043 return cgi_error_disabled(csp, rsp);
4046 err = get_number_param(csp, parameters, "s", §ionid);
4052 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4055 /* No filename specified, can't read file, modified, or out of memory. */
4056 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4060 cur_line = file->lines;
4063 while ((cur_line != NULL) && (line_number < sectionid))
4065 prev_line = cur_line;
4066 cur_line = cur_line->next;
4070 if ( (cur_line == NULL)
4071 || (cur_line->type != FILE_LINE_ACTION) )
4073 /* Invalid "sectionid" parameter */
4074 edit_free_file(file);
4075 return JB_ERR_CGI_PARAMS;
4078 if ( (cur_line->next != NULL)
4079 && (cur_line->next->type == FILE_LINE_URL) )
4081 /* Section not empty. */
4082 edit_free_file(file);
4083 return JB_ERR_CGI_PARAMS;
4086 /* At this point, the line to remove is in cur_line, and the previous
4087 * one is in prev_line
4090 /* Unlink cur_line */
4091 if (prev_line == NULL)
4093 /* Removing the first line from the file */
4094 file->lines = cur_line->next;
4098 prev_line->next = cur_line->next;
4100 cur_line->next = NULL;
4103 edit_free_file_lines(cur_line);
4105 err = edit_write_file(file);
4108 /* Error writing file */
4109 if (err == JB_ERR_FILE)
4111 /* Read-only file. */
4112 err = cgi_error_file_read_only(csp, rsp, file->filename);
4114 edit_free_file(file);
4118 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4119 (long) time(NULL), file->identifier);
4121 edit_free_file(file);
4123 return cgi_redirect(rsp, target);
4127 /*********************************************************************
4129 * Function : cgi_edit_actions_section_add
4131 * Description : CGI function that adds a new empty section to
4135 * 1 : csp = Current client state (buffers, headers, etc...)
4136 * 2 : rsp = http_response data structure for output
4137 * 3 : parameters = map of cgi parameters
4140 * f : (filename) Identifies the file to edit
4141 * v : (version) File's last-modified time
4142 * s : (section) Line number of section to add after, 0 for
4145 * Returns : JB_ERR_OK on success
4146 * JB_ERR_MEMORY on out-of-memory
4147 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4148 * specified or not valid.
4150 *********************************************************************/
4151 jb_err cgi_edit_actions_section_add(struct client_state *csp,
4152 struct http_response *rsp,
4153 const struct map *parameters)
4156 struct file_line * new_line;
4158 struct editable_file * file;
4159 struct file_line * cur_line;
4160 unsigned line_number;
4164 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4166 return cgi_error_disabled(csp, rsp);
4169 err = get_number_param(csp, parameters, "s", §ionid);
4175 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4178 /* No filename specified, can't read file, modified, or out of memory. */
4179 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4183 cur_line = file->lines;
4185 if (sectionid <= 1U)
4187 /* Add to start of file */
4188 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4190 /* There's something in the file, find the line before the first
4193 while ( (cur_line->next != NULL)
4194 && (cur_line->next->type != FILE_LINE_ACTION) )
4196 cur_line = cur_line->next;
4202 /* File starts with action line, so insert at top */
4208 /* Add after stated section. */
4209 while ((cur_line != NULL) && (line_number < sectionid))
4211 cur_line = cur_line->next;
4215 if ( (cur_line == NULL)
4216 || (cur_line->type != FILE_LINE_ACTION))
4218 /* Invalid "sectionid" parameter */
4219 edit_free_file(file);
4220 return JB_ERR_CGI_PARAMS;
4223 /* Skip through the section to find the last line in it. */
4224 while ( (cur_line->next != NULL)
4225 && (cur_line->next->type != FILE_LINE_ACTION) )
4227 cur_line = cur_line->next;
4232 /* At this point, the last line in the previous section is in cur_line
4233 * - add after this. (Or if we need to add as the first line, cur_line
4237 new_text = strdup("{}");
4238 if (NULL == new_text)
4240 edit_free_file(file);
4241 return JB_ERR_MEMORY;
4244 /* Allocate the new line */
4245 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4246 if (new_line == NULL)
4249 edit_free_file(file);
4250 return JB_ERR_MEMORY;
4253 /* Fill in the data members of the new line */
4254 new_line->raw = NULL;
4255 new_line->prefix = NULL;
4256 new_line->unprocessed = new_text;
4257 new_line->type = FILE_LINE_ACTION;
4259 if (cur_line != NULL)
4261 /* Link new_line into the list, after cur_line */
4262 new_line->next = cur_line->next;
4263 cur_line->next = new_line;
4267 /* Link new_line into the list, as first line */
4268 new_line->next = file->lines;
4269 file->lines = new_line;
4272 /* Done making changes, now commit */
4274 err = edit_write_file(file);
4277 /* Error writing file */
4278 if (err == JB_ERR_FILE)
4280 /* Read-only file. */
4281 err = cgi_error_file_read_only(csp, rsp, file->filename);
4283 edit_free_file(file);
4287 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4288 (long) time(NULL), file->identifier);
4290 edit_free_file(file);
4292 return cgi_redirect(rsp, target);
4296 /*********************************************************************
4298 * Function : cgi_edit_actions_section_swap
4300 * Description : CGI function that swaps the order of two sections
4301 * in the actions file. Note that this CGI can actually
4302 * swap any two arbitrary sections, but the GUI interface
4303 * currently only allows consecutive sections to be
4307 * 1 : csp = Current client state (buffers, headers, etc...)
4308 * 2 : rsp = http_response data structure for output
4309 * 3 : parameters = map of cgi parameters
4312 * f : (filename) Identifies the file to edit
4313 * v : (version) File's last-modified time
4314 * s1 : (section1) Line number of first section to swap
4315 * s2 : (section2) Line number of second section to swap
4317 * Returns : JB_ERR_OK on success
4318 * JB_ERR_MEMORY on out-of-memory
4319 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4320 * specified or not valid.
4322 *********************************************************************/
4323 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4324 struct http_response *rsp,
4325 const struct map *parameters)
4329 struct editable_file * file;
4330 struct file_line * cur_line;
4331 struct file_line * prev_line;
4332 struct file_line * line_before_section1;
4333 struct file_line * line_start_section1;
4334 struct file_line * line_end_section1;
4335 struct file_line * line_after_section1;
4336 struct file_line * line_before_section2;
4337 struct file_line * line_start_section2;
4338 struct file_line * line_end_section2;
4339 struct file_line * line_after_section2;
4340 unsigned line_number;
4344 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4346 return cgi_error_disabled(csp, rsp);
4349 err = get_number_param(csp, parameters, "s1", §ion1);
4350 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4356 if (section1 > section2)
4358 unsigned temp = section2;
4359 section2 = section1;
4363 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4366 /* No filename specified, can't read file, modified, or out of memory. */
4367 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4370 /* Start at the beginning... */
4372 cur_line = file->lines;
4375 /* ... find section1 ... */
4376 while ((cur_line != NULL) && (line_number < section1))
4378 prev_line = cur_line;
4379 cur_line = cur_line->next;
4383 if ( (cur_line == NULL)
4384 || (cur_line->type != FILE_LINE_ACTION) )
4386 /* Invalid "section1" parameter */
4387 edit_free_file(file);
4388 return JB_ERR_CGI_PARAMS;
4391 /* If no-op, we've validated params and can skip the rest. */
4392 if (section1 != section2)
4394 /* ... find the end of section1 ... */
4395 line_before_section1 = prev_line;
4396 line_start_section1 = cur_line;
4399 prev_line = cur_line;
4400 cur_line = cur_line->next;
4403 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4404 line_end_section1 = prev_line;
4405 line_after_section1 = cur_line;
4407 /* ... find section2 ... */
4408 while ((cur_line != NULL) && (line_number < section2))
4410 prev_line = cur_line;
4411 cur_line = cur_line->next;
4415 if ( (cur_line == NULL)
4416 || (cur_line->type != FILE_LINE_ACTION) )
4418 /* Invalid "section2" parameter */
4419 edit_free_file(file);
4420 return JB_ERR_CGI_PARAMS;
4423 /* ... find the end of section2 ... */
4424 line_before_section2 = prev_line;
4425 line_start_section2 = cur_line;
4428 prev_line = cur_line;
4429 cur_line = cur_line->next;
4432 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4433 line_end_section2 = prev_line;
4434 line_after_section2 = cur_line;
4436 /* Now have all the pointers we need. Do the swap. */
4438 /* Change the pointer to section1 to point to section2 instead */
4439 if (line_before_section1 == NULL)
4441 file->lines = line_start_section2;
4445 line_before_section1->next = line_start_section2;
4448 if (line_before_section2 == line_end_section1)
4450 /* Consecutive sections */
4451 line_end_section2->next = line_start_section1;
4455 line_end_section2->next = line_after_section1;
4456 line_before_section2->next = line_start_section1;
4459 /* Set the pointer from the end of section1 to the rest of the file */
4460 line_end_section1->next = line_after_section2;
4462 err = edit_write_file(file);
4465 /* Error writing file */
4466 if (err == JB_ERR_FILE)
4468 /* Read-only file. */
4469 err = cgi_error_file_read_only(csp, rsp, file->filename);
4471 edit_free_file(file);
4474 } /* END if (section1 != section2) */
4476 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4477 (long) time(NULL), file->identifier);
4479 edit_free_file(file);
4481 return cgi_redirect(rsp, target);
4484 #ifdef FEATURE_TOGGLE
4485 /*********************************************************************
4487 * Function : cgi_toggle
4489 * Description : CGI function that adds a new empty section to
4493 * 1 : csp = Current client state (buffers, headers, etc...)
4494 * 2 : rsp = http_response data structure for output
4495 * 3 : parameters = map of cgi parameters
4498 * set : If present, how to change toggle setting:
4499 * "enable", "disable", "toggle", or none (default).
4500 * mini : If present, use mini reply template.
4502 * Returns : JB_ERR_OK on success
4503 * JB_ERR_MEMORY on out-of-memory
4505 *********************************************************************/
4506 jb_err cgi_toggle(struct client_state *csp,
4507 struct http_response *rsp,
4508 const struct map *parameters)
4510 struct map *exports;
4512 const char *template_name;
4518 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4520 return cgi_error_disabled(csp, rsp);
4523 mode = get_char_param(parameters, "set");
4528 global_toggle_state = 1;
4530 else if (mode == 'D')
4533 global_toggle_state = 0;
4535 else if (mode == 'T')
4538 global_toggle_state = !global_toggle_state;
4541 if (NULL == (exports = default_exports(csp, "toggle")))
4543 return JB_ERR_MEMORY;
4546 template_name = (get_char_param(parameters, "mini")
4550 return template_fill_for_cgi(csp, template_name, exports, rsp);
4552 #endif /* def FEATURE_TOGGLE */
4554 /*********************************************************************
4556 * Function : javascriptify
4558 * Description : Converts a string into a form JavaScript will like.
4560 * Netscape 4's JavaScript sucks - it doesn't use
4561 * "id" parameters, so you have to set the "name"
4562 * used to submit a form element to something JavaScript
4563 * will like. (Or access the elements by index in an
4564 * array. That array contains >60 elements and will
4565 * be changed whenever we add a new action to the
4566 * editor, so I'm NOT going to use indexes that have
4567 * to be figured out by hand.)
4569 * Currently the only thing we have to worry about
4570 * is "-" ==> "_" conversion.
4572 * This is a length-preserving operation so it is
4573 * carried out in-place, no memory is allocated
4577 * 1 : identifier = String to make JavaScript-friendly.
4581 *********************************************************************/
4582 static void javascriptify(char * identifier)
4584 char * p = identifier;
4585 while (NULL != (p = strchr(p, '-')))
4592 /*********************************************************************
4594 * Function : actions_to_radio
4596 * Description : Converts a actionsfile entry into settings for
4597 * radio buttons and edit boxes on a HTML form.
4600 * 1 : exports = List of substitutions to add to.
4601 * 2 : action = Action to read
4603 * Returns : JB_ERR_OK on success
4604 * JB_ERR_MEMORY on out-of-memory
4606 *********************************************************************/
4607 static jb_err actions_to_radio(struct map * exports,
4608 const struct action_spec *action)
4610 unsigned mask = action->mask;
4611 unsigned add = action->add;
4619 mask = action->mask;
4622 /* sanity - prevents "-feature +feature" */
4626 #define DEFINE_ACTION_BOOL(name, bit) \
4627 if (!(mask & bit)) \
4629 current_mode = 'n'; \
4631 else if (add & bit) \
4633 current_mode = 'y'; \
4637 current_mode = 'x'; \
4639 if (map_radio(exports, name, "ynx", current_mode)) \
4641 return JB_ERR_MEMORY; \
4644 #define DEFINE_ACTION_STRING(name, bit, index) \
4645 DEFINE_ACTION_BOOL(name, bit); \
4648 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4651 checked = !strcmp(action->string[index], value); \
4655 checked = is_default; \
4657 mapped_param |= checked; \
4658 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4660 return JB_ERR_MEMORY; \
4663 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4664 if (map(exports, name "-param-custom", 1, \
4665 ((!mapped_param) ? "checked" : ""), 1)) \
4667 return JB_ERR_MEMORY; \
4669 if (map(exports, name "-param", 1, \
4670 (((add & bit) && !mapped_param) ? \
4671 action->string[index] : default_val), 1)) \
4673 return JB_ERR_MEMORY; \
4676 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4677 if (map(exports, name "-param", 1, \
4678 ((add & bit) ? action->string[index] : default_val), 1)) \
4680 return JB_ERR_MEMORY; \
4683 #define DEFINE_ACTION_MULTI(name, index) \
4684 if (action->multi_add[index]->first) \
4686 current_mode = 'y'; \
4688 else if (action->multi_remove_all[index]) \
4690 current_mode = 'n'; \
4692 else if (action->multi_remove[index]->first) \
4694 current_mode = 'y'; \
4698 current_mode = 'x'; \
4700 if (map_radio(exports, name, "ynx", current_mode)) \
4702 return JB_ERR_MEMORY; \
4705 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4707 #include "actionlist.h"
4709 #undef DEFINE_ACTION_MULTI
4710 #undef DEFINE_ACTION_STRING
4711 #undef DEFINE_ACTION_BOOL
4712 #undef DEFINE_ACTION_ALIAS
4713 #undef DEFINE_CGI_PARAM_CUSTOM
4714 #undef DEFINE_CGI_PARAM_RADIO
4715 #undef DEFINE_CGI_PARAM_NO_RADIO
4721 /*********************************************************************
4723 * Function : actions_from_radio
4725 * Description : Converts a map of parameters passed to a CGI function
4726 * into an actionsfile entry.
4729 * 1 : parameters = parameters to the CGI call
4730 * 2 : action = Action to change. Must be valid before
4731 * the call, actions not specified will be
4734 * Returns : JB_ERR_OK on success
4735 * JB_ERR_MEMORY on out-of-memory
4737 *********************************************************************/
4738 static jb_err actions_from_radio(const struct map * parameters,
4739 struct action_spec *action)
4744 const char * js_name;
4745 jb_err err = JB_ERR_OK;
4750 /* Statics are generally a potential race condition,
4751 * but in this case we're safe and don't need semaphores.
4752 * Be careful if you modify this function.
4754 * The js_name_arr's are never free()d, but this is no
4755 * problem, since they will only be created once and
4756 * used by all threads thereafter. -oes
4759 #define JAVASCRIPTIFY(dest_var, string) \
4761 static int first_time = 1; \
4762 static char *js_name_arr; \
4765 js_name_arr = strdup(string); \
4766 javascriptify(js_name_arr); \
4768 dest_var = js_name_arr; \
4772 #define DEFINE_ACTION_BOOL(name, bit) \
4773 JAVASCRIPTIFY(js_name, name); \
4774 ch = get_char_param(parameters, js_name); \
4777 action->add |= bit; \
4778 action->mask |= bit; \
4780 else if (ch == 'N') \
4782 action->add &= ~bit; \
4783 action->mask &= ~bit; \
4785 else if (ch == 'X') \
4787 action->add &= ~bit; \
4788 action->mask |= bit; \
4791 #define DEFINE_ACTION_STRING(name, bit, index) \
4792 JAVASCRIPTIFY(js_name, name); \
4793 ch = get_char_param(parameters, js_name); \
4797 JAVASCRIPTIFY(js_name, name "-mode"); \
4798 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4799 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4801 JAVASCRIPTIFY(js_name, name "-param"); \
4802 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4804 if (param != NULL) \
4806 if (NULL == (param_dup = strdup(param))) \
4808 return JB_ERR_MEMORY; \
4810 freez(action->string[index]); \
4811 action->add |= bit; \
4812 action->mask |= bit; \
4813 action->string[index] = param_dup; \
4816 else if (ch == 'N') \
4818 if (action->add & bit) \
4820 freez(action->string[index]); \
4822 action->add &= ~bit; \
4823 action->mask &= ~bit; \
4825 else if (ch == 'X') \
4827 if (action->add & bit) \
4829 freez(action->string[index]); \
4831 action->add &= ~bit; \
4832 action->mask |= bit; \
4835 #define DEFINE_ACTION_MULTI(name, index) \
4836 JAVASCRIPTIFY(js_name, name); \
4837 ch = get_char_param(parameters, js_name); \
4842 else if (ch == 'N') \
4844 list_remove_all(action->multi_add[index]); \
4845 list_remove_all(action->multi_remove[index]); \
4846 action->multi_remove_all[index] = 1; \
4848 else if (ch == 'X') \
4850 list_remove_all(action->multi_add[index]); \
4851 list_remove_all(action->multi_remove[index]); \
4852 action->multi_remove_all[index] = 0; \
4855 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4857 #include "actionlist.h"
4859 #undef DEFINE_ACTION_MULTI
4860 #undef DEFINE_ACTION_STRING
4861 #undef DEFINE_ACTION_BOOL
4862 #undef DEFINE_ACTION_ALIAS
4863 #undef JAVASCRIPTIFY
4869 #endif /* def FEATURE_CGI_EDIT_ACTIONS */