1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.52 2007/04/12 10:41:23 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.52 2007/04/12 10:41:23 fabiankeil
46 * - Don't mistake VC++'s _snprintf() for a snprintf() replacement.
47 * - Move some cgi_edit_actions_for_url() variables into structs.
48 * - Remove bogus comment.
50 * Revision 1.51 2007/04/08 13:21:05 fabiankeil
51 * Reference action files in CGI URLs by id instead
52 * of using the first part of the file name.
53 * Fixes BR 1694250 and BR 1590556.
55 * Revision 1.50 2007/03/29 11:40:34 fabiankeil
56 * Divide @filter-params@ into @client-header-filter-params@
57 * @content-filter-params@ and @server-header-filter-params@.
59 * Revision 1.49 2007/03/20 15:16:34 fabiankeil
60 * Use dedicated header filter actions instead of abusing "filter".
61 * Replace "filter-client-headers" and "filter-client-headers"
62 * with "server-header-filter" and "client-header-filter".
64 * Revision 1.48 2007/02/13 14:35:25 fabiankeil
65 * Replace hash escaping code to prevent
66 * crashes, memory and file corruption.
68 * Revision 1.47 2006/12/28 18:04:25 fabiankeil
69 * Fixed gcc43 conversion warnings.
71 * Revision 1.46 2006/12/27 18:44:52 fabiankeil
72 * Stop shadowing string.h's index().
74 * Revision 1.45 2006/12/21 12:57:48 fabiankeil
75 * Add config option "split-large-forms"
76 * to work around the browser bug reported
79 * Revision 1.44 2006/12/09 13:49:16 fabiankeil
80 * Fix configure option --disable-toggle.
81 * Thanks to Peter Thoenen for reporting this.
83 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
84 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
85 * with what was really the latest development (the v_3_0_branch branch)
87 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
88 * Remove a little residual debugging info
90 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
91 * Multiple filter file support
93 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
94 * Undo some damaging memory leak patches
96 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
97 * Removing a double free
99 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
100 * A few memory leaks plugged (mostly on error paths)
102 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
103 * Moved cgi_error_disabled() from cgiedit.c to
104 * cgi.c to re-enable build with --disable-editor.
105 * Fixes Bug #892744. Thanks to Matthew Fischer
108 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
109 * One line lost in last commit
111 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
112 * - All edit functions that redirect back to the list page
113 * now use cgi_redirect
114 * - All redirects now contain useless parameter "foo", whose
115 * value are raw seconds since epoch, in order to force
116 * Opera and Konqueror to properly reload the list. Closes
119 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
120 * Cosmetic: Renamed cryptic variable
122 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
123 * Fix: Don't free uninitialized struct editable_file
125 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
126 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
128 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
129 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
131 * Revision 1.41 2002/05/21 19:09:45 oes
132 * - Made Add/Edit/Remove URL Submit and Cancel
133 * buttons jump back to relevant section in eal
134 * - Bugfix: remove-url-form needs p export
136 * Revision 1.40 2002/05/19 11:34:35 jongfoster
137 * Handling read-only actions files better - report the actual
138 * error, not "Out of memory"!
141 * http://sourceforge.net/tracker/index.php?func=detail
142 * &aid=557905&group_id=11118&atid=111118
144 * Revision 1.39 2002/05/12 21:39:15 jongfoster
145 * - Adding Doxygen-style comments to structures and #defines.
146 * - Correcting function comments
148 * Revision 1.38 2002/05/03 23:00:38 jongfoster
149 * Support for templates for "standard actions" buttons.
152 * Revision 1.37 2002/04/30 11:14:52 oes
153 * Made csp the first parameter in *action_to_html
155 * Revision 1.36 2002/04/26 21:53:30 jongfoster
156 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
158 * Revision 1.35 2002/04/26 21:50:02 jongfoster
159 * Honouring default exports in edit-actions-for-url-filter template.
161 * Revision 1.34 2002/04/26 12:54:17 oes
162 * Adaptions to changes in actions.c
164 * Revision 1.33 2002/04/24 02:17:47 oes
165 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
167 * - Activated Jon's code for editing multiple AFs
168 * - cgi_edit_list_actions now provides context-sensitive
169 * help, looks up all action sets from standard.action and
170 * makes buttons for them in the catchall section
171 * - cgi_edit_action_submit now honors a p parameter, looks up
172 * the corresponding action set, and sets the catchall pattern's
173 * actions accordingly.
175 * Revision 1.32 2002/04/19 16:55:31 jongfoster
176 * Fixing newline problems. If we do our own text file newline
177 * mangling, we don't want the library to do any, so we need to
178 * open the files in *binary* mode.
180 * Revision 1.31 2002/04/18 19:21:08 jongfoster
181 * Added code to detect "conventional" action files, that start
182 * with a set of actions for all URLs (the pattern "/").
183 * These are special-cased in the "edit-actions-list" CGI, so
184 * that a special UI can be written for them.
186 * Revision 1.30 2002/04/10 13:38:35 oes
187 * load_template signature changed
189 * Revision 1.29 2002/04/08 16:59:08 oes
192 * Revision 1.28 2002/03/27 12:30:29 oes
193 * Deleted unsused variable
195 * Revision 1.27 2002/03/26 23:06:04 jongfoster
196 * Removing duplicate @ifs on the toggle page
198 * Revision 1.26 2002/03/26 22:59:17 jongfoster
199 * Fixing /toggle to display status consistently.
201 * Revision 1.25 2002/03/26 22:29:54 swa
202 * we have a new homepage!
204 * Revision 1.24 2002/03/24 15:23:33 jongfoster
207 * Revision 1.23 2002/03/24 13:32:41 swa
208 * name change related issues
210 * Revision 1.22 2002/03/24 13:25:43 swa
211 * name change related issues
213 * Revision 1.21 2002/03/22 18:02:48 jongfoster
214 * Fixing remote toggle
216 * Revision 1.20 2002/03/16 20:28:34 oes
217 * Added descriptions to the filters so users will know what they select in the cgi editor
219 * Revision 1.19 2002/03/16 18:38:14 jongfoster
220 * Stopping stupid or malicious users from breaking the actions
221 * file using the web-based editor.
223 * Revision 1.18 2002/03/16 14:57:44 jongfoster
224 * Full support for enabling/disabling modular filters.
226 * Revision 1.17 2002/03/16 14:26:42 jongfoster
227 * First version of modular filters support - READ ONLY!
228 * Fixing a double-free bug in the out-of-memory handling in map_radio().
230 * Revision 1.16 2002/03/07 03:46:17 oes
231 * Fixed compiler warnings
233 * Revision 1.15 2002/03/06 22:54:35 jongfoster
234 * Automated function-comment nitpicking.
236 * Revision 1.14 2002/03/05 00:24:51 jongfoster
237 * Patch to always edit the current actions file.
239 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
240 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
242 * Revision 1.12 2002/03/03 09:18:03 joergs
243 * Made jumbjuster work on AmigaOS again.
245 * Revision 1.11 2002/01/23 01:03:31 jongfoster
246 * Fixing gcc [CygWin] compiler warnings
248 * Revision 1.10 2002/01/23 00:22:59 jongfoster
249 * Adding new function cgi_edit_actions_section_swap(), to reorder
252 * Adding get_url_spec_param() to get a validated URL pattern.
254 * Moving edit_read_line() out of this file and into loaders.c.
256 * Adding missing html_encode() to many CGI functions.
258 * Moving the functions that #include actionlist.h to the end of the file,
259 * because the Visual C++ 97 debugger gets extremely confused if you try
260 * to debug any code that comes after them in the file.
262 * Major optimizations in cgi_edit_actions_list() to reduce the size of
263 * the generated HTML (down 40% from 550k to 304k), with major side-effects
264 * throughout the editor and templates. In particular, the length of the
265 * URLs throughout the editor has been drastically reduced, by cutting
266 * paramater names down to 1 character and CGI names down to 3-4
267 * characters, by removing all non-essential CGI paramaters even at the
268 * expense of having to re-read the actions file for the most trivial
269 * page, and by using relative rather than absolute URLs. This means
270 * that this (typical example):
272 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
273 * filename=ijb&ver=1011487572&section=12&pattern=13
274 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
278 * <a href="eau?f=ijb&v=1011487572&p=13">
280 * Revision 1.9 2002/01/17 20:56:22 jongfoster
281 * Replacing hard references to the URL of the config interface
282 * with #defines from project.h
284 * Revision 1.8 2001/11/30 23:35:51 jongfoster
285 * Renaming actionsfile to ijb.action
287 * Revision 1.7 2001/11/13 00:28:24 jongfoster
288 * - Renaming parameters from edit-actions-for-url so that they only
289 * contain legal JavaScript characters. If we wanted to write
290 * JavaScript that worked with Netscape 4, this is nessacery.
291 * (Note that at the moment the JavaScript doesn't actually work
292 * with Netscape 4, but now this is purely a template issue, not
293 * one affecting code).
294 * - Adding new CGIs for use by non-JavaScript browsers:
295 * edit-actions-url-form
296 * edit-actions-add-url-form
297 * edit-actions-remove-url-form
300 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
301 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
302 * by and __OS2__ ifdef.
304 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
305 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
306 * threads to call select() simultaneously. So, it's time to do a real, live,
307 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
308 * (native). Both versions will work, but using __OS2__ offers multi-threading.
310 * Revision 1.4 2001/10/23 21:48:19 jongfoster
311 * Cleaning up error handling in CGI functions - they now send back
312 * a HTML error page and should never cause a FATAL error. (Fixes one
313 * potential source of "denial of service" attacks).
315 * CGI actions file editor that works and is actually useful.
317 * Ability to toggle JunkBuster remotely using a CGI call.
319 * You can turn off both the above features in the main configuration
320 * file, e.g. if you are running a multi-user proxy.
322 * Revision 1.3 2001/10/14 22:12:49 jongfoster
323 * New version of CGI-based actionsfile editor.
324 * Major changes, including:
325 * - Completely new file parser and file output routines
326 * - edit-actions CGI renamed edit-actions-for-url
327 * - All CGIs now need a filename parameter, except for...
328 * - New CGI edit-actions which doesn't need a filename,
329 * to allow you to start the editor up.
330 * - edit-actions-submit now works, and now automatically
331 * redirects you back to the main edit-actions-list handler.
333 * Revision 1.2 2001/09/16 17:05:14 jongfoster
334 * Removing unused #include showarg.h
336 * Revision 1.1 2001/09/16 15:47:37 jongfoster
337 * First version of CGI-based edit interface. This is very much a
338 * work-in-progress, and you can't actually use it to edit anything
339 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
340 * to have any effect.
343 **********************************************************************/
349 * FIXME: Following includes copied from cgi.c - which are actually needed?
354 #include <sys/types.h>
358 #include <sys/stat.h>
363 #include "cgisimple.h"
367 #include "miscutil.h"
370 #ifdef FEATURE_TOGGLE
371 /* loadcfg.h is for global_toggle_state only */
373 #endif /* def FEATURE_TOGGLE */
374 #include "urlmatch.h"
376 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
379 #ifdef FEATURE_CGI_EDIT_ACTIONS
382 * A line in an editable_file.
386 /** Next entry in the linked list */
387 struct file_line * next;
389 /** The raw data, to write out if this line is unmodified. */
392 /** Comments and/or whitespace to put before this line if it's modified
393 and then written out. */
396 /** The actual data, as a string. Line continuation and comment removal
397 are performed on the data read from file before it's stored here, so
398 it will be a single line of data. */
401 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
404 /** The actual data, processed into some sensible data type. */
408 /** An action specification. */
409 struct action_spec action[1];
411 /** A name=value pair. */
415 /** The name in the name=value pair. */
418 /** The value in the name=value pair, as a string. */
421 /** The value in the name=value pair, as an integer. */
426 /* Add more data types here... e.g.
429 struct url_spec url[1];
433 struct action_spec action[1];
443 /** This file_line has not been processed yet. */
444 #define FILE_LINE_UNPROCESSED 1
446 /** This file_line is blank. Can only appear at the end of a file, due to
447 the way the parser works. */
448 #define FILE_LINE_BLANK 2
450 /** This file_line says {{alias}}. */
451 #define FILE_LINE_ALIAS_HEADER 3
453 /** This file_line defines an alias. */
454 #define FILE_LINE_ALIAS_ENTRY 4
456 /** This file_line defines an {action}. */
457 #define FILE_LINE_ACTION 5
459 /** This file_line specifies a URL pattern. */
460 #define FILE_LINE_URL 6
462 /** This file_line says {{settings}}. */
463 #define FILE_LINE_SETTINGS_HEADER 7
465 /** This file_line is in a {{settings}} block. */
466 #define FILE_LINE_SETTINGS_ENTRY 8
468 /** This file_line says {{description}}. */
469 #define FILE_LINE_DESCRIPTION_HEADER 9
471 /** This file_line is in a {{description}} block. */
472 #define FILE_LINE_DESCRIPTION_ENTRY 10
476 * A configuration file, in a format that can be edited and written back to
481 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
482 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
483 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
484 const char * version_str; /**< Last modification time, as a string. For CGI param. */
485 /**< Can be used in URL without using url_param(). */
486 unsigned version; /**< Last modification time - prevents chaos with
487 the browser's "back" button. Note that this is a
488 time_t cast to an unsigned. When comparing, always
489 cast the time_t to an unsigned, and *NOT* vice-versa.
490 This may lose the top few bits, but they're not
491 significant anyway. */
492 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
493 Note that changing this after the file has been
494 read in will cause a mess. */
495 struct file_line * parse_error; /**< On parse error, this is the offending line. */
496 const char * parse_error_text; /**< On parse error, this is the problem.
497 (Statically allocated) */
501 * Information about the filter types.
502 * Used for macro replacement in cgi_edit_actions_for_url.
504 struct filter_type_info
506 const int multi_action_index; /**< The multi action index as defined in project.h */
507 const char *macro_name; /**< Name of the macro that has to be replaced
508 with the prepared templates.
509 For example "content-filter-params" */
510 const char *type; /**< Name of the filter type,
511 for example "server-header-filter". */
512 const char *abbr_type; /**< Abbreviation of the filter type, usually the
513 first or second character capitalized */
514 const char *anchor; /**< Anchor for the User Manual link,
515 for example "SERVER-HEADER-FILTER" */
518 /* Accessed by index, keep the order in the way the FT_ macros are defined. */
519 const static struct filter_type_info filter_type_info[] =
523 "content-filter-params", "filter",
527 ACTION_MULTI_CLIENT_HEADER_FILTER,
528 "client-header-filter-params", "client-header-filter",
529 "C", "CLIENT-HEADER-FILTER"
532 ACTION_MULTI_SERVER_HEADER_FILTER,
533 "server-header-filter-params", "server-header-filter",
534 "S", "SERVER-HEADER-FILTER"
537 ACTION_MULTI_CLIENT_HEADER_TAGGER,
538 "client-header-tagger-params", "client-header-tagger",
539 "L", "CLIENT-HEADER-TAGGER"
542 ACTION_MULTI_SERVER_HEADER_TAGGER,
543 "server-header-tagger-params", "server-header-tagger",
544 "E", "SERVER-HEADER-TAGGER"
548 /* FIXME: Following non-static functions should be prototyped in .h or made static */
550 /* Functions to read and write arbitrary config files */
551 jb_err edit_read_file(struct client_state *csp,
552 const struct map *parameters,
554 struct editable_file **pfile);
555 jb_err edit_write_file(struct editable_file * file);
556 void edit_free_file(struct editable_file * file);
558 /* Functions to read and write actions files */
559 jb_err edit_parse_actions_file(struct editable_file * file);
560 jb_err edit_read_actions_file(struct client_state *csp,
561 struct http_response *rsp,
562 const struct map *parameters,
564 struct editable_file **pfile);
567 jb_err cgi_error_modified(struct client_state *csp,
568 struct http_response *rsp,
569 const char *filename);
570 jb_err cgi_error_parse(struct client_state *csp,
571 struct http_response *rsp,
572 struct editable_file *file);
573 jb_err cgi_error_file(struct client_state *csp,
574 struct http_response *rsp,
575 const char *filename);
576 jb_err cgi_error_file_read_only(struct client_state *csp,
577 struct http_response *rsp,
578 const char *filename);
580 /* Internal arbitrary config file support functions */
581 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
582 static void edit_free_file_lines(struct file_line * first_line);
584 /* Internal actions file support functions */
585 static int match_actions_file_header_line(const char * line, const char * name);
586 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
588 /* Internal parameter parsing functions */
589 static jb_err get_url_spec_param(struct client_state *csp,
590 const struct map *parameters,
595 /* Internal actionsfile <==> HTML conversion functions */
596 static jb_err map_radio(struct map * exports,
597 const char * optionname,
600 static jb_err actions_to_radio(struct map * exports,
601 const struct action_spec *action);
602 static jb_err actions_from_radio(const struct map * parameters,
603 struct action_spec *action);
606 static jb_err map_copy_parameter_html(struct map *out,
607 const struct map *in,
609 #if 0 /* unused function */
610 static jb_err map_copy_parameter_url(struct map *out,
611 const struct map *in,
613 #endif /* unused function */
615 /* Internal convenience functions */
616 static char *section_target(const unsigned sectionid);
618 /*********************************************************************
620 * Function : section_target
622 * Description : Given an unsigned (section id) n, produce a dynamically
623 * allocated string of the form #l<n>, for use in link
626 * XXX: The hash should be moved into the templates
627 * to make this function more generic and render
628 * stringify() obsolete.
631 * 1 : sectionid = start line number of section
633 * Returns : String with link target, or NULL if out of
636 *********************************************************************/
637 static char *section_target(const unsigned sectionid)
641 snprintf(buf, 30, "#l%d", sectionid);
647 /*********************************************************************
649 * Function : stringify
651 * Description : Convert a number into a dynamically allocated string.
654 * 1 : number = The number to convert.
656 * Returns : String with link target, or NULL if out of memory
658 *********************************************************************/
659 static char *stringify(const unsigned number)
663 snprintf(buf, sizeof(buf), "%i", number);
668 /*********************************************************************
670 * Function : map_copy_parameter_html
672 * Description : Copy a CGI parameter from one map to another, HTML
676 * 1 : out = target map
677 * 2 : in = source map
678 * 3 : name = name of cgi parameter to copy
680 * Returns : JB_ERR_OK on success
681 * JB_ERR_MEMORY on out-of-memory
682 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
685 *********************************************************************/
686 static jb_err map_copy_parameter_html(struct map *out,
687 const struct map *in,
697 value = lookup(in, name);
698 err = map(out, name, 1, html_encode(value), 0);
705 else if (*value == '\0')
707 return JB_ERR_CGI_PARAMS;
716 #if 0 /* unused function */
717 /*********************************************************************
719 * Function : map_copy_parameter_url
721 * Description : Copy a CGI parameter from one map to another, URL
725 * 1 : out = target map
726 * 2 : in = source map
727 * 3 : name = name of cgi parameter to copy
729 * Returns : JB_ERR_OK on success
730 * JB_ERR_MEMORY on out-of-memory
731 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
734 *********************************************************************/
735 static jb_err map_copy_parameter_url(struct map *out,
736 const struct map *in,
746 value = lookup(in, name);
747 err = map(out, name, 1, url_encode(value), 0);
754 else if (*value == '\0')
756 return JB_ERR_CGI_PARAMS;
763 #endif /* 0 - unused function */
766 /*********************************************************************
768 * Function : cgi_edit_actions_url_form
770 * Description : CGI function that displays a form for
774 * 1 : csp = Current client state (buffers, headers, etc...)
775 * 2 : rsp = http_response data structure for output
776 * 3 : parameters = map of cgi parameters
779 * i : (action index) Identifies the file to edit
780 * v : (version) File's last-modified time
781 * p : (pattern) Line number of pattern to edit
783 * Returns : JB_ERR_OK on success
784 * JB_ERR_MEMORY on out-of-memory
785 * JB_ERR_CGI_PARAMS if the CGI parameters are not
786 * specified or not valid.
788 *********************************************************************/
789 jb_err cgi_edit_actions_url_form(struct client_state *csp,
790 struct http_response *rsp,
791 const struct map *parameters)
793 struct map * exports;
795 struct editable_file * file;
796 struct file_line * cur_line;
797 unsigned line_number;
798 unsigned section_start_line_number = 0;
805 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
807 return cgi_error_disabled(csp, rsp);
810 err = get_number_param(csp, parameters, "p", &patternid);
816 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
819 /* No filename specified, can't read file, modified, or out of memory. */
820 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
823 cur_line = file->lines;
825 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
827 if (cur_line->type == FILE_LINE_ACTION)
829 section_start_line_number = line_number;
831 cur_line = cur_line->next;
834 if ( (cur_line == NULL)
835 || (line_number != patternid)
837 || (cur_line->type != FILE_LINE_URL))
839 /* Invalid "patternid" parameter */
840 edit_free_file(file);
841 return JB_ERR_CGI_PARAMS;
844 if (NULL == (exports = default_exports(csp, NULL)))
846 edit_free_file(file);
847 return JB_ERR_MEMORY;
850 err = map(exports, "f", 1, stringify(file->identifier), 0);
851 if (!err) err = map(exports, "v", 1, file->version_str, 1);
852 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
853 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
854 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
856 edit_free_file(file);
864 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
868 /*********************************************************************
870 * Function : cgi_edit_actions_add_url_form
872 * Description : CGI function that displays a form for
876 * 1 : csp = Current client state (buffers, headers, etc...)
877 * 2 : rsp = http_response data structure for output
878 * 3 : parameters = map of cgi parameters
881 * f : (filename) Identifies the file to edit
882 * v : (version) File's last-modified time
883 * s : (section) Line number of section to edit
885 * Returns : JB_ERR_OK on success
886 * JB_ERR_MEMORY on out-of-memory
887 * JB_ERR_CGI_PARAMS if the CGI parameters are not
888 * specified or not valid.
890 *********************************************************************/
891 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
892 struct http_response *rsp,
893 const struct map *parameters)
902 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
904 return cgi_error_disabled(csp, rsp);
907 if (NULL == (exports = default_exports(csp, NULL)))
909 return JB_ERR_MEMORY;
912 err = map_copy_parameter_html(exports, parameters, "f");
913 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
914 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
922 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
926 /*********************************************************************
928 * Function : cgi_edit_actions_remove_url_form
930 * Description : CGI function that displays a form for
934 * 1 : csp = Current client state (buffers, headers, etc...)
935 * 2 : rsp = http_response data structure for output
936 * 3 : parameters = map of cgi parameters
939 * f : (number) The action file identifier.
940 * v : (version) File's last-modified time
941 * p : (pattern) Line number of pattern to edit
943 * Returns : JB_ERR_OK on success
944 * JB_ERR_MEMORY on out-of-memory
945 * JB_ERR_CGI_PARAMS if the CGI parameters are not
946 * specified or not valid.
948 *********************************************************************/
949 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
950 struct http_response *rsp,
951 const struct map *parameters)
953 struct map * exports;
955 struct editable_file * file;
956 struct file_line * cur_line;
957 unsigned line_number;
958 unsigned section_start_line_number = 0;
965 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
967 return cgi_error_disabled(csp, rsp);
970 err = get_number_param(csp, parameters, "p", &patternid);
976 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
979 /* No filename specified, can't read file, modified, or out of memory. */
980 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
983 cur_line = file->lines;
985 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
987 if (cur_line->type == FILE_LINE_ACTION)
989 section_start_line_number = line_number;
991 cur_line = cur_line->next;
994 if ( (cur_line == NULL)
995 || (line_number != patternid)
997 || (cur_line->type != FILE_LINE_URL))
999 /* Invalid "patternid" parameter */
1000 edit_free_file(file);
1001 return JB_ERR_CGI_PARAMS;
1004 if (NULL == (exports = default_exports(csp, NULL)))
1006 edit_free_file(file);
1007 return JB_ERR_MEMORY;
1010 err = map(exports, "f", 1, stringify(file->identifier), 0);
1011 if (!err) err = map(exports, "v", 1, file->version_str, 1);
1012 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
1013 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
1014 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
1015 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
1017 edit_free_file(file);
1025 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
1029 /*********************************************************************
1031 * Function : edit_write_file
1033 * Description : Write a complete file to disk.
1036 * 1 : file = File to write.
1038 * Returns : JB_ERR_OK on success
1039 * JB_ERR_FILE on error writing to file.
1040 * JB_ERR_MEMORY on out of memory
1042 *********************************************************************/
1043 jb_err edit_write_file(struct editable_file * file)
1046 struct file_line * cur_line;
1047 struct stat statbuf[1];
1048 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
1049 digits in time_t, assuming this is a 64-bit
1050 machine, plus null terminator, plus one
1054 assert(file->filename);
1056 if (NULL == (fp = fopen(file->filename, "wb")))
1061 cur_line = file->lines;
1062 while (cur_line != NULL)
1066 if (fputs(cur_line->raw, fp) < 0)
1074 if (cur_line->prefix)
1076 if (fputs(cur_line->prefix, fp) < 0)
1082 if (cur_line->unprocessed)
1085 if (NULL != strchr(cur_line->unprocessed, '#'))
1087 /* Must quote '#' characters */
1094 /* Count number of # characters, so we know length of output string */
1095 src = cur_line->unprocessed;
1096 while (NULL != (src = strchr(src, '#')))
1101 assert(numhash > 0);
1103 /* Allocate new memory for string */
1104 len = strlen(cur_line->unprocessed) + (size_t)numhash;
1105 if (NULL == (str = malloc(len + 1)))
1107 /* Uh oh, just trashed file! */
1109 return JB_ERR_MEMORY;
1112 /* Copy string but quote hashes */
1113 src = cur_line->unprocessed;
1121 assert(numhash >= 0);
1127 assert(numhash == 0);
1128 assert(strlen(str) == len);
1129 assert(str == dest - len);
1130 assert(src - len <= cur_line->unprocessed);
1132 if ((strlen(str) != len) || (numhash != 0))
1135 * Escaping didn't work as expected, go spread the news.
1136 * Only reached in non-debugging builds.
1138 log_error(LOG_LEVEL_ERROR,
1139 "Looks like hash escaping failed. %s might be corrupted now.",
1143 if (fputs(str, fp) < 0)
1154 /* Can write without quoting '#' characters. */
1155 if (fputs(cur_line->unprocessed, fp) < 0)
1161 if (fputs(NEWLINE(file->newline), fp) < 0)
1169 /* FIXME: Write data from file->data->whatever */
1173 cur_line = cur_line->next;
1179 /* Update the version stamp in the file structure, since we just
1180 * wrote to the file & changed it's date.
1182 if (stat(file->filename, statbuf) < 0)
1184 /* Error, probably file not found. */
1187 file->version = (unsigned)statbuf->st_mtime;
1189 /* Correct file->version_str */
1190 freez(file->version_str);
1191 snprintf(version_buf, 22, "%u", file->version);
1192 version_buf[21] = '\0';
1193 file->version_str = strdup(version_buf);
1194 if (version_buf == NULL)
1196 return JB_ERR_MEMORY;
1203 /*********************************************************************
1205 * Function : edit_free_file
1207 * Description : Free a complete file in memory.
1210 * 1 : file = Data structure to free.
1214 *********************************************************************/
1215 void edit_free_file(struct editable_file * file)
1219 /* Silently ignore NULL pointer */
1223 edit_free_file_lines(file->lines);
1224 freez(file->version_str);
1226 file->parse_error_text = NULL; /* Statically allocated */
1227 file->parse_error = NULL;
1233 /*********************************************************************
1235 * Function : edit_free_file_lines
1237 * Description : Free an entire linked list of file lines.
1240 * 1 : first_line = Data structure to free.
1244 *********************************************************************/
1245 static void edit_free_file_lines(struct file_line * first_line)
1247 struct file_line * next_line;
1249 while (first_line != NULL)
1251 next_line = first_line->next;
1252 first_line->next = NULL;
1253 freez(first_line->raw);
1254 freez(first_line->prefix);
1255 freez(first_line->unprocessed);
1256 switch(first_line->type)
1258 case 0: /* special case if memory zeroed */
1259 case FILE_LINE_UNPROCESSED:
1260 case FILE_LINE_BLANK:
1261 case FILE_LINE_ALIAS_HEADER:
1262 case FILE_LINE_SETTINGS_HEADER:
1263 case FILE_LINE_DESCRIPTION_HEADER:
1264 case FILE_LINE_DESCRIPTION_ENTRY:
1265 case FILE_LINE_ALIAS_ENTRY:
1267 /* No data is stored for these */
1270 case FILE_LINE_ACTION:
1271 free_action(first_line->data.action);
1274 case FILE_LINE_SETTINGS_ENTRY:
1275 freez(first_line->data.setting.name);
1276 freez(first_line->data.setting.svalue);
1279 /* Should never happen */
1283 first_line->type = 0; /* paranoia */
1285 first_line = next_line;
1290 /*********************************************************************
1292 * Function : match_actions_file_header_line
1294 * Description : Match an actions file {{header}} line
1297 * 1 : line = String from file
1298 * 2 : name = Header to match against
1300 * Returns : 0 iff they match.
1302 *********************************************************************/
1303 static int match_actions_file_header_line(const char * line, const char * name)
1311 if ((line[0] != '{') || (line[1] != '{'))
1317 /* Look for optional whitespace */
1318 while ( (*line == ' ') || (*line == '\t') )
1323 /* Look for the specified name (case-insensitive) */
1325 if (0 != strncmpic(line, name, len))
1331 /* Look for optional whitespace */
1332 while ( (*line == ' ') || (*line == '\t') )
1337 /* Look for "}}" and end of string*/
1338 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1348 /*********************************************************************
1350 * Function : match_actions_file_header_line
1352 * Description : Match an actions file {{header}} line
1355 * 1 : line = String from file. Must not start with
1356 * whitespace (else infinite loop!)
1357 * 2 : pname = Destination for name
1358 * 2 : pvalue = Destination for value
1360 * Returns : JB_ERR_OK on success
1361 * JB_ERR_MEMORY on out-of-memory
1362 * JB_ERR_PARSE if there's no "=" sign, or if there's
1363 * nothing before the "=" sign (but empty
1364 * values *after* the "=" sign are legal).
1366 *********************************************************************/
1367 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1369 const char * name_end;
1370 const char * value_start;
1376 assert(*line != ' ');
1377 assert(*line != '\t');
1382 value_start = strchr(line, '=');
1383 if ((value_start == NULL) || (value_start == line))
1385 return JB_ERR_PARSE;
1388 name_end = value_start - 1;
1390 /* Eat any whitespace before the '=' */
1391 while ((*name_end == ' ') || (*name_end == '\t'))
1394 * we already know we must have at least 1 non-ws char
1395 * at start of buf - no need to check
1400 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1401 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1403 return JB_ERR_MEMORY;
1405 strncpy(*pname, line, name_len);
1406 (*pname)[name_len] = '\0';
1408 /* Eat any the whitespace after the '=' */
1410 while ((*value_start == ' ') || (*value_start == '\t'))
1415 if (NULL == (*pvalue = strdup(value_start)))
1419 return JB_ERR_MEMORY;
1426 /*********************************************************************
1428 * Function : edit_parse_actions_file
1430 * Description : Parse an actions file in memory.
1432 * Passed linked list must have the "data" member
1433 * zeroed, and must contain valid "next" and
1434 * "unprocessed" fields. The "raw" and "prefix"
1435 * fields are ignored, and "type" is just overwritten.
1437 * Note that on error the file may have been
1441 * 1 : file = Actions file to be parsed in-place.
1443 * Returns : JB_ERR_OK on success
1444 * JB_ERR_MEMORY on out-of-memory
1445 * JB_ERR_PARSE on error
1447 *********************************************************************/
1448 jb_err edit_parse_actions_file(struct editable_file * file)
1450 struct file_line * cur_line;
1452 const char * text; /* Text from a line */
1453 char * name; /* For lines of the form name=value */
1454 char * value; /* For lines of the form name=value */
1455 struct action_alias * alias_list = NULL;
1456 jb_err err = JB_ERR_OK;
1458 /* alias_list contains the aliases defined in this file.
1459 * It might be better to use the "file_line.data" fields
1460 * in the relavent places instead.
1463 cur_line = file->lines;
1465 /* A note about blank line support: Blank lines should only
1466 * ever occur as the last line in the file. This function
1467 * is more forgiving than that - FILE_LINE_BLANK can occur
1471 /* Skip leading blanks. Should only happen if file is
1472 * empty (which is valid, but pointless).
1474 while ( (cur_line != NULL)
1475 && (cur_line->unprocessed[0] == '\0') )
1478 cur_line->type = FILE_LINE_BLANK;
1479 cur_line = cur_line->next;
1482 if ( (cur_line != NULL)
1483 && (cur_line->unprocessed[0] != '{') )
1485 /* File doesn't start with a header */
1486 file->parse_error = cur_line;
1487 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1488 return JB_ERR_PARSE;
1491 if ( (cur_line != NULL) && (0 ==
1492 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1494 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1496 cur_line = cur_line->next;
1497 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1499 if (cur_line->unprocessed[0])
1501 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1503 err = split_line_on_equals(cur_line->unprocessed,
1504 &cur_line->data.setting.name,
1505 &cur_line->data.setting.svalue);
1506 if (err == JB_ERR_MEMORY)
1510 else if (err != JB_ERR_OK)
1512 /* Line does not contain a name=value pair */
1513 file->parse_error = cur_line;
1514 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1515 return JB_ERR_PARSE;
1520 cur_line->type = FILE_LINE_BLANK;
1522 cur_line = cur_line->next;
1526 if ( (cur_line != NULL) && (0 ==
1527 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1529 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1531 cur_line = cur_line->next;
1532 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1534 if (cur_line->unprocessed[0])
1536 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1540 cur_line->type = FILE_LINE_BLANK;
1542 cur_line = cur_line->next;
1546 if ( (cur_line != NULL) && (0 ==
1547 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1549 cur_line->type = FILE_LINE_ALIAS_HEADER;
1551 cur_line = cur_line->next;
1552 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1554 if (cur_line->unprocessed[0])
1556 /* define an alias */
1557 struct action_alias * new_alias;
1559 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1561 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1562 if (err == JB_ERR_MEMORY)
1566 else if (err != JB_ERR_OK)
1568 /* Line does not contain a name=value pair */
1569 file->parse_error = cur_line;
1570 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1571 return JB_ERR_PARSE;
1574 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1579 free_alias_list(alias_list);
1580 return JB_ERR_MEMORY;
1583 err = get_actions(value, alias_list, new_alias->action);
1586 /* Invalid action or out of memory */
1590 free_alias_list(alias_list);
1591 if (err == JB_ERR_MEMORY)
1597 /* Line does not contain a name=value pair */
1598 file->parse_error = cur_line;
1599 file->parse_error_text = "This alias does not specify a valid set of actions.";
1600 return JB_ERR_PARSE;
1606 new_alias->name = name;
1609 new_alias->next = alias_list;
1610 alias_list = new_alias;
1614 cur_line->type = FILE_LINE_BLANK;
1616 cur_line = cur_line->next;
1620 /* Header done, process the main part of the file */
1621 while (cur_line != NULL)
1623 /* At this point, (cur_line->unprocessed[0] == '{') */
1624 assert(cur_line->unprocessed[0] == '{');
1625 text = cur_line->unprocessed + 1;
1626 len = strlen(text) - 1;
1627 if (text[len] != '}')
1629 /* No closing } on header */
1630 free_alias_list(alias_list);
1631 file->parse_error = cur_line;
1632 file->parse_error_text = "Headers starting with '{' must have a "
1633 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1634 "must close with two brackets ('}}').";
1635 return JB_ERR_PARSE;
1640 /* An invalid {{ header. */
1641 free_alias_list(alias_list);
1642 file->parse_error = cur_line;
1643 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1644 "Please remember that the system (two-bracket) headers must "
1645 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1646 "and must appear before any actions (one-bracket) headers. "
1647 "Also note that system headers may not be repeated.";
1648 return JB_ERR_PARSE;
1651 while ( (*text == ' ') || (*text == '\t') )
1657 && ( (text[len - 1] == ' ')
1658 || (text[len - 1] == '\t') ) )
1663 cur_line->type = FILE_LINE_ACTION;
1665 /* Remove {} and make copy */
1666 if (NULL == (value = (char *) malloc(len + 1)))
1669 free_alias_list(alias_list);
1670 return JB_ERR_MEMORY;
1672 strncpy(value, text, len);
1676 err = get_actions(value, alias_list, cur_line->data.action);
1679 /* Invalid action or out of memory */
1681 free_alias_list(alias_list);
1682 if (err == JB_ERR_MEMORY)
1688 /* Line does not contain a name=value pair */
1689 file->parse_error = cur_line;
1690 file->parse_error_text = "This header does not specify a valid set of actions.";
1691 return JB_ERR_PARSE;
1695 /* Done with string - it was clobbered anyway */
1698 /* Process next line */
1699 cur_line = cur_line->next;
1701 /* Loop processing URL patterns */
1702 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1704 if (cur_line->unprocessed[0])
1706 /* Could parse URL here, but this isn't currently needed */
1708 cur_line->type = FILE_LINE_URL;
1712 cur_line->type = FILE_LINE_BLANK;
1714 cur_line = cur_line->next;
1716 } /* End main while(cur_line != NULL) loop */
1718 free_alias_list(alias_list);
1724 /*********************************************************************
1726 * Function : edit_read_file_lines
1728 * Description : Read all the lines of a file into memory.
1729 * Handles whitespace, comments and line continuation.
1732 * 1 : fp = File to read from. On return, this will be
1733 * at EOF but it will not have been closed.
1734 * 2 : pfile = Destination for a linked list of file_lines.
1735 * Will be set to NULL on error.
1736 * 3 : newline = How to handle newlines.
1738 * Returns : JB_ERR_OK on success
1739 * JB_ERR_MEMORY on out-of-memory
1741 *********************************************************************/
1742 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1744 struct file_line * first_line; /* Keep for return value or to free */
1745 struct file_line * cur_line; /* Current line */
1746 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1754 cur_line = first_line = zalloc(sizeof(struct file_line));
1755 if (cur_line == NULL)
1757 return JB_ERR_MEMORY;
1760 cur_line->type = FILE_LINE_UNPROCESSED;
1762 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1765 /* Out of memory or empty file. */
1766 /* Note that empty file is not an error we propogate up */
1768 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1773 prev_line = cur_line;
1774 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1775 if (cur_line == NULL)
1778 edit_free_file_lines(first_line);
1779 return JB_ERR_MEMORY;
1782 cur_line->type = FILE_LINE_UNPROCESSED;
1784 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1785 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1788 edit_free_file_lines(first_line);
1789 return JB_ERR_MEMORY;
1793 while (rval != JB_ERR_FILE);
1797 /* We allocated one too many - free it */
1798 prev_line->next = NULL;
1801 *pfile = first_line;
1806 /*********************************************************************
1808 * Function : edit_read_file
1810 * Description : Read a complete file into memory.
1811 * Handles CGI parameter parsing. If requested, also
1812 * checks the file's modification timestamp.
1815 * 1 : csp = Current client state (buffers, headers, etc...)
1816 * 2 : parameters = map of cgi parameters.
1817 * 3 : require_version = true to check "ver" parameter.
1818 * 4 : pfile = Destination for the file. Will be set
1822 * f : The action file identifier.
1823 * ver : (Only if require_version is nonzero)
1824 * Timestamp of the actions file. If wrong, this
1825 * function fails with JB_ERR_MODIFIED.
1827 * Returns : JB_ERR_OK on success
1828 * JB_ERR_MEMORY on out-of-memory
1829 * JB_ERR_CGI_PARAMS if "filename" was not specified
1831 * JB_ERR_FILE if the file cannot be opened or
1833 * JB_ERR_MODIFIED if version checking was requested and
1834 * failed - the file was modified outside
1835 * of this CGI editor instance.
1837 *********************************************************************/
1838 jb_err edit_read_file(struct client_state *csp,
1839 const struct map *parameters,
1840 int require_version,
1841 struct editable_file **pfile)
1843 struct file_line * lines;
1846 const char * filename = NULL;
1847 struct editable_file * file;
1848 unsigned version = 0;
1849 struct stat statbuf[1];
1850 char version_buf[22];
1851 int newline = NEWLINE_UNKNOWN;
1860 if ((JB_ERR_OK == get_number_param(csp, parameters, "f", &i))
1861 && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1863 filename = csp->config->actions_file[i];
1866 if (filename == NULL || stat(filename, statbuf) < 0)
1868 /* Error, probably file not found. */
1871 version = (unsigned) statbuf->st_mtime;
1873 if (require_version)
1875 unsigned specified_version;
1876 err = get_number_param(csp, parameters, "v", &specified_version);
1882 if (version != specified_version)
1884 return JB_ERR_MODIFIED;
1888 if (NULL == (fp = fopen(filename,"rb")))
1893 err = edit_read_file_lines(fp, &lines, &newline);
1902 file = (struct editable_file *) zalloc(sizeof(*file));
1905 edit_free_file_lines(lines);
1909 file->lines = lines;
1910 file->newline = newline;
1911 file->filename = filename;
1912 file->version = version;
1913 file->identifier = i;
1915 /* Correct file->version_str */
1916 freez(file->version_str);
1917 snprintf(version_buf, 22, "%u", file->version);
1918 version_buf[21] = '\0';
1919 file->version_str = strdup(version_buf);
1920 if (version_buf == NULL)
1922 edit_free_file(file);
1923 return JB_ERR_MEMORY;
1931 /*********************************************************************
1933 * Function : edit_read_actions_file
1935 * Description : Read a complete actions file into memory.
1936 * Handles CGI parameter parsing. If requested, also
1937 * checks the file's modification timestamp.
1939 * If this function detects an error in the categories
1940 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1941 * then it handles it by filling in the specified
1942 * response structure and returning JB_ERR_FILE.
1945 * 1 : csp = Current client state (buffers, headers, etc...)
1946 * 2 : rsp = HTTP response. Only filled in on error.
1947 * 2 : parameters = map of cgi parameters.
1948 * 3 : require_version = true to check "ver" parameter.
1949 * 4 : pfile = Destination for the file. Will be set
1953 * f : The actions file identifier.
1954 * ver : (Only if require_version is nonzero)
1955 * Timestamp of the actions file. If wrong, this
1956 * function fails with JB_ERR_MODIFIED.
1958 * Returns : JB_ERR_OK on success
1959 * JB_ERR_MEMORY on out-of-memory
1960 * JB_ERR_CGI_PARAMS if "filename" was not specified
1962 * JB_ERR_FILE if the file does not contain valid data,
1963 * or if file cannot be opened or
1964 * contains no data, or if version
1965 * checking was requested and failed.
1967 *********************************************************************/
1968 jb_err edit_read_actions_file(struct client_state *csp,
1969 struct http_response *rsp,
1970 const struct map *parameters,
1971 int require_version,
1972 struct editable_file **pfile)
1975 struct editable_file *file;
1983 err = edit_read_file(csp, parameters, require_version, &file);
1986 /* Try to handle if possible */
1987 if (err == JB_ERR_FILE)
1989 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1991 else if (err == JB_ERR_MODIFIED)
1993 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1995 if (err == JB_ERR_OK)
1998 * Signal to higher-level CGI code that there was a problem but we
1999 * handled it, they should just return JB_ERR_OK.
2006 err = edit_parse_actions_file(file);
2009 if (err == JB_ERR_PARSE)
2011 err = cgi_error_parse(csp, rsp, file);
2012 if (err == JB_ERR_OK)
2015 * Signal to higher-level CGI code that there was a problem but we
2016 * handled it, they should just return JB_ERR_OK.
2021 edit_free_file(file);
2032 * Currently not needed, but may become useful again in the future.
2034 /*********************************************************************
2036 * Function : get_file_name_param
2038 * Description : Get the name of the file to edit from the parameters
2039 * passed to a CGI function. This function handles
2040 * security checks such as blocking urls containing
2041 * "/" or ".", prepending the config file directory,
2042 * and adding the specified suffix.
2044 * (This is an essential security check, otherwise
2045 * users may be able to pass "../../../etc/passwd"
2046 * and overwrite the password file [linux], "prn:"
2047 * and print random data [Windows], etc...)
2049 * This function only allows filenames contining the
2050 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
2051 * That's probably too restrictive but at least it's
2055 * 1 : csp = Current client state (buffers, headers, etc...)
2056 * 2 : parameters = map of cgi parameters
2057 * 3 : param_name = The name of the parameter to read
2058 * 4 : suffix = File extension, e.g. ".actions"
2059 * 5 : pfilename = destination for full filename. Caller
2060 * free()s. Set to NULL on error.
2061 * 6 : pparam = destination for partial filename,
2062 * suitable for use in another URL. Allocated as part
2063 * of the map "parameters", so don't free it.
2064 * Set to NULL if not specified.
2066 * Returns : JB_ERR_OK on success
2067 * JB_ERR_MEMORY on out-of-memory
2068 * JB_ERR_CGI_PARAMS if "filename" was not specified
2071 *********************************************************************/
2072 static jb_err get_file_name_param(struct client_state *csp,
2073 const struct map *parameters,
2074 const char *param_name,
2077 const char **pparam)
2095 param = lookup(parameters, param_name);
2098 return JB_ERR_CGI_PARAMS;
2103 len = strlen(param);
2104 if (len >= FILENAME_MAX)
2107 return JB_ERR_CGI_PARAMS;
2110 /* Check every character to see if it's legal */
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 = malloc(len + strlen(suffix) + 1);
2129 return JB_ERR_MEMORY;
2131 strcpy(name, param);
2132 strcpy(name + len, suffix);
2135 fullpath = make_path(csp->config->confdir, name);
2138 if (fullpath == NULL)
2140 return JB_ERR_MEMORY;
2144 *pfilename = fullpath;
2151 /*********************************************************************
2153 * Function : get_url_spec_param
2155 * Description : Get a URL pattern from the parameters
2156 * passed to a CGI function. Removes leading/trailing
2157 * spaces and validates it.
2160 * 1 : csp = Current client state (buffers, headers, etc...)
2161 * 2 : parameters = map of cgi parameters
2162 * 3 : name = Name of CGI parameter to read
2163 * 4 : pvalue = destination for value. Will be malloc()'d.
2164 * Set to NULL on error.
2166 * Returns : JB_ERR_OK on success
2167 * JB_ERR_MEMORY on out-of-memory
2168 * JB_ERR_CGI_PARAMS if the parameter was not specified
2171 *********************************************************************/
2172 static jb_err get_url_spec_param(struct client_state *csp,
2173 const struct map *parameters,
2177 const char *orig_param;
2180 struct url_spec compiled[1];
2190 orig_param = lookup(parameters, name);
2193 return JB_ERR_CGI_PARAMS;
2196 /* Copy and trim whitespace */
2197 param = strdup(orig_param);
2200 return JB_ERR_MEMORY;
2204 /* Must be non-empty, and can't allow 1st character to be '{' */
2205 if (param[0] == '\0' || param[0] == '{')
2208 return JB_ERR_CGI_PARAMS;
2211 /* Check for embedded newlines */
2212 for (s = param; *s != '\0'; s++)
2214 if ((*s == '\r') || (*s == '\n'))
2217 return JB_ERR_CGI_PARAMS;
2221 /* Check that regex is valid */
2226 return JB_ERR_MEMORY;
2228 err = create_url_spec(compiled, s);
2233 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2235 free_url_spec(compiled);
2237 if (param[strlen(param) - 1] == '\\')
2240 * Must protect trailing '\\' from becoming line continuation character.
2241 * Two methods: 1) If it's a domain only, add a trailing '/'.
2242 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2244 if (strchr(param, '/') == NULL)
2246 err = string_append(¶m, "/");
2250 err = string_append(¶m, "(?:)");
2257 /* Check that the modified regex is valid */
2262 return JB_ERR_MEMORY;
2264 err = create_url_spec(compiled, s);
2269 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2271 free_url_spec(compiled);
2278 /*********************************************************************
2280 * Function : map_radio
2282 * Description : Map a set of radio button values. E.g. if you have
2283 * 3 radio buttons, declare them as:
2284 * <option type="radio" name="xyz" @xyz-a@>
2285 * <option type="radio" name="xyz" @xyz-b@>
2286 * <option type="radio" name="xyz" @xyz-c@>
2287 * Then map one of the @xyz-?@ variables to "checked"
2288 * and all the others to empty by calling:
2289 * map_radio(exports, "xyz", "abc", sel)
2290 * Where 'sel' is 'a', 'b', or 'c'.
2293 * 1 : exports = Exports map to modify.
2294 * 2 : optionname = name for map
2295 * 3 : values = null-terminated list of values;
2296 * 4 : value = Selected value.
2298 * CGI Parameters : None
2300 * Returns : JB_ERR_OK on success
2301 * JB_ERR_MEMORY on out-of-memory
2303 *********************************************************************/
2304 static jb_err map_radio(struct map * exports,
2305 const char * optionname,
2306 const char * values,
2318 len = strlen(optionname);
2319 buf = malloc(len + 3);
2322 return JB_ERR_MEMORY;
2325 strcpy(buf, optionname);
2330 while ((c = *values++) != '\0')
2335 if (map(exports, buf, 1, "", 1))
2337 return JB_ERR_MEMORY;
2343 return map(exports, buf, 0, "checked", 1);
2347 /*********************************************************************
2349 * Function : cgi_error_modified
2351 * Description : CGI function that is called when a file is modified
2352 * outside the CGI editor.
2355 * 1 : csp = Current client state (buffers, headers, etc...)
2356 * 2 : rsp = http_response data structure for output
2357 * 3 : filename = The file that was modified.
2359 * CGI Parameters : none
2361 * Returns : JB_ERR_OK on success
2362 * JB_ERR_MEMORY on out-of-memory error.
2364 *********************************************************************/
2365 jb_err cgi_error_modified(struct client_state *csp,
2366 struct http_response *rsp,
2367 const char *filename)
2369 struct map *exports;
2376 if (NULL == (exports = default_exports(csp, NULL)))
2378 return JB_ERR_MEMORY;
2381 err = map(exports, "f", 1, html_encode(filename), 0);
2388 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2392 /*********************************************************************
2394 * Function : cgi_error_parse
2396 * Description : CGI function that is called when a file cannot
2397 * be parsed by the CGI editor.
2400 * 1 : csp = Current client state (buffers, headers, etc...)
2401 * 2 : rsp = http_response data structure for output
2402 * 3 : file = The file that was modified.
2404 * CGI Parameters : none
2406 * Returns : JB_ERR_OK on success
2407 * JB_ERR_MEMORY on out-of-memory error.
2409 *********************************************************************/
2410 jb_err cgi_error_parse(struct client_state *csp,
2411 struct http_response *rsp,
2412 struct editable_file *file)
2414 struct map *exports;
2416 struct file_line *cur_line;
2422 if (NULL == (exports = default_exports(csp, NULL)))
2424 return JB_ERR_MEMORY;
2427 err = map(exports, "f", 1, stringify(file->identifier), 0);
2428 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2430 cur_line = file->parse_error;
2433 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2434 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2442 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2446 /*********************************************************************
2448 * Function : cgi_error_file
2450 * Description : CGI function that is called when a file cannot be
2451 * opened by the CGI editor.
2454 * 1 : csp = Current client state (buffers, headers, etc...)
2455 * 2 : rsp = http_response data structure for output
2456 * 3 : filename = The file that was modified.
2458 * CGI Parameters : none
2460 * Returns : JB_ERR_OK on success
2461 * JB_ERR_MEMORY on out-of-memory error.
2463 *********************************************************************/
2464 jb_err cgi_error_file(struct client_state *csp,
2465 struct http_response *rsp,
2466 const char *filename)
2468 struct map *exports;
2475 if (NULL == (exports = default_exports(csp, NULL)))
2477 return JB_ERR_MEMORY;
2480 err = map(exports, "f", 1, html_encode(filename), 0);
2487 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2491 /*********************************************************************
2493 * Function : cgi_error_file_read_only
2495 * Description : CGI function that is called when a file cannot be
2496 * opened for writing by the CGI editor.
2499 * 1 : csp = Current client state (buffers, headers, etc...)
2500 * 2 : rsp = http_response data structure for output
2501 * 3 : filename = The file that we can't write to
2503 * CGI Parameters : none
2505 * Returns : JB_ERR_OK on success
2506 * JB_ERR_MEMORY on out-of-memory error.
2508 *********************************************************************/
2509 jb_err cgi_error_file_read_only(struct client_state *csp,
2510 struct http_response *rsp,
2511 const char *filename)
2513 struct map *exports;
2520 if (NULL == (exports = default_exports(csp, NULL)))
2522 return JB_ERR_MEMORY;
2525 err = map(exports, "f", 1, html_encode(filename), 0);
2532 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2536 /*********************************************************************
2538 * Function : cgi_edit_actions
2540 * Description : CGI function that allows the user to choose which
2541 * actions file to edit.
2544 * 1 : csp = Current client state (buffers, headers, etc...)
2545 * 2 : rsp = http_response data structure for output
2546 * 3 : parameters = map of cgi parameters
2548 * CGI Parameters : None
2550 * Returns : JB_ERR_OK on success
2551 * JB_ERR_MEMORY on out-of-memory error
2553 *********************************************************************/
2554 jb_err cgi_edit_actions(struct client_state *csp,
2555 struct http_response *rsp,
2556 const struct map *parameters)
2559 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2561 return cgi_error_disabled(csp, rsp);
2564 /* FIXME: Incomplete */
2566 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2571 /*********************************************************************
2573 * Function : cgi_edit_actions_list
2575 * Description : CGI function that edits the actions list.
2576 * FIXME: This function shouldn't FATAL ever.
2577 * FIXME: This function doesn't check the retval of map()
2579 * 1 : csp = Current client state (buffers, headers, etc...)
2580 * 2 : rsp = http_response data structure for output
2581 * 3 : parameters = map of cgi parameters
2583 * CGI Parameters : filename
2585 * Returns : JB_ERR_OK on success
2586 * JB_ERR_MEMORY on out-of-memory
2587 * JB_ERR_FILE if the file cannot be opened or
2589 * JB_ERR_CGI_PARAMS if "filename" was not specified
2592 *********************************************************************/
2593 jb_err cgi_edit_actions_list(struct client_state *csp,
2594 struct http_response *rsp,
2595 const struct map *parameters)
2597 char * section_template;
2598 char * url_template;
2603 struct map * exports;
2604 struct map * section_exports;
2605 struct map * url_exports;
2606 struct editable_file * file;
2607 struct file_line * cur_line;
2608 unsigned line_number = 0;
2609 unsigned prev_section_line_number = ((unsigned) (-1));
2611 struct file_list * fl;
2612 struct url_actions * b;
2613 char * buttons = NULL;
2616 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2618 return cgi_error_disabled(csp, rsp);
2621 if (NULL == (exports = default_exports(csp, NULL)))
2623 return JB_ERR_MEMORY;
2626 /* Load actions file */
2627 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2630 /* No filename specified, can't read file, or out of memory. */
2631 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2634 /* Find start of actions in file */
2635 cur_line = file->lines;
2637 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2639 cur_line = cur_line->next;
2644 * Conventional actions files should have a match all block
2646 * cur_line = {...global actions...}
2647 * cur_line->next = /
2648 * cur_line->next->next = {...actions...} or EOF
2650 if ( (cur_line != NULL)
2651 && (cur_line->type == FILE_LINE_ACTION)
2652 && (cur_line->next != NULL)
2653 && (cur_line->next->type == FILE_LINE_URL)
2654 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2655 && ( (cur_line->next->next == NULL)
2656 || (cur_line->next->next->type != FILE_LINE_URL)
2660 * Generate string with buttons to set actions for "/" to
2661 * any predefined set of actions (named standard.*, probably
2662 * residing in standard.action).
2665 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2668 edit_free_file(file);
2670 if (err == JB_ERR_FILE)
2672 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2677 err = template_fill(§ion_template, exports);
2680 edit_free_file(file);
2685 buttons = strdup("");
2686 for (i = 0; i < MAX_AF_FILES; i++)
2688 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2690 for (b = b->next; NULL != b; b = b->next)
2692 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2694 if (err || (NULL == (section_exports = new_map())))
2697 free(section_template);
2698 edit_free_file(file);
2700 return JB_ERR_MEMORY;
2703 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2705 if (err || (NULL == (s = strdup(section_template))))
2707 free_map(section_exports);
2709 free(section_template);
2710 edit_free_file(file);
2712 return JB_ERR_MEMORY;
2715 if (!err) err = template_fill(&s, section_exports);
2716 free_map(section_exports);
2717 if (!err) err = string_join(&buttons, s);
2722 freez(section_template);
2723 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2726 * Conventional actions file, supply extra editing help.
2727 * (e.g. don't allow them to make it an unconventional one).
2729 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2731 snprintf(buf, 150, "%d", line_number);
2732 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2733 snprintf(buf, 150, "%d", line_number + 2);
2734 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2735 if (!err) err = map(exports, "all-urls-actions", 1,
2736 actions_to_html(csp, cur_line->data.action), 0);
2738 /* Skip the 2 lines */
2739 cur_line = cur_line->next->next;
2743 * Note that prev_section_line_number is NOT set here.
2744 * This is deliberate and not a bug. It stops a "Move up"
2745 * option appearing on the next section. Clicking "Move
2746 * up" would make the actions file unconventional, which
2747 * we don't want, so we hide this option.
2753 * Non-standard actions file - does not begin with
2754 * the "All URLs" section.
2756 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2759 /* Set up global exports */
2761 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2762 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2763 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2765 /* Discourage private additions to default.action */
2767 if (!err) err = map_conditional(exports, "default-action",
2768 (strstr("default.action", file->filename) != NULL));
2771 edit_free_file(file);
2776 /* Should do all global exports above this point */
2778 /* Load templates */
2780 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2783 edit_free_file(file);
2785 if (err == JB_ERR_FILE)
2787 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2792 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2795 free(section_template);
2796 edit_free_file(file);
2798 if (err == JB_ERR_FILE)
2800 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2805 err = template_fill(§ion_template, exports);
2809 edit_free_file(file);
2815 err = template_fill(&url_template, exports);
2818 free(section_template);
2819 edit_free_file(file);
2824 if (NULL == (sections = strdup("")))
2826 free(section_template);
2828 edit_free_file(file);
2830 return JB_ERR_MEMORY;
2833 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2835 if (NULL == (section_exports = new_map()))
2838 free(section_template);
2840 edit_free_file(file);
2842 return JB_ERR_MEMORY;
2845 snprintf(buf, 150, "%d", line_number);
2846 err = map(section_exports, "s", 1, buf, 1);
2847 if (!err) err = map(section_exports, "actions", 1,
2848 actions_to_html(csp, cur_line->data.action), 0);
2851 && (cur_line->next != NULL)
2852 && (cur_line->next->type == FILE_LINE_URL))
2854 /* This section contains at least one URL, don't allow delete */
2855 err = map_block_killer(section_exports, "empty-section");
2859 if (!err) err = map_block_keep(section_exports, "empty-section");
2862 if (prev_section_line_number != ((unsigned)(-1)))
2864 /* Not last section */
2865 snprintf(buf, 150, "%d", prev_section_line_number);
2866 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2867 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2872 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2874 prev_section_line_number = line_number;
2879 free(section_template);
2881 edit_free_file(file);
2883 free_map(section_exports);
2887 /* Should do all section-specific exports above this point */
2889 if (NULL == (urls = strdup("")))
2892 free(section_template);
2894 edit_free_file(file);
2896 free_map(section_exports);
2897 return JB_ERR_MEMORY;
2902 cur_line = cur_line->next;
2905 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2907 if (NULL == (url_exports = new_map()))
2911 free(section_template);
2913 edit_free_file(file);
2915 free_map(section_exports);
2916 return JB_ERR_MEMORY;
2919 snprintf(buf, 150, "%d", line_number);
2920 err = map(url_exports, "p", 1, buf, 1);
2922 snprintf(buf, 150, "%d", url_1_2);
2923 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2925 if (!err) err = map(url_exports, "url-html", 1,
2926 html_encode(cur_line->unprocessed), 0);
2927 if (!err) err = map(url_exports, "url", 1,
2928 url_encode(cur_line->unprocessed), 0);
2934 free(section_template);
2936 edit_free_file(file);
2938 free_map(section_exports);
2939 free_map(url_exports);
2943 if (NULL == (s = strdup(url_template)))
2947 free(section_template);
2949 edit_free_file(file);
2951 free_map(section_exports);
2952 free_map(url_exports);
2953 return JB_ERR_MEMORY;
2956 err = template_fill(&s, section_exports);
2957 if (!err) err = template_fill(&s, url_exports);
2958 if (!err) err = string_append(&urls, s);
2960 free_map(url_exports);
2967 free(section_template);
2969 edit_free_file(file);
2971 free_map(section_exports);
2975 url_1_2 = 3 - url_1_2;
2977 cur_line = cur_line->next;
2981 err = map(section_exports, "urls", 1, urls, 0);
2983 /* Could also do section-specific exports here, but it wouldn't be as fast */
2985 snprintf(buf, 150, "%d", line_number);
2986 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2988 if ( (cur_line != NULL)
2989 && (cur_line->type == FILE_LINE_ACTION))
2991 /* Not last section */
2992 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2997 if (!err) err = map_block_killer(section_exports, "s-next-exists");
3003 free(section_template);
3005 edit_free_file(file);
3007 free_map(section_exports);
3011 if (NULL == (s = strdup(section_template)))
3014 free(section_template);
3016 edit_free_file(file);
3018 free_map(section_exports);
3019 return JB_ERR_MEMORY;
3022 err = template_fill(&s, section_exports);
3023 if (!err) err = string_append(§ions, s);
3026 free_map(section_exports);
3031 free(section_template);
3033 edit_free_file(file);
3039 edit_free_file(file);
3040 free(section_template);
3043 err = map(exports, "sections", 1, sections, 0);
3050 /* Could also do global exports here, but it wouldn't be as fast */
3052 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
3056 /*********************************************************************
3058 * Function : cgi_edit_actions_for_url
3060 * Description : CGI function that edits the Actions list.
3063 * 1 : csp = Current client state (buffers, headers, etc...)
3064 * 2 : rsp = http_response data structure for output
3065 * 3 : parameters = map of cgi parameters
3067 * CGI Parameters : None
3069 * Returns : JB_ERR_OK on success
3070 * JB_ERR_MEMORY on out-of-memory
3071 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3072 * specified or not valid.
3074 *********************************************************************/
3075 jb_err cgi_edit_actions_for_url(struct client_state *csp,
3076 struct http_response *rsp,
3077 const struct map *parameters)
3079 struct map * exports;
3081 struct editable_file * file;
3082 struct file_line * cur_line;
3083 unsigned line_number;
3085 struct re_filterfile_spec *filter_group;
3086 int i, have_filters = 0;
3088 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3090 return cgi_error_disabled(csp, rsp);
3093 err = get_number_param(csp, parameters, "s", §ionid);
3099 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3102 /* No filename specified, can't read file, modified, or out of memory. */
3103 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3106 cur_line = file->lines;
3108 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3110 cur_line = cur_line->next;
3113 if ( (cur_line == NULL)
3114 || (line_number != sectionid)
3116 || (cur_line->type != FILE_LINE_ACTION))
3118 /* Invalid "sectionid" parameter */
3119 edit_free_file(file);
3120 return JB_ERR_CGI_PARAMS;
3123 if (NULL == (exports = default_exports(csp, NULL)))
3125 edit_free_file(file);
3126 return JB_ERR_MEMORY;
3129 err = map(exports, "f", 1, stringify(file->identifier), 0);
3130 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3131 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3133 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3136 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3137 * length limitation and ignore clicks on the Submit buttons if
3138 * the resulting GET URL would be longer than their limit.
3140 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3141 * reached this limit and action editing stopped working in these
3142 * browsers (BR #1570678).
3144 * The config option split-large-forms works around this browser
3145 * bug (HTTP has no URL lenght limitation) by deviding the action
3146 * list form into multiple smaller ones. It means the URLs are shorter
3147 * and work in broken browsers as well, but the user can no longer change
3148 * all actions with one submit.
3150 * A better solution would be to switch to POST requests,
3151 * but this will do for now.
3153 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3155 /* Generate multiple smaller form by killing the big one. */
3156 err = map_block_killer(exports, "one-form-only");
3160 /* Default: Generate one large form by killing the smaller ones. */
3161 err = map_block_killer(exports, "multiple-forms");
3164 for (i = 0; i < MAX_AF_FILES; i++)
3166 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3168 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3176 edit_free_file(file);
3181 if (0 == have_filters)
3183 err = map(exports, "filter-params", 1, "", 1);
3188 * List available filters and their settings.
3190 char *filter_template;
3191 int filter_identifier = 0;
3192 char *prepared_templates[MAX_FILTER_TYPES];
3194 for (i = 0; i < MAX_FILTER_TYPES; i++)
3196 prepared_templates[i] = strdup("");
3199 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3202 edit_free_file(file);
3204 if (err == JB_ERR_FILE)
3206 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3211 err = template_fill(&filter_template, exports);
3213 for (i = 0; i < MAX_AF_FILES; i++)
3215 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3217 filter_group = csp->rlist[i]->f;
3218 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3220 char current_mode = 'x';
3222 struct list_entry *filter_name;
3223 struct map *line_exports;
3224 const int type = filter_group->type;
3225 const int multi_action_index = filter_type_info[type].multi_action_index;
3227 assert(type < MAX_FILTER_TYPES);
3229 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
3230 while ((filter_name != NULL)
3231 && (0 != strcmp(filter_group->name, filter_name->str)))
3233 filter_name = filter_name->next;
3236 if (filter_name != NULL)
3242 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
3243 while ((filter_name != NULL)
3244 && (0 != strcmp(filter_group->name, filter_name->str)))
3246 filter_name = filter_name->next;
3248 if (filter_name != NULL)
3254 /* Generate a unique serial number */
3255 snprintf(number, sizeof(number), "%x", filter_identifier++);
3256 number[sizeof(number) - 1] = '\0';
3258 line_exports = new_map();
3259 if (line_exports == NULL)
3261 err = JB_ERR_MEMORY;
3267 if (!err) err = map(line_exports, "index", 1, number, 1);
3268 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3269 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3270 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3271 if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
3272 if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
3273 if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
3277 filter_line = strdup(filter_template);
3278 if (filter_line == NULL) err = JB_ERR_MEMORY;
3280 if (!err) err = template_fill(&filter_line, line_exports);
3281 string_join(&prepared_templates[type], filter_line);
3283 free_map(line_exports);
3288 freez(filter_template);
3290 /* Replace all filter macros with the aggregated templates */
3291 for (i = 0; i < MAX_FILTER_TYPES; i++)
3294 err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
3299 /* Free aggregated templates */
3300 for (i = 0; i < MAX_FILTER_TYPES; i++)
3302 freez(prepared_templates[i]);
3307 if (!err) err = map_radio(exports, "filter-all", "nx",
3308 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3310 edit_free_file(file);
3318 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3322 /*********************************************************************
3324 * Function : cgi_edit_actions_submit
3326 * Description : CGI function that actually edits the Actions list.
3329 * 1 : csp = Current client state (buffers, headers, etc...)
3330 * 2 : rsp = http_response data structure for output
3331 * 3 : parameters = map of cgi parameters
3333 * CGI Parameters : None
3335 * Returns : JB_ERR_OK on success
3336 * JB_ERR_MEMORY on out-of-memory
3337 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3338 * specified or not valid.
3340 *********************************************************************/
3341 jb_err cgi_edit_actions_submit(struct client_state *csp,
3342 struct http_response *rsp,
3343 const struct map *parameters)
3349 struct editable_file * file;
3350 struct file_line * cur_line;
3351 unsigned line_number;
3354 int filter_identifier;
3355 const char * action_set_name;
3357 struct file_list * fl;
3358 struct url_actions * b;
3360 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3362 return cgi_error_disabled(csp, rsp);
3365 err = get_number_param(csp, parameters, "s", §ionid);
3371 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3374 /* No filename specified, can't read file, modified, or out of memory. */
3375 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3378 cur_line = file->lines;
3380 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3382 cur_line = cur_line->next;
3385 if ( (cur_line == NULL)
3386 || (line_number != sectionid)
3388 || (cur_line->type != FILE_LINE_ACTION))
3390 /* Invalid "sectionid" parameter */
3391 edit_free_file(file);
3392 return JB_ERR_CGI_PARAMS;
3395 get_string_param(parameters, "p", &action_set_name);
3396 if (action_set_name != NULL)
3398 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3400 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3402 for (b = b->next; NULL != b; b = b->next)
3404 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3406 copy_action(cur_line->data.action, b->action);
3412 edit_free_file(file);
3413 return JB_ERR_CGI_PARAMS;
3419 err = actions_from_radio(parameters, cur_line->data.action);
3425 edit_free_file(file);
3429 ch = get_char_param(parameters, "filter_all");
3432 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3433 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3434 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3438 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3441 for (filter_identifier = 0; !err; filter_identifier++)
3448 * Filter state. Valid states are: 'Y' (active),
3449 * 'N' (inactive) and 'X' (no change).
3453 * Abbreviated filter type. Valid types are: 'F' (content filter),
3454 * 'S' (server-header filter) and 'C' (client-header filter).
3456 int multi_action_index = 0;
3458 /* Generate the keys */
3459 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3460 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3461 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3462 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3463 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3465 err = get_string_param(parameters, key_name, &name);
3474 type = get_char_param(parameters, key_type);
3478 multi_action_index = ACTION_MULTI_FILTER;
3481 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3484 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3487 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
3490 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
3493 log_error(LOG_LEVEL_ERROR,
3494 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3497 assert(multi_action_index);
3499 value = get_char_param(parameters, key_value);
3502 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3503 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3504 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3506 else if (value == 'N')
3508 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3509 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3511 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3512 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3515 else if (value == 'X')
3517 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3518 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3525 edit_free_file(file);
3529 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3532 edit_free_file(file);
3533 return JB_ERR_MEMORY;
3536 len = strlen(actiontext);
3540 * Empty action - must special-case this.
3541 * Simply setting len to 1 is sufficient...
3546 if (NULL == (newtext = malloc(len + 2)))
3550 edit_free_file(file);
3551 return JB_ERR_MEMORY;
3553 strcpy(newtext, actiontext);
3557 newtext[len + 1] = '\0';
3559 freez(cur_line->raw);
3560 freez(cur_line->unprocessed);
3561 cur_line->unprocessed = newtext;
3563 err = edit_write_file(file);
3566 /* Error writing file */
3567 if (err == JB_ERR_FILE)
3569 /* Read-only file. */
3570 err = cgi_error_file_read_only(csp, rsp, file->filename);
3572 edit_free_file(file);
3576 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3577 (long) time(NULL), file->identifier, sectionid);
3579 edit_free_file(file);
3581 return cgi_redirect(rsp, target);
3585 /*********************************************************************
3587 * Function : cgi_edit_actions_url
3589 * Description : CGI function that actually edits a URL pattern in
3593 * 1 : csp = Current client state (buffers, headers, etc...)
3594 * 2 : rsp = http_response data structure for output
3595 * 3 : parameters = map of cgi parameters
3598 * filename : Identifies the file to edit
3599 * ver : File's last-modified time
3600 * section : Line number of section to edit
3601 * pattern : Line number of pattern to edit
3602 * newval : New value for pattern
3604 * Returns : JB_ERR_OK on success
3605 * JB_ERR_MEMORY on out-of-memory
3606 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3607 * specified or not valid.
3609 *********************************************************************/
3610 jb_err cgi_edit_actions_url(struct client_state *csp,
3611 struct http_response *rsp,
3612 const struct map *parameters)
3616 struct editable_file * file;
3617 struct file_line * cur_line;
3618 unsigned line_number;
3619 unsigned section_start_line_number = 0;
3627 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3629 return cgi_error_disabled(csp, rsp);
3632 err = get_number_param(csp, parameters, "p", &patternid);
3639 return JB_ERR_CGI_PARAMS;
3642 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3648 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3651 /* No filename specified, can't read file, modified, or out of memory. */
3653 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3657 cur_line = file->lines;
3659 while ((cur_line != NULL) && (line_number < patternid))
3661 if (cur_line->type == FILE_LINE_ACTION)
3663 section_start_line_number = line_number;
3665 cur_line = cur_line->next;
3669 if ( (cur_line == NULL)
3670 || (cur_line->type != FILE_LINE_URL))
3672 /* Invalid "patternid" parameter */
3674 edit_free_file(file);
3675 return JB_ERR_CGI_PARAMS;
3678 /* At this point, the line to edit is in cur_line */
3680 freez(cur_line->raw);
3681 freez(cur_line->unprocessed);
3682 cur_line->unprocessed = new_pattern;
3684 err = edit_write_file(file);
3687 /* Error writing file */
3688 if (err == JB_ERR_FILE)
3690 /* Read-only file. */
3691 err = cgi_error_file_read_only(csp, rsp, file->filename);
3693 edit_free_file(file);
3697 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3698 (long) time(NULL), file->identifier, section_start_line_number);
3700 edit_free_file(file);
3702 return cgi_redirect(rsp, target);
3706 /*********************************************************************
3708 * Function : cgi_edit_actions_add_url
3710 * Description : CGI function that actually adds a URL pattern to
3714 * 1 : csp = Current client state (buffers, headers, etc...)
3715 * 2 : rsp = http_response data structure for output
3716 * 3 : parameters = map of cgi parameters
3719 * filename : Identifies the file to edit
3720 * ver : File's last-modified time
3721 * section : Line number of section to edit
3722 * newval : New pattern
3724 * Returns : JB_ERR_OK on success
3725 * JB_ERR_MEMORY on out-of-memory
3726 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3727 * specified or not valid.
3729 *********************************************************************/
3730 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3731 struct http_response *rsp,
3732 const struct map *parameters)
3736 struct file_line * new_line;
3737 struct editable_file * file;
3738 struct file_line * cur_line;
3739 unsigned line_number;
3743 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3745 return cgi_error_disabled(csp, rsp);
3748 err = get_number_param(csp, parameters, "s", §ionid);
3755 return JB_ERR_CGI_PARAMS;
3758 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3764 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3767 /* No filename specified, can't read file, modified, or out of memory. */
3769 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3773 cur_line = file->lines;
3775 while ((cur_line != NULL) && (line_number < sectionid))
3777 cur_line = cur_line->next;
3781 if ( (cur_line == NULL)
3782 || (cur_line->type != FILE_LINE_ACTION))
3784 /* Invalid "sectionid" parameter */
3786 edit_free_file(file);
3787 return JB_ERR_CGI_PARAMS;
3790 /* At this point, the section header is in cur_line - add after this. */
3792 /* Allocate the new line */
3793 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3794 if (new_line == NULL)
3797 edit_free_file(file);
3798 return JB_ERR_MEMORY;
3801 /* Fill in the data members of the new line */
3802 new_line->raw = NULL;
3803 new_line->prefix = NULL;
3804 new_line->unprocessed = new_pattern;
3805 new_line->type = FILE_LINE_URL;
3807 /* Link new_line into the list, after cur_line */
3808 new_line->next = cur_line->next;
3809 cur_line->next = new_line;
3811 /* Done making changes, now commit */
3813 err = edit_write_file(file);
3816 /* Error writing file */
3817 if (err == JB_ERR_FILE)
3819 /* Read-only file. */
3820 err = cgi_error_file_read_only(csp, rsp, file->filename);
3822 edit_free_file(file);
3826 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3827 (long) time(NULL), file->identifier, sectionid);
3829 edit_free_file(file);
3831 return cgi_redirect(rsp, target);
3835 /*********************************************************************
3837 * Function : cgi_edit_actions_remove_url
3839 * Description : CGI function that actually removes a URL pattern from
3843 * 1 : csp = Current client state (buffers, headers, etc...)
3844 * 2 : rsp = http_response data structure for output
3845 * 3 : parameters = map of cgi parameters
3848 * f : (filename) Identifies the file to edit
3849 * v : (version) File's last-modified time
3850 * p : (pattern) Line number of pattern to remove
3852 * Returns : JB_ERR_OK on success
3853 * JB_ERR_MEMORY on out-of-memory
3854 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3855 * specified or not valid.
3857 *********************************************************************/
3858 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3859 struct http_response *rsp,
3860 const struct map *parameters)
3863 struct editable_file * file;
3864 struct file_line * cur_line;
3865 struct file_line * prev_line;
3866 unsigned line_number;
3867 unsigned section_start_line_number = 0;
3871 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3873 return cgi_error_disabled(csp, rsp);
3876 err = get_number_param(csp, parameters, "p", &patternid);
3882 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3885 /* No filename specified, can't read file, modified, or out of memory. */
3886 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3891 cur_line = file->lines;
3893 while ((cur_line != NULL) && (line_number < patternid))
3895 if (cur_line->type == FILE_LINE_ACTION)
3897 section_start_line_number = line_number;
3899 prev_line = cur_line;
3900 cur_line = cur_line->next;
3904 if ( (cur_line == NULL)
3905 || (prev_line == NULL)
3906 || (cur_line->type != FILE_LINE_URL))
3908 /* Invalid "patternid" parameter */
3909 edit_free_file(file);
3910 return JB_ERR_CGI_PARAMS;
3913 /* At this point, the line to remove is in cur_line, and the previous
3914 * one is in prev_line
3917 /* Unlink cur_line */
3918 prev_line->next = cur_line->next;
3919 cur_line->next = NULL;
3922 edit_free_file_lines(cur_line);
3924 err = edit_write_file(file);
3927 /* Error writing file */
3928 if (err == JB_ERR_FILE)
3930 /* Read-only file. */
3931 err = cgi_error_file_read_only(csp, rsp, file->filename);
3933 edit_free_file(file);
3937 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3938 (long) time(NULL), file->identifier, section_start_line_number);
3940 edit_free_file(file);
3942 return cgi_redirect(rsp, target);
3946 /*********************************************************************
3948 * Function : cgi_edit_actions_section_remove
3950 * Description : CGI function that actually removes a whole section from
3951 * the actions file. The section must be empty first
3952 * (else JB_ERR_CGI_PARAMS).
3955 * 1 : csp = Current client state (buffers, headers, etc...)
3956 * 2 : rsp = http_response data structure for output
3957 * 3 : parameters = map of cgi parameters
3960 * f : (filename) Identifies the file to edit
3961 * v : (version) File's last-modified time
3962 * s : (section) Line number of section to edit
3964 * Returns : JB_ERR_OK on success
3965 * JB_ERR_MEMORY on out-of-memory
3966 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3967 * specified or not valid.
3969 *********************************************************************/
3970 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3971 struct http_response *rsp,
3972 const struct map *parameters)
3975 struct editable_file * file;
3976 struct file_line * cur_line;
3977 struct file_line * prev_line;
3978 unsigned line_number;
3982 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3984 return cgi_error_disabled(csp, rsp);
3987 err = get_number_param(csp, parameters, "s", §ionid);
3993 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3996 /* No filename specified, can't read file, modified, or out of memory. */
3997 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4001 cur_line = file->lines;
4004 while ((cur_line != NULL) && (line_number < sectionid))
4006 prev_line = cur_line;
4007 cur_line = cur_line->next;
4011 if ( (cur_line == NULL)
4012 || (cur_line->type != FILE_LINE_ACTION) )
4014 /* Invalid "sectionid" parameter */
4015 edit_free_file(file);
4016 return JB_ERR_CGI_PARAMS;
4019 if ( (cur_line->next != NULL)
4020 && (cur_line->next->type == FILE_LINE_URL) )
4022 /* Section not empty. */
4023 edit_free_file(file);
4024 return JB_ERR_CGI_PARAMS;
4027 /* At this point, the line to remove is in cur_line, and the previous
4028 * one is in prev_line
4031 /* Unlink cur_line */
4032 if (prev_line == NULL)
4034 /* Removing the first line from the file */
4035 file->lines = cur_line->next;
4039 prev_line->next = cur_line->next;
4041 cur_line->next = NULL;
4044 edit_free_file_lines(cur_line);
4046 err = edit_write_file(file);
4049 /* Error writing file */
4050 if (err == JB_ERR_FILE)
4052 /* Read-only file. */
4053 err = cgi_error_file_read_only(csp, rsp, file->filename);
4055 edit_free_file(file);
4059 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4060 (long) time(NULL), file->identifier);
4062 edit_free_file(file);
4064 return cgi_redirect(rsp, target);
4068 /*********************************************************************
4070 * Function : cgi_edit_actions_section_add
4072 * Description : CGI function that adds a new empty section to
4076 * 1 : csp = Current client state (buffers, headers, etc...)
4077 * 2 : rsp = http_response data structure for output
4078 * 3 : parameters = map of cgi parameters
4081 * f : (filename) Identifies the file to edit
4082 * v : (version) File's last-modified time
4083 * s : (section) Line number of section to add after, 0 for
4086 * Returns : JB_ERR_OK on success
4087 * JB_ERR_MEMORY on out-of-memory
4088 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4089 * specified or not valid.
4091 *********************************************************************/
4092 jb_err cgi_edit_actions_section_add(struct client_state *csp,
4093 struct http_response *rsp,
4094 const struct map *parameters)
4097 struct file_line * new_line;
4099 struct editable_file * file;
4100 struct file_line * cur_line;
4101 unsigned line_number;
4105 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4107 return cgi_error_disabled(csp, rsp);
4110 err = get_number_param(csp, parameters, "s", §ionid);
4116 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4119 /* No filename specified, can't read file, modified, or out of memory. */
4120 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4124 cur_line = file->lines;
4126 if (sectionid <= 1U)
4128 /* Add to start of file */
4129 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4131 /* There's something in the file, find the line before the first
4134 while ( (cur_line->next != NULL)
4135 && (cur_line->next->type != FILE_LINE_ACTION) )
4137 cur_line = cur_line->next;
4143 /* File starts with action line, so insert at top */
4149 /* Add after stated section. */
4150 while ((cur_line != NULL) && (line_number < sectionid))
4152 cur_line = cur_line->next;
4156 if ( (cur_line == NULL)
4157 || (cur_line->type != FILE_LINE_ACTION))
4159 /* Invalid "sectionid" parameter */
4160 edit_free_file(file);
4161 return JB_ERR_CGI_PARAMS;
4164 /* Skip through the section to find the last line in it. */
4165 while ( (cur_line->next != NULL)
4166 && (cur_line->next->type != FILE_LINE_ACTION) )
4168 cur_line = cur_line->next;
4173 /* At this point, the last line in the previous section is in cur_line
4174 * - add after this. (Or if we need to add as the first line, cur_line
4178 new_text = strdup("{}");
4179 if (NULL == new_text)
4181 edit_free_file(file);
4182 return JB_ERR_MEMORY;
4185 /* Allocate the new line */
4186 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4187 if (new_line == NULL)
4190 edit_free_file(file);
4191 return JB_ERR_MEMORY;
4194 /* Fill in the data members of the new line */
4195 new_line->raw = NULL;
4196 new_line->prefix = NULL;
4197 new_line->unprocessed = new_text;
4198 new_line->type = FILE_LINE_ACTION;
4200 if (cur_line != NULL)
4202 /* Link new_line into the list, after cur_line */
4203 new_line->next = cur_line->next;
4204 cur_line->next = new_line;
4208 /* Link new_line into the list, as first line */
4209 new_line->next = file->lines;
4210 file->lines = new_line;
4213 /* Done making changes, now commit */
4215 err = edit_write_file(file);
4218 /* Error writing file */
4219 if (err == JB_ERR_FILE)
4221 /* Read-only file. */
4222 err = cgi_error_file_read_only(csp, rsp, file->filename);
4224 edit_free_file(file);
4228 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4229 (long) time(NULL), file->identifier);
4231 edit_free_file(file);
4233 return cgi_redirect(rsp, target);
4237 /*********************************************************************
4239 * Function : cgi_edit_actions_section_swap
4241 * Description : CGI function that swaps the order of two sections
4242 * in the actions file. Note that this CGI can actually
4243 * swap any two arbitrary sections, but the GUI interface
4244 * currently only allows consecutive sections to be
4248 * 1 : csp = Current client state (buffers, headers, etc...)
4249 * 2 : rsp = http_response data structure for output
4250 * 3 : parameters = map of cgi parameters
4253 * f : (filename) Identifies the file to edit
4254 * v : (version) File's last-modified time
4255 * s1 : (section1) Line number of first section to swap
4256 * s2 : (section2) Line number of second section to swap
4258 * Returns : JB_ERR_OK on success
4259 * JB_ERR_MEMORY on out-of-memory
4260 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4261 * specified or not valid.
4263 *********************************************************************/
4264 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4265 struct http_response *rsp,
4266 const struct map *parameters)
4270 struct editable_file * file;
4271 struct file_line * cur_line;
4272 struct file_line * prev_line;
4273 struct file_line * line_before_section1;
4274 struct file_line * line_start_section1;
4275 struct file_line * line_end_section1;
4276 struct file_line * line_after_section1;
4277 struct file_line * line_before_section2;
4278 struct file_line * line_start_section2;
4279 struct file_line * line_end_section2;
4280 struct file_line * line_after_section2;
4281 unsigned line_number;
4285 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4287 return cgi_error_disabled(csp, rsp);
4290 err = get_number_param(csp, parameters, "s1", §ion1);
4291 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4297 if (section1 > section2)
4299 unsigned temp = section2;
4300 section2 = section1;
4304 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4307 /* No filename specified, can't read file, modified, or out of memory. */
4308 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4311 /* Start at the beginning... */
4313 cur_line = file->lines;
4316 /* ... find section1 ... */
4317 while ((cur_line != NULL) && (line_number < section1))
4319 prev_line = cur_line;
4320 cur_line = cur_line->next;
4324 if ( (cur_line == NULL)
4325 || (cur_line->type != FILE_LINE_ACTION) )
4327 /* Invalid "section1" parameter */
4328 edit_free_file(file);
4329 return JB_ERR_CGI_PARAMS;
4332 /* If no-op, we've validated params and can skip the rest. */
4333 if (section1 != section2)
4335 /* ... find the end of section1 ... */
4336 line_before_section1 = prev_line;
4337 line_start_section1 = cur_line;
4340 prev_line = cur_line;
4341 cur_line = cur_line->next;
4344 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4345 line_end_section1 = prev_line;
4346 line_after_section1 = cur_line;
4348 /* ... find section2 ... */
4349 while ((cur_line != NULL) && (line_number < section2))
4351 prev_line = cur_line;
4352 cur_line = cur_line->next;
4356 if ( (cur_line == NULL)
4357 || (cur_line->type != FILE_LINE_ACTION) )
4359 /* Invalid "section2" parameter */
4360 edit_free_file(file);
4361 return JB_ERR_CGI_PARAMS;
4364 /* ... find the end of section2 ... */
4365 line_before_section2 = prev_line;
4366 line_start_section2 = cur_line;
4369 prev_line = cur_line;
4370 cur_line = cur_line->next;
4373 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4374 line_end_section2 = prev_line;
4375 line_after_section2 = cur_line;
4377 /* Now have all the pointers we need. Do the swap. */
4379 /* Change the pointer to section1 to point to section2 instead */
4380 if (line_before_section1 == NULL)
4382 file->lines = line_start_section2;
4386 line_before_section1->next = line_start_section2;
4389 if (line_before_section2 == line_end_section1)
4391 /* Consecutive sections */
4392 line_end_section2->next = line_start_section1;
4396 line_end_section2->next = line_after_section1;
4397 line_before_section2->next = line_start_section1;
4400 /* Set the pointer from the end of section1 to the rest of the file */
4401 line_end_section1->next = line_after_section2;
4403 err = edit_write_file(file);
4406 /* Error writing file */
4407 if (err == JB_ERR_FILE)
4409 /* Read-only file. */
4410 err = cgi_error_file_read_only(csp, rsp, file->filename);
4412 edit_free_file(file);
4415 } /* END if (section1 != section2) */
4417 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4418 (long) time(NULL), file->identifier);
4420 edit_free_file(file);
4422 return cgi_redirect(rsp, target);
4425 #ifdef FEATURE_TOGGLE
4426 /*********************************************************************
4428 * Function : cgi_toggle
4430 * Description : CGI function that adds a new empty section to
4434 * 1 : csp = Current client state (buffers, headers, etc...)
4435 * 2 : rsp = http_response data structure for output
4436 * 3 : parameters = map of cgi parameters
4439 * set : If present, how to change toggle setting:
4440 * "enable", "disable", "toggle", or none (default).
4441 * mini : If present, use mini reply template.
4443 * Returns : JB_ERR_OK on success
4444 * JB_ERR_MEMORY on out-of-memory
4446 *********************************************************************/
4447 jb_err cgi_toggle(struct client_state *csp,
4448 struct http_response *rsp,
4449 const struct map *parameters)
4451 struct map *exports;
4453 const char *template_name;
4459 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4461 return cgi_error_disabled(csp, rsp);
4464 mode = get_char_param(parameters, "set");
4469 global_toggle_state = 1;
4471 else if (mode == 'D')
4474 global_toggle_state = 0;
4476 else if (mode == 'T')
4479 global_toggle_state = !global_toggle_state;
4482 if (NULL == (exports = default_exports(csp, "toggle")))
4484 return JB_ERR_MEMORY;
4487 template_name = (get_char_param(parameters, "mini")
4491 return template_fill_for_cgi(csp, template_name, exports, rsp);
4493 #endif /* def FEATURE_TOGGLE */
4495 /*********************************************************************
4497 * Function : javascriptify
4499 * Description : Converts a string into a form JavaScript will like.
4501 * Netscape 4's JavaScript sucks - it doesn't use
4502 * "id" parameters, so you have to set the "name"
4503 * used to submit a form element to something JavaScript
4504 * will like. (Or access the elements by index in an
4505 * array. That array contains >60 elements and will
4506 * be changed whenever we add a new action to the
4507 * editor, so I'm NOT going to use indexes that have
4508 * to be figured out by hand.)
4510 * Currently the only thing we have to worry about
4511 * is "-" ==> "_" conversion.
4513 * This is a length-preserving operation so it is
4514 * carried out in-place, no memory is allocated
4518 * 1 : identifier = String to make JavaScript-friendly.
4522 *********************************************************************/
4523 static void javascriptify(char * identifier)
4525 char * p = identifier;
4526 while (NULL != (p = strchr(p, '-')))
4533 /*********************************************************************
4535 * Function : actions_to_radio
4537 * Description : Converts a actionsfile entry into settings for
4538 * radio buttons and edit boxes on a HTML form.
4541 * 1 : exports = List of substitutions to add to.
4542 * 2 : action = Action to read
4544 * Returns : JB_ERR_OK on success
4545 * JB_ERR_MEMORY on out-of-memory
4547 *********************************************************************/
4548 static jb_err actions_to_radio(struct map * exports,
4549 const struct action_spec *action)
4551 unsigned mask = action->mask;
4552 unsigned add = action->add;
4560 mask = action->mask;
4563 /* sanity - prevents "-feature +feature" */
4567 #define DEFINE_ACTION_BOOL(name, bit) \
4568 if (!(mask & bit)) \
4570 current_mode = 'n'; \
4572 else if (add & bit) \
4574 current_mode = 'y'; \
4578 current_mode = 'x'; \
4580 if (map_radio(exports, name, "ynx", current_mode)) \
4582 return JB_ERR_MEMORY; \
4585 #define DEFINE_ACTION_STRING(name, bit, index) \
4586 DEFINE_ACTION_BOOL(name, bit); \
4589 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4592 checked = !strcmp(action->string[index], value); \
4596 checked = is_default; \
4598 mapped_param |= checked; \
4599 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4601 return JB_ERR_MEMORY; \
4604 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4605 if (map(exports, name "-param-custom", 1, \
4606 ((!mapped_param) ? "checked" : ""), 1)) \
4608 return JB_ERR_MEMORY; \
4610 if (map(exports, name "-param", 1, \
4611 (((add & bit) && !mapped_param) ? \
4612 action->string[index] : default_val), 1)) \
4614 return JB_ERR_MEMORY; \
4617 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4618 if (map(exports, name "-param", 1, \
4619 ((add & bit) ? action->string[index] : default_val), 1)) \
4621 return JB_ERR_MEMORY; \
4624 #define DEFINE_ACTION_MULTI(name, index) \
4625 if (action->multi_add[index]->first) \
4627 current_mode = 'y'; \
4629 else if (action->multi_remove_all[index]) \
4631 current_mode = 'n'; \
4633 else if (action->multi_remove[index]->first) \
4635 current_mode = 'y'; \
4639 current_mode = 'x'; \
4641 if (map_radio(exports, name, "ynx", current_mode)) \
4643 return JB_ERR_MEMORY; \
4646 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4648 #include "actionlist.h"
4650 #undef DEFINE_ACTION_MULTI
4651 #undef DEFINE_ACTION_STRING
4652 #undef DEFINE_ACTION_BOOL
4653 #undef DEFINE_ACTION_ALIAS
4654 #undef DEFINE_CGI_PARAM_CUSTOM
4655 #undef DEFINE_CGI_PARAM_RADIO
4656 #undef DEFINE_CGI_PARAM_NO_RADIO
4662 /*********************************************************************
4664 * Function : actions_from_radio
4666 * Description : Converts a map of parameters passed to a CGI function
4667 * into an actionsfile entry.
4670 * 1 : parameters = parameters to the CGI call
4671 * 2 : action = Action to change. Must be valid before
4672 * the call, actions not specified will be
4675 * Returns : JB_ERR_OK on success
4676 * JB_ERR_MEMORY on out-of-memory
4678 *********************************************************************/
4679 static jb_err actions_from_radio(const struct map * parameters,
4680 struct action_spec *action)
4685 const char * js_name;
4686 jb_err err = JB_ERR_OK;
4691 /* Statics are generally a potential race condition,
4692 * but in this case we're safe and don't need semaphores.
4693 * Be careful if you modify this function.
4695 * The js_name_arr's are never free()d, but this is no
4696 * problem, since they will only be created once and
4697 * used by all threads thereafter. -oes
4700 #define JAVASCRIPTIFY(dest_var, string) \
4702 static int first_time = 1; \
4703 static char *js_name_arr; \
4706 js_name_arr = strdup(string); \
4707 javascriptify(js_name_arr); \
4709 dest_var = js_name_arr; \
4713 #define DEFINE_ACTION_BOOL(name, bit) \
4714 JAVASCRIPTIFY(js_name, name); \
4715 ch = get_char_param(parameters, js_name); \
4718 action->add |= bit; \
4719 action->mask |= bit; \
4721 else if (ch == 'N') \
4723 action->add &= ~bit; \
4724 action->mask &= ~bit; \
4726 else if (ch == 'X') \
4728 action->add &= ~bit; \
4729 action->mask |= bit; \
4732 #define DEFINE_ACTION_STRING(name, bit, index) \
4733 JAVASCRIPTIFY(js_name, name); \
4734 ch = get_char_param(parameters, js_name); \
4738 JAVASCRIPTIFY(js_name, name "-mode"); \
4739 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4740 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4742 JAVASCRIPTIFY(js_name, name "-param"); \
4743 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4745 if (param != NULL) \
4747 if (NULL == (param_dup = strdup(param))) \
4749 return JB_ERR_MEMORY; \
4751 freez(action->string[index]); \
4752 action->add |= bit; \
4753 action->mask |= bit; \
4754 action->string[index] = param_dup; \
4757 else if (ch == 'N') \
4759 if (action->add & bit) \
4761 freez(action->string[index]); \
4763 action->add &= ~bit; \
4764 action->mask &= ~bit; \
4766 else if (ch == 'X') \
4768 if (action->add & bit) \
4770 freez(action->string[index]); \
4772 action->add &= ~bit; \
4773 action->mask |= bit; \
4776 #define DEFINE_ACTION_MULTI(name, index) \
4777 JAVASCRIPTIFY(js_name, name); \
4778 ch = get_char_param(parameters, js_name); \
4783 else if (ch == 'N') \
4785 list_remove_all(action->multi_add[index]); \
4786 list_remove_all(action->multi_remove[index]); \
4787 action->multi_remove_all[index] = 1; \
4789 else if (ch == 'X') \
4791 list_remove_all(action->multi_add[index]); \
4792 list_remove_all(action->multi_remove[index]); \
4793 action->multi_remove_all[index] = 0; \
4796 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4798 #include "actionlist.h"
4800 #undef DEFINE_ACTION_MULTI
4801 #undef DEFINE_ACTION_STRING
4802 #undef DEFINE_ACTION_BOOL
4803 #undef DEFINE_ACTION_ALIAS
4804 #undef JAVASCRIPTIFY
4810 #endif /* def FEATURE_CGI_EDIT_ACTIONS */