1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.18 2002/03/16 14:57:44 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
10 * NOTE: The CGIs in this file use parameter names
11 * such as "f" and "s" which are really *BAD* choices.
12 * However, I'm trying to save bytes in the
13 * edit-actions-list HTML page - the standard actions
14 * file generated a 550kbyte page, which is ridiculous.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001 the SourceForge
19 * 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.18 2002/03/16 14:57:44 jongfoster
46 * Full support for enabling/disabling modular filters.
48 * Revision 1.17 2002/03/16 14:26:42 jongfoster
49 * First version of modular filters support - READ ONLY!
50 * Fixing a double-free bug in the out-of-memory handling in map_radio().
52 * Revision 1.16 2002/03/07 03:46:17 oes
53 * Fixed compiler warnings
55 * Revision 1.15 2002/03/06 22:54:35 jongfoster
56 * Automated function-comment nitpicking.
58 * Revision 1.14 2002/03/05 00:24:51 jongfoster
59 * Patch to always edit the current actions file.
61 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
62 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
64 * Revision 1.12 2002/03/03 09:18:03 joergs
65 * Made jumbjuster work on AmigaOS again.
67 * Revision 1.11 2002/01/23 01:03:31 jongfoster
68 * Fixing gcc [CygWin] compiler warnings
70 * Revision 1.10 2002/01/23 00:22:59 jongfoster
71 * Adding new function cgi_edit_actions_section_swap(), to reorder
74 * Adding get_url_spec_param() to get a validated URL pattern.
76 * Moving edit_read_line() out of this file and into loaders.c.
78 * Adding missing html_encode() to many CGI functions.
80 * Moving the functions that #include actionlist.h to the end of the file,
81 * because the Visual C++ 97 debugger gets extremely confused if you try
82 * to debug any code that comes after them in the file.
84 * Major optimizations in cgi_edit_actions_list() to reduce the size of
85 * the generated HTML (down 40% from 550k to 304k), with major side-effects
86 * throughout the editor and templates. In particular, the length of the
87 * URLs throughout the editor has been drastically reduced, by cutting
88 * paramater names down to 1 character and CGI names down to 3-4
89 * characters, by removing all non-essential CGI paramaters even at the
90 * expense of having to re-read the actions file for the most trivial
91 * page, and by using relative rather than absolute URLs. This means
92 * that this (typical example):
94 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
95 * filename=ijb&ver=1011487572&section=12&pattern=13
96 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
100 * <a href="eau?f=ijb&v=1011487572&p=13">
102 * Revision 1.9 2002/01/17 20:56:22 jongfoster
103 * Replacing hard references to the URL of the config interface
104 * with #defines from project.h
106 * Revision 1.8 2001/11/30 23:35:51 jongfoster
107 * Renaming actionsfile to ijb.action
109 * Revision 1.7 2001/11/13 00:28:24 jongfoster
110 * - Renaming parameters from edit-actions-for-url so that they only
111 * contain legal JavaScript characters. If we wanted to write
112 * JavaScript that worked with Netscape 4, this is nessacery.
113 * (Note that at the moment the JavaScript doesn't actually work
114 * with Netscape 4, but now this is purely a template issue, not
115 * one affecting code).
116 * - Adding new CGIs for use by non-JavaScript browsers:
117 * edit-actions-url-form
118 * edit-actions-add-url-form
119 * edit-actions-remove-url-form
122 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
123 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
124 * by and __OS2__ ifdef.
126 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
127 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
128 * threads to call select() simultaneously. So, it's time to do a real, live,
129 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
130 * (native). Both versions will work, but using __OS2__ offers multi-threading.
132 * Revision 1.4 2001/10/23 21:48:19 jongfoster
133 * Cleaning up error handling in CGI functions - they now send back
134 * a HTML error page and should never cause a FATAL error. (Fixes one
135 * potential source of "denial of service" attacks).
137 * CGI actions file editor that works and is actually useful.
139 * Ability to toggle JunkBuster remotely using a CGI call.
141 * You can turn off both the above features in the main configuration
142 * file, e.g. if you are running a multi-user proxy.
144 * Revision 1.3 2001/10/14 22:12:49 jongfoster
145 * New version of CGI-based actionsfile editor.
146 * Major changes, including:
147 * - Completely new file parser and file output routines
148 * - edit-actions CGI renamed edit-actions-for-url
149 * - All CGIs now need a filename parameter, except for...
150 * - New CGI edit-actions which doesn't need a filename,
151 * to allow you to start the editor up.
152 * - edit-actions-submit now works, and now automatically
153 * redirects you back to the main edit-actions-list handler.
155 * Revision 1.2 2001/09/16 17:05:14 jongfoster
156 * Removing unused #include showarg.h
158 * Revision 1.1 2001/09/16 15:47:37 jongfoster
159 * First version of CGI-based edit interface. This is very much a
160 * work-in-progress, and you can't actually use it to edit anything
161 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
162 * to have any effect.
165 **********************************************************************/
171 * FIXME: Following includes copied from cgi.c - which are actually needed?
176 #include <sys/types.h>
181 #include <sys/stat.h>
184 #define snprintf _snprintf
185 #endif /* def _WIN32 */
190 #include "cgisimple.h"
194 #include "miscutil.h"
198 /* loadcfg.h is for g_bToggleIJB only */
199 #include "urlmatch.h"
201 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
204 #ifdef FEATURE_CGI_EDIT_ACTIONS
208 struct file_line * next;
216 struct action_spec action[1];
225 /* Add more data types here... e.g.
228 struct url_spec url[1];
232 struct action_spec action[1];
241 #define FILE_LINE_UNPROCESSED 1
242 #define FILE_LINE_BLANK 2
243 #define FILE_LINE_ALIAS_HEADER 3
244 #define FILE_LINE_ALIAS_ENTRY 4
245 #define FILE_LINE_ACTION 5
246 #define FILE_LINE_URL 6
247 #define FILE_LINE_SETTINGS_HEADER 7
248 #define FILE_LINE_SETTINGS_ENTRY 8
249 #define FILE_LINE_DESCRIPTION_HEADER 9
250 #define FILE_LINE_DESCRIPTION_ENTRY 10
255 struct file_line * lines;
256 const char * filename; /* Full pathname - e.g. "/etc/junkbuster/wibble.action" */
257 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
258 /* Pre-encoded with url_encode() for ease of use. */
259 const char * version_str; /* Last modification time, as a string. For CGI param */
260 /* Can be used in URL without using url_param(). */
261 unsigned version; /* Last modification time - prevents chaos with
262 * the browser's "back" button. Note that this is a
263 * time_t cast to an unsigned. When comparing, always
264 * cast the time_t to an unsigned, and *NOT* vice-versa.
265 * This may lose the top few bits, but they're not
266 * significant anyway.
268 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
269 * Note that changing this after the file has been
270 * read in will cause a mess.
272 struct file_line * parse_error; /* On parse error, this is the offending line. */
273 const char * parse_error_text; /* On parse error, this is the problem.
274 * (Statically allocated) */
277 #define CGI_ACTION_PARAM_LEN_MAX 500
279 /* FIXME: Following non-static functions should be prototyped in .h or made static */
281 /* Functions to read and write arbitrary config files */
282 jb_err edit_read_file(struct client_state *csp,
283 const struct map *parameters,
286 struct editable_file **pfile);
287 jb_err edit_write_file(struct editable_file * file);
288 void edit_free_file(struct editable_file * file);
290 /* Functions to read and write actions files */
291 jb_err edit_parse_actions_file(struct editable_file * file);
292 jb_err edit_read_actions_file(struct client_state *csp,
293 struct http_response *rsp,
294 const struct map *parameters,
296 struct editable_file **pfile);
299 jb_err cgi_error_modified(struct client_state *csp,
300 struct http_response *rsp,
301 const char *filename);
302 jb_err cgi_error_parse(struct client_state *csp,
303 struct http_response *rsp,
304 struct editable_file *file);
305 jb_err cgi_error_file(struct client_state *csp,
306 struct http_response *rsp,
307 const char *filename);
308 jb_err cgi_error_disabled(struct client_state *csp,
309 struct http_response *rsp);
311 /* Internal arbitrary config file support functions */
312 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
313 static void edit_free_file_lines(struct file_line * first_line);
315 /* Internal actions file support functions */
316 static int match_actions_file_header_line(const char * line, const char * name);
317 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
319 /* Internal parameter parsing functions */
320 static jb_err get_file_name_param(struct client_state *csp,
321 const struct map *parameters,
322 const char *param_name,
325 const char **pparam);
326 static jb_err get_number_param(struct client_state *csp,
327 const struct map *parameters,
330 static jb_err get_url_spec_param(struct client_state *csp,
331 const struct map *parameters,
334 static jb_err get_string_param(const struct map *parameters,
335 const char *param_name,
336 const char **pparam);
338 /* Internal actionsfile <==> HTML conversion functions */
339 static jb_err map_radio(struct map * exports,
340 const char * optionname,
343 static jb_err actions_to_radio(struct map * exports,
344 const struct action_spec *action);
345 static jb_err actions_from_radio(const struct map * parameters,
346 struct action_spec *action);
349 static jb_err map_copy_parameter_html(struct map *out,
350 const struct map *in,
352 #if 0 /* unused function */
353 static jb_err map_copy_parameter_url(struct map *out,
354 const struct map *in,
356 #endif /* unused function */
358 /*********************************************************************
360 * Function : map_copy_parameter_html
362 * Description : Copy a CGI parameter from one map to another, HTML
366 * 1 : out = target map
367 * 2 : in = source map
368 * 3 : name = name of cgi parameter to copy
370 * Returns : JB_ERR_OK on success
371 * JB_ERR_MEMORY on out-of-memory
372 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
375 *********************************************************************/
376 static jb_err map_copy_parameter_html(struct map *out,
377 const struct map *in,
387 value = lookup(in, name);
388 err = map(out, name, 1, html_encode(value), 0);
395 else if (*value == '\0')
397 return JB_ERR_CGI_PARAMS;
406 #if 0 /* unused function */
407 /*********************************************************************
409 * Function : map_copy_parameter_html
411 * Description : Copy a CGI parameter from one map to another, URL
415 * 1 : out = target map
416 * 2 : in = source map
417 * 3 : name = name of cgi parameter to copy
419 * Returns : JB_ERR_OK on success
420 * JB_ERR_MEMORY on out-of-memory
421 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
424 *********************************************************************/
425 static jb_err map_copy_parameter_url(struct map *out,
426 const struct map *in,
436 value = lookup(in, name);
437 err = map(out, name, 1, url_encode(value), 0);
444 else if (*value == '\0')
446 return JB_ERR_CGI_PARAMS;
453 #endif /* 0 - unused function */
455 /*********************************************************************
457 * Function : cgi_edit_actions_url_form
459 * Description : CGI function that displays a form for
463 * 1 : csp = Current client state (buffers, headers, etc...)
464 * 2 : rsp = http_response data structure for output
465 * 3 : parameters = map of cgi parameters
468 * f : (filename) Identifies the file to edit
469 * v : (version) File's last-modified time
470 * p : (pattern) Line number of pattern to edit
472 * Returns : JB_ERR_OK on success
473 * JB_ERR_MEMORY on out-of-memory
474 * JB_ERR_CGI_PARAMS if the CGI parameters are not
475 * specified or not valid.
477 *********************************************************************/
478 jb_err cgi_edit_actions_url_form(struct client_state *csp,
479 struct http_response *rsp,
480 const struct map *parameters)
482 struct map * exports;
484 struct editable_file * file;
485 struct file_line * cur_line;
486 unsigned line_number;
493 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
495 return cgi_error_disabled(csp, rsp);
498 err = get_number_param(csp, parameters, "p", &patternid);
504 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
507 /* No filename specified, can't read file, modified, or out of memory. */
508 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
511 cur_line = file->lines;
513 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
515 cur_line = cur_line->next;
518 if ( (cur_line == NULL)
519 || (line_number != patternid)
521 || (cur_line->type != FILE_LINE_URL))
523 /* Invalid "patternid" parameter */
524 edit_free_file(file);
525 return JB_ERR_CGI_PARAMS;
528 if (NULL == (exports = default_exports(csp, NULL)))
530 edit_free_file(file);
531 return JB_ERR_MEMORY;
534 err = map(exports, "f", 1, file->identifier, 1);
535 if (!err) err = map(exports, "v", 1, file->version_str, 1);
536 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
537 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
539 edit_free_file(file);
547 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
551 /*********************************************************************
553 * Function : cgi_edit_actions_add_url_form
555 * Description : CGI function that displays a form for
559 * 1 : csp = Current client state (buffers, headers, etc...)
560 * 2 : rsp = http_response data structure for output
561 * 3 : parameters = map of cgi parameters
564 * f : (filename) Identifies the file to edit
565 * v : (version) File's last-modified time
566 * s : (section) Line number of section to edit
568 * Returns : JB_ERR_OK on success
569 * JB_ERR_MEMORY on out-of-memory
570 * JB_ERR_CGI_PARAMS if the CGI parameters are not
571 * specified or not valid.
573 *********************************************************************/
574 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
575 struct http_response *rsp,
576 const struct map *parameters)
585 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
587 return cgi_error_disabled(csp, rsp);
590 if (NULL == (exports = default_exports(csp, NULL)))
592 return JB_ERR_MEMORY;
595 err = map_copy_parameter_html(exports, parameters, "f");
596 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
597 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
605 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
609 /*********************************************************************
611 * Function : cgi_edit_actions_remove_url_form
613 * Description : CGI function that displays a form for
617 * 1 : csp = Current client state (buffers, headers, etc...)
618 * 2 : rsp = http_response data structure for output
619 * 3 : parameters = map of cgi parameters
622 * f : (filename) Identifies the file to edit
623 * v : (version) File's last-modified time
624 * p : (pattern) Line number of pattern to edit
626 * Returns : JB_ERR_OK on success
627 * JB_ERR_MEMORY on out-of-memory
628 * JB_ERR_CGI_PARAMS if the CGI parameters are not
629 * specified or not valid.
631 *********************************************************************/
632 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
633 struct http_response *rsp,
634 const struct map *parameters)
636 struct map * exports;
638 struct editable_file * file;
639 struct file_line * cur_line;
640 unsigned line_number;
647 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
649 return cgi_error_disabled(csp, rsp);
652 err = get_number_param(csp, parameters, "p", &patternid);
658 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
661 /* No filename specified, can't read file, modified, or out of memory. */
662 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
665 cur_line = file->lines;
667 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
669 cur_line = cur_line->next;
672 if ( (cur_line == NULL)
673 || (line_number != patternid)
675 || (cur_line->type != FILE_LINE_URL))
677 /* Invalid "patternid" parameter */
678 edit_free_file(file);
679 return JB_ERR_CGI_PARAMS;
682 if (NULL == (exports = default_exports(csp, NULL)))
684 edit_free_file(file);
685 return JB_ERR_MEMORY;
688 err = map(exports, "f", 1, file->identifier, 1);
689 if (!err) err = map(exports, "v", 1, file->version_str, 1);
690 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
691 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
693 edit_free_file(file);
701 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
705 /*********************************************************************
707 * Function : edit_write_file
709 * Description : Write a complete file to disk.
712 * 1 : filename = File to write to.
713 * 2 : file = Data structure to write.
715 * Returns : JB_ERR_OK on success
716 * JB_ERR_FILE on error writing to file.
717 * JB_ERR_MEMORY on out of memory
719 *********************************************************************/
720 jb_err edit_write_file(struct editable_file * file)
723 struct file_line * cur_line;
724 struct stat statbuf[1];
725 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
726 digits in time_t, assuming this is a 64-bit
727 machine, plus null terminator, plus one
731 assert(file->filename);
733 #if defined(AMIGA) || defined(__OS2__)
734 if (NULL == (fp = fopen(file->filename, "w")))
736 if (NULL == (fp = fopen(file->filename, "wt")))
737 #endif /* def AMIGA */
742 cur_line = file->lines;
743 while (cur_line != NULL)
747 if (fputs(cur_line->raw, fp) < 0)
755 if (cur_line->prefix)
757 if (fputs(cur_line->prefix, fp) < 0)
763 if (cur_line->unprocessed)
765 /* This should be a single line - sanity check. */
766 assert(NULL == strchr(cur_line->unprocessed, '\r'));
767 assert(NULL == strchr(cur_line->unprocessed, '\n'));
769 if (NULL != strchr(cur_line->unprocessed, '#'))
771 /* Must quote '#' characters */
778 /* Count number of # characters, so we know length of output string */
779 src = cur_line->unprocessed;
780 while (NULL != (src = strchr(src, '#')))
787 /* Allocate new memory for string */
788 len = strlen(cur_line->unprocessed);
789 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
791 /* Uh oh, just trashed file! */
793 return JB_ERR_MEMORY;
796 /* Loop through string from end */
797 src = cur_line->unprocessed + len;
798 dest = str + len + numhash;
799 for ( ; len >= 0; len--)
801 if ((*dest-- = *src--) == '#')
805 assert(numhash >= 0);
808 assert(numhash == 0);
809 assert(src + 1 == cur_line->unprocessed);
810 assert(dest + 1 == str);
812 if (fputs(str, fp) < 0)
823 /* Can write without quoting '#' characters. */
824 if (fputs(cur_line->unprocessed, fp) < 0)
830 if (fputs(NEWLINE(file->newline), fp) < 0)
838 /* FIXME: Write data from file->data->whatever */
842 cur_line = cur_line->next;
848 /* Update the version stamp in the file structure, since we just
849 * wrote to the file & changed it's date.
851 if (stat(file->filename, statbuf) < 0)
853 /* Error, probably file not found. */
856 file->version = (unsigned)statbuf->st_mtime;
858 /* Correct file->version_str */
859 freez(file->version_str);
860 snprintf(version_buf, 22, "%u", file->version);
861 version_buf[21] = '\0';
862 file->version_str = strdup(version_buf);
863 if (version_buf == NULL)
865 return JB_ERR_MEMORY;
872 /*********************************************************************
874 * Function : edit_free_file
876 * Description : Free a complete file in memory.
879 * 1 : file = Data structure to free.
883 *********************************************************************/
884 void edit_free_file(struct editable_file * file)
888 /* Silently ignore NULL pointer */
892 edit_free_file_lines(file->lines);
893 freez(file->filename);
894 freez(file->identifier);
895 freez(file->version_str);
897 file->parse_error_text = NULL; /* Statically allocated */
898 file->parse_error = NULL;
904 /*********************************************************************
906 * Function : edit_free_file
908 * Description : Free an entire linked list of file lines.
911 * 1 : first_line = Data structure to free.
915 *********************************************************************/
916 static void edit_free_file_lines(struct file_line * first_line)
918 struct file_line * next_line;
920 while (first_line != NULL)
922 next_line = first_line->next;
923 first_line->next = NULL;
924 freez(first_line->raw);
925 freez(first_line->prefix);
926 freez(first_line->unprocessed);
927 switch(first_line->type)
929 case 0: /* special case if memory zeroed */
930 case FILE_LINE_UNPROCESSED:
931 case FILE_LINE_BLANK:
932 case FILE_LINE_ALIAS_HEADER:
933 case FILE_LINE_SETTINGS_HEADER:
934 case FILE_LINE_DESCRIPTION_HEADER:
935 case FILE_LINE_DESCRIPTION_ENTRY:
936 case FILE_LINE_ALIAS_ENTRY:
938 /* No data is stored for these */
941 case FILE_LINE_ACTION:
942 free_action(first_line->data.action);
945 case FILE_LINE_SETTINGS_ENTRY:
946 freez(first_line->data.setting.name);
947 freez(first_line->data.setting.svalue);
950 /* Should never happen */
954 first_line->type = 0; /* paranoia */
956 first_line = next_line;
961 /*********************************************************************
963 * Function : match_actions_file_header_line
965 * Description : Match an actions file {{header}} line
968 * 1 : line = String from file
969 * 2 : name = Header to match against
971 * Returns : 0 iff they match.
973 *********************************************************************/
974 static int match_actions_file_header_line(const char * line, const char * name)
982 if ((line[0] != '{') || (line[1] != '{'))
988 /* Look for optional whitespace */
989 while ( (*line == ' ') || (*line == '\t') )
994 /* Look for the specified name (case-insensitive) */
996 if (0 != strncmpic(line, name, len))
1002 /* Look for optional whitespace */
1003 while ( (*line == ' ') || (*line == '\t') )
1008 /* Look for "}}" and end of string*/
1009 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1019 /*********************************************************************
1021 * Function : match_actions_file_header_line
1023 * Description : Match an actions file {{header}} line
1026 * 1 : line = String from file. Must not start with
1027 * whitespace (else infinite loop!)
1028 * 2 : name = Destination for name
1029 * 2 : name = Destination for value
1031 * Returns : JB_ERR_OK on success
1032 * JB_ERR_MEMORY on out-of-memory
1033 * JB_ERR_PARSE if there's no "=" sign, or if there's
1034 * nothing before the "=" sign (but empty
1035 * values *after* the "=" sign are legal).
1037 *********************************************************************/
1038 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1040 const char * name_end;
1041 const char * value_start;
1047 assert(*line != ' ');
1048 assert(*line != '\t');
1053 value_start = strchr(line, '=');
1054 if ((value_start == NULL) || (value_start == line))
1056 return JB_ERR_PARSE;
1059 name_end = value_start - 1;
1061 /* Eat any whitespace before the '=' */
1062 while ((*name_end == ' ') || (*name_end == '\t'))
1065 * we already know we must have at least 1 non-ws char
1066 * at start of buf - no need to check
1071 name_len = name_end - line + 1; /* Length excluding \0 */
1072 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1074 return JB_ERR_MEMORY;
1076 strncpy(*pname, line, name_len);
1077 (*pname)[name_len] = '\0';
1079 /* Eat any the whitespace after the '=' */
1081 while ((*value_start == ' ') || (*value_start == '\t'))
1086 if (NULL == (*pvalue = strdup(value_start)))
1090 return JB_ERR_MEMORY;
1097 /*********************************************************************
1099 * Function : edit_parse_actions_file
1101 * Description : Parse an actions file in memory.
1103 * Passed linked list must have the "data" member
1104 * zeroed, and must contain valid "next" and
1105 * "unprocessed" fields. The "raw" and "prefix"
1106 * fields are ignored, and "type" is just overwritten.
1108 * Note that on error the file may have been
1112 * 1 : file = Actions file to be parsed in-place.
1114 * Returns : JB_ERR_OK on success
1115 * JB_ERR_MEMORY on out-of-memory
1116 * JB_ERR_PARSE on error
1118 *********************************************************************/
1119 jb_err edit_parse_actions_file(struct editable_file * file)
1121 struct file_line * cur_line;
1123 const char * text; /* Text from a line */
1124 char * name; /* For lines of the form name=value */
1125 char * value; /* For lines of the form name=value */
1126 struct action_alias * alias_list = NULL;
1127 jb_err err = JB_ERR_OK;
1129 /* alias_list contains the aliases defined in this file.
1130 * It might be better to use the "file_line.data" fields
1131 * in the relavent places instead.
1134 cur_line = file->lines;
1136 /* A note about blank line support: Blank lines should only
1137 * ever occur as the last line in the file. This function
1138 * is more forgiving than that - FILE_LINE_BLANK can occur
1142 /* Skip leading blanks. Should only happen if file is
1143 * empty (which is valid, but pointless).
1145 while ( (cur_line != NULL)
1146 && (cur_line->unprocessed[0] == '\0') )
1149 cur_line->type = FILE_LINE_BLANK;
1150 cur_line = cur_line->next;
1153 if ( (cur_line != NULL)
1154 && (cur_line->unprocessed[0] != '{') )
1156 /* File doesn't start with a header */
1157 file->parse_error = cur_line;
1158 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1159 return JB_ERR_PARSE;
1162 if ( (cur_line != NULL) && (0 ==
1163 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1165 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1167 cur_line = cur_line->next;
1168 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1170 if (cur_line->unprocessed[0])
1172 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1174 err = split_line_on_equals(cur_line->unprocessed,
1175 &cur_line->data.setting.name,
1176 &cur_line->data.setting.svalue);
1177 if (err == JB_ERR_MEMORY)
1181 else if (err != JB_ERR_OK)
1183 /* Line does not contain a name=value pair */
1184 file->parse_error = cur_line;
1185 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1186 return JB_ERR_PARSE;
1191 cur_line->type = FILE_LINE_BLANK;
1193 cur_line = cur_line->next;
1197 if ( (cur_line != NULL) && (0 ==
1198 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1200 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1202 cur_line = cur_line->next;
1203 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1205 if (cur_line->unprocessed[0])
1207 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1211 cur_line->type = FILE_LINE_BLANK;
1213 cur_line = cur_line->next;
1217 if ( (cur_line != NULL) && (0 ==
1218 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1220 cur_line->type = FILE_LINE_ALIAS_HEADER;
1222 cur_line = cur_line->next;
1223 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1225 if (cur_line->unprocessed[0])
1227 /* define an alias */
1228 struct action_alias * new_alias;
1230 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1232 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1233 if (err == JB_ERR_MEMORY)
1237 else if (err != JB_ERR_OK)
1239 /* Line does not contain a name=value pair */
1240 file->parse_error = cur_line;
1241 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1242 return JB_ERR_PARSE;
1245 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1250 free_alias_list(alias_list);
1251 return JB_ERR_MEMORY;
1254 err = get_actions(value, alias_list, new_alias->action);
1257 /* Invalid action or out of memory */
1261 free_alias_list(alias_list);
1262 if (err == JB_ERR_MEMORY)
1268 /* Line does not contain a name=value pair */
1269 file->parse_error = cur_line;
1270 file->parse_error_text = "This alias does not specify a valid set of actions.";
1271 return JB_ERR_PARSE;
1277 new_alias->name = name;
1280 new_alias->next = alias_list;
1281 alias_list = new_alias;
1285 cur_line->type = FILE_LINE_BLANK;
1287 cur_line = cur_line->next;
1291 /* Header done, process the main part of the file */
1292 while (cur_line != NULL)
1294 /* At this point, (cur_line->unprocessed[0] == '{') */
1295 assert(cur_line->unprocessed[0] == '{');
1296 text = cur_line->unprocessed + 1;
1297 len = strlen(text) - 1;
1298 if (text[len] != '}')
1300 /* No closing } on header */
1301 free_alias_list(alias_list);
1302 file->parse_error = cur_line;
1303 file->parse_error_text = "Headers starting with '{' must have a "
1304 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1305 "must close with two brackets ('}}').";
1306 return JB_ERR_PARSE;
1311 /* An invalid {{ header. */
1312 free_alias_list(alias_list);
1313 file->parse_error = cur_line;
1314 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1315 "Please remember that the system (two-bracket) headers must "
1316 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1317 "and must appear before any actions (one-bracket) headers. "
1318 "Also note that system headers may not be repeated.";
1319 return JB_ERR_PARSE;
1322 while ( (*text == ' ') || (*text == '\t') )
1328 && ( (text[len - 1] == ' ')
1329 || (text[len - 1] == '\t') ) )
1334 cur_line->type = FILE_LINE_ACTION;
1336 /* Remove {} and make copy */
1337 if (NULL == (value = (char *) malloc(len + 1)))
1340 free_alias_list(alias_list);
1341 return JB_ERR_MEMORY;
1343 strncpy(value, text, len);
1347 err = get_actions(value, alias_list, cur_line->data.action);
1350 /* Invalid action or out of memory */
1352 free_alias_list(alias_list);
1353 if (err == JB_ERR_MEMORY)
1359 /* Line does not contain a name=value pair */
1360 file->parse_error = cur_line;
1361 file->parse_error_text = "This header does not specify a valid set of actions.";
1362 return JB_ERR_PARSE;
1366 /* Done with string - it was clobbered anyway */
1369 /* Process next line */
1370 cur_line = cur_line->next;
1372 /* Loop processing URL patterns */
1373 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1375 if (cur_line->unprocessed[0])
1377 /* Could parse URL here, but this isn't currently needed */
1379 cur_line->type = FILE_LINE_URL;
1383 cur_line->type = FILE_LINE_BLANK;
1385 cur_line = cur_line->next;
1387 } /* End main while(cur_line != NULL) loop */
1389 free_alias_list(alias_list);
1395 /*********************************************************************
1397 * Function : edit_read_file_lines
1399 * Description : Read all the lines of a file into memory.
1400 * Handles whitespace, comments and line continuation.
1403 * 1 : fp = File to read from. On return, this will be
1404 * at EOF but it will not have been closed.
1405 * 2 : pfile = Destination for a linked list of file_lines.
1406 * Will be set to NULL on error.
1408 * Returns : JB_ERR_OK on success
1409 * JB_ERR_MEMORY on out-of-memory
1411 *********************************************************************/
1412 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1414 struct file_line * first_line; /* Keep for return value or to free */
1415 struct file_line * cur_line; /* Current line */
1416 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1424 cur_line = first_line = zalloc(sizeof(struct file_line));
1425 if (cur_line == NULL)
1427 return JB_ERR_MEMORY;
1430 cur_line->type = FILE_LINE_UNPROCESSED;
1432 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1435 /* Out of memory or empty file. */
1436 /* Note that empty file is not an error we propogate up */
1438 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1443 prev_line = cur_line;
1444 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1445 if (cur_line == NULL)
1448 edit_free_file_lines(first_line);
1449 return JB_ERR_MEMORY;
1452 cur_line->type = FILE_LINE_UNPROCESSED;
1454 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1455 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1458 edit_free_file_lines(first_line);
1459 return JB_ERR_MEMORY;
1463 while (rval != JB_ERR_FILE);
1467 /* We allocated one too many - free it */
1468 prev_line->next = NULL;
1471 *pfile = first_line;
1476 /*********************************************************************
1478 * Function : edit_read_file
1480 * Description : Read a complete file into memory.
1481 * Handles CGI parameter parsing. If requested, also
1482 * checks the file's modification timestamp.
1485 * 1 : csp = Current client state (buffers, headers, etc...)
1486 * 2 : parameters = map of cgi parameters.
1487 * 3 : require_version = true to check "ver" parameter.
1488 * 4 : suffix = File extension, e.g. ".action".
1489 * 5 : pfile = Destination for the file. Will be set
1493 * filename : The name of the file to read, without the
1494 * path or ".action" extension.
1495 * ver : (Only if require_version is nonzero)
1496 * Timestamp of the actions file. If wrong, this
1497 * function fails with JB_ERR_MODIFIED.
1499 * Returns : JB_ERR_OK on success
1500 * JB_ERR_MEMORY on out-of-memory
1501 * JB_ERR_CGI_PARAMS if "filename" was not specified
1503 * JB_ERR_FILE if the file cannot be opened or
1505 * JB_ERR_MODIFIED if version checking was requested and
1506 * failed - the file was modified outside
1507 * of this CGI editor instance.
1509 *********************************************************************/
1510 jb_err edit_read_file(struct client_state *csp,
1511 const struct map *parameters,
1512 int require_version,
1514 struct editable_file **pfile)
1516 struct file_line * lines;
1520 const char * identifier;
1521 struct editable_file * file;
1522 unsigned version = 0;
1523 struct stat statbuf[1];
1524 char version_buf[22];
1525 int newline = NEWLINE_UNKNOWN;
1533 err = get_file_name_param(csp, parameters, "f", suffix,
1534 &filename, &identifier);
1540 if (stat(filename, statbuf) < 0)
1542 /* Error, probably file not found. */
1546 version = (unsigned) statbuf->st_mtime;
1548 if (require_version)
1550 unsigned specified_version;
1551 err = get_number_param(csp, parameters, "v", &specified_version);
1558 if (version != specified_version)
1560 return JB_ERR_MODIFIED;
1564 #if defined(AMIGA) || defined(__OS2__)
1565 if (NULL == (fp = fopen(filename,"r")))
1567 if (NULL == (fp = fopen(filename,"rt")))
1568 #endif /* def AMIGA */
1574 err = edit_read_file_lines(fp, &lines, &newline);
1584 file = (struct editable_file *) zalloc(sizeof(*file));
1588 edit_free_file_lines(lines);
1592 file->lines = lines;
1593 file->newline = newline;
1594 file->filename = filename;
1595 file->version = version;
1596 file->identifier = url_encode(identifier);
1598 if (file->identifier == NULL)
1600 edit_free_file(file);
1601 return JB_ERR_MEMORY;
1604 /* Correct file->version_str */
1605 freez(file->version_str);
1606 snprintf(version_buf, 22, "%u", file->version);
1607 version_buf[21] = '\0';
1608 file->version_str = strdup(version_buf);
1609 if (version_buf == NULL)
1611 edit_free_file(file);
1612 return JB_ERR_MEMORY;
1620 /*********************************************************************
1622 * Function : edit_read_actions_file
1624 * Description : Read a complete actions file into memory.
1625 * Handles CGI parameter parsing. If requested, also
1626 * checks the file's modification timestamp.
1628 * If this function detects an error in the categories
1629 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1630 * then it handles it by filling in the specified
1631 * response structure and returning JB_ERR_FILE.
1634 * 1 : csp = Current client state (buffers, headers, etc...)
1635 * 2 : rsp = HTTP response. Only filled in on error.
1636 * 2 : parameters = map of cgi parameters.
1637 * 3 : require_version = true to check "ver" parameter.
1638 * 4 : pfile = Destination for the file. Will be set
1642 * filename : The name of the actions file to read, without the
1643 * path or ".action" extension.
1644 * ver : (Only if require_version is nonzero)
1645 * Timestamp of the actions file. If wrong, this
1646 * function fails with JB_ERR_MODIFIED.
1648 * Returns : JB_ERR_OK on success
1649 * JB_ERR_MEMORY on out-of-memory
1650 * JB_ERR_CGI_PARAMS if "filename" was not specified
1652 * JB_ERR_FILE if the file does not contain valid data,
1653 * or if file cannot be opened or
1654 * contains no data, or if version
1655 * checking was requested and failed.
1657 *********************************************************************/
1658 jb_err edit_read_actions_file(struct client_state *csp,
1659 struct http_response *rsp,
1660 const struct map *parameters,
1661 int require_version,
1662 struct editable_file **pfile)
1665 struct editable_file *file;
1673 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1676 /* Try to handle if possible */
1677 if (err == JB_ERR_FILE)
1679 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1681 else if (err == JB_ERR_MODIFIED)
1683 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1685 if (err == JB_ERR_OK)
1688 * Signal to higher-level CGI code that there was a problem but we
1689 * handled it, they should just return JB_ERR_OK.
1696 err = edit_parse_actions_file(file);
1699 if (err == JB_ERR_PARSE)
1701 err = cgi_error_parse(csp, rsp, file);
1702 if (err == JB_ERR_OK)
1705 * Signal to higher-level CGI code that there was a problem but we
1706 * handled it, they should just return JB_ERR_OK.
1711 edit_free_file(file);
1720 /*********************************************************************
1722 * Function : get_file_name_param
1724 * Description : Get the name of the file to edit from the parameters
1725 * passed to a CGI function. This function handles
1726 * security checks such as blocking urls containing
1727 * "/" or ".", prepending the config file directory,
1728 * and adding the specified suffix.
1730 * (This is an essential security check, otherwise
1731 * users may be able to pass "../../../etc/passwd"
1732 * and overwrite the password file [linux], "prn:"
1733 * and print random data [Windows], etc...)
1735 * This function only allows filenames contining the
1736 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1737 * That's probably too restrictive but at least it's
1741 * 1 : csp = Current client state (buffers, headers, etc...)
1742 * 2 : parameters = map of cgi parameters
1743 * 3 : param_name = The name of the parameter to read
1744 * 4 : suffix = File extension, e.g. ".actions"
1745 * 5 : pfilename = destination for full filename. Caller
1746 * free()s. Set to NULL on error.
1747 * 6 : pparam = destination for partial filename,
1748 * suitable for use in another URL. Allocated as part
1749 * of the map "parameters", so don't free it.
1750 * Set to NULL if not specified.
1752 * Returns : JB_ERR_OK on success
1753 * JB_ERR_MEMORY on out-of-memory
1754 * JB_ERR_CGI_PARAMS if "filename" was not specified
1757 *********************************************************************/
1758 static jb_err get_file_name_param(struct client_state *csp,
1759 const struct map *parameters,
1760 const char *param_name,
1763 const char **pparam)
1767 #if 0 /* Patch to make 3.0.0 work properly. */
1769 #endif /* 0 - Patch to make 3.0.0 work properly. */
1783 param = lookup(parameters, param_name);
1786 return JB_ERR_CGI_PARAMS;
1791 len = strlen(param);
1792 if (len >= FILENAME_MAX)
1795 return JB_ERR_CGI_PARAMS;
1798 /* Check every character to see if it's legal */
1800 while ((ch = *s++) != '\0')
1802 if ( ((ch < 'A') || (ch > 'Z'))
1803 && ((ch < 'a') || (ch > 'z'))
1804 && ((ch < '0') || (ch > '9'))
1808 /* Probable hack attempt. */
1809 return JB_ERR_CGI_PARAMS;
1814 * FIXME Following is a hack to make 3.0.0 work properly.
1815 * Change "#if 0" --> "#if 1" below when we have modular action
1819 #if 0 /* Patch to make 3.0.0 work properly. */
1820 /* Append extension */
1821 name = malloc(len + strlen(suffix) + 1);
1824 return JB_ERR_MEMORY;
1826 strcpy(name, param);
1827 strcpy(name + len, suffix);
1830 fullpath = make_path(csp->config->confdir, name);
1832 #else /* 1 - Patch to make 3.0.0 work properly. */
1833 if ((csp->actions_list == NULL)
1834 || (csp->actions_list->filename == NULL))
1836 return JB_ERR_CGI_PARAMS;
1839 fullpath = ( (csp->actions_list && csp->actions_list->filename)
1840 ? strdup(csp->actions_list->filename) : NULL);
1841 #endif /* 1 - Patch to make 3.0.0 work properly. */
1842 if (fullpath == NULL)
1844 return JB_ERR_MEMORY;
1848 *pfilename = fullpath;
1854 /*********************************************************************
1856 * Function : get_char_param
1858 * Description : Get a single-character parameter passed to a CGI
1862 * 1 : parameters = map of cgi parameters
1863 * 2 : param_name = The name of the parameter to read
1865 * Returns : Uppercase character on success, '\0' on error.
1867 *********************************************************************/
1868 static char get_char_param(const struct map *parameters,
1869 const char *param_name)
1876 ch = *(lookup(parameters, param_name));
1877 if ((ch >= 'a') && (ch <= 'z'))
1879 ch = ch - 'a' + 'A';
1886 /*********************************************************************
1888 * Function : get_string_param
1890 * Description : Get a string paramater, to be used as an
1891 * ACTION_STRING or ACTION_MULTI paramater.
1892 * Validates the input to prevent stupid/malicious
1893 * users from corrupting their action file.
1896 * 1 : parameters = map of cgi parameters
1897 * 2 : param_name = The name of the parameter to read
1898 * 3 : pparam = destination for paramater. Allocated as
1899 * part of the map "parameters", so don't free it.
1900 * Set to NULL if not specified.
1902 * Returns : JB_ERR_OK on success, or if the paramater
1903 * was not specified.
1904 * JB_ERR_MEMORY on out-of-memory.
1905 * JB_ERR_CGI_PARAMS if the paramater is not valid.
1907 *********************************************************************/
1908 static jb_err get_string_param(const struct map *parameters,
1909 const char *param_name,
1910 const char **pparam)
1922 param = lookup(parameters, param_name);
1928 if (strlen(param) >= CGI_ACTION_PARAM_LEN_MAX)
1933 * Note that the length limit is arbitrary, it just seems
1934 * sensible to limit it to *something*. There's no
1935 * technical reason for any limit at all.
1937 return JB_ERR_CGI_PARAMS;
1940 /* Check every character to see if it's legal */
1942 while ((ch = *s++) != '\0')
1944 if ( ((unsigned char)ch < (unsigned char)' ')
1947 /* Probable hack attempt, or user accidentally used '}'. */
1948 return JB_ERR_CGI_PARAMS;
1959 /*********************************************************************
1961 * Function : get_number_param
1963 * Description : Get a non-negative integer from the parameters
1964 * passed to a CGI function.
1967 * 1 : csp = Current client state (buffers, headers, etc...)
1968 * 2 : parameters = map of cgi parameters
1969 * 3 : name = Name of CGI parameter to read
1970 * 4 : pvalue = destination for value.
1971 * Set to -1 on error.
1973 * Returns : JB_ERR_OK on success
1974 * JB_ERR_MEMORY on out-of-memory
1975 * JB_ERR_CGI_PARAMS if the parameter was not specified
1978 *********************************************************************/
1979 static jb_err get_number_param(struct client_state *csp,
1980 const struct map *parameters,
1995 param = lookup(parameters, name);
1998 return JB_ERR_CGI_PARAMS;
2001 /* We don't use atoi because I want to check this carefully... */
2004 while ((ch = *param++) != '\0')
2006 if ((ch < '0') || (ch > '9'))
2008 return JB_ERR_CGI_PARAMS;
2015 * <limits.h> defines UINT_MAX
2017 * (UINT_MAX - ch) / 10 is the largest number that
2018 * can be safely multiplied by 10 then have ch added.
2020 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
2022 return JB_ERR_CGI_PARAMS;
2025 value = value * 10 + ch;
2036 /*********************************************************************
2038 * Function : get_url_spec_param
2040 * Description : Get a URL pattern from the parameters
2041 * passed to a CGI function. Removes leading/trailing
2042 * spaces and validates it.
2045 * 1 : csp = Current client state (buffers, headers, etc...)
2046 * 2 : parameters = map of cgi parameters
2047 * 3 : name = Name of CGI parameter to read
2048 * 4 : pvalue = destination for value. Will be malloc()'d.
2049 * Set to NULL on error.
2051 * Returns : JB_ERR_OK on success
2052 * JB_ERR_MEMORY on out-of-memory
2053 * JB_ERR_CGI_PARAMS if the parameter was not specified
2056 *********************************************************************/
2057 static jb_err get_url_spec_param(struct client_state *csp,
2058 const struct map *parameters,
2062 const char *orig_param;
2065 struct url_spec compiled[1];
2075 orig_param = lookup(parameters, name);
2078 return JB_ERR_CGI_PARAMS;
2081 /* Copy and trim whitespace */
2082 param = strdup(orig_param);
2085 return JB_ERR_MEMORY;
2089 /* Must be non-empty, and can't allow 1st character to be '{' */
2090 if (param[0] == '\0' || param[0] == '{')
2093 return JB_ERR_CGI_PARAMS;
2096 /* Check for embedded newlines */
2097 for (s = param; *s != '\0'; s++)
2099 if ((*s == '\r') || (*s == '\n'))
2102 return JB_ERR_CGI_PARAMS;
2106 /* Check that regex is valid */
2111 return JB_ERR_MEMORY;
2113 err = create_url_spec(compiled, s);
2118 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2120 free_url_spec(compiled);
2122 if (param[strlen(param) - 1] == '\\')
2125 * Must protect trailing '\\' from becoming line continuation character.
2126 * Two methods: 1) If it's a domain only, add a trailing '/'.
2127 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2129 if (strchr(param, '/') == NULL)
2131 err = string_append(¶m, "/");
2135 err = string_append(¶m, "(?:)");
2142 /* Check that the modified regex is valid */
2147 return JB_ERR_MEMORY;
2149 err = create_url_spec(compiled, s);
2154 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2156 free_url_spec(compiled);
2163 /*********************************************************************
2165 * Function : map_radio
2167 * Description : Map a set of radio button values. E.g. if you have
2168 * 3 radio buttons, declare them as:
2169 * <option type="radio" name="xyz" @xyz-a@>
2170 * <option type="radio" name="xyz" @xyz-b@>
2171 * <option type="radio" name="xyz" @xyz-c@>
2172 * Then map one of the @xyz-?@ variables to "checked"
2173 * and all the others to empty by calling:
2174 * map_radio(exports, "xyz", "abc", sel)
2175 * Where 'sel' is 'a', 'b', or 'c'.
2178 * 1 : exports = Exports map to modify.
2179 * 2 : optionname = name for map
2180 * 3 : values = null-terminated list of values;
2181 * 4 : value = Selected value.
2183 * CGI Parameters : None
2185 * Returns : JB_ERR_OK on success
2186 * JB_ERR_MEMORY on out-of-memory
2188 *********************************************************************/
2189 static jb_err map_radio(struct map * exports,
2190 const char * optionname,
2191 const char * values,
2203 len = strlen(optionname);
2204 buf = malloc(len + 3);
2207 return JB_ERR_MEMORY;
2210 strcpy(buf, optionname);
2215 while ((c = *values++) != '\0')
2220 if (map(exports, buf, 1, "", 1))
2222 return JB_ERR_MEMORY;
2228 return map(exports, buf, 0, "checked", 1);
2232 /*********************************************************************
2234 * Function : cgi_error_modified
2236 * Description : CGI function that is called when a file is modified
2237 * outside the CGI editor.
2240 * 1 : csp = Current client state (buffers, headers, etc...)
2241 * 2 : rsp = http_response data structure for output
2242 * 3 : filename = The file that was modified.
2244 * CGI Parameters : none
2246 * Returns : JB_ERR_OK on success
2247 * JB_ERR_MEMORY on out-of-memory error.
2249 *********************************************************************/
2250 jb_err cgi_error_modified(struct client_state *csp,
2251 struct http_response *rsp,
2252 const char *filename)
2254 struct map *exports;
2261 if (NULL == (exports = default_exports(csp, NULL)))
2263 return JB_ERR_MEMORY;
2266 err = map(exports, "f", 1, html_encode(filename), 0);
2273 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2277 /*********************************************************************
2279 * Function : cgi_error_parse
2281 * Description : CGI function that is called when a file cannot
2282 * be parsed by the CGI editor.
2285 * 1 : csp = Current client state (buffers, headers, etc...)
2286 * 2 : rsp = http_response data structure for output
2287 * 3 : file = The file that was modified.
2289 * CGI Parameters : none
2291 * Returns : JB_ERR_OK on success
2292 * JB_ERR_MEMORY on out-of-memory error.
2294 *********************************************************************/
2295 jb_err cgi_error_parse(struct client_state *csp,
2296 struct http_response *rsp,
2297 struct editable_file *file)
2299 struct map *exports;
2301 struct file_line *cur_line;
2307 if (NULL == (exports = default_exports(csp, NULL)))
2309 return JB_ERR_MEMORY;
2312 err = map(exports, "f", 1, file->identifier, 1);
2313 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2315 cur_line = file->parse_error;
2318 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2319 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2327 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2331 /*********************************************************************
2333 * Function : cgi_error_file
2335 * Description : CGI function that is called when a file cannot be
2336 * opened by the CGI editor.
2339 * 1 : csp = Current client state (buffers, headers, etc...)
2340 * 2 : rsp = http_response data structure for output
2341 * 3 : filename = The file that was modified.
2343 * CGI Parameters : none
2345 * Returns : JB_ERR_OK on success
2346 * JB_ERR_MEMORY on out-of-memory error.
2348 *********************************************************************/
2349 jb_err cgi_error_file(struct client_state *csp,
2350 struct http_response *rsp,
2351 const char *filename)
2353 struct map *exports;
2360 if (NULL == (exports = default_exports(csp, NULL)))
2362 return JB_ERR_MEMORY;
2365 err = map(exports, "f", 1, html_encode(filename), 0);
2372 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2376 /*********************************************************************
2378 * Function : cgi_error_bad_param
2380 * Description : CGI function that is called if the parameters
2381 * (query string) for a CGI were wrong.
2384 * 1 : csp = Current client state (buffers, headers, etc...)
2385 * 2 : rsp = http_response data structure for output
2387 * CGI Parameters : none
2389 * Returns : JB_ERR_OK on success
2390 * JB_ERR_MEMORY on out-of-memory error.
2392 *********************************************************************/
2393 jb_err cgi_error_disabled(struct client_state *csp,
2394 struct http_response *rsp)
2396 struct map *exports;
2401 if (NULL == (exports = default_exports(csp, NULL)))
2403 return JB_ERR_MEMORY;
2406 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2410 /*********************************************************************
2412 * Function : cgi_edit_actions
2414 * Description : CGI function that allows the user to choose which
2415 * actions file to edit.
2418 * 1 : csp = Current client state (buffers, headers, etc...)
2419 * 2 : rsp = http_response data structure for output
2420 * 3 : parameters = map of cgi parameters
2422 * CGI Parameters : None
2424 * Returns : JB_ERR_OK on success
2425 * JB_ERR_MEMORY on out-of-memory error
2427 *********************************************************************/
2428 jb_err cgi_edit_actions(struct client_state *csp,
2429 struct http_response *rsp,
2430 const struct map *parameters)
2433 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2435 return cgi_error_disabled(csp, rsp);
2438 /* FIXME: Incomplete */
2439 rsp->status = strdup("302 Local Redirect from Junkbuster");
2440 if (rsp->status == NULL)
2442 return JB_ERR_MEMORY;
2444 if (enlist_unique_header(rsp->headers, "Location",
2445 CGI_PREFIX "edit-actions-list?f=ijb"))
2449 return JB_ERR_MEMORY;
2456 /*********************************************************************
2458 * Function : cgi_edit_actions_list
2460 * Description : CGI function that edits the actions list.
2461 * FIXME: This function shouldn't FATAL ever.
2462 * FIXME: This function doesn't check the retval of map()
2464 * 1 : csp = Current client state (buffers, headers, etc...)
2465 * 2 : rsp = http_response data structure for output
2466 * 3 : parameters = map of cgi parameters
2468 * CGI Parameters : filename
2470 * Returns : JB_ERR_OK on success
2471 * JB_ERR_MEMORY on out-of-memory
2472 * JB_ERR_FILE if the file cannot be opened or
2474 * JB_ERR_CGI_PARAMS if "filename" was not specified
2477 *********************************************************************/
2478 jb_err cgi_edit_actions_list(struct client_state *csp,
2479 struct http_response *rsp,
2480 const struct map *parameters)
2482 char * section_template;
2483 char * url_template;
2488 struct map * exports;
2489 struct map * section_exports;
2490 struct map * url_exports;
2491 struct editable_file * file;
2492 struct file_line * cur_line;
2493 unsigned line_number = 0;
2494 unsigned prev_section_line_number = ((unsigned) (-1));
2498 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2500 return cgi_error_disabled(csp, rsp);
2503 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2506 /* No filename specified, can't read file, or out of memory. */
2507 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2510 if (NULL == (exports = default_exports(csp, NULL)))
2512 edit_free_file(file);
2513 return JB_ERR_MEMORY;
2516 err = map(exports, "f", 1, file->identifier, 1);
2517 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2521 edit_free_file(file);
2526 /* Should do all global exports above this point */
2528 err = template_load(csp, §ion_template, "edit-actions-list-section");
2531 edit_free_file(file);
2533 if (err == JB_ERR_FILE)
2535 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2540 err = template_load(csp, &url_template, "edit-actions-list-url");
2543 free(section_template);
2544 edit_free_file(file);
2546 if (err == JB_ERR_FILE)
2548 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2553 err = template_fill(§ion_template, exports);
2557 edit_free_file(file);
2563 err = template_fill(&url_template, exports);
2566 free(section_template);
2567 edit_free_file(file);
2572 /* Find start of actions in file */
2573 cur_line = file->lines;
2575 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2577 cur_line = cur_line->next;
2581 if (NULL == (sections = strdup("")))
2583 free(section_template);
2585 edit_free_file(file);
2587 return JB_ERR_MEMORY;
2590 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2592 if (NULL == (section_exports = new_map()))
2595 free(section_template);
2597 edit_free_file(file);
2599 return JB_ERR_MEMORY;
2602 snprintf(buf, 50, "%d", line_number);
2603 err = map(section_exports, "s", 1, buf, 1);
2604 if (!err) err = map(section_exports, "actions", 1,
2605 actions_to_html(cur_line->data.action), 0);
2608 && (cur_line->next != NULL)
2609 && (cur_line->next->type == FILE_LINE_URL))
2611 /* This section contains at least one URL, don't allow delete */
2612 err = map_block_killer(section_exports, "empty-section");
2616 if (!err) err = map_block_keep(section_exports, "empty-section");
2619 if (prev_section_line_number != ((unsigned)(-1)))
2621 /* Not last section */
2622 snprintf(buf, 50, "%d", prev_section_line_number);
2623 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2624 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2629 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2631 prev_section_line_number = line_number;
2636 free(section_template);
2638 edit_free_file(file);
2640 free_map(section_exports);
2644 /* Should do all section-specific exports above this point */
2646 if (NULL == (urls = strdup("")))
2649 free(section_template);
2651 edit_free_file(file);
2653 free_map(section_exports);
2654 return JB_ERR_MEMORY;
2659 cur_line = cur_line->next;
2662 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2664 if (NULL == (url_exports = new_map()))
2668 free(section_template);
2670 edit_free_file(file);
2672 free_map(section_exports);
2673 return JB_ERR_MEMORY;
2676 snprintf(buf, 50, "%d", line_number);
2677 err = map(url_exports, "p", 1, buf, 1);
2679 snprintf(buf, 50, "%d", url_1_2);
2680 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2682 if (!err) err = map(url_exports, "url-html", 1,
2683 html_encode(cur_line->unprocessed), 0);
2684 if (!err) err = map(url_exports, "url", 1,
2685 url_encode(cur_line->unprocessed), 0);
2691 free(section_template);
2693 edit_free_file(file);
2695 free_map(section_exports);
2696 free_map(url_exports);
2700 if (NULL == (s = strdup(url_template)))
2704 free(section_template);
2706 edit_free_file(file);
2708 free_map(section_exports);
2709 free_map(url_exports);
2710 return JB_ERR_MEMORY;
2713 err = template_fill(&s, section_exports);
2714 if (!err) err = template_fill(&s, url_exports);
2715 if (!err) err = string_append(&urls, s);
2717 free_map(url_exports);
2724 free(section_template);
2726 edit_free_file(file);
2728 free_map(section_exports);
2732 url_1_2 = 3 - url_1_2;
2734 cur_line = cur_line->next;
2738 err = map(section_exports, "urls", 1, urls, 0);
2740 /* Could also do section-specific exports here, but it wouldn't be as fast */
2742 if ( (cur_line != NULL)
2743 && (cur_line->type == FILE_LINE_ACTION))
2745 /* Not last section */
2746 snprintf(buf, 50, "%d", line_number);
2747 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2748 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2753 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2759 free(section_template);
2761 edit_free_file(file);
2763 free_map(section_exports);
2767 if (NULL == (s = strdup(section_template)))
2770 free(section_template);
2772 edit_free_file(file);
2774 free_map(section_exports);
2775 return JB_ERR_MEMORY;
2778 err = template_fill(&s, section_exports);
2779 if (!err) err = string_append(§ions, s);
2782 free_map(section_exports);
2787 free(section_template);
2789 edit_free_file(file);
2795 edit_free_file(file);
2796 free(section_template);
2799 err = map(exports, "sections", 1, sections, 0);
2806 /* Could also do global exports here, but it wouldn't be as fast */
2808 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2812 /*********************************************************************
2814 * Function : cgi_edit_actions
2816 * Description : CGI function that edits the Actions list.
2819 * 1 : csp = Current client state (buffers, headers, etc...)
2820 * 2 : rsp = http_response data structure for output
2821 * 3 : parameters = map of cgi parameters
2823 * CGI Parameters : None
2825 * Returns : JB_ERR_OK on success
2826 * JB_ERR_MEMORY on out-of-memory
2827 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2828 * specified or not valid.
2830 *********************************************************************/
2831 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2832 struct http_response *rsp,
2833 const struct map *parameters)
2835 struct map * exports;
2837 struct editable_file * file;
2838 struct file_line * cur_line;
2839 unsigned line_number;
2841 struct file_list *filter_file;
2842 struct re_filterfile_spec *filter_group;
2844 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2846 return cgi_error_disabled(csp, rsp);
2849 err = get_number_param(csp, parameters, "s", §ionid);
2855 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2858 /* No filename specified, can't read file, modified, or out of memory. */
2859 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2862 cur_line = file->lines;
2864 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2866 cur_line = cur_line->next;
2869 if ( (cur_line == NULL)
2870 || (line_number != sectionid)
2872 || (cur_line->type != FILE_LINE_ACTION))
2874 /* Invalid "sectionid" parameter */
2875 edit_free_file(file);
2876 return JB_ERR_CGI_PARAMS;
2879 if (NULL == (exports = default_exports(csp, NULL)))
2881 edit_free_file(file);
2882 return JB_ERR_MEMORY;
2885 err = map(exports, "f", 1, file->identifier, 1);
2886 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2887 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2889 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2891 filter_file = csp->rlist;
2892 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2894 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2898 edit_free_file(file);
2903 if (filter_group == NULL)
2905 err = map(exports, "filter-params", 1, "", 1);
2909 /* We have some entries in the filter list */
2912 char * filter_template;
2914 err = template_load(csp, &filter_template, "edit-actions-for-url-filter");
2917 edit_free_file(file);
2919 if (err == JB_ERR_FILE)
2921 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2926 result = strdup("");
2928 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2930 char current_mode = 'x';
2931 struct list_entry *filter_name;
2933 struct map *line_exports;
2936 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2937 while ((filter_name != NULL)
2938 && (0 != strcmp(filter_group->filtername, filter_name->str)))
2940 filter_name = filter_name->next;
2943 if (filter_name != NULL)
2949 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2950 while ((filter_name != NULL)
2951 && (0 != strcmp(filter_group->filtername, filter_name->str)))
2953 filter_name = filter_name->next;
2955 if (filter_name != NULL)
2961 /* Generate a unique serial number */
2962 snprintf(number, sizeof(number), "%x", index++);
2963 number[sizeof(number) - 1] = '\0';
2965 line_exports = new_map();
2966 if (line_exports == NULL)
2968 err = JB_ERR_MEMORY;
2973 if (!err) err = map(line_exports, "index", 1, number, 1);
2974 if (!err) err = map(line_exports, "name", 1, filter_group->filtername, 1);
2975 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2980 this_line = strdup(filter_template);
2981 if (this_line == NULL) err = JB_ERR_MEMORY;
2983 if (!err) err = template_fill(&this_line, line_exports);
2984 string_join(&result, this_line);
2986 free_map(line_exports);
2991 err = map(exports, "filter-params", 1, result, 0);
2999 if (!err) err = map_radio(exports, "filter-all", "nx",
3000 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
3002 edit_free_file(file);
3010 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
3014 /*********************************************************************
3016 * Function : cgi_edit_actions_submit
3018 * Description : CGI function that actually edits the Actions list.
3021 * 1 : csp = Current client state (buffers, headers, etc...)
3022 * 2 : rsp = http_response data structure for output
3023 * 3 : parameters = map of cgi parameters
3025 * CGI Parameters : None
3027 * Returns : JB_ERR_OK on success
3028 * JB_ERR_MEMORY on out-of-memory
3029 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3030 * specified or not valid.
3032 *********************************************************************/
3033 jb_err cgi_edit_actions_submit(struct client_state *csp,
3034 struct http_response *rsp,
3035 const struct map *parameters)
3041 struct editable_file * file;
3042 struct file_line * cur_line;
3043 unsigned line_number;
3049 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3051 return cgi_error_disabled(csp, rsp);
3054 err = get_number_param(csp, parameters, "s", §ionid);
3060 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3063 /* No filename specified, can't read file, modified, or out of memory. */
3064 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3067 cur_line = file->lines;
3069 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
3071 cur_line = cur_line->next;
3074 if ( (cur_line == NULL)
3075 || (line_number != sectionid)
3077 || (cur_line->type != FILE_LINE_ACTION))
3079 /* Invalid "sectionid" parameter */
3080 edit_free_file(file);
3081 return JB_ERR_CGI_PARAMS;
3084 err = actions_from_radio(parameters, cur_line->data.action);
3088 edit_free_file(file);
3092 ch = get_char_param(parameters, "filter_all");
3095 list_remove_all(cur_line->data.action->multi_add[ACTION_MULTI_FILTER]);
3096 list_remove_all(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]);
3097 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 1;
3101 cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
3104 for (index = 0; !err; index++)
3111 /* Generate the keys */
3112 snprintf(key_value, sizeof(key_value), "filter_r%x", index);
3113 key_value[sizeof(key_value) - 1] = '\0';
3114 snprintf(key_name, sizeof(key_name), "filter_n%x", index);
3115 key_name[sizeof(key_name) - 1] = '\0';
3117 err = get_string_param(parameters, key_name, &name);
3127 value = get_char_param(parameters, key_value);
3130 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3131 if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3132 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3134 else if (value == 'N')
3136 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3137 if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
3139 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3140 if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3143 else if (value == 'X')
3145 list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
3146 list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
3153 edit_free_file(file);
3157 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
3160 edit_free_file(file);
3161 return JB_ERR_MEMORY;
3164 len = strlen(actiontext);
3168 * Empty action - must special-case this.
3169 * Simply setting len to 1 is sufficient...
3174 if (NULL == (newtext = malloc(len + 2)))
3178 edit_free_file(file);
3179 return JB_ERR_MEMORY;
3181 strcpy(newtext, actiontext);
3185 newtext[len + 1] = '\0';
3187 freez(cur_line->raw);
3188 freez(cur_line->unprocessed);
3189 cur_line->unprocessed = newtext;
3191 err = edit_write_file(file);
3194 /* Error writing file */
3195 edit_free_file(file);
3199 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3200 string_append(&target, file->identifier);
3202 edit_free_file(file);
3207 return JB_ERR_MEMORY;
3210 rsp->status = strdup("302 Local Redirect from Junkbuster");
3211 if (rsp->status == NULL)
3214 return JB_ERR_MEMORY;
3216 err = enlist_unique_header(rsp->headers, "Location", target);
3223 /*********************************************************************
3225 * Function : cgi_edit_actions_url
3227 * Description : CGI function that actually edits a URL pattern in
3231 * 1 : csp = Current client state (buffers, headers, etc...)
3232 * 2 : rsp = http_response data structure for output
3233 * 3 : parameters = map of cgi parameters
3236 * filename : Identifies the file to edit
3237 * ver : File's last-modified time
3238 * section : Line number of section to edit
3239 * pattern : Line number of pattern to edit
3240 * newval : New value for pattern
3242 * Returns : JB_ERR_OK on success
3243 * JB_ERR_MEMORY on out-of-memory
3244 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3245 * specified or not valid.
3247 *********************************************************************/
3248 jb_err cgi_edit_actions_url(struct client_state *csp,
3249 struct http_response *rsp,
3250 const struct map *parameters)
3254 struct editable_file * file;
3255 struct file_line * cur_line;
3256 unsigned line_number;
3260 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3262 return cgi_error_disabled(csp, rsp);
3265 err = get_number_param(csp, parameters, "p", &patternid);
3272 return JB_ERR_CGI_PARAMS;
3275 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3281 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3284 /* No filename specified, can't read file, modified, or out of memory. */
3286 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3290 cur_line = file->lines;
3292 while ((cur_line != NULL) && (line_number < patternid))
3294 cur_line = cur_line->next;
3298 if ( (cur_line == NULL)
3299 || (cur_line->type != FILE_LINE_URL))
3301 /* Invalid "patternid" parameter */
3303 edit_free_file(file);
3304 return JB_ERR_CGI_PARAMS;
3307 /* At this point, the line to edit is in cur_line */
3309 freez(cur_line->raw);
3310 freez(cur_line->unprocessed);
3311 cur_line->unprocessed = new_pattern;
3313 err = edit_write_file(file);
3316 /* Error writing file */
3317 edit_free_file(file);
3321 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3322 string_append(&target, file->identifier);
3324 edit_free_file(file);
3329 return JB_ERR_MEMORY;
3332 rsp->status = strdup("302 Local Redirect from Junkbuster");
3333 if (rsp->status == NULL)
3336 return JB_ERR_MEMORY;
3338 err = enlist_unique_header(rsp->headers, "Location", target);
3345 /*********************************************************************
3347 * Function : cgi_edit_actions_add_url
3349 * Description : CGI function that actually adds a URL pattern to
3353 * 1 : csp = Current client state (buffers, headers, etc...)
3354 * 2 : rsp = http_response data structure for output
3355 * 3 : parameters = map of cgi parameters
3358 * filename : Identifies the file to edit
3359 * ver : File's last-modified time
3360 * section : Line number of section to edit
3361 * newval : New pattern
3363 * Returns : JB_ERR_OK on success
3364 * JB_ERR_MEMORY on out-of-memory
3365 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3366 * specified or not valid.
3368 *********************************************************************/
3369 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3370 struct http_response *rsp,
3371 const struct map *parameters)
3375 struct file_line * new_line;
3376 struct editable_file * file;
3377 struct file_line * cur_line;
3378 unsigned line_number;
3382 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3384 return cgi_error_disabled(csp, rsp);
3387 err = get_number_param(csp, parameters, "s", §ionid);
3394 return JB_ERR_CGI_PARAMS;
3397 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3403 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3406 /* No filename specified, can't read file, modified, or out of memory. */
3408 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3412 cur_line = file->lines;
3414 while ((cur_line != NULL) && (line_number < sectionid))
3416 cur_line = cur_line->next;
3420 if ( (cur_line == NULL)
3421 || (cur_line->type != FILE_LINE_ACTION))
3423 /* Invalid "sectionid" parameter */
3425 edit_free_file(file);
3426 return JB_ERR_CGI_PARAMS;
3429 /* At this point, the section header is in cur_line - add after this. */
3431 /* Allocate the new line */
3432 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3433 if (new_line == NULL)
3436 edit_free_file(file);
3437 return JB_ERR_MEMORY;
3440 /* Fill in the data members of the new line */
3441 new_line->raw = NULL;
3442 new_line->prefix = NULL;
3443 new_line->unprocessed = new_pattern;
3444 new_line->type = FILE_LINE_URL;
3446 /* Link new_line into the list, after cur_line */
3447 new_line->next = cur_line->next;
3448 cur_line->next = new_line;
3450 /* Done making changes, now commit */
3452 err = edit_write_file(file);
3455 /* Error writing file */
3456 edit_free_file(file);
3460 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3461 string_append(&target, file->identifier);
3463 edit_free_file(file);
3468 return JB_ERR_MEMORY;
3471 rsp->status = strdup("302 Local Redirect from Junkbuster");
3472 if (rsp->status == NULL)
3475 return JB_ERR_MEMORY;
3477 err = enlist_unique_header(rsp->headers, "Location", target);
3484 /*********************************************************************
3486 * Function : cgi_edit_actions_remove_url
3488 * Description : CGI function that actually removes a URL pattern from
3492 * 1 : csp = Current client state (buffers, headers, etc...)
3493 * 2 : rsp = http_response data structure for output
3494 * 3 : parameters = map of cgi parameters
3497 * f : (filename) Identifies the file to edit
3498 * v : (version) File's last-modified time
3499 * p : (pattern) Line number of pattern to remove
3501 * Returns : JB_ERR_OK on success
3502 * JB_ERR_MEMORY on out-of-memory
3503 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3504 * specified or not valid.
3506 *********************************************************************/
3507 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3508 struct http_response *rsp,
3509 const struct map *parameters)
3512 struct editable_file * file;
3513 struct file_line * cur_line;
3514 struct file_line * prev_line;
3515 unsigned line_number;
3519 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3521 return cgi_error_disabled(csp, rsp);
3524 err = get_number_param(csp, parameters, "p", &patternid);
3530 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3533 /* No filename specified, can't read file, modified, or out of memory. */
3534 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3539 cur_line = file->lines;
3541 while ((cur_line != NULL) && (line_number < patternid))
3543 prev_line = cur_line;
3544 cur_line = cur_line->next;
3548 if ( (cur_line == NULL)
3549 || (prev_line == NULL)
3550 || (cur_line->type != FILE_LINE_URL))
3552 /* Invalid "patternid" parameter */
3553 edit_free_file(file);
3554 return JB_ERR_CGI_PARAMS;
3557 /* At this point, the line to remove is in cur_line, and the previous
3558 * one is in prev_line
3561 /* Unlink cur_line */
3562 prev_line->next = cur_line->next;
3563 cur_line->next = NULL;
3566 edit_free_file_lines(cur_line);
3568 err = edit_write_file(file);
3571 /* Error writing file */
3572 edit_free_file(file);
3576 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3577 string_append(&target, file->identifier);
3579 edit_free_file(file);
3584 return JB_ERR_MEMORY;
3587 rsp->status = strdup("302 Local Redirect from Junkbuster");
3588 if (rsp->status == NULL)
3591 return JB_ERR_MEMORY;
3593 err = enlist_unique_header(rsp->headers, "Location", target);
3600 /*********************************************************************
3602 * Function : cgi_edit_actions_section_remove
3604 * Description : CGI function that actually removes a whole section from
3605 * the actions file. The section must be empty first
3606 * (else JB_ERR_CGI_PARAMS).
3609 * 1 : csp = Current client state (buffers, headers, etc...)
3610 * 2 : rsp = http_response data structure for output
3611 * 3 : parameters = map of cgi parameters
3614 * f : (filename) Identifies the file to edit
3615 * v : (version) File's last-modified time
3616 * s : (section) Line number of section to edit
3618 * Returns : JB_ERR_OK on success
3619 * JB_ERR_MEMORY on out-of-memory
3620 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3621 * specified or not valid.
3623 *********************************************************************/
3624 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3625 struct http_response *rsp,
3626 const struct map *parameters)
3629 struct editable_file * file;
3630 struct file_line * cur_line;
3631 struct file_line * prev_line;
3632 unsigned line_number;
3636 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3638 return cgi_error_disabled(csp, rsp);
3641 err = get_number_param(csp, parameters, "s", §ionid);
3647 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3650 /* No filename specified, can't read file, modified, or out of memory. */
3651 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3655 cur_line = file->lines;
3658 while ((cur_line != NULL) && (line_number < sectionid))
3660 prev_line = cur_line;
3661 cur_line = cur_line->next;
3665 if ( (cur_line == NULL)
3666 || (cur_line->type != FILE_LINE_ACTION) )
3668 /* Invalid "sectionid" parameter */
3669 edit_free_file(file);
3670 return JB_ERR_CGI_PARAMS;
3673 if ( (cur_line->next != NULL)
3674 && (cur_line->next->type == FILE_LINE_URL) )
3676 /* Section not empty. */
3677 edit_free_file(file);
3678 return JB_ERR_CGI_PARAMS;
3681 /* At this point, the line to remove is in cur_line, and the previous
3682 * one is in prev_line
3685 /* Unlink cur_line */
3686 if (prev_line == NULL)
3688 /* Removing the first line from the file */
3689 file->lines = cur_line->next;
3693 prev_line->next = cur_line->next;
3695 cur_line->next = NULL;
3698 edit_free_file_lines(cur_line);
3700 err = edit_write_file(file);
3703 /* Error writing file */
3704 edit_free_file(file);
3708 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3709 string_append(&target, file->identifier);
3711 edit_free_file(file);
3716 return JB_ERR_MEMORY;
3719 rsp->status = strdup("302 Local Redirect from Junkbuster");
3720 if (rsp->status == NULL)
3723 return JB_ERR_MEMORY;
3725 err = enlist_unique_header(rsp->headers, "Location", target);
3732 /*********************************************************************
3734 * Function : cgi_edit_actions_section_add
3736 * Description : CGI function that adds a new empty section to
3740 * 1 : csp = Current client state (buffers, headers, etc...)
3741 * 2 : rsp = http_response data structure for output
3742 * 3 : parameters = map of cgi parameters
3745 * f : (filename) Identifies the file to edit
3746 * v : (version) File's last-modified time
3747 * s : (section) Line number of section to add after, 0 for
3750 * Returns : JB_ERR_OK on success
3751 * JB_ERR_MEMORY on out-of-memory
3752 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3753 * specified or not valid.
3755 *********************************************************************/
3756 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3757 struct http_response *rsp,
3758 const struct map *parameters)
3761 struct file_line * new_line;
3763 struct editable_file * file;
3764 struct file_line * cur_line;
3765 unsigned line_number;
3769 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3771 return cgi_error_disabled(csp, rsp);
3774 err = get_number_param(csp, parameters, "s", §ionid);
3780 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3783 /* No filename specified, can't read file, modified, or out of memory. */
3784 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3788 cur_line = file->lines;
3792 /* Add to start of file */
3793 if (cur_line != NULL)
3795 /* There's something in the file, find the line before the first
3798 while ( (cur_line->next != NULL)
3799 && (cur_line->next->type != FILE_LINE_ACTION) )
3801 cur_line = cur_line->next;
3808 /* Add after stated section. */
3809 while ((cur_line != NULL) && (line_number < sectionid))
3811 cur_line = cur_line->next;
3815 if ( (cur_line == NULL)
3816 || (cur_line->type != FILE_LINE_ACTION))
3818 /* Invalid "sectionid" parameter */
3819 edit_free_file(file);
3820 return JB_ERR_CGI_PARAMS;
3823 /* Skip through the section to find the last line in it. */
3824 while ( (cur_line->next != NULL)
3825 && (cur_line->next->type != FILE_LINE_ACTION) )
3827 cur_line = cur_line->next;
3832 /* At this point, the last line in the previous section is in cur_line
3833 * - add after this. (Or if we need to add as the first line, cur_line
3837 new_text = strdup("{}");
3838 if (NULL == new_text)
3840 edit_free_file(file);
3841 return JB_ERR_MEMORY;
3844 /* Allocate the new line */
3845 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3846 if (new_line == NULL)
3849 edit_free_file(file);
3850 return JB_ERR_MEMORY;
3853 /* Fill in the data members of the new line */
3854 new_line->raw = NULL;
3855 new_line->prefix = NULL;
3856 new_line->unprocessed = new_text;
3857 new_line->type = FILE_LINE_ACTION;
3859 if (cur_line != NULL)
3861 /* Link new_line into the list, after cur_line */
3862 new_line->next = cur_line->next;
3863 cur_line->next = new_line;
3867 /* Link new_line into the list, as first line */
3868 new_line->next = file->lines;
3869 file->lines = new_line;
3872 /* Done making changes, now commit */
3874 err = edit_write_file(file);
3877 /* Error writing file */
3878 edit_free_file(file);
3882 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3883 string_append(&target, file->identifier);
3885 edit_free_file(file);
3890 return JB_ERR_MEMORY;
3893 rsp->status = strdup("302 Local Redirect from Junkbuster");
3894 if (rsp->status == NULL)
3897 return JB_ERR_MEMORY;
3899 err = enlist_unique_header(rsp->headers, "Location", target);
3906 /*********************************************************************
3908 * Function : cgi_edit_actions_section_swap
3910 * Description : CGI function that swaps the order of two sections
3911 * in the actions file. Note that this CGI can actually
3912 * swap any two arbitrary sections, but the GUI interface
3913 * currently only allows consecutive sections to be
3917 * 1 : csp = Current client state (buffers, headers, etc...)
3918 * 2 : rsp = http_response data structure for output
3919 * 3 : parameters = map of cgi parameters
3922 * f : (filename) Identifies the file to edit
3923 * v : (version) File's last-modified time
3924 * s1 : (section1) Line number of first section to swap
3925 * s2 : (section2) Line number of second section to swap
3927 * Returns : JB_ERR_OK on success
3928 * JB_ERR_MEMORY on out-of-memory
3929 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3930 * specified or not valid.
3932 *********************************************************************/
3933 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3934 struct http_response *rsp,
3935 const struct map *parameters)
3939 struct editable_file * file;
3940 struct file_line * cur_line;
3941 struct file_line * prev_line;
3942 struct file_line * line_before_section1;
3943 struct file_line * line_start_section1;
3944 struct file_line * line_end_section1;
3945 struct file_line * line_after_section1;
3946 struct file_line * line_before_section2;
3947 struct file_line * line_start_section2;
3948 struct file_line * line_end_section2;
3949 struct file_line * line_after_section2;
3950 unsigned line_number;
3954 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3956 return cgi_error_disabled(csp, rsp);
3959 err = get_number_param(csp, parameters, "s1", §ion1);
3960 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3966 if (section1 > section2)
3968 unsigned temp = section2;
3969 section2 = section1;
3973 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3976 /* No filename specified, can't read file, modified, or out of memory. */
3977 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3980 /* Start at the beginning... */
3982 cur_line = file->lines;
3985 /* ... find section1 ... */
3986 while ((cur_line != NULL) && (line_number < section1))
3988 prev_line = cur_line;
3989 cur_line = cur_line->next;
3993 if ( (cur_line == NULL)
3994 || (cur_line->type != FILE_LINE_ACTION) )
3996 /* Invalid "section1" parameter */
3997 edit_free_file(file);
3998 return JB_ERR_CGI_PARAMS;
4001 /* If no-op, we've validated params and can skip the rest. */
4002 if (section1 != section2)
4004 /* ... find the end of section1 ... */
4005 line_before_section1 = prev_line;
4006 line_start_section1 = cur_line;
4009 prev_line = cur_line;
4010 cur_line = cur_line->next;
4013 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4014 line_end_section1 = prev_line;
4015 line_after_section1 = cur_line;
4017 /* ... find section2 ... */
4018 while ((cur_line != NULL) && (line_number < section2))
4020 prev_line = cur_line;
4021 cur_line = cur_line->next;
4025 if ( (cur_line == NULL)
4026 || (cur_line->type != FILE_LINE_ACTION) )
4028 /* Invalid "section2" parameter */
4029 edit_free_file(file);
4030 return JB_ERR_CGI_PARAMS;
4033 /* ... find the end of section2 ... */
4034 line_before_section2 = prev_line;
4035 line_start_section2 = cur_line;
4038 prev_line = cur_line;
4039 cur_line = cur_line->next;
4042 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
4043 line_end_section2 = prev_line;
4044 line_after_section2 = cur_line;
4046 /* Now have all the pointers we need. Do the swap. */
4048 /* Change the pointer to section1 to point to section2 instead */
4049 if (line_before_section1 == NULL)
4051 file->lines = line_start_section2;
4055 line_before_section1->next = line_start_section2;
4058 if (line_before_section2 == line_end_section1)
4060 /* Consecutive sections */
4061 line_end_section2->next = line_start_section1;
4065 line_end_section2->next = line_after_section1;
4066 line_before_section2->next = line_start_section1;
4069 /* Set the pointer from the end of section1 to the rest of the file */
4070 line_end_section1->next = line_after_section2;
4072 err = edit_write_file(file);
4075 /* Error writing file */
4076 edit_free_file(file);
4079 } /* END if (section1 != section2) */
4081 target = strdup(CGI_PREFIX "edit-actions-list?f=");
4082 string_append(&target, file->identifier);
4084 edit_free_file(file);
4089 return JB_ERR_MEMORY;
4092 rsp->status = strdup("302 Local Redirect from Junkbuster");
4093 if (rsp->status == NULL)
4096 return JB_ERR_MEMORY;
4098 err = enlist_unique_header(rsp->headers, "Location", target);
4105 /*********************************************************************
4107 * Function : cgi_toggle
4109 * Description : CGI function that adds a new empty section to
4113 * 1 : csp = Current client state (buffers, headers, etc...)
4114 * 2 : rsp = http_response data structure for output
4115 * 3 : parameters = map of cgi parameters
4118 * set : If present, how to change toggle setting:
4119 * "enable", "disable", "toggle", or none (default).
4120 * mini : If present, use mini reply template.
4122 * Returns : JB_ERR_OK on success
4123 * JB_ERR_MEMORY on out-of-memory
4125 *********************************************************************/
4126 jb_err cgi_toggle(struct client_state *csp,
4127 struct http_response *rsp,
4128 const struct map *parameters)
4130 struct map *exports;
4132 const char *template_name;
4139 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
4141 return cgi_error_disabled(csp, rsp);
4144 if (NULL == (exports = default_exports(csp, "toggle")))
4146 return JB_ERR_MEMORY;
4149 mode = get_char_param(parameters, "set");
4156 else if (mode == 'd')
4161 else if (mode == 't')
4164 g_bToggleIJB = !g_bToggleIJB;
4167 err = map_conditional(exports, "enabled", g_bToggleIJB);
4174 template_name = (get_char_param(parameters, "mini")
4178 return template_fill_for_cgi(csp, template_name, exports, rsp);
4182 /*********************************************************************
4184 * Function : javascriptify
4186 * Description : Converts a string into a form JavaScript will like.
4188 * Netscape 4's JavaScript sucks - it doesn't use
4189 * "id" parameters, so you have to set the "name"
4190 * used to submit a form element to something JavaScript
4191 * will like. (Or access the elements by index in an
4192 * array. That array contains >60 elements and will
4193 * be changed whenever we add a new action to the
4194 * editor, so I'm NOT going to use indexes that have
4195 * to be figured out by hand.)
4197 * Currently the only thing we have to worry about
4198 * is "-" ==> "_" conversion.
4200 * This is a length-preserving operation so it is
4201 * carried out in-place, no memory is allocated
4205 * 1 : identifier = String to make JavaScript-friendly.
4209 *********************************************************************/
4210 static void javascriptify(char * identifier)
4212 char * p = identifier;
4213 while (NULL != (p = strchr(p, '-')))
4220 /*********************************************************************
4222 * Function : actions_to_radio
4224 * Description : Converts a actionsfile entry into settings for
4225 * radio buttons and edit boxes on a HTML form.
4228 * 1 : exports = List of substitutions to add to.
4229 * 2 : action = Action to read
4231 * Returns : JB_ERR_OK on success
4232 * JB_ERR_MEMORY on out-of-memory
4234 *********************************************************************/
4235 static jb_err actions_to_radio(struct map * exports,
4236 const struct action_spec *action)
4238 unsigned mask = action->mask;
4239 unsigned add = action->add;
4247 mask = action->mask;
4250 /* sanity - prevents "-feature +feature" */
4254 #define DEFINE_ACTION_BOOL(name, bit) \
4255 if (!(mask & bit)) \
4257 current_mode = 'n'; \
4259 else if (add & bit) \
4261 current_mode = 'y'; \
4265 current_mode = 'x'; \
4267 if (map_radio(exports, name, "ynx", current_mode)) \
4269 return JB_ERR_MEMORY; \
4272 #define DEFINE_ACTION_STRING(name, bit, index) \
4273 DEFINE_ACTION_BOOL(name, bit); \
4276 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4279 checked = !strcmp(action->string[index], value); \
4283 checked = is_default; \
4285 mapped_param |= checked; \
4286 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4288 return JB_ERR_MEMORY; \
4291 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4292 if (map(exports, name "-param-custom", 1, \
4293 ((!mapped_param) ? "checked" : ""), 1)) \
4295 return JB_ERR_MEMORY; \
4297 if (map(exports, name "-param", 1, \
4298 (((add & bit) && !mapped_param) ? \
4299 action->string[index] : default_val), 1)) \
4301 return JB_ERR_MEMORY; \
4304 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4305 if (map(exports, name "-param", 1, \
4306 ((add & bit) ? action->string[index] : default_val), 1)) \
4308 return JB_ERR_MEMORY; \
4311 #define DEFINE_ACTION_MULTI(name, index) \
4312 if (action->multi_add[index]->first) \
4314 current_mode = 'y'; \
4316 else if (action->multi_remove_all[index]) \
4318 current_mode = 'n'; \
4320 else if (action->multi_remove[index]->first) \
4322 current_mode = 'y'; \
4326 current_mode = 'x'; \
4328 if (map_radio(exports, name, "ynx", current_mode)) \
4330 return JB_ERR_MEMORY; \
4333 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4335 #include "actionlist.h"
4337 #undef DEFINE_ACTION_MULTI
4338 #undef DEFINE_ACTION_STRING
4339 #undef DEFINE_ACTION_BOOL
4340 #undef DEFINE_ACTION_ALIAS
4341 #undef DEFINE_CGI_PARAM_CUSTOM
4342 #undef DEFINE_CGI_PARAM_RADIO
4343 #undef DEFINE_CGI_PARAM_NO_RADIO
4349 /*********************************************************************
4351 * Function : actions_from_radio
4353 * Description : Converts a map of parameters passed to a CGI function
4354 * into an actionsfile entry.
4357 * 1 : parameters = parameters to the CGI call
4358 * 2 : action = Action to change. Must be valid before
4359 * the call, actions not specified will be
4362 * Returns : JB_ERR_OK on success
4363 * JB_ERR_MEMORY on out-of-memory
4365 *********************************************************************/
4366 static jb_err actions_from_radio(const struct map * parameters,
4367 struct action_spec *action)
4369 static int first_time = 1;
4373 const char * js_name;
4374 jb_err err = JB_ERR_OK;
4379 /* Statics are generally a potential race condition,
4380 * but in this case we're safe and don't need semaphores.
4381 * Be careful if you modify this function.
4385 #define JAVASCRIPTIFY(dest_var, string) \
4387 static char js_name_arr[] = string; \
4390 javascriptify(js_name_arr); \
4392 dest_var = js_name_arr; \
4395 #define DEFINE_ACTION_BOOL(name, bit) \
4396 JAVASCRIPTIFY(js_name, name); \
4397 ch = get_char_param(parameters, js_name); \
4400 action->add |= bit; \
4401 action->mask |= bit; \
4403 else if (ch == 'N') \
4405 action->add &= ~bit; \
4406 action->mask &= ~bit; \
4408 else if (ch == 'X') \
4410 action->add &= ~bit; \
4411 action->mask |= bit; \
4414 #define DEFINE_ACTION_STRING(name, bit, index) \
4415 JAVASCRIPTIFY(js_name, name); \
4416 ch = get_char_param(parameters, js_name); \
4420 JAVASCRIPTIFY(js_name, name "-mode"); \
4421 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4422 if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
4424 JAVASCRIPTIFY(js_name, name "-param"); \
4425 if (!err) err = get_string_param(parameters, js_name, ¶m); \
4427 if (param != NULL) \
4429 if (NULL == (param_dup = strdup(param))) \
4431 return JB_ERR_MEMORY; \
4433 freez(action->string[index]); \
4434 action->add |= bit; \
4435 action->mask |= bit; \
4436 action->string[index] = param_dup; \
4439 else if (ch == 'N') \
4441 if (action->add & bit) \
4443 freez(action->string[index]); \
4445 action->add &= ~bit; \
4446 action->mask &= ~bit; \
4448 else if (ch == 'X') \
4450 if (action->add & bit) \
4452 freez(action->string[index]); \
4454 action->add &= ~bit; \
4455 action->mask |= bit; \
4458 #define DEFINE_ACTION_MULTI(name, index) \
4459 JAVASCRIPTIFY(js_name, name); \
4460 ch = get_char_param(parameters, js_name); \
4465 else if (ch == 'N') \
4467 list_remove_all(action->multi_add[index]); \
4468 list_remove_all(action->multi_remove[index]); \
4469 action->multi_remove_all[index] = 1; \
4471 else if (ch == 'X') \
4473 list_remove_all(action->multi_add[index]); \
4474 list_remove_all(action->multi_remove[index]); \
4475 action->multi_remove_all[index] = 0; \
4478 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4480 #include "actionlist.h"
4482 #undef DEFINE_ACTION_MULTI
4483 #undef DEFINE_ACTION_STRING
4484 #undef DEFINE_ACTION_BOOL
4485 #undef DEFINE_ACTION_ALIAS
4486 #undef JAVASCRIPTIFY
4494 #endif /* def FEATURE_CGI_EDIT_ACTIONS */