1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.47 2006/12/28 18:04:25 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.47 2006/12/28 18:04:25 fabiankeil
46 * Fixed gcc43 conversion warnings.
48 * Revision 1.46 2006/12/27 18:44:52 fabiankeil
49 * Stop shadowing string.h's index().
51 * Revision 1.45 2006/12/21 12:57:48 fabiankeil
52 * Add config option "split-large-forms"
53 * to work around the browser bug reported
56 * Revision 1.44 2006/12/09 13:49:16 fabiankeil
57 * Fix configure option --disable-toggle.
58 * Thanks to Peter Thoenen for reporting this.
60 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
61 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
62 * with what was really the latest development (the v_3_0_branch branch)
64 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
65 * Remove a little residual debugging info
67 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
68 * Multiple filter file support
70 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
71 * Undo some damaging memory leak patches
73 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
74 * Removing a double free
76 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
77 * A few memory leaks plugged (mostly on error paths)
79 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
80 * Moved cgi_error_disabled() from cgiedit.c to
81 * cgi.c to re-enable build with --disable-editor.
82 * Fixes Bug #892744. Thanks to Matthew Fischer
85 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
86 * One line lost in last commit
88 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
89 * - All edit functions that redirect back to the list page
90 * now use cgi_redirect
91 * - All redirects now contain useless parameter "foo", whose
92 * value are raw seconds since epoch, in order to force
93 * Opera and Konqueror to properly reload the list. Closes
96 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
97 * Cosmetic: Renamed cryptic variable
99 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
100 * Fix: Don't free uninitialized struct editable_file
102 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
103 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
105 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
106 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
108 * Revision 1.41 2002/05/21 19:09:45 oes
109 * - Made Add/Edit/Remove URL Submit and Cancel
110 * buttons jump back to relevant section in eal
111 * - Bugfix: remove-url-form needs p export
113 * Revision 1.40 2002/05/19 11:34:35 jongfoster
114 * Handling read-only actions files better - report the actual
115 * error, not "Out of memory"!
118 * http://sourceforge.net/tracker/index.php?func=detail
119 * &aid=557905&group_id=11118&atid=111118
121 * Revision 1.39 2002/05/12 21:39:15 jongfoster
122 * - Adding Doxygen-style comments to structures and #defines.
123 * - Correcting function comments
125 * Revision 1.38 2002/05/03 23:00:38 jongfoster
126 * Support for templates for "standard actions" buttons.
129 * Revision 1.37 2002/04/30 11:14:52 oes
130 * Made csp the first parameter in *action_to_html
132 * Revision 1.36 2002/04/26 21:53:30 jongfoster
133 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
135 * Revision 1.35 2002/04/26 21:50:02 jongfoster
136 * Honouring default exports in edit-actions-for-url-filter template.
138 * Revision 1.34 2002/04/26 12:54:17 oes
139 * Adaptions to changes in actions.c
141 * Revision 1.33 2002/04/24 02:17:47 oes
142 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
144 * - Activated Jon's code for editing multiple AFs
145 * - cgi_edit_list_actions now provides context-sensitive
146 * help, looks up all action sets from standard.action and
147 * makes buttons for them in the catchall section
148 * - cgi_edit_action_submit now honors a p parameter, looks up
149 * the corresponding action set, and sets the catchall pattern's
150 * actions accordingly.
152 * Revision 1.32 2002/04/19 16:55:31 jongfoster
153 * Fixing newline problems. If we do our own text file newline
154 * mangling, we don't want the library to do any, so we need to
155 * open the files in *binary* mode.
157 * Revision 1.31 2002/04/18 19:21:08 jongfoster
158 * Added code to detect "conventional" action files, that start
159 * with a set of actions for all URLs (the pattern "/").
160 * These are special-cased in the "edit-actions-list" CGI, so
161 * that a special UI can be written for them.
163 * Revision 1.30 2002/04/10 13:38:35 oes
164 * load_template signature changed
166 * Revision 1.29 2002/04/08 16:59:08 oes
169 * Revision 1.28 2002/03/27 12:30:29 oes
170 * Deleted unsused variable
172 * Revision 1.27 2002/03/26 23:06:04 jongfoster
173 * Removing duplicate @ifs on the toggle page
175 * Revision 1.26 2002/03/26 22:59:17 jongfoster
176 * Fixing /toggle to display status consistently.
178 * Revision 1.25 2002/03/26 22:29:54 swa
179 * we have a new homepage!
181 * Revision 1.24 2002/03/24 15:23:33 jongfoster
184 * Revision 1.23 2002/03/24 13:32:41 swa
185 * name change related issues
187 * Revision 1.22 2002/03/24 13:25:43 swa
188 * name change related issues
190 * Revision 1.21 2002/03/22 18:02:48 jongfoster
191 * Fixing remote toggle
193 * Revision 1.20 2002/03/16 20:28:34 oes
194 * Added descriptions to the filters so users will know what they select in the cgi editor
196 * Revision 1.19 2002/03/16 18:38:14 jongfoster
197 * Stopping stupid or malicious users from breaking the actions
198 * file using the web-based editor.
200 * Revision 1.18 2002/03/16 14:57:44 jongfoster
201 * Full support for enabling/disabling modular filters.
203 * Revision 1.17 2002/03/16 14:26:42 jongfoster
204 * First version of modular filters support - READ ONLY!
205 * Fixing a double-free bug in the out-of-memory handling in map_radio().
207 * Revision 1.16 2002/03/07 03:46:17 oes
208 * Fixed compiler warnings
210 * Revision 1.15 2002/03/06 22:54:35 jongfoster
211 * Automated function-comment nitpicking.
213 * Revision 1.14 2002/03/05 00:24:51 jongfoster
214 * Patch to always edit the current actions file.
216 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
217 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
219 * Revision 1.12 2002/03/03 09:18:03 joergs
220 * Made jumbjuster work on AmigaOS again.
222 * Revision 1.11 2002/01/23 01:03:31 jongfoster
223 * Fixing gcc [CygWin] compiler warnings
225 * Revision 1.10 2002/01/23 00:22:59 jongfoster
226 * Adding new function cgi_edit_actions_section_swap(), to reorder
229 * Adding get_url_spec_param() to get a validated URL pattern.
231 * Moving edit_read_line() out of this file and into loaders.c.
233 * Adding missing html_encode() to many CGI functions.
235 * Moving the functions that #include actionlist.h to the end of the file,
236 * because the Visual C++ 97 debugger gets extremely confused if you try
237 * to debug any code that comes after them in the file.
239 * Major optimizations in cgi_edit_actions_list() to reduce the size of
240 * the generated HTML (down 40% from 550k to 304k), with major side-effects
241 * throughout the editor and templates. In particular, the length of the
242 * URLs throughout the editor has been drastically reduced, by cutting
243 * paramater names down to 1 character and CGI names down to 3-4
244 * characters, by removing all non-essential CGI paramaters even at the
245 * expense of having to re-read the actions file for the most trivial
246 * page, and by using relative rather than absolute URLs. This means
247 * that this (typical example):
249 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
250 * filename=ijb&ver=1011487572&section=12&pattern=13
251 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
255 * <a href="eau?f=ijb&v=1011487572&p=13">
257 * Revision 1.9 2002/01/17 20:56:22 jongfoster
258 * Replacing hard references to the URL of the config interface
259 * with #defines from project.h
261 * Revision 1.8 2001/11/30 23:35:51 jongfoster
262 * Renaming actionsfile to ijb.action
264 * Revision 1.7 2001/11/13 00:28:24 jongfoster
265 * - Renaming parameters from edit-actions-for-url so that they only
266 * contain legal JavaScript characters. If we wanted to write
267 * JavaScript that worked with Netscape 4, this is nessacery.
268 * (Note that at the moment the JavaScript doesn't actually work
269 * with Netscape 4, but now this is purely a template issue, not
270 * one affecting code).
271 * - Adding new CGIs for use by non-JavaScript browsers:
272 * edit-actions-url-form
273 * edit-actions-add-url-form
274 * edit-actions-remove-url-form
277 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
278 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
279 * by and __OS2__ ifdef.
281 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
282 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
283 * threads to call select() simultaneously. So, it's time to do a real, live,
284 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
285 * (native). Both versions will work, but using __OS2__ offers multi-threading.
287 * Revision 1.4 2001/10/23 21:48:19 jongfoster
288 * Cleaning up error handling in CGI functions - they now send back
289 * a HTML error page and should never cause a FATAL error. (Fixes one
290 * potential source of "denial of service" attacks).
292 * CGI actions file editor that works and is actually useful.
294 * Ability to toggle JunkBuster remotely using a CGI call.
296 * You can turn off both the above features in the main configuration
297 * file, e.g. if you are running a multi-user proxy.
299 * Revision 1.3 2001/10/14 22:12:49 jongfoster
300 * New version of CGI-based actionsfile editor.
301 * Major changes, including:
302 * - Completely new file parser and file output routines
303 * - edit-actions CGI renamed edit-actions-for-url
304 * - All CGIs now need a filename parameter, except for...
305 * - New CGI edit-actions which doesn't need a filename,
306 * to allow you to start the editor up.
307 * - edit-actions-submit now works, and now automatically
308 * redirects you back to the main edit-actions-list handler.
310 * Revision 1.2 2001/09/16 17:05:14 jongfoster
311 * Removing unused #include showarg.h
313 * Revision 1.1 2001/09/16 15:47:37 jongfoster
314 * First version of CGI-based edit interface. This is very much a
315 * work-in-progress, and you can't actually use it to edit anything
316 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
317 * to have any effect.
320 **********************************************************************/
326 * FIXME: Following includes copied from cgi.c - which are actually needed?
331 #include <sys/types.h>
335 #include <sys/stat.h>
338 #define snprintf _snprintf
339 #endif /* def _WIN32 */
344 #include "cgisimple.h"
348 #include "miscutil.h"
351 #ifdef FEATURE_TOGGLE
352 /* loadcfg.h is for global_toggle_state only */
354 #endif /* def FEATURE_TOGGLE */
355 #include "urlmatch.h"
357 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
360 #ifdef FEATURE_CGI_EDIT_ACTIONS
363 * A line in an editable_file.
367 /** Next entry in the linked list */
368 struct file_line * next;
370 /** The raw data, to write out if this line is unmodified. */
373 /** Comments and/or whitespace to put before this line if it's modified
374 and then written out. */
377 /** The actual data, as a string. Line continuation and comment removal
378 are performed on the data read from file before it's stored here, so
379 it will be a single line of data. */
382 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
385 /** The actual data, processed into some sensible data type. */
389 /** An action specification. */
390 struct action_spec action[1];
392 /** A name=value pair. */
396 /** The name in the name=value pair. */
399 /** The value in the name=value pair, as a string. */
402 /** The value in the name=value pair, as an integer. */
407 /* Add more data types here... e.g.
410 struct url_spec url[1];
414 struct action_spec action[1];
424 /** This file_line has not been processed yet. */
425 #define FILE_LINE_UNPROCESSED 1
427 /** This file_line is blank. Can only appear at the end of a file, due to
428 the way the parser works. */
429 #define FILE_LINE_BLANK 2
431 /** This file_line says {{alias}}. */
432 #define FILE_LINE_ALIAS_HEADER 3
434 /** This file_line defines an alias. */
435 #define FILE_LINE_ALIAS_ENTRY 4
437 /** This file_line defines an {action}. */
438 #define FILE_LINE_ACTION 5
440 /** This file_line specifies a URL pattern. */
441 #define FILE_LINE_URL 6
443 /** This file_line says {{settings}}. */
444 #define FILE_LINE_SETTINGS_HEADER 7
446 /** This file_line is in a {{settings}} block. */
447 #define FILE_LINE_SETTINGS_ENTRY 8
449 /** This file_line says {{description}}. */
450 #define FILE_LINE_DESCRIPTION_HEADER 9
452 /** This file_line is in a {{description}} block. */
453 #define FILE_LINE_DESCRIPTION_ENTRY 10
457 * A configuration file, in a format that can be edited and written back to
462 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
463 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
464 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
465 /**< Pre-encoded with url_encode() for ease of use. */
466 const char * version_str; /**< Last modification time, as a string. For CGI param. */
467 /**< Can be used in URL without using url_param(). */
468 unsigned version; /**< Last modification time - prevents chaos with
469 the browser's "back" button. Note that this is a
470 time_t cast to an unsigned. When comparing, always
471 cast the time_t to an unsigned, and *NOT* vice-versa.
472 This may lose the top few bits, but they're not
473 significant anyway. */
474 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
475 Note that changing this after the file has been
476 read in will cause a mess. */
477 struct file_line * parse_error; /**< On parse error, this is the offending line. */
478 const char * parse_error_text; /**< On parse error, this is the problem.
479 (Statically allocated) */
482 /* FIXME: Following non-static functions should be prototyped in .h or made static */
484 /* Functions to read and write arbitrary config files */
485 jb_err edit_read_file(struct client_state *csp,
486 const struct map *parameters,
489 struct editable_file **pfile);
490 jb_err edit_write_file(struct editable_file * file);
491 void edit_free_file(struct editable_file * file);
493 /* Functions to read and write actions files */
494 jb_err edit_parse_actions_file(struct editable_file * file);
495 jb_err edit_read_actions_file(struct client_state *csp,
496 struct http_response *rsp,
497 const struct map *parameters,
499 struct editable_file **pfile);
502 jb_err cgi_error_modified(struct client_state *csp,
503 struct http_response *rsp,
504 const char *filename);
505 jb_err cgi_error_parse(struct client_state *csp,
506 struct http_response *rsp,
507 struct editable_file *file);
508 jb_err cgi_error_file(struct client_state *csp,
509 struct http_response *rsp,
510 const char *filename);
511 jb_err cgi_error_file_read_only(struct client_state *csp,
512 struct http_response *rsp,
513 const char *filename);
515 /* Internal arbitrary config file support functions */
516 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
517 static void edit_free_file_lines(struct file_line * first_line);
519 /* Internal actions file support functions */
520 static int match_actions_file_header_line(const char * line, const char * name);
521 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
523 /* Internal parameter parsing functions */
524 static jb_err get_file_name_param(struct client_state *csp,
525 const struct map *parameters,
526 const char *param_name,
529 const char **pparam);
531 static jb_err get_url_spec_param(struct client_state *csp,
532 const struct map *parameters,
537 /* Internal actionsfile <==> HTML conversion functions */
538 static jb_err map_radio(struct map * exports,
539 const char * optionname,
542 static jb_err actions_to_radio(struct map * exports,
543 const struct action_spec *action);
544 static jb_err actions_from_radio(const struct map * parameters,
545 struct action_spec *action);
548 static jb_err map_copy_parameter_html(struct map *out,
549 const struct map *in,
551 #if 0 /* unused function */
552 static jb_err map_copy_parameter_url(struct map *out,
553 const struct map *in,
555 #endif /* unused function */
557 /* Internal convenience functions */
558 static char *section_target(const unsigned sectionid);
560 /*********************************************************************
562 * Function : section_target
564 * Description : Given an unsigned (section id) n, produce a dynamically
565 * allocated string of the form #l<n>, for use in link
569 * 1 : sectionid = start line number of section
571 * Returns : String with link target, or NULL if out of
574 *********************************************************************/
575 static char *section_target(const unsigned sectionid)
579 snprintf(buf, 30, "#l%d", sectionid);
585 /*********************************************************************
587 * Function : map_copy_parameter_html
589 * Description : Copy a CGI parameter from one map to another, HTML
593 * 1 : out = target map
594 * 2 : in = source map
595 * 3 : name = name of cgi parameter to copy
597 * Returns : JB_ERR_OK on success
598 * JB_ERR_MEMORY on out-of-memory
599 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
602 *********************************************************************/
603 static jb_err map_copy_parameter_html(struct map *out,
604 const struct map *in,
614 value = lookup(in, name);
615 err = map(out, name, 1, html_encode(value), 0);
622 else if (*value == '\0')
624 return JB_ERR_CGI_PARAMS;
633 #if 0 /* unused function */
634 /*********************************************************************
636 * Function : map_copy_parameter_url
638 * Description : Copy a CGI parameter from one map to another, URL
642 * 1 : out = target map
643 * 2 : in = source map
644 * 3 : name = name of cgi parameter to copy
646 * Returns : JB_ERR_OK on success
647 * JB_ERR_MEMORY on out-of-memory
648 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
651 *********************************************************************/
652 static jb_err map_copy_parameter_url(struct map *out,
653 const struct map *in,
663 value = lookup(in, name);
664 err = map(out, name, 1, url_encode(value), 0);
671 else if (*value == '\0')
673 return JB_ERR_CGI_PARAMS;
680 #endif /* 0 - unused function */
682 /*********************************************************************
684 * Function : cgi_edit_actions_url_form
686 * Description : CGI function that displays a form for
690 * 1 : csp = Current client state (buffers, headers, etc...)
691 * 2 : rsp = http_response data structure for output
692 * 3 : parameters = map of cgi parameters
695 * f : (filename) Identifies the file to edit
696 * v : (version) File's last-modified time
697 * p : (pattern) Line number of pattern to edit
699 * Returns : JB_ERR_OK on success
700 * JB_ERR_MEMORY on out-of-memory
701 * JB_ERR_CGI_PARAMS if the CGI parameters are not
702 * specified or not valid.
704 *********************************************************************/
705 jb_err cgi_edit_actions_url_form(struct client_state *csp,
706 struct http_response *rsp,
707 const struct map *parameters)
709 struct map * exports;
711 struct editable_file * file;
712 struct file_line * cur_line;
713 unsigned line_number;
714 unsigned section_start_line_number = 0;
721 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
723 return cgi_error_disabled(csp, rsp);
726 err = get_number_param(csp, parameters, "p", &patternid);
732 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
735 /* No filename specified, can't read file, modified, or out of memory. */
736 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
739 cur_line = file->lines;
741 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
743 if (cur_line->type == FILE_LINE_ACTION)
745 section_start_line_number = line_number;
747 cur_line = cur_line->next;
750 if ( (cur_line == NULL)
751 || (line_number != patternid)
753 || (cur_line->type != FILE_LINE_URL))
755 /* Invalid "patternid" parameter */
756 edit_free_file(file);
757 return JB_ERR_CGI_PARAMS;
760 if (NULL == (exports = default_exports(csp, NULL)))
762 edit_free_file(file);
763 return JB_ERR_MEMORY;
766 err = map(exports, "f", 1, file->identifier, 1);
767 if (!err) err = map(exports, "v", 1, file->version_str, 1);
768 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
769 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
770 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
772 edit_free_file(file);
780 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
784 /*********************************************************************
786 * Function : cgi_edit_actions_add_url_form
788 * Description : CGI function that displays a form for
792 * 1 : csp = Current client state (buffers, headers, etc...)
793 * 2 : rsp = http_response data structure for output
794 * 3 : parameters = map of cgi parameters
797 * f : (filename) Identifies the file to edit
798 * v : (version) File's last-modified time
799 * s : (section) Line number of section to edit
801 * Returns : JB_ERR_OK on success
802 * JB_ERR_MEMORY on out-of-memory
803 * JB_ERR_CGI_PARAMS if the CGI parameters are not
804 * specified or not valid.
806 *********************************************************************/
807 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
808 struct http_response *rsp,
809 const struct map *parameters)
818 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
820 return cgi_error_disabled(csp, rsp);
823 if (NULL == (exports = default_exports(csp, NULL)))
825 return JB_ERR_MEMORY;
828 err = map_copy_parameter_html(exports, parameters, "f");
829 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
830 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
838 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
842 /*********************************************************************
844 * Function : cgi_edit_actions_remove_url_form
846 * Description : CGI function that displays a form for
850 * 1 : csp = Current client state (buffers, headers, etc...)
851 * 2 : rsp = http_response data structure for output
852 * 3 : parameters = map of cgi parameters
855 * f : (filename) Identifies the file to edit
856 * v : (version) File's last-modified time
857 * p : (pattern) Line number of pattern to edit
859 * Returns : JB_ERR_OK on success
860 * JB_ERR_MEMORY on out-of-memory
861 * JB_ERR_CGI_PARAMS if the CGI parameters are not
862 * specified or not valid.
864 *********************************************************************/
865 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
866 struct http_response *rsp,
867 const struct map *parameters)
869 struct map * exports;
871 struct editable_file * file;
872 struct file_line * cur_line;
873 unsigned line_number;
874 unsigned section_start_line_number = 0;
881 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
883 return cgi_error_disabled(csp, rsp);
886 err = get_number_param(csp, parameters, "p", &patternid);
892 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
895 /* No filename specified, can't read file, modified, or out of memory. */
896 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
899 cur_line = file->lines;
901 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
903 if (cur_line->type == FILE_LINE_ACTION)
905 section_start_line_number = line_number;
907 cur_line = cur_line->next;
910 if ( (cur_line == NULL)
911 || (line_number != patternid)
913 || (cur_line->type != FILE_LINE_URL))
915 /* Invalid "patternid" parameter */
916 edit_free_file(file);
917 return JB_ERR_CGI_PARAMS;
920 if (NULL == (exports = default_exports(csp, NULL)))
922 edit_free_file(file);
923 return JB_ERR_MEMORY;
926 err = map(exports, "f", 1, file->identifier, 1);
927 if (!err) err = map(exports, "v", 1, file->version_str, 1);
928 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
929 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
930 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
932 edit_free_file(file);
940 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
944 /*********************************************************************
946 * Function : edit_write_file
948 * Description : Write a complete file to disk.
951 * 1 : file = File to write.
953 * Returns : JB_ERR_OK on success
954 * JB_ERR_FILE on error writing to file.
955 * JB_ERR_MEMORY on out of memory
957 *********************************************************************/
958 jb_err edit_write_file(struct editable_file * file)
961 struct file_line * cur_line;
962 struct stat statbuf[1];
963 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
964 digits in time_t, assuming this is a 64-bit
965 machine, plus null terminator, plus one
969 assert(file->filename);
971 if (NULL == (fp = fopen(file->filename, "wb")))
976 cur_line = file->lines;
977 while (cur_line != NULL)
981 if (fputs(cur_line->raw, fp) < 0)
989 if (cur_line->prefix)
991 if (fputs(cur_line->prefix, fp) < 0)
997 if (cur_line->unprocessed)
1000 if (NULL != strchr(cur_line->unprocessed, '#'))
1002 /* Must quote '#' characters */
1009 /* Count number of # characters, so we know length of output string */
1010 src = cur_line->unprocessed;
1011 while (NULL != (src = strchr(src, '#')))
1016 assert(numhash > 0);
1018 /* Allocate new memory for string */
1019 len = strlen(cur_line->unprocessed) + (size_t)numhash;
1020 if (NULL == (str = malloc(len + 1)))
1022 /* Uh oh, just trashed file! */
1024 return JB_ERR_MEMORY;
1027 /* Copy string but quote hashes */
1028 src = cur_line->unprocessed;
1036 assert(numhash >= 0);
1042 assert(numhash == 0);
1043 assert(strlen(str) == len);
1044 assert(str == dest - len);
1045 assert(src - len <= cur_line->unprocessed);
1047 if ((strlen(str) != len) || (numhash != 0))
1050 * Escaping didn't work as expected, go spread the news.
1051 * Only reached in non-debugging builds.
1053 log_error(LOG_LEVEL_ERROR,
1054 "Looks like hash escaping failed. %s might be corrupted now.",
1058 if (fputs(str, fp) < 0)
1069 /* Can write without quoting '#' characters. */
1070 if (fputs(cur_line->unprocessed, fp) < 0)
1076 if (fputs(NEWLINE(file->newline), fp) < 0)
1084 /* FIXME: Write data from file->data->whatever */
1088 cur_line = cur_line->next;
1094 /* Update the version stamp in the file structure, since we just
1095 * wrote to the file & changed it's date.
1097 if (stat(file->filename, statbuf) < 0)
1099 /* Error, probably file not found. */
1102 file->version = (unsigned)statbuf->st_mtime;
1104 /* Correct file->version_str */
1105 freez(file->version_str);
1106 snprintf(version_buf, 22, "%u", file->version);
1107 version_buf[21] = '\0';
1108 file->version_str = strdup(version_buf);
1109 if (version_buf == NULL)
1111 return JB_ERR_MEMORY;
1118 /*********************************************************************
1120 * Function : edit_free_file
1122 * Description : Free a complete file in memory.
1125 * 1 : file = Data structure to free.
1129 *********************************************************************/
1130 void edit_free_file(struct editable_file * file)
1134 /* Silently ignore NULL pointer */
1138 edit_free_file_lines(file->lines);
1139 freez(file->filename);
1140 freez(file->identifier);
1141 freez(file->version_str);
1143 file->parse_error_text = NULL; /* Statically allocated */
1144 file->parse_error = NULL;
1150 /*********************************************************************
1152 * Function : edit_free_file_lines
1154 * Description : Free an entire linked list of file lines.
1157 * 1 : first_line = Data structure to free.
1161 *********************************************************************/
1162 static void edit_free_file_lines(struct file_line * first_line)
1164 struct file_line * next_line;
1166 while (first_line != NULL)
1168 next_line = first_line->next;
1169 first_line->next = NULL;
1170 freez(first_line->raw);
1171 freez(first_line->prefix);
1172 freez(first_line->unprocessed);
1173 switch(first_line->type)
1175 case 0: /* special case if memory zeroed */
1176 case FILE_LINE_UNPROCESSED:
1177 case FILE_LINE_BLANK:
1178 case FILE_LINE_ALIAS_HEADER:
1179 case FILE_LINE_SETTINGS_HEADER:
1180 case FILE_LINE_DESCRIPTION_HEADER:
1181 case FILE_LINE_DESCRIPTION_ENTRY:
1182 case FILE_LINE_ALIAS_ENTRY:
1184 /* No data is stored for these */
1187 case FILE_LINE_ACTION:
1188 free_action(first_line->data.action);
1191 case FILE_LINE_SETTINGS_ENTRY:
1192 freez(first_line->data.setting.name);
1193 freez(first_line->data.setting.svalue);
1196 /* Should never happen */
1200 first_line->type = 0; /* paranoia */
1202 first_line = next_line;
1207 /*********************************************************************
1209 * Function : match_actions_file_header_line
1211 * Description : Match an actions file {{header}} line
1214 * 1 : line = String from file
1215 * 2 : name = Header to match against
1217 * Returns : 0 iff they match.
1219 *********************************************************************/
1220 static int match_actions_file_header_line(const char * line, const char * name)
1228 if ((line[0] != '{') || (line[1] != '{'))
1234 /* Look for optional whitespace */
1235 while ( (*line == ' ') || (*line == '\t') )
1240 /* Look for the specified name (case-insensitive) */
1242 if (0 != strncmpic(line, name, len))
1248 /* Look for optional whitespace */
1249 while ( (*line == ' ') || (*line == '\t') )
1254 /* Look for "}}" and end of string*/
1255 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1265 /*********************************************************************
1267 * Function : match_actions_file_header_line
1269 * Description : Match an actions file {{header}} line
1272 * 1 : line = String from file. Must not start with
1273 * whitespace (else infinite loop!)
1274 * 2 : pname = Destination for name
1275 * 2 : pvalue = Destination for value
1277 * Returns : JB_ERR_OK on success
1278 * JB_ERR_MEMORY on out-of-memory
1279 * JB_ERR_PARSE if there's no "=" sign, or if there's
1280 * nothing before the "=" sign (but empty
1281 * values *after* the "=" sign are legal).
1283 *********************************************************************/
1284 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1286 const char * name_end;
1287 const char * value_start;
1293 assert(*line != ' ');
1294 assert(*line != '\t');
1299 value_start = strchr(line, '=');
1300 if ((value_start == NULL) || (value_start == line))
1302 return JB_ERR_PARSE;
1305 name_end = value_start - 1;
1307 /* Eat any whitespace before the '=' */
1308 while ((*name_end == ' ') || (*name_end == '\t'))
1311 * we already know we must have at least 1 non-ws char
1312 * at start of buf - no need to check
1317 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1318 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1320 return JB_ERR_MEMORY;
1322 strncpy(*pname, line, name_len);
1323 (*pname)[name_len] = '\0';
1325 /* Eat any the whitespace after the '=' */
1327 while ((*value_start == ' ') || (*value_start == '\t'))
1332 if (NULL == (*pvalue = strdup(value_start)))
1336 return JB_ERR_MEMORY;
1343 /*********************************************************************
1345 * Function : edit_parse_actions_file
1347 * Description : Parse an actions file in memory.
1349 * Passed linked list must have the "data" member
1350 * zeroed, and must contain valid "next" and
1351 * "unprocessed" fields. The "raw" and "prefix"
1352 * fields are ignored, and "type" is just overwritten.
1354 * Note that on error the file may have been
1358 * 1 : file = Actions file to be parsed in-place.
1360 * Returns : JB_ERR_OK on success
1361 * JB_ERR_MEMORY on out-of-memory
1362 * JB_ERR_PARSE on error
1364 *********************************************************************/
1365 jb_err edit_parse_actions_file(struct editable_file * file)
1367 struct file_line * cur_line;
1369 const char * text; /* Text from a line */
1370 char * name; /* For lines of the form name=value */
1371 char * value; /* For lines of the form name=value */
1372 struct action_alias * alias_list = NULL;
1373 jb_err err = JB_ERR_OK;
1375 /* alias_list contains the aliases defined in this file.
1376 * It might be better to use the "file_line.data" fields
1377 * in the relavent places instead.
1380 cur_line = file->lines;
1382 /* A note about blank line support: Blank lines should only
1383 * ever occur as the last line in the file. This function
1384 * is more forgiving than that - FILE_LINE_BLANK can occur
1388 /* Skip leading blanks. Should only happen if file is
1389 * empty (which is valid, but pointless).
1391 while ( (cur_line != NULL)
1392 && (cur_line->unprocessed[0] == '\0') )
1395 cur_line->type = FILE_LINE_BLANK;
1396 cur_line = cur_line->next;
1399 if ( (cur_line != NULL)
1400 && (cur_line->unprocessed[0] != '{') )
1402 /* File doesn't start with a header */
1403 file->parse_error = cur_line;
1404 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1405 return JB_ERR_PARSE;
1408 if ( (cur_line != NULL) && (0 ==
1409 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1411 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1413 cur_line = cur_line->next;
1414 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1416 if (cur_line->unprocessed[0])
1418 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1420 err = split_line_on_equals(cur_line->unprocessed,
1421 &cur_line->data.setting.name,
1422 &cur_line->data.setting.svalue);
1423 if (err == JB_ERR_MEMORY)
1427 else if (err != JB_ERR_OK)
1429 /* Line does not contain a name=value pair */
1430 file->parse_error = cur_line;
1431 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1432 return JB_ERR_PARSE;
1437 cur_line->type = FILE_LINE_BLANK;
1439 cur_line = cur_line->next;
1443 if ( (cur_line != NULL) && (0 ==
1444 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1446 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1448 cur_line = cur_line->next;
1449 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1451 if (cur_line->unprocessed[0])
1453 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1457 cur_line->type = FILE_LINE_BLANK;
1459 cur_line = cur_line->next;
1463 if ( (cur_line != NULL) && (0 ==
1464 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1466 cur_line->type = FILE_LINE_ALIAS_HEADER;
1468 cur_line = cur_line->next;
1469 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1471 if (cur_line->unprocessed[0])
1473 /* define an alias */
1474 struct action_alias * new_alias;
1476 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1478 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1479 if (err == JB_ERR_MEMORY)
1483 else if (err != JB_ERR_OK)
1485 /* Line does not contain a name=value pair */
1486 file->parse_error = cur_line;
1487 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1488 return JB_ERR_PARSE;
1491 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1496 free_alias_list(alias_list);
1497 return JB_ERR_MEMORY;
1500 err = get_actions(value, alias_list, new_alias->action);
1503 /* Invalid action or out of memory */
1507 free_alias_list(alias_list);
1508 if (err == JB_ERR_MEMORY)
1514 /* Line does not contain a name=value pair */
1515 file->parse_error = cur_line;
1516 file->parse_error_text = "This alias does not specify a valid set of actions.";
1517 return JB_ERR_PARSE;
1523 new_alias->name = name;
1526 new_alias->next = alias_list;
1527 alias_list = new_alias;
1531 cur_line->type = FILE_LINE_BLANK;
1533 cur_line = cur_line->next;
1537 /* Header done, process the main part of the file */
1538 while (cur_line != NULL)
1540 /* At this point, (cur_line->unprocessed[0] == '{') */
1541 assert(cur_line->unprocessed[0] == '{');
1542 text = cur_line->unprocessed + 1;
1543 len = strlen(text) - 1;
1544 if (text[len] != '}')
1546 /* No closing } on header */
1547 free_alias_list(alias_list);
1548 file->parse_error = cur_line;
1549 file->parse_error_text = "Headers starting with '{' must have a "
1550 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1551 "must close with two brackets ('}}').";
1552 return JB_ERR_PARSE;
1557 /* An invalid {{ header. */
1558 free_alias_list(alias_list);
1559 file->parse_error = cur_line;
1560 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1561 "Please remember that the system (two-bracket) headers must "
1562 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1563 "and must appear before any actions (one-bracket) headers. "
1564 "Also note that system headers may not be repeated.";
1565 return JB_ERR_PARSE;
1568 while ( (*text == ' ') || (*text == '\t') )
1574 && ( (text[len - 1] == ' ')
1575 || (text[len - 1] == '\t') ) )
1580 cur_line->type = FILE_LINE_ACTION;
1582 /* Remove {} and make copy */
1583 if (NULL == (value = (char *) malloc(len + 1)))
1586 free_alias_list(alias_list);
1587 return JB_ERR_MEMORY;
1589 strncpy(value, text, len);
1593 err = get_actions(value, alias_list, cur_line->data.action);
1596 /* Invalid action or out of memory */
1598 free_alias_list(alias_list);
1599 if (err == JB_ERR_MEMORY)
1605 /* Line does not contain a name=value pair */
1606 file->parse_error = cur_line;
1607 file->parse_error_text = "This header does not specify a valid set of actions.";
1608 return JB_ERR_PARSE;
1612 /* Done with string - it was clobbered anyway */
1615 /* Process next line */
1616 cur_line = cur_line->next;
1618 /* Loop processing URL patterns */
1619 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1621 if (cur_line->unprocessed[0])
1623 /* Could parse URL here, but this isn't currently needed */
1625 cur_line->type = FILE_LINE_URL;
1629 cur_line->type = FILE_LINE_BLANK;
1631 cur_line = cur_line->next;
1633 } /* End main while(cur_line != NULL) loop */
1635 free_alias_list(alias_list);
1641 /*********************************************************************
1643 * Function : edit_read_file_lines
1645 * Description : Read all the lines of a file into memory.
1646 * Handles whitespace, comments and line continuation.
1649 * 1 : fp = File to read from. On return, this will be
1650 * at EOF but it will not have been closed.
1651 * 2 : pfile = Destination for a linked list of file_lines.
1652 * Will be set to NULL on error.
1653 * 3 : newline = How to handle newlines.
1655 * Returns : JB_ERR_OK on success
1656 * JB_ERR_MEMORY on out-of-memory
1658 *********************************************************************/
1659 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1661 struct file_line * first_line; /* Keep for return value or to free */
1662 struct file_line * cur_line; /* Current line */
1663 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1671 cur_line = first_line = zalloc(sizeof(struct file_line));
1672 if (cur_line == NULL)
1674 return JB_ERR_MEMORY;
1677 cur_line->type = FILE_LINE_UNPROCESSED;
1679 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1682 /* Out of memory or empty file. */
1683 /* Note that empty file is not an error we propogate up */
1685 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1690 prev_line = cur_line;
1691 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1692 if (cur_line == NULL)
1695 edit_free_file_lines(first_line);
1696 return JB_ERR_MEMORY;
1699 cur_line->type = FILE_LINE_UNPROCESSED;
1701 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1702 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1705 edit_free_file_lines(first_line);
1706 return JB_ERR_MEMORY;
1710 while (rval != JB_ERR_FILE);
1714 /* We allocated one too many - free it */
1715 prev_line->next = NULL;
1718 *pfile = first_line;
1723 /*********************************************************************
1725 * Function : edit_read_file
1727 * Description : Read a complete file into memory.
1728 * Handles CGI parameter parsing. If requested, also
1729 * checks the file's modification timestamp.
1732 * 1 : csp = Current client state (buffers, headers, etc...)
1733 * 2 : parameters = map of cgi parameters.
1734 * 3 : require_version = true to check "ver" parameter.
1735 * 4 : suffix = File extension, e.g. ".action".
1736 * 5 : pfile = Destination for the file. Will be set
1740 * filename : The name of the file to read, without the
1741 * path or ".action" extension.
1742 * ver : (Only if require_version is nonzero)
1743 * Timestamp of the actions file. If wrong, this
1744 * function fails with JB_ERR_MODIFIED.
1746 * Returns : JB_ERR_OK on success
1747 * JB_ERR_MEMORY on out-of-memory
1748 * JB_ERR_CGI_PARAMS if "filename" was not specified
1750 * JB_ERR_FILE if the file cannot be opened or
1752 * JB_ERR_MODIFIED if version checking was requested and
1753 * failed - the file was modified outside
1754 * of this CGI editor instance.
1756 *********************************************************************/
1757 jb_err edit_read_file(struct client_state *csp,
1758 const struct map *parameters,
1759 int require_version,
1761 struct editable_file **pfile)
1763 struct file_line * lines;
1767 const char * identifier;
1768 struct editable_file * file;
1769 unsigned version = 0;
1770 struct stat statbuf[1];
1771 char version_buf[22];
1772 int newline = NEWLINE_UNKNOWN;
1780 err = get_file_name_param(csp, parameters, "f", suffix,
1781 &filename, &identifier);
1787 if (stat(filename, statbuf) < 0)
1789 /* Error, probably file not found. */
1793 version = (unsigned) statbuf->st_mtime;
1795 if (require_version)
1797 unsigned specified_version;
1798 err = get_number_param(csp, parameters, "v", &specified_version);
1805 if (version != specified_version)
1807 return JB_ERR_MODIFIED;
1811 if (NULL == (fp = fopen(filename,"rb")))
1817 err = edit_read_file_lines(fp, &lines, &newline);
1827 file = (struct editable_file *) zalloc(sizeof(*file));
1831 edit_free_file_lines(lines);
1835 file->lines = lines;
1836 file->newline = newline;
1837 file->filename = filename;
1838 file->version = version;
1839 file->identifier = url_encode(identifier);
1841 if (file->identifier == NULL)
1843 edit_free_file(file);
1844 return JB_ERR_MEMORY;
1847 /* Correct file->version_str */
1848 freez(file->version_str);
1849 snprintf(version_buf, 22, "%u", file->version);
1850 version_buf[21] = '\0';
1851 file->version_str = strdup(version_buf);
1852 if (version_buf == NULL)
1854 edit_free_file(file);
1855 return JB_ERR_MEMORY;
1863 /*********************************************************************
1865 * Function : edit_read_actions_file
1867 * Description : Read a complete actions file into memory.
1868 * Handles CGI parameter parsing. If requested, also
1869 * checks the file's modification timestamp.
1871 * If this function detects an error in the categories
1872 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1873 * then it handles it by filling in the specified
1874 * response structure and returning JB_ERR_FILE.
1877 * 1 : csp = Current client state (buffers, headers, etc...)
1878 * 2 : rsp = HTTP response. Only filled in on error.
1879 * 2 : parameters = map of cgi parameters.
1880 * 3 : require_version = true to check "ver" parameter.
1881 * 4 : pfile = Destination for the file. Will be set
1885 * filename : The name of the actions file to read, without the
1886 * path or ".action" extension.
1887 * ver : (Only if require_version is nonzero)
1888 * Timestamp of the actions file. If wrong, this
1889 * function fails with JB_ERR_MODIFIED.
1891 * Returns : JB_ERR_OK on success
1892 * JB_ERR_MEMORY on out-of-memory
1893 * JB_ERR_CGI_PARAMS if "filename" was not specified
1895 * JB_ERR_FILE if the file does not contain valid data,
1896 * or if file cannot be opened or
1897 * contains no data, or if version
1898 * checking was requested and failed.
1900 *********************************************************************/
1901 jb_err edit_read_actions_file(struct client_state *csp,
1902 struct http_response *rsp,
1903 const struct map *parameters,
1904 int require_version,
1905 struct editable_file **pfile)
1908 struct editable_file *file;
1916 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1919 /* Try to handle if possible */
1920 if (err == JB_ERR_FILE)
1922 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1924 else if (err == JB_ERR_MODIFIED)
1926 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1928 if (err == JB_ERR_OK)
1931 * Signal to higher-level CGI code that there was a problem but we
1932 * handled it, they should just return JB_ERR_OK.
1939 err = edit_parse_actions_file(file);
1942 if (err == JB_ERR_PARSE)
1944 err = cgi_error_parse(csp, rsp, file);
1945 if (err == JB_ERR_OK)
1948 * Signal to higher-level CGI code that there was a problem but we
1949 * handled it, they should just return JB_ERR_OK.
1954 edit_free_file(file);
1963 /*********************************************************************
1965 * Function : get_file_name_param
1967 * Description : Get the name of the file to edit from the parameters
1968 * passed to a CGI function. This function handles
1969 * security checks such as blocking urls containing
1970 * "/" or ".", prepending the config file directory,
1971 * and adding the specified suffix.
1973 * (This is an essential security check, otherwise
1974 * users may be able to pass "../../../etc/passwd"
1975 * and overwrite the password file [linux], "prn:"
1976 * and print random data [Windows], etc...)
1978 * This function only allows filenames contining the
1979 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1980 * That's probably too restrictive but at least it's
1984 * 1 : csp = Current client state (buffers, headers, etc...)
1985 * 2 : parameters = map of cgi parameters
1986 * 3 : param_name = The name of the parameter to read
1987 * 4 : suffix = File extension, e.g. ".actions"
1988 * 5 : pfilename = destination for full filename. Caller
1989 * free()s. Set to NULL on error.
1990 * 6 : pparam = destination for partial filename,
1991 * suitable for use in another URL. Allocated as part
1992 * of the map "parameters", so don't free it.
1993 * Set to NULL if not specified.
1995 * Returns : JB_ERR_OK on success
1996 * JB_ERR_MEMORY on out-of-memory
1997 * JB_ERR_CGI_PARAMS if "filename" was not specified
2000 *********************************************************************/
2001 static jb_err get_file_name_param(struct client_state *csp,
2002 const struct map *parameters,
2003 const char *param_name,
2006 const char **pparam)
2024 param = lookup(parameters, param_name);
2027 return JB_ERR_CGI_PARAMS;
2032 len = strlen(param);
2033 if (len >= FILENAME_MAX)
2036 return JB_ERR_CGI_PARAMS;
2039 /* Check every character to see if it's legal */
2041 while ((ch = *s++) != '\0')
2043 if ( ((ch < 'A') || (ch > 'Z'))
2044 && ((ch < 'a') || (ch > 'z'))
2045 && ((ch < '0') || (ch > '9'))
2049 /* Probable hack attempt. */
2050 return JB_ERR_CGI_PARAMS;
2054 /* Append extension */
2055 name = malloc(len + strlen(suffix) + 1);
2058 return JB_ERR_MEMORY;
2060 strcpy(name, param);
2061 strcpy(name + len, suffix);
2064 fullpath = make_path(csp->config->confdir, name);
2067 if (fullpath == NULL)
2069 return JB_ERR_MEMORY;
2073 *pfilename = fullpath;
2079 /*********************************************************************
2081 * Function : get_url_spec_param
2083 * Description : Get a URL pattern from the parameters
2084 * passed to a CGI function. Removes leading/trailing
2085 * spaces and validates it.
2088 * 1 : csp = Current client state (buffers, headers, etc...)
2089 * 2 : parameters = map of cgi parameters
2090 * 3 : name = Name of CGI parameter to read
2091 * 4 : pvalue = destination for value. Will be malloc()'d.
2092 * Set to NULL on error.
2094 * Returns : JB_ERR_OK on success
2095 * JB_ERR_MEMORY on out-of-memory
2096 * JB_ERR_CGI_PARAMS if the parameter was not specified
2099 *********************************************************************/
2100 static jb_err get_url_spec_param(struct client_state *csp,
2101 const struct map *parameters,
2105 const char *orig_param;
2108 struct url_spec compiled[1];
2118 orig_param = lookup(parameters, name);
2121 return JB_ERR_CGI_PARAMS;
2124 /* Copy and trim whitespace */
2125 param = strdup(orig_param);
2128 return JB_ERR_MEMORY;
2132 /* Must be non-empty, and can't allow 1st character to be '{' */
2133 if (param[0] == '\0' || param[0] == '{')
2136 return JB_ERR_CGI_PARAMS;
2139 /* Check for embedded newlines */
2140 for (s = param; *s != '\0'; s++)
2142 if ((*s == '\r') || (*s == '\n'))
2145 return JB_ERR_CGI_PARAMS;
2149 /* Check that regex is valid */
2154 return JB_ERR_MEMORY;
2156 err = create_url_spec(compiled, s);
2161 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2163 free_url_spec(compiled);
2165 if (param[strlen(param) - 1] == '\\')
2168 * Must protect trailing '\\' from becoming line continuation character.
2169 * Two methods: 1) If it's a domain only, add a trailing '/'.
2170 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2172 if (strchr(param, '/') == NULL)
2174 err = string_append(¶m, "/");
2178 err = string_append(¶m, "(?:)");
2185 /* Check that the modified regex is valid */
2190 return JB_ERR_MEMORY;
2192 err = create_url_spec(compiled, s);
2197 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2199 free_url_spec(compiled);
2206 /*********************************************************************
2208 * Function : map_radio
2210 * Description : Map a set of radio button values. E.g. if you have
2211 * 3 radio buttons, declare them as:
2212 * <option type="radio" name="xyz" @xyz-a@>
2213 * <option type="radio" name="xyz" @xyz-b@>
2214 * <option type="radio" name="xyz" @xyz-c@>
2215 * Then map one of the @xyz-?@ variables to "checked"
2216 * and all the others to empty by calling:
2217 * map_radio(exports, "xyz", "abc", sel)
2218 * Where 'sel' is 'a', 'b', or 'c'.
2221 * 1 : exports = Exports map to modify.
2222 * 2 : optionname = name for map
2223 * 3 : values = null-terminated list of values;
2224 * 4 : value = Selected value.
2226 * CGI Parameters : None
2228 * Returns : JB_ERR_OK on success
2229 * JB_ERR_MEMORY on out-of-memory
2231 *********************************************************************/
2232 static jb_err map_radio(struct map * exports,
2233 const char * optionname,
2234 const char * values,
2246 len = strlen(optionname);
2247 buf = malloc(len + 3);
2250 return JB_ERR_MEMORY;
2253 strcpy(buf, optionname);
2258 while ((c = *values++) != '\0')
2263 if (map(exports, buf, 1, "", 1))
2265 return JB_ERR_MEMORY;
2271 return map(exports, buf, 0, "checked", 1);
2275 /*********************************************************************
2277 * Function : cgi_error_modified
2279 * Description : CGI function that is called when a file is modified
2280 * outside the CGI editor.
2283 * 1 : csp = Current client state (buffers, headers, etc...)
2284 * 2 : rsp = http_response data structure for output
2285 * 3 : filename = The file that was modified.
2287 * CGI Parameters : none
2289 * Returns : JB_ERR_OK on success
2290 * JB_ERR_MEMORY on out-of-memory error.
2292 *********************************************************************/
2293 jb_err cgi_error_modified(struct client_state *csp,
2294 struct http_response *rsp,
2295 const char *filename)
2297 struct map *exports;
2304 if (NULL == (exports = default_exports(csp, NULL)))
2306 return JB_ERR_MEMORY;
2309 err = map(exports, "f", 1, html_encode(filename), 0);
2316 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2320 /*********************************************************************
2322 * Function : cgi_error_parse
2324 * Description : CGI function that is called when a file cannot
2325 * be parsed by the CGI editor.
2328 * 1 : csp = Current client state (buffers, headers, etc...)
2329 * 2 : rsp = http_response data structure for output
2330 * 3 : file = The file that was modified.
2332 * CGI Parameters : none
2334 * Returns : JB_ERR_OK on success
2335 * JB_ERR_MEMORY on out-of-memory error.
2337 *********************************************************************/
2338 jb_err cgi_error_parse(struct client_state *csp,
2339 struct http_response *rsp,
2340 struct editable_file *file)
2342 struct map *exports;
2344 struct file_line *cur_line;
2350 if (NULL == (exports = default_exports(csp, NULL)))
2352 return JB_ERR_MEMORY;
2355 err = map(exports, "f", 1, file->identifier, 1);
2356 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2358 cur_line = file->parse_error;
2361 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2362 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2370 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2374 /*********************************************************************
2376 * Function : cgi_error_file
2378 * Description : CGI function that is called when a file cannot be
2379 * opened by the CGI editor.
2382 * 1 : csp = Current client state (buffers, headers, etc...)
2383 * 2 : rsp = http_response data structure for output
2384 * 3 : filename = The file that was modified.
2386 * CGI Parameters : none
2388 * Returns : JB_ERR_OK on success
2389 * JB_ERR_MEMORY on out-of-memory error.
2391 *********************************************************************/
2392 jb_err cgi_error_file(struct client_state *csp,
2393 struct http_response *rsp,
2394 const char *filename)
2396 struct map *exports;
2403 if (NULL == (exports = default_exports(csp, NULL)))
2405 return JB_ERR_MEMORY;
2408 err = map(exports, "f", 1, html_encode(filename), 0);
2415 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2419 /*********************************************************************
2421 * Function : cgi_error_file
2423 * Description : CGI function that is called when a file cannot be
2424 * opened for writing by the CGI editor.
2427 * 1 : csp = Current client state (buffers, headers, etc...)
2428 * 2 : rsp = http_response data structure for output
2429 * 3 : filename = The file that we can't write to
2431 * CGI Parameters : none
2433 * Returns : JB_ERR_OK on success
2434 * JB_ERR_MEMORY on out-of-memory error.
2436 *********************************************************************/
2437 jb_err cgi_error_file_read_only(struct client_state *csp,
2438 struct http_response *rsp,
2439 const char *filename)
2441 struct map *exports;
2448 if (NULL == (exports = default_exports(csp, NULL)))
2450 return JB_ERR_MEMORY;
2453 err = map(exports, "f", 1, html_encode(filename), 0);
2460 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2464 /*********************************************************************
2466 * Function : cgi_edit_actions
2468 * Description : CGI function that allows the user to choose which
2469 * actions file to edit.
2472 * 1 : csp = Current client state (buffers, headers, etc...)
2473 * 2 : rsp = http_response data structure for output
2474 * 3 : parameters = map of cgi parameters
2476 * CGI Parameters : None
2478 * Returns : JB_ERR_OK on success
2479 * JB_ERR_MEMORY on out-of-memory error
2481 *********************************************************************/
2482 jb_err cgi_edit_actions(struct client_state *csp,
2483 struct http_response *rsp,
2484 const struct map *parameters)
2487 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2489 return cgi_error_disabled(csp, rsp);
2492 /* FIXME: Incomplete */
2494 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2499 /*********************************************************************
2501 * Function : cgi_edit_actions_list
2503 * Description : CGI function that edits the actions list.
2504 * FIXME: This function shouldn't FATAL ever.
2505 * FIXME: This function doesn't check the retval of map()
2507 * 1 : csp = Current client state (buffers, headers, etc...)
2508 * 2 : rsp = http_response data structure for output
2509 * 3 : parameters = map of cgi parameters
2511 * CGI Parameters : filename
2513 * Returns : JB_ERR_OK on success
2514 * JB_ERR_MEMORY on out-of-memory
2515 * JB_ERR_FILE if the file cannot be opened or
2517 * JB_ERR_CGI_PARAMS if "filename" was not specified
2520 *********************************************************************/
2521 jb_err cgi_edit_actions_list(struct client_state *csp,
2522 struct http_response *rsp,
2523 const struct map *parameters)
2525 char * section_template;
2526 char * url_template;
2531 struct map * exports;
2532 struct map * section_exports;
2533 struct map * url_exports;
2534 struct editable_file * file;
2535 struct file_line * cur_line;
2536 unsigned line_number = 0;
2537 unsigned prev_section_line_number = ((unsigned) (-1));
2539 struct file_list * fl;
2540 struct url_actions * b;
2541 char * buttons = NULL;
2544 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2546 return cgi_error_disabled(csp, rsp);
2549 if (NULL == (exports = default_exports(csp, NULL)))
2551 return JB_ERR_MEMORY;
2554 /* Load actions file */
2555 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2558 /* No filename specified, can't read file, or out of memory. */
2559 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2562 /* Find start of actions in file */
2563 cur_line = file->lines;
2565 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2567 cur_line = cur_line->next;
2572 * Conventional actions files should have a match all block
2574 * cur_line = {...global actions...}
2575 * cur_line->next = /
2576 * cur_line->next->next = {...actions...} or EOF
2578 if ( (cur_line != NULL)
2579 && (cur_line->type == FILE_LINE_ACTION)
2580 && (cur_line->next != NULL)
2581 && (cur_line->next->type == FILE_LINE_URL)
2582 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2583 && ( (cur_line->next->next == NULL)
2584 || (cur_line->next->next->type != FILE_LINE_URL)
2588 * Generate string with buttons to set actions for "/" to
2589 * any predefined set of actions (named standard.*, probably
2590 * residing in standard.action).
2593 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2596 edit_free_file(file);
2598 if (err == JB_ERR_FILE)
2600 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2605 err = template_fill(§ion_template, exports);
2608 edit_free_file(file);
2613 buttons = strdup("");
2614 for (i = 0; i < MAX_AF_FILES; i++)
2616 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2618 for (b = b->next; NULL != b; b = b->next)
2620 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2622 if (err || (NULL == (section_exports = new_map())))
2625 free(section_template);
2626 edit_free_file(file);
2628 return JB_ERR_MEMORY;
2631 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2633 if (err || (NULL == (s = strdup(section_template))))
2635 free_map(section_exports);
2637 free(section_template);
2638 edit_free_file(file);
2640 return JB_ERR_MEMORY;
2643 if (!err) err = template_fill(&s, section_exports);
2644 free_map(section_exports);
2645 if (!err) err = string_join(&buttons, s);
2650 freez(section_template);
2651 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2654 * Conventional actions file, supply extra editing help.
2655 * (e.g. don't allow them to make it an unconventional one).
2657 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2659 snprintf(buf, 150, "%d", line_number);
2660 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2661 snprintf(buf, 150, "%d", line_number + 2);
2662 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2663 if (!err) err = map(exports, "all-urls-actions", 1,
2664 actions_to_html(csp, cur_line->data.action), 0);
2666 /* Skip the 2 lines */
2667 cur_line = cur_line->next->next;
2671 * Note that prev_section_line_number is NOT set here.
2672 * This is deliberate and not a bug. It stops a "Move up"
2673 * option appearing on the next section. Clicking "Move
2674 * up" would make the actions file unconventional, which
2675 * we don't want, so we hide this option.
2681 * Non-standard actions file - does not begin with
2682 * the "All URLs" section.
2684 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2687 /* Set up global exports */
2689 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2690 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2692 /* Discourage private additions to default.action */
2694 if (!err) err = map_conditional(exports, "default-action",
2695 (strcmp("default", lookup(parameters, "f")) == 0));
2698 edit_free_file(file);
2703 /* Should do all global exports above this point */
2705 /* Load templates */
2707 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2710 edit_free_file(file);
2712 if (err == JB_ERR_FILE)
2714 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2719 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2722 free(section_template);
2723 edit_free_file(file);
2725 if (err == JB_ERR_FILE)
2727 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2732 err = template_fill(§ion_template, exports);
2736 edit_free_file(file);
2742 err = template_fill(&url_template, exports);
2745 free(section_template);
2746 edit_free_file(file);
2751 if (NULL == (sections = strdup("")))
2753 free(section_template);
2755 edit_free_file(file);
2757 return JB_ERR_MEMORY;
2760 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2762 if (NULL == (section_exports = new_map()))
2765 free(section_template);
2767 edit_free_file(file);
2769 return JB_ERR_MEMORY;
2772 snprintf(buf, 150, "%d", line_number);
2773 err = map(section_exports, "s", 1, buf, 1);
2774 if (!err) err = map(section_exports, "actions", 1,
2775 actions_to_html(csp, cur_line->data.action), 0);
2778 && (cur_line->next != NULL)
2779 && (cur_line->next->type == FILE_LINE_URL))
2781 /* This section contains at least one URL, don't allow delete */
2782 err = map_block_killer(section_exports, "empty-section");
2786 if (!err) err = map_block_keep(section_exports, "empty-section");
2789 if (prev_section_line_number != ((unsigned)(-1)))
2791 /* Not last section */
2792 snprintf(buf, 150, "%d", prev_section_line_number);
2793 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2794 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2799 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2801 prev_section_line_number = line_number;
2806 free(section_template);
2808 edit_free_file(file);
2810 free_map(section_exports);
2814 /* Should do all section-specific exports above this point */
2816 if (NULL == (urls = strdup("")))
2819 free(section_template);
2821 edit_free_file(file);
2823 free_map(section_exports);
2824 return JB_ERR_MEMORY;
2829 cur_line = cur_line->next;
2832 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2834 if (NULL == (url_exports = new_map()))
2838 free(section_template);
2840 edit_free_file(file);
2842 free_map(section_exports);
2843 return JB_ERR_MEMORY;
2846 snprintf(buf, 150, "%d", line_number);
2847 err = map(url_exports, "p", 1, buf, 1);
2849 snprintf(buf, 150, "%d", url_1_2);
2850 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2852 if (!err) err = map(url_exports, "url-html", 1,
2853 html_encode(cur_line->unprocessed), 0);
2854 if (!err) err = map(url_exports, "url", 1,
2855 url_encode(cur_line->unprocessed), 0);
2861 free(section_template);
2863 edit_free_file(file);
2865 free_map(section_exports);
2866 free_map(url_exports);
2870 if (NULL == (s = strdup(url_template)))
2874 free(section_template);
2876 edit_free_file(file);
2878 free_map(section_exports);
2879 free_map(url_exports);
2880 return JB_ERR_MEMORY;
2883 err = template_fill(&s, section_exports);
2884 if (!err) err = template_fill(&s, url_exports);
2885 if (!err) err = string_append(&urls, s);
2887 free_map(url_exports);
2894 free(section_template);
2896 edit_free_file(file);
2898 free_map(section_exports);
2902 url_1_2 = 3 - url_1_2;
2904 cur_line = cur_line->next;
2908 err = map(section_exports, "urls", 1, urls, 0);
2910 /* Could also do section-specific exports here, but it wouldn't be as fast */
2912 snprintf(buf, 150, "%d", line_number);
2913 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2915 if ( (cur_line != NULL)
2916 && (cur_line->type == FILE_LINE_ACTION))
2918 /* Not last section */
2919 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2924 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2930 free(section_template);
2932 edit_free_file(file);
2934 free_map(section_exports);
2938 if (NULL == (s = strdup(section_template)))
2941 free(section_template);
2943 edit_free_file(file);
2945 free_map(section_exports);
2946 return JB_ERR_MEMORY;
2949 err = template_fill(&s, section_exports);
2950 if (!err) err = string_append(§ions, s);
2953 free_map(section_exports);
2958 free(section_template);
2960 edit_free_file(file);
2966 edit_free_file(file);
2967 free(section_template);
2970 err = map(exports, "sections", 1, sections, 0);
2977 /* Could also do global exports here, but it wouldn't be as fast */
2979 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2983 /*********************************************************************
2985 * Function : cgi_edit_actions_for_url
2987 * Description : CGI function that edits the Actions list.
2990 * 1 : csp = Current client state (buffers, headers, etc...)
2991 * 2 : rsp = http_response data structure for output
2992 * 3 : parameters = map of cgi parameters
2994 * CGI Parameters : None
2996 * Returns : JB_ERR_OK on success
2997 * JB_ERR_MEMORY on out-of-memory
2998 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2999 * specified or not valid.
3001 *********************************************************************/
3002 jb_err cgi_edit_actions_for_url(struct client_state *csp,
3003 struct http_response *rsp,
3004 const struct map *parameters)
3006 struct map * exports;
3008 struct editable_file * file;
3009 struct file_line * cur_line;
3010 unsigned line_number;
3012 struct re_filterfile_spec *filter_group;
3013 int i, have_filters = 0;
3015 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3017 return cgi_error_disabled(csp, rsp);
3020 err = get_number_param(csp, parameters, "s", §ionid);
3026 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3029 /* No filename specified, can't read file, modified, or out of memory. */
3030 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3033 cur_line = file->lines;
3035 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3037 cur_line = cur_line->next;
3040 if ( (cur_line == NULL)
3041 || (line_number != sectionid)
3043 || (cur_line->type != FILE_LINE_ACTION))
3045 /* Invalid "sectionid" parameter */
3046 edit_free_file(file);
3047 return JB_ERR_CGI_PARAMS;
3050 if (NULL == (exports = default_exports(csp, NULL)))
3052 edit_free_file(file);
3053 return JB_ERR_MEMORY;
3056 err = map(exports, "f", 1, file->identifier, 1);
3057 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3058 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3060 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3063 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3064 * length limitation and ignore clicks on the Submit buttons if
3065 * the resulting GET URL would be longer than their limit.
3067 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3068 * reached this limit and action editing stopped working in these
3069 * browsers (BR #1570678).
3071 * The config option split-large-forms works around this browser
3072 * bug (HTTP has no URL lenght limitation) by deviding the action
3073 * list form into multiple smaller ones. It means the URLs are shorter
3074 * and work in broken browsers as well, but the user can no longer change
3075 * all actions with one submit.
3077 * A better solution would be to switch to POST requests,
3078 * but this will do for now.
3080 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3082 /* Generate multiple smaller form by killing the big one. */
3083 err = map_block_killer(exports, "one-form-only");
3087 /* Default: Generate one large form by killing the smaller ones. */
3088 err = map_block_killer(exports, "multiple-forms");
3091 for (i = 0; i < MAX_AF_FILES; i++)
3093 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3095 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3103 edit_free_file(file);
3108 if (0 == have_filters)
3109 err = map(exports, "filter-params", 1, "", 1);
3112 /* We have some entries in the filter list */
3114 int filter_identifier = 0;
3115 char * filter_template;
3117 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3120 edit_free_file(file);
3122 if (err == JB_ERR_FILE)
3124 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3129 err = template_fill(&filter_template, exports);
3131 result = strdup("");
3133 for (i = 0; i < MAX_AF_FILES; i++)
3135 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3137 filter_group = csp->rlist[i]->f;
3138 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3140 char current_mode = 'x';
3141 struct list_entry *filter_name;
3143 struct map *line_exports;
3146 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
3147 while ((filter_name != NULL)
3148 && (0 != strcmp(filter_group->name, filter_name->str)))
3150 filter_name = filter_name->next;
3153 if (filter_name != NULL)
3159 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
3160 while ((filter_name != NULL)
3161 && (0 != strcmp(filter_group->name, filter_name->str)))
3163 filter_name = filter_name->next;
3165 if (filter_name != NULL)
3171 /* Generate a unique serial number */
3172 snprintf(number, sizeof(number), "%x", filter_identifier++);
3173 number[sizeof(number) - 1] = '\0';
3175 line_exports = new_map();
3176 if (line_exports == NULL)
3178 err = JB_ERR_MEMORY;
3183 if (!err) err = map(line_exports, "index", 1, number, 1);
3184 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3185 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3186 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3191 this_line = strdup(filter_template);
3192 if (this_line == NULL) err = JB_ERR_MEMORY;
3194 if (!err) err = template_fill(&this_line, line_exports);
3195 string_join(&result, this_line);
3197 free_map(line_exports);
3202 freez(filter_template);
3206 err = map(exports, "filter-params", 1, result, 0);
3214 if (!err) err = map_radio(exports, "filter-all", "nx",
3215 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3217 edit_free_file(file);
3225 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3229 /*********************************************************************
3231 * Function : cgi_edit_actions_submit
3233 * Description : CGI function that actually edits the Actions list.
3236 * 1 : csp = Current client state (buffers, headers, etc...)
3237 * 2 : rsp = http_response data structure for output
3238 * 3 : parameters = map of cgi parameters
3240 * CGI Parameters : None
3242 * Returns : JB_ERR_OK on success
3243 * JB_ERR_MEMORY on out-of-memory
3244 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3245 * specified or not valid.
3247 *********************************************************************/
3248 jb_err cgi_edit_actions_submit(struct client_state *csp,
3249 struct http_response *rsp,
3250 const struct map *parameters)
3256 struct editable_file * file;
3257 struct file_line * cur_line;
3258 unsigned line_number;
3261 int filter_identifier;
3262 const char * action_set_name;
3264 struct file_list * fl;
3265 struct url_actions * b;
3267 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3269 return cgi_error_disabled(csp, rsp);
3272 err = get_number_param(csp, parameters, "s", §ionid);
3278 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3281 /* No filename specified, can't read file, modified, or out of memory. */
3282 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3285 cur_line = file->lines;
3287 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3289 cur_line = cur_line->next;
3292 if ( (cur_line == NULL)
3293 || (line_number != sectionid)
3295 || (cur_line->type != FILE_LINE_ACTION))
3297 /* Invalid "sectionid" parameter */
3298 edit_free_file(file);
3299 return JB_ERR_CGI_PARAMS;
3302 get_string_param(parameters, "p", &action_set_name);
3303 if (action_set_name != NULL)
3305 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3307 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3309 for (b = b->next; NULL != b; b = b->next)
3311 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3313 copy_action(cur_line->data.action, b->action);
3319 edit_free_file(file);
3320 return JB_ERR_CGI_PARAMS;
3326 err = actions_from_radio(parameters, cur_line->data.action);
3332 edit_free_file(file);
3336 ch = get_char_param(parameters, "filter_all");
3339 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3340 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3341 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3345 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3348 for (filter_identifier = 0; !err; filter_identifier++)
3355 /* Generate the keys */
3356 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3357 key_value[sizeof(key_value) - 1] = '\0';
3358 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3359 key_name[sizeof(key_name) - 1] = '\0';
3361 err = get_string_param(parameters, key_name, &name);
3370 value = get_char_param(parameters, key_value);
3373 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3374 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3375 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3377 else if (value == 'N')
3379 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3380 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3382 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3383 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3386 else if (value == 'X')
3388 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3389 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3396 edit_free_file(file);
3400 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3403 edit_free_file(file);
3404 return JB_ERR_MEMORY;
3407 len = strlen(actiontext);
3411 * Empty action - must special-case this.
3412 * Simply setting len to 1 is sufficient...
3417 if (NULL == (newtext = malloc(len + 2)))
3421 edit_free_file(file);
3422 return JB_ERR_MEMORY;
3424 strcpy(newtext, actiontext);
3428 newtext[len + 1] = '\0';
3430 freez(cur_line->raw);
3431 freez(cur_line->unprocessed);
3432 cur_line->unprocessed = newtext;
3434 err = edit_write_file(file);
3437 /* Error writing file */
3438 if (err == JB_ERR_FILE)
3440 /* Read-only file. */
3441 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3443 edit_free_file(file);
3447 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3448 (long) time(NULL), file->identifier, sectionid);
3450 edit_free_file(file);
3452 return cgi_redirect(rsp, target);
3456 /*********************************************************************
3458 * Function : cgi_edit_actions_url
3460 * Description : CGI function that actually edits a URL pattern in
3464 * 1 : csp = Current client state (buffers, headers, etc...)
3465 * 2 : rsp = http_response data structure for output
3466 * 3 : parameters = map of cgi parameters
3469 * filename : Identifies the file to edit
3470 * ver : File's last-modified time
3471 * section : Line number of section to edit
3472 * pattern : Line number of pattern to edit
3473 * newval : New value for pattern
3475 * Returns : JB_ERR_OK on success
3476 * JB_ERR_MEMORY on out-of-memory
3477 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3478 * specified or not valid.
3480 *********************************************************************/
3481 jb_err cgi_edit_actions_url(struct client_state *csp,
3482 struct http_response *rsp,
3483 const struct map *parameters)
3487 struct editable_file * file;
3488 struct file_line * cur_line;
3489 unsigned line_number;
3490 unsigned section_start_line_number = 0;
3498 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3500 return cgi_error_disabled(csp, rsp);
3503 err = get_number_param(csp, parameters, "p", &patternid);
3510 return JB_ERR_CGI_PARAMS;
3513 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3519 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3522 /* No filename specified, can't read file, modified, or out of memory. */
3524 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3528 cur_line = file->lines;
3530 while ((cur_line != NULL) && (line_number < patternid))
3532 if (cur_line->type == FILE_LINE_ACTION)
3534 section_start_line_number = line_number;
3536 cur_line = cur_line->next;
3540 if ( (cur_line == NULL)
3541 || (cur_line->type != FILE_LINE_URL))
3543 /* Invalid "patternid" parameter */
3545 edit_free_file(file);
3546 return JB_ERR_CGI_PARAMS;
3549 /* At this point, the line to edit is in cur_line */
3551 freez(cur_line->raw);
3552 freez(cur_line->unprocessed);
3553 cur_line->unprocessed = new_pattern;
3555 err = edit_write_file(file);
3558 /* Error writing file */
3559 if (err == JB_ERR_FILE)
3561 /* Read-only file. */
3562 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3564 edit_free_file(file);
3568 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3569 (long) time(NULL), file->identifier, section_start_line_number);
3571 edit_free_file(file);
3573 return cgi_redirect(rsp, target);
3577 /*********************************************************************
3579 * Function : cgi_edit_actions_add_url
3581 * Description : CGI function that actually adds a URL pattern to
3585 * 1 : csp = Current client state (buffers, headers, etc...)
3586 * 2 : rsp = http_response data structure for output
3587 * 3 : parameters = map of cgi parameters
3590 * filename : Identifies the file to edit
3591 * ver : File's last-modified time
3592 * section : Line number of section to edit
3593 * newval : New pattern
3595 * Returns : JB_ERR_OK on success
3596 * JB_ERR_MEMORY on out-of-memory
3597 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3598 * specified or not valid.
3600 *********************************************************************/
3601 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3602 struct http_response *rsp,
3603 const struct map *parameters)
3607 struct file_line * new_line;
3608 struct editable_file * file;
3609 struct file_line * cur_line;
3610 unsigned line_number;
3614 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3616 return cgi_error_disabled(csp, rsp);
3619 err = get_number_param(csp, parameters, "s", §ionid);
3626 return JB_ERR_CGI_PARAMS;
3629 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3635 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3638 /* No filename specified, can't read file, modified, or out of memory. */
3640 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3644 cur_line = file->lines;
3646 while ((cur_line != NULL) && (line_number < sectionid))
3648 cur_line = cur_line->next;
3652 if ( (cur_line == NULL)
3653 || (cur_line->type != FILE_LINE_ACTION))
3655 /* Invalid "sectionid" parameter */
3657 edit_free_file(file);
3658 return JB_ERR_CGI_PARAMS;
3661 /* At this point, the section header is in cur_line - add after this. */
3663 /* Allocate the new line */
3664 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3665 if (new_line == NULL)
3668 edit_free_file(file);
3669 return JB_ERR_MEMORY;
3672 /* Fill in the data members of the new line */
3673 new_line->raw = NULL;
3674 new_line->prefix = NULL;
3675 new_line->unprocessed = new_pattern;
3676 new_line->type = FILE_LINE_URL;
3678 /* Link new_line into the list, after cur_line */
3679 new_line->next = cur_line->next;
3680 cur_line->next = new_line;
3682 /* Done making changes, now commit */
3684 err = edit_write_file(file);
3687 /* Error writing file */
3688 if (err == JB_ERR_FILE)
3690 /* Read-only file. */
3691 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3693 edit_free_file(file);
3697 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3698 (long) time(NULL), file->identifier, sectionid);
3700 edit_free_file(file);
3702 return cgi_redirect(rsp, target);
3706 /*********************************************************************
3708 * Function : cgi_edit_actions_remove_url
3710 * Description : CGI function that actually removes a URL pattern from
3714 * 1 : csp = Current client state (buffers, headers, etc...)
3715 * 2 : rsp = http_response data structure for output
3716 * 3 : parameters = map of cgi parameters
3719 * f : (filename) Identifies the file to edit
3720 * v : (version) File's last-modified time
3721 * p : (pattern) Line number of pattern to remove
3723 * Returns : JB_ERR_OK on success
3724 * JB_ERR_MEMORY on out-of-memory
3725 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3726 * specified or not valid.
3728 *********************************************************************/
3729 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3730 struct http_response *rsp,
3731 const struct map *parameters)
3734 struct editable_file * file;
3735 struct file_line * cur_line;
3736 struct file_line * prev_line;
3737 unsigned line_number;
3738 unsigned section_start_line_number = 0;
3742 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3744 return cgi_error_disabled(csp, rsp);
3747 err = get_number_param(csp, parameters, "p", &patternid);
3753 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3756 /* No filename specified, can't read file, modified, or out of memory. */
3757 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3762 cur_line = file->lines;
3764 while ((cur_line != NULL) && (line_number < patternid))
3766 if (cur_line->type == FILE_LINE_ACTION)
3768 section_start_line_number = line_number;
3770 prev_line = cur_line;
3771 cur_line = cur_line->next;
3775 if ( (cur_line == NULL)
3776 || (prev_line == NULL)
3777 || (cur_line->type != FILE_LINE_URL))
3779 /* Invalid "patternid" parameter */
3780 edit_free_file(file);
3781 return JB_ERR_CGI_PARAMS;
3784 /* At this point, the line to remove is in cur_line, and the previous
3785 * one is in prev_line
3788 /* Unlink cur_line */
3789 prev_line->next = cur_line->next;
3790 cur_line->next = NULL;
3793 edit_free_file_lines(cur_line);
3795 err = edit_write_file(file);
3798 /* Error writing file */
3799 if (err == JB_ERR_FILE)
3801 /* Read-only file. */
3802 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3804 edit_free_file(file);
3808 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3809 (long) time(NULL), file->identifier, section_start_line_number);
3811 edit_free_file(file);
3813 return cgi_redirect(rsp, target);
3817 /*********************************************************************
3819 * Function : cgi_edit_actions_section_remove
3821 * Description : CGI function that actually removes a whole section from
3822 * the actions file. The section must be empty first
3823 * (else JB_ERR_CGI_PARAMS).
3826 * 1 : csp = Current client state (buffers, headers, etc...)
3827 * 2 : rsp = http_response data structure for output
3828 * 3 : parameters = map of cgi parameters
3831 * f : (filename) Identifies the file to edit
3832 * v : (version) File's last-modified time
3833 * s : (section) Line number of section to edit
3835 * Returns : JB_ERR_OK on success
3836 * JB_ERR_MEMORY on out-of-memory
3837 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3838 * specified or not valid.
3840 *********************************************************************/
3841 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3842 struct http_response *rsp,
3843 const struct map *parameters)
3846 struct editable_file * file;
3847 struct file_line * cur_line;
3848 struct file_line * prev_line;
3849 unsigned line_number;
3853 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3855 return cgi_error_disabled(csp, rsp);
3858 err = get_number_param(csp, parameters, "s", §ionid);
3864 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3867 /* No filename specified, can't read file, modified, or out of memory. */
3868 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3872 cur_line = file->lines;
3875 while ((cur_line != NULL) && (line_number < sectionid))
3877 prev_line = cur_line;
3878 cur_line = cur_line->next;
3882 if ( (cur_line == NULL)
3883 || (cur_line->type != FILE_LINE_ACTION) )
3885 /* Invalid "sectionid" parameter */
3886 edit_free_file(file);
3887 return JB_ERR_CGI_PARAMS;
3890 if ( (cur_line->next != NULL)
3891 && (cur_line->next->type == FILE_LINE_URL) )
3893 /* Section not empty. */
3894 edit_free_file(file);
3895 return JB_ERR_CGI_PARAMS;
3898 /* At this point, the line to remove is in cur_line, and the previous
3899 * one is in prev_line
3902 /* Unlink cur_line */
3903 if (prev_line == NULL)
3905 /* Removing the first line from the file */
3906 file->lines = cur_line->next;
3910 prev_line->next = cur_line->next;
3912 cur_line->next = NULL;
3915 edit_free_file_lines(cur_line);
3917 err = edit_write_file(file);
3920 /* Error writing file */
3921 if (err == JB_ERR_FILE)
3923 /* Read-only file. */
3924 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3926 edit_free_file(file);
3930 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
3931 (long) time(NULL), file->identifier);
3933 edit_free_file(file);
3935 return cgi_redirect(rsp, target);
3939 /*********************************************************************
3941 * Function : cgi_edit_actions_section_add
3943 * Description : CGI function that adds a new empty section to
3947 * 1 : csp = Current client state (buffers, headers, etc...)
3948 * 2 : rsp = http_response data structure for output
3949 * 3 : parameters = map of cgi parameters
3952 * f : (filename) Identifies the file to edit
3953 * v : (version) File's last-modified time
3954 * s : (section) Line number of section to add after, 0 for
3957 * Returns : JB_ERR_OK on success
3958 * JB_ERR_MEMORY on out-of-memory
3959 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3960 * specified or not valid.
3962 *********************************************************************/
3963 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3964 struct http_response *rsp,
3965 const struct map *parameters)
3968 struct file_line * new_line;
3970 struct editable_file * file;
3971 struct file_line * cur_line;
3972 unsigned line_number;
3976 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3978 return cgi_error_disabled(csp, rsp);
3981 err = get_number_param(csp, parameters, "s", §ionid);
3987 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3990 /* No filename specified, can't read file, modified, or out of memory. */
3991 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3995 cur_line = file->lines;
3997 if (sectionid <= 1U)
3999 /* Add to start of file */
4000 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4002 /* There's something in the file, find the line before the first
4005 while ( (cur_line->next != NULL)
4006 && (cur_line->next->type != FILE_LINE_ACTION) )
4008 cur_line = cur_line->next;
4014 /* File starts with action line, so insert at top */
4020 /* Add after stated section. */
4021 while ((cur_line != NULL) && (line_number < sectionid))
4023 cur_line = cur_line->next;
4027 if ( (cur_line == NULL)
4028 || (cur_line->type != FILE_LINE_ACTION))
4030 /* Invalid "sectionid" parameter */
4031 edit_free_file(file);
4032 return JB_ERR_CGI_PARAMS;
4035 /* Skip through the section to find the last line in it. */
4036 while ( (cur_line->next != NULL)
4037 && (cur_line->next->type != FILE_LINE_ACTION) )
4039 cur_line = cur_line->next;
4044 /* At this point, the last line in the previous section is in cur_line
4045 * - add after this. (Or if we need to add as the first line, cur_line
4049 new_text = strdup("{}");
4050 if (NULL == new_text)
4052 edit_free_file(file);
4053 return JB_ERR_MEMORY;
4056 /* Allocate the new line */
4057 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4058 if (new_line == NULL)
4061 edit_free_file(file);
4062 return JB_ERR_MEMORY;
4065 /* Fill in the data members of the new line */
4066 new_line->raw = NULL;
4067 new_line->prefix = NULL;
4068 new_line->unprocessed = new_text;
4069 new_line->type = FILE_LINE_ACTION;
4071 if (cur_line != NULL)
4073 /* Link new_line into the list, after cur_line */
4074 new_line->next = cur_line->next;
4075 cur_line->next = new_line;
4079 /* Link new_line into the list, as first line */
4080 new_line->next = file->lines;
4081 file->lines = new_line;
4084 /* Done making changes, now commit */
4086 err = edit_write_file(file);
4089 /* Error writing file */
4090 if (err == JB_ERR_FILE)
4092 /* Read-only file. */
4093 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4095 edit_free_file(file);
4099 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4100 (long) time(NULL), file->identifier);
4102 edit_free_file(file);
4104 return cgi_redirect(rsp, target);
4108 /*********************************************************************
4110 * Function : cgi_edit_actions_section_swap
4112 * Description : CGI function that swaps the order of two sections
4113 * in the actions file. Note that this CGI can actually
4114 * swap any two arbitrary sections, but the GUI interface
4115 * currently only allows consecutive sections to be
4119 * 1 : csp = Current client state (buffers, headers, etc...)
4120 * 2 : rsp = http_response data structure for output
4121 * 3 : parameters = map of cgi parameters
4124 * f : (filename) Identifies the file to edit
4125 * v : (version) File's last-modified time
4126 * s1 : (section1) Line number of first section to swap
4127 * s2 : (section2) Line number of second section to swap
4129 * Returns : JB_ERR_OK on success
4130 * JB_ERR_MEMORY on out-of-memory
4131 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4132 * specified or not valid.
4134 *********************************************************************/
4135 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4136 struct http_response *rsp,
4137 const struct map *parameters)
4141 struct editable_file * file;
4142 struct file_line * cur_line;
4143 struct file_line * prev_line;
4144 struct file_line * line_before_section1;
4145 struct file_line * line_start_section1;
4146 struct file_line * line_end_section1;
4147 struct file_line * line_after_section1;
4148 struct file_line * line_before_section2;
4149 struct file_line * line_start_section2;
4150 struct file_line * line_end_section2;
4151 struct file_line * line_after_section2;
4152 unsigned line_number;
4156 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4158 return cgi_error_disabled(csp, rsp);
4161 err = get_number_param(csp, parameters, "s1", §ion1);
4162 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4168 if (section1 > section2)
4170 unsigned temp = section2;
4171 section2 = section1;
4175 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4178 /* No filename specified, can't read file, modified, or out of memory. */
4179 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4182 /* Start at the beginning... */
4184 cur_line = file->lines;
4187 /* ... find section1 ... */
4188 while ((cur_line != NULL) && (line_number < section1))
4190 prev_line = cur_line;
4191 cur_line = cur_line->next;
4195 if ( (cur_line == NULL)
4196 || (cur_line->type != FILE_LINE_ACTION) )
4198 /* Invalid "section1" parameter */
4199 edit_free_file(file);
4200 return JB_ERR_CGI_PARAMS;
4203 /* If no-op, we've validated params and can skip the rest. */
4204 if (section1 != section2)
4206 /* ... find the end of section1 ... */
4207 line_before_section1 = prev_line;
4208 line_start_section1 = cur_line;
4211 prev_line = cur_line;
4212 cur_line = cur_line->next;
4215 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4216 line_end_section1 = prev_line;
4217 line_after_section1 = cur_line;
4219 /* ... find section2 ... */
4220 while ((cur_line != NULL) && (line_number < section2))
4222 prev_line = cur_line;
4223 cur_line = cur_line->next;
4227 if ( (cur_line == NULL)
4228 || (cur_line->type != FILE_LINE_ACTION) )
4230 /* Invalid "section2" parameter */
4231 edit_free_file(file);
4232 return JB_ERR_CGI_PARAMS;
4235 /* ... find the end of section2 ... */
4236 line_before_section2 = prev_line;
4237 line_start_section2 = cur_line;
4240 prev_line = cur_line;
4241 cur_line = cur_line->next;
4244 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4245 line_end_section2 = prev_line;
4246 line_after_section2 = cur_line;
4248 /* Now have all the pointers we need. Do the swap. */
4250 /* Change the pointer to section1 to point to section2 instead */
4251 if (line_before_section1 == NULL)
4253 file->lines = line_start_section2;
4257 line_before_section1->next = line_start_section2;
4260 if (line_before_section2 == line_end_section1)
4262 /* Consecutive sections */
4263 line_end_section2->next = line_start_section1;
4267 line_end_section2->next = line_after_section1;
4268 line_before_section2->next = line_start_section1;
4271 /* Set the pointer from the end of section1 to the rest of the file */
4272 line_end_section1->next = line_after_section2;
4274 err = edit_write_file(file);
4277 /* Error writing file */
4278 if (err == JB_ERR_FILE)
4280 /* Read-only file. */
4281 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4283 edit_free_file(file);
4286 } /* END if (section1 != section2) */
4288 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4289 (long) time(NULL), file->identifier);
4291 edit_free_file(file);
4293 return cgi_redirect(rsp, target);
4296 #ifdef FEATURE_TOGGLE
4297 /*********************************************************************
4299 * Function : cgi_toggle
4301 * Description : CGI function that adds a new empty section to
4305 * 1 : csp = Current client state (buffers, headers, etc...)
4306 * 2 : rsp = http_response data structure for output
4307 * 3 : parameters = map of cgi parameters
4310 * set : If present, how to change toggle setting:
4311 * "enable", "disable", "toggle", or none (default).
4312 * mini : If present, use mini reply template.
4314 * Returns : JB_ERR_OK on success
4315 * JB_ERR_MEMORY on out-of-memory
4317 *********************************************************************/
4318 jb_err cgi_toggle(struct client_state *csp,
4319 struct http_response *rsp,
4320 const struct map *parameters)
4322 struct map *exports;
4324 const char *template_name;
4330 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4332 return cgi_error_disabled(csp, rsp);
4335 mode = get_char_param(parameters, "set");
4340 global_toggle_state = 1;
4342 else if (mode == 'D')
4345 global_toggle_state = 0;
4347 else if (mode == 'T')
4350 global_toggle_state = !global_toggle_state;
4353 if (NULL == (exports = default_exports(csp, "toggle")))
4355 return JB_ERR_MEMORY;
4358 template_name = (get_char_param(parameters, "mini")
4362 return template_fill_for_cgi(csp, template_name, exports, rsp);
4364 #endif /* def FEATURE_TOGGLE */
4366 /*********************************************************************
4368 * Function : javascriptify
4370 * Description : Converts a string into a form JavaScript will like.
4372 * Netscape 4's JavaScript sucks - it doesn't use
4373 * "id" parameters, so you have to set the "name"
4374 * used to submit a form element to something JavaScript
4375 * will like. (Or access the elements by index in an
4376 * array. That array contains >60 elements and will
4377 * be changed whenever we add a new action to the
4378 * editor, so I'm NOT going to use indexes that have
4379 * to be figured out by hand.)
4381 * Currently the only thing we have to worry about
4382 * is "-" ==> "_" conversion.
4384 * This is a length-preserving operation so it is
4385 * carried out in-place, no memory is allocated
4389 * 1 : identifier = String to make JavaScript-friendly.
4393 *********************************************************************/
4394 static void javascriptify(char * identifier)
4396 char * p = identifier;
4397 while (NULL != (p = strchr(p, '-')))
4404 /*********************************************************************
4406 * Function : actions_to_radio
4408 * Description : Converts a actionsfile entry into settings for
4409 * radio buttons and edit boxes on a HTML form.
4412 * 1 : exports = List of substitutions to add to.
4413 * 2 : action = Action to read
4415 * Returns : JB_ERR_OK on success
4416 * JB_ERR_MEMORY on out-of-memory
4418 *********************************************************************/
4419 static jb_err actions_to_radio(struct map * exports,
4420 const struct action_spec *action)
4422 unsigned mask = action->mask;
4423 unsigned add = action->add;
4431 mask = action->mask;
4434 /* sanity - prevents "-feature +feature" */
4438 #define DEFINE_ACTION_BOOL(name, bit) \
4439 if (!(mask & bit)) \
4441 current_mode = 'n'; \
4443 else if (add & bit) \
4445 current_mode = 'y'; \
4449 current_mode = 'x'; \
4451 if (map_radio(exports, name, "ynx", current_mode)) \
4453 return JB_ERR_MEMORY; \
4456 #define DEFINE_ACTION_STRING(name, bit, index) \
4457 DEFINE_ACTION_BOOL(name, bit); \
4460 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4463 checked = !strcmp(action->string[index], value); \
4467 checked = is_default; \
4469 mapped_param |= checked; \
4470 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4472 return JB_ERR_MEMORY; \
4475 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4476 if (map(exports, name "-param-custom", 1, \
4477 ((!mapped_param) ? "checked" : ""), 1)) \
4479 return JB_ERR_MEMORY; \
4481 if (map(exports, name "-param", 1, \
4482 (((add & bit) && !mapped_param) ? \
4483 action->string[index] : default_val), 1)) \
4485 return JB_ERR_MEMORY; \
4488 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4489 if (map(exports, name "-param", 1, \
4490 ((add & bit) ? action->string[index] : default_val), 1)) \
4492 return JB_ERR_MEMORY; \
4495 #define DEFINE_ACTION_MULTI(name, index) \
4496 if (action->multi_add[index]->first) \
4498 current_mode = 'y'; \
4500 else if (action->multi_remove_all[index]) \
4502 current_mode = 'n'; \
4504 else if (action->multi_remove[index]->first) \
4506 current_mode = 'y'; \
4510 current_mode = 'x'; \
4512 if (map_radio(exports, name, "ynx", current_mode)) \
4514 return JB_ERR_MEMORY; \
4517 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4519 #include "actionlist.h"
4521 #undef DEFINE_ACTION_MULTI
4522 #undef DEFINE_ACTION_STRING
4523 #undef DEFINE_ACTION_BOOL
4524 #undef DEFINE_ACTION_ALIAS
4525 #undef DEFINE_CGI_PARAM_CUSTOM
4526 #undef DEFINE_CGI_PARAM_RADIO
4527 #undef DEFINE_CGI_PARAM_NO_RADIO
4533 /*********************************************************************
4535 * Function : actions_from_radio
4537 * Description : Converts a map of parameters passed to a CGI function
4538 * into an actionsfile entry.
4541 * 1 : parameters = parameters to the CGI call
4542 * 2 : action = Action to change. Must be valid before
4543 * the call, actions not specified will be
4546 * Returns : JB_ERR_OK on success
4547 * JB_ERR_MEMORY on out-of-memory
4549 *********************************************************************/
4550 static jb_err actions_from_radio(const struct map * parameters,
4551 struct action_spec *action)
4556 const char * js_name;
4557 jb_err err = JB_ERR_OK;
4562 /* Statics are generally a potential race condition,
4563 * but in this case we're safe and don't need semaphores.
4564 * Be careful if you modify this function.
4566 * The js_name_arr's are never free()d, but this is no
4567 * problem, since they will only be created once and
4568 * used by all threads thereafter. -oes
4571 #define JAVASCRIPTIFY(dest_var, string) \
4573 static int first_time = 1; \
4574 static char *js_name_arr; \
4577 js_name_arr = strdup(string); \
4578 javascriptify(js_name_arr); \
4580 dest_var = js_name_arr; \
4584 #define DEFINE_ACTION_BOOL(name, bit) \
4585 JAVASCRIPTIFY(js_name, name); \
4586 ch = get_char_param(parameters, js_name); \
4589 action->add |= bit; \
4590 action->mask |= bit; \
4592 else if (ch == 'N') \
4594 action->add &= ~bit; \
4595 action->mask &= ~bit; \
4597 else if (ch == 'X') \
4599 action->add &= ~bit; \
4600 action->mask |= bit; \
4603 #define DEFINE_ACTION_STRING(name, bit, index) \
4604 JAVASCRIPTIFY(js_name, name); \
4605 ch = get_char_param(parameters, js_name); \
4609 JAVASCRIPTIFY(js_name, name "-mode"); \
4610 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4611 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4613 JAVASCRIPTIFY(js_name, name "-param"); \
4614 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4616 if (param != NULL) \
4618 if (NULL == (param_dup = strdup(param))) \
4620 return JB_ERR_MEMORY; \
4622 freez(action->string[index]); \
4623 action->add |= bit; \
4624 action->mask |= bit; \
4625 action->string[index] = param_dup; \
4628 else if (ch == 'N') \
4630 if (action->add & bit) \
4632 freez(action->string[index]); \
4634 action->add &= ~bit; \
4635 action->mask &= ~bit; \
4637 else if (ch == 'X') \
4639 if (action->add & bit) \
4641 freez(action->string[index]); \
4643 action->add &= ~bit; \
4644 action->mask |= bit; \
4647 #define DEFINE_ACTION_MULTI(name, index) \
4648 JAVASCRIPTIFY(js_name, name); \
4649 ch = get_char_param(parameters, js_name); \
4654 else if (ch == 'N') \
4656 list_remove_all(action->multi_add[index]); \
4657 list_remove_all(action->multi_remove[index]); \
4658 action->multi_remove_all[index] = 1; \
4660 else if (ch == 'X') \
4662 list_remove_all(action->multi_add[index]); \
4663 list_remove_all(action->multi_remove[index]); \
4664 action->multi_remove_all[index] = 0; \
4667 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4669 #include "actionlist.h"
4671 #undef DEFINE_ACTION_MULTI
4672 #undef DEFINE_ACTION_STRING
4673 #undef DEFINE_ACTION_BOOL
4674 #undef DEFINE_ACTION_ALIAS
4675 #undef JAVASCRIPTIFY
4681 #endif /* def FEATURE_CGI_EDIT_ACTIONS */