1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.45 2006/12/21 12:57:48 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
10 * NOTE: The CGIs in this file use parameter names
11 * such as "f" and "s" which are really *BAD* choices.
12 * However, I'm trying to save bytes in the
13 * edit-actions-list HTML page - the standard actions
14 * file generated a 550kbyte page, which is ridiculous.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001 the SourceForge
19 * Privoxy team. http://www.privoxy.org/
21 * Based on the Internet Junkbuster originally written
22 * by and Copyright (C) 1997 Anonymous Coders and
23 * Junkbusters Corporation. http://www.junkbusters.com
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General
27 * Public License as published by the Free Software
28 * Foundation; either version 2 of the License, or (at
29 * your option) any later version.
31 * This program is distributed in the hope that it will
32 * be useful, but WITHOUT ANY WARRANTY; without even the
33 * implied warranty of MERCHANTABILITY or FITNESS FOR A
34 * PARTICULAR PURPOSE. See the GNU General Public
35 * License for more details.
37 * The GNU General Public License should be included with
38 * this file. If not, you can view it at
39 * http://www.gnu.org/copyleft/gpl.html
40 * or write to the Free Software Foundation, Inc., 59
41 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 * Revision 1.45 2006/12/21 12:57:48 fabiankeil
46 * Add config option "split-large-forms"
47 * to work around the browser bug reported
50 * Revision 1.44 2006/12/09 13:49:16 fabiankeil
51 * Fix configure option --disable-toggle.
52 * Thanks to Peter Thoenen for reporting this.
54 * Revision 1.43 2006/07/18 14:48:45 david__schmidt
55 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
56 * with what was really the latest development (the v_3_0_branch branch)
58 * Revision 1.41.2.12 2006/01/30 15:16:25 david__schmidt
59 * Remove a little residual debugging info
61 * Revision 1.41.2.11 2006/01/29 23:10:56 david__schmidt
62 * Multiple filter file support
64 * Revision 1.41.2.10 2005/07/04 03:13:43 david__schmidt
65 * Undo some damaging memory leak patches
67 * Revision 1.41.2.9 2005/07/04 00:31:04 david__schmidt
68 * Removing a double free
70 * Revision 1.41.2.8 2005/05/07 21:50:54 david__schmidt
71 * A few memory leaks plugged (mostly on error paths)
73 * Revision 1.41.2.7 2004/02/17 13:30:23 oes
74 * Moved cgi_error_disabled() from cgiedit.c to
75 * cgi.c to re-enable build with --disable-editor.
76 * Fixes Bug #892744. Thanks to Matthew Fischer
79 * Revision 1.41.2.6 2003/12/18 08:13:48 oes
80 * One line lost in last commit
82 * Revision 1.41.2.5 2003/12/17 16:33:47 oes
83 * - All edit functions that redirect back to the list page
84 * now use cgi_redirect
85 * - All redirects now contain useless parameter "foo", whose
86 * value are raw seconds since epoch, in order to force
87 * Opera and Konqueror to properly reload the list. Closes
90 * Revision 1.41.2.4 2003/03/11 11:53:59 oes
91 * Cosmetic: Renamed cryptic variable
93 * Revision 1.41.2.3 2002/11/12 15:01:41 oes
94 * Fix: Don't free uninitialized struct editable_file
96 * Revision 1.41.2.2 2002/08/05 20:02:59 oes
97 * Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
99 * Revision 1.41.2.1 2002/08/02 12:43:14 oes
100 * Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
102 * Revision 1.41 2002/05/21 19:09:45 oes
103 * - Made Add/Edit/Remove URL Submit and Cancel
104 * buttons jump back to relevant section in eal
105 * - Bugfix: remove-url-form needs p export
107 * Revision 1.40 2002/05/19 11:34:35 jongfoster
108 * Handling read-only actions files better - report the actual
109 * error, not "Out of memory"!
112 * http://sourceforge.net/tracker/index.php?func=detail
113 * &aid=557905&group_id=11118&atid=111118
115 * Revision 1.39 2002/05/12 21:39:15 jongfoster
116 * - Adding Doxygen-style comments to structures and #defines.
117 * - Correcting function comments
119 * Revision 1.38 2002/05/03 23:00:38 jongfoster
120 * Support for templates for "standard actions" buttons.
123 * Revision 1.37 2002/04/30 11:14:52 oes
124 * Made csp the first parameter in *action_to_html
126 * Revision 1.36 2002/04/26 21:53:30 jongfoster
127 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
129 * Revision 1.35 2002/04/26 21:50:02 jongfoster
130 * Honouring default exports in edit-actions-for-url-filter template.
132 * Revision 1.34 2002/04/26 12:54:17 oes
133 * Adaptions to changes in actions.c
135 * Revision 1.33 2002/04/24 02:17:47 oes
136 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
138 * - Activated Jon's code for editing multiple AFs
139 * - cgi_edit_list_actions now provides context-sensitive
140 * help, looks up all action sets from standard.action and
141 * makes buttons for them in the catchall section
142 * - cgi_edit_action_submit now honors a p parameter, looks up
143 * the corresponding action set, and sets the catchall pattern's
144 * actions accordingly.
146 * Revision 1.32 2002/04/19 16:55:31 jongfoster
147 * Fixing newline problems. If we do our own text file newline
148 * mangling, we don't want the library to do any, so we need to
149 * open the files in *binary* mode.
151 * Revision 1.31 2002/04/18 19:21:08 jongfoster
152 * Added code to detect "conventional" action files, that start
153 * with a set of actions for all URLs (the pattern "/").
154 * These are special-cased in the "edit-actions-list" CGI, so
155 * that a special UI can be written for them.
157 * Revision 1.30 2002/04/10 13:38:35 oes
158 * load_template signature changed
160 * Revision 1.29 2002/04/08 16:59:08 oes
163 * Revision 1.28 2002/03/27 12:30:29 oes
164 * Deleted unsused variable
166 * Revision 1.27 2002/03/26 23:06:04 jongfoster
167 * Removing duplicate @ifs on the toggle page
169 * Revision 1.26 2002/03/26 22:59:17 jongfoster
170 * Fixing /toggle to display status consistently.
172 * Revision 1.25 2002/03/26 22:29:54 swa
173 * we have a new homepage!
175 * Revision 1.24 2002/03/24 15:23:33 jongfoster
178 * Revision 1.23 2002/03/24 13:32:41 swa
179 * name change related issues
181 * Revision 1.22 2002/03/24 13:25:43 swa
182 * name change related issues
184 * Revision 1.21 2002/03/22 18:02:48 jongfoster
185 * Fixing remote toggle
187 * Revision 1.20 2002/03/16 20:28:34 oes
188 * Added descriptions to the filters so users will know what they select in the cgi editor
190 * Revision 1.19 2002/03/16 18:38:14 jongfoster
191 * Stopping stupid or malicious users from breaking the actions
192 * file using the web-based editor.
194 * Revision 1.18 2002/03/16 14:57:44 jongfoster
195 * Full support for enabling/disabling modular filters.
197 * Revision 1.17 2002/03/16 14:26:42 jongfoster
198 * First version of modular filters support - READ ONLY!
199 * Fixing a double-free bug in the out-of-memory handling in map_radio().
201 * Revision 1.16 2002/03/07 03:46:17 oes
202 * Fixed compiler warnings
204 * Revision 1.15 2002/03/06 22:54:35 jongfoster
205 * Automated function-comment nitpicking.
207 * Revision 1.14 2002/03/05 00:24:51 jongfoster
208 * Patch to always edit the current actions file.
210 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
211 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
213 * Revision 1.12 2002/03/03 09:18:03 joergs
214 * Made jumbjuster work on AmigaOS again.
216 * Revision 1.11 2002/01/23 01:03:31 jongfoster
217 * Fixing gcc [CygWin] compiler warnings
219 * Revision 1.10 2002/01/23 00:22:59 jongfoster
220 * Adding new function cgi_edit_actions_section_swap(), to reorder
223 * Adding get_url_spec_param() to get a validated URL pattern.
225 * Moving edit_read_line() out of this file and into loaders.c.
227 * Adding missing html_encode() to many CGI functions.
229 * Moving the functions that #include actionlist.h to the end of the file,
230 * because the Visual C++ 97 debugger gets extremely confused if you try
231 * to debug any code that comes after them in the file.
233 * Major optimizations in cgi_edit_actions_list() to reduce the size of
234 * the generated HTML (down 40% from 550k to 304k), with major side-effects
235 * throughout the editor and templates. In particular, the length of the
236 * URLs throughout the editor has been drastically reduced, by cutting
237 * paramater names down to 1 character and CGI names down to 3-4
238 * characters, by removing all non-essential CGI paramaters even at the
239 * expense of having to re-read the actions file for the most trivial
240 * page, and by using relative rather than absolute URLs. This means
241 * that this (typical example):
243 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
244 * filename=ijb&ver=1011487572&section=12&pattern=13
245 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
249 * <a href="eau?f=ijb&v=1011487572&p=13">
251 * Revision 1.9 2002/01/17 20:56:22 jongfoster
252 * Replacing hard references to the URL of the config interface
253 * with #defines from project.h
255 * Revision 1.8 2001/11/30 23:35:51 jongfoster
256 * Renaming actionsfile to ijb.action
258 * Revision 1.7 2001/11/13 00:28:24 jongfoster
259 * - Renaming parameters from edit-actions-for-url so that they only
260 * contain legal JavaScript characters. If we wanted to write
261 * JavaScript that worked with Netscape 4, this is nessacery.
262 * (Note that at the moment the JavaScript doesn't actually work
263 * with Netscape 4, but now this is purely a template issue, not
264 * one affecting code).
265 * - Adding new CGIs for use by non-JavaScript browsers:
266 * edit-actions-url-form
267 * edit-actions-add-url-form
268 * edit-actions-remove-url-form
271 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
272 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
273 * by and __OS2__ ifdef.
275 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
276 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
277 * threads to call select() simultaneously. So, it's time to do a real, live,
278 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
279 * (native). Both versions will work, but using __OS2__ offers multi-threading.
281 * Revision 1.4 2001/10/23 21:48:19 jongfoster
282 * Cleaning up error handling in CGI functions - they now send back
283 * a HTML error page and should never cause a FATAL error. (Fixes one
284 * potential source of "denial of service" attacks).
286 * CGI actions file editor that works and is actually useful.
288 * Ability to toggle JunkBuster remotely using a CGI call.
290 * You can turn off both the above features in the main configuration
291 * file, e.g. if you are running a multi-user proxy.
293 * Revision 1.3 2001/10/14 22:12:49 jongfoster
294 * New version of CGI-based actionsfile editor.
295 * Major changes, including:
296 * - Completely new file parser and file output routines
297 * - edit-actions CGI renamed edit-actions-for-url
298 * - All CGIs now need a filename parameter, except for...
299 * - New CGI edit-actions which doesn't need a filename,
300 * to allow you to start the editor up.
301 * - edit-actions-submit now works, and now automatically
302 * redirects you back to the main edit-actions-list handler.
304 * Revision 1.2 2001/09/16 17:05:14 jongfoster
305 * Removing unused #include showarg.h
307 * Revision 1.1 2001/09/16 15:47:37 jongfoster
308 * First version of CGI-based edit interface. This is very much a
309 * work-in-progress, and you can't actually use it to edit anything
310 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
311 * to have any effect.
314 **********************************************************************/
320 * FIXME: Following includes copied from cgi.c - which are actually needed?
325 #include <sys/types.h>
329 #include <sys/stat.h>
332 #define snprintf _snprintf
333 #endif /* def _WIN32 */
338 #include "cgisimple.h"
342 #include "miscutil.h"
345 #ifdef FEATURE_TOGGLE
346 /* loadcfg.h is for global_toggle_state only */
348 #endif /* def FEATURE_TOGGLE */
349 #include "urlmatch.h"
351 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
354 #ifdef FEATURE_CGI_EDIT_ACTIONS
357 * A line in an editable_file.
361 /** Next entry in the linked list */
362 struct file_line * next;
364 /** The raw data, to write out if this line is unmodified. */
367 /** Comments and/or whitespace to put before this line if it's modified
368 and then written out. */
371 /** The actual data, as a string. Line continuation and comment removal
372 are performed on the data read from file before it's stored here, so
373 it will be a single line of data. */
376 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
379 /** The actual data, processed into some sensible data type. */
383 /** An action specification. */
384 struct action_spec action[1];
386 /** A name=value pair. */
390 /** The name in the name=value pair. */
393 /** The value in the name=value pair, as a string. */
396 /** The value in the name=value pair, as an integer. */
401 /* Add more data types here... e.g.
404 struct url_spec url[1];
408 struct action_spec action[1];
418 /** This file_line has not been processed yet. */
419 #define FILE_LINE_UNPROCESSED 1
421 /** This file_line is blank. Can only appear at the end of a file, due to
422 the way the parser works. */
423 #define FILE_LINE_BLANK 2
425 /** This file_line says {{alias}}. */
426 #define FILE_LINE_ALIAS_HEADER 3
428 /** This file_line defines an alias. */
429 #define FILE_LINE_ALIAS_ENTRY 4
431 /** This file_line defines an {action}. */
432 #define FILE_LINE_ACTION 5
434 /** This file_line specifies a URL pattern. */
435 #define FILE_LINE_URL 6
437 /** This file_line says {{settings}}. */
438 #define FILE_LINE_SETTINGS_HEADER 7
440 /** This file_line is in a {{settings}} block. */
441 #define FILE_LINE_SETTINGS_ENTRY 8
443 /** This file_line says {{description}}. */
444 #define FILE_LINE_DESCRIPTION_HEADER 9
446 /** This file_line is in a {{description}} block. */
447 #define FILE_LINE_DESCRIPTION_ENTRY 10
451 * A configuration file, in a format that can be edited and written back to
456 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
457 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
458 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
459 /**< Pre-encoded with url_encode() for ease of use. */
460 const char * version_str; /**< Last modification time, as a string. For CGI param. */
461 /**< Can be used in URL without using url_param(). */
462 unsigned version; /**< Last modification time - prevents chaos with
463 the browser's "back" button. Note that this is a
464 time_t cast to an unsigned. When comparing, always
465 cast the time_t to an unsigned, and *NOT* vice-versa.
466 This may lose the top few bits, but they're not
467 significant anyway. */
468 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
469 Note that changing this after the file has been
470 read in will cause a mess. */
471 struct file_line * parse_error; /**< On parse error, this is the offending line. */
472 const char * parse_error_text; /**< On parse error, this is the problem.
473 (Statically allocated) */
476 /* FIXME: Following non-static functions should be prototyped in .h or made static */
478 /* Functions to read and write arbitrary config files */
479 jb_err edit_read_file(struct client_state *csp,
480 const struct map *parameters,
483 struct editable_file **pfile);
484 jb_err edit_write_file(struct editable_file * file);
485 void edit_free_file(struct editable_file * file);
487 /* Functions to read and write actions files */
488 jb_err edit_parse_actions_file(struct editable_file * file);
489 jb_err edit_read_actions_file(struct client_state *csp,
490 struct http_response *rsp,
491 const struct map *parameters,
493 struct editable_file **pfile);
496 jb_err cgi_error_modified(struct client_state *csp,
497 struct http_response *rsp,
498 const char *filename);
499 jb_err cgi_error_parse(struct client_state *csp,
500 struct http_response *rsp,
501 struct editable_file *file);
502 jb_err cgi_error_file(struct client_state *csp,
503 struct http_response *rsp,
504 const char *filename);
505 jb_err cgi_error_file_read_only(struct client_state *csp,
506 struct http_response *rsp,
507 const char *filename);
509 /* Internal arbitrary config file support functions */
510 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
511 static void edit_free_file_lines(struct file_line * first_line);
513 /* Internal actions file support functions */
514 static int match_actions_file_header_line(const char * line, const char * name);
515 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
517 /* Internal parameter parsing functions */
518 static jb_err get_file_name_param(struct client_state *csp,
519 const struct map *parameters,
520 const char *param_name,
523 const char **pparam);
525 static jb_err get_url_spec_param(struct client_state *csp,
526 const struct map *parameters,
531 /* Internal actionsfile <==> HTML conversion functions */
532 static jb_err map_radio(struct map * exports,
533 const char * optionname,
536 static jb_err actions_to_radio(struct map * exports,
537 const struct action_spec *action);
538 static jb_err actions_from_radio(const struct map * parameters,
539 struct action_spec *action);
542 static jb_err map_copy_parameter_html(struct map *out,
543 const struct map *in,
545 #if 0 /* unused function */
546 static jb_err map_copy_parameter_url(struct map *out,
547 const struct map *in,
549 #endif /* unused function */
551 /* Internal convenience functions */
552 static char *section_target(const unsigned sectionid);
554 /*********************************************************************
556 * Function : section_target
558 * Description : Given an unsigned (section id) n, produce a dynamically
559 * allocated string of the form #l<n>, for use in link
563 * 1 : sectionid = start line number of section
565 * Returns : String with link target, or NULL if out of
568 *********************************************************************/
569 static char *section_target(const unsigned sectionid)
573 snprintf(buf, 30, "#l%d", sectionid);
579 /*********************************************************************
581 * Function : map_copy_parameter_html
583 * Description : Copy a CGI parameter from one map to another, HTML
587 * 1 : out = target map
588 * 2 : in = source map
589 * 3 : name = name of cgi parameter to copy
591 * Returns : JB_ERR_OK on success
592 * JB_ERR_MEMORY on out-of-memory
593 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
596 *********************************************************************/
597 static jb_err map_copy_parameter_html(struct map *out,
598 const struct map *in,
608 value = lookup(in, name);
609 err = map(out, name, 1, html_encode(value), 0);
616 else if (*value == '\0')
618 return JB_ERR_CGI_PARAMS;
627 #if 0 /* unused function */
628 /*********************************************************************
630 * Function : map_copy_parameter_url
632 * Description : Copy a CGI parameter from one map to another, URL
636 * 1 : out = target map
637 * 2 : in = source map
638 * 3 : name = name of cgi parameter to copy
640 * Returns : JB_ERR_OK on success
641 * JB_ERR_MEMORY on out-of-memory
642 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
645 *********************************************************************/
646 static jb_err map_copy_parameter_url(struct map *out,
647 const struct map *in,
657 value = lookup(in, name);
658 err = map(out, name, 1, url_encode(value), 0);
665 else if (*value == '\0')
667 return JB_ERR_CGI_PARAMS;
674 #endif /* 0 - unused function */
676 /*********************************************************************
678 * Function : cgi_edit_actions_url_form
680 * Description : CGI function that displays a form for
684 * 1 : csp = Current client state (buffers, headers, etc...)
685 * 2 : rsp = http_response data structure for output
686 * 3 : parameters = map of cgi parameters
689 * f : (filename) Identifies the file to edit
690 * v : (version) File's last-modified time
691 * p : (pattern) Line number of pattern to edit
693 * Returns : JB_ERR_OK on success
694 * JB_ERR_MEMORY on out-of-memory
695 * JB_ERR_CGI_PARAMS if the CGI parameters are not
696 * specified or not valid.
698 *********************************************************************/
699 jb_err cgi_edit_actions_url_form(struct client_state *csp,
700 struct http_response *rsp,
701 const struct map *parameters)
703 struct map * exports;
705 struct editable_file * file;
706 struct file_line * cur_line;
707 unsigned line_number;
708 unsigned section_start_line_number = 0;
715 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
717 return cgi_error_disabled(csp, rsp);
720 err = get_number_param(csp, parameters, "p", &patternid);
726 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
729 /* No filename specified, can't read file, modified, or out of memory. */
730 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
733 cur_line = file->lines;
735 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
737 if (cur_line->type == FILE_LINE_ACTION)
739 section_start_line_number = line_number;
741 cur_line = cur_line->next;
744 if ( (cur_line == NULL)
745 || (line_number != patternid)
747 || (cur_line->type != FILE_LINE_URL))
749 /* Invalid "patternid" parameter */
750 edit_free_file(file);
751 return JB_ERR_CGI_PARAMS;
754 if (NULL == (exports = default_exports(csp, NULL)))
756 edit_free_file(file);
757 return JB_ERR_MEMORY;
760 err = map(exports, "f", 1, file->identifier, 1);
761 if (!err) err = map(exports, "v", 1, file->version_str, 1);
762 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
763 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
764 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
766 edit_free_file(file);
774 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
778 /*********************************************************************
780 * Function : cgi_edit_actions_add_url_form
782 * Description : CGI function that displays a form for
786 * 1 : csp = Current client state (buffers, headers, etc...)
787 * 2 : rsp = http_response data structure for output
788 * 3 : parameters = map of cgi parameters
791 * f : (filename) Identifies the file to edit
792 * v : (version) File's last-modified time
793 * s : (section) Line number of section to edit
795 * Returns : JB_ERR_OK on success
796 * JB_ERR_MEMORY on out-of-memory
797 * JB_ERR_CGI_PARAMS if the CGI parameters are not
798 * specified or not valid.
800 *********************************************************************/
801 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
802 struct http_response *rsp,
803 const struct map *parameters)
812 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
814 return cgi_error_disabled(csp, rsp);
817 if (NULL == (exports = default_exports(csp, NULL)))
819 return JB_ERR_MEMORY;
822 err = map_copy_parameter_html(exports, parameters, "f");
823 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
824 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
832 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
836 /*********************************************************************
838 * Function : cgi_edit_actions_remove_url_form
840 * Description : CGI function that displays a form for
844 * 1 : csp = Current client state (buffers, headers, etc...)
845 * 2 : rsp = http_response data structure for output
846 * 3 : parameters = map of cgi parameters
849 * f : (filename) Identifies the file to edit
850 * v : (version) File's last-modified time
851 * p : (pattern) Line number of pattern to edit
853 * Returns : JB_ERR_OK on success
854 * JB_ERR_MEMORY on out-of-memory
855 * JB_ERR_CGI_PARAMS if the CGI parameters are not
856 * specified or not valid.
858 *********************************************************************/
859 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
860 struct http_response *rsp,
861 const struct map *parameters)
863 struct map * exports;
865 struct editable_file * file;
866 struct file_line * cur_line;
867 unsigned line_number;
868 unsigned section_start_line_number = 0;
875 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
877 return cgi_error_disabled(csp, rsp);
880 err = get_number_param(csp, parameters, "p", &patternid);
886 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
889 /* No filename specified, can't read file, modified, or out of memory. */
890 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
893 cur_line = file->lines;
895 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
897 if (cur_line->type == FILE_LINE_ACTION)
899 section_start_line_number = line_number;
901 cur_line = cur_line->next;
904 if ( (cur_line == NULL)
905 || (line_number != patternid)
907 || (cur_line->type != FILE_LINE_URL))
909 /* Invalid "patternid" parameter */
910 edit_free_file(file);
911 return JB_ERR_CGI_PARAMS;
914 if (NULL == (exports = default_exports(csp, NULL)))
916 edit_free_file(file);
917 return JB_ERR_MEMORY;
920 err = map(exports, "f", 1, file->identifier, 1);
921 if (!err) err = map(exports, "v", 1, file->version_str, 1);
922 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
923 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
924 if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
926 edit_free_file(file);
934 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
938 /*********************************************************************
940 * Function : edit_write_file
942 * Description : Write a complete file to disk.
945 * 1 : file = File to write.
947 * Returns : JB_ERR_OK on success
948 * JB_ERR_FILE on error writing to file.
949 * JB_ERR_MEMORY on out of memory
951 *********************************************************************/
952 jb_err edit_write_file(struct editable_file * file)
955 struct file_line * cur_line;
956 struct stat statbuf[1];
957 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
958 digits in time_t, assuming this is a 64-bit
959 machine, plus null terminator, plus one
963 assert(file->filename);
965 if (NULL == (fp = fopen(file->filename, "wb")))
970 cur_line = file->lines;
971 while (cur_line != NULL)
975 if (fputs(cur_line->raw, fp) < 0)
983 if (cur_line->prefix)
985 if (fputs(cur_line->prefix, fp) < 0)
991 if (cur_line->unprocessed)
994 if (NULL != strchr(cur_line->unprocessed, '#'))
996 /* Must quote '#' characters */
1003 /* Count number of # characters, so we know length of output string */
1004 src = cur_line->unprocessed;
1005 while (NULL != (src = strchr(src, '#')))
1010 assert(numhash > 0);
1012 /* Allocate new memory for string */
1013 len = strlen(cur_line->unprocessed);
1014 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
1016 /* Uh oh, just trashed file! */
1018 return JB_ERR_MEMORY;
1021 /* Loop through string from end */
1022 src = cur_line->unprocessed + len;
1023 dest = str + len + numhash;
1024 for ( ; len >= 0; len--)
1026 if ((*dest-- = *src--) == '#')
1030 assert(numhash >= 0);
1033 assert(numhash == 0);
1034 assert(src + 1 == cur_line->unprocessed);
1035 assert(dest + 1 == str);
1037 if (fputs(str, fp) < 0)
1048 /* Can write without quoting '#' characters. */
1049 if (fputs(cur_line->unprocessed, fp) < 0)
1055 if (fputs(NEWLINE(file->newline), fp) < 0)
1063 /* FIXME: Write data from file->data->whatever */
1067 cur_line = cur_line->next;
1073 /* Update the version stamp in the file structure, since we just
1074 * wrote to the file & changed it's date.
1076 if (stat(file->filename, statbuf) < 0)
1078 /* Error, probably file not found. */
1081 file->version = (unsigned)statbuf->st_mtime;
1083 /* Correct file->version_str */
1084 freez(file->version_str);
1085 snprintf(version_buf, 22, "%u", file->version);
1086 version_buf[21] = '\0';
1087 file->version_str = strdup(version_buf);
1088 if (version_buf == NULL)
1090 return JB_ERR_MEMORY;
1097 /*********************************************************************
1099 * Function : edit_free_file
1101 * Description : Free a complete file in memory.
1104 * 1 : file = Data structure to free.
1108 *********************************************************************/
1109 void edit_free_file(struct editable_file * file)
1113 /* Silently ignore NULL pointer */
1117 edit_free_file_lines(file->lines);
1118 freez(file->filename);
1119 freez(file->identifier);
1120 freez(file->version_str);
1122 file->parse_error_text = NULL; /* Statically allocated */
1123 file->parse_error = NULL;
1129 /*********************************************************************
1131 * Function : edit_free_file_lines
1133 * Description : Free an entire linked list of file lines.
1136 * 1 : first_line = Data structure to free.
1140 *********************************************************************/
1141 static void edit_free_file_lines(struct file_line * first_line)
1143 struct file_line * next_line;
1145 while (first_line != NULL)
1147 next_line = first_line->next;
1148 first_line->next = NULL;
1149 freez(first_line->raw);
1150 freez(first_line->prefix);
1151 freez(first_line->unprocessed);
1152 switch(first_line->type)
1154 case 0: /* special case if memory zeroed */
1155 case FILE_LINE_UNPROCESSED:
1156 case FILE_LINE_BLANK:
1157 case FILE_LINE_ALIAS_HEADER:
1158 case FILE_LINE_SETTINGS_HEADER:
1159 case FILE_LINE_DESCRIPTION_HEADER:
1160 case FILE_LINE_DESCRIPTION_ENTRY:
1161 case FILE_LINE_ALIAS_ENTRY:
1163 /* No data is stored for these */
1166 case FILE_LINE_ACTION:
1167 free_action(first_line->data.action);
1170 case FILE_LINE_SETTINGS_ENTRY:
1171 freez(first_line->data.setting.name);
1172 freez(first_line->data.setting.svalue);
1175 /* Should never happen */
1179 first_line->type = 0; /* paranoia */
1181 first_line = next_line;
1186 /*********************************************************************
1188 * Function : match_actions_file_header_line
1190 * Description : Match an actions file {{header}} line
1193 * 1 : line = String from file
1194 * 2 : name = Header to match against
1196 * Returns : 0 iff they match.
1198 *********************************************************************/
1199 static int match_actions_file_header_line(const char * line, const char * name)
1207 if ((line[0] != '{') || (line[1] != '{'))
1213 /* Look for optional whitespace */
1214 while ( (*line == ' ') || (*line == '\t') )
1219 /* Look for the specified name (case-insensitive) */
1221 if (0 != strncmpic(line, name, len))
1227 /* Look for optional whitespace */
1228 while ( (*line == ' ') || (*line == '\t') )
1233 /* Look for "}}" and end of string*/
1234 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1244 /*********************************************************************
1246 * Function : match_actions_file_header_line
1248 * Description : Match an actions file {{header}} line
1251 * 1 : line = String from file. Must not start with
1252 * whitespace (else infinite loop!)
1253 * 2 : pname = Destination for name
1254 * 2 : pvalue = Destination for value
1256 * Returns : JB_ERR_OK on success
1257 * JB_ERR_MEMORY on out-of-memory
1258 * JB_ERR_PARSE if there's no "=" sign, or if there's
1259 * nothing before the "=" sign (but empty
1260 * values *after* the "=" sign are legal).
1262 *********************************************************************/
1263 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1265 const char * name_end;
1266 const char * value_start;
1272 assert(*line != ' ');
1273 assert(*line != '\t');
1278 value_start = strchr(line, '=');
1279 if ((value_start == NULL) || (value_start == line))
1281 return JB_ERR_PARSE;
1284 name_end = value_start - 1;
1286 /* Eat any whitespace before the '=' */
1287 while ((*name_end == ' ') || (*name_end == '\t'))
1290 * we already know we must have at least 1 non-ws char
1291 * at start of buf - no need to check
1296 name_len = name_end - line + 1; /* Length excluding \0 */
1297 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1299 return JB_ERR_MEMORY;
1301 strncpy(*pname, line, name_len);
1302 (*pname)[name_len] = '\0';
1304 /* Eat any the whitespace after the '=' */
1306 while ((*value_start == ' ') || (*value_start == '\t'))
1311 if (NULL == (*pvalue = strdup(value_start)))
1315 return JB_ERR_MEMORY;
1322 /*********************************************************************
1324 * Function : edit_parse_actions_file
1326 * Description : Parse an actions file in memory.
1328 * Passed linked list must have the "data" member
1329 * zeroed, and must contain valid "next" and
1330 * "unprocessed" fields. The "raw" and "prefix"
1331 * fields are ignored, and "type" is just overwritten.
1333 * Note that on error the file may have been
1337 * 1 : file = Actions file to be parsed in-place.
1339 * Returns : JB_ERR_OK on success
1340 * JB_ERR_MEMORY on out-of-memory
1341 * JB_ERR_PARSE on error
1343 *********************************************************************/
1344 jb_err edit_parse_actions_file(struct editable_file * file)
1346 struct file_line * cur_line;
1348 const char * text; /* Text from a line */
1349 char * name; /* For lines of the form name=value */
1350 char * value; /* For lines of the form name=value */
1351 struct action_alias * alias_list = NULL;
1352 jb_err err = JB_ERR_OK;
1354 /* alias_list contains the aliases defined in this file.
1355 * It might be better to use the "file_line.data" fields
1356 * in the relavent places instead.
1359 cur_line = file->lines;
1361 /* A note about blank line support: Blank lines should only
1362 * ever occur as the last line in the file. This function
1363 * is more forgiving than that - FILE_LINE_BLANK can occur
1367 /* Skip leading blanks. Should only happen if file is
1368 * empty (which is valid, but pointless).
1370 while ( (cur_line != NULL)
1371 && (cur_line->unprocessed[0] == '\0') )
1374 cur_line->type = FILE_LINE_BLANK;
1375 cur_line = cur_line->next;
1378 if ( (cur_line != NULL)
1379 && (cur_line->unprocessed[0] != '{') )
1381 /* File doesn't start with a header */
1382 file->parse_error = cur_line;
1383 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1384 return JB_ERR_PARSE;
1387 if ( (cur_line != NULL) && (0 ==
1388 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1390 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1392 cur_line = cur_line->next;
1393 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1395 if (cur_line->unprocessed[0])
1397 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1399 err = split_line_on_equals(cur_line->unprocessed,
1400 &cur_line->data.setting.name,
1401 &cur_line->data.setting.svalue);
1402 if (err == JB_ERR_MEMORY)
1406 else if (err != JB_ERR_OK)
1408 /* Line does not contain a name=value pair */
1409 file->parse_error = cur_line;
1410 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1411 return JB_ERR_PARSE;
1416 cur_line->type = FILE_LINE_BLANK;
1418 cur_line = cur_line->next;
1422 if ( (cur_line != NULL) && (0 ==
1423 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1425 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1427 cur_line = cur_line->next;
1428 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1430 if (cur_line->unprocessed[0])
1432 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1436 cur_line->type = FILE_LINE_BLANK;
1438 cur_line = cur_line->next;
1442 if ( (cur_line != NULL) && (0 ==
1443 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1445 cur_line->type = FILE_LINE_ALIAS_HEADER;
1447 cur_line = cur_line->next;
1448 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1450 if (cur_line->unprocessed[0])
1452 /* define an alias */
1453 struct action_alias * new_alias;
1455 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1457 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1458 if (err == JB_ERR_MEMORY)
1462 else if (err != JB_ERR_OK)
1464 /* Line does not contain a name=value pair */
1465 file->parse_error = cur_line;
1466 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1467 return JB_ERR_PARSE;
1470 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1475 free_alias_list(alias_list);
1476 return JB_ERR_MEMORY;
1479 err = get_actions(value, alias_list, new_alias->action);
1482 /* Invalid action or out of memory */
1486 free_alias_list(alias_list);
1487 if (err == JB_ERR_MEMORY)
1493 /* Line does not contain a name=value pair */
1494 file->parse_error = cur_line;
1495 file->parse_error_text = "This alias does not specify a valid set of actions.";
1496 return JB_ERR_PARSE;
1502 new_alias->name = name;
1505 new_alias->next = alias_list;
1506 alias_list = new_alias;
1510 cur_line->type = FILE_LINE_BLANK;
1512 cur_line = cur_line->next;
1516 /* Header done, process the main part of the file */
1517 while (cur_line != NULL)
1519 /* At this point, (cur_line->unprocessed[0] == '{') */
1520 assert(cur_line->unprocessed[0] == '{');
1521 text = cur_line->unprocessed + 1;
1522 len = strlen(text) - 1;
1523 if (text[len] != '}')
1525 /* No closing } on header */
1526 free_alias_list(alias_list);
1527 file->parse_error = cur_line;
1528 file->parse_error_text = "Headers starting with '{' must have a "
1529 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1530 "must close with two brackets ('}}').";
1531 return JB_ERR_PARSE;
1536 /* An invalid {{ header. */
1537 free_alias_list(alias_list);
1538 file->parse_error = cur_line;
1539 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1540 "Please remember that the system (two-bracket) headers must "
1541 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1542 "and must appear before any actions (one-bracket) headers. "
1543 "Also note that system headers may not be repeated.";
1544 return JB_ERR_PARSE;
1547 while ( (*text == ' ') || (*text == '\t') )
1553 && ( (text[len - 1] == ' ')
1554 || (text[len - 1] == '\t') ) )
1559 cur_line->type = FILE_LINE_ACTION;
1561 /* Remove {} and make copy */
1562 if (NULL == (value = (char *) malloc(len + 1)))
1565 free_alias_list(alias_list);
1566 return JB_ERR_MEMORY;
1568 strncpy(value, text, len);
1572 err = get_actions(value, alias_list, cur_line->data.action);
1575 /* Invalid action or out of memory */
1577 free_alias_list(alias_list);
1578 if (err == JB_ERR_MEMORY)
1584 /* Line does not contain a name=value pair */
1585 file->parse_error = cur_line;
1586 file->parse_error_text = "This header does not specify a valid set of actions.";
1587 return JB_ERR_PARSE;
1591 /* Done with string - it was clobbered anyway */
1594 /* Process next line */
1595 cur_line = cur_line->next;
1597 /* Loop processing URL patterns */
1598 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1600 if (cur_line->unprocessed[0])
1602 /* Could parse URL here, but this isn't currently needed */
1604 cur_line->type = FILE_LINE_URL;
1608 cur_line->type = FILE_LINE_BLANK;
1610 cur_line = cur_line->next;
1612 } /* End main while(cur_line != NULL) loop */
1614 free_alias_list(alias_list);
1620 /*********************************************************************
1622 * Function : edit_read_file_lines
1624 * Description : Read all the lines of a file into memory.
1625 * Handles whitespace, comments and line continuation.
1628 * 1 : fp = File to read from. On return, this will be
1629 * at EOF but it will not have been closed.
1630 * 2 : pfile = Destination for a linked list of file_lines.
1631 * Will be set to NULL on error.
1632 * 3 : newline = How to handle newlines.
1634 * Returns : JB_ERR_OK on success
1635 * JB_ERR_MEMORY on out-of-memory
1637 *********************************************************************/
1638 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1640 struct file_line * first_line; /* Keep for return value or to free */
1641 struct file_line * cur_line; /* Current line */
1642 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1650 cur_line = first_line = zalloc(sizeof(struct file_line));
1651 if (cur_line == NULL)
1653 return JB_ERR_MEMORY;
1656 cur_line->type = FILE_LINE_UNPROCESSED;
1658 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1661 /* Out of memory or empty file. */
1662 /* Note that empty file is not an error we propogate up */
1664 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1669 prev_line = cur_line;
1670 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1671 if (cur_line == NULL)
1674 edit_free_file_lines(first_line);
1675 return JB_ERR_MEMORY;
1678 cur_line->type = FILE_LINE_UNPROCESSED;
1680 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1681 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1684 edit_free_file_lines(first_line);
1685 return JB_ERR_MEMORY;
1689 while (rval != JB_ERR_FILE);
1693 /* We allocated one too many - free it */
1694 prev_line->next = NULL;
1697 *pfile = first_line;
1702 /*********************************************************************
1704 * Function : edit_read_file
1706 * Description : Read a complete file into memory.
1707 * Handles CGI parameter parsing. If requested, also
1708 * checks the file's modification timestamp.
1711 * 1 : csp = Current client state (buffers, headers, etc...)
1712 * 2 : parameters = map of cgi parameters.
1713 * 3 : require_version = true to check "ver" parameter.
1714 * 4 : suffix = File extension, e.g. ".action".
1715 * 5 : pfile = Destination for the file. Will be set
1719 * filename : The name of the file to read, without the
1720 * path or ".action" extension.
1721 * ver : (Only if require_version is nonzero)
1722 * Timestamp of the actions file. If wrong, this
1723 * function fails with JB_ERR_MODIFIED.
1725 * Returns : JB_ERR_OK on success
1726 * JB_ERR_MEMORY on out-of-memory
1727 * JB_ERR_CGI_PARAMS if "filename" was not specified
1729 * JB_ERR_FILE if the file cannot be opened or
1731 * JB_ERR_MODIFIED if version checking was requested and
1732 * failed - the file was modified outside
1733 * of this CGI editor instance.
1735 *********************************************************************/
1736 jb_err edit_read_file(struct client_state *csp,
1737 const struct map *parameters,
1738 int require_version,
1740 struct editable_file **pfile)
1742 struct file_line * lines;
1746 const char * identifier;
1747 struct editable_file * file;
1748 unsigned version = 0;
1749 struct stat statbuf[1];
1750 char version_buf[22];
1751 int newline = NEWLINE_UNKNOWN;
1759 err = get_file_name_param(csp, parameters, "f", suffix,
1760 &filename, &identifier);
1766 if (stat(filename, statbuf) < 0)
1768 /* Error, probably file not found. */
1772 version = (unsigned) statbuf->st_mtime;
1774 if (require_version)
1776 unsigned specified_version;
1777 err = get_number_param(csp, parameters, "v", &specified_version);
1784 if (version != specified_version)
1786 return JB_ERR_MODIFIED;
1790 if (NULL == (fp = fopen(filename,"rb")))
1796 err = edit_read_file_lines(fp, &lines, &newline);
1806 file = (struct editable_file *) zalloc(sizeof(*file));
1810 edit_free_file_lines(lines);
1814 file->lines = lines;
1815 file->newline = newline;
1816 file->filename = filename;
1817 file->version = version;
1818 file->identifier = url_encode(identifier);
1820 if (file->identifier == NULL)
1822 edit_free_file(file);
1823 return JB_ERR_MEMORY;
1826 /* Correct file->version_str */
1827 freez(file->version_str);
1828 snprintf(version_buf, 22, "%u", file->version);
1829 version_buf[21] = '\0';
1830 file->version_str = strdup(version_buf);
1831 if (version_buf == NULL)
1833 edit_free_file(file);
1834 return JB_ERR_MEMORY;
1842 /*********************************************************************
1844 * Function : edit_read_actions_file
1846 * Description : Read a complete actions file into memory.
1847 * Handles CGI parameter parsing. If requested, also
1848 * checks the file's modification timestamp.
1850 * If this function detects an error in the categories
1851 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1852 * then it handles it by filling in the specified
1853 * response structure and returning JB_ERR_FILE.
1856 * 1 : csp = Current client state (buffers, headers, etc...)
1857 * 2 : rsp = HTTP response. Only filled in on error.
1858 * 2 : parameters = map of cgi parameters.
1859 * 3 : require_version = true to check "ver" parameter.
1860 * 4 : pfile = Destination for the file. Will be set
1864 * filename : The name of the actions file to read, without the
1865 * path or ".action" extension.
1866 * ver : (Only if require_version is nonzero)
1867 * Timestamp of the actions file. If wrong, this
1868 * function fails with JB_ERR_MODIFIED.
1870 * Returns : JB_ERR_OK on success
1871 * JB_ERR_MEMORY on out-of-memory
1872 * JB_ERR_CGI_PARAMS if "filename" was not specified
1874 * JB_ERR_FILE if the file does not contain valid data,
1875 * or if file cannot be opened or
1876 * contains no data, or if version
1877 * checking was requested and failed.
1879 *********************************************************************/
1880 jb_err edit_read_actions_file(struct client_state *csp,
1881 struct http_response *rsp,
1882 const struct map *parameters,
1883 int require_version,
1884 struct editable_file **pfile)
1887 struct editable_file *file;
1895 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1898 /* Try to handle if possible */
1899 if (err == JB_ERR_FILE)
1901 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1903 else if (err == JB_ERR_MODIFIED)
1905 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1907 if (err == JB_ERR_OK)
1910 * Signal to higher-level CGI code that there was a problem but we
1911 * handled it, they should just return JB_ERR_OK.
1918 err = edit_parse_actions_file(file);
1921 if (err == JB_ERR_PARSE)
1923 err = cgi_error_parse(csp, rsp, file);
1924 if (err == JB_ERR_OK)
1927 * Signal to higher-level CGI code that there was a problem but we
1928 * handled it, they should just return JB_ERR_OK.
1933 edit_free_file(file);
1942 /*********************************************************************
1944 * Function : get_file_name_param
1946 * Description : Get the name of the file to edit from the parameters
1947 * passed to a CGI function. This function handles
1948 * security checks such as blocking urls containing
1949 * "/" or ".", prepending the config file directory,
1950 * and adding the specified suffix.
1952 * (This is an essential security check, otherwise
1953 * users may be able to pass "../../../etc/passwd"
1954 * and overwrite the password file [linux], "prn:"
1955 * and print random data [Windows], etc...)
1957 * This function only allows filenames contining the
1958 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1959 * That's probably too restrictive but at least it's
1963 * 1 : csp = Current client state (buffers, headers, etc...)
1964 * 2 : parameters = map of cgi parameters
1965 * 3 : param_name = The name of the parameter to read
1966 * 4 : suffix = File extension, e.g. ".actions"
1967 * 5 : pfilename = destination for full filename. Caller
1968 * free()s. Set to NULL on error.
1969 * 6 : pparam = destination for partial filename,
1970 * suitable for use in another URL. Allocated as part
1971 * of the map "parameters", so don't free it.
1972 * Set to NULL if not specified.
1974 * Returns : JB_ERR_OK on success
1975 * JB_ERR_MEMORY on out-of-memory
1976 * JB_ERR_CGI_PARAMS if "filename" was not specified
1979 *********************************************************************/
1980 static jb_err get_file_name_param(struct client_state *csp,
1981 const struct map *parameters,
1982 const char *param_name,
1985 const char **pparam)
2003 param = lookup(parameters, param_name);
2006 return JB_ERR_CGI_PARAMS;
2011 len = strlen(param);
2012 if (len >= FILENAME_MAX)
2015 return JB_ERR_CGI_PARAMS;
2018 /* Check every character to see if it's legal */
2020 while ((ch = *s++) != '\0')
2022 if ( ((ch < 'A') || (ch > 'Z'))
2023 && ((ch < 'a') || (ch > 'z'))
2024 && ((ch < '0') || (ch > '9'))
2028 /* Probable hack attempt. */
2029 return JB_ERR_CGI_PARAMS;
2033 /* Append extension */
2034 name = malloc(len + strlen(suffix) + 1);
2037 return JB_ERR_MEMORY;
2039 strcpy(name, param);
2040 strcpy(name + len, suffix);
2043 fullpath = make_path(csp->config->confdir, name);
2046 if (fullpath == NULL)
2048 return JB_ERR_MEMORY;
2052 *pfilename = fullpath;
2058 /*********************************************************************
2060 * Function : get_url_spec_param
2062 * Description : Get a URL pattern from the parameters
2063 * passed to a CGI function. Removes leading/trailing
2064 * spaces and validates it.
2067 * 1 : csp = Current client state (buffers, headers, etc...)
2068 * 2 : parameters = map of cgi parameters
2069 * 3 : name = Name of CGI parameter to read
2070 * 4 : pvalue = destination for value. Will be malloc()'d.
2071 * Set to NULL on error.
2073 * Returns : JB_ERR_OK on success
2074 * JB_ERR_MEMORY on out-of-memory
2075 * JB_ERR_CGI_PARAMS if the parameter was not specified
2078 *********************************************************************/
2079 static jb_err get_url_spec_param(struct client_state *csp,
2080 const struct map *parameters,
2084 const char *orig_param;
2087 struct url_spec compiled[1];
2097 orig_param = lookup(parameters, name);
2100 return JB_ERR_CGI_PARAMS;
2103 /* Copy and trim whitespace */
2104 param = strdup(orig_param);
2107 return JB_ERR_MEMORY;
2111 /* Must be non-empty, and can't allow 1st character to be '{' */
2112 if (param[0] == '\0' || param[0] == '{')
2115 return JB_ERR_CGI_PARAMS;
2118 /* Check for embedded newlines */
2119 for (s = param; *s != '\0'; s++)
2121 if ((*s == '\r') || (*s == '\n'))
2124 return JB_ERR_CGI_PARAMS;
2128 /* Check that regex is valid */
2133 return JB_ERR_MEMORY;
2135 err = create_url_spec(compiled, s);
2140 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2142 free_url_spec(compiled);
2144 if (param[strlen(param) - 1] == '\\')
2147 * Must protect trailing '\\' from becoming line continuation character.
2148 * Two methods: 1) If it's a domain only, add a trailing '/'.
2149 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2151 if (strchr(param, '/') == NULL)
2153 err = string_append(¶m, "/");
2157 err = string_append(¶m, "(?:)");
2164 /* Check that the modified regex is valid */
2169 return JB_ERR_MEMORY;
2171 err = create_url_spec(compiled, s);
2176 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2178 free_url_spec(compiled);
2185 /*********************************************************************
2187 * Function : map_radio
2189 * Description : Map a set of radio button values. E.g. if you have
2190 * 3 radio buttons, declare them as:
2191 * <option type="radio" name="xyz" @xyz-a@>
2192 * <option type="radio" name="xyz" @xyz-b@>
2193 * <option type="radio" name="xyz" @xyz-c@>
2194 * Then map one of the @xyz-?@ variables to "checked"
2195 * and all the others to empty by calling:
2196 * map_radio(exports, "xyz", "abc", sel)
2197 * Where 'sel' is 'a', 'b', or 'c'.
2200 * 1 : exports = Exports map to modify.
2201 * 2 : optionname = name for map
2202 * 3 : values = null-terminated list of values;
2203 * 4 : value = Selected value.
2205 * CGI Parameters : None
2207 * Returns : JB_ERR_OK on success
2208 * JB_ERR_MEMORY on out-of-memory
2210 *********************************************************************/
2211 static jb_err map_radio(struct map * exports,
2212 const char * optionname,
2213 const char * values,
2225 len = strlen(optionname);
2226 buf = malloc(len + 3);
2229 return JB_ERR_MEMORY;
2232 strcpy(buf, optionname);
2237 while ((c = *values++) != '\0')
2242 if (map(exports, buf, 1, "", 1))
2244 return JB_ERR_MEMORY;
2250 return map(exports, buf, 0, "checked", 1);
2254 /*********************************************************************
2256 * Function : cgi_error_modified
2258 * Description : CGI function that is called when a file is modified
2259 * outside the CGI editor.
2262 * 1 : csp = Current client state (buffers, headers, etc...)
2263 * 2 : rsp = http_response data structure for output
2264 * 3 : filename = The file that was modified.
2266 * CGI Parameters : none
2268 * Returns : JB_ERR_OK on success
2269 * JB_ERR_MEMORY on out-of-memory error.
2271 *********************************************************************/
2272 jb_err cgi_error_modified(struct client_state *csp,
2273 struct http_response *rsp,
2274 const char *filename)
2276 struct map *exports;
2283 if (NULL == (exports = default_exports(csp, NULL)))
2285 return JB_ERR_MEMORY;
2288 err = map(exports, "f", 1, html_encode(filename), 0);
2295 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2299 /*********************************************************************
2301 * Function : cgi_error_parse
2303 * Description : CGI function that is called when a file cannot
2304 * be parsed by the CGI editor.
2307 * 1 : csp = Current client state (buffers, headers, etc...)
2308 * 2 : rsp = http_response data structure for output
2309 * 3 : file = The file that was modified.
2311 * CGI Parameters : none
2313 * Returns : JB_ERR_OK on success
2314 * JB_ERR_MEMORY on out-of-memory error.
2316 *********************************************************************/
2317 jb_err cgi_error_parse(struct client_state *csp,
2318 struct http_response *rsp,
2319 struct editable_file *file)
2321 struct map *exports;
2323 struct file_line *cur_line;
2329 if (NULL == (exports = default_exports(csp, NULL)))
2331 return JB_ERR_MEMORY;
2334 err = map(exports, "f", 1, file->identifier, 1);
2335 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2337 cur_line = file->parse_error;
2340 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2341 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2349 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2353 /*********************************************************************
2355 * Function : cgi_error_file
2357 * Description : CGI function that is called when a file cannot be
2358 * opened by the CGI editor.
2361 * 1 : csp = Current client state (buffers, headers, etc...)
2362 * 2 : rsp = http_response data structure for output
2363 * 3 : filename = The file that was modified.
2365 * CGI Parameters : none
2367 * Returns : JB_ERR_OK on success
2368 * JB_ERR_MEMORY on out-of-memory error.
2370 *********************************************************************/
2371 jb_err cgi_error_file(struct client_state *csp,
2372 struct http_response *rsp,
2373 const char *filename)
2375 struct map *exports;
2382 if (NULL == (exports = default_exports(csp, NULL)))
2384 return JB_ERR_MEMORY;
2387 err = map(exports, "f", 1, html_encode(filename), 0);
2394 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2398 /*********************************************************************
2400 * Function : cgi_error_file
2402 * Description : CGI function that is called when a file cannot be
2403 * opened for writing by the CGI editor.
2406 * 1 : csp = Current client state (buffers, headers, etc...)
2407 * 2 : rsp = http_response data structure for output
2408 * 3 : filename = The file that we can't write to
2410 * CGI Parameters : none
2412 * Returns : JB_ERR_OK on success
2413 * JB_ERR_MEMORY on out-of-memory error.
2415 *********************************************************************/
2416 jb_err cgi_error_file_read_only(struct client_state *csp,
2417 struct http_response *rsp,
2418 const char *filename)
2420 struct map *exports;
2427 if (NULL == (exports = default_exports(csp, NULL)))
2429 return JB_ERR_MEMORY;
2432 err = map(exports, "f", 1, html_encode(filename), 0);
2439 return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
2443 /*********************************************************************
2445 * Function : cgi_edit_actions
2447 * Description : CGI function that allows the user to choose which
2448 * actions file to edit.
2451 * 1 : csp = Current client state (buffers, headers, etc...)
2452 * 2 : rsp = http_response data structure for output
2453 * 3 : parameters = map of cgi parameters
2455 * CGI Parameters : None
2457 * Returns : JB_ERR_OK on success
2458 * JB_ERR_MEMORY on out-of-memory error
2460 *********************************************************************/
2461 jb_err cgi_edit_actions(struct client_state *csp,
2462 struct http_response *rsp,
2463 const struct map *parameters)
2466 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2468 return cgi_error_disabled(csp, rsp);
2471 /* FIXME: Incomplete */
2473 return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
2478 /*********************************************************************
2480 * Function : cgi_edit_actions_list
2482 * Description : CGI function that edits the actions list.
2483 * FIXME: This function shouldn't FATAL ever.
2484 * FIXME: This function doesn't check the retval of map()
2486 * 1 : csp = Current client state (buffers, headers, etc...)
2487 * 2 : rsp = http_response data structure for output
2488 * 3 : parameters = map of cgi parameters
2490 * CGI Parameters : filename
2492 * Returns : JB_ERR_OK on success
2493 * JB_ERR_MEMORY on out-of-memory
2494 * JB_ERR_FILE if the file cannot be opened or
2496 * JB_ERR_CGI_PARAMS if "filename" was not specified
2499 *********************************************************************/
2500 jb_err cgi_edit_actions_list(struct client_state *csp,
2501 struct http_response *rsp,
2502 const struct map *parameters)
2504 char * section_template;
2505 char * url_template;
2510 struct map * exports;
2511 struct map * section_exports;
2512 struct map * url_exports;
2513 struct editable_file * file;
2514 struct file_line * cur_line;
2515 unsigned line_number = 0;
2516 unsigned prev_section_line_number = ((unsigned) (-1));
2518 struct file_list * fl;
2519 struct url_actions * b;
2520 char * buttons = NULL;
2523 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2525 return cgi_error_disabled(csp, rsp);
2528 if (NULL == (exports = default_exports(csp, NULL)))
2530 return JB_ERR_MEMORY;
2533 /* Load actions file */
2534 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2537 /* No filename specified, can't read file, or out of memory. */
2538 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2541 /* Find start of actions in file */
2542 cur_line = file->lines;
2544 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2546 cur_line = cur_line->next;
2551 * Conventional actions files should have a match all block
2553 * cur_line = {...global actions...}
2554 * cur_line->next = /
2555 * cur_line->next->next = {...actions...} or EOF
2557 if ( (cur_line != NULL)
2558 && (cur_line->type == FILE_LINE_ACTION)
2559 && (cur_line->next != NULL)
2560 && (cur_line->next->type == FILE_LINE_URL)
2561 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2562 && ( (cur_line->next->next == NULL)
2563 || (cur_line->next->next->type != FILE_LINE_URL)
2567 * Generate string with buttons to set actions for "/" to
2568 * any predefined set of actions (named standard.*, probably
2569 * residing in standard.action).
2572 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2575 edit_free_file(file);
2577 if (err == JB_ERR_FILE)
2579 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2584 err = template_fill(§ion_template, exports);
2587 edit_free_file(file);
2592 buttons = strdup("");
2593 for (i = 0; i < MAX_AF_FILES; i++)
2595 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2597 for (b = b->next; NULL != b; b = b->next)
2599 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2601 if (err || (NULL == (section_exports = new_map())))
2604 free(section_template);
2605 edit_free_file(file);
2607 return JB_ERR_MEMORY;
2610 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2612 if (err || (NULL == (s = strdup(section_template))))
2614 free_map(section_exports);
2616 free(section_template);
2617 edit_free_file(file);
2619 return JB_ERR_MEMORY;
2622 if (!err) err = template_fill(&s, section_exports);
2623 free_map(section_exports);
2624 if (!err) err = string_join(&buttons, s);
2629 freez(section_template);
2630 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2633 * Conventional actions file, supply extra editing help.
2634 * (e.g. don't allow them to make it an unconventional one).
2636 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2638 snprintf(buf, 150, "%d", line_number);
2639 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2640 snprintf(buf, 150, "%d", line_number + 2);
2641 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2642 if (!err) err = map(exports, "all-urls-actions", 1,
2643 actions_to_html(csp, cur_line->data.action), 0);
2645 /* Skip the 2 lines */
2646 cur_line = cur_line->next->next;
2650 * Note that prev_section_line_number is NOT set here.
2651 * This is deliberate and not a bug. It stops a "Move up"
2652 * option appearing on the next section. Clicking "Move
2653 * up" would make the actions file unconventional, which
2654 * we don't want, so we hide this option.
2660 * Non-standard actions file - does not begin with
2661 * the "All URLs" section.
2663 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2666 /* Set up global exports */
2668 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2669 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2671 /* Discourage private additions to default.action */
2673 if (!err) err = map_conditional(exports, "default-action",
2674 (strcmp("default", lookup(parameters, "f")) == 0));
2677 edit_free_file(file);
2682 /* Should do all global exports above this point */
2684 /* Load templates */
2686 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2689 edit_free_file(file);
2691 if (err == JB_ERR_FILE)
2693 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2698 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2701 free(section_template);
2702 edit_free_file(file);
2704 if (err == JB_ERR_FILE)
2706 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2711 err = template_fill(§ion_template, exports);
2715 edit_free_file(file);
2721 err = template_fill(&url_template, exports);
2724 free(section_template);
2725 edit_free_file(file);
2730 if (NULL == (sections = strdup("")))
2732 free(section_template);
2734 edit_free_file(file);
2736 return JB_ERR_MEMORY;
2739 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2741 if (NULL == (section_exports = new_map()))
2744 free(section_template);
2746 edit_free_file(file);
2748 return JB_ERR_MEMORY;
2751 snprintf(buf, 150, "%d", line_number);
2752 err = map(section_exports, "s", 1, buf, 1);
2753 if (!err) err = map(section_exports, "actions", 1,
2754 actions_to_html(csp, cur_line->data.action), 0);
2757 && (cur_line->next != NULL)
2758 && (cur_line->next->type == FILE_LINE_URL))
2760 /* This section contains at least one URL, don't allow delete */
2761 err = map_block_killer(section_exports, "empty-section");
2765 if (!err) err = map_block_keep(section_exports, "empty-section");
2768 if (prev_section_line_number != ((unsigned)(-1)))
2770 /* Not last section */
2771 snprintf(buf, 150, "%d", prev_section_line_number);
2772 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2773 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2778 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2780 prev_section_line_number = line_number;
2785 free(section_template);
2787 edit_free_file(file);
2789 free_map(section_exports);
2793 /* Should do all section-specific exports above this point */
2795 if (NULL == (urls = strdup("")))
2798 free(section_template);
2800 edit_free_file(file);
2802 free_map(section_exports);
2803 return JB_ERR_MEMORY;
2808 cur_line = cur_line->next;
2811 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2813 if (NULL == (url_exports = new_map()))
2817 free(section_template);
2819 edit_free_file(file);
2821 free_map(section_exports);
2822 return JB_ERR_MEMORY;
2825 snprintf(buf, 150, "%d", line_number);
2826 err = map(url_exports, "p", 1, buf, 1);
2828 snprintf(buf, 150, "%d", url_1_2);
2829 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2831 if (!err) err = map(url_exports, "url-html", 1,
2832 html_encode(cur_line->unprocessed), 0);
2833 if (!err) err = map(url_exports, "url", 1,
2834 url_encode(cur_line->unprocessed), 0);
2840 free(section_template);
2842 edit_free_file(file);
2844 free_map(section_exports);
2845 free_map(url_exports);
2849 if (NULL == (s = strdup(url_template)))
2853 free(section_template);
2855 edit_free_file(file);
2857 free_map(section_exports);
2858 free_map(url_exports);
2859 return JB_ERR_MEMORY;
2862 err = template_fill(&s, section_exports);
2863 if (!err) err = template_fill(&s, url_exports);
2864 if (!err) err = string_append(&urls, s);
2866 free_map(url_exports);
2873 free(section_template);
2875 edit_free_file(file);
2877 free_map(section_exports);
2881 url_1_2 = 3 - url_1_2;
2883 cur_line = cur_line->next;
2887 err = map(section_exports, "urls", 1, urls, 0);
2889 /* Could also do section-specific exports here, but it wouldn't be as fast */
2891 snprintf(buf, 150, "%d", line_number);
2892 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2894 if ( (cur_line != NULL)
2895 && (cur_line->type == FILE_LINE_ACTION))
2897 /* Not last section */
2898 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2903 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2909 free(section_template);
2911 edit_free_file(file);
2913 free_map(section_exports);
2917 if (NULL == (s = strdup(section_template)))
2920 free(section_template);
2922 edit_free_file(file);
2924 free_map(section_exports);
2925 return JB_ERR_MEMORY;
2928 err = template_fill(&s, section_exports);
2929 if (!err) err = string_append(§ions, s);
2932 free_map(section_exports);
2937 free(section_template);
2939 edit_free_file(file);
2945 edit_free_file(file);
2946 free(section_template);
2949 err = map(exports, "sections", 1, sections, 0);
2956 /* Could also do global exports here, but it wouldn't be as fast */
2958 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2962 /*********************************************************************
2964 * Function : cgi_edit_actions_for_url
2966 * Description : CGI function that edits the Actions list.
2969 * 1 : csp = Current client state (buffers, headers, etc...)
2970 * 2 : rsp = http_response data structure for output
2971 * 3 : parameters = map of cgi parameters
2973 * CGI Parameters : None
2975 * Returns : JB_ERR_OK on success
2976 * JB_ERR_MEMORY on out-of-memory
2977 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2978 * specified or not valid.
2980 *********************************************************************/
2981 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2982 struct http_response *rsp,
2983 const struct map *parameters)
2985 struct map * exports;
2987 struct editable_file * file;
2988 struct file_line * cur_line;
2989 unsigned line_number;
2991 struct re_filterfile_spec *filter_group;
2992 int i, have_filters = 0;
2994 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2996 return cgi_error_disabled(csp, rsp);
2999 err = get_number_param(csp, parameters, "s", §ionid);
3005 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3008 /* No filename specified, can't read file, modified, or out of memory. */
3009 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3012 cur_line = file->lines;
3014 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3016 cur_line = cur_line->next;
3019 if ( (cur_line == NULL)
3020 || (line_number != sectionid)
3022 || (cur_line->type != FILE_LINE_ACTION))
3024 /* Invalid "sectionid" parameter */
3025 edit_free_file(file);
3026 return JB_ERR_CGI_PARAMS;
3029 if (NULL == (exports = default_exports(csp, NULL)))
3031 edit_free_file(file);
3032 return JB_ERR_MEMORY;
3035 err = map(exports, "f", 1, file->identifier, 1);
3036 if (!err) err = map(exports, "v", 1, file->version_str, 1);
3037 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
3039 if (!err) err = actions_to_radio(exports, cur_line->data.action);
3042 * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
3043 * length limitation and ignore clicks on the Submit buttons if
3044 * the resulting GET URL would be longer than their limit.
3046 * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
3047 * reached this limit and action editing stopped working in these
3048 * browsers (BR #1570678).
3050 * The config option split-large-forms works around this browser
3051 * bug (HTTP has no URL lenght limitation) by deviding the action
3052 * list form into multiple smaller ones. It means the URLs are shorter
3053 * and work in broken browsers as well, but the user can no longer change
3054 * all actions with one submit.
3056 * A better solution would be to switch to POST requests,
3057 * but this will do for now.
3059 if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
3061 /* Generate multiple smaller form by killing the big one. */
3062 err = map_block_killer(exports, "one-form-only");
3066 /* Default: Generate one large form by killing the smaller ones. */
3067 err = map_block_killer(exports, "multiple-forms");
3070 for (i = 0; i < MAX_AF_FILES; i++)
3072 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3074 if (!err) err = map_conditional(exports, "any-filters-defined", 1);
3082 edit_free_file(file);
3087 if (0 == have_filters)
3088 err = map(exports, "filter-params", 1, "", 1);
3091 /* We have some entries in the filter list */
3093 int filter_identifier = 0;
3094 char * filter_template;
3096 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
3099 edit_free_file(file);
3101 if (err == JB_ERR_FILE)
3103 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
3108 err = template_fill(&filter_template, exports);
3110 result = strdup("");
3112 for (i = 0; i < MAX_AF_FILES; i++)
3114 if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
3116 filter_group = csp->rlist[i]->f;
3117 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
3119 char current_mode = 'x';
3120 struct list_entry *filter_name;
3122 struct map *line_exports;
3125 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
3126 while ((filter_name != NULL)
3127 && (0 != strcmp(filter_group->name, filter_name->str)))
3129 filter_name = filter_name->next;
3132 if (filter_name != NULL)
3138 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
3139 while ((filter_name != NULL)
3140 && (0 != strcmp(filter_group->name, filter_name->str)))
3142 filter_name = filter_name->next;
3144 if (filter_name != NULL)
3150 /* Generate a unique serial number */
3151 snprintf(number, sizeof(number), "%x", filter_identifier++);
3152 number[sizeof(number) - 1] = '\0';
3154 line_exports = new_map();
3155 if (line_exports == NULL)
3157 err = JB_ERR_MEMORY;
3162 if (!err) err = map(line_exports, "index", 1, number, 1);
3163 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3164 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3165 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3170 this_line = strdup(filter_template);
3171 if (this_line == NULL) err = JB_ERR_MEMORY;
3173 if (!err) err = template_fill(&this_line, line_exports);
3174 string_join(&result, this_line);
3176 free_map(line_exports);
3181 freez(filter_template);
3185 err = map(exports, "filter-params", 1, result, 0);
3193 if (!err) err = map_radio(exports, "filter-all", "nx",
3194 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3196 edit_free_file(file);
3204 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3208 /*********************************************************************
3210 * Function : cgi_edit_actions_submit
3212 * Description : CGI function that actually edits the Actions list.
3215 * 1 : csp = Current client state (buffers, headers, etc...)
3216 * 2 : rsp = http_response data structure for output
3217 * 3 : parameters = map of cgi parameters
3219 * CGI Parameters : None
3221 * Returns : JB_ERR_OK on success
3222 * JB_ERR_MEMORY on out-of-memory
3223 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3224 * specified or not valid.
3226 *********************************************************************/
3227 jb_err cgi_edit_actions_submit(struct client_state *csp,
3228 struct http_response *rsp,
3229 const struct map *parameters)
3235 struct editable_file * file;
3236 struct file_line * cur_line;
3237 unsigned line_number;
3240 int filter_identifier;
3241 const char * action_set_name;
3243 struct file_list * fl;
3244 struct url_actions * b;
3246 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3248 return cgi_error_disabled(csp, rsp);
3251 err = get_number_param(csp, parameters, "s", §ionid);
3257 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3260 /* No filename specified, can't read file, modified, or out of memory. */
3261 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3264 cur_line = file->lines;
3266 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3268 cur_line = cur_line->next;
3271 if ( (cur_line == NULL)
3272 || (line_number != sectionid)
3274 || (cur_line->type != FILE_LINE_ACTION))
3276 /* Invalid "sectionid" parameter */
3277 edit_free_file(file);
3278 return JB_ERR_CGI_PARAMS;
3281 get_string_param(parameters, "p", &action_set_name);
3282 if (action_set_name != NULL)
3284 for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
3286 if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
3288 for (b = b->next; NULL != b; b = b->next)
3290 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3292 copy_action(cur_line->data.action, b->action);
3298 edit_free_file(file);
3299 return JB_ERR_CGI_PARAMS;
3305 err = actions_from_radio(parameters, cur_line->data.action);
3311 edit_free_file(file);
3315 ch = get_char_param(parameters, "filter_all");
3318 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3319 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3320 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3324 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3327 for (filter_identifier = 0; !err; filter_identifier++)
3334 /* Generate the keys */
3335 snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
3336 key_value[sizeof(key_value) - 1] = '\0';
3337 snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
3338 key_name[sizeof(key_name) - 1] = '\0';
3340 err = get_string_param(parameters, key_name, &name);
3349 value = get_char_param(parameters, key_value);
3352 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3353 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3354 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3356 else if (value == 'N')
3358 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3359 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3361 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3362 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3365 else if (value == 'X')
3367 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3368 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3375 edit_free_file(file);
3379 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3382 edit_free_file(file);
3383 return JB_ERR_MEMORY;
3386 len = strlen(actiontext);
3390 * Empty action - must special-case this.
3391 * Simply setting len to 1 is sufficient...
3396 if (NULL == (newtext = malloc(len + 2)))
3400 edit_free_file(file);
3401 return JB_ERR_MEMORY;
3403 strcpy(newtext, actiontext);
3407 newtext[len + 1] = '\0';
3409 freez(cur_line->raw);
3410 freez(cur_line->unprocessed);
3411 cur_line->unprocessed = newtext;
3413 err = edit_write_file(file);
3416 /* Error writing file */
3417 if (err == JB_ERR_FILE)
3419 /* Read-only file. */
3420 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3422 edit_free_file(file);
3426 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3427 (long) time(NULL), file->identifier, sectionid);
3429 edit_free_file(file);
3431 return cgi_redirect(rsp, target);
3435 /*********************************************************************
3437 * Function : cgi_edit_actions_url
3439 * Description : CGI function that actually edits a URL pattern in
3443 * 1 : csp = Current client state (buffers, headers, etc...)
3444 * 2 : rsp = http_response data structure for output
3445 * 3 : parameters = map of cgi parameters
3448 * filename : Identifies the file to edit
3449 * ver : File's last-modified time
3450 * section : Line number of section to edit
3451 * pattern : Line number of pattern to edit
3452 * newval : New value for pattern
3454 * Returns : JB_ERR_OK on success
3455 * JB_ERR_MEMORY on out-of-memory
3456 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3457 * specified or not valid.
3459 *********************************************************************/
3460 jb_err cgi_edit_actions_url(struct client_state *csp,
3461 struct http_response *rsp,
3462 const struct map *parameters)
3466 struct editable_file * file;
3467 struct file_line * cur_line;
3468 unsigned line_number;
3469 unsigned section_start_line_number = 0;
3477 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3479 return cgi_error_disabled(csp, rsp);
3482 err = get_number_param(csp, parameters, "p", &patternid);
3489 return JB_ERR_CGI_PARAMS;
3492 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3498 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3501 /* No filename specified, can't read file, modified, or out of memory. */
3503 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3507 cur_line = file->lines;
3509 while ((cur_line != NULL) && (line_number < patternid))
3511 if (cur_line->type == FILE_LINE_ACTION)
3513 section_start_line_number = line_number;
3515 cur_line = cur_line->next;
3519 if ( (cur_line == NULL)
3520 || (cur_line->type != FILE_LINE_URL))
3522 /* Invalid "patternid" parameter */
3524 edit_free_file(file);
3525 return JB_ERR_CGI_PARAMS;
3528 /* At this point, the line to edit is in cur_line */
3530 freez(cur_line->raw);
3531 freez(cur_line->unprocessed);
3532 cur_line->unprocessed = new_pattern;
3534 err = edit_write_file(file);
3537 /* Error writing file */
3538 if (err == JB_ERR_FILE)
3540 /* Read-only file. */
3541 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3543 edit_free_file(file);
3547 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3548 (long) time(NULL), file->identifier, section_start_line_number);
3550 edit_free_file(file);
3552 return cgi_redirect(rsp, target);
3556 /*********************************************************************
3558 * Function : cgi_edit_actions_add_url
3560 * Description : CGI function that actually adds a URL pattern to
3564 * 1 : csp = Current client state (buffers, headers, etc...)
3565 * 2 : rsp = http_response data structure for output
3566 * 3 : parameters = map of cgi parameters
3569 * filename : Identifies the file to edit
3570 * ver : File's last-modified time
3571 * section : Line number of section to edit
3572 * newval : New pattern
3574 * Returns : JB_ERR_OK on success
3575 * JB_ERR_MEMORY on out-of-memory
3576 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3577 * specified or not valid.
3579 *********************************************************************/
3580 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3581 struct http_response *rsp,
3582 const struct map *parameters)
3586 struct file_line * new_line;
3587 struct editable_file * file;
3588 struct file_line * cur_line;
3589 unsigned line_number;
3593 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3595 return cgi_error_disabled(csp, rsp);
3598 err = get_number_param(csp, parameters, "s", §ionid);
3605 return JB_ERR_CGI_PARAMS;
3608 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3614 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3617 /* No filename specified, can't read file, modified, or out of memory. */
3619 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3623 cur_line = file->lines;
3625 while ((cur_line != NULL) && (line_number < sectionid))
3627 cur_line = cur_line->next;
3631 if ( (cur_line == NULL)
3632 || (cur_line->type != FILE_LINE_ACTION))
3634 /* Invalid "sectionid" parameter */
3636 edit_free_file(file);
3637 return JB_ERR_CGI_PARAMS;
3640 /* At this point, the section header is in cur_line - add after this. */
3642 /* Allocate the new line */
3643 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3644 if (new_line == NULL)
3647 edit_free_file(file);
3648 return JB_ERR_MEMORY;
3651 /* Fill in the data members of the new line */
3652 new_line->raw = NULL;
3653 new_line->prefix = NULL;
3654 new_line->unprocessed = new_pattern;
3655 new_line->type = FILE_LINE_URL;
3657 /* Link new_line into the list, after cur_line */
3658 new_line->next = cur_line->next;
3659 cur_line->next = new_line;
3661 /* Done making changes, now commit */
3663 err = edit_write_file(file);
3666 /* Error writing file */
3667 if (err == JB_ERR_FILE)
3669 /* Read-only file. */
3670 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3672 edit_free_file(file);
3676 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3677 (long) time(NULL), file->identifier, sectionid);
3679 edit_free_file(file);
3681 return cgi_redirect(rsp, target);
3685 /*********************************************************************
3687 * Function : cgi_edit_actions_remove_url
3689 * Description : CGI function that actually removes a URL pattern from
3693 * 1 : csp = Current client state (buffers, headers, etc...)
3694 * 2 : rsp = http_response data structure for output
3695 * 3 : parameters = map of cgi parameters
3698 * f : (filename) Identifies the file to edit
3699 * v : (version) File's last-modified time
3700 * p : (pattern) Line number of pattern to remove
3702 * Returns : JB_ERR_OK on success
3703 * JB_ERR_MEMORY on out-of-memory
3704 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3705 * specified or not valid.
3707 *********************************************************************/
3708 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3709 struct http_response *rsp,
3710 const struct map *parameters)
3713 struct editable_file * file;
3714 struct file_line * cur_line;
3715 struct file_line * prev_line;
3716 unsigned line_number;
3717 unsigned section_start_line_number = 0;
3721 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3723 return cgi_error_disabled(csp, rsp);
3726 err = get_number_param(csp, parameters, "p", &patternid);
3732 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3735 /* No filename specified, can't read file, modified, or out of memory. */
3736 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3741 cur_line = file->lines;
3743 while ((cur_line != NULL) && (line_number < patternid))
3745 if (cur_line->type == FILE_LINE_ACTION)
3747 section_start_line_number = line_number;
3749 prev_line = cur_line;
3750 cur_line = cur_line->next;
3754 if ( (cur_line == NULL)
3755 || (prev_line == NULL)
3756 || (cur_line->type != FILE_LINE_URL))
3758 /* Invalid "patternid" parameter */
3759 edit_free_file(file);
3760 return JB_ERR_CGI_PARAMS;
3763 /* At this point, the line to remove is in cur_line, and the previous
3764 * one is in prev_line
3767 /* Unlink cur_line */
3768 prev_line->next = cur_line->next;
3769 cur_line->next = NULL;
3772 edit_free_file_lines(cur_line);
3774 err = edit_write_file(file);
3777 /* Error writing file */
3778 if (err == JB_ERR_FILE)
3780 /* Read-only file. */
3781 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3783 edit_free_file(file);
3787 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
3788 (long) time(NULL), file->identifier, section_start_line_number);
3790 edit_free_file(file);
3792 return cgi_redirect(rsp, target);
3796 /*********************************************************************
3798 * Function : cgi_edit_actions_section_remove
3800 * Description : CGI function that actually removes a whole section from
3801 * the actions file. The section must be empty first
3802 * (else JB_ERR_CGI_PARAMS).
3805 * 1 : csp = Current client state (buffers, headers, etc...)
3806 * 2 : rsp = http_response data structure for output
3807 * 3 : parameters = map of cgi parameters
3810 * f : (filename) Identifies the file to edit
3811 * v : (version) File's last-modified time
3812 * s : (section) Line number of section to edit
3814 * Returns : JB_ERR_OK on success
3815 * JB_ERR_MEMORY on out-of-memory
3816 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3817 * specified or not valid.
3819 *********************************************************************/
3820 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3821 struct http_response *rsp,
3822 const struct map *parameters)
3825 struct editable_file * file;
3826 struct file_line * cur_line;
3827 struct file_line * prev_line;
3828 unsigned line_number;
3832 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3834 return cgi_error_disabled(csp, rsp);
3837 err = get_number_param(csp, parameters, "s", §ionid);
3843 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3846 /* No filename specified, can't read file, modified, or out of memory. */
3847 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3851 cur_line = file->lines;
3854 while ((cur_line != NULL) && (line_number < sectionid))
3856 prev_line = cur_line;
3857 cur_line = cur_line->next;
3861 if ( (cur_line == NULL)
3862 || (cur_line->type != FILE_LINE_ACTION) )
3864 /* Invalid "sectionid" parameter */
3865 edit_free_file(file);
3866 return JB_ERR_CGI_PARAMS;
3869 if ( (cur_line->next != NULL)
3870 && (cur_line->next->type == FILE_LINE_URL) )
3872 /* Section not empty. */
3873 edit_free_file(file);
3874 return JB_ERR_CGI_PARAMS;
3877 /* At this point, the line to remove is in cur_line, and the previous
3878 * one is in prev_line
3881 /* Unlink cur_line */
3882 if (prev_line == NULL)
3884 /* Removing the first line from the file */
3885 file->lines = cur_line->next;
3889 prev_line->next = cur_line->next;
3891 cur_line->next = NULL;
3894 edit_free_file_lines(cur_line);
3896 err = edit_write_file(file);
3899 /* Error writing file */
3900 if (err == JB_ERR_FILE)
3902 /* Read-only file. */
3903 err = cgi_error_file_read_only(csp, rsp, file->identifier);
3905 edit_free_file(file);
3909 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
3910 (long) time(NULL), file->identifier);
3912 edit_free_file(file);
3914 return cgi_redirect(rsp, target);
3918 /*********************************************************************
3920 * Function : cgi_edit_actions_section_add
3922 * Description : CGI function that adds a new empty section to
3926 * 1 : csp = Current client state (buffers, headers, etc...)
3927 * 2 : rsp = http_response data structure for output
3928 * 3 : parameters = map of cgi parameters
3931 * f : (filename) Identifies the file to edit
3932 * v : (version) File's last-modified time
3933 * s : (section) Line number of section to add after, 0 for
3936 * Returns : JB_ERR_OK on success
3937 * JB_ERR_MEMORY on out-of-memory
3938 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3939 * specified or not valid.
3941 *********************************************************************/
3942 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3943 struct http_response *rsp,
3944 const struct map *parameters)
3947 struct file_line * new_line;
3949 struct editable_file * file;
3950 struct file_line * cur_line;
3951 unsigned line_number;
3955 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3957 return cgi_error_disabled(csp, rsp);
3960 err = get_number_param(csp, parameters, "s", §ionid);
3966 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3969 /* No filename specified, can't read file, modified, or out of memory. */
3970 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3974 cur_line = file->lines;
3976 if (sectionid <= 1U)
3978 /* Add to start of file */
3979 if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
3981 /* There's something in the file, find the line before the first
3984 while ( (cur_line->next != NULL)
3985 && (cur_line->next->type != FILE_LINE_ACTION) )
3987 cur_line = cur_line->next;
3993 /* File starts with action line, so insert at top */
3999 /* Add after stated section. */
4000 while ((cur_line != NULL) && (line_number < sectionid))
4002 cur_line = cur_line->next;
4006 if ( (cur_line == NULL)
4007 || (cur_line->type != FILE_LINE_ACTION))
4009 /* Invalid "sectionid" parameter */
4010 edit_free_file(file);
4011 return JB_ERR_CGI_PARAMS;
4014 /* Skip through the section to find the last line in it. */
4015 while ( (cur_line->next != NULL)
4016 && (cur_line->next->type != FILE_LINE_ACTION) )
4018 cur_line = cur_line->next;
4023 /* At this point, the last line in the previous section is in cur_line
4024 * - add after this. (Or if we need to add as the first line, cur_line
4028 new_text = strdup("{}");
4029 if (NULL == new_text)
4031 edit_free_file(file);
4032 return JB_ERR_MEMORY;
4035 /* Allocate the new line */
4036 new_line = (struct file_line *)zalloc(sizeof(*new_line));
4037 if (new_line == NULL)
4040 edit_free_file(file);
4041 return JB_ERR_MEMORY;
4044 /* Fill in the data members of the new line */
4045 new_line->raw = NULL;
4046 new_line->prefix = NULL;
4047 new_line->unprocessed = new_text;
4048 new_line->type = FILE_LINE_ACTION;
4050 if (cur_line != NULL)
4052 /* Link new_line into the list, after cur_line */
4053 new_line->next = cur_line->next;
4054 cur_line->next = new_line;
4058 /* Link new_line into the list, as first line */
4059 new_line->next = file->lines;
4060 file->lines = new_line;
4063 /* Done making changes, now commit */
4065 err = edit_write_file(file);
4068 /* Error writing file */
4069 if (err == JB_ERR_FILE)
4071 /* Read-only file. */
4072 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4074 edit_free_file(file);
4078 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4079 (long) time(NULL), file->identifier);
4081 edit_free_file(file);
4083 return cgi_redirect(rsp, target);
4087 /*********************************************************************
4089 * Function : cgi_edit_actions_section_swap
4091 * Description : CGI function that swaps the order of two sections
4092 * in the actions file. Note that this CGI can actually
4093 * swap any two arbitrary sections, but the GUI interface
4094 * currently only allows consecutive sections to be
4098 * 1 : csp = Current client state (buffers, headers, etc...)
4099 * 2 : rsp = http_response data structure for output
4100 * 3 : parameters = map of cgi parameters
4103 * f : (filename) Identifies the file to edit
4104 * v : (version) File's last-modified time
4105 * s1 : (section1) Line number of first section to swap
4106 * s2 : (section2) Line number of second section to swap
4108 * Returns : JB_ERR_OK on success
4109 * JB_ERR_MEMORY on out-of-memory
4110 * JB_ERR_CGI_PARAMS if the CGI parameters are not
4111 * specified or not valid.
4113 *********************************************************************/
4114 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4115 struct http_response *rsp,
4116 const struct map *parameters)
4120 struct editable_file * file;
4121 struct file_line * cur_line;
4122 struct file_line * prev_line;
4123 struct file_line * line_before_section1;
4124 struct file_line * line_start_section1;
4125 struct file_line * line_end_section1;
4126 struct file_line * line_after_section1;
4127 struct file_line * line_before_section2;
4128 struct file_line * line_start_section2;
4129 struct file_line * line_end_section2;
4130 struct file_line * line_after_section2;
4131 unsigned line_number;
4135 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4137 return cgi_error_disabled(csp, rsp);
4140 err = get_number_param(csp, parameters, "s1", §ion1);
4141 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4147 if (section1 > section2)
4149 unsigned temp = section2;
4150 section2 = section1;
4154 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4157 /* No filename specified, can't read file, modified, or out of memory. */
4158 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4161 /* Start at the beginning... */
4163 cur_line = file->lines;
4166 /* ... find section1 ... */
4167 while ((cur_line != NULL) && (line_number < section1))
4169 prev_line = cur_line;
4170 cur_line = cur_line->next;
4174 if ( (cur_line == NULL)
4175 || (cur_line->type != FILE_LINE_ACTION) )
4177 /* Invalid "section1" parameter */
4178 edit_free_file(file);
4179 return JB_ERR_CGI_PARAMS;
4182 /* If no-op, we've validated params and can skip the rest. */
4183 if (section1 != section2)
4185 /* ... find the end of section1 ... */
4186 line_before_section1 = prev_line;
4187 line_start_section1 = cur_line;
4190 prev_line = cur_line;
4191 cur_line = cur_line->next;
4194 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4195 line_end_section1 = prev_line;
4196 line_after_section1 = cur_line;
4198 /* ... find section2 ... */
4199 while ((cur_line != NULL) && (line_number < section2))
4201 prev_line = cur_line;
4202 cur_line = cur_line->next;
4206 if ( (cur_line == NULL)
4207 || (cur_line->type != FILE_LINE_ACTION) )
4209 /* Invalid "section2" parameter */
4210 edit_free_file(file);
4211 return JB_ERR_CGI_PARAMS;
4214 /* ... find the end of section2 ... */
4215 line_before_section2 = prev_line;
4216 line_start_section2 = cur_line;
4219 prev_line = cur_line;
4220 cur_line = cur_line->next;
4223 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4224 line_end_section2 = prev_line;
4225 line_after_section2 = cur_line;
4227 /* Now have all the pointers we need. Do the swap. */
4229 /* Change the pointer to section1 to point to section2 instead */
4230 if (line_before_section1 == NULL)
4232 file->lines = line_start_section2;
4236 line_before_section1->next = line_start_section2;
4239 if (line_before_section2 == line_end_section1)
4241 /* Consecutive sections */
4242 line_end_section2->next = line_start_section1;
4246 line_end_section2->next = line_after_section1;
4247 line_before_section2->next = line_start_section1;
4250 /* Set the pointer from the end of section1 to the rest of the file */
4251 line_end_section1->next = line_after_section2;
4253 err = edit_write_file(file);
4256 /* Error writing file */
4257 if (err == JB_ERR_FILE)
4259 /* Read-only file. */
4260 err = cgi_error_file_read_only(csp, rsp, file->identifier);
4262 edit_free_file(file);
4265 } /* END if (section1 != section2) */
4267 snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
4268 (long) time(NULL), file->identifier);
4270 edit_free_file(file);
4272 return cgi_redirect(rsp, target);
4275 #ifdef FEATURE_TOGGLE
4276 /*********************************************************************
4278 * Function : cgi_toggle
4280 * Description : CGI function that adds a new empty section to
4284 * 1 : csp = Current client state (buffers, headers, etc...)
4285 * 2 : rsp = http_response data structure for output
4286 * 3 : parameters = map of cgi parameters
4289 * set : If present, how to change toggle setting:
4290 * "enable", "disable", "toggle", or none (default).
4291 * mini : If present, use mini reply template.
4293 * Returns : JB_ERR_OK on success
4294 * JB_ERR_MEMORY on out-of-memory
4296 *********************************************************************/
4297 jb_err cgi_toggle(struct client_state *csp,
4298 struct http_response *rsp,
4299 const struct map *parameters)
4301 struct map *exports;
4303 const char *template_name;
4309 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4311 return cgi_error_disabled(csp, rsp);
4314 mode = get_char_param(parameters, "set");
4319 global_toggle_state = 1;
4321 else if (mode == 'D')
4324 global_toggle_state = 0;
4326 else if (mode == 'T')
4329 global_toggle_state = !global_toggle_state;
4332 if (NULL == (exports = default_exports(csp, "toggle")))
4334 return JB_ERR_MEMORY;
4337 template_name = (get_char_param(parameters, "mini")
4341 return template_fill_for_cgi(csp, template_name, exports, rsp);
4343 #endif /* def FEATURE_TOGGLE */
4345 /*********************************************************************
4347 * Function : javascriptify
4349 * Description : Converts a string into a form JavaScript will like.
4351 * Netscape 4's JavaScript sucks - it doesn't use
4352 * "id" parameters, so you have to set the "name"
4353 * used to submit a form element to something JavaScript
4354 * will like. (Or access the elements by index in an
4355 * array. That array contains >60 elements and will
4356 * be changed whenever we add a new action to the
4357 * editor, so I'm NOT going to use indexes that have
4358 * to be figured out by hand.)
4360 * Currently the only thing we have to worry about
4361 * is "-" ==> "_" conversion.
4363 * This is a length-preserving operation so it is
4364 * carried out in-place, no memory is allocated
4368 * 1 : identifier = String to make JavaScript-friendly.
4372 *********************************************************************/
4373 static void javascriptify(char * identifier)
4375 char * p = identifier;
4376 while (NULL != (p = strchr(p, '-')))
4383 /*********************************************************************
4385 * Function : actions_to_radio
4387 * Description : Converts a actionsfile entry into settings for
4388 * radio buttons and edit boxes on a HTML form.
4391 * 1 : exports = List of substitutions to add to.
4392 * 2 : action = Action to read
4394 * Returns : JB_ERR_OK on success
4395 * JB_ERR_MEMORY on out-of-memory
4397 *********************************************************************/
4398 static jb_err actions_to_radio(struct map * exports,
4399 const struct action_spec *action)
4401 unsigned mask = action->mask;
4402 unsigned add = action->add;
4410 mask = action->mask;
4413 /* sanity - prevents "-feature +feature" */
4417 #define DEFINE_ACTION_BOOL(name, bit) \
4418 if (!(mask & bit)) \
4420 current_mode = 'n'; \
4422 else if (add & bit) \
4424 current_mode = 'y'; \
4428 current_mode = 'x'; \
4430 if (map_radio(exports, name, "ynx", current_mode)) \
4432 return JB_ERR_MEMORY; \
4435 #define DEFINE_ACTION_STRING(name, bit, index) \
4436 DEFINE_ACTION_BOOL(name, bit); \
4439 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4442 checked = !strcmp(action->string[index], value); \
4446 checked = is_default; \
4448 mapped_param |= checked; \
4449 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4451 return JB_ERR_MEMORY; \
4454 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4455 if (map(exports, name "-param-custom", 1, \
4456 ((!mapped_param) ? "checked" : ""), 1)) \
4458 return JB_ERR_MEMORY; \
4460 if (map(exports, name "-param", 1, \
4461 (((add & bit) && !mapped_param) ? \
4462 action->string[index] : default_val), 1)) \
4464 return JB_ERR_MEMORY; \
4467 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4468 if (map(exports, name "-param", 1, \
4469 ((add & bit) ? action->string[index] : default_val), 1)) \
4471 return JB_ERR_MEMORY; \
4474 #define DEFINE_ACTION_MULTI(name, index) \
4475 if (action->multi_add[index]->first) \
4477 current_mode = 'y'; \
4479 else if (action->multi_remove_all[index]) \
4481 current_mode = 'n'; \
4483 else if (action->multi_remove[index]->first) \
4485 current_mode = 'y'; \
4489 current_mode = 'x'; \
4491 if (map_radio(exports, name, "ynx", current_mode)) \
4493 return JB_ERR_MEMORY; \
4496 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4498 #include "actionlist.h"
4500 #undef DEFINE_ACTION_MULTI
4501 #undef DEFINE_ACTION_STRING
4502 #undef DEFINE_ACTION_BOOL
4503 #undef DEFINE_ACTION_ALIAS
4504 #undef DEFINE_CGI_PARAM_CUSTOM
4505 #undef DEFINE_CGI_PARAM_RADIO
4506 #undef DEFINE_CGI_PARAM_NO_RADIO
4512 /*********************************************************************
4514 * Function : actions_from_radio
4516 * Description : Converts a map of parameters passed to a CGI function
4517 * into an actionsfile entry.
4520 * 1 : parameters = parameters to the CGI call
4521 * 2 : action = Action to change. Must be valid before
4522 * the call, actions not specified will be
4525 * Returns : JB_ERR_OK on success
4526 * JB_ERR_MEMORY on out-of-memory
4528 *********************************************************************/
4529 static jb_err actions_from_radio(const struct map * parameters,
4530 struct action_spec *action)
4535 const char * js_name;
4536 jb_err err = JB_ERR_OK;
4541 /* Statics are generally a potential race condition,
4542 * but in this case we're safe and don't need semaphores.
4543 * Be careful if you modify this function.
4545 * The js_name_arr's are never free()d, but this is no
4546 * problem, since they will only be created once and
4547 * used by all threads thereafter. -oes
4550 #define JAVASCRIPTIFY(dest_var, string) \
4552 static int first_time = 1; \
4553 static char *js_name_arr; \
4556 js_name_arr = strdup(string); \
4557 javascriptify(js_name_arr); \
4559 dest_var = js_name_arr; \
4563 #define DEFINE_ACTION_BOOL(name, bit) \
4564 JAVASCRIPTIFY(js_name, name); \
4565 ch = get_char_param(parameters, js_name); \
4568 action->add |= bit; \
4569 action->mask |= bit; \
4571 else if (ch == 'N') \
4573 action->add &= ~bit; \
4574 action->mask &= ~bit; \
4576 else if (ch == 'X') \
4578 action->add &= ~bit; \
4579 action->mask |= bit; \
4582 #define DEFINE_ACTION_STRING(name, bit, index) \
4583 JAVASCRIPTIFY(js_name, name); \
4584 ch = get_char_param(parameters, js_name); \
4588 JAVASCRIPTIFY(js_name, name "-mode"); \
4589 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4590 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4592 JAVASCRIPTIFY(js_name, name "-param"); \
4593 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4595 if (param != NULL) \
4597 if (NULL == (param_dup = strdup(param))) \
4599 return JB_ERR_MEMORY; \
4601 freez(action->string[index]); \
4602 action->add |= bit; \
4603 action->mask |= bit; \
4604 action->string[index] = param_dup; \
4607 else if (ch == 'N') \
4609 if (action->add & bit) \
4611 freez(action->string[index]); \
4613 action->add &= ~bit; \
4614 action->mask &= ~bit; \
4616 else if (ch == 'X') \
4618 if (action->add & bit) \
4620 freez(action->string[index]); \
4622 action->add &= ~bit; \
4623 action->mask |= bit; \
4626 #define DEFINE_ACTION_MULTI(name, index) \
4627 JAVASCRIPTIFY(js_name, name); \
4628 ch = get_char_param(parameters, js_name); \
4633 else if (ch == 'N') \
4635 list_remove_all(action->multi_add[index]); \
4636 list_remove_all(action->multi_remove[index]); \
4637 action->multi_remove_all[index] = 1; \
4639 else if (ch == 'X') \
4641 list_remove_all(action->multi_add[index]); \
4642 list_remove_all(action->multi_remove[index]); \
4643 action->multi_remove_all[index] = 0; \
4646 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4648 #include "actionlist.h"
4650 #undef DEFINE_ACTION_MULTI
4651 #undef DEFINE_ACTION_STRING
4652 #undef DEFINE_ACTION_BOOL
4653 #undef DEFINE_ACTION_ALIAS
4654 #undef JAVASCRIPTIFY
4660 #endif /* def FEATURE_CGI_EDIT_ACTIONS */