1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.21 2002/03/22 18:02:48 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://ijbswa.sourceforge.net
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.21 2002/03/22 18:02:48 jongfoster
46 * Fixing remote toggle
48 * Revision 1.20 2002/03/16 20:28:34 oes
49 * Added descriptions to the filters so users will know what they select in the cgi editor
51 * Revision 1.19 2002/03/16 18:38:14 jongfoster
52 * Stopping stupid or malicious users from breaking the actions
53 * file using the web-based editor.
55 * Revision 1.18 2002/03/16 14:57:44 jongfoster
56 * Full support for enabling/disabling modular filters.
58 * Revision 1.17 2002/03/16 14:26:42 jongfoster
59 * First version of modular filters support - READ ONLY!
60 * Fixing a double-free bug in the out-of-memory handling in map_radio().
62 * Revision 1.16 2002/03/07 03:46:17 oes
63 * Fixed compiler warnings
65 * Revision 1.15 2002/03/06 22:54:35 jongfoster
66 * Automated function-comment nitpicking.
68 * Revision 1.14 2002/03/05 00:24:51 jongfoster
69 * Patch to always edit the current actions file.
71 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
72 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
74 * Revision 1.12 2002/03/03 09:18:03 joergs
75 * Made jumbjuster work on AmigaOS again.
77 * Revision 1.11 2002/01/23 01:03:31 jongfoster
78 * Fixing gcc [CygWin] compiler warnings
80 * Revision 1.10 2002/01/23 00:22:59 jongfoster
81 * Adding new function cgi_edit_actions_section_swap(), to reorder
84 * Adding get_url_spec_param() to get a validated URL pattern.
86 * Moving edit_read_line() out of this file and into loaders.c.
88 * Adding missing html_encode() to many CGI functions.
90 * Moving the functions that #include actionlist.h to the end of the file,
91 * because the Visual C++ 97 debugger gets extremely confused if you try
92 * to debug any code that comes after them in the file.
94 * Major optimizations in cgi_edit_actions_list() to reduce the size of
95 * the generated HTML (down 40% from 550k to 304k), with major side-effects
96 * throughout the editor and templates. In particular, the length of the
97 * URLs throughout the editor has been drastically reduced, by cutting
98 * paramater names down to 1 character and CGI names down to 3-4
99 * characters, by removing all non-essential CGI paramaters even at the
100 * expense of having to re-read the actions file for the most trivial
101 * page, and by using relative rather than absolute URLs. This means
102 * that this (typical example):
104 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
105 * filename=ijb&ver=1011487572&section=12&pattern=13
106 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
110 * <a href="eau?f=ijb&v=1011487572&p=13">
112 * Revision 1.9 2002/01/17 20:56:22 jongfoster
113 * Replacing hard references to the URL of the config interface
114 * with #defines from project.h
116 * Revision 1.8 2001/11/30 23:35:51 jongfoster
117 * Renaming actionsfile to ijb.action
119 * Revision 1.7 2001/11/13 00:28:24 jongfoster
120 * - Renaming parameters from edit-actions-for-url so that they only
121 * contain legal JavaScript characters. If we wanted to write
122 * JavaScript that worked with Netscape 4, this is nessacery.
123 * (Note that at the moment the JavaScript doesn't actually work
124 * with Netscape 4, but now this is purely a template issue, not
125 * one affecting code).
126 * - Adding new CGIs for use by non-JavaScript browsers:
127 * edit-actions-url-form
128 * edit-actions-add-url-form
129 * edit-actions-remove-url-form
132 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
133 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
134 * by and __OS2__ ifdef.
136 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
137 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
138 * threads to call select() simultaneously. So, it's time to do a real, live,
139 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
140 * (native). Both versions will work, but using __OS2__ offers multi-threading.
142 * Revision 1.4 2001/10/23 21:48:19 jongfoster
143 * Cleaning up error handling in CGI functions - they now send back
144 * a HTML error page and should never cause a FATAL error. (Fixes one
145 * potential source of "denial of service" attacks).
147 * CGI actions file editor that works and is actually useful.
149 * Ability to toggle JunkBuster remotely using a CGI call.
151 * You can turn off both the above features in the main configuration
152 * file, e.g. if you are running a multi-user proxy.
154 * Revision 1.3 2001/10/14 22:12:49 jongfoster
155 * New version of CGI-based actionsfile editor.
156 * Major changes, including:
157 * - Completely new file parser and file output routines
158 * - edit-actions CGI renamed edit-actions-for-url
159 * - All CGIs now need a filename parameter, except for...
160 * - New CGI edit-actions which doesn't need a filename,
161 * to allow you to start the editor up.
162 * - edit-actions-submit now works, and now automatically
163 * redirects you back to the main edit-actions-list handler.
165 * Revision 1.2 2001/09/16 17:05:14 jongfoster
166 * Removing unused #include showarg.h
168 * Revision 1.1 2001/09/16 15:47:37 jongfoster
169 * First version of CGI-based edit interface. This is very much a
170 * work-in-progress, and you can't actually use it to edit anything
171 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
172 * to have any effect.
175 **********************************************************************/
181 * FIXME: Following includes copied from cgi.c - which are actually needed?
186 #include <sys/types.h>
191 #include <sys/stat.h>
194 #define snprintf _snprintf
195 #endif /* def _WIN32 */
200 #include "cgisimple.h"
204 #include "miscutil.h"
208 /* loadcfg.h is for g_bToggleIJB only */
209 #include "urlmatch.h"
211 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
214 #ifdef FEATURE_CGI_EDIT_ACTIONS
218 struct file_line * next;
226 struct action_spec action[1];
235 /* Add more data types here... e.g.
238 struct url_spec url[1];
242 struct action_spec action[1];
251 #define FILE_LINE_UNPROCESSED 1
252 #define FILE_LINE_BLANK 2
253 #define FILE_LINE_ALIAS_HEADER 3
254 #define FILE_LINE_ALIAS_ENTRY 4
255 #define FILE_LINE_ACTION 5
256 #define FILE_LINE_URL 6
257 #define FILE_LINE_SETTINGS_HEADER 7
258 #define FILE_LINE_SETTINGS_ENTRY 8
259 #define FILE_LINE_DESCRIPTION_HEADER 9
260 #define FILE_LINE_DESCRIPTION_ENTRY 10
265 struct file_line * lines;
266 const char * filename; /* Full pathname - e.g. "/etc/junkbuster/wibble.action" */
267 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
268 /* Pre-encoded with url_encode() for ease of use. */
269 const char * version_str; /* Last modification time, as a string. For CGI param */
270 /* Can be used in URL without using url_param(). */
271 unsigned version; /* Last modification time - prevents chaos with
272 * the browser's "back" button. Note that this is a
273 * time_t cast to an unsigned. When comparing, always
274 * cast the time_t to an unsigned, and *NOT* vice-versa.
275 * This may lose the top few bits, but they're not
276 * significant anyway.
278 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
279 * Note that changing this after the file has been
280 * read in will cause a mess.
282 struct file_line * parse_error; /* On parse error, this is the offending line. */
283 const char * parse_error_text; /* On parse error, this is the problem.
284 * (Statically allocated) */
287 #define CGI_ACTION_PARAM_LEN_MAX 500
289 /* FIXME: Following non-static functions should be prototyped in .h or made static */
291 /* Functions to read and write arbitrary config files */
292 jb_err edit_read_file(struct client_state *csp,
293 const struct map *parameters,
296 struct editable_file **pfile);
297 jb_err edit_write_file(struct editable_file * file);
298 void edit_free_file(struct editable_file * file);
300 /* Functions to read and write actions files */
301 jb_err edit_parse_actions_file(struct editable_file * file);
302 jb_err edit_read_actions_file(struct client_state *csp,
303 struct http_response *rsp,
304 const struct map *parameters,
306 struct editable_file **pfile);
309 jb_err cgi_error_modified(struct client_state *csp,
310 struct http_response *rsp,
311 const char *filename);
312 jb_err cgi_error_parse(struct client_state *csp,
313 struct http_response *rsp,
314 struct editable_file *file);
315 jb_err cgi_error_file(struct client_state *csp,
316 struct http_response *rsp,
317 const char *filename);
318 jb_err cgi_error_disabled(struct client_state *csp,
319 struct http_response *rsp);
321 /* Internal arbitrary config file support functions */
322 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
323 static void edit_free_file_lines(struct file_line * first_line);
325 /* Internal actions file support functions */
326 static int match_actions_file_header_line(const char * line, const char * name);
327 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
329 /* Internal parameter parsing functions */
330 static jb_err get_file_name_param(struct client_state *csp,
331 const struct map *parameters,
332 const char *param_name,
335 const char **pparam);
336 static jb_err get_number_param(struct client_state *csp,
337 const struct map *parameters,
340 static jb_err get_url_spec_param(struct client_state *csp,
341 const struct map *parameters,
344 static jb_err get_string_param(const struct map *parameters,
345 const char *param_name,
346 const char **pparam);
348 /* Internal actionsfile <==> HTML conversion functions */
349 static jb_err map_radio(struct map * exports,
350 const char * optionname,
353 static jb_err actions_to_radio(struct map * exports,
354 const struct action_spec *action);
355 static jb_err actions_from_radio(const struct map * parameters,
356 struct action_spec *action);
359 static jb_err map_copy_parameter_html(struct map *out,
360 const struct map *in,
362 #if 0 /* unused function */
363 static jb_err map_copy_parameter_url(struct map *out,
364 const struct map *in,
366 #endif /* unused function */
368 /*********************************************************************
370 * Function : map_copy_parameter_html
372 * Description : Copy a CGI parameter from one map to another, HTML
376 * 1 : out = target map
377 * 2 : in = source map
378 * 3 : name = name of cgi parameter to copy
380 * Returns : JB_ERR_OK on success
381 * JB_ERR_MEMORY on out-of-memory
382 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
385 *********************************************************************/
386 static jb_err map_copy_parameter_html(struct map *out,
387 const struct map *in,
397 value = lookup(in, name);
398 err = map(out, name, 1, html_encode(value), 0);
405 else if (*value == '\0')
407 return JB_ERR_CGI_PARAMS;
416 #if 0 /* unused function */
417 /*********************************************************************
419 * Function : map_copy_parameter_html
421 * Description : Copy a CGI parameter from one map to another, URL
425 * 1 : out = target map
426 * 2 : in = source map
427 * 3 : name = name of cgi parameter to copy
429 * Returns : JB_ERR_OK on success
430 * JB_ERR_MEMORY on out-of-memory
431 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
434 *********************************************************************/
435 static jb_err map_copy_parameter_url(struct map *out,
436 const struct map *in,
446 value = lookup(in, name);
447 err = map(out, name, 1, url_encode(value), 0);
454 else if (*value == '\0')
456 return JB_ERR_CGI_PARAMS;
463 #endif /* 0 - unused function */
465 /*********************************************************************
467 * Function : cgi_edit_actions_url_form
469 * Description : CGI function that displays a form for
473 * 1 : csp = Current client state (buffers, headers, etc...)
474 * 2 : rsp = http_response data structure for output
475 * 3 : parameters = map of cgi parameters
478 * f : (filename) Identifies the file to edit
479 * v : (version) File's last-modified time
480 * p : (pattern) Line number of pattern to edit
482 * Returns : JB_ERR_OK on success
483 * JB_ERR_MEMORY on out-of-memory
484 * JB_ERR_CGI_PARAMS if the CGI parameters are not
485 * specified or not valid.
487 *********************************************************************/
488 jb_err cgi_edit_actions_url_form(struct client_state *csp,
489 struct http_response *rsp,
490 const struct map *parameters)
492 struct map * exports;
494 struct editable_file * file;
495 struct file_line * cur_line;
496 unsigned line_number;
503 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
505 return cgi_error_disabled(csp, rsp);
508 err = get_number_param(csp, parameters, "p", &patternid);
514 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
517 /* No filename specified, can't read file, modified, or out of memory. */
518 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
521 cur_line = file->lines;
523 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
525 cur_line = cur_line->next;
528 if ( (cur_line == NULL)
529 || (line_number != patternid)
531 || (cur_line->type != FILE_LINE_URL))
533 /* Invalid "patternid" parameter */
534 edit_free_file(file);
535 return JB_ERR_CGI_PARAMS;
538 if (NULL == (exports = default_exports(csp, NULL)))
540 edit_free_file(file);
541 return JB_ERR_MEMORY;
544 err = map(exports, "f", 1, file->identifier, 1);
545 if (!err) err = map(exports, "v", 1, file->version_str, 1);
546 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
547 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
549 edit_free_file(file);
557 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
561 /*********************************************************************
563 * Function : cgi_edit_actions_add_url_form
565 * Description : CGI function that displays a form for
569 * 1 : csp = Current client state (buffers, headers, etc...)
570 * 2 : rsp = http_response data structure for output
571 * 3 : parameters = map of cgi parameters
574 * f : (filename) Identifies the file to edit
575 * v : (version) File's last-modified time
576 * s : (section) Line number of section to edit
578 * Returns : JB_ERR_OK on success
579 * JB_ERR_MEMORY on out-of-memory
580 * JB_ERR_CGI_PARAMS if the CGI parameters are not
581 * specified or not valid.
583 *********************************************************************/
584 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
585 struct http_response *rsp,
586 const struct map *parameters)
595 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
597 return cgi_error_disabled(csp, rsp);
600 if (NULL == (exports = default_exports(csp, NULL)))
602 return JB_ERR_MEMORY;
605 err = map_copy_parameter_html(exports, parameters, "f");
606 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
607 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
615 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
619 /*********************************************************************
621 * Function : cgi_edit_actions_remove_url_form
623 * Description : CGI function that displays a form for
627 * 1 : csp = Current client state (buffers, headers, etc...)
628 * 2 : rsp = http_response data structure for output
629 * 3 : parameters = map of cgi parameters
632 * f : (filename) Identifies the file to edit
633 * v : (version) File's last-modified time
634 * p : (pattern) Line number of pattern to edit
636 * Returns : JB_ERR_OK on success
637 * JB_ERR_MEMORY on out-of-memory
638 * JB_ERR_CGI_PARAMS if the CGI parameters are not
639 * specified or not valid.
641 *********************************************************************/
642 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
643 struct http_response *rsp,
644 const struct map *parameters)
646 struct map * exports;
648 struct editable_file * file;
649 struct file_line * cur_line;
650 unsigned line_number;
657 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
659 return cgi_error_disabled(csp, rsp);
662 err = get_number_param(csp, parameters, "p", &patternid);
668 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
671 /* No filename specified, can't read file, modified, or out of memory. */
672 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
675 cur_line = file->lines;
677 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
679 cur_line = cur_line->next;
682 if ( (cur_line == NULL)
683 || (line_number != patternid)
685 || (cur_line->type != FILE_LINE_URL))
687 /* Invalid "patternid" parameter */
688 edit_free_file(file);
689 return JB_ERR_CGI_PARAMS;
692 if (NULL == (exports = default_exports(csp, NULL)))
694 edit_free_file(file);
695 return JB_ERR_MEMORY;
698 err = map(exports, "f", 1, file->identifier, 1);
699 if (!err) err = map(exports, "v", 1, file->version_str, 1);
700 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
701 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
703 edit_free_file(file);
711 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
715 /*********************************************************************
717 * Function : edit_write_file
719 * Description : Write a complete file to disk.
722 * 1 : filename = File to write to.
723 * 2 : file = Data structure to write.
725 * Returns : JB_ERR_OK on success
726 * JB_ERR_FILE on error writing to file.
727 * JB_ERR_MEMORY on out of memory
729 *********************************************************************/
730 jb_err edit_write_file(struct editable_file * file)
733 struct file_line * cur_line;
734 struct stat statbuf[1];
735 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
736 digits in time_t, assuming this is a 64-bit
737 machine, plus null terminator, plus one
741 assert(file->filename);
743 #if defined(AMIGA) || defined(__OS2__)
744 if (NULL == (fp = fopen(file->filename, "w")))
746 if (NULL == (fp = fopen(file->filename, "wt")))
747 #endif /* def AMIGA */
752 cur_line = file->lines;
753 while (cur_line != NULL)
757 if (fputs(cur_line->raw, fp) < 0)
765 if (cur_line->prefix)
767 if (fputs(cur_line->prefix, fp) < 0)
773 if (cur_line->unprocessed)
775 /* This should be a single line - sanity check. */
776 assert(NULL == strchr(cur_line->unprocessed, '\r'));
777 assert(NULL == strchr(cur_line->unprocessed, '\n'));
779 if (NULL != strchr(cur_line->unprocessed, '#'))
781 /* Must quote '#' characters */
788 /* Count number of # characters, so we know length of output string */
789 src = cur_line->unprocessed;
790 while (NULL != (src = strchr(src, '#')))
797 /* Allocate new memory for string */
798 len = strlen(cur_line->unprocessed);
799 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
801 /* Uh oh, just trashed file! */
803 return JB_ERR_MEMORY;
806 /* Loop through string from end */
807 src = cur_line->unprocessed + len;
808 dest = str + len + numhash;
809 for ( ; len >= 0; len--)
811 if ((*dest-- = *src--) == '#')
815 assert(numhash >= 0);
818 assert(numhash == 0);
819 assert(src + 1 == cur_line->unprocessed);
820 assert(dest + 1 == str);
822 if (fputs(str, fp) < 0)
833 /* Can write without quoting '#' characters. */
834 if (fputs(cur_line->unprocessed, fp) < 0)
840 if (fputs(NEWLINE(file->newline), fp) < 0)
848 /* FIXME: Write data from file->data->whatever */
852 cur_line = cur_line->next;
858 /* Update the version stamp in the file structure, since we just
859 * wrote to the file & changed it's date.
861 if (stat(file->filename, statbuf) < 0)
863 /* Error, probably file not found. */
866 file->version = (unsigned)statbuf->st_mtime;
868 /* Correct file->version_str */
869 freez(file->version_str);
870 snprintf(version_buf, 22, "%u", file->version);
871 version_buf[21] = '\0';
872 file->version_str = strdup(version_buf);
873 if (version_buf == NULL)
875 return JB_ERR_MEMORY;
882 /*********************************************************************
884 * Function : edit_free_file
886 * Description : Free a complete file in memory.
889 * 1 : file = Data structure to free.
893 *********************************************************************/
894 void edit_free_file(struct editable_file * file)
898 /* Silently ignore NULL pointer */
902 edit_free_file_lines(file->lines);
903 freez(file->filename);
904 freez(file->identifier);
905 freez(file->version_str);
907 file->parse_error_text = NULL; /* Statically allocated */
908 file->parse_error = NULL;
914 /*********************************************************************
916 * Function : edit_free_file
918 * Description : Free an entire linked list of file lines.
921 * 1 : first_line = Data structure to free.
925 *********************************************************************/
926 static void edit_free_file_lines(struct file_line * first_line)
928 struct file_line * next_line;
930 while (first_line != NULL)
932 next_line = first_line->next;
933 first_line->next = NULL;
934 freez(first_line->raw);
935 freez(first_line->prefix);
936 freez(first_line->unprocessed);
937 switch(first_line->type)
939 case 0: /* special case if memory zeroed */
940 case FILE_LINE_UNPROCESSED:
941 case FILE_LINE_BLANK:
942 case FILE_LINE_ALIAS_HEADER:
943 case FILE_LINE_SETTINGS_HEADER:
944 case FILE_LINE_DESCRIPTION_HEADER:
945 case FILE_LINE_DESCRIPTION_ENTRY:
946 case FILE_LINE_ALIAS_ENTRY:
948 /* No data is stored for these */
951 case FILE_LINE_ACTION:
952 free_action(first_line->data.action);
955 case FILE_LINE_SETTINGS_ENTRY:
956 freez(first_line->data.setting.name);
957 freez(first_line->data.setting.svalue);
960 /* Should never happen */
964 first_line->type = 0; /* paranoia */
966 first_line = next_line;
971 /*********************************************************************
973 * Function : match_actions_file_header_line
975 * Description : Match an actions file {{header}} line
978 * 1 : line = String from file
979 * 2 : name = Header to match against
981 * Returns : 0 iff they match.
983 *********************************************************************/
984 static int match_actions_file_header_line(const char * line, const char * name)
992 if ((line[0] != '{') || (line[1] != '{'))
998 /* Look for optional whitespace */
999 while ( (*line == ' ') || (*line == '\t') )
1004 /* Look for the specified name (case-insensitive) */
1006 if (0 != strncmpic(line, name, len))
1012 /* Look for optional whitespace */
1013 while ( (*line == ' ') || (*line == '\t') )
1018 /* Look for "}}" and end of string*/
1019 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1029 /*********************************************************************
1031 * Function : match_actions_file_header_line
1033 * Description : Match an actions file {{header}} line
1036 * 1 : line = String from file. Must not start with
1037 * whitespace (else infinite loop!)
1038 * 2 : name = Destination for name
1039 * 2 : name = Destination for value
1041 * Returns : JB_ERR_OK on success
1042 * JB_ERR_MEMORY on out-of-memory
1043 * JB_ERR_PARSE if there's no "=" sign, or if there's
1044 * nothing before the "=" sign (but empty
1045 * values *after* the "=" sign are legal).
1047 *********************************************************************/
1048 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1050 const char * name_end;
1051 const char * value_start;
1057 assert(*line != ' ');
1058 assert(*line != '\t');
1063 value_start = strchr(line, '=');
1064 if ((value_start == NULL) || (value_start == line))
1066 return JB_ERR_PARSE;
1069 name_end = value_start - 1;
1071 /* Eat any whitespace before the '=' */
1072 while ((*name_end == ' ') || (*name_end == '\t'))
1075 * we already know we must have at least 1 non-ws char
1076 * at start of buf - no need to check
1081 name_len = name_end - line + 1; /* Length excluding \0 */
1082 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1084 return JB_ERR_MEMORY;
1086 strncpy(*pname, line, name_len);
1087 (*pname)[name_len] = '\0';
1089 /* Eat any the whitespace after the '=' */
1091 while ((*value_start == ' ') || (*value_start == '\t'))
1096 if (NULL == (*pvalue = strdup(value_start)))
1100 return JB_ERR_MEMORY;
1107 /*********************************************************************
1109 * Function : edit_parse_actions_file
1111 * Description : Parse an actions file in memory.
1113 * Passed linked list must have the "data" member
1114 * zeroed, and must contain valid "next" and
1115 * "unprocessed" fields. The "raw" and "prefix"
1116 * fields are ignored, and "type" is just overwritten.
1118 * Note that on error the file may have been
1122 * 1 : file = Actions file to be parsed in-place.
1124 * Returns : JB_ERR_OK on success
1125 * JB_ERR_MEMORY on out-of-memory
1126 * JB_ERR_PARSE on error
1128 *********************************************************************/
1129 jb_err edit_parse_actions_file(struct editable_file * file)
1131 struct file_line * cur_line;
1133 const char * text; /* Text from a line */
1134 char * name; /* For lines of the form name=value */
1135 char * value; /* For lines of the form name=value */
1136 struct action_alias * alias_list = NULL;
1137 jb_err err = JB_ERR_OK;
1139 /* alias_list contains the aliases defined in this file.
1140 * It might be better to use the "file_line.data" fields
1141 * in the relavent places instead.
1144 cur_line = file->lines;
1146 /* A note about blank line support: Blank lines should only
1147 * ever occur as the last line in the file. This function
1148 * is more forgiving than that - FILE_LINE_BLANK can occur
1152 /* Skip leading blanks. Should only happen if file is
1153 * empty (which is valid, but pointless).
1155 while ( (cur_line != NULL)
1156 && (cur_line->unprocessed[0] == '\0') )
1159 cur_line->type = FILE_LINE_BLANK;
1160 cur_line = cur_line->next;
1163 if ( (cur_line != NULL)
1164 && (cur_line->unprocessed[0] != '{') )
1166 /* File doesn't start with a header */
1167 file->parse_error = cur_line;
1168 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1169 return JB_ERR_PARSE;
1172 if ( (cur_line != NULL) && (0 ==
1173 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1175 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1177 cur_line = cur_line->next;
1178 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1180 if (cur_line->unprocessed[0])
1182 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1184 err = split_line_on_equals(cur_line->unprocessed,
1185 &cur_line->data.setting.name,
1186 &cur_line->data.setting.svalue);
1187 if (err == JB_ERR_MEMORY)
1191 else if (err != JB_ERR_OK)
1193 /* Line does not contain a name=value pair */
1194 file->parse_error = cur_line;
1195 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1196 return JB_ERR_PARSE;
1201 cur_line->type = FILE_LINE_BLANK;
1203 cur_line = cur_line->next;
1207 if ( (cur_line != NULL) && (0 ==
1208 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1210 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1212 cur_line = cur_line->next;
1213 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1215 if (cur_line->unprocessed[0])
1217 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1221 cur_line->type = FILE_LINE_BLANK;
1223 cur_line = cur_line->next;
1227 if ( (cur_line != NULL) && (0 ==
1228 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1230 cur_line->type = FILE_LINE_ALIAS_HEADER;
1232 cur_line = cur_line->next;
1233 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1235 if (cur_line->unprocessed[0])
1237 /* define an alias */
1238 struct action_alias * new_alias;
1240 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1242 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1243 if (err == JB_ERR_MEMORY)
1247 else if (err != JB_ERR_OK)
1249 /* Line does not contain a name=value pair */
1250 file->parse_error = cur_line;
1251 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1252 return JB_ERR_PARSE;
1255 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1260 free_alias_list(alias_list);
1261 return JB_ERR_MEMORY;
1264 err = get_actions(value, alias_list, new_alias->action);
1267 /* Invalid action or out of memory */
1271 free_alias_list(alias_list);
1272 if (err == JB_ERR_MEMORY)
1278 /* Line does not contain a name=value pair */
1279 file->parse_error = cur_line;
1280 file->parse_error_text = "This alias does not specify a valid set of actions.";
1281 return JB_ERR_PARSE;
1287 new_alias->name = name;
1290 new_alias->next = alias_list;
1291 alias_list = new_alias;
1295 cur_line->type = FILE_LINE_BLANK;
1297 cur_line = cur_line->next;
1301 /* Header done, process the main part of the file */
1302 while (cur_line != NULL)
1304 /* At this point, (cur_line->unprocessed[0] == '{') */
1305 assert(cur_line->unprocessed[0] == '{');
1306 text = cur_line->unprocessed + 1;
1307 len = strlen(text) - 1;
1308 if (text[len] != '}')
1310 /* No closing } on header */
1311 free_alias_list(alias_list);
1312 file->parse_error = cur_line;
1313 file->parse_error_text = "Headers starting with '{' must have a "
1314 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1315 "must close with two brackets ('}}').";
1316 return JB_ERR_PARSE;
1321 /* An invalid {{ header. */
1322 free_alias_list(alias_list);
1323 file->parse_error = cur_line;
1324 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1325 "Please remember that the system (two-bracket) headers must "
1326 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1327 "and must appear before any actions (one-bracket) headers. "
1328 "Also note that system headers may not be repeated.";
1329 return JB_ERR_PARSE;
1332 while ( (*text == ' ') || (*text == '\t') )
1338 && ( (text[len - 1] == ' ')
1339 || (text[len - 1] == '\t') ) )
1344 cur_line->type = FILE_LINE_ACTION;
1346 /* Remove {} and make copy */
1347 if (NULL == (value = (char *) malloc(len + 1)))
1350 free_alias_list(alias_list);
1351 return JB_ERR_MEMORY;
1353 strncpy(value, text, len);
1357 err = get_actions(value, alias_list, cur_line->data.action);
1360 /* Invalid action or out of memory */
1362 free_alias_list(alias_list);
1363 if (err == JB_ERR_MEMORY)
1369 /* Line does not contain a name=value pair */
1370 file->parse_error = cur_line;
1371 file->parse_error_text = "This header does not specify a valid set of actions.";
1372 return JB_ERR_PARSE;
1376 /* Done with string - it was clobbered anyway */
1379 /* Process next line */
1380 cur_line = cur_line->next;
1382 /* Loop processing URL patterns */
1383 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1385 if (cur_line->unprocessed[0])
1387 /* Could parse URL here, but this isn't currently needed */
1389 cur_line->type = FILE_LINE_URL;
1393 cur_line->type = FILE_LINE_BLANK;
1395 cur_line = cur_line->next;
1397 } /* End main while(cur_line != NULL) loop */
1399 free_alias_list(alias_list);
1405 /*********************************************************************
1407 * Function : edit_read_file_lines
1409 * Description : Read all the lines of a file into memory.
1410 * Handles whitespace, comments and line continuation.
1413 * 1 : fp = File to read from. On return, this will be
1414 * at EOF but it will not have been closed.
1415 * 2 : pfile = Destination for a linked list of file_lines.
1416 * Will be set to NULL on error.
1418 * Returns : JB_ERR_OK on success
1419 * JB_ERR_MEMORY on out-of-memory
1421 *********************************************************************/
1422 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1424 struct file_line * first_line; /* Keep for return value or to free */
1425 struct file_line * cur_line; /* Current line */
1426 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1434 cur_line = first_line = zalloc(sizeof(struct file_line));
1435 if (cur_line == NULL)
1437 return JB_ERR_MEMORY;
1440 cur_line->type = FILE_LINE_UNPROCESSED;
1442 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1445 /* Out of memory or empty file. */
1446 /* Note that empty file is not an error we propogate up */
1448 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1453 prev_line = cur_line;
1454 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1455 if (cur_line == NULL)
1458 edit_free_file_lines(first_line);
1459 return JB_ERR_MEMORY;
1462 cur_line->type = FILE_LINE_UNPROCESSED;
1464 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1465 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1468 edit_free_file_lines(first_line);
1469 return JB_ERR_MEMORY;
1473 while (rval != JB_ERR_FILE);
1477 /* We allocated one too many - free it */
1478 prev_line->next = NULL;
1481 *pfile = first_line;
1486 /*********************************************************************
1488 * Function : edit_read_file
1490 * Description : Read a complete file into memory.
1491 * Handles CGI parameter parsing. If requested, also
1492 * checks the file's modification timestamp.
1495 * 1 : csp = Current client state (buffers, headers, etc...)
1496 * 2 : parameters = map of cgi parameters.
1497 * 3 : require_version = true to check "ver" parameter.
1498 * 4 : suffix = File extension, e.g. ".action".
1499 * 5 : pfile = Destination for the file. Will be set
1503 * filename : The name of the file to read, without the
1504 * path or ".action" extension.
1505 * ver : (Only if require_version is nonzero)
1506 * Timestamp of the actions file. If wrong, this
1507 * function fails with JB_ERR_MODIFIED.
1509 * Returns : JB_ERR_OK on success
1510 * JB_ERR_MEMORY on out-of-memory
1511 * JB_ERR_CGI_PARAMS if "filename" was not specified
1513 * JB_ERR_FILE if the file cannot be opened or
1515 * JB_ERR_MODIFIED if version checking was requested and
1516 * failed - the file was modified outside
1517 * of this CGI editor instance.
1519 *********************************************************************/
1520 jb_err edit_read_file(struct client_state *csp,
1521 const struct map *parameters,
1522 int require_version,
1524 struct editable_file **pfile)
1526 struct file_line * lines;
1530 const char * identifier;
1531 struct editable_file * file;
1532 unsigned version = 0;
1533 struct stat statbuf[1];
1534 char version_buf[22];
1535 int newline = NEWLINE_UNKNOWN;
1543 err = get_file_name_param(csp, parameters, "f", suffix,
1544 &filename, &identifier);
1550 if (stat(filename, statbuf) < 0)
1552 /* Error, probably file not found. */
1556 version = (unsigned) statbuf->st_mtime;
1558 if (require_version)
1560 unsigned specified_version;
1561 err = get_number_param(csp, parameters, "v", &specified_version);
1568 if (version != specified_version)
1570 return JB_ERR_MODIFIED;
1574 #if defined(AMIGA) || defined(__OS2__)
1575 if (NULL == (fp = fopen(filename,"r")))
1577 if (NULL == (fp = fopen(filename,"rt")))
1578 #endif /* def AMIGA */
1584 err = edit_read_file_lines(fp, &lines, &newline);
1594 file = (struct editable_file *) zalloc(sizeof(*file));
1598 edit_free_file_lines(lines);
1602 file->lines = lines;
1603 file->newline = newline;
1604 file->filename = filename;
1605 file->version = version;
1606 file->identifier = url_encode(identifier);
1608 if (file->identifier == NULL)
1610 edit_free_file(file);
1611 return JB_ERR_MEMORY;
1614 /* Correct file->version_str */
1615 freez(file->version_str);
1616 snprintf(version_buf, 22, "%u", file->version);
1617 version_buf[21] = '\0';
1618 file->version_str = strdup(version_buf);
1619 if (version_buf == NULL)
1621 edit_free_file(file);
1622 return JB_ERR_MEMORY;
1630 /*********************************************************************
1632 * Function : edit_read_actions_file
1634 * Description : Read a complete actions file into memory.
1635 * Handles CGI parameter parsing. If requested, also
1636 * checks the file's modification timestamp.
1638 * If this function detects an error in the categories
1639 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1640 * then it handles it by filling in the specified
1641 * response structure and returning JB_ERR_FILE.
1644 * 1 : csp = Current client state (buffers, headers, etc...)
1645 * 2 : rsp = HTTP response. Only filled in on error.
1646 * 2 : parameters = map of cgi parameters.
1647 * 3 : require_version = true to check "ver" parameter.
1648 * 4 : pfile = Destination for the file. Will be set
1652 * filename : The name of the actions file to read, without the
1653 * path or ".action" extension.
1654 * ver : (Only if require_version is nonzero)
1655 * Timestamp of the actions file. If wrong, this
1656 * function fails with JB_ERR_MODIFIED.
1658 * Returns : JB_ERR_OK on success
1659 * JB_ERR_MEMORY on out-of-memory
1660 * JB_ERR_CGI_PARAMS if "filename" was not specified
1662 * JB_ERR_FILE if the file does not contain valid data,
1663 * or if file cannot be opened or
1664 * contains no data, or if version
1665 * checking was requested and failed.
1667 *********************************************************************/
1668 jb_err edit_read_actions_file(struct client_state *csp,
1669 struct http_response *rsp,
1670 const struct map *parameters,
1671 int require_version,
1672 struct editable_file **pfile)
1675 struct editable_file *file;
1683 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1686 /* Try to handle if possible */
1687 if (err == JB_ERR_FILE)
1689 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1691 else if (err == JB_ERR_MODIFIED)
1693 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1695 if (err == JB_ERR_OK)
1698 * Signal to higher-level CGI code that there was a problem but we
1699 * handled it, they should just return JB_ERR_OK.
1706 err = edit_parse_actions_file(file);
1709 if (err == JB_ERR_PARSE)
1711 err = cgi_error_parse(csp, rsp, file);
1712 if (err == JB_ERR_OK)
1715 * Signal to higher-level CGI code that there was a problem but we
1716 * handled it, they should just return JB_ERR_OK.
1721 edit_free_file(file);
1730 /*********************************************************************
1732 * Function : get_file_name_param
1734 * Description : Get the name of the file to edit from the parameters
1735 * passed to a CGI function. This function handles
1736 * security checks such as blocking urls containing
1737 * "/" or ".", prepending the config file directory,
1738 * and adding the specified suffix.
1740 * (This is an essential security check, otherwise
1741 * users may be able to pass "../../../etc/passwd"
1742 * and overwrite the password file [linux], "prn:"
1743 * and print random data [Windows], etc...)
1745 * This function only allows filenames contining the
1746 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1747 * That's probably too restrictive but at least it's
1751 * 1 : csp = Current client state (buffers, headers, etc...)
1752 * 2 : parameters = map of cgi parameters
1753 * 3 : param_name = The name of the parameter to read
1754 * 4 : suffix = File extension, e.g. ".actions"
1755 * 5 : pfilename = destination for full filename. Caller
1756 * free()s. Set to NULL on error.
1757 * 6 : pparam = destination for partial filename,
1758 * suitable for use in another URL. Allocated as part
1759 * of the map "parameters", so don't free it.
1760 * Set to NULL if not specified.
1762 * Returns : JB_ERR_OK on success
1763 * JB_ERR_MEMORY on out-of-memory
1764 * JB_ERR_CGI_PARAMS if "filename" was not specified
1767 *********************************************************************/
1768 static jb_err get_file_name_param(struct client_state *csp,
1769 const struct map *parameters,
1770 const char *param_name,
1773 const char **pparam)
1777 #if 0 /* Patch to make 3.0.0 work properly. */
1779 #endif /* 0 - Patch to make 3.0.0 work properly. */
1793 param = lookup(parameters, param_name);
1796 return JB_ERR_CGI_PARAMS;
1801 len = strlen(param);
1802 if (len >= FILENAME_MAX)
1805 return JB_ERR_CGI_PARAMS;
1808 /* Check every character to see if it's legal */
1810 while ((ch = *s++) != '\0')
1812 if ( ((ch < 'A') || (ch > 'Z'))
1813 && ((ch < 'a') || (ch > 'z'))
1814 && ((ch < '0') || (ch > '9'))
1818 /* Probable hack attempt. */
1819 return JB_ERR_CGI_PARAMS;
1824 * FIXME Following is a hack to make 3.0.0 work properly.
1825 * Change "#if 0" --> "#if 1" below when we have modular action
1829 #if 0 /* Patch to make 3.0.0 work properly. */
1830 /* Append extension */
1831 name = malloc(len + strlen(suffix) + 1);
1834 return JB_ERR_MEMORY;
1836 strcpy(name, param);
1837 strcpy(name + len, suffix);
1840 fullpath = make_path(csp->config->confdir, name);
1842 #else /* 1 - Patch to make 3.0.0 work properly. */
1843 if ((csp->actions_list == NULL)
1844 || (csp->actions_list->filename == NULL))
1846 return JB_ERR_CGI_PARAMS;
1849 fullpath = ( (csp->actions_list && csp->actions_list->filename)
1850 ? strdup(csp->actions_list->filename) : NULL);
1851 #endif /* 1 - Patch to make 3.0.0 work properly. */
1852 if (fullpath == NULL)
1854 return JB_ERR_MEMORY;
1858 *pfilename = fullpath;
1864 /*********************************************************************
1866 * Function : get_char_param
1868 * Description : Get a single-character parameter passed to a CGI
1872 * 1 : parameters = map of cgi parameters
1873 * 2 : param_name = The name of the parameter to read
1875 * Returns : Uppercase character on success, '\0' on error.
1877 *********************************************************************/
1878 static char get_char_param(const struct map *parameters,
1879 const char *param_name)
1886 ch = *(lookup(parameters, param_name));
1887 if ((ch >= 'a') && (ch <= 'z'))
1889 ch = ch - 'a' + 'A';
1896 /*********************************************************************
1898 * Function : get_string_param
1900 * Description : Get a string paramater, to be used as an
1901 * ACTION_STRING or ACTION_MULTI paramater.
1902 * Validates the input to prevent stupid/malicious
1903 * users from corrupting their action file.
1906 * 1 : parameters = map of cgi parameters
1907 * 2 : param_name = The name of the parameter to read
1908 * 3 : pparam = destination for paramater. Allocated as
1909 * part of the map "parameters", so don't free it.
1910 * Set to NULL if not specified.
1912 * Returns : JB_ERR_OK on success, or if the paramater
1913 * was not specified.
1914 * JB_ERR_MEMORY on out-of-memory.
1915 * JB_ERR_CGI_PARAMS if the paramater is not valid.
1917 *********************************************************************/
1918 static jb_err get_string_param(const struct map *parameters,
1919 const char *param_name,
1920 const char **pparam)
1932 param = lookup(parameters, param_name);
1938 if (strlen(param) >= CGI_ACTION_PARAM_LEN_MAX)
1943 * Note that the length limit is arbitrary, it just seems
1944 * sensible to limit it to *something*. There's no
1945 * technical reason for any limit at all.
1947 return JB_ERR_CGI_PARAMS;
1950 /* Check every character to see if it's legal */
1952 while ((ch = *s++) != '\0')
1954 if ( ((unsigned char)ch < (unsigned char)' ')
1957 /* Probable hack attempt, or user accidentally used '}'. */
1958 return JB_ERR_CGI_PARAMS;
1969 /*********************************************************************
1971 * Function : get_number_param
1973 * Description : Get a non-negative integer from the parameters
1974 * passed to a CGI function.
1977 * 1 : csp = Current client state (buffers, headers, etc...)
1978 * 2 : parameters = map of cgi parameters
1979 * 3 : name = Name of CGI parameter to read
1980 * 4 : pvalue = destination for value.
1981 * Set to -1 on error.
1983 * Returns : JB_ERR_OK on success
1984 * JB_ERR_MEMORY on out-of-memory
1985 * JB_ERR_CGI_PARAMS if the parameter was not specified
1988 *********************************************************************/
1989 static jb_err get_number_param(struct client_state *csp,
1990 const struct map *parameters,
2005 param = lookup(parameters, name);
2008 return JB_ERR_CGI_PARAMS;
2011 /* We don't use atoi because I want to check this carefully... */
2014 while ((ch = *param++) != '\0')
2016 if ((ch < '0') || (ch > '9'))
2018 return JB_ERR_CGI_PARAMS;
2025 * <limits.h> defines UINT_MAX
2027 * (UINT_MAX - ch) / 10 is the largest number that
2028 * can be safely multiplied by 10 then have ch added.
2030 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
2032 return JB_ERR_CGI_PARAMS;
2035 value = value * 10 + ch;
2046 /*********************************************************************
2048 * Function : get_url_spec_param
2050 * Description : Get a URL pattern from the parameters
2051 * passed to a CGI function. Removes leading/trailing
2052 * spaces and validates it.
2055 * 1 : csp = Current client state (buffers, headers, etc...)
2056 * 2 : parameters = map of cgi parameters
2057 * 3 : name = Name of CGI parameter to read
2058 * 4 : pvalue = destination for value. Will be malloc()'d.
2059 * Set to NULL on error.
2061 * Returns : JB_ERR_OK on success
2062 * JB_ERR_MEMORY on out-of-memory
2063 * JB_ERR_CGI_PARAMS if the parameter was not specified
2066 *********************************************************************/
2067 static jb_err get_url_spec_param(struct client_state *csp,
2068 const struct map *parameters,
2072 const char *orig_param;
2075 struct url_spec compiled[1];
2085 orig_param = lookup(parameters, name);
2088 return JB_ERR_CGI_PARAMS;
2091 /* Copy and trim whitespace */
2092 param = strdup(orig_param);
2095 return JB_ERR_MEMORY;
2099 /* Must be non-empty, and can't allow 1st character to be '{' */
2100 if (param[0] == '\0' || param[0] == '{')
2103 return JB_ERR_CGI_PARAMS;
2106 /* Check for embedded newlines */
2107 for (s = param; *s != '\0'; s++)
2109 if ((*s == '\r') || (*s == '\n'))
2112 return JB_ERR_CGI_PARAMS;
2116 /* Check that regex is valid */
2121 return JB_ERR_MEMORY;
2123 err = create_url_spec(compiled, s);
2128 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2130 free_url_spec(compiled);
2132 if (param[strlen(param) - 1] == '\\')
2135 * Must protect trailing '\\' from becoming line continuation character.
2136 * Two methods: 1) If it's a domain only, add a trailing '/'.
2137 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2139 if (strchr(param, '/') == NULL)
2141 err = string_append(¶m, "/");
2145 err = string_append(¶m, "(?:)");
2152 /* Check that the modified regex is valid */
2157 return JB_ERR_MEMORY;
2159 err = create_url_spec(compiled, s);
2164 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2166 free_url_spec(compiled);
2173 /*********************************************************************
2175 * Function : map_radio
2177 * Description : Map a set of radio button values. E.g. if you have
2178 * 3 radio buttons, declare them as:
2179 * <option type="radio" name="xyz" @xyz-a@>
2180 * <option type="radio" name="xyz" @xyz-b@>
2181 * <option type="radio" name="xyz" @xyz-c@>
2182 * Then map one of the @xyz-?@ variables to "checked"
2183 * and all the others to empty by calling:
2184 * map_radio(exports, "xyz", "abc", sel)
2185 * Where 'sel' is 'a', 'b', or 'c'.
2188 * 1 : exports = Exports map to modify.
2189 * 2 : optionname = name for map
2190 * 3 : values = null-terminated list of values;
2191 * 4 : value = Selected value.
2193 * CGI Parameters : None
2195 * Returns : JB_ERR_OK on success
2196 * JB_ERR_MEMORY on out-of-memory
2198 *********************************************************************/
2199 static jb_err map_radio(struct map * exports,
2200 const char * optionname,
2201 const char * values,
2213 len = strlen(optionname);
2214 buf = malloc(len + 3);
2217 return JB_ERR_MEMORY;
2220 strcpy(buf, optionname);
2225 while ((c = *values++) != '\0')
2230 if (map(exports, buf, 1, "", 1))
2232 return JB_ERR_MEMORY;
2238 return map(exports, buf, 0, "checked", 1);
2242 /*********************************************************************
2244 * Function : cgi_error_modified
2246 * Description : CGI function that is called when a file is modified
2247 * outside the CGI editor.
2250 * 1 : csp = Current client state (buffers, headers, etc...)
2251 * 2 : rsp = http_response data structure for output
2252 * 3 : filename = The file that was modified.
2254 * CGI Parameters : none
2256 * Returns : JB_ERR_OK on success
2257 * JB_ERR_MEMORY on out-of-memory error.
2259 *********************************************************************/
2260 jb_err cgi_error_modified(struct client_state *csp,
2261 struct http_response *rsp,
2262 const char *filename)
2264 struct map *exports;
2271 if (NULL == (exports = default_exports(csp, NULL)))
2273 return JB_ERR_MEMORY;
2276 err = map(exports, "f", 1, html_encode(filename), 0);
2283 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2287 /*********************************************************************
2289 * Function : cgi_error_parse
2291 * Description : CGI function that is called when a file cannot
2292 * be parsed by the CGI editor.
2295 * 1 : csp = Current client state (buffers, headers, etc...)
2296 * 2 : rsp = http_response data structure for output
2297 * 3 : file = The file that was modified.
2299 * CGI Parameters : none
2301 * Returns : JB_ERR_OK on success
2302 * JB_ERR_MEMORY on out-of-memory error.
2304 *********************************************************************/
2305 jb_err cgi_error_parse(struct client_state *csp,
2306 struct http_response *rsp,
2307 struct editable_file *file)
2309 struct map *exports;
2311 struct file_line *cur_line;
2317 if (NULL == (exports = default_exports(csp, NULL)))
2319 return JB_ERR_MEMORY;
2322 err = map(exports, "f", 1, file->identifier, 1);
2323 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2325 cur_line = file->parse_error;
2328 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2329 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2337 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2341 /*********************************************************************
2343 * Function : cgi_error_file
2345 * Description : CGI function that is called when a file cannot be
2346 * opened by the CGI editor.
2349 * 1 : csp = Current client state (buffers, headers, etc...)
2350 * 2 : rsp = http_response data structure for output
2351 * 3 : filename = The file that was modified.
2353 * CGI Parameters : none
2355 * Returns : JB_ERR_OK on success
2356 * JB_ERR_MEMORY on out-of-memory error.
2358 *********************************************************************/
2359 jb_err cgi_error_file(struct client_state *csp,
2360 struct http_response *rsp,
2361 const char *filename)
2363 struct map *exports;
2370 if (NULL == (exports = default_exports(csp, NULL)))
2372 return JB_ERR_MEMORY;
2375 err = map(exports, "f", 1, html_encode(filename), 0);
2382 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2386 /*********************************************************************
2388 * Function : cgi_error_bad_param
2390 * Description : CGI function that is called if the parameters
2391 * (query string) for a CGI were wrong.
2394 * 1 : csp = Current client state (buffers, headers, etc...)
2395 * 2 : rsp = http_response data structure for output
2397 * CGI Parameters : none
2399 * Returns : JB_ERR_OK on success
2400 * JB_ERR_MEMORY on out-of-memory error.
2402 *********************************************************************/
2403 jb_err cgi_error_disabled(struct client_state *csp,
2404 struct http_response *rsp)
2406 struct map *exports;
2411 if (NULL == (exports = default_exports(csp, NULL)))
2413 return JB_ERR_MEMORY;
2416 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2420 /*********************************************************************
2422 * Function : cgi_edit_actions
2424 * Description : CGI function that allows the user to choose which
2425 * actions file to edit.
2428 * 1 : csp = Current client state (buffers, headers, etc...)
2429 * 2 : rsp = http_response data structure for output
2430 * 3 : parameters = map of cgi parameters
2432 * CGI Parameters : None
2434 * Returns : JB_ERR_OK on success
2435 * JB_ERR_MEMORY on out-of-memory error
2437 *********************************************************************/
2438 jb_err cgi_edit_actions(struct client_state *csp,
2439 struct http_response *rsp,
2440 const struct map *parameters)
2443 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2445 return cgi_error_disabled(csp, rsp);
2448 /* FIXME: Incomplete */
2449 rsp->status = strdup("302 Local Redirect from Junkbuster");
2450 if (rsp->status == NULL)
2452 return JB_ERR_MEMORY;
2454 if (enlist_unique_header(rsp->headers, "Location",
2455 CGI_PREFIX "edit-actions-list?f=ijb"))
2459 return JB_ERR_MEMORY;
2466 /*********************************************************************
2468 * Function : cgi_edit_actions_list
2470 * Description : CGI function that edits the actions list.
2471 * FIXME: This function shouldn't FATAL ever.
2472 * FIXME: This function doesn't check the retval of map()
2474 * 1 : csp = Current client state (buffers, headers, etc...)
2475 * 2 : rsp = http_response data structure for output
2476 * 3 : parameters = map of cgi parameters
2478 * CGI Parameters : filename
2480 * Returns : JB_ERR_OK on success
2481 * JB_ERR_MEMORY on out-of-memory
2482 * JB_ERR_FILE if the file cannot be opened or
2484 * JB_ERR_CGI_PARAMS if "filename" was not specified
2487 *********************************************************************/
2488 jb_err cgi_edit_actions_list(struct client_state *csp,
2489 struct http_response *rsp,
2490 const struct map *parameters)
2492 char * section_template;
2493 char * url_template;
2498 struct map * exports;
2499 struct map * section_exports;
2500 struct map * url_exports;
2501 struct editable_file * file;
2502 struct file_line * cur_line;
2503 unsigned line_number = 0;
2504 unsigned prev_section_line_number = ((unsigned) (-1));
2508 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2510 return cgi_error_disabled(csp, rsp);
2513 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2516 /* No filename specified, can't read file, or out of memory. */
2517 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2520 if (NULL == (exports = default_exports(csp, NULL)))
2522 edit_free_file(file);
2523 return JB_ERR_MEMORY;
2526 err = map(exports, "f", 1, file->identifier, 1);
2527 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2531 edit_free_file(file);
2536 /* Should do all global exports above this point */
2538 err = template_load(csp, §ion_template, "edit-actions-list-section");
2541 edit_free_file(file);
2543 if (err == JB_ERR_FILE)
2545 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2550 err = template_load(csp, &url_template, "edit-actions-list-url");
2553 free(section_template);
2554 edit_free_file(file);
2556 if (err == JB_ERR_FILE)
2558 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2563 err = template_fill(§ion_template, exports);
2567 edit_free_file(file);
2573 err = template_fill(&url_template, exports);
2576 free(section_template);
2577 edit_free_file(file);
2582 /* Find start of actions in file */
2583 cur_line = file->lines;
2585 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2587 cur_line = cur_line->next;
2591 if (NULL == (sections = strdup("")))
2593 free(section_template);
2595 edit_free_file(file);
2597 return JB_ERR_MEMORY;
2600 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2602 if (NULL == (section_exports = new_map()))
2605 free(section_template);
2607 edit_free_file(file);
2609 return JB_ERR_MEMORY;
2612 snprintf(buf, 50, "%d", line_number);
2613 err = map(section_exports, "s", 1, buf, 1);
2614 if (!err) err = map(section_exports, "actions", 1,
2615 actions_to_html(cur_line->data.action), 0);
2618 && (cur_line->next != NULL)
2619 && (cur_line->next->type == FILE_LINE_URL))
2621 /* This section contains at least one URL, don't allow delete */
2622 err = map_block_killer(section_exports, "empty-section");
2626 if (!err) err = map_block_keep(section_exports, "empty-section");
2629 if (prev_section_line_number != ((unsigned)(-1)))
2631 /* Not last section */
2632 snprintf(buf, 50, "%d", prev_section_line_number);
2633 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2634 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2639 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2641 prev_section_line_number = line_number;
2646 free(section_template);
2648 edit_free_file(file);
2650 free_map(section_exports);
2654 /* Should do all section-specific exports above this point */
2656 if (NULL == (urls = strdup("")))
2659 free(section_template);
2661 edit_free_file(file);
2663 free_map(section_exports);
2664 return JB_ERR_MEMORY;
2669 cur_line = cur_line->next;
2672 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2674 if (NULL == (url_exports = new_map()))
2678 free(section_template);
2680 edit_free_file(file);
2682 free_map(section_exports);
2683 return JB_ERR_MEMORY;
2686 snprintf(buf, 50, "%d", line_number);
2687 err = map(url_exports, "p", 1, buf, 1);
2689 snprintf(buf, 50, "%d", url_1_2);
2690 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2692 if (!err) err = map(url_exports, "url-html", 1,
2693 html_encode(cur_line->unprocessed), 0);
2694 if (!err) err = map(url_exports, "url", 1,
2695 url_encode(cur_line->unprocessed), 0);
2701 free(section_template);
2703 edit_free_file(file);
2705 free_map(section_exports);
2706 free_map(url_exports);
2710 if (NULL == (s = strdup(url_template)))
2714 free(section_template);
2716 edit_free_file(file);
2718 free_map(section_exports);
2719 free_map(url_exports);
2720 return JB_ERR_MEMORY;
2723 err = template_fill(&s, section_exports);
2724 if (!err) err = template_fill(&s, url_exports);
2725 if (!err) err = string_append(&urls, s);
2727 free_map(url_exports);
2734 free(section_template);
2736 edit_free_file(file);
2738 free_map(section_exports);
2742 url_1_2 = 3 - url_1_2;
2744 cur_line = cur_line->next;
2748 err = map(section_exports, "urls", 1, urls, 0);
2750 /* Could also do section-specific exports here, but it wouldn't be as fast */
2752 if ( (cur_line != NULL)
2753 && (cur_line->type == FILE_LINE_ACTION))
2755 /* Not last section */
2756 snprintf(buf, 50, "%d", line_number);
2757 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2758 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2763 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2769 free(section_template);
2771 edit_free_file(file);
2773 free_map(section_exports);
2777 if (NULL == (s = strdup(section_template)))
2780 free(section_template);
2782 edit_free_file(file);
2784 free_map(section_exports);
2785 return JB_ERR_MEMORY;
2788 err = template_fill(&s, section_exports);
2789 if (!err) err = string_append(§ions, s);
2792 free_map(section_exports);
2797 free(section_template);
2799 edit_free_file(file);
2805 edit_free_file(file);
2806 free(section_template);
2809 err = map(exports, "sections", 1, sections, 0);
2816 /* Could also do global exports here, but it wouldn't be as fast */
2818 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2822 /*********************************************************************
2824 * Function : cgi_edit_actions
2826 * Description : CGI function that edits the Actions list.
2829 * 1 : csp = Current client state (buffers, headers, etc...)
2830 * 2 : rsp = http_response data structure for output
2831 * 3 : parameters = map of cgi parameters
2833 * CGI Parameters : None
2835 * Returns : JB_ERR_OK on success
2836 * JB_ERR_MEMORY on out-of-memory
2837 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2838 * specified or not valid.
2840 *********************************************************************/
2841 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2842 struct http_response *rsp,
2843 const struct map *parameters)
2845 struct map * exports;
2847 struct editable_file * file;
2848 struct file_line * cur_line;
2849 unsigned line_number;
2851 struct file_list *filter_file;
2852 struct re_filterfile_spec *filter_group;
2854 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2856 return cgi_error_disabled(csp, rsp);
2859 err = get_number_param(csp, parameters, "s", §ionid);
2865 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2868 /* No filename specified, can't read file, modified, or out of memory. */
2869 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2872 cur_line = file->lines;
2874 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2876 cur_line = cur_line->next;
2879 if ( (cur_line == NULL)
2880 || (line_number != sectionid)
2882 || (cur_line->type != FILE_LINE_ACTION))
2884 /* Invalid "sectionid" parameter */
2885 edit_free_file(file);
2886 return JB_ERR_CGI_PARAMS;
2889 if (NULL == (exports = default_exports(csp, NULL)))
2891 edit_free_file(file);
2892 return JB_ERR_MEMORY;
2895 err = map(exports, "f", 1, file->identifier, 1);
2896 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2897 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2899 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2901 filter_file = csp->rlist;
2902 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2904 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2908 edit_free_file(file);
2913 if (filter_group == NULL)
2915 err = map(exports, "filter-params", 1, "", 1);
2919 /* We have some entries in the filter list */
2922 char * filter_template;
2924 err = template_load(csp, &filter_template, "edit-actions-for-url-filter");
2927 edit_free_file(file);
2929 if (err == JB_ERR_FILE)
2931 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2936 result = strdup("");
2938 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2940 char current_mode = 'x';
2941 struct list_entry *filter_name;
2943 struct map *line_exports;
2946 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2947 while ((filter_name != NULL)
2948 && (0 != strcmp(filter_group->name, filter_name->str)))
2950 filter_name = filter_name->next;
2953 if (filter_name != NULL)
2959 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2960 while ((filter_name != NULL)
2961 && (0 != strcmp(filter_group->name, filter_name->str)))
2963 filter_name = filter_name->next;
2965 if (filter_name != NULL)
2971 /* Generate a unique serial number */
2972 snprintf(number, sizeof(number), "%x", index++);
2973 number[sizeof(number) - 1] = '\0';
2975 line_exports = new_map();
2976 if (line_exports == NULL)
2978 err = JB_ERR_MEMORY;
2983 if (!err) err = map(line_exports, "index", 1, number, 1);
2984 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2985 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2986 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2991 this_line = strdup(filter_template);
2992 if (this_line == NULL) err = JB_ERR_MEMORY;
2994 if (!err) err = template_fill(&this_line, line_exports);
2995 string_join(&result, this_line);
2997 free_map(line_exports);
3002 err = map(exports, "filter-params", 1, result, 0);
3010 if (!err) err = map_radio(exports, "filter-all", "nx",
3011 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3013 edit_free_file(file);
3021 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3025 /*********************************************************************
3027 * Function : cgi_edit_actions_submit
3029 * Description : CGI function that actually edits the Actions list.
3032 * 1 : csp = Current client state (buffers, headers, etc...)
3033 * 2 : rsp = http_response data structure for output
3034 * 3 : parameters = map of cgi parameters
3036 * CGI Parameters : None
3038 * Returns : JB_ERR_OK on success
3039 * JB_ERR_MEMORY on out-of-memory
3040 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3041 * specified or not valid.
3043 *********************************************************************/
3044 jb_err cgi_edit_actions_submit(struct client_state *csp,
3045 struct http_response *rsp,
3046 const struct map *parameters)
3052 struct editable_file * file;
3053 struct file_line * cur_line;
3054 unsigned line_number;
3060 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3062 return cgi_error_disabled(csp, rsp);
3065 err = get_number_param(csp, parameters, "s", §ionid);
3071 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3074 /* No filename specified, can't read file, modified, or out of memory. */
3075 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3078 cur_line = file->lines;
3080 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3082 cur_line = cur_line->next;
3085 if ( (cur_line == NULL)
3086 || (line_number != sectionid)
3088 || (cur_line->type != FILE_LINE_ACTION))
3090 /* Invalid "sectionid" parameter */
3091 edit_free_file(file);
3092 return JB_ERR_CGI_PARAMS;
3095 err = actions_from_radio(parameters, cur_line->data.action);
3099 edit_free_file(file);
3103 ch = get_char_param(parameters, "filter_all");
3106 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3107 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3108 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3112 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3115 for (index = 0; !err; index++)
3122 /* Generate the keys */
3123 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3124 key_value[sizeof(key_value) - 1] = '\0';
3125 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3126 key_name[sizeof(key_name) - 1] = '\0';
3128 err = get_string_param(parameters, key_name, &name);
3138 value = get_char_param(parameters, key_value);
3141 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3142 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3143 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3145 else if (value == 'N')
3147 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3148 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3150 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3151 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3154 else if (value == 'X')
3156 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3157 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3164 edit_free_file(file);
3168 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3171 edit_free_file(file);
3172 return JB_ERR_MEMORY;
3175 len = strlen(actiontext);
3179 * Empty action - must special-case this.
3180 * Simply setting len to 1 is sufficient...
3185 if (NULL == (newtext = malloc(len + 2)))
3189 edit_free_file(file);
3190 return JB_ERR_MEMORY;
3192 strcpy(newtext, actiontext);
3196 newtext[len + 1] = '\0';
3198 freez(cur_line->raw);
3199 freez(cur_line->unprocessed);
3200 cur_line->unprocessed = newtext;
3202 err = edit_write_file(file);
3205 /* Error writing file */
3206 edit_free_file(file);
3210 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3211 string_append(&target, file->identifier);
3213 edit_free_file(file);
3218 return JB_ERR_MEMORY;
3221 rsp->status = strdup("302 Local Redirect from Junkbuster");
3222 if (rsp->status == NULL)
3225 return JB_ERR_MEMORY;
3227 err = enlist_unique_header(rsp->headers, "Location", target);
3234 /*********************************************************************
3236 * Function : cgi_edit_actions_url
3238 * Description : CGI function that actually edits a URL pattern in
3242 * 1 : csp = Current client state (buffers, headers, etc...)
3243 * 2 : rsp = http_response data structure for output
3244 * 3 : parameters = map of cgi parameters
3247 * filename : Identifies the file to edit
3248 * ver : File's last-modified time
3249 * section : Line number of section to edit
3250 * pattern : Line number of pattern to edit
3251 * newval : New value for pattern
3253 * Returns : JB_ERR_OK on success
3254 * JB_ERR_MEMORY on out-of-memory
3255 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3256 * specified or not valid.
3258 *********************************************************************/
3259 jb_err cgi_edit_actions_url(struct client_state *csp,
3260 struct http_response *rsp,
3261 const struct map *parameters)
3265 struct editable_file * file;
3266 struct file_line * cur_line;
3267 unsigned line_number;
3271 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3273 return cgi_error_disabled(csp, rsp);
3276 err = get_number_param(csp, parameters, "p", &patternid);
3283 return JB_ERR_CGI_PARAMS;
3286 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3292 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3295 /* No filename specified, can't read file, modified, or out of memory. */
3297 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3301 cur_line = file->lines;
3303 while ((cur_line != NULL) && (line_number < patternid))
3305 cur_line = cur_line->next;
3309 if ( (cur_line == NULL)
3310 || (cur_line->type != FILE_LINE_URL))
3312 /* Invalid "patternid" parameter */
3314 edit_free_file(file);
3315 return JB_ERR_CGI_PARAMS;
3318 /* At this point, the line to edit is in cur_line */
3320 freez(cur_line->raw);
3321 freez(cur_line->unprocessed);
3322 cur_line->unprocessed = new_pattern;
3324 err = edit_write_file(file);
3327 /* Error writing file */
3328 edit_free_file(file);
3332 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3333 string_append(&target, file->identifier);
3335 edit_free_file(file);
3340 return JB_ERR_MEMORY;
3343 rsp->status = strdup("302 Local Redirect from Junkbuster");
3344 if (rsp->status == NULL)
3347 return JB_ERR_MEMORY;
3349 err = enlist_unique_header(rsp->headers, "Location", target);
3356 /*********************************************************************
3358 * Function : cgi_edit_actions_add_url
3360 * Description : CGI function that actually adds a URL pattern to
3364 * 1 : csp = Current client state (buffers, headers, etc...)
3365 * 2 : rsp = http_response data structure for output
3366 * 3 : parameters = map of cgi parameters
3369 * filename : Identifies the file to edit
3370 * ver : File's last-modified time
3371 * section : Line number of section to edit
3372 * newval : New pattern
3374 * Returns : JB_ERR_OK on success
3375 * JB_ERR_MEMORY on out-of-memory
3376 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3377 * specified or not valid.
3379 *********************************************************************/
3380 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3381 struct http_response *rsp,
3382 const struct map *parameters)
3386 struct file_line * new_line;
3387 struct editable_file * file;
3388 struct file_line * cur_line;
3389 unsigned line_number;
3393 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3395 return cgi_error_disabled(csp, rsp);
3398 err = get_number_param(csp, parameters, "s", §ionid);
3405 return JB_ERR_CGI_PARAMS;
3408 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3414 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3417 /* No filename specified, can't read file, modified, or out of memory. */
3419 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3423 cur_line = file->lines;
3425 while ((cur_line != NULL) && (line_number < sectionid))
3427 cur_line = cur_line->next;
3431 if ( (cur_line == NULL)
3432 || (cur_line->type != FILE_LINE_ACTION))
3434 /* Invalid "sectionid" parameter */
3436 edit_free_file(file);
3437 return JB_ERR_CGI_PARAMS;
3440 /* At this point, the section header is in cur_line - add after this. */
3442 /* Allocate the new line */
3443 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3444 if (new_line == NULL)
3447 edit_free_file(file);
3448 return JB_ERR_MEMORY;
3451 /* Fill in the data members of the new line */
3452 new_line->raw = NULL;
3453 new_line->prefix = NULL;
3454 new_line->unprocessed = new_pattern;
3455 new_line->type = FILE_LINE_URL;
3457 /* Link new_line into the list, after cur_line */
3458 new_line->next = cur_line->next;
3459 cur_line->next = new_line;
3461 /* Done making changes, now commit */
3463 err = edit_write_file(file);
3466 /* Error writing file */
3467 edit_free_file(file);
3471 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3472 string_append(&target, file->identifier);
3474 edit_free_file(file);
3479 return JB_ERR_MEMORY;
3482 rsp->status = strdup("302 Local Redirect from Junkbuster");
3483 if (rsp->status == NULL)
3486 return JB_ERR_MEMORY;
3488 err = enlist_unique_header(rsp->headers, "Location", target);
3495 /*********************************************************************
3497 * Function : cgi_edit_actions_remove_url
3499 * Description : CGI function that actually removes a URL pattern from
3503 * 1 : csp = Current client state (buffers, headers, etc...)
3504 * 2 : rsp = http_response data structure for output
3505 * 3 : parameters = map of cgi parameters
3508 * f : (filename) Identifies the file to edit
3509 * v : (version) File's last-modified time
3510 * p : (pattern) Line number of pattern to remove
3512 * Returns : JB_ERR_OK on success
3513 * JB_ERR_MEMORY on out-of-memory
3514 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3515 * specified or not valid.
3517 *********************************************************************/
3518 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3519 struct http_response *rsp,
3520 const struct map *parameters)
3523 struct editable_file * file;
3524 struct file_line * cur_line;
3525 struct file_line * prev_line;
3526 unsigned line_number;
3530 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3532 return cgi_error_disabled(csp, rsp);
3535 err = get_number_param(csp, parameters, "p", &patternid);
3541 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3544 /* No filename specified, can't read file, modified, or out of memory. */
3545 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3550 cur_line = file->lines;
3552 while ((cur_line != NULL) && (line_number < patternid))
3554 prev_line = cur_line;
3555 cur_line = cur_line->next;
3559 if ( (cur_line == NULL)
3560 || (prev_line == NULL)
3561 || (cur_line->type != FILE_LINE_URL))
3563 /* Invalid "patternid" parameter */
3564 edit_free_file(file);
3565 return JB_ERR_CGI_PARAMS;
3568 /* At this point, the line to remove is in cur_line, and the previous
3569 * one is in prev_line
3572 /* Unlink cur_line */
3573 prev_line->next = cur_line->next;
3574 cur_line->next = NULL;
3577 edit_free_file_lines(cur_line);
3579 err = edit_write_file(file);
3582 /* Error writing file */
3583 edit_free_file(file);
3587 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3588 string_append(&target, file->identifier);
3590 edit_free_file(file);
3595 return JB_ERR_MEMORY;
3598 rsp->status = strdup("302 Local Redirect from Junkbuster");
3599 if (rsp->status == NULL)
3602 return JB_ERR_MEMORY;
3604 err = enlist_unique_header(rsp->headers, "Location", target);
3611 /*********************************************************************
3613 * Function : cgi_edit_actions_section_remove
3615 * Description : CGI function that actually removes a whole section from
3616 * the actions file. The section must be empty first
3617 * (else JB_ERR_CGI_PARAMS).
3620 * 1 : csp = Current client state (buffers, headers, etc...)
3621 * 2 : rsp = http_response data structure for output
3622 * 3 : parameters = map of cgi parameters
3625 * f : (filename) Identifies the file to edit
3626 * v : (version) File's last-modified time
3627 * s : (section) Line number of section to edit
3629 * Returns : JB_ERR_OK on success
3630 * JB_ERR_MEMORY on out-of-memory
3631 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3632 * specified or not valid.
3634 *********************************************************************/
3635 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3636 struct http_response *rsp,
3637 const struct map *parameters)
3640 struct editable_file * file;
3641 struct file_line * cur_line;
3642 struct file_line * prev_line;
3643 unsigned line_number;
3647 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3649 return cgi_error_disabled(csp, rsp);
3652 err = get_number_param(csp, parameters, "s", §ionid);
3658 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3661 /* No filename specified, can't read file, modified, or out of memory. */
3662 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3666 cur_line = file->lines;
3669 while ((cur_line != NULL) && (line_number < sectionid))
3671 prev_line = cur_line;
3672 cur_line = cur_line->next;
3676 if ( (cur_line == NULL)
3677 || (cur_line->type != FILE_LINE_ACTION) )
3679 /* Invalid "sectionid" parameter */
3680 edit_free_file(file);
3681 return JB_ERR_CGI_PARAMS;
3684 if ( (cur_line->next != NULL)
3685 && (cur_line->next->type == FILE_LINE_URL) )
3687 /* Section not empty. */
3688 edit_free_file(file);
3689 return JB_ERR_CGI_PARAMS;
3692 /* At this point, the line to remove is in cur_line, and the previous
3693 * one is in prev_line
3696 /* Unlink cur_line */
3697 if (prev_line == NULL)
3699 /* Removing the first line from the file */
3700 file->lines = cur_line->next;
3704 prev_line->next = cur_line->next;
3706 cur_line->next = NULL;
3709 edit_free_file_lines(cur_line);
3711 err = edit_write_file(file);
3714 /* Error writing file */
3715 edit_free_file(file);
3719 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3720 string_append(&target, file->identifier);
3722 edit_free_file(file);
3727 return JB_ERR_MEMORY;
3730 rsp->status = strdup("302 Local Redirect from Junkbuster");
3731 if (rsp->status == NULL)
3734 return JB_ERR_MEMORY;
3736 err = enlist_unique_header(rsp->headers, "Location", target);
3743 /*********************************************************************
3745 * Function : cgi_edit_actions_section_add
3747 * Description : CGI function that adds a new empty section to
3751 * 1 : csp = Current client state (buffers, headers, etc...)
3752 * 2 : rsp = http_response data structure for output
3753 * 3 : parameters = map of cgi parameters
3756 * f : (filename) Identifies the file to edit
3757 * v : (version) File's last-modified time
3758 * s : (section) Line number of section to add after, 0 for
3761 * Returns : JB_ERR_OK on success
3762 * JB_ERR_MEMORY on out-of-memory
3763 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3764 * specified or not valid.
3766 *********************************************************************/
3767 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3768 struct http_response *rsp,
3769 const struct map *parameters)
3772 struct file_line * new_line;
3774 struct editable_file * file;
3775 struct file_line * cur_line;
3776 unsigned line_number;
3780 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3782 return cgi_error_disabled(csp, rsp);
3785 err = get_number_param(csp, parameters, "s", §ionid);
3791 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3794 /* No filename specified, can't read file, modified, or out of memory. */
3795 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3799 cur_line = file->lines;
3803 /* Add to start of file */
3804 if (cur_line != NULL)
3806 /* There's something in the file, find the line before the first
3809 while ( (cur_line->next != NULL)
3810 && (cur_line->next->type != FILE_LINE_ACTION) )
3812 cur_line = cur_line->next;
3819 /* Add after stated section. */
3820 while ((cur_line != NULL) && (line_number < sectionid))
3822 cur_line = cur_line->next;
3826 if ( (cur_line == NULL)
3827 || (cur_line->type != FILE_LINE_ACTION))
3829 /* Invalid "sectionid" parameter */
3830 edit_free_file(file);
3831 return JB_ERR_CGI_PARAMS;
3834 /* Skip through the section to find the last line in it. */
3835 while ( (cur_line->next != NULL)
3836 && (cur_line->next->type != FILE_LINE_ACTION) )
3838 cur_line = cur_line->next;
3843 /* At this point, the last line in the previous section is in cur_line
3844 * - add after this. (Or if we need to add as the first line, cur_line
3848 new_text = strdup("{}");
3849 if (NULL == new_text)
3851 edit_free_file(file);
3852 return JB_ERR_MEMORY;
3855 /* Allocate the new line */
3856 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3857 if (new_line == NULL)
3860 edit_free_file(file);
3861 return JB_ERR_MEMORY;
3864 /* Fill in the data members of the new line */
3865 new_line->raw = NULL;
3866 new_line->prefix = NULL;
3867 new_line->unprocessed = new_text;
3868 new_line->type = FILE_LINE_ACTION;
3870 if (cur_line != NULL)
3872 /* Link new_line into the list, after cur_line */
3873 new_line->next = cur_line->next;
3874 cur_line->next = new_line;
3878 /* Link new_line into the list, as first line */
3879 new_line->next = file->lines;
3880 file->lines = new_line;
3883 /* Done making changes, now commit */
3885 err = edit_write_file(file);
3888 /* Error writing file */
3889 edit_free_file(file);
3893 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3894 string_append(&target, file->identifier);
3896 edit_free_file(file);
3901 return JB_ERR_MEMORY;
3904 rsp->status = strdup("302 Local Redirect from Junkbuster");
3905 if (rsp->status == NULL)
3908 return JB_ERR_MEMORY;
3910 err = enlist_unique_header(rsp->headers, "Location", target);
3917 /*********************************************************************
3919 * Function : cgi_edit_actions_section_swap
3921 * Description : CGI function that swaps the order of two sections
3922 * in the actions file. Note that this CGI can actually
3923 * swap any two arbitrary sections, but the GUI interface
3924 * currently only allows consecutive sections to be
3928 * 1 : csp = Current client state (buffers, headers, etc...)
3929 * 2 : rsp = http_response data structure for output
3930 * 3 : parameters = map of cgi parameters
3933 * f : (filename) Identifies the file to edit
3934 * v : (version) File's last-modified time
3935 * s1 : (section1) Line number of first section to swap
3936 * s2 : (section2) Line number of second section to swap
3938 * Returns : JB_ERR_OK on success
3939 * JB_ERR_MEMORY on out-of-memory
3940 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3941 * specified or not valid.
3943 *********************************************************************/
3944 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3945 struct http_response *rsp,
3946 const struct map *parameters)
3950 struct editable_file * file;
3951 struct file_line * cur_line;
3952 struct file_line * prev_line;
3953 struct file_line * line_before_section1;
3954 struct file_line * line_start_section1;
3955 struct file_line * line_end_section1;
3956 struct file_line * line_after_section1;
3957 struct file_line * line_before_section2;
3958 struct file_line * line_start_section2;
3959 struct file_line * line_end_section2;
3960 struct file_line * line_after_section2;
3961 unsigned line_number;
3965 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3967 return cgi_error_disabled(csp, rsp);
3970 err = get_number_param(csp, parameters, "s1", §ion1);
3971 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3977 if (section1 > section2)
3979 unsigned temp = section2;
3980 section2 = section1;
3984 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3987 /* No filename specified, can't read file, modified, or out of memory. */
3988 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3991 /* Start at the beginning... */
3993 cur_line = file->lines;
3996 /* ... find section1 ... */
3997 while ((cur_line != NULL) && (line_number < section1))
3999 prev_line = cur_line;
4000 cur_line = cur_line->next;
4004 if ( (cur_line == NULL)
4005 || (cur_line->type != FILE_LINE_ACTION) )
4007 /* Invalid "section1" parameter */
4008 edit_free_file(file);
4009 return JB_ERR_CGI_PARAMS;
4012 /* If no-op, we've validated params and can skip the rest. */
4013 if (section1 != section2)
4015 /* ... find the end of section1 ... */
4016 line_before_section1 = prev_line;
4017 line_start_section1 = cur_line;
4020 prev_line = cur_line;
4021 cur_line = cur_line->next;
4024 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4025 line_end_section1 = prev_line;
4026 line_after_section1 = cur_line;
4028 /* ... find section2 ... */
4029 while ((cur_line != NULL) && (line_number < section2))
4031 prev_line = cur_line;
4032 cur_line = cur_line->next;
4036 if ( (cur_line == NULL)
4037 || (cur_line->type != FILE_LINE_ACTION) )
4039 /* Invalid "section2" parameter */
4040 edit_free_file(file);
4041 return JB_ERR_CGI_PARAMS;
4044 /* ... find the end of section2 ... */
4045 line_before_section2 = prev_line;
4046 line_start_section2 = cur_line;
4049 prev_line = cur_line;
4050 cur_line = cur_line->next;
4053 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4054 line_end_section2 = prev_line;
4055 line_after_section2 = cur_line;
4057 /* Now have all the pointers we need. Do the swap. */
4059 /* Change the pointer to section1 to point to section2 instead */
4060 if (line_before_section1 == NULL)
4062 file->lines = line_start_section2;
4066 line_before_section1->next = line_start_section2;
4069 if (line_before_section2 == line_end_section1)
4071 /* Consecutive sections */
4072 line_end_section2->next = line_start_section1;
4076 line_end_section2->next = line_after_section1;
4077 line_before_section2->next = line_start_section1;
4080 /* Set the pointer from the end of section1 to the rest of the file */
4081 line_end_section1->next = line_after_section2;
4083 err = edit_write_file(file);
4086 /* Error writing file */
4087 edit_free_file(file);
4090 } /* END if (section1 != section2) */
4092 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4093 string_append(&target, file->identifier);
4095 edit_free_file(file);
4100 return JB_ERR_MEMORY;
4103 rsp->status = strdup("302 Local Redirect from Junkbuster");
4104 if (rsp->status == NULL)
4107 return JB_ERR_MEMORY;
4109 err = enlist_unique_header(rsp->headers, "Location", target);
4116 /*********************************************************************
4118 * Function : cgi_toggle
4120 * Description : CGI function that adds a new empty section to
4124 * 1 : csp = Current client state (buffers, headers, etc...)
4125 * 2 : rsp = http_response data structure for output
4126 * 3 : parameters = map of cgi parameters
4129 * set : If present, how to change toggle setting:
4130 * "enable", "disable", "toggle", or none (default).
4131 * mini : If present, use mini reply template.
4133 * Returns : JB_ERR_OK on success
4134 * JB_ERR_MEMORY on out-of-memory
4136 *********************************************************************/
4137 jb_err cgi_toggle(struct client_state *csp,
4138 struct http_response *rsp,
4139 const struct map *parameters)
4141 struct map *exports;
4143 const char *template_name;
4150 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4152 return cgi_error_disabled(csp, rsp);
4155 if (NULL == (exports = default_exports(csp, "toggle")))
4157 return JB_ERR_MEMORY;
4160 mode = get_char_param(parameters, "set");
4167 else if (mode == 'D')
4172 else if (mode == 'T')
4175 g_bToggleIJB = !g_bToggleIJB;
4178 err = map_conditional(exports, "enabled", g_bToggleIJB);
4185 template_name = (get_char_param(parameters, "mini")
4189 return template_fill_for_cgi(csp, template_name, exports, rsp);
4193 /*********************************************************************
4195 * Function : javascriptify
4197 * Description : Converts a string into a form JavaScript will like.
4199 * Netscape 4's JavaScript sucks - it doesn't use
4200 * "id" parameters, so you have to set the "name"
4201 * used to submit a form element to something JavaScript
4202 * will like. (Or access the elements by index in an
4203 * array. That array contains >60 elements and will
4204 * be changed whenever we add a new action to the
4205 * editor, so I'm NOT going to use indexes that have
4206 * to be figured out by hand.)
4208 * Currently the only thing we have to worry about
4209 * is "-" ==> "_" conversion.
4211 * This is a length-preserving operation so it is
4212 * carried out in-place, no memory is allocated
4216 * 1 : identifier = String to make JavaScript-friendly.
4220 *********************************************************************/
4221 static void javascriptify(char * identifier)
4223 char * p = identifier;
4224 while (NULL != (p = strchr(p, '-')))
4231 /*********************************************************************
4233 * Function : actions_to_radio
4235 * Description : Converts a actionsfile entry into settings for
4236 * radio buttons and edit boxes on a HTML form.
4239 * 1 : exports = List of substitutions to add to.
4240 * 2 : action = Action to read
4242 * Returns : JB_ERR_OK on success
4243 * JB_ERR_MEMORY on out-of-memory
4245 *********************************************************************/
4246 static jb_err actions_to_radio(struct map * exports,
4247 const struct action_spec *action)
4249 unsigned mask = action->mask;
4250 unsigned add = action->add;
4258 mask = action->mask;
4261 /* sanity - prevents "-feature +feature" */
4265 #define DEFINE_ACTION_BOOL(name, bit) \
4266 if (!(mask & bit)) \
4268 current_mode = 'n'; \
4270 else if (add & bit) \
4272 current_mode = 'y'; \
4276 current_mode = 'x'; \
4278 if (map_radio(exports, name, "ynx", current_mode)) \
4280 return JB_ERR_MEMORY; \
4283 #define DEFINE_ACTION_STRING(name, bit, index) \
4284 DEFINE_ACTION_BOOL(name, bit); \
4287 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4290 checked = !strcmp(action->string[index], value); \
4294 checked = is_default; \
4296 mapped_param |= checked; \
4297 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4299 return JB_ERR_MEMORY; \
4302 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4303 if (map(exports, name "-param-custom", 1, \
4304 ((!mapped_param) ? "checked" : ""), 1)) \
4306 return JB_ERR_MEMORY; \
4308 if (map(exports, name "-param", 1, \
4309 (((add & bit) && !mapped_param) ? \
4310 action->string[index] : default_val), 1)) \
4312 return JB_ERR_MEMORY; \
4315 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4316 if (map(exports, name "-param", 1, \
4317 ((add & bit) ? action->string[index] : default_val), 1)) \
4319 return JB_ERR_MEMORY; \
4322 #define DEFINE_ACTION_MULTI(name, index) \
4323 if (action->multi_add[index]->first) \
4325 current_mode = 'y'; \
4327 else if (action->multi_remove_all[index]) \
4329 current_mode = 'n'; \
4331 else if (action->multi_remove[index]->first) \
4333 current_mode = 'y'; \
4337 current_mode = 'x'; \
4339 if (map_radio(exports, name, "ynx", current_mode)) \
4341 return JB_ERR_MEMORY; \
4344 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4346 #include "actionlist.h"
4348 #undef DEFINE_ACTION_MULTI
4349 #undef DEFINE_ACTION_STRING
4350 #undef DEFINE_ACTION_BOOL
4351 #undef DEFINE_ACTION_ALIAS
4352 #undef DEFINE_CGI_PARAM_CUSTOM
4353 #undef DEFINE_CGI_PARAM_RADIO
4354 #undef DEFINE_CGI_PARAM_NO_RADIO
4360 /*********************************************************************
4362 * Function : actions_from_radio
4364 * Description : Converts a map of parameters passed to a CGI function
4365 * into an actionsfile entry.
4368 * 1 : parameters = parameters to the CGI call
4369 * 2 : action = Action to change. Must be valid before
4370 * the call, actions not specified will be
4373 * Returns : JB_ERR_OK on success
4374 * JB_ERR_MEMORY on out-of-memory
4376 *********************************************************************/
4377 static jb_err actions_from_radio(const struct map * parameters,
4378 struct action_spec *action)
4380 static int first_time = 1;
4384 const char * js_name;
4385 jb_err err = JB_ERR_OK;
4390 /* Statics are generally a potential race condition,
4391 * but in this case we're safe and don't need semaphores.
4392 * Be careful if you modify this function.
4396 #define JAVASCRIPTIFY(dest_var, string) \
4398 static char js_name_arr[] = string; \
4401 javascriptify(js_name_arr); \
4403 dest_var = js_name_arr; \
4406 #define DEFINE_ACTION_BOOL(name, bit) \
4407 JAVASCRIPTIFY(js_name, name); \
4408 ch = get_char_param(parameters, js_name); \
4411 action->add |= bit; \
4412 action->mask |= bit; \
4414 else if (ch == 'N') \
4416 action->add &= ~bit; \
4417 action->mask &= ~bit; \
4419 else if (ch == 'X') \
4421 action->add &= ~bit; \
4422 action->mask |= bit; \
4425 #define DEFINE_ACTION_STRING(name, bit, index) \
4426 JAVASCRIPTIFY(js_name, name); \
4427 ch = get_char_param(parameters, js_name); \
4431 JAVASCRIPTIFY(js_name, name "-mode"); \
4432 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4433 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4435 JAVASCRIPTIFY(js_name, name "-param"); \
4436 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4438 if (param != NULL) \
4440 if (NULL == (param_dup = strdup(param))) \
4442 return JB_ERR_MEMORY; \
4444 freez(action->string[index]); \
4445 action->add |= bit; \
4446 action->mask |= bit; \
4447 action->string[index] = param_dup; \
4450 else if (ch == 'N') \
4452 if (action->add & bit) \
4454 freez(action->string[index]); \
4456 action->add &= ~bit; \
4457 action->mask &= ~bit; \
4459 else if (ch == 'X') \
4461 if (action->add & bit) \
4463 freez(action->string[index]); \
4465 action->add &= ~bit; \
4466 action->mask |= bit; \
4469 #define DEFINE_ACTION_MULTI(name, index) \
4470 JAVASCRIPTIFY(js_name, name); \
4471 ch = get_char_param(parameters, js_name); \
4476 else if (ch == 'N') \
4478 list_remove_all(action->multi_add[index]); \
4479 list_remove_all(action->multi_remove[index]); \
4480 action->multi_remove_all[index] = 1; \
4482 else if (ch == 'X') \
4484 list_remove_all(action->multi_add[index]); \
4485 list_remove_all(action->multi_remove[index]); \
4486 action->multi_remove_all[index] = 0; \
4489 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4491 #include "actionlist.h"
4493 #undef DEFINE_ACTION_MULTI
4494 #undef DEFINE_ACTION_STRING
4495 #undef DEFINE_ACTION_BOOL
4496 #undef DEFINE_ACTION_ALIAS
4497 #undef JAVASCRIPTIFY
4505 #endif /* def FEATURE_CGI_EDIT_ACTIONS */