1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.41.2.5 2003/12/17 16:33:47 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/Attic/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.41.2.5 2003/12/17 16:33:47 oes
46 * - All edit functions that redirect back to the list page
47 * now use cgi_redirect
48 * - All redirects now contain useless parameter "foo", whose
49 * value are raw seconds since epoch, in order to force
50 * Opera and Konqueror to properly reload the list. Closes
53 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
54 * Cosmetic: Renamed cryptic variable
56 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
57 * Fix: Don't free uninitialized struct editable_file
59 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
60 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
62 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
63 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
65 * Revision 1.41 2002/05/21 19:09:45 oes
66 * - Made Add/Edit/Remove URL Submit and Cancel
67 * buttons jump back to relevant section in eal
68 * - Bugfix: remove-url-form needs p export
70 * Revision 1.40 2002/05/19 11:34:35 jongfoster
71 * Handling read-only actions files better - report the actual
72 * error, not "Out of memory"!
75 * http://sourceforge.net/tracker/index.php?func=detail
76 * &aid=557905&group_id=11118&atid=111118
78 * Revision 1.39 2002/05/12 21:39:15 jongfoster
79 * - Adding Doxygen-style comments to structures and #defines.
80 * - Correcting function comments
82 * Revision 1.38 2002/05/03 23:00:38 jongfoster
83 * Support for templates for "standard actions" buttons.
86 * Revision 1.37 2002/04/30 11:14:52 oes
87 * Made csp the first parameter in *action_to_html
89 * Revision 1.36 2002/04/26 21:53:30 jongfoster
90 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
92 * Revision 1.35 2002/04/26 21:50:02 jongfoster
93 * Honouring default exports in edit-actions-for-url-filter template.
95 * Revision 1.34 2002/04/26 12:54:17 oes
96 * Adaptions to changes in actions.c
98 * Revision 1.33 2002/04/24 02:17:47 oes
99 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
101 * - Activated Jon's code for editing multiple AFs
102 * - cgi_edit_list_actions now provides context-sensitive
103 * help, looks up all action sets from standard.action and
104 * makes buttons for them in the catchall section
105 * - cgi_edit_action_submit now honors a p parameter, looks up
106 * the corresponding action set, and sets the catchall pattern's
107 * actions accordingly.
109 * Revision 1.32 2002/04/19 16:55:31 jongfoster
110 * Fixing newline problems. If we do our own text file newline
111 * mangling, we don't want the library to do any, so we need to
112 * open the files in *binary* mode.
114 * Revision 1.31 2002/04/18 19:21:08 jongfoster
115 * Added code to detect "conventional" action files, that start
116 * with a set of actions for all URLs (the pattern "/").
117 * These are special-cased in the "edit-actions-list" CGI, so
118 * that a special UI can be written for them.
120 * Revision 1.30 2002/04/10 13:38:35 oes
121 * load_template signature changed
123 * Revision 1.29 2002/04/08 16:59:08 oes
126 * Revision 1.28 2002/03/27 12:30:29 oes
127 * Deleted unsused variable
129 * Revision 1.27 2002/03/26 23:06:04 jongfoster
130 * Removing duplicate @ifs on the toggle page
132 * Revision 1.26 2002/03/26 22:59:17 jongfoster
133 * Fixing /toggle to display status consistently.
135 * Revision 1.25 2002/03/26 22:29:54 swa
136 * we have a new homepage!
138 * Revision 1.24 2002/03/24 15:23:33 jongfoster
141 * Revision 1.23 2002/03/24 13:32:41 swa
142 * name change related issues
144 * Revision 1.22 2002/03/24 13:25:43 swa
145 * name change related issues
147 * Revision 1.21 2002/03/22 18:02:48 jongfoster
148 * Fixing remote toggle
150 * Revision 1.20 2002/03/16 20:28:34 oes
151 * Added descriptions to the filters so users will know what they select in the cgi editor
153 * Revision 1.19 2002/03/16 18:38:14 jongfoster
154 * Stopping stupid or malicious users from breaking the actions
155 * file using the web-based editor.
157 * Revision 1.18 2002/03/16 14:57:44 jongfoster
158 * Full support for enabling/disabling modular filters.
160 * Revision 1.17 2002/03/16 14:26:42 jongfoster
161 * First version of modular filters support - READ ONLY!
162 * Fixing a double-free bug in the out-of-memory handling in map_radio().
164 * Revision 1.16 2002/03/07 03:46:17 oes
165 * Fixed compiler warnings
167 * Revision 1.15 2002/03/06 22:54:35 jongfoster
168 * Automated function-comment nitpicking.
170 * Revision 1.14 2002/03/05 00:24:51 jongfoster
171 * Patch to always edit the current actions file.
173 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
174 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
176 * Revision 1.12 2002/03/03 09:18:03 joergs
177 * Made jumbjuster work on AmigaOS again.
179 * Revision 1.11 2002/01/23 01:03:31 jongfoster
180 * Fixing gcc [CygWin] compiler warnings
182 * Revision 1.10 2002/01/23 00:22:59 jongfoster
183 * Adding new function cgi_edit_actions_section_swap(), to reorder
186 * Adding get_url_spec_param() to get a validated URL pattern.
188 * Moving edit_read_line() out of this file and into loaders.c.
190 * Adding missing html_encode() to many CGI functions.
192 * Moving the functions that #include actionlist.h to the end of the file,
193 * because the Visual C++ 97 debugger gets extremely confused if you try
194 * to debug any code that comes after them in the file.
196 * Major optimizations in cgi_edit_actions_list() to reduce the size of
197 * the generated HTML (down 40% from 550k to 304k), with major side-effects
198 * throughout the editor and templates. In particular, the length of the
199 * URLs throughout the editor has been drastically reduced, by cutting
200 * paramater names down to 1 character and CGI names down to 3-4
201 * characters, by removing all non-essential CGI paramaters even at the
202 * expense of having to re-read the actions file for the most trivial
203 * page, and by using relative rather than absolute URLs. This means
204 * that this (typical example):
206 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
207 * filename=ijb&ver=1011487572&section=12&pattern=13
208 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
212 * <a href="eau?f=ijb&v=1011487572&p=13">
214 * Revision 1.9 2002/01/17 20:56:22 jongfoster
215 * Replacing hard references to the URL of the config interface
216 * with #defines from project.h
218 * Revision 1.8 2001/11/30 23:35:51 jongfoster
219 * Renaming actionsfile to ijb.action
221 * Revision 1.7 2001/11/13 00:28:24 jongfoster
222 * - Renaming parameters from edit-actions-for-url so that they only
223 * contain legal JavaScript characters. If we wanted to write
224 * JavaScript that worked with Netscape 4, this is nessacery.
225 * (Note that at the moment the JavaScript doesn't actually work
226 * with Netscape 4, but now this is purely a template issue, not
227 * one affecting code).
228 * - Adding new CGIs for use by non-JavaScript browsers:
229 * edit-actions-url-form
230 * edit-actions-add-url-form
231 * edit-actions-remove-url-form
234 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
235 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
236 * by and __OS2__ ifdef.
238 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
239 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
240 * threads to call select() simultaneously. So, it's time to do a real, live,
241 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
242 * (native). Both versions will work, but using __OS2__ offers multi-threading.
244 * Revision 1.4 2001/10/23 21:48:19 jongfoster
245 * Cleaning up error handling in CGI functions - they now send back
246 * a HTML error page and should never cause a FATAL error. (Fixes one
247 * potential source of "denial of service" attacks).
249 * CGI actions file editor that works and is actually useful.
251 * Ability to toggle JunkBuster remotely using a CGI call.
253 * You can turn off both the above features in the main configuration
254 * file, e.g. if you are running a multi-user proxy.
256 * Revision 1.3 2001/10/14 22:12:49 jongfoster
257 * New version of CGI-based actionsfile editor.
258 * Major changes, including:
259 * - Completely new file parser and file output routines
260 * - edit-actions CGI renamed edit-actions-for-url
261 * - All CGIs now need a filename parameter, except for...
262 * - New CGI edit-actions which doesn't need a filename,
263 * to allow you to start the editor up.
264 * - edit-actions-submit now works, and now automatically
265 * redirects you back to the main edit-actions-list handler.
267 * Revision 1.2 2001/09/16 17:05:14 jongfoster
268 * Removing unused #include showarg.h
270 * Revision 1.1 2001/09/16 15:47:37 jongfoster
271 * First version of CGI-based edit interface. This is very much a
272 * work-in-progress, and you can't actually use it to edit anything
273 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
274 * to have any effect.
277 **********************************************************************/
283 * FIXME: Following includes copied from cgi.c - which are actually needed?
288 #include <sys/types.h>
292 #include <sys/stat.h>
295 #define snprintf _snprintf
296 #endif /* def _WIN32 */
301 #include "cgisimple.h"
305 #include "miscutil.h"
309 /* loadcfg.h is for global_toggle_state only */
310 #include "urlmatch.h"
312 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
315 #ifdef FEATURE_CGI_EDIT_ACTIONS
318 * A line in an editable_file.
322 /** Next entry in the linked list */
323 struct file_line * next;
325 /** The raw data, to write out if this line is unmodified. */
328 /** Comments and/or whitespace to put before this line if it's modified
329 and then written out. */
332 /** The actual data, as a string. Line continuation and comment removal
333 are performed on the data read from file before it's stored here, so
334 it will be a single line of data. */
337 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
340 /** The actual data, processed into some sensible data type. */
344 /** An action specification. */
345 struct action_spec action[1];
347 /** A name=value pair. */
351 /** The name in the name=value pair. */
354 /** The value in the name=value pair, as a string. */
357 /** The value in the name=value pair, as an integer. */
362 /* Add more data types here... e.g.
365 struct url_spec url[1];
369 struct action_spec action[1];
379 /** This file_line has not been processed yet. */
380 #define FILE_LINE_UNPROCESSED 1
382 /** This file_line is blank. Can only appear at the end of a file, due to
383 the way the parser works. */
384 #define FILE_LINE_BLANK 2
386 /** This file_line says {{alias}}. */
387 #define FILE_LINE_ALIAS_HEADER 3
389 /** This file_line defines an alias. */
390 #define FILE_LINE_ALIAS_ENTRY 4
392 /** This file_line defines an {action}. */
393 #define FILE_LINE_ACTION 5
395 /** This file_line specifies a URL pattern. */
396 #define FILE_LINE_URL 6
398 /** This file_line says {{settings}}. */
399 #define FILE_LINE_SETTINGS_HEADER 7
401 /** This file_line is in a {{settings}} block. */
402 #define FILE_LINE_SETTINGS_ENTRY 8
404 /** This file_line says {{description}}. */
405 #define FILE_LINE_DESCRIPTION_HEADER 9
407 /** This file_line is in a {{description}} block. */
408 #define FILE_LINE_DESCRIPTION_ENTRY 10
412 * A configuration file, in a format that can be edited and written back to
417 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
418 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
419 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
420 /**< Pre-encoded with url_encode() for ease of use. */
421 const char * version_str; /**< Last modification time, as a string. For CGI param. */
422 /**< Can be used in URL without using url_param(). */
423 unsigned version; /**< Last modification time - prevents chaos with
424 the browser's "back" button. Note that this is a
425 time_t cast to an unsigned. When comparing, always
426 cast the time_t to an unsigned, and *NOT* vice-versa.
427 This may lose the top few bits, but they're not
428 significant anyway. */
429 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
430 Note that changing this after the file has been
431 read in will cause a mess. */
432 struct file_line * parse_error; /**< On parse error, this is the offending line. */
433 const char * parse_error_text; /**< On parse error, this is the problem.
434 (Statically allocated) */
437 /* FIXME: Following non-static functions should be prototyped in .h or made static */
439 /* Functions to read and write arbitrary config files */
440 jb_err edit_read_file(struct client_state *csp,
441 const struct map *parameters,
444 struct editable_file **pfile);
445 jb_err edit_write_file(struct editable_file * file);
446 void edit_free_file(struct editable_file * file);
448 /* Functions to read and write actions files */
449 jb_err edit_parse_actions_file(struct editable_file * file);
450 jb_err edit_read_actions_file(struct client_state *csp,
451 struct http_response *rsp,
452 const struct map *parameters,
454 struct editable_file **pfile);
457 jb_err cgi_error_modified(struct client_state *csp,
458 struct http_response *rsp,
459 const char *filename);
460 jb_err cgi_error_parse(struct client_state *csp,
461 struct http_response *rsp,
462 struct editable_file *file);
463 jb_err cgi_error_file(struct client_state *csp,
464 struct http_response *rsp,
465 const char *filename);
466 jb_err cgi_error_file_read_only(struct client_state *csp,
467 struct http_response *rsp,
468 const char *filename);
469 jb_err cgi_error_disabled(struct client_state *csp,
470 struct http_response *rsp);
472 /* Internal arbitrary config file support functions */
473 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
474 static void edit_free_file_lines(struct file_line * first_line);
476 /* Internal actions file support functions */
477 static int match_actions_file_header_line(const char * line, const char * name);
478 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
480 /* Internal parameter parsing functions */
481 static jb_err get_file_name_param(struct client_state *csp,
482 const struct map *parameters,
483 const char *param_name,
486 const char **pparam);
488 static jb_err get_url_spec_param(struct client_state *csp,
489 const struct map *parameters,
494 /* Internal actionsfile <==> HTML conversion functions */
495 static jb_err map_radio(struct map * exports,
496 const char * optionname,
499 static jb_err actions_to_radio(struct map * exports,
500 const struct action_spec *action);
501 static jb_err actions_from_radio(const struct map * parameters,
502 struct action_spec *action);
505 static jb_err map_copy_parameter_html(struct map *out,
506 const struct map *in,
508 #if 0 /* unused function */
509 static jb_err map_copy_parameter_url(struct map *out,
510 const struct map *in,
512 #endif /* unused function */
514 /* Internal convenience functions */
515 static char *section_target(const unsigned sectionid);
517 /*********************************************************************
519 * Function : section_target
521 * Description : Given an unsigned (section id) n, produce a dynamically
522 * allocated string of the form #l<n>, for use in link
526 * 1 : sectionid = start line number of section
528 * Returns : String with link target, or NULL if out of
531 *********************************************************************/
532 static char *section_target(const unsigned sectionid)
536 snprintf(buf, 30, "#l%d", sectionid);
542 /*********************************************************************
544 * Function : map_copy_parameter_html
546 * Description : Copy a CGI parameter from one map to another, HTML
550 * 1 : out = target map
551 * 2 : in = source map
552 * 3 : name = name of cgi parameter to copy
554 * Returns : JB_ERR_OK on success
555 * JB_ERR_MEMORY on out-of-memory
556 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
559 *********************************************************************/
560 static jb_err map_copy_parameter_html(struct map *out,
561 const struct map *in,
571 value = lookup(in, name);
572 err = map(out, name, 1, html_encode(value), 0);
579 else if (*value == '\0')
581 return JB_ERR_CGI_PARAMS;
590 #if 0 /* unused function */
591 /*********************************************************************
593 * Function : map_copy_parameter_url
595 * Description : Copy a CGI parameter from one map to another, URL
599 * 1 : out = target map
600 * 2 : in = source map
601 * 3 : name = name of cgi parameter to copy
603 * Returns : JB_ERR_OK on success
604 * JB_ERR_MEMORY on out-of-memory
605 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
608 *********************************************************************/
609 static jb_err map_copy_parameter_url(struct map *out,
610 const struct map *in,
620 value = lookup(in, name);
621 err = map(out, name, 1, url_encode(value), 0);
628 else if (*value == '\0')
630 return JB_ERR_CGI_PARAMS;
637 #endif /* 0 - unused function */
639 /*********************************************************************
641 * Function : cgi_edit_actions_url_form
643 * Description : CGI function that displays a form for
647 * 1 : csp = Current client state (buffers, headers, etc...)
648 * 2 : rsp = http_response data structure for output
649 * 3 : parameters = map of cgi parameters
652 * f : (filename) Identifies the file to edit
653 * v : (version) File's last-modified time
654 * p : (pattern) Line number of pattern to edit
656 * Returns : JB_ERR_OK on success
657 * JB_ERR_MEMORY on out-of-memory
658 * JB_ERR_CGI_PARAMS if the CGI parameters are not
659 * specified or not valid.
661 *********************************************************************/
662 jb_err cgi_edit_actions_url_form(struct client_state *csp,
663 struct http_response *rsp,
664 const struct map *parameters)
666 struct map * exports;
668 struct editable_file * file;
669 struct file_line * cur_line;
670 unsigned line_number;
671 unsigned section_start_line_number = 0;
678 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
680 return cgi_error_disabled(csp, rsp);
683 err = get_number_param(csp, parameters, "p", &patternid);
689 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
692 /* No filename specified, can't read file, modified, or out of memory. */
693 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
696 cur_line = file->lines;
698 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
700 if (cur_line->type == FILE_LINE_ACTION)
702 section_start_line_number = line_number;
704 cur_line = cur_line->next;
707 if ( (cur_line == NULL)
708 || (line_number != patternid)
710 || (cur_line->type != FILE_LINE_URL))
712 /* Invalid "patternid" parameter */
713 edit_free_file(file);
714 return JB_ERR_CGI_PARAMS;
717 if (NULL == (exports = default_exports(csp, NULL)))
719 edit_free_file(file);
720 return JB_ERR_MEMORY;
723 err = map(exports, "f", 1, file->identifier, 1);
724 if (!err) err = map(exports, "v", 1, file->version_str, 1);
725 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
726 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
727 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
729 edit_free_file(file);
737 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
741 /*********************************************************************
743 * Function : cgi_edit_actions_add_url_form
745 * Description : CGI function that displays a form for
749 * 1 : csp = Current client state (buffers, headers, etc...)
750 * 2 : rsp = http_response data structure for output
751 * 3 : parameters = map of cgi parameters
754 * f : (filename) Identifies the file to edit
755 * v : (version) File's last-modified time
756 * s : (section) Line number of section to edit
758 * Returns : JB_ERR_OK on success
759 * JB_ERR_MEMORY on out-of-memory
760 * JB_ERR_CGI_PARAMS if the CGI parameters are not
761 * specified or not valid.
763 *********************************************************************/
764 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
765 struct http_response *rsp,
766 const struct map *parameters)
775 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
777 return cgi_error_disabled(csp, rsp);
780 if (NULL == (exports = default_exports(csp, NULL)))
782 return JB_ERR_MEMORY;
785 err = map_copy_parameter_html(exports, parameters, "f");
786 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
787 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
795 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
799 /*********************************************************************
801 * Function : cgi_edit_actions_remove_url_form
803 * Description : CGI function that displays a form for
807 * 1 : csp = Current client state (buffers, headers, etc...)
808 * 2 : rsp = http_response data structure for output
809 * 3 : parameters = map of cgi parameters
812 * f : (filename) Identifies the file to edit
813 * v : (version) File's last-modified time
814 * p : (pattern) Line number of pattern to edit
816 * Returns : JB_ERR_OK on success
817 * JB_ERR_MEMORY on out-of-memory
818 * JB_ERR_CGI_PARAMS if the CGI parameters are not
819 * specified or not valid.
821 *********************************************************************/
822 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
823 struct http_response *rsp,
824 const struct map *parameters)
826 struct map * exports;
828 struct editable_file * file;
829 struct file_line * cur_line;
830 unsigned line_number;
831 unsigned section_start_line_number = 0;
838 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
840 return cgi_error_disabled(csp, rsp);
843 err = get_number_param(csp, parameters, "p", &patternid);
849 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
852 /* No filename specified, can't read file, modified, or out of memory. */
853 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
856 cur_line = file->lines;
858 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
860 if (cur_line->type == FILE_LINE_ACTION)
862 section_start_line_number = line_number;
864 cur_line = cur_line->next;
867 if ( (cur_line == NULL)
868 || (line_number != patternid)
870 || (cur_line->type != FILE_LINE_URL))
872 /* Invalid "patternid" parameter */
873 edit_free_file(file);
874 return JB_ERR_CGI_PARAMS;
877 if (NULL == (exports = default_exports(csp, NULL)))
879 edit_free_file(file);
880 return JB_ERR_MEMORY;
883 err = map(exports, "f", 1, file->identifier, 1);
884 if (!err) err = map(exports, "v", 1, file->version_str, 1);
885 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
886 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
887 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
889 edit_free_file(file);
897 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
901 /*********************************************************************
903 * Function : edit_write_file
905 * Description : Write a complete file to disk.
908 * 1 : file = File to write.
910 * Returns : JB_ERR_OK on success
911 * JB_ERR_FILE on error writing to file.
912 * JB_ERR_MEMORY on out of memory
914 *********************************************************************/
915 jb_err edit_write_file(struct editable_file * file)
918 struct file_line * cur_line;
919 struct stat statbuf[1];
920 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
921 digits in time_t, assuming this is a 64-bit
922 machine, plus null terminator, plus one
926 assert(file->filename);
928 if (NULL == (fp = fopen(file->filename, "wb")))
933 cur_line = file->lines;
934 while (cur_line != NULL)
938 if (fputs(cur_line->raw, fp) < 0)
946 if (cur_line->prefix)
948 if (fputs(cur_line->prefix, fp) < 0)
954 if (cur_line->unprocessed)
957 if (NULL != strchr(cur_line->unprocessed, '#'))
959 /* Must quote '#' characters */
966 /* Count number of # characters, so we know length of output string */
967 src = cur_line->unprocessed;
968 while (NULL != (src = strchr(src, '#')))
975 /* Allocate new memory for string */
976 len = strlen(cur_line->unprocessed);
977 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
979 /* Uh oh, just trashed file! */
981 return JB_ERR_MEMORY;
984 /* Loop through string from end */
985 src = cur_line->unprocessed + len;
986 dest = str + len + numhash;
987 for ( ; len >= 0; len--)
989 if ((*dest-- = *src--) == '#')
993 assert(numhash >= 0);
996 assert(numhash == 0);
997 assert(src + 1 == cur_line->unprocessed);
998 assert(dest + 1 == str);
1000 if (fputs(str, fp) < 0)
1011 /* Can write without quoting '#' characters. */
1012 if (fputs(cur_line->unprocessed, fp) < 0)
1018 if (fputs(NEWLINE(file->newline), fp) < 0)
1026 /* FIXME: Write data from file->data->whatever */
1030 cur_line = cur_line->next;
1036 /* Update the version stamp in the file structure, since we just
1037 * wrote to the file & changed it's date.
1039 if (stat(file->filename, statbuf) < 0)
1041 /* Error, probably file not found. */
1044 file->version = (unsigned)statbuf->st_mtime;
1046 /* Correct file->version_str */
1047 freez(file->version_str);
1048 snprintf(version_buf, 22, "%u", file->version);
1049 version_buf[21] = '\0';
1050 file->version_str = strdup(version_buf);
1051 if (version_buf == NULL)
1053 return JB_ERR_MEMORY;
1060 /*********************************************************************
1062 * Function : edit_free_file
1064 * Description : Free a complete file in memory.
1067 * 1 : file = Data structure to free.
1071 *********************************************************************/
1072 void edit_free_file(struct editable_file * file)
1076 /* Silently ignore NULL pointer */
1080 edit_free_file_lines(file->lines);
1081 freez(file->filename);
1082 freez(file->identifier);
1083 freez(file->version_str);
1085 file->parse_error_text = NULL; /* Statically allocated */
1086 file->parse_error = NULL;
1092 /*********************************************************************
1094 * Function : edit_free_file_lines
1096 * Description : Free an entire linked list of file lines.
1099 * 1 : first_line = Data structure to free.
1103 *********************************************************************/
1104 static void edit_free_file_lines(struct file_line * first_line)
1106 struct file_line * next_line;
1108 while (first_line != NULL)
1110 next_line = first_line->next;
1111 first_line->next = NULL;
1112 freez(first_line->raw);
1113 freez(first_line->prefix);
1114 freez(first_line->unprocessed);
1115 switch(first_line->type)
1117 case 0: /* special case if memory zeroed */
1118 case FILE_LINE_UNPROCESSED:
1119 case FILE_LINE_BLANK:
1120 case FILE_LINE_ALIAS_HEADER:
1121 case FILE_LINE_SETTINGS_HEADER:
1122 case FILE_LINE_DESCRIPTION_HEADER:
1123 case FILE_LINE_DESCRIPTION_ENTRY:
1124 case FILE_LINE_ALIAS_ENTRY:
1126 /* No data is stored for these */
1129 case FILE_LINE_ACTION:
1130 free_action(first_line->data.action);
1133 case FILE_LINE_SETTINGS_ENTRY:
1134 freez(first_line->data.setting.name);
1135 freez(first_line->data.setting.svalue);
1138 /* Should never happen */
1142 first_line->type = 0; /* paranoia */
1144 first_line = next_line;
1149 /*********************************************************************
1151 * Function : match_actions_file_header_line
1153 * Description : Match an actions file {{header}} line
1156 * 1 : line = String from file
1157 * 2 : name = Header to match against
1159 * Returns : 0 iff they match.
1161 *********************************************************************/
1162 static int match_actions_file_header_line(const char * line, const char * name)
1170 if ((line[0] != '{') || (line[1] != '{'))
1176 /* Look for optional whitespace */
1177 while ( (*line == ' ') || (*line == '\t') )
1182 /* Look for the specified name (case-insensitive) */
1184 if (0 != strncmpic(line, name, len))
1190 /* Look for optional whitespace */
1191 while ( (*line == ' ') || (*line == '\t') )
1196 /* Look for "}}" and end of string*/
1197 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1207 /*********************************************************************
1209 * Function : match_actions_file_header_line
1211 * Description : Match an actions file {{header}} line
1214 * 1 : line = String from file. Must not start with
1215 * whitespace (else infinite loop!)
1216 * 2 : pname = Destination for name
1217 * 2 : pvalue = Destination for value
1219 * Returns : JB_ERR_OK on success
1220 * JB_ERR_MEMORY on out-of-memory
1221 * JB_ERR_PARSE if there's no "=" sign, or if there's
1222 * nothing before the "=" sign (but empty
1223 * values *after* the "=" sign are legal).
1225 *********************************************************************/
1226 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1228 const char * name_end;
1229 const char * value_start;
1235 assert(*line != ' ');
1236 assert(*line != '\t');
1241 value_start = strchr(line, '=');
1242 if ((value_start == NULL) || (value_start == line))
1244 return JB_ERR_PARSE;
1247 name_end = value_start - 1;
1249 /* Eat any whitespace before the '=' */
1250 while ((*name_end == ' ') || (*name_end == '\t'))
1253 * we already know we must have at least 1 non-ws char
1254 * at start of buf - no need to check
1259 name_len = name_end - line + 1; /* Length excluding \0 */
1260 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1262 return JB_ERR_MEMORY;
1264 strncpy(*pname, line, name_len);
1265 (*pname)[name_len] = '\0';
1267 /* Eat any the whitespace after the '=' */
1269 while ((*value_start == ' ') || (*value_start == '\t'))
1274 if (NULL == (*pvalue = strdup(value_start)))
1278 return JB_ERR_MEMORY;
1285 /*********************************************************************
1287 * Function : edit_parse_actions_file
1289 * Description : Parse an actions file in memory.
1291 * Passed linked list must have the "data" member
1292 * zeroed, and must contain valid "next" and
1293 * "unprocessed" fields. The "raw" and "prefix"
1294 * fields are ignored, and "type" is just overwritten.
1296 * Note that on error the file may have been
1300 * 1 : file = Actions file to be parsed in-place.
1302 * Returns : JB_ERR_OK on success
1303 * JB_ERR_MEMORY on out-of-memory
1304 * JB_ERR_PARSE on error
1306 *********************************************************************/
1307 jb_err edit_parse_actions_file(struct editable_file * file)
1309 struct file_line * cur_line;
1311 const char * text; /* Text from a line */
1312 char * name; /* For lines of the form name=value */
1313 char * value; /* For lines of the form name=value */
1314 struct action_alias * alias_list = NULL;
1315 jb_err err = JB_ERR_OK;
1317 /* alias_list contains the aliases defined in this file.
1318 * It might be better to use the "file_line.data" fields
1319 * in the relavent places instead.
1322 cur_line = file->lines;
1324 /* A note about blank line support: Blank lines should only
1325 * ever occur as the last line in the file. This function
1326 * is more forgiving than that - FILE_LINE_BLANK can occur
1330 /* Skip leading blanks. Should only happen if file is
1331 * empty (which is valid, but pointless).
1333 while ( (cur_line != NULL)
1334 && (cur_line->unprocessed[0] == '\0') )
1337 cur_line->type = FILE_LINE_BLANK;
1338 cur_line = cur_line->next;
1341 if ( (cur_line != NULL)
1342 && (cur_line->unprocessed[0] != '{') )
1344 /* File doesn't start with a header */
1345 file->parse_error = cur_line;
1346 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1347 return JB_ERR_PARSE;
1350 if ( (cur_line != NULL) && (0 ==
1351 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1353 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1355 cur_line = cur_line->next;
1356 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1358 if (cur_line->unprocessed[0])
1360 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1362 err = split_line_on_equals(cur_line->unprocessed,
1363 &cur_line->data.setting.name,
1364 &cur_line->data.setting.svalue);
1365 if (err == JB_ERR_MEMORY)
1369 else if (err != JB_ERR_OK)
1371 /* Line does not contain a name=value pair */
1372 file->parse_error = cur_line;
1373 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1374 return JB_ERR_PARSE;
1379 cur_line->type = FILE_LINE_BLANK;
1381 cur_line = cur_line->next;
1385 if ( (cur_line != NULL) && (0 ==
1386 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1388 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1390 cur_line = cur_line->next;
1391 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1393 if (cur_line->unprocessed[0])
1395 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1399 cur_line->type = FILE_LINE_BLANK;
1401 cur_line = cur_line->next;
1405 if ( (cur_line != NULL) && (0 ==
1406 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1408 cur_line->type = FILE_LINE_ALIAS_HEADER;
1410 cur_line = cur_line->next;
1411 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1413 if (cur_line->unprocessed[0])
1415 /* define an alias */
1416 struct action_alias * new_alias;
1418 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1420 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1421 if (err == JB_ERR_MEMORY)
1425 else if (err != JB_ERR_OK)
1427 /* Line does not contain a name=value pair */
1428 file->parse_error = cur_line;
1429 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1430 return JB_ERR_PARSE;
1433 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1438 free_alias_list(alias_list);
1439 return JB_ERR_MEMORY;
1442 err = get_actions(value, alias_list, new_alias->action);
1445 /* Invalid action or out of memory */
1449 free_alias_list(alias_list);
1450 if (err == JB_ERR_MEMORY)
1456 /* Line does not contain a name=value pair */
1457 file->parse_error = cur_line;
1458 file->parse_error_text = "This alias does not specify a valid set of actions.";
1459 return JB_ERR_PARSE;
1465 new_alias->name = name;
1468 new_alias->next = alias_list;
1469 alias_list = new_alias;
1473 cur_line->type = FILE_LINE_BLANK;
1475 cur_line = cur_line->next;
1479 /* Header done, process the main part of the file */
1480 while (cur_line != NULL)
1482 /* At this point, (cur_line->unprocessed[0] == '{') */
1483 assert(cur_line->unprocessed[0] == '{');
1484 text = cur_line->unprocessed + 1;
1485 len = strlen(text) - 1;
1486 if (text[len] != '}')
1488 /* No closing } on header */
1489 free_alias_list(alias_list);
1490 file->parse_error = cur_line;
1491 file->parse_error_text = "Headers starting with '{' must have a "
1492 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1493 "must close with two brackets ('}}').";
1494 return JB_ERR_PARSE;
1499 /* An invalid {{ header. */
1500 free_alias_list(alias_list);
1501 file->parse_error = cur_line;
1502 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1503 "Please remember that the system (two-bracket) headers must "
1504 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1505 "and must appear before any actions (one-bracket) headers. "
1506 "Also note that system headers may not be repeated.";
1507 return JB_ERR_PARSE;
1510 while ( (*text == ' ') || (*text == '\t') )
1516 && ( (text[len - 1] == ' ')
1517 || (text[len - 1] == '\t') ) )
1522 cur_line->type = FILE_LINE_ACTION;
1524 /* Remove {} and make copy */
1525 if (NULL == (value = (char *) malloc(len + 1)))
1528 free_alias_list(alias_list);
1529 return JB_ERR_MEMORY;
1531 strncpy(value, text, len);
1535 err = get_actions(value, alias_list, cur_line->data.action);
1538 /* Invalid action or out of memory */
1540 free_alias_list(alias_list);
1541 if (err == JB_ERR_MEMORY)
1547 /* Line does not contain a name=value pair */
1548 file->parse_error = cur_line;
1549 file->parse_error_text = "This header does not specify a valid set of actions.";
1550 return JB_ERR_PARSE;
1554 /* Done with string - it was clobbered anyway */
1557 /* Process next line */
1558 cur_line = cur_line->next;
1560 /* Loop processing URL patterns */
1561 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1563 if (cur_line->unprocessed[0])
1565 /* Could parse URL here, but this isn't currently needed */
1567 cur_line->type = FILE_LINE_URL;
1571 cur_line->type = FILE_LINE_BLANK;
1573 cur_line = cur_line->next;
1575 } /* End main while(cur_line != NULL) loop */
1577 free_alias_list(alias_list);
1583 /*********************************************************************
1585 * Function : edit_read_file_lines
1587 * Description : Read all the lines of a file into memory.
1588 * Handles whitespace, comments and line continuation.
1591 * 1 : fp = File to read from. On return, this will be
1592 * at EOF but it will not have been closed.
1593 * 2 : pfile = Destination for a linked list of file_lines.
1594 * Will be set to NULL on error.
1595 * 3 : newline = How to handle newlines.
1597 * Returns : JB_ERR_OK on success
1598 * JB_ERR_MEMORY on out-of-memory
1600 *********************************************************************/
1601 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1603 struct file_line * first_line; /* Keep for return value or to free */
1604 struct file_line * cur_line; /* Current line */
1605 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1613 cur_line = first_line = zalloc(sizeof(struct file_line));
1614 if (cur_line == NULL)
1616 return JB_ERR_MEMORY;
1619 cur_line->type = FILE_LINE_UNPROCESSED;
1621 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1624 /* Out of memory or empty file. */
1625 /* Note that empty file is not an error we propogate up */
1627 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1632 prev_line = cur_line;
1633 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1634 if (cur_line == NULL)
1637 edit_free_file_lines(first_line);
1638 return JB_ERR_MEMORY;
1641 cur_line->type = FILE_LINE_UNPROCESSED;
1643 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1644 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1647 edit_free_file_lines(first_line);
1648 return JB_ERR_MEMORY;
1652 while (rval != JB_ERR_FILE);
1656 /* We allocated one too many - free it */
1657 prev_line->next = NULL;
1660 *pfile = first_line;
1665 /*********************************************************************
1667 * Function : edit_read_file
1669 * Description : Read a complete file into memory.
1670 * Handles CGI parameter parsing. If requested, also
1671 * checks the file's modification timestamp.
1674 * 1 : csp = Current client state (buffers, headers, etc...)
1675 * 2 : parameters = map of cgi parameters.
1676 * 3 : require_version = true to check "ver" parameter.
1677 * 4 : suffix = File extension, e.g. ".action".
1678 * 5 : pfile = Destination for the file. Will be set
1682 * filename : The name of the file to read, without the
1683 * path or ".action" extension.
1684 * ver : (Only if require_version is nonzero)
1685 * Timestamp of the actions file. If wrong, this
1686 * function fails with JB_ERR_MODIFIED.
1688 * Returns : JB_ERR_OK on success
1689 * JB_ERR_MEMORY on out-of-memory
1690 * JB_ERR_CGI_PARAMS if "filename" was not specified
1692 * JB_ERR_FILE if the file cannot be opened or
1694 * JB_ERR_MODIFIED if version checking was requested and
1695 * failed - the file was modified outside
1696 * of this CGI editor instance.
1698 *********************************************************************/
1699 jb_err edit_read_file(struct client_state *csp,
1700 const struct map *parameters,
1701 int require_version,
1703 struct editable_file **pfile)
1705 struct file_line * lines;
1709 const char * identifier;
1710 struct editable_file * file;
1711 unsigned version = 0;
1712 struct stat statbuf[1];
1713 char version_buf[22];
1714 int newline = NEWLINE_UNKNOWN;
1722 err = get_file_name_param(csp, parameters, "f", suffix,
1723 &filename, &identifier);
1729 if (stat(filename, statbuf) < 0)
1731 /* Error, probably file not found. */
1735 version = (unsigned) statbuf->st_mtime;
1737 if (require_version)
1739 unsigned specified_version;
1740 err = get_number_param(csp, parameters, "v", &specified_version);
1747 if (version != specified_version)
1749 return JB_ERR_MODIFIED;
1753 if (NULL == (fp = fopen(filename,"rb")))
1759 err = edit_read_file_lines(fp, &lines, &newline);
1769 file = (struct editable_file *) zalloc(sizeof(*file));
1773 edit_free_file_lines(lines);
1777 file->lines = lines;
1778 file->newline = newline;
1779 file->filename = filename;
1780 file->version = version;
1781 file->identifier = url_encode(identifier);
1783 if (file->identifier == NULL)
1785 edit_free_file(file);
1786 return JB_ERR_MEMORY;
1789 /* Correct file->version_str */
1790 freez(file->version_str);
1791 snprintf(version_buf, 22, "%u", file->version);
1792 version_buf[21] = '\0';
1793 file->version_str = strdup(version_buf);
1794 if (version_buf == NULL)
1796 edit_free_file(file);
1797 return JB_ERR_MEMORY;
1805 /*********************************************************************
1807 * Function : edit_read_actions_file
1809 * Description : Read a complete actions file into memory.
1810 * Handles CGI parameter parsing. If requested, also
1811 * checks the file's modification timestamp.
1813 * If this function detects an error in the categories
1814 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1815 * then it handles it by filling in the specified
1816 * response structure and returning JB_ERR_FILE.
1819 * 1 : csp = Current client state (buffers, headers, etc...)
1820 * 2 : rsp = HTTP response. Only filled in on error.
1821 * 2 : parameters = map of cgi parameters.
1822 * 3 : require_version = true to check "ver" parameter.
1823 * 4 : pfile = Destination for the file. Will be set
1827 * filename : The name of the actions file to read, without the
1828 * path or ".action" extension.
1829 * ver : (Only if require_version is nonzero)
1830 * Timestamp of the actions file. If wrong, this
1831 * function fails with JB_ERR_MODIFIED.
1833 * Returns : JB_ERR_OK on success
1834 * JB_ERR_MEMORY on out-of-memory
1835 * JB_ERR_CGI_PARAMS if "filename" was not specified
1837 * JB_ERR_FILE if the file does not contain valid data,
1838 * or if file cannot be opened or
1839 * contains no data, or if version
1840 * checking was requested and failed.
1842 *********************************************************************/
1843 jb_err edit_read_actions_file(struct client_state *csp,
1844 struct http_response *rsp,
1845 const struct map *parameters,
1846 int require_version,
1847 struct editable_file **pfile)
1850 struct editable_file *file;
1858 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1861 /* Try to handle if possible */
1862 if (err == JB_ERR_FILE)
1864 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1866 else if (err == JB_ERR_MODIFIED)
1868 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1870 if (err == JB_ERR_OK)
1873 * Signal to higher-level CGI code that there was a problem but we
1874 * handled it, they should just return JB_ERR_OK.
1881 err = edit_parse_actions_file(file);
1884 if (err == JB_ERR_PARSE)
1886 err = cgi_error_parse(csp, rsp, file);
1887 if (err == JB_ERR_OK)
1890 * Signal to higher-level CGI code that there was a problem but we
1891 * handled it, they should just return JB_ERR_OK.
1896 edit_free_file(file);
1905 /*********************************************************************
1907 * Function : get_file_name_param
1909 * Description : Get the name of the file to edit from the parameters
1910 * passed to a CGI function. This function handles
1911 * security checks such as blocking urls containing
1912 * "/" or ".", prepending the config file directory,
1913 * and adding the specified suffix.
1915 * (This is an essential security check, otherwise
1916 * users may be able to pass "../../../etc/passwd"
1917 * and overwrite the password file [linux], "prn:"
1918 * and print random data [Windows], etc...)
1920 * This function only allows filenames contining the
1921 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1922 * That's probably too restrictive but at least it's
1926 * 1 : csp = Current client state (buffers, headers, etc...)
1927 * 2 : parameters = map of cgi parameters
1928 * 3 : param_name = The name of the parameter to read
1929 * 4 : suffix = File extension, e.g. ".actions"
1930 * 5 : pfilename = destination for full filename. Caller
1931 * free()s. Set to NULL on error.
1932 * 6 : pparam = destination for partial filename,
1933 * suitable for use in another URL. Allocated as part
1934 * of the map "parameters", so don't free it.
1935 * Set to NULL if not specified.
1937 * Returns : JB_ERR_OK on success
1938 * JB_ERR_MEMORY on out-of-memory
1939 * JB_ERR_CGI_PARAMS if "filename" was not specified
1942 *********************************************************************/
1943 static jb_err get_file_name_param(struct client_state *csp,
1944 const struct map *parameters,
1945 const char *param_name,
1948 const char **pparam)
1966 param = lookup(parameters, param_name);
1969 return JB_ERR_CGI_PARAMS;
1974 len = strlen(param);
1975 if (len >= FILENAME_MAX)
1978 return JB_ERR_CGI_PARAMS;
1981 /* Check every character to see if it's legal */
1983 while ((ch = *s++) != '\0')
1985 if ( ((ch < 'A') || (ch > 'Z'))
1986 && ((ch < 'a') || (ch > 'z'))
1987 && ((ch < '0') || (ch > '9'))
1991 /* Probable hack attempt. */
1992 return JB_ERR_CGI_PARAMS;
1996 /* Append extension */
1997 name = malloc(len + strlen(suffix) + 1);
2000 return JB_ERR_MEMORY;
2002 strcpy(name, param);
2003 strcpy(name + len, suffix);
2006 fullpath = make_path(csp->config->confdir, name);
2009 if (fullpath == NULL)
2011 return JB_ERR_MEMORY;
2015 *pfilename = fullpath;
2021 /*********************************************************************
2023 * Function : get_url_spec_param
2025 * Description : Get a URL pattern from the parameters
2026 * passed to a CGI function. Removes leading/trailing
2027 * spaces and validates it.
2030 * 1 : csp = Current client state (buffers, headers, etc...)
2031 * 2 : parameters = map of cgi parameters
2032 * 3 : name = Name of CGI parameter to read
2033 * 4 : pvalue = destination for value. Will be malloc()'d.
2034 * Set to NULL on error.
2036 * Returns : JB_ERR_OK on success
2037 * JB_ERR_MEMORY on out-of-memory
2038 * JB_ERR_CGI_PARAMS if the parameter was not specified
2041 *********************************************************************/
2042 static jb_err get_url_spec_param(struct client_state *csp,
2043 const struct map *parameters,
2047 const char *orig_param;
2050 struct url_spec compiled[1];
2060 orig_param = lookup(parameters, name);
2063 return JB_ERR_CGI_PARAMS;
2066 /* Copy and trim whitespace */
2067 param = strdup(orig_param);
2070 return JB_ERR_MEMORY;
2074 /* Must be non-empty, and can't allow 1st character to be '{' */
2075 if (param[0] == '\0' || param[0] == '{')
2078 return JB_ERR_CGI_PARAMS;
2081 /* Check for embedded newlines */
2082 for (s = param; *s != '\0'; s++)
2084 if ((*s == '\r') || (*s == '\n'))
2087 return JB_ERR_CGI_PARAMS;
2091 /* Check that regex is valid */
2096 return JB_ERR_MEMORY;
2098 err = create_url_spec(compiled, s);
2103 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2105 free_url_spec(compiled);
2107 if (param[strlen(param) - 1] == '\\')
2110 * Must protect trailing '\\' from becoming line continuation character.
2111 * Two methods: 1) If it's a domain only, add a trailing '/'.
2112 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2114 if (strchr(param, '/') == NULL)
2116 err = string_append(¶m, "/");
2120 err = string_append(¶m, "(?:)");
2127 /* Check that the modified regex is valid */
2132 return JB_ERR_MEMORY;
2134 err = create_url_spec(compiled, s);
2139 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2141 free_url_spec(compiled);
2148 /*********************************************************************
2150 * Function : map_radio
2152 * Description : Map a set of radio button values. E.g. if you have
2153 * 3 radio buttons, declare them as:
2154 * <option type="radio" name="xyz" @xyz-a@>
2155 * <option type="radio" name="xyz" @xyz-b@>
2156 * <option type="radio" name="xyz" @xyz-c@>
2157 * Then map one of the @xyz-?@ variables to "checked"
2158 * and all the others to empty by calling:
2159 * map_radio(exports, "xyz", "abc", sel)
2160 * Where 'sel' is 'a', 'b', or 'c'.
2163 * 1 : exports = Exports map to modify.
2164 * 2 : optionname = name for map
2165 * 3 : values = null-terminated list of values;
2166 * 4 : value = Selected value.
2168 * CGI Parameters : None
2170 * Returns : JB_ERR_OK on success
2171 * JB_ERR_MEMORY on out-of-memory
2173 *********************************************************************/
2174 static jb_err map_radio(struct map * exports,
2175 const char * optionname,
2176 const char * values,
2188 len = strlen(optionname);
2189 buf = malloc(len + 3);
2192 return JB_ERR_MEMORY;
2195 strcpy(buf, optionname);
2200 while ((c = *values++) != '\0')
2205 if (map(exports, buf, 1, "", 1))
2207 return JB_ERR_MEMORY;
2213 return map(exports, buf, 0, "checked", 1);
2217 /*********************************************************************
2219 * Function : cgi_error_modified
2221 * Description : CGI function that is called when a file is modified
2222 * outside the CGI editor.
2225 * 1 : csp = Current client state (buffers, headers, etc...)
2226 * 2 : rsp = http_response data structure for output
2227 * 3 : filename = The file that was modified.
2229 * CGI Parameters : none
2231 * Returns : JB_ERR_OK on success
2232 * JB_ERR_MEMORY on out-of-memory error.
2234 *********************************************************************/
2235 jb_err cgi_error_modified(struct client_state *csp,
2236 struct http_response *rsp,
2237 const char *filename)
2239 struct map *exports;
2246 if (NULL == (exports = default_exports(csp, NULL)))
2248 return JB_ERR_MEMORY;
2251 err = map(exports, "f", 1, html_encode(filename), 0);
2258 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2262 /*********************************************************************
2264 * Function : cgi_error_parse
2266 * Description : CGI function that is called when a file cannot
2267 * be parsed by the CGI editor.
2270 * 1 : csp = Current client state (buffers, headers, etc...)
2271 * 2 : rsp = http_response data structure for output
2272 * 3 : file = The file that was modified.
2274 * CGI Parameters : none
2276 * Returns : JB_ERR_OK on success
2277 * JB_ERR_MEMORY on out-of-memory error.
2279 *********************************************************************/
2280 jb_err cgi_error_parse(struct client_state *csp,
2281 struct http_response *rsp,
2282 struct editable_file *file)
2284 struct map *exports;
2286 struct file_line *cur_line;
2292 if (NULL == (exports = default_exports(csp, NULL)))
2294 return JB_ERR_MEMORY;
2297 err = map(exports, "f", 1, file->identifier, 1);
2298 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2300 cur_line = file->parse_error;
2303 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2304 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2312 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2316 /*********************************************************************
2318 * Function : cgi_error_file
2320 * Description : CGI function that is called when a file cannot be
2321 * opened by the CGI editor.
2324 * 1 : csp = Current client state (buffers, headers, etc...)
2325 * 2 : rsp = http_response data structure for output
2326 * 3 : filename = The file that was modified.
2328 * CGI Parameters : none
2330 * Returns : JB_ERR_OK on success
2331 * JB_ERR_MEMORY on out-of-memory error.
2333 *********************************************************************/
2334 jb_err cgi_error_file(struct client_state *csp,
2335 struct http_response *rsp,
2336 const char *filename)
2338 struct map *exports;
2345 if (NULL == (exports = default_exports(csp, NULL)))
2347 return JB_ERR_MEMORY;
2350 err = map(exports, "f", 1, html_encode(filename), 0);
2357 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2361 /*********************************************************************
2363 * Function : cgi_error_file
2365 * Description : CGI function that is called when a file cannot be
2366 * opened for writing by the CGI editor.
2369 * 1 : csp = Current client state (buffers, headers, etc...)
2370 * 2 : rsp = http_response data structure for output
2371 * 3 : filename = The file that we can't write to
2373 * CGI Parameters : none
2375 * Returns : JB_ERR_OK on success
2376 * JB_ERR_MEMORY on out-of-memory error.
2378 *********************************************************************/
2379 jb_err cgi_error_file_read_only(struct client_state *csp,
2380 struct http_response *rsp,
2381 const char *filename)
2383 struct map *exports;
2390 if (NULL == (exports = default_exports(csp, NULL)))
2392 return JB_ERR_MEMORY;
2395 err = map(exports, "f", 1, html_encode(filename), 0);
2402 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2406 /*********************************************************************
2408 * Function : cgi_error_disabled
2410 * Description : CGI function that is called if the actions editor
2411 * is called although it's disabled in config
2414 * 1 : csp = Current client state (buffers, headers, etc...)
2415 * 2 : rsp = http_response data structure for output
2417 * CGI Parameters : none
2419 * Returns : JB_ERR_OK on success
2420 * JB_ERR_MEMORY on out-of-memory error.
2422 *********************************************************************/
2423 jb_err cgi_error_disabled(struct client_state *csp,
2424 struct http_response *rsp)
2426 struct map *exports;
2431 if (NULL == (exports = default_exports(csp, NULL)))
2433 return JB_ERR_MEMORY;
2436 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2440 /*********************************************************************
2442 * Function : cgi_edit_actions
2444 * Description : CGI function that allows the user to choose which
2445 * actions file to edit.
2448 * 1 : csp = Current client state (buffers, headers, etc...)
2449 * 2 : rsp = http_response data structure for output
2450 * 3 : parameters = map of cgi parameters
2452 * CGI Parameters : None
2454 * Returns : JB_ERR_OK on success
2455 * JB_ERR_MEMORY on out-of-memory error
2457 *********************************************************************/
2458 jb_err cgi_edit_actions(struct client_state *csp,
2459 struct http_response *rsp,
2460 const struct map *parameters)
2463 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2465 return cgi_error_disabled(csp, rsp);
2468 /* FIXME: Incomplete */
2470 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2475 /*********************************************************************
2477 * Function : cgi_edit_actions_list
2479 * Description : CGI function that edits the actions list.
2480 * FIXME: This function shouldn't FATAL ever.
2481 * FIXME: This function doesn't check the retval of map()
2483 * 1 : csp = Current client state (buffers, headers, etc...)
2484 * 2 : rsp = http_response data structure for output
2485 * 3 : parameters = map of cgi parameters
2487 * CGI Parameters : filename
2489 * Returns : JB_ERR_OK on success
2490 * JB_ERR_MEMORY on out-of-memory
2491 * JB_ERR_FILE if the file cannot be opened or
2493 * JB_ERR_CGI_PARAMS if "filename" was not specified
2496 *********************************************************************/
2497 jb_err cgi_edit_actions_list(struct client_state *csp,
2498 struct http_response *rsp,
2499 const struct map *parameters)
2501 char * section_template;
2502 char * url_template;
2507 struct map * exports;
2508 struct map * section_exports;
2509 struct map * url_exports;
2510 struct editable_file * file;
2511 struct file_line * cur_line;
2512 unsigned line_number = 0;
2513 unsigned prev_section_line_number = ((unsigned) (-1));
2515 struct file_list * fl;
2516 struct url_actions * b;
2517 char * buttons = NULL;
2520 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2522 return cgi_error_disabled(csp, rsp);
2525 if (NULL == (exports = default_exports(csp, NULL)))
2527 return JB_ERR_MEMORY;
2530 /* Load actions file */
2531 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2534 /* No filename specified, can't read file, or out of memory. */
2535 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2538 /* Find start of actions in file */
2539 cur_line = file->lines;
2541 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2543 cur_line = cur_line->next;
2548 * Conventional actions files should have a match all block
2550 * cur_line = {...global actions...}
2551 * cur_line->next = /
2552 * cur_line->next->next = {...actions...} or EOF
2554 if ( (cur_line != NULL)
2555 && (cur_line->type == FILE_LINE_ACTION)
2556 && (cur_line->next != NULL)
2557 && (cur_line->next->type == FILE_LINE_URL)
2558 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2559 && ( (cur_line->next->next == NULL)
2560 || (cur_line->next->next->type != FILE_LINE_URL)
2564 * Generate string with buttons to set actions for "/" to
2565 * any predefined set of actions (named standard.*, probably
2566 * residing in standard.action).
2569 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2572 edit_free_file(file);
2574 if (err == JB_ERR_FILE)
2576 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2581 err = template_fill(§ion_template, exports);
2584 edit_free_file(file);
2589 buttons = strdup("");
2590 for (i = 0; i < MAX_ACTION_FILES; i++)
2592 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2594 for (b = b->next; NULL != b; b = b->next)
2596 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2598 if (err || (NULL == (section_exports = new_map())))
2601 free(section_template);
2602 edit_free_file(file);
2604 return JB_ERR_MEMORY;
2607 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2609 if (err || (NULL == (s = strdup(section_template))))
2611 free_map(section_exports);
2613 free(section_template);
2614 edit_free_file(file);
2616 return JB_ERR_MEMORY;
2619 if (!err) err = template_fill(&s, section_exports);
2620 free_map(section_exports);
2621 if (!err) err = string_join(&buttons, s);
2626 freez(section_template);
2627 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2630 * Conventional actions file, supply extra editing help.
2631 * (e.g. don't allow them to make it an unconventional one).
2633 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2635 snprintf(buf, 150, "%d", line_number);
2636 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2637 snprintf(buf, 150, "%d", line_number + 2);
2638 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2639 if (!err) err = map(exports, "all-urls-actions", 1,
2640 actions_to_html(csp, cur_line->data.action), 0);
2642 /* Skip the 2 lines */
2643 cur_line = cur_line->next->next;
2647 * Note that prev_section_line_number is NOT set here.
2648 * This is deliberate and not a bug. It stops a "Move up"
2649 * option appearing on the next section. Clicking "Move
2650 * up" would make the actions file unconventional, which
2651 * we don't want, so we hide this option.
2657 * Non-standard actions file - does not begin with
2658 * the "All URLs" section.
2660 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2663 /* Set up global exports */
2665 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2666 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2668 /* Discourage private additions to default.action */
2670 if (!err) err = map_conditional(exports, "default-action",
2671 (strcmp("default", lookup(parameters, "f")) == 0));
2674 edit_free_file(file);
2679 /* Should do all global exports above this point */
2681 /* Load templates */
2683 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2686 edit_free_file(file);
2688 if (err == JB_ERR_FILE)
2690 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2695 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2698 free(section_template);
2699 edit_free_file(file);
2701 if (err == JB_ERR_FILE)
2703 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2708 err = template_fill(§ion_template, exports);
2712 edit_free_file(file);
2718 err = template_fill(&url_template, exports);
2721 free(section_template);
2722 edit_free_file(file);
2727 if (NULL == (sections = strdup("")))
2729 free(section_template);
2731 edit_free_file(file);
2733 return JB_ERR_MEMORY;
2736 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2738 if (NULL == (section_exports = new_map()))
2741 free(section_template);
2743 edit_free_file(file);
2745 return JB_ERR_MEMORY;
2748 snprintf(buf, 150, "%d", line_number);
2749 err = map(section_exports, "s", 1, buf, 1);
2750 if (!err) err = map(section_exports, "actions", 1,
2751 actions_to_html(csp, cur_line->data.action), 0);
2754 && (cur_line->next != NULL)
2755 && (cur_line->next->type == FILE_LINE_URL))
2757 /* This section contains at least one URL, don't allow delete */
2758 err = map_block_killer(section_exports, "empty-section");
2762 if (!err) err = map_block_keep(section_exports, "empty-section");
2765 if (prev_section_line_number != ((unsigned)(-1)))
2767 /* Not last section */
2768 snprintf(buf, 150, "%d", prev_section_line_number);
2769 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2770 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2775 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2777 prev_section_line_number = line_number;
2782 free(section_template);
2784 edit_free_file(file);
2786 free_map(section_exports);
2790 /* Should do all section-specific exports above this point */
2792 if (NULL == (urls = strdup("")))
2795 free(section_template);
2797 edit_free_file(file);
2799 free_map(section_exports);
2800 return JB_ERR_MEMORY;
2805 cur_line = cur_line->next;
2808 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2810 if (NULL == (url_exports = new_map()))
2814 free(section_template);
2816 edit_free_file(file);
2818 free_map(section_exports);
2819 return JB_ERR_MEMORY;
2822 snprintf(buf, 150, "%d", line_number);
2823 err = map(url_exports, "p", 1, buf, 1);
2825 snprintf(buf, 150, "%d", url_1_2);
2826 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2828 if (!err) err = map(url_exports, "url-html", 1,
2829 html_encode(cur_line->unprocessed), 0);
2830 if (!err) err = map(url_exports, "url", 1,
2831 url_encode(cur_line->unprocessed), 0);
2837 free(section_template);
2839 edit_free_file(file);
2841 free_map(section_exports);
2842 free_map(url_exports);
2846 if (NULL == (s = strdup(url_template)))
2850 free(section_template);
2852 edit_free_file(file);
2854 free_map(section_exports);
2855 free_map(url_exports);
2856 return JB_ERR_MEMORY;
2859 err = template_fill(&s, section_exports);
2860 if (!err) err = template_fill(&s, url_exports);
2861 if (!err) err = string_append(&urls, s);
2863 free_map(url_exports);
2870 free(section_template);
2872 edit_free_file(file);
2874 free_map(section_exports);
2878 url_1_2 = 3 - url_1_2;
2880 cur_line = cur_line->next;
2884 err = map(section_exports, "urls", 1, urls, 0);
2886 /* Could also do section-specific exports here, but it wouldn't be as fast */
2888 snprintf(buf, 150, "%d", line_number);
2889 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2891 if ( (cur_line != NULL)
2892 && (cur_line->type == FILE_LINE_ACTION))
2894 /* Not last section */
2895 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2900 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2906 free(section_template);
2908 edit_free_file(file);
2910 free_map(section_exports);
2914 if (NULL == (s = strdup(section_template)))
2917 free(section_template);
2919 edit_free_file(file);
2921 free_map(section_exports);
2922 return JB_ERR_MEMORY;
2925 err = template_fill(&s, section_exports);
2926 if (!err) err = string_append(§ions, s);
2929 free_map(section_exports);
2934 free(section_template);
2936 edit_free_file(file);
2942 edit_free_file(file);
2943 free(section_template);
2946 err = map(exports, "sections", 1, sections, 0);
2953 /* Could also do global exports here, but it wouldn't be as fast */
2955 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2959 /*********************************************************************
2961 * Function : cgi_edit_actions_for_url
2963 * Description : CGI function that edits the Actions list.
2966 * 1 : csp = Current client state (buffers, headers, etc...)
2967 * 2 : rsp = http_response data structure for output
2968 * 3 : parameters = map of cgi parameters
2970 * CGI Parameters : None
2972 * Returns : JB_ERR_OK on success
2973 * JB_ERR_MEMORY on out-of-memory
2974 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2975 * specified or not valid.
2977 *********************************************************************/
2978 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2979 struct http_response *rsp,
2980 const struct map *parameters)
2982 struct map * exports;
2984 struct editable_file * file;
2985 struct file_line * cur_line;
2986 unsigned line_number;
2988 struct file_list *filter_file;
2989 struct re_filterfile_spec *filter_group;
2991 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2993 return cgi_error_disabled(csp, rsp);
2996 err = get_number_param(csp, parameters, "s", §ionid);
3002 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3005 /* No filename specified, can't read file, modified, or out of memory. */
3006 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3009 cur_line = file->lines;
3011 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3013 cur_line = cur_line->next;
3016 if ( (cur_line == NULL)
3017 || (line_number != sectionid)
3019 || (cur_line->type != FILE_LINE_ACTION))
3021 /* Invalid "sectionid" parameter */
3022 edit_free_file(file);
3023 return JB_ERR_CGI_PARAMS;
3026 if (NULL == (exports = default_exports(csp, NULL)))
3028 edit_free_file(file);
3029 return JB_ERR_MEMORY;
3032 err = map(exports, "f", 1, file->identifier, 1);
3033 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3034 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3036 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3038 filter_file = csp->rlist;
3039 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
3041 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
3045 edit_free_file(file);
3050 if (filter_group == NULL)
3052 err = map(exports, "filter-params", 1, "", 1);
3056 /* We have some entries in the filter list */
3059 char * filter_template;
3061 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3064 edit_free_file(file);
3066 if (err == JB_ERR_FILE)
3068 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3073 err = template_fill(&filter_template, exports);
3075 result = strdup("");
3077 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3079 char current_mode = 'x';
3080 struct list_entry *filter_name;
3082 struct map *line_exports;
3085 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
3086 while ((filter_name != NULL)
3087 && (0 != strcmp(filter_group->name, filter_name->str)))
3089 filter_name = filter_name->next;
3092 if (filter_name != NULL)
3098 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
3099 while ((filter_name != NULL)
3100 && (0 != strcmp(filter_group->name, filter_name->str)))
3102 filter_name = filter_name->next;
3104 if (filter_name != NULL)
3110 /* Generate a unique serial number */
3111 snprintf(number, sizeof(number), "%x", index++);
3112 number[sizeof(number) - 1] = '\0';
3114 line_exports = new_map();
3115 if (line_exports == NULL)
3117 err = JB_ERR_MEMORY;
3122 if (!err) err = map(line_exports, "index", 1, number, 1);
3123 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3124 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3125 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3130 this_line = strdup(filter_template);
3131 if (this_line == NULL) err = JB_ERR_MEMORY;
3133 if (!err) err = template_fill(&this_line, line_exports);
3134 string_join(&result, this_line);
3136 free_map(line_exports);
3140 freez(filter_template);
3144 err = map(exports, "filter-params", 1, result, 0);
3152 if (!err) err = map_radio(exports, "filter-all", "nx",
3153 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3155 edit_free_file(file);
3163 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3167 /*********************************************************************
3169 * Function : cgi_edit_actions_submit
3171 * Description : CGI function that actually edits the Actions list.
3174 * 1 : csp = Current client state (buffers, headers, etc...)
3175 * 2 : rsp = http_response data structure for output
3176 * 3 : parameters = map of cgi parameters
3178 * CGI Parameters : None
3180 * Returns : JB_ERR_OK on success
3181 * JB_ERR_MEMORY on out-of-memory
3182 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3183 * specified or not valid.
3185 *********************************************************************/
3186 jb_err cgi_edit_actions_submit(struct client_state *csp,
3187 struct http_response *rsp,
3188 const struct map *parameters)
3194 struct editable_file * file;
3195 struct file_line * cur_line;
3196 unsigned line_number;
3200 const char * action_set_name;
3202 struct file_list * fl;
3203 struct url_actions * b;
3205 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3207 return cgi_error_disabled(csp, rsp);
3210 err = get_number_param(csp, parameters, "s", §ionid);
3216 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3219 /* No filename specified, can't read file, modified, or out of memory. */
3220 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3223 cur_line = file->lines;
3225 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3227 cur_line = cur_line->next;
3230 if ( (cur_line == NULL)
3231 || (line_number != sectionid)
3233 || (cur_line->type != FILE_LINE_ACTION))
3235 /* Invalid "sectionid" parameter */
3236 edit_free_file(file);
3237 return JB_ERR_CGI_PARAMS;
3240 get_string_param(parameters, "p", &action_set_name);
3241 if (action_set_name != NULL)
3243 for (index = 0; index < MAX_ACTION_FILES; index++)
3245 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3247 for (b = b->next; NULL != b; b = b->next)
3249 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3251 copy_action(cur_line->data.action, b->action);
3257 edit_free_file(file);
3258 return JB_ERR_CGI_PARAMS;
3264 err = actions_from_radio(parameters, cur_line->data.action);
3270 edit_free_file(file);
3274 ch = get_char_param(parameters, "filter_all");
3277 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3278 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3279 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3283 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3286 for (index = 0; !err; index++)
3293 /* Generate the keys */
3294 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3295 key_value[sizeof(key_value) - 1] = '\0';
3296 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3297 key_name[sizeof(key_name) - 1] = '\0';
3299 err = get_string_param(parameters, key_name, &name);
3308 value = get_char_param(parameters, key_value);
3311 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3312 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3313 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3315 else if (value == 'N')
3317 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3318 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3320 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3321 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3324 else if (value == 'X')
3326 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3327 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3334 edit_free_file(file);
3338 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3341 edit_free_file(file);
3342 return JB_ERR_MEMORY;
3345 len = strlen(actiontext);
3349 * Empty action - must special-case this.
3350 * Simply setting len to 1 is sufficient...
3355 if (NULL == (newtext = malloc(len + 2)))
3359 edit_free_file(file);
3360 return JB_ERR_MEMORY;
3362 strcpy(newtext, actiontext);
3366 newtext[len + 1] = '\0';
3368 freez(cur_line->raw);
3369 freez(cur_line->unprocessed);
3370 cur_line->unprocessed = newtext;
3372 err = edit_write_file(file);
3375 /* Error writing file */
3376 if (err == JB_ERR_FILE)
3378 /* Read-only file. */
3379 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3381 edit_free_file(file);
3385 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3386 (long) time(NULL), file->identifier, sectionid);
3388 edit_free_file(file);
3390 return cgi_redirect(rsp, target);
3394 /*********************************************************************
3396 * Function : cgi_edit_actions_url
3398 * Description : CGI function that actually edits a URL pattern in
3402 * 1 : csp = Current client state (buffers, headers, etc...)
3403 * 2 : rsp = http_response data structure for output
3404 * 3 : parameters = map of cgi parameters
3407 * filename : Identifies the file to edit
3408 * ver : File's last-modified time
3409 * section : Line number of section to edit
3410 * pattern : Line number of pattern to edit
3411 * newval : New value for pattern
3413 * Returns : JB_ERR_OK on success
3414 * JB_ERR_MEMORY on out-of-memory
3415 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3416 * specified or not valid.
3418 *********************************************************************/
3419 jb_err cgi_edit_actions_url(struct client_state *csp,
3420 struct http_response *rsp,
3421 const struct map *parameters)
3425 struct editable_file * file;
3426 struct file_line * cur_line;
3427 unsigned line_number;
3428 unsigned section_start_line_number = 0;
3436 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3438 return cgi_error_disabled(csp, rsp);
3441 err = get_number_param(csp, parameters, "p", &patternid);
3448 return JB_ERR_CGI_PARAMS;
3451 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3457 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3460 /* No filename specified, can't read file, modified, or out of memory. */
3462 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3466 cur_line = file->lines;
3468 while ((cur_line != NULL) && (line_number < patternid))
3470 if (cur_line->type == FILE_LINE_ACTION)
3472 section_start_line_number = line_number;
3474 cur_line = cur_line->next;
3478 if ( (cur_line == NULL)
3479 || (cur_line->type != FILE_LINE_URL))
3481 /* Invalid "patternid" parameter */
3483 edit_free_file(file);
3484 return JB_ERR_CGI_PARAMS;
3487 /* At this point, the line to edit is in cur_line */
3489 freez(cur_line->raw);
3490 freez(cur_line->unprocessed);
3491 cur_line->unprocessed = new_pattern;
3493 err = edit_write_file(file);
3496 /* Error writing file */
3497 if (err == JB_ERR_FILE)
3499 /* Read-only file. */
3500 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3502 edit_free_file(file);
3506 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3507 (long) time(NULL), file->identifier, section_start_line_number);
3509 edit_free_file(file);
3511 return cgi_redirect(rsp, target);
3515 /*********************************************************************
3517 * Function : cgi_edit_actions_add_url
3519 * Description : CGI function that actually adds a URL pattern to
3523 * 1 : csp = Current client state (buffers, headers, etc...)
3524 * 2 : rsp = http_response data structure for output
3525 * 3 : parameters = map of cgi parameters
3528 * filename : Identifies the file to edit
3529 * ver : File's last-modified time
3530 * section : Line number of section to edit
3531 * newval : New pattern
3533 * Returns : JB_ERR_OK on success
3534 * JB_ERR_MEMORY on out-of-memory
3535 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3536 * specified or not valid.
3538 *********************************************************************/
3539 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3540 struct http_response *rsp,
3541 const struct map *parameters)
3545 struct file_line * new_line;
3546 struct editable_file * file;
3547 struct file_line * cur_line;
3548 unsigned line_number;
3552 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3554 return cgi_error_disabled(csp, rsp);
3557 err = get_number_param(csp, parameters, "s", §ionid);
3564 return JB_ERR_CGI_PARAMS;
3567 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3573 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3576 /* No filename specified, can't read file, modified, or out of memory. */
3578 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3582 cur_line = file->lines;
3584 while ((cur_line != NULL) && (line_number < sectionid))
3586 cur_line = cur_line->next;
3590 if ( (cur_line == NULL)
3591 || (cur_line->type != FILE_LINE_ACTION))
3593 /* Invalid "sectionid" parameter */
3595 edit_free_file(file);
3596 return JB_ERR_CGI_PARAMS;
3599 /* At this point, the section header is in cur_line - add after this. */
3601 /* Allocate the new line */
3602 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3603 if (new_line == NULL)
3606 edit_free_file(file);
3607 return JB_ERR_MEMORY;
3610 /* Fill in the data members of the new line */
3611 new_line->raw = NULL;
3612 new_line->prefix = NULL;
3613 new_line->unprocessed = new_pattern;
3614 new_line->type = FILE_LINE_URL;
3616 /* Link new_line into the list, after cur_line */
3617 new_line->next = cur_line->next;
3618 cur_line->next = new_line;
3620 /* Done making changes, now commit */
3622 err = edit_write_file(file);
3625 /* Error writing file */
3626 if (err == JB_ERR_FILE)
3628 /* Read-only file. */
3629 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3631 edit_free_file(file);
3635 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3636 (long) time(NULL), file->identifier, sectionid);
3638 edit_free_file(file);
3640 return cgi_redirect(rsp, target);
3644 /*********************************************************************
3646 * Function : cgi_edit_actions_remove_url
3648 * Description : CGI function that actually removes a URL pattern from
3652 * 1 : csp = Current client state (buffers, headers, etc...)
3653 * 2 : rsp = http_response data structure for output
3654 * 3 : parameters = map of cgi parameters
3657 * f : (filename) Identifies the file to edit
3658 * v : (version) File's last-modified time
3659 * p : (pattern) Line number of pattern to remove
3661 * Returns : JB_ERR_OK on success
3662 * JB_ERR_MEMORY on out-of-memory
3663 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3664 * specified or not valid.
3666 *********************************************************************/
3667 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3668 struct http_response *rsp,
3669 const struct map *parameters)
3672 struct editable_file * file;
3673 struct file_line * cur_line;
3674 struct file_line * prev_line;
3675 unsigned line_number;
3676 unsigned section_start_line_number = 0;
3680 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3682 return cgi_error_disabled(csp, rsp);
3685 err = get_number_param(csp, parameters, "p", &patternid);
3691 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3694 /* No filename specified, can't read file, modified, or out of memory. */
3695 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3700 cur_line = file->lines;
3702 while ((cur_line != NULL) && (line_number < patternid))
3704 if (cur_line->type == FILE_LINE_ACTION)
3706 section_start_line_number = line_number;
3708 prev_line = cur_line;
3709 cur_line = cur_line->next;
3713 if ( (cur_line == NULL)
3714 || (prev_line == NULL)
3715 || (cur_line->type != FILE_LINE_URL))
3717 /* Invalid "patternid" parameter */
3718 edit_free_file(file);
3719 return JB_ERR_CGI_PARAMS;
3722 /* At this point, the line to remove is in cur_line, and the previous
3723 * one is in prev_line
3726 /* Unlink cur_line */
3727 prev_line->next = cur_line->next;
3728 cur_line->next = NULL;
3731 edit_free_file_lines(cur_line);
3733 err = edit_write_file(file);
3736 /* Error writing file */
3737 if (err == JB_ERR_FILE)
3739 /* Read-only file. */
3740 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3742 edit_free_file(file);
3746 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3747 (long) time(NULL), file->identifier, section_start_line_number);
3749 edit_free_file(file);
3751 return cgi_redirect(rsp, target);
3755 /*********************************************************************
3757 * Function : cgi_edit_actions_section_remove
3759 * Description : CGI function that actually removes a whole section from
3760 * the actions file. The section must be empty first
3761 * (else JB_ERR_CGI_PARAMS).
3764 * 1 : csp = Current client state (buffers, headers, etc...)
3765 * 2 : rsp = http_response data structure for output
3766 * 3 : parameters = map of cgi parameters
3769 * f : (filename) Identifies the file to edit
3770 * v : (version) File's last-modified time
3771 * s : (section) Line number of section to edit
3773 * Returns : JB_ERR_OK on success
3774 * JB_ERR_MEMORY on out-of-memory
3775 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3776 * specified or not valid.
3778 *********************************************************************/
3779 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3780 struct http_response *rsp,
3781 const struct map *parameters)
3784 struct editable_file * file;
3785 struct file_line * cur_line;
3786 struct file_line * prev_line;
3787 unsigned line_number;
3791 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3793 return cgi_error_disabled(csp, rsp);
3796 err = get_number_param(csp, parameters, "s", §ionid);
3802 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3805 /* No filename specified, can't read file, modified, or out of memory. */
3806 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3810 cur_line = file->lines;
3813 while ((cur_line != NULL) && (line_number < sectionid))
3815 prev_line = cur_line;
3816 cur_line = cur_line->next;
3820 if ( (cur_line == NULL)
3821 || (cur_line->type != FILE_LINE_ACTION) )
3823 /* Invalid "sectionid" parameter */
3824 edit_free_file(file);
3825 return JB_ERR_CGI_PARAMS;
3828 if ( (cur_line->next != NULL)
3829 && (cur_line->next->type == FILE_LINE_URL) )
3831 /* Section not empty. */
3832 edit_free_file(file);
3833 return JB_ERR_CGI_PARAMS;
3836 /* At this point, the line to remove is in cur_line, and the previous
3837 * one is in prev_line
3840 /* Unlink cur_line */
3841 if (prev_line == NULL)
3843 /* Removing the first line from the file */
3844 file->lines = cur_line->next;
3848 prev_line->next = cur_line->next;
3850 cur_line->next = NULL;
3853 edit_free_file_lines(cur_line);
3855 err = edit_write_file(file);
3858 /* Error writing file */
3859 if (err == JB_ERR_FILE)
3861 /* Read-only file. */
3862 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3864 edit_free_file(file);
3868 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
3869 (long) time(NULL), file->identifier);
3871 edit_free_file(file);
3873 return cgi_redirect(rsp, target);
3877 /*********************************************************************
3879 * Function : cgi_edit_actions_section_add
3881 * Description : CGI function that adds a new empty section to
3885 * 1 : csp = Current client state (buffers, headers, etc...)
3886 * 2 : rsp = http_response data structure for output
3887 * 3 : parameters = map of cgi parameters
3890 * f : (filename) Identifies the file to edit
3891 * v : (version) File's last-modified time
3892 * s : (section) Line number of section to add after, 0 for
3895 * Returns : JB_ERR_OK on success
3896 * JB_ERR_MEMORY on out-of-memory
3897 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3898 * specified or not valid.
3900 *********************************************************************/
3901 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3902 struct http_response *rsp,
3903 const struct map *parameters)
3906 struct file_line * new_line;
3908 struct editable_file * file;
3909 struct file_line * cur_line;
3910 unsigned line_number;
3914 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3916 return cgi_error_disabled(csp, rsp);
3919 err = get_number_param(csp, parameters, "s", §ionid);
3925 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3928 /* No filename specified, can't read file, modified, or out of memory. */
3929 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3933 cur_line = file->lines;
3935 if (sectionid <= 1U)
3937 /* Add to start of file */
3938 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
3940 /* There's something in the file, find the line before the first
3943 while ( (cur_line->next != NULL)
3944 && (cur_line->next->type != FILE_LINE_ACTION) )
3946 cur_line = cur_line->next;
3952 /* File starts with action line, so insert at top */
3958 /* Add after stated section. */
3959 while ((cur_line != NULL) && (line_number < sectionid))
3961 cur_line = cur_line->next;
3965 if ( (cur_line == NULL)
3966 || (cur_line->type != FILE_LINE_ACTION))
3968 /* Invalid "sectionid" parameter */
3969 edit_free_file(file);
3970 return JB_ERR_CGI_PARAMS;
3973 /* Skip through the section to find the last line in it. */
3974 while ( (cur_line->next != NULL)
3975 && (cur_line->next->type != FILE_LINE_ACTION) )
3977 cur_line = cur_line->next;
3982 /* At this point, the last line in the previous section is in cur_line
3983 * - add after this. (Or if we need to add as the first line, cur_line
3987 new_text = strdup("{}");
3988 if (NULL == new_text)
3990 edit_free_file(file);
3991 return JB_ERR_MEMORY;
3994 /* Allocate the new line */
3995 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3996 if (new_line == NULL)
3999 edit_free_file(file);
4000 return JB_ERR_MEMORY;
4003 /* Fill in the data members of the new line */
4004 new_line->raw = NULL;
4005 new_line->prefix = NULL;
4006 new_line->unprocessed = new_text;
4007 new_line->type = FILE_LINE_ACTION;
4009 if (cur_line != NULL)
4011 /* Link new_line into the list, after cur_line */
4012 new_line->next = cur_line->next;
4013 cur_line->next = new_line;
4017 /* Link new_line into the list, as first line */
4018 new_line->next = file->lines;
4019 file->lines = new_line;
4022 /* Done making changes, now commit */
4024 err = edit_write_file(file);
4027 /* Error writing file */
4028 if (err == JB_ERR_FILE)
4030 /* Read-only file. */
4031 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4033 edit_free_file(file);
4037 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4038 (long) time(NULL), file->identifier);
4040 edit_free_file(file);
4042 return cgi_redirect(rsp, target);
4046 /*********************************************************************
4048 * Function : cgi_edit_actions_section_swap
4050 * Description : CGI function that swaps the order of two sections
4051 * in the actions file. Note that this CGI can actually
4052 * swap any two arbitrary sections, but the GUI interface
4053 * currently only allows consecutive sections to be
4057 * 1 : csp = Current client state (buffers, headers, etc...)
4058 * 2 : rsp = http_response data structure for output
4059 * 3 : parameters = map of cgi parameters
4062 * f : (filename) Identifies the file to edit
4063 * v : (version) File's last-modified time
4064 * s1 : (section1) Line number of first section to swap
4065 * s2 : (section2) Line number of second section to swap
4067 * Returns : JB_ERR_OK on success
4068 * JB_ERR_MEMORY on out-of-memory
4069 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4070 * specified or not valid.
4072 *********************************************************************/
4073 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4074 struct http_response *rsp,
4075 const struct map *parameters)
4079 struct editable_file * file;
4080 struct file_line * cur_line;
4081 struct file_line * prev_line;
4082 struct file_line * line_before_section1;
4083 struct file_line * line_start_section1;
4084 struct file_line * line_end_section1;
4085 struct file_line * line_after_section1;
4086 struct file_line * line_before_section2;
4087 struct file_line * line_start_section2;
4088 struct file_line * line_end_section2;
4089 struct file_line * line_after_section2;
4090 unsigned line_number;
4094 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4096 return cgi_error_disabled(csp, rsp);
4099 err = get_number_param(csp, parameters, "s1", §ion1);
4100 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4106 if (section1 > section2)
4108 unsigned temp = section2;
4109 section2 = section1;
4113 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4116 /* No filename specified, can't read file, modified, or out of memory. */
4117 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4120 /* Start at the beginning... */
4122 cur_line = file->lines;
4125 /* ... find section1 ... */
4126 while ((cur_line != NULL) && (line_number < section1))
4128 prev_line = cur_line;
4129 cur_line = cur_line->next;
4133 if ( (cur_line == NULL)
4134 || (cur_line->type != FILE_LINE_ACTION) )
4136 /* Invalid "section1" parameter */
4137 edit_free_file(file);
4138 return JB_ERR_CGI_PARAMS;
4141 /* If no-op, we've validated params and can skip the rest. */
4142 if (section1 != section2)
4144 /* ... find the end of section1 ... */
4145 line_before_section1 = prev_line;
4146 line_start_section1 = cur_line;
4149 prev_line = cur_line;
4150 cur_line = cur_line->next;
4153 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4154 line_end_section1 = prev_line;
4155 line_after_section1 = cur_line;
4157 /* ... find section2 ... */
4158 while ((cur_line != NULL) && (line_number < section2))
4160 prev_line = cur_line;
4161 cur_line = cur_line->next;
4165 if ( (cur_line == NULL)
4166 || (cur_line->type != FILE_LINE_ACTION) )
4168 /* Invalid "section2" parameter */
4169 edit_free_file(file);
4170 return JB_ERR_CGI_PARAMS;
4173 /* ... find the end of section2 ... */
4174 line_before_section2 = prev_line;
4175 line_start_section2 = cur_line;
4178 prev_line = cur_line;
4179 cur_line = cur_line->next;
4182 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4183 line_end_section2 = prev_line;
4184 line_after_section2 = cur_line;
4186 /* Now have all the pointers we need. Do the swap. */
4188 /* Change the pointer to section1 to point to section2 instead */
4189 if (line_before_section1 == NULL)
4191 file->lines = line_start_section2;
4195 line_before_section1->next = line_start_section2;
4198 if (line_before_section2 == line_end_section1)
4200 /* Consecutive sections */
4201 line_end_section2->next = line_start_section1;
4205 line_end_section2->next = line_after_section1;
4206 line_before_section2->next = line_start_section1;
4209 /* Set the pointer from the end of section1 to the rest of the file */
4210 line_end_section1->next = line_after_section2;
4212 err = edit_write_file(file);
4215 /* Error writing file */
4216 if (err == JB_ERR_FILE)
4218 /* Read-only file. */
4219 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4221 edit_free_file(file);
4224 } /* END if (section1 != section2) */
4226 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4227 (long) time(NULL), file->identifier);
4229 edit_free_file(file);
4231 return cgi_redirect(rsp, target);
4235 /*********************************************************************
4237 * Function : cgi_toggle
4239 * Description : CGI function that adds a new empty section to
4243 * 1 : csp = Current client state (buffers, headers, etc...)
4244 * 2 : rsp = http_response data structure for output
4245 * 3 : parameters = map of cgi parameters
4248 * set : If present, how to change toggle setting:
4249 * "enable", "disable", "toggle", or none (default).
4250 * mini : If present, use mini reply template.
4252 * Returns : JB_ERR_OK on success
4253 * JB_ERR_MEMORY on out-of-memory
4255 *********************************************************************/
4256 jb_err cgi_toggle(struct client_state *csp,
4257 struct http_response *rsp,
4258 const struct map *parameters)
4260 struct map *exports;
4262 const char *template_name;
4268 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4270 return cgi_error_disabled(csp, rsp);
4273 mode = get_char_param(parameters, "set");
4278 global_toggle_state = 1;
4280 else if (mode == 'D')
4283 global_toggle_state = 0;
4285 else if (mode == 'T')
4288 global_toggle_state = !global_toggle_state;
4291 if (NULL == (exports = default_exports(csp, "toggle")))
4293 return JB_ERR_MEMORY;
4296 template_name = (get_char_param(parameters, "mini")
4300 return template_fill_for_cgi(csp, template_name, exports, rsp);
4304 /*********************************************************************
4306 * Function : javascriptify
4308 * Description : Converts a string into a form JavaScript will like.
4310 * Netscape 4's JavaScript sucks - it doesn't use
4311 * "id" parameters, so you have to set the "name"
4312 * used to submit a form element to something JavaScript
4313 * will like. (Or access the elements by index in an
4314 * array. That array contains >60 elements and will
4315 * be changed whenever we add a new action to the
4316 * editor, so I'm NOT going to use indexes that have
4317 * to be figured out by hand.)
4319 * Currently the only thing we have to worry about
4320 * is "-" ==> "_" conversion.
4322 * This is a length-preserving operation so it is
4323 * carried out in-place, no memory is allocated
4327 * 1 : identifier = String to make JavaScript-friendly.
4331 *********************************************************************/
4332 static void javascriptify(char * identifier)
4334 char * p = identifier;
4335 while (NULL != (p = strchr(p, '-')))
4342 /*********************************************************************
4344 * Function : actions_to_radio
4346 * Description : Converts a actionsfile entry into settings for
4347 * radio buttons and edit boxes on a HTML form.
4350 * 1 : exports = List of substitutions to add to.
4351 * 2 : action = Action to read
4353 * Returns : JB_ERR_OK on success
4354 * JB_ERR_MEMORY on out-of-memory
4356 *********************************************************************/
4357 static jb_err actions_to_radio(struct map * exports,
4358 const struct action_spec *action)
4360 unsigned mask = action->mask;
4361 unsigned add = action->add;
4369 mask = action->mask;
4372 /* sanity - prevents "-feature +feature" */
4376 #define DEFINE_ACTION_BOOL(name, bit) \
4377 if (!(mask & bit)) \
4379 current_mode = 'n'; \
4381 else if (add & bit) \
4383 current_mode = 'y'; \
4387 current_mode = 'x'; \
4389 if (map_radio(exports, name, "ynx", current_mode)) \
4391 return JB_ERR_MEMORY; \
4394 #define DEFINE_ACTION_STRING(name, bit, index) \
4395 DEFINE_ACTION_BOOL(name, bit); \
4398 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4401 checked = !strcmp(action->string[index], value); \
4405 checked = is_default; \
4407 mapped_param |= checked; \
4408 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4410 return JB_ERR_MEMORY; \
4413 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4414 if (map(exports, name "-param-custom", 1, \
4415 ((!mapped_param) ? "checked" : ""), 1)) \
4417 return JB_ERR_MEMORY; \
4419 if (map(exports, name "-param", 1, \
4420 (((add & bit) && !mapped_param) ? \
4421 action->string[index] : default_val), 1)) \
4423 return JB_ERR_MEMORY; \
4426 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4427 if (map(exports, name "-param", 1, \
4428 ((add & bit) ? action->string[index] : default_val), 1)) \
4430 return JB_ERR_MEMORY; \
4433 #define DEFINE_ACTION_MULTI(name, index) \
4434 if (action->multi_add[index]->first) \
4436 current_mode = 'y'; \
4438 else if (action->multi_remove_all[index]) \
4440 current_mode = 'n'; \
4442 else if (action->multi_remove[index]->first) \
4444 current_mode = 'y'; \
4448 current_mode = 'x'; \
4450 if (map_radio(exports, name, "ynx", current_mode)) \
4452 return JB_ERR_MEMORY; \
4455 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4457 #include "actionlist.h"
4459 #undef DEFINE_ACTION_MULTI
4460 #undef DEFINE_ACTION_STRING
4461 #undef DEFINE_ACTION_BOOL
4462 #undef DEFINE_ACTION_ALIAS
4463 #undef DEFINE_CGI_PARAM_CUSTOM
4464 #undef DEFINE_CGI_PARAM_RADIO
4465 #undef DEFINE_CGI_PARAM_NO_RADIO
4471 /*********************************************************************
4473 * Function : actions_from_radio
4475 * Description : Converts a map of parameters passed to a CGI function
4476 * into an actionsfile entry.
4479 * 1 : parameters = parameters to the CGI call
4480 * 2 : action = Action to change. Must be valid before
4481 * the call, actions not specified will be
4484 * Returns : JB_ERR_OK on success
4485 * JB_ERR_MEMORY on out-of-memory
4487 *********************************************************************/
4488 static jb_err actions_from_radio(const struct map * parameters,
4489 struct action_spec *action)
4494 const char * js_name;
4495 jb_err err = JB_ERR_OK;
4500 /* Statics are generally a potential race condition,
4501 * but in this case we're safe and don't need semaphores.
4502 * Be careful if you modify this function.
4504 * The js_name_arr's are never free()d, but this is no
4505 * problem, since they will only be created once and
4506 * used by all threads thereafter. -oes
4509 #define JAVASCRIPTIFY(dest_var, string) \
4511 static int first_time = 1; \
4512 static char *js_name_arr; \
4515 js_name_arr = strdup(string); \
4516 javascriptify(js_name_arr); \
4518 dest_var = js_name_arr; \
4522 #define DEFINE_ACTION_BOOL(name, bit) \
4523 JAVASCRIPTIFY(js_name, name); \
4524 ch = get_char_param(parameters, js_name); \
4527 action->add |= bit; \
4528 action->mask |= bit; \
4530 else if (ch == 'N') \
4532 action->add &= ~bit; \
4533 action->mask &= ~bit; \
4535 else if (ch == 'X') \
4537 action->add &= ~bit; \
4538 action->mask |= bit; \
4541 #define DEFINE_ACTION_STRING(name, bit, index) \
4542 JAVASCRIPTIFY(js_name, name); \
4543 ch = get_char_param(parameters, js_name); \
4547 JAVASCRIPTIFY(js_name, name "-mode"); \
4548 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4549 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4551 JAVASCRIPTIFY(js_name, name "-param"); \
4552 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4554 if (param != NULL) \
4556 if (NULL == (param_dup = strdup(param))) \
4558 return JB_ERR_MEMORY; \
4560 freez(action->string[index]); \
4561 action->add |= bit; \
4562 action->mask |= bit; \
4563 action->string[index] = param_dup; \
4566 else if (ch == 'N') \
4568 if (action->add & bit) \
4570 freez(action->string[index]); \
4572 action->add &= ~bit; \
4573 action->mask &= ~bit; \
4575 else if (ch == 'X') \
4577 if (action->add & bit) \
4579 freez(action->string[index]); \
4581 action->add &= ~bit; \
4582 action->mask |= bit; \
4585 #define DEFINE_ACTION_MULTI(name, index) \
4586 JAVASCRIPTIFY(js_name, name); \
4587 ch = get_char_param(parameters, js_name); \
4592 else if (ch == 'N') \
4594 list_remove_all(action->multi_add[index]); \
4595 list_remove_all(action->multi_remove[index]); \
4596 action->multi_remove_all[index] = 1; \
4598 else if (ch == 'X') \
4600 list_remove_all(action->multi_add[index]); \
4601 list_remove_all(action->multi_remove[index]); \
4602 action->multi_remove_all[index] = 0; \
4605 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4607 #include "actionlist.h"
4609 #undef DEFINE_ACTION_MULTI
4610 #undef DEFINE_ACTION_STRING
4611 #undef DEFINE_ACTION_BOOL
4612 #undef DEFINE_ACTION_ALIAS
4613 #undef JAVASCRIPTIFY
4619 #endif /* def FEATURE_CGI_EDIT_ACTIONS */