1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.38 2002/05/03 23:00:38 jongfoster 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.38 2002/05/03 23:00:38 jongfoster
46 * Support for templates for "standard actions" buttons.
49 * Revision 1.37 2002/04/30 11:14:52 oes
50 * Made csp the first parameter in *action_to_html
52 * Revision 1.36 2002/04/26 21:53:30 jongfoster
53 * Fixing a memory leak. (Near, but not caused by, my earlier commit).
55 * Revision 1.35 2002/04/26 21:50:02 jongfoster
56 * Honouring default exports in edit-actions-for-url-filter template.
58 * Revision 1.34 2002/04/26 12:54:17 oes
59 * Adaptions to changes in actions.c
61 * Revision 1.33 2002/04/24 02:17:47 oes
62 * - Moved get_char_param, get_string_param and get_number_param to cgi.c
64 * - Activated Jon's code for editing multiple AFs
65 * - cgi_edit_list_actions now provides context-sensitive
66 * help, looks up all action sets from standard.action and
67 * makes buttons for them in the catchall section
68 * - cgi_edit_action_submit now honors a p parameter, looks up
69 * the corresponding action set, and sets the catchall pattern's
70 * actions accordingly.
72 * Revision 1.32 2002/04/19 16:55:31 jongfoster
73 * Fixing newline problems. If we do our own text file newline
74 * mangling, we don't want the library to do any, so we need to
75 * open the files in *binary* mode.
77 * Revision 1.31 2002/04/18 19:21:08 jongfoster
78 * Added code to detect "conventional" action files, that start
79 * with a set of actions for all URLs (the pattern "/").
80 * These are special-cased in the "edit-actions-list" CGI, so
81 * that a special UI can be written for them.
83 * Revision 1.30 2002/04/10 13:38:35 oes
84 * load_template signature changed
86 * Revision 1.29 2002/04/08 16:59:08 oes
89 * Revision 1.28 2002/03/27 12:30:29 oes
90 * Deleted unsused variable
92 * Revision 1.27 2002/03/26 23:06:04 jongfoster
93 * Removing duplicate @ifs on the toggle page
95 * Revision 1.26 2002/03/26 22:59:17 jongfoster
96 * Fixing /toggle to display status consistently.
98 * Revision 1.25 2002/03/26 22:29:54 swa
99 * we have a new homepage!
101 * Revision 1.24 2002/03/24 15:23:33 jongfoster
104 * Revision 1.23 2002/03/24 13:32:41 swa
105 * name change related issues
107 * Revision 1.22 2002/03/24 13:25:43 swa
108 * name change related issues
110 * Revision 1.21 2002/03/22 18:02:48 jongfoster
111 * Fixing remote toggle
113 * Revision 1.20 2002/03/16 20:28:34 oes
114 * Added descriptions to the filters so users will know what they select in the cgi editor
116 * Revision 1.19 2002/03/16 18:38:14 jongfoster
117 * Stopping stupid or malicious users from breaking the actions
118 * file using the web-based editor.
120 * Revision 1.18 2002/03/16 14:57:44 jongfoster
121 * Full support for enabling/disabling modular filters.
123 * Revision 1.17 2002/03/16 14:26:42 jongfoster
124 * First version of modular filters support - READ ONLY!
125 * Fixing a double-free bug in the out-of-memory handling in map_radio().
127 * Revision 1.16 2002/03/07 03:46:17 oes
128 * Fixed compiler warnings
130 * Revision 1.15 2002/03/06 22:54:35 jongfoster
131 * Automated function-comment nitpicking.
133 * Revision 1.14 2002/03/05 00:24:51 jongfoster
134 * Patch to always edit the current actions file.
136 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
137 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
139 * Revision 1.12 2002/03/03 09:18:03 joergs
140 * Made jumbjuster work on AmigaOS again.
142 * Revision 1.11 2002/01/23 01:03:31 jongfoster
143 * Fixing gcc [CygWin] compiler warnings
145 * Revision 1.10 2002/01/23 00:22:59 jongfoster
146 * Adding new function cgi_edit_actions_section_swap(), to reorder
149 * Adding get_url_spec_param() to get a validated URL pattern.
151 * Moving edit_read_line() out of this file and into loaders.c.
153 * Adding missing html_encode() to many CGI functions.
155 * Moving the functions that #include actionlist.h to the end of the file,
156 * because the Visual C++ 97 debugger gets extremely confused if you try
157 * to debug any code that comes after them in the file.
159 * Major optimizations in cgi_edit_actions_list() to reduce the size of
160 * the generated HTML (down 40% from 550k to 304k), with major side-effects
161 * throughout the editor and templates. In particular, the length of the
162 * URLs throughout the editor has been drastically reduced, by cutting
163 * paramater names down to 1 character and CGI names down to 3-4
164 * characters, by removing all non-essential CGI paramaters even at the
165 * expense of having to re-read the actions file for the most trivial
166 * page, and by using relative rather than absolute URLs. This means
167 * that this (typical example):
169 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
170 * filename=ijb&ver=1011487572&section=12&pattern=13
171 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
175 * <a href="eau?f=ijb&v=1011487572&p=13">
177 * Revision 1.9 2002/01/17 20:56:22 jongfoster
178 * Replacing hard references to the URL of the config interface
179 * with #defines from project.h
181 * Revision 1.8 2001/11/30 23:35:51 jongfoster
182 * Renaming actionsfile to ijb.action
184 * Revision 1.7 2001/11/13 00:28:24 jongfoster
185 * - Renaming parameters from edit-actions-for-url so that they only
186 * contain legal JavaScript characters. If we wanted to write
187 * JavaScript that worked with Netscape 4, this is nessacery.
188 * (Note that at the moment the JavaScript doesn't actually work
189 * with Netscape 4, but now this is purely a template issue, not
190 * one affecting code).
191 * - Adding new CGIs for use by non-JavaScript browsers:
192 * edit-actions-url-form
193 * edit-actions-add-url-form
194 * edit-actions-remove-url-form
197 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
198 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
199 * by and __OS2__ ifdef.
201 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
202 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
203 * threads to call select() simultaneously. So, it's time to do a real, live,
204 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
205 * (native). Both versions will work, but using __OS2__ offers multi-threading.
207 * Revision 1.4 2001/10/23 21:48:19 jongfoster
208 * Cleaning up error handling in CGI functions - they now send back
209 * a HTML error page and should never cause a FATAL error. (Fixes one
210 * potential source of "denial of service" attacks).
212 * CGI actions file editor that works and is actually useful.
214 * Ability to toggle JunkBuster remotely using a CGI call.
216 * You can turn off both the above features in the main configuration
217 * file, e.g. if you are running a multi-user proxy.
219 * Revision 1.3 2001/10/14 22:12:49 jongfoster
220 * New version of CGI-based actionsfile editor.
221 * Major changes, including:
222 * - Completely new file parser and file output routines
223 * - edit-actions CGI renamed edit-actions-for-url
224 * - All CGIs now need a filename parameter, except for...
225 * - New CGI edit-actions which doesn't need a filename,
226 * to allow you to start the editor up.
227 * - edit-actions-submit now works, and now automatically
228 * redirects you back to the main edit-actions-list handler.
230 * Revision 1.2 2001/09/16 17:05:14 jongfoster
231 * Removing unused #include showarg.h
233 * Revision 1.1 2001/09/16 15:47:37 jongfoster
234 * First version of CGI-based edit interface. This is very much a
235 * work-in-progress, and you can't actually use it to edit anything
236 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
237 * to have any effect.
240 **********************************************************************/
246 * FIXME: Following includes copied from cgi.c - which are actually needed?
251 #include <sys/types.h>
255 #include <sys/stat.h>
258 #define snprintf _snprintf
259 #endif /* def _WIN32 */
264 #include "cgisimple.h"
268 #include "miscutil.h"
272 /* loadcfg.h is for g_bToggleIJB only */
273 #include "urlmatch.h"
275 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
278 #ifdef FEATURE_CGI_EDIT_ACTIONS
281 * A line in an editable_file.
285 /** Next entry in the linked list */
286 struct file_line * next;
288 /** The raw data, to write out if this line is unmodified. */
291 /** Comments and/or whitespace to put before this line if it's modified
292 and then written out. */
295 /** The actual data, as a string. Line continuation and comment removal
296 are performed on the data read from file before it's stored here, so
297 it will be a single line of data. */
300 /** The type of data on this line. One of the FILE_LINE_xxx constants. */
303 /** The actual data, processed into some sensible data type. */
307 /** An action specification. */
308 struct action_spec action[1];
310 /** A name=value pair. */
314 /** The name in the name=value pair. */
317 /** The value in the name=value pair, as a string. */
320 /** The value in the name=value pair, as an integer. */
325 /* Add more data types here... e.g.
328 struct url_spec url[1];
332 struct action_spec action[1];
342 /** This file_line has not been processed yet. */
343 #define FILE_LINE_UNPROCESSED 1
345 /** This file_line is blank. Can only appear at the end of a file, due to
346 the way the parser works. */
347 #define FILE_LINE_BLANK 2
349 /** This file_line says {{alias}}. */
350 #define FILE_LINE_ALIAS_HEADER 3
352 /** This file_line defines an alias. */
353 #define FILE_LINE_ALIAS_ENTRY 4
355 /** This file_line defines an {action}. */
356 #define FILE_LINE_ACTION 5
358 /** This file_line specifies a URL pattern. */
359 #define FILE_LINE_URL 6
361 /** This file_line says {{settings}}. */
362 #define FILE_LINE_SETTINGS_HEADER 7
364 /** This file_line is in a {{settings}} block. */
365 #define FILE_LINE_SETTINGS_ENTRY 8
367 /** This file_line says {{description}}. */
368 #define FILE_LINE_DESCRIPTION_HEADER 9
370 /** This file_line is in a {{description}} block. */
371 #define FILE_LINE_DESCRIPTION_ENTRY 10
375 * A configuration file, in a format that can be edited and written back to
380 struct file_line * lines; /**< The contents of the file. A linked list of lines. */
381 const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
382 const char * identifier; /**< Filename stub - e.g. "wibble". Use for CGI param. */
383 /**< Pre-encoded with url_encode() for ease of use. */
384 const char * version_str; /**< Last modification time, as a string. For CGI param. */
385 /**< Can be used in URL without using url_param(). */
386 unsigned version; /**< Last modification time - prevents chaos with
387 the browser's "back" button. Note that this is a
388 time_t cast to an unsigned. When comparing, always
389 cast the time_t to an unsigned, and *NOT* vice-versa.
390 This may lose the top few bits, but they're not
391 significant anyway. */
392 int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
393 Note that changing this after the file has been
394 read in will cause a mess. */
395 struct file_line * parse_error; /**< On parse error, this is the offending line. */
396 const char * parse_error_text; /**< On parse error, this is the problem.
397 (Statically allocated) */
400 /* FIXME: Following non-static functions should be prototyped in .h or made static */
402 /* Functions to read and write arbitrary config files */
403 jb_err edit_read_file(struct client_state *csp,
404 const struct map *parameters,
407 struct editable_file **pfile);
408 jb_err edit_write_file(struct editable_file * file);
409 void edit_free_file(struct editable_file * file);
411 /* Functions to read and write actions files */
412 jb_err edit_parse_actions_file(struct editable_file * file);
413 jb_err edit_read_actions_file(struct client_state *csp,
414 struct http_response *rsp,
415 const struct map *parameters,
417 struct editable_file **pfile);
420 jb_err cgi_error_modified(struct client_state *csp,
421 struct http_response *rsp,
422 const char *filename);
423 jb_err cgi_error_parse(struct client_state *csp,
424 struct http_response *rsp,
425 struct editable_file *file);
426 jb_err cgi_error_file(struct client_state *csp,
427 struct http_response *rsp,
428 const char *filename);
429 jb_err cgi_error_disabled(struct client_state *csp,
430 struct http_response *rsp);
432 /* Internal arbitrary config file support functions */
433 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
434 static void edit_free_file_lines(struct file_line * first_line);
436 /* Internal actions file support functions */
437 static int match_actions_file_header_line(const char * line, const char * name);
438 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
440 /* Internal parameter parsing functions */
441 static jb_err get_file_name_param(struct client_state *csp,
442 const struct map *parameters,
443 const char *param_name,
446 const char **pparam);
448 static jb_err get_url_spec_param(struct client_state *csp,
449 const struct map *parameters,
454 /* Internal actionsfile <==> HTML conversion functions */
455 static jb_err map_radio(struct map * exports,
456 const char * optionname,
459 static jb_err actions_to_radio(struct map * exports,
460 const struct action_spec *action);
461 static jb_err actions_from_radio(const struct map * parameters,
462 struct action_spec *action);
465 static jb_err map_copy_parameter_html(struct map *out,
466 const struct map *in,
468 #if 0 /* unused function */
469 static jb_err map_copy_parameter_url(struct map *out,
470 const struct map *in,
472 #endif /* unused function */
474 /*********************************************************************
476 * Function : map_copy_parameter_html
478 * Description : Copy a CGI parameter from one map to another, HTML
482 * 1 : out = target map
483 * 2 : in = source map
484 * 3 : name = name of cgi parameter to copy
486 * Returns : JB_ERR_OK on success
487 * JB_ERR_MEMORY on out-of-memory
488 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
491 *********************************************************************/
492 static jb_err map_copy_parameter_html(struct map *out,
493 const struct map *in,
503 value = lookup(in, name);
504 err = map(out, name, 1, html_encode(value), 0);
511 else if (*value == '\0')
513 return JB_ERR_CGI_PARAMS;
522 #if 0 /* unused function */
523 /*********************************************************************
525 * Function : map_copy_parameter_url
527 * Description : Copy a CGI parameter from one map to another, URL
531 * 1 : out = target map
532 * 2 : in = source map
533 * 3 : name = name of cgi parameter to copy
535 * Returns : JB_ERR_OK on success
536 * JB_ERR_MEMORY on out-of-memory
537 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
540 *********************************************************************/
541 static jb_err map_copy_parameter_url(struct map *out,
542 const struct map *in,
552 value = lookup(in, name);
553 err = map(out, name, 1, url_encode(value), 0);
560 else if (*value == '\0')
562 return JB_ERR_CGI_PARAMS;
569 #endif /* 0 - unused function */
571 /*********************************************************************
573 * Function : cgi_edit_actions_url_form
575 * Description : CGI function that displays a form for
579 * 1 : csp = Current client state (buffers, headers, etc...)
580 * 2 : rsp = http_response data structure for output
581 * 3 : parameters = map of cgi parameters
584 * f : (filename) Identifies the file to edit
585 * v : (version) File's last-modified time
586 * p : (pattern) Line number of pattern to edit
588 * Returns : JB_ERR_OK on success
589 * JB_ERR_MEMORY on out-of-memory
590 * JB_ERR_CGI_PARAMS if the CGI parameters are not
591 * specified or not valid.
593 *********************************************************************/
594 jb_err cgi_edit_actions_url_form(struct client_state *csp,
595 struct http_response *rsp,
596 const struct map *parameters)
598 struct map * exports;
600 struct editable_file * file;
601 struct file_line * cur_line;
602 unsigned line_number;
609 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
611 return cgi_error_disabled(csp, rsp);
614 err = get_number_param(csp, parameters, "p", &patternid);
620 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
623 /* No filename specified, can't read file, modified, or out of memory. */
624 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
627 cur_line = file->lines;
629 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
631 cur_line = cur_line->next;
634 if ( (cur_line == NULL)
635 || (line_number != patternid)
637 || (cur_line->type != FILE_LINE_URL))
639 /* Invalid "patternid" parameter */
640 edit_free_file(file);
641 return JB_ERR_CGI_PARAMS;
644 if (NULL == (exports = default_exports(csp, NULL)))
646 edit_free_file(file);
647 return JB_ERR_MEMORY;
650 err = map(exports, "f", 1, file->identifier, 1);
651 if (!err) err = map(exports, "v", 1, file->version_str, 1);
652 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
653 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
655 edit_free_file(file);
663 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
667 /*********************************************************************
669 * Function : cgi_edit_actions_add_url_form
671 * Description : CGI function that displays a form for
675 * 1 : csp = Current client state (buffers, headers, etc...)
676 * 2 : rsp = http_response data structure for output
677 * 3 : parameters = map of cgi parameters
680 * f : (filename) Identifies the file to edit
681 * v : (version) File's last-modified time
682 * s : (section) Line number of section to edit
684 * Returns : JB_ERR_OK on success
685 * JB_ERR_MEMORY on out-of-memory
686 * JB_ERR_CGI_PARAMS if the CGI parameters are not
687 * specified or not valid.
689 *********************************************************************/
690 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
691 struct http_response *rsp,
692 const struct map *parameters)
701 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
703 return cgi_error_disabled(csp, rsp);
706 if (NULL == (exports = default_exports(csp, NULL)))
708 return JB_ERR_MEMORY;
711 err = map_copy_parameter_html(exports, parameters, "f");
712 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
713 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
721 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
725 /*********************************************************************
727 * Function : cgi_edit_actions_remove_url_form
729 * Description : CGI function that displays a form for
733 * 1 : csp = Current client state (buffers, headers, etc...)
734 * 2 : rsp = http_response data structure for output
735 * 3 : parameters = map of cgi parameters
738 * f : (filename) Identifies the file to edit
739 * v : (version) File's last-modified time
740 * p : (pattern) Line number of pattern to edit
742 * Returns : JB_ERR_OK on success
743 * JB_ERR_MEMORY on out-of-memory
744 * JB_ERR_CGI_PARAMS if the CGI parameters are not
745 * specified or not valid.
747 *********************************************************************/
748 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
749 struct http_response *rsp,
750 const struct map *parameters)
752 struct map * exports;
754 struct editable_file * file;
755 struct file_line * cur_line;
756 unsigned line_number;
763 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
765 return cgi_error_disabled(csp, rsp);
768 err = get_number_param(csp, parameters, "p", &patternid);
774 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
777 /* No filename specified, can't read file, modified, or out of memory. */
778 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
781 cur_line = file->lines;
783 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
785 cur_line = cur_line->next;
788 if ( (cur_line == NULL)
789 || (line_number != patternid)
791 || (cur_line->type != FILE_LINE_URL))
793 /* Invalid "patternid" parameter */
794 edit_free_file(file);
795 return JB_ERR_CGI_PARAMS;
798 if (NULL == (exports = default_exports(csp, NULL)))
800 edit_free_file(file);
801 return JB_ERR_MEMORY;
804 err = map(exports, "f", 1, file->identifier, 1);
805 if (!err) err = map(exports, "v", 1, file->version_str, 1);
806 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
807 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
809 edit_free_file(file);
817 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
821 /*********************************************************************
823 * Function : edit_write_file
825 * Description : Write a complete file to disk.
828 * 1 : file = File to write.
830 * Returns : JB_ERR_OK on success
831 * JB_ERR_FILE on error writing to file.
832 * JB_ERR_MEMORY on out of memory
834 *********************************************************************/
835 jb_err edit_write_file(struct editable_file * file)
838 struct file_line * cur_line;
839 struct stat statbuf[1];
840 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
841 digits in time_t, assuming this is a 64-bit
842 machine, plus null terminator, plus one
846 assert(file->filename);
848 if (NULL == (fp = fopen(file->filename, "wb")))
853 cur_line = file->lines;
854 while (cur_line != NULL)
858 if (fputs(cur_line->raw, fp) < 0)
866 if (cur_line->prefix)
868 if (fputs(cur_line->prefix, fp) < 0)
874 if (cur_line->unprocessed)
877 if (NULL != strchr(cur_line->unprocessed, '#'))
879 /* Must quote '#' characters */
886 /* Count number of # characters, so we know length of output string */
887 src = cur_line->unprocessed;
888 while (NULL != (src = strchr(src, '#')))
895 /* Allocate new memory for string */
896 len = strlen(cur_line->unprocessed);
897 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
899 /* Uh oh, just trashed file! */
901 return JB_ERR_MEMORY;
904 /* Loop through string from end */
905 src = cur_line->unprocessed + len;
906 dest = str + len + numhash;
907 for ( ; len >= 0; len--)
909 if ((*dest-- = *src--) == '#')
913 assert(numhash >= 0);
916 assert(numhash == 0);
917 assert(src + 1 == cur_line->unprocessed);
918 assert(dest + 1 == str);
920 if (fputs(str, fp) < 0)
931 /* Can write without quoting '#' characters. */
932 if (fputs(cur_line->unprocessed, fp) < 0)
938 if (fputs(NEWLINE(file->newline), fp) < 0)
946 /* FIXME: Write data from file->data->whatever */
950 cur_line = cur_line->next;
956 /* Update the version stamp in the file structure, since we just
957 * wrote to the file & changed it's date.
959 if (stat(file->filename, statbuf) < 0)
961 /* Error, probably file not found. */
964 file->version = (unsigned)statbuf->st_mtime;
966 /* Correct file->version_str */
967 freez(file->version_str);
968 snprintf(version_buf, 22, "%u", file->version);
969 version_buf[21] = '\0';
970 file->version_str = strdup(version_buf);
971 if (version_buf == NULL)
973 return JB_ERR_MEMORY;
980 /*********************************************************************
982 * Function : edit_free_file
984 * Description : Free a complete file in memory.
987 * 1 : file = Data structure to free.
991 *********************************************************************/
992 void edit_free_file(struct editable_file * file)
996 /* Silently ignore NULL pointer */
1000 edit_free_file_lines(file->lines);
1001 freez(file->filename);
1002 freez(file->identifier);
1003 freez(file->version_str);
1005 file->parse_error_text = NULL; /* Statically allocated */
1006 file->parse_error = NULL;
1012 /*********************************************************************
1014 * Function : edit_free_file_lines
1016 * Description : Free an entire linked list of file lines.
1019 * 1 : first_line = Data structure to free.
1023 *********************************************************************/
1024 static void edit_free_file_lines(struct file_line * first_line)
1026 struct file_line * next_line;
1028 while (first_line != NULL)
1030 next_line = first_line->next;
1031 first_line->next = NULL;
1032 freez(first_line->raw);
1033 freez(first_line->prefix);
1034 freez(first_line->unprocessed);
1035 switch(first_line->type)
1037 case 0: /* special case if memory zeroed */
1038 case FILE_LINE_UNPROCESSED:
1039 case FILE_LINE_BLANK:
1040 case FILE_LINE_ALIAS_HEADER:
1041 case FILE_LINE_SETTINGS_HEADER:
1042 case FILE_LINE_DESCRIPTION_HEADER:
1043 case FILE_LINE_DESCRIPTION_ENTRY:
1044 case FILE_LINE_ALIAS_ENTRY:
1046 /* No data is stored for these */
1049 case FILE_LINE_ACTION:
1050 free_action(first_line->data.action);
1053 case FILE_LINE_SETTINGS_ENTRY:
1054 freez(first_line->data.setting.name);
1055 freez(first_line->data.setting.svalue);
1058 /* Should never happen */
1062 first_line->type = 0; /* paranoia */
1064 first_line = next_line;
1069 /*********************************************************************
1071 * Function : match_actions_file_header_line
1073 * Description : Match an actions file {{header}} line
1076 * 1 : line = String from file
1077 * 2 : name = Header to match against
1079 * Returns : 0 iff they match.
1081 *********************************************************************/
1082 static int match_actions_file_header_line(const char * line, const char * name)
1090 if ((line[0] != '{') || (line[1] != '{'))
1096 /* Look for optional whitespace */
1097 while ( (*line == ' ') || (*line == '\t') )
1102 /* Look for the specified name (case-insensitive) */
1104 if (0 != strncmpic(line, name, len))
1110 /* Look for optional whitespace */
1111 while ( (*line == ' ') || (*line == '\t') )
1116 /* Look for "}}" and end of string*/
1117 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1127 /*********************************************************************
1129 * Function : match_actions_file_header_line
1131 * Description : Match an actions file {{header}} line
1134 * 1 : line = String from file. Must not start with
1135 * whitespace (else infinite loop!)
1136 * 2 : pname = Destination for name
1137 * 2 : pvalue = Destination for value
1139 * Returns : JB_ERR_OK on success
1140 * JB_ERR_MEMORY on out-of-memory
1141 * JB_ERR_PARSE if there's no "=" sign, or if there's
1142 * nothing before the "=" sign (but empty
1143 * values *after* the "=" sign are legal).
1145 *********************************************************************/
1146 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1148 const char * name_end;
1149 const char * value_start;
1155 assert(*line != ' ');
1156 assert(*line != '\t');
1161 value_start = strchr(line, '=');
1162 if ((value_start == NULL) || (value_start == line))
1164 return JB_ERR_PARSE;
1167 name_end = value_start - 1;
1169 /* Eat any whitespace before the '=' */
1170 while ((*name_end == ' ') || (*name_end == '\t'))
1173 * we already know we must have at least 1 non-ws char
1174 * at start of buf - no need to check
1179 name_len = name_end - line + 1; /* Length excluding \0 */
1180 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1182 return JB_ERR_MEMORY;
1184 strncpy(*pname, line, name_len);
1185 (*pname)[name_len] = '\0';
1187 /* Eat any the whitespace after the '=' */
1189 while ((*value_start == ' ') || (*value_start == '\t'))
1194 if (NULL == (*pvalue = strdup(value_start)))
1198 return JB_ERR_MEMORY;
1205 /*********************************************************************
1207 * Function : edit_parse_actions_file
1209 * Description : Parse an actions file in memory.
1211 * Passed linked list must have the "data" member
1212 * zeroed, and must contain valid "next" and
1213 * "unprocessed" fields. The "raw" and "prefix"
1214 * fields are ignored, and "type" is just overwritten.
1216 * Note that on error the file may have been
1220 * 1 : file = Actions file to be parsed in-place.
1222 * Returns : JB_ERR_OK on success
1223 * JB_ERR_MEMORY on out-of-memory
1224 * JB_ERR_PARSE on error
1226 *********************************************************************/
1227 jb_err edit_parse_actions_file(struct editable_file * file)
1229 struct file_line * cur_line;
1231 const char * text; /* Text from a line */
1232 char * name; /* For lines of the form name=value */
1233 char * value; /* For lines of the form name=value */
1234 struct action_alias * alias_list = NULL;
1235 jb_err err = JB_ERR_OK;
1237 /* alias_list contains the aliases defined in this file.
1238 * It might be better to use the "file_line.data" fields
1239 * in the relavent places instead.
1242 cur_line = file->lines;
1244 /* A note about blank line support: Blank lines should only
1245 * ever occur as the last line in the file. This function
1246 * is more forgiving than that - FILE_LINE_BLANK can occur
1250 /* Skip leading blanks. Should only happen if file is
1251 * empty (which is valid, but pointless).
1253 while ( (cur_line != NULL)
1254 && (cur_line->unprocessed[0] == '\0') )
1257 cur_line->type = FILE_LINE_BLANK;
1258 cur_line = cur_line->next;
1261 if ( (cur_line != NULL)
1262 && (cur_line->unprocessed[0] != '{') )
1264 /* File doesn't start with a header */
1265 file->parse_error = cur_line;
1266 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1267 return JB_ERR_PARSE;
1270 if ( (cur_line != NULL) && (0 ==
1271 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1273 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1275 cur_line = cur_line->next;
1276 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1278 if (cur_line->unprocessed[0])
1280 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1282 err = split_line_on_equals(cur_line->unprocessed,
1283 &cur_line->data.setting.name,
1284 &cur_line->data.setting.svalue);
1285 if (err == JB_ERR_MEMORY)
1289 else if (err != JB_ERR_OK)
1291 /* Line does not contain a name=value pair */
1292 file->parse_error = cur_line;
1293 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1294 return JB_ERR_PARSE;
1299 cur_line->type = FILE_LINE_BLANK;
1301 cur_line = cur_line->next;
1305 if ( (cur_line != NULL) && (0 ==
1306 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1308 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1310 cur_line = cur_line->next;
1311 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1313 if (cur_line->unprocessed[0])
1315 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1319 cur_line->type = FILE_LINE_BLANK;
1321 cur_line = cur_line->next;
1325 if ( (cur_line != NULL) && (0 ==
1326 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1328 cur_line->type = FILE_LINE_ALIAS_HEADER;
1330 cur_line = cur_line->next;
1331 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1333 if (cur_line->unprocessed[0])
1335 /* define an alias */
1336 struct action_alias * new_alias;
1338 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1340 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1341 if (err == JB_ERR_MEMORY)
1345 else if (err != JB_ERR_OK)
1347 /* Line does not contain a name=value pair */
1348 file->parse_error = cur_line;
1349 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1350 return JB_ERR_PARSE;
1353 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1358 free_alias_list(alias_list);
1359 return JB_ERR_MEMORY;
1362 err = get_actions(value, alias_list, new_alias->action);
1365 /* Invalid action or out of memory */
1369 free_alias_list(alias_list);
1370 if (err == JB_ERR_MEMORY)
1376 /* Line does not contain a name=value pair */
1377 file->parse_error = cur_line;
1378 file->parse_error_text = "This alias does not specify a valid set of actions.";
1379 return JB_ERR_PARSE;
1385 new_alias->name = name;
1388 new_alias->next = alias_list;
1389 alias_list = new_alias;
1393 cur_line->type = FILE_LINE_BLANK;
1395 cur_line = cur_line->next;
1399 /* Header done, process the main part of the file */
1400 while (cur_line != NULL)
1402 /* At this point, (cur_line->unprocessed[0] == '{') */
1403 assert(cur_line->unprocessed[0] == '{');
1404 text = cur_line->unprocessed + 1;
1405 len = strlen(text) - 1;
1406 if (text[len] != '}')
1408 /* No closing } on header */
1409 free_alias_list(alias_list);
1410 file->parse_error = cur_line;
1411 file->parse_error_text = "Headers starting with '{' must have a "
1412 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1413 "must close with two brackets ('}}').";
1414 return JB_ERR_PARSE;
1419 /* An invalid {{ header. */
1420 free_alias_list(alias_list);
1421 file->parse_error = cur_line;
1422 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1423 "Please remember that the system (two-bracket) headers must "
1424 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1425 "and must appear before any actions (one-bracket) headers. "
1426 "Also note that system headers may not be repeated.";
1427 return JB_ERR_PARSE;
1430 while ( (*text == ' ') || (*text == '\t') )
1436 && ( (text[len - 1] == ' ')
1437 || (text[len - 1] == '\t') ) )
1442 cur_line->type = FILE_LINE_ACTION;
1444 /* Remove {} and make copy */
1445 if (NULL == (value = (char *) malloc(len + 1)))
1448 free_alias_list(alias_list);
1449 return JB_ERR_MEMORY;
1451 strncpy(value, text, len);
1455 err = get_actions(value, alias_list, cur_line->data.action);
1458 /* Invalid action or out of memory */
1460 free_alias_list(alias_list);
1461 if (err == JB_ERR_MEMORY)
1467 /* Line does not contain a name=value pair */
1468 file->parse_error = cur_line;
1469 file->parse_error_text = "This header does not specify a valid set of actions.";
1470 return JB_ERR_PARSE;
1474 /* Done with string - it was clobbered anyway */
1477 /* Process next line */
1478 cur_line = cur_line->next;
1480 /* Loop processing URL patterns */
1481 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1483 if (cur_line->unprocessed[0])
1485 /* Could parse URL here, but this isn't currently needed */
1487 cur_line->type = FILE_LINE_URL;
1491 cur_line->type = FILE_LINE_BLANK;
1493 cur_line = cur_line->next;
1495 } /* End main while(cur_line != NULL) loop */
1497 free_alias_list(alias_list);
1503 /*********************************************************************
1505 * Function : edit_read_file_lines
1507 * Description : Read all the lines of a file into memory.
1508 * Handles whitespace, comments and line continuation.
1511 * 1 : fp = File to read from. On return, this will be
1512 * at EOF but it will not have been closed.
1513 * 2 : pfile = Destination for a linked list of file_lines.
1514 * Will be set to NULL on error.
1515 * 3 : newline = How to handle newlines.
1517 * Returns : JB_ERR_OK on success
1518 * JB_ERR_MEMORY on out-of-memory
1520 *********************************************************************/
1521 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1523 struct file_line * first_line; /* Keep for return value or to free */
1524 struct file_line * cur_line; /* Current line */
1525 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1533 cur_line = first_line = zalloc(sizeof(struct file_line));
1534 if (cur_line == NULL)
1536 return JB_ERR_MEMORY;
1539 cur_line->type = FILE_LINE_UNPROCESSED;
1541 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1544 /* Out of memory or empty file. */
1545 /* Note that empty file is not an error we propogate up */
1547 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1552 prev_line = cur_line;
1553 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1554 if (cur_line == NULL)
1557 edit_free_file_lines(first_line);
1558 return JB_ERR_MEMORY;
1561 cur_line->type = FILE_LINE_UNPROCESSED;
1563 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1564 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1567 edit_free_file_lines(first_line);
1568 return JB_ERR_MEMORY;
1572 while (rval != JB_ERR_FILE);
1576 /* We allocated one too many - free it */
1577 prev_line->next = NULL;
1580 *pfile = first_line;
1585 /*********************************************************************
1587 * Function : edit_read_file
1589 * Description : Read a complete file into memory.
1590 * Handles CGI parameter parsing. If requested, also
1591 * checks the file's modification timestamp.
1594 * 1 : csp = Current client state (buffers, headers, etc...)
1595 * 2 : parameters = map of cgi parameters.
1596 * 3 : require_version = true to check "ver" parameter.
1597 * 4 : suffix = File extension, e.g. ".action".
1598 * 5 : pfile = Destination for the file. Will be set
1602 * filename : The name of the file to read, without the
1603 * path or ".action" extension.
1604 * ver : (Only if require_version is nonzero)
1605 * Timestamp of the actions file. If wrong, this
1606 * function fails with JB_ERR_MODIFIED.
1608 * Returns : JB_ERR_OK on success
1609 * JB_ERR_MEMORY on out-of-memory
1610 * JB_ERR_CGI_PARAMS if "filename" was not specified
1612 * JB_ERR_FILE if the file cannot be opened or
1614 * JB_ERR_MODIFIED if version checking was requested and
1615 * failed - the file was modified outside
1616 * of this CGI editor instance.
1618 *********************************************************************/
1619 jb_err edit_read_file(struct client_state *csp,
1620 const struct map *parameters,
1621 int require_version,
1623 struct editable_file **pfile)
1625 struct file_line * lines;
1629 const char * identifier;
1630 struct editable_file * file;
1631 unsigned version = 0;
1632 struct stat statbuf[1];
1633 char version_buf[22];
1634 int newline = NEWLINE_UNKNOWN;
1642 err = get_file_name_param(csp, parameters, "f", suffix,
1643 &filename, &identifier);
1649 if (stat(filename, statbuf) < 0)
1651 /* Error, probably file not found. */
1655 version = (unsigned) statbuf->st_mtime;
1657 if (require_version)
1659 unsigned specified_version;
1660 err = get_number_param(csp, parameters, "v", &specified_version);
1667 if (version != specified_version)
1669 return JB_ERR_MODIFIED;
1673 if (NULL == (fp = fopen(filename,"rb")))
1679 err = edit_read_file_lines(fp, &lines, &newline);
1689 file = (struct editable_file *) zalloc(sizeof(*file));
1693 edit_free_file_lines(lines);
1697 file->lines = lines;
1698 file->newline = newline;
1699 file->filename = filename;
1700 file->version = version;
1701 file->identifier = url_encode(identifier);
1703 if (file->identifier == NULL)
1705 edit_free_file(file);
1706 return JB_ERR_MEMORY;
1709 /* Correct file->version_str */
1710 freez(file->version_str);
1711 snprintf(version_buf, 22, "%u", file->version);
1712 version_buf[21] = '\0';
1713 file->version_str = strdup(version_buf);
1714 if (version_buf == NULL)
1716 edit_free_file(file);
1717 return JB_ERR_MEMORY;
1725 /*********************************************************************
1727 * Function : edit_read_actions_file
1729 * Description : Read a complete actions file into memory.
1730 * Handles CGI parameter parsing. If requested, also
1731 * checks the file's modification timestamp.
1733 * If this function detects an error in the categories
1734 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1735 * then it handles it by filling in the specified
1736 * response structure and returning JB_ERR_FILE.
1739 * 1 : csp = Current client state (buffers, headers, etc...)
1740 * 2 : rsp = HTTP response. Only filled in on error.
1741 * 2 : parameters = map of cgi parameters.
1742 * 3 : require_version = true to check "ver" parameter.
1743 * 4 : pfile = Destination for the file. Will be set
1747 * filename : The name of the actions file to read, without the
1748 * path or ".action" extension.
1749 * ver : (Only if require_version is nonzero)
1750 * Timestamp of the actions file. If wrong, this
1751 * function fails with JB_ERR_MODIFIED.
1753 * Returns : JB_ERR_OK on success
1754 * JB_ERR_MEMORY on out-of-memory
1755 * JB_ERR_CGI_PARAMS if "filename" was not specified
1757 * JB_ERR_FILE if the file does not contain valid data,
1758 * or if file cannot be opened or
1759 * contains no data, or if version
1760 * checking was requested and failed.
1762 *********************************************************************/
1763 jb_err edit_read_actions_file(struct client_state *csp,
1764 struct http_response *rsp,
1765 const struct map *parameters,
1766 int require_version,
1767 struct editable_file **pfile)
1770 struct editable_file *file;
1778 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1781 /* Try to handle if possible */
1782 if (err == JB_ERR_FILE)
1784 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1786 else if (err == JB_ERR_MODIFIED)
1788 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1790 if (err == JB_ERR_OK)
1793 * Signal to higher-level CGI code that there was a problem but we
1794 * handled it, they should just return JB_ERR_OK.
1801 err = edit_parse_actions_file(file);
1804 if (err == JB_ERR_PARSE)
1806 err = cgi_error_parse(csp, rsp, file);
1807 if (err == JB_ERR_OK)
1810 * Signal to higher-level CGI code that there was a problem but we
1811 * handled it, they should just return JB_ERR_OK.
1816 edit_free_file(file);
1825 /*********************************************************************
1827 * Function : get_file_name_param
1829 * Description : Get the name of the file to edit from the parameters
1830 * passed to a CGI function. This function handles
1831 * security checks such as blocking urls containing
1832 * "/" or ".", prepending the config file directory,
1833 * and adding the specified suffix.
1835 * (This is an essential security check, otherwise
1836 * users may be able to pass "../../../etc/passwd"
1837 * and overwrite the password file [linux], "prn:"
1838 * and print random data [Windows], etc...)
1840 * This function only allows filenames contining the
1841 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1842 * That's probably too restrictive but at least it's
1846 * 1 : csp = Current client state (buffers, headers, etc...)
1847 * 2 : parameters = map of cgi parameters
1848 * 3 : param_name = The name of the parameter to read
1849 * 4 : suffix = File extension, e.g. ".actions"
1850 * 5 : pfilename = destination for full filename. Caller
1851 * free()s. Set to NULL on error.
1852 * 6 : pparam = destination for partial filename,
1853 * suitable for use in another URL. Allocated as part
1854 * of the map "parameters", so don't free it.
1855 * Set to NULL if not specified.
1857 * Returns : JB_ERR_OK on success
1858 * JB_ERR_MEMORY on out-of-memory
1859 * JB_ERR_CGI_PARAMS if "filename" was not specified
1862 *********************************************************************/
1863 static jb_err get_file_name_param(struct client_state *csp,
1864 const struct map *parameters,
1865 const char *param_name,
1868 const char **pparam)
1886 param = lookup(parameters, param_name);
1889 return JB_ERR_CGI_PARAMS;
1894 len = strlen(param);
1895 if (len >= FILENAME_MAX)
1898 return JB_ERR_CGI_PARAMS;
1901 /* Check every character to see if it's legal */
1903 while ((ch = *s++) != '\0')
1905 if ( ((ch < 'A') || (ch > 'Z'))
1906 && ((ch < 'a') || (ch > 'z'))
1907 && ((ch < '0') || (ch > '9'))
1911 /* Probable hack attempt. */
1912 return JB_ERR_CGI_PARAMS;
1916 /* Append extension */
1917 name = malloc(len + strlen(suffix) + 1);
1920 return JB_ERR_MEMORY;
1922 strcpy(name, param);
1923 strcpy(name + len, suffix);
1926 fullpath = make_path(csp->config->confdir, name);
1929 if (fullpath == NULL)
1931 return JB_ERR_MEMORY;
1935 *pfilename = fullpath;
1941 /*********************************************************************
1943 * Function : get_url_spec_param
1945 * Description : Get a URL pattern from the parameters
1946 * passed to a CGI function. Removes leading/trailing
1947 * spaces and validates it.
1950 * 1 : csp = Current client state (buffers, headers, etc...)
1951 * 2 : parameters = map of cgi parameters
1952 * 3 : name = Name of CGI parameter to read
1953 * 4 : pvalue = destination for value. Will be malloc()'d.
1954 * Set to NULL on error.
1956 * Returns : JB_ERR_OK on success
1957 * JB_ERR_MEMORY on out-of-memory
1958 * JB_ERR_CGI_PARAMS if the parameter was not specified
1961 *********************************************************************/
1962 static jb_err get_url_spec_param(struct client_state *csp,
1963 const struct map *parameters,
1967 const char *orig_param;
1970 struct url_spec compiled[1];
1980 orig_param = lookup(parameters, name);
1983 return JB_ERR_CGI_PARAMS;
1986 /* Copy and trim whitespace */
1987 param = strdup(orig_param);
1990 return JB_ERR_MEMORY;
1994 /* Must be non-empty, and can't allow 1st character to be '{' */
1995 if (param[0] == '\0' || param[0] == '{')
1998 return JB_ERR_CGI_PARAMS;
2001 /* Check for embedded newlines */
2002 for (s = param; *s != '\0'; s++)
2004 if ((*s == '\r') || (*s == '\n'))
2007 return JB_ERR_CGI_PARAMS;
2011 /* Check that regex is valid */
2016 return JB_ERR_MEMORY;
2018 err = create_url_spec(compiled, s);
2023 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2025 free_url_spec(compiled);
2027 if (param[strlen(param) - 1] == '\\')
2030 * Must protect trailing '\\' from becoming line continuation character.
2031 * Two methods: 1) If it's a domain only, add a trailing '/'.
2032 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2034 if (strchr(param, '/') == NULL)
2036 err = string_append(¶m, "/");
2040 err = string_append(¶m, "(?:)");
2047 /* Check that the modified regex is valid */
2052 return JB_ERR_MEMORY;
2054 err = create_url_spec(compiled, s);
2059 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2061 free_url_spec(compiled);
2068 /*********************************************************************
2070 * Function : map_radio
2072 * Description : Map a set of radio button values. E.g. if you have
2073 * 3 radio buttons, declare them as:
2074 * <option type="radio" name="xyz" @xyz-a@>
2075 * <option type="radio" name="xyz" @xyz-b@>
2076 * <option type="radio" name="xyz" @xyz-c@>
2077 * Then map one of the @xyz-?@ variables to "checked"
2078 * and all the others to empty by calling:
2079 * map_radio(exports, "xyz", "abc", sel)
2080 * Where 'sel' is 'a', 'b', or 'c'.
2083 * 1 : exports = Exports map to modify.
2084 * 2 : optionname = name for map
2085 * 3 : values = null-terminated list of values;
2086 * 4 : value = Selected value.
2088 * CGI Parameters : None
2090 * Returns : JB_ERR_OK on success
2091 * JB_ERR_MEMORY on out-of-memory
2093 *********************************************************************/
2094 static jb_err map_radio(struct map * exports,
2095 const char * optionname,
2096 const char * values,
2108 len = strlen(optionname);
2109 buf = malloc(len + 3);
2112 return JB_ERR_MEMORY;
2115 strcpy(buf, optionname);
2120 while ((c = *values++) != '\0')
2125 if (map(exports, buf, 1, "", 1))
2127 return JB_ERR_MEMORY;
2133 return map(exports, buf, 0, "checked", 1);
2137 /*********************************************************************
2139 * Function : cgi_error_modified
2141 * Description : CGI function that is called when a file is modified
2142 * outside the CGI editor.
2145 * 1 : csp = Current client state (buffers, headers, etc...)
2146 * 2 : rsp = http_response data structure for output
2147 * 3 : filename = The file that was modified.
2149 * CGI Parameters : none
2151 * Returns : JB_ERR_OK on success
2152 * JB_ERR_MEMORY on out-of-memory error.
2154 *********************************************************************/
2155 jb_err cgi_error_modified(struct client_state *csp,
2156 struct http_response *rsp,
2157 const char *filename)
2159 struct map *exports;
2166 if (NULL == (exports = default_exports(csp, NULL)))
2168 return JB_ERR_MEMORY;
2171 err = map(exports, "f", 1, html_encode(filename), 0);
2178 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2182 /*********************************************************************
2184 * Function : cgi_error_parse
2186 * Description : CGI function that is called when a file cannot
2187 * be parsed by the CGI editor.
2190 * 1 : csp = Current client state (buffers, headers, etc...)
2191 * 2 : rsp = http_response data structure for output
2192 * 3 : file = The file that was modified.
2194 * CGI Parameters : none
2196 * Returns : JB_ERR_OK on success
2197 * JB_ERR_MEMORY on out-of-memory error.
2199 *********************************************************************/
2200 jb_err cgi_error_parse(struct client_state *csp,
2201 struct http_response *rsp,
2202 struct editable_file *file)
2204 struct map *exports;
2206 struct file_line *cur_line;
2212 if (NULL == (exports = default_exports(csp, NULL)))
2214 return JB_ERR_MEMORY;
2217 err = map(exports, "f", 1, file->identifier, 1);
2218 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2220 cur_line = file->parse_error;
2223 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2224 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2232 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2236 /*********************************************************************
2238 * Function : cgi_error_file
2240 * Description : CGI function that is called when a file cannot be
2241 * opened by the CGI editor.
2244 * 1 : csp = Current client state (buffers, headers, etc...)
2245 * 2 : rsp = http_response data structure for output
2246 * 3 : filename = The file that was modified.
2248 * CGI Parameters : none
2250 * Returns : JB_ERR_OK on success
2251 * JB_ERR_MEMORY on out-of-memory error.
2253 *********************************************************************/
2254 jb_err cgi_error_file(struct client_state *csp,
2255 struct http_response *rsp,
2256 const char *filename)
2258 struct map *exports;
2265 if (NULL == (exports = default_exports(csp, NULL)))
2267 return JB_ERR_MEMORY;
2270 err = map(exports, "f", 1, html_encode(filename), 0);
2277 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2281 /*********************************************************************
2283 * Function : cgi_error_disabled
2285 * Description : CGI function that is called if the actions editor
2286 * is called although it's disabled in config
2289 * 1 : csp = Current client state (buffers, headers, etc...)
2290 * 2 : rsp = http_response data structure for output
2292 * CGI Parameters : none
2294 * Returns : JB_ERR_OK on success
2295 * JB_ERR_MEMORY on out-of-memory error.
2297 *********************************************************************/
2298 jb_err cgi_error_disabled(struct client_state *csp,
2299 struct http_response *rsp)
2301 struct map *exports;
2306 if (NULL == (exports = default_exports(csp, NULL)))
2308 return JB_ERR_MEMORY;
2311 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2315 /*********************************************************************
2317 * Function : cgi_edit_actions
2319 * Description : CGI function that allows the user to choose which
2320 * actions file to edit.
2323 * 1 : csp = Current client state (buffers, headers, etc...)
2324 * 2 : rsp = http_response data structure for output
2325 * 3 : parameters = map of cgi parameters
2327 * CGI Parameters : None
2329 * Returns : JB_ERR_OK on success
2330 * JB_ERR_MEMORY on out-of-memory error
2332 *********************************************************************/
2333 jb_err cgi_edit_actions(struct client_state *csp,
2334 struct http_response *rsp,
2335 const struct map *parameters)
2338 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2340 return cgi_error_disabled(csp, rsp);
2343 /* FIXME: Incomplete */
2344 rsp->status = strdup("302 Local Redirect from Privoxy");
2345 if (rsp->status == NULL)
2347 return JB_ERR_MEMORY;
2349 if (enlist_unique_header(rsp->headers, "Location",
2350 CGI_PREFIX "edit-actions-list?f=default"))
2354 return JB_ERR_MEMORY;
2361 /*********************************************************************
2363 * Function : cgi_edit_actions_list
2365 * Description : CGI function that edits the actions list.
2366 * FIXME: This function shouldn't FATAL ever.
2367 * FIXME: This function doesn't check the retval of map()
2369 * 1 : csp = Current client state (buffers, headers, etc...)
2370 * 2 : rsp = http_response data structure for output
2371 * 3 : parameters = map of cgi parameters
2373 * CGI Parameters : filename
2375 * Returns : JB_ERR_OK on success
2376 * JB_ERR_MEMORY on out-of-memory
2377 * JB_ERR_FILE if the file cannot be opened or
2379 * JB_ERR_CGI_PARAMS if "filename" was not specified
2382 *********************************************************************/
2383 jb_err cgi_edit_actions_list(struct client_state *csp,
2384 struct http_response *rsp,
2385 const struct map *parameters)
2387 char * section_template;
2388 char * url_template;
2393 struct map * exports;
2394 struct map * section_exports;
2395 struct map * url_exports;
2396 struct editable_file * file;
2397 struct file_line * cur_line;
2398 unsigned line_number = 0;
2399 unsigned prev_section_line_number = ((unsigned) (-1));
2401 struct file_list * fl;
2402 struct url_actions * b;
2403 char * buttons = NULL;
2406 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2408 return cgi_error_disabled(csp, rsp);
2411 if (NULL == (exports = default_exports(csp, NULL)))
2413 edit_free_file(file);
2414 return JB_ERR_MEMORY;
2417 /* Load actions file */
2418 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2421 /* No filename specified, can't read file, or out of memory. */
2422 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2425 /* Find start of actions in file */
2426 cur_line = file->lines;
2428 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2430 cur_line = cur_line->next;
2435 * Conventional actions files should have a match all block
2437 * cur_line = {...global actions...}
2438 * cur_line->next = /
2439 * cur_line->next->next = {...actions...} or EOF
2441 if ( (cur_line != NULL)
2442 && (cur_line->type == FILE_LINE_ACTION)
2443 && (cur_line->next != NULL)
2444 && (cur_line->next->type == FILE_LINE_URL)
2445 && (0 == strcmp(cur_line->next->unprocessed, "/"))
2446 && ( (cur_line->next->next == NULL)
2447 || (cur_line->next->next->type != FILE_LINE_URL)
2451 * Generate string with buttons to set actions for "/" to
2452 * any predefined set of actions (named standard.*, probably
2453 * residing in standard.action).
2456 err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
2459 edit_free_file(file);
2461 if (err == JB_ERR_FILE)
2463 return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
2468 err = template_fill(§ion_template, exports);
2471 edit_free_file(file);
2476 buttons = strdup("");
2477 for (i = 0; i < MAX_ACTION_FILES; i++)
2479 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
2481 for (b = b->next; NULL != b; b = b->next)
2483 if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
2485 if (err || (NULL == (section_exports = new_map())))
2488 free(section_template);
2489 edit_free_file(file);
2491 return JB_ERR_MEMORY;
2494 err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
2496 if (err || (NULL == (s = strdup(section_template))))
2498 free_map(section_exports);
2500 free(section_template);
2501 edit_free_file(file);
2503 return JB_ERR_MEMORY;
2506 if (!err) err = template_fill(&s, section_exports);
2507 free_map(section_exports);
2508 if (!err) err = string_join(&buttons, s);
2513 freez(section_template);
2514 if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
2517 * Conventional actions file, supply extra editing help.
2518 * (e.g. don't allow them to make it an unconventional one).
2520 if (!err) err = map_conditional(exports, "all-urls-present", 1);
2522 snprintf(buf, 150, "%d", line_number);
2523 if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
2524 snprintf(buf, 150, "%d", line_number + 2);
2525 if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
2526 if (!err) err = map(exports, "all-urls-actions", 1,
2527 actions_to_html(csp, cur_line->data.action), 0);
2529 /* Skip the 2 lines */
2530 cur_line = cur_line->next->next;
2534 * Note that prev_section_line_number is NOT set here.
2535 * This is deliberate and not a bug. It stops a "Move up"
2536 * option appearing on the next section. Clicking "Move
2537 * up" would make the actions file unconventional, which
2538 * we don't want, so we hide this option.
2544 * Non-standard actions file - does not begin with
2545 * the "All URLs" section.
2547 if (!err) err = map_conditional(exports, "all-urls-present", 0);
2550 /* Set up global exports */
2552 if (!err) err = map(exports, "f", 1, file->identifier, 1);
2553 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2555 /* Discourage private additions to default.action */
2557 if (!err) err = map_conditional(exports, "default-action",
2558 (strcmp("default", lookup(parameters, "f")) == 0));
2561 edit_free_file(file);
2566 /* Should do all global exports above this point */
2568 /* Load templates */
2570 err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
2573 edit_free_file(file);
2575 if (err == JB_ERR_FILE)
2577 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2582 err = template_load(csp, &url_template, "edit-actions-list-url", 0);
2585 free(section_template);
2586 edit_free_file(file);
2588 if (err == JB_ERR_FILE)
2590 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2595 err = template_fill(§ion_template, exports);
2599 edit_free_file(file);
2605 err = template_fill(&url_template, exports);
2608 free(section_template);
2609 edit_free_file(file);
2614 if (NULL == (sections = strdup("")))
2616 free(section_template);
2618 edit_free_file(file);
2620 return JB_ERR_MEMORY;
2623 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2625 if (NULL == (section_exports = new_map()))
2628 free(section_template);
2630 edit_free_file(file);
2632 return JB_ERR_MEMORY;
2635 snprintf(buf, 150, "%d", line_number);
2636 err = map(section_exports, "s", 1, buf, 1);
2637 if (!err) err = map(section_exports, "actions", 1,
2638 actions_to_html(csp, cur_line->data.action), 0);
2641 && (cur_line->next != NULL)
2642 && (cur_line->next->type == FILE_LINE_URL))
2644 /* This section contains at least one URL, don't allow delete */
2645 err = map_block_killer(section_exports, "empty-section");
2649 if (!err) err = map_block_keep(section_exports, "empty-section");
2652 if (prev_section_line_number != ((unsigned)(-1)))
2654 /* Not last section */
2655 snprintf(buf, 150, "%d", prev_section_line_number);
2656 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2657 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2662 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2664 prev_section_line_number = line_number;
2669 free(section_template);
2671 edit_free_file(file);
2673 free_map(section_exports);
2677 /* Should do all section-specific exports above this point */
2679 if (NULL == (urls = strdup("")))
2682 free(section_template);
2684 edit_free_file(file);
2686 free_map(section_exports);
2687 return JB_ERR_MEMORY;
2692 cur_line = cur_line->next;
2695 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2697 if (NULL == (url_exports = new_map()))
2701 free(section_template);
2703 edit_free_file(file);
2705 free_map(section_exports);
2706 return JB_ERR_MEMORY;
2709 snprintf(buf, 150, "%d", line_number);
2710 err = map(url_exports, "p", 1, buf, 1);
2712 snprintf(buf, 150, "%d", url_1_2);
2713 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2715 if (!err) err = map(url_exports, "url-html", 1,
2716 html_encode(cur_line->unprocessed), 0);
2717 if (!err) err = map(url_exports, "url", 1,
2718 url_encode(cur_line->unprocessed), 0);
2724 free(section_template);
2726 edit_free_file(file);
2728 free_map(section_exports);
2729 free_map(url_exports);
2733 if (NULL == (s = strdup(url_template)))
2737 free(section_template);
2739 edit_free_file(file);
2741 free_map(section_exports);
2742 free_map(url_exports);
2743 return JB_ERR_MEMORY;
2746 err = template_fill(&s, section_exports);
2747 if (!err) err = template_fill(&s, url_exports);
2748 if (!err) err = string_append(&urls, s);
2750 free_map(url_exports);
2757 free(section_template);
2759 edit_free_file(file);
2761 free_map(section_exports);
2765 url_1_2 = 3 - url_1_2;
2767 cur_line = cur_line->next;
2771 err = map(section_exports, "urls", 1, urls, 0);
2773 /* Could also do section-specific exports here, but it wouldn't be as fast */
2775 if ( (cur_line != NULL)
2776 && (cur_line->type == FILE_LINE_ACTION))
2778 /* Not last section */
2779 snprintf(buf, 150, "%d", line_number);
2780 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2781 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2786 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2792 free(section_template);
2794 edit_free_file(file);
2796 free_map(section_exports);
2800 if (NULL == (s = strdup(section_template)))
2803 free(section_template);
2805 edit_free_file(file);
2807 free_map(section_exports);
2808 return JB_ERR_MEMORY;
2811 err = template_fill(&s, section_exports);
2812 if (!err) err = string_append(§ions, s);
2815 free_map(section_exports);
2820 free(section_template);
2822 edit_free_file(file);
2828 edit_free_file(file);
2829 free(section_template);
2832 err = map(exports, "sections", 1, sections, 0);
2839 /* Could also do global exports here, but it wouldn't be as fast */
2841 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2845 /*********************************************************************
2847 * Function : cgi_edit_actions_for_url
2849 * Description : CGI function that edits the Actions list.
2852 * 1 : csp = Current client state (buffers, headers, etc...)
2853 * 2 : rsp = http_response data structure for output
2854 * 3 : parameters = map of cgi parameters
2856 * CGI Parameters : None
2858 * Returns : JB_ERR_OK on success
2859 * JB_ERR_MEMORY on out-of-memory
2860 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2861 * specified or not valid.
2863 *********************************************************************/
2864 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2865 struct http_response *rsp,
2866 const struct map *parameters)
2868 struct map * exports;
2870 struct editable_file * file;
2871 struct file_line * cur_line;
2872 unsigned line_number;
2874 struct file_list *filter_file;
2875 struct re_filterfile_spec *filter_group;
2877 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2879 return cgi_error_disabled(csp, rsp);
2882 err = get_number_param(csp, parameters, "s", §ionid);
2888 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2891 /* No filename specified, can't read file, modified, or out of memory. */
2892 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2895 cur_line = file->lines;
2897 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2899 cur_line = cur_line->next;
2902 if ( (cur_line == NULL)
2903 || (line_number != sectionid)
2905 || (cur_line->type != FILE_LINE_ACTION))
2907 /* Invalid "sectionid" parameter */
2908 edit_free_file(file);
2909 return JB_ERR_CGI_PARAMS;
2912 if (NULL == (exports = default_exports(csp, NULL)))
2914 edit_free_file(file);
2915 return JB_ERR_MEMORY;
2918 err = map(exports, "f", 1, file->identifier, 1);
2919 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2920 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2922 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2924 filter_file = csp->rlist;
2925 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2927 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2931 edit_free_file(file);
2936 if (filter_group == NULL)
2938 err = map(exports, "filter-params", 1, "", 1);
2942 /* We have some entries in the filter list */
2945 char * filter_template;
2947 err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
2950 edit_free_file(file);
2952 if (err == JB_ERR_FILE)
2954 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2959 err = template_fill(&filter_template, exports);
2961 result = strdup("");
2963 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2965 char current_mode = 'x';
2966 struct list_entry *filter_name;
2968 struct map *line_exports;
2971 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2972 while ((filter_name != NULL)
2973 && (0 != strcmp(filter_group->name, filter_name->str)))
2975 filter_name = filter_name->next;
2978 if (filter_name != NULL)
2984 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2985 while ((filter_name != NULL)
2986 && (0 != strcmp(filter_group->name, filter_name->str)))
2988 filter_name = filter_name->next;
2990 if (filter_name != NULL)
2996 /* Generate a unique serial number */
2997 snprintf(number, sizeof(number), "%x", index++);
2998 number[sizeof(number) - 1] = '\0';
3000 line_exports = new_map();
3001 if (line_exports == NULL)
3003 err = JB_ERR_MEMORY;
3008 if (!err) err = map(line_exports, "index", 1, number, 1);
3009 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3010 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3011 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3016 this_line = strdup(filter_template);
3017 if (this_line == NULL) err = JB_ERR_MEMORY;
3019 if (!err) err = template_fill(&this_line, line_exports);
3020 string_join(&result, this_line);
3022 free_map(line_exports);
3026 freez(filter_template);
3030 err = map(exports, "filter-params", 1, result, 0);
3038 if (!err) err = map_radio(exports, "filter-all", "nx",
3039 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3041 edit_free_file(file);
3049 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3053 /*********************************************************************
3055 * Function : cgi_edit_actions_submit
3057 * Description : CGI function that actually edits the Actions list.
3060 * 1 : csp = Current client state (buffers, headers, etc...)
3061 * 2 : rsp = http_response data structure for output
3062 * 3 : parameters = map of cgi parameters
3064 * CGI Parameters : None
3066 * Returns : JB_ERR_OK on success
3067 * JB_ERR_MEMORY on out-of-memory
3068 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3069 * specified or not valid.
3071 *********************************************************************/
3072 jb_err cgi_edit_actions_submit(struct client_state *csp,
3073 struct http_response *rsp,
3074 const struct map *parameters)
3080 struct editable_file * file;
3081 struct file_line * cur_line;
3082 unsigned line_number;
3086 const char * action_set_name;
3088 struct file_list * fl;
3089 struct url_actions * b;
3091 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3093 return cgi_error_disabled(csp, rsp);
3096 err = get_number_param(csp, parameters, "s", §ionid);
3102 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3105 /* No filename specified, can't read file, modified, or out of memory. */
3106 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3109 cur_line = file->lines;
3111 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3113 cur_line = cur_line->next;
3116 if ( (cur_line == NULL)
3117 || (line_number != sectionid)
3119 || (cur_line->type != FILE_LINE_ACTION))
3121 /* Invalid "sectionid" parameter */
3122 edit_free_file(file);
3123 return JB_ERR_CGI_PARAMS;
3126 get_string_param(parameters, "p", &action_set_name);
3127 if (action_set_name != NULL)
3129 for (index = 0; index < MAX_ACTION_FILES; index++)
3131 if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
3133 for (b = b->next; NULL != b; b = b->next)
3135 if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
3137 copy_action(cur_line->data.action, b->action);
3143 edit_free_file(file);
3144 return JB_ERR_CGI_PARAMS;
3150 err = actions_from_radio(parameters, cur_line->data.action);
3156 edit_free_file(file);
3160 ch = get_char_param(parameters, "filter_all");
3163 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3164 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3165 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3169 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3172 for (index = 0; !err; index++)
3179 /* Generate the keys */
3180 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3181 key_value[sizeof(key_value) - 1] = '\0';
3182 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3183 key_name[sizeof(key_name) - 1] = '\0';
3185 err = get_string_param(parameters, key_name, &name);
3194 value = get_char_param(parameters, key_value);
3197 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3198 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3199 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3201 else if (value == 'N')
3203 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3204 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3206 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3207 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3210 else if (value == 'X')
3212 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3213 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3220 edit_free_file(file);
3224 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3227 edit_free_file(file);
3228 return JB_ERR_MEMORY;
3231 len = strlen(actiontext);
3235 * Empty action - must special-case this.
3236 * Simply setting len to 1 is sufficient...
3241 if (NULL == (newtext = malloc(len + 2)))
3245 edit_free_file(file);
3246 return JB_ERR_MEMORY;
3248 strcpy(newtext, actiontext);
3252 newtext[len + 1] = '\0';
3254 freez(cur_line->raw);
3255 freez(cur_line->unprocessed);
3256 cur_line->unprocessed = newtext;
3258 err = edit_write_file(file);
3261 /* Error writing file */
3262 edit_free_file(file);
3266 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3267 string_append(&target, file->identifier);
3269 edit_free_file(file);
3274 return JB_ERR_MEMORY;
3277 rsp->status = strdup("302 Local Redirect from Privoxy");
3278 if (rsp->status == NULL)
3281 return JB_ERR_MEMORY;
3283 err = enlist_unique_header(rsp->headers, "Location", target);
3290 /*********************************************************************
3292 * Function : cgi_edit_actions_url
3294 * Description : CGI function that actually edits a URL pattern in
3298 * 1 : csp = Current client state (buffers, headers, etc...)
3299 * 2 : rsp = http_response data structure for output
3300 * 3 : parameters = map of cgi parameters
3303 * filename : Identifies the file to edit
3304 * ver : File's last-modified time
3305 * section : Line number of section to edit
3306 * pattern : Line number of pattern to edit
3307 * newval : New value for pattern
3309 * Returns : JB_ERR_OK on success
3310 * JB_ERR_MEMORY on out-of-memory
3311 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3312 * specified or not valid.
3314 *********************************************************************/
3315 jb_err cgi_edit_actions_url(struct client_state *csp,
3316 struct http_response *rsp,
3317 const struct map *parameters)
3321 struct editable_file * file;
3322 struct file_line * cur_line;
3323 unsigned line_number;
3327 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3329 return cgi_error_disabled(csp, rsp);
3332 err = get_number_param(csp, parameters, "p", &patternid);
3339 return JB_ERR_CGI_PARAMS;
3342 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3348 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3351 /* No filename specified, can't read file, modified, or out of memory. */
3353 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3357 cur_line = file->lines;
3359 while ((cur_line != NULL) && (line_number < patternid))
3361 cur_line = cur_line->next;
3365 if ( (cur_line == NULL)
3366 || (cur_line->type != FILE_LINE_URL))
3368 /* Invalid "patternid" parameter */
3370 edit_free_file(file);
3371 return JB_ERR_CGI_PARAMS;
3374 /* At this point, the line to edit is in cur_line */
3376 freez(cur_line->raw);
3377 freez(cur_line->unprocessed);
3378 cur_line->unprocessed = new_pattern;
3380 err = edit_write_file(file);
3383 /* Error writing file */
3384 edit_free_file(file);
3388 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3389 string_append(&target, file->identifier);
3391 edit_free_file(file);
3396 return JB_ERR_MEMORY;
3399 rsp->status = strdup("302 Local Redirect from Privoxy");
3400 if (rsp->status == NULL)
3403 return JB_ERR_MEMORY;
3405 err = enlist_unique_header(rsp->headers, "Location", target);
3412 /*********************************************************************
3414 * Function : cgi_edit_actions_add_url
3416 * Description : CGI function that actually adds a URL pattern to
3420 * 1 : csp = Current client state (buffers, headers, etc...)
3421 * 2 : rsp = http_response data structure for output
3422 * 3 : parameters = map of cgi parameters
3425 * filename : Identifies the file to edit
3426 * ver : File's last-modified time
3427 * section : Line number of section to edit
3428 * newval : New pattern
3430 * Returns : JB_ERR_OK on success
3431 * JB_ERR_MEMORY on out-of-memory
3432 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3433 * specified or not valid.
3435 *********************************************************************/
3436 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3437 struct http_response *rsp,
3438 const struct map *parameters)
3442 struct file_line * new_line;
3443 struct editable_file * file;
3444 struct file_line * cur_line;
3445 unsigned line_number;
3449 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3451 return cgi_error_disabled(csp, rsp);
3454 err = get_number_param(csp, parameters, "s", §ionid);
3461 return JB_ERR_CGI_PARAMS;
3464 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3470 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3473 /* No filename specified, can't read file, modified, or out of memory. */
3475 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3479 cur_line = file->lines;
3481 while ((cur_line != NULL) && (line_number < sectionid))
3483 cur_line = cur_line->next;
3487 if ( (cur_line == NULL)
3488 || (cur_line->type != FILE_LINE_ACTION))
3490 /* Invalid "sectionid" parameter */
3492 edit_free_file(file);
3493 return JB_ERR_CGI_PARAMS;
3496 /* At this point, the section header is in cur_line - add after this. */
3498 /* Allocate the new line */
3499 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3500 if (new_line == NULL)
3503 edit_free_file(file);
3504 return JB_ERR_MEMORY;
3507 /* Fill in the data members of the new line */
3508 new_line->raw = NULL;
3509 new_line->prefix = NULL;
3510 new_line->unprocessed = new_pattern;
3511 new_line->type = FILE_LINE_URL;
3513 /* Link new_line into the list, after cur_line */
3514 new_line->next = cur_line->next;
3515 cur_line->next = new_line;
3517 /* Done making changes, now commit */
3519 err = edit_write_file(file);
3522 /* Error writing file */
3523 edit_free_file(file);
3527 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3528 string_append(&target, file->identifier);
3530 edit_free_file(file);
3535 return JB_ERR_MEMORY;
3538 rsp->status = strdup("302 Local Redirect from Privoxy");
3539 if (rsp->status == NULL)
3542 return JB_ERR_MEMORY;
3544 err = enlist_unique_header(rsp->headers, "Location", target);
3551 /*********************************************************************
3553 * Function : cgi_edit_actions_remove_url
3555 * Description : CGI function that actually removes a URL pattern from
3559 * 1 : csp = Current client state (buffers, headers, etc...)
3560 * 2 : rsp = http_response data structure for output
3561 * 3 : parameters = map of cgi parameters
3564 * f : (filename) Identifies the file to edit
3565 * v : (version) File's last-modified time
3566 * p : (pattern) Line number of pattern to remove
3568 * Returns : JB_ERR_OK on success
3569 * JB_ERR_MEMORY on out-of-memory
3570 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3571 * specified or not valid.
3573 *********************************************************************/
3574 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3575 struct http_response *rsp,
3576 const struct map *parameters)
3579 struct editable_file * file;
3580 struct file_line * cur_line;
3581 struct file_line * prev_line;
3582 unsigned line_number;
3586 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3588 return cgi_error_disabled(csp, rsp);
3591 err = get_number_param(csp, parameters, "p", &patternid);
3597 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3600 /* No filename specified, can't read file, modified, or out of memory. */
3601 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3606 cur_line = file->lines;
3608 while ((cur_line != NULL) && (line_number < patternid))
3610 prev_line = cur_line;
3611 cur_line = cur_line->next;
3615 if ( (cur_line == NULL)
3616 || (prev_line == NULL)
3617 || (cur_line->type != FILE_LINE_URL))
3619 /* Invalid "patternid" parameter */
3620 edit_free_file(file);
3621 return JB_ERR_CGI_PARAMS;
3624 /* At this point, the line to remove is in cur_line, and the previous
3625 * one is in prev_line
3628 /* Unlink cur_line */
3629 prev_line->next = cur_line->next;
3630 cur_line->next = NULL;
3633 edit_free_file_lines(cur_line);
3635 err = edit_write_file(file);
3638 /* Error writing file */
3639 edit_free_file(file);
3643 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3644 string_append(&target, file->identifier);
3646 edit_free_file(file);
3651 return JB_ERR_MEMORY;
3654 rsp->status = strdup("302 Local Redirect from Privoxy");
3655 if (rsp->status == NULL)
3658 return JB_ERR_MEMORY;
3660 err = enlist_unique_header(rsp->headers, "Location", target);
3667 /*********************************************************************
3669 * Function : cgi_edit_actions_section_remove
3671 * Description : CGI function that actually removes a whole section from
3672 * the actions file. The section must be empty first
3673 * (else JB_ERR_CGI_PARAMS).
3676 * 1 : csp = Current client state (buffers, headers, etc...)
3677 * 2 : rsp = http_response data structure for output
3678 * 3 : parameters = map of cgi parameters
3681 * f : (filename) Identifies the file to edit
3682 * v : (version) File's last-modified time
3683 * s : (section) Line number of section to edit
3685 * Returns : JB_ERR_OK on success
3686 * JB_ERR_MEMORY on out-of-memory
3687 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3688 * specified or not valid.
3690 *********************************************************************/
3691 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3692 struct http_response *rsp,
3693 const struct map *parameters)
3696 struct editable_file * file;
3697 struct file_line * cur_line;
3698 struct file_line * prev_line;
3699 unsigned line_number;
3703 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3705 return cgi_error_disabled(csp, rsp);
3708 err = get_number_param(csp, parameters, "s", §ionid);
3714 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3717 /* No filename specified, can't read file, modified, or out of memory. */
3718 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3722 cur_line = file->lines;
3725 while ((cur_line != NULL) && (line_number < sectionid))
3727 prev_line = cur_line;
3728 cur_line = cur_line->next;
3732 if ( (cur_line == NULL)
3733 || (cur_line->type != FILE_LINE_ACTION) )
3735 /* Invalid "sectionid" parameter */
3736 edit_free_file(file);
3737 return JB_ERR_CGI_PARAMS;
3740 if ( (cur_line->next != NULL)
3741 && (cur_line->next->type == FILE_LINE_URL) )
3743 /* Section not empty. */
3744 edit_free_file(file);
3745 return JB_ERR_CGI_PARAMS;
3748 /* At this point, the line to remove is in cur_line, and the previous
3749 * one is in prev_line
3752 /* Unlink cur_line */
3753 if (prev_line == NULL)
3755 /* Removing the first line from the file */
3756 file->lines = cur_line->next;
3760 prev_line->next = cur_line->next;
3762 cur_line->next = NULL;
3765 edit_free_file_lines(cur_line);
3767 err = edit_write_file(file);
3770 /* Error writing file */
3771 edit_free_file(file);
3775 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3776 string_append(&target, file->identifier);
3778 edit_free_file(file);
3783 return JB_ERR_MEMORY;
3786 rsp->status = strdup("302 Local Redirect from Privoxy");
3787 if (rsp->status == NULL)
3790 return JB_ERR_MEMORY;
3792 err = enlist_unique_header(rsp->headers, "Location", target);
3799 /*********************************************************************
3801 * Function : cgi_edit_actions_section_add
3803 * Description : CGI function that adds a new empty section to
3807 * 1 : csp = Current client state (buffers, headers, etc...)
3808 * 2 : rsp = http_response data structure for output
3809 * 3 : parameters = map of cgi parameters
3812 * f : (filename) Identifies the file to edit
3813 * v : (version) File's last-modified time
3814 * s : (section) Line number of section to add after, 0 for
3817 * Returns : JB_ERR_OK on success
3818 * JB_ERR_MEMORY on out-of-memory
3819 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3820 * specified or not valid.
3822 *********************************************************************/
3823 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3824 struct http_response *rsp,
3825 const struct map *parameters)
3828 struct file_line * new_line;
3830 struct editable_file * file;
3831 struct file_line * cur_line;
3832 unsigned line_number;
3836 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3838 return cgi_error_disabled(csp, rsp);
3841 err = get_number_param(csp, parameters, "s", §ionid);
3847 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3850 /* No filename specified, can't read file, modified, or out of memory. */
3851 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3855 cur_line = file->lines;
3859 /* Add to start of file */
3860 if (cur_line != NULL)
3862 /* There's something in the file, find the line before the first
3865 while ( (cur_line->next != NULL)
3866 && (cur_line->next->type != FILE_LINE_ACTION) )
3868 cur_line = cur_line->next;
3875 /* Add after stated section. */
3876 while ((cur_line != NULL) && (line_number < sectionid))
3878 cur_line = cur_line->next;
3882 if ( (cur_line == NULL)
3883 || (cur_line->type != FILE_LINE_ACTION))
3885 /* Invalid "sectionid" parameter */
3886 edit_free_file(file);
3887 return JB_ERR_CGI_PARAMS;
3890 /* Skip through the section to find the last line in it. */
3891 while ( (cur_line->next != NULL)
3892 && (cur_line->next->type != FILE_LINE_ACTION) )
3894 cur_line = cur_line->next;
3899 /* At this point, the last line in the previous section is in cur_line
3900 * - add after this. (Or if we need to add as the first line, cur_line
3904 new_text = strdup("{}");
3905 if (NULL == new_text)
3907 edit_free_file(file);
3908 return JB_ERR_MEMORY;
3911 /* Allocate the new line */
3912 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3913 if (new_line == NULL)
3916 edit_free_file(file);
3917 return JB_ERR_MEMORY;
3920 /* Fill in the data members of the new line */
3921 new_line->raw = NULL;
3922 new_line->prefix = NULL;
3923 new_line->unprocessed = new_text;
3924 new_line->type = FILE_LINE_ACTION;
3926 if (cur_line != NULL)
3928 /* Link new_line into the list, after cur_line */
3929 new_line->next = cur_line->next;
3930 cur_line->next = new_line;
3934 /* Link new_line into the list, as first line */
3935 new_line->next = file->lines;
3936 file->lines = new_line;
3939 /* Done making changes, now commit */
3941 err = edit_write_file(file);
3944 /* Error writing file */
3945 edit_free_file(file);
3949 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3950 string_append(&target, file->identifier);
3952 edit_free_file(file);
3957 return JB_ERR_MEMORY;
3960 rsp->status = strdup("302 Local Redirect from Privoxy");
3961 if (rsp->status == NULL)
3964 return JB_ERR_MEMORY;
3966 err = enlist_unique_header(rsp->headers, "Location", target);
3973 /*********************************************************************
3975 * Function : cgi_edit_actions_section_swap
3977 * Description : CGI function that swaps the order of two sections
3978 * in the actions file. Note that this CGI can actually
3979 * swap any two arbitrary sections, but the GUI interface
3980 * currently only allows consecutive sections to be
3984 * 1 : csp = Current client state (buffers, headers, etc...)
3985 * 2 : rsp = http_response data structure for output
3986 * 3 : parameters = map of cgi parameters
3989 * f : (filename) Identifies the file to edit
3990 * v : (version) File's last-modified time
3991 * s1 : (section1) Line number of first section to swap
3992 * s2 : (section2) Line number of second section to swap
3994 * Returns : JB_ERR_OK on success
3995 * JB_ERR_MEMORY on out-of-memory
3996 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3997 * specified or not valid.
3999 *********************************************************************/
4000 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
4001 struct http_response *rsp,
4002 const struct map *parameters)
4006 struct editable_file * file;
4007 struct file_line * cur_line;
4008 struct file_line * prev_line;
4009 struct file_line * line_before_section1;
4010 struct file_line * line_start_section1;
4011 struct file_line * line_end_section1;
4012 struct file_line * line_after_section1;
4013 struct file_line * line_before_section2;
4014 struct file_line * line_start_section2;
4015 struct file_line * line_end_section2;
4016 struct file_line * line_after_section2;
4017 unsigned line_number;
4021 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
4023 return cgi_error_disabled(csp, rsp);
4026 err = get_number_param(csp, parameters, "s1", §ion1);
4027 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
4033 if (section1 > section2)
4035 unsigned temp = section2;
4036 section2 = section1;
4040 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4043 /* No filename specified, can't read file, modified, or out of memory. */
4044 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4047 /* Start at the beginning... */
4049 cur_line = file->lines;
4052 /* ... find section1 ... */
4053 while ((cur_line != NULL) && (line_number < section1))
4055 prev_line = cur_line;
4056 cur_line = cur_line->next;
4060 if ( (cur_line == NULL)
4061 || (cur_line->type != FILE_LINE_ACTION) )
4063 /* Invalid "section1" parameter */
4064 edit_free_file(file);
4065 return JB_ERR_CGI_PARAMS;
4068 /* If no-op, we've validated params and can skip the rest. */
4069 if (section1 != section2)
4071 /* ... find the end of section1 ... */
4072 line_before_section1 = prev_line;
4073 line_start_section1 = cur_line;
4076 prev_line = cur_line;
4077 cur_line = cur_line->next;
4080 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4081 line_end_section1 = prev_line;
4082 line_after_section1 = cur_line;
4084 /* ... find section2 ... */
4085 while ((cur_line != NULL) && (line_number < section2))
4087 prev_line = cur_line;
4088 cur_line = cur_line->next;
4092 if ( (cur_line == NULL)
4093 || (cur_line->type != FILE_LINE_ACTION) )
4095 /* Invalid "section2" parameter */
4096 edit_free_file(file);
4097 return JB_ERR_CGI_PARAMS;
4100 /* ... find the end of section2 ... */
4101 line_before_section2 = prev_line;
4102 line_start_section2 = cur_line;
4105 prev_line = cur_line;
4106 cur_line = cur_line->next;
4109 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4110 line_end_section2 = prev_line;
4111 line_after_section2 = cur_line;
4113 /* Now have all the pointers we need. Do the swap. */
4115 /* Change the pointer to section1 to point to section2 instead */
4116 if (line_before_section1 == NULL)
4118 file->lines = line_start_section2;
4122 line_before_section1->next = line_start_section2;
4125 if (line_before_section2 == line_end_section1)
4127 /* Consecutive sections */
4128 line_end_section2->next = line_start_section1;
4132 line_end_section2->next = line_after_section1;
4133 line_before_section2->next = line_start_section1;
4136 /* Set the pointer from the end of section1 to the rest of the file */
4137 line_end_section1->next = line_after_section2;
4139 err = edit_write_file(file);
4142 /* Error writing file */
4143 edit_free_file(file);
4146 } /* END if (section1 != section2) */
4148 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4149 string_append(&target, file->identifier);
4151 edit_free_file(file);
4156 return JB_ERR_MEMORY;
4159 rsp->status = strdup("302 Local Redirect from Privoxy");
4160 if (rsp->status == NULL)
4163 return JB_ERR_MEMORY;
4165 err = enlist_unique_header(rsp->headers, "Location", target);
4172 /*********************************************************************
4174 * Function : cgi_toggle
4176 * Description : CGI function that adds a new empty section to
4180 * 1 : csp = Current client state (buffers, headers, etc...)
4181 * 2 : rsp = http_response data structure for output
4182 * 3 : parameters = map of cgi parameters
4185 * set : If present, how to change toggle setting:
4186 * "enable", "disable", "toggle", or none (default).
4187 * mini : If present, use mini reply template.
4189 * Returns : JB_ERR_OK on success
4190 * JB_ERR_MEMORY on out-of-memory
4192 *********************************************************************/
4193 jb_err cgi_toggle(struct client_state *csp,
4194 struct http_response *rsp,
4195 const struct map *parameters)
4197 struct map *exports;
4199 const char *template_name;
4205 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4207 return cgi_error_disabled(csp, rsp);
4210 mode = get_char_param(parameters, "set");
4217 else if (mode == 'D')
4222 else if (mode == 'T')
4225 g_bToggleIJB = !g_bToggleIJB;
4228 if (NULL == (exports = default_exports(csp, "toggle")))
4230 return JB_ERR_MEMORY;
4233 template_name = (get_char_param(parameters, "mini")
4237 return template_fill_for_cgi(csp, template_name, exports, rsp);
4241 /*********************************************************************
4243 * Function : javascriptify
4245 * Description : Converts a string into a form JavaScript will like.
4247 * Netscape 4's JavaScript sucks - it doesn't use
4248 * "id" parameters, so you have to set the "name"
4249 * used to submit a form element to something JavaScript
4250 * will like. (Or access the elements by index in an
4251 * array. That array contains >60 elements and will
4252 * be changed whenever we add a new action to the
4253 * editor, so I'm NOT going to use indexes that have
4254 * to be figured out by hand.)
4256 * Currently the only thing we have to worry about
4257 * is "-" ==> "_" conversion.
4259 * This is a length-preserving operation so it is
4260 * carried out in-place, no memory is allocated
4264 * 1 : identifier = String to make JavaScript-friendly.
4268 *********************************************************************/
4269 static void javascriptify(char * identifier)
4271 char * p = identifier;
4272 while (NULL != (p = strchr(p, '-')))
4279 /*********************************************************************
4281 * Function : actions_to_radio
4283 * Description : Converts a actionsfile entry into settings for
4284 * radio buttons and edit boxes on a HTML form.
4287 * 1 : exports = List of substitutions to add to.
4288 * 2 : action = Action to read
4290 * Returns : JB_ERR_OK on success
4291 * JB_ERR_MEMORY on out-of-memory
4293 *********************************************************************/
4294 static jb_err actions_to_radio(struct map * exports,
4295 const struct action_spec *action)
4297 unsigned mask = action->mask;
4298 unsigned add = action->add;
4306 mask = action->mask;
4309 /* sanity - prevents "-feature +feature" */
4313 #define DEFINE_ACTION_BOOL(name, bit) \
4314 if (!(mask & bit)) \
4316 current_mode = 'n'; \
4318 else if (add & bit) \
4320 current_mode = 'y'; \
4324 current_mode = 'x'; \
4326 if (map_radio(exports, name, "ynx", current_mode)) \
4328 return JB_ERR_MEMORY; \
4331 #define DEFINE_ACTION_STRING(name, bit, index) \
4332 DEFINE_ACTION_BOOL(name, bit); \
4335 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4338 checked = !strcmp(action->string[index], value); \
4342 checked = is_default; \
4344 mapped_param |= checked; \
4345 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4347 return JB_ERR_MEMORY; \
4350 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4351 if (map(exports, name "-param-custom", 1, \
4352 ((!mapped_param) ? "checked" : ""), 1)) \
4354 return JB_ERR_MEMORY; \
4356 if (map(exports, name "-param", 1, \
4357 (((add & bit) && !mapped_param) ? \
4358 action->string[index] : default_val), 1)) \
4360 return JB_ERR_MEMORY; \
4363 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4364 if (map(exports, name "-param", 1, \
4365 ((add & bit) ? action->string[index] : default_val), 1)) \
4367 return JB_ERR_MEMORY; \
4370 #define DEFINE_ACTION_MULTI(name, index) \
4371 if (action->multi_add[index]->first) \
4373 current_mode = 'y'; \
4375 else if (action->multi_remove_all[index]) \
4377 current_mode = 'n'; \
4379 else if (action->multi_remove[index]->first) \
4381 current_mode = 'y'; \
4385 current_mode = 'x'; \
4387 if (map_radio(exports, name, "ynx", current_mode)) \
4389 return JB_ERR_MEMORY; \
4392 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4394 #include "actionlist.h"
4396 #undef DEFINE_ACTION_MULTI
4397 #undef DEFINE_ACTION_STRING
4398 #undef DEFINE_ACTION_BOOL
4399 #undef DEFINE_ACTION_ALIAS
4400 #undef DEFINE_CGI_PARAM_CUSTOM
4401 #undef DEFINE_CGI_PARAM_RADIO
4402 #undef DEFINE_CGI_PARAM_NO_RADIO
4408 /*********************************************************************
4410 * Function : actions_from_radio
4412 * Description : Converts a map of parameters passed to a CGI function
4413 * into an actionsfile entry.
4416 * 1 : parameters = parameters to the CGI call
4417 * 2 : action = Action to change. Must be valid before
4418 * the call, actions not specified will be
4421 * Returns : JB_ERR_OK on success
4422 * JB_ERR_MEMORY on out-of-memory
4424 *********************************************************************/
4425 static jb_err actions_from_radio(const struct map * parameters,
4426 struct action_spec *action)
4428 static int first_time = 1;
4432 const char * js_name;
4433 jb_err err = JB_ERR_OK;
4438 /* Statics are generally a potential race condition,
4439 * but in this case we're safe and don't need semaphores.
4440 * Be careful if you modify this function.
4444 #define JAVASCRIPTIFY(dest_var, string) \
4446 static char js_name_arr[] = string; \
4449 javascriptify(js_name_arr); \
4451 dest_var = js_name_arr; \
4454 #define DEFINE_ACTION_BOOL(name, bit) \
4455 JAVASCRIPTIFY(js_name, name); \
4456 ch = get_char_param(parameters, js_name); \
4459 action->add |= bit; \
4460 action->mask |= bit; \
4462 else if (ch == 'N') \
4464 action->add &= ~bit; \
4465 action->mask &= ~bit; \
4467 else if (ch == 'X') \
4469 action->add &= ~bit; \
4470 action->mask |= bit; \
4473 #define DEFINE_ACTION_STRING(name, bit, index) \
4474 JAVASCRIPTIFY(js_name, name); \
4475 ch = get_char_param(parameters, js_name); \
4479 JAVASCRIPTIFY(js_name, name "-mode"); \
4480 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4481 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4483 JAVASCRIPTIFY(js_name, name "-param"); \
4484 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4486 if (param != NULL) \
4488 if (NULL == (param_dup = strdup(param))) \
4490 return JB_ERR_MEMORY; \
4492 freez(action->string[index]); \
4493 action->add |= bit; \
4494 action->mask |= bit; \
4495 action->string[index] = param_dup; \
4498 else if (ch == 'N') \
4500 if (action->add & bit) \
4502 freez(action->string[index]); \
4504 action->add &= ~bit; \
4505 action->mask &= ~bit; \
4507 else if (ch == 'X') \
4509 if (action->add & bit) \
4511 freez(action->string[index]); \
4513 action->add &= ~bit; \
4514 action->mask |= bit; \
4517 #define DEFINE_ACTION_MULTI(name, index) \
4518 JAVASCRIPTIFY(js_name, name); \
4519 ch = get_char_param(parameters, js_name); \
4524 else if (ch == 'N') \
4526 list_remove_all(action->multi_add[index]); \
4527 list_remove_all(action->multi_remove[index]); \
4528 action->multi_remove_all[index] = 1; \
4530 else if (ch == 'X') \
4532 list_remove_all(action->multi_add[index]); \
4533 list_remove_all(action->multi_remove[index]); \
4534 action->multi_remove_all[index] = 0; \
4537 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4539 #include "actionlist.h"
4541 #undef DEFINE_ACTION_MULTI
4542 #undef DEFINE_ACTION_STRING
4543 #undef DEFINE_ACTION_BOOL
4544 #undef DEFINE_ACTION_ALIAS
4545 #undef JAVASCRIPTIFY
4553 #endif /* def FEATURE_CGI_EDIT_ACTIONS */