1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.48 2007/02/13 14:35:25 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
10 * NOTE: The CGIs in this file use parameter names
11 * such as "f" and "s" which are really *BAD* choices.
12 * However, I'm trying to save bytes in the
13 * edit-actions-list HTML page - the standard actions
14 * file generated a 550kbyte page, which is ridiculous.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001-2007 the SourceForge
19 * Privoxy team. http://www.privoxy.org/
21 * Based on the Internet Junkbuster originally written
22 * by and Copyright (C) 1997 Anonymous Coders and
23 * Junkbusters Corporation. http://www.junkbusters.com
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General
27 * Public License as published by the Free Software
28 * Foundation; either version 2 of the License, or (at
29 * your option) any later version.
31 * This program is distributed in the hope that it will
32 * be useful, but WITHOUT ANY WARRANTY; without even the
33 * implied warranty of MERCHANTABILITY or FITNESS FOR A
34 * PARTICULAR PURPOSE. See the GNU General Public
35 * License for more details.
37 * The GNU General Public License should be included with
38 * this file. If not, you can view it at
39 * http://www.gnu.org/copyleft/gpl.html
40 * or write to the Free Software Foundation, Inc., 59
41 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 * Revision 1.48 2007/02/13 14:35:25 fabiankeil
46 * Replace hash escaping code to prevent
47 * crashes, memory and file corruption.
49 * Revision 1.47 2006/12/28 18:04:25 fabiankeil
50 * Fixed gcc43 conversion warnings.
52 * Revision 1.46 2006/12/27 18:44:52 fabiankeil
53 * Stop shadowing string.h's index().
55 * Revision 1.45 2006/12/21 12:57:48 fabiankeil
56 * Add config option "split-large-forms"
57 * to work around the browser bug reported
60 * Revision 1.44 2006/12/09 13:49:16 fabiankeil
61 * Fix configure option --disable-toggle.
62 * Thanks to Peter Thoenen for reporting this.
64 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
65 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
66 * with what was really the latest development (the v_3_0_branch branch)
68 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
69 * Remove a little residual debugging info
71 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
72 * Multiple filter file support
74 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
75 * Undo some damaging memory leak patches
77 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
78 * Removing a double free
80 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
81 * A few memory leaks plugged (mostly on error paths)
83 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
84 * Moved cgi_error_disabled() from cgiedit.c to
85 * cgi.c to re-enable build with --disable-editor.
86 * Fixes Bug #892744. Thanks to Matthew Fischer
89 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
90 * One line lost in last commit
92 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
93 * - All edit functions that redirect back to the list page
94 * now use cgi_redirect
95 * - All redirects now contain useless parameter "foo", whose
96 * value are raw seconds since epoch, in order to force
97 * Opera and Konqueror to properly reload the list. Closes
100 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
101 * Cosmetic: Renamed cryptic variable
103 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
104 * Fix: Don't free uninitialized struct editable_file
106 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
107 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
109 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
110 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
112 * Revision 1.41 2002/05/21 19:09:45 oes
113 * - Made Add/Edit/Remove URL Submit and Cancel
114 * buttons jump back to relevant section in eal
115 * - Bugfix: remove-url-form needs p export
117 * Revision 1.40 2002/05/19 11:34:35 jongfoster
118 * Handling read-only actions files better - report the actual
119 * error, not "Out of memory"!
122 * http://sourceforge.net/tracker/index.php?func=detail
123 * &aid=557905&group_id=11118&atid=111118
125 * Revision 1.39 2002/05/12 21:39:15 jongfoster
126 * - Adding Doxygen-style comments to structures and #defines.
127 * - Correcting function comments
129 * Revision 1.38 2002/05/03 23:00:38 jongfoster
130 * Support for templates for "standard actions" buttons.
133 * Revision 1.37 2002/04/30 11:14:52 oes
134 * Made csp the first parameter in *action_to_html
136 * Revision 1.36 2002/04/26 21:53:30 jongfoster
137 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
139 * Revision 1.35 2002/04/26 21:50:02 jongfoster
140 * Honouring default exports in edit-actions-for-url-filter template.
142 * Revision 1.34 2002/04/26 12:54:17 oes
143 * Adaptions to changes in actions.c
145 * Revision 1.33 2002/04/24 02:17:47 oes
146 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
148 * - Activated Jon's code for editing multiple AFs
149 * - cgi_edit_list_actions now provides context-sensitive
150 * help, looks up all action sets from standard.action and
151 * makes buttons for them in the catchall section
152 * - cgi_edit_action_submit now honors a p parameter, looks up
153 * the corresponding action set, and sets the catchall pattern's
154 * actions accordingly.
156 * Revision 1.32 2002/04/19 16:55:31 jongfoster
157 * Fixing newline problems. If we do our own text file newline
158 * mangling, we don't want the library to do any, so we need to
159 * open the files in *binary* mode.
161 * Revision 1.31 2002/04/18 19:21:08 jongfoster
162 * Added code to detect "conventional" action files, that start
163 * with a set of actions for all URLs (the pattern "/").
164 * These are special-cased in the "edit-actions-list" CGI, so
165 * that a special UI can be written for them.
167 * Revision 1.30 2002/04/10 13:38:35 oes
168 * load_template signature changed
170 * Revision 1.29 2002/04/08 16:59:08 oes
173 * Revision 1.28 2002/03/27 12:30:29 oes
174 * Deleted unsused variable
176 * Revision 1.27 2002/03/26 23:06:04 jongfoster
177 * Removing duplicate @ifs on the toggle page
179 * Revision 1.26 2002/03/26 22:59:17 jongfoster
180 * Fixing /toggle to display status consistently.
182 * Revision 1.25 2002/03/26 22:29:54 swa
183 * we have a new homepage!
185 * Revision 1.24 2002/03/24 15:23:33 jongfoster
188 * Revision 1.23 2002/03/24 13:32:41 swa
189 * name change related issues
191 * Revision 1.22 2002/03/24 13:25:43 swa
192 * name change related issues
194 * Revision 1.21 2002/03/22 18:02:48 jongfoster
195 * Fixing remote toggle
197 * Revision 1.20 2002/03/16 20:28:34 oes
198 * Added descriptions to the filters so users will know what they select in the cgi editor
200 * Revision 1.19 2002/03/16 18:38:14 jongfoster
201 * Stopping stupid or malicious users from breaking the actions
202 * file using the web-based editor.
204 * Revision 1.18 2002/03/16 14:57:44 jongfoster
205 * Full support for enabling/disabling modular filters.
207 * Revision 1.17 2002/03/16 14:26:42 jongfoster
208 * First version of modular filters support - READ ONLY!
209 * Fixing a double-free bug in the out-of-memory handling in map_radio().
211 * Revision 1.16 2002/03/07 03:46:17 oes
212 * Fixed compiler warnings
214 * Revision 1.15 2002/03/06 22:54:35 jongfoster
215 * Automated function-comment nitpicking.
217 * Revision 1.14 2002/03/05 00:24:51 jongfoster
218 * Patch to always edit the current actions file.
220 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
221 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
223 * Revision 1.12 2002/03/03 09:18:03 joergs
224 * Made jumbjuster work on AmigaOS again.
226 * Revision 1.11 2002/01/23 01:03:31 jongfoster
227 * Fixing gcc [CygWin] compiler warnings
229 * Revision 1.10 2002/01/23 00:22:59 jongfoster
230 * Adding new function cgi_edit_actions_section_swap(), to reorder
233 * Adding get_url_spec_param() to get a validated URL pattern.
235 * Moving edit_read_line() out of this file and into loaders.c.
237 * Adding missing html_encode() to many CGI functions.
239 * Moving the functions that #include actionlist.h to the end of the file,
240 * because the Visual C++ 97 debugger gets extremely confused if you try
241 * to debug any code that comes after them in the file.
243 * Major optimizations in cgi_edit_actions_list() to reduce the size of
244 * the generated HTML (down 40% from 550k to 304k), with major side-effects
245 * throughout the editor and templates. In particular, the length of the
246 * URLs throughout the editor has been drastically reduced, by cutting
247 * paramater names down to 1 character and CGI names down to 3-4
248 * characters, by removing all non-essential CGI paramaters even at the
249 * expense of having to re-read the actions file for the most trivial
250 * page, and by using relative rather than absolute URLs. This means
251 * that this (typical example):
253 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
254 * filename=ijb&ver=1011487572&section=12&pattern=13
255 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
259 * <a href="eau?f=ijb&v=1011487572&p=13">
261 * Revision 1.9 2002/01/17 20:56:22 jongfoster
262 * Replacing hard references to the URL of the config interface
263 * with #defines from project.h
265 * Revision 1.8 2001/11/30 23:35:51 jongfoster
266 * Renaming actionsfile to ijb.action
268 * Revision 1.7 2001/11/13 00:28:24 jongfoster
269 * - Renaming parameters from edit-actions-for-url so that they only
270 * contain legal JavaScript characters. If we wanted to write
271 * JavaScript that worked with Netscape 4, this is nessacery.
272 * (Note that at the moment the JavaScript doesn't actually work
273 * with Netscape 4, but now this is purely a template issue, not
274 * one affecting code).
275 * - Adding new CGIs for use by non-JavaScript browsers:
276 * edit-actions-url-form
277 * edit-actions-add-url-form
278 * edit-actions-remove-url-form
281 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
282 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
283 * by and __OS2__ ifdef.
285 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
286 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
287 * threads to call select() simultaneously. So, it's time to do a real, live,
288 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
289 * (native). Both versions will work, but using __OS2__ offers multi-threading.
291 * Revision 1.4 2001/10/23 21:48:19 jongfoster
292 * Cleaning up error handling in CGI functions - they now send back
293 * a HTML error page and should never cause a FATAL error. (Fixes one
294 * potential source of "denial of service" attacks).
296 * CGI actions file editor that works and is actually useful.
298 * Ability to toggle JunkBuster remotely using a CGI call.
300 * You can turn off both the above features in the main configuration
301 * file, e.g. if you are running a multi-user proxy.
303 * Revision 1.3 2001/10/14 22:12:49 jongfoster
304 * New version of CGI-based actionsfile editor.
305 * Major changes, including:
306 * - Completely new file parser and file output routines
307 * - edit-actions CGI renamed edit-actions-for-url
308 * - All CGIs now need a filename parameter, except for...
309 * - New CGI edit-actions which doesn't need a filename,
310 * to allow you to start the editor up.
311 * - edit-actions-submit now works, and now automatically
312 * redirects you back to the main edit-actions-list handler.
314 * Revision 1.2 2001/09/16 17:05:14 jongfoster
315 * Removing unused #include showarg.h
317 * Revision 1.1 2001/09/16 15:47:37 jongfoster
318 * First version of CGI-based edit interface. This is very much a
319 * work-in-progress, and you can't actually use it to edit anything
320 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
321 * to have any effect.
324 **********************************************************************/
330 * FIXME: Following includes copied from cgi.c - which are actually needed?
335 #include <sys/types.h>
339 #include <sys/stat.h>
342 #define snprintf _snprintf
343 #endif /* def _WIN32 */
348 #include "cgisimple.h"
352 #include "miscutil.h"
355 #ifdef FEATURE_TOGGLE
356 /* loadcfg.h is for global_toggle_state only */
358 #endif /* def FEATURE_TOGGLE */
359 #include "urlmatch.h"
361 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
364 #ifdef FEATURE_CGI_EDIT_ACTIONS
367 * A line in an editable_file.
371 /** Next entry in the linked list */
372 struct file_line * next;
374 /** The raw data, to write out if this line is unmodified. */
377 /** Comments and/or whitespace to put before this line if it's modified
378 and then written out. */
381 /** The actual data, as a string. Line continuation and comment removal
382 are performed on the data read from file before it's stored here, so
383 it will be a single line of data. */
386 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
389 /** The actual data, processed into some sensible data type. */
393 /** An action specification. */
394 struct action_spec action[1];
396 /** A name=value pair. */
400 /** The name in the name=value pair. */
403 /** The value in the name=value pair, as a string. */
406 /** The value in the name=value pair, as an integer. */
411 /* Add more data types here... e.g.
414 struct url_spec url[1];
418 struct action_spec action[1];
428 /** This file_line has not been processed yet. */
429 #define FILE_LINE_UNPROCESSED 1
431 /** This file_line is blank. Can only appear at the end of a file, due to
432 the way the parser works. */
433 #define FILE_LINE_BLANK 2
435 /** This file_line says {{alias}}. */
436 #define FILE_LINE_ALIAS_HEADER 3
438 /** This file_line defines an alias. */
439 #define FILE_LINE_ALIAS_ENTRY 4
441 /** This file_line defines an {action}. */
442 #define FILE_LINE_ACTION 5
444 /** This file_line specifies a URL pattern. */
445 #define FILE_LINE_URL 6
447 /** This file_line says {{settings}}. */
448 #define FILE_LINE_SETTINGS_HEADER 7
450 /** This file_line is in a {{settings}} block. */
451 #define FILE_LINE_SETTINGS_ENTRY 8
453 /** This file_line says {{description}}. */
454 #define FILE_LINE_DESCRIPTION_HEADER 9
456 /** This file_line is in a {{description}} block. */
457 #define FILE_LINE_DESCRIPTION_ENTRY 10
461 * A configuration file, in a format that can be edited and written back to
466 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
467 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
468 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
469 /**< Pre-encoded with url_encode() for ease of use. */
470 const char * version_str; /**< Last modification time, as a string. For CGI param. */
471 /**< Can be used in URL without using url_param(). */
472 unsigned version; /**< Last modification time - prevents chaos with
473 the browser's "back" button. Note that this is a
474 time_t cast to an unsigned. When comparing, always
475 cast the time_t to an unsigned, and *NOT* vice-versa.
476 This may lose the top few bits, but they're not
477 significant anyway. */
478 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
479 Note that changing this after the file has been
480 read in will cause a mess. */
481 struct file_line * parse_error; /**< On parse error, this is the offending line. */
482 const char * parse_error_text; /**< On parse error, this is the problem.
483 (Statically allocated) */
486 /* FIXME: Following non-static functions should be prototyped in .h or made static */
488 /* Functions to read and write arbitrary config files */
489 jb_err edit_read_file(struct client_state *csp,
490 const struct map *parameters,
493 struct editable_file **pfile);
494 jb_err edit_write_file(struct editable_file * file);
495 void edit_free_file(struct editable_file * file);
497 /* Functions to read and write actions files */
498 jb_err edit_parse_actions_file(struct editable_file * file);
499 jb_err edit_read_actions_file(struct client_state *csp,
500 struct http_response *rsp,
501 const struct map *parameters,
503 struct editable_file **pfile);
506 jb_err cgi_error_modified(struct client_state *csp,
507 struct http_response *rsp,
508 const char *filename);
509 jb_err cgi_error_parse(struct client_state *csp,
510 struct http_response *rsp,
511 struct editable_file *file);
512 jb_err cgi_error_file(struct client_state *csp,
513 struct http_response *rsp,
514 const char *filename);
515 jb_err cgi_error_file_read_only(struct client_state *csp,
516 struct http_response *rsp,
517 const char *filename);
519 /* Internal arbitrary config file support functions */
520 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
521 static void edit_free_file_lines(struct file_line * first_line);
523 /* Internal actions file support functions */
524 static int match_actions_file_header_line(const char * line, const char * name);
525 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
527 /* Internal parameter parsing functions */
528 static jb_err get_file_name_param(struct client_state *csp,
529 const struct map *parameters,
530 const char *param_name,
533 const char **pparam);
535 static jb_err get_url_spec_param(struct client_state *csp,
536 const struct map *parameters,
541 /* Internal actionsfile <==> HTML conversion functions */
542 static jb_err map_radio(struct map * exports,
543 const char * optionname,
546 static jb_err actions_to_radio(struct map * exports,
547 const struct action_spec *action);
548 static jb_err actions_from_radio(const struct map * parameters,
549 struct action_spec *action);
552 static jb_err map_copy_parameter_html(struct map *out,
553 const struct map *in,
555 #if 0 /* unused function */
556 static jb_err map_copy_parameter_url(struct map *out,
557 const struct map *in,
559 #endif /* unused function */
561 /* Internal convenience functions */
562 static char *section_target(const unsigned sectionid);
564 /*********************************************************************
566 * Function : section_target
568 * Description : Given an unsigned (section id) n, produce a dynamically
569 * allocated string of the form #l<n>, for use in link
573 * 1 : sectionid = start line number of section
575 * Returns : String with link target, or NULL if out of
578 *********************************************************************/
579 static char *section_target(const unsigned sectionid)
583 snprintf(buf, 30, "#l%d", sectionid);
589 /*********************************************************************
591 * Function : map_copy_parameter_html
593 * Description : Copy a CGI parameter from one map to another, HTML
597 * 1 : out = target map
598 * 2 : in = source map
599 * 3 : name = name of cgi parameter to copy
601 * Returns : JB_ERR_OK on success
602 * JB_ERR_MEMORY on out-of-memory
603 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
606 *********************************************************************/
607 static jb_err map_copy_parameter_html(struct map *out,
608 const struct map *in,
618 value = lookup(in, name);
619 err = map(out, name, 1, html_encode(value), 0);
626 else if (*value == '\0')
628 return JB_ERR_CGI_PARAMS;
637 #if 0 /* unused function */
638 /*********************************************************************
640 * Function : map_copy_parameter_url
642 * Description : Copy a CGI parameter from one map to another, URL
646 * 1 : out = target map
647 * 2 : in = source map
648 * 3 : name = name of cgi parameter to copy
650 * Returns : JB_ERR_OK on success
651 * JB_ERR_MEMORY on out-of-memory
652 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
655 *********************************************************************/
656 static jb_err map_copy_parameter_url(struct map *out,
657 const struct map *in,
667 value = lookup(in, name);
668 err = map(out, name, 1, url_encode(value), 0);
675 else if (*value == '\0')
677 return JB_ERR_CGI_PARAMS;
684 #endif /* 0 - unused function */
686 /*********************************************************************
688 * Function : cgi_edit_actions_url_form
690 * Description : CGI function that displays a form for
694 * 1 : csp = Current client state (buffers, headers, etc...)
695 * 2 : rsp = http_response data structure for output
696 * 3 : parameters = map of cgi parameters
699 * f : (filename) Identifies the file to edit
700 * v : (version) File's last-modified time
701 * p : (pattern) Line number of pattern to edit
703 * Returns : JB_ERR_OK on success
704 * JB_ERR_MEMORY on out-of-memory
705 * JB_ERR_CGI_PARAMS if the CGI parameters are not
706 * specified or not valid.
708 *********************************************************************/
709 jb_err cgi_edit_actions_url_form(struct client_state *csp,
710 struct http_response *rsp,
711 const struct map *parameters)
713 struct map * exports;
715 struct editable_file * file;
716 struct file_line * cur_line;
717 unsigned line_number;
718 unsigned section_start_line_number = 0;
725 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
727 return cgi_error_disabled(csp, rsp);
730 err = get_number_param(csp, parameters, "p", &patternid);
736 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
739 /* No filename specified, can't read file, modified, or out of memory. */
740 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
743 cur_line = file->lines;
745 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
747 if (cur_line->type == FILE_LINE_ACTION)
749 section_start_line_number = line_number;
751 cur_line = cur_line->next;
754 if ( (cur_line == NULL)
755 || (line_number != patternid)
757 || (cur_line->type != FILE_LINE_URL))
759 /* Invalid "patternid" parameter */
760 edit_free_file(file);
761 return JB_ERR_CGI_PARAMS;
764 if (NULL == (exports = default_exports(csp, NULL)))
766 edit_free_file(file);
767 return JB_ERR_MEMORY;
770 err = map(exports, "f", 1, file->identifier, 1);
771 if (!err) err = map(exports, "v", 1, file->version_str, 1);
772 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
773 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
774 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
776 edit_free_file(file);
784 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
788 /*********************************************************************
790 * Function : cgi_edit_actions_add_url_form
792 * Description : CGI function that displays a form for
796 * 1 : csp = Current client state (buffers, headers, etc...)
797 * 2 : rsp = http_response data structure for output
798 * 3 : parameters = map of cgi parameters
801 * f : (filename) Identifies the file to edit
802 * v : (version) File's last-modified time
803 * s : (section) Line number of section to edit
805 * Returns : JB_ERR_OK on success
806 * JB_ERR_MEMORY on out-of-memory
807 * JB_ERR_CGI_PARAMS if the CGI parameters are not
808 * specified or not valid.
810 *********************************************************************/
811 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
812 struct http_response *rsp,
813 const struct map *parameters)
822 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
824 return cgi_error_disabled(csp, rsp);
827 if (NULL == (exports = default_exports(csp, NULL)))
829 return JB_ERR_MEMORY;
832 err = map_copy_parameter_html(exports, parameters, "f");
833 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
834 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
842 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
846 /*********************************************************************
848 * Function : cgi_edit_actions_remove_url_form
850 * Description : CGI function that displays a form for
854 * 1 : csp = Current client state (buffers, headers, etc...)
855 * 2 : rsp = http_response data structure for output
856 * 3 : parameters = map of cgi parameters
859 * f : (filename) Identifies the file to edit
860 * v : (version) File's last-modified time
861 * p : (pattern) Line number of pattern to edit
863 * Returns : JB_ERR_OK on success
864 * JB_ERR_MEMORY on out-of-memory
865 * JB_ERR_CGI_PARAMS if the CGI parameters are not
866 * specified or not valid.
868 *********************************************************************/
869 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
870 struct http_response *rsp,
871 const struct map *parameters)
873 struct map * exports;
875 struct editable_file * file;
876 struct file_line * cur_line;
877 unsigned line_number;
878 unsigned section_start_line_number = 0;
885 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
887 return cgi_error_disabled(csp, rsp);
890 err = get_number_param(csp, parameters, "p", &patternid);
896 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
899 /* No filename specified, can't read file, modified, or out of memory. */
900 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
903 cur_line = file->lines;
905 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
907 if (cur_line->type == FILE_LINE_ACTION)
909 section_start_line_number = line_number;
911 cur_line = cur_line->next;
914 if ( (cur_line == NULL)
915 || (line_number != patternid)
917 || (cur_line->type != FILE_LINE_URL))
919 /* Invalid "patternid" parameter */
920 edit_free_file(file);
921 return JB_ERR_CGI_PARAMS;
924 if (NULL == (exports = default_exports(csp, NULL)))
926 edit_free_file(file);
927 return JB_ERR_MEMORY;
930 err = map(exports, "f", 1, file->identifier, 1);
931 if (!err) err = map(exports, "v", 1, file->version_str, 1);
932 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
933 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
934 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
936 edit_free_file(file);
944 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
948 /*********************************************************************
950 * Function : edit_write_file
952 * Description : Write a complete file to disk.
955 * 1 : file = File to write.
957 * Returns : JB_ERR_OK on success
958 * JB_ERR_FILE on error writing to file.
959 * JB_ERR_MEMORY on out of memory
961 *********************************************************************/
962 jb_err edit_write_file(struct editable_file * file)
965 struct file_line * cur_line;
966 struct stat statbuf[1];
967 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
968 digits in time_t, assuming this is a 64-bit
969 machine, plus null terminator, plus one
973 assert(file->filename);
975 if (NULL == (fp = fopen(file->filename, "wb")))
980 cur_line = file->lines;
981 while (cur_line != NULL)
985 if (fputs(cur_line->raw, fp) < 0)
993 if (cur_line->prefix)
995 if (fputs(cur_line->prefix, fp) < 0)
1001 if (cur_line->unprocessed)
1004 if (NULL != strchr(cur_line->unprocessed, '#'))
1006 /* Must quote '#' characters */
1013 /* Count number of # characters, so we know length of output string */
1014 src = cur_line->unprocessed;
1015 while (NULL != (src = strchr(src, '#')))
1020 assert(numhash > 0);
1022 /* Allocate new memory for string */
1023 len = strlen(cur_line->unprocessed) + (size_t)numhash;
1024 if (NULL == (str = malloc(len + 1)))
1026 /* Uh oh, just trashed file! */
1028 return JB_ERR_MEMORY;
1031 /* Copy string but quote hashes */
1032 src = cur_line->unprocessed;
1040 assert(numhash >= 0);
1046 assert(numhash == 0);
1047 assert(strlen(str) == len);
1048 assert(str == dest - len);
1049 assert(src - len <= cur_line->unprocessed);
1051 if ((strlen(str) != len) || (numhash != 0))
1054 * Escaping didn't work as expected, go spread the news.
1055 * Only reached in non-debugging builds.
1057 log_error(LOG_LEVEL_ERROR,
1058 "Looks like hash escaping failed. %s might be corrupted now.",
1062 if (fputs(str, fp) < 0)
1073 /* Can write without quoting '#' characters. */
1074 if (fputs(cur_line->unprocessed, fp) < 0)
1080 if (fputs(NEWLINE(file->newline), fp) < 0)
1088 /* FIXME: Write data from file->data->whatever */
1092 cur_line = cur_line->next;
1098 /* Update the version stamp in the file structure, since we just
1099 * wrote to the file & changed it's date.
1101 if (stat(file->filename, statbuf) < 0)
1103 /* Error, probably file not found. */
1106 file->version = (unsigned)statbuf->st_mtime;
1108 /* Correct file->version_str */
1109 freez(file->version_str);
1110 snprintf(version_buf, 22, "%u", file->version);
1111 version_buf[21] = '\0';
1112 file->version_str = strdup(version_buf);
1113 if (version_buf == NULL)
1115 return JB_ERR_MEMORY;
1122 /*********************************************************************
1124 * Function : edit_free_file
1126 * Description : Free a complete file in memory.
1129 * 1 : file = Data structure to free.
1133 *********************************************************************/
1134 void edit_free_file(struct editable_file * file)
1138 /* Silently ignore NULL pointer */
1142 edit_free_file_lines(file->lines);
1143 freez(file->filename);
1144 freez(file->identifier);
1145 freez(file->version_str);
1147 file->parse_error_text = NULL; /* Statically allocated */
1148 file->parse_error = NULL;
1154 /*********************************************************************
1156 * Function : edit_free_file_lines
1158 * Description : Free an entire linked list of file lines.
1161 * 1 : first_line = Data structure to free.
1165 *********************************************************************/
1166 static void edit_free_file_lines(struct file_line * first_line)
1168 struct file_line * next_line;
1170 while (first_line != NULL)
1172 next_line = first_line->next;
1173 first_line->next = NULL;
1174 freez(first_line->raw);
1175 freez(first_line->prefix);
1176 freez(first_line->unprocessed);
1177 switch(first_line->type)
1179 case 0: /* special case if memory zeroed */
1180 case FILE_LINE_UNPROCESSED:
1181 case FILE_LINE_BLANK:
1182 case FILE_LINE_ALIAS_HEADER:
1183 case FILE_LINE_SETTINGS_HEADER:
1184 case FILE_LINE_DESCRIPTION_HEADER:
1185 case FILE_LINE_DESCRIPTION_ENTRY:
1186 case FILE_LINE_ALIAS_ENTRY:
1188 /* No data is stored for these */
1191 case FILE_LINE_ACTION:
1192 free_action(first_line->data.action);
1195 case FILE_LINE_SETTINGS_ENTRY:
1196 freez(first_line->data.setting.name);
1197 freez(first_line->data.setting.svalue);
1200 /* Should never happen */
1204 first_line->type = 0; /* paranoia */
1206 first_line = next_line;
1211 /*********************************************************************
1213 * Function : match_actions_file_header_line
1215 * Description : Match an actions file {{header}} line
1218 * 1 : line = String from file
1219 * 2 : name = Header to match against
1221 * Returns : 0 iff they match.
1223 *********************************************************************/
1224 static int match_actions_file_header_line(const char * line, const char * name)
1232 if ((line[0] != '{') || (line[1] != '{'))
1238 /* Look for optional whitespace */
1239 while ( (*line == ' ') || (*line == '\t') )
1244 /* Look for the specified name (case-insensitive) */
1246 if (0 != strncmpic(line, name, len))
1252 /* Look for optional whitespace */
1253 while ( (*line == ' ') || (*line == '\t') )
1258 /* Look for "}}" and end of string*/
1259 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1269 /*********************************************************************
1271 * Function : match_actions_file_header_line
1273 * Description : Match an actions file {{header}} line
1276 * 1 : line = String from file. Must not start with
1277 * whitespace (else infinite loop!)
1278 * 2 : pname = Destination for name
1279 * 2 : pvalue = Destination for value
1281 * Returns : JB_ERR_OK on success
1282 * JB_ERR_MEMORY on out-of-memory
1283 * JB_ERR_PARSE if there's no "=" sign, or if there's
1284 * nothing before the "=" sign (but empty
1285 * values *after* the "=" sign are legal).
1287 *********************************************************************/
1288 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1290 const char * name_end;
1291 const char * value_start;
1297 assert(*line != ' ');
1298 assert(*line != '\t');
1303 value_start = strchr(line, '=');
1304 if ((value_start == NULL) || (value_start == line))
1306 return JB_ERR_PARSE;
1309 name_end = value_start - 1;
1311 /* Eat any whitespace before the '=' */
1312 while ((*name_end == ' ') || (*name_end == '\t'))
1315 * we already know we must have at least 1 non-ws char
1316 * at start of buf - no need to check
1321 name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
1322 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1324 return JB_ERR_MEMORY;
1326 strncpy(*pname, line, name_len);
1327 (*pname)[name_len] = '\0';
1329 /* Eat any the whitespace after the '=' */
1331 while ((*value_start == ' ') || (*value_start == '\t'))
1336 if (NULL == (*pvalue = strdup(value_start)))
1340 return JB_ERR_MEMORY;
1347 /*********************************************************************
1349 * Function : edit_parse_actions_file
1351 * Description : Parse an actions file in memory.
1353 * Passed linked list must have the "data" member
1354 * zeroed, and must contain valid "next" and
1355 * "unprocessed" fields. The "raw" and "prefix"
1356 * fields are ignored, and "type" is just overwritten.
1358 * Note that on error the file may have been
1362 * 1 : file = Actions file to be parsed in-place.
1364 * Returns : JB_ERR_OK on success
1365 * JB_ERR_MEMORY on out-of-memory
1366 * JB_ERR_PARSE on error
1368 *********************************************************************/
1369 jb_err edit_parse_actions_file(struct editable_file * file)
1371 struct file_line * cur_line;
1373 const char * text; /* Text from a line */
1374 char * name; /* For lines of the form name=value */
1375 char * value; /* For lines of the form name=value */
1376 struct action_alias * alias_list = NULL;
1377 jb_err err = JB_ERR_OK;
1379 /* alias_list contains the aliases defined in this file.
1380 * It might be better to use the "file_line.data" fields
1381 * in the relavent places instead.
1384 cur_line = file->lines;
1386 /* A note about blank line support: Blank lines should only
1387 * ever occur as the last line in the file. This function
1388 * is more forgiving than that - FILE_LINE_BLANK can occur
1392 /* Skip leading blanks. Should only happen if file is
1393 * empty (which is valid, but pointless).
1395 while ( (cur_line != NULL)
1396 && (cur_line->unprocessed[0] == '\0') )
1399 cur_line->type = FILE_LINE_BLANK;
1400 cur_line = cur_line->next;
1403 if ( (cur_line != NULL)
1404 && (cur_line->unprocessed[0] != '{') )
1406 /* File doesn't start with a header */
1407 file->parse_error = cur_line;
1408 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1409 return JB_ERR_PARSE;
1412 if ( (cur_line != NULL) && (0 ==
1413 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1415 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1417 cur_line = cur_line->next;
1418 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1420 if (cur_line->unprocessed[0])
1422 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1424 err = split_line_on_equals(cur_line->unprocessed,
1425 &cur_line->data.setting.name,
1426 &cur_line->data.setting.svalue);
1427 if (err == JB_ERR_MEMORY)
1431 else if (err != JB_ERR_OK)
1433 /* Line does not contain a name=value pair */
1434 file->parse_error = cur_line;
1435 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1436 return JB_ERR_PARSE;
1441 cur_line->type = FILE_LINE_BLANK;
1443 cur_line = cur_line->next;
1447 if ( (cur_line != NULL) && (0 ==
1448 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1450 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1452 cur_line = cur_line->next;
1453 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1455 if (cur_line->unprocessed[0])
1457 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1461 cur_line->type = FILE_LINE_BLANK;
1463 cur_line = cur_line->next;
1467 if ( (cur_line != NULL) && (0 ==
1468 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1470 cur_line->type = FILE_LINE_ALIAS_HEADER;
1472 cur_line = cur_line->next;
1473 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1475 if (cur_line->unprocessed[0])
1477 /* define an alias */
1478 struct action_alias * new_alias;
1480 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1482 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1483 if (err == JB_ERR_MEMORY)
1487 else if (err != JB_ERR_OK)
1489 /* Line does not contain a name=value pair */
1490 file->parse_error = cur_line;
1491 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1492 return JB_ERR_PARSE;
1495 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1500 free_alias_list(alias_list);
1501 return JB_ERR_MEMORY;
1504 err = get_actions(value, alias_list, new_alias->action);
1507 /* Invalid action or out of memory */
1511 free_alias_list(alias_list);
1512 if (err == JB_ERR_MEMORY)
1518 /* Line does not contain a name=value pair */
1519 file->parse_error = cur_line;
1520 file->parse_error_text = "This alias does not specify a valid set of actions.";
1521 return JB_ERR_PARSE;
1527 new_alias->name = name;
1530 new_alias->next = alias_list;
1531 alias_list = new_alias;
1535 cur_line->type = FILE_LINE_BLANK;
1537 cur_line = cur_line->next;
1541 /* Header done, process the main part of the file */
1542 while (cur_line != NULL)
1544 /* At this point, (cur_line->unprocessed[0] == '{') */
1545 assert(cur_line->unprocessed[0] == '{');
1546 text = cur_line->unprocessed + 1;
1547 len = strlen(text) - 1;
1548 if (text[len] != '}')
1550 /* No closing } on header */
1551 free_alias_list(alias_list);
1552 file->parse_error = cur_line;
1553 file->parse_error_text = "Headers starting with '{' must have a "
1554 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1555 "must close with two brackets ('}}').";
1556 return JB_ERR_PARSE;
1561 /* An invalid {{ header. */
1562 free_alias_list(alias_list);
1563 file->parse_error = cur_line;
1564 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1565 "Please remember that the system (two-bracket) headers must "
1566 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1567 "and must appear before any actions (one-bracket) headers. "
1568 "Also note that system headers may not be repeated.";
1569 return JB_ERR_PARSE;
1572 while ( (*text == ' ') || (*text == '\t') )
1578 && ( (text[len - 1] == ' ')
1579 || (text[len - 1] == '\t') ) )
1584 cur_line->type = FILE_LINE_ACTION;
1586 /* Remove {} and make copy */
1587 if (NULL == (value = (char *) malloc(len + 1)))
1590 free_alias_list(alias_list);
1591 return JB_ERR_MEMORY;
1593 strncpy(value, text, len);
1597 err = get_actions(value, alias_list, cur_line->data.action);
1600 /* Invalid action or out of memory */
1602 free_alias_list(alias_list);
1603 if (err == JB_ERR_MEMORY)
1609 /* Line does not contain a name=value pair */
1610 file->parse_error = cur_line;
1611 file->parse_error_text = "This header does not specify a valid set of actions.";
1612 return JB_ERR_PARSE;
1616 /* Done with string - it was clobbered anyway */
1619 /* Process next line */
1620 cur_line = cur_line->next;
1622 /* Loop processing URL patterns */
1623 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1625 if (cur_line->unprocessed[0])
1627 /* Could parse URL here, but this isn't currently needed */
1629 cur_line->type = FILE_LINE_URL;
1633 cur_line->type = FILE_LINE_BLANK;
1635 cur_line = cur_line->next;
1637 } /* End main while(cur_line != NULL) loop */
1639 free_alias_list(alias_list);
1645 /*********************************************************************
1647 * Function : edit_read_file_lines
1649 * Description : Read all the lines of a file into memory.
1650 * Handles whitespace, comments and line continuation.
1653 * 1 : fp = File to read from. On return, this will be
1654 * at EOF but it will not have been closed.
1655 * 2 : pfile = Destination for a linked list of file_lines.
1656 * Will be set to NULL on error.
1657 * 3 : newline = How to handle newlines.
1659 * Returns : JB_ERR_OK on success
1660 * JB_ERR_MEMORY on out-of-memory
1662 *********************************************************************/
1663 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1665 struct file_line * first_line; /* Keep for return value or to free */
1666 struct file_line * cur_line; /* Current line */
1667 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1675 cur_line = first_line = zalloc(sizeof(struct file_line));
1676 if (cur_line == NULL)
1678 return JB_ERR_MEMORY;
1681 cur_line->type = FILE_LINE_UNPROCESSED;
1683 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1686 /* Out of memory or empty file. */
1687 /* Note that empty file is not an error we propogate up */
1689 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1694 prev_line = cur_line;
1695 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1696 if (cur_line == NULL)
1699 edit_free_file_lines(first_line);
1700 return JB_ERR_MEMORY;
1703 cur_line->type = FILE_LINE_UNPROCESSED;
1705 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1706 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1709 edit_free_file_lines(first_line);
1710 return JB_ERR_MEMORY;
1714 while (rval != JB_ERR_FILE);
1718 /* We allocated one too many - free it */
1719 prev_line->next = NULL;
1722 *pfile = first_line;
1727 /*********************************************************************
1729 * Function : edit_read_file
1731 * Description : Read a complete file into memory.
1732 * Handles CGI parameter parsing. If requested, also
1733 * checks the file's modification timestamp.
1736 * 1 : csp = Current client state (buffers, headers, etc...)
1737 * 2 : parameters = map of cgi parameters.
1738 * 3 : require_version = true to check "ver" parameter.
1739 * 4 : suffix = File extension, e.g. ".action".
1740 * 5 : pfile = Destination for the file. Will be set
1744 * filename : The name of the file to read, without the
1745 * path or ".action" extension.
1746 * ver : (Only if require_version is nonzero)
1747 * Timestamp of the actions file. If wrong, this
1748 * function fails with JB_ERR_MODIFIED.
1750 * Returns : JB_ERR_OK on success
1751 * JB_ERR_MEMORY on out-of-memory
1752 * JB_ERR_CGI_PARAMS if "filename" was not specified
1754 * JB_ERR_FILE if the file cannot be opened or
1756 * JB_ERR_MODIFIED if version checking was requested and
1757 * failed - the file was modified outside
1758 * of this CGI editor instance.
1760 *********************************************************************/
1761 jb_err edit_read_file(struct client_state *csp,
1762 const struct map *parameters,
1763 int require_version,
1765 struct editable_file **pfile)
1767 struct file_line * lines;
1771 const char * identifier;
1772 struct editable_file * file;
1773 unsigned version = 0;
1774 struct stat statbuf[1];
1775 char version_buf[22];
1776 int newline = NEWLINE_UNKNOWN;
1784 err = get_file_name_param(csp, parameters, "f", suffix,
1785 &filename, &identifier);
1791 if (stat(filename, statbuf) < 0)
1793 /* Error, probably file not found. */
1797 version = (unsigned) statbuf->st_mtime;
1799 if (require_version)
1801 unsigned specified_version;
1802 err = get_number_param(csp, parameters, "v", &specified_version);
1809 if (version != specified_version)
1811 return JB_ERR_MODIFIED;
1815 if (NULL == (fp = fopen(filename,"rb")))
1821 err = edit_read_file_lines(fp, &lines, &newline);
1831 file = (struct editable_file *) zalloc(sizeof(*file));
1835 edit_free_file_lines(lines);
1839 file->lines = lines;
1840 file->newline = newline;
1841 file->filename = filename;
1842 file->version = version;
1843 file->identifier = url_encode(identifier);
1845 if (file->identifier == NULL)
1847 edit_free_file(file);
1848 return JB_ERR_MEMORY;
1851 /* Correct file->version_str */
1852 freez(file->version_str);
1853 snprintf(version_buf, 22, "%u", file->version);
1854 version_buf[21] = '\0';
1855 file->version_str = strdup(version_buf);
1856 if (version_buf == NULL)
1858 edit_free_file(file);
1859 return JB_ERR_MEMORY;
1867 /*********************************************************************
1869 * Function : edit_read_actions_file
1871 * Description : Read a complete actions file into memory.
1872 * Handles CGI parameter parsing. If requested, also
1873 * checks the file's modification timestamp.
1875 * If this function detects an error in the categories
1876 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1877 * then it handles it by filling in the specified
1878 * response structure and returning JB_ERR_FILE.
1881 * 1 : csp = Current client state (buffers, headers, etc...)
1882 * 2 : rsp = HTTP response. Only filled in on error.
1883 * 2 : parameters = map of cgi parameters.
1884 * 3 : require_version = true to check "ver" parameter.
1885 * 4 : pfile = Destination for the file. Will be set
1889 * filename : The name of the actions file to read, without the
1890 * path or ".action" extension.
1891 * ver : (Only if require_version is nonzero)
1892 * Timestamp of the actions file. If wrong, this
1893 * function fails with JB_ERR_MODIFIED.
1895 * Returns : JB_ERR_OK on success
1896 * JB_ERR_MEMORY on out-of-memory
1897 * JB_ERR_CGI_PARAMS if "filename" was not specified
1899 * JB_ERR_FILE if the file does not contain valid data,
1900 * or if file cannot be opened or
1901 * contains no data, or if version
1902 * checking was requested and failed.
1904 *********************************************************************/
1905 jb_err edit_read_actions_file(struct client_state *csp,
1906 struct http_response *rsp,
1907 const struct map *parameters,
1908 int require_version,
1909 struct editable_file **pfile)
1912 struct editable_file *file;
1920 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1923 /* Try to handle if possible */
1924 if (err == JB_ERR_FILE)
1926 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1928 else if (err == JB_ERR_MODIFIED)
1930 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1932 if (err == JB_ERR_OK)
1935 * Signal to higher-level CGI code that there was a problem but we
1936 * handled it, they should just return JB_ERR_OK.
1943 err = edit_parse_actions_file(file);
1946 if (err == JB_ERR_PARSE)
1948 err = cgi_error_parse(csp, rsp, file);
1949 if (err == JB_ERR_OK)
1952 * Signal to higher-level CGI code that there was a problem but we
1953 * handled it, they should just return JB_ERR_OK.
1958 edit_free_file(file);
1967 /*********************************************************************
1969 * Function : get_file_name_param
1971 * Description : Get the name of the file to edit from the parameters
1972 * passed to a CGI function. This function handles
1973 * security checks such as blocking urls containing
1974 * "/" or ".", prepending the config file directory,
1975 * and adding the specified suffix.
1977 * (This is an essential security check, otherwise
1978 * users may be able to pass "../../../etc/passwd"
1979 * and overwrite the password file [linux], "prn:"
1980 * and print random data [Windows], etc...)
1982 * This function only allows filenames contining the
1983 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1984 * That's probably too restrictive but at least it's
1988 * 1 : csp = Current client state (buffers, headers, etc...)
1989 * 2 : parameters = map of cgi parameters
1990 * 3 : param_name = The name of the parameter to read
1991 * 4 : suffix = File extension, e.g. ".actions"
1992 * 5 : pfilename = destination for full filename. Caller
1993 * free()s. Set to NULL on error.
1994 * 6 : pparam = destination for partial filename,
1995 * suitable for use in another URL. Allocated as part
1996 * of the map "parameters", so don't free it.
1997 * Set to NULL if not specified.
1999 * Returns : JB_ERR_OK on success
2000 * JB_ERR_MEMORY on out-of-memory
2001 * JB_ERR_CGI_PARAMS if "filename" was not specified
2004 *********************************************************************/
2005 static jb_err get_file_name_param(struct client_state *csp,
2006 const struct map *parameters,
2007 const char *param_name,
2010 const char **pparam)
2028 param = lookup(parameters, param_name);
2031 return JB_ERR_CGI_PARAMS;
2036 len = strlen(param);
2037 if (len >= FILENAME_MAX)
2040 return JB_ERR_CGI_PARAMS;
2043 /* Check every character to see if it's legal */
2045 while ((ch = *s++) != '\0')
2047 if ( ((ch < 'A') || (ch > 'Z'))
2048 && ((ch < 'a') || (ch > 'z'))
2049 && ((ch < '0') || (ch > '9'))
2053 /* Probable hack attempt. */
2054 return JB_ERR_CGI_PARAMS;
2058 /* Append extension */
2059 name = malloc(len + strlen(suffix) + 1);
2062 return JB_ERR_MEMORY;
2064 strcpy(name, param);
2065 strcpy(name + len, suffix);
2068 fullpath = make_path(csp->config->confdir, name);
2071 if (fullpath == NULL)
2073 return JB_ERR_MEMORY;
2077 *pfilename = fullpath;
2083 /*********************************************************************
2085 * Function : get_url_spec_param
2087 * Description : Get a URL pattern from the parameters
2088 * passed to a CGI function. Removes leading/trailing
2089 * spaces and validates it.
2092 * 1 : csp = Current client state (buffers, headers, etc...)
2093 * 2 : parameters = map of cgi parameters
2094 * 3 : name = Name of CGI parameter to read
2095 * 4 : pvalue = destination for value. Will be malloc()'d.
2096 * Set to NULL on error.
2098 * Returns : JB_ERR_OK on success
2099 * JB_ERR_MEMORY on out-of-memory
2100 * JB_ERR_CGI_PARAMS if the parameter was not specified
2103 *********************************************************************/
2104 static jb_err get_url_spec_param(struct client_state *csp,
2105 const struct map *parameters,
2109 const char *orig_param;
2112 struct url_spec compiled[1];
2122 orig_param = lookup(parameters, name);
2125 return JB_ERR_CGI_PARAMS;
2128 /* Copy and trim whitespace */
2129 param = strdup(orig_param);
2132 return JB_ERR_MEMORY;
2136 /* Must be non-empty, and can't allow 1st character to be '{' */
2137 if (param[0] == '\0' || param[0] == '{')
2140 return JB_ERR_CGI_PARAMS;
2143 /* Check for embedded newlines */
2144 for (s = param; *s != '\0'; s++)
2146 if ((*s == '\r') || (*s == '\n'))
2149 return JB_ERR_CGI_PARAMS;
2153 /* Check that regex is valid */
2158 return JB_ERR_MEMORY;
2160 err = create_url_spec(compiled, s);
2165 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2167 free_url_spec(compiled);
2169 if (param[strlen(param) - 1] == '\\')
2172 * Must protect trailing '\\' from becoming line continuation character.
2173 * Two methods: 1) If it's a domain only, add a trailing '/'.
2174 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2176 if (strchr(param, '/') == NULL)
2178 err = string_append(¶m, "/");
2182 err = string_append(¶m, "(?:)");
2189 /* Check that the modified regex is valid */
2194 return JB_ERR_MEMORY;
2196 err = create_url_spec(compiled, s);
2201 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2203 free_url_spec(compiled);
2210 /*********************************************************************
2212 * Function : map_radio
2214 * Description : Map a set of radio button values. E.g. if you have
2215 * 3 radio buttons, declare them as:
2216 * <option type="radio" name="xyz" @xyz-a@>
2217 * <option type="radio" name="xyz" @xyz-b@>
2218 * <option type="radio" name="xyz" @xyz-c@>
2219 * Then map one of the @xyz-?@ variables to "checked"
2220 * and all the others to empty by calling:
2221 * map_radio(exports, "xyz", "abc", sel)
2222 * Where 'sel' is 'a', 'b', or 'c'.
2225 * 1 : exports = Exports map to modify.
2226 * 2 : optionname = name for map
2227 * 3 : values = null-terminated list of values;
2228 * 4 : value = Selected value.
2230 * CGI Parameters : None
2232 * Returns : JB_ERR_OK on success
2233 * JB_ERR_MEMORY on out-of-memory
2235 *********************************************************************/
2236 static jb_err map_radio(struct map * exports,
2237 const char * optionname,
2238 const char * values,
2250 len = strlen(optionname);
2251 buf = malloc(len + 3);
2254 return JB_ERR_MEMORY;
2257 strcpy(buf, optionname);
2262 while ((c = *values++) != '\0')
2267 if (map(exports, buf, 1, "", 1))
2269 return JB_ERR_MEMORY;
2275 return map(exports, buf, 0, "checked", 1);
2279 /*********************************************************************
2281 * Function : cgi_error_modified
2283 * Description : CGI function that is called when a file is modified
2284 * outside the CGI editor.
2287 * 1 : csp = Current client state (buffers, headers, etc...)
2288 * 2 : rsp = http_response data structure for output
2289 * 3 : filename = The file that was modified.
2291 * CGI Parameters : none
2293 * Returns : JB_ERR_OK on success
2294 * JB_ERR_MEMORY on out-of-memory error.
2296 *********************************************************************/
2297 jb_err cgi_error_modified(struct client_state *csp,
2298 struct http_response *rsp,
2299 const char *filename)
2301 struct map *exports;
2308 if (NULL == (exports = default_exports(csp, NULL)))
2310 return JB_ERR_MEMORY;
2313 err = map(exports, "f", 1, html_encode(filename), 0);
2320 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2324 /*********************************************************************
2326 * Function : cgi_error_parse
2328 * Description : CGI function that is called when a file cannot
2329 * be parsed by the CGI editor.
2332 * 1 : csp = Current client state (buffers, headers, etc...)
2333 * 2 : rsp = http_response data structure for output
2334 * 3 : file = The file that was modified.
2336 * CGI Parameters : none
2338 * Returns : JB_ERR_OK on success
2339 * JB_ERR_MEMORY on out-of-memory error.
2341 *********************************************************************/
2342 jb_err cgi_error_parse(struct client_state *csp,
2343 struct http_response *rsp,
2344 struct editable_file *file)
2346 struct map *exports;
2348 struct file_line *cur_line;
2354 if (NULL == (exports = default_exports(csp, NULL)))
2356 return JB_ERR_MEMORY;
2359 err = map(exports, "f", 1, file->identifier, 1);
2360 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2362 cur_line = file->parse_error;
2365 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2366 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2374 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2378 /*********************************************************************
2380 * Function : cgi_error_file
2382 * Description : CGI function that is called when a file cannot be
2383 * opened by the CGI editor.
2386 * 1 : csp = Current client state (buffers, headers, etc...)
2387 * 2 : rsp = http_response data structure for output
2388 * 3 : filename = The file that was modified.
2390 * CGI Parameters : none
2392 * Returns : JB_ERR_OK on success
2393 * JB_ERR_MEMORY on out-of-memory error.
2395 *********************************************************************/
2396 jb_err cgi_error_file(struct client_state *csp,
2397 struct http_response *rsp,
2398 const char *filename)
2400 struct map *exports;
2407 if (NULL == (exports = default_exports(csp, NULL)))
2409 return JB_ERR_MEMORY;
2412 err = map(exports, "f", 1, html_encode(filename), 0);
2419 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2423 /*********************************************************************
2425 * Function : cgi_error_file
2427 * Description : CGI function that is called when a file cannot be
2428 * opened for writing by the CGI editor.
2431 * 1 : csp = Current client state (buffers, headers, etc...)
2432 * 2 : rsp = http_response data structure for output
2433 * 3 : filename = The file that we can't write to
2435 * CGI Parameters : none
2437 * Returns : JB_ERR_OK on success
2438 * JB_ERR_MEMORY on out-of-memory error.
2440 *********************************************************************/
2441 jb_err cgi_error_file_read_only(struct client_state *csp,
2442 struct http_response *rsp,
2443 const char *filename)
2445 struct map *exports;
2452 if (NULL == (exports = default_exports(csp, NULL)))
2454 return JB_ERR_MEMORY;
2457 err = map(exports, "f", 1, html_encode(filename), 0);
2464 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2468 /*********************************************************************
2470 * Function : cgi_edit_actions
2472 * Description : CGI function that allows the user to choose which
2473 * actions file to edit.
2476 * 1 : csp = Current client state (buffers, headers, etc...)
2477 * 2 : rsp = http_response data structure for output
2478 * 3 : parameters = map of cgi parameters
2480 * CGI Parameters : None
2482 * Returns : JB_ERR_OK on success
2483 * JB_ERR_MEMORY on out-of-memory error
2485 *********************************************************************/
2486 jb_err cgi_edit_actions(struct client_state *csp,
2487 struct http_response *rsp,
2488 const struct map *parameters)
2491 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2493 return cgi_error_disabled(csp, rsp);
2496 /* FIXME: Incomplete */
2498 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2503 /*********************************************************************
2505 * Function : cgi_edit_actions_list
2507 * Description : CGI function that edits the actions list.
2508 * FIXME: This function shouldn't FATAL ever.
2509 * FIXME: This function doesn't check the retval of map()
2511 * 1 : csp = Current client state (buffers, headers, etc...)
2512 * 2 : rsp = http_response data structure for output
2513 * 3 : parameters = map of cgi parameters
2515 * CGI Parameters : filename
2517 * Returns : JB_ERR_OK on success
2518 * JB_ERR_MEMORY on out-of-memory
2519 * JB_ERR_FILE if the file cannot be opened or
2521 * JB_ERR_CGI_PARAMS if "filename" was not specified
2524 *********************************************************************/
2525 jb_err cgi_edit_actions_list(struct client_state *csp,
2526 struct http_response *rsp,
2527 const struct map *parameters)
2529 char * section_template;
2530 char * url_template;
2535 struct map * exports;
2536 struct map * section_exports;
2537 struct map * url_exports;
2538 struct editable_file * file;
2539 struct file_line * cur_line;
2540 unsigned line_number = 0;
2541 unsigned prev_section_line_number = ((unsigned) (-1));
2543 struct file_list * fl;
2544 struct url_actions * b;
2545 char * buttons = NULL;
2548 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2550 return cgi_error_disabled(csp, rsp);
2553 if (NULL == (exports = default_exports(csp, NULL)))
2555 return JB_ERR_MEMORY;
2558 /* Load actions file */
2559 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2562 /* No filename specified, can't read file, or out of memory. */
2563 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2566 /* Find start of actions in file */
2567 cur_line = file->lines;
2569 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2571 cur_line = cur_line->next;
2576 * Conventional actions files should have a match all block
2578 * cur_line = {...global actions...}
2579 * cur_line->next = /
2580 * cur_line->next->next = {...actions...} or EOF
2582 if ( (cur_line != NULL)
2583 && (cur_line->type == FILE_LINE_ACTION)
2584 && (cur_line->next != NULL)
2585 && (cur_line->next->type == FILE_LINE_URL)
2586 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2587 && ( (cur_line->next->next == NULL)
2588 || (cur_line->next->next->type != FILE_LINE_URL)
2592 * Generate string with buttons to set actions for "/" to
2593 * any predefined set of actions (named standard.*, probably
2594 * residing in standard.action).
2597 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2600 edit_free_file(file);
2602 if (err == JB_ERR_FILE)
2604 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2609 err = template_fill(§ion_template, exports);
2612 edit_free_file(file);
2617 buttons = strdup("");
2618 for (i = 0; i < MAX_AF_FILES; i++)
2620 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2622 for (b = b->next; NULL != b; b = b->next)
2624 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2626 if (err || (NULL == (section_exports = new_map())))
2629 free(section_template);
2630 edit_free_file(file);
2632 return JB_ERR_MEMORY;
2635 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2637 if (err || (NULL == (s = strdup(section_template))))
2639 free_map(section_exports);
2641 free(section_template);
2642 edit_free_file(file);
2644 return JB_ERR_MEMORY;
2647 if (!err) err = template_fill(&s, section_exports);
2648 free_map(section_exports);
2649 if (!err) err = string_join(&buttons, s);
2654 freez(section_template);
2655 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2658 * Conventional actions file, supply extra editing help.
2659 * (e.g. don't allow them to make it an unconventional one).
2661 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2663 snprintf(buf, 150, "%d", line_number);
2664 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2665 snprintf(buf, 150, "%d", line_number + 2);
2666 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2667 if (!err) err = map(exports, "all-urls-actions", 1,
2668 actions_to_html(csp, cur_line->data.action), 0);
2670 /* Skip the 2 lines */
2671 cur_line = cur_line->next->next;
2675 * Note that prev_section_line_number is NOT set here.
2676 * This is deliberate and not a bug. It stops a "Move up"
2677 * option appearing on the next section. Clicking "Move
2678 * up" would make the actions file unconventional, which
2679 * we don't want, so we hide this option.
2685 * Non-standard actions file - does not begin with
2686 * the "All URLs" section.
2688 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2691 /* Set up global exports */
2693 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2694 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2696 /* Discourage private additions to default.action */
2698 if (!err) err = map_conditional(exports, "default-action",
2699 (strcmp("default", lookup(parameters, "f")) == 0));
2702 edit_free_file(file);
2707 /* Should do all global exports above this point */
2709 /* Load templates */
2711 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2714 edit_free_file(file);
2716 if (err == JB_ERR_FILE)
2718 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2723 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2726 free(section_template);
2727 edit_free_file(file);
2729 if (err == JB_ERR_FILE)
2731 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2736 err = template_fill(§ion_template, exports);
2740 edit_free_file(file);
2746 err = template_fill(&url_template, exports);
2749 free(section_template);
2750 edit_free_file(file);
2755 if (NULL == (sections = strdup("")))
2757 free(section_template);
2759 edit_free_file(file);
2761 return JB_ERR_MEMORY;
2764 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2766 if (NULL == (section_exports = new_map()))
2769 free(section_template);
2771 edit_free_file(file);
2773 return JB_ERR_MEMORY;
2776 snprintf(buf, 150, "%d", line_number);
2777 err = map(section_exports, "s", 1, buf, 1);
2778 if (!err) err = map(section_exports, "actions", 1,
2779 actions_to_html(csp, cur_line->data.action), 0);
2782 && (cur_line->next != NULL)
2783 && (cur_line->next->type == FILE_LINE_URL))
2785 /* This section contains at least one URL, don't allow delete */
2786 err = map_block_killer(section_exports, "empty-section");
2790 if (!err) err = map_block_keep(section_exports, "empty-section");
2793 if (prev_section_line_number != ((unsigned)(-1)))
2795 /* Not last section */
2796 snprintf(buf, 150, "%d", prev_section_line_number);
2797 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2798 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2803 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2805 prev_section_line_number = line_number;
2810 free(section_template);
2812 edit_free_file(file);
2814 free_map(section_exports);
2818 /* Should do all section-specific exports above this point */
2820 if (NULL == (urls = strdup("")))
2823 free(section_template);
2825 edit_free_file(file);
2827 free_map(section_exports);
2828 return JB_ERR_MEMORY;
2833 cur_line = cur_line->next;
2836 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2838 if (NULL == (url_exports = new_map()))
2842 free(section_template);
2844 edit_free_file(file);
2846 free_map(section_exports);
2847 return JB_ERR_MEMORY;
2850 snprintf(buf, 150, "%d", line_number);
2851 err = map(url_exports, "p", 1, buf, 1);
2853 snprintf(buf, 150, "%d", url_1_2);
2854 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2856 if (!err) err = map(url_exports, "url-html", 1,
2857 html_encode(cur_line->unprocessed), 0);
2858 if (!err) err = map(url_exports, "url", 1,
2859 url_encode(cur_line->unprocessed), 0);
2865 free(section_template);
2867 edit_free_file(file);
2869 free_map(section_exports);
2870 free_map(url_exports);
2874 if (NULL == (s = strdup(url_template)))
2878 free(section_template);
2880 edit_free_file(file);
2882 free_map(section_exports);
2883 free_map(url_exports);
2884 return JB_ERR_MEMORY;
2887 err = template_fill(&s, section_exports);
2888 if (!err) err = template_fill(&s, url_exports);
2889 if (!err) err = string_append(&urls, s);
2891 free_map(url_exports);
2898 free(section_template);
2900 edit_free_file(file);
2902 free_map(section_exports);
2906 url_1_2 = 3 - url_1_2;
2908 cur_line = cur_line->next;
2912 err = map(section_exports, "urls", 1, urls, 0);
2914 /* Could also do section-specific exports here, but it wouldn't be as fast */
2916 snprintf(buf, 150, "%d", line_number);
2917 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2919 if ( (cur_line != NULL)
2920 && (cur_line->type == FILE_LINE_ACTION))
2922 /* Not last section */
2923 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2928 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2934 free(section_template);
2936 edit_free_file(file);
2938 free_map(section_exports);
2942 if (NULL == (s = strdup(section_template)))
2945 free(section_template);
2947 edit_free_file(file);
2949 free_map(section_exports);
2950 return JB_ERR_MEMORY;
2953 err = template_fill(&s, section_exports);
2954 if (!err) err = string_append(§ions, s);
2957 free_map(section_exports);
2962 free(section_template);
2964 edit_free_file(file);
2970 edit_free_file(file);
2971 free(section_template);
2974 err = map(exports, "sections", 1, sections, 0);
2981 /* Could also do global exports here, but it wouldn't be as fast */
2983 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2987 /*********************************************************************
2989 * Function : cgi_edit_actions_for_url
2991 * Description : CGI function that edits the Actions list.
2994 * 1 : csp = Current client state (buffers, headers, etc...)
2995 * 2 : rsp = http_response data structure for output
2996 * 3 : parameters = map of cgi parameters
2998 * CGI Parameters : None
3000 * Returns : JB_ERR_OK on success
3001 * JB_ERR_MEMORY on out-of-memory
3002 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3003 * specified or not valid.
3005 *********************************************************************/
3006 jb_err cgi_edit_actions_for_url(struct client_state *csp,
3007 struct http_response *rsp,
3008 const struct map *parameters)
3010 struct map * exports;
3012 struct editable_file * file;
3013 struct file_line * cur_line;
3014 unsigned line_number;
3016 struct re_filterfile_spec *filter_group;
3017 int i, have_filters = 0;
3019 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3021 return cgi_error_disabled(csp, rsp);
3024 err = get_number_param(csp, parameters, "s", §ionid);
3030 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3033 /* No filename specified, can't read file, modified, or out of memory. */
3034 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3037 cur_line = file->lines;
3039 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3041 cur_line = cur_line->next;
3044 if ( (cur_line == NULL)
3045 || (line_number != sectionid)
3047 || (cur_line->type != FILE_LINE_ACTION))
3049 /* Invalid "sectionid" parameter */
3050 edit_free_file(file);
3051 return JB_ERR_CGI_PARAMS;
3054 if (NULL == (exports = default_exports(csp, NULL)))
3056 edit_free_file(file);
3057 return JB_ERR_MEMORY;
3060 err = map(exports, "f", 1, file->identifier, 1);
3061 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3062 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3064 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3067 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3068 * length limitation and ignore clicks on the Submit buttons if
3069 * the resulting GET URL would be longer than their limit.
3071 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3072 * reached this limit and action editing stopped working in these
3073 * browsers (BR #1570678).
3075 * The config option split-large-forms works around this browser
3076 * bug (HTTP has no URL lenght limitation) by deviding the action
3077 * list form into multiple smaller ones. It means the URLs are shorter
3078 * and work in broken browsers as well, but the user can no longer change
3079 * all actions with one submit.
3081 * A better solution would be to switch to POST requests,
3082 * but this will do for now.
3084 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3086 /* Generate multiple smaller form by killing the big one. */
3087 err = map_block_killer(exports, "one-form-only");
3091 /* Default: Generate one large form by killing the smaller ones. */
3092 err = map_block_killer(exports, "multiple-forms");
3095 for (i = 0; i < MAX_AF_FILES; i++)
3097 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3099 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3107 edit_free_file(file);
3112 if (0 == have_filters)
3114 err = map(exports, "filter-params", 1, "", 1);
3119 * List available filters and their settings.
3121 * XXX: Different types of filters should use different
3122 * @substitution strings@. They are currently listed
3123 * in the order they appear in the filter files and
3124 * are all inserted as @filter-params@ which can lead
3125 * to an "interesting" mix.
3128 int filter_identifier = 0;
3129 char * filter_template;
3131 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3134 edit_free_file(file);
3136 if (err == JB_ERR_FILE)
3138 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3143 err = template_fill(&filter_template, exports);
3145 result = strdup("");
3147 for (i = 0; i < MAX_AF_FILES; i++)
3149 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3151 filter_group = csp->rlist[i]->f;
3152 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3154 char current_mode = 'x';
3155 struct list_entry *filter_name;
3157 struct map *line_exports;
3159 int multi_action_index = 0;
3161 char *abbr_filter_type;
3163 switch (filter_group->type)
3165 case FT_CONTENT_FILTER:
3166 /* XXX: Should we call it content-filter instead? */
3167 filter_type = "filter";
3168 abbr_filter_type = "F";
3169 multi_action_index = ACTION_MULTI_FILTER;
3171 case FT_SERVER_HEADER_FILTER:
3172 filter_type = "server-header-filter";
3173 abbr_filter_type = "S";
3174 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3176 case FT_CLIENT_HEADER_FILTER:
3177 filter_type = "client-header-filter";
3178 abbr_filter_type = "C";
3179 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3182 log_error(LOG_LEVEL_FATAL,
3183 "cgi_edit_actions_for_url: Unknown filter type: %u for filter %s.",
3184 filter_group->type, filter_group->name);
3187 assert(multi_action_index);
3189 filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
3190 while ((filter_name != NULL)
3191 && (0 != strcmp(filter_group->name, filter_name->str)))
3193 filter_name = filter_name->next;
3196 if (filter_name != NULL)
3202 filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
3203 while ((filter_name != NULL)
3204 && (0 != strcmp(filter_group->name, filter_name->str)))
3206 filter_name = filter_name->next;
3208 if (filter_name != NULL)
3214 /* Generate a unique serial number */
3215 snprintf(number, sizeof(number), "%x", filter_identifier++);
3216 number[sizeof(number) - 1] = '\0';
3218 line_exports = new_map();
3219 if (line_exports == NULL)
3221 err = JB_ERR_MEMORY;
3226 if (!err) err = map(line_exports, "index", 1, number, 1);
3227 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3228 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3229 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3230 if (!err) err = map(line_exports, "filter-type", 1, filter_type, 1);
3231 if (!err) err = map(line_exports, "abbr-filter-type", 1, abbr_filter_type, 1);
3236 this_line = strdup(filter_template);
3237 if (this_line == NULL) err = JB_ERR_MEMORY;
3239 if (!err) err = template_fill(&this_line, line_exports);
3240 string_join(&result, this_line);
3242 free_map(line_exports);
3247 freez(filter_template);
3251 err = map(exports, "filter-params", 1, result, 0);
3259 if (!err) err = map_radio(exports, "filter-all", "nx",
3260 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3262 edit_free_file(file);
3270 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3274 /*********************************************************************
3276 * Function : cgi_edit_actions_submit
3278 * Description : CGI function that actually edits the Actions list.
3281 * 1 : csp = Current client state (buffers, headers, etc...)
3282 * 2 : rsp = http_response data structure for output
3283 * 3 : parameters = map of cgi parameters
3285 * CGI Parameters : None
3287 * Returns : JB_ERR_OK on success
3288 * JB_ERR_MEMORY on out-of-memory
3289 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3290 * specified or not valid.
3292 *********************************************************************/
3293 jb_err cgi_edit_actions_submit(struct client_state *csp,
3294 struct http_response *rsp,
3295 const struct map *parameters)
3301 struct editable_file * file;
3302 struct file_line * cur_line;
3303 unsigned line_number;
3306 int filter_identifier;
3307 const char * action_set_name;
3309 struct file_list * fl;
3310 struct url_actions * b;
3312 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3314 return cgi_error_disabled(csp, rsp);
3317 err = get_number_param(csp, parameters, "s", §ionid);
3323 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3326 /* No filename specified, can't read file, modified, or out of memory. */
3327 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3330 cur_line = file->lines;
3332 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3334 cur_line = cur_line->next;
3337 if ( (cur_line == NULL)
3338 || (line_number != sectionid)
3340 || (cur_line->type != FILE_LINE_ACTION))
3342 /* Invalid "sectionid" parameter */
3343 edit_free_file(file);
3344 return JB_ERR_CGI_PARAMS;
3347 get_string_param(parameters, "p", &action_set_name);
3348 if (action_set_name != NULL)
3350 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3352 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3354 for (b = b->next; NULL != b; b = b->next)
3356 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3358 copy_action(cur_line->data.action, b->action);
3364 edit_free_file(file);
3365 return JB_ERR_CGI_PARAMS;
3371 err = actions_from_radio(parameters, cur_line->data.action);
3377 edit_free_file(file);
3381 ch = get_char_param(parameters, "filter_all");
3384 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3385 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3386 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3390 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3393 for (filter_identifier = 0; !err; filter_identifier++)
3400 * Filter state. Valid states are: 'Y' (active),
3401 * 'N' (inactive) and 'X' (no change).
3405 * Abbreviated filter type. Valid types are: 'F' (content filter),
3406 * 'S' (server-header filter) and 'C' (client-header filter).
3408 int multi_action_index = 0;
3410 /* Generate the keys */
3411 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3412 key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
3413 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3414 key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
3415 snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
3417 err = get_string_param(parameters, key_name, &name);
3426 type = get_char_param(parameters, key_type);
3430 multi_action_index = ACTION_MULTI_FILTER;
3433 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
3436 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
3439 log_error(LOG_LEVEL_ERROR,
3440 "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
3443 assert(multi_action_index);
3445 value = get_char_param(parameters, key_value);
3448 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3449 if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
3450 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3452 else if (value == 'N')
3454 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3455 if (!cur_line->data.action->multi_remove_all[multi_action_index])
3457 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3458 if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
3461 else if (value == 'X')
3463 list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
3464 list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
3471 edit_free_file(file);
3475 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3478 edit_free_file(file);
3479 return JB_ERR_MEMORY;
3482 len = strlen(actiontext);
3486 * Empty action - must special-case this.
3487 * Simply setting len to 1 is sufficient...
3492 if (NULL == (newtext = malloc(len + 2)))
3496 edit_free_file(file);
3497 return JB_ERR_MEMORY;
3499 strcpy(newtext, actiontext);
3503 newtext[len + 1] = '\0';
3505 freez(cur_line->raw);
3506 freez(cur_line->unprocessed);
3507 cur_line->unprocessed = newtext;
3509 err = edit_write_file(file);
3512 /* Error writing file */
3513 if (err == JB_ERR_FILE)
3515 /* Read-only file. */
3516 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3518 edit_free_file(file);
3522 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3523 (long) time(NULL), file->identifier, sectionid);
3525 edit_free_file(file);
3527 return cgi_redirect(rsp, target);
3531 /*********************************************************************
3533 * Function : cgi_edit_actions_url
3535 * Description : CGI function that actually edits a URL pattern in
3539 * 1 : csp = Current client state (buffers, headers, etc...)
3540 * 2 : rsp = http_response data structure for output
3541 * 3 : parameters = map of cgi parameters
3544 * filename : Identifies the file to edit
3545 * ver : File's last-modified time
3546 * section : Line number of section to edit
3547 * pattern : Line number of pattern to edit
3548 * newval : New value for pattern
3550 * Returns : JB_ERR_OK on success
3551 * JB_ERR_MEMORY on out-of-memory
3552 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3553 * specified or not valid.
3555 *********************************************************************/
3556 jb_err cgi_edit_actions_url(struct client_state *csp,
3557 struct http_response *rsp,
3558 const struct map *parameters)
3562 struct editable_file * file;
3563 struct file_line * cur_line;
3564 unsigned line_number;
3565 unsigned section_start_line_number = 0;
3573 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3575 return cgi_error_disabled(csp, rsp);
3578 err = get_number_param(csp, parameters, "p", &patternid);
3585 return JB_ERR_CGI_PARAMS;
3588 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3594 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3597 /* No filename specified, can't read file, modified, or out of memory. */
3599 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3603 cur_line = file->lines;
3605 while ((cur_line != NULL) && (line_number < patternid))
3607 if (cur_line->type == FILE_LINE_ACTION)
3609 section_start_line_number = line_number;
3611 cur_line = cur_line->next;
3615 if ( (cur_line == NULL)
3616 || (cur_line->type != FILE_LINE_URL))
3618 /* Invalid "patternid" parameter */
3620 edit_free_file(file);
3621 return JB_ERR_CGI_PARAMS;
3624 /* At this point, the line to edit is in cur_line */
3626 freez(cur_line->raw);
3627 freez(cur_line->unprocessed);
3628 cur_line->unprocessed = new_pattern;
3630 err = edit_write_file(file);
3633 /* Error writing file */
3634 if (err == JB_ERR_FILE)
3636 /* Read-only file. */
3637 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3639 edit_free_file(file);
3643 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3644 (long) time(NULL), file->identifier, section_start_line_number);
3646 edit_free_file(file);
3648 return cgi_redirect(rsp, target);
3652 /*********************************************************************
3654 * Function : cgi_edit_actions_add_url
3656 * Description : CGI function that actually adds a URL pattern to
3660 * 1 : csp = Current client state (buffers, headers, etc...)
3661 * 2 : rsp = http_response data structure for output
3662 * 3 : parameters = map of cgi parameters
3665 * filename : Identifies the file to edit
3666 * ver : File's last-modified time
3667 * section : Line number of section to edit
3668 * newval : New pattern
3670 * Returns : JB_ERR_OK on success
3671 * JB_ERR_MEMORY on out-of-memory
3672 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3673 * specified or not valid.
3675 *********************************************************************/
3676 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3677 struct http_response *rsp,
3678 const struct map *parameters)
3682 struct file_line * new_line;
3683 struct editable_file * file;
3684 struct file_line * cur_line;
3685 unsigned line_number;
3689 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3691 return cgi_error_disabled(csp, rsp);
3694 err = get_number_param(csp, parameters, "s", §ionid);
3701 return JB_ERR_CGI_PARAMS;
3704 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3710 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3713 /* No filename specified, can't read file, modified, or out of memory. */
3715 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3719 cur_line = file->lines;
3721 while ((cur_line != NULL) && (line_number < sectionid))
3723 cur_line = cur_line->next;
3727 if ( (cur_line == NULL)
3728 || (cur_line->type != FILE_LINE_ACTION))
3730 /* Invalid "sectionid" parameter */
3732 edit_free_file(file);
3733 return JB_ERR_CGI_PARAMS;
3736 /* At this point, the section header is in cur_line - add after this. */
3738 /* Allocate the new line */
3739 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3740 if (new_line == NULL)
3743 edit_free_file(file);
3744 return JB_ERR_MEMORY;
3747 /* Fill in the data members of the new line */
3748 new_line->raw = NULL;
3749 new_line->prefix = NULL;
3750 new_line->unprocessed = new_pattern;
3751 new_line->type = FILE_LINE_URL;
3753 /* Link new_line into the list, after cur_line */
3754 new_line->next = cur_line->next;
3755 cur_line->next = new_line;
3757 /* Done making changes, now commit */
3759 err = edit_write_file(file);
3762 /* Error writing file */
3763 if (err == JB_ERR_FILE)
3765 /* Read-only file. */
3766 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3768 edit_free_file(file);
3772 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3773 (long) time(NULL), file->identifier, sectionid);
3775 edit_free_file(file);
3777 return cgi_redirect(rsp, target);
3781 /*********************************************************************
3783 * Function : cgi_edit_actions_remove_url
3785 * Description : CGI function that actually removes a URL pattern from
3789 * 1 : csp = Current client state (buffers, headers, etc...)
3790 * 2 : rsp = http_response data structure for output
3791 * 3 : parameters = map of cgi parameters
3794 * f : (filename) Identifies the file to edit
3795 * v : (version) File's last-modified time
3796 * p : (pattern) Line number of pattern to remove
3798 * Returns : JB_ERR_OK on success
3799 * JB_ERR_MEMORY on out-of-memory
3800 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3801 * specified or not valid.
3803 *********************************************************************/
3804 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3805 struct http_response *rsp,
3806 const struct map *parameters)
3809 struct editable_file * file;
3810 struct file_line * cur_line;
3811 struct file_line * prev_line;
3812 unsigned line_number;
3813 unsigned section_start_line_number = 0;
3817 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3819 return cgi_error_disabled(csp, rsp);
3822 err = get_number_param(csp, parameters, "p", &patternid);
3828 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3831 /* No filename specified, can't read file, modified, or out of memory. */
3832 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3837 cur_line = file->lines;
3839 while ((cur_line != NULL) && (line_number < patternid))
3841 if (cur_line->type == FILE_LINE_ACTION)
3843 section_start_line_number = line_number;
3845 prev_line = cur_line;
3846 cur_line = cur_line->next;
3850 if ( (cur_line == NULL)
3851 || (prev_line == NULL)
3852 || (cur_line->type != FILE_LINE_URL))
3854 /* Invalid "patternid" parameter */
3855 edit_free_file(file);
3856 return JB_ERR_CGI_PARAMS;
3859 /* At this point, the line to remove is in cur_line, and the previous
3860 * one is in prev_line
3863 /* Unlink cur_line */
3864 prev_line->next = cur_line->next;
3865 cur_line->next = NULL;
3868 edit_free_file_lines(cur_line);
3870 err = edit_write_file(file);
3873 /* Error writing file */
3874 if (err == JB_ERR_FILE)
3876 /* Read-only file. */
3877 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3879 edit_free_file(file);
3883 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3884 (long) time(NULL), file->identifier, section_start_line_number);
3886 edit_free_file(file);
3888 return cgi_redirect(rsp, target);
3892 /*********************************************************************
3894 * Function : cgi_edit_actions_section_remove
3896 * Description : CGI function that actually removes a whole section from
3897 * the actions file. The section must be empty first
3898 * (else JB_ERR_CGI_PARAMS).
3901 * 1 : csp = Current client state (buffers, headers, etc...)
3902 * 2 : rsp = http_response data structure for output
3903 * 3 : parameters = map of cgi parameters
3906 * f : (filename) Identifies the file to edit
3907 * v : (version) File's last-modified time
3908 * s : (section) Line number of section to edit
3910 * Returns : JB_ERR_OK on success
3911 * JB_ERR_MEMORY on out-of-memory
3912 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3913 * specified or not valid.
3915 *********************************************************************/
3916 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3917 struct http_response *rsp,
3918 const struct map *parameters)
3921 struct editable_file * file;
3922 struct file_line * cur_line;
3923 struct file_line * prev_line;
3924 unsigned line_number;
3928 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3930 return cgi_error_disabled(csp, rsp);
3933 err = get_number_param(csp, parameters, "s", §ionid);
3939 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3942 /* No filename specified, can't read file, modified, or out of memory. */
3943 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3947 cur_line = file->lines;
3950 while ((cur_line != NULL) && (line_number < sectionid))
3952 prev_line = cur_line;
3953 cur_line = cur_line->next;
3957 if ( (cur_line == NULL)
3958 || (cur_line->type != FILE_LINE_ACTION) )
3960 /* Invalid "sectionid" parameter */
3961 edit_free_file(file);
3962 return JB_ERR_CGI_PARAMS;
3965 if ( (cur_line->next != NULL)
3966 && (cur_line->next->type == FILE_LINE_URL) )
3968 /* Section not empty. */
3969 edit_free_file(file);
3970 return JB_ERR_CGI_PARAMS;
3973 /* At this point, the line to remove is in cur_line, and the previous
3974 * one is in prev_line
3977 /* Unlink cur_line */
3978 if (prev_line == NULL)
3980 /* Removing the first line from the file */
3981 file->lines = cur_line->next;
3985 prev_line->next = cur_line->next;
3987 cur_line->next = NULL;
3990 edit_free_file_lines(cur_line);
3992 err = edit_write_file(file);
3995 /* Error writing file */
3996 if (err == JB_ERR_FILE)
3998 /* Read-only file. */
3999 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4001 edit_free_file(file);
4005 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4006 (long) time(NULL), file->identifier);
4008 edit_free_file(file);
4010 return cgi_redirect(rsp, target);
4014 /*********************************************************************
4016 * Function : cgi_edit_actions_section_add
4018 * Description : CGI function that adds a new empty section to
4022 * 1 : csp = Current client state (buffers, headers, etc...)
4023 * 2 : rsp = http_response data structure for output
4024 * 3 : parameters = map of cgi parameters
4027 * f : (filename) Identifies the file to edit
4028 * v : (version) File's last-modified time
4029 * s : (section) Line number of section to add after, 0 for
4032 * Returns : JB_ERR_OK on success
4033 * JB_ERR_MEMORY on out-of-memory
4034 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4035 * specified or not valid.
4037 *********************************************************************/
4038 jb_err cgi_edit_actions_section_add(struct client_state *csp,
4039 struct http_response *rsp,
4040 const struct map *parameters)
4043 struct file_line * new_line;
4045 struct editable_file * file;
4046 struct file_line * cur_line;
4047 unsigned line_number;
4051 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4053 return cgi_error_disabled(csp, rsp);
4056 err = get_number_param(csp, parameters, "s", §ionid);
4062 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4065 /* No filename specified, can't read file, modified, or out of memory. */
4066 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4070 cur_line = file->lines;
4072 if (sectionid <= 1U)
4074 /* Add to start of file */
4075 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
4077 /* There's something in the file, find the line before the first
4080 while ( (cur_line->next != NULL)
4081 && (cur_line->next->type != FILE_LINE_ACTION) )
4083 cur_line = cur_line->next;
4089 /* File starts with action line, so insert at top */
4095 /* Add after stated section. */
4096 while ((cur_line != NULL) && (line_number < sectionid))
4098 cur_line = cur_line->next;
4102 if ( (cur_line == NULL)
4103 || (cur_line->type != FILE_LINE_ACTION))
4105 /* Invalid "sectionid" parameter */
4106 edit_free_file(file);
4107 return JB_ERR_CGI_PARAMS;
4110 /* Skip through the section to find the last line in it. */
4111 while ( (cur_line->next != NULL)
4112 && (cur_line->next->type != FILE_LINE_ACTION) )
4114 cur_line = cur_line->next;
4119 /* At this point, the last line in the previous section is in cur_line
4120 * - add after this. (Or if we need to add as the first line, cur_line
4124 new_text = strdup("{}");
4125 if (NULL == new_text)
4127 edit_free_file(file);
4128 return JB_ERR_MEMORY;
4131 /* Allocate the new line */
4132 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4133 if (new_line == NULL)
4136 edit_free_file(file);
4137 return JB_ERR_MEMORY;
4140 /* Fill in the data members of the new line */
4141 new_line->raw = NULL;
4142 new_line->prefix = NULL;
4143 new_line->unprocessed = new_text;
4144 new_line->type = FILE_LINE_ACTION;
4146 if (cur_line != NULL)
4148 /* Link new_line into the list, after cur_line */
4149 new_line->next = cur_line->next;
4150 cur_line->next = new_line;
4154 /* Link new_line into the list, as first line */
4155 new_line->next = file->lines;
4156 file->lines = new_line;
4159 /* Done making changes, now commit */
4161 err = edit_write_file(file);
4164 /* Error writing file */
4165 if (err == JB_ERR_FILE)
4167 /* Read-only file. */
4168 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4170 edit_free_file(file);
4174 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4175 (long) time(NULL), file->identifier);
4177 edit_free_file(file);
4179 return cgi_redirect(rsp, target);
4183 /*********************************************************************
4185 * Function : cgi_edit_actions_section_swap
4187 * Description : CGI function that swaps the order of two sections
4188 * in the actions file. Note that this CGI can actually
4189 * swap any two arbitrary sections, but the GUI interface
4190 * currently only allows consecutive sections to be
4194 * 1 : csp = Current client state (buffers, headers, etc...)
4195 * 2 : rsp = http_response data structure for output
4196 * 3 : parameters = map of cgi parameters
4199 * f : (filename) Identifies the file to edit
4200 * v : (version) File's last-modified time
4201 * s1 : (section1) Line number of first section to swap
4202 * s2 : (section2) Line number of second section to swap
4204 * Returns : JB_ERR_OK on success
4205 * JB_ERR_MEMORY on out-of-memory
4206 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4207 * specified or not valid.
4209 *********************************************************************/
4210 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4211 struct http_response *rsp,
4212 const struct map *parameters)
4216 struct editable_file * file;
4217 struct file_line * cur_line;
4218 struct file_line * prev_line;
4219 struct file_line * line_before_section1;
4220 struct file_line * line_start_section1;
4221 struct file_line * line_end_section1;
4222 struct file_line * line_after_section1;
4223 struct file_line * line_before_section2;
4224 struct file_line * line_start_section2;
4225 struct file_line * line_end_section2;
4226 struct file_line * line_after_section2;
4227 unsigned line_number;
4231 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4233 return cgi_error_disabled(csp, rsp);
4236 err = get_number_param(csp, parameters, "s1", §ion1);
4237 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4243 if (section1 > section2)
4245 unsigned temp = section2;
4246 section2 = section1;
4250 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4253 /* No filename specified, can't read file, modified, or out of memory. */
4254 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4257 /* Start at the beginning... */
4259 cur_line = file->lines;
4262 /* ... find section1 ... */
4263 while ((cur_line != NULL) && (line_number < section1))
4265 prev_line = cur_line;
4266 cur_line = cur_line->next;
4270 if ( (cur_line == NULL)
4271 || (cur_line->type != FILE_LINE_ACTION) )
4273 /* Invalid "section1" parameter */
4274 edit_free_file(file);
4275 return JB_ERR_CGI_PARAMS;
4278 /* If no-op, we've validated params and can skip the rest. */
4279 if (section1 != section2)
4281 /* ... find the end of section1 ... */
4282 line_before_section1 = prev_line;
4283 line_start_section1 = cur_line;
4286 prev_line = cur_line;
4287 cur_line = cur_line->next;
4290 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4291 line_end_section1 = prev_line;
4292 line_after_section1 = cur_line;
4294 /* ... find section2 ... */
4295 while ((cur_line != NULL) && (line_number < section2))
4297 prev_line = cur_line;
4298 cur_line = cur_line->next;
4302 if ( (cur_line == NULL)
4303 || (cur_line->type != FILE_LINE_ACTION) )
4305 /* Invalid "section2" parameter */
4306 edit_free_file(file);
4307 return JB_ERR_CGI_PARAMS;
4310 /* ... find the end of section2 ... */
4311 line_before_section2 = prev_line;
4312 line_start_section2 = cur_line;
4315 prev_line = cur_line;
4316 cur_line = cur_line->next;
4319 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4320 line_end_section2 = prev_line;
4321 line_after_section2 = cur_line;
4323 /* Now have all the pointers we need. Do the swap. */
4325 /* Change the pointer to section1 to point to section2 instead */
4326 if (line_before_section1 == NULL)
4328 file->lines = line_start_section2;
4332 line_before_section1->next = line_start_section2;
4335 if (line_before_section2 == line_end_section1)
4337 /* Consecutive sections */
4338 line_end_section2->next = line_start_section1;
4342 line_end_section2->next = line_after_section1;
4343 line_before_section2->next = line_start_section1;
4346 /* Set the pointer from the end of section1 to the rest of the file */
4347 line_end_section1->next = line_after_section2;
4349 err = edit_write_file(file);
4352 /* Error writing file */
4353 if (err == JB_ERR_FILE)
4355 /* Read-only file. */
4356 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4358 edit_free_file(file);
4361 } /* END if (section1 != section2) */
4363 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4364 (long) time(NULL), file->identifier);
4366 edit_free_file(file);
4368 return cgi_redirect(rsp, target);
4371 #ifdef FEATURE_TOGGLE
4372 /*********************************************************************
4374 * Function : cgi_toggle
4376 * Description : CGI function that adds a new empty section to
4380 * 1 : csp = Current client state (buffers, headers, etc...)
4381 * 2 : rsp = http_response data structure for output
4382 * 3 : parameters = map of cgi parameters
4385 * set : If present, how to change toggle setting:
4386 * "enable", "disable", "toggle", or none (default).
4387 * mini : If present, use mini reply template.
4389 * Returns : JB_ERR_OK on success
4390 * JB_ERR_MEMORY on out-of-memory
4392 *********************************************************************/
4393 jb_err cgi_toggle(struct client_state *csp,
4394 struct http_response *rsp,
4395 const struct map *parameters)
4397 struct map *exports;
4399 const char *template_name;
4405 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4407 return cgi_error_disabled(csp, rsp);
4410 mode = get_char_param(parameters, "set");
4415 global_toggle_state = 1;
4417 else if (mode == 'D')
4420 global_toggle_state = 0;
4422 else if (mode == 'T')
4425 global_toggle_state = !global_toggle_state;
4428 if (NULL == (exports = default_exports(csp, "toggle")))
4430 return JB_ERR_MEMORY;
4433 template_name = (get_char_param(parameters, "mini")
4437 return template_fill_for_cgi(csp, template_name, exports, rsp);
4439 #endif /* def FEATURE_TOGGLE */
4441 /*********************************************************************
4443 * Function : javascriptify
4445 * Description : Converts a string into a form JavaScript will like.
4447 * Netscape 4's JavaScript sucks - it doesn't use
4448 * "id" parameters, so you have to set the "name"
4449 * used to submit a form element to something JavaScript
4450 * will like. (Or access the elements by index in an
4451 * array. That array contains >60 elements and will
4452 * be changed whenever we add a new action to the
4453 * editor, so I'm NOT going to use indexes that have
4454 * to be figured out by hand.)
4456 * Currently the only thing we have to worry about
4457 * is "-" ==> "_" conversion.
4459 * This is a length-preserving operation so it is
4460 * carried out in-place, no memory is allocated
4464 * 1 : identifier = String to make JavaScript-friendly.
4468 *********************************************************************/
4469 static void javascriptify(char * identifier)
4471 char * p = identifier;
4472 while (NULL != (p = strchr(p, '-')))
4479 /*********************************************************************
4481 * Function : actions_to_radio
4483 * Description : Converts a actionsfile entry into settings for
4484 * radio buttons and edit boxes on a HTML form.
4487 * 1 : exports = List of substitutions to add to.
4488 * 2 : action = Action to read
4490 * Returns : JB_ERR_OK on success
4491 * JB_ERR_MEMORY on out-of-memory
4493 *********************************************************************/
4494 static jb_err actions_to_radio(struct map * exports,
4495 const struct action_spec *action)
4497 unsigned mask = action->mask;
4498 unsigned add = action->add;
4506 mask = action->mask;
4509 /* sanity - prevents "-feature +feature" */
4513 #define DEFINE_ACTION_BOOL(name, bit) \
4514 if (!(mask & bit)) \
4516 current_mode = 'n'; \
4518 else if (add & bit) \
4520 current_mode = 'y'; \
4524 current_mode = 'x'; \
4526 if (map_radio(exports, name, "ynx", current_mode)) \
4528 return JB_ERR_MEMORY; \
4531 #define DEFINE_ACTION_STRING(name, bit, index) \
4532 DEFINE_ACTION_BOOL(name, bit); \
4535 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4538 checked = !strcmp(action->string[index], value); \
4542 checked = is_default; \
4544 mapped_param |= checked; \
4545 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4547 return JB_ERR_MEMORY; \
4550 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4551 if (map(exports, name "-param-custom", 1, \
4552 ((!mapped_param) ? "checked" : ""), 1)) \
4554 return JB_ERR_MEMORY; \
4556 if (map(exports, name "-param", 1, \
4557 (((add & bit) && !mapped_param) ? \
4558 action->string[index] : default_val), 1)) \
4560 return JB_ERR_MEMORY; \
4563 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4564 if (map(exports, name "-param", 1, \
4565 ((add & bit) ? action->string[index] : default_val), 1)) \
4567 return JB_ERR_MEMORY; \
4570 #define DEFINE_ACTION_MULTI(name, index) \
4571 if (action->multi_add[index]->first) \
4573 current_mode = 'y'; \
4575 else if (action->multi_remove_all[index]) \
4577 current_mode = 'n'; \
4579 else if (action->multi_remove[index]->first) \
4581 current_mode = 'y'; \
4585 current_mode = 'x'; \
4587 if (map_radio(exports, name, "ynx", current_mode)) \
4589 return JB_ERR_MEMORY; \
4592 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4594 #include "actionlist.h"
4596 #undef DEFINE_ACTION_MULTI
4597 #undef DEFINE_ACTION_STRING
4598 #undef DEFINE_ACTION_BOOL
4599 #undef DEFINE_ACTION_ALIAS
4600 #undef DEFINE_CGI_PARAM_CUSTOM
4601 #undef DEFINE_CGI_PARAM_RADIO
4602 #undef DEFINE_CGI_PARAM_NO_RADIO
4608 /*********************************************************************
4610 * Function : actions_from_radio
4612 * Description : Converts a map of parameters passed to a CGI function
4613 * into an actionsfile entry.
4616 * 1 : parameters = parameters to the CGI call
4617 * 2 : action = Action to change. Must be valid before
4618 * the call, actions not specified will be
4621 * Returns : JB_ERR_OK on success
4622 * JB_ERR_MEMORY on out-of-memory
4624 *********************************************************************/
4625 static jb_err actions_from_radio(const struct map * parameters,
4626 struct action_spec *action)
4631 const char * js_name;
4632 jb_err err = JB_ERR_OK;
4637 /* Statics are generally a potential race condition,
4638 * but in this case we're safe and don't need semaphores.
4639 * Be careful if you modify this function.
4641 * The js_name_arr's are never free()d, but this is no
4642 * problem, since they will only be created once and
4643 * used by all threads thereafter. -oes
4646 #define JAVASCRIPTIFY(dest_var, string) \
4648 static int first_time = 1; \
4649 static char *js_name_arr; \
4652 js_name_arr = strdup(string); \
4653 javascriptify(js_name_arr); \
4655 dest_var = js_name_arr; \
4659 #define DEFINE_ACTION_BOOL(name, bit) \
4660 JAVASCRIPTIFY(js_name, name); \
4661 ch = get_char_param(parameters, js_name); \
4664 action->add |= bit; \
4665 action->mask |= bit; \
4667 else if (ch == 'N') \
4669 action->add &= ~bit; \
4670 action->mask &= ~bit; \
4672 else if (ch == 'X') \
4674 action->add &= ~bit; \
4675 action->mask |= bit; \
4678 #define DEFINE_ACTION_STRING(name, bit, index) \
4679 JAVASCRIPTIFY(js_name, name); \
4680 ch = get_char_param(parameters, js_name); \
4684 JAVASCRIPTIFY(js_name, name "-mode"); \
4685 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4686 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4688 JAVASCRIPTIFY(js_name, name "-param"); \
4689 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4691 if (param != NULL) \
4693 if (NULL == (param_dup = strdup(param))) \
4695 return JB_ERR_MEMORY; \
4697 freez(action->string[index]); \
4698 action->add |= bit; \
4699 action->mask |= bit; \
4700 action->string[index] = param_dup; \
4703 else if (ch == 'N') \
4705 if (action->add & bit) \
4707 freez(action->string[index]); \
4709 action->add &= ~bit; \
4710 action->mask &= ~bit; \
4712 else if (ch == 'X') \
4714 if (action->add & bit) \
4716 freez(action->string[index]); \
4718 action->add &= ~bit; \
4719 action->mask |= bit; \
4722 #define DEFINE_ACTION_MULTI(name, index) \
4723 JAVASCRIPTIFY(js_name, name); \
4724 ch = get_char_param(parameters, js_name); \
4729 else if (ch == 'N') \
4731 list_remove_all(action->multi_add[index]); \
4732 list_remove_all(action->multi_remove[index]); \
4733 action->multi_remove_all[index] = 1; \
4735 else if (ch == 'X') \
4737 list_remove_all(action->multi_add[index]); \
4738 list_remove_all(action->multi_remove[index]); \
4739 action->multi_remove_all[index] = 0; \
4742 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4744 #include "actionlist.h"
4746 #undef DEFINE_ACTION_MULTI
4747 #undef DEFINE_ACTION_STRING
4748 #undef DEFINE_ACTION_BOOL
4749 #undef DEFINE_ACTION_ALIAS
4750 #undef JAVASCRIPTIFY
4756 #endif /* def FEATURE_CGI_EDIT_ACTIONS */