1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.27 2002/03/26 23:06:04 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.27 2002/03/26 23:06:04 jongfoster
46 * Removing duplicate @ifs on the toggle page
48 * Revision 1.26 2002/03/26 22:59:17 jongfoster
49 * Fixing /toggle to display status consistently.
51 * Revision 1.25 2002/03/26 22:29:54 swa
52 * we have a new homepage!
54 * Revision 1.24 2002/03/24 15:23:33 jongfoster
57 * Revision 1.23 2002/03/24 13:32:41 swa
58 * name change related issues
60 * Revision 1.22 2002/03/24 13:25:43 swa
61 * name change related issues
63 * Revision 1.21 2002/03/22 18:02:48 jongfoster
64 * Fixing remote toggle
66 * Revision 1.20 2002/03/16 20:28:34 oes
67 * Added descriptions to the filters so users will know what they select in the cgi editor
69 * Revision 1.19 2002/03/16 18:38:14 jongfoster
70 * Stopping stupid or malicious users from breaking the actions
71 * file using the web-based editor.
73 * Revision 1.18 2002/03/16 14:57:44 jongfoster
74 * Full support for enabling/disabling modular filters.
76 * Revision 1.17 2002/03/16 14:26:42 jongfoster
77 * First version of modular filters support - READ ONLY!
78 * Fixing a double-free bug in the out-of-memory handling in map_radio().
80 * Revision 1.16 2002/03/07 03:46:17 oes
81 * Fixed compiler warnings
83 * Revision 1.15 2002/03/06 22:54:35 jongfoster
84 * Automated function-comment nitpicking.
86 * Revision 1.14 2002/03/05 00:24:51 jongfoster
87 * Patch to always edit the current actions file.
89 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
90 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
92 * Revision 1.12 2002/03/03 09:18:03 joergs
93 * Made jumbjuster work on AmigaOS again.
95 * Revision 1.11 2002/01/23 01:03:31 jongfoster
96 * Fixing gcc [CygWin] compiler warnings
98 * Revision 1.10 2002/01/23 00:22:59 jongfoster
99 * Adding new function cgi_edit_actions_section_swap(), to reorder
102 * Adding get_url_spec_param() to get a validated URL pattern.
104 * Moving edit_read_line() out of this file and into loaders.c.
106 * Adding missing html_encode() to many CGI functions.
108 * Moving the functions that #include actionlist.h to the end of the file,
109 * because the Visual C++ 97 debugger gets extremely confused if you try
110 * to debug any code that comes after them in the file.
112 * Major optimizations in cgi_edit_actions_list() to reduce the size of
113 * the generated HTML (down 40% from 550k to 304k), with major side-effects
114 * throughout the editor and templates. In particular, the length of the
115 * URLs throughout the editor has been drastically reduced, by cutting
116 * paramater names down to 1 character and CGI names down to 3-4
117 * characters, by removing all non-essential CGI paramaters even at the
118 * expense of having to re-read the actions file for the most trivial
119 * page, and by using relative rather than absolute URLs. This means
120 * that this (typical example):
122 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
123 * filename=ijb&ver=1011487572&section=12&pattern=13
124 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
128 * <a href="eau?f=ijb&v=1011487572&p=13">
130 * Revision 1.9 2002/01/17 20:56:22 jongfoster
131 * Replacing hard references to the URL of the config interface
132 * with #defines from project.h
134 * Revision 1.8 2001/11/30 23:35:51 jongfoster
135 * Renaming actionsfile to ijb.action
137 * Revision 1.7 2001/11/13 00:28:24 jongfoster
138 * - Renaming parameters from edit-actions-for-url so that they only
139 * contain legal JavaScript characters. If we wanted to write
140 * JavaScript that worked with Netscape 4, this is nessacery.
141 * (Note that at the moment the JavaScript doesn't actually work
142 * with Netscape 4, but now this is purely a template issue, not
143 * one affecting code).
144 * - Adding new CGIs for use by non-JavaScript browsers:
145 * edit-actions-url-form
146 * edit-actions-add-url-form
147 * edit-actions-remove-url-form
150 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
151 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
152 * by and __OS2__ ifdef.
154 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
155 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
156 * threads to call select() simultaneously. So, it's time to do a real, live,
157 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
158 * (native). Both versions will work, but using __OS2__ offers multi-threading.
160 * Revision 1.4 2001/10/23 21:48:19 jongfoster
161 * Cleaning up error handling in CGI functions - they now send back
162 * a HTML error page and should never cause a FATAL error. (Fixes one
163 * potential source of "denial of service" attacks).
165 * CGI actions file editor that works and is actually useful.
167 * Ability to toggle JunkBuster remotely using a CGI call.
169 * You can turn off both the above features in the main configuration
170 * file, e.g. if you are running a multi-user proxy.
172 * Revision 1.3 2001/10/14 22:12:49 jongfoster
173 * New version of CGI-based actionsfile editor.
174 * Major changes, including:
175 * - Completely new file parser and file output routines
176 * - edit-actions CGI renamed edit-actions-for-url
177 * - All CGIs now need a filename parameter, except for...
178 * - New CGI edit-actions which doesn't need a filename,
179 * to allow you to start the editor up.
180 * - edit-actions-submit now works, and now automatically
181 * redirects you back to the main edit-actions-list handler.
183 * Revision 1.2 2001/09/16 17:05:14 jongfoster
184 * Removing unused #include showarg.h
186 * Revision 1.1 2001/09/16 15:47:37 jongfoster
187 * First version of CGI-based edit interface. This is very much a
188 * work-in-progress, and you can't actually use it to edit anything
189 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
190 * to have any effect.
193 **********************************************************************/
199 * FIXME: Following includes copied from cgi.c - which are actually needed?
204 #include <sys/types.h>
209 #include <sys/stat.h>
212 #define snprintf _snprintf
213 #endif /* def _WIN32 */
218 #include "cgisimple.h"
222 #include "miscutil.h"
226 /* loadcfg.h is for g_bToggleIJB only */
227 #include "urlmatch.h"
229 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
232 #ifdef FEATURE_CGI_EDIT_ACTIONS
236 struct file_line * next;
244 struct action_spec action[1];
253 /* Add more data types here... e.g.
256 struct url_spec url[1];
260 struct action_spec action[1];
269 #define FILE_LINE_UNPROCESSED 1
270 #define FILE_LINE_BLANK 2
271 #define FILE_LINE_ALIAS_HEADER 3
272 #define FILE_LINE_ALIAS_ENTRY 4
273 #define FILE_LINE_ACTION 5
274 #define FILE_LINE_URL 6
275 #define FILE_LINE_SETTINGS_HEADER 7
276 #define FILE_LINE_SETTINGS_ENTRY 8
277 #define FILE_LINE_DESCRIPTION_HEADER 9
278 #define FILE_LINE_DESCRIPTION_ENTRY 10
283 struct file_line * lines;
284 const char * filename; /* Full pathname - e.g. "/etc/privoxy/wibble.action" */
285 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
286 /* Pre-encoded with url_encode() for ease of use. */
287 const char * version_str; /* Last modification time, as a string. For CGI param */
288 /* Can be used in URL without using url_param(). */
289 unsigned version; /* Last modification time - prevents chaos with
290 * the browser's "back" button. Note that this is a
291 * time_t cast to an unsigned. When comparing, always
292 * cast the time_t to an unsigned, and *NOT* vice-versa.
293 * This may lose the top few bits, but they're not
294 * significant anyway.
296 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
297 * Note that changing this after the file has been
298 * read in will cause a mess.
300 struct file_line * parse_error; /* On parse error, this is the offending line. */
301 const char * parse_error_text; /* On parse error, this is the problem.
302 * (Statically allocated) */
305 #define CGI_ACTION_PARAM_LEN_MAX 500
307 /* FIXME: Following non-static functions should be prototyped in .h or made static */
309 /* Functions to read and write arbitrary config files */
310 jb_err edit_read_file(struct client_state *csp,
311 const struct map *parameters,
314 struct editable_file **pfile);
315 jb_err edit_write_file(struct editable_file * file);
316 void edit_free_file(struct editable_file * file);
318 /* Functions to read and write actions files */
319 jb_err edit_parse_actions_file(struct editable_file * file);
320 jb_err edit_read_actions_file(struct client_state *csp,
321 struct http_response *rsp,
322 const struct map *parameters,
324 struct editable_file **pfile);
327 jb_err cgi_error_modified(struct client_state *csp,
328 struct http_response *rsp,
329 const char *filename);
330 jb_err cgi_error_parse(struct client_state *csp,
331 struct http_response *rsp,
332 struct editable_file *file);
333 jb_err cgi_error_file(struct client_state *csp,
334 struct http_response *rsp,
335 const char *filename);
336 jb_err cgi_error_disabled(struct client_state *csp,
337 struct http_response *rsp);
339 /* Internal arbitrary config file support functions */
340 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
341 static void edit_free_file_lines(struct file_line * first_line);
343 /* Internal actions file support functions */
344 static int match_actions_file_header_line(const char * line, const char * name);
345 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
347 /* Internal parameter parsing functions */
348 static jb_err get_file_name_param(struct client_state *csp,
349 const struct map *parameters,
350 const char *param_name,
353 const char **pparam);
354 static jb_err get_number_param(struct client_state *csp,
355 const struct map *parameters,
358 static jb_err get_url_spec_param(struct client_state *csp,
359 const struct map *parameters,
362 static jb_err get_string_param(const struct map *parameters,
363 const char *param_name,
364 const char **pparam);
366 /* Internal actionsfile <==> HTML conversion functions */
367 static jb_err map_radio(struct map * exports,
368 const char * optionname,
371 static jb_err actions_to_radio(struct map * exports,
372 const struct action_spec *action);
373 static jb_err actions_from_radio(const struct map * parameters,
374 struct action_spec *action);
377 static jb_err map_copy_parameter_html(struct map *out,
378 const struct map *in,
380 #if 0 /* unused function */
381 static jb_err map_copy_parameter_url(struct map *out,
382 const struct map *in,
384 #endif /* unused function */
386 /*********************************************************************
388 * Function : map_copy_parameter_html
390 * Description : Copy a CGI parameter from one map to another, HTML
394 * 1 : out = target map
395 * 2 : in = source map
396 * 3 : name = name of cgi parameter to copy
398 * Returns : JB_ERR_OK on success
399 * JB_ERR_MEMORY on out-of-memory
400 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
403 *********************************************************************/
404 static jb_err map_copy_parameter_html(struct map *out,
405 const struct map *in,
415 value = lookup(in, name);
416 err = map(out, name, 1, html_encode(value), 0);
423 else if (*value == '\0')
425 return JB_ERR_CGI_PARAMS;
434 #if 0 /* unused function */
435 /*********************************************************************
437 * Function : map_copy_parameter_html
439 * Description : Copy a CGI parameter from one map to another, URL
443 * 1 : out = target map
444 * 2 : in = source map
445 * 3 : name = name of cgi parameter to copy
447 * Returns : JB_ERR_OK on success
448 * JB_ERR_MEMORY on out-of-memory
449 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
452 *********************************************************************/
453 static jb_err map_copy_parameter_url(struct map *out,
454 const struct map *in,
464 value = lookup(in, name);
465 err = map(out, name, 1, url_encode(value), 0);
472 else if (*value == '\0')
474 return JB_ERR_CGI_PARAMS;
481 #endif /* 0 - unused function */
483 /*********************************************************************
485 * Function : cgi_edit_actions_url_form
487 * Description : CGI function that displays a form for
491 * 1 : csp = Current client state (buffers, headers, etc...)
492 * 2 : rsp = http_response data structure for output
493 * 3 : parameters = map of cgi parameters
496 * f : (filename) Identifies the file to edit
497 * v : (version) File's last-modified time
498 * p : (pattern) Line number of pattern to edit
500 * Returns : JB_ERR_OK on success
501 * JB_ERR_MEMORY on out-of-memory
502 * JB_ERR_CGI_PARAMS if the CGI parameters are not
503 * specified or not valid.
505 *********************************************************************/
506 jb_err cgi_edit_actions_url_form(struct client_state *csp,
507 struct http_response *rsp,
508 const struct map *parameters)
510 struct map * exports;
512 struct editable_file * file;
513 struct file_line * cur_line;
514 unsigned line_number;
521 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
523 return cgi_error_disabled(csp, rsp);
526 err = get_number_param(csp, parameters, "p", &patternid);
532 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
535 /* No filename specified, can't read file, modified, or out of memory. */
536 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
539 cur_line = file->lines;
541 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
543 cur_line = cur_line->next;
546 if ( (cur_line == NULL)
547 || (line_number != patternid)
549 || (cur_line->type != FILE_LINE_URL))
551 /* Invalid "patternid" parameter */
552 edit_free_file(file);
553 return JB_ERR_CGI_PARAMS;
556 if (NULL == (exports = default_exports(csp, NULL)))
558 edit_free_file(file);
559 return JB_ERR_MEMORY;
562 err = map(exports, "f", 1, file->identifier, 1);
563 if (!err) err = map(exports, "v", 1, file->version_str, 1);
564 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
565 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
567 edit_free_file(file);
575 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
579 /*********************************************************************
581 * Function : cgi_edit_actions_add_url_form
583 * Description : CGI function that displays a form for
587 * 1 : csp = Current client state (buffers, headers, etc...)
588 * 2 : rsp = http_response data structure for output
589 * 3 : parameters = map of cgi parameters
592 * f : (filename) Identifies the file to edit
593 * v : (version) File's last-modified time
594 * s : (section) Line number of section to edit
596 * Returns : JB_ERR_OK on success
597 * JB_ERR_MEMORY on out-of-memory
598 * JB_ERR_CGI_PARAMS if the CGI parameters are not
599 * specified or not valid.
601 *********************************************************************/
602 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
603 struct http_response *rsp,
604 const struct map *parameters)
613 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
615 return cgi_error_disabled(csp, rsp);
618 if (NULL == (exports = default_exports(csp, NULL)))
620 return JB_ERR_MEMORY;
623 err = map_copy_parameter_html(exports, parameters, "f");
624 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
625 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
633 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
637 /*********************************************************************
639 * Function : cgi_edit_actions_remove_url_form
641 * Description : CGI function that displays a form for
645 * 1 : csp = Current client state (buffers, headers, etc...)
646 * 2 : rsp = http_response data structure for output
647 * 3 : parameters = map of cgi parameters
650 * f : (filename) Identifies the file to edit
651 * v : (version) File's last-modified time
652 * p : (pattern) Line number of pattern to edit
654 * Returns : JB_ERR_OK on success
655 * JB_ERR_MEMORY on out-of-memory
656 * JB_ERR_CGI_PARAMS if the CGI parameters are not
657 * specified or not valid.
659 *********************************************************************/
660 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
661 struct http_response *rsp,
662 const struct map *parameters)
664 struct map * exports;
666 struct editable_file * file;
667 struct file_line * cur_line;
668 unsigned line_number;
675 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
677 return cgi_error_disabled(csp, rsp);
680 err = get_number_param(csp, parameters, "p", &patternid);
686 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
689 /* No filename specified, can't read file, modified, or out of memory. */
690 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
693 cur_line = file->lines;
695 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
697 cur_line = cur_line->next;
700 if ( (cur_line == NULL)
701 || (line_number != patternid)
703 || (cur_line->type != FILE_LINE_URL))
705 /* Invalid "patternid" parameter */
706 edit_free_file(file);
707 return JB_ERR_CGI_PARAMS;
710 if (NULL == (exports = default_exports(csp, NULL)))
712 edit_free_file(file);
713 return JB_ERR_MEMORY;
716 err = map(exports, "f", 1, file->identifier, 1);
717 if (!err) err = map(exports, "v", 1, file->version_str, 1);
718 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
719 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
721 edit_free_file(file);
729 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
733 /*********************************************************************
735 * Function : edit_write_file
737 * Description : Write a complete file to disk.
740 * 1 : filename = File to write to.
741 * 2 : file = Data structure to write.
743 * Returns : JB_ERR_OK on success
744 * JB_ERR_FILE on error writing to file.
745 * JB_ERR_MEMORY on out of memory
747 *********************************************************************/
748 jb_err edit_write_file(struct editable_file * file)
751 struct file_line * cur_line;
752 struct stat statbuf[1];
753 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
754 digits in time_t, assuming this is a 64-bit
755 machine, plus null terminator, plus one
759 assert(file->filename);
761 #if defined(AMIGA) || defined(__OS2__)
762 if (NULL == (fp = fopen(file->filename, "w")))
764 if (NULL == (fp = fopen(file->filename, "wt")))
765 #endif /* def AMIGA */
770 cur_line = file->lines;
771 while (cur_line != NULL)
775 if (fputs(cur_line->raw, fp) < 0)
783 if (cur_line->prefix)
785 if (fputs(cur_line->prefix, fp) < 0)
791 if (cur_line->unprocessed)
793 /* This should be a single line - sanity check. */
794 assert(NULL == strchr(cur_line->unprocessed, '\r'));
795 assert(NULL == strchr(cur_line->unprocessed, '\n'));
797 if (NULL != strchr(cur_line->unprocessed, '#'))
799 /* Must quote '#' characters */
806 /* Count number of # characters, so we know length of output string */
807 src = cur_line->unprocessed;
808 while (NULL != (src = strchr(src, '#')))
815 /* Allocate new memory for string */
816 len = strlen(cur_line->unprocessed);
817 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
819 /* Uh oh, just trashed file! */
821 return JB_ERR_MEMORY;
824 /* Loop through string from end */
825 src = cur_line->unprocessed + len;
826 dest = str + len + numhash;
827 for ( ; len >= 0; len--)
829 if ((*dest-- = *src--) == '#')
833 assert(numhash >= 0);
836 assert(numhash == 0);
837 assert(src + 1 == cur_line->unprocessed);
838 assert(dest + 1 == str);
840 if (fputs(str, fp) < 0)
851 /* Can write without quoting '#' characters. */
852 if (fputs(cur_line->unprocessed, fp) < 0)
858 if (fputs(NEWLINE(file->newline), fp) < 0)
866 /* FIXME: Write data from file->data->whatever */
870 cur_line = cur_line->next;
876 /* Update the version stamp in the file structure, since we just
877 * wrote to the file & changed it's date.
879 if (stat(file->filename, statbuf) < 0)
881 /* Error, probably file not found. */
884 file->version = (unsigned)statbuf->st_mtime;
886 /* Correct file->version_str */
887 freez(file->version_str);
888 snprintf(version_buf, 22, "%u", file->version);
889 version_buf[21] = '\0';
890 file->version_str = strdup(version_buf);
891 if (version_buf == NULL)
893 return JB_ERR_MEMORY;
900 /*********************************************************************
902 * Function : edit_free_file
904 * Description : Free a complete file in memory.
907 * 1 : file = Data structure to free.
911 *********************************************************************/
912 void edit_free_file(struct editable_file * file)
916 /* Silently ignore NULL pointer */
920 edit_free_file_lines(file->lines);
921 freez(file->filename);
922 freez(file->identifier);
923 freez(file->version_str);
925 file->parse_error_text = NULL; /* Statically allocated */
926 file->parse_error = NULL;
932 /*********************************************************************
934 * Function : edit_free_file
936 * Description : Free an entire linked list of file lines.
939 * 1 : first_line = Data structure to free.
943 *********************************************************************/
944 static void edit_free_file_lines(struct file_line * first_line)
946 struct file_line * next_line;
948 while (first_line != NULL)
950 next_line = first_line->next;
951 first_line->next = NULL;
952 freez(first_line->raw);
953 freez(first_line->prefix);
954 freez(first_line->unprocessed);
955 switch(first_line->type)
957 case 0: /* special case if memory zeroed */
958 case FILE_LINE_UNPROCESSED:
959 case FILE_LINE_BLANK:
960 case FILE_LINE_ALIAS_HEADER:
961 case FILE_LINE_SETTINGS_HEADER:
962 case FILE_LINE_DESCRIPTION_HEADER:
963 case FILE_LINE_DESCRIPTION_ENTRY:
964 case FILE_LINE_ALIAS_ENTRY:
966 /* No data is stored for these */
969 case FILE_LINE_ACTION:
970 free_action(first_line->data.action);
973 case FILE_LINE_SETTINGS_ENTRY:
974 freez(first_line->data.setting.name);
975 freez(first_line->data.setting.svalue);
978 /* Should never happen */
982 first_line->type = 0; /* paranoia */
984 first_line = next_line;
989 /*********************************************************************
991 * Function : match_actions_file_header_line
993 * Description : Match an actions file {{header}} line
996 * 1 : line = String from file
997 * 2 : name = Header to match against
999 * Returns : 0 iff they match.
1001 *********************************************************************/
1002 static int match_actions_file_header_line(const char * line, const char * name)
1010 if ((line[0] != '{') || (line[1] != '{'))
1016 /* Look for optional whitespace */
1017 while ( (*line == ' ') || (*line == '\t') )
1022 /* Look for the specified name (case-insensitive) */
1024 if (0 != strncmpic(line, name, len))
1030 /* Look for optional whitespace */
1031 while ( (*line == ' ') || (*line == '\t') )
1036 /* Look for "}}" and end of string*/
1037 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1047 /*********************************************************************
1049 * Function : match_actions_file_header_line
1051 * Description : Match an actions file {{header}} line
1054 * 1 : line = String from file. Must not start with
1055 * whitespace (else infinite loop!)
1056 * 2 : name = Destination for name
1057 * 2 : name = Destination for value
1059 * Returns : JB_ERR_OK on success
1060 * JB_ERR_MEMORY on out-of-memory
1061 * JB_ERR_PARSE if there's no "=" sign, or if there's
1062 * nothing before the "=" sign (but empty
1063 * values *after* the "=" sign are legal).
1065 *********************************************************************/
1066 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1068 const char * name_end;
1069 const char * value_start;
1075 assert(*line != ' ');
1076 assert(*line != '\t');
1081 value_start = strchr(line, '=');
1082 if ((value_start == NULL) || (value_start == line))
1084 return JB_ERR_PARSE;
1087 name_end = value_start - 1;
1089 /* Eat any whitespace before the '=' */
1090 while ((*name_end == ' ') || (*name_end == '\t'))
1093 * we already know we must have at least 1 non-ws char
1094 * at start of buf - no need to check
1099 name_len = name_end - line + 1; /* Length excluding \0 */
1100 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1102 return JB_ERR_MEMORY;
1104 strncpy(*pname, line, name_len);
1105 (*pname)[name_len] = '\0';
1107 /* Eat any the whitespace after the '=' */
1109 while ((*value_start == ' ') || (*value_start == '\t'))
1114 if (NULL == (*pvalue = strdup(value_start)))
1118 return JB_ERR_MEMORY;
1125 /*********************************************************************
1127 * Function : edit_parse_actions_file
1129 * Description : Parse an actions file in memory.
1131 * Passed linked list must have the "data" member
1132 * zeroed, and must contain valid "next" and
1133 * "unprocessed" fields. The "raw" and "prefix"
1134 * fields are ignored, and "type" is just overwritten.
1136 * Note that on error the file may have been
1140 * 1 : file = Actions file to be parsed in-place.
1142 * Returns : JB_ERR_OK on success
1143 * JB_ERR_MEMORY on out-of-memory
1144 * JB_ERR_PARSE on error
1146 *********************************************************************/
1147 jb_err edit_parse_actions_file(struct editable_file * file)
1149 struct file_line * cur_line;
1151 const char * text; /* Text from a line */
1152 char * name; /* For lines of the form name=value */
1153 char * value; /* For lines of the form name=value */
1154 struct action_alias * alias_list = NULL;
1155 jb_err err = JB_ERR_OK;
1157 /* alias_list contains the aliases defined in this file.
1158 * It might be better to use the "file_line.data" fields
1159 * in the relavent places instead.
1162 cur_line = file->lines;
1164 /* A note about blank line support: Blank lines should only
1165 * ever occur as the last line in the file. This function
1166 * is more forgiving than that - FILE_LINE_BLANK can occur
1170 /* Skip leading blanks. Should only happen if file is
1171 * empty (which is valid, but pointless).
1173 while ( (cur_line != NULL)
1174 && (cur_line->unprocessed[0] == '\0') )
1177 cur_line->type = FILE_LINE_BLANK;
1178 cur_line = cur_line->next;
1181 if ( (cur_line != NULL)
1182 && (cur_line->unprocessed[0] != '{') )
1184 /* File doesn't start with a header */
1185 file->parse_error = cur_line;
1186 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1187 return JB_ERR_PARSE;
1190 if ( (cur_line != NULL) && (0 ==
1191 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1193 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1195 cur_line = cur_line->next;
1196 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1198 if (cur_line->unprocessed[0])
1200 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1202 err = split_line_on_equals(cur_line->unprocessed,
1203 &cur_line->data.setting.name,
1204 &cur_line->data.setting.svalue);
1205 if (err == JB_ERR_MEMORY)
1209 else if (err != JB_ERR_OK)
1211 /* Line does not contain a name=value pair */
1212 file->parse_error = cur_line;
1213 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1214 return JB_ERR_PARSE;
1219 cur_line->type = FILE_LINE_BLANK;
1221 cur_line = cur_line->next;
1225 if ( (cur_line != NULL) && (0 ==
1226 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1228 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1230 cur_line = cur_line->next;
1231 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1233 if (cur_line->unprocessed[0])
1235 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1239 cur_line->type = FILE_LINE_BLANK;
1241 cur_line = cur_line->next;
1245 if ( (cur_line != NULL) && (0 ==
1246 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1248 cur_line->type = FILE_LINE_ALIAS_HEADER;
1250 cur_line = cur_line->next;
1251 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1253 if (cur_line->unprocessed[0])
1255 /* define an alias */
1256 struct action_alias * new_alias;
1258 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1260 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1261 if (err == JB_ERR_MEMORY)
1265 else if (err != JB_ERR_OK)
1267 /* Line does not contain a name=value pair */
1268 file->parse_error = cur_line;
1269 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1270 return JB_ERR_PARSE;
1273 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1278 free_alias_list(alias_list);
1279 return JB_ERR_MEMORY;
1282 err = get_actions(value, alias_list, new_alias->action);
1285 /* Invalid action or out of memory */
1289 free_alias_list(alias_list);
1290 if (err == JB_ERR_MEMORY)
1296 /* Line does not contain a name=value pair */
1297 file->parse_error = cur_line;
1298 file->parse_error_text = "This alias does not specify a valid set of actions.";
1299 return JB_ERR_PARSE;
1305 new_alias->name = name;
1308 new_alias->next = alias_list;
1309 alias_list = new_alias;
1313 cur_line->type = FILE_LINE_BLANK;
1315 cur_line = cur_line->next;
1319 /* Header done, process the main part of the file */
1320 while (cur_line != NULL)
1322 /* At this point, (cur_line->unprocessed[0] == '{') */
1323 assert(cur_line->unprocessed[0] == '{');
1324 text = cur_line->unprocessed + 1;
1325 len = strlen(text) - 1;
1326 if (text[len] != '}')
1328 /* No closing } on header */
1329 free_alias_list(alias_list);
1330 file->parse_error = cur_line;
1331 file->parse_error_text = "Headers starting with '{' must have a "
1332 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1333 "must close with two brackets ('}}').";
1334 return JB_ERR_PARSE;
1339 /* An invalid {{ header. */
1340 free_alias_list(alias_list);
1341 file->parse_error = cur_line;
1342 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1343 "Please remember that the system (two-bracket) headers must "
1344 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1345 "and must appear before any actions (one-bracket) headers. "
1346 "Also note that system headers may not be repeated.";
1347 return JB_ERR_PARSE;
1350 while ( (*text == ' ') || (*text == '\t') )
1356 && ( (text[len - 1] == ' ')
1357 || (text[len - 1] == '\t') ) )
1362 cur_line->type = FILE_LINE_ACTION;
1364 /* Remove {} and make copy */
1365 if (NULL == (value = (char *) malloc(len + 1)))
1368 free_alias_list(alias_list);
1369 return JB_ERR_MEMORY;
1371 strncpy(value, text, len);
1375 err = get_actions(value, alias_list, cur_line->data.action);
1378 /* Invalid action or out of memory */
1380 free_alias_list(alias_list);
1381 if (err == JB_ERR_MEMORY)
1387 /* Line does not contain a name=value pair */
1388 file->parse_error = cur_line;
1389 file->parse_error_text = "This header does not specify a valid set of actions.";
1390 return JB_ERR_PARSE;
1394 /* Done with string - it was clobbered anyway */
1397 /* Process next line */
1398 cur_line = cur_line->next;
1400 /* Loop processing URL patterns */
1401 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1403 if (cur_line->unprocessed[0])
1405 /* Could parse URL here, but this isn't currently needed */
1407 cur_line->type = FILE_LINE_URL;
1411 cur_line->type = FILE_LINE_BLANK;
1413 cur_line = cur_line->next;
1415 } /* End main while(cur_line != NULL) loop */
1417 free_alias_list(alias_list);
1423 /*********************************************************************
1425 * Function : edit_read_file_lines
1427 * Description : Read all the lines of a file into memory.
1428 * Handles whitespace, comments and line continuation.
1431 * 1 : fp = File to read from. On return, this will be
1432 * at EOF but it will not have been closed.
1433 * 2 : pfile = Destination for a linked list of file_lines.
1434 * Will be set to NULL on error.
1436 * Returns : JB_ERR_OK on success
1437 * JB_ERR_MEMORY on out-of-memory
1439 *********************************************************************/
1440 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1442 struct file_line * first_line; /* Keep for return value or to free */
1443 struct file_line * cur_line; /* Current line */
1444 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1452 cur_line = first_line = zalloc(sizeof(struct file_line));
1453 if (cur_line == NULL)
1455 return JB_ERR_MEMORY;
1458 cur_line->type = FILE_LINE_UNPROCESSED;
1460 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1463 /* Out of memory or empty file. */
1464 /* Note that empty file is not an error we propogate up */
1466 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1471 prev_line = cur_line;
1472 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1473 if (cur_line == NULL)
1476 edit_free_file_lines(first_line);
1477 return JB_ERR_MEMORY;
1480 cur_line->type = FILE_LINE_UNPROCESSED;
1482 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1483 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1486 edit_free_file_lines(first_line);
1487 return JB_ERR_MEMORY;
1491 while (rval != JB_ERR_FILE);
1495 /* We allocated one too many - free it */
1496 prev_line->next = NULL;
1499 *pfile = first_line;
1504 /*********************************************************************
1506 * Function : edit_read_file
1508 * Description : Read a complete file into memory.
1509 * Handles CGI parameter parsing. If requested, also
1510 * checks the file's modification timestamp.
1513 * 1 : csp = Current client state (buffers, headers, etc...)
1514 * 2 : parameters = map of cgi parameters.
1515 * 3 : require_version = true to check "ver" parameter.
1516 * 4 : suffix = File extension, e.g. ".action".
1517 * 5 : pfile = Destination for the file. Will be set
1521 * filename : The name of the file to read, without the
1522 * path or ".action" extension.
1523 * ver : (Only if require_version is nonzero)
1524 * Timestamp of the actions file. If wrong, this
1525 * function fails with JB_ERR_MODIFIED.
1527 * Returns : JB_ERR_OK on success
1528 * JB_ERR_MEMORY on out-of-memory
1529 * JB_ERR_CGI_PARAMS if "filename" was not specified
1531 * JB_ERR_FILE if the file cannot be opened or
1533 * JB_ERR_MODIFIED if version checking was requested and
1534 * failed - the file was modified outside
1535 * of this CGI editor instance.
1537 *********************************************************************/
1538 jb_err edit_read_file(struct client_state *csp,
1539 const struct map *parameters,
1540 int require_version,
1542 struct editable_file **pfile)
1544 struct file_line * lines;
1548 const char * identifier;
1549 struct editable_file * file;
1550 unsigned version = 0;
1551 struct stat statbuf[1];
1552 char version_buf[22];
1553 int newline = NEWLINE_UNKNOWN;
1561 err = get_file_name_param(csp, parameters, "f", suffix,
1562 &filename, &identifier);
1568 if (stat(filename, statbuf) < 0)
1570 /* Error, probably file not found. */
1574 version = (unsigned) statbuf->st_mtime;
1576 if (require_version)
1578 unsigned specified_version;
1579 err = get_number_param(csp, parameters, "v", &specified_version);
1586 if (version != specified_version)
1588 return JB_ERR_MODIFIED;
1592 #if defined(AMIGA) || defined(__OS2__)
1593 if (NULL == (fp = fopen(filename,"r")))
1595 if (NULL == (fp = fopen(filename,"rt")))
1596 #endif /* def AMIGA */
1602 err = edit_read_file_lines(fp, &lines, &newline);
1612 file = (struct editable_file *) zalloc(sizeof(*file));
1616 edit_free_file_lines(lines);
1620 file->lines = lines;
1621 file->newline = newline;
1622 file->filename = filename;
1623 file->version = version;
1624 file->identifier = url_encode(identifier);
1626 if (file->identifier == NULL)
1628 edit_free_file(file);
1629 return JB_ERR_MEMORY;
1632 /* Correct file->version_str */
1633 freez(file->version_str);
1634 snprintf(version_buf, 22, "%u", file->version);
1635 version_buf[21] = '\0';
1636 file->version_str = strdup(version_buf);
1637 if (version_buf == NULL)
1639 edit_free_file(file);
1640 return JB_ERR_MEMORY;
1648 /*********************************************************************
1650 * Function : edit_read_actions_file
1652 * Description : Read a complete actions file into memory.
1653 * Handles CGI parameter parsing. If requested, also
1654 * checks the file's modification timestamp.
1656 * If this function detects an error in the categories
1657 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1658 * then it handles it by filling in the specified
1659 * response structure and returning JB_ERR_FILE.
1662 * 1 : csp = Current client state (buffers, headers, etc...)
1663 * 2 : rsp = HTTP response. Only filled in on error.
1664 * 2 : parameters = map of cgi parameters.
1665 * 3 : require_version = true to check "ver" parameter.
1666 * 4 : pfile = Destination for the file. Will be set
1670 * filename : The name of the actions file to read, without the
1671 * path or ".action" extension.
1672 * ver : (Only if require_version is nonzero)
1673 * Timestamp of the actions file. If wrong, this
1674 * function fails with JB_ERR_MODIFIED.
1676 * Returns : JB_ERR_OK on success
1677 * JB_ERR_MEMORY on out-of-memory
1678 * JB_ERR_CGI_PARAMS if "filename" was not specified
1680 * JB_ERR_FILE if the file does not contain valid data,
1681 * or if file cannot be opened or
1682 * contains no data, or if version
1683 * checking was requested and failed.
1685 *********************************************************************/
1686 jb_err edit_read_actions_file(struct client_state *csp,
1687 struct http_response *rsp,
1688 const struct map *parameters,
1689 int require_version,
1690 struct editable_file **pfile)
1693 struct editable_file *file;
1701 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1704 /* Try to handle if possible */
1705 if (err == JB_ERR_FILE)
1707 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1709 else if (err == JB_ERR_MODIFIED)
1711 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1713 if (err == JB_ERR_OK)
1716 * Signal to higher-level CGI code that there was a problem but we
1717 * handled it, they should just return JB_ERR_OK.
1724 err = edit_parse_actions_file(file);
1727 if (err == JB_ERR_PARSE)
1729 err = cgi_error_parse(csp, rsp, file);
1730 if (err == JB_ERR_OK)
1733 * Signal to higher-level CGI code that there was a problem but we
1734 * handled it, they should just return JB_ERR_OK.
1739 edit_free_file(file);
1748 /*********************************************************************
1750 * Function : get_file_name_param
1752 * Description : Get the name of the file to edit from the parameters
1753 * passed to a CGI function. This function handles
1754 * security checks such as blocking urls containing
1755 * "/" or ".", prepending the config file directory,
1756 * and adding the specified suffix.
1758 * (This is an essential security check, otherwise
1759 * users may be able to pass "../../../etc/passwd"
1760 * and overwrite the password file [linux], "prn:"
1761 * and print random data [Windows], etc...)
1763 * This function only allows filenames contining the
1764 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1765 * That's probably too restrictive but at least it's
1769 * 1 : csp = Current client state (buffers, headers, etc...)
1770 * 2 : parameters = map of cgi parameters
1771 * 3 : param_name = The name of the parameter to read
1772 * 4 : suffix = File extension, e.g. ".actions"
1773 * 5 : pfilename = destination for full filename. Caller
1774 * free()s. Set to NULL on error.
1775 * 6 : pparam = destination for partial filename,
1776 * suitable for use in another URL. Allocated as part
1777 * of the map "parameters", so don't free it.
1778 * Set to NULL if not specified.
1780 * Returns : JB_ERR_OK on success
1781 * JB_ERR_MEMORY on out-of-memory
1782 * JB_ERR_CGI_PARAMS if "filename" was not specified
1785 *********************************************************************/
1786 static jb_err get_file_name_param(struct client_state *csp,
1787 const struct map *parameters,
1788 const char *param_name,
1791 const char **pparam)
1795 #if 0 /* Patch to make 3.0.0 work properly. */
1797 #endif /* 0 - Patch to make 3.0.0 work properly. */
1811 param = lookup(parameters, param_name);
1814 return JB_ERR_CGI_PARAMS;
1819 len = strlen(param);
1820 if (len >= FILENAME_MAX)
1823 return JB_ERR_CGI_PARAMS;
1826 /* Check every character to see if it's legal */
1828 while ((ch = *s++) != '\0')
1830 if ( ((ch < 'A') || (ch > 'Z'))
1831 && ((ch < 'a') || (ch > 'z'))
1832 && ((ch < '0') || (ch > '9'))
1836 /* Probable hack attempt. */
1837 return JB_ERR_CGI_PARAMS;
1842 * FIXME Following is a hack to make 3.0.0 work properly.
1843 * Change "#if 0" --> "#if 1" below when we have modular action
1847 #if 0 /* Patch to make 3.0.0 work properly. */
1848 /* Append extension */
1849 name = malloc(len + strlen(suffix) + 1);
1852 return JB_ERR_MEMORY;
1854 strcpy(name, param);
1855 strcpy(name + len, suffix);
1858 fullpath = make_path(csp->config->confdir, name);
1860 #else /* 1 - Patch to make 3.0.0 work properly. */
1861 if ((csp->actions_list == NULL)
1862 || (csp->actions_list->filename == NULL))
1864 return JB_ERR_CGI_PARAMS;
1867 fullpath = ( (csp->actions_list && csp->actions_list->filename)
1868 ? strdup(csp->actions_list->filename) : NULL);
1869 #endif /* 1 - Patch to make 3.0.0 work properly. */
1870 if (fullpath == NULL)
1872 return JB_ERR_MEMORY;
1876 *pfilename = fullpath;
1882 /*********************************************************************
1884 * Function : get_char_param
1886 * Description : Get a single-character parameter passed to a CGI
1890 * 1 : parameters = map of cgi parameters
1891 * 2 : param_name = The name of the parameter to read
1893 * Returns : Uppercase character on success, '\0' on error.
1895 *********************************************************************/
1896 static char get_char_param(const struct map *parameters,
1897 const char *param_name)
1904 ch = *(lookup(parameters, param_name));
1905 if ((ch >= 'a') && (ch <= 'z'))
1907 ch = ch - 'a' + 'A';
1914 /*********************************************************************
1916 * Function : get_string_param
1918 * Description : Get a string paramater, to be used as an
1919 * ACTION_STRING or ACTION_MULTI paramater.
1920 * Validates the input to prevent stupid/malicious
1921 * users from corrupting their action file.
1924 * 1 : parameters = map of cgi parameters
1925 * 2 : param_name = The name of the parameter to read
1926 * 3 : pparam = destination for paramater. Allocated as
1927 * part of the map "parameters", so don't free it.
1928 * Set to NULL if not specified.
1930 * Returns : JB_ERR_OK on success, or if the paramater
1931 * was not specified.
1932 * JB_ERR_MEMORY on out-of-memory.
1933 * JB_ERR_CGI_PARAMS if the paramater is not valid.
1935 *********************************************************************/
1936 static jb_err get_string_param(const struct map *parameters,
1937 const char *param_name,
1938 const char **pparam)
1950 param = lookup(parameters, param_name);
1956 if (strlen(param) >= CGI_ACTION_PARAM_LEN_MAX)
1961 * Note that the length limit is arbitrary, it just seems
1962 * sensible to limit it to *something*. There's no
1963 * technical reason for any limit at all.
1965 return JB_ERR_CGI_PARAMS;
1968 /* Check every character to see if it's legal */
1970 while ((ch = *s++) != '\0')
1972 if ( ((unsigned char)ch < (unsigned char)' ')
1975 /* Probable hack attempt, or user accidentally used '}'. */
1976 return JB_ERR_CGI_PARAMS;
1987 /*********************************************************************
1989 * Function : get_number_param
1991 * Description : Get a non-negative integer from the parameters
1992 * passed to a CGI function.
1995 * 1 : csp = Current client state (buffers, headers, etc...)
1996 * 2 : parameters = map of cgi parameters
1997 * 3 : name = Name of CGI parameter to read
1998 * 4 : pvalue = destination for value.
1999 * Set to -1 on error.
2001 * Returns : JB_ERR_OK on success
2002 * JB_ERR_MEMORY on out-of-memory
2003 * JB_ERR_CGI_PARAMS if the parameter was not specified
2006 *********************************************************************/
2007 static jb_err get_number_param(struct client_state *csp,
2008 const struct map *parameters,
2023 param = lookup(parameters, name);
2026 return JB_ERR_CGI_PARAMS;
2029 /* We don't use atoi because I want to check this carefully... */
2032 while ((ch = *param++) != '\0')
2034 if ((ch < '0') || (ch > '9'))
2036 return JB_ERR_CGI_PARAMS;
2043 * <limits.h> defines UINT_MAX
2045 * (UINT_MAX - ch) / 10 is the largest number that
2046 * can be safely multiplied by 10 then have ch added.
2048 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
2050 return JB_ERR_CGI_PARAMS;
2053 value = value * 10 + ch;
2064 /*********************************************************************
2066 * Function : get_url_spec_param
2068 * Description : Get a URL pattern from the parameters
2069 * passed to a CGI function. Removes leading/trailing
2070 * spaces and validates it.
2073 * 1 : csp = Current client state (buffers, headers, etc...)
2074 * 2 : parameters = map of cgi parameters
2075 * 3 : name = Name of CGI parameter to read
2076 * 4 : pvalue = destination for value. Will be malloc()'d.
2077 * Set to NULL on error.
2079 * Returns : JB_ERR_OK on success
2080 * JB_ERR_MEMORY on out-of-memory
2081 * JB_ERR_CGI_PARAMS if the parameter was not specified
2084 *********************************************************************/
2085 static jb_err get_url_spec_param(struct client_state *csp,
2086 const struct map *parameters,
2090 const char *orig_param;
2093 struct url_spec compiled[1];
2103 orig_param = lookup(parameters, name);
2106 return JB_ERR_CGI_PARAMS;
2109 /* Copy and trim whitespace */
2110 param = strdup(orig_param);
2113 return JB_ERR_MEMORY;
2117 /* Must be non-empty, and can't allow 1st character to be '{' */
2118 if (param[0] == '\0' || param[0] == '{')
2121 return JB_ERR_CGI_PARAMS;
2124 /* Check for embedded newlines */
2125 for (s = param; *s != '\0'; s++)
2127 if ((*s == '\r') || (*s == '\n'))
2130 return JB_ERR_CGI_PARAMS;
2134 /* Check that regex is valid */
2139 return JB_ERR_MEMORY;
2141 err = create_url_spec(compiled, s);
2146 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2148 free_url_spec(compiled);
2150 if (param[strlen(param) - 1] == '\\')
2153 * Must protect trailing '\\' from becoming line continuation character.
2154 * Two methods: 1) If it's a domain only, add a trailing '/'.
2155 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2157 if (strchr(param, '/') == NULL)
2159 err = string_append(¶m, "/");
2163 err = string_append(¶m, "(?:)");
2170 /* Check that the modified regex is valid */
2175 return JB_ERR_MEMORY;
2177 err = create_url_spec(compiled, s);
2182 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2184 free_url_spec(compiled);
2191 /*********************************************************************
2193 * Function : map_radio
2195 * Description : Map a set of radio button values. E.g. if you have
2196 * 3 radio buttons, declare them as:
2197 * <option type="radio" name="xyz" @xyz-a@>
2198 * <option type="radio" name="xyz" @xyz-b@>
2199 * <option type="radio" name="xyz" @xyz-c@>
2200 * Then map one of the @xyz-?@ variables to "checked"
2201 * and all the others to empty by calling:
2202 * map_radio(exports, "xyz", "abc", sel)
2203 * Where 'sel' is 'a', 'b', or 'c'.
2206 * 1 : exports = Exports map to modify.
2207 * 2 : optionname = name for map
2208 * 3 : values = null-terminated list of values;
2209 * 4 : value = Selected value.
2211 * CGI Parameters : None
2213 * Returns : JB_ERR_OK on success
2214 * JB_ERR_MEMORY on out-of-memory
2216 *********************************************************************/
2217 static jb_err map_radio(struct map * exports,
2218 const char * optionname,
2219 const char * values,
2231 len = strlen(optionname);
2232 buf = malloc(len + 3);
2235 return JB_ERR_MEMORY;
2238 strcpy(buf, optionname);
2243 while ((c = *values++) != '\0')
2248 if (map(exports, buf, 1, "", 1))
2250 return JB_ERR_MEMORY;
2256 return map(exports, buf, 0, "checked", 1);
2260 /*********************************************************************
2262 * Function : cgi_error_modified
2264 * Description : CGI function that is called when a file is modified
2265 * outside the CGI editor.
2268 * 1 : csp = Current client state (buffers, headers, etc...)
2269 * 2 : rsp = http_response data structure for output
2270 * 3 : filename = The file that was modified.
2272 * CGI Parameters : none
2274 * Returns : JB_ERR_OK on success
2275 * JB_ERR_MEMORY on out-of-memory error.
2277 *********************************************************************/
2278 jb_err cgi_error_modified(struct client_state *csp,
2279 struct http_response *rsp,
2280 const char *filename)
2282 struct map *exports;
2289 if (NULL == (exports = default_exports(csp, NULL)))
2291 return JB_ERR_MEMORY;
2294 err = map(exports, "f", 1, html_encode(filename), 0);
2301 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2305 /*********************************************************************
2307 * Function : cgi_error_parse
2309 * Description : CGI function that is called when a file cannot
2310 * be parsed by the CGI editor.
2313 * 1 : csp = Current client state (buffers, headers, etc...)
2314 * 2 : rsp = http_response data structure for output
2315 * 3 : file = The file that was modified.
2317 * CGI Parameters : none
2319 * Returns : JB_ERR_OK on success
2320 * JB_ERR_MEMORY on out-of-memory error.
2322 *********************************************************************/
2323 jb_err cgi_error_parse(struct client_state *csp,
2324 struct http_response *rsp,
2325 struct editable_file *file)
2327 struct map *exports;
2329 struct file_line *cur_line;
2335 if (NULL == (exports = default_exports(csp, NULL)))
2337 return JB_ERR_MEMORY;
2340 err = map(exports, "f", 1, file->identifier, 1);
2341 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2343 cur_line = file->parse_error;
2346 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2347 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2355 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2359 /*********************************************************************
2361 * Function : cgi_error_file
2363 * Description : CGI function that is called when a file cannot be
2364 * opened by the CGI editor.
2367 * 1 : csp = Current client state (buffers, headers, etc...)
2368 * 2 : rsp = http_response data structure for output
2369 * 3 : filename = The file that was modified.
2371 * CGI Parameters : none
2373 * Returns : JB_ERR_OK on success
2374 * JB_ERR_MEMORY on out-of-memory error.
2376 *********************************************************************/
2377 jb_err cgi_error_file(struct client_state *csp,
2378 struct http_response *rsp,
2379 const char *filename)
2381 struct map *exports;
2388 if (NULL == (exports = default_exports(csp, NULL)))
2390 return JB_ERR_MEMORY;
2393 err = map(exports, "f", 1, html_encode(filename), 0);
2400 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2404 /*********************************************************************
2406 * Function : cgi_error_bad_param
2408 * Description : CGI function that is called if the parameters
2409 * (query string) for a CGI were wrong.
2412 * 1 : csp = Current client state (buffers, headers, etc...)
2413 * 2 : rsp = http_response data structure for output
2415 * CGI Parameters : none
2417 * Returns : JB_ERR_OK on success
2418 * JB_ERR_MEMORY on out-of-memory error.
2420 *********************************************************************/
2421 jb_err cgi_error_disabled(struct client_state *csp,
2422 struct http_response *rsp)
2424 struct map *exports;
2429 if (NULL == (exports = default_exports(csp, NULL)))
2431 return JB_ERR_MEMORY;
2434 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2438 /*********************************************************************
2440 * Function : cgi_edit_actions
2442 * Description : CGI function that allows the user to choose which
2443 * actions file to edit.
2446 * 1 : csp = Current client state (buffers, headers, etc...)
2447 * 2 : rsp = http_response data structure for output
2448 * 3 : parameters = map of cgi parameters
2450 * CGI Parameters : None
2452 * Returns : JB_ERR_OK on success
2453 * JB_ERR_MEMORY on out-of-memory error
2455 *********************************************************************/
2456 jb_err cgi_edit_actions(struct client_state *csp,
2457 struct http_response *rsp,
2458 const struct map *parameters)
2461 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2463 return cgi_error_disabled(csp, rsp);
2466 /* FIXME: Incomplete */
2467 rsp->status = strdup("302 Local Redirect from Privoxy");
2468 if (rsp->status == NULL)
2470 return JB_ERR_MEMORY;
2472 if (enlist_unique_header(rsp->headers, "Location",
2473 CGI_PREFIX "edit-actions-list?f=default"))
2477 return JB_ERR_MEMORY;
2484 /*********************************************************************
2486 * Function : cgi_edit_actions_list
2488 * Description : CGI function that edits the actions list.
2489 * FIXME: This function shouldn't FATAL ever.
2490 * FIXME: This function doesn't check the retval of map()
2492 * 1 : csp = Current client state (buffers, headers, etc...)
2493 * 2 : rsp = http_response data structure for output
2494 * 3 : parameters = map of cgi parameters
2496 * CGI Parameters : filename
2498 * Returns : JB_ERR_OK on success
2499 * JB_ERR_MEMORY on out-of-memory
2500 * JB_ERR_FILE if the file cannot be opened or
2502 * JB_ERR_CGI_PARAMS if "filename" was not specified
2505 *********************************************************************/
2506 jb_err cgi_edit_actions_list(struct client_state *csp,
2507 struct http_response *rsp,
2508 const struct map *parameters)
2510 char * section_template;
2511 char * url_template;
2516 struct map * exports;
2517 struct map * section_exports;
2518 struct map * url_exports;
2519 struct editable_file * file;
2520 struct file_line * cur_line;
2521 unsigned line_number = 0;
2522 unsigned prev_section_line_number = ((unsigned) (-1));
2526 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2528 return cgi_error_disabled(csp, rsp);
2531 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2534 /* No filename specified, can't read file, or out of memory. */
2535 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2538 if (NULL == (exports = default_exports(csp, NULL)))
2540 edit_free_file(file);
2541 return JB_ERR_MEMORY;
2544 err = map(exports, "f", 1, file->identifier, 1);
2545 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2549 edit_free_file(file);
2554 /* Should do all global exports above this point */
2556 err = template_load(csp, §ion_template, "edit-actions-list-section");
2559 edit_free_file(file);
2561 if (err == JB_ERR_FILE)
2563 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2568 err = template_load(csp, &url_template, "edit-actions-list-url");
2571 free(section_template);
2572 edit_free_file(file);
2574 if (err == JB_ERR_FILE)
2576 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2581 err = template_fill(§ion_template, exports);
2585 edit_free_file(file);
2591 err = template_fill(&url_template, exports);
2594 free(section_template);
2595 edit_free_file(file);
2600 /* Find start of actions in file */
2601 cur_line = file->lines;
2603 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2605 cur_line = cur_line->next;
2609 if (NULL == (sections = strdup("")))
2611 free(section_template);
2613 edit_free_file(file);
2615 return JB_ERR_MEMORY;
2618 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2620 if (NULL == (section_exports = new_map()))
2623 free(section_template);
2625 edit_free_file(file);
2627 return JB_ERR_MEMORY;
2630 snprintf(buf, 50, "%d", line_number);
2631 err = map(section_exports, "s", 1, buf, 1);
2632 if (!err) err = map(section_exports, "actions", 1,
2633 actions_to_html(cur_line->data.action), 0);
2636 && (cur_line->next != NULL)
2637 && (cur_line->next->type == FILE_LINE_URL))
2639 /* This section contains at least one URL, don't allow delete */
2640 err = map_block_killer(section_exports, "empty-section");
2644 if (!err) err = map_block_keep(section_exports, "empty-section");
2647 if (prev_section_line_number != ((unsigned)(-1)))
2649 /* Not last section */
2650 snprintf(buf, 50, "%d", prev_section_line_number);
2651 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2652 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2657 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2659 prev_section_line_number = line_number;
2664 free(section_template);
2666 edit_free_file(file);
2668 free_map(section_exports);
2672 /* Should do all section-specific exports above this point */
2674 if (NULL == (urls = strdup("")))
2677 free(section_template);
2679 edit_free_file(file);
2681 free_map(section_exports);
2682 return JB_ERR_MEMORY;
2687 cur_line = cur_line->next;
2690 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2692 if (NULL == (url_exports = new_map()))
2696 free(section_template);
2698 edit_free_file(file);
2700 free_map(section_exports);
2701 return JB_ERR_MEMORY;
2704 snprintf(buf, 50, "%d", line_number);
2705 err = map(url_exports, "p", 1, buf, 1);
2707 snprintf(buf, 50, "%d", url_1_2);
2708 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2710 if (!err) err = map(url_exports, "url-html", 1,
2711 html_encode(cur_line->unprocessed), 0);
2712 if (!err) err = map(url_exports, "url", 1,
2713 url_encode(cur_line->unprocessed), 0);
2719 free(section_template);
2721 edit_free_file(file);
2723 free_map(section_exports);
2724 free_map(url_exports);
2728 if (NULL == (s = strdup(url_template)))
2732 free(section_template);
2734 edit_free_file(file);
2736 free_map(section_exports);
2737 free_map(url_exports);
2738 return JB_ERR_MEMORY;
2741 err = template_fill(&s, section_exports);
2742 if (!err) err = template_fill(&s, url_exports);
2743 if (!err) err = string_append(&urls, s);
2745 free_map(url_exports);
2752 free(section_template);
2754 edit_free_file(file);
2756 free_map(section_exports);
2760 url_1_2 = 3 - url_1_2;
2762 cur_line = cur_line->next;
2766 err = map(section_exports, "urls", 1, urls, 0);
2768 /* Could also do section-specific exports here, but it wouldn't be as fast */
2770 if ( (cur_line != NULL)
2771 && (cur_line->type == FILE_LINE_ACTION))
2773 /* Not last section */
2774 snprintf(buf, 50, "%d", line_number);
2775 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2776 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2781 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2787 free(section_template);
2789 edit_free_file(file);
2791 free_map(section_exports);
2795 if (NULL == (s = strdup(section_template)))
2798 free(section_template);
2800 edit_free_file(file);
2802 free_map(section_exports);
2803 return JB_ERR_MEMORY;
2806 err = template_fill(&s, section_exports);
2807 if (!err) err = string_append(§ions, s);
2810 free_map(section_exports);
2815 free(section_template);
2817 edit_free_file(file);
2823 edit_free_file(file);
2824 free(section_template);
2827 err = map(exports, "sections", 1, sections, 0);
2834 /* Could also do global exports here, but it wouldn't be as fast */
2836 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2840 /*********************************************************************
2842 * Function : cgi_edit_actions
2844 * Description : CGI function that edits the Actions list.
2847 * 1 : csp = Current client state (buffers, headers, etc...)
2848 * 2 : rsp = http_response data structure for output
2849 * 3 : parameters = map of cgi parameters
2851 * CGI Parameters : None
2853 * Returns : JB_ERR_OK on success
2854 * JB_ERR_MEMORY on out-of-memory
2855 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2856 * specified or not valid.
2858 *********************************************************************/
2859 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2860 struct http_response *rsp,
2861 const struct map *parameters)
2863 struct map * exports;
2865 struct editable_file * file;
2866 struct file_line * cur_line;
2867 unsigned line_number;
2869 struct file_list *filter_file;
2870 struct re_filterfile_spec *filter_group;
2872 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2874 return cgi_error_disabled(csp, rsp);
2877 err = get_number_param(csp, parameters, "s", §ionid);
2883 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2886 /* No filename specified, can't read file, modified, or out of memory. */
2887 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2890 cur_line = file->lines;
2892 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2894 cur_line = cur_line->next;
2897 if ( (cur_line == NULL)
2898 || (line_number != sectionid)
2900 || (cur_line->type != FILE_LINE_ACTION))
2902 /* Invalid "sectionid" parameter */
2903 edit_free_file(file);
2904 return JB_ERR_CGI_PARAMS;
2907 if (NULL == (exports = default_exports(csp, NULL)))
2909 edit_free_file(file);
2910 return JB_ERR_MEMORY;
2913 err = map(exports, "f", 1, file->identifier, 1);
2914 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2915 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2917 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2919 filter_file = csp->rlist;
2920 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2922 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2926 edit_free_file(file);
2931 if (filter_group == NULL)
2933 err = map(exports, "filter-params", 1, "", 1);
2937 /* We have some entries in the filter list */
2940 char * filter_template;
2942 err = template_load(csp, &filter_template, "edit-actions-for-url-filter");
2945 edit_free_file(file);
2947 if (err == JB_ERR_FILE)
2949 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2954 result = strdup("");
2956 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2958 char current_mode = 'x';
2959 struct list_entry *filter_name;
2961 struct map *line_exports;
2964 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2965 while ((filter_name != NULL)
2966 && (0 != strcmp(filter_group->name, filter_name->str)))
2968 filter_name = filter_name->next;
2971 if (filter_name != NULL)
2977 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2978 while ((filter_name != NULL)
2979 && (0 != strcmp(filter_group->name, filter_name->str)))
2981 filter_name = filter_name->next;
2983 if (filter_name != NULL)
2989 /* Generate a unique serial number */
2990 snprintf(number, sizeof(number), "%x", index++);
2991 number[sizeof(number) - 1] = '\0';
2993 line_exports = new_map();
2994 if (line_exports == NULL)
2996 err = JB_ERR_MEMORY;
3001 if (!err) err = map(line_exports, "index", 1, number, 1);
3002 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
3003 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
3004 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
3009 this_line = strdup(filter_template);
3010 if (this_line == NULL) err = JB_ERR_MEMORY;
3012 if (!err) err = template_fill(&this_line, line_exports);
3013 string_join(&result, this_line);
3015 free_map(line_exports);
3020 err = map(exports, "filter-params", 1, result, 0);
3028 if (!err) err = map_radio(exports, "filter-all", "nx",
3029 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3031 edit_free_file(file);
3039 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3043 /*********************************************************************
3045 * Function : cgi_edit_actions_submit
3047 * Description : CGI function that actually edits the Actions list.
3050 * 1 : csp = Current client state (buffers, headers, etc...)
3051 * 2 : rsp = http_response data structure for output
3052 * 3 : parameters = map of cgi parameters
3054 * CGI Parameters : None
3056 * Returns : JB_ERR_OK on success
3057 * JB_ERR_MEMORY on out-of-memory
3058 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3059 * specified or not valid.
3061 *********************************************************************/
3062 jb_err cgi_edit_actions_submit(struct client_state *csp,
3063 struct http_response *rsp,
3064 const struct map *parameters)
3070 struct editable_file * file;
3071 struct file_line * cur_line;
3072 unsigned line_number;
3078 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3080 return cgi_error_disabled(csp, rsp);
3083 err = get_number_param(csp, parameters, "s", §ionid);
3089 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3092 /* No filename specified, can't read file, modified, or out of memory. */
3093 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3096 cur_line = file->lines;
3098 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3100 cur_line = cur_line->next;
3103 if ( (cur_line == NULL)
3104 || (line_number != sectionid)
3106 || (cur_line->type != FILE_LINE_ACTION))
3108 /* Invalid "sectionid" parameter */
3109 edit_free_file(file);
3110 return JB_ERR_CGI_PARAMS;
3113 err = actions_from_radio(parameters, cur_line->data.action);
3117 edit_free_file(file);
3121 ch = get_char_param(parameters, "filter_all");
3124 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3125 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3126 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3130 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3133 for (index = 0; !err; index++)
3140 /* Generate the keys */
3141 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3142 key_value[sizeof(key_value) - 1] = '\0';
3143 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3144 key_name[sizeof(key_name) - 1] = '\0';
3146 err = get_string_param(parameters, key_name, &name);
3156 value = get_char_param(parameters, key_value);
3159 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3160 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3161 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3163 else if (value == 'N')
3165 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3166 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3168 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3169 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3172 else if (value == 'X')
3174 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3175 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3182 edit_free_file(file);
3186 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3189 edit_free_file(file);
3190 return JB_ERR_MEMORY;
3193 len = strlen(actiontext);
3197 * Empty action - must special-case this.
3198 * Simply setting len to 1 is sufficient...
3203 if (NULL == (newtext = malloc(len + 2)))
3207 edit_free_file(file);
3208 return JB_ERR_MEMORY;
3210 strcpy(newtext, actiontext);
3214 newtext[len + 1] = '\0';
3216 freez(cur_line->raw);
3217 freez(cur_line->unprocessed);
3218 cur_line->unprocessed = newtext;
3220 err = edit_write_file(file);
3223 /* Error writing file */
3224 edit_free_file(file);
3228 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3229 string_append(&target, file->identifier);
3231 edit_free_file(file);
3236 return JB_ERR_MEMORY;
3239 rsp->status = strdup("302 Local Redirect from Privoxy");
3240 if (rsp->status == NULL)
3243 return JB_ERR_MEMORY;
3245 err = enlist_unique_header(rsp->headers, "Location", target);
3252 /*********************************************************************
3254 * Function : cgi_edit_actions_url
3256 * Description : CGI function that actually edits a URL pattern in
3260 * 1 : csp = Current client state (buffers, headers, etc...)
3261 * 2 : rsp = http_response data structure for output
3262 * 3 : parameters = map of cgi parameters
3265 * filename : Identifies the file to edit
3266 * ver : File's last-modified time
3267 * section : Line number of section to edit
3268 * pattern : Line number of pattern to edit
3269 * newval : New value for pattern
3271 * Returns : JB_ERR_OK on success
3272 * JB_ERR_MEMORY on out-of-memory
3273 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3274 * specified or not valid.
3276 *********************************************************************/
3277 jb_err cgi_edit_actions_url(struct client_state *csp,
3278 struct http_response *rsp,
3279 const struct map *parameters)
3283 struct editable_file * file;
3284 struct file_line * cur_line;
3285 unsigned line_number;
3289 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3291 return cgi_error_disabled(csp, rsp);
3294 err = get_number_param(csp, parameters, "p", &patternid);
3301 return JB_ERR_CGI_PARAMS;
3304 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3310 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3313 /* No filename specified, can't read file, modified, or out of memory. */
3315 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3319 cur_line = file->lines;
3321 while ((cur_line != NULL) && (line_number < patternid))
3323 cur_line = cur_line->next;
3327 if ( (cur_line == NULL)
3328 || (cur_line->type != FILE_LINE_URL))
3330 /* Invalid "patternid" parameter */
3332 edit_free_file(file);
3333 return JB_ERR_CGI_PARAMS;
3336 /* At this point, the line to edit is in cur_line */
3338 freez(cur_line->raw);
3339 freez(cur_line->unprocessed);
3340 cur_line->unprocessed = new_pattern;
3342 err = edit_write_file(file);
3345 /* Error writing file */
3346 edit_free_file(file);
3350 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3351 string_append(&target, file->identifier);
3353 edit_free_file(file);
3358 return JB_ERR_MEMORY;
3361 rsp->status = strdup("302 Local Redirect from Privoxy");
3362 if (rsp->status == NULL)
3365 return JB_ERR_MEMORY;
3367 err = enlist_unique_header(rsp->headers, "Location", target);
3374 /*********************************************************************
3376 * Function : cgi_edit_actions_add_url
3378 * Description : CGI function that actually adds a URL pattern to
3382 * 1 : csp = Current client state (buffers, headers, etc...)
3383 * 2 : rsp = http_response data structure for output
3384 * 3 : parameters = map of cgi parameters
3387 * filename : Identifies the file to edit
3388 * ver : File's last-modified time
3389 * section : Line number of section to edit
3390 * newval : New pattern
3392 * Returns : JB_ERR_OK on success
3393 * JB_ERR_MEMORY on out-of-memory
3394 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3395 * specified or not valid.
3397 *********************************************************************/
3398 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3399 struct http_response *rsp,
3400 const struct map *parameters)
3404 struct file_line * new_line;
3405 struct editable_file * file;
3406 struct file_line * cur_line;
3407 unsigned line_number;
3411 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3413 return cgi_error_disabled(csp, rsp);
3416 err = get_number_param(csp, parameters, "s", §ionid);
3423 return JB_ERR_CGI_PARAMS;
3426 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3432 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3435 /* No filename specified, can't read file, modified, or out of memory. */
3437 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3441 cur_line = file->lines;
3443 while ((cur_line != NULL) && (line_number < sectionid))
3445 cur_line = cur_line->next;
3449 if ( (cur_line == NULL)
3450 || (cur_line->type != FILE_LINE_ACTION))
3452 /* Invalid "sectionid" parameter */
3454 edit_free_file(file);
3455 return JB_ERR_CGI_PARAMS;
3458 /* At this point, the section header is in cur_line - add after this. */
3460 /* Allocate the new line */
3461 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3462 if (new_line == NULL)
3465 edit_free_file(file);
3466 return JB_ERR_MEMORY;
3469 /* Fill in the data members of the new line */
3470 new_line->raw = NULL;
3471 new_line->prefix = NULL;
3472 new_line->unprocessed = new_pattern;
3473 new_line->type = FILE_LINE_URL;
3475 /* Link new_line into the list, after cur_line */
3476 new_line->next = cur_line->next;
3477 cur_line->next = new_line;
3479 /* Done making changes, now commit */
3481 err = edit_write_file(file);
3484 /* Error writing file */
3485 edit_free_file(file);
3489 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3490 string_append(&target, file->identifier);
3492 edit_free_file(file);
3497 return JB_ERR_MEMORY;
3500 rsp->status = strdup("302 Local Redirect from Privoxy");
3501 if (rsp->status == NULL)
3504 return JB_ERR_MEMORY;
3506 err = enlist_unique_header(rsp->headers, "Location", target);
3513 /*********************************************************************
3515 * Function : cgi_edit_actions_remove_url
3517 * Description : CGI function that actually removes a URL pattern from
3521 * 1 : csp = Current client state (buffers, headers, etc...)
3522 * 2 : rsp = http_response data structure for output
3523 * 3 : parameters = map of cgi parameters
3526 * f : (filename) Identifies the file to edit
3527 * v : (version) File's last-modified time
3528 * p : (pattern) Line number of pattern to remove
3530 * Returns : JB_ERR_OK on success
3531 * JB_ERR_MEMORY on out-of-memory
3532 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3533 * specified or not valid.
3535 *********************************************************************/
3536 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3537 struct http_response *rsp,
3538 const struct map *parameters)
3541 struct editable_file * file;
3542 struct file_line * cur_line;
3543 struct file_line * prev_line;
3544 unsigned line_number;
3548 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3550 return cgi_error_disabled(csp, rsp);
3553 err = get_number_param(csp, parameters, "p", &patternid);
3559 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3562 /* No filename specified, can't read file, modified, or out of memory. */
3563 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3568 cur_line = file->lines;
3570 while ((cur_line != NULL) && (line_number < patternid))
3572 prev_line = cur_line;
3573 cur_line = cur_line->next;
3577 if ( (cur_line == NULL)
3578 || (prev_line == NULL)
3579 || (cur_line->type != FILE_LINE_URL))
3581 /* Invalid "patternid" parameter */
3582 edit_free_file(file);
3583 return JB_ERR_CGI_PARAMS;
3586 /* At this point, the line to remove is in cur_line, and the previous
3587 * one is in prev_line
3590 /* Unlink cur_line */
3591 prev_line->next = cur_line->next;
3592 cur_line->next = NULL;
3595 edit_free_file_lines(cur_line);
3597 err = edit_write_file(file);
3600 /* Error writing file */
3601 edit_free_file(file);
3605 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3606 string_append(&target, file->identifier);
3608 edit_free_file(file);
3613 return JB_ERR_MEMORY;
3616 rsp->status = strdup("302 Local Redirect from Privoxy");
3617 if (rsp->status == NULL)
3620 return JB_ERR_MEMORY;
3622 err = enlist_unique_header(rsp->headers, "Location", target);
3629 /*********************************************************************
3631 * Function : cgi_edit_actions_section_remove
3633 * Description : CGI function that actually removes a whole section from
3634 * the actions file. The section must be empty first
3635 * (else JB_ERR_CGI_PARAMS).
3638 * 1 : csp = Current client state (buffers, headers, etc...)
3639 * 2 : rsp = http_response data structure for output
3640 * 3 : parameters = map of cgi parameters
3643 * f : (filename) Identifies the file to edit
3644 * v : (version) File's last-modified time
3645 * s : (section) Line number of section to edit
3647 * Returns : JB_ERR_OK on success
3648 * JB_ERR_MEMORY on out-of-memory
3649 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3650 * specified or not valid.
3652 *********************************************************************/
3653 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3654 struct http_response *rsp,
3655 const struct map *parameters)
3658 struct editable_file * file;
3659 struct file_line * cur_line;
3660 struct file_line * prev_line;
3661 unsigned line_number;
3665 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3667 return cgi_error_disabled(csp, rsp);
3670 err = get_number_param(csp, parameters, "s", §ionid);
3676 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3679 /* No filename specified, can't read file, modified, or out of memory. */
3680 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3684 cur_line = file->lines;
3687 while ((cur_line != NULL) && (line_number < sectionid))
3689 prev_line = cur_line;
3690 cur_line = cur_line->next;
3694 if ( (cur_line == NULL)
3695 || (cur_line->type != FILE_LINE_ACTION) )
3697 /* Invalid "sectionid" parameter */
3698 edit_free_file(file);
3699 return JB_ERR_CGI_PARAMS;
3702 if ( (cur_line->next != NULL)
3703 && (cur_line->next->type == FILE_LINE_URL) )
3705 /* Section not empty. */
3706 edit_free_file(file);
3707 return JB_ERR_CGI_PARAMS;
3710 /* At this point, the line to remove is in cur_line, and the previous
3711 * one is in prev_line
3714 /* Unlink cur_line */
3715 if (prev_line == NULL)
3717 /* Removing the first line from the file */
3718 file->lines = cur_line->next;
3722 prev_line->next = cur_line->next;
3724 cur_line->next = NULL;
3727 edit_free_file_lines(cur_line);
3729 err = edit_write_file(file);
3732 /* Error writing file */
3733 edit_free_file(file);
3737 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3738 string_append(&target, file->identifier);
3740 edit_free_file(file);
3745 return JB_ERR_MEMORY;
3748 rsp->status = strdup("302 Local Redirect from Privoxy");
3749 if (rsp->status == NULL)
3752 return JB_ERR_MEMORY;
3754 err = enlist_unique_header(rsp->headers, "Location", target);
3761 /*********************************************************************
3763 * Function : cgi_edit_actions_section_add
3765 * Description : CGI function that adds a new empty section to
3769 * 1 : csp = Current client state (buffers, headers, etc...)
3770 * 2 : rsp = http_response data structure for output
3771 * 3 : parameters = map of cgi parameters
3774 * f : (filename) Identifies the file to edit
3775 * v : (version) File's last-modified time
3776 * s : (section) Line number of section to add after, 0 for
3779 * Returns : JB_ERR_OK on success
3780 * JB_ERR_MEMORY on out-of-memory
3781 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3782 * specified or not valid.
3784 *********************************************************************/
3785 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3786 struct http_response *rsp,
3787 const struct map *parameters)
3790 struct file_line * new_line;
3792 struct editable_file * file;
3793 struct file_line * cur_line;
3794 unsigned line_number;
3798 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3800 return cgi_error_disabled(csp, rsp);
3803 err = get_number_param(csp, parameters, "s", §ionid);
3809 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3812 /* No filename specified, can't read file, modified, or out of memory. */
3813 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3817 cur_line = file->lines;
3821 /* Add to start of file */
3822 if (cur_line != NULL)
3824 /* There's something in the file, find the line before the first
3827 while ( (cur_line->next != NULL)
3828 && (cur_line->next->type != FILE_LINE_ACTION) )
3830 cur_line = cur_line->next;
3837 /* Add after stated section. */
3838 while ((cur_line != NULL) && (line_number < sectionid))
3840 cur_line = cur_line->next;
3844 if ( (cur_line == NULL)
3845 || (cur_line->type != FILE_LINE_ACTION))
3847 /* Invalid "sectionid" parameter */
3848 edit_free_file(file);
3849 return JB_ERR_CGI_PARAMS;
3852 /* Skip through the section to find the last line in it. */
3853 while ( (cur_line->next != NULL)
3854 && (cur_line->next->type != FILE_LINE_ACTION) )
3856 cur_line = cur_line->next;
3861 /* At this point, the last line in the previous section is in cur_line
3862 * - add after this. (Or if we need to add as the first line, cur_line
3866 new_text = strdup("{}");
3867 if (NULL == new_text)
3869 edit_free_file(file);
3870 return JB_ERR_MEMORY;
3873 /* Allocate the new line */
3874 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3875 if (new_line == NULL)
3878 edit_free_file(file);
3879 return JB_ERR_MEMORY;
3882 /* Fill in the data members of the new line */
3883 new_line->raw = NULL;
3884 new_line->prefix = NULL;
3885 new_line->unprocessed = new_text;
3886 new_line->type = FILE_LINE_ACTION;
3888 if (cur_line != NULL)
3890 /* Link new_line into the list, after cur_line */
3891 new_line->next = cur_line->next;
3892 cur_line->next = new_line;
3896 /* Link new_line into the list, as first line */
3897 new_line->next = file->lines;
3898 file->lines = new_line;
3901 /* Done making changes, now commit */
3903 err = edit_write_file(file);
3906 /* Error writing file */
3907 edit_free_file(file);
3911 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3912 string_append(&target, file->identifier);
3914 edit_free_file(file);
3919 return JB_ERR_MEMORY;
3922 rsp->status = strdup("302 Local Redirect from Privoxy");
3923 if (rsp->status == NULL)
3926 return JB_ERR_MEMORY;
3928 err = enlist_unique_header(rsp->headers, "Location", target);
3935 /*********************************************************************
3937 * Function : cgi_edit_actions_section_swap
3939 * Description : CGI function that swaps the order of two sections
3940 * in the actions file. Note that this CGI can actually
3941 * swap any two arbitrary sections, but the GUI interface
3942 * currently only allows consecutive sections to be
3946 * 1 : csp = Current client state (buffers, headers, etc...)
3947 * 2 : rsp = http_response data structure for output
3948 * 3 : parameters = map of cgi parameters
3951 * f : (filename) Identifies the file to edit
3952 * v : (version) File's last-modified time
3953 * s1 : (section1) Line number of first section to swap
3954 * s2 : (section2) Line number of second section to swap
3956 * Returns : JB_ERR_OK on success
3957 * JB_ERR_MEMORY on out-of-memory
3958 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3959 * specified or not valid.
3961 *********************************************************************/
3962 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3963 struct http_response *rsp,
3964 const struct map *parameters)
3968 struct editable_file * file;
3969 struct file_line * cur_line;
3970 struct file_line * prev_line;
3971 struct file_line * line_before_section1;
3972 struct file_line * line_start_section1;
3973 struct file_line * line_end_section1;
3974 struct file_line * line_after_section1;
3975 struct file_line * line_before_section2;
3976 struct file_line * line_start_section2;
3977 struct file_line * line_end_section2;
3978 struct file_line * line_after_section2;
3979 unsigned line_number;
3983 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3985 return cgi_error_disabled(csp, rsp);
3988 err = get_number_param(csp, parameters, "s1", §ion1);
3989 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3995 if (section1 > section2)
3997 unsigned temp = section2;
3998 section2 = section1;
4002 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
4005 /* No filename specified, can't read file, modified, or out of memory. */
4006 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
4009 /* Start at the beginning... */
4011 cur_line = file->lines;
4014 /* ... find section1 ... */
4015 while ((cur_line != NULL) && (line_number < section1))
4017 prev_line = cur_line;
4018 cur_line = cur_line->next;
4022 if ( (cur_line == NULL)
4023 || (cur_line->type != FILE_LINE_ACTION) )
4025 /* Invalid "section1" parameter */
4026 edit_free_file(file);
4027 return JB_ERR_CGI_PARAMS;
4030 /* If no-op, we've validated params and can skip the rest. */
4031 if (section1 != section2)
4033 /* ... find the end of section1 ... */
4034 line_before_section1 = prev_line;
4035 line_start_section1 = cur_line;
4038 prev_line = cur_line;
4039 cur_line = cur_line->next;
4042 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4043 line_end_section1 = prev_line;
4044 line_after_section1 = cur_line;
4046 /* ... find section2 ... */
4047 while ((cur_line != NULL) && (line_number < section2))
4049 prev_line = cur_line;
4050 cur_line = cur_line->next;
4054 if ( (cur_line == NULL)
4055 || (cur_line->type != FILE_LINE_ACTION) )
4057 /* Invalid "section2" parameter */
4058 edit_free_file(file);
4059 return JB_ERR_CGI_PARAMS;
4062 /* ... find the end of section2 ... */
4063 line_before_section2 = prev_line;
4064 line_start_section2 = cur_line;
4067 prev_line = cur_line;
4068 cur_line = cur_line->next;
4071 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4072 line_end_section2 = prev_line;
4073 line_after_section2 = cur_line;
4075 /* Now have all the pointers we need. Do the swap. */
4077 /* Change the pointer to section1 to point to section2 instead */
4078 if (line_before_section1 == NULL)
4080 file->lines = line_start_section2;
4084 line_before_section1->next = line_start_section2;
4087 if (line_before_section2 == line_end_section1)
4089 /* Consecutive sections */
4090 line_end_section2->next = line_start_section1;
4094 line_end_section2->next = line_after_section1;
4095 line_before_section2->next = line_start_section1;
4098 /* Set the pointer from the end of section1 to the rest of the file */
4099 line_end_section1->next = line_after_section2;
4101 err = edit_write_file(file);
4104 /* Error writing file */
4105 edit_free_file(file);
4108 } /* END if (section1 != section2) */
4110 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4111 string_append(&target, file->identifier);
4113 edit_free_file(file);
4118 return JB_ERR_MEMORY;
4121 rsp->status = strdup("302 Local Redirect from Privoxy");
4122 if (rsp->status == NULL)
4125 return JB_ERR_MEMORY;
4127 err = enlist_unique_header(rsp->headers, "Location", target);
4134 /*********************************************************************
4136 * Function : cgi_toggle
4138 * Description : CGI function that adds a new empty section to
4142 * 1 : csp = Current client state (buffers, headers, etc...)
4143 * 2 : rsp = http_response data structure for output
4144 * 3 : parameters = map of cgi parameters
4147 * set : If present, how to change toggle setting:
4148 * "enable", "disable", "toggle", or none (default).
4149 * mini : If present, use mini reply template.
4151 * Returns : JB_ERR_OK on success
4152 * JB_ERR_MEMORY on out-of-memory
4154 *********************************************************************/
4155 jb_err cgi_toggle(struct client_state *csp,
4156 struct http_response *rsp,
4157 const struct map *parameters)
4159 struct map *exports;
4161 const char *template_name;
4167 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4169 return cgi_error_disabled(csp, rsp);
4172 mode = get_char_param(parameters, "set");
4179 else if (mode == 'D')
4184 else if (mode == 'T')
4187 g_bToggleIJB = !g_bToggleIJB;
4190 if (NULL == (exports = default_exports(csp, "toggle")))
4192 return JB_ERR_MEMORY;
4195 template_name = (get_char_param(parameters, "mini")
4199 return template_fill_for_cgi(csp, template_name, exports, rsp);
4203 /*********************************************************************
4205 * Function : javascriptify
4207 * Description : Converts a string into a form JavaScript will like.
4209 * Netscape 4's JavaScript sucks - it doesn't use
4210 * "id" parameters, so you have to set the "name"
4211 * used to submit a form element to something JavaScript
4212 * will like. (Or access the elements by index in an
4213 * array. That array contains >60 elements and will
4214 * be changed whenever we add a new action to the
4215 * editor, so I'm NOT going to use indexes that have
4216 * to be figured out by hand.)
4218 * Currently the only thing we have to worry about
4219 * is "-" ==> "_" conversion.
4221 * This is a length-preserving operation so it is
4222 * carried out in-place, no memory is allocated
4226 * 1 : identifier = String to make JavaScript-friendly.
4230 *********************************************************************/
4231 static void javascriptify(char * identifier)
4233 char * p = identifier;
4234 while (NULL != (p = strchr(p, '-')))
4241 /*********************************************************************
4243 * Function : actions_to_radio
4245 * Description : Converts a actionsfile entry into settings for
4246 * radio buttons and edit boxes on a HTML form.
4249 * 1 : exports = List of substitutions to add to.
4250 * 2 : action = Action to read
4252 * Returns : JB_ERR_OK on success
4253 * JB_ERR_MEMORY on out-of-memory
4255 *********************************************************************/
4256 static jb_err actions_to_radio(struct map * exports,
4257 const struct action_spec *action)
4259 unsigned mask = action->mask;
4260 unsigned add = action->add;
4268 mask = action->mask;
4271 /* sanity - prevents "-feature +feature" */
4275 #define DEFINE_ACTION_BOOL(name, bit) \
4276 if (!(mask & bit)) \
4278 current_mode = 'n'; \
4280 else if (add & bit) \
4282 current_mode = 'y'; \
4286 current_mode = 'x'; \
4288 if (map_radio(exports, name, "ynx", current_mode)) \
4290 return JB_ERR_MEMORY; \
4293 #define DEFINE_ACTION_STRING(name, bit, index) \
4294 DEFINE_ACTION_BOOL(name, bit); \
4297 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4300 checked = !strcmp(action->string[index], value); \
4304 checked = is_default; \
4306 mapped_param |= checked; \
4307 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4309 return JB_ERR_MEMORY; \
4312 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4313 if (map(exports, name "-param-custom", 1, \
4314 ((!mapped_param) ? "checked" : ""), 1)) \
4316 return JB_ERR_MEMORY; \
4318 if (map(exports, name "-param", 1, \
4319 (((add & bit) && !mapped_param) ? \
4320 action->string[index] : default_val), 1)) \
4322 return JB_ERR_MEMORY; \
4325 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4326 if (map(exports, name "-param", 1, \
4327 ((add & bit) ? action->string[index] : default_val), 1)) \
4329 return JB_ERR_MEMORY; \
4332 #define DEFINE_ACTION_MULTI(name, index) \
4333 if (action->multi_add[index]->first) \
4335 current_mode = 'y'; \
4337 else if (action->multi_remove_all[index]) \
4339 current_mode = 'n'; \
4341 else if (action->multi_remove[index]->first) \
4343 current_mode = 'y'; \
4347 current_mode = 'x'; \
4349 if (map_radio(exports, name, "ynx", current_mode)) \
4351 return JB_ERR_MEMORY; \
4354 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4356 #include "actionlist.h"
4358 #undef DEFINE_ACTION_MULTI
4359 #undef DEFINE_ACTION_STRING
4360 #undef DEFINE_ACTION_BOOL
4361 #undef DEFINE_ACTION_ALIAS
4362 #undef DEFINE_CGI_PARAM_CUSTOM
4363 #undef DEFINE_CGI_PARAM_RADIO
4364 #undef DEFINE_CGI_PARAM_NO_RADIO
4370 /*********************************************************************
4372 * Function : actions_from_radio
4374 * Description : Converts a map of parameters passed to a CGI function
4375 * into an actionsfile entry.
4378 * 1 : parameters = parameters to the CGI call
4379 * 2 : action = Action to change. Must be valid before
4380 * the call, actions not specified will be
4383 * Returns : JB_ERR_OK on success
4384 * JB_ERR_MEMORY on out-of-memory
4386 *********************************************************************/
4387 static jb_err actions_from_radio(const struct map * parameters,
4388 struct action_spec *action)
4390 static int first_time = 1;
4394 const char * js_name;
4395 jb_err err = JB_ERR_OK;
4400 /* Statics are generally a potential race condition,
4401 * but in this case we're safe and don't need semaphores.
4402 * Be careful if you modify this function.
4406 #define JAVASCRIPTIFY(dest_var, string) \
4408 static char js_name_arr[] = string; \
4411 javascriptify(js_name_arr); \
4413 dest_var = js_name_arr; \
4416 #define DEFINE_ACTION_BOOL(name, bit) \
4417 JAVASCRIPTIFY(js_name, name); \
4418 ch = get_char_param(parameters, js_name); \
4421 action->add |= bit; \
4422 action->mask |= bit; \
4424 else if (ch == 'N') \
4426 action->add &= ~bit; \
4427 action->mask &= ~bit; \
4429 else if (ch == 'X') \
4431 action->add &= ~bit; \
4432 action->mask |= bit; \
4435 #define DEFINE_ACTION_STRING(name, bit, index) \
4436 JAVASCRIPTIFY(js_name, name); \
4437 ch = get_char_param(parameters, js_name); \
4441 JAVASCRIPTIFY(js_name, name "-mode"); \
4442 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4443 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4445 JAVASCRIPTIFY(js_name, name "-param"); \
4446 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4448 if (param != NULL) \
4450 if (NULL == (param_dup = strdup(param))) \
4452 return JB_ERR_MEMORY; \
4454 freez(action->string[index]); \
4455 action->add |= bit; \
4456 action->mask |= bit; \
4457 action->string[index] = param_dup; \
4460 else if (ch == 'N') \
4462 if (action->add & bit) \
4464 freez(action->string[index]); \
4466 action->add &= ~bit; \
4467 action->mask &= ~bit; \
4469 else if (ch == 'X') \
4471 if (action->add & bit) \
4473 freez(action->string[index]); \
4475 action->add &= ~bit; \
4476 action->mask |= bit; \
4479 #define DEFINE_ACTION_MULTI(name, index) \
4480 JAVASCRIPTIFY(js_name, name); \
4481 ch = get_char_param(parameters, js_name); \
4486 else if (ch == 'N') \
4488 list_remove_all(action->multi_add[index]); \
4489 list_remove_all(action->multi_remove[index]); \
4490 action->multi_remove_all[index] = 1; \
4492 else if (ch == 'X') \
4494 list_remove_all(action->multi_add[index]); \
4495 list_remove_all(action->multi_remove[index]); \
4496 action->multi_remove_all[index] = 0; \
4499 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4501 #include "actionlist.h"
4503 #undef DEFINE_ACTION_MULTI
4504 #undef DEFINE_ACTION_STRING
4505 #undef DEFINE_ACTION_BOOL
4506 #undef DEFINE_ACTION_ALIAS
4507 #undef JAVASCRIPTIFY
4515 #endif /* def FEATURE_CGI_EDIT_ACTIONS */