1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.49 2007/03/20 15:16:34 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.49 2007/03/20 15:16:34 fabiankeil
46 * Use dedicated header filter actions instead of abusing "filter".
47 * Replace "filter-client-headers" and "filter-client-headers"
48 * with "server-header-filter" and "client-header-filter".
50 * Revision 1.48 2007/02/13 14:35:25 fabiankeil
51 * Replace hash escaping code to prevent
52 * crashes, memory and file corruption.
54 * Revision 1.47 2006/12/28 18:04:25 fabiankeil
55 * Fixed gcc43 conversion warnings.
57 * Revision 1.46 2006/12/27 18:44:52 fabiankeil
58 * Stop shadowing string.h's index().
60 * Revision 1.45 2006/12/21 12:57:48 fabiankeil
61 * Add config option "split-large-forms"
62 * to work around the browser bug reported
65 * Revision 1.44 2006/12/09 13:49:16 fabiankeil
66 * Fix configure option --disable-toggle.
67 * Thanks to Peter Thoenen for reporting this.
69 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
70 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
71 * with what was really the latest development (the v_3_0_branch branch)
73 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
74 * Remove a little residual debugging info
76 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
77 * Multiple filter file support
79 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
80 * Undo some damaging memory leak patches
82 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
83 * Removing a double free
85 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
86 * A few memory leaks plugged (mostly on error paths)
88 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
89 * Moved cgi_error_disabled() from cgiedit.c to
90 * cgi.c to re-enable build with --disable-editor.
91 * Fixes Bug #892744. Thanks to Matthew Fischer
94 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
95 * One line lost in last commit
97 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
98 * - All edit functions that redirect back to the list page
99 * now use cgi_redirect
100 * - All redirects now contain useless parameter "foo", whose
101 * value are raw seconds since epoch, in order to force
102 * Opera and Konqueror to properly reload the list. Closes
105 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
106 * Cosmetic: Renamed cryptic variable
108 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
109 * Fix: Don't free uninitialized struct editable_file
111 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
112 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
114 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
115 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
117 * Revision 1.41 2002/05/21 19:09:45 oes
118 * - Made Add/Edit/Remove URL Submit and Cancel
119 * buttons jump back to relevant section in eal
120 * - Bugfix: remove-url-form needs p export
122 * Revision 1.40 2002/05/19 11:34:35 jongfoster
123 * Handling read-only actions files better - report the actual
124 * error, not "Out of memory"!
127 * http://sourceforge.net/tracker/index.php?func=detail
128 * &aid=557905&group_id=11118&atid=111118
130 * Revision 1.39 2002/05/12 21:39:15 jongfoster
131 * - Adding Doxygen-style comments to structures and #defines.
132 * - Correcting function comments
134 * Revision 1.38 2002/05/03 23:00:38 jongfoster
135 * Support for templates for "standard actions" buttons.
138 * Revision 1.37 2002/04/30 11:14:52 oes
139 * Made csp the first parameter in *action_to_html
141 * Revision 1.36 2002/04/26 21:53:30 jongfoster
142 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
144 * Revision 1.35 2002/04/26 21:50:02 jongfoster
145 * Honouring default exports in edit-actions-for-url-filter template.
147 * Revision 1.34 2002/04/26 12:54:17 oes
148 * Adaptions to changes in actions.c
150 * Revision 1.33 2002/04/24 02:17:47 oes
151 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
153 * - Activated Jon's code for editing multiple AFs
154 * - cgi_edit_list_actions now provides context-sensitive
155 * help, looks up all action sets from standard.action and
156 * makes buttons for them in the catchall section
157 * - cgi_edit_action_submit now honors a p parameter, looks up
158 * the corresponding action set, and sets the catchall pattern's
159 * actions accordingly.
161 * Revision 1.32 2002/04/19 16:55:31 jongfoster
162 * Fixing newline problems. If we do our own text file newline
163 * mangling, we don't want the library to do any, so we need to
164 * open the files in *binary* mode.
166 * Revision 1.31 2002/04/18 19:21:08 jongfoster
167 * Added code to detect "conventional" action files, that start
168 * with a set of actions for all URLs (the pattern "/").
169 * These are special-cased in the "edit-actions-list" CGI, so
170 * that a special UI can be written for them.
172 * Revision 1.30 2002/04/10 13:38:35 oes
173 * load_template signature changed
175 * Revision 1.29 2002/04/08 16:59:08 oes
178 * Revision 1.28 2002/03/27 12:30:29 oes
179 * Deleted unsused variable
181 * Revision 1.27 2002/03/26 23:06:04 jongfoster
182 * Removing duplicate @ifs on the toggle page
184 * Revision 1.26 2002/03/26 22:59:17 jongfoster
185 * Fixing /toggle to display status consistently.
187 * Revision 1.25 2002/03/26 22:29:54 swa
188 * we have a new homepage!
190 * Revision 1.24 2002/03/24 15:23:33 jongfoster
193 * Revision 1.23 2002/03/24 13:32:41 swa
194 * name change related issues
196 * Revision 1.22 2002/03/24 13:25:43 swa
197 * name change related issues
199 * Revision 1.21 2002/03/22 18:02:48 jongfoster
200 * Fixing remote toggle
202 * Revision 1.20 2002/03/16 20:28:34 oes
203 * Added descriptions to the filters so users will know what they select in the cgi editor
205 * Revision 1.19 2002/03/16 18:38:14 jongfoster
206 * Stopping stupid or malicious users from breaking the actions
207 * file using the web-based editor.
209 * Revision 1.18 2002/03/16 14:57:44 jongfoster
210 * Full support for enabling/disabling modular filters.
212 * Revision 1.17 2002/03/16 14:26:42 jongfoster
213 * First version of modular filters support - READ ONLY!
214 * Fixing a double-free bug in the out-of-memory handling in map_radio().
216 * Revision 1.16 2002/03/07 03:46:17 oes
217 * Fixed compiler warnings
219 * Revision 1.15 2002/03/06 22:54:35 jongfoster
220 * Automated function-comment nitpicking.
222 * Revision 1.14 2002/03/05 00:24:51 jongfoster
223 * Patch to always edit the current actions file.
225 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
226 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
228 * Revision 1.12 2002/03/03 09:18:03 joergs
229 * Made jumbjuster work on AmigaOS again.
231 * Revision 1.11 2002/01/23 01:03:31 jongfoster
232 * Fixing gcc [CygWin] compiler warnings
234 * Revision 1.10 2002/01/23 00:22:59 jongfoster
235 * Adding new function cgi_edit_actions_section_swap(), to reorder
238 * Adding get_url_spec_param() to get a validated URL pattern.
240 * Moving edit_read_line() out of this file and into loaders.c.
242 * Adding missing html_encode() to many CGI functions.
244 * Moving the functions that #include actionlist.h to the end of the file,
245 * because the Visual C++ 97 debugger gets extremely confused if you try
246 * to debug any code that comes after them in the file.
248 * Major optimizations in cgi_edit_actions_list() to reduce the size of
249 * the generated HTML (down 40% from 550k to 304k), with major side-effects
250 * throughout the editor and templates. In particular, the length of the
251 * URLs throughout the editor has been drastically reduced, by cutting
252 * paramater names down to 1 character and CGI names down to 3-4
253 * characters, by removing all non-essential CGI paramaters even at the
254 * expense of having to re-read the actions file for the most trivial
255 * page, and by using relative rather than absolute URLs. This means
256 * that this (typical example):
258 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
259 * filename=ijb&ver=1011487572&section=12&pattern=13
260 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
264 * <a href="eau?f=ijb&v=1011487572&p=13">
266 * Revision 1.9 2002/01/17 20:56:22 jongfoster
267 * Replacing hard references to the URL of the config interface
268 * with #defines from project.h
270 * Revision 1.8 2001/11/30 23:35:51 jongfoster
271 * Renaming actionsfile to ijb.action
273 * Revision 1.7 2001/11/13 00:28:24 jongfoster
274 * - Renaming parameters from edit-actions-for-url so that they only
275 * contain legal JavaScript characters. If we wanted to write
276 * JavaScript that worked with Netscape 4, this is nessacery.
277 * (Note that at the moment the JavaScript doesn't actually work
278 * with Netscape 4, but now this is purely a template issue, not
279 * one affecting code).
280 * - Adding new CGIs for use by non-JavaScript browsers:
281 * edit-actions-url-form
282 * edit-actions-add-url-form
283 * edit-actions-remove-url-form
286 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
287 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
288 * by and __OS2__ ifdef.
290 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
291 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
292 * threads to call select() simultaneously. So, it's time to do a real, live,
293 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
294 * (native). Both versions will work, but using __OS2__ offers multi-threading.
296 * Revision 1.4 2001/10/23 21:48:19 jongfoster
297 * Cleaning up error handling in CGI functions - they now send back
298 * a HTML error page and should never cause a FATAL error. (Fixes one
299 * potential source of "denial of service" attacks).
301 * CGI actions file editor that works and is actually useful.
303 * Ability to toggle JunkBuster remotely using a CGI call.
305 * You can turn off both the above features in the main configuration
306 * file, e.g. if you are running a multi-user proxy.
308 * Revision 1.3 2001/10/14 22:12:49 jongfoster
309 * New version of CGI-based actionsfile editor.
310 * Major changes, including:
311 * - Completely new file parser and file output routines
312 * - edit-actions CGI renamed edit-actions-for-url
313 * - All CGIs now need a filename parameter, except for...
314 * - New CGI edit-actions which doesn't need a filename,
315 * to allow you to start the editor up.
316 * - edit-actions-submit now works, and now automatically
317 * redirects you back to the main edit-actions-list handler.
319 * Revision 1.2 2001/09/16 17:05:14 jongfoster
320 * Removing unused #include showarg.h
322 * Revision 1.1 2001/09/16 15:47:37 jongfoster
323 * First version of CGI-based edit interface. This is very much a
324 * work-in-progress, and you can't actually use it to edit anything
325 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
326 * to have any effect.
329 **********************************************************************/
335 * FIXME: Following includes copied from cgi.c - which are actually needed?
340 #include <sys/types.h>
344 #include <sys/stat.h>
347 #define snprintf _snprintf
348 #endif /* def _WIN32 */
353 #include "cgisimple.h"
357 #include "miscutil.h"
360 #ifdef FEATURE_TOGGLE
361 /* loadcfg.h is for global_toggle_state only */
363 #endif /* def FEATURE_TOGGLE */
364 #include "urlmatch.h"
366 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
369 #ifdef FEATURE_CGI_EDIT_ACTIONS
372 * A line in an editable_file.
376 /** Next entry in the linked list */
377 struct file_line * next;
379 /** The raw data, to write out if this line is unmodified. */
382 /** Comments and/or whitespace to put before this line if it's modified
383 and then written out. */
386 /** The actual data, as a string. Line continuation and comment removal
387 are performed on the data read from file before it's stored here, so
388 it will be a single line of data. */
391 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
394 /** The actual data, processed into some sensible data type. */
398 /** An action specification. */
399 struct action_spec action[1];
401 /** A name=value pair. */
405 /** The name in the name=value pair. */
408 /** The value in the name=value pair, as a string. */
411 /** The value in the name=value pair, as an integer. */
416 /* Add more data types here... e.g.
419 struct url_spec url[1];
423 struct action_spec action[1];
433 /** This file_line has not been processed yet. */
434 #define FILE_LINE_UNPROCESSED 1
436 /** This file_line is blank. Can only appear at the end of a file, due to
437 the way the parser works. */
438 #define FILE_LINE_BLANK 2
440 /** This file_line says {{alias}}. */
441 #define FILE_LINE_ALIAS_HEADER 3
443 /** This file_line defines an alias. */
444 #define FILE_LINE_ALIAS_ENTRY 4
446 /** This file_line defines an {action}. */
447 #define FILE_LINE_ACTION 5
449 /** This file_line specifies a URL pattern. */
450 #define FILE_LINE_URL 6
452 /** This file_line says {{settings}}. */
453 #define FILE_LINE_SETTINGS_HEADER 7
455 /** This file_line is in a {{settings}} block. */
456 #define FILE_LINE_SETTINGS_ENTRY 8
458 /** This file_line says {{description}}. */
459 #define FILE_LINE_DESCRIPTION_HEADER 9
461 /** This file_line is in a {{description}} block. */
462 #define FILE_LINE_DESCRIPTION_ENTRY 10
466 * A configuration file, in a format that can be edited and written back to
471 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
472 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
473 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
474 /**< Pre-encoded with url_encode() for ease of use. */
475 const char * version_str; /**< Last modification time, as a string. For CGI param. */
476 /**< Can be used in URL without using url_param(). */
477 unsigned version; /**< Last modification time - prevents chaos with
478 the browser's "back" button. Note that this is a
479 time_t cast to an unsigned. When comparing, always
480 cast the time_t to an unsigned, and *NOT* vice-versa.
481 This may lose the top few bits, but they're not
482 significant anyway. */
483 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
484 Note that changing this after the file has been
485 read in will cause a mess. */
486 struct file_line * parse_error; /**< On parse error, this is the offending line. */
487 const char * parse_error_text; /**< On parse error, this is the problem.
488 (Statically allocated) */
491 /* FIXME: Following non-static functions should be prototyped in .h or made static */
493 /* Functions to read and write arbitrary config files */
494 jb_err edit_read_file(struct client_state *csp,
495 const struct map *parameters,
498 struct editable_file **pfile);
499 jb_err edit_write_file(struct editable_file * file);
500 void edit_free_file(struct editable_file * file);
502 /* Functions to read and write actions files */
503 jb_err edit_parse_actions_file(struct editable_file * file);
504 jb_err edit_read_actions_file(struct client_state *csp,
505 struct http_response *rsp,
506 const struct map *parameters,
508 struct editable_file **pfile);
511 jb_err cgi_error_modified(struct client_state *csp,
512 struct http_response *rsp,
513 const char *filename);
514 jb_err cgi_error_parse(struct client_state *csp,
515 struct http_response *rsp,
516 struct editable_file *file);
517 jb_err cgi_error_file(struct client_state *csp,
518 struct http_response *rsp,
519 const char *filename);
520 jb_err cgi_error_file_read_only(struct client_state *csp,
521 struct http_response *rsp,
522 const char *filename);
524 /* Internal arbitrary config file support functions */
525 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
526 static void edit_free_file_lines(struct file_line * first_line);
528 /* Internal actions file support functions */
529 static int match_actions_file_header_line(const char * line, const char * name);
530 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
532 /* Internal parameter parsing functions */
533 static jb_err get_file_name_param(struct client_state *csp,
534 const struct map *parameters,
535 const char *param_name,
538 const char **pparam);
540 static jb_err get_url_spec_param(struct client_state *csp,
541 const struct map *parameters,
546 /* Internal actionsfile <==> HTML conversion functions */
547 static jb_err map_radio(struct map * exports,
548 const char * optionname,
551 static jb_err actions_to_radio(struct map * exports,
552 const struct action_spec *action);
553 static jb_err actions_from_radio(const struct map * parameters,
554 struct action_spec *action);
557 static jb_err map_copy_parameter_html(struct map *out,
558 const struct map *in,
560 #if 0 /* unused function */
561 static jb_err map_copy_parameter_url(struct map *out,
562 const struct map *in,
564 #endif /* unused function */
566 /* Internal convenience functions */
567 static char *section_target(const unsigned sectionid);
569 /*********************************************************************
571 * Function : section_target
573 * Description : Given an unsigned (section id) n, produce a dynamically
574 * allocated string of the form #l<n>, for use in link
578 * 1 : sectionid = start line number of section
580 * Returns : String with link target, or NULL if out of
583 *********************************************************************/
584 static char *section_target(const unsigned sectionid)
588 snprintf(buf, 30, "#l%d", sectionid);
594 /*********************************************************************
596 * Function : map_copy_parameter_html
598 * Description : Copy a CGI parameter from one map to another, HTML
602 * 1 : out = target map
603 * 2 : in = source map
604 * 3 : name = name of cgi parameter to copy
606 * Returns : JB_ERR_OK on success
607 * JB_ERR_MEMORY on out-of-memory
608 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
611 *********************************************************************/
612 static jb_err map_copy_parameter_html(struct map *out,
613 const struct map *in,
623 value = lookup(in, name);
624 err = map(out, name, 1, html_encode(value), 0);
631 else if (*value == '\0')
633 return JB_ERR_CGI_PARAMS;
642 #if 0 /* unused function */
643 /*********************************************************************
645 * Function : map_copy_parameter_url
647 * Description : Copy a CGI parameter from one map to another, URL
651 * 1 : out = target map
652 * 2 : in = source map
653 * 3 : name = name of cgi parameter to copy
655 * Returns : JB_ERR_OK on success
656 * JB_ERR_MEMORY on out-of-memory
657 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
660 *********************************************************************/
661 static jb_err map_copy_parameter_url(struct map *out,
662 const struct map *in,
672 value = lookup(in, name);
673 err = map(out, name, 1, url_encode(value), 0);
680 else if (*value == '\0')
682 return JB_ERR_CGI_PARAMS;
689 #endif /* 0 - unused function */
691 /*********************************************************************
693 * Function : cgi_edit_actions_url_form
695 * Description : CGI function that displays a form for
699 * 1 : csp = Current client state (buffers, headers, etc...)
700 * 2 : rsp = http_response data structure for output
701 * 3 : parameters = map of cgi parameters
704 * f : (filename) Identifies the file to edit
705 * v : (version) File's last-modified time
706 * p : (pattern) Line number of pattern to edit
708 * Returns : JB_ERR_OK on success
709 * JB_ERR_MEMORY on out-of-memory
710 * JB_ERR_CGI_PARAMS if the CGI parameters are not
711 * specified or not valid.
713 *********************************************************************/
714 jb_err cgi_edit_actions_url_form(struct client_state *csp,
715 struct http_response *rsp,
716 const struct map *parameters)
718 struct map * exports;
720 struct editable_file * file;
721 struct file_line * cur_line;
722 unsigned line_number;
723 unsigned section_start_line_number = 0;
730 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
732 return cgi_error_disabled(csp, rsp);
735 err = get_number_param(csp, parameters, "p", &patternid);
741 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
744 /* No filename specified, can't read file, modified, or out of memory. */
745 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
748 cur_line = file->lines;
750 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
752 if (cur_line->type == FILE_LINE_ACTION)
754 section_start_line_number = line_number;
756 cur_line = cur_line->next;
759 if ( (cur_line == NULL)
760 || (line_number != patternid)
762 || (cur_line->type != FILE_LINE_URL))
764 /* Invalid "patternid" parameter */
765 edit_free_file(file);
766 return JB_ERR_CGI_PARAMS;
769 if (NULL == (exports = default_exports(csp, NULL)))
771 edit_free_file(file);
772 return JB_ERR_MEMORY;
775 err = map(exports, "f", 1, file->identifier, 1);
776 if (!err) err = map(exports, "v", 1, file->version_str, 1);
777 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
778 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
779 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
781 edit_free_file(file);
789 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
793 /*********************************************************************
795 * Function : cgi_edit_actions_add_url_form
797 * Description : CGI function that displays a form for
801 * 1 : csp = Current client state (buffers, headers, etc...)
802 * 2 : rsp = http_response data structure for output
803 * 3 : parameters = map of cgi parameters
806 * f : (filename) Identifies the file to edit
807 * v : (version) File's last-modified time
808 * s : (section) Line number of section to edit
810 * Returns : JB_ERR_OK on success
811 * JB_ERR_MEMORY on out-of-memory
812 * JB_ERR_CGI_PARAMS if the CGI parameters are not
813 * specified or not valid.
815 *********************************************************************/
816 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
817 struct http_response *rsp,
818 const struct map *parameters)
827 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
829 return cgi_error_disabled(csp, rsp);
832 if (NULL == (exports = default_exports(csp, NULL)))
834 return JB_ERR_MEMORY;
837 err = map_copy_parameter_html(exports, parameters, "f");
838 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
839 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
847 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
851 /*********************************************************************
853 * Function : cgi_edit_actions_remove_url_form
855 * Description : CGI function that displays a form for
859 * 1 : csp = Current client state (buffers, headers, etc...)
860 * 2 : rsp = http_response data structure for output
861 * 3 : parameters = map of cgi parameters
864 * f : (filename) Identifies the file to edit
865 * v : (version) File's last-modified time
866 * p : (pattern) Line number of pattern to edit
868 * Returns : JB_ERR_OK on success
869 * JB_ERR_MEMORY on out-of-memory
870 * JB_ERR_CGI_PARAMS if the CGI parameters are not
871 * specified or not valid.
873 *********************************************************************/
874 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
875 struct http_response *rsp,
876 const struct map *parameters)
878 struct map * exports;
880 struct editable_file * file;
881 struct file_line * cur_line;
882 unsigned line_number;
883 unsigned section_start_line_number = 0;
890 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
892 return cgi_error_disabled(csp, rsp);
895 err = get_number_param(csp, parameters, "p", &patternid);
901 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
904 /* No filename specified, can't read file, modified, or out of memory. */
905 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
908 cur_line = file->lines;
910 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
912 if (cur_line->type == FILE_LINE_ACTION)
914 section_start_line_number = line_number;
916 cur_line = cur_line->next;
919 if ( (cur_line == NULL)
920 || (line_number != patternid)
922 || (cur_line->type != FILE_LINE_URL))
924 /* Invalid "patternid" parameter */
925 edit_free_file(file);
926 return JB_ERR_CGI_PARAMS;
929 if (NULL == (exports = default_exports(csp, NULL)))
931 edit_free_file(file);
932 return JB_ERR_MEMORY;
935 err = map(exports, "f", 1, file->identifier, 1);
936 if (!err) err = map(exports, "v", 1, file->version_str, 1);
937 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
938 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
939 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
941 edit_free_file(file);
949 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
953 /*********************************************************************
955 * Function : edit_write_file
957 * Description : Write a complete file to disk.
960 * 1 : file = File to write.
962 * Returns : JB_ERR_OK on success
963 * JB_ERR_FILE on error writing to file.
964 * JB_ERR_MEMORY on out of memory
966 *********************************************************************/
967 jb_err edit_write_file(struct editable_file * file)
970 struct file_line * cur_line;
971 struct stat statbuf[1];
972 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
973 digits in time_t, assuming this is a 64-bit
974 machine, plus null terminator, plus one
978 assert(file->filename);
980 if (NULL == (fp = fopen(file->filename, "wb")))
985 cur_line = file->lines;
986 while (cur_line != NULL)
990 if (fputs(cur_line->raw, fp) < 0)
998 if (cur_line->prefix)
1000 if (fputs(cur_line->prefix, fp) < 0)
1006 if (cur_line->unprocessed)
1009 if (NULL != strchr(cur_line->unprocessed, '#'))
1011 /* Must quote '#' characters */
1018 /* Count number of # characters, so we know length of output string */
1019 src = cur_line->unprocessed;
1020 while (NULL != (src = strchr(src, '#')))
1025 assert(numhash > 0);
1027 /* Allocate new memory for string */
1028 len = strlen(cur_line->unprocessed) + (size_t)numhash;
1029 if (NULL == (str = malloc(len + 1)))
1031 /* Uh oh, just trashed file! */
1033 return JB_ERR_MEMORY;
1036 /* Copy string but quote hashes */
1037 src = cur_line->unprocessed;
1045 assert(numhash >= 0);
1051 assert(numhash == 0);
1052 assert(strlen(str) == len);
1053 assert(str == dest - len);
1054 assert(src - len <= cur_line->unprocessed);
1056 if ((strlen(str) != len) || (numhash != 0))
1059 * Escaping didn't work as expected, go spread the news.
1060 * Only reached in non-debugging builds.
1062 log_error(LOG_LEVEL_ERROR,
1063 "Looks like hash escaping failed. %s might be corrupted now.",
1067 if (fputs(str, fp) < 0)
1078 /* Can write without quoting '#' characters. */
1079 if (fputs(cur_line->unprocessed, fp) < 0)
1085 if (fputs(NEWLINE(file->newline), fp) < 0)
1093 /* FIXME: Write data from file->data->whatever */
1097 cur_line = cur_line->next;
1103 /* Update the version stamp in the file structure, since we just
1104 * wrote to the file & changed it's date.
1106 if (stat(file->filename, statbuf) < 0)
1108 /* Error, probably file not found. */
1111 file->version = (unsigned)statbuf->st_mtime;
1113 /* Correct file->version_str */
1114 freez(file->version_str);
1115 snprintf(version_buf, 22, "%u", file->version);
1116 version_buf[21] = '\0';
1117 file->version_str = strdup(version_buf);
1118 if (version_buf == NULL)
1120 return JB_ERR_MEMORY;
1127 /*********************************************************************
1129 * Function : edit_free_file
1131 * Description : Free a complete file in memory.
1134 * 1 : file = Data structure to free.
1138 *********************************************************************/
1139 void edit_free_file(struct editable_file * file)
1143 /* Silently ignore NULL pointer */
1147 edit_free_file_lines(file->lines);
1148 freez(file->filename);
1149 freez(file->identifier);
1150 freez(file->version_str);
1152 file->parse_error_text = NULL; /* Statically allocated */
1153 file->parse_error = NULL;
1159 /*********************************************************************
1161 * Function : edit_free_file_lines
1163 * Description : Free an entire linked list of file lines.
1166 * 1 : first_line = Data structure to free.
1170 *********************************************************************/
1171 static void edit_free_file_lines(struct file_line * first_line)
1173 struct file_line * next_line;
1175 while (first_line != NULL)
1177 next_line = first_line->next;
1178 first_line->next = NULL;
1179 freez(first_line->raw);
1180 freez(first_line->prefix);
1181 freez(first_line->unprocessed);
1182 switch(first_line->type)
1184 case 0: /* special case if memory zeroed */
1185 case FILE_LINE_UNPROCESSED:
1186 case FILE_LINE_BLANK:
1187 case FILE_LINE_ALIAS_HEADER:
1188 case FILE_LINE_SETTINGS_HEADER:
1189 case FILE_LINE_DESCRIPTION_HEADER:
1190 case FILE_LINE_DESCRIPTION_ENTRY:
1191 case FILE_LINE_ALIAS_ENTRY:
1193 /* No data is stored for these */
1196 case FILE_LINE_ACTION:
1197 free_action(first_line->data.action);
1200 case FILE_LINE_SETTINGS_ENTRY:
1201 freez(first_line->data.setting.name);
1202 freez(first_line->data.setting.svalue);
1205 /* Should never happen */
1209 first_line->type = 0; /* paranoia */
1211 first_line = next_line;
1216 /*********************************************************************
1218 * Function : match_actions_file_header_line
1220 * Description : Match an actions file {{header}} line
1223 * 1 : line = String from file
1224 * 2 : name = Header to match against
1226 * Returns : 0 iff they match.
1228 *********************************************************************/
1229 static int match_actions_file_header_line(const char * line, const char * name)
1237 if ((line[0] != '{') || (line[1] != '{'))
1243 /* Look for optional whitespace */
1244 while ( (*line == ' ') || (*line == '\t') )
1249 /* Look for the specified name (case-insensitive) */
1251 if (0 != strncmpic(line, name, len))
1257 /* Look for optional whitespace */
1258 while ( (*line == ' ') || (*line == '\t') )
1263 /* Look for "}}" and end of string*/
1264 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1274 /*********************************************************************
1276 * Function : match_actions_file_header_line
1278 * Description : Match an actions file {{header}} line
1281 * 1 : line = String from file. Must not start with
1282 * whitespace (else infinite loop!)
1283 * 2 : pname = Destination for name
1284 * 2 : pvalue = Destination for value
1286 * Returns : JB_ERR_OK on success
1287 * JB_ERR_MEMORY on out-of-memory
1288 * JB_ERR_PARSE if there's no "=" sign, or if there's
1289 * nothing before the "=" sign (but empty
1290 * values *after* the "=" sign are legal).
1292 *********************************************************************/
1293 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1295 const char * name_end;
1296 const char * value_start;
1302 assert(*line != ' ');
1303 assert(*line != '\t');
1308 value_start = strchr(line, '=');
1309 if ((value_start == NULL) || (value_start == line))
1311 return JB_ERR_PARSE;
1314 name_end = value_start - 1;
1316 /* Eat any whitespace before the '=' */
1317 while ((*name_end == ' ') || (*name_end == '\t'))
1320 * we already know we must have at least 1 non-ws char
1321 * at start of buf - no need to check
1326 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1327 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1329 return JB_ERR_MEMORY;
1331 strncpy(*pname, line, name_len);
1332 (*pname)[name_len] = '\0';
1334 /* Eat any the whitespace after the '=' */
1336 while ((*value_start == ' ') || (*value_start == '\t'))
1341 if (NULL == (*pvalue = strdup(value_start)))
1345 return JB_ERR_MEMORY;
1352 /*********************************************************************
1354 * Function : edit_parse_actions_file
1356 * Description : Parse an actions file in memory.
1358 * Passed linked list must have the "data" member
1359 * zeroed, and must contain valid "next" and
1360 * "unprocessed" fields. The "raw" and "prefix"
1361 * fields are ignored, and "type" is just overwritten.
1363 * Note that on error the file may have been
1367 * 1 : file = Actions file to be parsed in-place.
1369 * Returns : JB_ERR_OK on success
1370 * JB_ERR_MEMORY on out-of-memory
1371 * JB_ERR_PARSE on error
1373 *********************************************************************/
1374 jb_err edit_parse_actions_file(struct editable_file * file)
1376 struct file_line * cur_line;
1378 const char * text; /* Text from a line */
1379 char * name; /* For lines of the form name=value */
1380 char * value; /* For lines of the form name=value */
1381 struct action_alias * alias_list = NULL;
1382 jb_err err = JB_ERR_OK;
1384 /* alias_list contains the aliases defined in this file.
1385 * It might be better to use the "file_line.data" fields
1386 * in the relavent places instead.
1389 cur_line = file->lines;
1391 /* A note about blank line support: Blank lines should only
1392 * ever occur as the last line in the file. This function
1393 * is more forgiving than that - FILE_LINE_BLANK can occur
1397 /* Skip leading blanks. Should only happen if file is
1398 * empty (which is valid, but pointless).
1400 while ( (cur_line != NULL)
1401 && (cur_line->unprocessed[0] == '\0') )
1404 cur_line->type = FILE_LINE_BLANK;
1405 cur_line = cur_line->next;
1408 if ( (cur_line != NULL)
1409 && (cur_line->unprocessed[0] != '{') )
1411 /* File doesn't start with a header */
1412 file->parse_error = cur_line;
1413 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1414 return JB_ERR_PARSE;
1417 if ( (cur_line != NULL) && (0 ==
1418 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1420 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1422 cur_line = cur_line->next;
1423 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1425 if (cur_line->unprocessed[0])
1427 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1429 err = split_line_on_equals(cur_line->unprocessed,
1430 &cur_line->data.setting.name,
1431 &cur_line->data.setting.svalue);
1432 if (err == JB_ERR_MEMORY)
1436 else if (err != JB_ERR_OK)
1438 /* Line does not contain a name=value pair */
1439 file->parse_error = cur_line;
1440 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1441 return JB_ERR_PARSE;
1446 cur_line->type = FILE_LINE_BLANK;
1448 cur_line = cur_line->next;
1452 if ( (cur_line != NULL) && (0 ==
1453 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1455 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1457 cur_line = cur_line->next;
1458 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1460 if (cur_line->unprocessed[0])
1462 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1466 cur_line->type = FILE_LINE_BLANK;
1468 cur_line = cur_line->next;
1472 if ( (cur_line != NULL) && (0 ==
1473 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1475 cur_line->type = FILE_LINE_ALIAS_HEADER;
1477 cur_line = cur_line->next;
1478 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1480 if (cur_line->unprocessed[0])
1482 /* define an alias */
1483 struct action_alias * new_alias;
1485 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1487 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1488 if (err == JB_ERR_MEMORY)
1492 else if (err != JB_ERR_OK)
1494 /* Line does not contain a name=value pair */
1495 file->parse_error = cur_line;
1496 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1497 return JB_ERR_PARSE;
1500 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1505 free_alias_list(alias_list);
1506 return JB_ERR_MEMORY;
1509 err = get_actions(value, alias_list, new_alias->action);
1512 /* Invalid action or out of memory */
1516 free_alias_list(alias_list);
1517 if (err == JB_ERR_MEMORY)
1523 /* Line does not contain a name=value pair */
1524 file->parse_error = cur_line;
1525 file->parse_error_text = "This alias does not specify a valid set of actions.";
1526 return JB_ERR_PARSE;
1532 new_alias->name = name;
1535 new_alias->next = alias_list;
1536 alias_list = new_alias;
1540 cur_line->type = FILE_LINE_BLANK;
1542 cur_line = cur_line->next;
1546 /* Header done, process the main part of the file */
1547 while (cur_line != NULL)
1549 /* At this point, (cur_line->unprocessed[0] == '{') */
1550 assert(cur_line->unprocessed[0] == '{');
1551 text = cur_line->unprocessed + 1;
1552 len = strlen(text) - 1;
1553 if (text[len] != '}')
1555 /* No closing } on header */
1556 free_alias_list(alias_list);
1557 file->parse_error = cur_line;
1558 file->parse_error_text = "Headers starting with '{' must have a "
1559 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1560 "must close with two brackets ('}}').";
1561 return JB_ERR_PARSE;
1566 /* An invalid {{ header. */
1567 free_alias_list(alias_list);
1568 file->parse_error = cur_line;
1569 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1570 "Please remember that the system (two-bracket) headers must "
1571 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1572 "and must appear before any actions (one-bracket) headers. "
1573 "Also note that system headers may not be repeated.";
1574 return JB_ERR_PARSE;
1577 while ( (*text == ' ') || (*text == '\t') )
1583 && ( (text[len - 1] == ' ')
1584 || (text[len - 1] == '\t') ) )
1589 cur_line->type = FILE_LINE_ACTION;
1591 /* Remove {} and make copy */
1592 if (NULL == (value = (char *) malloc(len + 1)))
1595 free_alias_list(alias_list);
1596 return JB_ERR_MEMORY;
1598 strncpy(value, text, len);
1602 err = get_actions(value, alias_list, cur_line->data.action);
1605 /* Invalid action or out of memory */
1607 free_alias_list(alias_list);
1608 if (err == JB_ERR_MEMORY)
1614 /* Line does not contain a name=value pair */
1615 file->parse_error = cur_line;
1616 file->parse_error_text = "This header does not specify a valid set of actions.";
1617 return JB_ERR_PARSE;
1621 /* Done with string - it was clobbered anyway */
1624 /* Process next line */
1625 cur_line = cur_line->next;
1627 /* Loop processing URL patterns */
1628 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1630 if (cur_line->unprocessed[0])
1632 /* Could parse URL here, but this isn't currently needed */
1634 cur_line->type = FILE_LINE_URL;
1638 cur_line->type = FILE_LINE_BLANK;
1640 cur_line = cur_line->next;
1642 } /* End main while(cur_line != NULL) loop */
1644 free_alias_list(alias_list);
1650 /*********************************************************************
1652 * Function : edit_read_file_lines
1654 * Description : Read all the lines of a file into memory.
1655 * Handles whitespace, comments and line continuation.
1658 * 1 : fp = File to read from. On return, this will be
1659 * at EOF but it will not have been closed.
1660 * 2 : pfile = Destination for a linked list of file_lines.
1661 * Will be set to NULL on error.
1662 * 3 : newline = How to handle newlines.
1664 * Returns : JB_ERR_OK on success
1665 * JB_ERR_MEMORY on out-of-memory
1667 *********************************************************************/
1668 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1670 struct file_line * first_line; /* Keep for return value or to free */
1671 struct file_line * cur_line; /* Current line */
1672 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1680 cur_line = first_line = zalloc(sizeof(struct file_line));
1681 if (cur_line == NULL)
1683 return JB_ERR_MEMORY;
1686 cur_line->type = FILE_LINE_UNPROCESSED;
1688 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1691 /* Out of memory or empty file. */
1692 /* Note that empty file is not an error we propogate up */
1694 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1699 prev_line = cur_line;
1700 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1701 if (cur_line == NULL)
1704 edit_free_file_lines(first_line);
1705 return JB_ERR_MEMORY;
1708 cur_line->type = FILE_LINE_UNPROCESSED;
1710 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1711 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1714 edit_free_file_lines(first_line);
1715 return JB_ERR_MEMORY;
1719 while (rval != JB_ERR_FILE);
1723 /* We allocated one too many - free it */
1724 prev_line->next = NULL;
1727 *pfile = first_line;
1732 /*********************************************************************
1734 * Function : edit_read_file
1736 * Description : Read a complete file into memory.
1737 * Handles CGI parameter parsing. If requested, also
1738 * checks the file's modification timestamp.
1741 * 1 : csp = Current client state (buffers, headers, etc...)
1742 * 2 : parameters = map of cgi parameters.
1743 * 3 : require_version = true to check "ver" parameter.
1744 * 4 : suffix = File extension, e.g. ".action".
1745 * 5 : pfile = Destination for the file. Will be set
1749 * filename : The name of the file to read, without the
1750 * path or ".action" extension.
1751 * ver : (Only if require_version is nonzero)
1752 * Timestamp of the actions file. If wrong, this
1753 * function fails with JB_ERR_MODIFIED.
1755 * Returns : JB_ERR_OK on success
1756 * JB_ERR_MEMORY on out-of-memory
1757 * JB_ERR_CGI_PARAMS if "filename" was not specified
1759 * JB_ERR_FILE if the file cannot be opened or
1761 * JB_ERR_MODIFIED if version checking was requested and
1762 * failed - the file was modified outside
1763 * of this CGI editor instance.
1765 *********************************************************************/
1766 jb_err edit_read_file(struct client_state *csp,
1767 const struct map *parameters,
1768 int require_version,
1770 struct editable_file **pfile)
1772 struct file_line * lines;
1776 const char * identifier;
1777 struct editable_file * file;
1778 unsigned version = 0;
1779 struct stat statbuf[1];
1780 char version_buf[22];
1781 int newline = NEWLINE_UNKNOWN;
1789 err = get_file_name_param(csp, parameters, "f", suffix,
1790 &filename, &identifier);
1796 if (stat(filename, statbuf) < 0)
1798 /* Error, probably file not found. */
1802 version = (unsigned) statbuf->st_mtime;
1804 if (require_version)
1806 unsigned specified_version;
1807 err = get_number_param(csp, parameters, "v", &specified_version);
1814 if (version != specified_version)
1816 return JB_ERR_MODIFIED;
1820 if (NULL == (fp = fopen(filename,"rb")))
1826 err = edit_read_file_lines(fp, &lines, &newline);
1836 file = (struct editable_file *) zalloc(sizeof(*file));
1840 edit_free_file_lines(lines);
1844 file->lines = lines;
1845 file->newline = newline;
1846 file->filename = filename;
1847 file->version = version;
1848 file->identifier = url_encode(identifier);
1850 if (file->identifier == NULL)
1852 edit_free_file(file);
1853 return JB_ERR_MEMORY;
1856 /* Correct file->version_str */
1857 freez(file->version_str);
1858 snprintf(version_buf, 22, "%u", file->version);
1859 version_buf[21] = '\0';
1860 file->version_str = strdup(version_buf);
1861 if (version_buf == NULL)
1863 edit_free_file(file);
1864 return JB_ERR_MEMORY;
1872 /*********************************************************************
1874 * Function : edit_read_actions_file
1876 * Description : Read a complete actions file into memory.
1877 * Handles CGI parameter parsing. If requested, also
1878 * checks the file's modification timestamp.
1880 * If this function detects an error in the categories
1881 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1882 * then it handles it by filling in the specified
1883 * response structure and returning JB_ERR_FILE.
1886 * 1 : csp = Current client state (buffers, headers, etc...)
1887 * 2 : rsp = HTTP response. Only filled in on error.
1888 * 2 : parameters = map of cgi parameters.
1889 * 3 : require_version = true to check "ver" parameter.
1890 * 4 : pfile = Destination for the file. Will be set
1894 * filename : The name of the actions file to read, without the
1895 * path or ".action" extension.
1896 * ver : (Only if require_version is nonzero)
1897 * Timestamp of the actions file. If wrong, this
1898 * function fails with JB_ERR_MODIFIED.
1900 * Returns : JB_ERR_OK on success
1901 * JB_ERR_MEMORY on out-of-memory
1902 * JB_ERR_CGI_PARAMS if "filename" was not specified
1904 * JB_ERR_FILE if the file does not contain valid data,
1905 * or if file cannot be opened or
1906 * contains no data, or if version
1907 * checking was requested and failed.
1909 *********************************************************************/
1910 jb_err edit_read_actions_file(struct client_state *csp,
1911 struct http_response *rsp,
1912 const struct map *parameters,
1913 int require_version,
1914 struct editable_file **pfile)
1917 struct editable_file *file;
1925 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1928 /* Try to handle if possible */
1929 if (err == JB_ERR_FILE)
1931 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1933 else if (err == JB_ERR_MODIFIED)
1935 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1937 if (err == JB_ERR_OK)
1940 * Signal to higher-level CGI code that there was a problem but we
1941 * handled it, they should just return JB_ERR_OK.
1948 err = edit_parse_actions_file(file);
1951 if (err == JB_ERR_PARSE)
1953 err = cgi_error_parse(csp, rsp, file);
1954 if (err == JB_ERR_OK)
1957 * Signal to higher-level CGI code that there was a problem but we
1958 * handled it, they should just return JB_ERR_OK.
1963 edit_free_file(file);
1972 /*********************************************************************
1974 * Function : get_file_name_param
1976 * Description : Get the name of the file to edit from the parameters
1977 * passed to a CGI function. This function handles
1978 * security checks such as blocking urls containing
1979 * "/" or ".", prepending the config file directory,
1980 * and adding the specified suffix.
1982 * (This is an essential security check, otherwise
1983 * users may be able to pass "../../../etc/passwd"
1984 * and overwrite the password file [linux], "prn:"
1985 * and print random data [Windows], etc...)
1987 * This function only allows filenames contining the
1988 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1989 * That's probably too restrictive but at least it's
1993 * 1 : csp = Current client state (buffers, headers, etc...)
1994 * 2 : parameters = map of cgi parameters
1995 * 3 : param_name = The name of the parameter to read
1996 * 4 : suffix = File extension, e.g. ".actions"
1997 * 5 : pfilename = destination for full filename. Caller
1998 * free()s. Set to NULL on error.
1999 * 6 : pparam = destination for partial filename,
2000 * suitable for use in another URL. Allocated as part
2001 * of the map "parameters", so don't free it.
2002 * Set to NULL if not specified.
2004 * Returns : JB_ERR_OK on success
2005 * JB_ERR_MEMORY on out-of-memory
2006 * JB_ERR_CGI_PARAMS if "filename" was not specified
2009 *********************************************************************/
2010 static jb_err get_file_name_param(struct client_state *csp,
2011 const struct map *parameters,
2012 const char *param_name,
2015 const char **pparam)
2033 param = lookup(parameters, param_name);
2036 return JB_ERR_CGI_PARAMS;
2041 len = strlen(param);
2042 if (len >= FILENAME_MAX)
2045 return JB_ERR_CGI_PARAMS;
2048 /* Check every character to see if it's legal */
2050 while ((ch = *s++) != '\0')
2052 if ( ((ch < 'A') || (ch > 'Z'))
2053 && ((ch < 'a') || (ch > 'z'))
2054 && ((ch < '0') || (ch > '9'))
2058 /* Probable hack attempt. */
2059 return JB_ERR_CGI_PARAMS;
2063 /* Append extension */
2064 name = malloc(len + strlen(suffix) + 1);
2067 return JB_ERR_MEMORY;
2069 strcpy(name, param);
2070 strcpy(name + len, suffix);
2073 fullpath = make_path(csp->config->confdir, name);
2076 if (fullpath == NULL)
2078 return JB_ERR_MEMORY;
2082 *pfilename = fullpath;
2088 /*********************************************************************
2090 * Function : get_url_spec_param
2092 * Description : Get a URL pattern from the parameters
2093 * passed to a CGI function. Removes leading/trailing
2094 * spaces and validates it.
2097 * 1 : csp = Current client state (buffers, headers, etc...)
2098 * 2 : parameters = map of cgi parameters
2099 * 3 : name = Name of CGI parameter to read
2100 * 4 : pvalue = destination for value. Will be malloc()'d.
2101 * Set to NULL on error.
2103 * Returns : JB_ERR_OK on success
2104 * JB_ERR_MEMORY on out-of-memory
2105 * JB_ERR_CGI_PARAMS if the parameter was not specified
2108 *********************************************************************/
2109 static jb_err get_url_spec_param(struct client_state *csp,
2110 const struct map *parameters,
2114 const char *orig_param;
2117 struct url_spec compiled[1];
2127 orig_param = lookup(parameters, name);
2130 return JB_ERR_CGI_PARAMS;
2133 /* Copy and trim whitespace */
2134 param = strdup(orig_param);
2137 return JB_ERR_MEMORY;
2141 /* Must be non-empty, and can't allow 1st character to be '{' */
2142 if (param[0] == '\0' || param[0] == '{')
2145 return JB_ERR_CGI_PARAMS;
2148 /* Check for embedded newlines */
2149 for (s = param; *s != '\0'; s++)
2151 if ((*s == '\r') || (*s == '\n'))
2154 return JB_ERR_CGI_PARAMS;
2158 /* Check that regex is valid */
2163 return JB_ERR_MEMORY;
2165 err = create_url_spec(compiled, s);
2170 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2172 free_url_spec(compiled);
2174 if (param[strlen(param) - 1] == '\\')
2177 * Must protect trailing '\\' from becoming line continuation character.
2178 * Two methods: 1) If it's a domain only, add a trailing '/'.
2179 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2181 if (strchr(param, '/') == NULL)
2183 err = string_append(¶m, "/");
2187 err = string_append(¶m, "(?:)");
2194 /* Check that the modified regex is valid */
2199 return JB_ERR_MEMORY;
2201 err = create_url_spec(compiled, s);
2206 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2208 free_url_spec(compiled);
2215 /*********************************************************************
2217 * Function : map_radio
2219 * Description : Map a set of radio button values. E.g. if you have
2220 * 3 radio buttons, declare them as:
2221 * <option type="radio" name="xyz" @xyz-a@>
2222 * <option type="radio" name="xyz" @xyz-b@>
2223 * <option type="radio" name="xyz" @xyz-c@>
2224 * Then map one of the @xyz-?@ variables to "checked"
2225 * and all the others to empty by calling:
2226 * map_radio(exports, "xyz", "abc", sel)
2227 * Where 'sel' is 'a', 'b', or 'c'.
2230 * 1 : exports = Exports map to modify.
2231 * 2 : optionname = name for map
2232 * 3 : values = null-terminated list of values;
2233 * 4 : value = Selected value.
2235 * CGI Parameters : None
2237 * Returns : JB_ERR_OK on success
2238 * JB_ERR_MEMORY on out-of-memory
2240 *********************************************************************/
2241 static jb_err map_radio(struct map * exports,
2242 const char * optionname,
2243 const char * values,
2255 len = strlen(optionname);
2256 buf = malloc(len + 3);
2259 return JB_ERR_MEMORY;
2262 strcpy(buf, optionname);
2267 while ((c = *values++) != '\0')
2272 if (map(exports, buf, 1, "", 1))
2274 return JB_ERR_MEMORY;
2280 return map(exports, buf, 0, "checked", 1);
2284 /*********************************************************************
2286 * Function : cgi_error_modified
2288 * Description : CGI function that is called when a file is modified
2289 * outside the CGI editor.
2292 * 1 : csp = Current client state (buffers, headers, etc...)
2293 * 2 : rsp = http_response data structure for output
2294 * 3 : filename = The file that was modified.
2296 * CGI Parameters : none
2298 * Returns : JB_ERR_OK on success
2299 * JB_ERR_MEMORY on out-of-memory error.
2301 *********************************************************************/
2302 jb_err cgi_error_modified(struct client_state *csp,
2303 struct http_response *rsp,
2304 const char *filename)
2306 struct map *exports;
2313 if (NULL == (exports = default_exports(csp, NULL)))
2315 return JB_ERR_MEMORY;
2318 err = map(exports, "f", 1, html_encode(filename), 0);
2325 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2329 /*********************************************************************
2331 * Function : cgi_error_parse
2333 * Description : CGI function that is called when a file cannot
2334 * be parsed by the CGI editor.
2337 * 1 : csp = Current client state (buffers, headers, etc...)
2338 * 2 : rsp = http_response data structure for output
2339 * 3 : file = The file that was modified.
2341 * CGI Parameters : none
2343 * Returns : JB_ERR_OK on success
2344 * JB_ERR_MEMORY on out-of-memory error.
2346 *********************************************************************/
2347 jb_err cgi_error_parse(struct client_state *csp,
2348 struct http_response *rsp,
2349 struct editable_file *file)
2351 struct map *exports;
2353 struct file_line *cur_line;
2359 if (NULL == (exports = default_exports(csp, NULL)))
2361 return JB_ERR_MEMORY;
2364 err = map(exports, "f", 1, file->identifier, 1);
2365 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2367 cur_line = file->parse_error;
2370 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2371 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2379 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2383 /*********************************************************************
2385 * Function : cgi_error_file
2387 * Description : CGI function that is called when a file cannot be
2388 * opened by the CGI editor.
2391 * 1 : csp = Current client state (buffers, headers, etc...)
2392 * 2 : rsp = http_response data structure for output
2393 * 3 : filename = The file that was modified.
2395 * CGI Parameters : none
2397 * Returns : JB_ERR_OK on success
2398 * JB_ERR_MEMORY on out-of-memory error.
2400 *********************************************************************/
2401 jb_err cgi_error_file(struct client_state *csp,
2402 struct http_response *rsp,
2403 const char *filename)
2405 struct map *exports;
2412 if (NULL == (exports = default_exports(csp, NULL)))
2414 return JB_ERR_MEMORY;
2417 err = map(exports, "f", 1, html_encode(filename), 0);
2424 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2428 /*********************************************************************
2430 * Function : cgi_error_file
2432 * Description : CGI function that is called when a file cannot be
2433 * opened for writing by the CGI editor.
2436 * 1 : csp = Current client state (buffers, headers, etc...)
2437 * 2 : rsp = http_response data structure for output
2438 * 3 : filename = The file that we can't write to
2440 * CGI Parameters : none
2442 * Returns : JB_ERR_OK on success
2443 * JB_ERR_MEMORY on out-of-memory error.
2445 *********************************************************************/
2446 jb_err cgi_error_file_read_only(struct client_state *csp,
2447 struct http_response *rsp,
2448 const char *filename)
2450 struct map *exports;
2457 if (NULL == (exports = default_exports(csp, NULL)))
2459 return JB_ERR_MEMORY;
2462 err = map(exports, "f", 1, html_encode(filename), 0);
2469 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2473 /*********************************************************************
2475 * Function : cgi_edit_actions
2477 * Description : CGI function that allows the user to choose which
2478 * actions file to edit.
2481 * 1 : csp = Current client state (buffers, headers, etc...)
2482 * 2 : rsp = http_response data structure for output
2483 * 3 : parameters = map of cgi parameters
2485 * CGI Parameters : None
2487 * Returns : JB_ERR_OK on success
2488 * JB_ERR_MEMORY on out-of-memory error
2490 *********************************************************************/
2491 jb_err cgi_edit_actions(struct client_state *csp,
2492 struct http_response *rsp,
2493 const struct map *parameters)
2496 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2498 return cgi_error_disabled(csp, rsp);
2501 /* FIXME: Incomplete */
2503 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2508 /*********************************************************************
2510 * Function : cgi_edit_actions_list
2512 * Description : CGI function that edits the actions list.
2513 * FIXME: This function shouldn't FATAL ever.
2514 * FIXME: This function doesn't check the retval of map()
2516 * 1 : csp = Current client state (buffers, headers, etc...)
2517 * 2 : rsp = http_response data structure for output
2518 * 3 : parameters = map of cgi parameters
2520 * CGI Parameters : filename
2522 * Returns : JB_ERR_OK on success
2523 * JB_ERR_MEMORY on out-of-memory
2524 * JB_ERR_FILE if the file cannot be opened or
2526 * JB_ERR_CGI_PARAMS if "filename" was not specified
2529 *********************************************************************/
2530 jb_err cgi_edit_actions_list(struct client_state *csp,
2531 struct http_response *rsp,
2532 const struct map *parameters)
2534 char * section_template;
2535 char * url_template;
2540 struct map * exports;
2541 struct map * section_exports;
2542 struct map * url_exports;
2543 struct editable_file * file;
2544 struct file_line * cur_line;
2545 unsigned line_number = 0;
2546 unsigned prev_section_line_number = ((unsigned) (-1));
2548 struct file_list * fl;
2549 struct url_actions * b;
2550 char * buttons = NULL;
2553 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2555 return cgi_error_disabled(csp, rsp);
2558 if (NULL == (exports = default_exports(csp, NULL)))
2560 return JB_ERR_MEMORY;
2563 /* Load actions file */
2564 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2567 /* No filename specified, can't read file, or out of memory. */
2568 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2571 /* Find start of actions in file */
2572 cur_line = file->lines;
2574 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2576 cur_line = cur_line->next;
2581 * Conventional actions files should have a match all block
2583 * cur_line = {...global actions...}
2584 * cur_line->next = /
2585 * cur_line->next->next = {...actions...} or EOF
2587 if ( (cur_line != NULL)
2588 && (cur_line->type == FILE_LINE_ACTION)
2589 && (cur_line->next != NULL)
2590 && (cur_line->next->type == FILE_LINE_URL)
2591 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2592 && ( (cur_line->next->next == NULL)
2593 || (cur_line->next->next->type != FILE_LINE_URL)
2597 * Generate string with buttons to set actions for "/" to
2598 * any predefined set of actions (named standard.*, probably
2599 * residing in standard.action).
2602 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2605 edit_free_file(file);
2607 if (err == JB_ERR_FILE)
2609 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2614 err = template_fill(§ion_template, exports);
2617 edit_free_file(file);
2622 buttons = strdup("");
2623 for (i = 0; i < MAX_AF_FILES; i++)
2625 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2627 for (b = b->next; NULL != b; b = b->next)
2629 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2631 if (err || (NULL == (section_exports = new_map())))
2634 free(section_template);
2635 edit_free_file(file);
2637 return JB_ERR_MEMORY;
2640 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2642 if (err || (NULL == (s = strdup(section_template))))
2644 free_map(section_exports);
2646 free(section_template);
2647 edit_free_file(file);
2649 return JB_ERR_MEMORY;
2652 if (!err) err = template_fill(&s, section_exports);
2653 free_map(section_exports);
2654 if (!err) err = string_join(&buttons, s);
2659 freez(section_template);
2660 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2663 * Conventional actions file, supply extra editing help.
2664 * (e.g. don't allow them to make it an unconventional one).
2666 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2668 snprintf(buf, 150, "%d", line_number);
2669 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2670 snprintf(buf, 150, "%d", line_number + 2);
2671 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2672 if (!err) err = map(exports, "all-urls-actions", 1,
2673 actions_to_html(csp, cur_line->data.action), 0);
2675 /* Skip the 2 lines */
2676 cur_line = cur_line->next->next;
2680 * Note that prev_section_line_number is NOT set here.
2681 * This is deliberate and not a bug. It stops a "Move up"
2682 * option appearing on the next section. Clicking "Move
2683 * up" would make the actions file unconventional, which
2684 * we don't want, so we hide this option.
2690 * Non-standard actions file - does not begin with
2691 * the "All URLs" section.
2693 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2696 /* Set up global exports */
2698 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2699 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2701 /* Discourage private additions to default.action */
2703 if (!err) err = map_conditional(exports, "default-action",
2704 (strcmp("default", lookup(parameters, "f")) == 0));
2707 edit_free_file(file);
2712 /* Should do all global exports above this point */
2714 /* Load templates */
2716 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2719 edit_free_file(file);
2721 if (err == JB_ERR_FILE)
2723 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2728 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2731 free(section_template);
2732 edit_free_file(file);
2734 if (err == JB_ERR_FILE)
2736 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2741 err = template_fill(§ion_template, exports);
2745 edit_free_file(file);
2751 err = template_fill(&url_template, exports);
2754 free(section_template);
2755 edit_free_file(file);
2760 if (NULL == (sections = strdup("")))
2762 free(section_template);
2764 edit_free_file(file);
2766 return JB_ERR_MEMORY;
2769 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2771 if (NULL == (section_exports = new_map()))
2774 free(section_template);
2776 edit_free_file(file);
2778 return JB_ERR_MEMORY;
2781 snprintf(buf, 150, "%d", line_number);
2782 err = map(section_exports, "s", 1, buf, 1);
2783 if (!err) err = map(section_exports, "actions", 1,
2784 actions_to_html(csp, cur_line->data.action), 0);
2787 && (cur_line->next != NULL)
2788 && (cur_line->next->type == FILE_LINE_URL))
2790 /* This section contains at least one URL, don't allow delete */
2791 err = map_block_killer(section_exports, "empty-section");
2795 if (!err) err = map_block_keep(section_exports, "empty-section");
2798 if (prev_section_line_number != ((unsigned)(-1)))
2800 /* Not last section */
2801 snprintf(buf, 150, "%d", prev_section_line_number);
2802 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2803 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2808 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2810 prev_section_line_number = line_number;
2815 free(section_template);
2817 edit_free_file(file);
2819 free_map(section_exports);
2823 /* Should do all section-specific exports above this point */
2825 if (NULL == (urls = strdup("")))
2828 free(section_template);
2830 edit_free_file(file);
2832 free_map(section_exports);
2833 return JB_ERR_MEMORY;
2838 cur_line = cur_line->next;
2841 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2843 if (NULL == (url_exports = new_map()))
2847 free(section_template);
2849 edit_free_file(file);
2851 free_map(section_exports);
2852 return JB_ERR_MEMORY;
2855 snprintf(buf, 150, "%d", line_number);
2856 err = map(url_exports, "p", 1, buf, 1);
2858 snprintf(buf, 150, "%d", url_1_2);
2859 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2861 if (!err) err = map(url_exports, "url-html", 1,
2862 html_encode(cur_line->unprocessed), 0);
2863 if (!err) err = map(url_exports, "url", 1,
2864 url_encode(cur_line->unprocessed), 0);
2870 free(section_template);
2872 edit_free_file(file);
2874 free_map(section_exports);
2875 free_map(url_exports);
2879 if (NULL == (s = strdup(url_template)))
2883 free(section_template);
2885 edit_free_file(file);
2887 free_map(section_exports);
2888 free_map(url_exports);
2889 return JB_ERR_MEMORY;
2892 err = template_fill(&s, section_exports);
2893 if (!err) err = template_fill(&s, url_exports);
2894 if (!err) err = string_append(&urls, s);
2896 free_map(url_exports);
2903 free(section_template);
2905 edit_free_file(file);
2907 free_map(section_exports);
2911 url_1_2 = 3 - url_1_2;
2913 cur_line = cur_line->next;
2917 err = map(section_exports, "urls", 1, urls, 0);
2919 /* Could also do section-specific exports here, but it wouldn't be as fast */
2921 snprintf(buf, 150, "%d", line_number);
2922 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2924 if ( (cur_line != NULL)
2925 && (cur_line->type == FILE_LINE_ACTION))
2927 /* Not last section */
2928 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2933 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2939 free(section_template);
2941 edit_free_file(file);
2943 free_map(section_exports);
2947 if (NULL == (s = strdup(section_template)))
2950 free(section_template);
2952 edit_free_file(file);
2954 free_map(section_exports);
2955 return JB_ERR_MEMORY;
2958 err = template_fill(&s, section_exports);
2959 if (!err) err = string_append(§ions, s);
2962 free_map(section_exports);
2967 free(section_template);
2969 edit_free_file(file);
2975 edit_free_file(file);
2976 free(section_template);
2979 err = map(exports, "sections", 1, sections, 0);
2986 /* Could also do global exports here, but it wouldn't be as fast */
2988 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2992 /*********************************************************************
2994 * Function : cgi_edit_actions_for_url
2996 * Description : CGI function that edits the Actions list.
2999 * 1 : csp = Current client state (buffers, headers, etc...)
3000 * 2 : rsp = http_response data structure for output
3001 * 3 : parameters = map of cgi parameters
3003 * CGI Parameters : None
3005 * Returns : JB_ERR_OK on success
3006 * JB_ERR_MEMORY on out-of-memory
3007 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3008 * specified or not valid.
3010 *********************************************************************/
3011 jb_err cgi_edit_actions_for_url(struct client_state *csp,
3012 struct http_response *rsp,
3013 const struct map *parameters)
3015 struct map * exports;
3017 struct editable_file * file;
3018 struct file_line * cur_line;
3019 unsigned line_number;
3021 struct re_filterfile_spec *filter_group;
3022 int i, have_filters = 0;
3024 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3026 return cgi_error_disabled(csp, rsp);
3029 err = get_number_param(csp, parameters, "s", §ionid);
3035 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3038 /* No filename specified, can't read file, modified, or out of memory. */
3039 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3042 cur_line = file->lines;
3044 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3046 cur_line = cur_line->next;
3049 if ( (cur_line == NULL)
3050 || (line_number != sectionid)
3052 || (cur_line->type != FILE_LINE_ACTION))
3054 /* Invalid "sectionid" parameter */
3055 edit_free_file(file);
3056 return JB_ERR_CGI_PARAMS;
3059 if (NULL == (exports = default_exports(csp, NULL)))
3061 edit_free_file(file);
3062 return JB_ERR_MEMORY;
3065 err = map(exports, "f", 1, file->identifier, 1);
3066 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3067 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3069 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3072 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3073 * length limitation and ignore clicks on the Submit buttons if
3074 * the resulting GET URL would be longer than their limit.
3076 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3077 * reached this limit and action editing stopped working in these
3078 * browsers (BR #1570678).
3080 * The config option split-large-forms works around this browser
3081 * bug (HTTP has no URL lenght limitation) by deviding the action
3082 * list form into multiple smaller ones. It means the URLs are shorter
3083 * and work in broken browsers as well, but the user can no longer change
3084 * all actions with one submit.
3086 * A better solution would be to switch to POST requests,
3087 * but this will do for now.
3089 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3091 /* Generate multiple smaller form by killing the big one. */
3092 err = map_block_killer(exports, "one-form-only");
3096 /* Default: Generate one large form by killing the smaller ones. */
3097 err = map_block_killer(exports, "multiple-forms");
3100 for (i = 0; i < MAX_AF_FILES; i++)
3102 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3104 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3112 edit_free_file(file);
3117 if (0 == have_filters)
3119 err = map(exports, "filter-params", 1, "", 1);
3124 * List available filters and their settings.
3126 char *content_filter_params;
3127 char *server_header_filter_params;
3128 char *client_header_filter_params;
3129 char *filter_template;
3130 int filter_identifier = 0;
3132 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3135 edit_free_file(file);
3137 if (err == JB_ERR_FILE)
3139 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3144 err = template_fill(&filter_template, exports);
3146 content_filter_params = strdup("");
3147 server_header_filter_params = strdup("");
3148 client_header_filter_params = strdup("");
3150 for (i = 0; i < MAX_AF_FILES; i++)
3152 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3154 filter_group = csp->rlist[i]->f;
3155 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3157 char current_mode = 'x';
3159 int multi_action_index = 0;
3160 struct list_entry *filter_name;
3161 struct map *line_exports;
3164 char *abbr_filter_type;
3166 char **current_params;
3168 switch (filter_group->type)
3170 case FT_CONTENT_FILTER:
3171 /* XXX: Should we call it content-filter instead? */
3172 filter_type = "filter";
3173 abbr_filter_type = "F";
3174 multi_action_index = ACTION_MULTI_FILTER;
3176 current_params = &content_filter_params;
3178 case FT_SERVER_HEADER_FILTER:
3179 filter_type = "server-header-filter";
3180 abbr_filter_type = "S";
3181 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3182 current_params = &server_header_filter_params;
3183 anchor = "SERVER-HEADER-FILTER"; /* XXX: no documentation available yet */
3185 case FT_CLIENT_HEADER_FILTER:
3186 filter_type = "client-header-filter";
3187 abbr_filter_type = "C";
3188 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3189 current_params = &client_header_filter_params;
3190 anchor = "CLIENT-HEADER-FILTER"; /* XXX: no documentation available yet */
3193 log_error(LOG_LEVEL_FATAL,
3194 "cgi_edit_actions_for_url: Unknown filter type: %u for filter %s.",
3195 filter_group->type, filter_group->name);
3198 assert(multi_action_index);
3200 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
3201 while ((filter_name != NULL)
3202 && (0 != strcmp(filter_group->name, filter_name->str)))
3204 filter_name = filter_name->next;
3207 if (filter_name != NULL)
3213 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
3214 while ((filter_name != NULL)
3215 && (0 != strcmp(filter_group->name, filter_name->str)))
3217 filter_name = filter_name->next;
3219 if (filter_name != NULL)
3225 /* Generate a unique serial number */
3226 snprintf(number, sizeof(number), "%x", filter_identifier++);
3227 number[sizeof(number) - 1] = '\0';
3229 line_exports = new_map();
3230 if (line_exports == NULL)
3232 err = JB_ERR_MEMORY;
3233 freez(*current_params); /* XXX: really necessary? */
3237 if (!err) err = map(line_exports, "index", 1, number, 1);
3238 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3239 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3240 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3241 if (!err) err = map(line_exports, "filter-type", 1, filter_type, 1);
3242 if (!err) err = map(line_exports, "abbr-filter-type", 1, abbr_filter_type, 1);
3243 if (!err) err = map(line_exports, "anchor", 1, anchor, 1);
3248 this_line = strdup(filter_template);
3249 if (this_line == NULL) err = JB_ERR_MEMORY;
3251 if (!err) err = template_fill(&this_line, line_exports);
3252 string_join(current_params, this_line);
3254 free_map(line_exports);
3259 freez(filter_template);
3261 if (!err) err = map(exports, "content-filter-params", 1, content_filter_params, 0);
3262 if (!err) err = map(exports, "server-header-filter-params", 1, server_header_filter_params, 0);
3263 if (!err) err = map(exports, "client-header-filter-params", 1, client_header_filter_params, 0);
3266 freez(content_filter_params);
3267 freez(server_header_filter_params);
3268 freez(client_header_filter_params);
3272 if (!err) err = map_radio(exports, "filter-all", "nx",
3273 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3275 edit_free_file(file);
3283 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3287 /*********************************************************************
3289 * Function : cgi_edit_actions_submit
3291 * Description : CGI function that actually edits the Actions list.
3294 * 1 : csp = Current client state (buffers, headers, etc...)
3295 * 2 : rsp = http_response data structure for output
3296 * 3 : parameters = map of cgi parameters
3298 * CGI Parameters : None
3300 * Returns : JB_ERR_OK on success
3301 * JB_ERR_MEMORY on out-of-memory
3302 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3303 * specified or not valid.
3305 *********************************************************************/
3306 jb_err cgi_edit_actions_submit(struct client_state *csp,
3307 struct http_response *rsp,
3308 const struct map *parameters)
3314 struct editable_file * file;
3315 struct file_line * cur_line;
3316 unsigned line_number;
3319 int filter_identifier;
3320 const char * action_set_name;
3322 struct file_list * fl;
3323 struct url_actions * b;
3325 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3327 return cgi_error_disabled(csp, rsp);
3330 err = get_number_param(csp, parameters, "s", §ionid);
3336 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3339 /* No filename specified, can't read file, modified, or out of memory. */
3340 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3343 cur_line = file->lines;
3345 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3347 cur_line = cur_line->next;
3350 if ( (cur_line == NULL)
3351 || (line_number != sectionid)
3353 || (cur_line->type != FILE_LINE_ACTION))
3355 /* Invalid "sectionid" parameter */
3356 edit_free_file(file);
3357 return JB_ERR_CGI_PARAMS;
3360 get_string_param(parameters, "p", &action_set_name);
3361 if (action_set_name != NULL)
3363 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3365 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3367 for (b = b->next; NULL != b; b = b->next)
3369 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3371 copy_action(cur_line->data.action, b->action);
3377 edit_free_file(file);
3378 return JB_ERR_CGI_PARAMS;
3384 err = actions_from_radio(parameters, cur_line->data.action);
3390 edit_free_file(file);
3394 ch = get_char_param(parameters, "filter_all");
3397 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3398 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3399 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3403 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3406 for (filter_identifier = 0; !err; filter_identifier++)
3413 * Filter state. Valid states are: 'Y' (active),
3414 * 'N' (inactive) and 'X' (no change).
3418 * Abbreviated filter type. Valid types are: 'F' (content filter),
3419 * 'S' (server-header filter) and 'C' (client-header filter).
3421 int multi_action_index = 0;
3423 /* Generate the keys */
3424 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3425 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3426 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3427 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3428 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3430 err = get_string_param(parameters, key_name, &name);
3439 type = get_char_param(parameters, key_type);
3443 multi_action_index = ACTION_MULTI_FILTER;
3446 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3449 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3452 log_error(LOG_LEVEL_ERROR,
3453 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3456 assert(multi_action_index);
3458 value = get_char_param(parameters, key_value);
3461 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3462 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3463 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3465 else if (value == 'N')
3467 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3468 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3470 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3471 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3474 else if (value == 'X')
3476 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3477 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3484 edit_free_file(file);
3488 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3491 edit_free_file(file);
3492 return JB_ERR_MEMORY;
3495 len = strlen(actiontext);
3499 * Empty action - must special-case this.
3500 * Simply setting len to 1 is sufficient...
3505 if (NULL == (newtext = malloc(len + 2)))
3509 edit_free_file(file);
3510 return JB_ERR_MEMORY;
3512 strcpy(newtext, actiontext);
3516 newtext[len + 1] = '\0';
3518 freez(cur_line->raw);
3519 freez(cur_line->unprocessed);
3520 cur_line->unprocessed = newtext;
3522 err = edit_write_file(file);
3525 /* Error writing file */
3526 if (err == JB_ERR_FILE)
3528 /* Read-only file. */
3529 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3531 edit_free_file(file);
3535 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3536 (long) time(NULL), file->identifier, sectionid);
3538 edit_free_file(file);
3540 return cgi_redirect(rsp, target);
3544 /*********************************************************************
3546 * Function : cgi_edit_actions_url
3548 * Description : CGI function that actually edits a URL pattern in
3552 * 1 : csp = Current client state (buffers, headers, etc...)
3553 * 2 : rsp = http_response data structure for output
3554 * 3 : parameters = map of cgi parameters
3557 * filename : Identifies the file to edit
3558 * ver : File's last-modified time
3559 * section : Line number of section to edit
3560 * pattern : Line number of pattern to edit
3561 * newval : New value for pattern
3563 * Returns : JB_ERR_OK on success
3564 * JB_ERR_MEMORY on out-of-memory
3565 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3566 * specified or not valid.
3568 *********************************************************************/
3569 jb_err cgi_edit_actions_url(struct client_state *csp,
3570 struct http_response *rsp,
3571 const struct map *parameters)
3575 struct editable_file * file;
3576 struct file_line * cur_line;
3577 unsigned line_number;
3578 unsigned section_start_line_number = 0;
3586 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3588 return cgi_error_disabled(csp, rsp);
3591 err = get_number_param(csp, parameters, "p", &patternid);
3598 return JB_ERR_CGI_PARAMS;
3601 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3607 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3610 /* No filename specified, can't read file, modified, or out of memory. */
3612 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3616 cur_line = file->lines;
3618 while ((cur_line != NULL) && (line_number < patternid))
3620 if (cur_line->type == FILE_LINE_ACTION)
3622 section_start_line_number = line_number;
3624 cur_line = cur_line->next;
3628 if ( (cur_line == NULL)
3629 || (cur_line->type != FILE_LINE_URL))
3631 /* Invalid "patternid" parameter */
3633 edit_free_file(file);
3634 return JB_ERR_CGI_PARAMS;
3637 /* At this point, the line to edit is in cur_line */
3639 freez(cur_line->raw);
3640 freez(cur_line->unprocessed);
3641 cur_line->unprocessed = new_pattern;
3643 err = edit_write_file(file);
3646 /* Error writing file */
3647 if (err == JB_ERR_FILE)
3649 /* Read-only file. */
3650 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3652 edit_free_file(file);
3656 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3657 (long) time(NULL), file->identifier, section_start_line_number);
3659 edit_free_file(file);
3661 return cgi_redirect(rsp, target);
3665 /*********************************************************************
3667 * Function : cgi_edit_actions_add_url
3669 * Description : CGI function that actually adds a URL pattern to
3673 * 1 : csp = Current client state (buffers, headers, etc...)
3674 * 2 : rsp = http_response data structure for output
3675 * 3 : parameters = map of cgi parameters
3678 * filename : Identifies the file to edit
3679 * ver : File's last-modified time
3680 * section : Line number of section to edit
3681 * newval : New pattern
3683 * Returns : JB_ERR_OK on success
3684 * JB_ERR_MEMORY on out-of-memory
3685 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3686 * specified or not valid.
3688 *********************************************************************/
3689 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3690 struct http_response *rsp,
3691 const struct map *parameters)
3695 struct file_line * new_line;
3696 struct editable_file * file;
3697 struct file_line * cur_line;
3698 unsigned line_number;
3702 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3704 return cgi_error_disabled(csp, rsp);
3707 err = get_number_param(csp, parameters, "s", §ionid);
3714 return JB_ERR_CGI_PARAMS;
3717 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3723 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3726 /* No filename specified, can't read file, modified, or out of memory. */
3728 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3732 cur_line = file->lines;
3734 while ((cur_line != NULL) && (line_number < sectionid))
3736 cur_line = cur_line->next;
3740 if ( (cur_line == NULL)
3741 || (cur_line->type != FILE_LINE_ACTION))
3743 /* Invalid "sectionid" parameter */
3745 edit_free_file(file);
3746 return JB_ERR_CGI_PARAMS;
3749 /* At this point, the section header is in cur_line - add after this. */
3751 /* Allocate the new line */
3752 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3753 if (new_line == NULL)
3756 edit_free_file(file);
3757 return JB_ERR_MEMORY;
3760 /* Fill in the data members of the new line */
3761 new_line->raw = NULL;
3762 new_line->prefix = NULL;
3763 new_line->unprocessed = new_pattern;
3764 new_line->type = FILE_LINE_URL;
3766 /* Link new_line into the list, after cur_line */
3767 new_line->next = cur_line->next;
3768 cur_line->next = new_line;
3770 /* Done making changes, now commit */
3772 err = edit_write_file(file);
3775 /* Error writing file */
3776 if (err == JB_ERR_FILE)
3778 /* Read-only file. */
3779 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3781 edit_free_file(file);
3785 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3786 (long) time(NULL), file->identifier, sectionid);
3788 edit_free_file(file);
3790 return cgi_redirect(rsp, target);
3794 /*********************************************************************
3796 * Function : cgi_edit_actions_remove_url
3798 * Description : CGI function that actually removes a URL pattern from
3802 * 1 : csp = Current client state (buffers, headers, etc...)
3803 * 2 : rsp = http_response data structure for output
3804 * 3 : parameters = map of cgi parameters
3807 * f : (filename) Identifies the file to edit
3808 * v : (version) File's last-modified time
3809 * p : (pattern) Line number of pattern to remove
3811 * Returns : JB_ERR_OK on success
3812 * JB_ERR_MEMORY on out-of-memory
3813 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3814 * specified or not valid.
3816 *********************************************************************/
3817 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3818 struct http_response *rsp,
3819 const struct map *parameters)
3822 struct editable_file * file;
3823 struct file_line * cur_line;
3824 struct file_line * prev_line;
3825 unsigned line_number;
3826 unsigned section_start_line_number = 0;
3830 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3832 return cgi_error_disabled(csp, rsp);
3835 err = get_number_param(csp, parameters, "p", &patternid);
3841 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3844 /* No filename specified, can't read file, modified, or out of memory. */
3845 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3850 cur_line = file->lines;
3852 while ((cur_line != NULL) && (line_number < patternid))
3854 if (cur_line->type == FILE_LINE_ACTION)
3856 section_start_line_number = line_number;
3858 prev_line = cur_line;
3859 cur_line = cur_line->next;
3863 if ( (cur_line == NULL)
3864 || (prev_line == NULL)
3865 || (cur_line->type != FILE_LINE_URL))
3867 /* Invalid "patternid" parameter */
3868 edit_free_file(file);
3869 return JB_ERR_CGI_PARAMS;
3872 /* At this point, the line to remove is in cur_line, and the previous
3873 * one is in prev_line
3876 /* Unlink cur_line */
3877 prev_line->next = cur_line->next;
3878 cur_line->next = NULL;
3881 edit_free_file_lines(cur_line);
3883 err = edit_write_file(file);
3886 /* Error writing file */
3887 if (err == JB_ERR_FILE)
3889 /* Read-only file. */
3890 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3892 edit_free_file(file);
3896 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3897 (long) time(NULL), file->identifier, section_start_line_number);
3899 edit_free_file(file);
3901 return cgi_redirect(rsp, target);
3905 /*********************************************************************
3907 * Function : cgi_edit_actions_section_remove
3909 * Description : CGI function that actually removes a whole section from
3910 * the actions file. The section must be empty first
3911 * (else JB_ERR_CGI_PARAMS).
3914 * 1 : csp = Current client state (buffers, headers, etc...)
3915 * 2 : rsp = http_response data structure for output
3916 * 3 : parameters = map of cgi parameters
3919 * f : (filename) Identifies the file to edit
3920 * v : (version) File's last-modified time
3921 * s : (section) Line number of section to edit
3923 * Returns : JB_ERR_OK on success
3924 * JB_ERR_MEMORY on out-of-memory
3925 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3926 * specified or not valid.
3928 *********************************************************************/
3929 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3930 struct http_response *rsp,
3931 const struct map *parameters)
3934 struct editable_file * file;
3935 struct file_line * cur_line;
3936 struct file_line * prev_line;
3937 unsigned line_number;
3941 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3943 return cgi_error_disabled(csp, rsp);
3946 err = get_number_param(csp, parameters, "s", §ionid);
3952 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3955 /* No filename specified, can't read file, modified, or out of memory. */
3956 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3960 cur_line = file->lines;
3963 while ((cur_line != NULL) && (line_number < sectionid))
3965 prev_line = cur_line;
3966 cur_line = cur_line->next;
3970 if ( (cur_line == NULL)
3971 || (cur_line->type != FILE_LINE_ACTION) )
3973 /* Invalid "sectionid" parameter */
3974 edit_free_file(file);
3975 return JB_ERR_CGI_PARAMS;
3978 if ( (cur_line->next != NULL)
3979 && (cur_line->next->type == FILE_LINE_URL) )
3981 /* Section not empty. */
3982 edit_free_file(file);
3983 return JB_ERR_CGI_PARAMS;
3986 /* At this point, the line to remove is in cur_line, and the previous
3987 * one is in prev_line
3990 /* Unlink cur_line */
3991 if (prev_line == NULL)
3993 /* Removing the first line from the file */
3994 file->lines = cur_line->next;
3998 prev_line->next = cur_line->next;
4000 cur_line->next = NULL;
4003 edit_free_file_lines(cur_line);
4005 err = edit_write_file(file);
4008 /* Error writing file */
4009 if (err == JB_ERR_FILE)
4011 /* Read-only file. */
4012 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4014 edit_free_file(file);
4018 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4019 (long) time(NULL), file->identifier);
4021 edit_free_file(file);
4023 return cgi_redirect(rsp, target);
4027 /*********************************************************************
4029 * Function : cgi_edit_actions_section_add
4031 * Description : CGI function that adds a new empty section to
4035 * 1 : csp = Current client state (buffers, headers, etc...)
4036 * 2 : rsp = http_response data structure for output
4037 * 3 : parameters = map of cgi parameters
4040 * f : (filename) Identifies the file to edit
4041 * v : (version) File's last-modified time
4042 * s : (section) Line number of section to add after, 0 for
4045 * Returns : JB_ERR_OK on success
4046 * JB_ERR_MEMORY on out-of-memory
4047 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4048 * specified or not valid.
4050 *********************************************************************/
4051 jb_err cgi_edit_actions_section_add(struct client_state *csp,
4052 struct http_response *rsp,
4053 const struct map *parameters)
4056 struct file_line * new_line;
4058 struct editable_file * file;
4059 struct file_line * cur_line;
4060 unsigned line_number;
4064 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4066 return cgi_error_disabled(csp, rsp);
4069 err = get_number_param(csp, parameters, "s", §ionid);
4075 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4078 /* No filename specified, can't read file, modified, or out of memory. */
4079 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4083 cur_line = file->lines;
4085 if (sectionid <= 1U)
4087 /* Add to start of file */
4088 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4090 /* There's something in the file, find the line before the first
4093 while ( (cur_line->next != NULL)
4094 && (cur_line->next->type != FILE_LINE_ACTION) )
4096 cur_line = cur_line->next;
4102 /* File starts with action line, so insert at top */
4108 /* Add after stated section. */
4109 while ((cur_line != NULL) && (line_number < sectionid))
4111 cur_line = cur_line->next;
4115 if ( (cur_line == NULL)
4116 || (cur_line->type != FILE_LINE_ACTION))
4118 /* Invalid "sectionid" parameter */
4119 edit_free_file(file);
4120 return JB_ERR_CGI_PARAMS;
4123 /* Skip through the section to find the last line in it. */
4124 while ( (cur_line->next != NULL)
4125 && (cur_line->next->type != FILE_LINE_ACTION) )
4127 cur_line = cur_line->next;
4132 /* At this point, the last line in the previous section is in cur_line
4133 * - add after this. (Or if we need to add as the first line, cur_line
4137 new_text = strdup("{}");
4138 if (NULL == new_text)
4140 edit_free_file(file);
4141 return JB_ERR_MEMORY;
4144 /* Allocate the new line */
4145 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4146 if (new_line == NULL)
4149 edit_free_file(file);
4150 return JB_ERR_MEMORY;
4153 /* Fill in the data members of the new line */
4154 new_line->raw = NULL;
4155 new_line->prefix = NULL;
4156 new_line->unprocessed = new_text;
4157 new_line->type = FILE_LINE_ACTION;
4159 if (cur_line != NULL)
4161 /* Link new_line into the list, after cur_line */
4162 new_line->next = cur_line->next;
4163 cur_line->next = new_line;
4167 /* Link new_line into the list, as first line */
4168 new_line->next = file->lines;
4169 file->lines = new_line;
4172 /* Done making changes, now commit */
4174 err = edit_write_file(file);
4177 /* Error writing file */
4178 if (err == JB_ERR_FILE)
4180 /* Read-only file. */
4181 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4183 edit_free_file(file);
4187 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4188 (long) time(NULL), file->identifier);
4190 edit_free_file(file);
4192 return cgi_redirect(rsp, target);
4196 /*********************************************************************
4198 * Function : cgi_edit_actions_section_swap
4200 * Description : CGI function that swaps the order of two sections
4201 * in the actions file. Note that this CGI can actually
4202 * swap any two arbitrary sections, but the GUI interface
4203 * currently only allows consecutive sections to be
4207 * 1 : csp = Current client state (buffers, headers, etc...)
4208 * 2 : rsp = http_response data structure for output
4209 * 3 : parameters = map of cgi parameters
4212 * f : (filename) Identifies the file to edit
4213 * v : (version) File's last-modified time
4214 * s1 : (section1) Line number of first section to swap
4215 * s2 : (section2) Line number of second section to swap
4217 * Returns : JB_ERR_OK on success
4218 * JB_ERR_MEMORY on out-of-memory
4219 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4220 * specified or not valid.
4222 *********************************************************************/
4223 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4224 struct http_response *rsp,
4225 const struct map *parameters)
4229 struct editable_file * file;
4230 struct file_line * cur_line;
4231 struct file_line * prev_line;
4232 struct file_line * line_before_section1;
4233 struct file_line * line_start_section1;
4234 struct file_line * line_end_section1;
4235 struct file_line * line_after_section1;
4236 struct file_line * line_before_section2;
4237 struct file_line * line_start_section2;
4238 struct file_line * line_end_section2;
4239 struct file_line * line_after_section2;
4240 unsigned line_number;
4244 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4246 return cgi_error_disabled(csp, rsp);
4249 err = get_number_param(csp, parameters, "s1", §ion1);
4250 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4256 if (section1 > section2)
4258 unsigned temp = section2;
4259 section2 = section1;
4263 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4266 /* No filename specified, can't read file, modified, or out of memory. */
4267 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4270 /* Start at the beginning... */
4272 cur_line = file->lines;
4275 /* ... find section1 ... */
4276 while ((cur_line != NULL) && (line_number < section1))
4278 prev_line = cur_line;
4279 cur_line = cur_line->next;
4283 if ( (cur_line == NULL)
4284 || (cur_line->type != FILE_LINE_ACTION) )
4286 /* Invalid "section1" parameter */
4287 edit_free_file(file);
4288 return JB_ERR_CGI_PARAMS;
4291 /* If no-op, we've validated params and can skip the rest. */
4292 if (section1 != section2)
4294 /* ... find the end of section1 ... */
4295 line_before_section1 = prev_line;
4296 line_start_section1 = cur_line;
4299 prev_line = cur_line;
4300 cur_line = cur_line->next;
4303 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4304 line_end_section1 = prev_line;
4305 line_after_section1 = cur_line;
4307 /* ... find section2 ... */
4308 while ((cur_line != NULL) && (line_number < section2))
4310 prev_line = cur_line;
4311 cur_line = cur_line->next;
4315 if ( (cur_line == NULL)
4316 || (cur_line->type != FILE_LINE_ACTION) )
4318 /* Invalid "section2" parameter */
4319 edit_free_file(file);
4320 return JB_ERR_CGI_PARAMS;
4323 /* ... find the end of section2 ... */
4324 line_before_section2 = prev_line;
4325 line_start_section2 = cur_line;
4328 prev_line = cur_line;
4329 cur_line = cur_line->next;
4332 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4333 line_end_section2 = prev_line;
4334 line_after_section2 = cur_line;
4336 /* Now have all the pointers we need. Do the swap. */
4338 /* Change the pointer to section1 to point to section2 instead */
4339 if (line_before_section1 == NULL)
4341 file->lines = line_start_section2;
4345 line_before_section1->next = line_start_section2;
4348 if (line_before_section2 == line_end_section1)
4350 /* Consecutive sections */
4351 line_end_section2->next = line_start_section1;
4355 line_end_section2->next = line_after_section1;
4356 line_before_section2->next = line_start_section1;
4359 /* Set the pointer from the end of section1 to the rest of the file */
4360 line_end_section1->next = line_after_section2;
4362 err = edit_write_file(file);
4365 /* Error writing file */
4366 if (err == JB_ERR_FILE)
4368 /* Read-only file. */
4369 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4371 edit_free_file(file);
4374 } /* END if (section1 != section2) */
4376 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4377 (long) time(NULL), file->identifier);
4379 edit_free_file(file);
4381 return cgi_redirect(rsp, target);
4384 #ifdef FEATURE_TOGGLE
4385 /*********************************************************************
4387 * Function : cgi_toggle
4389 * Description : CGI function that adds a new empty section to
4393 * 1 : csp = Current client state (buffers, headers, etc...)
4394 * 2 : rsp = http_response data structure for output
4395 * 3 : parameters = map of cgi parameters
4398 * set : If present, how to change toggle setting:
4399 * "enable", "disable", "toggle", or none (default).
4400 * mini : If present, use mini reply template.
4402 * Returns : JB_ERR_OK on success
4403 * JB_ERR_MEMORY on out-of-memory
4405 *********************************************************************/
4406 jb_err cgi_toggle(struct client_state *csp,
4407 struct http_response *rsp,
4408 const struct map *parameters)
4410 struct map *exports;
4412 const char *template_name;
4418 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4420 return cgi_error_disabled(csp, rsp);
4423 mode = get_char_param(parameters, "set");
4428 global_toggle_state = 1;
4430 else if (mode == 'D')
4433 global_toggle_state = 0;
4435 else if (mode == 'T')
4438 global_toggle_state = !global_toggle_state;
4441 if (NULL == (exports = default_exports(csp, "toggle")))
4443 return JB_ERR_MEMORY;
4446 template_name = (get_char_param(parameters, "mini")
4450 return template_fill_for_cgi(csp, template_name, exports, rsp);
4452 #endif /* def FEATURE_TOGGLE */
4454 /*********************************************************************
4456 * Function : javascriptify
4458 * Description : Converts a string into a form JavaScript will like.
4460 * Netscape 4's JavaScript sucks - it doesn't use
4461 * "id" parameters, so you have to set the "name"
4462 * used to submit a form element to something JavaScript
4463 * will like. (Or access the elements by index in an
4464 * array. That array contains >60 elements and will
4465 * be changed whenever we add a new action to the
4466 * editor, so I'm NOT going to use indexes that have
4467 * to be figured out by hand.)
4469 * Currently the only thing we have to worry about
4470 * is "-" ==> "_" conversion.
4472 * This is a length-preserving operation so it is
4473 * carried out in-place, no memory is allocated
4477 * 1 : identifier = String to make JavaScript-friendly.
4481 *********************************************************************/
4482 static void javascriptify(char * identifier)
4484 char * p = identifier;
4485 while (NULL != (p = strchr(p, '-')))
4492 /*********************************************************************
4494 * Function : actions_to_radio
4496 * Description : Converts a actionsfile entry into settings for
4497 * radio buttons and edit boxes on a HTML form.
4500 * 1 : exports = List of substitutions to add to.
4501 * 2 : action = Action to read
4503 * Returns : JB_ERR_OK on success
4504 * JB_ERR_MEMORY on out-of-memory
4506 *********************************************************************/
4507 static jb_err actions_to_radio(struct map * exports,
4508 const struct action_spec *action)
4510 unsigned mask = action->mask;
4511 unsigned add = action->add;
4519 mask = action->mask;
4522 /* sanity - prevents "-feature +feature" */
4526 #define DEFINE_ACTION_BOOL(name, bit) \
4527 if (!(mask & bit)) \
4529 current_mode = 'n'; \
4531 else if (add & bit) \
4533 current_mode = 'y'; \
4537 current_mode = 'x'; \
4539 if (map_radio(exports, name, "ynx", current_mode)) \
4541 return JB_ERR_MEMORY; \
4544 #define DEFINE_ACTION_STRING(name, bit, index) \
4545 DEFINE_ACTION_BOOL(name, bit); \
4548 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4551 checked = !strcmp(action->string[index], value); \
4555 checked = is_default; \
4557 mapped_param |= checked; \
4558 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4560 return JB_ERR_MEMORY; \
4563 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4564 if (map(exports, name "-param-custom", 1, \
4565 ((!mapped_param) ? "checked" : ""), 1)) \
4567 return JB_ERR_MEMORY; \
4569 if (map(exports, name "-param", 1, \
4570 (((add & bit) && !mapped_param) ? \
4571 action->string[index] : default_val), 1)) \
4573 return JB_ERR_MEMORY; \
4576 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4577 if (map(exports, name "-param", 1, \
4578 ((add & bit) ? action->string[index] : default_val), 1)) \
4580 return JB_ERR_MEMORY; \
4583 #define DEFINE_ACTION_MULTI(name, index) \
4584 if (action->multi_add[index]->first) \
4586 current_mode = 'y'; \
4588 else if (action->multi_remove_all[index]) \
4590 current_mode = 'n'; \
4592 else if (action->multi_remove[index]->first) \
4594 current_mode = 'y'; \
4598 current_mode = 'x'; \
4600 if (map_radio(exports, name, "ynx", current_mode)) \
4602 return JB_ERR_MEMORY; \
4605 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4607 #include "actionlist.h"
4609 #undef DEFINE_ACTION_MULTI
4610 #undef DEFINE_ACTION_STRING
4611 #undef DEFINE_ACTION_BOOL
4612 #undef DEFINE_ACTION_ALIAS
4613 #undef DEFINE_CGI_PARAM_CUSTOM
4614 #undef DEFINE_CGI_PARAM_RADIO
4615 #undef DEFINE_CGI_PARAM_NO_RADIO
4621 /*********************************************************************
4623 * Function : actions_from_radio
4625 * Description : Converts a map of parameters passed to a CGI function
4626 * into an actionsfile entry.
4629 * 1 : parameters = parameters to the CGI call
4630 * 2 : action = Action to change. Must be valid before
4631 * the call, actions not specified will be
4634 * Returns : JB_ERR_OK on success
4635 * JB_ERR_MEMORY on out-of-memory
4637 *********************************************************************/
4638 static jb_err actions_from_radio(const struct map * parameters,
4639 struct action_spec *action)
4644 const char * js_name;
4645 jb_err err = JB_ERR_OK;
4650 /* Statics are generally a potential race condition,
4651 * but in this case we're safe and don't need semaphores.
4652 * Be careful if you modify this function.
4654 * The js_name_arr's are never free()d, but this is no
4655 * problem, since they will only be created once and
4656 * used by all threads thereafter. -oes
4659 #define JAVASCRIPTIFY(dest_var, string) \
4661 static int first_time = 1; \
4662 static char *js_name_arr; \
4665 js_name_arr = strdup(string); \
4666 javascriptify(js_name_arr); \
4668 dest_var = js_name_arr; \
4672 #define DEFINE_ACTION_BOOL(name, bit) \
4673 JAVASCRIPTIFY(js_name, name); \
4674 ch = get_char_param(parameters, js_name); \
4677 action->add |= bit; \
4678 action->mask |= bit; \
4680 else if (ch == 'N') \
4682 action->add &= ~bit; \
4683 action->mask &= ~bit; \
4685 else if (ch == 'X') \
4687 action->add &= ~bit; \
4688 action->mask |= bit; \
4691 #define DEFINE_ACTION_STRING(name, bit, index) \
4692 JAVASCRIPTIFY(js_name, name); \
4693 ch = get_char_param(parameters, js_name); \
4697 JAVASCRIPTIFY(js_name, name "-mode"); \
4698 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4699 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4701 JAVASCRIPTIFY(js_name, name "-param"); \
4702 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4704 if (param != NULL) \
4706 if (NULL == (param_dup = strdup(param))) \
4708 return JB_ERR_MEMORY; \
4710 freez(action->string[index]); \
4711 action->add |= bit; \
4712 action->mask |= bit; \
4713 action->string[index] = param_dup; \
4716 else if (ch == 'N') \
4718 if (action->add & bit) \
4720 freez(action->string[index]); \
4722 action->add &= ~bit; \
4723 action->mask &= ~bit; \
4725 else if (ch == 'X') \
4727 if (action->add & bit) \
4729 freez(action->string[index]); \
4731 action->add &= ~bit; \
4732 action->mask |= bit; \
4735 #define DEFINE_ACTION_MULTI(name, index) \
4736 JAVASCRIPTIFY(js_name, name); \
4737 ch = get_char_param(parameters, js_name); \
4742 else if (ch == 'N') \
4744 list_remove_all(action->multi_add[index]); \
4745 list_remove_all(action->multi_remove[index]); \
4746 action->multi_remove_all[index] = 1; \
4748 else if (ch == 'X') \
4750 list_remove_all(action->multi_add[index]); \
4751 list_remove_all(action->multi_remove[index]); \
4752 action->multi_remove_all[index] = 0; \
4755 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4757 #include "actionlist.h"
4759 #undef DEFINE_ACTION_MULTI
4760 #undef DEFINE_ACTION_STRING
4761 #undef DEFINE_ACTION_BOOL
4762 #undef DEFINE_ACTION_ALIAS
4763 #undef JAVASCRIPTIFY
4769 #endif /* def FEATURE_CGI_EDIT_ACTIONS */