1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.54 2007/05/14 10:33:51 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.54 2007/05/14 10:33:51 fabiankeil
46 * - Use strlcpy() and strlcat() instead of strcpy() and strcat().
48 * Revision 1.53 2007/04/15 16:39:20 fabiankeil
49 * Introduce tags as alternative way to specify which
50 * actions apply to a request. At the moment tags can be
51 * created based on client and server headers.
53 * Revision 1.52 2007/04/12 10:41:23 fabiankeil
54 * - Don't mistake VC++'s _snprintf() for a snprintf() replacement.
55 * - Move some cgi_edit_actions_for_url() variables into structs.
56 * - Remove bogus comment.
58 * Revision 1.51 2007/04/08 13:21:05 fabiankeil
59 * Reference action files in CGI URLs by id instead
60 * of using the first part of the file name.
61 * Fixes BR 1694250 and BR 1590556.
63 * Revision 1.50 2007/03/29 11:40:34 fabiankeil
64 * Divide @filter-params@ into @client-header-filter-params@
65 * @content-filter-params@ and @server-header-filter-params@.
67 * Revision 1.49 2007/03/20 15:16:34 fabiankeil
68 * Use dedicated header filter actions instead of abusing "filter".
69 * Replace "filter-client-headers" and "filter-client-headers"
70 * with "server-header-filter" and "client-header-filter".
72 * Revision 1.48 2007/02/13 14:35:25 fabiankeil
73 * Replace hash escaping code to prevent
74 * crashes, memory and file corruption.
76 * Revision 1.47 2006/12/28 18:04:25 fabiankeil
77 * Fixed gcc43 conversion warnings.
79 * Revision 1.46 2006/12/27 18:44:52 fabiankeil
80 * Stop shadowing string.h's index().
82 * Revision 1.45 2006/12/21 12:57:48 fabiankeil
83 * Add config option "split-large-forms"
84 * to work around the browser bug reported
87 * Revision 1.44 2006/12/09 13:49:16 fabiankeil
88 * Fix configure option --disable-toggle.
89 * Thanks to Peter Thoenen for reporting this.
91 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
92 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
93 * with what was really the latest development (the v_3_0_branch branch)
95 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
96 * Remove a little residual debugging info
98 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
99 * Multiple filter file support
101 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
102 * Undo some damaging memory leak patches
104 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
105 * Removing a double free
107 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
108 * A few memory leaks plugged (mostly on error paths)
110 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
111 * Moved cgi_error_disabled() from cgiedit.c to
112 * cgi.c to re-enable build with --disable-editor.
113 * Fixes Bug #892744. Thanks to Matthew Fischer
116 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
117 * One line lost in last commit
119 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
120 * - All edit functions that redirect back to the list page
121 * now use cgi_redirect
122 * - All redirects now contain useless parameter "foo", whose
123 * value are raw seconds since epoch, in order to force
124 * Opera and Konqueror to properly reload the list. Closes
127 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
128 * Cosmetic: Renamed cryptic variable
130 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
131 * Fix: Don't free uninitialized struct editable_file
133 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
134 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
136 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
137 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
139 * Revision 1.41 2002/05/21 19:09:45 oes
140 * - Made Add/Edit/Remove URL Submit and Cancel
141 * buttons jump back to relevant section in eal
142 * - Bugfix: remove-url-form needs p export
144 * Revision 1.40 2002/05/19 11:34:35 jongfoster
145 * Handling read-only actions files better - report the actual
146 * error, not "Out of memory"!
149 * http://sourceforge.net/tracker/index.php?func=detail
150 * &aid=557905&group_id=11118&atid=111118
152 * Revision 1.39 2002/05/12 21:39:15 jongfoster
153 * - Adding Doxygen-style comments to structures and #defines.
154 * - Correcting function comments
156 * Revision 1.38 2002/05/03 23:00:38 jongfoster
157 * Support for templates for "standard actions" buttons.
160 * Revision 1.37 2002/04/30 11:14:52 oes
161 * Made csp the first parameter in *action_to_html
163 * Revision 1.36 2002/04/26 21:53:30 jongfoster
164 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
166 * Revision 1.35 2002/04/26 21:50:02 jongfoster
167 * Honouring default exports in edit-actions-for-url-filter template.
169 * Revision 1.34 2002/04/26 12:54:17 oes
170 * Adaptions to changes in actions.c
172 * Revision 1.33 2002/04/24 02:17:47 oes
173 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
175 * - Activated Jon's code for editing multiple AFs
176 * - cgi_edit_list_actions now provides context-sensitive
177 * help, looks up all action sets from standard.action and
178 * makes buttons for them in the catchall section
179 * - cgi_edit_action_submit now honors a p parameter, looks up
180 * the corresponding action set, and sets the catchall pattern's
181 * actions accordingly.
183 * Revision 1.32 2002/04/19 16:55:31 jongfoster
184 * Fixing newline problems. If we do our own text file newline
185 * mangling, we don't want the library to do any, so we need to
186 * open the files in *binary* mode.
188 * Revision 1.31 2002/04/18 19:21:08 jongfoster
189 * Added code to detect "conventional" action files, that start
190 * with a set of actions for all URLs (the pattern "/").
191 * These are special-cased in the "edit-actions-list" CGI, so
192 * that a special UI can be written for them.
194 * Revision 1.30 2002/04/10 13:38:35 oes
195 * load_template signature changed
197 * Revision 1.29 2002/04/08 16:59:08 oes
200 * Revision 1.28 2002/03/27 12:30:29 oes
201 * Deleted unsused variable
203 * Revision 1.27 2002/03/26 23:06:04 jongfoster
204 * Removing duplicate @ifs on the toggle page
206 * Revision 1.26 2002/03/26 22:59:17 jongfoster
207 * Fixing /toggle to display status consistently.
209 * Revision 1.25 2002/03/26 22:29:54 swa
210 * we have a new homepage!
212 * Revision 1.24 2002/03/24 15:23:33 jongfoster
215 * Revision 1.23 2002/03/24 13:32:41 swa
216 * name change related issues
218 * Revision 1.22 2002/03/24 13:25:43 swa
219 * name change related issues
221 * Revision 1.21 2002/03/22 18:02:48 jongfoster
222 * Fixing remote toggle
224 * Revision 1.20 2002/03/16 20:28:34 oes
225 * Added descriptions to the filters so users will know what they select in the cgi editor
227 * Revision 1.19 2002/03/16 18:38:14 jongfoster
228 * Stopping stupid or malicious users from breaking the actions
229 * file using the web-based editor.
231 * Revision 1.18 2002/03/16 14:57:44 jongfoster
232 * Full support for enabling/disabling modular filters.
234 * Revision 1.17 2002/03/16 14:26:42 jongfoster
235 * First version of modular filters support - READ ONLY!
236 * Fixing a double-free bug in the out-of-memory handling in map_radio().
238 * Revision 1.16 2002/03/07 03:46:17 oes
239 * Fixed compiler warnings
241 * Revision 1.15 2002/03/06 22:54:35 jongfoster
242 * Automated function-comment nitpicking.
244 * Revision 1.14 2002/03/05 00:24:51 jongfoster
245 * Patch to always edit the current actions file.
247 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
248 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
250 * Revision 1.12 2002/03/03 09:18:03 joergs
251 * Made jumbjuster work on AmigaOS again.
253 * Revision 1.11 2002/01/23 01:03:31 jongfoster
254 * Fixing gcc [CygWin] compiler warnings
256 * Revision 1.10 2002/01/23 00:22:59 jongfoster
257 * Adding new function cgi_edit_actions_section_swap(), to reorder
260 * Adding get_url_spec_param() to get a validated URL pattern.
262 * Moving edit_read_line() out of this file and into loaders.c.
264 * Adding missing html_encode() to many CGI functions.
266 * Moving the functions that #include actionlist.h to the end of the file,
267 * because the Visual C++ 97 debugger gets extremely confused if you try
268 * to debug any code that comes after them in the file.
270 * Major optimizations in cgi_edit_actions_list() to reduce the size of
271 * the generated HTML (down 40% from 550k to 304k), with major side-effects
272 * throughout the editor and templates. In particular, the length of the
273 * URLs throughout the editor has been drastically reduced, by cutting
274 * paramater names down to 1 character and CGI names down to 3-4
275 * characters, by removing all non-essential CGI paramaters even at the
276 * expense of having to re-read the actions file for the most trivial
277 * page, and by using relative rather than absolute URLs. This means
278 * that this (typical example):
280 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
281 * filename=ijb&ver=1011487572&section=12&pattern=13
282 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
286 * <a href="eau?f=ijb&v=1011487572&p=13">
288 * Revision 1.9 2002/01/17 20:56:22 jongfoster
289 * Replacing hard references to the URL of the config interface
290 * with #defines from project.h
292 * Revision 1.8 2001/11/30 23:35:51 jongfoster
293 * Renaming actionsfile to ijb.action
295 * Revision 1.7 2001/11/13 00:28:24 jongfoster
296 * - Renaming parameters from edit-actions-for-url so that they only
297 * contain legal JavaScript characters. If we wanted to write
298 * JavaScript that worked with Netscape 4, this is nessacery.
299 * (Note that at the moment the JavaScript doesn't actually work
300 * with Netscape 4, but now this is purely a template issue, not
301 * one affecting code).
302 * - Adding new CGIs for use by non-JavaScript browsers:
303 * edit-actions-url-form
304 * edit-actions-add-url-form
305 * edit-actions-remove-url-form
308 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
309 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
310 * by and __OS2__ ifdef.
312 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
313 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
314 * threads to call select() simultaneously. So, it's time to do a real, live,
315 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
316 * (native). Both versions will work, but using __OS2__ offers multi-threading.
318 * Revision 1.4 2001/10/23 21:48:19 jongfoster
319 * Cleaning up error handling in CGI functions - they now send back
320 * a HTML error page and should never cause a FATAL error. (Fixes one
321 * potential source of "denial of service" attacks).
323 * CGI actions file editor that works and is actually useful.
325 * Ability to toggle JunkBuster remotely using a CGI call.
327 * You can turn off both the above features in the main configuration
328 * file, e.g. if you are running a multi-user proxy.
330 * Revision 1.3 2001/10/14 22:12:49 jongfoster
331 * New version of CGI-based actionsfile editor.
332 * Major changes, including:
333 * - Completely new file parser and file output routines
334 * - edit-actions CGI renamed edit-actions-for-url
335 * - All CGIs now need a filename parameter, except for...
336 * - New CGI edit-actions which doesn't need a filename,
337 * to allow you to start the editor up.
338 * - edit-actions-submit now works, and now automatically
339 * redirects you back to the main edit-actions-list handler.
341 * Revision 1.2 2001/09/16 17:05:14 jongfoster
342 * Removing unused #include showarg.h
344 * Revision 1.1 2001/09/16 15:47:37 jongfoster
345 * First version of CGI-based edit interface. This is very much a
346 * work-in-progress, and you can't actually use it to edit anything
347 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
348 * to have any effect.
351 **********************************************************************/
357 * FIXME: Following includes copied from cgi.c - which are actually needed?
362 #include <sys/types.h>
366 #include <sys/stat.h>
371 #include "cgisimple.h"
375 #include "miscutil.h"
378 #ifdef FEATURE_TOGGLE
379 /* loadcfg.h is for global_toggle_state only */
381 #endif /* def FEATURE_TOGGLE */
382 #include "urlmatch.h"
384 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
387 #ifdef FEATURE_CGI_EDIT_ACTIONS
390 * A line in an editable_file.
394 /** Next entry in the linked list */
395 struct file_line * next;
397 /** The raw data, to write out if this line is unmodified. */
400 /** Comments and/or whitespace to put before this line if it's modified
401 and then written out. */
404 /** The actual data, as a string. Line continuation and comment removal
405 are performed on the data read from file before it's stored here, so
406 it will be a single line of data. */
409 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
412 /** The actual data, processed into some sensible data type. */
416 /** An action specification. */
417 struct action_spec action[1];
419 /** A name=value pair. */
423 /** The name in the name=value pair. */
426 /** The value in the name=value pair, as a string. */
429 /** The value in the name=value pair, as an integer. */
434 /* Add more data types here... e.g.
437 struct url_spec url[1];
441 struct action_spec action[1];
451 /** This file_line has not been processed yet. */
452 #define FILE_LINE_UNPROCESSED 1
454 /** This file_line is blank. Can only appear at the end of a file, due to
455 the way the parser works. */
456 #define FILE_LINE_BLANK 2
458 /** This file_line says {{alias}}. */
459 #define FILE_LINE_ALIAS_HEADER 3
461 /** This file_line defines an alias. */
462 #define FILE_LINE_ALIAS_ENTRY 4
464 /** This file_line defines an {action}. */
465 #define FILE_LINE_ACTION 5
467 /** This file_line specifies a URL pattern. */
468 #define FILE_LINE_URL 6
470 /** This file_line says {{settings}}. */
471 #define FILE_LINE_SETTINGS_HEADER 7
473 /** This file_line is in a {{settings}} block. */
474 #define FILE_LINE_SETTINGS_ENTRY 8
476 /** This file_line says {{description}}. */
477 #define FILE_LINE_DESCRIPTION_HEADER 9
479 /** This file_line is in a {{description}} block. */
480 #define FILE_LINE_DESCRIPTION_ENTRY 10
484 * A configuration file, in a format that can be edited and written back to
489 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
490 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
491 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
492 const char * version_str; /**< Last modification time, as a string. For CGI param. */
493 /**< Can be used in URL without using url_param(). */
494 unsigned version; /**< Last modification time - prevents chaos with
495 the browser's "back" button. Note that this is a
496 time_t cast to an unsigned. When comparing, always
497 cast the time_t to an unsigned, and *NOT* vice-versa.
498 This may lose the top few bits, but they're not
499 significant anyway. */
500 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
501 Note that changing this after the file has been
502 read in will cause a mess. */
503 struct file_line * parse_error; /**< On parse error, this is the offending line. */
504 const char * parse_error_text; /**< On parse error, this is the problem.
505 (Statically allocated) */
509 * Information about the filter types.
510 * Used for macro replacement in cgi_edit_actions_for_url.
512 struct filter_type_info
514 const int multi_action_index; /**< The multi action index as defined in project.h */
515 const char *macro_name; /**< Name of the macro that has to be replaced
516 with the prepared templates.
517 For example "content-filter-params" */
518 const char *type; /**< Name of the filter type,
519 for example "server-header-filter". */
520 const char *abbr_type; /**< Abbreviation of the filter type, usually the
521 first or second character capitalized */
522 const char *anchor; /**< Anchor for the User Manual link,
523 for example "SERVER-HEADER-FILTER" */
526 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
527 const static struct filter_type_info filter_type_info[] =
531 "content-filter-params", "filter",
535 ACTION_MULTI_CLIENT_HEADER_FILTER,
536 "client-header-filter-params", "client-header-filter",
537 "C", "CLIENT-HEADER-FILTER"
540 ACTION_MULTI_SERVER_HEADER_FILTER,
541 "server-header-filter-params", "server-header-filter",
542 "S", "SERVER-HEADER-FILTER"
545 ACTION_MULTI_CLIENT_HEADER_TAGGER,
546 "client-header-tagger-params", "client-header-tagger",
547 "L", "CLIENT-HEADER-TAGGER"
550 ACTION_MULTI_SERVER_HEADER_TAGGER,
551 "server-header-tagger-params", "server-header-tagger",
552 "E", "SERVER-HEADER-TAGGER"
556 /* FIXME: Following non-static functions should be prototyped in .h or made static */
558 /* Functions to read and write arbitrary config files */
559 jb_err edit_read_file(struct client_state *csp,
560 const struct map *parameters,
562 struct editable_file **pfile);
563 jb_err edit_write_file(struct editable_file * file);
564 void edit_free_file(struct editable_file * file);
566 /* Functions to read and write actions files */
567 jb_err edit_parse_actions_file(struct editable_file * file);
568 jb_err edit_read_actions_file(struct client_state *csp,
569 struct http_response *rsp,
570 const struct map *parameters,
572 struct editable_file **pfile);
575 jb_err cgi_error_modified(struct client_state *csp,
576 struct http_response *rsp,
577 const char *filename);
578 jb_err cgi_error_parse(struct client_state *csp,
579 struct http_response *rsp,
580 struct editable_file *file);
581 jb_err cgi_error_file(struct client_state *csp,
582 struct http_response *rsp,
583 const char *filename);
584 jb_err cgi_error_file_read_only(struct client_state *csp,
585 struct http_response *rsp,
586 const char *filename);
588 /* Internal arbitrary config file support functions */
589 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
590 static void edit_free_file_lines(struct file_line * first_line);
592 /* Internal actions file support functions */
593 static int match_actions_file_header_line(const char * line, const char * name);
594 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
596 /* Internal parameter parsing functions */
597 static jb_err get_url_spec_param(struct client_state *csp,
598 const struct map *parameters,
603 /* Internal actionsfile <==> HTML conversion functions */
604 static jb_err map_radio(struct map * exports,
605 const char * optionname,
608 static jb_err actions_to_radio(struct map * exports,
609 const struct action_spec *action);
610 static jb_err actions_from_radio(const struct map * parameters,
611 struct action_spec *action);
614 static jb_err map_copy_parameter_html(struct map *out,
615 const struct map *in,
617 #if 0 /* unused function */
618 static jb_err map_copy_parameter_url(struct map *out,
619 const struct map *in,
621 #endif /* unused function */
623 static jb_err get_file_name_param(struct client_state *csp,
624 const struct map *parameters,
625 const char *param_name,
626 const char **pfilename);
628 /* Internal convenience functions */
629 static char *section_target(const unsigned sectionid);
631 /*********************************************************************
633 * Function : section_target
635 * Description : Given an unsigned (section id) n, produce a dynamically
636 * allocated string of the form #l<n>, for use in link
639 * XXX: The hash should be moved into the templates
640 * to make this function more generic and render
641 * stringify() obsolete.
644 * 1 : sectionid = start line number of section
646 * Returns : String with link target, or NULL if out of
649 *********************************************************************/
650 static char *section_target(const unsigned sectionid)
654 snprintf(buf, 30, "#l%d", sectionid);
660 /*********************************************************************
662 * Function : stringify
664 * Description : Convert a number into a dynamically allocated string.
667 * 1 : number = The number to convert.
669 * Returns : String with link target, or NULL if out of memory
671 *********************************************************************/
672 static char *stringify(const unsigned number)
676 snprintf(buf, sizeof(buf), "%i", number);
681 /*********************************************************************
683 * Function : map_copy_parameter_html
685 * Description : Copy a CGI parameter from one map to another, HTML
689 * 1 : out = target map
690 * 2 : in = source map
691 * 3 : name = name of cgi parameter to copy
693 * Returns : JB_ERR_OK on success
694 * JB_ERR_MEMORY on out-of-memory
695 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
698 *********************************************************************/
699 static jb_err map_copy_parameter_html(struct map *out,
700 const struct map *in,
710 value = lookup(in, name);
711 err = map(out, name, 1, html_encode(value), 0);
718 else if (*value == '\0')
720 return JB_ERR_CGI_PARAMS;
729 #if 0 /* unused function */
730 /*********************************************************************
732 * Function : map_copy_parameter_url
734 * Description : Copy a CGI parameter from one map to another, URL
738 * 1 : out = target map
739 * 2 : in = source map
740 * 3 : name = name of cgi parameter to copy
742 * Returns : JB_ERR_OK on success
743 * JB_ERR_MEMORY on out-of-memory
744 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
747 *********************************************************************/
748 static jb_err map_copy_parameter_url(struct map *out,
749 const struct map *in,
759 value = lookup(in, name);
760 err = map(out, name, 1, url_encode(value), 0);
767 else if (*value == '\0')
769 return JB_ERR_CGI_PARAMS;
776 #endif /* 0 - unused function */
779 /*********************************************************************
781 * Function : cgi_edit_actions_url_form
783 * Description : CGI function that displays a form for
787 * 1 : csp = Current client state (buffers, headers, etc...)
788 * 2 : rsp = http_response data structure for output
789 * 3 : parameters = map of cgi parameters
792 * i : (action index) Identifies the file to edit
793 * v : (version) File's last-modified time
794 * p : (pattern) Line number of pattern to edit
796 * Returns : JB_ERR_OK on success
797 * JB_ERR_MEMORY on out-of-memory
798 * JB_ERR_CGI_PARAMS if the CGI parameters are not
799 * specified or not valid.
801 *********************************************************************/
802 jb_err cgi_edit_actions_url_form(struct client_state *csp,
803 struct http_response *rsp,
804 const struct map *parameters)
806 struct map * exports;
808 struct editable_file * file;
809 struct file_line * cur_line;
810 unsigned line_number;
811 unsigned section_start_line_number = 0;
818 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
820 return cgi_error_disabled(csp, rsp);
823 err = get_number_param(csp, parameters, "p", &patternid);
829 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
832 /* No filename specified, can't read file, modified, or out of memory. */
833 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
836 cur_line = file->lines;
838 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
840 if (cur_line->type == FILE_LINE_ACTION)
842 section_start_line_number = line_number;
844 cur_line = cur_line->next;
847 if ( (cur_line == NULL)
848 || (line_number != patternid)
850 || (cur_line->type != FILE_LINE_URL))
852 /* Invalid "patternid" parameter */
853 edit_free_file(file);
854 return JB_ERR_CGI_PARAMS;
857 if (NULL == (exports = default_exports(csp, NULL)))
859 edit_free_file(file);
860 return JB_ERR_MEMORY;
863 err = map(exports, "f", 1, stringify(file->identifier), 0);
864 if (!err) err = map(exports, "v", 1, file->version_str, 1);
865 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
866 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
867 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
869 edit_free_file(file);
877 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
881 /*********************************************************************
883 * Function : cgi_edit_actions_add_url_form
885 * Description : CGI function that displays a form for
889 * 1 : csp = Current client state (buffers, headers, etc...)
890 * 2 : rsp = http_response data structure for output
891 * 3 : parameters = map of cgi parameters
894 * f : (filename) Identifies the file to edit
895 * v : (version) File's last-modified time
896 * s : (section) Line number of section to edit
898 * Returns : JB_ERR_OK on success
899 * JB_ERR_MEMORY on out-of-memory
900 * JB_ERR_CGI_PARAMS if the CGI parameters are not
901 * specified or not valid.
903 *********************************************************************/
904 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
905 struct http_response *rsp,
906 const struct map *parameters)
915 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
917 return cgi_error_disabled(csp, rsp);
920 if (NULL == (exports = default_exports(csp, NULL)))
922 return JB_ERR_MEMORY;
925 err = map_copy_parameter_html(exports, parameters, "f");
926 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
927 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
935 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
939 /*********************************************************************
941 * Function : cgi_edit_actions_remove_url_form
943 * Description : CGI function that displays a form for
947 * 1 : csp = Current client state (buffers, headers, etc...)
948 * 2 : rsp = http_response data structure for output
949 * 3 : parameters = map of cgi parameters
952 * f : (number) The action file identifier.
953 * v : (version) File's last-modified time
954 * p : (pattern) Line number of pattern to edit
956 * Returns : JB_ERR_OK on success
957 * JB_ERR_MEMORY on out-of-memory
958 * JB_ERR_CGI_PARAMS if the CGI parameters are not
959 * specified or not valid.
961 *********************************************************************/
962 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
963 struct http_response *rsp,
964 const struct map *parameters)
966 struct map * exports;
968 struct editable_file * file;
969 struct file_line * cur_line;
970 unsigned line_number;
971 unsigned section_start_line_number = 0;
978 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
980 return cgi_error_disabled(csp, rsp);
983 err = get_number_param(csp, parameters, "p", &patternid);
989 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
992 /* No filename specified, can't read file, modified, or out of memory. */
993 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
996 cur_line = file->lines;
998 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
1000 if (cur_line->type == FILE_LINE_ACTION)
1002 section_start_line_number = line_number;
1004 cur_line = cur_line->next;
1007 if ( (cur_line == NULL)
1008 || (line_number != patternid)
1010 || (cur_line->type != FILE_LINE_URL))
1012 /* Invalid "patternid" parameter */
1013 edit_free_file(file);
1014 return JB_ERR_CGI_PARAMS;
1017 if (NULL == (exports = default_exports(csp, NULL)))
1019 edit_free_file(file);
1020 return JB_ERR_MEMORY;
1023 err = map(exports, "f", 1, stringify(file->identifier), 0);
1024 if (!err) err = map(exports, "v", 1, file->version_str, 1);
1025 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
1026 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
1027 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
1028 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
1030 edit_free_file(file);
1038 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
1042 /*********************************************************************
1044 * Function : edit_write_file
1046 * Description : Write a complete file to disk.
1049 * 1 : file = File to write.
1051 * Returns : JB_ERR_OK on success
1052 * JB_ERR_FILE on error writing to file.
1053 * JB_ERR_MEMORY on out of memory
1055 *********************************************************************/
1056 jb_err edit_write_file(struct editable_file * file)
1059 struct file_line * cur_line;
1060 struct stat statbuf[1];
1061 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
1062 digits in time_t, assuming this is a 64-bit
1063 machine, plus null terminator, plus one
1067 assert(file->filename);
1069 if (NULL == (fp = fopen(file->filename, "wb")))
1074 cur_line = file->lines;
1075 while (cur_line != NULL)
1079 if (fputs(cur_line->raw, fp) < 0)
1087 if (cur_line->prefix)
1089 if (fputs(cur_line->prefix, fp) < 0)
1095 if (cur_line->unprocessed)
1098 if (NULL != strchr(cur_line->unprocessed, '#'))
1100 /* Must quote '#' characters */
1107 /* Count number of # characters, so we know length of output string */
1108 src = cur_line->unprocessed;
1109 while (NULL != (src = strchr(src, '#')))
1114 assert(numhash > 0);
1116 /* Allocate new memory for string */
1117 len = strlen(cur_line->unprocessed) + (size_t)numhash;
1118 if (NULL == (str = malloc(len + 1)))
1120 /* Uh oh, just trashed file! */
1122 return JB_ERR_MEMORY;
1125 /* Copy string but quote hashes */
1126 src = cur_line->unprocessed;
1134 assert(numhash >= 0);
1140 assert(numhash == 0);
1141 assert(strlen(str) == len);
1142 assert(str == dest - len);
1143 assert(src - len <= cur_line->unprocessed);
1145 if ((strlen(str) != len) || (numhash != 0))
1148 * Escaping didn't work as expected, go spread the news.
1149 * Only reached in non-debugging builds.
1151 log_error(LOG_LEVEL_ERROR,
1152 "Looks like hash escaping failed. %s might be corrupted now.",
1156 if (fputs(str, fp) < 0)
1167 /* Can write without quoting '#' characters. */
1168 if (fputs(cur_line->unprocessed, fp) < 0)
1174 if (fputs(NEWLINE(file->newline), fp) < 0)
1182 /* FIXME: Write data from file->data->whatever */
1186 cur_line = cur_line->next;
1192 /* Update the version stamp in the file structure, since we just
1193 * wrote to the file & changed it's date.
1195 if (stat(file->filename, statbuf) < 0)
1197 /* Error, probably file not found. */
1200 file->version = (unsigned)statbuf->st_mtime;
1202 /* Correct file->version_str */
1203 freez(file->version_str);
1204 snprintf(version_buf, 22, "%u", file->version);
1205 version_buf[21] = '\0';
1206 file->version_str = strdup(version_buf);
1207 if (version_buf == NULL)
1209 return JB_ERR_MEMORY;
1216 /*********************************************************************
1218 * Function : edit_free_file
1220 * Description : Free a complete file in memory.
1223 * 1 : file = Data structure to free.
1227 *********************************************************************/
1228 void edit_free_file(struct editable_file * file)
1232 /* Silently ignore NULL pointer */
1236 edit_free_file_lines(file->lines);
1237 freez(file->version_str);
1239 file->parse_error_text = NULL; /* Statically allocated */
1240 file->parse_error = NULL;
1246 /*********************************************************************
1248 * Function : edit_free_file_lines
1250 * Description : Free an entire linked list of file lines.
1253 * 1 : first_line = Data structure to free.
1257 *********************************************************************/
1258 static void edit_free_file_lines(struct file_line * first_line)
1260 struct file_line * next_line;
1262 while (first_line != NULL)
1264 next_line = first_line->next;
1265 first_line->next = NULL;
1266 freez(first_line->raw);
1267 freez(first_line->prefix);
1268 freez(first_line->unprocessed);
1269 switch(first_line->type)
1271 case 0: /* special case if memory zeroed */
1272 case FILE_LINE_UNPROCESSED:
1273 case FILE_LINE_BLANK:
1274 case FILE_LINE_ALIAS_HEADER:
1275 case FILE_LINE_SETTINGS_HEADER:
1276 case FILE_LINE_DESCRIPTION_HEADER:
1277 case FILE_LINE_DESCRIPTION_ENTRY:
1278 case FILE_LINE_ALIAS_ENTRY:
1280 /* No data is stored for these */
1283 case FILE_LINE_ACTION:
1284 free_action(first_line->data.action);
1287 case FILE_LINE_SETTINGS_ENTRY:
1288 freez(first_line->data.setting.name);
1289 freez(first_line->data.setting.svalue);
1292 /* Should never happen */
1296 first_line->type = 0; /* paranoia */
1298 first_line = next_line;
1303 /*********************************************************************
1305 * Function : match_actions_file_header_line
1307 * Description : Match an actions file {{header}} line
1310 * 1 : line = String from file
1311 * 2 : name = Header to match against
1313 * Returns : 0 iff they match.
1315 *********************************************************************/
1316 static int match_actions_file_header_line(const char * line, const char * name)
1324 if ((line[0] != '{') || (line[1] != '{'))
1330 /* Look for optional whitespace */
1331 while ( (*line == ' ') || (*line == '\t') )
1336 /* Look for the specified name (case-insensitive) */
1338 if (0 != strncmpic(line, name, len))
1344 /* Look for optional whitespace */
1345 while ( (*line == ' ') || (*line == '\t') )
1350 /* Look for "}}" and end of string*/
1351 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1361 /*********************************************************************
1363 * Function : match_actions_file_header_line
1365 * Description : Match an actions file {{header}} line
1368 * 1 : line = String from file. Must not start with
1369 * whitespace (else infinite loop!)
1370 * 2 : pname = Destination for name
1371 * 2 : pvalue = Destination for value
1373 * Returns : JB_ERR_OK on success
1374 * JB_ERR_MEMORY on out-of-memory
1375 * JB_ERR_PARSE if there's no "=" sign, or if there's
1376 * nothing before the "=" sign (but empty
1377 * values *after* the "=" sign are legal).
1379 *********************************************************************/
1380 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1382 const char * name_end;
1383 const char * value_start;
1389 assert(*line != ' ');
1390 assert(*line != '\t');
1395 value_start = strchr(line, '=');
1396 if ((value_start == NULL) || (value_start == line))
1398 return JB_ERR_PARSE;
1401 name_end = value_start - 1;
1403 /* Eat any whitespace before the '=' */
1404 while ((*name_end == ' ') || (*name_end == '\t'))
1407 * we already know we must have at least 1 non-ws char
1408 * at start of buf - no need to check
1413 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1414 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1416 return JB_ERR_MEMORY;
1418 strncpy(*pname, line, name_len);
1419 (*pname)[name_len] = '\0';
1421 /* Eat any the whitespace after the '=' */
1423 while ((*value_start == ' ') || (*value_start == '\t'))
1428 if (NULL == (*pvalue = strdup(value_start)))
1432 return JB_ERR_MEMORY;
1439 /*********************************************************************
1441 * Function : edit_parse_actions_file
1443 * Description : Parse an actions file in memory.
1445 * Passed linked list must have the "data" member
1446 * zeroed, and must contain valid "next" and
1447 * "unprocessed" fields. The "raw" and "prefix"
1448 * fields are ignored, and "type" is just overwritten.
1450 * Note that on error the file may have been
1454 * 1 : file = Actions file to be parsed in-place.
1456 * Returns : JB_ERR_OK on success
1457 * JB_ERR_MEMORY on out-of-memory
1458 * JB_ERR_PARSE on error
1460 *********************************************************************/
1461 jb_err edit_parse_actions_file(struct editable_file * file)
1463 struct file_line * cur_line;
1465 const char * text; /* Text from a line */
1466 char * name; /* For lines of the form name=value */
1467 char * value; /* For lines of the form name=value */
1468 struct action_alias * alias_list = NULL;
1469 jb_err err = JB_ERR_OK;
1471 /* alias_list contains the aliases defined in this file.
1472 * It might be better to use the "file_line.data" fields
1473 * in the relavent places instead.
1476 cur_line = file->lines;
1478 /* A note about blank line support: Blank lines should only
1479 * ever occur as the last line in the file. This function
1480 * is more forgiving than that - FILE_LINE_BLANK can occur
1484 /* Skip leading blanks. Should only happen if file is
1485 * empty (which is valid, but pointless).
1487 while ( (cur_line != NULL)
1488 && (cur_line->unprocessed[0] == '\0') )
1491 cur_line->type = FILE_LINE_BLANK;
1492 cur_line = cur_line->next;
1495 if ( (cur_line != NULL)
1496 && (cur_line->unprocessed[0] != '{') )
1498 /* File doesn't start with a header */
1499 file->parse_error = cur_line;
1500 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1501 return JB_ERR_PARSE;
1504 if ( (cur_line != NULL) && (0 ==
1505 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1507 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1509 cur_line = cur_line->next;
1510 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1512 if (cur_line->unprocessed[0])
1514 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1516 err = split_line_on_equals(cur_line->unprocessed,
1517 &cur_line->data.setting.name,
1518 &cur_line->data.setting.svalue);
1519 if (err == JB_ERR_MEMORY)
1523 else if (err != JB_ERR_OK)
1525 /* Line does not contain a name=value pair */
1526 file->parse_error = cur_line;
1527 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1528 return JB_ERR_PARSE;
1533 cur_line->type = FILE_LINE_BLANK;
1535 cur_line = cur_line->next;
1539 if ( (cur_line != NULL) && (0 ==
1540 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1542 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1544 cur_line = cur_line->next;
1545 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1547 if (cur_line->unprocessed[0])
1549 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1553 cur_line->type = FILE_LINE_BLANK;
1555 cur_line = cur_line->next;
1559 if ( (cur_line != NULL) && (0 ==
1560 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1562 cur_line->type = FILE_LINE_ALIAS_HEADER;
1564 cur_line = cur_line->next;
1565 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1567 if (cur_line->unprocessed[0])
1569 /* define an alias */
1570 struct action_alias * new_alias;
1572 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1574 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1575 if (err == JB_ERR_MEMORY)
1579 else if (err != JB_ERR_OK)
1581 /* Line does not contain a name=value pair */
1582 file->parse_error = cur_line;
1583 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1584 return JB_ERR_PARSE;
1587 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1592 free_alias_list(alias_list);
1593 return JB_ERR_MEMORY;
1596 err = get_actions(value, alias_list, new_alias->action);
1599 /* Invalid action or out of memory */
1603 free_alias_list(alias_list);
1604 if (err == JB_ERR_MEMORY)
1610 /* Line does not contain a name=value pair */
1611 file->parse_error = cur_line;
1612 file->parse_error_text = "This alias does not specify a valid set of actions.";
1613 return JB_ERR_PARSE;
1619 new_alias->name = name;
1622 new_alias->next = alias_list;
1623 alias_list = new_alias;
1627 cur_line->type = FILE_LINE_BLANK;
1629 cur_line = cur_line->next;
1633 /* Header done, process the main part of the file */
1634 while (cur_line != NULL)
1636 /* At this point, (cur_line->unprocessed[0] == '{') */
1637 assert(cur_line->unprocessed[0] == '{');
1638 text = cur_line->unprocessed + 1;
1639 len = strlen(text) - 1;
1640 if (text[len] != '}')
1642 /* No closing } on header */
1643 free_alias_list(alias_list);
1644 file->parse_error = cur_line;
1645 file->parse_error_text = "Headers starting with '{' must have a "
1646 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1647 "must close with two brackets ('}}').";
1648 return JB_ERR_PARSE;
1653 /* An invalid {{ header. */
1654 free_alias_list(alias_list);
1655 file->parse_error = cur_line;
1656 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1657 "Please remember that the system (two-bracket) headers must "
1658 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1659 "and must appear before any actions (one-bracket) headers. "
1660 "Also note that system headers may not be repeated.";
1661 return JB_ERR_PARSE;
1664 while ( (*text == ' ') || (*text == '\t') )
1670 && ( (text[len - 1] == ' ')
1671 || (text[len - 1] == '\t') ) )
1676 cur_line->type = FILE_LINE_ACTION;
1678 /* Remove {} and make copy */
1679 if (NULL == (value = (char *) malloc(len + 1)))
1682 free_alias_list(alias_list);
1683 return JB_ERR_MEMORY;
1685 strncpy(value, text, len);
1689 err = get_actions(value, alias_list, cur_line->data.action);
1692 /* Invalid action or out of memory */
1694 free_alias_list(alias_list);
1695 if (err == JB_ERR_MEMORY)
1701 /* Line does not contain a name=value pair */
1702 file->parse_error = cur_line;
1703 file->parse_error_text = "This header does not specify a valid set of actions.";
1704 return JB_ERR_PARSE;
1708 /* Done with string - it was clobbered anyway */
1711 /* Process next line */
1712 cur_line = cur_line->next;
1714 /* Loop processing URL patterns */
1715 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1717 if (cur_line->unprocessed[0])
1719 /* Could parse URL here, but this isn't currently needed */
1721 cur_line->type = FILE_LINE_URL;
1725 cur_line->type = FILE_LINE_BLANK;
1727 cur_line = cur_line->next;
1729 } /* End main while(cur_line != NULL) loop */
1731 free_alias_list(alias_list);
1737 /*********************************************************************
1739 * Function : edit_read_file_lines
1741 * Description : Read all the lines of a file into memory.
1742 * Handles whitespace, comments and line continuation.
1745 * 1 : fp = File to read from. On return, this will be
1746 * at EOF but it will not have been closed.
1747 * 2 : pfile = Destination for a linked list of file_lines.
1748 * Will be set to NULL on error.
1749 * 3 : newline = How to handle newlines.
1751 * Returns : JB_ERR_OK on success
1752 * JB_ERR_MEMORY on out-of-memory
1754 *********************************************************************/
1755 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1757 struct file_line * first_line; /* Keep for return value or to free */
1758 struct file_line * cur_line; /* Current line */
1759 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1767 cur_line = first_line = zalloc(sizeof(struct file_line));
1768 if (cur_line == NULL)
1770 return JB_ERR_MEMORY;
1773 cur_line->type = FILE_LINE_UNPROCESSED;
1775 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1778 /* Out of memory or empty file. */
1779 /* Note that empty file is not an error we propogate up */
1781 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1786 prev_line = cur_line;
1787 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1788 if (cur_line == NULL)
1791 edit_free_file_lines(first_line);
1792 return JB_ERR_MEMORY;
1795 cur_line->type = FILE_LINE_UNPROCESSED;
1797 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1798 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1801 edit_free_file_lines(first_line);
1802 return JB_ERR_MEMORY;
1806 while (rval != JB_ERR_FILE);
1810 /* We allocated one too many - free it */
1811 prev_line->next = NULL;
1814 *pfile = first_line;
1819 /*********************************************************************
1821 * Function : edit_read_file
1823 * Description : Read a complete file into memory.
1824 * Handles CGI parameter parsing. If requested, also
1825 * checks the file's modification timestamp.
1828 * 1 : csp = Current client state (buffers, headers, etc...)
1829 * 2 : parameters = map of cgi parameters.
1830 * 3 : require_version = true to check "ver" parameter.
1831 * 4 : pfile = Destination for the file. Will be set
1835 * f : The action file identifier.
1836 * ver : (Only if require_version is nonzero)
1837 * Timestamp of the actions file. If wrong, this
1838 * function fails with JB_ERR_MODIFIED.
1840 * Returns : JB_ERR_OK on success
1841 * JB_ERR_MEMORY on out-of-memory
1842 * JB_ERR_CGI_PARAMS if "filename" was not specified
1844 * JB_ERR_FILE if the file cannot be opened or
1846 * JB_ERR_MODIFIED if version checking was requested and
1847 * failed - the file was modified outside
1848 * of this CGI editor instance.
1850 *********************************************************************/
1851 jb_err edit_read_file(struct client_state *csp,
1852 const struct map *parameters,
1853 int require_version,
1854 struct editable_file **pfile)
1856 struct file_line * lines;
1859 const char *filename = NULL;
1860 struct editable_file * file;
1861 unsigned version = 0;
1862 struct stat statbuf[1];
1863 char version_buf[22];
1864 int newline = NEWLINE_UNKNOWN;
1873 err = get_number_param(csp, parameters, "f", &i);
1874 if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1876 filename = csp->config->actions_file[i];
1878 else if (JB_ERR_CGI_PARAMS == err)
1881 * Probably an old-school URL like
1882 * http://config.privoxy.org/edit-actions-list?f=default
1884 err = get_file_name_param(csp, parameters, "f", &filename);
1887 if (NULL == filename || stat(filename, statbuf) < 0)
1889 /* Error, probably file not found. */
1892 version = (unsigned) statbuf->st_mtime;
1894 if (require_version)
1896 unsigned specified_version;
1897 err = get_number_param(csp, parameters, "v", &specified_version);
1903 if (version != specified_version)
1905 return JB_ERR_MODIFIED;
1909 if (NULL == (fp = fopen(filename,"rb")))
1914 err = edit_read_file_lines(fp, &lines, &newline);
1923 file = (struct editable_file *) zalloc(sizeof(*file));
1926 edit_free_file_lines(lines);
1930 file->lines = lines;
1931 file->newline = newline;
1932 file->filename = filename;
1933 file->version = version;
1934 file->identifier = i;
1936 /* Correct file->version_str */
1937 freez(file->version_str);
1938 snprintf(version_buf, 22, "%u", file->version);
1939 version_buf[21] = '\0';
1940 file->version_str = strdup(version_buf);
1941 if (version_buf == NULL)
1943 edit_free_file(file);
1944 return JB_ERR_MEMORY;
1952 /*********************************************************************
1954 * Function : edit_read_actions_file
1956 * Description : Read a complete actions file into memory.
1957 * Handles CGI parameter parsing. If requested, also
1958 * checks the file's modification timestamp.
1960 * If this function detects an error in the categories
1961 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1962 * then it handles it by filling in the specified
1963 * response structure and returning JB_ERR_FILE.
1966 * 1 : csp = Current client state (buffers, headers, etc...)
1967 * 2 : rsp = HTTP response. Only filled in on error.
1968 * 2 : parameters = map of cgi parameters.
1969 * 3 : require_version = true to check "ver" parameter.
1970 * 4 : pfile = Destination for the file. Will be set
1974 * f : The actions file identifier.
1975 * ver : (Only if require_version is nonzero)
1976 * Timestamp of the actions file. If wrong, this
1977 * function fails with JB_ERR_MODIFIED.
1979 * Returns : JB_ERR_OK on success
1980 * JB_ERR_MEMORY on out-of-memory
1981 * JB_ERR_CGI_PARAMS if "filename" was not specified
1983 * JB_ERR_FILE if the file does not contain valid data,
1984 * or if file cannot be opened or
1985 * contains no data, or if version
1986 * checking was requested and failed.
1988 *********************************************************************/
1989 jb_err edit_read_actions_file(struct client_state *csp,
1990 struct http_response *rsp,
1991 const struct map *parameters,
1992 int require_version,
1993 struct editable_file **pfile)
1996 struct editable_file *file;
2004 err = edit_read_file(csp, parameters, require_version, &file);
2007 /* Try to handle if possible */
2008 if (err == JB_ERR_FILE)
2010 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
2012 else if (err == JB_ERR_MODIFIED)
2014 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
2016 if (err == JB_ERR_OK)
2019 * Signal to higher-level CGI code that there was a problem but we
2020 * handled it, they should just return JB_ERR_OK.
2027 err = edit_parse_actions_file(file);
2030 if (err == JB_ERR_PARSE)
2032 err = cgi_error_parse(csp, rsp, file);
2033 if (err == JB_ERR_OK)
2036 * Signal to higher-level CGI code that there was a problem but we
2037 * handled it, they should just return JB_ERR_OK.
2042 edit_free_file(file);
2051 /*********************************************************************
2053 * Function : get_file_name_param
2055 * Description : Get the name of the file to edit from the parameters
2056 * passed to a CGI function using the old syntax.
2057 * This function handles security checks and only
2058 * accepts files that Privoxy already knows.
2061 * 1 : csp = Current client state (buffers, headers, etc...)
2062 * 2 : parameters = map of cgi parameters
2063 * 3 : param_name = The name of the parameter to read
2064 * 4 : pfilename = pointer to the filename in
2065 * csp->config->actions_file[] if found. Set to NULL on error.
2067 * Returns : JB_ERR_OK on success
2068 * JB_ERR_MEMORY on out-of-memory
2069 * JB_ERR_CGI_PARAMS if "filename" was not specified
2072 *********************************************************************/
2073 static jb_err get_file_name_param(struct client_state *csp,
2074 const struct map *parameters,
2075 const char *param_name,
2076 const char **pfilename)
2079 const char suffix[] = ".action";
2094 param = lookup(parameters, param_name);
2097 return JB_ERR_CGI_PARAMS;
2100 len = strlen(param);
2101 if (len >= FILENAME_MAX)
2104 return JB_ERR_CGI_PARAMS;
2108 * Check every character to see if it's legal.
2109 * Totally unnecessary but we do it anyway.
2112 while ((ch = *s++) != '\0')
2114 if ( ((ch < 'A') || (ch > 'Z'))
2115 && ((ch < 'a') || (ch > 'z'))
2116 && ((ch < '0') || (ch > '9'))
2120 /* Probable hack attempt. */
2121 return JB_ERR_CGI_PARAMS;
2125 /* Append extension */
2126 name_size = len + strlen(suffix) + 1;
2127 name = malloc(name_size);
2130 return JB_ERR_MEMORY;
2132 strlcpy(name, param, name_size);
2133 strlcat(name, suffix, name_size);
2136 fullpath = make_path(csp->config->confdir, name);
2139 if (fullpath == NULL)
2141 return JB_ERR_MEMORY;
2144 /* Check if the file is known */
2145 for (i = 0; i < MAX_AF_FILES; i++)
2147 if (NULL != csp->config->actions_file[i] &&
2148 !strcmp(fullpath, csp->config->actions_file[i]))
2151 *pfilename = csp->config->actions_file[i];
2159 return JB_ERR_CGI_PARAMS;
2163 /*********************************************************************
2165 * Function : get_url_spec_param
2167 * Description : Get a URL pattern from the parameters
2168 * passed to a CGI function. Removes leading/trailing
2169 * spaces and validates it.
2172 * 1 : csp = Current client state (buffers, headers, etc...)
2173 * 2 : parameters = map of cgi parameters
2174 * 3 : name = Name of CGI parameter to read
2175 * 4 : pvalue = destination for value. Will be malloc()'d.
2176 * Set to NULL on error.
2178 * Returns : JB_ERR_OK on success
2179 * JB_ERR_MEMORY on out-of-memory
2180 * JB_ERR_CGI_PARAMS if the parameter was not specified
2183 *********************************************************************/
2184 static jb_err get_url_spec_param(struct client_state *csp,
2185 const struct map *parameters,
2189 const char *orig_param;
2192 struct url_spec compiled[1];
2202 orig_param = lookup(parameters, name);
2205 return JB_ERR_CGI_PARAMS;
2208 /* Copy and trim whitespace */
2209 param = strdup(orig_param);
2212 return JB_ERR_MEMORY;
2216 /* Must be non-empty, and can't allow 1st character to be '{' */
2217 if (param[0] == '\0' || param[0] == '{')
2220 return JB_ERR_CGI_PARAMS;
2223 /* Check for embedded newlines */
2224 for (s = param; *s != '\0'; s++)
2226 if ((*s == '\r') || (*s == '\n'))
2229 return JB_ERR_CGI_PARAMS;
2233 /* Check that regex is valid */
2238 return JB_ERR_MEMORY;
2240 err = create_url_spec(compiled, s);
2245 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2247 free_url_spec(compiled);
2249 if (param[strlen(param) - 1] == '\\')
2252 * Must protect trailing '\\' from becoming line continuation character.
2253 * Two methods: 1) If it's a domain only, add a trailing '/'.
2254 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2256 if (strchr(param, '/') == NULL)
2258 err = string_append(¶m, "/");
2262 err = string_append(¶m, "(?:)");
2269 /* Check that the modified regex is valid */
2274 return JB_ERR_MEMORY;
2276 err = create_url_spec(compiled, s);
2281 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2283 free_url_spec(compiled);
2290 /*********************************************************************
2292 * Function : map_radio
2294 * Description : Map a set of radio button values. E.g. if you have
2295 * 3 radio buttons, declare them as:
2296 * <option type="radio" name="xyz" @xyz-a@>
2297 * <option type="radio" name="xyz" @xyz-b@>
2298 * <option type="radio" name="xyz" @xyz-c@>
2299 * Then map one of the @xyz-?@ variables to "checked"
2300 * and all the others to empty by calling:
2301 * map_radio(exports, "xyz", "abc", sel)
2302 * Where 'sel' is 'a', 'b', or 'c'.
2305 * 1 : exports = Exports map to modify.
2306 * 2 : optionname = name for map
2307 * 3 : values = null-terminated list of values;
2308 * 4 : value = Selected value.
2310 * CGI Parameters : None
2312 * Returns : JB_ERR_OK on success
2313 * JB_ERR_MEMORY on out-of-memory
2315 *********************************************************************/
2316 static jb_err map_radio(struct map * exports,
2317 const char * optionname,
2318 const char * values,
2324 const size_t len = strlen(optionname);
2325 const size_t buf_size = len + 3;
2331 buf = malloc(buf_size);
2334 return JB_ERR_MEMORY;
2337 strlcpy(buf, optionname, buf_size);
2339 /* XXX: this looks ... interesting */
2344 while ((c = *values++) != '\0')
2349 if (map(exports, buf, 1, "", 1))
2351 return JB_ERR_MEMORY;
2357 return map(exports, buf, 0, "checked", 1);
2361 /*********************************************************************
2363 * Function : cgi_error_modified
2365 * Description : CGI function that is called when a file is modified
2366 * outside the CGI editor.
2369 * 1 : csp = Current client state (buffers, headers, etc...)
2370 * 2 : rsp = http_response data structure for output
2371 * 3 : filename = The file that was modified.
2373 * CGI Parameters : none
2375 * Returns : JB_ERR_OK on success
2376 * JB_ERR_MEMORY on out-of-memory error.
2378 *********************************************************************/
2379 jb_err cgi_error_modified(struct client_state *csp,
2380 struct http_response *rsp,
2381 const char *filename)
2383 struct map *exports;
2390 if (NULL == (exports = default_exports(csp, NULL)))
2392 return JB_ERR_MEMORY;
2395 err = map(exports, "f", 1, html_encode(filename), 0);
2402 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2406 /*********************************************************************
2408 * Function : cgi_error_parse
2410 * Description : CGI function that is called when a file cannot
2411 * be parsed by the CGI editor.
2414 * 1 : csp = Current client state (buffers, headers, etc...)
2415 * 2 : rsp = http_response data structure for output
2416 * 3 : file = The file that was modified.
2418 * CGI Parameters : none
2420 * Returns : JB_ERR_OK on success
2421 * JB_ERR_MEMORY on out-of-memory error.
2423 *********************************************************************/
2424 jb_err cgi_error_parse(struct client_state *csp,
2425 struct http_response *rsp,
2426 struct editable_file *file)
2428 struct map *exports;
2430 struct file_line *cur_line;
2436 if (NULL == (exports = default_exports(csp, NULL)))
2438 return JB_ERR_MEMORY;
2441 err = map(exports, "f", 1, stringify(file->identifier), 0);
2442 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2444 cur_line = file->parse_error;
2447 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2448 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2456 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2460 /*********************************************************************
2462 * Function : cgi_error_file
2464 * Description : CGI function that is called when a file cannot be
2465 * opened by the CGI editor.
2468 * 1 : csp = Current client state (buffers, headers, etc...)
2469 * 2 : rsp = http_response data structure for output
2470 * 3 : filename = The file that was modified.
2472 * CGI Parameters : none
2474 * Returns : JB_ERR_OK on success
2475 * JB_ERR_MEMORY on out-of-memory error.
2477 *********************************************************************/
2478 jb_err cgi_error_file(struct client_state *csp,
2479 struct http_response *rsp,
2480 const char *filename)
2482 struct map *exports;
2489 if (NULL == (exports = default_exports(csp, NULL)))
2491 return JB_ERR_MEMORY;
2494 err = map(exports, "f", 1, html_encode(filename), 0);
2501 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2505 /*********************************************************************
2507 * Function : cgi_error_file_read_only
2509 * Description : CGI function that is called when a file cannot be
2510 * opened for writing by the CGI editor.
2513 * 1 : csp = Current client state (buffers, headers, etc...)
2514 * 2 : rsp = http_response data structure for output
2515 * 3 : filename = The file that we can't write to
2517 * CGI Parameters : none
2519 * Returns : JB_ERR_OK on success
2520 * JB_ERR_MEMORY on out-of-memory error.
2522 *********************************************************************/
2523 jb_err cgi_error_file_read_only(struct client_state *csp,
2524 struct http_response *rsp,
2525 const char *filename)
2527 struct map *exports;
2534 if (NULL == (exports = default_exports(csp, NULL)))
2536 return JB_ERR_MEMORY;
2539 err = map(exports, "f", 1, html_encode(filename), 0);
2546 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2550 /*********************************************************************
2552 * Function : cgi_edit_actions
2554 * Description : CGI function that allows the user to choose which
2555 * actions file to edit.
2558 * 1 : csp = Current client state (buffers, headers, etc...)
2559 * 2 : rsp = http_response data structure for output
2560 * 3 : parameters = map of cgi parameters
2562 * CGI Parameters : None
2564 * Returns : JB_ERR_OK on success
2565 * JB_ERR_MEMORY on out-of-memory error
2567 *********************************************************************/
2568 jb_err cgi_edit_actions(struct client_state *csp,
2569 struct http_response *rsp,
2570 const struct map *parameters)
2573 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2575 return cgi_error_disabled(csp, rsp);
2578 /* FIXME: Incomplete */
2580 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2585 /*********************************************************************
2587 * Function : cgi_edit_actions_list
2589 * Description : CGI function that edits the actions list.
2590 * FIXME: This function shouldn't FATAL ever.
2591 * FIXME: This function doesn't check the retval of map()
2593 * 1 : csp = Current client state (buffers, headers, etc...)
2594 * 2 : rsp = http_response data structure for output
2595 * 3 : parameters = map of cgi parameters
2597 * CGI Parameters : filename
2599 * Returns : JB_ERR_OK on success
2600 * JB_ERR_MEMORY on out-of-memory
2601 * JB_ERR_FILE if the file cannot be opened or
2603 * JB_ERR_CGI_PARAMS if "filename" was not specified
2606 *********************************************************************/
2607 jb_err cgi_edit_actions_list(struct client_state *csp,
2608 struct http_response *rsp,
2609 const struct map *parameters)
2611 char * section_template;
2612 char * url_template;
2617 struct map * exports;
2618 struct map * section_exports;
2619 struct map * url_exports;
2620 struct editable_file * file;
2621 struct file_line * cur_line;
2622 unsigned line_number = 0;
2623 unsigned prev_section_line_number = ((unsigned) (-1));
2625 struct file_list * fl;
2626 struct url_actions * b;
2627 char * buttons = NULL;
2630 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2632 return cgi_error_disabled(csp, rsp);
2635 if (NULL == (exports = default_exports(csp, NULL)))
2637 return JB_ERR_MEMORY;
2640 /* Load actions file */
2641 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2644 /* No filename specified, can't read file, or out of memory. */
2645 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2648 /* Find start of actions in file */
2649 cur_line = file->lines;
2651 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2653 cur_line = cur_line->next;
2658 * Conventional actions files should have a match all block
2660 * cur_line = {...global actions...}
2661 * cur_line->next = /
2662 * cur_line->next->next = {...actions...} or EOF
2664 if ( (cur_line != NULL)
2665 && (cur_line->type == FILE_LINE_ACTION)
2666 && (cur_line->next != NULL)
2667 && (cur_line->next->type == FILE_LINE_URL)
2668 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2669 && ( (cur_line->next->next == NULL)
2670 || (cur_line->next->next->type != FILE_LINE_URL)
2674 * Generate string with buttons to set actions for "/" to
2675 * any predefined set of actions (named standard.*, probably
2676 * residing in standard.action).
2679 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2682 edit_free_file(file);
2684 if (err == JB_ERR_FILE)
2686 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2691 err = template_fill(§ion_template, exports);
2694 edit_free_file(file);
2699 buttons = strdup("");
2700 for (i = 0; i < MAX_AF_FILES; i++)
2702 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2704 for (b = b->next; NULL != b; b = b->next)
2706 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2708 if (err || (NULL == (section_exports = new_map())))
2711 free(section_template);
2712 edit_free_file(file);
2714 return JB_ERR_MEMORY;
2717 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2719 if (err || (NULL == (s = strdup(section_template))))
2721 free_map(section_exports);
2723 free(section_template);
2724 edit_free_file(file);
2726 return JB_ERR_MEMORY;
2729 if (!err) err = template_fill(&s, section_exports);
2730 free_map(section_exports);
2731 if (!err) err = string_join(&buttons, s);
2736 freez(section_template);
2737 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2740 * Conventional actions file, supply extra editing help.
2741 * (e.g. don't allow them to make it an unconventional one).
2743 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2745 snprintf(buf, 150, "%d", line_number);
2746 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2747 snprintf(buf, 150, "%d", line_number + 2);
2748 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2749 if (!err) err = map(exports, "all-urls-actions", 1,
2750 actions_to_html(csp, cur_line->data.action), 0);
2752 /* Skip the 2 lines */
2753 cur_line = cur_line->next->next;
2757 * Note that prev_section_line_number is NOT set here.
2758 * This is deliberate and not a bug. It stops a "Move up"
2759 * option appearing on the next section. Clicking "Move
2760 * up" would make the actions file unconventional, which
2761 * we don't want, so we hide this option.
2767 * Non-standard actions file - does not begin with
2768 * the "All URLs" section.
2770 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2773 /* Set up global exports */
2775 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2776 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2777 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2779 /* Discourage private additions to default.action */
2781 if (!err) err = map_conditional(exports, "default-action",
2782 (strstr("default.action", file->filename) != NULL));
2785 edit_free_file(file);
2790 /* Should do all global exports above this point */
2792 /* Load templates */
2794 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2797 edit_free_file(file);
2799 if (err == JB_ERR_FILE)
2801 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2806 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2809 free(section_template);
2810 edit_free_file(file);
2812 if (err == JB_ERR_FILE)
2814 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2819 err = template_fill(§ion_template, exports);
2823 edit_free_file(file);
2829 err = template_fill(&url_template, exports);
2832 free(section_template);
2833 edit_free_file(file);
2838 if (NULL == (sections = strdup("")))
2840 free(section_template);
2842 edit_free_file(file);
2844 return JB_ERR_MEMORY;
2847 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2849 if (NULL == (section_exports = new_map()))
2852 free(section_template);
2854 edit_free_file(file);
2856 return JB_ERR_MEMORY;
2859 snprintf(buf, 150, "%d", line_number);
2860 err = map(section_exports, "s", 1, buf, 1);
2861 if (!err) err = map(section_exports, "actions", 1,
2862 actions_to_html(csp, cur_line->data.action), 0);
2865 && (cur_line->next != NULL)
2866 && (cur_line->next->type == FILE_LINE_URL))
2868 /* This section contains at least one URL, don't allow delete */
2869 err = map_block_killer(section_exports, "empty-section");
2873 if (!err) err = map_block_keep(section_exports, "empty-section");
2876 if (prev_section_line_number != ((unsigned)(-1)))
2878 /* Not last section */
2879 snprintf(buf, 150, "%d", prev_section_line_number);
2880 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2881 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2886 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2888 prev_section_line_number = line_number;
2893 free(section_template);
2895 edit_free_file(file);
2897 free_map(section_exports);
2901 /* Should do all section-specific exports above this point */
2903 if (NULL == (urls = strdup("")))
2906 free(section_template);
2908 edit_free_file(file);
2910 free_map(section_exports);
2911 return JB_ERR_MEMORY;
2916 cur_line = cur_line->next;
2919 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2921 if (NULL == (url_exports = new_map()))
2925 free(section_template);
2927 edit_free_file(file);
2929 free_map(section_exports);
2930 return JB_ERR_MEMORY;
2933 snprintf(buf, 150, "%d", line_number);
2934 err = map(url_exports, "p", 1, buf, 1);
2936 snprintf(buf, 150, "%d", url_1_2);
2937 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2939 if (!err) err = map(url_exports, "url-html", 1,
2940 html_encode(cur_line->unprocessed), 0);
2941 if (!err) err = map(url_exports, "url", 1,
2942 url_encode(cur_line->unprocessed), 0);
2948 free(section_template);
2950 edit_free_file(file);
2952 free_map(section_exports);
2953 free_map(url_exports);
2957 if (NULL == (s = strdup(url_template)))
2961 free(section_template);
2963 edit_free_file(file);
2965 free_map(section_exports);
2966 free_map(url_exports);
2967 return JB_ERR_MEMORY;
2970 err = template_fill(&s, section_exports);
2971 if (!err) err = template_fill(&s, url_exports);
2972 if (!err) err = string_append(&urls, s);
2974 free_map(url_exports);
2981 free(section_template);
2983 edit_free_file(file);
2985 free_map(section_exports);
2989 url_1_2 = 3 - url_1_2;
2991 cur_line = cur_line->next;
2995 err = map(section_exports, "urls", 1, urls, 0);
2997 /* Could also do section-specific exports here, but it wouldn't be as fast */
2999 snprintf(buf, 150, "%d", line_number);
3000 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
3002 if ( (cur_line != NULL)
3003 && (cur_line->type == FILE_LINE_ACTION))
3005 /* Not last section */
3006 if (!err) err = map_block_keep(section_exports, "s-next-exists");
3011 if (!err) err = map_block_killer(section_exports, "s-next-exists");
3017 free(section_template);
3019 edit_free_file(file);
3021 free_map(section_exports);
3025 if (NULL == (s = strdup(section_template)))
3028 free(section_template);
3030 edit_free_file(file);
3032 free_map(section_exports);
3033 return JB_ERR_MEMORY;
3036 err = template_fill(&s, section_exports);
3037 if (!err) err = string_append(§ions, s);
3040 free_map(section_exports);
3045 free(section_template);
3047 edit_free_file(file);
3053 edit_free_file(file);
3054 free(section_template);
3057 err = map(exports, "sections", 1, sections, 0);
3064 /* Could also do global exports here, but it wouldn't be as fast */
3066 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
3070 /*********************************************************************
3072 * Function : cgi_edit_actions_for_url
3074 * Description : CGI function that edits the Actions list.
3077 * 1 : csp = Current client state (buffers, headers, etc...)
3078 * 2 : rsp = http_response data structure for output
3079 * 3 : parameters = map of cgi parameters
3081 * CGI Parameters : None
3083 * Returns : JB_ERR_OK on success
3084 * JB_ERR_MEMORY on out-of-memory
3085 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3086 * specified or not valid.
3088 *********************************************************************/
3089 jb_err cgi_edit_actions_for_url(struct client_state *csp,
3090 struct http_response *rsp,
3091 const struct map *parameters)
3093 struct map * exports;
3095 struct editable_file * file;
3096 struct file_line * cur_line;
3097 unsigned line_number;
3099 struct re_filterfile_spec *filter_group;
3100 int i, have_filters = 0;
3102 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3104 return cgi_error_disabled(csp, rsp);
3107 err = get_number_param(csp, parameters, "s", §ionid);
3113 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3116 /* No filename specified, can't read file, modified, or out of memory. */
3117 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3120 cur_line = file->lines;
3122 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3124 cur_line = cur_line->next;
3127 if ( (cur_line == NULL)
3128 || (line_number != sectionid)
3130 || (cur_line->type != FILE_LINE_ACTION))
3132 /* Invalid "sectionid" parameter */
3133 edit_free_file(file);
3134 return JB_ERR_CGI_PARAMS;
3137 if (NULL == (exports = default_exports(csp, NULL)))
3139 edit_free_file(file);
3140 return JB_ERR_MEMORY;
3143 err = map(exports, "f", 1, stringify(file->identifier), 0);
3144 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3145 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3147 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3150 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3151 * length limitation and ignore clicks on the Submit buttons if
3152 * the resulting GET URL would be longer than their limit.
3154 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3155 * reached this limit and action editing stopped working in these
3156 * browsers (BR #1570678).
3158 * The config option split-large-forms works around this browser
3159 * bug (HTTP has no URL lenght limitation) by deviding the action
3160 * list form into multiple smaller ones. It means the URLs are shorter
3161 * and work in broken browsers as well, but the user can no longer change
3162 * all actions with one submit.
3164 * A better solution would be to switch to POST requests,
3165 * but this will do for now.
3167 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3169 /* Generate multiple smaller form by killing the big one. */
3170 err = map_block_killer(exports, "one-form-only");
3174 /* Default: Generate one large form by killing the smaller ones. */
3175 err = map_block_killer(exports, "multiple-forms");
3178 for (i = 0; i < MAX_AF_FILES; i++)
3180 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3182 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3190 edit_free_file(file);
3195 if (0 == have_filters)
3197 err = map(exports, "filter-params", 1, "", 1);
3202 * List available filters and their settings.
3204 char *filter_template;
3205 int filter_identifier = 0;
3206 char *prepared_templates[MAX_FILTER_TYPES];
3208 for (i = 0; i < MAX_FILTER_TYPES; i++)
3210 prepared_templates[i] = strdup("");
3213 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3216 edit_free_file(file);
3218 if (err == JB_ERR_FILE)
3220 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3225 err = template_fill(&filter_template, exports);
3227 for (i = 0; i < MAX_AF_FILES; i++)
3229 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3231 filter_group = csp->rlist[i]->f;
3232 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3234 char current_mode = 'x';
3236 struct list_entry *filter_name;
3237 struct map *line_exports;
3238 const int type = filter_group->type;
3239 const int multi_action_index = filter_type_info[type].multi_action_index;
3241 assert(type < MAX_FILTER_TYPES);
3243 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
3244 while ((filter_name != NULL)
3245 && (0 != strcmp(filter_group->name, filter_name->str)))
3247 filter_name = filter_name->next;
3250 if (filter_name != NULL)
3256 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
3257 while ((filter_name != NULL)
3258 && (0 != strcmp(filter_group->name, filter_name->str)))
3260 filter_name = filter_name->next;
3262 if (filter_name != NULL)
3268 /* Generate a unique serial number */
3269 snprintf(number, sizeof(number), "%x", filter_identifier++);
3270 number[sizeof(number) - 1] = '\0';
3272 line_exports = new_map();
3273 if (line_exports == NULL)
3275 err = JB_ERR_MEMORY;
3281 if (!err) err = map(line_exports, "index", 1, number, 1);
3282 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3283 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3284 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3285 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
3286 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
3287 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
3291 filter_line = strdup(filter_template);
3292 if (filter_line == NULL) err = JB_ERR_MEMORY;
3294 if (!err) err = template_fill(&filter_line, line_exports);
3295 string_join(&prepared_templates[type], filter_line);
3297 free_map(line_exports);
3302 freez(filter_template);
3304 /* Replace all filter macros with the aggregated templates */
3305 for (i = 0; i < MAX_FILTER_TYPES; i++)
3308 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
3313 /* Free aggregated templates */
3314 for (i = 0; i < MAX_FILTER_TYPES; i++)
3316 freez(prepared_templates[i]);
3321 if (!err) err = map_radio(exports, "filter-all", "nx",
3322 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3324 edit_free_file(file);
3332 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3336 /*********************************************************************
3338 * Function : cgi_edit_actions_submit
3340 * Description : CGI function that actually edits the Actions list.
3343 * 1 : csp = Current client state (buffers, headers, etc...)
3344 * 2 : rsp = http_response data structure for output
3345 * 3 : parameters = map of cgi parameters
3347 * CGI Parameters : None
3349 * Returns : JB_ERR_OK on success
3350 * JB_ERR_MEMORY on out-of-memory
3351 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3352 * specified or not valid.
3354 *********************************************************************/
3355 jb_err cgi_edit_actions_submit(struct client_state *csp,
3356 struct http_response *rsp,
3357 const struct map *parameters)
3362 size_t newtext_size;
3364 struct editable_file * file;
3365 struct file_line * cur_line;
3366 unsigned line_number;
3369 int filter_identifier;
3370 const char * action_set_name;
3372 struct file_list * fl;
3373 struct url_actions * b;
3375 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3377 return cgi_error_disabled(csp, rsp);
3380 err = get_number_param(csp, parameters, "s", §ionid);
3386 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3389 /* No filename specified, can't read file, modified, or out of memory. */
3390 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3393 cur_line = file->lines;
3395 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3397 cur_line = cur_line->next;
3400 if ( (cur_line == NULL)
3401 || (line_number != sectionid)
3403 || (cur_line->type != FILE_LINE_ACTION))
3405 /* Invalid "sectionid" parameter */
3406 edit_free_file(file);
3407 return JB_ERR_CGI_PARAMS;
3410 get_string_param(parameters, "p", &action_set_name);
3411 if (action_set_name != NULL)
3413 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3415 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3417 for (b = b->next; NULL != b; b = b->next)
3419 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3421 copy_action(cur_line->data.action, b->action);
3427 edit_free_file(file);
3428 return JB_ERR_CGI_PARAMS;
3434 err = actions_from_radio(parameters, cur_line->data.action);
3440 edit_free_file(file);
3444 ch = get_char_param(parameters, "filter_all");
3447 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3448 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3449 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3453 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3456 for (filter_identifier = 0; !err; filter_identifier++)
3463 * Filter state. Valid states are: 'Y' (active),
3464 * 'N' (inactive) and 'X' (no change).
3468 * Abbreviated filter type. Valid types are: 'F' (content filter),
3469 * 'S' (server-header filter) and 'C' (client-header filter).
3471 int multi_action_index = 0;
3473 /* Generate the keys */
3474 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3475 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3476 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3477 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3478 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3480 err = get_string_param(parameters, key_name, &name);
3489 type = get_char_param(parameters, key_type);
3493 multi_action_index = ACTION_MULTI_FILTER;
3496 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3499 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3502 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3505 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3508 log_error(LOG_LEVEL_ERROR,
3509 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3512 assert(multi_action_index);
3514 value = get_char_param(parameters, key_value);
3517 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3518 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3519 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3521 else if (value == 'N')
3523 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3524 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3526 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3527 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3530 else if (value == 'X')
3532 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3533 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3540 edit_free_file(file);
3544 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3547 edit_free_file(file);
3548 return JB_ERR_MEMORY;
3551 len = strlen(actiontext);
3555 * Empty action - must special-case this.
3556 * Simply setting len to 1 is sufficient...
3561 newtext_size = len + 2;
3562 if (NULL == (newtext = malloc(newtext_size)))
3566 edit_free_file(file);
3567 return JB_ERR_MEMORY;
3569 strlcpy(newtext, actiontext, newtext_size);
3573 newtext[len + 1] = '\0';
3575 freez(cur_line->raw);
3576 freez(cur_line->unprocessed);
3577 cur_line->unprocessed = newtext;
3579 err = edit_write_file(file);
3582 /* Error writing file */
3583 if (err == JB_ERR_FILE)
3585 /* Read-only file. */
3586 err = cgi_error_file_read_only(csp, rsp, file->filename);
3588 edit_free_file(file);
3592 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3593 (long) time(NULL), file->identifier, sectionid);
3595 edit_free_file(file);
3597 return cgi_redirect(rsp, target);
3601 /*********************************************************************
3603 * Function : cgi_edit_actions_url
3605 * Description : CGI function that actually edits a URL pattern in
3609 * 1 : csp = Current client state (buffers, headers, etc...)
3610 * 2 : rsp = http_response data structure for output
3611 * 3 : parameters = map of cgi parameters
3614 * filename : Identifies the file to edit
3615 * ver : File's last-modified time
3616 * section : Line number of section to edit
3617 * pattern : Line number of pattern to edit
3618 * newval : New value for pattern
3620 * Returns : JB_ERR_OK on success
3621 * JB_ERR_MEMORY on out-of-memory
3622 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3623 * specified or not valid.
3625 *********************************************************************/
3626 jb_err cgi_edit_actions_url(struct client_state *csp,
3627 struct http_response *rsp,
3628 const struct map *parameters)
3632 struct editable_file * file;
3633 struct file_line * cur_line;
3634 unsigned line_number;
3635 unsigned section_start_line_number = 0;
3643 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3645 return cgi_error_disabled(csp, rsp);
3648 err = get_number_param(csp, parameters, "p", &patternid);
3655 return JB_ERR_CGI_PARAMS;
3658 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3664 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3667 /* No filename specified, can't read file, modified, or out of memory. */
3669 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3673 cur_line = file->lines;
3675 while ((cur_line != NULL) && (line_number < patternid))
3677 if (cur_line->type == FILE_LINE_ACTION)
3679 section_start_line_number = line_number;
3681 cur_line = cur_line->next;
3685 if ( (cur_line == NULL)
3686 || (cur_line->type != FILE_LINE_URL))
3688 /* Invalid "patternid" parameter */
3690 edit_free_file(file);
3691 return JB_ERR_CGI_PARAMS;
3694 /* At this point, the line to edit is in cur_line */
3696 freez(cur_line->raw);
3697 freez(cur_line->unprocessed);
3698 cur_line->unprocessed = new_pattern;
3700 err = edit_write_file(file);
3703 /* Error writing file */
3704 if (err == JB_ERR_FILE)
3706 /* Read-only file. */
3707 err = cgi_error_file_read_only(csp, rsp, file->filename);
3709 edit_free_file(file);
3713 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3714 (long) time(NULL), file->identifier, section_start_line_number);
3716 edit_free_file(file);
3718 return cgi_redirect(rsp, target);
3722 /*********************************************************************
3724 * Function : cgi_edit_actions_add_url
3726 * Description : CGI function that actually adds a URL pattern to
3730 * 1 : csp = Current client state (buffers, headers, etc...)
3731 * 2 : rsp = http_response data structure for output
3732 * 3 : parameters = map of cgi parameters
3735 * filename : Identifies the file to edit
3736 * ver : File's last-modified time
3737 * section : Line number of section to edit
3738 * newval : New pattern
3740 * Returns : JB_ERR_OK on success
3741 * JB_ERR_MEMORY on out-of-memory
3742 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3743 * specified or not valid.
3745 *********************************************************************/
3746 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3747 struct http_response *rsp,
3748 const struct map *parameters)
3752 struct file_line * new_line;
3753 struct editable_file * file;
3754 struct file_line * cur_line;
3755 unsigned line_number;
3759 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3761 return cgi_error_disabled(csp, rsp);
3764 err = get_number_param(csp, parameters, "s", §ionid);
3771 return JB_ERR_CGI_PARAMS;
3774 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3780 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3783 /* No filename specified, can't read file, modified, or out of memory. */
3785 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3789 cur_line = file->lines;
3791 while ((cur_line != NULL) && (line_number < sectionid))
3793 cur_line = cur_line->next;
3797 if ( (cur_line == NULL)
3798 || (cur_line->type != FILE_LINE_ACTION))
3800 /* Invalid "sectionid" parameter */
3802 edit_free_file(file);
3803 return JB_ERR_CGI_PARAMS;
3806 /* At this point, the section header is in cur_line - add after this. */
3808 /* Allocate the new line */
3809 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3810 if (new_line == NULL)
3813 edit_free_file(file);
3814 return JB_ERR_MEMORY;
3817 /* Fill in the data members of the new line */
3818 new_line->raw = NULL;
3819 new_line->prefix = NULL;
3820 new_line->unprocessed = new_pattern;
3821 new_line->type = FILE_LINE_URL;
3823 /* Link new_line into the list, after cur_line */
3824 new_line->next = cur_line->next;
3825 cur_line->next = new_line;
3827 /* Done making changes, now commit */
3829 err = edit_write_file(file);
3832 /* Error writing file */
3833 if (err == JB_ERR_FILE)
3835 /* Read-only file. */
3836 err = cgi_error_file_read_only(csp, rsp, file->filename);
3838 edit_free_file(file);
3842 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3843 (long) time(NULL), file->identifier, sectionid);
3845 edit_free_file(file);
3847 return cgi_redirect(rsp, target);
3851 /*********************************************************************
3853 * Function : cgi_edit_actions_remove_url
3855 * Description : CGI function that actually removes a URL pattern from
3859 * 1 : csp = Current client state (buffers, headers, etc...)
3860 * 2 : rsp = http_response data structure for output
3861 * 3 : parameters = map of cgi parameters
3864 * f : (filename) Identifies the file to edit
3865 * v : (version) File's last-modified time
3866 * p : (pattern) Line number of pattern to remove
3868 * Returns : JB_ERR_OK on success
3869 * JB_ERR_MEMORY on out-of-memory
3870 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3871 * specified or not valid.
3873 *********************************************************************/
3874 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3875 struct http_response *rsp,
3876 const struct map *parameters)
3879 struct editable_file * file;
3880 struct file_line * cur_line;
3881 struct file_line * prev_line;
3882 unsigned line_number;
3883 unsigned section_start_line_number = 0;
3887 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3889 return cgi_error_disabled(csp, rsp);
3892 err = get_number_param(csp, parameters, "p", &patternid);
3898 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3901 /* No filename specified, can't read file, modified, or out of memory. */
3902 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3907 cur_line = file->lines;
3909 while ((cur_line != NULL) && (line_number < patternid))
3911 if (cur_line->type == FILE_LINE_ACTION)
3913 section_start_line_number = line_number;
3915 prev_line = cur_line;
3916 cur_line = cur_line->next;
3920 if ( (cur_line == NULL)
3921 || (prev_line == NULL)
3922 || (cur_line->type != FILE_LINE_URL))
3924 /* Invalid "patternid" parameter */
3925 edit_free_file(file);
3926 return JB_ERR_CGI_PARAMS;
3929 /* At this point, the line to remove is in cur_line, and the previous
3930 * one is in prev_line
3933 /* Unlink cur_line */
3934 prev_line->next = cur_line->next;
3935 cur_line->next = NULL;
3938 edit_free_file_lines(cur_line);
3940 err = edit_write_file(file);
3943 /* Error writing file */
3944 if (err == JB_ERR_FILE)
3946 /* Read-only file. */
3947 err = cgi_error_file_read_only(csp, rsp, file->filename);
3949 edit_free_file(file);
3953 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3954 (long) time(NULL), file->identifier, section_start_line_number);
3956 edit_free_file(file);
3958 return cgi_redirect(rsp, target);
3962 /*********************************************************************
3964 * Function : cgi_edit_actions_section_remove
3966 * Description : CGI function that actually removes a whole section from
3967 * the actions file. The section must be empty first
3968 * (else JB_ERR_CGI_PARAMS).
3971 * 1 : csp = Current client state (buffers, headers, etc...)
3972 * 2 : rsp = http_response data structure for output
3973 * 3 : parameters = map of cgi parameters
3976 * f : (filename) Identifies the file to edit
3977 * v : (version) File's last-modified time
3978 * s : (section) Line number of section to edit
3980 * Returns : JB_ERR_OK on success
3981 * JB_ERR_MEMORY on out-of-memory
3982 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3983 * specified or not valid.
3985 *********************************************************************/
3986 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3987 struct http_response *rsp,
3988 const struct map *parameters)
3991 struct editable_file * file;
3992 struct file_line * cur_line;
3993 struct file_line * prev_line;
3994 unsigned line_number;
3998 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4000 return cgi_error_disabled(csp, rsp);
4003 err = get_number_param(csp, parameters, "s", §ionid);
4009 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4012 /* No filename specified, can't read file, modified, or out of memory. */
4013 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4017 cur_line = file->lines;
4020 while ((cur_line != NULL) && (line_number < sectionid))
4022 prev_line = cur_line;
4023 cur_line = cur_line->next;
4027 if ( (cur_line == NULL)
4028 || (cur_line->type != FILE_LINE_ACTION) )
4030 /* Invalid "sectionid" parameter */
4031 edit_free_file(file);
4032 return JB_ERR_CGI_PARAMS;
4035 if ( (cur_line->next != NULL)
4036 && (cur_line->next->type == FILE_LINE_URL) )
4038 /* Section not empty. */
4039 edit_free_file(file);
4040 return JB_ERR_CGI_PARAMS;
4043 /* At this point, the line to remove is in cur_line, and the previous
4044 * one is in prev_line
4047 /* Unlink cur_line */
4048 if (prev_line == NULL)
4050 /* Removing the first line from the file */
4051 file->lines = cur_line->next;
4055 prev_line->next = cur_line->next;
4057 cur_line->next = NULL;
4060 edit_free_file_lines(cur_line);
4062 err = edit_write_file(file);
4065 /* Error writing file */
4066 if (err == JB_ERR_FILE)
4068 /* Read-only file. */
4069 err = cgi_error_file_read_only(csp, rsp, file->filename);
4071 edit_free_file(file);
4075 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4076 (long) time(NULL), file->identifier);
4078 edit_free_file(file);
4080 return cgi_redirect(rsp, target);
4084 /*********************************************************************
4086 * Function : cgi_edit_actions_section_add
4088 * Description : CGI function that adds a new empty section to
4092 * 1 : csp = Current client state (buffers, headers, etc...)
4093 * 2 : rsp = http_response data structure for output
4094 * 3 : parameters = map of cgi parameters
4097 * f : (filename) Identifies the file to edit
4098 * v : (version) File's last-modified time
4099 * s : (section) Line number of section to add after, 0 for
4102 * Returns : JB_ERR_OK on success
4103 * JB_ERR_MEMORY on out-of-memory
4104 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4105 * specified or not valid.
4107 *********************************************************************/
4108 jb_err cgi_edit_actions_section_add(struct client_state *csp,
4109 struct http_response *rsp,
4110 const struct map *parameters)
4113 struct file_line * new_line;
4115 struct editable_file * file;
4116 struct file_line * cur_line;
4117 unsigned line_number;
4121 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4123 return cgi_error_disabled(csp, rsp);
4126 err = get_number_param(csp, parameters, "s", §ionid);
4132 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4135 /* No filename specified, can't read file, modified, or out of memory. */
4136 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4140 cur_line = file->lines;
4142 if (sectionid <= 1U)
4144 /* Add to start of file */
4145 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4147 /* There's something in the file, find the line before the first
4150 while ( (cur_line->next != NULL)
4151 && (cur_line->next->type != FILE_LINE_ACTION) )
4153 cur_line = cur_line->next;
4159 /* File starts with action line, so insert at top */
4165 /* Add after stated section. */
4166 while ((cur_line != NULL) && (line_number < sectionid))
4168 cur_line = cur_line->next;
4172 if ( (cur_line == NULL)
4173 || (cur_line->type != FILE_LINE_ACTION))
4175 /* Invalid "sectionid" parameter */
4176 edit_free_file(file);
4177 return JB_ERR_CGI_PARAMS;
4180 /* Skip through the section to find the last line in it. */
4181 while ( (cur_line->next != NULL)
4182 && (cur_line->next->type != FILE_LINE_ACTION) )
4184 cur_line = cur_line->next;
4189 /* At this point, the last line in the previous section is in cur_line
4190 * - add after this. (Or if we need to add as the first line, cur_line
4194 new_text = strdup("{}");
4195 if (NULL == new_text)
4197 edit_free_file(file);
4198 return JB_ERR_MEMORY;
4201 /* Allocate the new line */
4202 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4203 if (new_line == NULL)
4206 edit_free_file(file);
4207 return JB_ERR_MEMORY;
4210 /* Fill in the data members of the new line */
4211 new_line->raw = NULL;
4212 new_line->prefix = NULL;
4213 new_line->unprocessed = new_text;
4214 new_line->type = FILE_LINE_ACTION;
4216 if (cur_line != NULL)
4218 /* Link new_line into the list, after cur_line */
4219 new_line->next = cur_line->next;
4220 cur_line->next = new_line;
4224 /* Link new_line into the list, as first line */
4225 new_line->next = file->lines;
4226 file->lines = new_line;
4229 /* Done making changes, now commit */
4231 err = edit_write_file(file);
4234 /* Error writing file */
4235 if (err == JB_ERR_FILE)
4237 /* Read-only file. */
4238 err = cgi_error_file_read_only(csp, rsp, file->filename);
4240 edit_free_file(file);
4244 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4245 (long) time(NULL), file->identifier);
4247 edit_free_file(file);
4249 return cgi_redirect(rsp, target);
4253 /*********************************************************************
4255 * Function : cgi_edit_actions_section_swap
4257 * Description : CGI function that swaps the order of two sections
4258 * in the actions file. Note that this CGI can actually
4259 * swap any two arbitrary sections, but the GUI interface
4260 * currently only allows consecutive sections to be
4264 * 1 : csp = Current client state (buffers, headers, etc...)
4265 * 2 : rsp = http_response data structure for output
4266 * 3 : parameters = map of cgi parameters
4269 * f : (filename) Identifies the file to edit
4270 * v : (version) File's last-modified time
4271 * s1 : (section1) Line number of first section to swap
4272 * s2 : (section2) Line number of second section to swap
4274 * Returns : JB_ERR_OK on success
4275 * JB_ERR_MEMORY on out-of-memory
4276 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4277 * specified or not valid.
4279 *********************************************************************/
4280 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4281 struct http_response *rsp,
4282 const struct map *parameters)
4286 struct editable_file * file;
4287 struct file_line * cur_line;
4288 struct file_line * prev_line;
4289 struct file_line * line_before_section1;
4290 struct file_line * line_start_section1;
4291 struct file_line * line_end_section1;
4292 struct file_line * line_after_section1;
4293 struct file_line * line_before_section2;
4294 struct file_line * line_start_section2;
4295 struct file_line * line_end_section2;
4296 struct file_line * line_after_section2;
4297 unsigned line_number;
4301 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4303 return cgi_error_disabled(csp, rsp);
4306 err = get_number_param(csp, parameters, "s1", §ion1);
4307 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4313 if (section1 > section2)
4315 unsigned temp = section2;
4316 section2 = section1;
4320 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4323 /* No filename specified, can't read file, modified, or out of memory. */
4324 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4327 /* Start at the beginning... */
4329 cur_line = file->lines;
4332 /* ... find section1 ... */
4333 while ((cur_line != NULL) && (line_number < section1))
4335 prev_line = cur_line;
4336 cur_line = cur_line->next;
4340 if ( (cur_line == NULL)
4341 || (cur_line->type != FILE_LINE_ACTION) )
4343 /* Invalid "section1" parameter */
4344 edit_free_file(file);
4345 return JB_ERR_CGI_PARAMS;
4348 /* If no-op, we've validated params and can skip the rest. */
4349 if (section1 != section2)
4351 /* ... find the end of section1 ... */
4352 line_before_section1 = prev_line;
4353 line_start_section1 = cur_line;
4356 prev_line = cur_line;
4357 cur_line = cur_line->next;
4360 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4361 line_end_section1 = prev_line;
4362 line_after_section1 = cur_line;
4364 /* ... find section2 ... */
4365 while ((cur_line != NULL) && (line_number < section2))
4367 prev_line = cur_line;
4368 cur_line = cur_line->next;
4372 if ( (cur_line == NULL)
4373 || (cur_line->type != FILE_LINE_ACTION) )
4375 /* Invalid "section2" parameter */
4376 edit_free_file(file);
4377 return JB_ERR_CGI_PARAMS;
4380 /* ... find the end of section2 ... */
4381 line_before_section2 = prev_line;
4382 line_start_section2 = cur_line;
4385 prev_line = cur_line;
4386 cur_line = cur_line->next;
4389 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4390 line_end_section2 = prev_line;
4391 line_after_section2 = cur_line;
4393 /* Now have all the pointers we need. Do the swap. */
4395 /* Change the pointer to section1 to point to section2 instead */
4396 if (line_before_section1 == NULL)
4398 file->lines = line_start_section2;
4402 line_before_section1->next = line_start_section2;
4405 if (line_before_section2 == line_end_section1)
4407 /* Consecutive sections */
4408 line_end_section2->next = line_start_section1;
4412 line_end_section2->next = line_after_section1;
4413 line_before_section2->next = line_start_section1;
4416 /* Set the pointer from the end of section1 to the rest of the file */
4417 line_end_section1->next = line_after_section2;
4419 err = edit_write_file(file);
4422 /* Error writing file */
4423 if (err == JB_ERR_FILE)
4425 /* Read-only file. */
4426 err = cgi_error_file_read_only(csp, rsp, file->filename);
4428 edit_free_file(file);
4431 } /* END if (section1 != section2) */
4433 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4434 (long) time(NULL), file->identifier);
4436 edit_free_file(file);
4438 return cgi_redirect(rsp, target);
4441 #ifdef FEATURE_TOGGLE
4442 /*********************************************************************
4444 * Function : cgi_toggle
4446 * Description : CGI function that adds a new empty section to
4450 * 1 : csp = Current client state (buffers, headers, etc...)
4451 * 2 : rsp = http_response data structure for output
4452 * 3 : parameters = map of cgi parameters
4455 * set : If present, how to change toggle setting:
4456 * "enable", "disable", "toggle", or none (default).
4457 * mini : If present, use mini reply template.
4459 * Returns : JB_ERR_OK on success
4460 * JB_ERR_MEMORY on out-of-memory
4462 *********************************************************************/
4463 jb_err cgi_toggle(struct client_state *csp,
4464 struct http_response *rsp,
4465 const struct map *parameters)
4467 struct map *exports;
4469 const char *template_name;
4475 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4477 return cgi_error_disabled(csp, rsp);
4480 mode = get_char_param(parameters, "set");
4485 global_toggle_state = 1;
4487 else if (mode == 'D')
4490 global_toggle_state = 0;
4492 else if (mode == 'T')
4495 global_toggle_state = !global_toggle_state;
4498 if (NULL == (exports = default_exports(csp, "toggle")))
4500 return JB_ERR_MEMORY;
4503 template_name = (get_char_param(parameters, "mini")
4507 return template_fill_for_cgi(csp, template_name, exports, rsp);
4509 #endif /* def FEATURE_TOGGLE */
4511 /*********************************************************************
4513 * Function : javascriptify
4515 * Description : Converts a string into a form JavaScript will like.
4517 * Netscape 4's JavaScript sucks - it doesn't use
4518 * "id" parameters, so you have to set the "name"
4519 * used to submit a form element to something JavaScript
4520 * will like. (Or access the elements by index in an
4521 * array. That array contains >60 elements and will
4522 * be changed whenever we add a new action to the
4523 * editor, so I'm NOT going to use indexes that have
4524 * to be figured out by hand.)
4526 * Currently the only thing we have to worry about
4527 * is "-" ==> "_" conversion.
4529 * This is a length-preserving operation so it is
4530 * carried out in-place, no memory is allocated
4534 * 1 : identifier = String to make JavaScript-friendly.
4538 *********************************************************************/
4539 static void javascriptify(char * identifier)
4541 char * p = identifier;
4542 while (NULL != (p = strchr(p, '-')))
4549 /*********************************************************************
4551 * Function : actions_to_radio
4553 * Description : Converts a actionsfile entry into settings for
4554 * radio buttons and edit boxes on a HTML form.
4557 * 1 : exports = List of substitutions to add to.
4558 * 2 : action = Action to read
4560 * Returns : JB_ERR_OK on success
4561 * JB_ERR_MEMORY on out-of-memory
4563 *********************************************************************/
4564 static jb_err actions_to_radio(struct map * exports,
4565 const struct action_spec *action)
4567 unsigned mask = action->mask;
4568 unsigned add = action->add;
4576 mask = action->mask;
4579 /* sanity - prevents "-feature +feature" */
4583 #define DEFINE_ACTION_BOOL(name, bit) \
4584 if (!(mask & bit)) \
4586 current_mode = 'n'; \
4588 else if (add & bit) \
4590 current_mode = 'y'; \
4594 current_mode = 'x'; \
4596 if (map_radio(exports, name, "ynx", current_mode)) \
4598 return JB_ERR_MEMORY; \
4601 #define DEFINE_ACTION_STRING(name, bit, index) \
4602 DEFINE_ACTION_BOOL(name, bit); \
4605 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4608 checked = !strcmp(action->string[index], value); \
4612 checked = is_default; \
4614 mapped_param |= checked; \
4615 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4617 return JB_ERR_MEMORY; \
4620 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4621 if (map(exports, name "-param-custom", 1, \
4622 ((!mapped_param) ? "checked" : ""), 1)) \
4624 return JB_ERR_MEMORY; \
4626 if (map(exports, name "-param", 1, \
4627 (((add & bit) && !mapped_param) ? \
4628 action->string[index] : default_val), 1)) \
4630 return JB_ERR_MEMORY; \
4633 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4634 if (map(exports, name "-param", 1, \
4635 ((add & bit) ? action->string[index] : default_val), 1)) \
4637 return JB_ERR_MEMORY; \
4640 #define DEFINE_ACTION_MULTI(name, index) \
4641 if (action->multi_add[index]->first) \
4643 current_mode = 'y'; \
4645 else if (action->multi_remove_all[index]) \
4647 current_mode = 'n'; \
4649 else if (action->multi_remove[index]->first) \
4651 current_mode = 'y'; \
4655 current_mode = 'x'; \
4657 if (map_radio(exports, name, "ynx", current_mode)) \
4659 return JB_ERR_MEMORY; \
4662 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4664 #include "actionlist.h"
4666 #undef DEFINE_ACTION_MULTI
4667 #undef DEFINE_ACTION_STRING
4668 #undef DEFINE_ACTION_BOOL
4669 #undef DEFINE_ACTION_ALIAS
4670 #undef DEFINE_CGI_PARAM_CUSTOM
4671 #undef DEFINE_CGI_PARAM_RADIO
4672 #undef DEFINE_CGI_PARAM_NO_RADIO
4678 /*********************************************************************
4680 * Function : actions_from_radio
4682 * Description : Converts a map of parameters passed to a CGI function
4683 * into an actionsfile entry.
4686 * 1 : parameters = parameters to the CGI call
4687 * 2 : action = Action to change. Must be valid before
4688 * the call, actions not specified will be
4691 * Returns : JB_ERR_OK on success
4692 * JB_ERR_MEMORY on out-of-memory
4694 *********************************************************************/
4695 static jb_err actions_from_radio(const struct map * parameters,
4696 struct action_spec *action)
4701 const char * js_name;
4702 jb_err err = JB_ERR_OK;
4707 /* Statics are generally a potential race condition,
4708 * but in this case we're safe and don't need semaphores.
4709 * Be careful if you modify this function.
4711 * The js_name_arr's are never free()d, but this is no
4712 * problem, since they will only be created once and
4713 * used by all threads thereafter. -oes
4716 #define JAVASCRIPTIFY(dest_var, string) \
4718 static int first_time = 1; \
4719 static char *js_name_arr; \
4722 js_name_arr = strdup(string); \
4723 javascriptify(js_name_arr); \
4725 dest_var = js_name_arr; \
4729 #define DEFINE_ACTION_BOOL(name, bit) \
4730 JAVASCRIPTIFY(js_name, name); \
4731 ch = get_char_param(parameters, js_name); \
4734 action->add |= bit; \
4735 action->mask |= bit; \
4737 else if (ch == 'N') \
4739 action->add &= ~bit; \
4740 action->mask &= ~bit; \
4742 else if (ch == 'X') \
4744 action->add &= ~bit; \
4745 action->mask |= bit; \
4748 #define DEFINE_ACTION_STRING(name, bit, index) \
4749 JAVASCRIPTIFY(js_name, name); \
4750 ch = get_char_param(parameters, js_name); \
4754 JAVASCRIPTIFY(js_name, name "-mode"); \
4755 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4756 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4758 JAVASCRIPTIFY(js_name, name "-param"); \
4759 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4761 if (param != NULL) \
4763 if (NULL == (param_dup = strdup(param))) \
4765 return JB_ERR_MEMORY; \
4767 freez(action->string[index]); \
4768 action->add |= bit; \
4769 action->mask |= bit; \
4770 action->string[index] = param_dup; \
4773 else if (ch == 'N') \
4775 if (action->add & bit) \
4777 freez(action->string[index]); \
4779 action->add &= ~bit; \
4780 action->mask &= ~bit; \
4782 else if (ch == 'X') \
4784 if (action->add & bit) \
4786 freez(action->string[index]); \
4788 action->add &= ~bit; \
4789 action->mask |= bit; \
4792 #define DEFINE_ACTION_MULTI(name, index) \
4793 JAVASCRIPTIFY(js_name, name); \
4794 ch = get_char_param(parameters, js_name); \
4799 else if (ch == 'N') \
4801 list_remove_all(action->multi_add[index]); \
4802 list_remove_all(action->multi_remove[index]); \
4803 action->multi_remove_all[index] = 1; \
4805 else if (ch == 'X') \
4807 list_remove_all(action->multi_add[index]); \
4808 list_remove_all(action->multi_remove[index]); \
4809 action->multi_remove_all[index] = 0; \
4812 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4814 #include "actionlist.h"
4816 #undef DEFINE_ACTION_MULTI
4817 #undef DEFINE_ACTION_STRING
4818 #undef DEFINE_ACTION_BOOL
4819 #undef DEFINE_ACTION_ALIAS
4820 #undef JAVASCRIPTIFY
4826 #endif /* def FEATURE_CGI_EDIT_ACTIONS */