1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.51 2007/04/08 13:21:05 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
10 * NOTE: The CGIs in this file use parameter names
11 * such as "f" and "s" which are really *BAD* choices.
12 * However, I'm trying to save bytes in the
13 * edit-actions-list HTML page - the standard actions
14 * file generated a 550kbyte page, which is ridiculous.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001-2007 the SourceForge
19 * Privoxy team. http://www.privoxy.org/
21 * Based on the Internet Junkbuster originally written
22 * by and Copyright (C) 1997 Anonymous Coders and
23 * Junkbusters Corporation. http://www.junkbusters.com
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General
27 * Public License as published by the Free Software
28 * Foundation; either version 2 of the License, or (at
29 * your option) any later version.
31 * This program is distributed in the hope that it will
32 * be useful, but WITHOUT ANY WARRANTY; without even the
33 * implied warranty of MERCHANTABILITY or FITNESS FOR A
34 * PARTICULAR PURPOSE. See the GNU General Public
35 * License for more details.
37 * The GNU General Public License should be included with
38 * this file. If not, you can view it at
39 * http://www.gnu.org/copyleft/gpl.html
40 * or write to the Free Software Foundation, Inc., 59
41 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 * Revision 1.51 2007/04/08 13:21:05 fabiankeil
46 * Reference action files in CGI URLs by id instead
47 * of using the first part of the file name.
48 * Fixes BR 1694250 and BR 1590556.
50 * Revision 1.50 2007/03/29 11:40:34 fabiankeil
51 * Divide @filter-params@ into @client-header-filter-params@
52 * @content-filter-params@ and @server-header-filter-params@.
54 * Revision 1.49 2007/03/20 15:16:34 fabiankeil
55 * Use dedicated header filter actions instead of abusing "filter".
56 * Replace "filter-client-headers" and "filter-client-headers"
57 * with "server-header-filter" and "client-header-filter".
59 * Revision 1.48 2007/02/13 14:35:25 fabiankeil
60 * Replace hash escaping code to prevent
61 * crashes, memory and file corruption.
63 * Revision 1.47 2006/12/28 18:04:25 fabiankeil
64 * Fixed gcc43 conversion warnings.
66 * Revision 1.46 2006/12/27 18:44:52 fabiankeil
67 * Stop shadowing string.h's index().
69 * Revision 1.45 2006/12/21 12:57:48 fabiankeil
70 * Add config option "split-large-forms"
71 * to work around the browser bug reported
74 * Revision 1.44 2006/12/09 13:49:16 fabiankeil
75 * Fix configure option --disable-toggle.
76 * Thanks to Peter Thoenen for reporting this.
78 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
79 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
80 * with what was really the latest development (the v_3_0_branch branch)
82 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
83 * Remove a little residual debugging info
85 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
86 * Multiple filter file support
88 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
89 * Undo some damaging memory leak patches
91 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
92 * Removing a double free
94 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
95 * A few memory leaks plugged (mostly on error paths)
97 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
98 * Moved cgi_error_disabled() from cgiedit.c to
99 * cgi.c to re-enable build with --disable-editor.
100 * Fixes Bug #892744. Thanks to Matthew Fischer
103 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
104 * One line lost in last commit
106 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
107 * - All edit functions that redirect back to the list page
108 * now use cgi_redirect
109 * - All redirects now contain useless parameter "foo", whose
110 * value are raw seconds since epoch, in order to force
111 * Opera and Konqueror to properly reload the list. Closes
114 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
115 * Cosmetic: Renamed cryptic variable
117 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
118 * Fix: Don't free uninitialized struct editable_file
120 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
121 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
123 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
124 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
126 * Revision 1.41 2002/05/21 19:09:45 oes
127 * - Made Add/Edit/Remove URL Submit and Cancel
128 * buttons jump back to relevant section in eal
129 * - Bugfix: remove-url-form needs p export
131 * Revision 1.40 2002/05/19 11:34:35 jongfoster
132 * Handling read-only actions files better - report the actual
133 * error, not "Out of memory"!
136 * http://sourceforge.net/tracker/index.php?func=detail
137 * &aid=557905&group_id=11118&atid=111118
139 * Revision 1.39 2002/05/12 21:39:15 jongfoster
140 * - Adding Doxygen-style comments to structures and #defines.
141 * - Correcting function comments
143 * Revision 1.38 2002/05/03 23:00:38 jongfoster
144 * Support for templates for "standard actions" buttons.
147 * Revision 1.37 2002/04/30 11:14:52 oes
148 * Made csp the first parameter in *action_to_html
150 * Revision 1.36 2002/04/26 21:53:30 jongfoster
151 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
153 * Revision 1.35 2002/04/26 21:50:02 jongfoster
154 * Honouring default exports in edit-actions-for-url-filter template.
156 * Revision 1.34 2002/04/26 12:54:17 oes
157 * Adaptions to changes in actions.c
159 * Revision 1.33 2002/04/24 02:17:47 oes
160 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
162 * - Activated Jon's code for editing multiple AFs
163 * - cgi_edit_list_actions now provides context-sensitive
164 * help, looks up all action sets from standard.action and
165 * makes buttons for them in the catchall section
166 * - cgi_edit_action_submit now honors a p parameter, looks up
167 * the corresponding action set, and sets the catchall pattern's
168 * actions accordingly.
170 * Revision 1.32 2002/04/19 16:55:31 jongfoster
171 * Fixing newline problems. If we do our own text file newline
172 * mangling, we don't want the library to do any, so we need to
173 * open the files in *binary* mode.
175 * Revision 1.31 2002/04/18 19:21:08 jongfoster
176 * Added code to detect "conventional" action files, that start
177 * with a set of actions for all URLs (the pattern "/").
178 * These are special-cased in the "edit-actions-list" CGI, so
179 * that a special UI can be written for them.
181 * Revision 1.30 2002/04/10 13:38:35 oes
182 * load_template signature changed
184 * Revision 1.29 2002/04/08 16:59:08 oes
187 * Revision 1.28 2002/03/27 12:30:29 oes
188 * Deleted unsused variable
190 * Revision 1.27 2002/03/26 23:06:04 jongfoster
191 * Removing duplicate @ifs on the toggle page
193 * Revision 1.26 2002/03/26 22:59:17 jongfoster
194 * Fixing /toggle to display status consistently.
196 * Revision 1.25 2002/03/26 22:29:54 swa
197 * we have a new homepage!
199 * Revision 1.24 2002/03/24 15:23:33 jongfoster
202 * Revision 1.23 2002/03/24 13:32:41 swa
203 * name change related issues
205 * Revision 1.22 2002/03/24 13:25:43 swa
206 * name change related issues
208 * Revision 1.21 2002/03/22 18:02:48 jongfoster
209 * Fixing remote toggle
211 * Revision 1.20 2002/03/16 20:28:34 oes
212 * Added descriptions to the filters so users will know what they select in the cgi editor
214 * Revision 1.19 2002/03/16 18:38:14 jongfoster
215 * Stopping stupid or malicious users from breaking the actions
216 * file using the web-based editor.
218 * Revision 1.18 2002/03/16 14:57:44 jongfoster
219 * Full support for enabling/disabling modular filters.
221 * Revision 1.17 2002/03/16 14:26:42 jongfoster
222 * First version of modular filters support - READ ONLY!
223 * Fixing a double-free bug in the out-of-memory handling in map_radio().
225 * Revision 1.16 2002/03/07 03:46:17 oes
226 * Fixed compiler warnings
228 * Revision 1.15 2002/03/06 22:54:35 jongfoster
229 * Automated function-comment nitpicking.
231 * Revision 1.14 2002/03/05 00:24:51 jongfoster
232 * Patch to always edit the current actions file.
234 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
235 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
237 * Revision 1.12 2002/03/03 09:18:03 joergs
238 * Made jumbjuster work on AmigaOS again.
240 * Revision 1.11 2002/01/23 01:03:31 jongfoster
241 * Fixing gcc [CygWin] compiler warnings
243 * Revision 1.10 2002/01/23 00:22:59 jongfoster
244 * Adding new function cgi_edit_actions_section_swap(), to reorder
247 * Adding get_url_spec_param() to get a validated URL pattern.
249 * Moving edit_read_line() out of this file and into loaders.c.
251 * Adding missing html_encode() to many CGI functions.
253 * Moving the functions that #include actionlist.h to the end of the file,
254 * because the Visual C++ 97 debugger gets extremely confused if you try
255 * to debug any code that comes after them in the file.
257 * Major optimizations in cgi_edit_actions_list() to reduce the size of
258 * the generated HTML (down 40% from 550k to 304k), with major side-effects
259 * throughout the editor and templates. In particular, the length of the
260 * URLs throughout the editor has been drastically reduced, by cutting
261 * paramater names down to 1 character and CGI names down to 3-4
262 * characters, by removing all non-essential CGI paramaters even at the
263 * expense of having to re-read the actions file for the most trivial
264 * page, and by using relative rather than absolute URLs. This means
265 * that this (typical example):
267 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
268 * filename=ijb&ver=1011487572&section=12&pattern=13
269 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
273 * <a href="eau?f=ijb&v=1011487572&p=13">
275 * Revision 1.9 2002/01/17 20:56:22 jongfoster
276 * Replacing hard references to the URL of the config interface
277 * with #defines from project.h
279 * Revision 1.8 2001/11/30 23:35:51 jongfoster
280 * Renaming actionsfile to ijb.action
282 * Revision 1.7 2001/11/13 00:28:24 jongfoster
283 * - Renaming parameters from edit-actions-for-url so that they only
284 * contain legal JavaScript characters. If we wanted to write
285 * JavaScript that worked with Netscape 4, this is nessacery.
286 * (Note that at the moment the JavaScript doesn't actually work
287 * with Netscape 4, but now this is purely a template issue, not
288 * one affecting code).
289 * - Adding new CGIs for use by non-JavaScript browsers:
290 * edit-actions-url-form
291 * edit-actions-add-url-form
292 * edit-actions-remove-url-form
295 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
296 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
297 * by and __OS2__ ifdef.
299 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
300 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
301 * threads to call select() simultaneously. So, it's time to do a real, live,
302 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
303 * (native). Both versions will work, but using __OS2__ offers multi-threading.
305 * Revision 1.4 2001/10/23 21:48:19 jongfoster
306 * Cleaning up error handling in CGI functions - they now send back
307 * a HTML error page and should never cause a FATAL error. (Fixes one
308 * potential source of "denial of service" attacks).
310 * CGI actions file editor that works and is actually useful.
312 * Ability to toggle JunkBuster remotely using a CGI call.
314 * You can turn off both the above features in the main configuration
315 * file, e.g. if you are running a multi-user proxy.
317 * Revision 1.3 2001/10/14 22:12:49 jongfoster
318 * New version of CGI-based actionsfile editor.
319 * Major changes, including:
320 * - Completely new file parser and file output routines
321 * - edit-actions CGI renamed edit-actions-for-url
322 * - All CGIs now need a filename parameter, except for...
323 * - New CGI edit-actions which doesn't need a filename,
324 * to allow you to start the editor up.
325 * - edit-actions-submit now works, and now automatically
326 * redirects you back to the main edit-actions-list handler.
328 * Revision 1.2 2001/09/16 17:05:14 jongfoster
329 * Removing unused #include showarg.h
331 * Revision 1.1 2001/09/16 15:47:37 jongfoster
332 * First version of CGI-based edit interface. This is very much a
333 * work-in-progress, and you can't actually use it to edit anything
334 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
335 * to have any effect.
338 **********************************************************************/
344 * FIXME: Following includes copied from cgi.c - which are actually needed?
349 #include <sys/types.h>
353 #include <sys/stat.h>
358 #include "cgisimple.h"
362 #include "miscutil.h"
365 #ifdef FEATURE_TOGGLE
366 /* loadcfg.h is for global_toggle_state only */
368 #endif /* def FEATURE_TOGGLE */
369 #include "urlmatch.h"
371 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
374 #ifdef FEATURE_CGI_EDIT_ACTIONS
377 * A line in an editable_file.
381 /** Next entry in the linked list */
382 struct file_line * next;
384 /** The raw data, to write out if this line is unmodified. */
387 /** Comments and/or whitespace to put before this line if it's modified
388 and then written out. */
391 /** The actual data, as a string. Line continuation and comment removal
392 are performed on the data read from file before it's stored here, so
393 it will be a single line of data. */
396 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
399 /** The actual data, processed into some sensible data type. */
403 /** An action specification. */
404 struct action_spec action[1];
406 /** A name=value pair. */
410 /** The name in the name=value pair. */
413 /** The value in the name=value pair, as a string. */
416 /** The value in the name=value pair, as an integer. */
421 /* Add more data types here... e.g.
424 struct url_spec url[1];
428 struct action_spec action[1];
438 /** This file_line has not been processed yet. */
439 #define FILE_LINE_UNPROCESSED 1
441 /** This file_line is blank. Can only appear at the end of a file, due to
442 the way the parser works. */
443 #define FILE_LINE_BLANK 2
445 /** This file_line says {{alias}}. */
446 #define FILE_LINE_ALIAS_HEADER 3
448 /** This file_line defines an alias. */
449 #define FILE_LINE_ALIAS_ENTRY 4
451 /** This file_line defines an {action}. */
452 #define FILE_LINE_ACTION 5
454 /** This file_line specifies a URL pattern. */
455 #define FILE_LINE_URL 6
457 /** This file_line says {{settings}}. */
458 #define FILE_LINE_SETTINGS_HEADER 7
460 /** This file_line is in a {{settings}} block. */
461 #define FILE_LINE_SETTINGS_ENTRY 8
463 /** This file_line says {{description}}. */
464 #define FILE_LINE_DESCRIPTION_HEADER 9
466 /** This file_line is in a {{description}} block. */
467 #define FILE_LINE_DESCRIPTION_ENTRY 10
471 * A configuration file, in a format that can be edited and written back to
476 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
477 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
478 unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
479 const char * version_str; /**< Last modification time, as a string. For CGI param. */
480 /**< Can be used in URL without using url_param(). */
481 unsigned version; /**< Last modification time - prevents chaos with
482 the browser's "back" button. Note that this is a
483 time_t cast to an unsigned. When comparing, always
484 cast the time_t to an unsigned, and *NOT* vice-versa.
485 This may lose the top few bits, but they're not
486 significant anyway. */
487 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
488 Note that changing this after the file has been
489 read in will cause a mess. */
490 struct file_line * parse_error; /**< On parse error, this is the offending line. */
491 const char * parse_error_text; /**< On parse error, this is the problem.
492 (Statically allocated) */
496 * Used by cgi_edit_actions_for_url() to replace filter related macros.
498 struct cgi_filter_info
500 const int multi_action_index; /**< The multi action index as defined in project.h */
501 char *prepared_templates; /**< Temporary space for the filled-in templates for
502 this filter. Once all templated are aggregated
503 they replace the @$filtername-params@ macro. */
504 const char *type; /**< Name of the filter type,
505 for example "server-header-filter". */
506 const char *abbr_type; /**< Abbreviation of the filter type,
507 usually the first character capitalized */
508 const char *anchor; /**< Anchor for the User Manual link,
509 for example "SERVER-HEADER-FILTER" */
513 /* FIXME: Following non-static functions should be prototyped in .h or made static */
515 /* Functions to read and write arbitrary config files */
516 jb_err edit_read_file(struct client_state *csp,
517 const struct map *parameters,
519 struct editable_file **pfile);
520 jb_err edit_write_file(struct editable_file * file);
521 void edit_free_file(struct editable_file * file);
523 /* Functions to read and write actions files */
524 jb_err edit_parse_actions_file(struct editable_file * file);
525 jb_err edit_read_actions_file(struct client_state *csp,
526 struct http_response *rsp,
527 const struct map *parameters,
529 struct editable_file **pfile);
532 jb_err cgi_error_modified(struct client_state *csp,
533 struct http_response *rsp,
534 const char *filename);
535 jb_err cgi_error_parse(struct client_state *csp,
536 struct http_response *rsp,
537 struct editable_file *file);
538 jb_err cgi_error_file(struct client_state *csp,
539 struct http_response *rsp,
540 const char *filename);
541 jb_err cgi_error_file_read_only(struct client_state *csp,
542 struct http_response *rsp,
543 const char *filename);
545 /* Internal arbitrary config file support functions */
546 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
547 static void edit_free_file_lines(struct file_line * first_line);
549 /* Internal actions file support functions */
550 static int match_actions_file_header_line(const char * line, const char * name);
551 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
553 /* Internal parameter parsing functions */
554 static jb_err get_url_spec_param(struct client_state *csp,
555 const struct map *parameters,
560 /* Internal actionsfile <==> HTML conversion functions */
561 static jb_err map_radio(struct map * exports,
562 const char * optionname,
565 static jb_err actions_to_radio(struct map * exports,
566 const struct action_spec *action);
567 static jb_err actions_from_radio(const struct map * parameters,
568 struct action_spec *action);
571 static jb_err map_copy_parameter_html(struct map *out,
572 const struct map *in,
574 #if 0 /* unused function */
575 static jb_err map_copy_parameter_url(struct map *out,
576 const struct map *in,
578 #endif /* unused function */
580 /* Internal convenience functions */
581 static char *section_target(const unsigned sectionid);
583 /*********************************************************************
585 * Function : section_target
587 * Description : Given an unsigned (section id) n, produce a dynamically
588 * allocated string of the form #l<n>, for use in link
591 * XXX: The hash should be moved into the templates
592 * to make this function more generic and render
593 * stringify() obsolete.
596 * 1 : sectionid = start line number of section
598 * Returns : String with link target, or NULL if out of
601 *********************************************************************/
602 static char *section_target(const unsigned sectionid)
606 snprintf(buf, 30, "#l%d", sectionid);
612 /*********************************************************************
614 * Function : stringify
616 * Description : Convert a number into a dynamically allocated string.
619 * 1 : number = The number to convert.
621 * Returns : String with link target, or NULL if out of memory
623 *********************************************************************/
624 static char *stringify(const unsigned number)
628 snprintf(buf, sizeof(buf), "%i", number);
633 /*********************************************************************
635 * Function : map_copy_parameter_html
637 * Description : Copy a CGI parameter from one map to another, HTML
641 * 1 : out = target map
642 * 2 : in = source map
643 * 3 : name = name of cgi parameter to copy
645 * Returns : JB_ERR_OK on success
646 * JB_ERR_MEMORY on out-of-memory
647 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
650 *********************************************************************/
651 static jb_err map_copy_parameter_html(struct map *out,
652 const struct map *in,
662 value = lookup(in, name);
663 err = map(out, name, 1, html_encode(value), 0);
670 else if (*value == '\0')
672 return JB_ERR_CGI_PARAMS;
681 #if 0 /* unused function */
682 /*********************************************************************
684 * Function : map_copy_parameter_url
686 * Description : Copy a CGI parameter from one map to another, URL
690 * 1 : out = target map
691 * 2 : in = source map
692 * 3 : name = name of cgi parameter to copy
694 * Returns : JB_ERR_OK on success
695 * JB_ERR_MEMORY on out-of-memory
696 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
699 *********************************************************************/
700 static jb_err map_copy_parameter_url(struct map *out,
701 const struct map *in,
711 value = lookup(in, name);
712 err = map(out, name, 1, url_encode(value), 0);
719 else if (*value == '\0')
721 return JB_ERR_CGI_PARAMS;
728 #endif /* 0 - unused function */
731 /*********************************************************************
733 * Function : cgi_edit_actions_url_form
735 * Description : CGI function that displays a form for
739 * 1 : csp = Current client state (buffers, headers, etc...)
740 * 2 : rsp = http_response data structure for output
741 * 3 : parameters = map of cgi parameters
744 * i : (action index) Identifies the file to edit
745 * v : (version) File's last-modified time
746 * p : (pattern) Line number of pattern to edit
748 * Returns : JB_ERR_OK on success
749 * JB_ERR_MEMORY on out-of-memory
750 * JB_ERR_CGI_PARAMS if the CGI parameters are not
751 * specified or not valid.
753 *********************************************************************/
754 jb_err cgi_edit_actions_url_form(struct client_state *csp,
755 struct http_response *rsp,
756 const struct map *parameters)
758 struct map * exports;
760 struct editable_file * file;
761 struct file_line * cur_line;
762 unsigned line_number;
763 unsigned section_start_line_number = 0;
770 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
772 return cgi_error_disabled(csp, rsp);
775 err = get_number_param(csp, parameters, "p", &patternid);
781 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
784 /* No filename specified, can't read file, modified, or out of memory. */
785 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
788 cur_line = file->lines;
790 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
792 if (cur_line->type == FILE_LINE_ACTION)
794 section_start_line_number = line_number;
796 cur_line = cur_line->next;
799 if ( (cur_line == NULL)
800 || (line_number != patternid)
802 || (cur_line->type != FILE_LINE_URL))
804 /* Invalid "patternid" parameter */
805 edit_free_file(file);
806 return JB_ERR_CGI_PARAMS;
809 if (NULL == (exports = default_exports(csp, NULL)))
811 edit_free_file(file);
812 return JB_ERR_MEMORY;
815 err = map(exports, "f", 1, stringify(file->identifier), 0);
816 if (!err) err = map(exports, "v", 1, file->version_str, 1);
817 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
818 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
819 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
821 edit_free_file(file);
829 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
833 /*********************************************************************
835 * Function : cgi_edit_actions_add_url_form
837 * Description : CGI function that displays a form for
841 * 1 : csp = Current client state (buffers, headers, etc...)
842 * 2 : rsp = http_response data structure for output
843 * 3 : parameters = map of cgi parameters
846 * f : (filename) Identifies the file to edit
847 * v : (version) File's last-modified time
848 * s : (section) Line number of section to edit
850 * Returns : JB_ERR_OK on success
851 * JB_ERR_MEMORY on out-of-memory
852 * JB_ERR_CGI_PARAMS if the CGI parameters are not
853 * specified or not valid.
855 *********************************************************************/
856 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
857 struct http_response *rsp,
858 const struct map *parameters)
867 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
869 return cgi_error_disabled(csp, rsp);
872 if (NULL == (exports = default_exports(csp, NULL)))
874 return JB_ERR_MEMORY;
877 err = map_copy_parameter_html(exports, parameters, "f");
878 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
879 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
887 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
891 /*********************************************************************
893 * Function : cgi_edit_actions_remove_url_form
895 * Description : CGI function that displays a form for
899 * 1 : csp = Current client state (buffers, headers, etc...)
900 * 2 : rsp = http_response data structure for output
901 * 3 : parameters = map of cgi parameters
904 * f : (number) The action file identifier.
905 * v : (version) File's last-modified time
906 * p : (pattern) Line number of pattern to edit
908 * Returns : JB_ERR_OK on success
909 * JB_ERR_MEMORY on out-of-memory
910 * JB_ERR_CGI_PARAMS if the CGI parameters are not
911 * specified or not valid.
913 *********************************************************************/
914 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
915 struct http_response *rsp,
916 const struct map *parameters)
918 struct map * exports;
920 struct editable_file * file;
921 struct file_line * cur_line;
922 unsigned line_number;
923 unsigned section_start_line_number = 0;
930 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
932 return cgi_error_disabled(csp, rsp);
935 err = get_number_param(csp, parameters, "p", &patternid);
941 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
944 /* No filename specified, can't read file, modified, or out of memory. */
945 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
948 cur_line = file->lines;
950 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
952 if (cur_line->type == FILE_LINE_ACTION)
954 section_start_line_number = line_number;
956 cur_line = cur_line->next;
959 if ( (cur_line == NULL)
960 || (line_number != patternid)
962 || (cur_line->type != FILE_LINE_URL))
964 /* Invalid "patternid" parameter */
965 edit_free_file(file);
966 return JB_ERR_CGI_PARAMS;
969 if (NULL == (exports = default_exports(csp, NULL)))
971 edit_free_file(file);
972 return JB_ERR_MEMORY;
975 err = map(exports, "f", 1, stringify(file->identifier), 0);
976 if (!err) err = map(exports, "v", 1, file->version_str, 1);
977 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
978 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
979 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
980 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
982 edit_free_file(file);
990 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
994 /*********************************************************************
996 * Function : edit_write_file
998 * Description : Write a complete file to disk.
1001 * 1 : file = File to write.
1003 * Returns : JB_ERR_OK on success
1004 * JB_ERR_FILE on error writing to file.
1005 * JB_ERR_MEMORY on out of memory
1007 *********************************************************************/
1008 jb_err edit_write_file(struct editable_file * file)
1011 struct file_line * cur_line;
1012 struct stat statbuf[1];
1013 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
1014 digits in time_t, assuming this is a 64-bit
1015 machine, plus null terminator, plus one
1019 assert(file->filename);
1021 if (NULL == (fp = fopen(file->filename, "wb")))
1026 cur_line = file->lines;
1027 while (cur_line != NULL)
1031 if (fputs(cur_line->raw, fp) < 0)
1039 if (cur_line->prefix)
1041 if (fputs(cur_line->prefix, fp) < 0)
1047 if (cur_line->unprocessed)
1050 if (NULL != strchr(cur_line->unprocessed, '#'))
1052 /* Must quote '#' characters */
1059 /* Count number of # characters, so we know length of output string */
1060 src = cur_line->unprocessed;
1061 while (NULL != (src = strchr(src, '#')))
1066 assert(numhash > 0);
1068 /* Allocate new memory for string */
1069 len = strlen(cur_line->unprocessed) + (size_t)numhash;
1070 if (NULL == (str = malloc(len + 1)))
1072 /* Uh oh, just trashed file! */
1074 return JB_ERR_MEMORY;
1077 /* Copy string but quote hashes */
1078 src = cur_line->unprocessed;
1086 assert(numhash >= 0);
1092 assert(numhash == 0);
1093 assert(strlen(str) == len);
1094 assert(str == dest - len);
1095 assert(src - len <= cur_line->unprocessed);
1097 if ((strlen(str) != len) || (numhash != 0))
1100 * Escaping didn't work as expected, go spread the news.
1101 * Only reached in non-debugging builds.
1103 log_error(LOG_LEVEL_ERROR,
1104 "Looks like hash escaping failed. %s might be corrupted now.",
1108 if (fputs(str, fp) < 0)
1119 /* Can write without quoting '#' characters. */
1120 if (fputs(cur_line->unprocessed, fp) < 0)
1126 if (fputs(NEWLINE(file->newline), fp) < 0)
1134 /* FIXME: Write data from file->data->whatever */
1138 cur_line = cur_line->next;
1144 /* Update the version stamp in the file structure, since we just
1145 * wrote to the file & changed it's date.
1147 if (stat(file->filename, statbuf) < 0)
1149 /* Error, probably file not found. */
1152 file->version = (unsigned)statbuf->st_mtime;
1154 /* Correct file->version_str */
1155 freez(file->version_str);
1156 snprintf(version_buf, 22, "%u", file->version);
1157 version_buf[21] = '\0';
1158 file->version_str = strdup(version_buf);
1159 if (version_buf == NULL)
1161 return JB_ERR_MEMORY;
1168 /*********************************************************************
1170 * Function : edit_free_file
1172 * Description : Free a complete file in memory.
1175 * 1 : file = Data structure to free.
1179 *********************************************************************/
1180 void edit_free_file(struct editable_file * file)
1184 /* Silently ignore NULL pointer */
1188 edit_free_file_lines(file->lines);
1189 freez(file->version_str);
1191 file->parse_error_text = NULL; /* Statically allocated */
1192 file->parse_error = NULL;
1198 /*********************************************************************
1200 * Function : edit_free_file_lines
1202 * Description : Free an entire linked list of file lines.
1205 * 1 : first_line = Data structure to free.
1209 *********************************************************************/
1210 static void edit_free_file_lines(struct file_line * first_line)
1212 struct file_line * next_line;
1214 while (first_line != NULL)
1216 next_line = first_line->next;
1217 first_line->next = NULL;
1218 freez(first_line->raw);
1219 freez(first_line->prefix);
1220 freez(first_line->unprocessed);
1221 switch(first_line->type)
1223 case 0: /* special case if memory zeroed */
1224 case FILE_LINE_UNPROCESSED:
1225 case FILE_LINE_BLANK:
1226 case FILE_LINE_ALIAS_HEADER:
1227 case FILE_LINE_SETTINGS_HEADER:
1228 case FILE_LINE_DESCRIPTION_HEADER:
1229 case FILE_LINE_DESCRIPTION_ENTRY:
1230 case FILE_LINE_ALIAS_ENTRY:
1232 /* No data is stored for these */
1235 case FILE_LINE_ACTION:
1236 free_action(first_line->data.action);
1239 case FILE_LINE_SETTINGS_ENTRY:
1240 freez(first_line->data.setting.name);
1241 freez(first_line->data.setting.svalue);
1244 /* Should never happen */
1248 first_line->type = 0; /* paranoia */
1250 first_line = next_line;
1255 /*********************************************************************
1257 * Function : match_actions_file_header_line
1259 * Description : Match an actions file {{header}} line
1262 * 1 : line = String from file
1263 * 2 : name = Header to match against
1265 * Returns : 0 iff they match.
1267 *********************************************************************/
1268 static int match_actions_file_header_line(const char * line, const char * name)
1276 if ((line[0] != '{') || (line[1] != '{'))
1282 /* Look for optional whitespace */
1283 while ( (*line == ' ') || (*line == '\t') )
1288 /* Look for the specified name (case-insensitive) */
1290 if (0 != strncmpic(line, name, len))
1296 /* Look for optional whitespace */
1297 while ( (*line == ' ') || (*line == '\t') )
1302 /* Look for "}}" and end of string*/
1303 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1313 /*********************************************************************
1315 * Function : match_actions_file_header_line
1317 * Description : Match an actions file {{header}} line
1320 * 1 : line = String from file. Must not start with
1321 * whitespace (else infinite loop!)
1322 * 2 : pname = Destination for name
1323 * 2 : pvalue = Destination for value
1325 * Returns : JB_ERR_OK on success
1326 * JB_ERR_MEMORY on out-of-memory
1327 * JB_ERR_PARSE if there's no "=" sign, or if there's
1328 * nothing before the "=" sign (but empty
1329 * values *after* the "=" sign are legal).
1331 *********************************************************************/
1332 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1334 const char * name_end;
1335 const char * value_start;
1341 assert(*line != ' ');
1342 assert(*line != '\t');
1347 value_start = strchr(line, '=');
1348 if ((value_start == NULL) || (value_start == line))
1350 return JB_ERR_PARSE;
1353 name_end = value_start - 1;
1355 /* Eat any whitespace before the '=' */
1356 while ((*name_end == ' ') || (*name_end == '\t'))
1359 * we already know we must have at least 1 non-ws char
1360 * at start of buf - no need to check
1365 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1366 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1368 return JB_ERR_MEMORY;
1370 strncpy(*pname, line, name_len);
1371 (*pname)[name_len] = '\0';
1373 /* Eat any the whitespace after the '=' */
1375 while ((*value_start == ' ') || (*value_start == '\t'))
1380 if (NULL == (*pvalue = strdup(value_start)))
1384 return JB_ERR_MEMORY;
1391 /*********************************************************************
1393 * Function : edit_parse_actions_file
1395 * Description : Parse an actions file in memory.
1397 * Passed linked list must have the "data" member
1398 * zeroed, and must contain valid "next" and
1399 * "unprocessed" fields. The "raw" and "prefix"
1400 * fields are ignored, and "type" is just overwritten.
1402 * Note that on error the file may have been
1406 * 1 : file = Actions file to be parsed in-place.
1408 * Returns : JB_ERR_OK on success
1409 * JB_ERR_MEMORY on out-of-memory
1410 * JB_ERR_PARSE on error
1412 *********************************************************************/
1413 jb_err edit_parse_actions_file(struct editable_file * file)
1415 struct file_line * cur_line;
1417 const char * text; /* Text from a line */
1418 char * name; /* For lines of the form name=value */
1419 char * value; /* For lines of the form name=value */
1420 struct action_alias * alias_list = NULL;
1421 jb_err err = JB_ERR_OK;
1423 /* alias_list contains the aliases defined in this file.
1424 * It might be better to use the "file_line.data" fields
1425 * in the relavent places instead.
1428 cur_line = file->lines;
1430 /* A note about blank line support: Blank lines should only
1431 * ever occur as the last line in the file. This function
1432 * is more forgiving than that - FILE_LINE_BLANK can occur
1436 /* Skip leading blanks. Should only happen if file is
1437 * empty (which is valid, but pointless).
1439 while ( (cur_line != NULL)
1440 && (cur_line->unprocessed[0] == '\0') )
1443 cur_line->type = FILE_LINE_BLANK;
1444 cur_line = cur_line->next;
1447 if ( (cur_line != NULL)
1448 && (cur_line->unprocessed[0] != '{') )
1450 /* File doesn't start with a header */
1451 file->parse_error = cur_line;
1452 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1453 return JB_ERR_PARSE;
1456 if ( (cur_line != NULL) && (0 ==
1457 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1459 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1461 cur_line = cur_line->next;
1462 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1464 if (cur_line->unprocessed[0])
1466 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1468 err = split_line_on_equals(cur_line->unprocessed,
1469 &cur_line->data.setting.name,
1470 &cur_line->data.setting.svalue);
1471 if (err == JB_ERR_MEMORY)
1475 else if (err != JB_ERR_OK)
1477 /* Line does not contain a name=value pair */
1478 file->parse_error = cur_line;
1479 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1480 return JB_ERR_PARSE;
1485 cur_line->type = FILE_LINE_BLANK;
1487 cur_line = cur_line->next;
1491 if ( (cur_line != NULL) && (0 ==
1492 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1494 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1496 cur_line = cur_line->next;
1497 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1499 if (cur_line->unprocessed[0])
1501 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1505 cur_line->type = FILE_LINE_BLANK;
1507 cur_line = cur_line->next;
1511 if ( (cur_line != NULL) && (0 ==
1512 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1514 cur_line->type = FILE_LINE_ALIAS_HEADER;
1516 cur_line = cur_line->next;
1517 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1519 if (cur_line->unprocessed[0])
1521 /* define an alias */
1522 struct action_alias * new_alias;
1524 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1526 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1527 if (err == JB_ERR_MEMORY)
1531 else if (err != JB_ERR_OK)
1533 /* Line does not contain a name=value pair */
1534 file->parse_error = cur_line;
1535 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1536 return JB_ERR_PARSE;
1539 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1544 free_alias_list(alias_list);
1545 return JB_ERR_MEMORY;
1548 err = get_actions(value, alias_list, new_alias->action);
1551 /* Invalid action or out of memory */
1555 free_alias_list(alias_list);
1556 if (err == JB_ERR_MEMORY)
1562 /* Line does not contain a name=value pair */
1563 file->parse_error = cur_line;
1564 file->parse_error_text = "This alias does not specify a valid set of actions.";
1565 return JB_ERR_PARSE;
1571 new_alias->name = name;
1574 new_alias->next = alias_list;
1575 alias_list = new_alias;
1579 cur_line->type = FILE_LINE_BLANK;
1581 cur_line = cur_line->next;
1585 /* Header done, process the main part of the file */
1586 while (cur_line != NULL)
1588 /* At this point, (cur_line->unprocessed[0] == '{') */
1589 assert(cur_line->unprocessed[0] == '{');
1590 text = cur_line->unprocessed + 1;
1591 len = strlen(text) - 1;
1592 if (text[len] != '}')
1594 /* No closing } on header */
1595 free_alias_list(alias_list);
1596 file->parse_error = cur_line;
1597 file->parse_error_text = "Headers starting with '{' must have a "
1598 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1599 "must close with two brackets ('}}').";
1600 return JB_ERR_PARSE;
1605 /* An invalid {{ header. */
1606 free_alias_list(alias_list);
1607 file->parse_error = cur_line;
1608 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1609 "Please remember that the system (two-bracket) headers must "
1610 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1611 "and must appear before any actions (one-bracket) headers. "
1612 "Also note that system headers may not be repeated.";
1613 return JB_ERR_PARSE;
1616 while ( (*text == ' ') || (*text == '\t') )
1622 && ( (text[len - 1] == ' ')
1623 || (text[len - 1] == '\t') ) )
1628 cur_line->type = FILE_LINE_ACTION;
1630 /* Remove {} and make copy */
1631 if (NULL == (value = (char *) malloc(len + 1)))
1634 free_alias_list(alias_list);
1635 return JB_ERR_MEMORY;
1637 strncpy(value, text, len);
1641 err = get_actions(value, alias_list, cur_line->data.action);
1644 /* Invalid action or out of memory */
1646 free_alias_list(alias_list);
1647 if (err == JB_ERR_MEMORY)
1653 /* Line does not contain a name=value pair */
1654 file->parse_error = cur_line;
1655 file->parse_error_text = "This header does not specify a valid set of actions.";
1656 return JB_ERR_PARSE;
1660 /* Done with string - it was clobbered anyway */
1663 /* Process next line */
1664 cur_line = cur_line->next;
1666 /* Loop processing URL patterns */
1667 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1669 if (cur_line->unprocessed[0])
1671 /* Could parse URL here, but this isn't currently needed */
1673 cur_line->type = FILE_LINE_URL;
1677 cur_line->type = FILE_LINE_BLANK;
1679 cur_line = cur_line->next;
1681 } /* End main while(cur_line != NULL) loop */
1683 free_alias_list(alias_list);
1689 /*********************************************************************
1691 * Function : edit_read_file_lines
1693 * Description : Read all the lines of a file into memory.
1694 * Handles whitespace, comments and line continuation.
1697 * 1 : fp = File to read from. On return, this will be
1698 * at EOF but it will not have been closed.
1699 * 2 : pfile = Destination for a linked list of file_lines.
1700 * Will be set to NULL on error.
1701 * 3 : newline = How to handle newlines.
1703 * Returns : JB_ERR_OK on success
1704 * JB_ERR_MEMORY on out-of-memory
1706 *********************************************************************/
1707 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1709 struct file_line * first_line; /* Keep for return value or to free */
1710 struct file_line * cur_line; /* Current line */
1711 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1719 cur_line = first_line = zalloc(sizeof(struct file_line));
1720 if (cur_line == NULL)
1722 return JB_ERR_MEMORY;
1725 cur_line->type = FILE_LINE_UNPROCESSED;
1727 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1730 /* Out of memory or empty file. */
1731 /* Note that empty file is not an error we propogate up */
1733 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1738 prev_line = cur_line;
1739 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1740 if (cur_line == NULL)
1743 edit_free_file_lines(first_line);
1744 return JB_ERR_MEMORY;
1747 cur_line->type = FILE_LINE_UNPROCESSED;
1749 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1750 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1753 edit_free_file_lines(first_line);
1754 return JB_ERR_MEMORY;
1758 while (rval != JB_ERR_FILE);
1762 /* We allocated one too many - free it */
1763 prev_line->next = NULL;
1766 *pfile = first_line;
1771 /*********************************************************************
1773 * Function : edit_read_file
1775 * Description : Read a complete file into memory.
1776 * Handles CGI parameter parsing. If requested, also
1777 * checks the file's modification timestamp.
1780 * 1 : csp = Current client state (buffers, headers, etc...)
1781 * 2 : parameters = map of cgi parameters.
1782 * 3 : require_version = true to check "ver" parameter.
1783 * 4 : pfile = Destination for the file. Will be set
1787 * f : The action file identifier.
1788 * ver : (Only if require_version is nonzero)
1789 * Timestamp of the actions file. If wrong, this
1790 * function fails with JB_ERR_MODIFIED.
1792 * Returns : JB_ERR_OK on success
1793 * JB_ERR_MEMORY on out-of-memory
1794 * JB_ERR_CGI_PARAMS if "filename" was not specified
1796 * JB_ERR_FILE if the file cannot be opened or
1798 * JB_ERR_MODIFIED if version checking was requested and
1799 * failed - the file was modified outside
1800 * of this CGI editor instance.
1802 *********************************************************************/
1803 jb_err edit_read_file(struct client_state *csp,
1804 const struct map *parameters,
1805 int require_version,
1806 struct editable_file **pfile)
1808 struct file_line * lines;
1811 const char * filename = NULL;
1812 struct editable_file * file;
1813 unsigned version = 0;
1814 struct stat statbuf[1];
1815 char version_buf[22];
1816 int newline = NEWLINE_UNKNOWN;
1825 if ((JB_ERR_OK == get_number_param(csp, parameters, "f", &i))
1826 && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
1828 filename = csp->config->actions_file[i];
1831 if (filename == NULL || stat(filename, statbuf) < 0)
1833 /* Error, probably file not found. */
1836 version = (unsigned) statbuf->st_mtime;
1838 if (require_version)
1840 unsigned specified_version;
1841 err = get_number_param(csp, parameters, "v", &specified_version);
1847 if (version != specified_version)
1849 return JB_ERR_MODIFIED;
1853 if (NULL == (fp = fopen(filename,"rb")))
1858 err = edit_read_file_lines(fp, &lines, &newline);
1867 file = (struct editable_file *) zalloc(sizeof(*file));
1870 edit_free_file_lines(lines);
1874 file->lines = lines;
1875 file->newline = newline;
1876 file->filename = filename;
1877 file->version = version;
1878 file->identifier = i;
1880 /* Correct file->version_str */
1881 freez(file->version_str);
1882 snprintf(version_buf, 22, "%u", file->version);
1883 version_buf[21] = '\0';
1884 file->version_str = strdup(version_buf);
1885 if (version_buf == NULL)
1887 edit_free_file(file);
1888 return JB_ERR_MEMORY;
1896 /*********************************************************************
1898 * Function : edit_read_actions_file
1900 * Description : Read a complete actions file into memory.
1901 * Handles CGI parameter parsing. If requested, also
1902 * checks the file's modification timestamp.
1904 * If this function detects an error in the categories
1905 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1906 * then it handles it by filling in the specified
1907 * response structure and returning JB_ERR_FILE.
1910 * 1 : csp = Current client state (buffers, headers, etc...)
1911 * 2 : rsp = HTTP response. Only filled in on error.
1912 * 2 : parameters = map of cgi parameters.
1913 * 3 : require_version = true to check "ver" parameter.
1914 * 4 : pfile = Destination for the file. Will be set
1918 * f : The actions file identifier.
1919 * ver : (Only if require_version is nonzero)
1920 * Timestamp of the actions file. If wrong, this
1921 * function fails with JB_ERR_MODIFIED.
1923 * Returns : JB_ERR_OK on success
1924 * JB_ERR_MEMORY on out-of-memory
1925 * JB_ERR_CGI_PARAMS if "filename" was not specified
1927 * JB_ERR_FILE if the file does not contain valid data,
1928 * or if file cannot be opened or
1929 * contains no data, or if version
1930 * checking was requested and failed.
1932 *********************************************************************/
1933 jb_err edit_read_actions_file(struct client_state *csp,
1934 struct http_response *rsp,
1935 const struct map *parameters,
1936 int require_version,
1937 struct editable_file **pfile)
1940 struct editable_file *file;
1948 err = edit_read_file(csp, parameters, require_version, &file);
1951 /* Try to handle if possible */
1952 if (err == JB_ERR_FILE)
1954 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1956 else if (err == JB_ERR_MODIFIED)
1958 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1960 if (err == JB_ERR_OK)
1963 * Signal to higher-level CGI code that there was a problem but we
1964 * handled it, they should just return JB_ERR_OK.
1971 err = edit_parse_actions_file(file);
1974 if (err == JB_ERR_PARSE)
1976 err = cgi_error_parse(csp, rsp, file);
1977 if (err == JB_ERR_OK)
1980 * Signal to higher-level CGI code that there was a problem but we
1981 * handled it, they should just return JB_ERR_OK.
1986 edit_free_file(file);
1997 * Currently not needed, but may become useful again in the future.
1999 /*********************************************************************
2001 * Function : get_file_name_param
2003 * Description : Get the name of the file to edit from the parameters
2004 * passed to a CGI function. This function handles
2005 * security checks such as blocking urls containing
2006 * "/" or ".", prepending the config file directory,
2007 * and adding the specified suffix.
2009 * (This is an essential security check, otherwise
2010 * users may be able to pass "../../../etc/passwd"
2011 * and overwrite the password file [linux], "prn:"
2012 * and print random data [Windows], etc...)
2014 * This function only allows filenames contining the
2015 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
2016 * That's probably too restrictive but at least it's
2020 * 1 : csp = Current client state (buffers, headers, etc...)
2021 * 2 : parameters = map of cgi parameters
2022 * 3 : param_name = The name of the parameter to read
2023 * 4 : suffix = File extension, e.g. ".actions"
2024 * 5 : pfilename = destination for full filename. Caller
2025 * free()s. Set to NULL on error.
2026 * 6 : pparam = destination for partial filename,
2027 * suitable for use in another URL. Allocated as part
2028 * of the map "parameters", so don't free it.
2029 * Set to NULL if not specified.
2031 * Returns : JB_ERR_OK on success
2032 * JB_ERR_MEMORY on out-of-memory
2033 * JB_ERR_CGI_PARAMS if "filename" was not specified
2036 *********************************************************************/
2037 static jb_err get_file_name_param(struct client_state *csp,
2038 const struct map *parameters,
2039 const char *param_name,
2042 const char **pparam)
2060 param = lookup(parameters, param_name);
2063 return JB_ERR_CGI_PARAMS;
2068 len = strlen(param);
2069 if (len >= FILENAME_MAX)
2072 return JB_ERR_CGI_PARAMS;
2075 /* Check every character to see if it's legal */
2077 while ((ch = *s++) != '\0')
2079 if ( ((ch < 'A') || (ch > 'Z'))
2080 && ((ch < 'a') || (ch > 'z'))
2081 && ((ch < '0') || (ch > '9'))
2085 /* Probable hack attempt. */
2086 return JB_ERR_CGI_PARAMS;
2090 /* Append extension */
2091 name = malloc(len + strlen(suffix) + 1);
2094 return JB_ERR_MEMORY;
2096 strcpy(name, param);
2097 strcpy(name + len, suffix);
2100 fullpath = make_path(csp->config->confdir, name);
2103 if (fullpath == NULL)
2105 return JB_ERR_MEMORY;
2109 *pfilename = fullpath;
2116 /*********************************************************************
2118 * Function : get_url_spec_param
2120 * Description : Get a URL pattern from the parameters
2121 * passed to a CGI function. Removes leading/trailing
2122 * spaces and validates it.
2125 * 1 : csp = Current client state (buffers, headers, etc...)
2126 * 2 : parameters = map of cgi parameters
2127 * 3 : name = Name of CGI parameter to read
2128 * 4 : pvalue = destination for value. Will be malloc()'d.
2129 * Set to NULL on error.
2131 * Returns : JB_ERR_OK on success
2132 * JB_ERR_MEMORY on out-of-memory
2133 * JB_ERR_CGI_PARAMS if the parameter was not specified
2136 *********************************************************************/
2137 static jb_err get_url_spec_param(struct client_state *csp,
2138 const struct map *parameters,
2142 const char *orig_param;
2145 struct url_spec compiled[1];
2155 orig_param = lookup(parameters, name);
2158 return JB_ERR_CGI_PARAMS;
2161 /* Copy and trim whitespace */
2162 param = strdup(orig_param);
2165 return JB_ERR_MEMORY;
2169 /* Must be non-empty, and can't allow 1st character to be '{' */
2170 if (param[0] == '\0' || param[0] == '{')
2173 return JB_ERR_CGI_PARAMS;
2176 /* Check for embedded newlines */
2177 for (s = param; *s != '\0'; s++)
2179 if ((*s == '\r') || (*s == '\n'))
2182 return JB_ERR_CGI_PARAMS;
2186 /* Check that regex is valid */
2191 return JB_ERR_MEMORY;
2193 err = create_url_spec(compiled, s);
2198 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2200 free_url_spec(compiled);
2202 if (param[strlen(param) - 1] == '\\')
2205 * Must protect trailing '\\' from becoming line continuation character.
2206 * Two methods: 1) If it's a domain only, add a trailing '/'.
2207 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2209 if (strchr(param, '/') == NULL)
2211 err = string_append(¶m, "/");
2215 err = string_append(¶m, "(?:)");
2222 /* Check that the modified regex is valid */
2227 return JB_ERR_MEMORY;
2229 err = create_url_spec(compiled, s);
2234 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2236 free_url_spec(compiled);
2243 /*********************************************************************
2245 * Function : map_radio
2247 * Description : Map a set of radio button values. E.g. if you have
2248 * 3 radio buttons, declare them as:
2249 * <option type="radio" name="xyz" @xyz-a@>
2250 * <option type="radio" name="xyz" @xyz-b@>
2251 * <option type="radio" name="xyz" @xyz-c@>
2252 * Then map one of the @xyz-?@ variables to "checked"
2253 * and all the others to empty by calling:
2254 * map_radio(exports, "xyz", "abc", sel)
2255 * Where 'sel' is 'a', 'b', or 'c'.
2258 * 1 : exports = Exports map to modify.
2259 * 2 : optionname = name for map
2260 * 3 : values = null-terminated list of values;
2261 * 4 : value = Selected value.
2263 * CGI Parameters : None
2265 * Returns : JB_ERR_OK on success
2266 * JB_ERR_MEMORY on out-of-memory
2268 *********************************************************************/
2269 static jb_err map_radio(struct map * exports,
2270 const char * optionname,
2271 const char * values,
2283 len = strlen(optionname);
2284 buf = malloc(len + 3);
2287 return JB_ERR_MEMORY;
2290 strcpy(buf, optionname);
2295 while ((c = *values++) != '\0')
2300 if (map(exports, buf, 1, "", 1))
2302 return JB_ERR_MEMORY;
2308 return map(exports, buf, 0, "checked", 1);
2312 /*********************************************************************
2314 * Function : cgi_error_modified
2316 * Description : CGI function that is called when a file is modified
2317 * outside the CGI editor.
2320 * 1 : csp = Current client state (buffers, headers, etc...)
2321 * 2 : rsp = http_response data structure for output
2322 * 3 : filename = The file that was modified.
2324 * CGI Parameters : none
2326 * Returns : JB_ERR_OK on success
2327 * JB_ERR_MEMORY on out-of-memory error.
2329 *********************************************************************/
2330 jb_err cgi_error_modified(struct client_state *csp,
2331 struct http_response *rsp,
2332 const char *filename)
2334 struct map *exports;
2341 if (NULL == (exports = default_exports(csp, NULL)))
2343 return JB_ERR_MEMORY;
2346 err = map(exports, "f", 1, html_encode(filename), 0);
2353 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2357 /*********************************************************************
2359 * Function : cgi_error_parse
2361 * Description : CGI function that is called when a file cannot
2362 * be parsed by the CGI editor.
2365 * 1 : csp = Current client state (buffers, headers, etc...)
2366 * 2 : rsp = http_response data structure for output
2367 * 3 : file = The file that was modified.
2369 * CGI Parameters : none
2371 * Returns : JB_ERR_OK on success
2372 * JB_ERR_MEMORY on out-of-memory error.
2374 *********************************************************************/
2375 jb_err cgi_error_parse(struct client_state *csp,
2376 struct http_response *rsp,
2377 struct editable_file *file)
2379 struct map *exports;
2381 struct file_line *cur_line;
2387 if (NULL == (exports = default_exports(csp, NULL)))
2389 return JB_ERR_MEMORY;
2392 err = map(exports, "f", 1, stringify(file->identifier), 0);
2393 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2395 cur_line = file->parse_error;
2398 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2399 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2407 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2411 /*********************************************************************
2413 * Function : cgi_error_file
2415 * Description : CGI function that is called when a file cannot be
2416 * opened by the CGI editor.
2419 * 1 : csp = Current client state (buffers, headers, etc...)
2420 * 2 : rsp = http_response data structure for output
2421 * 3 : filename = The file that was modified.
2423 * CGI Parameters : none
2425 * Returns : JB_ERR_OK on success
2426 * JB_ERR_MEMORY on out-of-memory error.
2428 *********************************************************************/
2429 jb_err cgi_error_file(struct client_state *csp,
2430 struct http_response *rsp,
2431 const char *filename)
2433 struct map *exports;
2440 if (NULL == (exports = default_exports(csp, NULL)))
2442 return JB_ERR_MEMORY;
2445 err = map(exports, "f", 1, html_encode(filename), 0);
2452 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2456 /*********************************************************************
2458 * Function : cgi_error_file_read_only
2460 * Description : CGI function that is called when a file cannot be
2461 * opened for writing by the CGI editor.
2464 * 1 : csp = Current client state (buffers, headers, etc...)
2465 * 2 : rsp = http_response data structure for output
2466 * 3 : filename = The file that we can't write to
2468 * CGI Parameters : none
2470 * Returns : JB_ERR_OK on success
2471 * JB_ERR_MEMORY on out-of-memory error.
2473 *********************************************************************/
2474 jb_err cgi_error_file_read_only(struct client_state *csp,
2475 struct http_response *rsp,
2476 const char *filename)
2478 struct map *exports;
2485 if (NULL == (exports = default_exports(csp, NULL)))
2487 return JB_ERR_MEMORY;
2490 err = map(exports, "f", 1, html_encode(filename), 0);
2497 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2501 /*********************************************************************
2503 * Function : cgi_edit_actions
2505 * Description : CGI function that allows the user to choose which
2506 * actions file to edit.
2509 * 1 : csp = Current client state (buffers, headers, etc...)
2510 * 2 : rsp = http_response data structure for output
2511 * 3 : parameters = map of cgi parameters
2513 * CGI Parameters : None
2515 * Returns : JB_ERR_OK on success
2516 * JB_ERR_MEMORY on out-of-memory error
2518 *********************************************************************/
2519 jb_err cgi_edit_actions(struct client_state *csp,
2520 struct http_response *rsp,
2521 const struct map *parameters)
2524 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2526 return cgi_error_disabled(csp, rsp);
2529 /* FIXME: Incomplete */
2531 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2536 /*********************************************************************
2538 * Function : cgi_edit_actions_list
2540 * Description : CGI function that edits the actions list.
2541 * FIXME: This function shouldn't FATAL ever.
2542 * FIXME: This function doesn't check the retval of map()
2544 * 1 : csp = Current client state (buffers, headers, etc...)
2545 * 2 : rsp = http_response data structure for output
2546 * 3 : parameters = map of cgi parameters
2548 * CGI Parameters : filename
2550 * Returns : JB_ERR_OK on success
2551 * JB_ERR_MEMORY on out-of-memory
2552 * JB_ERR_FILE if the file cannot be opened or
2554 * JB_ERR_CGI_PARAMS if "filename" was not specified
2557 *********************************************************************/
2558 jb_err cgi_edit_actions_list(struct client_state *csp,
2559 struct http_response *rsp,
2560 const struct map *parameters)
2562 char * section_template;
2563 char * url_template;
2568 struct map * exports;
2569 struct map * section_exports;
2570 struct map * url_exports;
2571 struct editable_file * file;
2572 struct file_line * cur_line;
2573 unsigned line_number = 0;
2574 unsigned prev_section_line_number = ((unsigned) (-1));
2576 struct file_list * fl;
2577 struct url_actions * b;
2578 char * buttons = NULL;
2581 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2583 return cgi_error_disabled(csp, rsp);
2586 if (NULL == (exports = default_exports(csp, NULL)))
2588 return JB_ERR_MEMORY;
2591 /* Load actions file */
2592 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2595 /* No filename specified, can't read file, or out of memory. */
2596 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2599 /* Find start of actions in file */
2600 cur_line = file->lines;
2602 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2604 cur_line = cur_line->next;
2609 * Conventional actions files should have a match all block
2611 * cur_line = {...global actions...}
2612 * cur_line->next = /
2613 * cur_line->next->next = {...actions...} or EOF
2615 if ( (cur_line != NULL)
2616 && (cur_line->type == FILE_LINE_ACTION)
2617 && (cur_line->next != NULL)
2618 && (cur_line->next->type == FILE_LINE_URL)
2619 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2620 && ( (cur_line->next->next == NULL)
2621 || (cur_line->next->next->type != FILE_LINE_URL)
2625 * Generate string with buttons to set actions for "/" to
2626 * any predefined set of actions (named standard.*, probably
2627 * residing in standard.action).
2630 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2633 edit_free_file(file);
2635 if (err == JB_ERR_FILE)
2637 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2642 err = template_fill(§ion_template, exports);
2645 edit_free_file(file);
2650 buttons = strdup("");
2651 for (i = 0; i < MAX_AF_FILES; i++)
2653 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2655 for (b = b->next; NULL != b; b = b->next)
2657 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2659 if (err || (NULL == (section_exports = new_map())))
2662 free(section_template);
2663 edit_free_file(file);
2665 return JB_ERR_MEMORY;
2668 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2670 if (err || (NULL == (s = strdup(section_template))))
2672 free_map(section_exports);
2674 free(section_template);
2675 edit_free_file(file);
2677 return JB_ERR_MEMORY;
2680 if (!err) err = template_fill(&s, section_exports);
2681 free_map(section_exports);
2682 if (!err) err = string_join(&buttons, s);
2687 freez(section_template);
2688 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2691 * Conventional actions file, supply extra editing help.
2692 * (e.g. don't allow them to make it an unconventional one).
2694 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2696 snprintf(buf, 150, "%d", line_number);
2697 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2698 snprintf(buf, 150, "%d", line_number + 2);
2699 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2700 if (!err) err = map(exports, "all-urls-actions", 1,
2701 actions_to_html(csp, cur_line->data.action), 0);
2703 /* Skip the 2 lines */
2704 cur_line = cur_line->next->next;
2708 * Note that prev_section_line_number is NOT set here.
2709 * This is deliberate and not a bug. It stops a "Move up"
2710 * option appearing on the next section. Clicking "Move
2711 * up" would make the actions file unconventional, which
2712 * we don't want, so we hide this option.
2718 * Non-standard actions file - does not begin with
2719 * the "All URLs" section.
2721 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2724 /* Set up global exports */
2726 if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
2727 if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
2728 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2730 /* Discourage private additions to default.action */
2732 if (!err) err = map_conditional(exports, "default-action",
2733 (strstr("default.action", file->filename) != NULL));
2736 edit_free_file(file);
2741 /* Should do all global exports above this point */
2743 /* Load templates */
2745 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2748 edit_free_file(file);
2750 if (err == JB_ERR_FILE)
2752 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2757 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2760 free(section_template);
2761 edit_free_file(file);
2763 if (err == JB_ERR_FILE)
2765 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2770 err = template_fill(§ion_template, exports);
2774 edit_free_file(file);
2780 err = template_fill(&url_template, exports);
2783 free(section_template);
2784 edit_free_file(file);
2789 if (NULL == (sections = strdup("")))
2791 free(section_template);
2793 edit_free_file(file);
2795 return JB_ERR_MEMORY;
2798 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2800 if (NULL == (section_exports = new_map()))
2803 free(section_template);
2805 edit_free_file(file);
2807 return JB_ERR_MEMORY;
2810 snprintf(buf, 150, "%d", line_number);
2811 err = map(section_exports, "s", 1, buf, 1);
2812 if (!err) err = map(section_exports, "actions", 1,
2813 actions_to_html(csp, cur_line->data.action), 0);
2816 && (cur_line->next != NULL)
2817 && (cur_line->next->type == FILE_LINE_URL))
2819 /* This section contains at least one URL, don't allow delete */
2820 err = map_block_killer(section_exports, "empty-section");
2824 if (!err) err = map_block_keep(section_exports, "empty-section");
2827 if (prev_section_line_number != ((unsigned)(-1)))
2829 /* Not last section */
2830 snprintf(buf, 150, "%d", prev_section_line_number);
2831 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2832 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2837 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2839 prev_section_line_number = line_number;
2844 free(section_template);
2846 edit_free_file(file);
2848 free_map(section_exports);
2852 /* Should do all section-specific exports above this point */
2854 if (NULL == (urls = strdup("")))
2857 free(section_template);
2859 edit_free_file(file);
2861 free_map(section_exports);
2862 return JB_ERR_MEMORY;
2867 cur_line = cur_line->next;
2870 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2872 if (NULL == (url_exports = new_map()))
2876 free(section_template);
2878 edit_free_file(file);
2880 free_map(section_exports);
2881 return JB_ERR_MEMORY;
2884 snprintf(buf, 150, "%d", line_number);
2885 err = map(url_exports, "p", 1, buf, 1);
2887 snprintf(buf, 150, "%d", url_1_2);
2888 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2890 if (!err) err = map(url_exports, "url-html", 1,
2891 html_encode(cur_line->unprocessed), 0);
2892 if (!err) err = map(url_exports, "url", 1,
2893 url_encode(cur_line->unprocessed), 0);
2899 free(section_template);
2901 edit_free_file(file);
2903 free_map(section_exports);
2904 free_map(url_exports);
2908 if (NULL == (s = strdup(url_template)))
2912 free(section_template);
2914 edit_free_file(file);
2916 free_map(section_exports);
2917 free_map(url_exports);
2918 return JB_ERR_MEMORY;
2921 err = template_fill(&s, section_exports);
2922 if (!err) err = template_fill(&s, url_exports);
2923 if (!err) err = string_append(&urls, s);
2925 free_map(url_exports);
2932 free(section_template);
2934 edit_free_file(file);
2936 free_map(section_exports);
2940 url_1_2 = 3 - url_1_2;
2942 cur_line = cur_line->next;
2946 err = map(section_exports, "urls", 1, urls, 0);
2948 /* Could also do section-specific exports here, but it wouldn't be as fast */
2950 snprintf(buf, 150, "%d", line_number);
2951 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2953 if ( (cur_line != NULL)
2954 && (cur_line->type == FILE_LINE_ACTION))
2956 /* Not last section */
2957 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2962 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2968 free(section_template);
2970 edit_free_file(file);
2972 free_map(section_exports);
2976 if (NULL == (s = strdup(section_template)))
2979 free(section_template);
2981 edit_free_file(file);
2983 free_map(section_exports);
2984 return JB_ERR_MEMORY;
2987 err = template_fill(&s, section_exports);
2988 if (!err) err = string_append(§ions, s);
2991 free_map(section_exports);
2996 free(section_template);
2998 edit_free_file(file);
3004 edit_free_file(file);
3005 free(section_template);
3008 err = map(exports, "sections", 1, sections, 0);
3015 /* Could also do global exports here, but it wouldn't be as fast */
3017 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
3021 /*********************************************************************
3023 * Function : cgi_edit_actions_for_url
3025 * Description : CGI function that edits the Actions list.
3028 * 1 : csp = Current client state (buffers, headers, etc...)
3029 * 2 : rsp = http_response data structure for output
3030 * 3 : parameters = map of cgi parameters
3032 * CGI Parameters : None
3034 * Returns : JB_ERR_OK on success
3035 * JB_ERR_MEMORY on out-of-memory
3036 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3037 * specified or not valid.
3039 *********************************************************************/
3040 jb_err cgi_edit_actions_for_url(struct client_state *csp,
3041 struct http_response *rsp,
3042 const struct map *parameters)
3044 struct map * exports;
3046 struct editable_file * file;
3047 struct file_line * cur_line;
3048 unsigned line_number;
3050 struct re_filterfile_spec *filter_group;
3051 int i, have_filters = 0;
3053 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3055 return cgi_error_disabled(csp, rsp);
3058 err = get_number_param(csp, parameters, "s", §ionid);
3064 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3067 /* No filename specified, can't read file, modified, or out of memory. */
3068 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3071 cur_line = file->lines;
3073 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3075 cur_line = cur_line->next;
3078 if ( (cur_line == NULL)
3079 || (line_number != sectionid)
3081 || (cur_line->type != FILE_LINE_ACTION))
3083 /* Invalid "sectionid" parameter */
3084 edit_free_file(file);
3085 return JB_ERR_CGI_PARAMS;
3088 if (NULL == (exports = default_exports(csp, NULL)))
3090 edit_free_file(file);
3091 return JB_ERR_MEMORY;
3094 err = map(exports, "f", 1, stringify(file->identifier), 0);
3095 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3096 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3098 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3101 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3102 * length limitation and ignore clicks on the Submit buttons if
3103 * the resulting GET URL would be longer than their limit.
3105 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3106 * reached this limit and action editing stopped working in these
3107 * browsers (BR #1570678).
3109 * The config option split-large-forms works around this browser
3110 * bug (HTTP has no URL lenght limitation) by deviding the action
3111 * list form into multiple smaller ones. It means the URLs are shorter
3112 * and work in broken browsers as well, but the user can no longer change
3113 * all actions with one submit.
3115 * A better solution would be to switch to POST requests,
3116 * but this will do for now.
3118 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3120 /* Generate multiple smaller form by killing the big one. */
3121 err = map_block_killer(exports, "one-form-only");
3125 /* Default: Generate one large form by killing the smaller ones. */
3126 err = map_block_killer(exports, "multiple-forms");
3129 for (i = 0; i < MAX_AF_FILES; i++)
3131 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3133 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3141 edit_free_file(file);
3146 if (0 == have_filters)
3148 err = map(exports, "filter-params", 1, "", 1);
3153 * List available filters and their settings.
3155 char *filter_template;
3156 int filter_identifier = 0;
3157 /* XXX: Should we put these into an array? */
3158 static struct cgi_filter_info content_filter = {
3159 ACTION_MULTI_FILTER, NULL,
3160 "filter", "F", "FILTER"
3162 static struct cgi_filter_info server_header_filter = {
3163 ACTION_MULTI_SERVER_HEADER_FILTER, NULL,
3164 "server-header-filter", "S", "SERVER-HEADER-FILTER"
3166 static struct cgi_filter_info client_header_filter = {
3167 ACTION_MULTI_CLIENT_HEADER_FILTER, NULL,
3168 "client-header-filter", "C", "CLIENT-HEADER-FILTER"
3171 content_filter.prepared_templates = strdup("");
3172 server_header_filter.prepared_templates = strdup("");
3173 client_header_filter.prepared_templates = strdup("");
3175 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3178 edit_free_file(file);
3180 if (err == JB_ERR_FILE)
3182 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3187 err = template_fill(&filter_template, exports);
3189 for (i = 0; i < MAX_AF_FILES; i++)
3191 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3193 filter_group = csp->rlist[i]->f;
3194 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3196 int multi_action_index;
3197 char current_mode = 'x';
3199 struct list_entry *filter_name;
3200 struct map *line_exports;
3201 struct cgi_filter_info *current_filter = NULL;
3203 switch (filter_group->type)
3205 case FT_CONTENT_FILTER:
3206 current_filter = &content_filter;
3208 case FT_SERVER_HEADER_FILTER:
3209 current_filter = &server_header_filter;
3211 case FT_CLIENT_HEADER_FILTER:
3212 current_filter = &client_header_filter;
3215 log_error(LOG_LEVEL_FATAL,
3216 "cgi_edit_actions_for_url: Unknown filter type: %u for filter %s.",
3217 filter_group->type, filter_group->name);
3220 assert(current_filter != NULL);
3221 multi_action_index = current_filter->multi_action_index;
3223 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
3224 while ((filter_name != NULL)
3225 && (0 != strcmp(filter_group->name, filter_name->str)))
3227 filter_name = filter_name->next;
3230 if (filter_name != NULL)
3236 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
3237 while ((filter_name != NULL)
3238 && (0 != strcmp(filter_group->name, filter_name->str)))
3240 filter_name = filter_name->next;
3242 if (filter_name != NULL)
3248 /* Generate a unique serial number */
3249 snprintf(number, sizeof(number), "%x", filter_identifier++);
3250 number[sizeof(number) - 1] = '\0';
3252 line_exports = new_map();
3253 if (line_exports == NULL)
3255 err = JB_ERR_MEMORY;
3256 freez(current_filter->prepared_templates); /* XXX: really necessary? */
3262 if (!err) err = map(line_exports, "index", 1, number, 1);
3263 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3264 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3265 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3266 if (!err) err = map(line_exports, "filter-type", 1, current_filter->type, 1);
3267 if (!err) err = map(line_exports, "abbr-filter-type", 1, current_filter->abbr_type, 1);
3268 if (!err) err = map(line_exports, "anchor", 1, current_filter->anchor, 1);
3272 filter_line = strdup(filter_template);
3273 if (filter_line == NULL) err = JB_ERR_MEMORY;
3275 if (!err) err = template_fill(&filter_line, line_exports);
3276 string_join(¤t_filter->prepared_templates, filter_line);
3278 free_map(line_exports);
3283 freez(filter_template);
3285 if (!err) err = map(exports, "content-filter-params", 1, content_filter.prepared_templates, 0);
3286 if (!err) err = map(exports, "server-header-filter-params", 1, server_header_filter.prepared_templates, 0);
3287 if (!err) err = map(exports, "client-header-filter-params", 1, client_header_filter.prepared_templates, 0);
3290 freez(content_filter.prepared_templates);
3291 freez(server_header_filter.prepared_templates);
3292 freez(client_header_filter.prepared_templates);
3296 if (!err) err = map_radio(exports, "filter-all", "nx",
3297 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3299 edit_free_file(file);
3307 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3311 /*********************************************************************
3313 * Function : cgi_edit_actions_submit
3315 * Description : CGI function that actually edits the Actions list.
3318 * 1 : csp = Current client state (buffers, headers, etc...)
3319 * 2 : rsp = http_response data structure for output
3320 * 3 : parameters = map of cgi parameters
3322 * CGI Parameters : None
3324 * Returns : JB_ERR_OK on success
3325 * JB_ERR_MEMORY on out-of-memory
3326 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3327 * specified or not valid.
3329 *********************************************************************/
3330 jb_err cgi_edit_actions_submit(struct client_state *csp,
3331 struct http_response *rsp,
3332 const struct map *parameters)
3338 struct editable_file * file;
3339 struct file_line * cur_line;
3340 unsigned line_number;
3343 int filter_identifier;
3344 const char * action_set_name;
3346 struct file_list * fl;
3347 struct url_actions * b;
3349 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3351 return cgi_error_disabled(csp, rsp);
3354 err = get_number_param(csp, parameters, "s", §ionid);
3360 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3363 /* No filename specified, can't read file, modified, or out of memory. */
3364 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3367 cur_line = file->lines;
3369 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3371 cur_line = cur_line->next;
3374 if ( (cur_line == NULL)
3375 || (line_number != sectionid)
3377 || (cur_line->type != FILE_LINE_ACTION))
3379 /* Invalid "sectionid" parameter */
3380 edit_free_file(file);
3381 return JB_ERR_CGI_PARAMS;
3384 get_string_param(parameters, "p", &action_set_name);
3385 if (action_set_name != NULL)
3387 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3389 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3391 for (b = b->next; NULL != b; b = b->next)
3393 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3395 copy_action(cur_line->data.action, b->action);
3401 edit_free_file(file);
3402 return JB_ERR_CGI_PARAMS;
3408 err = actions_from_radio(parameters, cur_line->data.action);
3414 edit_free_file(file);
3418 ch = get_char_param(parameters, "filter_all");
3421 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3422 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3423 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3427 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3430 for (filter_identifier = 0; !err; filter_identifier++)
3437 * Filter state. Valid states are: 'Y' (active),
3438 * 'N' (inactive) and 'X' (no change).
3442 * Abbreviated filter type. Valid types are: 'F' (content filter),
3443 * 'S' (server-header filter) and 'C' (client-header filter).
3445 int multi_action_index = 0;
3447 /* Generate the keys */
3448 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3449 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3450 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3451 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3452 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3454 err = get_string_param(parameters, key_name, &name);
3463 type = get_char_param(parameters, key_type);
3467 multi_action_index = ACTION_MULTI_FILTER;
3470 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3473 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3476 log_error(LOG_LEVEL_ERROR,
3477 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3480 assert(multi_action_index);
3482 value = get_char_param(parameters, key_value);
3485 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3486 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3487 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3489 else if (value == 'N')
3491 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3492 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3494 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3495 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3498 else if (value == 'X')
3500 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3501 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3508 edit_free_file(file);
3512 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3515 edit_free_file(file);
3516 return JB_ERR_MEMORY;
3519 len = strlen(actiontext);
3523 * Empty action - must special-case this.
3524 * Simply setting len to 1 is sufficient...
3529 if (NULL == (newtext = malloc(len + 2)))
3533 edit_free_file(file);
3534 return JB_ERR_MEMORY;
3536 strcpy(newtext, actiontext);
3540 newtext[len + 1] = '\0';
3542 freez(cur_line->raw);
3543 freez(cur_line->unprocessed);
3544 cur_line->unprocessed = newtext;
3546 err = edit_write_file(file);
3549 /* Error writing file */
3550 if (err == JB_ERR_FILE)
3552 /* Read-only file. */
3553 err = cgi_error_file_read_only(csp, rsp, file->filename);
3555 edit_free_file(file);
3559 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3560 (long) time(NULL), file->identifier, sectionid);
3562 edit_free_file(file);
3564 return cgi_redirect(rsp, target);
3568 /*********************************************************************
3570 * Function : cgi_edit_actions_url
3572 * Description : CGI function that actually edits a URL pattern in
3576 * 1 : csp = Current client state (buffers, headers, etc...)
3577 * 2 : rsp = http_response data structure for output
3578 * 3 : parameters = map of cgi parameters
3581 * filename : Identifies the file to edit
3582 * ver : File's last-modified time
3583 * section : Line number of section to edit
3584 * pattern : Line number of pattern to edit
3585 * newval : New value for pattern
3587 * Returns : JB_ERR_OK on success
3588 * JB_ERR_MEMORY on out-of-memory
3589 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3590 * specified or not valid.
3592 *********************************************************************/
3593 jb_err cgi_edit_actions_url(struct client_state *csp,
3594 struct http_response *rsp,
3595 const struct map *parameters)
3599 struct editable_file * file;
3600 struct file_line * cur_line;
3601 unsigned line_number;
3602 unsigned section_start_line_number = 0;
3610 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3612 return cgi_error_disabled(csp, rsp);
3615 err = get_number_param(csp, parameters, "p", &patternid);
3622 return JB_ERR_CGI_PARAMS;
3625 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3631 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3634 /* No filename specified, can't read file, modified, or out of memory. */
3636 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3640 cur_line = file->lines;
3642 while ((cur_line != NULL) && (line_number < patternid))
3644 if (cur_line->type == FILE_LINE_ACTION)
3646 section_start_line_number = line_number;
3648 cur_line = cur_line->next;
3652 if ( (cur_line == NULL)
3653 || (cur_line->type != FILE_LINE_URL))
3655 /* Invalid "patternid" parameter */
3657 edit_free_file(file);
3658 return JB_ERR_CGI_PARAMS;
3661 /* At this point, the line to edit is in cur_line */
3663 freez(cur_line->raw);
3664 freez(cur_line->unprocessed);
3665 cur_line->unprocessed = new_pattern;
3667 err = edit_write_file(file);
3670 /* Error writing file */
3671 if (err == JB_ERR_FILE)
3673 /* Read-only file. */
3674 err = cgi_error_file_read_only(csp, rsp, file->filename);
3676 edit_free_file(file);
3680 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3681 (long) time(NULL), file->identifier, section_start_line_number);
3683 edit_free_file(file);
3685 return cgi_redirect(rsp, target);
3689 /*********************************************************************
3691 * Function : cgi_edit_actions_add_url
3693 * Description : CGI function that actually adds a URL pattern to
3697 * 1 : csp = Current client state (buffers, headers, etc...)
3698 * 2 : rsp = http_response data structure for output
3699 * 3 : parameters = map of cgi parameters
3702 * filename : Identifies the file to edit
3703 * ver : File's last-modified time
3704 * section : Line number of section to edit
3705 * newval : New pattern
3707 * Returns : JB_ERR_OK on success
3708 * JB_ERR_MEMORY on out-of-memory
3709 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3710 * specified or not valid.
3712 *********************************************************************/
3713 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3714 struct http_response *rsp,
3715 const struct map *parameters)
3719 struct file_line * new_line;
3720 struct editable_file * file;
3721 struct file_line * cur_line;
3722 unsigned line_number;
3726 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3728 return cgi_error_disabled(csp, rsp);
3731 err = get_number_param(csp, parameters, "s", §ionid);
3738 return JB_ERR_CGI_PARAMS;
3741 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3747 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3750 /* No filename specified, can't read file, modified, or out of memory. */
3752 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3756 cur_line = file->lines;
3758 while ((cur_line != NULL) && (line_number < sectionid))
3760 cur_line = cur_line->next;
3764 if ( (cur_line == NULL)
3765 || (cur_line->type != FILE_LINE_ACTION))
3767 /* Invalid "sectionid" parameter */
3769 edit_free_file(file);
3770 return JB_ERR_CGI_PARAMS;
3773 /* At this point, the section header is in cur_line - add after this. */
3775 /* Allocate the new line */
3776 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3777 if (new_line == NULL)
3780 edit_free_file(file);
3781 return JB_ERR_MEMORY;
3784 /* Fill in the data members of the new line */
3785 new_line->raw = NULL;
3786 new_line->prefix = NULL;
3787 new_line->unprocessed = new_pattern;
3788 new_line->type = FILE_LINE_URL;
3790 /* Link new_line into the list, after cur_line */
3791 new_line->next = cur_line->next;
3792 cur_line->next = new_line;
3794 /* Done making changes, now commit */
3796 err = edit_write_file(file);
3799 /* Error writing file */
3800 if (err == JB_ERR_FILE)
3802 /* Read-only file. */
3803 err = cgi_error_file_read_only(csp, rsp, file->filename);
3805 edit_free_file(file);
3809 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3810 (long) time(NULL), file->identifier, sectionid);
3812 edit_free_file(file);
3814 return cgi_redirect(rsp, target);
3818 /*********************************************************************
3820 * Function : cgi_edit_actions_remove_url
3822 * Description : CGI function that actually removes a URL pattern from
3826 * 1 : csp = Current client state (buffers, headers, etc...)
3827 * 2 : rsp = http_response data structure for output
3828 * 3 : parameters = map of cgi parameters
3831 * f : (filename) Identifies the file to edit
3832 * v : (version) File's last-modified time
3833 * p : (pattern) Line number of pattern to remove
3835 * Returns : JB_ERR_OK on success
3836 * JB_ERR_MEMORY on out-of-memory
3837 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3838 * specified or not valid.
3840 *********************************************************************/
3841 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3842 struct http_response *rsp,
3843 const struct map *parameters)
3846 struct editable_file * file;
3847 struct file_line * cur_line;
3848 struct file_line * prev_line;
3849 unsigned line_number;
3850 unsigned section_start_line_number = 0;
3854 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3856 return cgi_error_disabled(csp, rsp);
3859 err = get_number_param(csp, parameters, "p", &patternid);
3865 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3868 /* No filename specified, can't read file, modified, or out of memory. */
3869 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3874 cur_line = file->lines;
3876 while ((cur_line != NULL) && (line_number < patternid))
3878 if (cur_line->type == FILE_LINE_ACTION)
3880 section_start_line_number = line_number;
3882 prev_line = cur_line;
3883 cur_line = cur_line->next;
3887 if ( (cur_line == NULL)
3888 || (prev_line == NULL)
3889 || (cur_line->type != FILE_LINE_URL))
3891 /* Invalid "patternid" parameter */
3892 edit_free_file(file);
3893 return JB_ERR_CGI_PARAMS;
3896 /* At this point, the line to remove is in cur_line, and the previous
3897 * one is in prev_line
3900 /* Unlink cur_line */
3901 prev_line->next = cur_line->next;
3902 cur_line->next = NULL;
3905 edit_free_file_lines(cur_line);
3907 err = edit_write_file(file);
3910 /* Error writing file */
3911 if (err == JB_ERR_FILE)
3913 /* Read-only file. */
3914 err = cgi_error_file_read_only(csp, rsp, file->filename);
3916 edit_free_file(file);
3920 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i#l%d",
3921 (long) time(NULL), file->identifier, section_start_line_number);
3923 edit_free_file(file);
3925 return cgi_redirect(rsp, target);
3929 /*********************************************************************
3931 * Function : cgi_edit_actions_section_remove
3933 * Description : CGI function that actually removes a whole section from
3934 * the actions file. The section must be empty first
3935 * (else JB_ERR_CGI_PARAMS).
3938 * 1 : csp = Current client state (buffers, headers, etc...)
3939 * 2 : rsp = http_response data structure for output
3940 * 3 : parameters = map of cgi parameters
3943 * f : (filename) Identifies the file to edit
3944 * v : (version) File's last-modified time
3945 * s : (section) Line number of section to edit
3947 * Returns : JB_ERR_OK on success
3948 * JB_ERR_MEMORY on out-of-memory
3949 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3950 * specified or not valid.
3952 *********************************************************************/
3953 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3954 struct http_response *rsp,
3955 const struct map *parameters)
3958 struct editable_file * file;
3959 struct file_line * cur_line;
3960 struct file_line * prev_line;
3961 unsigned line_number;
3965 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3967 return cgi_error_disabled(csp, rsp);
3970 err = get_number_param(csp, parameters, "s", §ionid);
3976 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3979 /* No filename specified, can't read file, modified, or out of memory. */
3980 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3984 cur_line = file->lines;
3987 while ((cur_line != NULL) && (line_number < sectionid))
3989 prev_line = cur_line;
3990 cur_line = cur_line->next;
3994 if ( (cur_line == NULL)
3995 || (cur_line->type != FILE_LINE_ACTION) )
3997 /* Invalid "sectionid" parameter */
3998 edit_free_file(file);
3999 return JB_ERR_CGI_PARAMS;
4002 if ( (cur_line->next != NULL)
4003 && (cur_line->next->type == FILE_LINE_URL) )
4005 /* Section not empty. */
4006 edit_free_file(file);
4007 return JB_ERR_CGI_PARAMS;
4010 /* At this point, the line to remove is in cur_line, and the previous
4011 * one is in prev_line
4014 /* Unlink cur_line */
4015 if (prev_line == NULL)
4017 /* Removing the first line from the file */
4018 file->lines = cur_line->next;
4022 prev_line->next = cur_line->next;
4024 cur_line->next = NULL;
4027 edit_free_file_lines(cur_line);
4029 err = edit_write_file(file);
4032 /* Error writing file */
4033 if (err == JB_ERR_FILE)
4035 /* Read-only file. */
4036 err = cgi_error_file_read_only(csp, rsp, file->filename);
4038 edit_free_file(file);
4042 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4043 (long) time(NULL), file->identifier);
4045 edit_free_file(file);
4047 return cgi_redirect(rsp, target);
4051 /*********************************************************************
4053 * Function : cgi_edit_actions_section_add
4055 * Description : CGI function that adds a new empty section to
4059 * 1 : csp = Current client state (buffers, headers, etc...)
4060 * 2 : rsp = http_response data structure for output
4061 * 3 : parameters = map of cgi parameters
4064 * f : (filename) Identifies the file to edit
4065 * v : (version) File's last-modified time
4066 * s : (section) Line number of section to add after, 0 for
4069 * Returns : JB_ERR_OK on success
4070 * JB_ERR_MEMORY on out-of-memory
4071 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4072 * specified or not valid.
4074 *********************************************************************/
4075 jb_err cgi_edit_actions_section_add(struct client_state *csp,
4076 struct http_response *rsp,
4077 const struct map *parameters)
4080 struct file_line * new_line;
4082 struct editable_file * file;
4083 struct file_line * cur_line;
4084 unsigned line_number;
4088 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4090 return cgi_error_disabled(csp, rsp);
4093 err = get_number_param(csp, parameters, "s", §ionid);
4099 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4102 /* No filename specified, can't read file, modified, or out of memory. */
4103 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4107 cur_line = file->lines;
4109 if (sectionid <= 1U)
4111 /* Add to start of file */
4112 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4114 /* There's something in the file, find the line before the first
4117 while ( (cur_line->next != NULL)
4118 && (cur_line->next->type != FILE_LINE_ACTION) )
4120 cur_line = cur_line->next;
4126 /* File starts with action line, so insert at top */
4132 /* Add after stated section. */
4133 while ((cur_line != NULL) && (line_number < sectionid))
4135 cur_line = cur_line->next;
4139 if ( (cur_line == NULL)
4140 || (cur_line->type != FILE_LINE_ACTION))
4142 /* Invalid "sectionid" parameter */
4143 edit_free_file(file);
4144 return JB_ERR_CGI_PARAMS;
4147 /* Skip through the section to find the last line in it. */
4148 while ( (cur_line->next != NULL)
4149 && (cur_line->next->type != FILE_LINE_ACTION) )
4151 cur_line = cur_line->next;
4156 /* At this point, the last line in the previous section is in cur_line
4157 * - add after this. (Or if we need to add as the first line, cur_line
4161 new_text = strdup("{}");
4162 if (NULL == new_text)
4164 edit_free_file(file);
4165 return JB_ERR_MEMORY;
4168 /* Allocate the new line */
4169 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4170 if (new_line == NULL)
4173 edit_free_file(file);
4174 return JB_ERR_MEMORY;
4177 /* Fill in the data members of the new line */
4178 new_line->raw = NULL;
4179 new_line->prefix = NULL;
4180 new_line->unprocessed = new_text;
4181 new_line->type = FILE_LINE_ACTION;
4183 if (cur_line != NULL)
4185 /* Link new_line into the list, after cur_line */
4186 new_line->next = cur_line->next;
4187 cur_line->next = new_line;
4191 /* Link new_line into the list, as first line */
4192 new_line->next = file->lines;
4193 file->lines = new_line;
4196 /* Done making changes, now commit */
4198 err = edit_write_file(file);
4201 /* Error writing file */
4202 if (err == JB_ERR_FILE)
4204 /* Read-only file. */
4205 err = cgi_error_file_read_only(csp, rsp, file->filename);
4207 edit_free_file(file);
4211 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4212 (long) time(NULL), file->identifier);
4214 edit_free_file(file);
4216 return cgi_redirect(rsp, target);
4220 /*********************************************************************
4222 * Function : cgi_edit_actions_section_swap
4224 * Description : CGI function that swaps the order of two sections
4225 * in the actions file. Note that this CGI can actually
4226 * swap any two arbitrary sections, but the GUI interface
4227 * currently only allows consecutive sections to be
4231 * 1 : csp = Current client state (buffers, headers, etc...)
4232 * 2 : rsp = http_response data structure for output
4233 * 3 : parameters = map of cgi parameters
4236 * f : (filename) Identifies the file to edit
4237 * v : (version) File's last-modified time
4238 * s1 : (section1) Line number of first section to swap
4239 * s2 : (section2) Line number of second section to swap
4241 * Returns : JB_ERR_OK on success
4242 * JB_ERR_MEMORY on out-of-memory
4243 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4244 * specified or not valid.
4246 *********************************************************************/
4247 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4248 struct http_response *rsp,
4249 const struct map *parameters)
4253 struct editable_file * file;
4254 struct file_line * cur_line;
4255 struct file_line * prev_line;
4256 struct file_line * line_before_section1;
4257 struct file_line * line_start_section1;
4258 struct file_line * line_end_section1;
4259 struct file_line * line_after_section1;
4260 struct file_line * line_before_section2;
4261 struct file_line * line_start_section2;
4262 struct file_line * line_end_section2;
4263 struct file_line * line_after_section2;
4264 unsigned line_number;
4268 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4270 return cgi_error_disabled(csp, rsp);
4273 err = get_number_param(csp, parameters, "s1", §ion1);
4274 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4280 if (section1 > section2)
4282 unsigned temp = section2;
4283 section2 = section1;
4287 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4290 /* No filename specified, can't read file, modified, or out of memory. */
4291 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4294 /* Start at the beginning... */
4296 cur_line = file->lines;
4299 /* ... find section1 ... */
4300 while ((cur_line != NULL) && (line_number < section1))
4302 prev_line = cur_line;
4303 cur_line = cur_line->next;
4307 if ( (cur_line == NULL)
4308 || (cur_line->type != FILE_LINE_ACTION) )
4310 /* Invalid "section1" parameter */
4311 edit_free_file(file);
4312 return JB_ERR_CGI_PARAMS;
4315 /* If no-op, we've validated params and can skip the rest. */
4316 if (section1 != section2)
4318 /* ... find the end of section1 ... */
4319 line_before_section1 = prev_line;
4320 line_start_section1 = cur_line;
4323 prev_line = cur_line;
4324 cur_line = cur_line->next;
4327 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4328 line_end_section1 = prev_line;
4329 line_after_section1 = cur_line;
4331 /* ... find section2 ... */
4332 while ((cur_line != NULL) && (line_number < section2))
4334 prev_line = cur_line;
4335 cur_line = cur_line->next;
4339 if ( (cur_line == NULL)
4340 || (cur_line->type != FILE_LINE_ACTION) )
4342 /* Invalid "section2" parameter */
4343 edit_free_file(file);
4344 return JB_ERR_CGI_PARAMS;
4347 /* ... find the end of section2 ... */
4348 line_before_section2 = prev_line;
4349 line_start_section2 = cur_line;
4352 prev_line = cur_line;
4353 cur_line = cur_line->next;
4356 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4357 line_end_section2 = prev_line;
4358 line_after_section2 = cur_line;
4360 /* Now have all the pointers we need. Do the swap. */
4362 /* Change the pointer to section1 to point to section2 instead */
4363 if (line_before_section1 == NULL)
4365 file->lines = line_start_section2;
4369 line_before_section1->next = line_start_section2;
4372 if (line_before_section2 == line_end_section1)
4374 /* Consecutive sections */
4375 line_end_section2->next = line_start_section1;
4379 line_end_section2->next = line_after_section1;
4380 line_before_section2->next = line_start_section1;
4383 /* Set the pointer from the end of section1 to the rest of the file */
4384 line_end_section1->next = line_after_section2;
4386 err = edit_write_file(file);
4389 /* Error writing file */
4390 if (err == JB_ERR_FILE)
4392 /* Read-only file. */
4393 err = cgi_error_file_read_only(csp, rsp, file->filename);
4395 edit_free_file(file);
4398 } /* END if (section1 != section2) */
4400 snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%i",
4401 (long) time(NULL), file->identifier);
4403 edit_free_file(file);
4405 return cgi_redirect(rsp, target);
4408 #ifdef FEATURE_TOGGLE
4409 /*********************************************************************
4411 * Function : cgi_toggle
4413 * Description : CGI function that adds a new empty section to
4417 * 1 : csp = Current client state (buffers, headers, etc...)
4418 * 2 : rsp = http_response data structure for output
4419 * 3 : parameters = map of cgi parameters
4422 * set : If present, how to change toggle setting:
4423 * "enable", "disable", "toggle", or none (default).
4424 * mini : If present, use mini reply template.
4426 * Returns : JB_ERR_OK on success
4427 * JB_ERR_MEMORY on out-of-memory
4429 *********************************************************************/
4430 jb_err cgi_toggle(struct client_state *csp,
4431 struct http_response *rsp,
4432 const struct map *parameters)
4434 struct map *exports;
4436 const char *template_name;
4442 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4444 return cgi_error_disabled(csp, rsp);
4447 mode = get_char_param(parameters, "set");
4452 global_toggle_state = 1;
4454 else if (mode == 'D')
4457 global_toggle_state = 0;
4459 else if (mode == 'T')
4462 global_toggle_state = !global_toggle_state;
4465 if (NULL == (exports = default_exports(csp, "toggle")))
4467 return JB_ERR_MEMORY;
4470 template_name = (get_char_param(parameters, "mini")
4474 return template_fill_for_cgi(csp, template_name, exports, rsp);
4476 #endif /* def FEATURE_TOGGLE */
4478 /*********************************************************************
4480 * Function : javascriptify
4482 * Description : Converts a string into a form JavaScript will like.
4484 * Netscape 4's JavaScript sucks - it doesn't use
4485 * "id" parameters, so you have to set the "name"
4486 * used to submit a form element to something JavaScript
4487 * will like. (Or access the elements by index in an
4488 * array. That array contains >60 elements and will
4489 * be changed whenever we add a new action to the
4490 * editor, so I'm NOT going to use indexes that have
4491 * to be figured out by hand.)
4493 * Currently the only thing we have to worry about
4494 * is "-" ==> "_" conversion.
4496 * This is a length-preserving operation so it is
4497 * carried out in-place, no memory is allocated
4501 * 1 : identifier = String to make JavaScript-friendly.
4505 *********************************************************************/
4506 static void javascriptify(char * identifier)
4508 char * p = identifier;
4509 while (NULL != (p = strchr(p, '-')))
4516 /*********************************************************************
4518 * Function : actions_to_radio
4520 * Description : Converts a actionsfile entry into settings for
4521 * radio buttons and edit boxes on a HTML form.
4524 * 1 : exports = List of substitutions to add to.
4525 * 2 : action = Action to read
4527 * Returns : JB_ERR_OK on success
4528 * JB_ERR_MEMORY on out-of-memory
4530 *********************************************************************/
4531 static jb_err actions_to_radio(struct map * exports,
4532 const struct action_spec *action)
4534 unsigned mask = action->mask;
4535 unsigned add = action->add;
4543 mask = action->mask;
4546 /* sanity - prevents "-feature +feature" */
4550 #define DEFINE_ACTION_BOOL(name, bit) \
4551 if (!(mask & bit)) \
4553 current_mode = 'n'; \
4555 else if (add & bit) \
4557 current_mode = 'y'; \
4561 current_mode = 'x'; \
4563 if (map_radio(exports, name, "ynx", current_mode)) \
4565 return JB_ERR_MEMORY; \
4568 #define DEFINE_ACTION_STRING(name, bit, index) \
4569 DEFINE_ACTION_BOOL(name, bit); \
4572 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4575 checked = !strcmp(action->string[index], value); \
4579 checked = is_default; \
4581 mapped_param |= checked; \
4582 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4584 return JB_ERR_MEMORY; \
4587 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4588 if (map(exports, name "-param-custom", 1, \
4589 ((!mapped_param) ? "checked" : ""), 1)) \
4591 return JB_ERR_MEMORY; \
4593 if (map(exports, name "-param", 1, \
4594 (((add & bit) && !mapped_param) ? \
4595 action->string[index] : default_val), 1)) \
4597 return JB_ERR_MEMORY; \
4600 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4601 if (map(exports, name "-param", 1, \
4602 ((add & bit) ? action->string[index] : default_val), 1)) \
4604 return JB_ERR_MEMORY; \
4607 #define DEFINE_ACTION_MULTI(name, index) \
4608 if (action->multi_add[index]->first) \
4610 current_mode = 'y'; \
4612 else if (action->multi_remove_all[index]) \
4614 current_mode = 'n'; \
4616 else if (action->multi_remove[index]->first) \
4618 current_mode = 'y'; \
4622 current_mode = 'x'; \
4624 if (map_radio(exports, name, "ynx", current_mode)) \
4626 return JB_ERR_MEMORY; \
4629 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4631 #include "actionlist.h"
4633 #undef DEFINE_ACTION_MULTI
4634 #undef DEFINE_ACTION_STRING
4635 #undef DEFINE_ACTION_BOOL
4636 #undef DEFINE_ACTION_ALIAS
4637 #undef DEFINE_CGI_PARAM_CUSTOM
4638 #undef DEFINE_CGI_PARAM_RADIO
4639 #undef DEFINE_CGI_PARAM_NO_RADIO
4645 /*********************************************************************
4647 * Function : actions_from_radio
4649 * Description : Converts a map of parameters passed to a CGI function
4650 * into an actionsfile entry.
4653 * 1 : parameters = parameters to the CGI call
4654 * 2 : action = Action to change. Must be valid before
4655 * the call, actions not specified will be
4658 * Returns : JB_ERR_OK on success
4659 * JB_ERR_MEMORY on out-of-memory
4661 *********************************************************************/
4662 static jb_err actions_from_radio(const struct map * parameters,
4663 struct action_spec *action)
4668 const char * js_name;
4669 jb_err err = JB_ERR_OK;
4674 /* Statics are generally a potential race condition,
4675 * but in this case we're safe and don't need semaphores.
4676 * Be careful if you modify this function.
4678 * The js_name_arr's are never free()d, but this is no
4679 * problem, since they will only be created once and
4680 * used by all threads thereafter. -oes
4683 #define JAVASCRIPTIFY(dest_var, string) \
4685 static int first_time = 1; \
4686 static char *js_name_arr; \
4689 js_name_arr = strdup(string); \
4690 javascriptify(js_name_arr); \
4692 dest_var = js_name_arr; \
4696 #define DEFINE_ACTION_BOOL(name, bit) \
4697 JAVASCRIPTIFY(js_name, name); \
4698 ch = get_char_param(parameters, js_name); \
4701 action->add |= bit; \
4702 action->mask |= bit; \
4704 else if (ch == 'N') \
4706 action->add &= ~bit; \
4707 action->mask &= ~bit; \
4709 else if (ch == 'X') \
4711 action->add &= ~bit; \
4712 action->mask |= bit; \
4715 #define DEFINE_ACTION_STRING(name, bit, index) \
4716 JAVASCRIPTIFY(js_name, name); \
4717 ch = get_char_param(parameters, js_name); \
4721 JAVASCRIPTIFY(js_name, name "-mode"); \
4722 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4723 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4725 JAVASCRIPTIFY(js_name, name "-param"); \
4726 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4728 if (param != NULL) \
4730 if (NULL == (param_dup = strdup(param))) \
4732 return JB_ERR_MEMORY; \
4734 freez(action->string[index]); \
4735 action->add |= bit; \
4736 action->mask |= bit; \
4737 action->string[index] = param_dup; \
4740 else if (ch == 'N') \
4742 if (action->add & bit) \
4744 freez(action->string[index]); \
4746 action->add &= ~bit; \
4747 action->mask &= ~bit; \
4749 else if (ch == 'X') \
4751 if (action->add & bit) \
4753 freez(action->string[index]); \
4755 action->add &= ~bit; \
4756 action->mask |= bit; \
4759 #define DEFINE_ACTION_MULTI(name, index) \
4760 JAVASCRIPTIFY(js_name, name); \
4761 ch = get_char_param(parameters, js_name); \
4766 else if (ch == 'N') \
4768 list_remove_all(action->multi_add[index]); \
4769 list_remove_all(action->multi_remove[index]); \
4770 action->multi_remove_all[index] = 1; \
4772 else if (ch == 'X') \
4774 list_remove_all(action->multi_add[index]); \
4775 list_remove_all(action->multi_remove[index]); \
4776 action->multi_remove_all[index] = 0; \
4779 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4781 #include "actionlist.h"
4783 #undef DEFINE_ACTION_MULTI
4784 #undef DEFINE_ACTION_STRING
4785 #undef DEFINE_ACTION_BOOL
4786 #undef DEFINE_ACTION_ALIAS
4787 #undef JAVASCRIPTIFY
4793 #endif /* def FEATURE_CGI_EDIT_ACTIONS */