1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.44 2006/12/09 13:49:16 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 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.44 2006/12/09 13:49:16 fabiankeil
46 * Fix configure option --disable-toggle.
47 * Thanks to Peter Thoenen for reporting this.
49 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
50 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
51 * with what was really the latest development (the v_3_0_branch branch)
53 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
54 * Remove a little residual debugging info
56 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
57 * Multiple filter file support
59 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
60 * Undo some damaging memory leak patches
62 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
63 * Removing a double free
65 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
66 * A few memory leaks plugged (mostly on error paths)
68 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
69 * Moved cgi_error_disabled() from cgiedit.c to
70 * cgi.c to re-enable build with --disable-editor.
71 * Fixes Bug #892744. Thanks to Matthew Fischer
74 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
75 * One line lost in last commit
77 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
78 * - All edit functions that redirect back to the list page
79 * now use cgi_redirect
80 * - All redirects now contain useless parameter "foo", whose
81 * value are raw seconds since epoch, in order to force
82 * Opera and Konqueror to properly reload the list. Closes
85 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
86 * Cosmetic: Renamed cryptic variable
88 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
89 * Fix: Don't free uninitialized struct editable_file
91 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
92 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
94 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
95 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
97 * Revision 1.41 2002/05/21 19:09:45 oes
98 * - Made Add/Edit/Remove URL Submit and Cancel
99 * buttons jump back to relevant section in eal
100 * - Bugfix: remove-url-form needs p export
102 * Revision 1.40 2002/05/19 11:34:35 jongfoster
103 * Handling read-only actions files better - report the actual
104 * error, not "Out of memory"!
107 * http://sourceforge.net/tracker/index.php?func=detail
108 * &aid=557905&group_id=11118&atid=111118
110 * Revision 1.39 2002/05/12 21:39:15 jongfoster
111 * - Adding Doxygen-style comments to structures and #defines.
112 * - Correcting function comments
114 * Revision 1.38 2002/05/03 23:00:38 jongfoster
115 * Support for templates for "standard actions" buttons.
118 * Revision 1.37 2002/04/30 11:14:52 oes
119 * Made csp the first parameter in *action_to_html
121 * Revision 1.36 2002/04/26 21:53:30 jongfoster
122 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
124 * Revision 1.35 2002/04/26 21:50:02 jongfoster
125 * Honouring default exports in edit-actions-for-url-filter template.
127 * Revision 1.34 2002/04/26 12:54:17 oes
128 * Adaptions to changes in actions.c
130 * Revision 1.33 2002/04/24 02:17:47 oes
131 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
133 * - Activated Jon's code for editing multiple AFs
134 * - cgi_edit_list_actions now provides context-sensitive
135 * help, looks up all action sets from standard.action and
136 * makes buttons for them in the catchall section
137 * - cgi_edit_action_submit now honors a p parameter, looks up
138 * the corresponding action set, and sets the catchall pattern's
139 * actions accordingly.
141 * Revision 1.32 2002/04/19 16:55:31 jongfoster
142 * Fixing newline problems. If we do our own text file newline
143 * mangling, we don't want the library to do any, so we need to
144 * open the files in *binary* mode.
146 * Revision 1.31 2002/04/18 19:21:08 jongfoster
147 * Added code to detect "conventional" action files, that start
148 * with a set of actions for all URLs (the pattern "/").
149 * These are special-cased in the "edit-actions-list" CGI, so
150 * that a special UI can be written for them.
152 * Revision 1.30 2002/04/10 13:38:35 oes
153 * load_template signature changed
155 * Revision 1.29 2002/04/08 16:59:08 oes
158 * Revision 1.28 2002/03/27 12:30:29 oes
159 * Deleted unsused variable
161 * Revision 1.27 2002/03/26 23:06:04 jongfoster
162 * Removing duplicate @ifs on the toggle page
164 * Revision 1.26 2002/03/26 22:59:17 jongfoster
165 * Fixing /toggle to display status consistently.
167 * Revision 1.25 2002/03/26 22:29:54 swa
168 * we have a new homepage!
170 * Revision 1.24 2002/03/24 15:23:33 jongfoster
173 * Revision 1.23 2002/03/24 13:32:41 swa
174 * name change related issues
176 * Revision 1.22 2002/03/24 13:25:43 swa
177 * name change related issues
179 * Revision 1.21 2002/03/22 18:02:48 jongfoster
180 * Fixing remote toggle
182 * Revision 1.20 2002/03/16 20:28:34 oes
183 * Added descriptions to the filters so users will know what they select in the cgi editor
185 * Revision 1.19 2002/03/16 18:38:14 jongfoster
186 * Stopping stupid or malicious users from breaking the actions
187 * file using the web-based editor.
189 * Revision 1.18 2002/03/16 14:57:44 jongfoster
190 * Full support for enabling/disabling modular filters.
192 * Revision 1.17 2002/03/16 14:26:42 jongfoster
193 * First version of modular filters support - READ ONLY!
194 * Fixing a double-free bug in the out-of-memory handling in map_radio().
196 * Revision 1.16 2002/03/07 03:46:17 oes
197 * Fixed compiler warnings
199 * Revision 1.15 2002/03/06 22:54:35 jongfoster
200 * Automated function-comment nitpicking.
202 * Revision 1.14 2002/03/05 00:24:51 jongfoster
203 * Patch to always edit the current actions file.
205 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
206 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
208 * Revision 1.12 2002/03/03 09:18:03 joergs
209 * Made jumbjuster work on AmigaOS again.
211 * Revision 1.11 2002/01/23 01:03:31 jongfoster
212 * Fixing gcc [CygWin] compiler warnings
214 * Revision 1.10 2002/01/23 00:22:59 jongfoster
215 * Adding new function cgi_edit_actions_section_swap(), to reorder
218 * Adding get_url_spec_param() to get a validated URL pattern.
220 * Moving edit_read_line() out of this file and into loaders.c.
222 * Adding missing html_encode() to many CGI functions.
224 * Moving the functions that #include actionlist.h to the end of the file,
225 * because the Visual C++ 97 debugger gets extremely confused if you try
226 * to debug any code that comes after them in the file.
228 * Major optimizations in cgi_edit_actions_list() to reduce the size of
229 * the generated HTML (down 40% from 550k to 304k), with major side-effects
230 * throughout the editor and templates. In particular, the length of the
231 * URLs throughout the editor has been drastically reduced, by cutting
232 * paramater names down to 1 character and CGI names down to 3-4
233 * characters, by removing all non-essential CGI paramaters even at the
234 * expense of having to re-read the actions file for the most trivial
235 * page, and by using relative rather than absolute URLs. This means
236 * that this (typical example):
238 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
239 * filename=ijb&ver=1011487572&section=12&pattern=13
240 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
244 * <a href="eau?f=ijb&v=1011487572&p=13">
246 * Revision 1.9 2002/01/17 20:56:22 jongfoster
247 * Replacing hard references to the URL of the config interface
248 * with #defines from project.h
250 * Revision 1.8 2001/11/30 23:35:51 jongfoster
251 * Renaming actionsfile to ijb.action
253 * Revision 1.7 2001/11/13 00:28:24 jongfoster
254 * - Renaming parameters from edit-actions-for-url so that they only
255 * contain legal JavaScript characters. If we wanted to write
256 * JavaScript that worked with Netscape 4, this is nessacery.
257 * (Note that at the moment the JavaScript doesn't actually work
258 * with Netscape 4, but now this is purely a template issue, not
259 * one affecting code).
260 * - Adding new CGIs for use by non-JavaScript browsers:
261 * edit-actions-url-form
262 * edit-actions-add-url-form
263 * edit-actions-remove-url-form
266 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
267 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
268 * by and __OS2__ ifdef.
270 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
271 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
272 * threads to call select() simultaneously. So, it's time to do a real, live,
273 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
274 * (native). Both versions will work, but using __OS2__ offers multi-threading.
276 * Revision 1.4 2001/10/23 21:48:19 jongfoster
277 * Cleaning up error handling in CGI functions - they now send back
278 * a HTML error page and should never cause a FATAL error. (Fixes one
279 * potential source of "denial of service" attacks).
281 * CGI actions file editor that works and is actually useful.
283 * Ability to toggle JunkBuster remotely using a CGI call.
285 * You can turn off both the above features in the main configuration
286 * file, e.g. if you are running a multi-user proxy.
288 * Revision 1.3 2001/10/14 22:12:49 jongfoster
289 * New version of CGI-based actionsfile editor.
290 * Major changes, including:
291 * - Completely new file parser and file output routines
292 * - edit-actions CGI renamed edit-actions-for-url
293 * - All CGIs now need a filename parameter, except for...
294 * - New CGI edit-actions which doesn't need a filename,
295 * to allow you to start the editor up.
296 * - edit-actions-submit now works, and now automatically
297 * redirects you back to the main edit-actions-list handler.
299 * Revision 1.2 2001/09/16 17:05:14 jongfoster
300 * Removing unused #include showarg.h
302 * Revision 1.1 2001/09/16 15:47:37 jongfoster
303 * First version of CGI-based edit interface. This is very much a
304 * work-in-progress, and you can't actually use it to edit anything
305 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
306 * to have any effect.
309 **********************************************************************/
315 * FIXME: Following includes copied from cgi.c - which are actually needed?
320 #include <sys/types.h>
324 #include <sys/stat.h>
327 #define snprintf _snprintf
328 #endif /* def _WIN32 */
333 #include "cgisimple.h"
337 #include "miscutil.h"
340 #ifdef FEATURE_TOGGLE
341 /* loadcfg.h is for global_toggle_state only */
343 #endif /* def FEATURE_TOGGLE */
344 #include "urlmatch.h"
346 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
349 #ifdef FEATURE_CGI_EDIT_ACTIONS
352 * A line in an editable_file.
356 /** Next entry in the linked list */
357 struct file_line * next;
359 /** The raw data, to write out if this line is unmodified. */
362 /** Comments and/or whitespace to put before this line if it's modified
363 and then written out. */
366 /** The actual data, as a string. Line continuation and comment removal
367 are performed on the data read from file before it's stored here, so
368 it will be a single line of data. */
371 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
374 /** The actual data, processed into some sensible data type. */
378 /** An action specification. */
379 struct action_spec action[1];
381 /** A name=value pair. */
385 /** The name in the name=value pair. */
388 /** The value in the name=value pair, as a string. */
391 /** The value in the name=value pair, as an integer. */
396 /* Add more data types here... e.g.
399 struct url_spec url[1];
403 struct action_spec action[1];
413 /** This file_line has not been processed yet. */
414 #define FILE_LINE_UNPROCESSED 1
416 /** This file_line is blank. Can only appear at the end of a file, due to
417 the way the parser works. */
418 #define FILE_LINE_BLANK 2
420 /** This file_line says {{alias}}. */
421 #define FILE_LINE_ALIAS_HEADER 3
423 /** This file_line defines an alias. */
424 #define FILE_LINE_ALIAS_ENTRY 4
426 /** This file_line defines an {action}. */
427 #define FILE_LINE_ACTION 5
429 /** This file_line specifies a URL pattern. */
430 #define FILE_LINE_URL 6
432 /** This file_line says {{settings}}. */
433 #define FILE_LINE_SETTINGS_HEADER 7
435 /** This file_line is in a {{settings}} block. */
436 #define FILE_LINE_SETTINGS_ENTRY 8
438 /** This file_line says {{description}}. */
439 #define FILE_LINE_DESCRIPTION_HEADER 9
441 /** This file_line is in a {{description}} block. */
442 #define FILE_LINE_DESCRIPTION_ENTRY 10
446 * A configuration file, in a format that can be edited and written back to
451 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
452 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
453 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
454 /**< Pre-encoded with url_encode() for ease of use. */
455 const char * version_str; /**< Last modification time, as a string. For CGI param. */
456 /**< Can be used in URL without using url_param(). */
457 unsigned version; /**< Last modification time - prevents chaos with
458 the browser's "back" button. Note that this is a
459 time_t cast to an unsigned. When comparing, always
460 cast the time_t to an unsigned, and *NOT* vice-versa.
461 This may lose the top few bits, but they're not
462 significant anyway. */
463 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
464 Note that changing this after the file has been
465 read in will cause a mess. */
466 struct file_line * parse_error; /**< On parse error, this is the offending line. */
467 const char * parse_error_text; /**< On parse error, this is the problem.
468 (Statically allocated) */
471 /* FIXME: Following non-static functions should be prototyped in .h or made static */
473 /* Functions to read and write arbitrary config files */
474 jb_err edit_read_file(struct client_state *csp,
475 const struct map *parameters,
478 struct editable_file **pfile);
479 jb_err edit_write_file(struct editable_file * file);
480 void edit_free_file(struct editable_file * file);
482 /* Functions to read and write actions files */
483 jb_err edit_parse_actions_file(struct editable_file * file);
484 jb_err edit_read_actions_file(struct client_state *csp,
485 struct http_response *rsp,
486 const struct map *parameters,
488 struct editable_file **pfile);
491 jb_err cgi_error_modified(struct client_state *csp,
492 struct http_response *rsp,
493 const char *filename);
494 jb_err cgi_error_parse(struct client_state *csp,
495 struct http_response *rsp,
496 struct editable_file *file);
497 jb_err cgi_error_file(struct client_state *csp,
498 struct http_response *rsp,
499 const char *filename);
500 jb_err cgi_error_file_read_only(struct client_state *csp,
501 struct http_response *rsp,
502 const char *filename);
504 /* Internal arbitrary config file support functions */
505 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
506 static void edit_free_file_lines(struct file_line * first_line);
508 /* Internal actions file support functions */
509 static int match_actions_file_header_line(const char * line, const char * name);
510 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
512 /* Internal parameter parsing functions */
513 static jb_err get_file_name_param(struct client_state *csp,
514 const struct map *parameters,
515 const char *param_name,
518 const char **pparam);
520 static jb_err get_url_spec_param(struct client_state *csp,
521 const struct map *parameters,
526 /* Internal actionsfile <==> HTML conversion functions */
527 static jb_err map_radio(struct map * exports,
528 const char * optionname,
531 static jb_err actions_to_radio(struct map * exports,
532 const struct action_spec *action);
533 static jb_err actions_from_radio(const struct map * parameters,
534 struct action_spec *action);
537 static jb_err map_copy_parameter_html(struct map *out,
538 const struct map *in,
540 #if 0 /* unused function */
541 static jb_err map_copy_parameter_url(struct map *out,
542 const struct map *in,
544 #endif /* unused function */
546 /* Internal convenience functions */
547 static char *section_target(const unsigned sectionid);
549 /*********************************************************************
551 * Function : section_target
553 * Description : Given an unsigned (section id) n, produce a dynamically
554 * allocated string of the form #l<n>, for use in link
558 * 1 : sectionid = start line number of section
560 * Returns : String with link target, or NULL if out of
563 *********************************************************************/
564 static char *section_target(const unsigned sectionid)
568 snprintf(buf, 30, "#l%d", sectionid);
574 /*********************************************************************
576 * Function : map_copy_parameter_html
578 * Description : Copy a CGI parameter from one map to another, HTML
582 * 1 : out = target map
583 * 2 : in = source map
584 * 3 : name = name of cgi parameter to copy
586 * Returns : JB_ERR_OK on success
587 * JB_ERR_MEMORY on out-of-memory
588 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
591 *********************************************************************/
592 static jb_err map_copy_parameter_html(struct map *out,
593 const struct map *in,
603 value = lookup(in, name);
604 err = map(out, name, 1, html_encode(value), 0);
611 else if (*value == '\0')
613 return JB_ERR_CGI_PARAMS;
622 #if 0 /* unused function */
623 /*********************************************************************
625 * Function : map_copy_parameter_url
627 * Description : Copy a CGI parameter from one map to another, URL
631 * 1 : out = target map
632 * 2 : in = source map
633 * 3 : name = name of cgi parameter to copy
635 * Returns : JB_ERR_OK on success
636 * JB_ERR_MEMORY on out-of-memory
637 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
640 *********************************************************************/
641 static jb_err map_copy_parameter_url(struct map *out,
642 const struct map *in,
652 value = lookup(in, name);
653 err = map(out, name, 1, url_encode(value), 0);
660 else if (*value == '\0')
662 return JB_ERR_CGI_PARAMS;
669 #endif /* 0 - unused function */
671 /*********************************************************************
673 * Function : cgi_edit_actions_url_form
675 * Description : CGI function that displays a form for
679 * 1 : csp = Current client state (buffers, headers, etc...)
680 * 2 : rsp = http_response data structure for output
681 * 3 : parameters = map of cgi parameters
684 * f : (filename) Identifies the file to edit
685 * v : (version) File's last-modified time
686 * p : (pattern) Line number of pattern to edit
688 * Returns : JB_ERR_OK on success
689 * JB_ERR_MEMORY on out-of-memory
690 * JB_ERR_CGI_PARAMS if the CGI parameters are not
691 * specified or not valid.
693 *********************************************************************/
694 jb_err cgi_edit_actions_url_form(struct client_state *csp,
695 struct http_response *rsp,
696 const struct map *parameters)
698 struct map * exports;
700 struct editable_file * file;
701 struct file_line * cur_line;
702 unsigned line_number;
703 unsigned section_start_line_number = 0;
710 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
712 return cgi_error_disabled(csp, rsp);
715 err = get_number_param(csp, parameters, "p", &patternid);
721 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
724 /* No filename specified, can't read file, modified, or out of memory. */
725 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
728 cur_line = file->lines;
730 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
732 if (cur_line->type == FILE_LINE_ACTION)
734 section_start_line_number = line_number;
736 cur_line = cur_line->next;
739 if ( (cur_line == NULL)
740 || (line_number != patternid)
742 || (cur_line->type != FILE_LINE_URL))
744 /* Invalid "patternid" parameter */
745 edit_free_file(file);
746 return JB_ERR_CGI_PARAMS;
749 if (NULL == (exports = default_exports(csp, NULL)))
751 edit_free_file(file);
752 return JB_ERR_MEMORY;
755 err = map(exports, "f", 1, file->identifier, 1);
756 if (!err) err = map(exports, "v", 1, file->version_str, 1);
757 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
758 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
759 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
761 edit_free_file(file);
769 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
773 /*********************************************************************
775 * Function : cgi_edit_actions_add_url_form
777 * Description : CGI function that displays a form for
781 * 1 : csp = Current client state (buffers, headers, etc...)
782 * 2 : rsp = http_response data structure for output
783 * 3 : parameters = map of cgi parameters
786 * f : (filename) Identifies the file to edit
787 * v : (version) File's last-modified time
788 * s : (section) Line number of section to edit
790 * Returns : JB_ERR_OK on success
791 * JB_ERR_MEMORY on out-of-memory
792 * JB_ERR_CGI_PARAMS if the CGI parameters are not
793 * specified or not valid.
795 *********************************************************************/
796 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
797 struct http_response *rsp,
798 const struct map *parameters)
807 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
809 return cgi_error_disabled(csp, rsp);
812 if (NULL == (exports = default_exports(csp, NULL)))
814 return JB_ERR_MEMORY;
817 err = map_copy_parameter_html(exports, parameters, "f");
818 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
819 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
827 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
831 /*********************************************************************
833 * Function : cgi_edit_actions_remove_url_form
835 * Description : CGI function that displays a form for
839 * 1 : csp = Current client state (buffers, headers, etc...)
840 * 2 : rsp = http_response data structure for output
841 * 3 : parameters = map of cgi parameters
844 * f : (filename) Identifies the file to edit
845 * v : (version) File's last-modified time
846 * p : (pattern) Line number of pattern to edit
848 * Returns : JB_ERR_OK on success
849 * JB_ERR_MEMORY on out-of-memory
850 * JB_ERR_CGI_PARAMS if the CGI parameters are not
851 * specified or not valid.
853 *********************************************************************/
854 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
855 struct http_response *rsp,
856 const struct map *parameters)
858 struct map * exports;
860 struct editable_file * file;
861 struct file_line * cur_line;
862 unsigned line_number;
863 unsigned section_start_line_number = 0;
870 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
872 return cgi_error_disabled(csp, rsp);
875 err = get_number_param(csp, parameters, "p", &patternid);
881 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
884 /* No filename specified, can't read file, modified, or out of memory. */
885 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
888 cur_line = file->lines;
890 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
892 if (cur_line->type == FILE_LINE_ACTION)
894 section_start_line_number = line_number;
896 cur_line = cur_line->next;
899 if ( (cur_line == NULL)
900 || (line_number != patternid)
902 || (cur_line->type != FILE_LINE_URL))
904 /* Invalid "patternid" parameter */
905 edit_free_file(file);
906 return JB_ERR_CGI_PARAMS;
909 if (NULL == (exports = default_exports(csp, NULL)))
911 edit_free_file(file);
912 return JB_ERR_MEMORY;
915 err = map(exports, "f", 1, file->identifier, 1);
916 if (!err) err = map(exports, "v", 1, file->version_str, 1);
917 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
918 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
919 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
921 edit_free_file(file);
929 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
933 /*********************************************************************
935 * Function : edit_write_file
937 * Description : Write a complete file to disk.
940 * 1 : file = File to write.
942 * Returns : JB_ERR_OK on success
943 * JB_ERR_FILE on error writing to file.
944 * JB_ERR_MEMORY on out of memory
946 *********************************************************************/
947 jb_err edit_write_file(struct editable_file * file)
950 struct file_line * cur_line;
951 struct stat statbuf[1];
952 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
953 digits in time_t, assuming this is a 64-bit
954 machine, plus null terminator, plus one
958 assert(file->filename);
960 if (NULL == (fp = fopen(file->filename, "wb")))
965 cur_line = file->lines;
966 while (cur_line != NULL)
970 if (fputs(cur_line->raw, fp) < 0)
978 if (cur_line->prefix)
980 if (fputs(cur_line->prefix, fp) < 0)
986 if (cur_line->unprocessed)
989 if (NULL != strchr(cur_line->unprocessed, '#'))
991 /* Must quote '#' characters */
998 /* Count number of # characters, so we know length of output string */
999 src = cur_line->unprocessed;
1000 while (NULL != (src = strchr(src, '#')))
1005 assert(numhash > 0);
1007 /* Allocate new memory for string */
1008 len = strlen(cur_line->unprocessed);
1009 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
1011 /* Uh oh, just trashed file! */
1013 return JB_ERR_MEMORY;
1016 /* Loop through string from end */
1017 src = cur_line->unprocessed + len;
1018 dest = str + len + numhash;
1019 for ( ; len >= 0; len--)
1021 if ((*dest-- = *src--) == '#')
1025 assert(numhash >= 0);
1028 assert(numhash == 0);
1029 assert(src + 1 == cur_line->unprocessed);
1030 assert(dest + 1 == str);
1032 if (fputs(str, fp) < 0)
1043 /* Can write without quoting '#' characters. */
1044 if (fputs(cur_line->unprocessed, fp) < 0)
1050 if (fputs(NEWLINE(file->newline), fp) < 0)
1058 /* FIXME: Write data from file->data->whatever */
1062 cur_line = cur_line->next;
1068 /* Update the version stamp in the file structure, since we just
1069 * wrote to the file & changed it's date.
1071 if (stat(file->filename, statbuf) < 0)
1073 /* Error, probably file not found. */
1076 file->version = (unsigned)statbuf->st_mtime;
1078 /* Correct file->version_str */
1079 freez(file->version_str);
1080 snprintf(version_buf, 22, "%u", file->version);
1081 version_buf[21] = '\0';
1082 file->version_str = strdup(version_buf);
1083 if (version_buf == NULL)
1085 return JB_ERR_MEMORY;
1092 /*********************************************************************
1094 * Function : edit_free_file
1096 * Description : Free a complete file in memory.
1099 * 1 : file = Data structure to free.
1103 *********************************************************************/
1104 void edit_free_file(struct editable_file * file)
1108 /* Silently ignore NULL pointer */
1112 edit_free_file_lines(file->lines);
1113 freez(file->filename);
1114 freez(file->identifier);
1115 freez(file->version_str);
1117 file->parse_error_text = NULL; /* Statically allocated */
1118 file->parse_error = NULL;
1124 /*********************************************************************
1126 * Function : edit_free_file_lines
1128 * Description : Free an entire linked list of file lines.
1131 * 1 : first_line = Data structure to free.
1135 *********************************************************************/
1136 static void edit_free_file_lines(struct file_line * first_line)
1138 struct file_line * next_line;
1140 while (first_line != NULL)
1142 next_line = first_line->next;
1143 first_line->next = NULL;
1144 freez(first_line->raw);
1145 freez(first_line->prefix);
1146 freez(first_line->unprocessed);
1147 switch(first_line->type)
1149 case 0: /* special case if memory zeroed */
1150 case FILE_LINE_UNPROCESSED:
1151 case FILE_LINE_BLANK:
1152 case FILE_LINE_ALIAS_HEADER:
1153 case FILE_LINE_SETTINGS_HEADER:
1154 case FILE_LINE_DESCRIPTION_HEADER:
1155 case FILE_LINE_DESCRIPTION_ENTRY:
1156 case FILE_LINE_ALIAS_ENTRY:
1158 /* No data is stored for these */
1161 case FILE_LINE_ACTION:
1162 free_action(first_line->data.action);
1165 case FILE_LINE_SETTINGS_ENTRY:
1166 freez(first_line->data.setting.name);
1167 freez(first_line->data.setting.svalue);
1170 /* Should never happen */
1174 first_line->type = 0; /* paranoia */
1176 first_line = next_line;
1181 /*********************************************************************
1183 * Function : match_actions_file_header_line
1185 * Description : Match an actions file {{header}} line
1188 * 1 : line = String from file
1189 * 2 : name = Header to match against
1191 * Returns : 0 iff they match.
1193 *********************************************************************/
1194 static int match_actions_file_header_line(const char * line, const char * name)
1202 if ((line[0] != '{') || (line[1] != '{'))
1208 /* Look for optional whitespace */
1209 while ( (*line == ' ') || (*line == '\t') )
1214 /* Look for the specified name (case-insensitive) */
1216 if (0 != strncmpic(line, name, len))
1222 /* Look for optional whitespace */
1223 while ( (*line == ' ') || (*line == '\t') )
1228 /* Look for "}}" and end of string*/
1229 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1239 /*********************************************************************
1241 * Function : match_actions_file_header_line
1243 * Description : Match an actions file {{header}} line
1246 * 1 : line = String from file. Must not start with
1247 * whitespace (else infinite loop!)
1248 * 2 : pname = Destination for name
1249 * 2 : pvalue = Destination for value
1251 * Returns : JB_ERR_OK on success
1252 * JB_ERR_MEMORY on out-of-memory
1253 * JB_ERR_PARSE if there's no "=" sign, or if there's
1254 * nothing before the "=" sign (but empty
1255 * values *after* the "=" sign are legal).
1257 *********************************************************************/
1258 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1260 const char * name_end;
1261 const char * value_start;
1267 assert(*line != ' ');
1268 assert(*line != '\t');
1273 value_start = strchr(line, '=');
1274 if ((value_start == NULL) || (value_start == line))
1276 return JB_ERR_PARSE;
1279 name_end = value_start - 1;
1281 /* Eat any whitespace before the '=' */
1282 while ((*name_end == ' ') || (*name_end == '\t'))
1285 * we already know we must have at least 1 non-ws char
1286 * at start of buf - no need to check
1291 name_len = name_end - line + 1; /* Length excluding \0 */
1292 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1294 return JB_ERR_MEMORY;
1296 strncpy(*pname, line, name_len);
1297 (*pname)[name_len] = '\0';
1299 /* Eat any the whitespace after the '=' */
1301 while ((*value_start == ' ') || (*value_start == '\t'))
1306 if (NULL == (*pvalue = strdup(value_start)))
1310 return JB_ERR_MEMORY;
1317 /*********************************************************************
1319 * Function : edit_parse_actions_file
1321 * Description : Parse an actions file in memory.
1323 * Passed linked list must have the "data" member
1324 * zeroed, and must contain valid "next" and
1325 * "unprocessed" fields. The "raw" and "prefix"
1326 * fields are ignored, and "type" is just overwritten.
1328 * Note that on error the file may have been
1332 * 1 : file = Actions file to be parsed in-place.
1334 * Returns : JB_ERR_OK on success
1335 * JB_ERR_MEMORY on out-of-memory
1336 * JB_ERR_PARSE on error
1338 *********************************************************************/
1339 jb_err edit_parse_actions_file(struct editable_file * file)
1341 struct file_line * cur_line;
1343 const char * text; /* Text from a line */
1344 char * name; /* For lines of the form name=value */
1345 char * value; /* For lines of the form name=value */
1346 struct action_alias * alias_list = NULL;
1347 jb_err err = JB_ERR_OK;
1349 /* alias_list contains the aliases defined in this file.
1350 * It might be better to use the "file_line.data" fields
1351 * in the relavent places instead.
1354 cur_line = file->lines;
1356 /* A note about blank line support: Blank lines should only
1357 * ever occur as the last line in the file. This function
1358 * is more forgiving than that - FILE_LINE_BLANK can occur
1362 /* Skip leading blanks. Should only happen if file is
1363 * empty (which is valid, but pointless).
1365 while ( (cur_line != NULL)
1366 && (cur_line->unprocessed[0] == '\0') )
1369 cur_line->type = FILE_LINE_BLANK;
1370 cur_line = cur_line->next;
1373 if ( (cur_line != NULL)
1374 && (cur_line->unprocessed[0] != '{') )
1376 /* File doesn't start with a header */
1377 file->parse_error = cur_line;
1378 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1379 return JB_ERR_PARSE;
1382 if ( (cur_line != NULL) && (0 ==
1383 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1385 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1387 cur_line = cur_line->next;
1388 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1390 if (cur_line->unprocessed[0])
1392 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1394 err = split_line_on_equals(cur_line->unprocessed,
1395 &cur_line->data.setting.name,
1396 &cur_line->data.setting.svalue);
1397 if (err == JB_ERR_MEMORY)
1401 else if (err != JB_ERR_OK)
1403 /* Line does not contain a name=value pair */
1404 file->parse_error = cur_line;
1405 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1406 return JB_ERR_PARSE;
1411 cur_line->type = FILE_LINE_BLANK;
1413 cur_line = cur_line->next;
1417 if ( (cur_line != NULL) && (0 ==
1418 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1420 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1422 cur_line = cur_line->next;
1423 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1425 if (cur_line->unprocessed[0])
1427 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1431 cur_line->type = FILE_LINE_BLANK;
1433 cur_line = cur_line->next;
1437 if ( (cur_line != NULL) && (0 ==
1438 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1440 cur_line->type = FILE_LINE_ALIAS_HEADER;
1442 cur_line = cur_line->next;
1443 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1445 if (cur_line->unprocessed[0])
1447 /* define an alias */
1448 struct action_alias * new_alias;
1450 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1452 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1453 if (err == JB_ERR_MEMORY)
1457 else if (err != JB_ERR_OK)
1459 /* Line does not contain a name=value pair */
1460 file->parse_error = cur_line;
1461 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1462 return JB_ERR_PARSE;
1465 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1470 free_alias_list(alias_list);
1471 return JB_ERR_MEMORY;
1474 err = get_actions(value, alias_list, new_alias->action);
1477 /* Invalid action or out of memory */
1481 free_alias_list(alias_list);
1482 if (err == JB_ERR_MEMORY)
1488 /* Line does not contain a name=value pair */
1489 file->parse_error = cur_line;
1490 file->parse_error_text = "This alias does not specify a valid set of actions.";
1491 return JB_ERR_PARSE;
1497 new_alias->name = name;
1500 new_alias->next = alias_list;
1501 alias_list = new_alias;
1505 cur_line->type = FILE_LINE_BLANK;
1507 cur_line = cur_line->next;
1511 /* Header done, process the main part of the file */
1512 while (cur_line != NULL)
1514 /* At this point, (cur_line->unprocessed[0] == '{') */
1515 assert(cur_line->unprocessed[0] == '{');
1516 text = cur_line->unprocessed + 1;
1517 len = strlen(text) - 1;
1518 if (text[len] != '}')
1520 /* No closing } on header */
1521 free_alias_list(alias_list);
1522 file->parse_error = cur_line;
1523 file->parse_error_text = "Headers starting with '{' must have a "
1524 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1525 "must close with two brackets ('}}').";
1526 return JB_ERR_PARSE;
1531 /* An invalid {{ header. */
1532 free_alias_list(alias_list);
1533 file->parse_error = cur_line;
1534 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1535 "Please remember that the system (two-bracket) headers must "
1536 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1537 "and must appear before any actions (one-bracket) headers. "
1538 "Also note that system headers may not be repeated.";
1539 return JB_ERR_PARSE;
1542 while ( (*text == ' ') || (*text == '\t') )
1548 && ( (text[len - 1] == ' ')
1549 || (text[len - 1] == '\t') ) )
1554 cur_line->type = FILE_LINE_ACTION;
1556 /* Remove {} and make copy */
1557 if (NULL == (value = (char *) malloc(len + 1)))
1560 free_alias_list(alias_list);
1561 return JB_ERR_MEMORY;
1563 strncpy(value, text, len);
1567 err = get_actions(value, alias_list, cur_line->data.action);
1570 /* Invalid action or out of memory */
1572 free_alias_list(alias_list);
1573 if (err == JB_ERR_MEMORY)
1579 /* Line does not contain a name=value pair */
1580 file->parse_error = cur_line;
1581 file->parse_error_text = "This header does not specify a valid set of actions.";
1582 return JB_ERR_PARSE;
1586 /* Done with string - it was clobbered anyway */
1589 /* Process next line */
1590 cur_line = cur_line->next;
1592 /* Loop processing URL patterns */
1593 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1595 if (cur_line->unprocessed[0])
1597 /* Could parse URL here, but this isn't currently needed */
1599 cur_line->type = FILE_LINE_URL;
1603 cur_line->type = FILE_LINE_BLANK;
1605 cur_line = cur_line->next;
1607 } /* End main while(cur_line != NULL) loop */
1609 free_alias_list(alias_list);
1615 /*********************************************************************
1617 * Function : edit_read_file_lines
1619 * Description : Read all the lines of a file into memory.
1620 * Handles whitespace, comments and line continuation.
1623 * 1 : fp = File to read from. On return, this will be
1624 * at EOF but it will not have been closed.
1625 * 2 : pfile = Destination for a linked list of file_lines.
1626 * Will be set to NULL on error.
1627 * 3 : newline = How to handle newlines.
1629 * Returns : JB_ERR_OK on success
1630 * JB_ERR_MEMORY on out-of-memory
1632 *********************************************************************/
1633 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1635 struct file_line * first_line; /* Keep for return value or to free */
1636 struct file_line * cur_line; /* Current line */
1637 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1645 cur_line = first_line = zalloc(sizeof(struct file_line));
1646 if (cur_line == NULL)
1648 return JB_ERR_MEMORY;
1651 cur_line->type = FILE_LINE_UNPROCESSED;
1653 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1656 /* Out of memory or empty file. */
1657 /* Note that empty file is not an error we propogate up */
1659 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1664 prev_line = cur_line;
1665 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1666 if (cur_line == NULL)
1669 edit_free_file_lines(first_line);
1670 return JB_ERR_MEMORY;
1673 cur_line->type = FILE_LINE_UNPROCESSED;
1675 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1676 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1679 edit_free_file_lines(first_line);
1680 return JB_ERR_MEMORY;
1684 while (rval != JB_ERR_FILE);
1688 /* We allocated one too many - free it */
1689 prev_line->next = NULL;
1692 *pfile = first_line;
1697 /*********************************************************************
1699 * Function : edit_read_file
1701 * Description : Read a complete file into memory.
1702 * Handles CGI parameter parsing. If requested, also
1703 * checks the file's modification timestamp.
1706 * 1 : csp = Current client state (buffers, headers, etc...)
1707 * 2 : parameters = map of cgi parameters.
1708 * 3 : require_version = true to check "ver" parameter.
1709 * 4 : suffix = File extension, e.g. ".action".
1710 * 5 : pfile = Destination for the file. Will be set
1714 * filename : The name of the file to read, without the
1715 * path or ".action" extension.
1716 * ver : (Only if require_version is nonzero)
1717 * Timestamp of the actions file. If wrong, this
1718 * function fails with JB_ERR_MODIFIED.
1720 * Returns : JB_ERR_OK on success
1721 * JB_ERR_MEMORY on out-of-memory
1722 * JB_ERR_CGI_PARAMS if "filename" was not specified
1724 * JB_ERR_FILE if the file cannot be opened or
1726 * JB_ERR_MODIFIED if version checking was requested and
1727 * failed - the file was modified outside
1728 * of this CGI editor instance.
1730 *********************************************************************/
1731 jb_err edit_read_file(struct client_state *csp,
1732 const struct map *parameters,
1733 int require_version,
1735 struct editable_file **pfile)
1737 struct file_line * lines;
1741 const char * identifier;
1742 struct editable_file * file;
1743 unsigned version = 0;
1744 struct stat statbuf[1];
1745 char version_buf[22];
1746 int newline = NEWLINE_UNKNOWN;
1754 err = get_file_name_param(csp, parameters, "f", suffix,
1755 &filename, &identifier);
1761 if (stat(filename, statbuf) < 0)
1763 /* Error, probably file not found. */
1767 version = (unsigned) statbuf->st_mtime;
1769 if (require_version)
1771 unsigned specified_version;
1772 err = get_number_param(csp, parameters, "v", &specified_version);
1779 if (version != specified_version)
1781 return JB_ERR_MODIFIED;
1785 if (NULL == (fp = fopen(filename,"rb")))
1791 err = edit_read_file_lines(fp, &lines, &newline);
1801 file = (struct editable_file *) zalloc(sizeof(*file));
1805 edit_free_file_lines(lines);
1809 file->lines = lines;
1810 file->newline = newline;
1811 file->filename = filename;
1812 file->version = version;
1813 file->identifier = url_encode(identifier);
1815 if (file->identifier == NULL)
1817 edit_free_file(file);
1818 return JB_ERR_MEMORY;
1821 /* Correct file->version_str */
1822 freez(file->version_str);
1823 snprintf(version_buf, 22, "%u", file->version);
1824 version_buf[21] = '\0';
1825 file->version_str = strdup(version_buf);
1826 if (version_buf == NULL)
1828 edit_free_file(file);
1829 return JB_ERR_MEMORY;
1837 /*********************************************************************
1839 * Function : edit_read_actions_file
1841 * Description : Read a complete actions file into memory.
1842 * Handles CGI parameter parsing. If requested, also
1843 * checks the file's modification timestamp.
1845 * If this function detects an error in the categories
1846 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1847 * then it handles it by filling in the specified
1848 * response structure and returning JB_ERR_FILE.
1851 * 1 : csp = Current client state (buffers, headers, etc...)
1852 * 2 : rsp = HTTP response. Only filled in on error.
1853 * 2 : parameters = map of cgi parameters.
1854 * 3 : require_version = true to check "ver" parameter.
1855 * 4 : pfile = Destination for the file. Will be set
1859 * filename : The name of the actions file to read, without the
1860 * path or ".action" extension.
1861 * ver : (Only if require_version is nonzero)
1862 * Timestamp of the actions file. If wrong, this
1863 * function fails with JB_ERR_MODIFIED.
1865 * Returns : JB_ERR_OK on success
1866 * JB_ERR_MEMORY on out-of-memory
1867 * JB_ERR_CGI_PARAMS if "filename" was not specified
1869 * JB_ERR_FILE if the file does not contain valid data,
1870 * or if file cannot be opened or
1871 * contains no data, or if version
1872 * checking was requested and failed.
1874 *********************************************************************/
1875 jb_err edit_read_actions_file(struct client_state *csp,
1876 struct http_response *rsp,
1877 const struct map *parameters,
1878 int require_version,
1879 struct editable_file **pfile)
1882 struct editable_file *file;
1890 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1893 /* Try to handle if possible */
1894 if (err == JB_ERR_FILE)
1896 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1898 else if (err == JB_ERR_MODIFIED)
1900 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1902 if (err == JB_ERR_OK)
1905 * Signal to higher-level CGI code that there was a problem but we
1906 * handled it, they should just return JB_ERR_OK.
1913 err = edit_parse_actions_file(file);
1916 if (err == JB_ERR_PARSE)
1918 err = cgi_error_parse(csp, rsp, file);
1919 if (err == JB_ERR_OK)
1922 * Signal to higher-level CGI code that there was a problem but we
1923 * handled it, they should just return JB_ERR_OK.
1928 edit_free_file(file);
1937 /*********************************************************************
1939 * Function : get_file_name_param
1941 * Description : Get the name of the file to edit from the parameters
1942 * passed to a CGI function. This function handles
1943 * security checks such as blocking urls containing
1944 * "/" or ".", prepending the config file directory,
1945 * and adding the specified suffix.
1947 * (This is an essential security check, otherwise
1948 * users may be able to pass "../../../etc/passwd"
1949 * and overwrite the password file [linux], "prn:"
1950 * and print random data [Windows], etc...)
1952 * This function only allows filenames contining the
1953 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1954 * That's probably too restrictive but at least it's
1958 * 1 : csp = Current client state (buffers, headers, etc...)
1959 * 2 : parameters = map of cgi parameters
1960 * 3 : param_name = The name of the parameter to read
1961 * 4 : suffix = File extension, e.g. ".actions"
1962 * 5 : pfilename = destination for full filename. Caller
1963 * free()s. Set to NULL on error.
1964 * 6 : pparam = destination for partial filename,
1965 * suitable for use in another URL. Allocated as part
1966 * of the map "parameters", so don't free it.
1967 * Set to NULL if not specified.
1969 * Returns : JB_ERR_OK on success
1970 * JB_ERR_MEMORY on out-of-memory
1971 * JB_ERR_CGI_PARAMS if "filename" was not specified
1974 *********************************************************************/
1975 static jb_err get_file_name_param(struct client_state *csp,
1976 const struct map *parameters,
1977 const char *param_name,
1980 const char **pparam)
1998 param = lookup(parameters, param_name);
2001 return JB_ERR_CGI_PARAMS;
2006 len = strlen(param);
2007 if (len >= FILENAME_MAX)
2010 return JB_ERR_CGI_PARAMS;
2013 /* Check every character to see if it's legal */
2015 while ((ch = *s++) != '\0')
2017 if ( ((ch < 'A') || (ch > 'Z'))
2018 && ((ch < 'a') || (ch > 'z'))
2019 && ((ch < '0') || (ch > '9'))
2023 /* Probable hack attempt. */
2024 return JB_ERR_CGI_PARAMS;
2028 /* Append extension */
2029 name = malloc(len + strlen(suffix) + 1);
2032 return JB_ERR_MEMORY;
2034 strcpy(name, param);
2035 strcpy(name + len, suffix);
2038 fullpath = make_path(csp->config->confdir, name);
2041 if (fullpath == NULL)
2043 return JB_ERR_MEMORY;
2047 *pfilename = fullpath;
2053 /*********************************************************************
2055 * Function : get_url_spec_param
2057 * Description : Get a URL pattern from the parameters
2058 * passed to a CGI function. Removes leading/trailing
2059 * spaces and validates it.
2062 * 1 : csp = Current client state (buffers, headers, etc...)
2063 * 2 : parameters = map of cgi parameters
2064 * 3 : name = Name of CGI parameter to read
2065 * 4 : pvalue = destination for value. Will be malloc()'d.
2066 * Set to NULL on error.
2068 * Returns : JB_ERR_OK on success
2069 * JB_ERR_MEMORY on out-of-memory
2070 * JB_ERR_CGI_PARAMS if the parameter was not specified
2073 *********************************************************************/
2074 static jb_err get_url_spec_param(struct client_state *csp,
2075 const struct map *parameters,
2079 const char *orig_param;
2082 struct url_spec compiled[1];
2092 orig_param = lookup(parameters, name);
2095 return JB_ERR_CGI_PARAMS;
2098 /* Copy and trim whitespace */
2099 param = strdup(orig_param);
2102 return JB_ERR_MEMORY;
2106 /* Must be non-empty, and can't allow 1st character to be '{' */
2107 if (param[0] == '\0' || param[0] == '{')
2110 return JB_ERR_CGI_PARAMS;
2113 /* Check for embedded newlines */
2114 for (s = param; *s != '\0'; s++)
2116 if ((*s == '\r') || (*s == '\n'))
2119 return JB_ERR_CGI_PARAMS;
2123 /* Check that regex is valid */
2128 return JB_ERR_MEMORY;
2130 err = create_url_spec(compiled, s);
2135 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2137 free_url_spec(compiled);
2139 if (param[strlen(param) - 1] == '\\')
2142 * Must protect trailing '\\' from becoming line continuation character.
2143 * Two methods: 1) If it's a domain only, add a trailing '/'.
2144 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2146 if (strchr(param, '/') == NULL)
2148 err = string_append(¶m, "/");
2152 err = string_append(¶m, "(?:)");
2159 /* Check that the modified regex is valid */
2164 return JB_ERR_MEMORY;
2166 err = create_url_spec(compiled, s);
2171 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2173 free_url_spec(compiled);
2180 /*********************************************************************
2182 * Function : map_radio
2184 * Description : Map a set of radio button values. E.g. if you have
2185 * 3 radio buttons, declare them as:
2186 * <option type="radio" name="xyz" @xyz-a@>
2187 * <option type="radio" name="xyz" @xyz-b@>
2188 * <option type="radio" name="xyz" @xyz-c@>
2189 * Then map one of the @xyz-?@ variables to "checked"
2190 * and all the others to empty by calling:
2191 * map_radio(exports, "xyz", "abc", sel)
2192 * Where 'sel' is 'a', 'b', or 'c'.
2195 * 1 : exports = Exports map to modify.
2196 * 2 : optionname = name for map
2197 * 3 : values = null-terminated list of values;
2198 * 4 : value = Selected value.
2200 * CGI Parameters : None
2202 * Returns : JB_ERR_OK on success
2203 * JB_ERR_MEMORY on out-of-memory
2205 *********************************************************************/
2206 static jb_err map_radio(struct map * exports,
2207 const char * optionname,
2208 const char * values,
2220 len = strlen(optionname);
2221 buf = malloc(len + 3);
2224 return JB_ERR_MEMORY;
2227 strcpy(buf, optionname);
2232 while ((c = *values++) != '\0')
2237 if (map(exports, buf, 1, "", 1))
2239 return JB_ERR_MEMORY;
2245 return map(exports, buf, 0, "checked", 1);
2249 /*********************************************************************
2251 * Function : cgi_error_modified
2253 * Description : CGI function that is called when a file is modified
2254 * outside the CGI editor.
2257 * 1 : csp = Current client state (buffers, headers, etc...)
2258 * 2 : rsp = http_response data structure for output
2259 * 3 : filename = The file that was modified.
2261 * CGI Parameters : none
2263 * Returns : JB_ERR_OK on success
2264 * JB_ERR_MEMORY on out-of-memory error.
2266 *********************************************************************/
2267 jb_err cgi_error_modified(struct client_state *csp,
2268 struct http_response *rsp,
2269 const char *filename)
2271 struct map *exports;
2278 if (NULL == (exports = default_exports(csp, NULL)))
2280 return JB_ERR_MEMORY;
2283 err = map(exports, "f", 1, html_encode(filename), 0);
2290 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2294 /*********************************************************************
2296 * Function : cgi_error_parse
2298 * Description : CGI function that is called when a file cannot
2299 * be parsed by the CGI editor.
2302 * 1 : csp = Current client state (buffers, headers, etc...)
2303 * 2 : rsp = http_response data structure for output
2304 * 3 : file = The file that was modified.
2306 * CGI Parameters : none
2308 * Returns : JB_ERR_OK on success
2309 * JB_ERR_MEMORY on out-of-memory error.
2311 *********************************************************************/
2312 jb_err cgi_error_parse(struct client_state *csp,
2313 struct http_response *rsp,
2314 struct editable_file *file)
2316 struct map *exports;
2318 struct file_line *cur_line;
2324 if (NULL == (exports = default_exports(csp, NULL)))
2326 return JB_ERR_MEMORY;
2329 err = map(exports, "f", 1, file->identifier, 1);
2330 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2332 cur_line = file->parse_error;
2335 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2336 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2344 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2348 /*********************************************************************
2350 * Function : cgi_error_file
2352 * Description : CGI function that is called when a file cannot be
2353 * opened by the CGI editor.
2356 * 1 : csp = Current client state (buffers, headers, etc...)
2357 * 2 : rsp = http_response data structure for output
2358 * 3 : filename = The file that was modified.
2360 * CGI Parameters : none
2362 * Returns : JB_ERR_OK on success
2363 * JB_ERR_MEMORY on out-of-memory error.
2365 *********************************************************************/
2366 jb_err cgi_error_file(struct client_state *csp,
2367 struct http_response *rsp,
2368 const char *filename)
2370 struct map *exports;
2377 if (NULL == (exports = default_exports(csp, NULL)))
2379 return JB_ERR_MEMORY;
2382 err = map(exports, "f", 1, html_encode(filename), 0);
2389 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2393 /*********************************************************************
2395 * Function : cgi_error_file
2397 * Description : CGI function that is called when a file cannot be
2398 * opened for writing by the CGI editor.
2401 * 1 : csp = Current client state (buffers, headers, etc...)
2402 * 2 : rsp = http_response data structure for output
2403 * 3 : filename = The file that we can't write to
2405 * CGI Parameters : none
2407 * Returns : JB_ERR_OK on success
2408 * JB_ERR_MEMORY on out-of-memory error.
2410 *********************************************************************/
2411 jb_err cgi_error_file_read_only(struct client_state *csp,
2412 struct http_response *rsp,
2413 const char *filename)
2415 struct map *exports;
2422 if (NULL == (exports = default_exports(csp, NULL)))
2424 return JB_ERR_MEMORY;
2427 err = map(exports, "f", 1, html_encode(filename), 0);
2434 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2438 /*********************************************************************
2440 * Function : cgi_edit_actions
2442 * Description : CGI function that allows the user to choose which
2443 * actions file to edit.
2446 * 1 : csp = Current client state (buffers, headers, etc...)
2447 * 2 : rsp = http_response data structure for output
2448 * 3 : parameters = map of cgi parameters
2450 * CGI Parameters : None
2452 * Returns : JB_ERR_OK on success
2453 * JB_ERR_MEMORY on out-of-memory error
2455 *********************************************************************/
2456 jb_err cgi_edit_actions(struct client_state *csp,
2457 struct http_response *rsp,
2458 const struct map *parameters)
2461 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2463 return cgi_error_disabled(csp, rsp);
2466 /* FIXME: Incomplete */
2468 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2473 /*********************************************************************
2475 * Function : cgi_edit_actions_list
2477 * Description : CGI function that edits the actions list.
2478 * FIXME: This function shouldn't FATAL ever.
2479 * FIXME: This function doesn't check the retval of map()
2481 * 1 : csp = Current client state (buffers, headers, etc...)
2482 * 2 : rsp = http_response data structure for output
2483 * 3 : parameters = map of cgi parameters
2485 * CGI Parameters : filename
2487 * Returns : JB_ERR_OK on success
2488 * JB_ERR_MEMORY on out-of-memory
2489 * JB_ERR_FILE if the file cannot be opened or
2491 * JB_ERR_CGI_PARAMS if "filename" was not specified
2494 *********************************************************************/
2495 jb_err cgi_edit_actions_list(struct client_state *csp,
2496 struct http_response *rsp,
2497 const struct map *parameters)
2499 char * section_template;
2500 char * url_template;
2505 struct map * exports;
2506 struct map * section_exports;
2507 struct map * url_exports;
2508 struct editable_file * file;
2509 struct file_line * cur_line;
2510 unsigned line_number = 0;
2511 unsigned prev_section_line_number = ((unsigned) (-1));
2513 struct file_list * fl;
2514 struct url_actions * b;
2515 char * buttons = NULL;
2518 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2520 return cgi_error_disabled(csp, rsp);
2523 if (NULL == (exports = default_exports(csp, NULL)))
2525 return JB_ERR_MEMORY;
2528 /* Load actions file */
2529 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2532 /* No filename specified, can't read file, or out of memory. */
2533 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2536 /* Find start of actions in file */
2537 cur_line = file->lines;
2539 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2541 cur_line = cur_line->next;
2546 * Conventional actions files should have a match all block
2548 * cur_line = {...global actions...}
2549 * cur_line->next = /
2550 * cur_line->next->next = {...actions...} or EOF
2552 if ( (cur_line != NULL)
2553 && (cur_line->type == FILE_LINE_ACTION)
2554 && (cur_line->next != NULL)
2555 && (cur_line->next->type == FILE_LINE_URL)
2556 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2557 && ( (cur_line->next->next == NULL)
2558 || (cur_line->next->next->type != FILE_LINE_URL)
2562 * Generate string with buttons to set actions for "/" to
2563 * any predefined set of actions (named standard.*, probably
2564 * residing in standard.action).
2567 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2570 edit_free_file(file);
2572 if (err == JB_ERR_FILE)
2574 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2579 err = template_fill(§ion_template, exports);
2582 edit_free_file(file);
2587 buttons = strdup("");
2588 for (i = 0; i < MAX_AF_FILES; i++)
2590 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2592 for (b = b->next; NULL != b; b = b->next)
2594 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2596 if (err || (NULL == (section_exports = new_map())))
2599 free(section_template);
2600 edit_free_file(file);
2602 return JB_ERR_MEMORY;
2605 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2607 if (err || (NULL == (s = strdup(section_template))))
2609 free_map(section_exports);
2611 free(section_template);
2612 edit_free_file(file);
2614 return JB_ERR_MEMORY;
2617 if (!err) err = template_fill(&s, section_exports);
2618 free_map(section_exports);
2619 if (!err) err = string_join(&buttons, s);
2624 freez(section_template);
2625 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2628 * Conventional actions file, supply extra editing help.
2629 * (e.g. don't allow them to make it an unconventional one).
2631 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2633 snprintf(buf, 150, "%d", line_number);
2634 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2635 snprintf(buf, 150, "%d", line_number + 2);
2636 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2637 if (!err) err = map(exports, "all-urls-actions", 1,
2638 actions_to_html(csp, cur_line->data.action), 0);
2640 /* Skip the 2 lines */
2641 cur_line = cur_line->next->next;
2645 * Note that prev_section_line_number is NOT set here.
2646 * This is deliberate and not a bug. It stops a "Move up"
2647 * option appearing on the next section. Clicking "Move
2648 * up" would make the actions file unconventional, which
2649 * we don't want, so we hide this option.
2655 * Non-standard actions file - does not begin with
2656 * the "All URLs" section.
2658 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2661 /* Set up global exports */
2663 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2664 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2666 /* Discourage private additions to default.action */
2668 if (!err) err = map_conditional(exports, "default-action",
2669 (strcmp("default", lookup(parameters, "f")) == 0));
2672 edit_free_file(file);
2677 /* Should do all global exports above this point */
2679 /* Load templates */
2681 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2684 edit_free_file(file);
2686 if (err == JB_ERR_FILE)
2688 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2693 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2696 free(section_template);
2697 edit_free_file(file);
2699 if (err == JB_ERR_FILE)
2701 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2706 err = template_fill(§ion_template, exports);
2710 edit_free_file(file);
2716 err = template_fill(&url_template, exports);
2719 free(section_template);
2720 edit_free_file(file);
2725 if (NULL == (sections = strdup("")))
2727 free(section_template);
2729 edit_free_file(file);
2731 return JB_ERR_MEMORY;
2734 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2736 if (NULL == (section_exports = new_map()))
2739 free(section_template);
2741 edit_free_file(file);
2743 return JB_ERR_MEMORY;
2746 snprintf(buf, 150, "%d", line_number);
2747 err = map(section_exports, "s", 1, buf, 1);
2748 if (!err) err = map(section_exports, "actions", 1,
2749 actions_to_html(csp, cur_line->data.action), 0);
2752 && (cur_line->next != NULL)
2753 && (cur_line->next->type == FILE_LINE_URL))
2755 /* This section contains at least one URL, don't allow delete */
2756 err = map_block_killer(section_exports, "empty-section");
2760 if (!err) err = map_block_keep(section_exports, "empty-section");
2763 if (prev_section_line_number != ((unsigned)(-1)))
2765 /* Not last section */
2766 snprintf(buf, 150, "%d", prev_section_line_number);
2767 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2768 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2773 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2775 prev_section_line_number = line_number;
2780 free(section_template);
2782 edit_free_file(file);
2784 free_map(section_exports);
2788 /* Should do all section-specific exports above this point */
2790 if (NULL == (urls = strdup("")))
2793 free(section_template);
2795 edit_free_file(file);
2797 free_map(section_exports);
2798 return JB_ERR_MEMORY;
2803 cur_line = cur_line->next;
2806 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2808 if (NULL == (url_exports = new_map()))
2812 free(section_template);
2814 edit_free_file(file);
2816 free_map(section_exports);
2817 return JB_ERR_MEMORY;
2820 snprintf(buf, 150, "%d", line_number);
2821 err = map(url_exports, "p", 1, buf, 1);
2823 snprintf(buf, 150, "%d", url_1_2);
2824 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2826 if (!err) err = map(url_exports, "url-html", 1,
2827 html_encode(cur_line->unprocessed), 0);
2828 if (!err) err = map(url_exports, "url", 1,
2829 url_encode(cur_line->unprocessed), 0);
2835 free(section_template);
2837 edit_free_file(file);
2839 free_map(section_exports);
2840 free_map(url_exports);
2844 if (NULL == (s = strdup(url_template)))
2848 free(section_template);
2850 edit_free_file(file);
2852 free_map(section_exports);
2853 free_map(url_exports);
2854 return JB_ERR_MEMORY;
2857 err = template_fill(&s, section_exports);
2858 if (!err) err = template_fill(&s, url_exports);
2859 if (!err) err = string_append(&urls, s);
2861 free_map(url_exports);
2868 free(section_template);
2870 edit_free_file(file);
2872 free_map(section_exports);
2876 url_1_2 = 3 - url_1_2;
2878 cur_line = cur_line->next;
2882 err = map(section_exports, "urls", 1, urls, 0);
2884 /* Could also do section-specific exports here, but it wouldn't be as fast */
2886 snprintf(buf, 150, "%d", line_number);
2887 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2889 if ( (cur_line != NULL)
2890 && (cur_line->type == FILE_LINE_ACTION))
2892 /* Not last section */
2893 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2898 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2904 free(section_template);
2906 edit_free_file(file);
2908 free_map(section_exports);
2912 if (NULL == (s = strdup(section_template)))
2915 free(section_template);
2917 edit_free_file(file);
2919 free_map(section_exports);
2920 return JB_ERR_MEMORY;
2923 err = template_fill(&s, section_exports);
2924 if (!err) err = string_append(§ions, s);
2927 free_map(section_exports);
2932 free(section_template);
2934 edit_free_file(file);
2940 edit_free_file(file);
2941 free(section_template);
2944 err = map(exports, "sections", 1, sections, 0);
2951 /* Could also do global exports here, but it wouldn't be as fast */
2953 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2957 /*********************************************************************
2959 * Function : cgi_edit_actions_for_url
2961 * Description : CGI function that edits the Actions list.
2964 * 1 : csp = Current client state (buffers, headers, etc...)
2965 * 2 : rsp = http_response data structure for output
2966 * 3 : parameters = map of cgi parameters
2968 * CGI Parameters : None
2970 * Returns : JB_ERR_OK on success
2971 * JB_ERR_MEMORY on out-of-memory
2972 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2973 * specified or not valid.
2975 *********************************************************************/
2976 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2977 struct http_response *rsp,
2978 const struct map *parameters)
2980 struct map * exports;
2982 struct editable_file * file;
2983 struct file_line * cur_line;
2984 unsigned line_number;
2986 struct re_filterfile_spec *filter_group;
2987 int i, have_filters = 0;
2989 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2991 return cgi_error_disabled(csp, rsp);
2994 err = get_number_param(csp, parameters, "s", §ionid);
3000 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3003 /* No filename specified, can't read file, modified, or out of memory. */
3004 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3007 cur_line = file->lines;
3009 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3011 cur_line = cur_line->next;
3014 if ( (cur_line == NULL)
3015 || (line_number != sectionid)
3017 || (cur_line->type != FILE_LINE_ACTION))
3019 /* Invalid "sectionid" parameter */
3020 edit_free_file(file);
3021 return JB_ERR_CGI_PARAMS;
3024 if (NULL == (exports = default_exports(csp, NULL)))
3026 edit_free_file(file);
3027 return JB_ERR_MEMORY;
3030 err = map(exports, "f", 1, file->identifier, 1);
3031 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3032 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3034 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3037 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3038 * length limitation and ignore clicks on the Submit buttons if
3039 * the resulting GET URL would be longer than their limit.
3041 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3042 * reached this limit and action editing stopped working in these
3043 * browsers (BR #1570678).
3045 * The config option split-large-forms works around this browser
3046 * bug (HTTP has no URL lenght limitation) by deviding the action
3047 * list form into multiple smaller ones. It means the URLs are shorter
3048 * and work in broken browsers as well, but the user can no longer change
3049 * all actions with one submit.
3051 * A better solution would be to switch to POST requests,
3052 * but this will do for now.
3054 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3056 /* Generate multiple smaller form by killing the big one. */
3057 err = map_block_killer(exports, "one-form-only");
3061 /* Default: Generate one large form by killing the smaller ones. */
3062 err = map_block_killer(exports, "multiple-forms");
3065 for (i = 0; i < MAX_AF_FILES; i++)
3067 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3069 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3077 edit_free_file(file);
3082 if (0 == have_filters)
3083 err = map(exports, "filter-params", 1, "", 1);
3086 /* We have some entries in the filter list */
3089 char * filter_template;
3091 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3094 edit_free_file(file);
3096 if (err == JB_ERR_FILE)
3098 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3103 err = template_fill(&filter_template, exports);
3105 result = strdup("");
3107 for (i = 0; i < MAX_AF_FILES; i++)
3109 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3111 filter_group = csp->rlist[i]->f;
3112 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3114 char current_mode = 'x';
3115 struct list_entry *filter_name;
3117 struct map *line_exports;
3120 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
3121 while ((filter_name != NULL)
3122 && (0 != strcmp(filter_group->name, filter_name->str)))
3124 filter_name = filter_name->next;
3127 if (filter_name != NULL)
3133 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
3134 while ((filter_name != NULL)
3135 && (0 != strcmp(filter_group->name, filter_name->str)))
3137 filter_name = filter_name->next;
3139 if (filter_name != NULL)
3145 /* Generate a unique serial number */
3146 snprintf(number, sizeof(number), "%x", index++);
3147 number[sizeof(number) - 1] = '\0';
3149 line_exports = new_map();
3150 if (line_exports == NULL)
3152 err = JB_ERR_MEMORY;
3157 if (!err) err = map(line_exports, "index", 1, number, 1);
3158 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3159 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3160 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3165 this_line = strdup(filter_template);
3166 if (this_line == NULL) err = JB_ERR_MEMORY;
3168 if (!err) err = template_fill(&this_line, line_exports);
3169 string_join(&result, this_line);
3171 free_map(line_exports);
3176 freez(filter_template);
3180 err = map(exports, "filter-params", 1, result, 0);
3188 if (!err) err = map_radio(exports, "filter-all", "nx",
3189 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3191 edit_free_file(file);
3199 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3203 /*********************************************************************
3205 * Function : cgi_edit_actions_submit
3207 * Description : CGI function that actually edits the Actions list.
3210 * 1 : csp = Current client state (buffers, headers, etc...)
3211 * 2 : rsp = http_response data structure for output
3212 * 3 : parameters = map of cgi parameters
3214 * CGI Parameters : None
3216 * Returns : JB_ERR_OK on success
3217 * JB_ERR_MEMORY on out-of-memory
3218 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3219 * specified or not valid.
3221 *********************************************************************/
3222 jb_err cgi_edit_actions_submit(struct client_state *csp,
3223 struct http_response *rsp,
3224 const struct map *parameters)
3230 struct editable_file * file;
3231 struct file_line * cur_line;
3232 unsigned line_number;
3236 const char * action_set_name;
3238 struct file_list * fl;
3239 struct url_actions * b;
3241 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3243 return cgi_error_disabled(csp, rsp);
3246 err = get_number_param(csp, parameters, "s", §ionid);
3252 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3255 /* No filename specified, can't read file, modified, or out of memory. */
3256 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3259 cur_line = file->lines;
3261 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3263 cur_line = cur_line->next;
3266 if ( (cur_line == NULL)
3267 || (line_number != sectionid)
3269 || (cur_line->type != FILE_LINE_ACTION))
3271 /* Invalid "sectionid" parameter */
3272 edit_free_file(file);
3273 return JB_ERR_CGI_PARAMS;
3276 get_string_param(parameters, "p", &action_set_name);
3277 if (action_set_name != NULL)
3279 for (index = 0; index < MAX_AF_FILES; index++)
3281 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3283 for (b = b->next; NULL != b; b = b->next)
3285 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3287 copy_action(cur_line->data.action, b->action);
3293 edit_free_file(file);
3294 return JB_ERR_CGI_PARAMS;
3300 err = actions_from_radio(parameters, cur_line->data.action);
3306 edit_free_file(file);
3310 ch = get_char_param(parameters, "filter_all");
3313 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3314 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3315 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3319 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3322 for (index = 0; !err; index++)
3329 /* Generate the keys */
3330 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3331 key_value[sizeof(key_value) - 1] = '\0';
3332 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3333 key_name[sizeof(key_name) - 1] = '\0';
3335 err = get_string_param(parameters, key_name, &name);
3344 value = get_char_param(parameters, key_value);
3347 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3348 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3349 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3351 else if (value == 'N')
3353 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3354 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3356 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3357 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3360 else if (value == 'X')
3362 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3363 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3370 edit_free_file(file);
3374 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3377 edit_free_file(file);
3378 return JB_ERR_MEMORY;
3381 len = strlen(actiontext);
3385 * Empty action - must special-case this.
3386 * Simply setting len to 1 is sufficient...
3391 if (NULL == (newtext = malloc(len + 2)))
3395 edit_free_file(file);
3396 return JB_ERR_MEMORY;
3398 strcpy(newtext, actiontext);
3402 newtext[len + 1] = '\0';
3404 freez(cur_line->raw);
3405 freez(cur_line->unprocessed);
3406 cur_line->unprocessed = newtext;
3408 err = edit_write_file(file);
3411 /* Error writing file */
3412 if (err == JB_ERR_FILE)
3414 /* Read-only file. */
3415 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3417 edit_free_file(file);
3421 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3422 (long) time(NULL), file->identifier, sectionid);
3424 edit_free_file(file);
3426 return cgi_redirect(rsp, target);
3430 /*********************************************************************
3432 * Function : cgi_edit_actions_url
3434 * Description : CGI function that actually edits a URL pattern in
3438 * 1 : csp = Current client state (buffers, headers, etc...)
3439 * 2 : rsp = http_response data structure for output
3440 * 3 : parameters = map of cgi parameters
3443 * filename : Identifies the file to edit
3444 * ver : File's last-modified time
3445 * section : Line number of section to edit
3446 * pattern : Line number of pattern to edit
3447 * newval : New value for pattern
3449 * Returns : JB_ERR_OK on success
3450 * JB_ERR_MEMORY on out-of-memory
3451 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3452 * specified or not valid.
3454 *********************************************************************/
3455 jb_err cgi_edit_actions_url(struct client_state *csp,
3456 struct http_response *rsp,
3457 const struct map *parameters)
3461 struct editable_file * file;
3462 struct file_line * cur_line;
3463 unsigned line_number;
3464 unsigned section_start_line_number = 0;
3472 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3474 return cgi_error_disabled(csp, rsp);
3477 err = get_number_param(csp, parameters, "p", &patternid);
3484 return JB_ERR_CGI_PARAMS;
3487 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3493 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3496 /* No filename specified, can't read file, modified, or out of memory. */
3498 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3502 cur_line = file->lines;
3504 while ((cur_line != NULL) && (line_number < patternid))
3506 if (cur_line->type == FILE_LINE_ACTION)
3508 section_start_line_number = line_number;
3510 cur_line = cur_line->next;
3514 if ( (cur_line == NULL)
3515 || (cur_line->type != FILE_LINE_URL))
3517 /* Invalid "patternid" parameter */
3519 edit_free_file(file);
3520 return JB_ERR_CGI_PARAMS;
3523 /* At this point, the line to edit is in cur_line */
3525 freez(cur_line->raw);
3526 freez(cur_line->unprocessed);
3527 cur_line->unprocessed = new_pattern;
3529 err = edit_write_file(file);
3532 /* Error writing file */
3533 if (err == JB_ERR_FILE)
3535 /* Read-only file. */
3536 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3538 edit_free_file(file);
3542 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3543 (long) time(NULL), file->identifier, section_start_line_number);
3545 edit_free_file(file);
3547 return cgi_redirect(rsp, target);
3551 /*********************************************************************
3553 * Function : cgi_edit_actions_add_url
3555 * Description : CGI function that actually adds a URL pattern to
3559 * 1 : csp = Current client state (buffers, headers, etc...)
3560 * 2 : rsp = http_response data structure for output
3561 * 3 : parameters = map of cgi parameters
3564 * filename : Identifies the file to edit
3565 * ver : File's last-modified time
3566 * section : Line number of section to edit
3567 * newval : New pattern
3569 * Returns : JB_ERR_OK on success
3570 * JB_ERR_MEMORY on out-of-memory
3571 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3572 * specified or not valid.
3574 *********************************************************************/
3575 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3576 struct http_response *rsp,
3577 const struct map *parameters)
3581 struct file_line * new_line;
3582 struct editable_file * file;
3583 struct file_line * cur_line;
3584 unsigned line_number;
3588 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3590 return cgi_error_disabled(csp, rsp);
3593 err = get_number_param(csp, parameters, "s", §ionid);
3600 return JB_ERR_CGI_PARAMS;
3603 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3609 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3612 /* No filename specified, can't read file, modified, or out of memory. */
3614 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3618 cur_line = file->lines;
3620 while ((cur_line != NULL) && (line_number < sectionid))
3622 cur_line = cur_line->next;
3626 if ( (cur_line == NULL)
3627 || (cur_line->type != FILE_LINE_ACTION))
3629 /* Invalid "sectionid" parameter */
3631 edit_free_file(file);
3632 return JB_ERR_CGI_PARAMS;
3635 /* At this point, the section header is in cur_line - add after this. */
3637 /* Allocate the new line */
3638 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3639 if (new_line == NULL)
3642 edit_free_file(file);
3643 return JB_ERR_MEMORY;
3646 /* Fill in the data members of the new line */
3647 new_line->raw = NULL;
3648 new_line->prefix = NULL;
3649 new_line->unprocessed = new_pattern;
3650 new_line->type = FILE_LINE_URL;
3652 /* Link new_line into the list, after cur_line */
3653 new_line->next = cur_line->next;
3654 cur_line->next = new_line;
3656 /* Done making changes, now commit */
3658 err = edit_write_file(file);
3661 /* Error writing file */
3662 if (err == JB_ERR_FILE)
3664 /* Read-only file. */
3665 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3667 edit_free_file(file);
3671 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3672 (long) time(NULL), file->identifier, sectionid);
3674 edit_free_file(file);
3676 return cgi_redirect(rsp, target);
3680 /*********************************************************************
3682 * Function : cgi_edit_actions_remove_url
3684 * Description : CGI function that actually removes a URL pattern from
3688 * 1 : csp = Current client state (buffers, headers, etc...)
3689 * 2 : rsp = http_response data structure for output
3690 * 3 : parameters = map of cgi parameters
3693 * f : (filename) Identifies the file to edit
3694 * v : (version) File's last-modified time
3695 * p : (pattern) Line number of pattern to remove
3697 * Returns : JB_ERR_OK on success
3698 * JB_ERR_MEMORY on out-of-memory
3699 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3700 * specified or not valid.
3702 *********************************************************************/
3703 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3704 struct http_response *rsp,
3705 const struct map *parameters)
3708 struct editable_file * file;
3709 struct file_line * cur_line;
3710 struct file_line * prev_line;
3711 unsigned line_number;
3712 unsigned section_start_line_number = 0;
3716 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3718 return cgi_error_disabled(csp, rsp);
3721 err = get_number_param(csp, parameters, "p", &patternid);
3727 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3730 /* No filename specified, can't read file, modified, or out of memory. */
3731 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3736 cur_line = file->lines;
3738 while ((cur_line != NULL) && (line_number < patternid))
3740 if (cur_line->type == FILE_LINE_ACTION)
3742 section_start_line_number = line_number;
3744 prev_line = cur_line;
3745 cur_line = cur_line->next;
3749 if ( (cur_line == NULL)
3750 || (prev_line == NULL)
3751 || (cur_line->type != FILE_LINE_URL))
3753 /* Invalid "patternid" parameter */
3754 edit_free_file(file);
3755 return JB_ERR_CGI_PARAMS;
3758 /* At this point, the line to remove is in cur_line, and the previous
3759 * one is in prev_line
3762 /* Unlink cur_line */
3763 prev_line->next = cur_line->next;
3764 cur_line->next = NULL;
3767 edit_free_file_lines(cur_line);
3769 err = edit_write_file(file);
3772 /* Error writing file */
3773 if (err == JB_ERR_FILE)
3775 /* Read-only file. */
3776 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3778 edit_free_file(file);
3782 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3783 (long) time(NULL), file->identifier, section_start_line_number);
3785 edit_free_file(file);
3787 return cgi_redirect(rsp, target);
3791 /*********************************************************************
3793 * Function : cgi_edit_actions_section_remove
3795 * Description : CGI function that actually removes a whole section from
3796 * the actions file. The section must be empty first
3797 * (else JB_ERR_CGI_PARAMS).
3800 * 1 : csp = Current client state (buffers, headers, etc...)
3801 * 2 : rsp = http_response data structure for output
3802 * 3 : parameters = map of cgi parameters
3805 * f : (filename) Identifies the file to edit
3806 * v : (version) File's last-modified time
3807 * s : (section) Line number of section to edit
3809 * Returns : JB_ERR_OK on success
3810 * JB_ERR_MEMORY on out-of-memory
3811 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3812 * specified or not valid.
3814 *********************************************************************/
3815 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3816 struct http_response *rsp,
3817 const struct map *parameters)
3820 struct editable_file * file;
3821 struct file_line * cur_line;
3822 struct file_line * prev_line;
3823 unsigned line_number;
3827 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3829 return cgi_error_disabled(csp, rsp);
3832 err = get_number_param(csp, parameters, "s", §ionid);
3838 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3841 /* No filename specified, can't read file, modified, or out of memory. */
3842 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3846 cur_line = file->lines;
3849 while ((cur_line != NULL) && (line_number < sectionid))
3851 prev_line = cur_line;
3852 cur_line = cur_line->next;
3856 if ( (cur_line == NULL)
3857 || (cur_line->type != FILE_LINE_ACTION) )
3859 /* Invalid "sectionid" parameter */
3860 edit_free_file(file);
3861 return JB_ERR_CGI_PARAMS;
3864 if ( (cur_line->next != NULL)
3865 && (cur_line->next->type == FILE_LINE_URL) )
3867 /* Section not empty. */
3868 edit_free_file(file);
3869 return JB_ERR_CGI_PARAMS;
3872 /* At this point, the line to remove is in cur_line, and the previous
3873 * one is in prev_line
3876 /* Unlink cur_line */
3877 if (prev_line == NULL)
3879 /* Removing the first line from the file */
3880 file->lines = cur_line->next;
3884 prev_line->next = cur_line->next;
3886 cur_line->next = NULL;
3889 edit_free_file_lines(cur_line);
3891 err = edit_write_file(file);
3894 /* Error writing file */
3895 if (err == JB_ERR_FILE)
3897 /* Read-only file. */
3898 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3900 edit_free_file(file);
3904 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
3905 (long) time(NULL), file->identifier);
3907 edit_free_file(file);
3909 return cgi_redirect(rsp, target);
3913 /*********************************************************************
3915 * Function : cgi_edit_actions_section_add
3917 * Description : CGI function that adds a new empty section to
3921 * 1 : csp = Current client state (buffers, headers, etc...)
3922 * 2 : rsp = http_response data structure for output
3923 * 3 : parameters = map of cgi parameters
3926 * f : (filename) Identifies the file to edit
3927 * v : (version) File's last-modified time
3928 * s : (section) Line number of section to add after, 0 for
3931 * Returns : JB_ERR_OK on success
3932 * JB_ERR_MEMORY on out-of-memory
3933 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3934 * specified or not valid.
3936 *********************************************************************/
3937 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3938 struct http_response *rsp,
3939 const struct map *parameters)
3942 struct file_line * new_line;
3944 struct editable_file * file;
3945 struct file_line * cur_line;
3946 unsigned line_number;
3950 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3952 return cgi_error_disabled(csp, rsp);
3955 err = get_number_param(csp, parameters, "s", §ionid);
3961 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3964 /* No filename specified, can't read file, modified, or out of memory. */
3965 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3969 cur_line = file->lines;
3971 if (sectionid <= 1U)
3973 /* Add to start of file */
3974 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
3976 /* There's something in the file, find the line before the first
3979 while ( (cur_line->next != NULL)
3980 && (cur_line->next->type != FILE_LINE_ACTION) )
3982 cur_line = cur_line->next;
3988 /* File starts with action line, so insert at top */
3994 /* Add after stated section. */
3995 while ((cur_line != NULL) && (line_number < sectionid))
3997 cur_line = cur_line->next;
4001 if ( (cur_line == NULL)
4002 || (cur_line->type != FILE_LINE_ACTION))
4004 /* Invalid "sectionid" parameter */
4005 edit_free_file(file);
4006 return JB_ERR_CGI_PARAMS;
4009 /* Skip through the section to find the last line in it. */
4010 while ( (cur_line->next != NULL)
4011 && (cur_line->next->type != FILE_LINE_ACTION) )
4013 cur_line = cur_line->next;
4018 /* At this point, the last line in the previous section is in cur_line
4019 * - add after this. (Or if we need to add as the first line, cur_line
4023 new_text = strdup("{}");
4024 if (NULL == new_text)
4026 edit_free_file(file);
4027 return JB_ERR_MEMORY;
4030 /* Allocate the new line */
4031 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4032 if (new_line == NULL)
4035 edit_free_file(file);
4036 return JB_ERR_MEMORY;
4039 /* Fill in the data members of the new line */
4040 new_line->raw = NULL;
4041 new_line->prefix = NULL;
4042 new_line->unprocessed = new_text;
4043 new_line->type = FILE_LINE_ACTION;
4045 if (cur_line != NULL)
4047 /* Link new_line into the list, after cur_line */
4048 new_line->next = cur_line->next;
4049 cur_line->next = new_line;
4053 /* Link new_line into the list, as first line */
4054 new_line->next = file->lines;
4055 file->lines = new_line;
4058 /* Done making changes, now commit */
4060 err = edit_write_file(file);
4063 /* Error writing file */
4064 if (err == JB_ERR_FILE)
4066 /* Read-only file. */
4067 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4069 edit_free_file(file);
4073 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4074 (long) time(NULL), file->identifier);
4076 edit_free_file(file);
4078 return cgi_redirect(rsp, target);
4082 /*********************************************************************
4084 * Function : cgi_edit_actions_section_swap
4086 * Description : CGI function that swaps the order of two sections
4087 * in the actions file. Note that this CGI can actually
4088 * swap any two arbitrary sections, but the GUI interface
4089 * currently only allows consecutive sections to be
4093 * 1 : csp = Current client state (buffers, headers, etc...)
4094 * 2 : rsp = http_response data structure for output
4095 * 3 : parameters = map of cgi parameters
4098 * f : (filename) Identifies the file to edit
4099 * v : (version) File's last-modified time
4100 * s1 : (section1) Line number of first section to swap
4101 * s2 : (section2) Line number of second section to swap
4103 * Returns : JB_ERR_OK on success
4104 * JB_ERR_MEMORY on out-of-memory
4105 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4106 * specified or not valid.
4108 *********************************************************************/
4109 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4110 struct http_response *rsp,
4111 const struct map *parameters)
4115 struct editable_file * file;
4116 struct file_line * cur_line;
4117 struct file_line * prev_line;
4118 struct file_line * line_before_section1;
4119 struct file_line * line_start_section1;
4120 struct file_line * line_end_section1;
4121 struct file_line * line_after_section1;
4122 struct file_line * line_before_section2;
4123 struct file_line * line_start_section2;
4124 struct file_line * line_end_section2;
4125 struct file_line * line_after_section2;
4126 unsigned line_number;
4130 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4132 return cgi_error_disabled(csp, rsp);
4135 err = get_number_param(csp, parameters, "s1", §ion1);
4136 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4142 if (section1 > section2)
4144 unsigned temp = section2;
4145 section2 = section1;
4149 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4152 /* No filename specified, can't read file, modified, or out of memory. */
4153 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4156 /* Start at the beginning... */
4158 cur_line = file->lines;
4161 /* ... find section1 ... */
4162 while ((cur_line != NULL) && (line_number < section1))
4164 prev_line = cur_line;
4165 cur_line = cur_line->next;
4169 if ( (cur_line == NULL)
4170 || (cur_line->type != FILE_LINE_ACTION) )
4172 /* Invalid "section1" parameter */
4173 edit_free_file(file);
4174 return JB_ERR_CGI_PARAMS;
4177 /* If no-op, we've validated params and can skip the rest. */
4178 if (section1 != section2)
4180 /* ... find the end of section1 ... */
4181 line_before_section1 = prev_line;
4182 line_start_section1 = cur_line;
4185 prev_line = cur_line;
4186 cur_line = cur_line->next;
4189 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4190 line_end_section1 = prev_line;
4191 line_after_section1 = cur_line;
4193 /* ... find section2 ... */
4194 while ((cur_line != NULL) && (line_number < section2))
4196 prev_line = cur_line;
4197 cur_line = cur_line->next;
4201 if ( (cur_line == NULL)
4202 || (cur_line->type != FILE_LINE_ACTION) )
4204 /* Invalid "section2" parameter */
4205 edit_free_file(file);
4206 return JB_ERR_CGI_PARAMS;
4209 /* ... find the end of section2 ... */
4210 line_before_section2 = prev_line;
4211 line_start_section2 = cur_line;
4214 prev_line = cur_line;
4215 cur_line = cur_line->next;
4218 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4219 line_end_section2 = prev_line;
4220 line_after_section2 = cur_line;
4222 /* Now have all the pointers we need. Do the swap. */
4224 /* Change the pointer to section1 to point to section2 instead */
4225 if (line_before_section1 == NULL)
4227 file->lines = line_start_section2;
4231 line_before_section1->next = line_start_section2;
4234 if (line_before_section2 == line_end_section1)
4236 /* Consecutive sections */
4237 line_end_section2->next = line_start_section1;
4241 line_end_section2->next = line_after_section1;
4242 line_before_section2->next = line_start_section1;
4245 /* Set the pointer from the end of section1 to the rest of the file */
4246 line_end_section1->next = line_after_section2;
4248 err = edit_write_file(file);
4251 /* Error writing file */
4252 if (err == JB_ERR_FILE)
4254 /* Read-only file. */
4255 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4257 edit_free_file(file);
4260 } /* END if (section1 != section2) */
4262 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4263 (long) time(NULL), file->identifier);
4265 edit_free_file(file);
4267 return cgi_redirect(rsp, target);
4270 #ifdef FEATURE_TOGGLE
4271 /*********************************************************************
4273 * Function : cgi_toggle
4275 * Description : CGI function that adds a new empty section to
4279 * 1 : csp = Current client state (buffers, headers, etc...)
4280 * 2 : rsp = http_response data structure for output
4281 * 3 : parameters = map of cgi parameters
4284 * set : If present, how to change toggle setting:
4285 * "enable", "disable", "toggle", or none (default).
4286 * mini : If present, use mini reply template.
4288 * Returns : JB_ERR_OK on success
4289 * JB_ERR_MEMORY on out-of-memory
4291 *********************************************************************/
4292 jb_err cgi_toggle(struct client_state *csp,
4293 struct http_response *rsp,
4294 const struct map *parameters)
4296 struct map *exports;
4298 const char *template_name;
4304 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4306 return cgi_error_disabled(csp, rsp);
4309 mode = get_char_param(parameters, "set");
4314 global_toggle_state = 1;
4316 else if (mode == 'D')
4319 global_toggle_state = 0;
4321 else if (mode == 'T')
4324 global_toggle_state = !global_toggle_state;
4327 if (NULL == (exports = default_exports(csp, "toggle")))
4329 return JB_ERR_MEMORY;
4332 template_name = (get_char_param(parameters, "mini")
4336 return template_fill_for_cgi(csp, template_name, exports, rsp);
4338 #endif /* def FEATURE_TOGGLE */
4340 /*********************************************************************
4342 * Function : javascriptify
4344 * Description : Converts a string into a form JavaScript will like.
4346 * Netscape 4's JavaScript sucks - it doesn't use
4347 * "id" parameters, so you have to set the "name"
4348 * used to submit a form element to something JavaScript
4349 * will like. (Or access the elements by index in an
4350 * array. That array contains >60 elements and will
4351 * be changed whenever we add a new action to the
4352 * editor, so I'm NOT going to use indexes that have
4353 * to be figured out by hand.)
4355 * Currently the only thing we have to worry about
4356 * is "-" ==> "_" conversion.
4358 * This is a length-preserving operation so it is
4359 * carried out in-place, no memory is allocated
4363 * 1 : identifier = String to make JavaScript-friendly.
4367 *********************************************************************/
4368 static void javascriptify(char * identifier)
4370 char * p = identifier;
4371 while (NULL != (p = strchr(p, '-')))
4378 /*********************************************************************
4380 * Function : actions_to_radio
4382 * Description : Converts a actionsfile entry into settings for
4383 * radio buttons and edit boxes on a HTML form.
4386 * 1 : exports = List of substitutions to add to.
4387 * 2 : action = Action to read
4389 * Returns : JB_ERR_OK on success
4390 * JB_ERR_MEMORY on out-of-memory
4392 *********************************************************************/
4393 static jb_err actions_to_radio(struct map * exports,
4394 const struct action_spec *action)
4396 unsigned mask = action->mask;
4397 unsigned add = action->add;
4405 mask = action->mask;
4408 /* sanity - prevents "-feature +feature" */
4412 #define DEFINE_ACTION_BOOL(name, bit) \
4413 if (!(mask & bit)) \
4415 current_mode = 'n'; \
4417 else if (add & bit) \
4419 current_mode = 'y'; \
4423 current_mode = 'x'; \
4425 if (map_radio(exports, name, "ynx", current_mode)) \
4427 return JB_ERR_MEMORY; \
4430 #define DEFINE_ACTION_STRING(name, bit, index) \
4431 DEFINE_ACTION_BOOL(name, bit); \
4434 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4437 checked = !strcmp(action->string[index], value); \
4441 checked = is_default; \
4443 mapped_param |= checked; \
4444 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4446 return JB_ERR_MEMORY; \
4449 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4450 if (map(exports, name "-param-custom", 1, \
4451 ((!mapped_param) ? "checked" : ""), 1)) \
4453 return JB_ERR_MEMORY; \
4455 if (map(exports, name "-param", 1, \
4456 (((add & bit) && !mapped_param) ? \
4457 action->string[index] : default_val), 1)) \
4459 return JB_ERR_MEMORY; \
4462 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4463 if (map(exports, name "-param", 1, \
4464 ((add & bit) ? action->string[index] : default_val), 1)) \
4466 return JB_ERR_MEMORY; \
4469 #define DEFINE_ACTION_MULTI(name, index) \
4470 if (action->multi_add[index]->first) \
4472 current_mode = 'y'; \
4474 else if (action->multi_remove_all[index]) \
4476 current_mode = 'n'; \
4478 else if (action->multi_remove[index]->first) \
4480 current_mode = 'y'; \
4484 current_mode = 'x'; \
4486 if (map_radio(exports, name, "ynx", current_mode)) \
4488 return JB_ERR_MEMORY; \
4491 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4493 #include "actionlist.h"
4495 #undef DEFINE_ACTION_MULTI
4496 #undef DEFINE_ACTION_STRING
4497 #undef DEFINE_ACTION_BOOL
4498 #undef DEFINE_ACTION_ALIAS
4499 #undef DEFINE_CGI_PARAM_CUSTOM
4500 #undef DEFINE_CGI_PARAM_RADIO
4501 #undef DEFINE_CGI_PARAM_NO_RADIO
4507 /*********************************************************************
4509 * Function : actions_from_radio
4511 * Description : Converts a map of parameters passed to a CGI function
4512 * into an actionsfile entry.
4515 * 1 : parameters = parameters to the CGI call
4516 * 2 : action = Action to change. Must be valid before
4517 * the call, actions not specified will be
4520 * Returns : JB_ERR_OK on success
4521 * JB_ERR_MEMORY on out-of-memory
4523 *********************************************************************/
4524 static jb_err actions_from_radio(const struct map * parameters,
4525 struct action_spec *action)
4530 const char * js_name;
4531 jb_err err = JB_ERR_OK;
4536 /* Statics are generally a potential race condition,
4537 * but in this case we're safe and don't need semaphores.
4538 * Be careful if you modify this function.
4540 * The js_name_arr's are never free()d, but this is no
4541 * problem, since they will only be created once and
4542 * used by all threads thereafter. -oes
4545 #define JAVASCRIPTIFY(dest_var, string) \
4547 static int first_time = 1; \
4548 static char *js_name_arr; \
4551 js_name_arr = strdup(string); \
4552 javascriptify(js_name_arr); \
4554 dest_var = js_name_arr; \
4558 #define DEFINE_ACTION_BOOL(name, bit) \
4559 JAVASCRIPTIFY(js_name, name); \
4560 ch = get_char_param(parameters, js_name); \
4563 action->add |= bit; \
4564 action->mask |= bit; \
4566 else if (ch == 'N') \
4568 action->add &= ~bit; \
4569 action->mask &= ~bit; \
4571 else if (ch == 'X') \
4573 action->add &= ~bit; \
4574 action->mask |= bit; \
4577 #define DEFINE_ACTION_STRING(name, bit, index) \
4578 JAVASCRIPTIFY(js_name, name); \
4579 ch = get_char_param(parameters, js_name); \
4583 JAVASCRIPTIFY(js_name, name "-mode"); \
4584 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4585 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4587 JAVASCRIPTIFY(js_name, name "-param"); \
4588 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4590 if (param != NULL) \
4592 if (NULL == (param_dup = strdup(param))) \
4594 return JB_ERR_MEMORY; \
4596 freez(action->string[index]); \
4597 action->add |= bit; \
4598 action->mask |= bit; \
4599 action->string[index] = param_dup; \
4602 else if (ch == 'N') \
4604 if (action->add & bit) \
4606 freez(action->string[index]); \
4608 action->add &= ~bit; \
4609 action->mask &= ~bit; \
4611 else if (ch == 'X') \
4613 if (action->add & bit) \
4615 freez(action->string[index]); \
4617 action->add &= ~bit; \
4618 action->mask |= bit; \
4621 #define DEFINE_ACTION_MULTI(name, index) \
4622 JAVASCRIPTIFY(js_name, name); \
4623 ch = get_char_param(parameters, js_name); \
4628 else if (ch == 'N') \
4630 list_remove_all(action->multi_add[index]); \
4631 list_remove_all(action->multi_remove[index]); \
4632 action->multi_remove_all[index] = 1; \
4634 else if (ch == 'X') \
4636 list_remove_all(action->multi_add[index]); \
4637 list_remove_all(action->multi_remove[index]); \
4638 action->multi_remove_all[index] = 0; \
4641 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4643 #include "actionlist.h"
4645 #undef DEFINE_ACTION_MULTI
4646 #undef DEFINE_ACTION_STRING
4647 #undef DEFINE_ACTION_BOOL
4648 #undef DEFINE_ACTION_ALIAS
4649 #undef JAVASCRIPTIFY
4655 #endif /* def FEATURE_CGI_EDIT_ACTIONS */