1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.20 2002/03/16 20:28:34 oes 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 * IJBSWA 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.20 2002/03/16 20:28:34 oes
46 * Added descriptions to the filters so users will know what they select in the cgi editor
48 * Revision 1.19 2002/03/16 18:38:14 jongfoster
49 * Stopping stupid or malicious users from breaking the actions
50 * file using the web-based editor.
52 * Revision 1.18 2002/03/16 14:57:44 jongfoster
53 * Full support for enabling/disabling modular filters.
55 * Revision 1.17 2002/03/16 14:26:42 jongfoster
56 * First version of modular filters support - READ ONLY!
57 * Fixing a double-free bug in the out-of-memory handling in map_radio().
59 * Revision 1.16 2002/03/07 03:46:17 oes
60 * Fixed compiler warnings
62 * Revision 1.15 2002/03/06 22:54:35 jongfoster
63 * Automated function-comment nitpicking.
65 * Revision 1.14 2002/03/05 00:24:51 jongfoster
66 * Patch to always edit the current actions file.
68 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
69 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
71 * Revision 1.12 2002/03/03 09:18:03 joergs
72 * Made jumbjuster work on AmigaOS again.
74 * Revision 1.11 2002/01/23 01:03:31 jongfoster
75 * Fixing gcc [CygWin] compiler warnings
77 * Revision 1.10 2002/01/23 00:22:59 jongfoster
78 * Adding new function cgi_edit_actions_section_swap(), to reorder
81 * Adding get_url_spec_param() to get a validated URL pattern.
83 * Moving edit_read_line() out of this file and into loaders.c.
85 * Adding missing html_encode() to many CGI functions.
87 * Moving the functions that #include actionlist.h to the end of the file,
88 * because the Visual C++ 97 debugger gets extremely confused if you try
89 * to debug any code that comes after them in the file.
91 * Major optimizations in cgi_edit_actions_list() to reduce the size of
92 * the generated HTML (down 40% from 550k to 304k), with major side-effects
93 * throughout the editor and templates. In particular, the length of the
94 * URLs throughout the editor has been drastically reduced, by cutting
95 * paramater names down to 1 character and CGI names down to 3-4
96 * characters, by removing all non-essential CGI paramaters even at the
97 * expense of having to re-read the actions file for the most trivial
98 * page, and by using relative rather than absolute URLs. This means
99 * that this (typical example):
101 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
102 * filename=ijb&ver=1011487572&section=12&pattern=13
103 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
107 * <a href="eau?f=ijb&v=1011487572&p=13">
109 * Revision 1.9 2002/01/17 20:56:22 jongfoster
110 * Replacing hard references to the URL of the config interface
111 * with #defines from project.h
113 * Revision 1.8 2001/11/30 23:35:51 jongfoster
114 * Renaming actionsfile to ijb.action
116 * Revision 1.7 2001/11/13 00:28:24 jongfoster
117 * - Renaming parameters from edit-actions-for-url so that they only
118 * contain legal JavaScript characters. If we wanted to write
119 * JavaScript that worked with Netscape 4, this is nessacery.
120 * (Note that at the moment the JavaScript doesn't actually work
121 * with Netscape 4, but now this is purely a template issue, not
122 * one affecting code).
123 * - Adding new CGIs for use by non-JavaScript browsers:
124 * edit-actions-url-form
125 * edit-actions-add-url-form
126 * edit-actions-remove-url-form
129 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
130 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
131 * by and __OS2__ ifdef.
133 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
134 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
135 * threads to call select() simultaneously. So, it's time to do a real, live,
136 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
137 * (native). Both versions will work, but using __OS2__ offers multi-threading.
139 * Revision 1.4 2001/10/23 21:48:19 jongfoster
140 * Cleaning up error handling in CGI functions - they now send back
141 * a HTML error page and should never cause a FATAL error. (Fixes one
142 * potential source of "denial of service" attacks).
144 * CGI actions file editor that works and is actually useful.
146 * Ability to toggle JunkBuster remotely using a CGI call.
148 * You can turn off both the above features in the main configuration
149 * file, e.g. if you are running a multi-user proxy.
151 * Revision 1.3 2001/10/14 22:12:49 jongfoster
152 * New version of CGI-based actionsfile editor.
153 * Major changes, including:
154 * - Completely new file parser and file output routines
155 * - edit-actions CGI renamed edit-actions-for-url
156 * - All CGIs now need a filename parameter, except for...
157 * - New CGI edit-actions which doesn't need a filename,
158 * to allow you to start the editor up.
159 * - edit-actions-submit now works, and now automatically
160 * redirects you back to the main edit-actions-list handler.
162 * Revision 1.2 2001/09/16 17:05:14 jongfoster
163 * Removing unused #include showarg.h
165 * Revision 1.1 2001/09/16 15:47:37 jongfoster
166 * First version of CGI-based edit interface. This is very much a
167 * work-in-progress, and you can't actually use it to edit anything
168 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
169 * to have any effect.
172 **********************************************************************/
178 * FIXME: Following includes copied from cgi.c - which are actually needed?
183 #include <sys/types.h>
188 #include <sys/stat.h>
191 #define snprintf _snprintf
192 #endif /* def _WIN32 */
197 #include "cgisimple.h"
201 #include "miscutil.h"
205 /* loadcfg.h is for g_bToggleIJB only */
206 #include "urlmatch.h"
208 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
211 #ifdef FEATURE_CGI_EDIT_ACTIONS
215 struct file_line * next;
223 struct action_spec action[1];
232 /* Add more data types here... e.g.
235 struct url_spec url[1];
239 struct action_spec action[1];
248 #define FILE_LINE_UNPROCESSED 1
249 #define FILE_LINE_BLANK 2
250 #define FILE_LINE_ALIAS_HEADER 3
251 #define FILE_LINE_ALIAS_ENTRY 4
252 #define FILE_LINE_ACTION 5
253 #define FILE_LINE_URL 6
254 #define FILE_LINE_SETTINGS_HEADER 7
255 #define FILE_LINE_SETTINGS_ENTRY 8
256 #define FILE_LINE_DESCRIPTION_HEADER 9
257 #define FILE_LINE_DESCRIPTION_ENTRY 10
262 struct file_line * lines;
263 const char * filename; /* Full pathname - e.g. "/etc/junkbuster/wibble.action" */
264 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
265 /* Pre-encoded with url_encode() for ease of use. */
266 const char * version_str; /* Last modification time, as a string. For CGI param */
267 /* Can be used in URL without using url_param(). */
268 unsigned version; /* Last modification time - prevents chaos with
269 * the browser's "back" button. Note that this is a
270 * time_t cast to an unsigned. When comparing, always
271 * cast the time_t to an unsigned, and *NOT* vice-versa.
272 * This may lose the top few bits, but they're not
273 * significant anyway.
275 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
276 * Note that changing this after the file has been
277 * read in will cause a mess.
279 struct file_line * parse_error; /* On parse error, this is the offending line. */
280 const char * parse_error_text; /* On parse error, this is the problem.
281 * (Statically allocated) */
284 #define CGI_ACTION_PARAM_LEN_MAX 500
286 /* FIXME: Following non-static functions should be prototyped in .h or made static */
288 /* Functions to read and write arbitrary config files */
289 jb_err edit_read_file(struct client_state *csp,
290 const struct map *parameters,
293 struct editable_file **pfile);
294 jb_err edit_write_file(struct editable_file * file);
295 void edit_free_file(struct editable_file * file);
297 /* Functions to read and write actions files */
298 jb_err edit_parse_actions_file(struct editable_file * file);
299 jb_err edit_read_actions_file(struct client_state *csp,
300 struct http_response *rsp,
301 const struct map *parameters,
303 struct editable_file **pfile);
306 jb_err cgi_error_modified(struct client_state *csp,
307 struct http_response *rsp,
308 const char *filename);
309 jb_err cgi_error_parse(struct client_state *csp,
310 struct http_response *rsp,
311 struct editable_file *file);
312 jb_err cgi_error_file(struct client_state *csp,
313 struct http_response *rsp,
314 const char *filename);
315 jb_err cgi_error_disabled(struct client_state *csp,
316 struct http_response *rsp);
318 /* Internal arbitrary config file support functions */
319 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
320 static void edit_free_file_lines(struct file_line * first_line);
322 /* Internal actions file support functions */
323 static int match_actions_file_header_line(const char * line, const char * name);
324 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
326 /* Internal parameter parsing functions */
327 static jb_err get_file_name_param(struct client_state *csp,
328 const struct map *parameters,
329 const char *param_name,
332 const char **pparam);
333 static jb_err get_number_param(struct client_state *csp,
334 const struct map *parameters,
337 static jb_err get_url_spec_param(struct client_state *csp,
338 const struct map *parameters,
341 static jb_err get_string_param(const struct map *parameters,
342 const char *param_name,
343 const char **pparam);
345 /* Internal actionsfile <==> HTML conversion functions */
346 static jb_err map_radio(struct map * exports,
347 const char * optionname,
350 static jb_err actions_to_radio(struct map * exports,
351 const struct action_spec *action);
352 static jb_err actions_from_radio(const struct map * parameters,
353 struct action_spec *action);
356 static jb_err map_copy_parameter_html(struct map *out,
357 const struct map *in,
359 #if 0 /* unused function */
360 static jb_err map_copy_parameter_url(struct map *out,
361 const struct map *in,
363 #endif /* unused function */
365 /*********************************************************************
367 * Function : map_copy_parameter_html
369 * Description : Copy a CGI parameter from one map to another, HTML
373 * 1 : out = target map
374 * 2 : in = source map
375 * 3 : name = name of cgi parameter to copy
377 * Returns : JB_ERR_OK on success
378 * JB_ERR_MEMORY on out-of-memory
379 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
382 *********************************************************************/
383 static jb_err map_copy_parameter_html(struct map *out,
384 const struct map *in,
394 value = lookup(in, name);
395 err = map(out, name, 1, html_encode(value), 0);
402 else if (*value == '\0')
404 return JB_ERR_CGI_PARAMS;
413 #if 0 /* unused function */
414 /*********************************************************************
416 * Function : map_copy_parameter_html
418 * Description : Copy a CGI parameter from one map to another, URL
422 * 1 : out = target map
423 * 2 : in = source map
424 * 3 : name = name of cgi parameter to copy
426 * Returns : JB_ERR_OK on success
427 * JB_ERR_MEMORY on out-of-memory
428 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
431 *********************************************************************/
432 static jb_err map_copy_parameter_url(struct map *out,
433 const struct map *in,
443 value = lookup(in, name);
444 err = map(out, name, 1, url_encode(value), 0);
451 else if (*value == '\0')
453 return JB_ERR_CGI_PARAMS;
460 #endif /* 0 - unused function */
462 /*********************************************************************
464 * Function : cgi_edit_actions_url_form
466 * Description : CGI function that displays a form for
470 * 1 : csp = Current client state (buffers, headers, etc...)
471 * 2 : rsp = http_response data structure for output
472 * 3 : parameters = map of cgi parameters
475 * f : (filename) Identifies the file to edit
476 * v : (version) File's last-modified time
477 * p : (pattern) Line number of pattern to edit
479 * Returns : JB_ERR_OK on success
480 * JB_ERR_MEMORY on out-of-memory
481 * JB_ERR_CGI_PARAMS if the CGI parameters are not
482 * specified or not valid.
484 *********************************************************************/
485 jb_err cgi_edit_actions_url_form(struct client_state *csp,
486 struct http_response *rsp,
487 const struct map *parameters)
489 struct map * exports;
491 struct editable_file * file;
492 struct file_line * cur_line;
493 unsigned line_number;
500 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
502 return cgi_error_disabled(csp, rsp);
505 err = get_number_param(csp, parameters, "p", &patternid);
511 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
514 /* No filename specified, can't read file, modified, or out of memory. */
515 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
518 cur_line = file->lines;
520 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
522 cur_line = cur_line->next;
525 if ( (cur_line == NULL)
526 || (line_number != patternid)
528 || (cur_line->type != FILE_LINE_URL))
530 /* Invalid "patternid" parameter */
531 edit_free_file(file);
532 return JB_ERR_CGI_PARAMS;
535 if (NULL == (exports = default_exports(csp, NULL)))
537 edit_free_file(file);
538 return JB_ERR_MEMORY;
541 err = map(exports, "f", 1, file->identifier, 1);
542 if (!err) err = map(exports, "v", 1, file->version_str, 1);
543 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
544 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
546 edit_free_file(file);
554 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
558 /*********************************************************************
560 * Function : cgi_edit_actions_add_url_form
562 * Description : CGI function that displays a form for
566 * 1 : csp = Current client state (buffers, headers, etc...)
567 * 2 : rsp = http_response data structure for output
568 * 3 : parameters = map of cgi parameters
571 * f : (filename) Identifies the file to edit
572 * v : (version) File's last-modified time
573 * s : (section) Line number of section to edit
575 * Returns : JB_ERR_OK on success
576 * JB_ERR_MEMORY on out-of-memory
577 * JB_ERR_CGI_PARAMS if the CGI parameters are not
578 * specified or not valid.
580 *********************************************************************/
581 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
582 struct http_response *rsp,
583 const struct map *parameters)
592 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
594 return cgi_error_disabled(csp, rsp);
597 if (NULL == (exports = default_exports(csp, NULL)))
599 return JB_ERR_MEMORY;
602 err = map_copy_parameter_html(exports, parameters, "f");
603 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
604 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
612 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
616 /*********************************************************************
618 * Function : cgi_edit_actions_remove_url_form
620 * Description : CGI function that displays a form for
624 * 1 : csp = Current client state (buffers, headers, etc...)
625 * 2 : rsp = http_response data structure for output
626 * 3 : parameters = map of cgi parameters
629 * f : (filename) Identifies the file to edit
630 * v : (version) File's last-modified time
631 * p : (pattern) Line number of pattern to edit
633 * Returns : JB_ERR_OK on success
634 * JB_ERR_MEMORY on out-of-memory
635 * JB_ERR_CGI_PARAMS if the CGI parameters are not
636 * specified or not valid.
638 *********************************************************************/
639 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
640 struct http_response *rsp,
641 const struct map *parameters)
643 struct map * exports;
645 struct editable_file * file;
646 struct file_line * cur_line;
647 unsigned line_number;
654 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
656 return cgi_error_disabled(csp, rsp);
659 err = get_number_param(csp, parameters, "p", &patternid);
665 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
668 /* No filename specified, can't read file, modified, or out of memory. */
669 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
672 cur_line = file->lines;
674 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
676 cur_line = cur_line->next;
679 if ( (cur_line == NULL)
680 || (line_number != patternid)
682 || (cur_line->type != FILE_LINE_URL))
684 /* Invalid "patternid" parameter */
685 edit_free_file(file);
686 return JB_ERR_CGI_PARAMS;
689 if (NULL == (exports = default_exports(csp, NULL)))
691 edit_free_file(file);
692 return JB_ERR_MEMORY;
695 err = map(exports, "f", 1, file->identifier, 1);
696 if (!err) err = map(exports, "v", 1, file->version_str, 1);
697 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
698 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
700 edit_free_file(file);
708 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
712 /*********************************************************************
714 * Function : edit_write_file
716 * Description : Write a complete file to disk.
719 * 1 : filename = File to write to.
720 * 2 : file = Data structure to write.
722 * Returns : JB_ERR_OK on success
723 * JB_ERR_FILE on error writing to file.
724 * JB_ERR_MEMORY on out of memory
726 *********************************************************************/
727 jb_err edit_write_file(struct editable_file * file)
730 struct file_line * cur_line;
731 struct stat statbuf[1];
732 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
733 digits in time_t, assuming this is a 64-bit
734 machine, plus null terminator, plus one
738 assert(file->filename);
740 #if defined(AMIGA) || defined(__OS2__)
741 if (NULL == (fp = fopen(file->filename, "w")))
743 if (NULL == (fp = fopen(file->filename, "wt")))
744 #endif /* def AMIGA */
749 cur_line = file->lines;
750 while (cur_line != NULL)
754 if (fputs(cur_line->raw, fp) < 0)
762 if (cur_line->prefix)
764 if (fputs(cur_line->prefix, fp) < 0)
770 if (cur_line->unprocessed)
772 /* This should be a single line - sanity check. */
773 assert(NULL == strchr(cur_line->unprocessed, '\r'));
774 assert(NULL == strchr(cur_line->unprocessed, '\n'));
776 if (NULL != strchr(cur_line->unprocessed, '#'))
778 /* Must quote '#' characters */
785 /* Count number of # characters, so we know length of output string */
786 src = cur_line->unprocessed;
787 while (NULL != (src = strchr(src, '#')))
794 /* Allocate new memory for string */
795 len = strlen(cur_line->unprocessed);
796 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
798 /* Uh oh, just trashed file! */
800 return JB_ERR_MEMORY;
803 /* Loop through string from end */
804 src = cur_line->unprocessed + len;
805 dest = str + len + numhash;
806 for ( ; len >= 0; len--)
808 if ((*dest-- = *src--) == '#')
812 assert(numhash >= 0);
815 assert(numhash == 0);
816 assert(src + 1 == cur_line->unprocessed);
817 assert(dest + 1 == str);
819 if (fputs(str, fp) < 0)
830 /* Can write without quoting '#' characters. */
831 if (fputs(cur_line->unprocessed, fp) < 0)
837 if (fputs(NEWLINE(file->newline), fp) < 0)
845 /* FIXME: Write data from file->data->whatever */
849 cur_line = cur_line->next;
855 /* Update the version stamp in the file structure, since we just
856 * wrote to the file & changed it's date.
858 if (stat(file->filename, statbuf) < 0)
860 /* Error, probably file not found. */
863 file->version = (unsigned)statbuf->st_mtime;
865 /* Correct file->version_str */
866 freez(file->version_str);
867 snprintf(version_buf, 22, "%u", file->version);
868 version_buf[21] = '\0';
869 file->version_str = strdup(version_buf);
870 if (version_buf == NULL)
872 return JB_ERR_MEMORY;
879 /*********************************************************************
881 * Function : edit_free_file
883 * Description : Free a complete file in memory.
886 * 1 : file = Data structure to free.
890 *********************************************************************/
891 void edit_free_file(struct editable_file * file)
895 /* Silently ignore NULL pointer */
899 edit_free_file_lines(file->lines);
900 freez(file->filename);
901 freez(file->identifier);
902 freez(file->version_str);
904 file->parse_error_text = NULL; /* Statically allocated */
905 file->parse_error = NULL;
911 /*********************************************************************
913 * Function : edit_free_file
915 * Description : Free an entire linked list of file lines.
918 * 1 : first_line = Data structure to free.
922 *********************************************************************/
923 static void edit_free_file_lines(struct file_line * first_line)
925 struct file_line * next_line;
927 while (first_line != NULL)
929 next_line = first_line->next;
930 first_line->next = NULL;
931 freez(first_line->raw);
932 freez(first_line->prefix);
933 freez(first_line->unprocessed);
934 switch(first_line->type)
936 case 0: /* special case if memory zeroed */
937 case FILE_LINE_UNPROCESSED:
938 case FILE_LINE_BLANK:
939 case FILE_LINE_ALIAS_HEADER:
940 case FILE_LINE_SETTINGS_HEADER:
941 case FILE_LINE_DESCRIPTION_HEADER:
942 case FILE_LINE_DESCRIPTION_ENTRY:
943 case FILE_LINE_ALIAS_ENTRY:
945 /* No data is stored for these */
948 case FILE_LINE_ACTION:
949 free_action(first_line->data.action);
952 case FILE_LINE_SETTINGS_ENTRY:
953 freez(first_line->data.setting.name);
954 freez(first_line->data.setting.svalue);
957 /* Should never happen */
961 first_line->type = 0; /* paranoia */
963 first_line = next_line;
968 /*********************************************************************
970 * Function : match_actions_file_header_line
972 * Description : Match an actions file {{header}} line
975 * 1 : line = String from file
976 * 2 : name = Header to match against
978 * Returns : 0 iff they match.
980 *********************************************************************/
981 static int match_actions_file_header_line(const char * line, const char * name)
989 if ((line[0] != '{') || (line[1] != '{'))
995 /* Look for optional whitespace */
996 while ( (*line == ' ') || (*line == '\t') )
1001 /* Look for the specified name (case-insensitive) */
1003 if (0 != strncmpic(line, name, len))
1009 /* Look for optional whitespace */
1010 while ( (*line == ' ') || (*line == '\t') )
1015 /* Look for "}}" and end of string*/
1016 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1026 /*********************************************************************
1028 * Function : match_actions_file_header_line
1030 * Description : Match an actions file {{header}} line
1033 * 1 : line = String from file. Must not start with
1034 * whitespace (else infinite loop!)
1035 * 2 : name = Destination for name
1036 * 2 : name = Destination for value
1038 * Returns : JB_ERR_OK on success
1039 * JB_ERR_MEMORY on out-of-memory
1040 * JB_ERR_PARSE if there's no "=" sign, or if there's
1041 * nothing before the "=" sign (but empty
1042 * values *after* the "=" sign are legal).
1044 *********************************************************************/
1045 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1047 const char * name_end;
1048 const char * value_start;
1054 assert(*line != ' ');
1055 assert(*line != '\t');
1060 value_start = strchr(line, '=');
1061 if ((value_start == NULL) || (value_start == line))
1063 return JB_ERR_PARSE;
1066 name_end = value_start - 1;
1068 /* Eat any whitespace before the '=' */
1069 while ((*name_end == ' ') || (*name_end == '\t'))
1072 * we already know we must have at least 1 non-ws char
1073 * at start of buf - no need to check
1078 name_len = name_end - line + 1; /* Length excluding \0 */
1079 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1081 return JB_ERR_MEMORY;
1083 strncpy(*pname, line, name_len);
1084 (*pname)[name_len] = '\0';
1086 /* Eat any the whitespace after the '=' */
1088 while ((*value_start == ' ') || (*value_start == '\t'))
1093 if (NULL == (*pvalue = strdup(value_start)))
1097 return JB_ERR_MEMORY;
1104 /*********************************************************************
1106 * Function : edit_parse_actions_file
1108 * Description : Parse an actions file in memory.
1110 * Passed linked list must have the "data" member
1111 * zeroed, and must contain valid "next" and
1112 * "unprocessed" fields. The "raw" and "prefix"
1113 * fields are ignored, and "type" is just overwritten.
1115 * Note that on error the file may have been
1119 * 1 : file = Actions file to be parsed in-place.
1121 * Returns : JB_ERR_OK on success
1122 * JB_ERR_MEMORY on out-of-memory
1123 * JB_ERR_PARSE on error
1125 *********************************************************************/
1126 jb_err edit_parse_actions_file(struct editable_file * file)
1128 struct file_line * cur_line;
1130 const char * text; /* Text from a line */
1131 char * name; /* For lines of the form name=value */
1132 char * value; /* For lines of the form name=value */
1133 struct action_alias * alias_list = NULL;
1134 jb_err err = JB_ERR_OK;
1136 /* alias_list contains the aliases defined in this file.
1137 * It might be better to use the "file_line.data" fields
1138 * in the relavent places instead.
1141 cur_line = file->lines;
1143 /* A note about blank line support: Blank lines should only
1144 * ever occur as the last line in the file. This function
1145 * is more forgiving than that - FILE_LINE_BLANK can occur
1149 /* Skip leading blanks. Should only happen if file is
1150 * empty (which is valid, but pointless).
1152 while ( (cur_line != NULL)
1153 && (cur_line->unprocessed[0] == '\0') )
1156 cur_line->type = FILE_LINE_BLANK;
1157 cur_line = cur_line->next;
1160 if ( (cur_line != NULL)
1161 && (cur_line->unprocessed[0] != '{') )
1163 /* File doesn't start with a header */
1164 file->parse_error = cur_line;
1165 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1166 return JB_ERR_PARSE;
1169 if ( (cur_line != NULL) && (0 ==
1170 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1172 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1174 cur_line = cur_line->next;
1175 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1177 if (cur_line->unprocessed[0])
1179 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1181 err = split_line_on_equals(cur_line->unprocessed,
1182 &cur_line->data.setting.name,
1183 &cur_line->data.setting.svalue);
1184 if (err == JB_ERR_MEMORY)
1188 else if (err != JB_ERR_OK)
1190 /* Line does not contain a name=value pair */
1191 file->parse_error = cur_line;
1192 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1193 return JB_ERR_PARSE;
1198 cur_line->type = FILE_LINE_BLANK;
1200 cur_line = cur_line->next;
1204 if ( (cur_line != NULL) && (0 ==
1205 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1207 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1209 cur_line = cur_line->next;
1210 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1212 if (cur_line->unprocessed[0])
1214 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1218 cur_line->type = FILE_LINE_BLANK;
1220 cur_line = cur_line->next;
1224 if ( (cur_line != NULL) && (0 ==
1225 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1227 cur_line->type = FILE_LINE_ALIAS_HEADER;
1229 cur_line = cur_line->next;
1230 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1232 if (cur_line->unprocessed[0])
1234 /* define an alias */
1235 struct action_alias * new_alias;
1237 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1239 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1240 if (err == JB_ERR_MEMORY)
1244 else if (err != JB_ERR_OK)
1246 /* Line does not contain a name=value pair */
1247 file->parse_error = cur_line;
1248 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1249 return JB_ERR_PARSE;
1252 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1257 free_alias_list(alias_list);
1258 return JB_ERR_MEMORY;
1261 err = get_actions(value, alias_list, new_alias->action);
1264 /* Invalid action or out of memory */
1268 free_alias_list(alias_list);
1269 if (err == JB_ERR_MEMORY)
1275 /* Line does not contain a name=value pair */
1276 file->parse_error = cur_line;
1277 file->parse_error_text = "This alias does not specify a valid set of actions.";
1278 return JB_ERR_PARSE;
1284 new_alias->name = name;
1287 new_alias->next = alias_list;
1288 alias_list = new_alias;
1292 cur_line->type = FILE_LINE_BLANK;
1294 cur_line = cur_line->next;
1298 /* Header done, process the main part of the file */
1299 while (cur_line != NULL)
1301 /* At this point, (cur_line->unprocessed[0] == '{') */
1302 assert(cur_line->unprocessed[0] == '{');
1303 text = cur_line->unprocessed + 1;
1304 len = strlen(text) - 1;
1305 if (text[len] != '}')
1307 /* No closing } on header */
1308 free_alias_list(alias_list);
1309 file->parse_error = cur_line;
1310 file->parse_error_text = "Headers starting with '{' must have a "
1311 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1312 "must close with two brackets ('}}').";
1313 return JB_ERR_PARSE;
1318 /* An invalid {{ header. */
1319 free_alias_list(alias_list);
1320 file->parse_error = cur_line;
1321 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1322 "Please remember that the system (two-bracket) headers must "
1323 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1324 "and must appear before any actions (one-bracket) headers. "
1325 "Also note that system headers may not be repeated.";
1326 return JB_ERR_PARSE;
1329 while ( (*text == ' ') || (*text == '\t') )
1335 && ( (text[len - 1] == ' ')
1336 || (text[len - 1] == '\t') ) )
1341 cur_line->type = FILE_LINE_ACTION;
1343 /* Remove {} and make copy */
1344 if (NULL == (value = (char *) malloc(len + 1)))
1347 free_alias_list(alias_list);
1348 return JB_ERR_MEMORY;
1350 strncpy(value, text, len);
1354 err = get_actions(value, alias_list, cur_line->data.action);
1357 /* Invalid action or out of memory */
1359 free_alias_list(alias_list);
1360 if (err == JB_ERR_MEMORY)
1366 /* Line does not contain a name=value pair */
1367 file->parse_error = cur_line;
1368 file->parse_error_text = "This header does not specify a valid set of actions.";
1369 return JB_ERR_PARSE;
1373 /* Done with string - it was clobbered anyway */
1376 /* Process next line */
1377 cur_line = cur_line->next;
1379 /* Loop processing URL patterns */
1380 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1382 if (cur_line->unprocessed[0])
1384 /* Could parse URL here, but this isn't currently needed */
1386 cur_line->type = FILE_LINE_URL;
1390 cur_line->type = FILE_LINE_BLANK;
1392 cur_line = cur_line->next;
1394 } /* End main while(cur_line != NULL) loop */
1396 free_alias_list(alias_list);
1402 /*********************************************************************
1404 * Function : edit_read_file_lines
1406 * Description : Read all the lines of a file into memory.
1407 * Handles whitespace, comments and line continuation.
1410 * 1 : fp = File to read from. On return, this will be
1411 * at EOF but it will not have been closed.
1412 * 2 : pfile = Destination for a linked list of file_lines.
1413 * Will be set to NULL on error.
1415 * Returns : JB_ERR_OK on success
1416 * JB_ERR_MEMORY on out-of-memory
1418 *********************************************************************/
1419 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1421 struct file_line * first_line; /* Keep for return value or to free */
1422 struct file_line * cur_line; /* Current line */
1423 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1431 cur_line = first_line = zalloc(sizeof(struct file_line));
1432 if (cur_line == NULL)
1434 return JB_ERR_MEMORY;
1437 cur_line->type = FILE_LINE_UNPROCESSED;
1439 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1442 /* Out of memory or empty file. */
1443 /* Note that empty file is not an error we propogate up */
1445 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1450 prev_line = cur_line;
1451 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1452 if (cur_line == NULL)
1455 edit_free_file_lines(first_line);
1456 return JB_ERR_MEMORY;
1459 cur_line->type = FILE_LINE_UNPROCESSED;
1461 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1462 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1465 edit_free_file_lines(first_line);
1466 return JB_ERR_MEMORY;
1470 while (rval != JB_ERR_FILE);
1474 /* We allocated one too many - free it */
1475 prev_line->next = NULL;
1478 *pfile = first_line;
1483 /*********************************************************************
1485 * Function : edit_read_file
1487 * Description : Read a complete file into memory.
1488 * Handles CGI parameter parsing. If requested, also
1489 * checks the file's modification timestamp.
1492 * 1 : csp = Current client state (buffers, headers, etc...)
1493 * 2 : parameters = map of cgi parameters.
1494 * 3 : require_version = true to check "ver" parameter.
1495 * 4 : suffix = File extension, e.g. ".action".
1496 * 5 : pfile = Destination for the file. Will be set
1500 * filename : The name of the file to read, without the
1501 * path or ".action" extension.
1502 * ver : (Only if require_version is nonzero)
1503 * Timestamp of the actions file. If wrong, this
1504 * function fails with JB_ERR_MODIFIED.
1506 * Returns : JB_ERR_OK on success
1507 * JB_ERR_MEMORY on out-of-memory
1508 * JB_ERR_CGI_PARAMS if "filename" was not specified
1510 * JB_ERR_FILE if the file cannot be opened or
1512 * JB_ERR_MODIFIED if version checking was requested and
1513 * failed - the file was modified outside
1514 * of this CGI editor instance.
1516 *********************************************************************/
1517 jb_err edit_read_file(struct client_state *csp,
1518 const struct map *parameters,
1519 int require_version,
1521 struct editable_file **pfile)
1523 struct file_line * lines;
1527 const char * identifier;
1528 struct editable_file * file;
1529 unsigned version = 0;
1530 struct stat statbuf[1];
1531 char version_buf[22];
1532 int newline = NEWLINE_UNKNOWN;
1540 err = get_file_name_param(csp, parameters, "f", suffix,
1541 &filename, &identifier);
1547 if (stat(filename, statbuf) < 0)
1549 /* Error, probably file not found. */
1553 version = (unsigned) statbuf->st_mtime;
1555 if (require_version)
1557 unsigned specified_version;
1558 err = get_number_param(csp, parameters, "v", &specified_version);
1565 if (version != specified_version)
1567 return JB_ERR_MODIFIED;
1571 #if defined(AMIGA) || defined(__OS2__)
1572 if (NULL == (fp = fopen(filename,"r")))
1574 if (NULL == (fp = fopen(filename,"rt")))
1575 #endif /* def AMIGA */
1581 err = edit_read_file_lines(fp, &lines, &newline);
1591 file = (struct editable_file *) zalloc(sizeof(*file));
1595 edit_free_file_lines(lines);
1599 file->lines = lines;
1600 file->newline = newline;
1601 file->filename = filename;
1602 file->version = version;
1603 file->identifier = url_encode(identifier);
1605 if (file->identifier == NULL)
1607 edit_free_file(file);
1608 return JB_ERR_MEMORY;
1611 /* Correct file->version_str */
1612 freez(file->version_str);
1613 snprintf(version_buf, 22, "%u", file->version);
1614 version_buf[21] = '\0';
1615 file->version_str = strdup(version_buf);
1616 if (version_buf == NULL)
1618 edit_free_file(file);
1619 return JB_ERR_MEMORY;
1627 /*********************************************************************
1629 * Function : edit_read_actions_file
1631 * Description : Read a complete actions file into memory.
1632 * Handles CGI parameter parsing. If requested, also
1633 * checks the file's modification timestamp.
1635 * If this function detects an error in the categories
1636 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1637 * then it handles it by filling in the specified
1638 * response structure and returning JB_ERR_FILE.
1641 * 1 : csp = Current client state (buffers, headers, etc...)
1642 * 2 : rsp = HTTP response. Only filled in on error.
1643 * 2 : parameters = map of cgi parameters.
1644 * 3 : require_version = true to check "ver" parameter.
1645 * 4 : pfile = Destination for the file. Will be set
1649 * filename : The name of the actions file to read, without the
1650 * path or ".action" extension.
1651 * ver : (Only if require_version is nonzero)
1652 * Timestamp of the actions file. If wrong, this
1653 * function fails with JB_ERR_MODIFIED.
1655 * Returns : JB_ERR_OK on success
1656 * JB_ERR_MEMORY on out-of-memory
1657 * JB_ERR_CGI_PARAMS if "filename" was not specified
1659 * JB_ERR_FILE if the file does not contain valid data,
1660 * or if file cannot be opened or
1661 * contains no data, or if version
1662 * checking was requested and failed.
1664 *********************************************************************/
1665 jb_err edit_read_actions_file(struct client_state *csp,
1666 struct http_response *rsp,
1667 const struct map *parameters,
1668 int require_version,
1669 struct editable_file **pfile)
1672 struct editable_file *file;
1680 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1683 /* Try to handle if possible */
1684 if (err == JB_ERR_FILE)
1686 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1688 else if (err == JB_ERR_MODIFIED)
1690 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1692 if (err == JB_ERR_OK)
1695 * Signal to higher-level CGI code that there was a problem but we
1696 * handled it, they should just return JB_ERR_OK.
1703 err = edit_parse_actions_file(file);
1706 if (err == JB_ERR_PARSE)
1708 err = cgi_error_parse(csp, rsp, file);
1709 if (err == JB_ERR_OK)
1712 * Signal to higher-level CGI code that there was a problem but we
1713 * handled it, they should just return JB_ERR_OK.
1718 edit_free_file(file);
1727 /*********************************************************************
1729 * Function : get_file_name_param
1731 * Description : Get the name of the file to edit from the parameters
1732 * passed to a CGI function. This function handles
1733 * security checks such as blocking urls containing
1734 * "/" or ".", prepending the config file directory,
1735 * and adding the specified suffix.
1737 * (This is an essential security check, otherwise
1738 * users may be able to pass "../../../etc/passwd"
1739 * and overwrite the password file [linux], "prn:"
1740 * and print random data [Windows], etc...)
1742 * This function only allows filenames contining the
1743 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1744 * That's probably too restrictive but at least it's
1748 * 1 : csp = Current client state (buffers, headers, etc...)
1749 * 2 : parameters = map of cgi parameters
1750 * 3 : param_name = The name of the parameter to read
1751 * 4 : suffix = File extension, e.g. ".actions"
1752 * 5 : pfilename = destination for full filename. Caller
1753 * free()s. Set to NULL on error.
1754 * 6 : pparam = destination for partial filename,
1755 * suitable for use in another URL. Allocated as part
1756 * of the map "parameters", so don't free it.
1757 * Set to NULL if not specified.
1759 * Returns : JB_ERR_OK on success
1760 * JB_ERR_MEMORY on out-of-memory
1761 * JB_ERR_CGI_PARAMS if "filename" was not specified
1764 *********************************************************************/
1765 static jb_err get_file_name_param(struct client_state *csp,
1766 const struct map *parameters,
1767 const char *param_name,
1770 const char **pparam)
1774 #if 0 /* Patch to make 3.0.0 work properly. */
1776 #endif /* 0 - Patch to make 3.0.0 work properly. */
1790 param = lookup(parameters, param_name);
1793 return JB_ERR_CGI_PARAMS;
1798 len = strlen(param);
1799 if (len >= FILENAME_MAX)
1802 return JB_ERR_CGI_PARAMS;
1805 /* Check every character to see if it's legal */
1807 while ((ch = *s++) != '\0')
1809 if ( ((ch < 'A') || (ch > 'Z'))
1810 && ((ch < 'a') || (ch > 'z'))
1811 && ((ch < '0') || (ch > '9'))
1815 /* Probable hack attempt. */
1816 return JB_ERR_CGI_PARAMS;
1821 * FIXME Following is a hack to make 3.0.0 work properly.
1822 * Change "#if 0" --> "#if 1" below when we have modular action
1826 #if 0 /* Patch to make 3.0.0 work properly. */
1827 /* Append extension */
1828 name = malloc(len + strlen(suffix) + 1);
1831 return JB_ERR_MEMORY;
1833 strcpy(name, param);
1834 strcpy(name + len, suffix);
1837 fullpath = make_path(csp->config->confdir, name);
1839 #else /* 1 - Patch to make 3.0.0 work properly. */
1840 if ((csp->actions_list == NULL)
1841 || (csp->actions_list->filename == NULL))
1843 return JB_ERR_CGI_PARAMS;
1846 fullpath = ( (csp->actions_list && csp->actions_list->filename)
1847 ? strdup(csp->actions_list->filename) : NULL);
1848 #endif /* 1 - Patch to make 3.0.0 work properly. */
1849 if (fullpath == NULL)
1851 return JB_ERR_MEMORY;
1855 *pfilename = fullpath;
1861 /*********************************************************************
1863 * Function : get_char_param
1865 * Description : Get a single-character parameter passed to a CGI
1869 * 1 : parameters = map of cgi parameters
1870 * 2 : param_name = The name of the parameter to read
1872 * Returns : Uppercase character on success, '\0' on error.
1874 *********************************************************************/
1875 static char get_char_param(const struct map *parameters,
1876 const char *param_name)
1883 ch = *(lookup(parameters, param_name));
1884 if ((ch >= 'a') && (ch <= 'z'))
1886 ch = ch - 'a' + 'A';
1893 /*********************************************************************
1895 * Function : get_string_param
1897 * Description : Get a string paramater, to be used as an
1898 * ACTION_STRING or ACTION_MULTI paramater.
1899 * Validates the input to prevent stupid/malicious
1900 * users from corrupting their action file.
1903 * 1 : parameters = map of cgi parameters
1904 * 2 : param_name = The name of the parameter to read
1905 * 3 : pparam = destination for paramater. Allocated as
1906 * part of the map "parameters", so don't free it.
1907 * Set to NULL if not specified.
1909 * Returns : JB_ERR_OK on success, or if the paramater
1910 * was not specified.
1911 * JB_ERR_MEMORY on out-of-memory.
1912 * JB_ERR_CGI_PARAMS if the paramater is not valid.
1914 *********************************************************************/
1915 static jb_err get_string_param(const struct map *parameters,
1916 const char *param_name,
1917 const char **pparam)
1929 param = lookup(parameters, param_name);
1935 if (strlen(param) >= CGI_ACTION_PARAM_LEN_MAX)
1940 * Note that the length limit is arbitrary, it just seems
1941 * sensible to limit it to *something*. There's no
1942 * technical reason for any limit at all.
1944 return JB_ERR_CGI_PARAMS;
1947 /* Check every character to see if it's legal */
1949 while ((ch = *s++) != '\0')
1951 if ( ((unsigned char)ch < (unsigned char)' ')
1954 /* Probable hack attempt, or user accidentally used '}'. */
1955 return JB_ERR_CGI_PARAMS;
1966 /*********************************************************************
1968 * Function : get_number_param
1970 * Description : Get a non-negative integer from the parameters
1971 * passed to a CGI function.
1974 * 1 : csp = Current client state (buffers, headers, etc...)
1975 * 2 : parameters = map of cgi parameters
1976 * 3 : name = Name of CGI parameter to read
1977 * 4 : pvalue = destination for value.
1978 * Set to -1 on error.
1980 * Returns : JB_ERR_OK on success
1981 * JB_ERR_MEMORY on out-of-memory
1982 * JB_ERR_CGI_PARAMS if the parameter was not specified
1985 *********************************************************************/
1986 static jb_err get_number_param(struct client_state *csp,
1987 const struct map *parameters,
2002 param = lookup(parameters, name);
2005 return JB_ERR_CGI_PARAMS;
2008 /* We don't use atoi because I want to check this carefully... */
2011 while ((ch = *param++) != '\0')
2013 if ((ch < '0') || (ch > '9'))
2015 return JB_ERR_CGI_PARAMS;
2022 * <limits.h> defines UINT_MAX
2024 * (UINT_MAX - ch) / 10 is the largest number that
2025 * can be safely multiplied by 10 then have ch added.
2027 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
2029 return JB_ERR_CGI_PARAMS;
2032 value = value * 10 + ch;
2043 /*********************************************************************
2045 * Function : get_url_spec_param
2047 * Description : Get a URL pattern from the parameters
2048 * passed to a CGI function. Removes leading/trailing
2049 * spaces and validates it.
2052 * 1 : csp = Current client state (buffers, headers, etc...)
2053 * 2 : parameters = map of cgi parameters
2054 * 3 : name = Name of CGI parameter to read
2055 * 4 : pvalue = destination for value. Will be malloc()'d.
2056 * Set to NULL on error.
2058 * Returns : JB_ERR_OK on success
2059 * JB_ERR_MEMORY on out-of-memory
2060 * JB_ERR_CGI_PARAMS if the parameter was not specified
2063 *********************************************************************/
2064 static jb_err get_url_spec_param(struct client_state *csp,
2065 const struct map *parameters,
2069 const char *orig_param;
2072 struct url_spec compiled[1];
2082 orig_param = lookup(parameters, name);
2085 return JB_ERR_CGI_PARAMS;
2088 /* Copy and trim whitespace */
2089 param = strdup(orig_param);
2092 return JB_ERR_MEMORY;
2096 /* Must be non-empty, and can't allow 1st character to be '{' */
2097 if (param[0] == '\0' || param[0] == '{')
2100 return JB_ERR_CGI_PARAMS;
2103 /* Check for embedded newlines */
2104 for (s = param; *s != '\0'; s++)
2106 if ((*s == '\r') || (*s == '\n'))
2109 return JB_ERR_CGI_PARAMS;
2113 /* Check that regex is valid */
2118 return JB_ERR_MEMORY;
2120 err = create_url_spec(compiled, s);
2125 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2127 free_url_spec(compiled);
2129 if (param[strlen(param) - 1] == '\\')
2132 * Must protect trailing '\\' from becoming line continuation character.
2133 * Two methods: 1) If it's a domain only, add a trailing '/'.
2134 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2136 if (strchr(param, '/') == NULL)
2138 err = string_append(¶m, "/");
2142 err = string_append(¶m, "(?:)");
2149 /* Check that the modified regex is valid */
2154 return JB_ERR_MEMORY;
2156 err = create_url_spec(compiled, s);
2161 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2163 free_url_spec(compiled);
2170 /*********************************************************************
2172 * Function : map_radio
2174 * Description : Map a set of radio button values. E.g. if you have
2175 * 3 radio buttons, declare them as:
2176 * <option type="radio" name="xyz" @xyz-a@>
2177 * <option type="radio" name="xyz" @xyz-b@>
2178 * <option type="radio" name="xyz" @xyz-c@>
2179 * Then map one of the @xyz-?@ variables to "checked"
2180 * and all the others to empty by calling:
2181 * map_radio(exports, "xyz", "abc", sel)
2182 * Where 'sel' is 'a', 'b', or 'c'.
2185 * 1 : exports = Exports map to modify.
2186 * 2 : optionname = name for map
2187 * 3 : values = null-terminated list of values;
2188 * 4 : value = Selected value.
2190 * CGI Parameters : None
2192 * Returns : JB_ERR_OK on success
2193 * JB_ERR_MEMORY on out-of-memory
2195 *********************************************************************/
2196 static jb_err map_radio(struct map * exports,
2197 const char * optionname,
2198 const char * values,
2210 len = strlen(optionname);
2211 buf = malloc(len + 3);
2214 return JB_ERR_MEMORY;
2217 strcpy(buf, optionname);
2222 while ((c = *values++) != '\0')
2227 if (map(exports, buf, 1, "", 1))
2229 return JB_ERR_MEMORY;
2235 return map(exports, buf, 0, "checked", 1);
2239 /*********************************************************************
2241 * Function : cgi_error_modified
2243 * Description : CGI function that is called when a file is modified
2244 * outside the CGI editor.
2247 * 1 : csp = Current client state (buffers, headers, etc...)
2248 * 2 : rsp = http_response data structure for output
2249 * 3 : filename = The file that was modified.
2251 * CGI Parameters : none
2253 * Returns : JB_ERR_OK on success
2254 * JB_ERR_MEMORY on out-of-memory error.
2256 *********************************************************************/
2257 jb_err cgi_error_modified(struct client_state *csp,
2258 struct http_response *rsp,
2259 const char *filename)
2261 struct map *exports;
2268 if (NULL == (exports = default_exports(csp, NULL)))
2270 return JB_ERR_MEMORY;
2273 err = map(exports, "f", 1, html_encode(filename), 0);
2280 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2284 /*********************************************************************
2286 * Function : cgi_error_parse
2288 * Description : CGI function that is called when a file cannot
2289 * be parsed by the CGI editor.
2292 * 1 : csp = Current client state (buffers, headers, etc...)
2293 * 2 : rsp = http_response data structure for output
2294 * 3 : file = The file that was modified.
2296 * CGI Parameters : none
2298 * Returns : JB_ERR_OK on success
2299 * JB_ERR_MEMORY on out-of-memory error.
2301 *********************************************************************/
2302 jb_err cgi_error_parse(struct client_state *csp,
2303 struct http_response *rsp,
2304 struct editable_file *file)
2306 struct map *exports;
2308 struct file_line *cur_line;
2314 if (NULL == (exports = default_exports(csp, NULL)))
2316 return JB_ERR_MEMORY;
2319 err = map(exports, "f", 1, file->identifier, 1);
2320 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2322 cur_line = file->parse_error;
2325 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2326 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2334 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2338 /*********************************************************************
2340 * Function : cgi_error_file
2342 * Description : CGI function that is called when a file cannot be
2343 * opened by the CGI editor.
2346 * 1 : csp = Current client state (buffers, headers, etc...)
2347 * 2 : rsp = http_response data structure for output
2348 * 3 : filename = The file that was modified.
2350 * CGI Parameters : none
2352 * Returns : JB_ERR_OK on success
2353 * JB_ERR_MEMORY on out-of-memory error.
2355 *********************************************************************/
2356 jb_err cgi_error_file(struct client_state *csp,
2357 struct http_response *rsp,
2358 const char *filename)
2360 struct map *exports;
2367 if (NULL == (exports = default_exports(csp, NULL)))
2369 return JB_ERR_MEMORY;
2372 err = map(exports, "f", 1, html_encode(filename), 0);
2379 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2383 /*********************************************************************
2385 * Function : cgi_error_bad_param
2387 * Description : CGI function that is called if the parameters
2388 * (query string) for a CGI were wrong.
2391 * 1 : csp = Current client state (buffers, headers, etc...)
2392 * 2 : rsp = http_response data structure for output
2394 * CGI Parameters : none
2396 * Returns : JB_ERR_OK on success
2397 * JB_ERR_MEMORY on out-of-memory error.
2399 *********************************************************************/
2400 jb_err cgi_error_disabled(struct client_state *csp,
2401 struct http_response *rsp)
2403 struct map *exports;
2408 if (NULL == (exports = default_exports(csp, NULL)))
2410 return JB_ERR_MEMORY;
2413 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2417 /*********************************************************************
2419 * Function : cgi_edit_actions
2421 * Description : CGI function that allows the user to choose which
2422 * actions file to edit.
2425 * 1 : csp = Current client state (buffers, headers, etc...)
2426 * 2 : rsp = http_response data structure for output
2427 * 3 : parameters = map of cgi parameters
2429 * CGI Parameters : None
2431 * Returns : JB_ERR_OK on success
2432 * JB_ERR_MEMORY on out-of-memory error
2434 *********************************************************************/
2435 jb_err cgi_edit_actions(struct client_state *csp,
2436 struct http_response *rsp,
2437 const struct map *parameters)
2440 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2442 return cgi_error_disabled(csp, rsp);
2445 /* FIXME: Incomplete */
2446 rsp->status = strdup("302 Local Redirect from Junkbuster");
2447 if (rsp->status == NULL)
2449 return JB_ERR_MEMORY;
2451 if (enlist_unique_header(rsp->headers, "Location",
2452 CGI_PREFIX "edit-actions-list?f=ijb"))
2456 return JB_ERR_MEMORY;
2463 /*********************************************************************
2465 * Function : cgi_edit_actions_list
2467 * Description : CGI function that edits the actions list.
2468 * FIXME: This function shouldn't FATAL ever.
2469 * FIXME: This function doesn't check the retval of map()
2471 * 1 : csp = Current client state (buffers, headers, etc...)
2472 * 2 : rsp = http_response data structure for output
2473 * 3 : parameters = map of cgi parameters
2475 * CGI Parameters : filename
2477 * Returns : JB_ERR_OK on success
2478 * JB_ERR_MEMORY on out-of-memory
2479 * JB_ERR_FILE if the file cannot be opened or
2481 * JB_ERR_CGI_PARAMS if "filename" was not specified
2484 *********************************************************************/
2485 jb_err cgi_edit_actions_list(struct client_state *csp,
2486 struct http_response *rsp,
2487 const struct map *parameters)
2489 char * section_template;
2490 char * url_template;
2495 struct map * exports;
2496 struct map * section_exports;
2497 struct map * url_exports;
2498 struct editable_file * file;
2499 struct file_line * cur_line;
2500 unsigned line_number = 0;
2501 unsigned prev_section_line_number = ((unsigned) (-1));
2505 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2507 return cgi_error_disabled(csp, rsp);
2510 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2513 /* No filename specified, can't read file, or out of memory. */
2514 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2517 if (NULL == (exports = default_exports(csp, NULL)))
2519 edit_free_file(file);
2520 return JB_ERR_MEMORY;
2523 err = map(exports, "f", 1, file->identifier, 1);
2524 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2528 edit_free_file(file);
2533 /* Should do all global exports above this point */
2535 err = template_load(csp, §ion_template, "edit-actions-list-section");
2538 edit_free_file(file);
2540 if (err == JB_ERR_FILE)
2542 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2547 err = template_load(csp, &url_template, "edit-actions-list-url");
2550 free(section_template);
2551 edit_free_file(file);
2553 if (err == JB_ERR_FILE)
2555 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2560 err = template_fill(§ion_template, exports);
2564 edit_free_file(file);
2570 err = template_fill(&url_template, exports);
2573 free(section_template);
2574 edit_free_file(file);
2579 /* Find start of actions in file */
2580 cur_line = file->lines;
2582 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2584 cur_line = cur_line->next;
2588 if (NULL == (sections = strdup("")))
2590 free(section_template);
2592 edit_free_file(file);
2594 return JB_ERR_MEMORY;
2597 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2599 if (NULL == (section_exports = new_map()))
2602 free(section_template);
2604 edit_free_file(file);
2606 return JB_ERR_MEMORY;
2609 snprintf(buf, 50, "%d", line_number);
2610 err = map(section_exports, "s", 1, buf, 1);
2611 if (!err) err = map(section_exports, "actions", 1,
2612 actions_to_html(cur_line->data.action), 0);
2615 && (cur_line->next != NULL)
2616 && (cur_line->next->type == FILE_LINE_URL))
2618 /* This section contains at least one URL, don't allow delete */
2619 err = map_block_killer(section_exports, "empty-section");
2623 if (!err) err = map_block_keep(section_exports, "empty-section");
2626 if (prev_section_line_number != ((unsigned)(-1)))
2628 /* Not last section */
2629 snprintf(buf, 50, "%d", prev_section_line_number);
2630 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2631 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2636 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2638 prev_section_line_number = line_number;
2643 free(section_template);
2645 edit_free_file(file);
2647 free_map(section_exports);
2651 /* Should do all section-specific exports above this point */
2653 if (NULL == (urls = strdup("")))
2656 free(section_template);
2658 edit_free_file(file);
2660 free_map(section_exports);
2661 return JB_ERR_MEMORY;
2666 cur_line = cur_line->next;
2669 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2671 if (NULL == (url_exports = new_map()))
2675 free(section_template);
2677 edit_free_file(file);
2679 free_map(section_exports);
2680 return JB_ERR_MEMORY;
2683 snprintf(buf, 50, "%d", line_number);
2684 err = map(url_exports, "p", 1, buf, 1);
2686 snprintf(buf, 50, "%d", url_1_2);
2687 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2689 if (!err) err = map(url_exports, "url-html", 1,
2690 html_encode(cur_line->unprocessed), 0);
2691 if (!err) err = map(url_exports, "url", 1,
2692 url_encode(cur_line->unprocessed), 0);
2698 free(section_template);
2700 edit_free_file(file);
2702 free_map(section_exports);
2703 free_map(url_exports);
2707 if (NULL == (s = strdup(url_template)))
2711 free(section_template);
2713 edit_free_file(file);
2715 free_map(section_exports);
2716 free_map(url_exports);
2717 return JB_ERR_MEMORY;
2720 err = template_fill(&s, section_exports);
2721 if (!err) err = template_fill(&s, url_exports);
2722 if (!err) err = string_append(&urls, s);
2724 free_map(url_exports);
2731 free(section_template);
2733 edit_free_file(file);
2735 free_map(section_exports);
2739 url_1_2 = 3 - url_1_2;
2741 cur_line = cur_line->next;
2745 err = map(section_exports, "urls", 1, urls, 0);
2747 /* Could also do section-specific exports here, but it wouldn't be as fast */
2749 if ( (cur_line != NULL)
2750 && (cur_line->type == FILE_LINE_ACTION))
2752 /* Not last section */
2753 snprintf(buf, 50, "%d", line_number);
2754 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2755 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2760 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2766 free(section_template);
2768 edit_free_file(file);
2770 free_map(section_exports);
2774 if (NULL == (s = strdup(section_template)))
2777 free(section_template);
2779 edit_free_file(file);
2781 free_map(section_exports);
2782 return JB_ERR_MEMORY;
2785 err = template_fill(&s, section_exports);
2786 if (!err) err = string_append(§ions, s);
2789 free_map(section_exports);
2794 free(section_template);
2796 edit_free_file(file);
2802 edit_free_file(file);
2803 free(section_template);
2806 err = map(exports, "sections", 1, sections, 0);
2813 /* Could also do global exports here, but it wouldn't be as fast */
2815 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2819 /*********************************************************************
2821 * Function : cgi_edit_actions
2823 * Description : CGI function that edits the Actions list.
2826 * 1 : csp = Current client state (buffers, headers, etc...)
2827 * 2 : rsp = http_response data structure for output
2828 * 3 : parameters = map of cgi parameters
2830 * CGI Parameters : None
2832 * Returns : JB_ERR_OK on success
2833 * JB_ERR_MEMORY on out-of-memory
2834 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2835 * specified or not valid.
2837 *********************************************************************/
2838 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2839 struct http_response *rsp,
2840 const struct map *parameters)
2842 struct map * exports;
2844 struct editable_file * file;
2845 struct file_line * cur_line;
2846 unsigned line_number;
2848 struct file_list *filter_file;
2849 struct re_filterfile_spec *filter_group;
2851 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2853 return cgi_error_disabled(csp, rsp);
2856 err = get_number_param(csp, parameters, "s", §ionid);
2862 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2865 /* No filename specified, can't read file, modified, or out of memory. */
2866 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2869 cur_line = file->lines;
2871 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2873 cur_line = cur_line->next;
2876 if ( (cur_line == NULL)
2877 || (line_number != sectionid)
2879 || (cur_line->type != FILE_LINE_ACTION))
2881 /* Invalid "sectionid" parameter */
2882 edit_free_file(file);
2883 return JB_ERR_CGI_PARAMS;
2886 if (NULL == (exports = default_exports(csp, NULL)))
2888 edit_free_file(file);
2889 return JB_ERR_MEMORY;
2892 err = map(exports, "f", 1, file->identifier, 1);
2893 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2894 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2896 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2898 filter_file = csp->rlist;
2899 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2901 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2905 edit_free_file(file);
2910 if (filter_group == NULL)
2912 err = map(exports, "filter-params", 1, "", 1);
2916 /* We have some entries in the filter list */
2919 char * filter_template;
2921 err = template_load(csp, &filter_template, "edit-actions-for-url-filter");
2924 edit_free_file(file);
2926 if (err == JB_ERR_FILE)
2928 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2933 result = strdup("");
2935 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2937 char current_mode = 'x';
2938 struct list_entry *filter_name;
2940 struct map *line_exports;
2943 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2944 while ((filter_name != NULL)
2945 && (0 != strcmp(filter_group->name, filter_name->str)))
2947 filter_name = filter_name->next;
2950 if (filter_name != NULL)
2956 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2957 while ((filter_name != NULL)
2958 && (0 != strcmp(filter_group->name, filter_name->str)))
2960 filter_name = filter_name->next;
2962 if (filter_name != NULL)
2968 /* Generate a unique serial number */
2969 snprintf(number, sizeof(number), "%x", index++);
2970 number[sizeof(number) - 1] = '\0';
2972 line_exports = new_map();
2973 if (line_exports == NULL)
2975 err = JB_ERR_MEMORY;
2980 if (!err) err = map(line_exports, "index", 1, number, 1);
2981 if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
2982 if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
2983 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2988 this_line = strdup(filter_template);
2989 if (this_line == NULL) err = JB_ERR_MEMORY;
2991 if (!err) err = template_fill(&this_line, line_exports);
2992 string_join(&result, this_line);
2994 free_map(line_exports);
2999 err = map(exports, "filter-params", 1, result, 0);
3007 if (!err) err = map_radio(exports, "filter-all", "nx",
3008 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3010 edit_free_file(file);
3018 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3022 /*********************************************************************
3024 * Function : cgi_edit_actions_submit
3026 * Description : CGI function that actually edits the Actions list.
3029 * 1 : csp = Current client state (buffers, headers, etc...)
3030 * 2 : rsp = http_response data structure for output
3031 * 3 : parameters = map of cgi parameters
3033 * CGI Parameters : None
3035 * Returns : JB_ERR_OK on success
3036 * JB_ERR_MEMORY on out-of-memory
3037 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3038 * specified or not valid.
3040 *********************************************************************/
3041 jb_err cgi_edit_actions_submit(struct client_state *csp,
3042 struct http_response *rsp,
3043 const struct map *parameters)
3049 struct editable_file * file;
3050 struct file_line * cur_line;
3051 unsigned line_number;
3057 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3059 return cgi_error_disabled(csp, rsp);
3062 err = get_number_param(csp, parameters, "s", §ionid);
3068 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3071 /* No filename specified, can't read file, modified, or out of memory. */
3072 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3075 cur_line = file->lines;
3077 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3079 cur_line = cur_line->next;
3082 if ( (cur_line == NULL)
3083 || (line_number != sectionid)
3085 || (cur_line->type != FILE_LINE_ACTION))
3087 /* Invalid "sectionid" parameter */
3088 edit_free_file(file);
3089 return JB_ERR_CGI_PARAMS;
3092 err = actions_from_radio(parameters, cur_line->data.action);
3096 edit_free_file(file);
3100 ch = get_char_param(parameters, "filter_all");
3103 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3104 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3105 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3109 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3112 for (index = 0; !err; index++)
3119 /* Generate the keys */
3120 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3121 key_value[sizeof(key_value) - 1] = '\0';
3122 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3123 key_name[sizeof(key_name) - 1] = '\0';
3125 err = get_string_param(parameters, key_name, &name);
3135 value = get_char_param(parameters, key_value);
3138 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3139 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3140 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3142 else if (value == 'N')
3144 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3145 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3147 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3148 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3151 else if (value == 'X')
3153 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3154 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3161 edit_free_file(file);
3165 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3168 edit_free_file(file);
3169 return JB_ERR_MEMORY;
3172 len = strlen(actiontext);
3176 * Empty action - must special-case this.
3177 * Simply setting len to 1 is sufficient...
3182 if (NULL == (newtext = malloc(len + 2)))
3186 edit_free_file(file);
3187 return JB_ERR_MEMORY;
3189 strcpy(newtext, actiontext);
3193 newtext[len + 1] = '\0';
3195 freez(cur_line->raw);
3196 freez(cur_line->unprocessed);
3197 cur_line->unprocessed = newtext;
3199 err = edit_write_file(file);
3202 /* Error writing file */
3203 edit_free_file(file);
3207 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3208 string_append(&target, file->identifier);
3210 edit_free_file(file);
3215 return JB_ERR_MEMORY;
3218 rsp->status = strdup("302 Local Redirect from Junkbuster");
3219 if (rsp->status == NULL)
3222 return JB_ERR_MEMORY;
3224 err = enlist_unique_header(rsp->headers, "Location", target);
3231 /*********************************************************************
3233 * Function : cgi_edit_actions_url
3235 * Description : CGI function that actually edits a URL pattern in
3239 * 1 : csp = Current client state (buffers, headers, etc...)
3240 * 2 : rsp = http_response data structure for output
3241 * 3 : parameters = map of cgi parameters
3244 * filename : Identifies the file to edit
3245 * ver : File's last-modified time
3246 * section : Line number of section to edit
3247 * pattern : Line number of pattern to edit
3248 * newval : New value for pattern
3250 * Returns : JB_ERR_OK on success
3251 * JB_ERR_MEMORY on out-of-memory
3252 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3253 * specified or not valid.
3255 *********************************************************************/
3256 jb_err cgi_edit_actions_url(struct client_state *csp,
3257 struct http_response *rsp,
3258 const struct map *parameters)
3262 struct editable_file * file;
3263 struct file_line * cur_line;
3264 unsigned line_number;
3268 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3270 return cgi_error_disabled(csp, rsp);
3273 err = get_number_param(csp, parameters, "p", &patternid);
3280 return JB_ERR_CGI_PARAMS;
3283 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3289 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3292 /* No filename specified, can't read file, modified, or out of memory. */
3294 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3298 cur_line = file->lines;
3300 while ((cur_line != NULL) && (line_number < patternid))
3302 cur_line = cur_line->next;
3306 if ( (cur_line == NULL)
3307 || (cur_line->type != FILE_LINE_URL))
3309 /* Invalid "patternid" parameter */
3311 edit_free_file(file);
3312 return JB_ERR_CGI_PARAMS;
3315 /* At this point, the line to edit is in cur_line */
3317 freez(cur_line->raw);
3318 freez(cur_line->unprocessed);
3319 cur_line->unprocessed = new_pattern;
3321 err = edit_write_file(file);
3324 /* Error writing file */
3325 edit_free_file(file);
3329 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3330 string_append(&target, file->identifier);
3332 edit_free_file(file);
3337 return JB_ERR_MEMORY;
3340 rsp->status = strdup("302 Local Redirect from Junkbuster");
3341 if (rsp->status == NULL)
3344 return JB_ERR_MEMORY;
3346 err = enlist_unique_header(rsp->headers, "Location", target);
3353 /*********************************************************************
3355 * Function : cgi_edit_actions_add_url
3357 * Description : CGI function that actually adds a URL pattern to
3361 * 1 : csp = Current client state (buffers, headers, etc...)
3362 * 2 : rsp = http_response data structure for output
3363 * 3 : parameters = map of cgi parameters
3366 * filename : Identifies the file to edit
3367 * ver : File's last-modified time
3368 * section : Line number of section to edit
3369 * newval : New pattern
3371 * Returns : JB_ERR_OK on success
3372 * JB_ERR_MEMORY on out-of-memory
3373 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3374 * specified or not valid.
3376 *********************************************************************/
3377 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3378 struct http_response *rsp,
3379 const struct map *parameters)
3383 struct file_line * new_line;
3384 struct editable_file * file;
3385 struct file_line * cur_line;
3386 unsigned line_number;
3390 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3392 return cgi_error_disabled(csp, rsp);
3395 err = get_number_param(csp, parameters, "s", §ionid);
3402 return JB_ERR_CGI_PARAMS;
3405 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3411 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3414 /* No filename specified, can't read file, modified, or out of memory. */
3416 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3420 cur_line = file->lines;
3422 while ((cur_line != NULL) && (line_number < sectionid))
3424 cur_line = cur_line->next;
3428 if ( (cur_line == NULL)
3429 || (cur_line->type != FILE_LINE_ACTION))
3431 /* Invalid "sectionid" parameter */
3433 edit_free_file(file);
3434 return JB_ERR_CGI_PARAMS;
3437 /* At this point, the section header is in cur_line - add after this. */
3439 /* Allocate the new line */
3440 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3441 if (new_line == NULL)
3444 edit_free_file(file);
3445 return JB_ERR_MEMORY;
3448 /* Fill in the data members of the new line */
3449 new_line->raw = NULL;
3450 new_line->prefix = NULL;
3451 new_line->unprocessed = new_pattern;
3452 new_line->type = FILE_LINE_URL;
3454 /* Link new_line into the list, after cur_line */
3455 new_line->next = cur_line->next;
3456 cur_line->next = new_line;
3458 /* Done making changes, now commit */
3460 err = edit_write_file(file);
3463 /* Error writing file */
3464 edit_free_file(file);
3468 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3469 string_append(&target, file->identifier);
3471 edit_free_file(file);
3476 return JB_ERR_MEMORY;
3479 rsp->status = strdup("302 Local Redirect from Junkbuster");
3480 if (rsp->status == NULL)
3483 return JB_ERR_MEMORY;
3485 err = enlist_unique_header(rsp->headers, "Location", target);
3492 /*********************************************************************
3494 * Function : cgi_edit_actions_remove_url
3496 * Description : CGI function that actually removes a URL pattern from
3500 * 1 : csp = Current client state (buffers, headers, etc...)
3501 * 2 : rsp = http_response data structure for output
3502 * 3 : parameters = map of cgi parameters
3505 * f : (filename) Identifies the file to edit
3506 * v : (version) File's last-modified time
3507 * p : (pattern) Line number of pattern to remove
3509 * Returns : JB_ERR_OK on success
3510 * JB_ERR_MEMORY on out-of-memory
3511 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3512 * specified or not valid.
3514 *********************************************************************/
3515 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3516 struct http_response *rsp,
3517 const struct map *parameters)
3520 struct editable_file * file;
3521 struct file_line * cur_line;
3522 struct file_line * prev_line;
3523 unsigned line_number;
3527 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3529 return cgi_error_disabled(csp, rsp);
3532 err = get_number_param(csp, parameters, "p", &patternid);
3538 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3541 /* No filename specified, can't read file, modified, or out of memory. */
3542 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3547 cur_line = file->lines;
3549 while ((cur_line != NULL) && (line_number < patternid))
3551 prev_line = cur_line;
3552 cur_line = cur_line->next;
3556 if ( (cur_line == NULL)
3557 || (prev_line == NULL)
3558 || (cur_line->type != FILE_LINE_URL))
3560 /* Invalid "patternid" parameter */
3561 edit_free_file(file);
3562 return JB_ERR_CGI_PARAMS;
3565 /* At this point, the line to remove is in cur_line, and the previous
3566 * one is in prev_line
3569 /* Unlink cur_line */
3570 prev_line->next = cur_line->next;
3571 cur_line->next = NULL;
3574 edit_free_file_lines(cur_line);
3576 err = edit_write_file(file);
3579 /* Error writing file */
3580 edit_free_file(file);
3584 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3585 string_append(&target, file->identifier);
3587 edit_free_file(file);
3592 return JB_ERR_MEMORY;
3595 rsp->status = strdup("302 Local Redirect from Junkbuster");
3596 if (rsp->status == NULL)
3599 return JB_ERR_MEMORY;
3601 err = enlist_unique_header(rsp->headers, "Location", target);
3608 /*********************************************************************
3610 * Function : cgi_edit_actions_section_remove
3612 * Description : CGI function that actually removes a whole section from
3613 * the actions file. The section must be empty first
3614 * (else JB_ERR_CGI_PARAMS).
3617 * 1 : csp = Current client state (buffers, headers, etc...)
3618 * 2 : rsp = http_response data structure for output
3619 * 3 : parameters = map of cgi parameters
3622 * f : (filename) Identifies the file to edit
3623 * v : (version) File's last-modified time
3624 * s : (section) Line number of section to edit
3626 * Returns : JB_ERR_OK on success
3627 * JB_ERR_MEMORY on out-of-memory
3628 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3629 * specified or not valid.
3631 *********************************************************************/
3632 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3633 struct http_response *rsp,
3634 const struct map *parameters)
3637 struct editable_file * file;
3638 struct file_line * cur_line;
3639 struct file_line * prev_line;
3640 unsigned line_number;
3644 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3646 return cgi_error_disabled(csp, rsp);
3649 err = get_number_param(csp, parameters, "s", §ionid);
3655 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3658 /* No filename specified, can't read file, modified, or out of memory. */
3659 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3663 cur_line = file->lines;
3666 while ((cur_line != NULL) && (line_number < sectionid))
3668 prev_line = cur_line;
3669 cur_line = cur_line->next;
3673 if ( (cur_line == NULL)
3674 || (cur_line->type != FILE_LINE_ACTION) )
3676 /* Invalid "sectionid" parameter */
3677 edit_free_file(file);
3678 return JB_ERR_CGI_PARAMS;
3681 if ( (cur_line->next != NULL)
3682 && (cur_line->next->type == FILE_LINE_URL) )
3684 /* Section not empty. */
3685 edit_free_file(file);
3686 return JB_ERR_CGI_PARAMS;
3689 /* At this point, the line to remove is in cur_line, and the previous
3690 * one is in prev_line
3693 /* Unlink cur_line */
3694 if (prev_line == NULL)
3696 /* Removing the first line from the file */
3697 file->lines = cur_line->next;
3701 prev_line->next = cur_line->next;
3703 cur_line->next = NULL;
3706 edit_free_file_lines(cur_line);
3708 err = edit_write_file(file);
3711 /* Error writing file */
3712 edit_free_file(file);
3716 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3717 string_append(&target, file->identifier);
3719 edit_free_file(file);
3724 return JB_ERR_MEMORY;
3727 rsp->status = strdup("302 Local Redirect from Junkbuster");
3728 if (rsp->status == NULL)
3731 return JB_ERR_MEMORY;
3733 err = enlist_unique_header(rsp->headers, "Location", target);
3740 /*********************************************************************
3742 * Function : cgi_edit_actions_section_add
3744 * Description : CGI function that adds a new empty section to
3748 * 1 : csp = Current client state (buffers, headers, etc...)
3749 * 2 : rsp = http_response data structure for output
3750 * 3 : parameters = map of cgi parameters
3753 * f : (filename) Identifies the file to edit
3754 * v : (version) File's last-modified time
3755 * s : (section) Line number of section to add after, 0 for
3758 * Returns : JB_ERR_OK on success
3759 * JB_ERR_MEMORY on out-of-memory
3760 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3761 * specified or not valid.
3763 *********************************************************************/
3764 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3765 struct http_response *rsp,
3766 const struct map *parameters)
3769 struct file_line * new_line;
3771 struct editable_file * file;
3772 struct file_line * cur_line;
3773 unsigned line_number;
3777 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3779 return cgi_error_disabled(csp, rsp);
3782 err = get_number_param(csp, parameters, "s", §ionid);
3788 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3791 /* No filename specified, can't read file, modified, or out of memory. */
3792 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3796 cur_line = file->lines;
3800 /* Add to start of file */
3801 if (cur_line != NULL)
3803 /* There's something in the file, find the line before the first
3806 while ( (cur_line->next != NULL)
3807 && (cur_line->next->type != FILE_LINE_ACTION) )
3809 cur_line = cur_line->next;
3816 /* Add after stated section. */
3817 while ((cur_line != NULL) && (line_number < sectionid))
3819 cur_line = cur_line->next;
3823 if ( (cur_line == NULL)
3824 || (cur_line->type != FILE_LINE_ACTION))
3826 /* Invalid "sectionid" parameter */
3827 edit_free_file(file);
3828 return JB_ERR_CGI_PARAMS;
3831 /* Skip through the section to find the last line in it. */
3832 while ( (cur_line->next != NULL)
3833 && (cur_line->next->type != FILE_LINE_ACTION) )
3835 cur_line = cur_line->next;
3840 /* At this point, the last line in the previous section is in cur_line
3841 * - add after this. (Or if we need to add as the first line, cur_line
3845 new_text = strdup("{}");
3846 if (NULL == new_text)
3848 edit_free_file(file);
3849 return JB_ERR_MEMORY;
3852 /* Allocate the new line */
3853 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3854 if (new_line == NULL)
3857 edit_free_file(file);
3858 return JB_ERR_MEMORY;
3861 /* Fill in the data members of the new line */
3862 new_line->raw = NULL;
3863 new_line->prefix = NULL;
3864 new_line->unprocessed = new_text;
3865 new_line->type = FILE_LINE_ACTION;
3867 if (cur_line != NULL)
3869 /* Link new_line into the list, after cur_line */
3870 new_line->next = cur_line->next;
3871 cur_line->next = new_line;
3875 /* Link new_line into the list, as first line */
3876 new_line->next = file->lines;
3877 file->lines = new_line;
3880 /* Done making changes, now commit */
3882 err = edit_write_file(file);
3885 /* Error writing file */
3886 edit_free_file(file);
3890 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3891 string_append(&target, file->identifier);
3893 edit_free_file(file);
3898 return JB_ERR_MEMORY;
3901 rsp->status = strdup("302 Local Redirect from Junkbuster");
3902 if (rsp->status == NULL)
3905 return JB_ERR_MEMORY;
3907 err = enlist_unique_header(rsp->headers, "Location", target);
3914 /*********************************************************************
3916 * Function : cgi_edit_actions_section_swap
3918 * Description : CGI function that swaps the order of two sections
3919 * in the actions file. Note that this CGI can actually
3920 * swap any two arbitrary sections, but the GUI interface
3921 * currently only allows consecutive sections to be
3925 * 1 : csp = Current client state (buffers, headers, etc...)
3926 * 2 : rsp = http_response data structure for output
3927 * 3 : parameters = map of cgi parameters
3930 * f : (filename) Identifies the file to edit
3931 * v : (version) File's last-modified time
3932 * s1 : (section1) Line number of first section to swap
3933 * s2 : (section2) Line number of second section to swap
3935 * Returns : JB_ERR_OK on success
3936 * JB_ERR_MEMORY on out-of-memory
3937 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3938 * specified or not valid.
3940 *********************************************************************/
3941 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3942 struct http_response *rsp,
3943 const struct map *parameters)
3947 struct editable_file * file;
3948 struct file_line * cur_line;
3949 struct file_line * prev_line;
3950 struct file_line * line_before_section1;
3951 struct file_line * line_start_section1;
3952 struct file_line * line_end_section1;
3953 struct file_line * line_after_section1;
3954 struct file_line * line_before_section2;
3955 struct file_line * line_start_section2;
3956 struct file_line * line_end_section2;
3957 struct file_line * line_after_section2;
3958 unsigned line_number;
3962 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3964 return cgi_error_disabled(csp, rsp);
3967 err = get_number_param(csp, parameters, "s1", §ion1);
3968 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3974 if (section1 > section2)
3976 unsigned temp = section2;
3977 section2 = section1;
3981 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3984 /* No filename specified, can't read file, modified, or out of memory. */
3985 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3988 /* Start at the beginning... */
3990 cur_line = file->lines;
3993 /* ... find section1 ... */
3994 while ((cur_line != NULL) && (line_number < section1))
3996 prev_line = cur_line;
3997 cur_line = cur_line->next;
4001 if ( (cur_line == NULL)
4002 || (cur_line->type != FILE_LINE_ACTION) )
4004 /* Invalid "section1" parameter */
4005 edit_free_file(file);
4006 return JB_ERR_CGI_PARAMS;
4009 /* If no-op, we've validated params and can skip the rest. */
4010 if (section1 != section2)
4012 /* ... find the end of section1 ... */
4013 line_before_section1 = prev_line;
4014 line_start_section1 = cur_line;
4017 prev_line = cur_line;
4018 cur_line = cur_line->next;
4021 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4022 line_end_section1 = prev_line;
4023 line_after_section1 = cur_line;
4025 /* ... find section2 ... */
4026 while ((cur_line != NULL) && (line_number < section2))
4028 prev_line = cur_line;
4029 cur_line = cur_line->next;
4033 if ( (cur_line == NULL)
4034 || (cur_line->type != FILE_LINE_ACTION) )
4036 /* Invalid "section2" parameter */
4037 edit_free_file(file);
4038 return JB_ERR_CGI_PARAMS;
4041 /* ... find the end of section2 ... */
4042 line_before_section2 = prev_line;
4043 line_start_section2 = cur_line;
4046 prev_line = cur_line;
4047 cur_line = cur_line->next;
4050 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4051 line_end_section2 = prev_line;
4052 line_after_section2 = cur_line;
4054 /* Now have all the pointers we need. Do the swap. */
4056 /* Change the pointer to section1 to point to section2 instead */
4057 if (line_before_section1 == NULL)
4059 file->lines = line_start_section2;
4063 line_before_section1->next = line_start_section2;
4066 if (line_before_section2 == line_end_section1)
4068 /* Consecutive sections */
4069 line_end_section2->next = line_start_section1;
4073 line_end_section2->next = line_after_section1;
4074 line_before_section2->next = line_start_section1;
4077 /* Set the pointer from the end of section1 to the rest of the file */
4078 line_end_section1->next = line_after_section2;
4080 err = edit_write_file(file);
4083 /* Error writing file */
4084 edit_free_file(file);
4087 } /* END if (section1 != section2) */
4089 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4090 string_append(&target, file->identifier);
4092 edit_free_file(file);
4097 return JB_ERR_MEMORY;
4100 rsp->status = strdup("302 Local Redirect from Junkbuster");
4101 if (rsp->status == NULL)
4104 return JB_ERR_MEMORY;
4106 err = enlist_unique_header(rsp->headers, "Location", target);
4113 /*********************************************************************
4115 * Function : cgi_toggle
4117 * Description : CGI function that adds a new empty section to
4121 * 1 : csp = Current client state (buffers, headers, etc...)
4122 * 2 : rsp = http_response data structure for output
4123 * 3 : parameters = map of cgi parameters
4126 * set : If present, how to change toggle setting:
4127 * "enable", "disable", "toggle", or none (default).
4128 * mini : If present, use mini reply template.
4130 * Returns : JB_ERR_OK on success
4131 * JB_ERR_MEMORY on out-of-memory
4133 *********************************************************************/
4134 jb_err cgi_toggle(struct client_state *csp,
4135 struct http_response *rsp,
4136 const struct map *parameters)
4138 struct map *exports;
4140 const char *template_name;
4147 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4149 return cgi_error_disabled(csp, rsp);
4152 if (NULL == (exports = default_exports(csp, "toggle")))
4154 return JB_ERR_MEMORY;
4157 mode = get_char_param(parameters, "set");
4164 else if (mode == 'D')
4169 else if (mode == 'T')
4172 g_bToggleIJB = !g_bToggleIJB;
4175 err = map_conditional(exports, "enabled", g_bToggleIJB);
4182 template_name = (get_char_param(parameters, "mini")
4186 return template_fill_for_cgi(csp, template_name, exports, rsp);
4190 /*********************************************************************
4192 * Function : javascriptify
4194 * Description : Converts a string into a form JavaScript will like.
4196 * Netscape 4's JavaScript sucks - it doesn't use
4197 * "id" parameters, so you have to set the "name"
4198 * used to submit a form element to something JavaScript
4199 * will like. (Or access the elements by index in an
4200 * array. That array contains >60 elements and will
4201 * be changed whenever we add a new action to the
4202 * editor, so I'm NOT going to use indexes that have
4203 * to be figured out by hand.)
4205 * Currently the only thing we have to worry about
4206 * is "-" ==> "_" conversion.
4208 * This is a length-preserving operation so it is
4209 * carried out in-place, no memory is allocated
4213 * 1 : identifier = String to make JavaScript-friendly.
4217 *********************************************************************/
4218 static void javascriptify(char * identifier)
4220 char * p = identifier;
4221 while (NULL != (p = strchr(p, '-')))
4228 /*********************************************************************
4230 * Function : actions_to_radio
4232 * Description : Converts a actionsfile entry into settings for
4233 * radio buttons and edit boxes on a HTML form.
4236 * 1 : exports = List of substitutions to add to.
4237 * 2 : action = Action to read
4239 * Returns : JB_ERR_OK on success
4240 * JB_ERR_MEMORY on out-of-memory
4242 *********************************************************************/
4243 static jb_err actions_to_radio(struct map * exports,
4244 const struct action_spec *action)
4246 unsigned mask = action->mask;
4247 unsigned add = action->add;
4255 mask = action->mask;
4258 /* sanity - prevents "-feature +feature" */
4262 #define DEFINE_ACTION_BOOL(name, bit) \
4263 if (!(mask & bit)) \
4265 current_mode = 'n'; \
4267 else if (add & bit) \
4269 current_mode = 'y'; \
4273 current_mode = 'x'; \
4275 if (map_radio(exports, name, "ynx", current_mode)) \
4277 return JB_ERR_MEMORY; \
4280 #define DEFINE_ACTION_STRING(name, bit, index) \
4281 DEFINE_ACTION_BOOL(name, bit); \
4284 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4287 checked = !strcmp(action->string[index], value); \
4291 checked = is_default; \
4293 mapped_param |= checked; \
4294 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4296 return JB_ERR_MEMORY; \
4299 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4300 if (map(exports, name "-param-custom", 1, \
4301 ((!mapped_param) ? "checked" : ""), 1)) \
4303 return JB_ERR_MEMORY; \
4305 if (map(exports, name "-param", 1, \
4306 (((add & bit) && !mapped_param) ? \
4307 action->string[index] : default_val), 1)) \
4309 return JB_ERR_MEMORY; \
4312 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4313 if (map(exports, name "-param", 1, \
4314 ((add & bit) ? action->string[index] : default_val), 1)) \
4316 return JB_ERR_MEMORY; \
4319 #define DEFINE_ACTION_MULTI(name, index) \
4320 if (action->multi_add[index]->first) \
4322 current_mode = 'y'; \
4324 else if (action->multi_remove_all[index]) \
4326 current_mode = 'n'; \
4328 else if (action->multi_remove[index]->first) \
4330 current_mode = 'y'; \
4334 current_mode = 'x'; \
4336 if (map_radio(exports, name, "ynx", current_mode)) \
4338 return JB_ERR_MEMORY; \
4341 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4343 #include "actionlist.h"
4345 #undef DEFINE_ACTION_MULTI
4346 #undef DEFINE_ACTION_STRING
4347 #undef DEFINE_ACTION_BOOL
4348 #undef DEFINE_ACTION_ALIAS
4349 #undef DEFINE_CGI_PARAM_CUSTOM
4350 #undef DEFINE_CGI_PARAM_RADIO
4351 #undef DEFINE_CGI_PARAM_NO_RADIO
4357 /*********************************************************************
4359 * Function : actions_from_radio
4361 * Description : Converts a map of parameters passed to a CGI function
4362 * into an actionsfile entry.
4365 * 1 : parameters = parameters to the CGI call
4366 * 2 : action = Action to change. Must be valid before
4367 * the call, actions not specified will be
4370 * Returns : JB_ERR_OK on success
4371 * JB_ERR_MEMORY on out-of-memory
4373 *********************************************************************/
4374 static jb_err actions_from_radio(const struct map * parameters,
4375 struct action_spec *action)
4377 static int first_time = 1;
4381 const char * js_name;
4382 jb_err err = JB_ERR_OK;
4387 /* Statics are generally a potential race condition,
4388 * but in this case we're safe and don't need semaphores.
4389 * Be careful if you modify this function.
4393 #define JAVASCRIPTIFY(dest_var, string) \
4395 static char js_name_arr[] = string; \
4398 javascriptify(js_name_arr); \
4400 dest_var = js_name_arr; \
4403 #define DEFINE_ACTION_BOOL(name, bit) \
4404 JAVASCRIPTIFY(js_name, name); \
4405 ch = get_char_param(parameters, js_name); \
4408 action->add |= bit; \
4409 action->mask |= bit; \
4411 else if (ch == 'N') \
4413 action->add &= ~bit; \
4414 action->mask &= ~bit; \
4416 else if (ch == 'X') \
4418 action->add &= ~bit; \
4419 action->mask |= bit; \
4422 #define DEFINE_ACTION_STRING(name, bit, index) \
4423 JAVASCRIPTIFY(js_name, name); \
4424 ch = get_char_param(parameters, js_name); \
4428 JAVASCRIPTIFY(js_name, name "-mode"); \
4429 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4430 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4432 JAVASCRIPTIFY(js_name, name "-param"); \
4433 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4435 if (param != NULL) \
4437 if (NULL == (param_dup = strdup(param))) \
4439 return JB_ERR_MEMORY; \
4441 freez(action->string[index]); \
4442 action->add |= bit; \
4443 action->mask |= bit; \
4444 action->string[index] = param_dup; \
4447 else if (ch == 'N') \
4449 if (action->add & bit) \
4451 freez(action->string[index]); \
4453 action->add &= ~bit; \
4454 action->mask &= ~bit; \
4456 else if (ch == 'X') \
4458 if (action->add & bit) \
4460 freez(action->string[index]); \
4462 action->add &= ~bit; \
4463 action->mask |= bit; \
4466 #define DEFINE_ACTION_MULTI(name, index) \
4467 JAVASCRIPTIFY(js_name, name); \
4468 ch = get_char_param(parameters, js_name); \
4473 else if (ch == 'N') \
4475 list_remove_all(action->multi_add[index]); \
4476 list_remove_all(action->multi_remove[index]); \
4477 action->multi_remove_all[index] = 1; \
4479 else if (ch == 'X') \
4481 list_remove_all(action->multi_add[index]); \
4482 list_remove_all(action->multi_remove[index]); \
4483 action->multi_remove_all[index] = 0; \
4486 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4488 #include "actionlist.h"
4490 #undef DEFINE_ACTION_MULTI
4491 #undef DEFINE_ACTION_STRING
4492 #undef DEFINE_ACTION_BOOL
4493 #undef DEFINE_ACTION_ALIAS
4494 #undef JAVASCRIPTIFY
4502 #endif /* def FEATURE_CGI_EDIT_ACTIONS */