1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.41.2.12 2006/01/30 15:16:25 david__schmidt 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.12 2006/01/30 15:16:25 david__schmidt
46 * Remove a little residual debugging info
48 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
49 * Multiple filter file support
51 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
52 * Undo some damaging memory leak patches
54 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
55 * Removing a double free
57 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
58 * A few memory leaks plugged (mostly on error paths)
60 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
61 * Moved cgi_error_disabled() from cgiedit.c to
62 * cgi.c to re-enable build with --disable-editor.
63 * Fixes Bug #892744. Thanks to Matthew Fischer
66 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
67 * One line lost in last commit
69 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
70 * - All edit functions that redirect back to the list page
71 * now use cgi_redirect
72 * - All redirects now contain useless parameter "foo", whose
73 * value are raw seconds since epoch, in order to force
74 * Opera and Konqueror to properly reload the list. Closes
77 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
78 * Cosmetic: Renamed cryptic variable
80 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
81 * Fix: Don't free uninitialized struct editable_file
83 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
84 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
86 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
87 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
89 * Revision 1.41 2002/05/21 19:09:45 oes
90 * - Made Add/Edit/Remove URL Submit and Cancel
91 * buttons jump back to relevant section in eal
92 * - Bugfix: remove-url-form needs p export
94 * Revision 1.40 2002/05/19 11:34:35 jongfoster
95 * Handling read-only actions files better - report the actual
96 * error, not "Out of memory"!
99 * http://sourceforge.net/tracker/index.php?func=detail
100 * &aid=557905&group_id=11118&atid=111118
102 * Revision 1.39 2002/05/12 21:39:15 jongfoster
103 * - Adding Doxygen-style comments to structures and #defines.
104 * - Correcting function comments
106 * Revision 1.38 2002/05/03 23:00:38 jongfoster
107 * Support for templates for "standard actions" buttons.
110 * Revision 1.37 2002/04/30 11:14:52 oes
111 * Made csp the first parameter in *action_to_html
113 * Revision 1.36 2002/04/26 21:53:30 jongfoster
114 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
116 * Revision 1.35 2002/04/26 21:50:02 jongfoster
117 * Honouring default exports in edit-actions-for-url-filter template.
119 * Revision 1.34 2002/04/26 12:54:17 oes
120 * Adaptions to changes in actions.c
122 * Revision 1.33 2002/04/24 02:17:47 oes
123 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
125 * - Activated Jon's code for editing multiple AFs
126 * - cgi_edit_list_actions now provides context-sensitive
127 * help, looks up all action sets from standard.action and
128 * makes buttons for them in the catchall section
129 * - cgi_edit_action_submit now honors a p parameter, looks up
130 * the corresponding action set, and sets the catchall pattern's
131 * actions accordingly.
133 * Revision 1.32 2002/04/19 16:55:31 jongfoster
134 * Fixing newline problems. If we do our own text file newline
135 * mangling, we don't want the library to do any, so we need to
136 * open the files in *binary* mode.
138 * Revision 1.31 2002/04/18 19:21:08 jongfoster
139 * Added code to detect "conventional" action files, that start
140 * with a set of actions for all URLs (the pattern "/").
141 * These are special-cased in the "edit-actions-list" CGI, so
142 * that a special UI can be written for them.
144 * Revision 1.30 2002/04/10 13:38:35 oes
145 * load_template signature changed
147 * Revision 1.29 2002/04/08 16:59:08 oes
150 * Revision 1.28 2002/03/27 12:30:29 oes
151 * Deleted unsused variable
153 * Revision 1.27 2002/03/26 23:06:04 jongfoster
154 * Removing duplicate @ifs on the toggle page
156 * Revision 1.26 2002/03/26 22:59:17 jongfoster
157 * Fixing /toggle to display status consistently.
159 * Revision 1.25 2002/03/26 22:29:54 swa
160 * we have a new homepage!
162 * Revision 1.24 2002/03/24 15:23:33 jongfoster
165 * Revision 1.23 2002/03/24 13:32:41 swa
166 * name change related issues
168 * Revision 1.22 2002/03/24 13:25:43 swa
169 * name change related issues
171 * Revision 1.21 2002/03/22 18:02:48 jongfoster
172 * Fixing remote toggle
174 * Revision 1.20 2002/03/16 20:28:34 oes
175 * Added descriptions to the filters so users will know what they select in the cgi editor
177 * Revision 1.19 2002/03/16 18:38:14 jongfoster
178 * Stopping stupid or malicious users from breaking the actions
179 * file using the web-based editor.
181 * Revision 1.18 2002/03/16 14:57:44 jongfoster
182 * Full support for enabling/disabling modular filters.
184 * Revision 1.17 2002/03/16 14:26:42 jongfoster
185 * First version of modular filters support - READ ONLY!
186 * Fixing a double-free bug in the out-of-memory handling in map_radio().
188 * Revision 1.16 2002/03/07 03:46:17 oes
189 * Fixed compiler warnings
191 * Revision 1.15 2002/03/06 22:54:35 jongfoster
192 * Automated function-comment nitpicking.
194 * Revision 1.14 2002/03/05 00:24:51 jongfoster
195 * Patch to always edit the current actions file.
197 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
198 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
200 * Revision 1.12 2002/03/03 09:18:03 joergs
201 * Made jumbjuster work on AmigaOS again.
203 * Revision 1.11 2002/01/23 01:03:31 jongfoster
204 * Fixing gcc [CygWin] compiler warnings
206 * Revision 1.10 2002/01/23 00:22:59 jongfoster
207 * Adding new function cgi_edit_actions_section_swap(), to reorder
210 * Adding get_url_spec_param() to get a validated URL pattern.
212 * Moving edit_read_line() out of this file and into loaders.c.
214 * Adding missing html_encode() to many CGI functions.
216 * Moving the functions that #include actionlist.h to the end of the file,
217 * because the Visual C++ 97 debugger gets extremely confused if you try
218 * to debug any code that comes after them in the file.
220 * Major optimizations in cgi_edit_actions_list() to reduce the size of
221 * the generated HTML (down 40% from 550k to 304k), with major side-effects
222 * throughout the editor and templates. In particular, the length of the
223 * URLs throughout the editor has been drastically reduced, by cutting
224 * paramater names down to 1 character and CGI names down to 3-4
225 * characters, by removing all non-essential CGI paramaters even at the
226 * expense of having to re-read the actions file for the most trivial
227 * page, and by using relative rather than absolute URLs. This means
228 * that this (typical example):
230 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
231 * filename=ijb&ver=1011487572&section=12&pattern=13
232 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
236 * <a href="eau?f=ijb&v=1011487572&p=13">
238 * Revision 1.9 2002/01/17 20:56:22 jongfoster
239 * Replacing hard references to the URL of the config interface
240 * with #defines from project.h
242 * Revision 1.8 2001/11/30 23:35:51 jongfoster
243 * Renaming actionsfile to ijb.action
245 * Revision 1.7 2001/11/13 00:28:24 jongfoster
246 * - Renaming parameters from edit-actions-for-url so that they only
247 * contain legal JavaScript characters. If we wanted to write
248 * JavaScript that worked with Netscape 4, this is nessacery.
249 * (Note that at the moment the JavaScript doesn't actually work
250 * with Netscape 4, but now this is purely a template issue, not
251 * one affecting code).
252 * - Adding new CGIs for use by non-JavaScript browsers:
253 * edit-actions-url-form
254 * edit-actions-add-url-form
255 * edit-actions-remove-url-form
258 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
259 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
260 * by and __OS2__ ifdef.
262 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
263 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
264 * threads to call select() simultaneously. So, it's time to do a real, live,
265 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
266 * (native). Both versions will work, but using __OS2__ offers multi-threading.
268 * Revision 1.4 2001/10/23 21:48:19 jongfoster
269 * Cleaning up error handling in CGI functions - they now send back
270 * a HTML error page and should never cause a FATAL error. (Fixes one
271 * potential source of "denial of service" attacks).
273 * CGI actions file editor that works and is actually useful.
275 * Ability to toggle JunkBuster remotely using a CGI call.
277 * You can turn off both the above features in the main configuration
278 * file, e.g. if you are running a multi-user proxy.
280 * Revision 1.3 2001/10/14 22:12:49 jongfoster
281 * New version of CGI-based actionsfile editor.
282 * Major changes, including:
283 * - Completely new file parser and file output routines
284 * - edit-actions CGI renamed edit-actions-for-url
285 * - All CGIs now need a filename parameter, except for...
286 * - New CGI edit-actions which doesn't need a filename,
287 * to allow you to start the editor up.
288 * - edit-actions-submit now works, and now automatically
289 * redirects you back to the main edit-actions-list handler.
291 * Revision 1.2 2001/09/16 17:05:14 jongfoster
292 * Removing unused #include showarg.h
294 * Revision 1.1 2001/09/16 15:47:37 jongfoster
295 * First version of CGI-based edit interface. This is very much a
296 * work-in-progress, and you can't actually use it to edit anything
297 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
298 * to have any effect.
301 **********************************************************************/
307 * FIXME: Following includes copied from cgi.c - which are actually needed?
312 #include <sys/types.h>
316 #include <sys/stat.h>
319 #define snprintf _snprintf
320 #endif /* def _WIN32 */
325 #include "cgisimple.h"
329 #include "miscutil.h"
333 /* loadcfg.h is for global_toggle_state only */
334 #include "urlmatch.h"
336 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
339 #ifdef FEATURE_CGI_EDIT_ACTIONS
342 * A line in an editable_file.
346 /** Next entry in the linked list */
347 struct file_line * next;
349 /** The raw data, to write out if this line is unmodified. */
352 /** Comments and/or whitespace to put before this line if it's modified
353 and then written out. */
356 /** The actual data, as a string. Line continuation and comment removal
357 are performed on the data read from file before it's stored here, so
358 it will be a single line of data. */
361 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
364 /** The actual data, processed into some sensible data type. */
368 /** An action specification. */
369 struct action_spec action[1];
371 /** A name=value pair. */
375 /** The name in the name=value pair. */
378 /** The value in the name=value pair, as a string. */
381 /** The value in the name=value pair, as an integer. */
386 /* Add more data types here... e.g.
389 struct url_spec url[1];
393 struct action_spec action[1];
403 /** This file_line has not been processed yet. */
404 #define FILE_LINE_UNPROCESSED 1
406 /** This file_line is blank. Can only appear at the end of a file, due to
407 the way the parser works. */
408 #define FILE_LINE_BLANK 2
410 /** This file_line says {{alias}}. */
411 #define FILE_LINE_ALIAS_HEADER 3
413 /** This file_line defines an alias. */
414 #define FILE_LINE_ALIAS_ENTRY 4
416 /** This file_line defines an {action}. */
417 #define FILE_LINE_ACTION 5
419 /** This file_line specifies a URL pattern. */
420 #define FILE_LINE_URL 6
422 /** This file_line says {{settings}}. */
423 #define FILE_LINE_SETTINGS_HEADER 7
425 /** This file_line is in a {{settings}} block. */
426 #define FILE_LINE_SETTINGS_ENTRY 8
428 /** This file_line says {{description}}. */
429 #define FILE_LINE_DESCRIPTION_HEADER 9
431 /** This file_line is in a {{description}} block. */
432 #define FILE_LINE_DESCRIPTION_ENTRY 10
436 * A configuration file, in a format that can be edited and written back to
441 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
442 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
443 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
444 /**< Pre-encoded with url_encode() for ease of use. */
445 const char * version_str; /**< Last modification time, as a string. For CGI param. */
446 /**< Can be used in URL without using url_param(). */
447 unsigned version; /**< Last modification time - prevents chaos with
448 the browser's "back" button. Note that this is a
449 time_t cast to an unsigned. When comparing, always
450 cast the time_t to an unsigned, and *NOT* vice-versa.
451 This may lose the top few bits, but they're not
452 significant anyway. */
453 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
454 Note that changing this after the file has been
455 read in will cause a mess. */
456 struct file_line * parse_error; /**< On parse error, this is the offending line. */
457 const char * parse_error_text; /**< On parse error, this is the problem.
458 (Statically allocated) */
461 /* FIXME: Following non-static functions should be prototyped in .h or made static */
463 /* Functions to read and write arbitrary config files */
464 jb_err edit_read_file(struct client_state *csp,
465 const struct map *parameters,
468 struct editable_file **pfile);
469 jb_err edit_write_file(struct editable_file * file);
470 void edit_free_file(struct editable_file * file);
472 /* Functions to read and write actions files */
473 jb_err edit_parse_actions_file(struct editable_file * file);
474 jb_err edit_read_actions_file(struct client_state *csp,
475 struct http_response *rsp,
476 const struct map *parameters,
478 struct editable_file **pfile);
481 jb_err cgi_error_modified(struct client_state *csp,
482 struct http_response *rsp,
483 const char *filename);
484 jb_err cgi_error_parse(struct client_state *csp,
485 struct http_response *rsp,
486 struct editable_file *file);
487 jb_err cgi_error_file(struct client_state *csp,
488 struct http_response *rsp,
489 const char *filename);
490 jb_err cgi_error_file_read_only(struct client_state *csp,
491 struct http_response *rsp,
492 const char *filename);
494 /* Internal arbitrary config file support functions */
495 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
496 static void edit_free_file_lines(struct file_line * first_line);
498 /* Internal actions file support functions */
499 static int match_actions_file_header_line(const char * line, const char * name);
500 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
502 /* Internal parameter parsing functions */
503 static jb_err get_file_name_param(struct client_state *csp,
504 const struct map *parameters,
505 const char *param_name,
508 const char **pparam);
510 static jb_err get_url_spec_param(struct client_state *csp,
511 const struct map *parameters,
516 /* Internal actionsfile <==> HTML conversion functions */
517 static jb_err map_radio(struct map * exports,
518 const char * optionname,
521 static jb_err actions_to_radio(struct map * exports,
522 const struct action_spec *action);
523 static jb_err actions_from_radio(const struct map * parameters,
524 struct action_spec *action);
527 static jb_err map_copy_parameter_html(struct map *out,
528 const struct map *in,
530 #if 0 /* unused function */
531 static jb_err map_copy_parameter_url(struct map *out,
532 const struct map *in,
534 #endif /* unused function */
536 /* Internal convenience functions */
537 static char *section_target(const unsigned sectionid);
539 /*********************************************************************
541 * Function : section_target
543 * Description : Given an unsigned (section id) n, produce a dynamically
544 * allocated string of the form #l<n>, for use in link
548 * 1 : sectionid = start line number of section
550 * Returns : String with link target, or NULL if out of
553 *********************************************************************/
554 static char *section_target(const unsigned sectionid)
558 snprintf(buf, 30, "#l%d", sectionid);
564 /*********************************************************************
566 * Function : map_copy_parameter_html
568 * Description : Copy a CGI parameter from one map to another, HTML
572 * 1 : out = target map
573 * 2 : in = source map
574 * 3 : name = name of cgi parameter to copy
576 * Returns : JB_ERR_OK on success
577 * JB_ERR_MEMORY on out-of-memory
578 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
581 *********************************************************************/
582 static jb_err map_copy_parameter_html(struct map *out,
583 const struct map *in,
593 value = lookup(in, name);
594 err = map(out, name, 1, html_encode(value), 0);
601 else if (*value == '\0')
603 return JB_ERR_CGI_PARAMS;
612 #if 0 /* unused function */
613 /*********************************************************************
615 * Function : map_copy_parameter_url
617 * Description : Copy a CGI parameter from one map to another, URL
621 * 1 : out = target map
622 * 2 : in = source map
623 * 3 : name = name of cgi parameter to copy
625 * Returns : JB_ERR_OK on success
626 * JB_ERR_MEMORY on out-of-memory
627 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
630 *********************************************************************/
631 static jb_err map_copy_parameter_url(struct map *out,
632 const struct map *in,
642 value = lookup(in, name);
643 err = map(out, name, 1, url_encode(value), 0);
650 else if (*value == '\0')
652 return JB_ERR_CGI_PARAMS;
659 #endif /* 0 - unused function */
661 /*********************************************************************
663 * Function : cgi_edit_actions_url_form
665 * Description : CGI function that displays a form for
669 * 1 : csp = Current client state (buffers, headers, etc...)
670 * 2 : rsp = http_response data structure for output
671 * 3 : parameters = map of cgi parameters
674 * f : (filename) Identifies the file to edit
675 * v : (version) File's last-modified time
676 * p : (pattern) Line number of pattern to edit
678 * Returns : JB_ERR_OK on success
679 * JB_ERR_MEMORY on out-of-memory
680 * JB_ERR_CGI_PARAMS if the CGI parameters are not
681 * specified or not valid.
683 *********************************************************************/
684 jb_err cgi_edit_actions_url_form(struct client_state *csp,
685 struct http_response *rsp,
686 const struct map *parameters)
688 struct map * exports;
690 struct editable_file * file;
691 struct file_line * cur_line;
692 unsigned line_number;
693 unsigned section_start_line_number = 0;
700 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
702 return cgi_error_disabled(csp, rsp);
705 err = get_number_param(csp, parameters, "p", &patternid);
711 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
714 /* No filename specified, can't read file, modified, or out of memory. */
715 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
718 cur_line = file->lines;
720 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
722 if (cur_line->type == FILE_LINE_ACTION)
724 section_start_line_number = line_number;
726 cur_line = cur_line->next;
729 if ( (cur_line == NULL)
730 || (line_number != patternid)
732 || (cur_line->type != FILE_LINE_URL))
734 /* Invalid "patternid" parameter */
735 edit_free_file(file);
736 return JB_ERR_CGI_PARAMS;
739 if (NULL == (exports = default_exports(csp, NULL)))
741 edit_free_file(file);
742 return JB_ERR_MEMORY;
745 err = map(exports, "f", 1, file->identifier, 1);
746 if (!err) err = map(exports, "v", 1, file->version_str, 1);
747 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
748 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
749 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
751 edit_free_file(file);
759 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
763 /*********************************************************************
765 * Function : cgi_edit_actions_add_url_form
767 * Description : CGI function that displays a form for
771 * 1 : csp = Current client state (buffers, headers, etc...)
772 * 2 : rsp = http_response data structure for output
773 * 3 : parameters = map of cgi parameters
776 * f : (filename) Identifies the file to edit
777 * v : (version) File's last-modified time
778 * s : (section) Line number of section to edit
780 * Returns : JB_ERR_OK on success
781 * JB_ERR_MEMORY on out-of-memory
782 * JB_ERR_CGI_PARAMS if the CGI parameters are not
783 * specified or not valid.
785 *********************************************************************/
786 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
787 struct http_response *rsp,
788 const struct map *parameters)
797 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
799 return cgi_error_disabled(csp, rsp);
802 if (NULL == (exports = default_exports(csp, NULL)))
804 return JB_ERR_MEMORY;
807 err = map_copy_parameter_html(exports, parameters, "f");
808 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
809 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
817 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
821 /*********************************************************************
823 * Function : cgi_edit_actions_remove_url_form
825 * Description : CGI function that displays a form for
829 * 1 : csp = Current client state (buffers, headers, etc...)
830 * 2 : rsp = http_response data structure for output
831 * 3 : parameters = map of cgi parameters
834 * f : (filename) Identifies the file to edit
835 * v : (version) File's last-modified time
836 * p : (pattern) Line number of pattern to edit
838 * Returns : JB_ERR_OK on success
839 * JB_ERR_MEMORY on out-of-memory
840 * JB_ERR_CGI_PARAMS if the CGI parameters are not
841 * specified or not valid.
843 *********************************************************************/
844 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
845 struct http_response *rsp,
846 const struct map *parameters)
848 struct map * exports;
850 struct editable_file * file;
851 struct file_line * cur_line;
852 unsigned line_number;
853 unsigned section_start_line_number = 0;
860 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
862 return cgi_error_disabled(csp, rsp);
865 err = get_number_param(csp, parameters, "p", &patternid);
871 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
874 /* No filename specified, can't read file, modified, or out of memory. */
875 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
878 cur_line = file->lines;
880 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
882 if (cur_line->type == FILE_LINE_ACTION)
884 section_start_line_number = line_number;
886 cur_line = cur_line->next;
889 if ( (cur_line == NULL)
890 || (line_number != patternid)
892 || (cur_line->type != FILE_LINE_URL))
894 /* Invalid "patternid" parameter */
895 edit_free_file(file);
896 return JB_ERR_CGI_PARAMS;
899 if (NULL == (exports = default_exports(csp, NULL)))
901 edit_free_file(file);
902 return JB_ERR_MEMORY;
905 err = map(exports, "f", 1, file->identifier, 1);
906 if (!err) err = map(exports, "v", 1, file->version_str, 1);
907 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
908 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
909 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
911 edit_free_file(file);
919 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
923 /*********************************************************************
925 * Function : edit_write_file
927 * Description : Write a complete file to disk.
930 * 1 : file = File to write.
932 * Returns : JB_ERR_OK on success
933 * JB_ERR_FILE on error writing to file.
934 * JB_ERR_MEMORY on out of memory
936 *********************************************************************/
937 jb_err edit_write_file(struct editable_file * file)
940 struct file_line * cur_line;
941 struct stat statbuf[1];
942 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
943 digits in time_t, assuming this is a 64-bit
944 machine, plus null terminator, plus one
948 assert(file->filename);
950 if (NULL == (fp = fopen(file->filename, "wb")))
955 cur_line = file->lines;
956 while (cur_line != NULL)
960 if (fputs(cur_line->raw, fp) < 0)
968 if (cur_line->prefix)
970 if (fputs(cur_line->prefix, fp) < 0)
976 if (cur_line->unprocessed)
979 if (NULL != strchr(cur_line->unprocessed, '#'))
981 /* Must quote '#' characters */
988 /* Count number of # characters, so we know length of output string */
989 src = cur_line->unprocessed;
990 while (NULL != (src = strchr(src, '#')))
997 /* Allocate new memory for string */
998 len = strlen(cur_line->unprocessed);
999 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
1001 /* Uh oh, just trashed file! */
1003 return JB_ERR_MEMORY;
1006 /* Loop through string from end */
1007 src = cur_line->unprocessed + len;
1008 dest = str + len + numhash;
1009 for ( ; len >= 0; len--)
1011 if ((*dest-- = *src--) == '#')
1015 assert(numhash >= 0);
1018 assert(numhash == 0);
1019 assert(src + 1 == cur_line->unprocessed);
1020 assert(dest + 1 == str);
1022 if (fputs(str, fp) < 0)
1033 /* Can write without quoting '#' characters. */
1034 if (fputs(cur_line->unprocessed, fp) < 0)
1040 if (fputs(NEWLINE(file->newline), fp) < 0)
1048 /* FIXME: Write data from file->data->whatever */
1052 cur_line = cur_line->next;
1058 /* Update the version stamp in the file structure, since we just
1059 * wrote to the file & changed it's date.
1061 if (stat(file->filename, statbuf) < 0)
1063 /* Error, probably file not found. */
1066 file->version = (unsigned)statbuf->st_mtime;
1068 /* Correct file->version_str */
1069 freez(file->version_str);
1070 snprintf(version_buf, 22, "%u", file->version);
1071 version_buf[21] = '\0';
1072 file->version_str = strdup(version_buf);
1073 if (version_buf == NULL)
1075 return JB_ERR_MEMORY;
1082 /*********************************************************************
1084 * Function : edit_free_file
1086 * Description : Free a complete file in memory.
1089 * 1 : file = Data structure to free.
1093 *********************************************************************/
1094 void edit_free_file(struct editable_file * file)
1098 /* Silently ignore NULL pointer */
1102 edit_free_file_lines(file->lines);
1103 freez(file->filename);
1104 freez(file->identifier);
1105 freez(file->version_str);
1107 file->parse_error_text = NULL; /* Statically allocated */
1108 file->parse_error = NULL;
1114 /*********************************************************************
1116 * Function : edit_free_file_lines
1118 * Description : Free an entire linked list of file lines.
1121 * 1 : first_line = Data structure to free.
1125 *********************************************************************/
1126 static void edit_free_file_lines(struct file_line * first_line)
1128 struct file_line * next_line;
1130 while (first_line != NULL)
1132 next_line = first_line->next;
1133 first_line->next = NULL;
1134 freez(first_line->raw);
1135 freez(first_line->prefix);
1136 freez(first_line->unprocessed);
1137 switch(first_line->type)
1139 case 0: /* special case if memory zeroed */
1140 case FILE_LINE_UNPROCESSED:
1141 case FILE_LINE_BLANK:
1142 case FILE_LINE_ALIAS_HEADER:
1143 case FILE_LINE_SETTINGS_HEADER:
1144 case FILE_LINE_DESCRIPTION_HEADER:
1145 case FILE_LINE_DESCRIPTION_ENTRY:
1146 case FILE_LINE_ALIAS_ENTRY:
1148 /* No data is stored for these */
1151 case FILE_LINE_ACTION:
1152 free_action(first_line->data.action);
1155 case FILE_LINE_SETTINGS_ENTRY:
1156 freez(first_line->data.setting.name);
1157 freez(first_line->data.setting.svalue);
1160 /* Should never happen */
1164 first_line->type = 0; /* paranoia */
1166 first_line = next_line;
1171 /*********************************************************************
1173 * Function : match_actions_file_header_line
1175 * Description : Match an actions file {{header}} line
1178 * 1 : line = String from file
1179 * 2 : name = Header to match against
1181 * Returns : 0 iff they match.
1183 *********************************************************************/
1184 static int match_actions_file_header_line(const char * line, const char * name)
1192 if ((line[0] != '{') || (line[1] != '{'))
1198 /* Look for optional whitespace */
1199 while ( (*line == ' ') || (*line == '\t') )
1204 /* Look for the specified name (case-insensitive) */
1206 if (0 != strncmpic(line, name, len))
1212 /* Look for optional whitespace */
1213 while ( (*line == ' ') || (*line == '\t') )
1218 /* Look for "}}" and end of string*/
1219 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1229 /*********************************************************************
1231 * Function : match_actions_file_header_line
1233 * Description : Match an actions file {{header}} line
1236 * 1 : line = String from file. Must not start with
1237 * whitespace (else infinite loop!)
1238 * 2 : pname = Destination for name
1239 * 2 : pvalue = Destination for value
1241 * Returns : JB_ERR_OK on success
1242 * JB_ERR_MEMORY on out-of-memory
1243 * JB_ERR_PARSE if there's no "=" sign, or if there's
1244 * nothing before the "=" sign (but empty
1245 * values *after* the "=" sign are legal).
1247 *********************************************************************/
1248 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1250 const char * name_end;
1251 const char * value_start;
1257 assert(*line != ' ');
1258 assert(*line != '\t');
1263 value_start = strchr(line, '=');
1264 if ((value_start == NULL) || (value_start == line))
1266 return JB_ERR_PARSE;
1269 name_end = value_start - 1;
1271 /* Eat any whitespace before the '=' */
1272 while ((*name_end == ' ') || (*name_end == '\t'))
1275 * we already know we must have at least 1 non-ws char
1276 * at start of buf - no need to check
1281 name_len = name_end - line + 1; /* Length excluding \0 */
1282 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1284 return JB_ERR_MEMORY;
1286 strncpy(*pname, line, name_len);
1287 (*pname)[name_len] = '\0';
1289 /* Eat any the whitespace after the '=' */
1291 while ((*value_start == ' ') || (*value_start == '\t'))
1296 if (NULL == (*pvalue = strdup(value_start)))
1300 return JB_ERR_MEMORY;
1307 /*********************************************************************
1309 * Function : edit_parse_actions_file
1311 * Description : Parse an actions file in memory.
1313 * Passed linked list must have the "data" member
1314 * zeroed, and must contain valid "next" and
1315 * "unprocessed" fields. The "raw" and "prefix"
1316 * fields are ignored, and "type" is just overwritten.
1318 * Note that on error the file may have been
1322 * 1 : file = Actions file to be parsed in-place.
1324 * Returns : JB_ERR_OK on success
1325 * JB_ERR_MEMORY on out-of-memory
1326 * JB_ERR_PARSE on error
1328 *********************************************************************/
1329 jb_err edit_parse_actions_file(struct editable_file * file)
1331 struct file_line * cur_line;
1333 const char * text; /* Text from a line */
1334 char * name; /* For lines of the form name=value */
1335 char * value; /* For lines of the form name=value */
1336 struct action_alias * alias_list = NULL;
1337 jb_err err = JB_ERR_OK;
1339 /* alias_list contains the aliases defined in this file.
1340 * It might be better to use the "file_line.data" fields
1341 * in the relavent places instead.
1344 cur_line = file->lines;
1346 /* A note about blank line support: Blank lines should only
1347 * ever occur as the last line in the file. This function
1348 * is more forgiving than that - FILE_LINE_BLANK can occur
1352 /* Skip leading blanks. Should only happen if file is
1353 * empty (which is valid, but pointless).
1355 while ( (cur_line != NULL)
1356 && (cur_line->unprocessed[0] == '\0') )
1359 cur_line->type = FILE_LINE_BLANK;
1360 cur_line = cur_line->next;
1363 if ( (cur_line != NULL)
1364 && (cur_line->unprocessed[0] != '{') )
1366 /* File doesn't start with a header */
1367 file->parse_error = cur_line;
1368 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1369 return JB_ERR_PARSE;
1372 if ( (cur_line != NULL) && (0 ==
1373 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1375 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1377 cur_line = cur_line->next;
1378 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1380 if (cur_line->unprocessed[0])
1382 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1384 err = split_line_on_equals(cur_line->unprocessed,
1385 &cur_line->data.setting.name,
1386 &cur_line->data.setting.svalue);
1387 if (err == JB_ERR_MEMORY)
1391 else if (err != JB_ERR_OK)
1393 /* Line does not contain a name=value pair */
1394 file->parse_error = cur_line;
1395 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1396 return JB_ERR_PARSE;
1401 cur_line->type = FILE_LINE_BLANK;
1403 cur_line = cur_line->next;
1407 if ( (cur_line != NULL) && (0 ==
1408 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1410 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1412 cur_line = cur_line->next;
1413 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1415 if (cur_line->unprocessed[0])
1417 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1421 cur_line->type = FILE_LINE_BLANK;
1423 cur_line = cur_line->next;
1427 if ( (cur_line != NULL) && (0 ==
1428 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1430 cur_line->type = FILE_LINE_ALIAS_HEADER;
1432 cur_line = cur_line->next;
1433 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1435 if (cur_line->unprocessed[0])
1437 /* define an alias */
1438 struct action_alias * new_alias;
1440 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1442 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1443 if (err == JB_ERR_MEMORY)
1447 else if (err != JB_ERR_OK)
1449 /* Line does not contain a name=value pair */
1450 file->parse_error = cur_line;
1451 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1452 return JB_ERR_PARSE;
1455 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1460 free_alias_list(alias_list);
1461 return JB_ERR_MEMORY;
1464 err = get_actions(value, alias_list, new_alias->action);
1467 /* Invalid action or out of memory */
1471 free_alias_list(alias_list);
1472 if (err == JB_ERR_MEMORY)
1478 /* Line does not contain a name=value pair */
1479 file->parse_error = cur_line;
1480 file->parse_error_text = "This alias does not specify a valid set of actions.";
1481 return JB_ERR_PARSE;
1487 new_alias->name = name;
1490 new_alias->next = alias_list;
1491 alias_list = new_alias;
1495 cur_line->type = FILE_LINE_BLANK;
1497 cur_line = cur_line->next;
1501 /* Header done, process the main part of the file */
1502 while (cur_line != NULL)
1504 /* At this point, (cur_line->unprocessed[0] == '{') */
1505 assert(cur_line->unprocessed[0] == '{');
1506 text = cur_line->unprocessed + 1;
1507 len = strlen(text) - 1;
1508 if (text[len] != '}')
1510 /* No closing } on header */
1511 free_alias_list(alias_list);
1512 file->parse_error = cur_line;
1513 file->parse_error_text = "Headers starting with '{' must have a "
1514 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1515 "must close with two brackets ('}}').";
1516 return JB_ERR_PARSE;
1521 /* An invalid {{ header. */
1522 free_alias_list(alias_list);
1523 file->parse_error = cur_line;
1524 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1525 "Please remember that the system (two-bracket) headers must "
1526 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1527 "and must appear before any actions (one-bracket) headers. "
1528 "Also note that system headers may not be repeated.";
1529 return JB_ERR_PARSE;
1532 while ( (*text == ' ') || (*text == '\t') )
1538 && ( (text[len - 1] == ' ')
1539 || (text[len - 1] == '\t') ) )
1544 cur_line->type = FILE_LINE_ACTION;
1546 /* Remove {} and make copy */
1547 if (NULL == (value = (char *) malloc(len + 1)))
1550 free_alias_list(alias_list);
1551 return JB_ERR_MEMORY;
1553 strncpy(value, text, len);
1557 err = get_actions(value, alias_list, cur_line->data.action);
1560 /* Invalid action or out of memory */
1562 free_alias_list(alias_list);
1563 if (err == JB_ERR_MEMORY)
1569 /* Line does not contain a name=value pair */
1570 file->parse_error = cur_line;
1571 file->parse_error_text = "This header does not specify a valid set of actions.";
1572 return JB_ERR_PARSE;
1576 /* Done with string - it was clobbered anyway */
1579 /* Process next line */
1580 cur_line = cur_line->next;
1582 /* Loop processing URL patterns */
1583 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1585 if (cur_line->unprocessed[0])
1587 /* Could parse URL here, but this isn't currently needed */
1589 cur_line->type = FILE_LINE_URL;
1593 cur_line->type = FILE_LINE_BLANK;
1595 cur_line = cur_line->next;
1597 } /* End main while(cur_line != NULL) loop */
1599 free_alias_list(alias_list);
1605 /*********************************************************************
1607 * Function : edit_read_file_lines
1609 * Description : Read all the lines of a file into memory.
1610 * Handles whitespace, comments and line continuation.
1613 * 1 : fp = File to read from. On return, this will be
1614 * at EOF but it will not have been closed.
1615 * 2 : pfile = Destination for a linked list of file_lines.
1616 * Will be set to NULL on error.
1617 * 3 : newline = How to handle newlines.
1619 * Returns : JB_ERR_OK on success
1620 * JB_ERR_MEMORY on out-of-memory
1622 *********************************************************************/
1623 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1625 struct file_line * first_line; /* Keep for return value or to free */
1626 struct file_line * cur_line; /* Current line */
1627 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1635 cur_line = first_line = zalloc(sizeof(struct file_line));
1636 if (cur_line == NULL)
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);
1646 /* Out of memory or empty file. */
1647 /* Note that empty file is not an error we propogate up */
1649 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1654 prev_line = cur_line;
1655 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1656 if (cur_line == NULL)
1659 edit_free_file_lines(first_line);
1660 return JB_ERR_MEMORY;
1663 cur_line->type = FILE_LINE_UNPROCESSED;
1665 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1666 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1669 edit_free_file_lines(first_line);
1670 return JB_ERR_MEMORY;
1674 while (rval != JB_ERR_FILE);
1678 /* We allocated one too many - free it */
1679 prev_line->next = NULL;
1682 *pfile = first_line;
1687 /*********************************************************************
1689 * Function : edit_read_file
1691 * Description : Read a complete file into memory.
1692 * Handles CGI parameter parsing. If requested, also
1693 * checks the file's modification timestamp.
1696 * 1 : csp = Current client state (buffers, headers, etc...)
1697 * 2 : parameters = map of cgi parameters.
1698 * 3 : require_version = true to check "ver" parameter.
1699 * 4 : suffix = File extension, e.g. ".action".
1700 * 5 : pfile = Destination for the file. Will be set
1704 * filename : The name of the file to read, without the
1705 * path or ".action" extension.
1706 * ver : (Only if require_version is nonzero)
1707 * Timestamp of the actions file. If wrong, this
1708 * function fails with JB_ERR_MODIFIED.
1710 * Returns : JB_ERR_OK on success
1711 * JB_ERR_MEMORY on out-of-memory
1712 * JB_ERR_CGI_PARAMS if "filename" was not specified
1714 * JB_ERR_FILE if the file cannot be opened or
1716 * JB_ERR_MODIFIED if version checking was requested and
1717 * failed - the file was modified outside
1718 * of this CGI editor instance.
1720 *********************************************************************/
1721 jb_err edit_read_file(struct client_state *csp,
1722 const struct map *parameters,
1723 int require_version,
1725 struct editable_file **pfile)
1727 struct file_line * lines;
1731 const char * identifier;
1732 struct editable_file * file;
1733 unsigned version = 0;
1734 struct stat statbuf[1];
1735 char version_buf[22];
1736 int newline = NEWLINE_UNKNOWN;
1744 err = get_file_name_param(csp, parameters, "f", suffix,
1745 &filename, &identifier);
1751 if (stat(filename, statbuf) < 0)
1753 /* Error, probably file not found. */
1757 version = (unsigned) statbuf->st_mtime;
1759 if (require_version)
1761 unsigned specified_version;
1762 err = get_number_param(csp, parameters, "v", &specified_version);
1769 if (version != specified_version)
1771 return JB_ERR_MODIFIED;
1775 if (NULL == (fp = fopen(filename,"rb")))
1781 err = edit_read_file_lines(fp, &lines, &newline);
1791 file = (struct editable_file *) zalloc(sizeof(*file));
1795 edit_free_file_lines(lines);
1799 file->lines = lines;
1800 file->newline = newline;
1801 file->filename = filename;
1802 file->version = version;
1803 file->identifier = url_encode(identifier);
1805 if (file->identifier == NULL)
1807 edit_free_file(file);
1808 return JB_ERR_MEMORY;
1811 /* Correct file->version_str */
1812 freez(file->version_str);
1813 snprintf(version_buf, 22, "%u", file->version);
1814 version_buf[21] = '\0';
1815 file->version_str = strdup(version_buf);
1816 if (version_buf == NULL)
1818 edit_free_file(file);
1819 return JB_ERR_MEMORY;
1827 /*********************************************************************
1829 * Function : edit_read_actions_file
1831 * Description : Read a complete actions file into memory.
1832 * Handles CGI parameter parsing. If requested, also
1833 * checks the file's modification timestamp.
1835 * If this function detects an error in the categories
1836 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1837 * then it handles it by filling in the specified
1838 * response structure and returning JB_ERR_FILE.
1841 * 1 : csp = Current client state (buffers, headers, etc...)
1842 * 2 : rsp = HTTP response. Only filled in on error.
1843 * 2 : parameters = map of cgi parameters.
1844 * 3 : require_version = true to check "ver" parameter.
1845 * 4 : pfile = Destination for the file. Will be set
1849 * filename : The name of the actions file to read, without the
1850 * path or ".action" extension.
1851 * ver : (Only if require_version is nonzero)
1852 * Timestamp of the actions file. If wrong, this
1853 * function fails with JB_ERR_MODIFIED.
1855 * Returns : JB_ERR_OK on success
1856 * JB_ERR_MEMORY on out-of-memory
1857 * JB_ERR_CGI_PARAMS if "filename" was not specified
1859 * JB_ERR_FILE if the file does not contain valid data,
1860 * or if file cannot be opened or
1861 * contains no data, or if version
1862 * checking was requested and failed.
1864 *********************************************************************/
1865 jb_err edit_read_actions_file(struct client_state *csp,
1866 struct http_response *rsp,
1867 const struct map *parameters,
1868 int require_version,
1869 struct editable_file **pfile)
1872 struct editable_file *file;
1880 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1883 /* Try to handle if possible */
1884 if (err == JB_ERR_FILE)
1886 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1888 else if (err == JB_ERR_MODIFIED)
1890 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1892 if (err == JB_ERR_OK)
1895 * Signal to higher-level CGI code that there was a problem but we
1896 * handled it, they should just return JB_ERR_OK.
1903 err = edit_parse_actions_file(file);
1906 if (err == JB_ERR_PARSE)
1908 err = cgi_error_parse(csp, rsp, file);
1909 if (err == JB_ERR_OK)
1912 * Signal to higher-level CGI code that there was a problem but we
1913 * handled it, they should just return JB_ERR_OK.
1918 edit_free_file(file);
1927 /*********************************************************************
1929 * Function : get_file_name_param
1931 * Description : Get the name of the file to edit from the parameters
1932 * passed to a CGI function. This function handles
1933 * security checks such as blocking urls containing
1934 * "/" or ".", prepending the config file directory,
1935 * and adding the specified suffix.
1937 * (This is an essential security check, otherwise
1938 * users may be able to pass "../../../etc/passwd"
1939 * and overwrite the password file [linux], "prn:"
1940 * and print random data [Windows], etc...)
1942 * This function only allows filenames contining the
1943 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1944 * That's probably too restrictive but at least it's
1948 * 1 : csp = Current client state (buffers, headers, etc...)
1949 * 2 : parameters = map of cgi parameters
1950 * 3 : param_name = The name of the parameter to read
1951 * 4 : suffix = File extension, e.g. ".actions"
1952 * 5 : pfilename = destination for full filename. Caller
1953 * free()s. Set to NULL on error.
1954 * 6 : pparam = destination for partial filename,
1955 * suitable for use in another URL. Allocated as part
1956 * of the map "parameters", so don't free it.
1957 * Set to NULL if not specified.
1959 * Returns : JB_ERR_OK on success
1960 * JB_ERR_MEMORY on out-of-memory
1961 * JB_ERR_CGI_PARAMS if "filename" was not specified
1964 *********************************************************************/
1965 static jb_err get_file_name_param(struct client_state *csp,
1966 const struct map *parameters,
1967 const char *param_name,
1970 const char **pparam)
1988 param = lookup(parameters, param_name);
1991 return JB_ERR_CGI_PARAMS;
1996 len = strlen(param);
1997 if (len >= FILENAME_MAX)
2000 return JB_ERR_CGI_PARAMS;
2003 /* Check every character to see if it's legal */
2005 while ((ch = *s++) != '\0')
2007 if ( ((ch < 'A') || (ch > 'Z'))
2008 && ((ch < 'a') || (ch > 'z'))
2009 && ((ch < '0') || (ch > '9'))
2013 /* Probable hack attempt. */
2014 return JB_ERR_CGI_PARAMS;
2018 /* Append extension */
2019 name = malloc(len + strlen(suffix) + 1);
2022 return JB_ERR_MEMORY;
2024 strcpy(name, param);
2025 strcpy(name + len, suffix);
2028 fullpath = make_path(csp->config->confdir, name);
2031 if (fullpath == NULL)
2033 return JB_ERR_MEMORY;
2037 *pfilename = fullpath;
2043 /*********************************************************************
2045 * Function : get_url_spec_param
2047 * Description : Get a URL pattern from the parameters
2048 * passed to a CGI function. Removes leading/trailing
2049 * spaces and validates it.
2052 * 1 : csp = Current client state (buffers, headers, etc...)
2053 * 2 : parameters = map of cgi parameters
2054 * 3 : name = Name of CGI parameter to read
2055 * 4 : pvalue = destination for value. Will be malloc()'d.
2056 * Set to NULL on error.
2058 * Returns : JB_ERR_OK on success
2059 * JB_ERR_MEMORY on out-of-memory
2060 * JB_ERR_CGI_PARAMS if the parameter was not specified
2063 *********************************************************************/
2064 static jb_err get_url_spec_param(struct client_state *csp,
2065 const struct map *parameters,
2069 const char *orig_param;
2072 struct url_spec compiled[1];
2082 orig_param = lookup(parameters, name);
2085 return JB_ERR_CGI_PARAMS;
2088 /* Copy and trim whitespace */
2089 param = strdup(orig_param);
2092 return JB_ERR_MEMORY;
2096 /* Must be non-empty, and can't allow 1st character to be '{' */
2097 if (param[0] == '\0' || param[0] == '{')
2100 return JB_ERR_CGI_PARAMS;
2103 /* Check for embedded newlines */
2104 for (s = param; *s != '\0'; s++)
2106 if ((*s == '\r') || (*s == '\n'))
2109 return JB_ERR_CGI_PARAMS;
2113 /* Check that regex is valid */
2118 return JB_ERR_MEMORY;
2120 err = create_url_spec(compiled, s);
2125 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2127 free_url_spec(compiled);
2129 if (param[strlen(param) - 1] == '\\')
2132 * Must protect trailing '\\' from becoming line continuation character.
2133 * Two methods: 1) If it's a domain only, add a trailing '/'.
2134 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2136 if (strchr(param, '/') == NULL)
2138 err = string_append(¶m, "/");
2142 err = string_append(¶m, "(?:)");
2149 /* Check that the modified regex is valid */
2154 return JB_ERR_MEMORY;
2156 err = create_url_spec(compiled, s);
2161 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2163 free_url_spec(compiled);
2170 /*********************************************************************
2172 * Function : map_radio
2174 * Description : Map a set of radio button values. E.g. if you have
2175 * 3 radio buttons, declare them as:
2176 * <option type="radio" name="xyz" @xyz-a@>
2177 * <option type="radio" name="xyz" @xyz-b@>
2178 * <option type="radio" name="xyz" @xyz-c@>
2179 * Then map one of the @xyz-?@ variables to "checked"
2180 * and all the others to empty by calling:
2181 * map_radio(exports, "xyz", "abc", sel)
2182 * Where 'sel' is 'a', 'b', or 'c'.
2185 * 1 : exports = Exports map to modify.
2186 * 2 : optionname = name for map
2187 * 3 : values = null-terminated list of values;
2188 * 4 : value = Selected value.
2190 * CGI Parameters : None
2192 * Returns : JB_ERR_OK on success
2193 * JB_ERR_MEMORY on out-of-memory
2195 *********************************************************************/
2196 static jb_err map_radio(struct map * exports,
2197 const char * optionname,
2198 const char * values,
2210 len = strlen(optionname);
2211 buf = malloc(len + 3);
2214 return JB_ERR_MEMORY;
2217 strcpy(buf, optionname);
2222 while ((c = *values++) != '\0')
2227 if (map(exports, buf, 1, "", 1))
2229 return JB_ERR_MEMORY;
2235 return map(exports, buf, 0, "checked", 1);
2239 /*********************************************************************
2241 * Function : cgi_error_modified
2243 * Description : CGI function that is called when a file is modified
2244 * outside the CGI editor.
2247 * 1 : csp = Current client state (buffers, headers, etc...)
2248 * 2 : rsp = http_response data structure for output
2249 * 3 : filename = The file that was modified.
2251 * CGI Parameters : none
2253 * Returns : JB_ERR_OK on success
2254 * JB_ERR_MEMORY on out-of-memory error.
2256 *********************************************************************/
2257 jb_err cgi_error_modified(struct client_state *csp,
2258 struct http_response *rsp,
2259 const char *filename)
2261 struct map *exports;
2268 if (NULL == (exports = default_exports(csp, NULL)))
2270 return JB_ERR_MEMORY;
2273 err = map(exports, "f", 1, html_encode(filename), 0);
2280 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2284 /*********************************************************************
2286 * Function : cgi_error_parse
2288 * Description : CGI function that is called when a file cannot
2289 * be parsed by the CGI editor.
2292 * 1 : csp = Current client state (buffers, headers, etc...)
2293 * 2 : rsp = http_response data structure for output
2294 * 3 : file = The file that was modified.
2296 * CGI Parameters : none
2298 * Returns : JB_ERR_OK on success
2299 * JB_ERR_MEMORY on out-of-memory error.
2301 *********************************************************************/
2302 jb_err cgi_error_parse(struct client_state *csp,
2303 struct http_response *rsp,
2304 struct editable_file *file)
2306 struct map *exports;
2308 struct file_line *cur_line;
2314 if (NULL == (exports = default_exports(csp, NULL)))
2316 return JB_ERR_MEMORY;
2319 err = map(exports, "f", 1, file->identifier, 1);
2320 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2322 cur_line = file->parse_error;
2325 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2326 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2334 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2338 /*********************************************************************
2340 * Function : cgi_error_file
2342 * Description : CGI function that is called when a file cannot be
2343 * opened by the CGI editor.
2346 * 1 : csp = Current client state (buffers, headers, etc...)
2347 * 2 : rsp = http_response data structure for output
2348 * 3 : filename = The file that was modified.
2350 * CGI Parameters : none
2352 * Returns : JB_ERR_OK on success
2353 * JB_ERR_MEMORY on out-of-memory error.
2355 *********************************************************************/
2356 jb_err cgi_error_file(struct client_state *csp,
2357 struct http_response *rsp,
2358 const char *filename)
2360 struct map *exports;
2367 if (NULL == (exports = default_exports(csp, NULL)))
2369 return JB_ERR_MEMORY;
2372 err = map(exports, "f", 1, html_encode(filename), 0);
2379 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2383 /*********************************************************************
2385 * Function : cgi_error_file
2387 * Description : CGI function that is called when a file cannot be
2388 * opened for writing by the CGI editor.
2391 * 1 : csp = Current client state (buffers, headers, etc...)
2392 * 2 : rsp = http_response data structure for output
2393 * 3 : filename = The file that we can't write to
2395 * CGI Parameters : none
2397 * Returns : JB_ERR_OK on success
2398 * JB_ERR_MEMORY on out-of-memory error.
2400 *********************************************************************/
2401 jb_err cgi_error_file_read_only(struct client_state *csp,
2402 struct http_response *rsp,
2403 const char *filename)
2405 struct map *exports;
2412 if (NULL == (exports = default_exports(csp, NULL)))
2414 return JB_ERR_MEMORY;
2417 err = map(exports, "f", 1, html_encode(filename), 0);
2424 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2428 /*********************************************************************
2430 * Function : cgi_edit_actions
2432 * Description : CGI function that allows the user to choose which
2433 * actions file to edit.
2436 * 1 : csp = Current client state (buffers, headers, etc...)
2437 * 2 : rsp = http_response data structure for output
2438 * 3 : parameters = map of cgi parameters
2440 * CGI Parameters : None
2442 * Returns : JB_ERR_OK on success
2443 * JB_ERR_MEMORY on out-of-memory error
2445 *********************************************************************/
2446 jb_err cgi_edit_actions(struct client_state *csp,
2447 struct http_response *rsp,
2448 const struct map *parameters)
2451 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2453 return cgi_error_disabled(csp, rsp);
2456 /* FIXME: Incomplete */
2458 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2463 /*********************************************************************
2465 * Function : cgi_edit_actions_list
2467 * Description : CGI function that edits the actions list.
2468 * FIXME: This function shouldn't FATAL ever.
2469 * FIXME: This function doesn't check the retval of map()
2471 * 1 : csp = Current client state (buffers, headers, etc...)
2472 * 2 : rsp = http_response data structure for output
2473 * 3 : parameters = map of cgi parameters
2475 * CGI Parameters : filename
2477 * Returns : JB_ERR_OK on success
2478 * JB_ERR_MEMORY on out-of-memory
2479 * JB_ERR_FILE if the file cannot be opened or
2481 * JB_ERR_CGI_PARAMS if "filename" was not specified
2484 *********************************************************************/
2485 jb_err cgi_edit_actions_list(struct client_state *csp,
2486 struct http_response *rsp,
2487 const struct map *parameters)
2489 char * section_template;
2490 char * url_template;
2495 struct map * exports;
2496 struct map * section_exports;
2497 struct map * url_exports;
2498 struct editable_file * file;
2499 struct file_line * cur_line;
2500 unsigned line_number = 0;
2501 unsigned prev_section_line_number = ((unsigned) (-1));
2503 struct file_list * fl;
2504 struct url_actions * b;
2505 char * buttons = NULL;
2508 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2510 return cgi_error_disabled(csp, rsp);
2513 if (NULL == (exports = default_exports(csp, NULL)))
2515 return JB_ERR_MEMORY;
2518 /* Load actions file */
2519 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2522 /* No filename specified, can't read file, or out of memory. */
2523 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2526 /* Find start of actions in file */
2527 cur_line = file->lines;
2529 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2531 cur_line = cur_line->next;
2536 * Conventional actions files should have a match all block
2538 * cur_line = {...global actions...}
2539 * cur_line->next = /
2540 * cur_line->next->next = {...actions...} or EOF
2542 if ( (cur_line != NULL)
2543 && (cur_line->type == FILE_LINE_ACTION)
2544 && (cur_line->next != NULL)
2545 && (cur_line->next->type == FILE_LINE_URL)
2546 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2547 && ( (cur_line->next->next == NULL)
2548 || (cur_line->next->next->type != FILE_LINE_URL)
2552 * Generate string with buttons to set actions for "/" to
2553 * any predefined set of actions (named standard.*, probably
2554 * residing in standard.action).
2557 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2560 edit_free_file(file);
2562 if (err == JB_ERR_FILE)
2564 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2569 err = template_fill(§ion_template, exports);
2572 edit_free_file(file);
2577 buttons = strdup("");
2578 for (i = 0; i < MAX_AF_FILES; i++)
2580 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2582 for (b = b->next; NULL != b; b = b->next)
2584 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2586 if (err || (NULL == (section_exports = new_map())))
2589 free(section_template);
2590 edit_free_file(file);
2592 return JB_ERR_MEMORY;
2595 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2597 if (err || (NULL == (s = strdup(section_template))))
2599 free_map(section_exports);
2601 free(section_template);
2602 edit_free_file(file);
2604 return JB_ERR_MEMORY;
2607 if (!err) err = template_fill(&s, section_exports);
2608 free_map(section_exports);
2609 if (!err) err = string_join(&buttons, s);
2614 freez(section_template);
2615 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2618 * Conventional actions file, supply extra editing help.
2619 * (e.g. don't allow them to make it an unconventional one).
2621 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2623 snprintf(buf, 150, "%d", line_number);
2624 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2625 snprintf(buf, 150, "%d", line_number + 2);
2626 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2627 if (!err) err = map(exports, "all-urls-actions", 1,
2628 actions_to_html(csp, cur_line->data.action), 0);
2630 /* Skip the 2 lines */
2631 cur_line = cur_line->next->next;
2635 * Note that prev_section_line_number is NOT set here.
2636 * This is deliberate and not a bug. It stops a "Move up"
2637 * option appearing on the next section. Clicking "Move
2638 * up" would make the actions file unconventional, which
2639 * we don't want, so we hide this option.
2645 * Non-standard actions file - does not begin with
2646 * the "All URLs" section.
2648 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2651 /* Set up global exports */
2653 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2654 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2656 /* Discourage private additions to default.action */
2658 if (!err) err = map_conditional(exports, "default-action",
2659 (strcmp("default", lookup(parameters, "f")) == 0));
2662 edit_free_file(file);
2667 /* Should do all global exports above this point */
2669 /* Load templates */
2671 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2674 edit_free_file(file);
2676 if (err == JB_ERR_FILE)
2678 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2683 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2686 free(section_template);
2687 edit_free_file(file);
2689 if (err == JB_ERR_FILE)
2691 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2696 err = template_fill(§ion_template, exports);
2700 edit_free_file(file);
2706 err = template_fill(&url_template, exports);
2709 free(section_template);
2710 edit_free_file(file);
2715 if (NULL == (sections = strdup("")))
2717 free(section_template);
2719 edit_free_file(file);
2721 return JB_ERR_MEMORY;
2724 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2726 if (NULL == (section_exports = new_map()))
2729 free(section_template);
2731 edit_free_file(file);
2733 return JB_ERR_MEMORY;
2736 snprintf(buf, 150, "%d", line_number);
2737 err = map(section_exports, "s", 1, buf, 1);
2738 if (!err) err = map(section_exports, "actions", 1,
2739 actions_to_html(csp, cur_line->data.action), 0);
2742 && (cur_line->next != NULL)
2743 && (cur_line->next->type == FILE_LINE_URL))
2745 /* This section contains at least one URL, don't allow delete */
2746 err = map_block_killer(section_exports, "empty-section");
2750 if (!err) err = map_block_keep(section_exports, "empty-section");
2753 if (prev_section_line_number != ((unsigned)(-1)))
2755 /* Not last section */
2756 snprintf(buf, 150, "%d", prev_section_line_number);
2757 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2758 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2763 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2765 prev_section_line_number = line_number;
2770 free(section_template);
2772 edit_free_file(file);
2774 free_map(section_exports);
2778 /* Should do all section-specific exports above this point */
2780 if (NULL == (urls = strdup("")))
2783 free(section_template);
2785 edit_free_file(file);
2787 free_map(section_exports);
2788 return JB_ERR_MEMORY;
2793 cur_line = cur_line->next;
2796 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2798 if (NULL == (url_exports = new_map()))
2802 free(section_template);
2804 edit_free_file(file);
2806 free_map(section_exports);
2807 return JB_ERR_MEMORY;
2810 snprintf(buf, 150, "%d", line_number);
2811 err = map(url_exports, "p", 1, buf, 1);
2813 snprintf(buf, 150, "%d", url_1_2);
2814 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2816 if (!err) err = map(url_exports, "url-html", 1,
2817 html_encode(cur_line->unprocessed), 0);
2818 if (!err) err = map(url_exports, "url", 1,
2819 url_encode(cur_line->unprocessed), 0);
2825 free(section_template);
2827 edit_free_file(file);
2829 free_map(section_exports);
2830 free_map(url_exports);
2834 if (NULL == (s = strdup(url_template)))
2838 free(section_template);
2840 edit_free_file(file);
2842 free_map(section_exports);
2843 free_map(url_exports);
2844 return JB_ERR_MEMORY;
2847 err = template_fill(&s, section_exports);
2848 if (!err) err = template_fill(&s, url_exports);
2849 if (!err) err = string_append(&urls, s);
2851 free_map(url_exports);
2858 free(section_template);
2860 edit_free_file(file);
2862 free_map(section_exports);
2866 url_1_2 = 3 - url_1_2;
2868 cur_line = cur_line->next;
2872 err = map(section_exports, "urls", 1, urls, 0);
2874 /* Could also do section-specific exports here, but it wouldn't be as fast */
2876 snprintf(buf, 150, "%d", line_number);
2877 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2879 if ( (cur_line != NULL)
2880 && (cur_line->type == FILE_LINE_ACTION))
2882 /* Not last section */
2883 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2888 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2894 free(section_template);
2896 edit_free_file(file);
2898 free_map(section_exports);
2902 if (NULL == (s = strdup(section_template)))
2905 free(section_template);
2907 edit_free_file(file);
2909 free_map(section_exports);
2910 return JB_ERR_MEMORY;
2913 err = template_fill(&s, section_exports);
2914 if (!err) err = string_append(§ions, s);
2917 free_map(section_exports);
2922 free(section_template);
2924 edit_free_file(file);
2930 edit_free_file(file);
2931 free(section_template);
2934 err = map(exports, "sections", 1, sections, 0);
2941 /* Could also do global exports here, but it wouldn't be as fast */
2943 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2947 /*********************************************************************
2949 * Function : cgi_edit_actions_for_url
2951 * Description : CGI function that edits the Actions list.
2954 * 1 : csp = Current client state (buffers, headers, etc...)
2955 * 2 : rsp = http_response data structure for output
2956 * 3 : parameters = map of cgi parameters
2958 * CGI Parameters : None
2960 * Returns : JB_ERR_OK on success
2961 * JB_ERR_MEMORY on out-of-memory
2962 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2963 * specified or not valid.
2965 *********************************************************************/
2966 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2967 struct http_response *rsp,
2968 const struct map *parameters)
2970 struct map * exports;
2972 struct editable_file * file;
2973 struct file_line * cur_line;
2974 unsigned line_number;
2976 struct re_filterfile_spec *filter_group;
2977 int i, have_filters = 0;
2979 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2981 return cgi_error_disabled(csp, rsp);
2984 err = get_number_param(csp, parameters, "s", §ionid);
2990 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2993 /* No filename specified, can't read file, modified, or out of memory. */
2994 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2997 cur_line = file->lines;
2999 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3001 cur_line = cur_line->next;
3004 if ( (cur_line == NULL)
3005 || (line_number != sectionid)
3007 || (cur_line->type != FILE_LINE_ACTION))
3009 /* Invalid "sectionid" parameter */
3010 edit_free_file(file);
3011 return JB_ERR_CGI_PARAMS;
3014 if (NULL == (exports = default_exports(csp, NULL)))
3016 edit_free_file(file);
3017 return JB_ERR_MEMORY;
3020 err = map(exports, "f", 1, file->identifier, 1);
3021 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3022 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3024 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3026 for (i = 0; i < MAX_AF_FILES; i++)
3028 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3030 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3038 edit_free_file(file);
3043 if (0 == have_filters)
3044 err = map(exports, "filter-params", 1, "", 1);
3047 /* We have some entries in the filter list */
3050 char * filter_template;
3052 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3055 edit_free_file(file);
3057 if (err == JB_ERR_FILE)
3059 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3064 err = template_fill(&filter_template, exports);
3066 result = strdup("");
3068 for (i = 0; i < MAX_AF_FILES; i++)
3070 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3072 filter_group = csp->rlist[i]->f;
3073 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3075 char current_mode = 'x';
3076 struct list_entry *filter_name;
3078 struct map *line_exports;
3081 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
3082 while ((filter_name != NULL)
3083 && (0 != strcmp(filter_group->name, filter_name->str)))
3085 filter_name = filter_name->next;
3088 if (filter_name != NULL)
3094 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
3095 while ((filter_name != NULL)
3096 && (0 != strcmp(filter_group->name, filter_name->str)))
3098 filter_name = filter_name->next;
3100 if (filter_name != NULL)
3106 /* Generate a unique serial number */
3107 snprintf(number, sizeof(number), "%x", index++);
3108 number[sizeof(number) - 1] = '\0';
3110 line_exports = new_map();
3111 if (line_exports == NULL)
3113 err = JB_ERR_MEMORY;
3118 if (!err) err = map(line_exports, "index", 1, number, 1);
3119 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3120 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3121 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3126 this_line = strdup(filter_template);
3127 if (this_line == NULL) err = JB_ERR_MEMORY;
3129 if (!err) err = template_fill(&this_line, line_exports);
3130 string_join(&result, this_line);
3132 free_map(line_exports);
3137 freez(filter_template);
3141 err = map(exports, "filter-params", 1, result, 0);
3149 if (!err) err = map_radio(exports, "filter-all", "nx",
3150 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3152 edit_free_file(file);
3160 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3164 /*********************************************************************
3166 * Function : cgi_edit_actions_submit
3168 * Description : CGI function that actually edits the Actions list.
3171 * 1 : csp = Current client state (buffers, headers, etc...)
3172 * 2 : rsp = http_response data structure for output
3173 * 3 : parameters = map of cgi parameters
3175 * CGI Parameters : None
3177 * Returns : JB_ERR_OK on success
3178 * JB_ERR_MEMORY on out-of-memory
3179 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3180 * specified or not valid.
3182 *********************************************************************/
3183 jb_err cgi_edit_actions_submit(struct client_state *csp,
3184 struct http_response *rsp,
3185 const struct map *parameters)
3191 struct editable_file * file;
3192 struct file_line * cur_line;
3193 unsigned line_number;
3197 const char * action_set_name;
3199 struct file_list * fl;
3200 struct url_actions * b;
3202 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3204 return cgi_error_disabled(csp, rsp);
3207 err = get_number_param(csp, parameters, "s", §ionid);
3213 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3216 /* No filename specified, can't read file, modified, or out of memory. */
3217 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3220 cur_line = file->lines;
3222 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3224 cur_line = cur_line->next;
3227 if ( (cur_line == NULL)
3228 || (line_number != sectionid)
3230 || (cur_line->type != FILE_LINE_ACTION))
3232 /* Invalid "sectionid" parameter */
3233 edit_free_file(file);
3234 return JB_ERR_CGI_PARAMS;
3237 get_string_param(parameters, "p", &action_set_name);
3238 if (action_set_name != NULL)
3240 for (index = 0; index < MAX_AF_FILES; index++)
3242 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3244 for (b = b->next; NULL != b; b = b->next)
3246 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3248 copy_action(cur_line->data.action, b->action);
3254 edit_free_file(file);
3255 return JB_ERR_CGI_PARAMS;
3261 err = actions_from_radio(parameters, cur_line->data.action);
3267 edit_free_file(file);
3271 ch = get_char_param(parameters, "filter_all");
3274 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3275 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3276 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3280 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3283 for (index = 0; !err; index++)
3290 /* Generate the keys */
3291 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3292 key_value[sizeof(key_value) - 1] = '\0';
3293 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3294 key_name[sizeof(key_name) - 1] = '\0';
3296 err = get_string_param(parameters, key_name, &name);
3305 value = get_char_param(parameters, key_value);
3308 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3309 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3310 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3312 else if (value == 'N')
3314 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3315 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3317 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3318 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3321 else if (value == 'X')
3323 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3324 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3331 edit_free_file(file);
3335 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3338 edit_free_file(file);
3339 return JB_ERR_MEMORY;
3342 len = strlen(actiontext);
3346 * Empty action - must special-case this.
3347 * Simply setting len to 1 is sufficient...
3352 if (NULL == (newtext = malloc(len + 2)))
3356 edit_free_file(file);
3357 return JB_ERR_MEMORY;
3359 strcpy(newtext, actiontext);
3363 newtext[len + 1] = '\0';
3365 freez(cur_line->raw);
3366 freez(cur_line->unprocessed);
3367 cur_line->unprocessed = newtext;
3369 err = edit_write_file(file);
3372 /* Error writing file */
3373 if (err == JB_ERR_FILE)
3375 /* Read-only file. */
3376 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3378 edit_free_file(file);
3382 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3383 (long) time(NULL), file->identifier, sectionid);
3385 edit_free_file(file);
3387 return cgi_redirect(rsp, target);
3391 /*********************************************************************
3393 * Function : cgi_edit_actions_url
3395 * Description : CGI function that actually edits a URL pattern in
3399 * 1 : csp = Current client state (buffers, headers, etc...)
3400 * 2 : rsp = http_response data structure for output
3401 * 3 : parameters = map of cgi parameters
3404 * filename : Identifies the file to edit
3405 * ver : File's last-modified time
3406 * section : Line number of section to edit
3407 * pattern : Line number of pattern to edit
3408 * newval : New value for pattern
3410 * Returns : JB_ERR_OK on success
3411 * JB_ERR_MEMORY on out-of-memory
3412 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3413 * specified or not valid.
3415 *********************************************************************/
3416 jb_err cgi_edit_actions_url(struct client_state *csp,
3417 struct http_response *rsp,
3418 const struct map *parameters)
3422 struct editable_file * file;
3423 struct file_line * cur_line;
3424 unsigned line_number;
3425 unsigned section_start_line_number = 0;
3433 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3435 return cgi_error_disabled(csp, rsp);
3438 err = get_number_param(csp, parameters, "p", &patternid);
3445 return JB_ERR_CGI_PARAMS;
3448 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3454 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3457 /* No filename specified, can't read file, modified, or out of memory. */
3459 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3463 cur_line = file->lines;
3465 while ((cur_line != NULL) && (line_number < patternid))
3467 if (cur_line->type == FILE_LINE_ACTION)
3469 section_start_line_number = line_number;
3471 cur_line = cur_line->next;
3475 if ( (cur_line == NULL)
3476 || (cur_line->type != FILE_LINE_URL))
3478 /* Invalid "patternid" parameter */
3480 edit_free_file(file);
3481 return JB_ERR_CGI_PARAMS;
3484 /* At this point, the line to edit is in cur_line */
3486 freez(cur_line->raw);
3487 freez(cur_line->unprocessed);
3488 cur_line->unprocessed = new_pattern;
3490 err = edit_write_file(file);
3493 /* Error writing file */
3494 if (err == JB_ERR_FILE)
3496 /* Read-only file. */
3497 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3499 edit_free_file(file);
3503 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3504 (long) time(NULL), file->identifier, section_start_line_number);
3506 edit_free_file(file);
3508 return cgi_redirect(rsp, target);
3512 /*********************************************************************
3514 * Function : cgi_edit_actions_add_url
3516 * Description : CGI function that actually adds a URL pattern to
3520 * 1 : csp = Current client state (buffers, headers, etc...)
3521 * 2 : rsp = http_response data structure for output
3522 * 3 : parameters = map of cgi parameters
3525 * filename : Identifies the file to edit
3526 * ver : File's last-modified time
3527 * section : Line number of section to edit
3528 * newval : New pattern
3530 * Returns : JB_ERR_OK on success
3531 * JB_ERR_MEMORY on out-of-memory
3532 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3533 * specified or not valid.
3535 *********************************************************************/
3536 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3537 struct http_response *rsp,
3538 const struct map *parameters)
3542 struct file_line * new_line;
3543 struct editable_file * file;
3544 struct file_line * cur_line;
3545 unsigned line_number;
3549 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3551 return cgi_error_disabled(csp, rsp);
3554 err = get_number_param(csp, parameters, "s", §ionid);
3561 return JB_ERR_CGI_PARAMS;
3564 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3570 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3573 /* No filename specified, can't read file, modified, or out of memory. */
3575 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3579 cur_line = file->lines;
3581 while ((cur_line != NULL) && (line_number < sectionid))
3583 cur_line = cur_line->next;
3587 if ( (cur_line == NULL)
3588 || (cur_line->type != FILE_LINE_ACTION))
3590 /* Invalid "sectionid" parameter */
3592 edit_free_file(file);
3593 return JB_ERR_CGI_PARAMS;
3596 /* At this point, the section header is in cur_line - add after this. */
3598 /* Allocate the new line */
3599 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3600 if (new_line == NULL)
3603 edit_free_file(file);
3604 return JB_ERR_MEMORY;
3607 /* Fill in the data members of the new line */
3608 new_line->raw = NULL;
3609 new_line->prefix = NULL;
3610 new_line->unprocessed = new_pattern;
3611 new_line->type = FILE_LINE_URL;
3613 /* Link new_line into the list, after cur_line */
3614 new_line->next = cur_line->next;
3615 cur_line->next = new_line;
3617 /* Done making changes, now commit */
3619 err = edit_write_file(file);
3622 /* Error writing file */
3623 if (err == JB_ERR_FILE)
3625 /* Read-only file. */
3626 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3628 edit_free_file(file);
3632 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3633 (long) time(NULL), file->identifier, sectionid);
3635 edit_free_file(file);
3637 return cgi_redirect(rsp, target);
3641 /*********************************************************************
3643 * Function : cgi_edit_actions_remove_url
3645 * Description : CGI function that actually removes a URL pattern from
3649 * 1 : csp = Current client state (buffers, headers, etc...)
3650 * 2 : rsp = http_response data structure for output
3651 * 3 : parameters = map of cgi parameters
3654 * f : (filename) Identifies the file to edit
3655 * v : (version) File's last-modified time
3656 * p : (pattern) Line number of pattern to remove
3658 * Returns : JB_ERR_OK on success
3659 * JB_ERR_MEMORY on out-of-memory
3660 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3661 * specified or not valid.
3663 *********************************************************************/
3664 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3665 struct http_response *rsp,
3666 const struct map *parameters)
3669 struct editable_file * file;
3670 struct file_line * cur_line;
3671 struct file_line * prev_line;
3672 unsigned line_number;
3673 unsigned section_start_line_number = 0;
3677 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3679 return cgi_error_disabled(csp, rsp);
3682 err = get_number_param(csp, parameters, "p", &patternid);
3688 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3691 /* No filename specified, can't read file, modified, or out of memory. */
3692 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3697 cur_line = file->lines;
3699 while ((cur_line != NULL) && (line_number < patternid))
3701 if (cur_line->type == FILE_LINE_ACTION)
3703 section_start_line_number = line_number;
3705 prev_line = cur_line;
3706 cur_line = cur_line->next;
3710 if ( (cur_line == NULL)
3711 || (prev_line == NULL)
3712 || (cur_line->type != FILE_LINE_URL))
3714 /* Invalid "patternid" parameter */
3715 edit_free_file(file);
3716 return JB_ERR_CGI_PARAMS;
3719 /* At this point, the line to remove is in cur_line, and the previous
3720 * one is in prev_line
3723 /* Unlink cur_line */
3724 prev_line->next = cur_line->next;
3725 cur_line->next = NULL;
3728 edit_free_file_lines(cur_line);
3730 err = edit_write_file(file);
3733 /* Error writing file */
3734 if (err == JB_ERR_FILE)
3736 /* Read-only file. */
3737 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3739 edit_free_file(file);
3743 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3744 (long) time(NULL), file->identifier, section_start_line_number);
3746 edit_free_file(file);
3748 return cgi_redirect(rsp, target);
3752 /*********************************************************************
3754 * Function : cgi_edit_actions_section_remove
3756 * Description : CGI function that actually removes a whole section from
3757 * the actions file. The section must be empty first
3758 * (else JB_ERR_CGI_PARAMS).
3761 * 1 : csp = Current client state (buffers, headers, etc...)
3762 * 2 : rsp = http_response data structure for output
3763 * 3 : parameters = map of cgi parameters
3766 * f : (filename) Identifies the file to edit
3767 * v : (version) File's last-modified time
3768 * s : (section) Line number of section to edit
3770 * Returns : JB_ERR_OK on success
3771 * JB_ERR_MEMORY on out-of-memory
3772 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3773 * specified or not valid.
3775 *********************************************************************/
3776 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3777 struct http_response *rsp,
3778 const struct map *parameters)
3781 struct editable_file * file;
3782 struct file_line * cur_line;
3783 struct file_line * prev_line;
3784 unsigned line_number;
3788 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3790 return cgi_error_disabled(csp, rsp);
3793 err = get_number_param(csp, parameters, "s", §ionid);
3799 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3802 /* No filename specified, can't read file, modified, or out of memory. */
3803 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3807 cur_line = file->lines;
3810 while ((cur_line != NULL) && (line_number < sectionid))
3812 prev_line = cur_line;
3813 cur_line = cur_line->next;
3817 if ( (cur_line == NULL)
3818 || (cur_line->type != FILE_LINE_ACTION) )
3820 /* Invalid "sectionid" parameter */
3821 edit_free_file(file);
3822 return JB_ERR_CGI_PARAMS;
3825 if ( (cur_line->next != NULL)
3826 && (cur_line->next->type == FILE_LINE_URL) )
3828 /* Section not empty. */
3829 edit_free_file(file);
3830 return JB_ERR_CGI_PARAMS;
3833 /* At this point, the line to remove is in cur_line, and the previous
3834 * one is in prev_line
3837 /* Unlink cur_line */
3838 if (prev_line == NULL)
3840 /* Removing the first line from the file */
3841 file->lines = cur_line->next;
3845 prev_line->next = cur_line->next;
3847 cur_line->next = NULL;
3850 edit_free_file_lines(cur_line);
3852 err = edit_write_file(file);
3855 /* Error writing file */
3856 if (err == JB_ERR_FILE)
3858 /* Read-only file. */
3859 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3861 edit_free_file(file);
3865 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
3866 (long) time(NULL), file->identifier);
3868 edit_free_file(file);
3870 return cgi_redirect(rsp, target);
3874 /*********************************************************************
3876 * Function : cgi_edit_actions_section_add
3878 * Description : CGI function that adds a new empty section to
3882 * 1 : csp = Current client state (buffers, headers, etc...)
3883 * 2 : rsp = http_response data structure for output
3884 * 3 : parameters = map of cgi parameters
3887 * f : (filename) Identifies the file to edit
3888 * v : (version) File's last-modified time
3889 * s : (section) Line number of section to add after, 0 for
3892 * Returns : JB_ERR_OK on success
3893 * JB_ERR_MEMORY on out-of-memory
3894 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3895 * specified or not valid.
3897 *********************************************************************/
3898 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3899 struct http_response *rsp,
3900 const struct map *parameters)
3903 struct file_line * new_line;
3905 struct editable_file * file;
3906 struct file_line * cur_line;
3907 unsigned line_number;
3911 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3913 return cgi_error_disabled(csp, rsp);
3916 err = get_number_param(csp, parameters, "s", §ionid);
3922 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3925 /* No filename specified, can't read file, modified, or out of memory. */
3926 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3930 cur_line = file->lines;
3932 if (sectionid <= 1U)
3934 /* Add to start of file */
3935 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
3937 /* There's something in the file, find the line before the first
3940 while ( (cur_line->next != NULL)
3941 && (cur_line->next->type != FILE_LINE_ACTION) )
3943 cur_line = cur_line->next;
3949 /* File starts with action line, so insert at top */
3955 /* Add after stated section. */
3956 while ((cur_line != NULL) && (line_number < sectionid))
3958 cur_line = cur_line->next;
3962 if ( (cur_line == NULL)
3963 || (cur_line->type != FILE_LINE_ACTION))
3965 /* Invalid "sectionid" parameter */
3966 edit_free_file(file);
3967 return JB_ERR_CGI_PARAMS;
3970 /* Skip through the section to find the last line in it. */
3971 while ( (cur_line->next != NULL)
3972 && (cur_line->next->type != FILE_LINE_ACTION) )
3974 cur_line = cur_line->next;
3979 /* At this point, the last line in the previous section is in cur_line
3980 * - add after this. (Or if we need to add as the first line, cur_line
3984 new_text = strdup("{}");
3985 if (NULL == new_text)
3987 edit_free_file(file);
3988 return JB_ERR_MEMORY;
3991 /* Allocate the new line */
3992 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3993 if (new_line == NULL)
3996 edit_free_file(file);
3997 return JB_ERR_MEMORY;
4000 /* Fill in the data members of the new line */
4001 new_line->raw = NULL;
4002 new_line->prefix = NULL;
4003 new_line->unprocessed = new_text;
4004 new_line->type = FILE_LINE_ACTION;
4006 if (cur_line != NULL)
4008 /* Link new_line into the list, after cur_line */
4009 new_line->next = cur_line->next;
4010 cur_line->next = new_line;
4014 /* Link new_line into the list, as first line */
4015 new_line->next = file->lines;
4016 file->lines = new_line;
4019 /* Done making changes, now commit */
4021 err = edit_write_file(file);
4024 /* Error writing file */
4025 if (err == JB_ERR_FILE)
4027 /* Read-only file. */
4028 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4030 edit_free_file(file);
4034 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4035 (long) time(NULL), file->identifier);
4037 edit_free_file(file);
4039 return cgi_redirect(rsp, target);
4043 /*********************************************************************
4045 * Function : cgi_edit_actions_section_swap
4047 * Description : CGI function that swaps the order of two sections
4048 * in the actions file. Note that this CGI can actually
4049 * swap any two arbitrary sections, but the GUI interface
4050 * currently only allows consecutive sections to be
4054 * 1 : csp = Current client state (buffers, headers, etc...)
4055 * 2 : rsp = http_response data structure for output
4056 * 3 : parameters = map of cgi parameters
4059 * f : (filename) Identifies the file to edit
4060 * v : (version) File's last-modified time
4061 * s1 : (section1) Line number of first section to swap
4062 * s2 : (section2) Line number of second section to swap
4064 * Returns : JB_ERR_OK on success
4065 * JB_ERR_MEMORY on out-of-memory
4066 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4067 * specified or not valid.
4069 *********************************************************************/
4070 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4071 struct http_response *rsp,
4072 const struct map *parameters)
4076 struct editable_file * file;
4077 struct file_line * cur_line;
4078 struct file_line * prev_line;
4079 struct file_line * line_before_section1;
4080 struct file_line * line_start_section1;
4081 struct file_line * line_end_section1;
4082 struct file_line * line_after_section1;
4083 struct file_line * line_before_section2;
4084 struct file_line * line_start_section2;
4085 struct file_line * line_end_section2;
4086 struct file_line * line_after_section2;
4087 unsigned line_number;
4091 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4093 return cgi_error_disabled(csp, rsp);
4096 err = get_number_param(csp, parameters, "s1", §ion1);
4097 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4103 if (section1 > section2)
4105 unsigned temp = section2;
4106 section2 = section1;
4110 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4113 /* No filename specified, can't read file, modified, or out of memory. */
4114 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4117 /* Start at the beginning... */
4119 cur_line = file->lines;
4122 /* ... find section1 ... */
4123 while ((cur_line != NULL) && (line_number < section1))
4125 prev_line = cur_line;
4126 cur_line = cur_line->next;
4130 if ( (cur_line == NULL)
4131 || (cur_line->type != FILE_LINE_ACTION) )
4133 /* Invalid "section1" parameter */
4134 edit_free_file(file);
4135 return JB_ERR_CGI_PARAMS;
4138 /* If no-op, we've validated params and can skip the rest. */
4139 if (section1 != section2)
4141 /* ... find the end of section1 ... */
4142 line_before_section1 = prev_line;
4143 line_start_section1 = cur_line;
4146 prev_line = cur_line;
4147 cur_line = cur_line->next;
4150 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4151 line_end_section1 = prev_line;
4152 line_after_section1 = cur_line;
4154 /* ... find section2 ... */
4155 while ((cur_line != NULL) && (line_number < section2))
4157 prev_line = cur_line;
4158 cur_line = cur_line->next;
4162 if ( (cur_line == NULL)
4163 || (cur_line->type != FILE_LINE_ACTION) )
4165 /* Invalid "section2" parameter */
4166 edit_free_file(file);
4167 return JB_ERR_CGI_PARAMS;
4170 /* ... find the end of section2 ... */
4171 line_before_section2 = prev_line;
4172 line_start_section2 = cur_line;
4175 prev_line = cur_line;
4176 cur_line = cur_line->next;
4179 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4180 line_end_section2 = prev_line;
4181 line_after_section2 = cur_line;
4183 /* Now have all the pointers we need. Do the swap. */
4185 /* Change the pointer to section1 to point to section2 instead */
4186 if (line_before_section1 == NULL)
4188 file->lines = line_start_section2;
4192 line_before_section1->next = line_start_section2;
4195 if (line_before_section2 == line_end_section1)
4197 /* Consecutive sections */
4198 line_end_section2->next = line_start_section1;
4202 line_end_section2->next = line_after_section1;
4203 line_before_section2->next = line_start_section1;
4206 /* Set the pointer from the end of section1 to the rest of the file */
4207 line_end_section1->next = line_after_section2;
4209 err = edit_write_file(file);
4212 /* Error writing file */
4213 if (err == JB_ERR_FILE)
4215 /* Read-only file. */
4216 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4218 edit_free_file(file);
4221 } /* END if (section1 != section2) */
4223 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4224 (long) time(NULL), file->identifier);
4226 edit_free_file(file);
4228 return cgi_redirect(rsp, target);
4232 /*********************************************************************
4234 * Function : cgi_toggle
4236 * Description : CGI function that adds a new empty section to
4240 * 1 : csp = Current client state (buffers, headers, etc...)
4241 * 2 : rsp = http_response data structure for output
4242 * 3 : parameters = map of cgi parameters
4245 * set : If present, how to change toggle setting:
4246 * "enable", "disable", "toggle", or none (default).
4247 * mini : If present, use mini reply template.
4249 * Returns : JB_ERR_OK on success
4250 * JB_ERR_MEMORY on out-of-memory
4252 *********************************************************************/
4253 jb_err cgi_toggle(struct client_state *csp,
4254 struct http_response *rsp,
4255 const struct map *parameters)
4257 struct map *exports;
4259 const char *template_name;
4265 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4267 return cgi_error_disabled(csp, rsp);
4270 mode = get_char_param(parameters, "set");
4275 global_toggle_state = 1;
4277 else if (mode == 'D')
4280 global_toggle_state = 0;
4282 else if (mode == 'T')
4285 global_toggle_state = !global_toggle_state;
4288 if (NULL == (exports = default_exports(csp, "toggle")))
4290 return JB_ERR_MEMORY;
4293 template_name = (get_char_param(parameters, "mini")
4297 return template_fill_for_cgi(csp, template_name, exports, rsp);
4301 /*********************************************************************
4303 * Function : javascriptify
4305 * Description : Converts a string into a form JavaScript will like.
4307 * Netscape 4's JavaScript sucks - it doesn't use
4308 * "id" parameters, so you have to set the "name"
4309 * used to submit a form element to something JavaScript
4310 * will like. (Or access the elements by index in an
4311 * array. That array contains >60 elements and will
4312 * be changed whenever we add a new action to the
4313 * editor, so I'm NOT going to use indexes that have
4314 * to be figured out by hand.)
4316 * Currently the only thing we have to worry about
4317 * is "-" ==> "_" conversion.
4319 * This is a length-preserving operation so it is
4320 * carried out in-place, no memory is allocated
4324 * 1 : identifier = String to make JavaScript-friendly.
4328 *********************************************************************/
4329 static void javascriptify(char * identifier)
4331 char * p = identifier;
4332 while (NULL != (p = strchr(p, '-')))
4339 /*********************************************************************
4341 * Function : actions_to_radio
4343 * Description : Converts a actionsfile entry into settings for
4344 * radio buttons and edit boxes on a HTML form.
4347 * 1 : exports = List of substitutions to add to.
4348 * 2 : action = Action to read
4350 * Returns : JB_ERR_OK on success
4351 * JB_ERR_MEMORY on out-of-memory
4353 *********************************************************************/
4354 static jb_err actions_to_radio(struct map * exports,
4355 const struct action_spec *action)
4357 unsigned mask = action->mask;
4358 unsigned add = action->add;
4366 mask = action->mask;
4369 /* sanity - prevents "-feature +feature" */
4373 #define DEFINE_ACTION_BOOL(name, bit) \
4374 if (!(mask & bit)) \
4376 current_mode = 'n'; \
4378 else if (add & bit) \
4380 current_mode = 'y'; \
4384 current_mode = 'x'; \
4386 if (map_radio(exports, name, "ynx", current_mode)) \
4388 return JB_ERR_MEMORY; \
4391 #define DEFINE_ACTION_STRING(name, bit, index) \
4392 DEFINE_ACTION_BOOL(name, bit); \
4395 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4398 checked = !strcmp(action->string[index], value); \
4402 checked = is_default; \
4404 mapped_param |= checked; \
4405 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4407 return JB_ERR_MEMORY; \
4410 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4411 if (map(exports, name "-param-custom", 1, \
4412 ((!mapped_param) ? "checked" : ""), 1)) \
4414 return JB_ERR_MEMORY; \
4416 if (map(exports, name "-param", 1, \
4417 (((add & bit) && !mapped_param) ? \
4418 action->string[index] : default_val), 1)) \
4420 return JB_ERR_MEMORY; \
4423 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4424 if (map(exports, name "-param", 1, \
4425 ((add & bit) ? action->string[index] : default_val), 1)) \
4427 return JB_ERR_MEMORY; \
4430 #define DEFINE_ACTION_MULTI(name, index) \
4431 if (action->multi_add[index]->first) \
4433 current_mode = 'y'; \
4435 else if (action->multi_remove_all[index]) \
4437 current_mode = 'n'; \
4439 else if (action->multi_remove[index]->first) \
4441 current_mode = 'y'; \
4445 current_mode = 'x'; \
4447 if (map_radio(exports, name, "ynx", current_mode)) \
4449 return JB_ERR_MEMORY; \
4452 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4454 #include "actionlist.h"
4456 #undef DEFINE_ACTION_MULTI
4457 #undef DEFINE_ACTION_STRING
4458 #undef DEFINE_ACTION_BOOL
4459 #undef DEFINE_ACTION_ALIAS
4460 #undef DEFINE_CGI_PARAM_CUSTOM
4461 #undef DEFINE_CGI_PARAM_RADIO
4462 #undef DEFINE_CGI_PARAM_NO_RADIO
4468 /*********************************************************************
4470 * Function : actions_from_radio
4472 * Description : Converts a map of parameters passed to a CGI function
4473 * into an actionsfile entry.
4476 * 1 : parameters = parameters to the CGI call
4477 * 2 : action = Action to change. Must be valid before
4478 * the call, actions not specified will be
4481 * Returns : JB_ERR_OK on success
4482 * JB_ERR_MEMORY on out-of-memory
4484 *********************************************************************/
4485 static jb_err actions_from_radio(const struct map * parameters,
4486 struct action_spec *action)
4491 const char * js_name;
4492 jb_err err = JB_ERR_OK;
4497 /* Statics are generally a potential race condition,
4498 * but in this case we're safe and don't need semaphores.
4499 * Be careful if you modify this function.
4501 * The js_name_arr's are never free()d, but this is no
4502 * problem, since they will only be created once and
4503 * used by all threads thereafter. -oes
4506 #define JAVASCRIPTIFY(dest_var, string) \
4508 static int first_time = 1; \
4509 static char *js_name_arr; \
4512 js_name_arr = strdup(string); \
4513 javascriptify(js_name_arr); \
4515 dest_var = js_name_arr; \
4519 #define DEFINE_ACTION_BOOL(name, bit) \
4520 JAVASCRIPTIFY(js_name, name); \
4521 ch = get_char_param(parameters, js_name); \
4524 action->add |= bit; \
4525 action->mask |= bit; \
4527 else if (ch == 'N') \
4529 action->add &= ~bit; \
4530 action->mask &= ~bit; \
4532 else if (ch == 'X') \
4534 action->add &= ~bit; \
4535 action->mask |= bit; \
4538 #define DEFINE_ACTION_STRING(name, bit, index) \
4539 JAVASCRIPTIFY(js_name, name); \
4540 ch = get_char_param(parameters, js_name); \
4544 JAVASCRIPTIFY(js_name, name "-mode"); \
4545 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4546 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4548 JAVASCRIPTIFY(js_name, name "-param"); \
4549 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4551 if (param != NULL) \
4553 if (NULL == (param_dup = strdup(param))) \
4555 return JB_ERR_MEMORY; \
4557 freez(action->string[index]); \
4558 action->add |= bit; \
4559 action->mask |= bit; \
4560 action->string[index] = param_dup; \
4563 else if (ch == 'N') \
4565 if (action->add & bit) \
4567 freez(action->string[index]); \
4569 action->add &= ~bit; \
4570 action->mask &= ~bit; \
4572 else if (ch == 'X') \
4574 if (action->add & bit) \
4576 freez(action->string[index]); \
4578 action->add &= ~bit; \
4579 action->mask |= bit; \
4582 #define DEFINE_ACTION_MULTI(name, index) \
4583 JAVASCRIPTIFY(js_name, name); \
4584 ch = get_char_param(parameters, js_name); \
4589 else if (ch == 'N') \
4591 list_remove_all(action->multi_add[index]); \
4592 list_remove_all(action->multi_remove[index]); \
4593 action->multi_remove_all[index] = 1; \
4595 else if (ch == 'X') \
4597 list_remove_all(action->multi_add[index]); \
4598 list_remove_all(action->multi_remove[index]); \
4599 action->multi_remove_all[index] = 0; \
4602 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4604 #include "actionlist.h"
4606 #undef DEFINE_ACTION_MULTI
4607 #undef DEFINE_ACTION_STRING
4608 #undef DEFINE_ACTION_BOOL
4609 #undef DEFINE_ACTION_ALIAS
4610 #undef JAVASCRIPTIFY
4616 #endif /* def FEATURE_CGI_EDIT_ACTIONS */